blob: 106d3a85f77bf646bae221ba5c6305fd66c44216 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050061extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jeff Layton2de970f2010-10-06 19:51:12 -040063/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040064#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040065#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040066
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040067enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040068 /* Mount options that take no arguments */
69 Opt_user_xattr, Opt_nouser_xattr,
70 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040071 Opt_forcegid, Opt_noforcegid,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040072 Opt_noblocksend, Opt_noautotune,
73 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050074 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040075 Opt_mapchars, Opt_nomapchars, Opt_sfu,
76 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
Steve Frenchb3266142018-05-20 23:41:10 -050077 Opt_noposixpaths, Opt_nounix, Opt_unix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040078 Opt_nocase,
79 Opt_brl, Opt_nobrl,
Steve French3d4ef9a2018-04-25 22:19:09 -050080 Opt_handlecache, Opt_nohandlecache,
Steve French95932652016-09-23 01:36:34 -050081 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040082 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
83 Opt_nohard, Opt_nosoft,
84 Opt_nointr, Opt_intr,
85 Opt_nostrictsync, Opt_strictsync,
86 Opt_serverino, Opt_noserverino,
87 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
88 Opt_acl, Opt_noacl, Opt_locallease,
Jeff Layton1b359202012-09-19 15:20:27 -070089 Opt_sign, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040090 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040091 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050092 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060093 Opt_resilient, Opt_noresilient,
Long Li8339dd32017-11-07 01:54:55 -070094 Opt_domainauto, Opt_rdma,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040095
96 /* Mount options which take numeric value */
97 Opt_backupuid, Opt_backupgid, Opt_uid,
98 Opt_cruid, Opt_gid, Opt_file_mode,
99 Opt_dirmode, Opt_port,
100 Opt_rsize, Opt_wsize, Opt_actimeo,
Steve French141891f2016-09-23 00:44:16 -0500101 Opt_echo_interval, Opt_max_credits,
Steve French8b217fe2016-11-11 22:36:20 -0600102 Opt_snapshot,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400103
104 /* Mount options which take string value */
105 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400106 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400107 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400108 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400109
110 /* Mount options to be ignored */
111 Opt_ignore,
112
113 /* Options which could be blank */
114 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100115 Opt_blank_user,
116 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400117
118 Opt_err
119};
120
121static const match_table_t cifs_mount_option_tokens = {
122
123 { Opt_user_xattr, "user_xattr" },
124 { Opt_nouser_xattr, "nouser_xattr" },
125 { Opt_forceuid, "forceuid" },
126 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400127 { Opt_forcegid, "forcegid" },
128 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400129 { Opt_noblocksend, "noblocksend" },
130 { Opt_noautotune, "noautotune" },
131 { Opt_hard, "hard" },
132 { Opt_soft, "soft" },
133 { Opt_perm, "perm" },
134 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500135 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400136 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500137 { Opt_mapposix, "mapposix" }, /* SFM style */
138 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400139 { Opt_sfu, "sfu" },
140 { Opt_nosfu, "nosfu" },
141 { Opt_nodfs, "nodfs" },
142 { Opt_posixpaths, "posixpaths" },
143 { Opt_noposixpaths, "noposixpaths" },
144 { Opt_nounix, "nounix" },
145 { Opt_nounix, "nolinux" },
Steve Frenchb3266142018-05-20 23:41:10 -0500146 { Opt_nounix, "noposix" },
147 { Opt_unix, "unix" },
148 { Opt_unix, "linux" },
149 { Opt_unix, "posix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400150 { Opt_nocase, "nocase" },
151 { Opt_nocase, "ignorecase" },
152 { Opt_brl, "brl" },
153 { Opt_nobrl, "nobrl" },
Steve French3d4ef9a2018-04-25 22:19:09 -0500154 { Opt_handlecache, "handlecache" },
155 { Opt_nohandlecache, "nohandlecache" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400156 { Opt_nobrl, "nolock" },
157 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400158 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400159 { Opt_setuids, "setuids" },
160 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500161 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400162 { Opt_dynperm, "dynperm" },
163 { Opt_nodynperm, "nodynperm" },
164 { Opt_nohard, "nohard" },
165 { Opt_nosoft, "nosoft" },
166 { Opt_nointr, "nointr" },
167 { Opt_intr, "intr" },
168 { Opt_nostrictsync, "nostrictsync" },
169 { Opt_strictsync, "strictsync" },
170 { Opt_serverino, "serverino" },
171 { Opt_noserverino, "noserverino" },
172 { Opt_rwpidforward, "rwpidforward" },
173 { Opt_cifsacl, "cifsacl" },
174 { Opt_nocifsacl, "nocifsacl" },
175 { Opt_acl, "acl" },
176 { Opt_noacl, "noacl" },
177 { Opt_locallease, "locallease" },
178 { Opt_sign, "sign" },
179 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400180 { Opt_noac, "noac" },
181 { Opt_fsc, "fsc" },
182 { Opt_mfsymlinks, "mfsymlinks" },
183 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400184 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400185 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500186 { Opt_persistent, "persistenthandles"},
187 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600188 { Opt_resilient, "resilienthandles"},
189 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530190 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700191 { Opt_rdma, "rdma"},
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400192
193 { Opt_backupuid, "backupuid=%s" },
194 { Opt_backupgid, "backupgid=%s" },
195 { Opt_uid, "uid=%s" },
196 { Opt_cruid, "cruid=%s" },
197 { Opt_gid, "gid=%s" },
198 { Opt_file_mode, "file_mode=%s" },
199 { Opt_dirmode, "dirmode=%s" },
200 { Opt_dirmode, "dir_mode=%s" },
201 { Opt_port, "port=%s" },
202 { Opt_rsize, "rsize=%s" },
203 { Opt_wsize, "wsize=%s" },
204 { Opt_actimeo, "actimeo=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600205 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500206 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600207 { Opt_snapshot, "snapshot=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400208
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100209 { Opt_blank_user, "user=" },
210 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400211 { Opt_user, "user=%s" },
212 { Opt_user, "username=%s" },
213 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100214 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400215 { Opt_pass, "pass=%s" },
216 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100217 { Opt_blank_ip, "ip=" },
218 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400219 { Opt_ip, "ip=%s" },
220 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400221 { Opt_ignore, "unc=%s" },
222 { Opt_ignore, "target=%s" },
223 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400224 { Opt_domain, "dom=%s" },
225 { Opt_domain, "domain=%s" },
226 { Opt_domain, "workgroup=%s" },
227 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400228 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400229 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400230 { Opt_netbiosname, "netbiosname=%s" },
231 { Opt_servern, "servern=%s" },
232 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400233 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400234 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400235 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400236
237 { Opt_ignore, "cred" },
238 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400239 { Opt_ignore, "cred=%s" },
240 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400241 { Opt_ignore, "guest" },
242 { Opt_ignore, "rw" },
243 { Opt_ignore, "ro" },
244 { Opt_ignore, "suid" },
245 { Opt_ignore, "nosuid" },
246 { Opt_ignore, "exec" },
247 { Opt_ignore, "noexec" },
248 { Opt_ignore, "nodev" },
249 { Opt_ignore, "noauto" },
250 { Opt_ignore, "dev" },
251 { Opt_ignore, "mand" },
252 { Opt_ignore, "nomand" },
253 { Opt_ignore, "_netdev" },
254
255 { Opt_err, NULL }
256};
257
258enum {
259 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
260 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400261 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
262 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400263 Opt_sec_none,
264
265 Opt_sec_err
266};
267
268static const match_table_t cifs_secflavor_tokens = {
269 { Opt_sec_krb5, "krb5" },
270 { Opt_sec_krb5i, "krb5i" },
271 { Opt_sec_krb5p, "krb5p" },
272 { Opt_sec_ntlmsspi, "ntlmsspi" },
273 { Opt_sec_ntlmssp, "ntlmssp" },
274 { Opt_ntlm, "ntlm" },
275 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400276 { Opt_sec_ntlmv2, "nontlm" },
277 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400278 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400279 { Opt_sec_lanman, "lanman" },
280 { Opt_sec_none, "none" },
281
282 { Opt_sec_err, NULL }
283};
284
Jeff Layton15b6a472012-05-16 07:50:15 -0400285/* cache flavors */
286enum {
287 Opt_cache_loose,
288 Opt_cache_strict,
289 Opt_cache_none,
290 Opt_cache_err
291};
292
293static const match_table_t cifs_cacheflavor_tokens = {
294 { Opt_cache_loose, "loose" },
295 { Opt_cache_strict, "strict" },
296 { Opt_cache_none, "none" },
297 { Opt_cache_err, NULL }
298};
299
Jeff Layton23db65f2012-05-15 12:20:51 -0400300static const match_table_t cifs_smb_version_tokens = {
301 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600302 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000303 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500304 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500305 { Smb_302, SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600306 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500307 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French9764c022017-09-17 10:41:35 -0500308 { Smb_3any, SMB3ANY_VERSION_STRING },
309 { Smb_default, SMBDEFAULT_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600310 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400311};
312
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300313static int ip_connect(struct TCP_Server_Info *server);
314static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400315static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400316static void cifs_prune_tlinks(struct work_struct *work);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -0400317static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -0500318 const char *devname, bool is_smb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Jeff Laytond5c56052008-12-01 18:42:33 -0500320/*
321 * cifs tcp session reconnection
322 *
323 * mark tcp session as reconnecting so temporarily locked
324 * mark all smb sessions as reconnecting for tcp session
325 * reconnect tcp session
326 * wake up waiters on reconnection? - (not needed currently)
327 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400328int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329cifs_reconnect(struct TCP_Server_Info *server)
330{
331 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500332 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000333 struct cifs_ses *ses;
334 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000335 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400336 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000339 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000340 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 next time through the loop */
342 spin_unlock(&GlobalMid_Lock);
343 return rc;
344 } else
345 server->tcpStatus = CifsNeedReconnect;
346 spin_unlock(&GlobalMid_Lock);
347 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400348 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Joe Perchesf96637b2013-05-04 22:12:25 -0500350 cifs_dbg(FYI, "Reconnecting tcp session\n");
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500351 trace_smb3_reconnect(server->CurrentMid, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* before reconnecting the tcp session, mark the smb session (uid)
354 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500355 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
356 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530357 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500358 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000359 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500360 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500361 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000362 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500363 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100365 if (ses->tcon_ipc)
366 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530368 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500371 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500372 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000373 if (server->ssocket) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500374 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
375 server->ssocket->state, server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800376 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesf96637b2013-05-04 22:12:25 -0500377 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
378 server->ssocket->state, server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 sock_release(server->ssocket);
380 server->ssocket = NULL;
381 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500382 server->sequence_number = 0;
383 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500384 kfree(server->session_key.response);
385 server->session_key.response = NULL;
386 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000387 server->lstrp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500389 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400390 INIT_LIST_HEAD(&retry_list);
Joe Perchesf96637b2013-05-04 22:12:25 -0500391 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500393 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
394 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400395 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
396 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400397 list_move(&mid_entry->qhead, &retry_list);
398 }
399 spin_unlock(&GlobalMid_Lock);
Rabin Vincent820962d2015-12-23 07:32:41 +0100400 mutex_unlock(&server->srv_mutex);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400401
Joe Perchesf96637b2013-05-04 22:12:25 -0500402 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400403 list_for_each_safe(tmp, tmp2, &retry_list) {
404 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500405 list_del_init(&mid_entry->qhead);
406 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400409 do {
Steve French6c3d8902006-07-31 22:46:20 +0000410 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300411
412 /* we should try only the port we connected to before */
Jeff Layton73e216a2013-09-05 08:38:10 -0400413 mutex_lock(&server->srv_mutex);
Long Li781a8052017-11-22 17:38:36 -0700414 if (cifs_rdma_enabled(server))
415 rc = smbd_reconnect(server);
416 else
417 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000418 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500419 cifs_dbg(FYI, "reconnect error %d\n", rc);
Federico Sauter4afe2602015-03-17 17:45:28 +0100420 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700421 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } else {
423 atomic_inc(&tcpSesReconnectCount);
424 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000425 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000426 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000427 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100428 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400430 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500431
Sachin Prabhub8c60012016-10-20 19:52:24 -0400432 if (server->tcpStatus == CifsNeedNegotiate)
433 mod_delayed_work(cifsiod_wq, &server->echo, 0);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return rc;
436}
437
Jeff Laytonc74093b2011-01-11 07:24:23 -0500438static void
439cifs_echo_request(struct work_struct *work)
440{
441 int rc;
442 struct TCP_Server_Info *server = container_of(work,
443 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400444 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500445
Jeff Layton247ec9b2011-02-04 17:09:50 -0500446 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400447 * If we need to renegotiate, set echo interval to zero to
448 * immediately call echo service where we can renegotiate.
449 */
450 if (server->tcpStatus == CifsNeedNegotiate)
451 echo_interval = 0;
452 else
453 echo_interval = server->echo_interval;
454
455 /*
456 * We cannot send an echo if it is disabled.
457 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500458 */
Steve French4fcd1812016-06-22 20:12:05 -0500459
460 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400461 server->tcpStatus == CifsExiting ||
462 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400463 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600464 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500465 goto requeue_echo;
466
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400467 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500468 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500469 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
470 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500471
472requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400473 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500474}
475
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400476static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400477allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400478{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400479 if (!server->bigbuf) {
480 server->bigbuf = (char *)cifs_buf_get();
481 if (!server->bigbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500482 cifs_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400483 msleep(3000);
484 /* retry will check if exiting */
485 return false;
486 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400487 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400488 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400489 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400490 }
491
Jeff Layton2a37ef92011-10-19 15:29:23 -0400492 if (!server->smallbuf) {
493 server->smallbuf = (char *)cifs_small_buf_get();
494 if (!server->smallbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500495 cifs_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400496 msleep(1000);
497 /* retry will check if exiting */
498 return false;
499 }
500 /* beginning of smb buffer is cleared in our buf_get */
501 } else {
502 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400503 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400504 }
505
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400506 return true;
507}
508
Jeff Laytonba749e62011-10-11 06:41:32 -0400509static bool
510server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400511{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300512 /*
513 * We need to wait 2 echo intervals to make sure we handle such
514 * situations right:
515 * 1s client sends a normal SMB request
516 * 2s client gets a response
517 * 30s echo workqueue job pops, and decides we got a response recently
518 * and don't need to send another
519 * ...
520 * 65s kernel_recvmsg times out, and we see that we haven't gotten
521 * a response in >60s.
522 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200523 if ((server->tcpStatus == CifsGood ||
524 server->tcpStatus == CifsNeedNegotiate) &&
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600525 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
526 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
527 server->hostname, (2 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400528 cifs_reconnect(server);
529 wake_up(&server->response_q);
530 return true;
531 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400532
Jeff Laytonba749e62011-10-11 06:41:32 -0400533 return false;
534}
535
Al Viro71335662016-01-09 19:54:50 -0500536static int
537cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400538{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400539 int length = 0;
540 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400541
Al Viro71335662016-01-09 19:54:50 -0500542 smb_msg->msg_control = NULL;
543 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400544
Al Viro71335662016-01-09 19:54:50 -0500545 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500546 try_to_freeze();
547
Al Viro71335662016-01-09 19:54:50 -0500548 if (server_unresponsive(server))
549 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700550 if (cifs_rdma_enabled(server) && server->smbd_conn)
551 length = smbd_recv(server->smbd_conn, smb_msg);
552 else
553 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500554
555 if (server->tcpStatus == CifsExiting)
556 return -ESHUTDOWN;
557
558 if (server->tcpStatus == CifsNeedReconnect) {
559 cifs_reconnect(server);
560 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400561 }
562
Al Viro71335662016-01-09 19:54:50 -0500563 if (length == -ERESTARTSYS ||
564 length == -EAGAIN ||
565 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400566 /*
567 * Minimum sleep to prevent looping, allowing socket
568 * to clear and app threads to set tcpStatus
569 * CifsNeedReconnect if server hung.
570 */
571 usleep_range(1000, 2000);
572 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400573 continue;
Al Viro71335662016-01-09 19:54:50 -0500574 }
575
576 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500577 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400578 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500579 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400580 }
581 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400582 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400583}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400584
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400585int
586cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
587 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400588{
Al Viro71335662016-01-09 19:54:50 -0500589 struct msghdr smb_msg;
590 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
591 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400592
Al Viro71335662016-01-09 19:54:50 -0500593 return cifs_readv_from_socket(server, &smb_msg);
594}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400595
Al Viro71335662016-01-09 19:54:50 -0500596int
597cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700598 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500599{
600 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700601 struct bio_vec bv = {
602 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
Al Viro71335662016-01-09 19:54:50 -0500603 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
604 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400605}
606
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400607static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400608is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400609{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400610 /*
611 * The first byte big endian of the length field,
612 * is actually not part of the length but the type
613 * with the most common, zero, as regular data.
614 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400615 switch (type) {
616 case RFC1002_SESSION_MESSAGE:
617 /* Regular SMB response */
618 return true;
619 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500620 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400621 break;
622 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500623 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400624 break;
625 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400626 /*
627 * We get this from Windows 98 instead of an error on
628 * SMB negprot response.
629 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500630 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400631 /* give server a second to clean up */
632 msleep(1000);
633 /*
634 * Always try 445 first on reconnect since we get NACK
635 * on some if we ever connected to port 139 (the NACK
636 * is since we do not begin with RFC1001 session
637 * initialize frame).
638 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400639 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400640 cifs_reconnect(server);
641 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400642 break;
643 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500644 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400645 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400646 }
647
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400648 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400649}
650
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400651void
652dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400653{
654#ifdef CONFIG_CIFS_STATS2
655 mid->when_received = jiffies;
656#endif
657 spin_lock(&GlobalMid_Lock);
658 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400659 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400660 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400661 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000662 /*
663 * Trying to handle/dequeue a mid after the send_recv()
664 * function has finished processing it is a bug.
665 */
666 if (mid->mid_flags & MID_DELETED)
667 printk_once(KERN_WARNING
668 "trying to dequeue a deleted mid\n");
669 else
670 list_del_init(&mid->qhead);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400671 spin_unlock(&GlobalMid_Lock);
672}
673
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400674static void
675handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400676 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400677{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400678 if (server->ops->check_trans2 &&
679 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400680 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400681 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400682 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400683 /* Was previous buf put in mpx struct for multi-rsp? */
684 if (!mid->multiRsp) {
685 /* smb buffer will be freed by user thread */
686 if (server->large_buf)
687 server->bigbuf = NULL;
688 else
689 server->smallbuf = NULL;
690 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400691 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400692}
693
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400694static void clean_demultiplex_info(struct TCP_Server_Info *server)
695{
696 int length;
697
698 /* take it off the list, if it's not already */
699 spin_lock(&cifs_tcp_ses_lock);
700 list_del_init(&server->tcp_ses_list);
701 spin_unlock(&cifs_tcp_ses_lock);
702
703 spin_lock(&GlobalMid_Lock);
704 server->tcpStatus = CifsExiting;
705 spin_unlock(&GlobalMid_Lock);
706 wake_up_all(&server->response_q);
707
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400708 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300709 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400710 if (server->credits <= 0)
711 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300712 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400713 /*
714 * Although there should not be any requests blocked on this queue it
715 * can not hurt to be paranoid and try to wake up requests that may
716 * haven been blocked when more than 50 at time were on the wire to the
717 * same server - they now will see the session is in exit state and get
718 * out of SendReceive.
719 */
720 wake_up_all(&server->request_q);
721 /* give those requests time to exit */
722 msleep(125);
Long Libce9ce72017-11-22 17:38:38 -0700723 if (cifs_rdma_enabled(server) && server->smbd_conn) {
724 smbd_destroy(server->smbd_conn);
725 server->smbd_conn = NULL;
726 }
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400727 if (server->ssocket) {
728 sock_release(server->ssocket);
729 server->ssocket = NULL;
730 }
731
732 if (!list_empty(&server->pending_mid_q)) {
733 struct list_head dispose_list;
734 struct mid_q_entry *mid_entry;
735 struct list_head *tmp, *tmp2;
736
737 INIT_LIST_HEAD(&dispose_list);
738 spin_lock(&GlobalMid_Lock);
739 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
740 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500741 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400742 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400743 list_move(&mid_entry->qhead, &dispose_list);
744 }
745 spin_unlock(&GlobalMid_Lock);
746
747 /* now walk dispose list and issue callbacks */
748 list_for_each_safe(tmp, tmp2, &dispose_list) {
749 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500750 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400751 list_del_init(&mid_entry->qhead);
752 mid_entry->callback(mid_entry);
753 }
754 /* 1/8th of sec is more than enough time for them to exit */
755 msleep(125);
756 }
757
758 if (!list_empty(&server->pending_mid_q)) {
759 /*
760 * mpx threads have not exited yet give them at least the smb
761 * send timeout time for long ops.
762 *
763 * Due to delays on oplock break requests, we need to wait at
764 * least 45 seconds before giving up on a request getting a
765 * response and going ahead and killing cifsd.
766 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500767 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400768 msleep(46000);
769 /*
770 * If threads still have not exited they are probably never
771 * coming home not much else we can do but free the memory.
772 */
773 }
774
775 kfree(server->hostname);
776 kfree(server);
777
778 length = atomic_dec_return(&tcpSesAllocCount);
779 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700780 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400781}
782
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400783static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400784standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
785{
786 int length;
787 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000788 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400789
790 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100791 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
792 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500793 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400794 cifs_reconnect(server);
795 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400796 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400797 }
798
799 /* switch to large buffer if too big for a small one */
800 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
801 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400802 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400803 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400804 }
805
806 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400807 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100808 pdu_length - HEADER_SIZE(server) + 1
809 + server->vals->header_preamble_size);
810
Jeff Laytone9097ab2011-10-19 15:29:40 -0400811 if (length < 0)
812 return length;
813 server->total_read += length;
814
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400815 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400816
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800817 return cifs_handle_standard(server, mid);
818}
819
820int
821cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
822{
823 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
824 int length;
825
Jeff Laytone9097ab2011-10-19 15:29:40 -0400826 /*
827 * We know that we received enough to get to the MID as we
828 * checked the pdu_length earlier. Now check to see
829 * if the rest of the header is OK. We borrow the length
830 * var for the rest of the loop to avoid a new stack var.
831 *
832 * 48 bytes is enough to display the header and a little bit
833 * into the payload for debugging purposes.
834 */
Steve French373512e2015-12-18 13:05:30 -0600835 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400836 if (length != 0)
837 cifs_dump_mem("Bad SMB: ", buf,
838 min_t(unsigned int, server->total_read, 48));
839
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700840 if (server->ops->is_session_expired &&
841 server->ops->is_session_expired(buf)) {
842 cifs_reconnect(server);
843 wake_up(&server->response_q);
844 return -1;
845 }
846
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700847 if (server->ops->is_status_pending &&
848 server->ops->is_status_pending(buf, server, length))
849 return -1;
850
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500851 if (!mid)
852 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400853
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400854 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500855 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400856}
857
858static int
Al Viro7c97c202011-06-21 08:51:28 -0400859cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000861 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400862 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400863 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000864 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400865 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500866 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000867 struct mid_q_entry *mids[MAX_COMPOUND];
868 char *bufs[MAX_COMPOUND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500871 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400872
873 length = atomic_inc_return(&tcpSesAllocCount);
874 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700875 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700877 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000878 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700879 if (try_to_freeze())
880 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700881
Jeff Layton2a37ef92011-10-19 15:29:23 -0400882 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400883 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700884
Jeff Layton2a37ef92011-10-19 15:29:23 -0400885 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400886 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000887 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000888
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400889 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400890 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000891 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000892
893 if (server->vals->header_preamble_size == 0)
894 server->total_read = 0;
895 else
896 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700897
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400898 /*
899 * The right amount was read from socket - 4 bytes,
900 * so we can now interpret the length field.
901 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400902 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700903
Joe Perchesf96637b2013-05-04 22:12:25 -0500904 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400905 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000906 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000907next_pdu:
908 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700909
Jeff Layton89482a52011-10-19 15:28:57 -0400910 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000911 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100912 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500913 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000914 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400915 cifs_reconnect(server);
916 wake_up(&server->response_q);
917 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700918 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400919
Jeff Layton89482a52011-10-19 15:28:57 -0400920 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100921 length = cifs_read_from_socket(server,
922 buf + server->vals->header_preamble_size,
923 HEADER_SIZE(server) - 1
924 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400925 if (length < 0)
926 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400927 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400928
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000929 if (server->ops->next_header) {
930 next_offset = server->ops->next_header(buf);
931 if (next_offset)
932 server->pdu_size = next_offset;
933 }
934
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000935 memset(mids, 0, sizeof(mids));
936 memset(bufs, 0, sizeof(bufs));
937 num_mids = 0;
938
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800939 if (server->ops->is_transform_hdr &&
940 server->ops->receive_transform &&
941 server->ops->is_transform_hdr(buf)) {
942 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000943 mids,
944 bufs,
945 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800946 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000947 mids[0] = server->ops->find_mid(server, buf);
948 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -0500949 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400950
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000951 if (!mids[0] || !mids[0]->receive)
952 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800953 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000954 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800955 }
Jeff Layton44d22d82011-10-19 15:29:49 -0400956
Lars Persson696e4202018-06-25 14:05:25 +0200957 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000958 for (i = 0; i < num_mids; i++)
959 if (mids[i])
960 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -0700961 continue;
Lars Persson696e4202018-06-25 14:05:25 +0200962 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700963
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400964 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -0400965 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700966
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000967
Steve Frenchfda35942011-01-20 18:06:34 +0000968 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000969
970 for (i = 0; i < num_mids; i++) {
971 if (mids[i] != NULL) {
972 mids[i]->resp_buf_size = server->pdu_size;
973 if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
974 mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
975 server->ops->handle_cancelled_mid)
976 server->ops->handle_cancelled_mid(
977 mids[i]->resp_buf,
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800978 server);
979
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000980 if (!mids[i]->multiRsp || mids[i]->multiEnd)
981 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +0200982
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000983 cifs_mid_q_entry_release(mids[i]);
984 } else if (server->ops->is_oplock_break &&
985 server->ops->is_oplock_break(bufs[i],
986 server)) {
987 cifs_dbg(FYI, "Received oplock break\n");
988 } else {
989 cifs_dbg(VFS, "No task to wake, unknown frame "
990 "received! NumMids %d\n",
991 atomic_read(&midCount));
992 cifs_dump_mem("Received Data is: ", bufs[i],
993 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +0000994#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000995 if (server->ops->dump_detail)
996 server->ops->dump_detail(bufs[i],
997 server);
998 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +0000999#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001000 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001001 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001002
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001003 if (pdu_length > server->pdu_size) {
1004 if (!allocate_buffers(server))
1005 continue;
1006 pdu_length -= server->pdu_size;
1007 server->total_read = 0;
1008 server->large_buf = false;
1009 buf = server->smallbuf;
1010 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001011 }
1012 } /* end while !EXITING */
1013
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001014 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001015 cifs_buf_release(server->bigbuf);
1016 if (server->smallbuf) /* no sense logging a debug message if NULL */
1017 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001019 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001020 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001021
1022 /* if server->tsk was NULL then wait for a signal before exiting */
1023 if (!task_to_wake) {
1024 set_current_state(TASK_INTERRUPTIBLE);
1025 while (!signal_pending(current)) {
1026 schedule();
1027 set_current_state(TASK_INTERRUPTIBLE);
1028 }
1029 set_current_state(TASK_RUNNING);
1030 }
1031
Jeff Layton0468a2c2008-12-01 07:09:35 -05001032 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Jeff Laytonc359cf32007-11-16 22:22:06 +00001035/* extract the host portion of the UNC string */
1036static char *
1037extract_hostname(const char *unc)
1038{
1039 const char *src;
1040 char *dst, *delim;
1041 unsigned int len;
1042
1043 /* skip double chars at beginning of string */
1044 /* BB: check validity of these bytes? */
1045 src = unc + 2;
1046
1047 /* delimiter between hostname and sharename is always '\\' now */
1048 delim = strchr(src, '\\');
1049 if (!delim)
1050 return ERR_PTR(-EINVAL);
1051
1052 len = delim - src;
1053 dst = kmalloc((len + 1), GFP_KERNEL);
1054 if (dst == NULL)
1055 return ERR_PTR(-ENOMEM);
1056
1057 memcpy(dst, src, len);
1058 dst[len] = '\0';
1059
1060 return dst;
1061}
1062
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001063static int get_option_ul(substring_t args[], unsigned long *option)
1064{
1065 int rc;
1066 char *string;
1067
1068 string = match_strdup(args);
1069 if (string == NULL)
1070 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001071 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001072 kfree(string);
1073
1074 return rc;
1075}
1076
Eric W. Biederman3da46562013-02-06 01:37:39 -08001077static int get_option_uid(substring_t args[], kuid_t *result)
1078{
1079 unsigned long value;
1080 kuid_t uid;
1081 int rc;
1082
1083 rc = get_option_ul(args, &value);
1084 if (rc)
1085 return rc;
1086
1087 uid = make_kuid(current_user_ns(), value);
1088 if (!uid_valid(uid))
1089 return -EINVAL;
1090
1091 *result = uid;
1092 return 0;
1093}
1094
1095static int get_option_gid(substring_t args[], kgid_t *result)
1096{
1097 unsigned long value;
1098 kgid_t gid;
1099 int rc;
1100
1101 rc = get_option_ul(args, &value);
1102 if (rc)
1103 return rc;
1104
1105 gid = make_kgid(current_user_ns(), value);
1106 if (!gid_valid(gid))
1107 return -EINVAL;
1108
1109 *result = gid;
1110 return 0;
1111}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001112
1113static int cifs_parse_security_flavors(char *value,
1114 struct smb_vol *vol)
1115{
1116
1117 substring_t args[MAX_OPT_ARGS];
1118
Jeff Layton1e3cc572013-06-10 17:12:23 -05001119 /*
1120 * With mount options, the last one should win. Reset any existing
1121 * settings back to default.
1122 */
1123 vol->sectype = Unspecified;
1124 vol->sign = false;
1125
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001126 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001127 case Opt_sec_krb5p:
1128 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1129 return 1;
1130 case Opt_sec_krb5i:
1131 vol->sign = true;
1132 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001133 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001134 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001135 break;
1136 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001137 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001138 /* Fallthrough */
1139 case Opt_sec_ntlmssp:
1140 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001141 break;
1142 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001143 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001144 /* Fallthrough */
1145 case Opt_ntlm:
1146 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001147 break;
1148 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001149 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001150 /* Fallthrough */
1151 case Opt_sec_ntlmv2:
1152 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001153 break;
1154#ifdef CONFIG_CIFS_WEAK_PW_HASH
1155 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001156 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001157 break;
1158#endif
1159 case Opt_sec_none:
1160 vol->nullauth = 1;
1161 break;
1162 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001163 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001164 return 1;
1165 }
1166
1167 return 0;
1168}
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001171cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1172{
1173 substring_t args[MAX_OPT_ARGS];
1174
1175 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1176 case Opt_cache_loose:
1177 vol->direct_io = false;
1178 vol->strict_io = false;
1179 break;
1180 case Opt_cache_strict:
1181 vol->direct_io = false;
1182 vol->strict_io = true;
1183 break;
1184 case Opt_cache_none:
1185 vol->direct_io = true;
1186 vol->strict_io = false;
1187 break;
1188 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001189 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001190 return 1;
1191 }
1192 return 0;
1193}
1194
1195static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001196cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001197{
1198 substring_t args[MAX_OPT_ARGS];
1199
1200 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001201#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001202 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001203 if (disable_legacy_dialects) {
1204 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1205 return 1;
1206 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001207 if (is_smb3) {
1208 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1209 return 1;
1210 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001211 vol->ops = &smb1_operations;
1212 vol->vals = &smb1_values;
1213 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001214 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001215 if (disable_legacy_dialects) {
1216 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1217 return 1;
1218 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001219 if (is_smb3) {
1220 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1221 return 1;
1222 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001223 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001224 vol->vals = &smb20_values;
1225 break;
Steve French74204512018-06-19 14:34:08 -05001226#else
1227 case Smb_1:
1228 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1229 return 1;
1230 case Smb_20:
1231 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1232 return 1;
1233#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001234 case Smb_21:
1235 vol->ops = &smb21_operations;
1236 vol->vals = &smb21_values;
1237 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001238 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001239 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001240 vol->vals = &smb30_values;
1241 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001242 case Smb_302:
1243 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1244 vol->vals = &smb302_values;
1245 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001246 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001247 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001248 vol->vals = &smb311_values;
1249 break;
Steve French9764c022017-09-17 10:41:35 -05001250 case Smb_3any:
1251 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1252 vol->vals = &smb3any_values;
1253 break;
1254 case Smb_default:
1255 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1256 vol->vals = &smbdefault_values;
1257 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001258 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001259 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001260 return 1;
1261 }
1262 return 0;
1263}
1264
Jeff Laytond387a5c2012-12-10 06:10:46 -05001265/*
1266 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1267 * fields with the result. Returns 0 on success and an error otherwise.
1268 */
1269static int
1270cifs_parse_devname(const char *devname, struct smb_vol *vol)
1271{
1272 char *pos;
1273 const char *delims = "/\\";
1274 size_t len;
1275
1276 /* make sure we have a valid UNC double delimiter prefix */
1277 len = strspn(devname, delims);
1278 if (len != 2)
1279 return -EINVAL;
1280
1281 /* find delimiter between host and sharename */
1282 pos = strpbrk(devname + 2, delims);
1283 if (!pos)
1284 return -EINVAL;
1285
1286 /* skip past delimiter */
1287 ++pos;
1288
1289 /* now go until next delimiter or end of string */
1290 len = strcspn(pos, delims);
1291
1292 /* move "pos" up to delimiter or NULL */
1293 pos += len;
1294 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1295 if (!vol->UNC)
1296 return -ENOMEM;
1297
1298 convert_delimiter(vol->UNC, '\\');
1299
Sachin Prabhu11e31642016-02-08 13:44:01 +05301300 /* skip any delimiter */
1301 if (*pos == '/' || *pos == '\\')
1302 pos++;
1303
1304 /* If pos is NULL then no prepath */
1305 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001306 return 0;
1307
1308 vol->prepath = kstrdup(pos, GFP_KERNEL);
1309 if (!vol->prepath)
1310 return -ENOMEM;
1311
1312 return 0;
1313}
1314
Jeff Layton23db65f2012-05-15 12:20:51 -04001315static int
Sean Finneyb9468452011-04-11 13:19:32 +00001316cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001317 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001319 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001320 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 unsigned int temp_len, i, j;
1322 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001323 short int override_uid = -1;
1324 short int override_gid = -1;
1325 bool uid_specified = false;
1326 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001327 bool sloppy = false;
1328 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001329 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001330 char *string = NULL;
1331 char *tmp_end, *value;
1332 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001333 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001334 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001335 unsigned short port = 0;
1336 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001339 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001340 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Jeff Layton6ee95422012-11-26 11:09:57 -05001342 /* ensure we always start with zeroed-out smb_vol */
1343 memset(vol, 0, sizeof(*vol));
1344
Jeff Layton88463992010-11-22 15:31:03 -05001345 /*
1346 * does not have to be perfect mapping since field is
1347 * informational, only used for servers that do not support
1348 * port 445 and it can be overridden at mount time
1349 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001350 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1351 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001352 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1353
Jeff Layton1397f2e2011-01-07 11:30:28 -05001354 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001355 /* null target name indicates to use *SMBSERVR default called name
1356 if we end up sending RFC1001 session initialize */
1357 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001358 vol->cred_uid = current_uid();
1359 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001360 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001361
Steve French2baa2682014-09-27 02:19:01 -05001362 /*
1363 * default to SFM style remapping of seven reserved characters
1364 * unless user overrides it or we negotiate CIFS POSIX where
1365 * it is unnecessary. Can not simultaneously use more than one mapping
1366 * since then readdir could list files that open could not open
1367 */
1368 vol->remap = true;
1369
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001370 /* default to only allowing write access to owner of the mount */
1371 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001374 /* default is always to request posix paths. */
1375 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001376 /* default to using server inode numbers where available */
1377 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001378
Jeff Layton1b359202012-09-19 15:20:27 -07001379 /* default is to use strict cifs caching semantics */
1380 vol->strict_io = true;
1381
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301382 vol->actimeo = CIFS_DEF_ACTIMEO;
1383
Steve French9764c022017-09-17 10:41:35 -05001384 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1385 vol->ops = &smb30_operations;
1386 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001387
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001388 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1389
Sean Finneyb9468452011-04-11 13:19:32 +00001390 if (!mountdata)
1391 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Sean Finneyb9468452011-04-11 13:19:32 +00001393 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1394 if (!mountdata_copy)
1395 goto cifs_parse_mount_err;
1396
1397 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001398 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001399
Steve French50c2f752007-07-13 00:33:32 +00001400 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001401 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 separator[0] = options[4];
1403 options += 5;
1404 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001405 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001408 vol->backupuid_specified = false; /* no backup intent for a user */
1409 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001410
Jeff Layton37d4f992013-05-24 07:40:05 -04001411 switch (cifs_parse_devname(devname, vol)) {
1412 case 0:
1413 break;
1414 case -ENOMEM:
1415 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1416 goto cifs_parse_mount_err;
1417 case -EINVAL:
1418 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1419 goto cifs_parse_mount_err;
1420 default:
1421 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1422 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001426 substring_t args[MAX_OPT_ARGS];
1427 unsigned long option;
1428 int token;
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (!*data)
1431 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001433 token = match_token(data, cifs_mount_option_tokens, args);
1434
1435 switch (token) {
1436
1437 /* Ingnore the following */
1438 case Opt_ignore:
1439 break;
1440
1441 /* Boolean values */
1442 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001444 break;
1445 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001447 break;
1448 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001449 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001450 break;
1451 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001452 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001453 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001454 case Opt_forcegid:
1455 override_gid = 1;
1456 break;
1457 case Opt_noforcegid:
1458 override_gid = 0;
1459 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001460 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001461 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001462 break;
1463 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001464 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001465 break;
1466 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001468 break;
1469 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001471 break;
1472 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001474 break;
1475 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001477 break;
1478 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001479 vol->sfu_remap = true;
1480 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001481 break;
1482 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001483 vol->sfu_remap = false;
1484 break;
1485 case Opt_mapposix:
1486 vol->remap = true;
1487 vol->sfu_remap = false; /* disable SFU mapping */
1488 break;
1489 case Opt_nomapposix:
1490 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001491 break;
1492 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001493 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001494 break;
1495 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001496 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001497 break;
1498 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001499 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001500 break;
1501 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001502 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001503 break;
1504 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001505 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001506 break;
1507 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001508 if (vol->linux_ext)
1509 cifs_dbg(VFS,
1510 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001511 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001512 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001513 case Opt_unix:
1514 if (vol->no_linux_ext)
1515 cifs_dbg(VFS,
1516 "conflicting unix mount options\n");
1517 vol->linux_ext = 1;
1518 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001519 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001520 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001521 break;
1522 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001523 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001524 break;
1525 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001526 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001527 /*
1528 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001529 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001530 * local vfs will do advisory
1531 */
Steve French50c2f752007-07-13 00:33:32 +00001532 if (vol->file_mode ==
1533 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001534 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001535 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001536 case Opt_nohandlecache:
1537 vol->nohandlecache = 1;
1538 break;
1539 case Opt_handlecache:
1540 vol->nohandlecache = 0;
1541 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001542 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001543 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001544 break;
1545 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001547 break;
1548 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001550 break;
Steve French95932652016-09-23 01:36:34 -05001551 case Opt_setuidfromacl:
1552 vol->setuidfromacl = 1;
1553 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001554 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001555 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001556 break;
1557 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001558 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001559 break;
1560 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001562 break;
1563 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001565 break;
1566 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001568 break;
1569 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001571 break;
1572 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001573 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001574 break;
1575 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001576 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001577 break;
1578 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001580 break;
1581 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001583 break;
1584 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001585 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001586 break;
1587 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001588 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001589 break;
1590 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001591 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001592 break;
1593 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001595 break;
1596 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001598 break;
1599 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001600 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001601 break;
1602 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001603 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001604 break;
1605 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001606 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001607 * is a per tree connection (mount) not a per socket
1608 * or per-smb connection option in the protocol
1609 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1610 */
Steve French95b1cb92008-05-15 16:44:38 +00001611 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001612 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001613 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001614 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001615 break;
1616 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301617#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001618 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001619 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301620#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301621 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001622 break;
1623 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001624 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001625 break;
1626 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001627 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001628 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001629 case Opt_sloppy:
1630 sloppy = true;
1631 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001632 case Opt_nosharesock:
1633 vol->nosharesock = true;
1634 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001635 case Opt_nopersistent:
1636 vol->nopersistent = true;
1637 if (vol->persistent) {
1638 cifs_dbg(VFS,
1639 "persistenthandles mount options conflict\n");
1640 goto cifs_parse_mount_err;
1641 }
1642 break;
1643 case Opt_persistent:
1644 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001645 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001646 cifs_dbg(VFS,
1647 "persistenthandles mount options conflict\n");
1648 goto cifs_parse_mount_err;
1649 }
1650 break;
Steve French592fafe2015-11-03 10:08:53 -06001651 case Opt_resilient:
1652 vol->resilient = true;
1653 if (vol->persistent) {
1654 cifs_dbg(VFS,
1655 "persistenthandles mount options conflict\n");
1656 goto cifs_parse_mount_err;
1657 }
1658 break;
1659 case Opt_noresilient:
1660 vol->resilient = false; /* already the default */
1661 break;
Germano Percossi39566442016-12-15 12:31:18 +05301662 case Opt_domainauto:
1663 vol->domainauto = true;
1664 break;
Long Li8339dd32017-11-07 01:54:55 -07001665 case Opt_rdma:
1666 vol->rdma = true;
1667 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001668
1669 /* Numeric Values */
1670 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001671 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001672 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1673 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001674 goto cifs_parse_mount_err;
1675 }
1676 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001677 break;
1678 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001679 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001680 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1681 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001682 goto cifs_parse_mount_err;
1683 }
1684 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001685 break;
1686 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001687 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001688 cifs_dbg(VFS, "%s: Invalid uid value\n",
1689 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001690 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001692 uid_specified = true;
1693 break;
1694 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001695 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001696 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1697 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001698 goto cifs_parse_mount_err;
1699 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001700 break;
1701 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001702 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001703 cifs_dbg(VFS, "%s: Invalid gid value\n",
1704 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001705 goto cifs_parse_mount_err;
1706 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001707 gid_specified = true;
1708 break;
1709 case Opt_file_mode:
1710 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001711 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1712 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001713 goto cifs_parse_mount_err;
1714 }
1715 vol->file_mode = option;
1716 break;
1717 case Opt_dirmode:
1718 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001719 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1720 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001721 goto cifs_parse_mount_err;
1722 }
1723 vol->dir_mode = option;
1724 break;
1725 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001726 if (get_option_ul(args, &option) ||
1727 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001728 cifs_dbg(VFS, "%s: Invalid port value\n",
1729 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001730 goto cifs_parse_mount_err;
1731 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001732 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001733 break;
1734 case Opt_rsize:
1735 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001736 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1737 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001738 goto cifs_parse_mount_err;
1739 }
1740 vol->rsize = option;
1741 break;
1742 case Opt_wsize:
1743 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001744 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1745 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001746 goto cifs_parse_mount_err;
1747 }
1748 vol->wsize = option;
1749 break;
1750 case Opt_actimeo:
1751 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001752 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1753 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001754 goto cifs_parse_mount_err;
1755 }
1756 vol->actimeo = HZ * option;
1757 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001758 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001759 goto cifs_parse_mount_err;
1760 }
1761 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001762 case Opt_echo_interval:
1763 if (get_option_ul(args, &option)) {
1764 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1765 __func__);
1766 goto cifs_parse_mount_err;
1767 }
1768 vol->echo_interval = option;
1769 break;
Steve French8b217fe2016-11-11 22:36:20 -06001770 case Opt_snapshot:
1771 if (get_option_ul(args, &option)) {
1772 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1773 __func__);
1774 goto cifs_parse_mount_err;
1775 }
1776 vol->snapshot_time = option;
1777 break;
Steve French141891f2016-09-23 00:44:16 -05001778 case Opt_max_credits:
1779 if (get_option_ul(args, &option) || (option < 20) ||
1780 (option > 60000)) {
1781 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1782 __func__);
1783 goto cifs_parse_mount_err;
1784 }
1785 vol->max_credits = option;
1786 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001787
1788 /* String Arguments */
1789
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001790 case Opt_blank_user:
1791 /* null user, ie. anonymous authentication */
1792 vol->nullauth = 1;
1793 vol->username = NULL;
1794 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001795 case Opt_user:
1796 string = match_strdup(args);
1797 if (string == NULL)
1798 goto out_nomem;
1799
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001800 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1801 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001802 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001803 goto cifs_parse_mount_err;
1804 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001805
1806 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001807 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001808 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001809 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001810 break;
1811 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001812 /* passwords have to be handled differently
1813 * to allow the character used for deliminator
1814 * to be passed within them
1815 */
1816
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001817 /*
1818 * Check if this is a case where the password
1819 * starts with a delimiter
1820 */
1821 tmp_end = strchr(data, '=');
1822 tmp_end++;
1823 if (!(tmp_end < end && tmp_end[1] == delim)) {
1824 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001825 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001826 vol->password = NULL;
1827 break;
1828 }
1829 /* Yes it is. Drop down to Opt_pass below.*/
1830 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001831 /* Obtain the value string */
1832 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001833 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001834
1835 /* Set tmp_end to end of the string */
1836 tmp_end = (char *) value + strlen(value);
1837
1838 /* Check if following character is the deliminator
1839 * If yes, we have encountered a double deliminator
1840 * reset the NULL character to the deliminator
1841 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301842 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001843 tmp_end[0] = delim;
1844
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301845 /* Keep iterating until we get to a single
1846 * deliminator OR the end
1847 */
1848 while ((tmp_end = strchr(tmp_end, delim))
1849 != NULL && (tmp_end[1] == delim)) {
1850 tmp_end = (char *) &tmp_end[2];
1851 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001852
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301853 /* Reset var options to point to next element */
1854 if (tmp_end) {
1855 tmp_end[0] = '\0';
1856 options = (char *) &tmp_end[1];
1857 } else
1858 /* Reached the end of the mount option
1859 * string */
1860 options = end;
1861 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001862
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001863 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001864 /* Now build new password string */
1865 temp_len = strlen(value);
1866 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1867 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001868 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001869 goto cifs_parse_mount_err;
1870 }
1871
1872 for (i = 0, j = 0; i < temp_len; i++, j++) {
1873 vol->password[j] = value[i];
1874 if ((value[i] == delim) &&
1875 value[i+1] == delim)
1876 /* skip the second deliminator */
1877 i++;
1878 }
1879 vol->password[j] = '\0';
1880 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001881 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001882 /* FIXME: should this be an error instead? */
1883 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001884 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001885 case Opt_ip:
1886 string = match_strdup(args);
1887 if (string == NULL)
1888 goto out_nomem;
1889
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001890 if (!cifs_convert_address(dstaddr, string,
1891 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001892 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001893 goto cifs_parse_mount_err;
1894 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001895 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001896 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001897 case Opt_domain:
1898 string = match_strdup(args);
1899 if (string == NULL)
1900 goto out_nomem;
1901
Chen Gang057d6332013-07-19 09:01:36 +08001902 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1903 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001904 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001905 goto cifs_parse_mount_err;
1906 }
1907
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001908 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001909 vol->domainname = kstrdup(string, GFP_KERNEL);
1910 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001911 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001912 goto cifs_parse_mount_err;
1913 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001914 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001915 break;
1916 case Opt_srcaddr:
1917 string = match_strdup(args);
1918 if (string == NULL)
1919 goto out_nomem;
1920
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001921 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001922 (struct sockaddr *)&vol->srcaddr,
1923 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001924 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1925 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001926 goto cifs_parse_mount_err;
1927 }
1928 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001929 case Opt_iocharset:
1930 string = match_strdup(args);
1931 if (string == NULL)
1932 goto out_nomem;
1933
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001934 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001935 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001936 goto cifs_parse_mount_err;
1937 }
1938
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001939 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001940 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001941 vol->iocharset = kstrdup(string,
1942 GFP_KERNEL);
1943 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001944 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001945 goto cifs_parse_mount_err;
1946 }
1947 }
1948 /* if iocharset not set then load_nls_default
1949 * is used by caller
1950 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001951 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001952 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001953 case Opt_netbiosname:
1954 string = match_strdup(args);
1955 if (string == NULL)
1956 goto out_nomem;
1957
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001958 memset(vol->source_rfc1001_name, 0x20,
1959 RFC1001_NAME_LEN);
1960 /*
1961 * FIXME: are there cases in which a comma can
1962 * be valid in workstation netbios name (and
1963 * need special handling)?
1964 */
1965 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1966 /* don't ucase netbiosname for user */
1967 if (string[i] == 0)
1968 break;
1969 vol->source_rfc1001_name[i] = string[i];
1970 }
1971 /* The string has 16th byte zero still from
1972 * set at top of the function
1973 */
1974 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001975 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001976 break;
1977 case Opt_servern:
1978 /* servernetbiosname specified override *SMBSERVER */
1979 string = match_strdup(args);
1980 if (string == NULL)
1981 goto out_nomem;
1982
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001983 /* last byte, type, is 0x20 for servr type */
1984 memset(vol->target_rfc1001_name, 0x20,
1985 RFC1001_NAME_LEN_WITH_NULL);
1986
1987 /* BB are there cases in which a comma can be
1988 valid in this workstation netbios name
1989 (and need special handling)? */
1990
1991 /* user or mount helper must uppercase the
1992 netbios name */
1993 for (i = 0; i < 15; i++) {
1994 if (string[i] == 0)
1995 break;
1996 vol->target_rfc1001_name[i] = string[i];
1997 }
1998 /* The string has 16th byte zero still from
1999 set at top of the function */
2000 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002001 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002002 break;
2003 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002004 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002005 string = match_strdup(args);
2006 if (string == NULL)
2007 goto out_nomem;
2008
Steve French7e682f72017-08-31 21:34:24 -05002009 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002010 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002011 if (strlen(string) > 1) {
2012 pr_warn("Bad mount helper ver=%s. Did "
2013 "you want SMB1 (CIFS) dialect "
2014 "and mean to type vers=1.0 "
2015 "instead?\n", string);
2016 goto cifs_parse_mount_err;
2017 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002018 /* This is the default */
2019 break;
2020 }
2021 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002022 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002023 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002024 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002025 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002026 string = match_strdup(args);
2027 if (string == NULL)
2028 goto out_nomem;
2029
Steve Frenchc7c137b2018-06-06 17:59:29 -05002030 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002031 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002032 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002033 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002034 case Opt_sec:
2035 string = match_strdup(args);
2036 if (string == NULL)
2037 goto out_nomem;
2038
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002039 if (cifs_parse_security_flavors(string, vol) != 0)
2040 goto cifs_parse_mount_err;
2041 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002042 case Opt_cache:
2043 string = match_strdup(args);
2044 if (string == NULL)
2045 goto out_nomem;
2046
2047 if (cifs_parse_cache_flavor(string, vol) != 0)
2048 goto cifs_parse_mount_err;
2049 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002050 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002051 /*
2052 * An option we don't recognize. Save it off for later
2053 * if we haven't already found one
2054 */
2055 if (!invalid)
2056 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002057 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002059 /* Free up any allocated string */
2060 kfree(string);
2061 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002063
Jeff Laytond8162552012-03-23 14:40:56 -04002064 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002065 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002066 goto cifs_parse_mount_err;
2067 }
2068
Long Li8339dd32017-11-07 01:54:55 -07002069 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2070 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2071 goto cifs_parse_mount_err;
2072 }
2073
Jeff Layton8a8798a2012-01-17 16:09:15 -05002074#ifndef CONFIG_KEYS
2075 /* Muliuser mounts require CONFIG_KEYS support */
2076 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002077 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002078 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002079 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002080#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002081 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002082 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002083 goto cifs_parse_mount_err;
2084 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002085
Jeff Layton62a1a432012-12-10 06:10:45 -05002086 /* make sure UNC has a share name */
2087 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002088 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002089 goto cifs_parse_mount_err;
2090 }
2091
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002092 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002093 int len;
2094 const char *slash;
2095
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002096 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002097 /* Use the address part of the UNC. */
2098 slash = strchr(&vol->UNC[2], '\\');
2099 len = slash - &vol->UNC[2];
2100 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002101 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002102 goto cifs_parse_mount_err;
2103 }
2104 }
2105
2106 /* set the port that we got earlier */
2107 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002109 if (uid_specified)
2110 vol->override_uid = override_uid;
2111 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002112 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002113
2114 if (gid_specified)
2115 vol->override_gid = override_gid;
2116 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002117 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002118
Steve French7e682f72017-08-31 21:34:24 -05002119 if (got_version == false)
2120 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002121 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002122 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002123 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2124 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002125
Sean Finneyb9468452011-04-11 13:19:32 +00002126 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002128
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002129out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002130 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002131cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002132 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002133 kfree(mountdata_copy);
2134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Ben Greear3eb9a882010-09-01 17:06:02 -07002137/** Returns true if srcaddr isn't specified and rhs isn't
2138 * specified, or if srcaddr is specified and
2139 * matches the IP address of the rhs argument.
2140 */
Jeff Layton45151482010-07-06 20:43:02 -04002141static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002142srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2143{
2144 switch (srcaddr->sa_family) {
2145 case AF_UNSPEC:
2146 return (rhs->sa_family == AF_UNSPEC);
2147 case AF_INET: {
2148 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2149 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2150 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2151 }
2152 case AF_INET6: {
2153 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002154 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002155 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2156 }
2157 default:
2158 WARN_ON(1);
2159 return false; /* don't expect to be here */
2160 }
2161}
2162
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002163/*
2164 * If no port is specified in addr structure, we try to match with 445 port
2165 * and if it fails - with 139 ports. It should be called only if address
2166 * families of server and addr are equal.
2167 */
2168static bool
2169match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2170{
Steve French6da97912011-03-13 18:55:55 +00002171 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002172
2173 switch (addr->sa_family) {
2174 case AF_INET:
2175 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2176 port = ((struct sockaddr_in *) addr)->sin_port;
2177 break;
2178 case AF_INET6:
2179 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2180 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2181 break;
2182 default:
2183 WARN_ON(1);
2184 return false;
2185 }
2186
2187 if (!port) {
2188 port = htons(CIFS_PORT);
2189 if (port == *sport)
2190 return true;
2191
2192 port = htons(RFC1001_PORT);
2193 }
2194
2195 return port == *sport;
2196}
Ben Greear3eb9a882010-09-01 17:06:02 -07002197
2198static bool
2199match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2200 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Jeff Layton45151482010-07-06 20:43:02 -04002202 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002203 case AF_INET: {
2204 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2205 struct sockaddr_in *srv_addr4 =
2206 (struct sockaddr_in *)&server->dstaddr;
2207
2208 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002209 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002210 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002211 }
2212 case AF_INET6: {
2213 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2214 struct sockaddr_in6 *srv_addr6 =
2215 (struct sockaddr_in6 *)&server->dstaddr;
2216
Jeff Layton45151482010-07-06 20:43:02 -04002217 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002218 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002219 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002220 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002221 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002222 break;
2223 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002224 default:
2225 WARN_ON(1);
2226 return false; /* don't expect to be here */
2227 }
Jeff Layton45151482010-07-06 20:43:02 -04002228
Ben Greear3eb9a882010-09-01 17:06:02 -07002229 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2230 return false;
2231
Jeff Layton45151482010-07-06 20:43:02 -04002232 return true;
2233}
2234
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002235static bool
2236match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2237{
Jeff Layton3f618222013-06-12 19:52:14 -05002238 /*
2239 * The select_sectype function should either return the vol->sectype
2240 * that was specified, or "Unspecified" if that sectype was not
2241 * compatible with the given NEGOTIATE request.
2242 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302243 if (server->ops->select_sectype(server, vol->sectype)
2244 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002245 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002246
Jeff Layton3f618222013-06-12 19:52:14 -05002247 /*
2248 * Now check if signing mode is acceptable. No need to check
2249 * global_secflags at this point since if MUST_SIGN is set then
2250 * the server->sign had better be too.
2251 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002252 if (vol->sign && !server->sign)
2253 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002254
2255 return true;
2256}
2257
Jeff Layton9fa114f2012-11-26 11:09:57 -05002258static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002259{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002260 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2261
Jeff Laytona0b3df52013-05-24 07:40:59 -04002262 if (vol->nosharesock)
2263 return 0;
2264
Steve French9764c022017-09-17 10:41:35 -05002265 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002266 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2267 return 0;
2268
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002269 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2270 return 0;
2271
2272 if (!match_address(server, addr,
2273 (struct sockaddr *)&vol->srcaddr))
2274 return 0;
2275
2276 if (!match_port(server, addr))
2277 return 0;
2278
2279 if (!match_security(server, vol))
2280 return 0;
2281
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002282 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002283 return 0;
2284
Long Li8339dd32017-11-07 01:54:55 -07002285 if (server->rdma != vol->rdma)
2286 return 0;
2287
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002288 return 1;
2289}
2290
Jeff Layton45151482010-07-06 20:43:02 -04002291static struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002292cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002293{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002294 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302296 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002297 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002298 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002299 continue;
2300
Jeff Laytone7ddee92008-11-14 13:44:38 -05002301 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302302 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002303 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002304 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302306 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return NULL;
2308}
2309
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002310void
2311cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002313 struct task_struct *task;
2314
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302315 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002316 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302317 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002318 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002320
Rob Landleyf1d0c992011-01-22 15:44:05 -06002321 put_net(cifs_net_ns(server));
2322
Jeff Laytone7ddee92008-11-14 13:44:38 -05002323 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302324 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002325
Jeff Laytonc74093b2011-01-11 07:24:23 -05002326 cancel_delayed_work_sync(&server->echo);
2327
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002328 if (from_reconnect)
2329 /*
2330 * Avoid deadlock here: reconnect work calls
2331 * cifs_put_tcp_session() at its end. Need to be sure
2332 * that reconnect work does nothing with server pointer after
2333 * that step.
2334 */
2335 cancel_delayed_work(&server->reconnect);
2336 else
2337 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002338
Jeff Laytone7ddee92008-11-14 13:44:38 -05002339 spin_lock(&GlobalMid_Lock);
2340 server->tcpStatus = CifsExiting;
2341 spin_unlock(&GlobalMid_Lock);
2342
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002343 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302344 cifs_fscache_release_client_cookie(server);
2345
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002346 kfree(server->session_key.response);
2347 server->session_key.response = NULL;
2348 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002349
2350 task = xchg(&server->tsk, NULL);
2351 if (task)
2352 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
Jeff Layton63c038c2008-12-01 18:41:46 -05002355static struct TCP_Server_Info *
2356cifs_get_tcp_session(struct smb_vol *volume_info)
2357{
2358 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002359 int rc;
2360
Joe Perchesf96637b2013-05-04 22:12:25 -05002361 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002362
2363 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002364 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002365 if (tcp_ses)
2366 return tcp_ses;
2367
2368 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2369 if (!tcp_ses) {
2370 rc = -ENOMEM;
2371 goto out_err;
2372 }
2373
Jeff Layton23db65f2012-05-15 12:20:51 -04002374 tcp_ses->ops = volume_info->ops;
2375 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002376 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002377 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2378 if (IS_ERR(tcp_ses->hostname)) {
2379 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002380 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002381 }
2382
2383 tcp_ses->noblocksnd = volume_info->noblocksnd;
2384 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002385 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002386 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002387 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002388 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002389 init_waitqueue_head(&tcp_ses->response_q);
2390 init_waitqueue_head(&tcp_ses->request_q);
2391 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2392 mutex_init(&tcp_ses->srv_mutex);
2393 memcpy(tcp_ses->workstation_RFC1001_name,
2394 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2395 memcpy(tcp_ses->server_RFC1001_name,
2396 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002397 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002398 tcp_ses->sequence_number = 0;
Steve French9e1a37d2018-09-19 02:38:17 -05002399 tcp_ses->reconnect_instance = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002400 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002401 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002402 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2403 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002404 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002405 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2406 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002407 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2408 sizeof(tcp_ses->srcaddr));
2409 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2410 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002411 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002412 /*
2413 * at this point we are the only ones with the pointer
2414 * to the struct since the kernel thread not created yet
2415 * no need to spinlock this init of tcpStatus or srv_count
2416 */
2417 tcp_ses->tcpStatus = CifsNew;
2418 ++tcp_ses->srv_count;
2419
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002420 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2421 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2422 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2423 else
2424 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002425 if (tcp_ses->rdma) {
2426#ifndef CONFIG_CIFS_SMB_DIRECT
2427 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2428 rc = -ENOENT;
2429 goto out_err_crypto_release;
2430#endif
2431 tcp_ses->smbd_conn = smbd_get_connection(
2432 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2433 if (tcp_ses->smbd_conn) {
2434 cifs_dbg(VFS, "RDMA transport established\n");
2435 rc = 0;
2436 goto smbd_connected;
2437 } else {
2438 rc = -ENOENT;
2439 goto out_err_crypto_release;
2440 }
2441 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002442 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002443 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002444 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002445 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002446 }
Long Li2f894642017-11-22 17:38:34 -07002447smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002448 /*
2449 * since we're in a cifs function already, we know that
2450 * this will succeed. No need for try_module_get().
2451 */
2452 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002453 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002454 tcp_ses, "cifsd");
2455 if (IS_ERR(tcp_ses->tsk)) {
2456 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002457 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002458 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002459 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002460 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002461 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002462
2463 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302464 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002465 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302466 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002467
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302468 cifs_fscache_get_client_cookie(tcp_ses);
2469
Jeff Laytonc74093b2011-01-11 07:24:23 -05002470 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002471 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002472
Jeff Layton63c038c2008-12-01 18:41:46 -05002473 return tcp_ses;
2474
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002475out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002476 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002477
Rob Landleyf1d0c992011-01-22 15:44:05 -06002478 put_net(cifs_net_ns(tcp_ses));
2479
Jeff Layton63c038c2008-12-01 18:41:46 -05002480out_err:
2481 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002482 if (!IS_ERR(tcp_ses->hostname))
2483 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002484 if (tcp_ses->ssocket)
2485 sock_release(tcp_ses->ssocket);
2486 kfree(tcp_ses);
2487 }
2488 return ERR_PTR(rc);
2489}
2490
Steve French96daf2b2011-05-27 04:34:02 +00002491static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002492{
Jeff Layton3f618222013-06-12 19:52:14 -05002493 if (vol->sectype != Unspecified &&
2494 vol->sectype != ses->sectype)
2495 return 0;
2496
2497 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002498 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002499 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002500 return 0;
2501 break;
2502 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002503 /* NULL username means anonymous session */
2504 if (ses->user_name == NULL) {
2505 if (!vol->nullauth)
2506 return 0;
2507 break;
2508 }
2509
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002510 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002511 if (strncmp(ses->user_name,
2512 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002513 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002514 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002515 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002516 ses->password != NULL &&
2517 strncmp(ses->password,
2518 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002519 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002520 return 0;
2521 }
2522 return 1;
2523}
2524
Aurelien Aptelb327a712018-01-24 13:46:10 +01002525/**
2526 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2527 *
2528 * A new IPC connection is made and stored in the session
2529 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2530 */
2531static int
2532cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2533{
2534 int rc = 0, xid;
2535 struct cifs_tcon *tcon;
2536 struct nls_table *nls_codepage;
2537 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2538 bool seal = false;
2539
2540 /*
2541 * If the mount request that resulted in the creation of the
2542 * session requires encryption, force IPC to be encrypted too.
2543 */
2544 if (volume_info->seal) {
2545 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2546 seal = true;
2547 else {
2548 cifs_dbg(VFS,
2549 "IPC: server doesn't support encryption\n");
2550 return -EOPNOTSUPP;
2551 }
2552 }
2553
2554 tcon = tconInfoAlloc();
2555 if (tcon == NULL)
2556 return -ENOMEM;
2557
Thomas Werschlein395a2072018-08-30 18:29:20 +02002558 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002559
2560 /* cannot fail */
2561 nls_codepage = load_nls_default();
2562
2563 xid = get_xid();
2564 tcon->ses = ses;
2565 tcon->ipc = true;
2566 tcon->seal = seal;
2567 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2568 free_xid(xid);
2569
2570 if (rc) {
2571 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2572 tconInfoFree(tcon);
2573 goto out;
2574 }
2575
2576 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2577
2578 ses->tcon_ipc = tcon;
2579out:
2580 unload_nls(nls_codepage);
2581 return rc;
2582}
2583
2584/**
2585 * cifs_free_ipc - helper to release the session IPC tcon
2586 *
2587 * Needs to be called everytime a session is destroyed
2588 */
2589static int
2590cifs_free_ipc(struct cifs_ses *ses)
2591{
2592 int rc = 0, xid;
2593 struct cifs_tcon *tcon = ses->tcon_ipc;
2594
2595 if (tcon == NULL)
2596 return 0;
2597
2598 if (ses->server->ops->tree_disconnect) {
2599 xid = get_xid();
2600 rc = ses->server->ops->tree_disconnect(xid, tcon);
2601 free_xid(xid);
2602 }
2603
2604 if (rc)
2605 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2606
2607 tconInfoFree(tcon);
2608 ses->tcon_ipc = NULL;
2609 return rc;
2610}
2611
Steve French96daf2b2011-05-27 04:34:02 +00002612static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002613cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Steve French96daf2b2011-05-27 04:34:02 +00002615 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302617 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002618 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002619 if (ses->status == CifsExiting)
2620 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002621 if (!match_session(ses, vol))
2622 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002623 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302624 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002625 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302627 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return NULL;
2629}
2630
Jeff Layton14fbf502008-11-14 13:53:46 -05002631static void
Steve French96daf2b2011-05-27 04:34:02 +00002632cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002633{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002634 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002635 struct TCP_Server_Info *server = ses->server;
2636
Joe Perchesf96637b2013-05-04 22:12:25 -05002637 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002638
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302639 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002640 if (ses->status == CifsExiting) {
2641 spin_unlock(&cifs_tcp_ses_lock);
2642 return;
2643 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002644 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302645 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002646 return;
2647 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002648 if (ses->status == CifsGood)
2649 ses->status = CifsExiting;
2650 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002651
Aurelien Aptelb327a712018-01-24 13:46:10 +01002652 cifs_free_ipc(ses);
2653
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002654 if (ses->status == CifsExiting && server->ops->logoff) {
2655 xid = get_xid();
2656 rc = server->ops->logoff(xid, ses);
2657 if (rc)
2658 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2659 __func__, rc);
2660 _free_xid(xid);
2661 }
2662
2663 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002664 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302665 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002666
Jeff Layton14fbf502008-11-14 13:53:46 -05002667 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002668 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002669}
2670
Jeff Layton8a8798a2012-01-17 16:09:15 -05002671#ifdef CONFIG_KEYS
2672
Chen Gang057d6332013-07-19 09:01:36 +08002673/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2674#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002675
2676/* Populate username and pw fields from keyring if possible */
2677static int
2678cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2679{
2680 int rc = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002681 const char *delim, *payload;
2682 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002683 ssize_t len;
2684 struct key *key;
2685 struct TCP_Server_Info *server = ses->server;
2686 struct sockaddr_in *sa;
2687 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002688 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002689
2690 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2691 if (!desc)
2692 return -ENOMEM;
2693
2694 /* try to find an address key first */
2695 switch (server->dstaddr.ss_family) {
2696 case AF_INET:
2697 sa = (struct sockaddr_in *)&server->dstaddr;
2698 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2699 break;
2700 case AF_INET6:
2701 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2702 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2703 break;
2704 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002705 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2706 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002707 rc = -EINVAL;
2708 goto out_err;
2709 }
2710
Joe Perchesf96637b2013-05-04 22:12:25 -05002711 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002712 key = request_key(&key_type_logon, desc, "");
2713 if (IS_ERR(key)) {
2714 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002715 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002716 rc = PTR_ERR(key);
2717 goto out_err;
2718 }
2719
2720 /* didn't work, try to find a domain key */
2721 sprintf(desc, "cifs:d:%s", ses->domainName);
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 rc = PTR_ERR(key);
2726 goto out_err;
2727 }
2728 }
2729
2730 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002731 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002732 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002733 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002734 goto out_key_put;
2735 }
2736
2737 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002738 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002739 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002740 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002741 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002742 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2743 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002744 rc = -EINVAL;
2745 goto out_key_put;
2746 }
2747
2748 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002749 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002750 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2751 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002752 rc = -EINVAL;
2753 goto out_key_put;
2754 }
2755
2756 vol->username = kstrndup(payload, len, GFP_KERNEL);
2757 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002758 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2759 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002760 rc = -ENOMEM;
2761 goto out_key_put;
2762 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002763 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002764
2765 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002766 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002767 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002768 rc = -EINVAL;
2769 kfree(vol->username);
2770 vol->username = NULL;
2771 goto out_key_put;
2772 }
2773
2774 ++delim;
2775 vol->password = kstrndup(delim, len, GFP_KERNEL);
2776 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002777 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2778 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002779 rc = -ENOMEM;
2780 kfree(vol->username);
2781 vol->username = NULL;
2782 goto out_key_put;
2783 }
2784
2785out_key_put:
2786 up_read(&key->sem);
2787 key_put(key);
2788out_err:
2789 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002790 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002791 return rc;
2792}
2793#else /* ! CONFIG_KEYS */
2794static inline int
2795cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2796 struct cifs_ses *ses __attribute__((unused)))
2797{
2798 return -ENOSYS;
2799}
2800#endif /* CONFIG_KEYS */
2801
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002802/**
2803 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2804 *
2805 * This function assumes it is being called from cifs_mount() where we
2806 * already got a server reference (server refcount +1). See
2807 * cifs_get_tcon() for refcount explanations.
2808 */
Steve French96daf2b2011-05-27 04:34:02 +00002809static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002810cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2811{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002812 int rc = -ENOMEM;
2813 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002814 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002815 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2816 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002817
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002818 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002819
Jeff Layton4ff67b72010-07-06 20:43:02 -04002820 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002821 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002822 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2823 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002824
Jeff Layton36988c72010-04-24 07:57:43 -04002825 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002826 rc = cifs_negotiate_protocol(xid, ses);
2827 if (rc) {
2828 mutex_unlock(&ses->session_mutex);
2829 /* problem -- put our ses reference */
2830 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002831 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002832 return ERR_PTR(rc);
2833 }
Jeff Layton36988c72010-04-24 07:57:43 -04002834 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002835 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002836 rc = cifs_setup_session(xid, ses,
2837 volume_info->local_nls);
2838 if (rc) {
2839 mutex_unlock(&ses->session_mutex);
2840 /* problem -- put our reference */
2841 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002842 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002843 return ERR_PTR(rc);
2844 }
2845 }
2846 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002847
2848 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002849 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002850 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002851 return ses;
2852 }
2853
Joe Perchesf96637b2013-05-04 22:12:25 -05002854 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002855 ses = sesInfoAlloc();
2856 if (ses == NULL)
2857 goto get_ses_fail;
2858
2859 /* new SMB session uses our server ref */
2860 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002861 if (server->dstaddr.ss_family == AF_INET6)
2862 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002863 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002864 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002865
Steve French8727c8a2011-02-25 01:11:56 -06002866 if (volume_info->username) {
2867 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2868 if (!ses->user_name)
2869 goto get_ses_fail;
2870 }
Jeff Layton36988c72010-04-24 07:57:43 -04002871
2872 /* volume_info->password freed at unmount */
2873 if (volume_info->password) {
2874 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2875 if (!ses->password)
2876 goto get_ses_fail;
2877 }
2878 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002879 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2880 if (!ses->domainName)
2881 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002882 }
Germano Percossi39566442016-12-15 12:31:18 +05302883 if (volume_info->domainauto)
2884 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002885 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002886 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002887
Jeff Layton28e11bd2013-05-26 07:01:00 -04002888 ses->sectype = volume_info->sectype;
2889 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002890
2891 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002892 rc = cifs_negotiate_protocol(xid, ses);
2893 if (!rc)
2894 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002895 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002896 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002897 goto get_ses_fail;
2898
2899 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302900 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002901 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302902 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002903
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002904 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002905
2906 cifs_setup_ipc(ses, volume_info);
2907
Jeff Layton36988c72010-04-24 07:57:43 -04002908 return ses;
2909
2910get_ses_fail:
2911 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002912 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002913 return ERR_PTR(rc);
2914}
2915
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002916static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002917{
2918 if (tcon->tidStatus == CifsExiting)
2919 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002920 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002921 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002922 if (tcon->seal != volume_info->seal)
2923 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002924 if (tcon->snapshot_time != volume_info->snapshot_time)
2925 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002926 return 1;
2927}
2928
Steve French96daf2b2011-05-27 04:34:02 +00002929static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06002930cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002933 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302935 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002936 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002937 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002938 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002939 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002940 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302941 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return tcon;
2943 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302944 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return NULL;
2946}
2947
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002948void
Steve French96daf2b2011-05-27 04:34:02 +00002949cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002950{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002951 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002952 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002953
Aurelien Aptelb327a712018-01-24 13:46:10 +01002954 /*
2955 * IPC tcon share the lifetime of their session and are
2956 * destroyed in the session put function
2957 */
2958 if (tcon == NULL || tcon->ipc)
2959 return;
2960
2961 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002962 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302963 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002964 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302965 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002966 return;
2967 }
2968
2969 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302970 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002971
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002972 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002973 if (ses->server->ops->tree_disconnect)
2974 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002975 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002976
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302977 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002978 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002979 cifs_put_smb_ses(ses);
2980}
2981
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002982/**
2983 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
2984 *
2985 * - tcon refcount is the number of mount points using the tcon.
2986 * - ses refcount is the number of tcon using the session.
2987 *
2988 * 1. This function assumes it is being called from cifs_mount() where
2989 * we already got a session reference (ses refcount +1).
2990 *
2991 * 2. Since we're in the context of adding a mount point, the end
2992 * result should be either:
2993 *
2994 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2995 * its session refcount incremented (1 new tcon). This +1 was
2996 * already done in (1).
2997 *
2998 * b) an existing tcon with refcount+1 (add a mount point to it) and
2999 * identical ses refcount (no new tcon). Because of (1) we need to
3000 * decrement the ses refcount.
3001 */
Steve French96daf2b2011-05-27 04:34:02 +00003002static struct cifs_tcon *
3003cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003004{
3005 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003006 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003007
Steve French8b217fe2016-11-11 22:36:20 -06003008 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003009 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003010 /*
3011 * tcon has refcount already incremented but we need to
3012 * decrement extra ses reference gotten by caller (case b)
3013 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003014 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003015 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003016 return tcon;
3017 }
3018
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003019 if (!ses->server->ops->tree_connect) {
3020 rc = -ENOSYS;
3021 goto out_fail;
3022 }
3023
Jeff Laytond00c28d2010-04-24 07:57:44 -04003024 tcon = tconInfoAlloc();
3025 if (tcon == NULL) {
3026 rc = -ENOMEM;
3027 goto out_fail;
3028 }
3029
Steve French8b217fe2016-11-11 22:36:20 -06003030 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003031 if (ses->server->vals->protocol_id == 0) {
3032 cifs_dbg(VFS,
3033 "Use SMB2 or later for snapshot mount option\n");
3034 rc = -EOPNOTSUPP;
3035 goto out_fail;
3036 } else
3037 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003038 }
3039
Jeff Laytond00c28d2010-04-24 07:57:44 -04003040 tcon->ses = ses;
3041 if (volume_info->password) {
3042 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3043 if (!tcon->password) {
3044 rc = -ENOMEM;
3045 goto out_fail;
3046 }
3047 }
3048
Steve French23657ad2018-04-22 15:14:58 -05003049 if (volume_info->seal) {
3050 if (ses->server->vals->protocol_id == 0) {
3051 cifs_dbg(VFS,
3052 "SMB3 or later required for encryption\n");
3053 rc = -EOPNOTSUPP;
3054 goto out_fail;
3055 } else if (tcon->ses->server->capabilities &
3056 SMB2_GLOBAL_CAP_ENCRYPTION)
3057 tcon->seal = true;
3058 else {
3059 cifs_dbg(VFS, "Encryption is not supported on share\n");
3060 rc = -EOPNOTSUPP;
3061 goto out_fail;
3062 }
3063 }
3064
Steve French8505c8b2018-06-18 14:01:59 -05003065 if (volume_info->linux_ext) {
3066 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003067 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003068 printk_once(KERN_WARNING
3069 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003070 } else {
3071 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3072 rc = -EOPNOTSUPP;
3073 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003074 }
Steve Frenchb3266142018-05-20 23:41:10 -05003075 }
Steve Frenchb3266142018-05-20 23:41:10 -05003076
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003077 /*
3078 * BB Do we need to wrap session_mutex around this TCon call and Unix
3079 * SetFS as we do on SessSetup and reconnect?
3080 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003081 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003082 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3083 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003084 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003085 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003086 if (rc)
3087 goto out_fail;
3088
3089 if (volume_info->nodfs) {
3090 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
Joe Perchesf96637b2013-05-04 22:12:25 -05003091 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003092 }
Steve Frenchb618f002015-11-03 09:15:03 -06003093 tcon->use_persistent = false;
3094 /* check if SMB2 or later, CIFS does not support persistent handles */
3095 if (volume_info->persistent) {
3096 if (ses->server->vals->protocol_id == 0) {
3097 cifs_dbg(VFS,
3098 "SMB3 or later required for persistent handles\n");
3099 rc = -EOPNOTSUPP;
3100 goto out_fail;
3101 } else if (ses->server->capabilities &
3102 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3103 tcon->use_persistent = true;
3104 else /* persistent handles requested but not supported */ {
3105 cifs_dbg(VFS,
3106 "Persistent handles not supported on share\n");
3107 rc = -EOPNOTSUPP;
3108 goto out_fail;
3109 }
3110 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3111 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3112 && (volume_info->nopersistent == false)) {
3113 cifs_dbg(FYI, "enabling persistent handles\n");
3114 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003115 } else if (volume_info->resilient) {
3116 if (ses->server->vals->protocol_id == 0) {
3117 cifs_dbg(VFS,
3118 "SMB2.1 or later required for resilient handles\n");
3119 rc = -EOPNOTSUPP;
3120 goto out_fail;
3121 }
3122 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003123 }
3124
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003125 /*
3126 * We can have only one retry value for a connection to a share so for
3127 * resources mounted more than once to the same server share the last
3128 * value passed in for the retry flag is used.
3129 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003130 tcon->retry = volume_info->retry;
3131 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003132 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003133 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003134 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003135
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303136 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003137 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303138 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003139
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303140 cifs_fscache_get_super_cookie(tcon);
3141
Jeff Laytond00c28d2010-04-24 07:57:44 -04003142 return tcon;
3143
3144out_fail:
3145 tconInfoFree(tcon);
3146 return ERR_PTR(rc);
3147}
3148
Jeff Layton9d002df2010-10-06 19:51:11 -04003149void
3150cifs_put_tlink(struct tcon_link *tlink)
3151{
3152 if (!tlink || IS_ERR(tlink))
3153 return;
3154
3155 if (!atomic_dec_and_test(&tlink->tl_count) ||
3156 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3157 tlink->tl_time = jiffies;
3158 return;
3159 }
3160
3161 if (!IS_ERR(tlink_tcon(tlink)))
3162 cifs_put_tcon(tlink_tcon(tlink));
3163 kfree(tlink);
3164 return;
3165}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003166
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003167static int
3168compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3169{
3170 struct cifs_sb_info *old = CIFS_SB(sb);
3171 struct cifs_sb_info *new = mnt_data->cifs_sb;
3172
3173 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3174 return 0;
3175
3176 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3177 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3178 return 0;
3179
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003180 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003181 * We want to share sb only if we don't specify an r/wsize or
3182 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003183 */
3184 if (new->wsize && new->wsize < old->wsize)
3185 return 0;
3186
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003187 if (new->rsize && new->rsize < old->rsize)
3188 return 0;
3189
Eric W. Biederman1f682332013-02-06 01:20:20 -08003190 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003191 return 0;
3192
3193 if (old->mnt_file_mode != new->mnt_file_mode ||
3194 old->mnt_dir_mode != new->mnt_dir_mode)
3195 return 0;
3196
3197 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3198 return 0;
3199
3200 if (old->actimeo != new->actimeo)
3201 return 0;
3202
3203 return 1;
3204}
3205
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003206static int
3207match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3208{
3209 struct cifs_sb_info *old = CIFS_SB(sb);
3210 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003211 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3212 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003213
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003214 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003215 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003216 else if (!old_set && !new_set)
3217 return 1;
3218
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003219 return 0;
3220}
3221
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003222int
3223cifs_match_super(struct super_block *sb, void *data)
3224{
3225 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3226 struct smb_vol *volume_info;
3227 struct cifs_sb_info *cifs_sb;
3228 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003229 struct cifs_ses *ses;
3230 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003231 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003232 int rc = 0;
3233
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003234 spin_lock(&cifs_tcp_ses_lock);
3235 cifs_sb = CIFS_SB(sb);
3236 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3237 if (IS_ERR(tlink)) {
3238 spin_unlock(&cifs_tcp_ses_lock);
3239 return rc;
3240 }
3241 tcon = tlink_tcon(tlink);
3242 ses = tcon->ses;
3243 tcp_srv = ses->server;
3244
3245 volume_info = mnt_data->vol;
3246
Jeff Layton9fa114f2012-11-26 11:09:57 -05003247 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003248 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003249 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003250 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003251 rc = 0;
3252 goto out;
3253 }
3254
3255 rc = compare_mount_options(sb, mnt_data);
3256out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003257 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003258 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003259 return rc;
3260}
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003263get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003264 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3265 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 int rc = 0;
3268
Aurelien Aptelb327a712018-01-24 13:46:10 +01003269 if (!ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003270 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003272 *num_referrals = 0;
3273 *referrals = NULL;
3274
Aurelien Aptelb327a712018-01-24 13:46:10 +01003275 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3276 referrals, num_referrals,
3277 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return rc;
3279}
3280
Jeff Layton09e50d52008-07-23 10:11:19 -04003281#ifdef CONFIG_DEBUG_LOCK_ALLOC
3282static struct lock_class_key cifs_key[2];
3283static struct lock_class_key cifs_slock_key[2];
3284
3285static inline void
3286cifs_reclassify_socket4(struct socket *sock)
3287{
3288 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003289 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003290 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3291 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3292}
3293
3294static inline void
3295cifs_reclassify_socket6(struct socket *sock)
3296{
3297 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003298 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003299 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3300 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3301}
3302#else
3303static inline void
3304cifs_reclassify_socket4(struct socket *sock)
3305{
3306}
3307
3308static inline void
3309cifs_reclassify_socket6(struct socket *sock)
3310{
3311}
3312#endif
3313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003315static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
Steve French50c2f752007-07-13 00:33:32 +00003317 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Steve French50c2f752007-07-13 00:33:32 +00003319 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 /* mask a nibble at a time and encode */
3321 target[j] = 'A' + (0x0F & (source[i] >> 4));
3322 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003323 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325
3326}
3327
Ben Greear3eb9a882010-09-01 17:06:02 -07003328static int
3329bind_socket(struct TCP_Server_Info *server)
3330{
3331 int rc = 0;
3332 if (server->srcaddr.ss_family != AF_UNSPEC) {
3333 /* Bind to the specified local IP address */
3334 struct socket *socket = server->ssocket;
3335 rc = socket->ops->bind(socket,
3336 (struct sockaddr *) &server->srcaddr,
3337 sizeof(server->srcaddr));
3338 if (rc < 0) {
3339 struct sockaddr_in *saddr4;
3340 struct sockaddr_in6 *saddr6;
3341 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3342 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3343 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003344 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3345 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003346 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003347 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3348 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003349 }
3350 }
3351 return rc;
3352}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
3354static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003355ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
3357 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003358 /*
3359 * some servers require RFC1001 sessinit before sending
3360 * negprot - BB check reconnection in case where second
3361 * sessinit is sent but no second negprot
3362 */
3363 struct rfc1002_session_packet *ses_init_buf;
3364 struct smb_hdr *smb_buf;
3365 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3366 GFP_KERNEL);
3367 if (ses_init_buf) {
3368 ses_init_buf->trailer.session_req.called_len = 32;
3369
Colin Ian King997152f2016-01-25 16:25:54 +00003370 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003371 rfc1002mangle(ses_init_buf->trailer.
3372 session_req.called_name,
3373 server->server_RFC1001_name,
3374 RFC1001_NAME_LEN_WITH_NULL);
3375 else
3376 rfc1002mangle(ses_init_buf->trailer.
3377 session_req.called_name,
3378 DEFAULT_CIFS_CALLED_NAME,
3379 RFC1001_NAME_LEN_WITH_NULL);
3380
3381 ses_init_buf->trailer.session_req.calling_len = 32;
3382
3383 /*
3384 * calling name ends in null (byte 16) from old smb
3385 * convention.
3386 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003387 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003388 rfc1002mangle(ses_init_buf->trailer.
3389 session_req.calling_name,
3390 server->workstation_RFC1001_name,
3391 RFC1001_NAME_LEN_WITH_NULL);
3392 else
3393 rfc1002mangle(ses_init_buf->trailer.
3394 session_req.calling_name,
3395 "LINUX_CIFS_CLNT",
3396 RFC1001_NAME_LEN_WITH_NULL);
3397
3398 ses_init_buf->trailer.session_req.scope1 = 0;
3399 ses_init_buf->trailer.session_req.scope2 = 0;
3400 smb_buf = (struct smb_hdr *)ses_init_buf;
3401
3402 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003403 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003404 rc = smb_send(server, smb_buf, 0x44);
3405 kfree(ses_init_buf);
3406 /*
3407 * RFC1001 layer in at least one server
3408 * requires very short break before negprot
3409 * presumably because not expecting negprot
3410 * to follow so fast. This is a simple
3411 * solution that works without
3412 * complicating the code and causes no
3413 * significant slowing down on mount
3414 * for everyone else
3415 */
3416 usleep_range(1000, 2000);
3417 }
3418 /*
3419 * else the negprot may still work without this
3420 * even though malloc failed
3421 */
3422
3423 return rc;
3424}
3425
3426static int
3427generic_ip_connect(struct TCP_Server_Info *server)
3428{
3429 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003430 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003431 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003432 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003433 struct sockaddr *saddr;
3434
3435 saddr = (struct sockaddr *) &server->dstaddr;
3436
3437 if (server->dstaddr.ss_family == AF_INET6) {
3438 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3439 slen = sizeof(struct sockaddr_in6);
3440 sfamily = AF_INET6;
3441 } else {
3442 sport = ((struct sockaddr_in *) saddr)->sin_port;
3443 slen = sizeof(struct sockaddr_in);
3444 sfamily = AF_INET;
3445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003447 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003448 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3449 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003451 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003452 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003455
3456 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003457 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003458 server->ssocket = socket;
3459 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003460 if (sfamily == AF_INET6)
3461 cifs_reclassify_socket6(socket);
3462 else
3463 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
3465
Ben Greear3eb9a882010-09-01 17:06:02 -07003466 rc = bind_socket(server);
3467 if (rc < 0)
3468 return rc;
3469
Jeff Laytond5c56052008-12-01 18:42:33 -05003470 /*
3471 * Eventually check for other socket options to change from
3472 * the default. sock_setsockopt not used because it expects
3473 * user space buffer
3474 */
3475 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003476 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003477
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003478 /* make the bufsizes depend on wsize/rsize and max requests */
3479 if (server->noautotune) {
3480 if (socket->sk->sk_sndbuf < (200 * 1024))
3481 socket->sk->sk_sndbuf = 200 * 1024;
3482 if (socket->sk->sk_rcvbuf < (140 * 1024))
3483 socket->sk->sk_rcvbuf = 140 * 1024;
3484 }
3485
Steve French6a5fa2362010-01-01 01:28:43 +00003486 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003487 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003488 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3489 (char *)&val, sizeof(val));
3490 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003491 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3492 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003493 }
3494
Joe Perchesf96637b2013-05-04 22:12:25 -05003495 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003496 socket->sk->sk_sndbuf,
3497 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3498
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003499 rc = socket->ops->connect(socket, saddr, slen, 0);
3500 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003501 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003502 sock_release(socket);
3503 server->ssocket = NULL;
3504 return rc;
3505 }
3506
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003507 if (sport == htons(RFC1001_PORT))
3508 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 return rc;
3511}
3512
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003513static int
3514ip_connect(struct TCP_Server_Info *server)
3515{
Steve French6da97912011-03-13 18:55:55 +00003516 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003517 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3518 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3519
3520 if (server->dstaddr.ss_family == AF_INET6)
3521 sport = &addr6->sin6_port;
3522 else
3523 sport = &addr->sin_port;
3524
3525 if (*sport == 0) {
3526 int rc;
3527
3528 /* try with 445 port at first */
3529 *sport = htons(CIFS_PORT);
3530
3531 rc = generic_ip_connect(server);
3532 if (rc >= 0)
3533 return rc;
3534
3535 /* if it failed, try with 139 port */
3536 *sport = htons(RFC1001_PORT);
3537 }
3538
3539 return generic_ip_connect(server);
3540}
3541
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003542void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003543 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003544{
3545 /* if we are reconnecting then should we check to see if
3546 * any requested capabilities changed locally e.g. via
3547 * remount but we can not do much about it here
3548 * if they have (even if we could detect it by the following)
3549 * Perhaps we could add a backpointer to array of sb from tcon
3550 * or if we change to make all sb to same share the same
3551 * sb as NFS - then we only have one backpointer to sb.
3552 * What if we wanted to mount the server share twice once with
3553 * and once without posixacls or posix paths? */
3554 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003555
Steve Frenchc18c8422007-07-18 23:21:09 +00003556 if (vol_info && vol_info->no_linux_ext) {
3557 tcon->fsUnixInfo.Capability = 0;
3558 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003559 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003560 return;
3561 } else if (vol_info)
3562 tcon->unix_ext = 1; /* Unix Extensions supported */
3563
3564 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003565 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003566 return;
3567 }
Steve French50c2f752007-07-13 00:33:32 +00003568
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003569 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003570 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003571 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003572 /* check for reconnect case in which we do not
3573 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003574 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003575 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003576 originally at mount time */
3577 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3578 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003579 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3580 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003581 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003582 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003583 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003584 cifs_dbg(VFS, "possible reconnect error\n");
3585 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003586 }
Steve French8af18972007-02-14 04:42:51 +00003587 }
Steve French50c2f752007-07-13 00:33:32 +00003588
Steve French6848b732011-05-26 18:38:54 +00003589 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003590 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003591
Steve French8af18972007-02-14 04:42:51 +00003592 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003593 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003594 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003595 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003596 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003597 if (cifs_sb)
3598 cifs_sb->mnt_cifs_flags |=
3599 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003600 }
3601
Steve French75865f8c2007-06-24 18:30:48 +00003602 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003603 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003604 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003605 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003606 if (cifs_sb)
3607 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003608 CIFS_MOUNT_POSIX_PATHS;
3609 }
Steve French50c2f752007-07-13 00:33:32 +00003610
Joe Perchesf96637b2013-05-04 22:12:25 -05003611 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003612#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003613 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003614 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003615 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003616 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003617 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003618 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003619 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003620 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003621 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003622 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003623 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003624 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003625 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003626 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003627 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003628 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003629 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003630 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003631#endif /* CIFS_DEBUG2 */
3632 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003633 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003634 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003635 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003636 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 +00003637
Steve French8af18972007-02-14 04:42:51 +00003638 }
3639 }
3640}
3641
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003642int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003643 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003644{
Jeff Layton2de970f2010-10-06 19:51:12 -04003645 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3646
Al Viro2ced6f62011-06-17 09:20:04 -04003647 spin_lock_init(&cifs_sb->tlink_tree_lock);
3648 cifs_sb->tlink_tree = RB_ROOT;
3649
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003650 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003651 * Temporarily set r/wsize for matching superblock. If we end up using
3652 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003653 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003654 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003655 cifs_sb->wsize = pvolume_info->wsize;
3656
Steve French3b795212008-11-13 19:45:32 +00003657 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3658 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3659 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3660 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003661 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3662 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003663
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303664 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003665 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303666
Steve French3b795212008-11-13 19:45:32 +00003667 if (pvolume_info->noperm)
3668 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3669 if (pvolume_info->setuids)
3670 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003671 if (pvolume_info->setuidfromacl)
3672 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003673 if (pvolume_info->server_ino)
3674 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3675 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003676 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3677 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003678 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3679 if (pvolume_info->no_xattr)
3680 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3681 if (pvolume_info->sfu_emul)
3682 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3683 if (pvolume_info->nobrl)
3684 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003685 if (pvolume_info->nohandlecache)
3686 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003687 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003688 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003689 if (pvolume_info->mand_lock)
3690 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003691 if (pvolume_info->rwpidforward)
3692 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003693 if (pvolume_info->cifs_acl)
3694 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003695 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003696 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003697 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3698 }
3699 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003700 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003701 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3702 }
Steve French3b795212008-11-13 19:45:32 +00003703 if (pvolume_info->override_uid)
3704 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3705 if (pvolume_info->override_gid)
3706 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3707 if (pvolume_info->dynperm)
3708 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303709 if (pvolume_info->fsc)
3710 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003711 if (pvolume_info->multiuser)
3712 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3713 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003714 if (pvolume_info->strict_io)
3715 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003716 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003717 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003718 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3719 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003720 if (pvolume_info->mfsymlinks) {
3721 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003722 /*
3723 * Our SFU ("Services for Unix" emulation does not allow
3724 * creating symlinks but does allow reading existing SFU
3725 * symlinks (it does allow both creating and reading SFU
3726 * style mknod and FIFOs though). When "mfsymlinks" and
3727 * "sfu" are both enabled at the same time, it allows
3728 * reading both types of symlinks, but will only create
3729 * them with mfsymlinks format. This allows better
3730 * Apple compatibility (probably better for Samba too)
3731 * while still recognizing old Windows style symlinks.
3732 */
3733 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003734 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003735 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003736 }
Steve French3b795212008-11-13 19:45:32 +00003737
3738 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003739 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003740
3741 if (pvolume_info->prepath) {
3742 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3743 if (cifs_sb->prepath == NULL)
3744 return -ENOMEM;
3745 }
3746
3747 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003748}
3749
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003750static void
3751cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003752{
Sean Finneyb9468452011-04-11 13:19:32 +00003753 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003754 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003755 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003756 kfree(volume_info->domainname);
3757 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003758 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003759}
3760
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003761void
3762cifs_cleanup_volume_info(struct smb_vol *volume_info)
3763{
3764 if (!volume_info)
3765 return;
3766 cleanup_volume_info_contents(volume_info);
3767 kfree(volume_info);
3768}
3769
3770
Steve French2d6d5892009-04-09 00:36:44 +00003771#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003772/*
3773 * cifs_build_path_to_root returns full path to root when we do not have an
3774 * exiting connection (tcon)
3775 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003776static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003777build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003778 const struct cifs_sb_info *cifs_sb)
3779{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003780 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003781 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003782 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003783
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003784 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003785 if (full_path == NULL)
3786 return ERR_PTR(-ENOMEM);
3787
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003788 strncpy(full_path, vol->UNC, unc_len);
3789 pos = full_path + unc_len;
3790
3791 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003792 *pos = CIFS_DIR_SEP(cifs_sb);
3793 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003794 pos += pplen;
3795 }
3796
3797 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003798 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003799 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003800 return full_path;
3801}
Sean Finneydd613942011-04-11 13:19:30 +00003802
3803/*
3804 * Perform a dfs referral query for a share and (optionally) prefix
3805 *
Sean Finney046462a2011-04-11 13:19:33 +00003806 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3807 * to a string containing updated options for the submount. Otherwise it
3808 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003809 *
3810 * Returns the rc from get_dfs_path to the caller, which can be used to
3811 * determine whether there were referrals.
3812 */
3813static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003814expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003815 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003816 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003817{
3818 int rc;
3819 unsigned int num_referrals = 0;
3820 struct dfs_info3_param *referrals = NULL;
3821 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3822
3823 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3824 if (IS_ERR(full_path))
3825 return PTR_ERR(full_path);
3826
3827 /* For DFS paths, skip the first '\' of the UNC */
3828 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3829
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003830 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003831 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003832
3833 if (!rc && num_referrals > 0) {
3834 char *fake_devname = NULL;
3835
3836 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3837 full_path + 1, referrals,
3838 &fake_devname);
3839
3840 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003841
Sean Finneydd613942011-04-11 13:19:30 +00003842 if (IS_ERR(mdata)) {
3843 rc = PTR_ERR(mdata);
3844 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003845 } else {
3846 cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003847 rc = cifs_setup_volume_info(volume_info, mdata,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003848 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00003849 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003850 kfree(fake_devname);
3851 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003852 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003853 }
3854 kfree(full_path);
3855 return rc;
3856}
Steve French2d6d5892009-04-09 00:36:44 +00003857#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003858
Jeff Layton04db79b2011-07-06 08:10:38 -04003859static int
3860cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003861 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003863 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003864
Steve Frenchc7c137b2018-06-06 17:59:29 -05003865 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04003866 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Jeff Layton7586b762008-12-01 18:41:49 -05003868 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003869 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05003870 kfree(volume_info->username);
3871 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05003872 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05003874 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003876 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003877 /* In userspace mount helper we can get user name from alternate
3878 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003879 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 }
3881
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05003883 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003884 /* load_nls_default cannot return null */
3885 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003887 volume_info->local_nls = load_nls(volume_info->iocharset);
3888 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003889 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003890 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04003891 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
3893 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003894
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003895 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04003896}
3897
3898struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05003899cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04003900{
3901 int rc;
3902 struct smb_vol *volume_info;
3903
Jeff Layton6ee95422012-11-26 11:09:57 -05003904 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04003905 if (!volume_info)
3906 return ERR_PTR(-ENOMEM);
3907
Steve Frenchc7c137b2018-06-06 17:59:29 -05003908 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04003909 if (rc) {
3910 cifs_cleanup_volume_info(volume_info);
3911 volume_info = ERR_PTR(rc);
3912 }
3913
3914 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003915}
3916
Aurelien Aptela6b50582016-05-25 19:59:09 +02003917static int
3918cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3919 unsigned int xid,
3920 struct cifs_tcon *tcon,
3921 struct cifs_sb_info *cifs_sb,
3922 char *full_path)
3923{
3924 int rc;
3925 char *s;
3926 char sep, tmp;
3927
3928 sep = CIFS_DIR_SEP(cifs_sb);
3929 s = full_path;
3930
3931 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3932 while (rc == 0) {
3933 /* skip separators */
3934 while (*s == sep)
3935 s++;
3936 if (!*s)
3937 break;
3938 /* next separator */
3939 while (*s && *s != sep)
3940 s++;
3941
3942 /*
3943 * temporarily null-terminate the path at the end of
3944 * the current component
3945 */
3946 tmp = *s;
3947 *s = 0;
3948 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3949 full_path);
3950 *s = tmp;
3951 }
3952 return rc;
3953}
3954
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003955int
Al Viro2c6292a2011-06-17 09:05:48 -04003956cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003957{
Jeff Layton1daaae82012-03-21 06:30:40 -04003958 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003959 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003960 struct cifs_ses *ses;
Steve French96daf2b2011-05-27 04:34:02 +00003961 struct cifs_tcon *tcon;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003962 struct TCP_Server_Info *server;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003963 char *full_path;
3964 struct tcon_link *tlink;
3965#ifdef CONFIG_CIFS_DFS_UPCALL
3966 int referral_walks_count = 0;
Jeff Layton20547492011-07-09 12:21:07 -04003967#endif
Al Virodd854462011-06-17 08:24:42 -04003968
Jeff Layton20547492011-07-09 12:21:07 -04003969#ifdef CONFIG_CIFS_DFS_UPCALL
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003970try_mount_again:
3971 /* cleanup activities if we're chasing a referral */
3972 if (referral_walks_count) {
3973 if (tcon)
3974 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003975 else if (ses)
3976 cifs_put_smb_ses(ses);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003977
Sachin Prabhu1dfd18d2015-06-16 16:36:17 +01003978 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3979
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003980 free_xid(xid);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003981 }
3982#endif
Jeff Layton1daaae82012-03-21 06:30:40 -04003983 rc = 0;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003984 tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003985 ses = NULL;
3986 server = NULL;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003987 full_path = NULL;
3988 tlink = NULL;
3989
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003990 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Jeff Layton63c038c2008-12-01 18:41:46 -05003992 /* get a reference to a tcp session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003993 server = cifs_get_tcp_session(volume_info);
3994 if (IS_ERR(server)) {
3995 rc = PTR_ERR(server);
Jeff Layton63c038c2008-12-01 18:41:46 -05003996 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
Steve French141891f2016-09-23 00:44:16 -05003998 if ((volume_info->max_credits < 20) ||
3999 (volume_info->max_credits > 60000))
4000 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
4001 else
4002 server->max_credits = volume_info->max_credits;
Jeff Layton36988c72010-04-24 07:57:43 -04004003 /* get a reference to a SMB session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004004 ses = cifs_get_smb_ses(server, volume_info);
4005 if (IS_ERR(ses)) {
4006 rc = PTR_ERR(ses);
4007 ses = NULL;
Jeff Layton36988c72010-04-24 07:57:43 -04004008 goto mount_fail_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
Steve French50c2f752007-07-13 00:33:32 +00004010
Steve Frenchb618f002015-11-03 09:15:03 -06004011 if ((volume_info->persistent == true) && ((ses->server->capabilities &
4012 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
4013 cifs_dbg(VFS, "persistent handles not supported by server\n");
4014 rc = -EOPNOTSUPP;
4015 goto mount_fail_check;
4016 }
Steve French592fafe2015-11-03 10:08:53 -06004017
Jeff Laytond00c28d2010-04-24 07:57:44 -04004018 /* search for existing tcon to this server share */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004019 tcon = cifs_get_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04004020 if (IS_ERR(tcon)) {
4021 rc = PTR_ERR(tcon);
4022 tcon = NULL;
Mark Syms40920c22016-11-29 11:36:46 +00004023 if (rc == -EACCES)
4024 goto mount_fail_check;
4025
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004026 goto remote_path_check;
Jeff Laytond00c28d2010-04-24 07:57:44 -04004027 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004028
Steve Frenchce558b02018-05-31 19:16:54 -05004029 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4030 if (tcon->posix_extensions)
4031 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve Frenchce558b02018-05-31 19:16:54 -05004032
Steve French6848b732011-05-26 18:38:54 +00004033 /* tell server which Unix caps we support */
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004034 if (cap_unix(tcon->ses)) {
Steve French6848b732011-05-26 18:38:54 +00004035 /* reset of caps checks mount to see if unix extensions
4036 disabled for just this mount */
Al Viro2c6292a2011-06-17 09:05:48 -04004037 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
Steve French6848b732011-05-26 18:38:54 +00004038 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4039 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4040 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
4041 rc = -EACCES;
4042 goto mount_fail_check;
4043 }
4044 } else
4045 tcon->unix_ext = 0; /* server does not support them */
4046
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004047 /* do not care if a following call succeed - informational */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004048 if (!tcon->pipe && server->ops->qfs_tcon)
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004049 server->ops->qfs_tcon(xid, tcon);
Steve Frenchd82c2df2008-11-15 00:07:26 +00004050
Pavel Shilovsky24985c52012-09-18 16:20:28 -07004051 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
4052 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04004053
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004054remote_path_check:
Sean Finneyc1508ca2011-04-11 13:19:31 +00004055#ifdef CONFIG_CIFS_DFS_UPCALL
4056 /*
4057 * Perform an unconditional check for whether there are DFS
4058 * referrals for this path without prefix, to provide support
4059 * for DFS referrals from w2k8 servers which don't seem to respond
4060 * with PATH_NOT_COVERED to requests that include the prefix.
4061 * Chase the referral if found, otherwise continue normally.
4062 */
4063 if (referral_walks_count == 0) {
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004064 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
4065 false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004066 if (!refrc) {
4067 referral_walks_count++;
4068 goto try_mount_again;
4069 }
4070 }
4071#endif
4072
Steve Frenchf87d39d2011-05-27 03:50:55 +00004073 /* check if a whole path is not remote */
Jeff Layton70945642011-03-14 13:48:08 -04004074 if (!rc && tcon) {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004075 if (!server->ops->is_path_accessible) {
4076 rc = -ENOSYS;
4077 goto mount_fail_check;
4078 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004079 /*
4080 * cifs_build_path_to_root works only when we have a valid tcon
4081 */
Sachin Prabhu374402a2016-12-15 12:31:19 +05304082 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
4083 tcon->Flags & SMB_SHARE_IS_IN_DFS);
Igor Mammedove4cce942009-02-10 14:10:26 +03004084 if (full_path == NULL) {
4085 rc = -ENOMEM;
4086 goto mount_fail_check;
4087 }
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004088 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4089 full_path);
Jeff Layton03ceace2010-12-06 21:07:33 -05004090 if (rc != 0 && rc != -EREMOTE) {
Igor Mammedove4cce942009-02-10 14:10:26 +03004091 kfree(full_path);
4092 goto mount_fail_check;
4093 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004094
Sachin Prabhud1713562016-09-06 13:22:34 +01004095 if (rc != -EREMOTE) {
4096 rc = cifs_are_all_path_components_accessible(server,
Aurelien Aptela6b50582016-05-25 19:59:09 +02004097 xid, tcon, cifs_sb,
4098 full_path);
Sachin Prabhud1713562016-09-06 13:22:34 +01004099 if (rc != 0) {
4100 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4101 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4102 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4103 rc = 0;
4104 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004105 }
Igor Mammedove4cce942009-02-10 14:10:26 +03004106 kfree(full_path);
4107 }
4108
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004109 /* get referral if needed */
4110 if (rc == -EREMOTE) {
Steve Frenchd036f502009-04-03 03:12:08 +00004111#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004112 if (referral_walks_count > MAX_NESTED_LINKS) {
4113 /*
4114 * BB: when we implement proper loop detection,
4115 * we will remove this check. But now we need it
4116 * to prevent an indefinite loop if 'DFS tree' is
4117 * misconfigured (i.e. has loops).
4118 */
4119 rc = -ELOOP;
4120 goto mount_fail_check;
4121 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004122
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004123 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
Jeff Layton7b91e262009-07-23 15:22:30 -04004124
Sean Finneydd613942011-04-11 13:19:30 +00004125 if (!rc) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004126 referral_walks_count++;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004127 goto try_mount_again;
4128 }
Sean Finneydd613942011-04-11 13:19:30 +00004129 goto mount_fail_check;
Steve Frenchd036f502009-04-03 03:12:08 +00004130#else /* No DFS support, return error on mount */
4131 rc = -EOPNOTSUPP;
4132#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004133 }
4134
Jeff Layton9d002df2010-10-06 19:51:11 -04004135 if (rc)
4136 goto mount_fail_check;
4137
4138 /* now, hang the tcon off of the superblock */
4139 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
4140 if (tlink == NULL) {
4141 rc = -ENOMEM;
4142 goto mount_fail_check;
4143 }
4144
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004145 tlink->tl_uid = ses->linux_uid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004146 tlink->tl_tcon = tcon;
4147 tlink->tl_time = jiffies;
4148 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4149 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4150
Jeff Layton413e6612010-10-28 13:33:38 -04004151 cifs_sb->master_tlink = tlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004152 spin_lock(&cifs_sb->tlink_tree_lock);
4153 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4154 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton413e6612010-10-28 13:33:38 -04004155
Jeff Laytonda472fc2012-03-23 14:40:53 -04004156 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004157 TLINK_IDLE_EXPIRE);
4158
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004159mount_fail_check:
4160 /* on error free sesinfo and tcon struct if needed */
4161 if (rc) {
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004162 /* If find_unc succeeded then rc == 0 so we can not end */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004163 /* up accidentally freeing someone elses tcon struct */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004164 if (tcon)
4165 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004166 else if (ses)
4167 cifs_put_smb_ses(ses);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004168 else
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004169 cifs_put_tcp_session(server, 0);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004170 }
4171
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004172out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004173 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return rc;
4175}
4176
Jeff Layton8d1bca32011-06-11 21:17:10 -04004177/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004178 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004181CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004182 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 const struct nls_table *nls_codepage)
4184{
4185 struct smb_hdr *smb_buffer;
4186 struct smb_hdr *smb_buffer_response;
4187 TCONX_REQ *pSMB;
4188 TCONX_RSP *pSMBr;
4189 unsigned char *bcc_ptr;
4190 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004191 int length;
4192 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
4194 if (ses == NULL)
4195 return -EIO;
4196
4197 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004198 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 smb_buffer_response = smb_buffer;
4202
4203 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4204 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004205
Pavel Shilovsky88257362012-05-23 14:01:59 +04004206 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 smb_buffer->Uid = ses->Suid;
4208 pSMB = (TCONX_REQ *) smb_buffer;
4209 pSMBr = (TCONX_RSP *) smb_buffer_response;
4210
4211 pSMB->AndXCommand = 0xFF;
4212 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004214 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004215 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004216 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004217 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004218 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004219 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004220 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004221 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4222 specified as required (when that support is added to
4223 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004224 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004225 by Samba (not sure whether other servers allow
4226 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004227#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004228 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004229 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004230 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004231 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004232 SECMODE_PW_ENCRYPT ? true : false,
4233 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004234 else
4235#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004236 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004237 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004238 if (rc) {
4239 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4240 __func__, rc);
4241 cifs_buf_release(smb_buffer);
4242 return rc;
4243 }
Steve Frencheeac8042006-01-13 21:34:58 -08004244
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004245 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004246 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004247 /* must align unicode strings */
4248 *bcc_ptr = 0; /* null byte password */
4249 bcc_ptr++;
4250 }
Steve Frencheeac8042006-01-13 21:34:58 -08004251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Jeff Layton38d77c52013-05-26 07:01:00 -04004253 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4255
4256 if (ses->capabilities & CAP_STATUS32) {
4257 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4258 }
4259 if (ses->capabilities & CAP_DFS) {
4260 smb_buffer->Flags2 |= SMBFLG2_DFS;
4261 }
4262 if (ses->capabilities & CAP_UNICODE) {
4263 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4264 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004265 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004266 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004267 (/* server len*/ + 256 /* share len */), nls_codepage);
4268 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 bcc_ptr += 2; /* skip trailing null */
4270 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 strcpy(bcc_ptr, tree);
4272 bcc_ptr += strlen(tree) + 1;
4273 }
4274 strcpy(bcc_ptr, "?????");
4275 bcc_ptr += strlen("?????");
4276 bcc_ptr += 1;
4277 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004278 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4279 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 pSMB->ByteCount = cpu_to_le16(count);
4281
Steve French133672e2007-11-13 22:41:37 +00004282 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004283 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004286 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004287 bool is_unicode;
4288
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004290 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 tcon->tid = smb_buffer_response->Tid;
4292 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004293 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004294 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004295 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4296 is_unicode = true;
4297 else
4298 is_unicode = false;
4299
Jeff Laytoncc20c032009-04-30 07:16:21 -04004300
Steve French50c2f752007-07-13 00:33:32 +00004301 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004302 if (length == 3) {
4303 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4304 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004305 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004306 tcon->ipc = true;
4307 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004308 }
4309 } else if (length == 2) {
4310 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4311 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004312 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004313 }
4314 }
Steve French50c2f752007-07-13 00:33:32 +00004315 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004316 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004317 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004318
4319 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004320 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004321 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004322 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004323 nls_codepage);
4324
Joe Perchesf96637b2013-05-04 22:12:25 -05004325 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004326
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004327 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004328 (smb_buffer_response->WordCount == 7))
4329 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004330 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4331 else
4332 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004333 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 }
4335
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004336 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 return rc;
4338}
4339
Al Viro2e32cf52013-10-03 12:53:37 -04004340static void delayed_free(struct rcu_head *p)
4341{
4342 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4343 unload_nls(sbi->local_nls);
4344 kfree(sbi);
4345}
4346
Al Viro2a9b9952011-06-17 09:27:16 -04004347void
4348cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349{
Jeff Laytonb647c352010-10-28 11:16:44 -04004350 struct rb_root *root = &cifs_sb->tlink_tree;
4351 struct rb_node *node;
4352 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Jeff Layton2de970f2010-10-06 19:51:12 -04004354 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4355
Jeff Laytonb647c352010-10-28 11:16:44 -04004356 spin_lock(&cifs_sb->tlink_tree_lock);
4357 while ((node = rb_first(root))) {
4358 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4359 cifs_get_tlink(tlink);
4360 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4361 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004362
Jeff Laytonb647c352010-10-28 11:16:44 -04004363 spin_unlock(&cifs_sb->tlink_tree_lock);
4364 cifs_put_tlink(tlink);
4365 spin_lock(&cifs_sb->tlink_tree_lock);
4366 }
4367 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004368
Al Virod757d712011-06-17 09:42:43 -04004369 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004370 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004371 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004374int
4375cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
4377 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004378 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004380 if (!server->ops->need_neg || !server->ops->negotiate)
4381 return -ENOSYS;
4382
Jeff Layton198b5682010-04-24 07:57:48 -04004383 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004384 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
Pavel Shilovsky45275782012-05-17 17:53:29 +04004387 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004388
4389 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004390 if (rc == 0) {
4391 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004392 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004393 server->tcpStatus = CifsGood;
4394 else
4395 rc = -EHOSTDOWN;
4396 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 }
Steve French26b994f2008-08-06 05:11:33 +00004398
Jeff Layton198b5682010-04-24 07:57:48 -04004399 return rc;
4400}
Steve French26b994f2008-08-06 05:11:33 +00004401
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004402int
4403cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4404 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004405{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004406 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004407 struct TCP_Server_Info *server = ses->server;
4408
Jeff Layton198b5682010-04-24 07:57:48 -04004409 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004410 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004411 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004412
Joe Perchesf96637b2013-05-04 22:12:25 -05004413 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004414 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004415
Shu Wangf5c4ba82017-09-08 18:48:33 +08004416 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05004417 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08004418 ses->auth_key.response);
4419 kfree(ses->auth_key.response);
4420 ses->auth_key.response = NULL;
4421 ses->auth_key.len = 0;
4422 }
4423
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004424 if (server->ops->sess_setup)
4425 rc = server->ops->sess_setup(xid, ses, nls_info);
4426
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004427 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004428 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 return rc;
4431}
4432
Jeff Layton8a8798a2012-01-17 16:09:15 -05004433static int
4434cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4435{
Jeff Layton3f618222013-06-12 19:52:14 -05004436 vol->sectype = ses->sectype;
4437
4438 /* krb5 is special, since we don't need username or pw */
4439 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004440 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004441
4442 return cifs_set_cifscreds(vol, ses);
4443}
4444
Steve French96daf2b2011-05-27 04:34:02 +00004445static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004446cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004447{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004448 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004449 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4450 struct cifs_ses *ses;
4451 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004452 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004453
4454 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004455 if (vol_info == NULL)
4456 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004457
Jeff Layton9d002df2010-10-06 19:51:11 -04004458 vol_info->local_nls = cifs_sb->local_nls;
4459 vol_info->linux_uid = fsuid;
4460 vol_info->cred_uid = fsuid;
4461 vol_info->UNC = master_tcon->treeName;
4462 vol_info->retry = master_tcon->retry;
4463 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05004464 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04004465 vol_info->local_lease = master_tcon->local_lease;
4466 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004467 vol_info->sectype = master_tcon->ses->sectype;
4468 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004469
Jeff Layton8a8798a2012-01-17 16:09:15 -05004470 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4471 if (rc) {
4472 tcon = ERR_PTR(rc);
4473 goto out;
4474 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004475
4476 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304477 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004478 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304479 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004480
4481 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4482 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004483 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004484 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004485 goto out;
4486 }
4487
4488 tcon = cifs_get_tcon(ses, vol_info);
4489 if (IS_ERR(tcon)) {
4490 cifs_put_smb_ses(ses);
4491 goto out;
4492 }
4493
Steve Frenchce558b02018-05-31 19:16:54 -05004494 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4495 if (tcon->posix_extensions)
4496 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05004497
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004498 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004499 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05004500
Jeff Layton9d002df2010-10-06 19:51:11 -04004501out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004502 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01004503 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004504 kfree(vol_info);
4505
4506 return tcon;
4507}
4508
Steve French96daf2b2011-05-27 04:34:02 +00004509struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004510cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4511{
4512 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4513}
4514
Jeff Laytonb647c352010-10-28 11:16:44 -04004515/* find and return a tlink with given uid */
4516static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004517tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004518{
4519 struct rb_node *node = root->rb_node;
4520 struct tcon_link *tlink;
4521
4522 while (node) {
4523 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4524
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004525 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004526 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004527 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004528 node = node->rb_right;
4529 else
4530 return tlink;
4531 }
4532 return NULL;
4533}
4534
4535/* insert a tcon_link into the tree */
4536static void
4537tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4538{
4539 struct rb_node **new = &(root->rb_node), *parent = NULL;
4540 struct tcon_link *tlink;
4541
4542 while (*new) {
4543 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4544 parent = *new;
4545
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004546 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004547 new = &((*new)->rb_left);
4548 else
4549 new = &((*new)->rb_right);
4550 }
4551
4552 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4553 rb_insert_color(&new_tlink->tl_rbnode, root);
4554}
4555
Jeff Layton9d002df2010-10-06 19:51:11 -04004556/*
4557 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4558 * current task.
4559 *
4560 * If the superblock doesn't refer to a multiuser mount, then just return
4561 * the master tcon for the mount.
4562 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304563 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004564 * exists, then check to see if it's pending construction. If it is then wait
4565 * for construction to complete. Once it's no longer pending, check to see if
4566 * it failed and either return an error or retry construction, depending on
4567 * the timeout.
4568 *
4569 * If one doesn't exist then insert a new tcon_link struct into the tree and
4570 * try to construct a new one.
4571 */
4572struct tcon_link *
4573cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4574{
4575 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004576 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004577 struct tcon_link *tlink, *newtlink;
4578
4579 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4580 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4581
4582 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004583 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004584 if (tlink)
4585 cifs_get_tlink(tlink);
4586 spin_unlock(&cifs_sb->tlink_tree_lock);
4587
4588 if (tlink == NULL) {
4589 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4590 if (newtlink == NULL)
4591 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004592 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004593 newtlink->tl_tcon = ERR_PTR(-EACCES);
4594 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4595 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4596 cifs_get_tlink(newtlink);
4597
Jeff Layton9d002df2010-10-06 19:51:11 -04004598 spin_lock(&cifs_sb->tlink_tree_lock);
4599 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004600 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004601 if (tlink) {
4602 cifs_get_tlink(tlink);
4603 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004604 kfree(newtlink);
4605 goto wait_for_construction;
4606 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004607 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004608 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4609 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004610 } else {
4611wait_for_construction:
4612 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004613 TASK_INTERRUPTIBLE);
4614 if (ret) {
4615 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004616 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004617 }
4618
4619 /* if it's good, return it */
4620 if (!IS_ERR(tlink->tl_tcon))
4621 return tlink;
4622
4623 /* return error if we tried this already recently */
4624 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4625 cifs_put_tlink(tlink);
4626 return ERR_PTR(-EACCES);
4627 }
4628
4629 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4630 goto wait_for_construction;
4631 }
4632
4633 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4634 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4635 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4636
4637 if (IS_ERR(tlink->tl_tcon)) {
4638 cifs_put_tlink(tlink);
4639 return ERR_PTR(-EACCES);
4640 }
4641
4642 return tlink;
4643}
Jeff Layton2de970f2010-10-06 19:51:12 -04004644
4645/*
4646 * periodic workqueue job that scans tcon_tree for a superblock and closes
4647 * out tcons.
4648 */
4649static void
4650cifs_prune_tlinks(struct work_struct *work)
4651{
4652 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4653 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004654 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004655 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004656 struct rb_node *tmp;
4657 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004658
Jeff Laytonb647c352010-10-28 11:16:44 -04004659 /*
4660 * Because we drop the spinlock in the loop in order to put the tlink
4661 * it's not guarded against removal of links from the tree. The only
4662 * places that remove entries from the tree are this function and
4663 * umounts. Because this function is non-reentrant and is canceled
4664 * before umount can proceed, this is safe.
4665 */
4666 spin_lock(&cifs_sb->tlink_tree_lock);
4667 node = rb_first(root);
4668 while (node != NULL) {
4669 tmp = node;
4670 node = rb_next(tmp);
4671 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4672
4673 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4674 atomic_read(&tlink->tl_count) != 0 ||
4675 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4676 continue;
4677
4678 cifs_get_tlink(tlink);
4679 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4680 rb_erase(tmp, root);
4681
Jeff Layton2de970f2010-10-06 19:51:12 -04004682 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004683 cifs_put_tlink(tlink);
4684 spin_lock(&cifs_sb->tlink_tree_lock);
4685 }
4686 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004687
Jeff Laytonda472fc2012-03-23 14:40:53 -04004688 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004689 TLINK_IDLE_EXPIRE);
4690}