blob: 975f800b9dd4d169dece68e33a1c3bb0a05859e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/connect.c
3 *
Steve French1080ef72011-02-24 18:07:19 +00004 * Copyright (C) International Business Machines Corp., 2002,2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
Steve Frenchfb8c4b12007-07-10 01:16:18 +000019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010024#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/list.h>
26#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/pagemap.h>
29#include <linux/ctype.h>
30#include <linux/utsname.h>
31#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070032#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070033#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000034#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070035#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040037#include <linux/namei.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020038#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040041#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040042#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050043#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000044#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040045#include <linux/parser.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060046#include <linux/bvec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "cifspdu.h"
48#include "cifsglob.h"
49#include "cifsproto.h"
50#include "cifs_unicode.h"
51#include "cifs_debug.h"
52#include "cifs_fs_sb.h"
Paulo Alcantarac0be6242018-11-20 15:16:36 -020053#include "dns_resolve.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "ntlmssp.h"
55#include "nterr.h"
56#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053057#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070058#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070059#include "smbdirect.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050062extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jeff Layton2de970f2010-10-06 19:51:12 -040064/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040065#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040066#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040067
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040068enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040069 /* Mount options that take no arguments */
70 Opt_user_xattr, Opt_nouser_xattr,
71 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040072 Opt_forcegid, Opt_noforcegid,
Steve French2e96c932019-09-11 21:46:20 -050073 Opt_noblocksend, Opt_noautotune, Opt_nolease,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040074 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050075 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040076 Opt_mapchars, Opt_nomapchars, Opt_sfu,
77 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
Steve Frenchb3266142018-05-20 23:41:10 -050078 Opt_noposixpaths, Opt_nounix, Opt_unix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040079 Opt_nocase,
80 Opt_brl, Opt_nobrl,
Steve French3d4ef9a2018-04-25 22:19:09 -050081 Opt_handlecache, Opt_nohandlecache,
Steve French95932652016-09-23 01:36:34 -050082 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040083 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
84 Opt_nohard, Opt_nosoft,
85 Opt_nointr, Opt_intr,
86 Opt_nostrictsync, Opt_strictsync,
87 Opt_serverino, Opt_noserverino,
88 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
89 Opt_acl, Opt_noacl, Opt_locallease,
Jeff Layton1b359202012-09-19 15:20:27 -070090 Opt_sign, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040091 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040092 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050093 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060094 Opt_resilient, Opt_noresilient,
Long Li8339dd32017-11-07 01:54:55 -070095 Opt_domainauto, Opt_rdma,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040096
97 /* Mount options which take numeric value */
98 Opt_backupuid, Opt_backupgid, Opt_uid,
99 Opt_cruid, Opt_gid, Opt_file_mode,
100 Opt_dirmode, Opt_port,
101 Opt_rsize, Opt_wsize, Opt_actimeo,
Steve French141891f2016-09-23 00:44:16 -0500102 Opt_echo_interval, Opt_max_credits,
Steve French8b217fe2016-11-11 22:36:20 -0600103 Opt_snapshot,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400104
105 /* Mount options which take string value */
106 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400107 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400108 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400109 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400110
111 /* Mount options to be ignored */
112 Opt_ignore,
113
114 /* Options which could be blank */
115 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100116 Opt_blank_user,
117 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400118
119 Opt_err
120};
121
122static const match_table_t cifs_mount_option_tokens = {
123
124 { Opt_user_xattr, "user_xattr" },
125 { Opt_nouser_xattr, "nouser_xattr" },
126 { Opt_forceuid, "forceuid" },
127 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400128 { Opt_forcegid, "forcegid" },
129 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400130 { Opt_noblocksend, "noblocksend" },
131 { Opt_noautotune, "noautotune" },
Steve French2e96c932019-09-11 21:46:20 -0500132 { Opt_nolease, "nolease" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400133 { Opt_hard, "hard" },
134 { Opt_soft, "soft" },
135 { Opt_perm, "perm" },
136 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500137 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400138 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500139 { Opt_mapposix, "mapposix" }, /* SFM style */
140 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400141 { Opt_sfu, "sfu" },
142 { Opt_nosfu, "nosfu" },
143 { Opt_nodfs, "nodfs" },
144 { Opt_posixpaths, "posixpaths" },
145 { Opt_noposixpaths, "noposixpaths" },
146 { Opt_nounix, "nounix" },
147 { Opt_nounix, "nolinux" },
Steve Frenchb3266142018-05-20 23:41:10 -0500148 { Opt_nounix, "noposix" },
149 { Opt_unix, "unix" },
150 { Opt_unix, "linux" },
151 { Opt_unix, "posix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400152 { Opt_nocase, "nocase" },
153 { Opt_nocase, "ignorecase" },
154 { Opt_brl, "brl" },
155 { Opt_nobrl, "nobrl" },
Steve French3d4ef9a2018-04-25 22:19:09 -0500156 { Opt_handlecache, "handlecache" },
157 { Opt_nohandlecache, "nohandlecache" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400158 { Opt_nobrl, "nolock" },
159 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400160 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400161 { Opt_setuids, "setuids" },
162 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500163 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400164 { Opt_dynperm, "dynperm" },
165 { Opt_nodynperm, "nodynperm" },
166 { Opt_nohard, "nohard" },
167 { Opt_nosoft, "nosoft" },
168 { Opt_nointr, "nointr" },
169 { Opt_intr, "intr" },
170 { Opt_nostrictsync, "nostrictsync" },
171 { Opt_strictsync, "strictsync" },
172 { Opt_serverino, "serverino" },
173 { Opt_noserverino, "noserverino" },
174 { Opt_rwpidforward, "rwpidforward" },
175 { Opt_cifsacl, "cifsacl" },
176 { Opt_nocifsacl, "nocifsacl" },
177 { Opt_acl, "acl" },
178 { Opt_noacl, "noacl" },
179 { Opt_locallease, "locallease" },
180 { Opt_sign, "sign" },
181 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400182 { Opt_noac, "noac" },
183 { Opt_fsc, "fsc" },
184 { Opt_mfsymlinks, "mfsymlinks" },
185 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400186 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400187 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500188 { Opt_persistent, "persistenthandles"},
189 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600190 { Opt_resilient, "resilienthandles"},
191 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530192 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700193 { Opt_rdma, "rdma"},
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400194
195 { Opt_backupuid, "backupuid=%s" },
196 { Opt_backupgid, "backupgid=%s" },
197 { Opt_uid, "uid=%s" },
198 { Opt_cruid, "cruid=%s" },
199 { Opt_gid, "gid=%s" },
200 { Opt_file_mode, "file_mode=%s" },
201 { Opt_dirmode, "dirmode=%s" },
202 { Opt_dirmode, "dir_mode=%s" },
203 { Opt_port, "port=%s" },
204 { Opt_rsize, "rsize=%s" },
205 { Opt_wsize, "wsize=%s" },
206 { Opt_actimeo, "actimeo=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600207 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500208 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600209 { Opt_snapshot, "snapshot=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400210
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100211 { Opt_blank_user, "user=" },
212 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400213 { Opt_user, "user=%s" },
214 { Opt_user, "username=%s" },
215 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100216 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400217 { Opt_pass, "pass=%s" },
218 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100219 { Opt_blank_ip, "ip=" },
220 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400221 { Opt_ip, "ip=%s" },
222 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400223 { Opt_ignore, "unc=%s" },
224 { Opt_ignore, "target=%s" },
225 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400226 { Opt_domain, "dom=%s" },
227 { Opt_domain, "domain=%s" },
228 { Opt_domain, "workgroup=%s" },
229 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400230 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400231 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400232 { Opt_netbiosname, "netbiosname=%s" },
233 { Opt_servern, "servern=%s" },
234 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400235 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400236 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400237 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400238
239 { Opt_ignore, "cred" },
240 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400241 { Opt_ignore, "cred=%s" },
242 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400243 { Opt_ignore, "guest" },
244 { Opt_ignore, "rw" },
245 { Opt_ignore, "ro" },
246 { Opt_ignore, "suid" },
247 { Opt_ignore, "nosuid" },
248 { Opt_ignore, "exec" },
249 { Opt_ignore, "noexec" },
250 { Opt_ignore, "nodev" },
251 { Opt_ignore, "noauto" },
252 { Opt_ignore, "dev" },
253 { Opt_ignore, "mand" },
254 { Opt_ignore, "nomand" },
255 { Opt_ignore, "_netdev" },
256
257 { Opt_err, NULL }
258};
259
260enum {
261 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
262 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400263 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
264 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400265 Opt_sec_none,
266
267 Opt_sec_err
268};
269
270static const match_table_t cifs_secflavor_tokens = {
271 { Opt_sec_krb5, "krb5" },
272 { Opt_sec_krb5i, "krb5i" },
273 { Opt_sec_krb5p, "krb5p" },
274 { Opt_sec_ntlmsspi, "ntlmsspi" },
275 { Opt_sec_ntlmssp, "ntlmssp" },
276 { Opt_ntlm, "ntlm" },
277 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400278 { Opt_sec_ntlmv2, "nontlm" },
279 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400280 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400281 { Opt_sec_lanman, "lanman" },
282 { Opt_sec_none, "none" },
283
284 { Opt_sec_err, NULL }
285};
286
Jeff Layton15b6a472012-05-16 07:50:15 -0400287/* cache flavors */
288enum {
289 Opt_cache_loose,
290 Opt_cache_strict,
291 Opt_cache_none,
292 Opt_cache_err
293};
294
295static const match_table_t cifs_cacheflavor_tokens = {
296 { Opt_cache_loose, "loose" },
297 { Opt_cache_strict, "strict" },
298 { Opt_cache_none, "none" },
299 { Opt_cache_err, NULL }
300};
301
Jeff Layton23db65f2012-05-15 12:20:51 -0400302static const match_table_t cifs_smb_version_tokens = {
303 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600304 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000305 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500306 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500307 { Smb_302, SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600308 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500309 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French9764c022017-09-17 10:41:35 -0500310 { Smb_3any, SMB3ANY_VERSION_STRING },
311 { Smb_default, SMBDEFAULT_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600312 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400313};
314
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300315static int ip_connect(struct TCP_Server_Info *server);
316static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400317static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400318static void cifs_prune_tlinks(struct work_struct *work);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -0400319static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -0500320 const char *devname, bool is_smb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Jeff Laytond5c56052008-12-01 18:42:33 -0500322/*
Paulo Alcantarac0be6242018-11-20 15:16:36 -0200323 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
324 * get their ip addresses changed at some point.
325 *
326 * This should be called with server->srv_mutex held.
327 */
328#ifdef CONFIG_CIFS_DFS_UPCALL
329static int reconn_set_ipaddr(struct TCP_Server_Info *server)
330{
331 int rc;
332 int len;
333 char *unc, *ipaddr = NULL;
334
335 if (!server->hostname)
336 return -EINVAL;
337
338 len = strlen(server->hostname) + 3;
339
340 unc = kmalloc(len, GFP_KERNEL);
341 if (!unc) {
342 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
343 return -ENOMEM;
344 }
345 snprintf(unc, len, "\\\\%s", server->hostname);
346
347 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
348 kfree(unc);
349
350 if (rc < 0) {
351 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
352 __func__, server->hostname, rc);
353 return rc;
354 }
355
356 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
357 strlen(ipaddr));
358 kfree(ipaddr);
359
360 return !rc ? -1 : 0;
361}
362#else
363static inline int reconn_set_ipaddr(struct TCP_Server_Info *server)
364{
365 return 0;
366}
367#endif
368
369/*
Jeff Laytond5c56052008-12-01 18:42:33 -0500370 * cifs tcp session reconnection
371 *
372 * mark tcp session as reconnecting so temporarily locked
373 * mark all smb sessions as reconnecting for tcp session
374 * reconnect tcp session
375 * wake up waiters on reconnection? - (not needed currently)
376 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400377int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378cifs_reconnect(struct TCP_Server_Info *server)
379{
380 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500381 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000382 struct cifs_ses *ses;
383 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000384 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400385 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000388 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000389 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 next time through the loop */
391 spin_unlock(&GlobalMid_Lock);
392 return rc;
393 } else
394 server->tcpStatus = CifsNeedReconnect;
395 spin_unlock(&GlobalMid_Lock);
396 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400397 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Joe Perchesf96637b2013-05-04 22:12:25 -0500399 cifs_dbg(FYI, "Reconnecting tcp session\n");
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500400 trace_smb3_reconnect(server->CurrentMid, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* before reconnecting the tcp session, mark the smb session (uid)
403 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500404 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
405 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530406 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500407 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000408 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500409 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500410 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000411 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500412 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100414 if (ses->tcon_ipc)
415 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530417 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500420 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500421 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000422 if (server->ssocket) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500423 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
424 server->ssocket->state, server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800425 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesf96637b2013-05-04 22:12:25 -0500426 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
427 server->ssocket->state, server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 sock_release(server->ssocket);
429 server->ssocket = NULL;
430 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500431 server->sequence_number = 0;
432 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500433 kfree(server->session_key.response);
434 server->session_key.response = NULL;
435 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000436 server->lstrp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500438 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400439 INIT_LIST_HEAD(&retry_list);
Joe Perchesf96637b2013-05-04 22:12:25 -0500440 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500442 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
443 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400444 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
445 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400446 list_move(&mid_entry->qhead, &retry_list);
447 }
448 spin_unlock(&GlobalMid_Lock);
Rabin Vincent820962d2015-12-23 07:32:41 +0100449 mutex_unlock(&server->srv_mutex);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400450
Joe Perchesf96637b2013-05-04 22:12:25 -0500451 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400452 list_for_each_safe(tmp, tmp2, &retry_list) {
453 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500454 list_del_init(&mid_entry->qhead);
455 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400458 do {
Steve French6c3d8902006-07-31 22:46:20 +0000459 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300460
461 /* we should try only the port we connected to before */
Jeff Layton73e216a2013-09-05 08:38:10 -0400462 mutex_lock(&server->srv_mutex);
Long Li781a8052017-11-22 17:38:36 -0700463 if (cifs_rdma_enabled(server))
464 rc = smbd_reconnect(server);
465 else
466 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000467 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500468 cifs_dbg(FYI, "reconnect error %d\n", rc);
Paulo Alcantarac0be6242018-11-20 15:16:36 -0200469 rc = reconn_set_ipaddr(server);
470 if (rc) {
471 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
472 __func__, rc);
473 }
Federico Sauter4afe2602015-03-17 17:45:28 +0100474 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700475 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else {
477 atomic_inc(&tcpSesReconnectCount);
478 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000479 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000480 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000481 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100482 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400484 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500485
Sachin Prabhub8c60012016-10-20 19:52:24 -0400486 if (server->tcpStatus == CifsNeedNegotiate)
487 mod_delayed_work(cifsiod_wq, &server->echo, 0);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return rc;
490}
491
Jeff Laytonc74093b2011-01-11 07:24:23 -0500492static void
493cifs_echo_request(struct work_struct *work)
494{
495 int rc;
496 struct TCP_Server_Info *server = container_of(work,
497 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400498 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500499
Jeff Layton247ec9b2011-02-04 17:09:50 -0500500 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400501 * If we need to renegotiate, set echo interval to zero to
502 * immediately call echo service where we can renegotiate.
503 */
504 if (server->tcpStatus == CifsNeedNegotiate)
505 echo_interval = 0;
506 else
507 echo_interval = server->echo_interval;
508
509 /*
510 * We cannot send an echo if it is disabled.
511 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500512 */
Steve French4fcd1812016-06-22 20:12:05 -0500513
514 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400515 server->tcpStatus == CifsExiting ||
516 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400517 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600518 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500519 goto requeue_echo;
520
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400521 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500522 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500523 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
524 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500525
526requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400527 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500528}
529
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400530static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400531allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400532{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400533 if (!server->bigbuf) {
534 server->bigbuf = (char *)cifs_buf_get();
535 if (!server->bigbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500536 cifs_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400537 msleep(3000);
538 /* retry will check if exiting */
539 return false;
540 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400541 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400542 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400543 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400544 }
545
Jeff Layton2a37ef92011-10-19 15:29:23 -0400546 if (!server->smallbuf) {
547 server->smallbuf = (char *)cifs_small_buf_get();
548 if (!server->smallbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500549 cifs_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400550 msleep(1000);
551 /* retry will check if exiting */
552 return false;
553 }
554 /* beginning of smb buffer is cleared in our buf_get */
555 } else {
556 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400557 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400558 }
559
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400560 return true;
561}
562
Jeff Laytonba749e62011-10-11 06:41:32 -0400563static bool
564server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400565{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300566 /*
Ronnie Sahlbergd29fbf62019-07-06 06:52:46 +1000567 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300568 * situations right:
569 * 1s client sends a normal SMB request
Ronnie Sahlbergd29fbf62019-07-06 06:52:46 +1000570 * 3s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300571 * 30s echo workqueue job pops, and decides we got a response recently
572 * and don't need to send another
573 * ...
574 * 65s kernel_recvmsg times out, and we see that we haven't gotten
575 * a response in >60s.
576 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200577 if ((server->tcpStatus == CifsGood ||
578 server->tcpStatus == CifsNeedNegotiate) &&
Ronnie Sahlbergd29fbf62019-07-06 06:52:46 +1000579 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600580 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
Ronnie Sahlbergd29fbf62019-07-06 06:52:46 +1000581 server->hostname, (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400582 cifs_reconnect(server);
583 wake_up(&server->response_q);
584 return true;
585 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400586
Jeff Laytonba749e62011-10-11 06:41:32 -0400587 return false;
588}
589
Pavel Shilovsky779c65b2019-01-18 17:25:36 -0800590static inline bool
591zero_credits(struct TCP_Server_Info *server)
592{
593 int val;
594
595 spin_lock(&server->req_lock);
596 val = server->credits + server->echo_credits + server->oplock_credits;
597 if (server->in_flight == 0 && val == 0) {
598 spin_unlock(&server->req_lock);
599 return true;
600 }
601 spin_unlock(&server->req_lock);
602 return false;
603}
604
Al Viro71335662016-01-09 19:54:50 -0500605static int
606cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400607{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400608 int length = 0;
609 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400610
Al Viro71335662016-01-09 19:54:50 -0500611 smb_msg->msg_control = NULL;
612 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400613
Al Viro71335662016-01-09 19:54:50 -0500614 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500615 try_to_freeze();
616
Pavel Shilovsky779c65b2019-01-18 17:25:36 -0800617 /* reconnect if no credits and no requests in flight */
618 if (zero_credits(server)) {
619 cifs_reconnect(server);
620 return -ECONNABORTED;
621 }
622
Al Viro71335662016-01-09 19:54:50 -0500623 if (server_unresponsive(server))
624 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700625 if (cifs_rdma_enabled(server) && server->smbd_conn)
626 length = smbd_recv(server->smbd_conn, smb_msg);
627 else
628 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500629
630 if (server->tcpStatus == CifsExiting)
631 return -ESHUTDOWN;
632
633 if (server->tcpStatus == CifsNeedReconnect) {
634 cifs_reconnect(server);
635 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400636 }
637
Al Viro71335662016-01-09 19:54:50 -0500638 if (length == -ERESTARTSYS ||
639 length == -EAGAIN ||
640 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400641 /*
642 * Minimum sleep to prevent looping, allowing socket
643 * to clear and app threads to set tcpStatus
644 * CifsNeedReconnect if server hung.
645 */
646 usleep_range(1000, 2000);
647 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400648 continue;
Al Viro71335662016-01-09 19:54:50 -0500649 }
650
651 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500652 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400653 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500654 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400655 }
656 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400657 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400658}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400659
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400660int
661cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
662 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400663{
Al Viro71335662016-01-09 19:54:50 -0500664 struct msghdr smb_msg;
665 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
666 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400667
Al Viro71335662016-01-09 19:54:50 -0500668 return cifs_readv_from_socket(server, &smb_msg);
669}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400670
Al Viro71335662016-01-09 19:54:50 -0500671int
672cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700673 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500674{
675 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700676 struct bio_vec bv = {
677 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
Al Viro71335662016-01-09 19:54:50 -0500678 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
679 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400680}
681
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400682static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400683is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400684{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400685 /*
686 * The first byte big endian of the length field,
687 * is actually not part of the length but the type
688 * with the most common, zero, as regular data.
689 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400690 switch (type) {
691 case RFC1002_SESSION_MESSAGE:
692 /* Regular SMB response */
693 return true;
694 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500695 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400696 break;
697 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500698 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400699 break;
700 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400701 /*
702 * We get this from Windows 98 instead of an error on
703 * SMB negprot response.
704 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500705 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400706 /* give server a second to clean up */
707 msleep(1000);
708 /*
709 * Always try 445 first on reconnect since we get NACK
710 * on some if we ever connected to port 139 (the NACK
711 * is since we do not begin with RFC1001 session
712 * initialize frame).
713 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400714 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400715 cifs_reconnect(server);
716 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400717 break;
718 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500719 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400720 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400721 }
722
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400723 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400724}
725
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400726void
727dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400728{
729#ifdef CONFIG_CIFS_STATS2
730 mid->when_received = jiffies;
731#endif
732 spin_lock(&GlobalMid_Lock);
733 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400734 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400735 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400736 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000737 /*
738 * Trying to handle/dequeue a mid after the send_recv()
739 * function has finished processing it is a bug.
740 */
741 if (mid->mid_flags & MID_DELETED)
742 printk_once(KERN_WARNING
743 "trying to dequeue a deleted mid\n");
744 else
745 list_del_init(&mid->qhead);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400746 spin_unlock(&GlobalMid_Lock);
747}
748
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400749static void
750handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400751 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400752{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400753 if (server->ops->check_trans2 &&
754 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400755 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400756 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400757 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400758 /* Was previous buf put in mpx struct for multi-rsp? */
759 if (!mid->multiRsp) {
760 /* smb buffer will be freed by user thread */
761 if (server->large_buf)
762 server->bigbuf = NULL;
763 else
764 server->smallbuf = NULL;
765 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400766 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400767}
768
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400769static void clean_demultiplex_info(struct TCP_Server_Info *server)
770{
771 int length;
772
773 /* take it off the list, if it's not already */
774 spin_lock(&cifs_tcp_ses_lock);
775 list_del_init(&server->tcp_ses_list);
776 spin_unlock(&cifs_tcp_ses_lock);
777
778 spin_lock(&GlobalMid_Lock);
779 server->tcpStatus = CifsExiting;
780 spin_unlock(&GlobalMid_Lock);
781 wake_up_all(&server->response_q);
782
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400783 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300784 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400785 if (server->credits <= 0)
786 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300787 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400788 /*
789 * Although there should not be any requests blocked on this queue it
790 * can not hurt to be paranoid and try to wake up requests that may
791 * haven been blocked when more than 50 at time were on the wire to the
792 * same server - they now will see the session is in exit state and get
793 * out of SendReceive.
794 */
795 wake_up_all(&server->request_q);
796 /* give those requests time to exit */
797 msleep(125);
Long Libce9ce72017-11-22 17:38:38 -0700798 if (cifs_rdma_enabled(server) && server->smbd_conn) {
799 smbd_destroy(server->smbd_conn);
800 server->smbd_conn = NULL;
801 }
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400802 if (server->ssocket) {
803 sock_release(server->ssocket);
804 server->ssocket = NULL;
805 }
806
807 if (!list_empty(&server->pending_mid_q)) {
808 struct list_head dispose_list;
809 struct mid_q_entry *mid_entry;
810 struct list_head *tmp, *tmp2;
811
812 INIT_LIST_HEAD(&dispose_list);
813 spin_lock(&GlobalMid_Lock);
814 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
815 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500816 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400817 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400818 list_move(&mid_entry->qhead, &dispose_list);
819 }
820 spin_unlock(&GlobalMid_Lock);
821
822 /* now walk dispose list and issue callbacks */
823 list_for_each_safe(tmp, tmp2, &dispose_list) {
824 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500825 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400826 list_del_init(&mid_entry->qhead);
827 mid_entry->callback(mid_entry);
828 }
829 /* 1/8th of sec is more than enough time for them to exit */
830 msleep(125);
831 }
832
833 if (!list_empty(&server->pending_mid_q)) {
834 /*
835 * mpx threads have not exited yet give them at least the smb
836 * send timeout time for long ops.
837 *
838 * Due to delays on oplock break requests, we need to wait at
839 * least 45 seconds before giving up on a request getting a
840 * response and going ahead and killing cifsd.
841 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500842 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400843 msleep(46000);
844 /*
845 * If threads still have not exited they are probably never
846 * coming home not much else we can do but free the memory.
847 */
848 }
849
850 kfree(server->hostname);
851 kfree(server);
852
853 length = atomic_dec_return(&tcpSesAllocCount);
854 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700855 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400856}
857
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400858static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400859standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
860{
861 int length;
862 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000863 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400864
865 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100866 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
867 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500868 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400869 cifs_reconnect(server);
870 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400871 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400872 }
873
874 /* switch to large buffer if too big for a small one */
875 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
876 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400877 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400878 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400879 }
880
881 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400882 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100883 pdu_length - HEADER_SIZE(server) + 1
884 + server->vals->header_preamble_size);
885
Jeff Laytone9097ab2011-10-19 15:29:40 -0400886 if (length < 0)
887 return length;
888 server->total_read += length;
889
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400890 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400891
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800892 return cifs_handle_standard(server, mid);
893}
894
895int
896cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
897{
898 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
899 int length;
900
Jeff Laytone9097ab2011-10-19 15:29:40 -0400901 /*
902 * We know that we received enough to get to the MID as we
903 * checked the pdu_length earlier. Now check to see
904 * if the rest of the header is OK. We borrow the length
905 * var for the rest of the loop to avoid a new stack var.
906 *
907 * 48 bytes is enough to display the header and a little bit
908 * into the payload for debugging purposes.
909 */
Steve French373512e2015-12-18 13:05:30 -0600910 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400911 if (length != 0)
912 cifs_dump_mem("Bad SMB: ", buf,
913 min_t(unsigned int, server->total_read, 48));
914
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700915 if (server->ops->is_session_expired &&
916 server->ops->is_session_expired(buf)) {
917 cifs_reconnect(server);
918 wake_up(&server->response_q);
919 return -1;
920 }
921
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700922 if (server->ops->is_status_pending &&
923 server->ops->is_status_pending(buf, server, length))
924 return -1;
925
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500926 if (!mid)
927 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400928
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400929 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500930 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400931}
932
Ronnie Sahlbergb73132b2019-02-05 12:56:44 +1000933static void
934smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
935{
936 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
937
938 /*
939 * SMB1 does not use credits.
940 */
941 if (server->vals->header_preamble_size)
942 return;
943
944 if (shdr->CreditRequest) {
945 spin_lock(&server->req_lock);
946 server->credits += le16_to_cpu(shdr->CreditRequest);
947 spin_unlock(&server->req_lock);
948 wake_up(&server->request_q);
949 }
950}
951
952
Jeff Laytone9097ab2011-10-19 15:29:40 -0400953static int
Al Viro7c97c202011-06-21 08:51:28 -0400954cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000956 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400957 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400958 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000959 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400960 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500961 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000962 struct mid_q_entry *mids[MAX_COMPOUND];
963 char *bufs[MAX_COMPOUND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500966 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400967
968 length = atomic_inc_return(&tcpSesAllocCount);
969 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700970 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700972 set_freezable();
Eric W. Biederman6db0e282019-08-16 12:33:54 -0500973 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +0000974 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700975 if (try_to_freeze())
976 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700977
Jeff Layton2a37ef92011-10-19 15:29:23 -0400978 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400979 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700980
Jeff Layton2a37ef92011-10-19 15:29:23 -0400981 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400982 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000983 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000984
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400985 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400986 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000987 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000988
989 if (server->vals->header_preamble_size == 0)
990 server->total_read = 0;
991 else
992 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700993
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400994 /*
995 * The right amount was read from socket - 4 bytes,
996 * so we can now interpret the length field.
997 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400998 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700999
Joe Perchesf96637b2013-05-04 22:12:25 -05001000 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -04001001 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001002 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001003next_pdu:
1004 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -07001005
Jeff Layton89482a52011-10-19 15:28:57 -04001006 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001007 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001008 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001009 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001010 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001011 cifs_reconnect(server);
1012 wake_up(&server->response_q);
1013 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -07001014 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001015
Jeff Layton89482a52011-10-19 15:28:57 -04001016 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001017 length = cifs_read_from_socket(server,
1018 buf + server->vals->header_preamble_size,
1019 HEADER_SIZE(server) - 1
1020 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001021 if (length < 0)
1022 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001023 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001024
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001025 if (server->ops->next_header) {
1026 next_offset = server->ops->next_header(buf);
1027 if (next_offset)
1028 server->pdu_size = next_offset;
1029 }
1030
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001031 memset(mids, 0, sizeof(mids));
1032 memset(bufs, 0, sizeof(bufs));
1033 num_mids = 0;
1034
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001035 if (server->ops->is_transform_hdr &&
1036 server->ops->receive_transform &&
1037 server->ops->is_transform_hdr(buf)) {
1038 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001039 mids,
1040 bufs,
1041 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001042 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001043 mids[0] = server->ops->find_mid(server, buf);
1044 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001045 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001046
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001047 if (!mids[0] || !mids[0]->receive)
1048 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001049 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001050 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001051 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001052
Lars Persson696e4202018-06-25 14:05:25 +02001053 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001054 for (i = 0; i < num_mids; i++)
1055 if (mids[i])
1056 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001057 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001058 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001059
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001060 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -04001061 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -07001062
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001063
Steve Frenchfda35942011-01-20 18:06:34 +00001064 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001065
1066 for (i = 0; i < num_mids; i++) {
1067 if (mids[i] != NULL) {
1068 mids[i]->resp_buf_size = server->pdu_size;
1069 if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
1070 mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
1071 server->ops->handle_cancelled_mid)
1072 server->ops->handle_cancelled_mid(
1073 mids[i]->resp_buf,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001074 server);
1075
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001076 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1077 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001078
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001079 cifs_mid_q_entry_release(mids[i]);
1080 } else if (server->ops->is_oplock_break &&
1081 server->ops->is_oplock_break(bufs[i],
1082 server)) {
Ronnie Sahlbergb73132b2019-02-05 12:56:44 +10001083 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001084 cifs_dbg(FYI, "Received oplock break\n");
1085 } else {
1086 cifs_dbg(VFS, "No task to wake, unknown frame "
1087 "received! NumMids %d\n",
1088 atomic_read(&midCount));
1089 cifs_dump_mem("Received Data is: ", bufs[i],
1090 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +00001091#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001092 if (server->ops->dump_detail)
1093 server->ops->dump_detail(bufs[i],
1094 server);
Ronnie Sahlbergb73132b2019-02-05 12:56:44 +10001095 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001096 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001097#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001098 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001099 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001100
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001101 if (pdu_length > server->pdu_size) {
1102 if (!allocate_buffers(server))
1103 continue;
1104 pdu_length -= server->pdu_size;
1105 server->total_read = 0;
1106 server->large_buf = false;
1107 buf = server->smallbuf;
1108 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001109 }
1110 } /* end while !EXITING */
1111
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001112 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001113 cifs_buf_release(server->bigbuf);
1114 if (server->smallbuf) /* no sense logging a debug message if NULL */
1115 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001117 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001118 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001119
1120 /* if server->tsk was NULL then wait for a signal before exiting */
1121 if (!task_to_wake) {
1122 set_current_state(TASK_INTERRUPTIBLE);
1123 while (!signal_pending(current)) {
1124 schedule();
1125 set_current_state(TASK_INTERRUPTIBLE);
1126 }
1127 set_current_state(TASK_RUNNING);
1128 }
1129
Jeff Layton0468a2c2008-12-01 07:09:35 -05001130 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Jeff Laytonc359cf32007-11-16 22:22:06 +00001133/* extract the host portion of the UNC string */
1134static char *
1135extract_hostname(const char *unc)
1136{
1137 const char *src;
1138 char *dst, *delim;
1139 unsigned int len;
1140
1141 /* skip double chars at beginning of string */
1142 /* BB: check validity of these bytes? */
1143 src = unc + 2;
1144
1145 /* delimiter between hostname and sharename is always '\\' now */
1146 delim = strchr(src, '\\');
1147 if (!delim)
1148 return ERR_PTR(-EINVAL);
1149
1150 len = delim - src;
1151 dst = kmalloc((len + 1), GFP_KERNEL);
1152 if (dst == NULL)
1153 return ERR_PTR(-ENOMEM);
1154
1155 memcpy(dst, src, len);
1156 dst[len] = '\0';
1157
1158 return dst;
1159}
1160
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001161static int get_option_ul(substring_t args[], unsigned long *option)
1162{
1163 int rc;
1164 char *string;
1165
1166 string = match_strdup(args);
1167 if (string == NULL)
1168 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001169 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001170 kfree(string);
1171
1172 return rc;
1173}
1174
Eric W. Biederman3da46562013-02-06 01:37:39 -08001175static int get_option_uid(substring_t args[], kuid_t *result)
1176{
1177 unsigned long value;
1178 kuid_t uid;
1179 int rc;
1180
1181 rc = get_option_ul(args, &value);
1182 if (rc)
1183 return rc;
1184
1185 uid = make_kuid(current_user_ns(), value);
1186 if (!uid_valid(uid))
1187 return -EINVAL;
1188
1189 *result = uid;
1190 return 0;
1191}
1192
1193static int get_option_gid(substring_t args[], kgid_t *result)
1194{
1195 unsigned long value;
1196 kgid_t gid;
1197 int rc;
1198
1199 rc = get_option_ul(args, &value);
1200 if (rc)
1201 return rc;
1202
1203 gid = make_kgid(current_user_ns(), value);
1204 if (!gid_valid(gid))
1205 return -EINVAL;
1206
1207 *result = gid;
1208 return 0;
1209}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001210
1211static int cifs_parse_security_flavors(char *value,
1212 struct smb_vol *vol)
1213{
1214
1215 substring_t args[MAX_OPT_ARGS];
1216
Jeff Layton1e3cc572013-06-10 17:12:23 -05001217 /*
1218 * With mount options, the last one should win. Reset any existing
1219 * settings back to default.
1220 */
1221 vol->sectype = Unspecified;
1222 vol->sign = false;
1223
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001224 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001225 case Opt_sec_krb5p:
1226 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1227 return 1;
1228 case Opt_sec_krb5i:
1229 vol->sign = true;
1230 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001231 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001232 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001233 break;
1234 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001235 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001236 /* Fallthrough */
1237 case Opt_sec_ntlmssp:
1238 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001239 break;
1240 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001241 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001242 /* Fallthrough */
1243 case Opt_ntlm:
1244 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001245 break;
1246 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001247 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001248 /* Fallthrough */
1249 case Opt_sec_ntlmv2:
1250 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001251 break;
1252#ifdef CONFIG_CIFS_WEAK_PW_HASH
1253 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001254 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001255 break;
1256#endif
1257 case Opt_sec_none:
1258 vol->nullauth = 1;
1259 break;
1260 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001261 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001262 return 1;
1263 }
1264
1265 return 0;
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001269cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1270{
1271 substring_t args[MAX_OPT_ARGS];
1272
1273 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1274 case Opt_cache_loose:
1275 vol->direct_io = false;
1276 vol->strict_io = false;
1277 break;
1278 case Opt_cache_strict:
1279 vol->direct_io = false;
1280 vol->strict_io = true;
1281 break;
1282 case Opt_cache_none:
1283 vol->direct_io = true;
1284 vol->strict_io = false;
1285 break;
1286 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001287 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001288 return 1;
1289 }
1290 return 0;
1291}
1292
1293static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001294cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001295{
1296 substring_t args[MAX_OPT_ARGS];
1297
1298 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001299#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001300 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001301 if (disable_legacy_dialects) {
1302 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1303 return 1;
1304 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001305 if (is_smb3) {
1306 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1307 return 1;
1308 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001309 vol->ops = &smb1_operations;
1310 vol->vals = &smb1_values;
1311 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001312 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001313 if (disable_legacy_dialects) {
1314 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1315 return 1;
1316 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001317 if (is_smb3) {
1318 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1319 return 1;
1320 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001321 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001322 vol->vals = &smb20_values;
1323 break;
Steve French74204512018-06-19 14:34:08 -05001324#else
1325 case Smb_1:
1326 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1327 return 1;
1328 case Smb_20:
1329 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1330 return 1;
1331#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001332 case Smb_21:
1333 vol->ops = &smb21_operations;
1334 vol->vals = &smb21_values;
1335 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001336 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001337 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001338 vol->vals = &smb30_values;
1339 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001340 case Smb_302:
1341 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1342 vol->vals = &smb302_values;
1343 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001344 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001345 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001346 vol->vals = &smb311_values;
1347 break;
Steve French9764c022017-09-17 10:41:35 -05001348 case Smb_3any:
1349 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1350 vol->vals = &smb3any_values;
1351 break;
1352 case Smb_default:
1353 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1354 vol->vals = &smbdefault_values;
1355 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001356 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001357 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001358 return 1;
1359 }
1360 return 0;
1361}
1362
Jeff Laytond387a5c2012-12-10 06:10:46 -05001363/*
1364 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1365 * fields with the result. Returns 0 on success and an error otherwise.
1366 */
1367static int
1368cifs_parse_devname(const char *devname, struct smb_vol *vol)
1369{
1370 char *pos;
1371 const char *delims = "/\\";
1372 size_t len;
1373
Yao Liu36a32192019-01-28 19:47:28 +08001374 if (unlikely(!devname || !*devname)) {
1375 cifs_dbg(VFS, "Device name not specified.\n");
1376 return -EINVAL;
1377 }
1378
Jeff Laytond387a5c2012-12-10 06:10:46 -05001379 /* make sure we have a valid UNC double delimiter prefix */
1380 len = strspn(devname, delims);
1381 if (len != 2)
1382 return -EINVAL;
1383
1384 /* find delimiter between host and sharename */
1385 pos = strpbrk(devname + 2, delims);
1386 if (!pos)
1387 return -EINVAL;
1388
1389 /* skip past delimiter */
1390 ++pos;
1391
1392 /* now go until next delimiter or end of string */
1393 len = strcspn(pos, delims);
1394
1395 /* move "pos" up to delimiter or NULL */
1396 pos += len;
1397 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1398 if (!vol->UNC)
1399 return -ENOMEM;
1400
1401 convert_delimiter(vol->UNC, '\\');
1402
Sachin Prabhu11e31642016-02-08 13:44:01 +05301403 /* skip any delimiter */
1404 if (*pos == '/' || *pos == '\\')
1405 pos++;
1406
1407 /* If pos is NULL then no prepath */
1408 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001409 return 0;
1410
1411 vol->prepath = kstrdup(pos, GFP_KERNEL);
1412 if (!vol->prepath)
1413 return -ENOMEM;
1414
1415 return 0;
1416}
1417
Jeff Layton23db65f2012-05-15 12:20:51 -04001418static int
Sean Finneyb9468452011-04-11 13:19:32 +00001419cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001420 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001422 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001423 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 unsigned int temp_len, i, j;
1425 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001426 short int override_uid = -1;
1427 short int override_gid = -1;
1428 bool uid_specified = false;
1429 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001430 bool sloppy = false;
1431 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001432 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001433 char *string = NULL;
1434 char *tmp_end, *value;
1435 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001436 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001437 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001438 unsigned short port = 0;
1439 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001442 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001443 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Jeff Layton6ee95422012-11-26 11:09:57 -05001445 /* ensure we always start with zeroed-out smb_vol */
1446 memset(vol, 0, sizeof(*vol));
1447
Jeff Layton88463992010-11-22 15:31:03 -05001448 /*
1449 * does not have to be perfect mapping since field is
1450 * informational, only used for servers that do not support
1451 * port 445 and it can be overridden at mount time
1452 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001453 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1454 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001455 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1456
Jeff Layton1397f2e2011-01-07 11:30:28 -05001457 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001458 /* null target name indicates to use *SMBSERVR default called name
1459 if we end up sending RFC1001 session initialize */
1460 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001461 vol->cred_uid = current_uid();
1462 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001463 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001464
Steve French2baa2682014-09-27 02:19:01 -05001465 /*
1466 * default to SFM style remapping of seven reserved characters
1467 * unless user overrides it or we negotiate CIFS POSIX where
1468 * it is unnecessary. Can not simultaneously use more than one mapping
1469 * since then readdir could list files that open could not open
1470 */
1471 vol->remap = true;
1472
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001473 /* default to only allowing write access to owner of the mount */
1474 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001477 /* default is always to request posix paths. */
1478 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001479 /* default to using server inode numbers where available */
1480 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001481
Jeff Layton1b359202012-09-19 15:20:27 -07001482 /* default is to use strict cifs caching semantics */
1483 vol->strict_io = true;
1484
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301485 vol->actimeo = CIFS_DEF_ACTIMEO;
1486
Steve French9764c022017-09-17 10:41:35 -05001487 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1488 vol->ops = &smb30_operations;
1489 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001490
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001491 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1492
Sean Finneyb9468452011-04-11 13:19:32 +00001493 if (!mountdata)
1494 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Sean Finneyb9468452011-04-11 13:19:32 +00001496 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1497 if (!mountdata_copy)
1498 goto cifs_parse_mount_err;
1499
1500 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001501 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001502
Steve French50c2f752007-07-13 00:33:32 +00001503 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001504 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 separator[0] = options[4];
1506 options += 5;
1507 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001508 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001511 vol->backupuid_specified = false; /* no backup intent for a user */
1512 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001513
Jeff Layton37d4f992013-05-24 07:40:05 -04001514 switch (cifs_parse_devname(devname, vol)) {
1515 case 0:
1516 break;
1517 case -ENOMEM:
1518 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1519 goto cifs_parse_mount_err;
1520 case -EINVAL:
1521 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1522 goto cifs_parse_mount_err;
1523 default:
1524 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1525 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001526 }
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001529 substring_t args[MAX_OPT_ARGS];
1530 unsigned long option;
1531 int token;
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (!*data)
1534 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001536 token = match_token(data, cifs_mount_option_tokens, args);
1537
1538 switch (token) {
1539
1540 /* Ingnore the following */
1541 case Opt_ignore:
1542 break;
1543
1544 /* Boolean values */
1545 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001547 break;
1548 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001550 break;
1551 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001552 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001553 break;
1554 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001555 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001556 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001557 case Opt_forcegid:
1558 override_gid = 1;
1559 break;
1560 case Opt_noforcegid:
1561 override_gid = 0;
1562 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001563 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001564 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001565 break;
1566 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001567 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001568 break;
Steve French2e96c932019-09-11 21:46:20 -05001569 case Opt_nolease:
1570 vol->no_lease = 1;
1571 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001572 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001574 break;
1575 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001577 break;
1578 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001580 break;
1581 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001583 break;
1584 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001585 vol->sfu_remap = true;
1586 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001587 break;
1588 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001589 vol->sfu_remap = false;
1590 break;
1591 case Opt_mapposix:
1592 vol->remap = true;
1593 vol->sfu_remap = false; /* disable SFU mapping */
1594 break;
1595 case Opt_nomapposix:
1596 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001597 break;
1598 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001599 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001600 break;
1601 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001602 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001603 break;
1604 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001605 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001606 break;
1607 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001608 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001609 break;
1610 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001611 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001612 break;
1613 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001614 if (vol->linux_ext)
1615 cifs_dbg(VFS,
1616 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001617 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001618 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001619 case Opt_unix:
1620 if (vol->no_linux_ext)
1621 cifs_dbg(VFS,
1622 "conflicting unix mount options\n");
1623 vol->linux_ext = 1;
1624 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001625 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001626 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001627 break;
1628 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001629 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001630 break;
1631 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001632 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001633 /*
1634 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001635 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001636 * local vfs will do advisory
1637 */
Steve French50c2f752007-07-13 00:33:32 +00001638 if (vol->file_mode ==
1639 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001640 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001641 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001642 case Opt_nohandlecache:
1643 vol->nohandlecache = 1;
1644 break;
1645 case Opt_handlecache:
1646 vol->nohandlecache = 0;
1647 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001648 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001649 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001650 break;
1651 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001653 break;
1654 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001656 break;
Steve French95932652016-09-23 01:36:34 -05001657 case Opt_setuidfromacl:
1658 vol->setuidfromacl = 1;
1659 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001660 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001661 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001662 break;
1663 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001664 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001665 break;
1666 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001668 break;
1669 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001671 break;
1672 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001674 break;
1675 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001677 break;
1678 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001679 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001680 break;
1681 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001682 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001683 break;
1684 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001686 break;
1687 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001689 break;
1690 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001691 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001692 break;
1693 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001694 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001695 break;
1696 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001697 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001698 break;
1699 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001701 break;
1702 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001704 break;
1705 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001706 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001707 break;
1708 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001709 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001710 break;
1711 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001712 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001713 * is a per tree connection (mount) not a per socket
1714 * or per-smb connection option in the protocol
1715 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1716 */
Steve French95b1cb92008-05-15 16:44:38 +00001717 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001718 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001719 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001720 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001721 break;
1722 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301723#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001724 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001725 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301726#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301727 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001728 break;
1729 case Opt_mfsymlinks:
Stefan Metzmacher736a3322010-07-30 14:56:00 +02001730 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001731 break;
1732 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001733 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001734 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001735 case Opt_sloppy:
1736 sloppy = true;
1737 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001738 case Opt_nosharesock:
1739 vol->nosharesock = true;
1740 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001741 case Opt_nopersistent:
1742 vol->nopersistent = true;
1743 if (vol->persistent) {
1744 cifs_dbg(VFS,
1745 "persistenthandles mount options conflict\n");
1746 goto cifs_parse_mount_err;
1747 }
1748 break;
1749 case Opt_persistent:
1750 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001751 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001752 cifs_dbg(VFS,
1753 "persistenthandles mount options conflict\n");
1754 goto cifs_parse_mount_err;
1755 }
1756 break;
Steve French592fafe2015-11-03 10:08:53 -06001757 case Opt_resilient:
1758 vol->resilient = true;
1759 if (vol->persistent) {
1760 cifs_dbg(VFS,
1761 "persistenthandles mount options conflict\n");
1762 goto cifs_parse_mount_err;
1763 }
1764 break;
1765 case Opt_noresilient:
1766 vol->resilient = false; /* already the default */
1767 break;
Germano Percossi39566442016-12-15 12:31:18 +05301768 case Opt_domainauto:
1769 vol->domainauto = true;
1770 break;
Long Li8339dd32017-11-07 01:54:55 -07001771 case Opt_rdma:
1772 vol->rdma = true;
1773 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001774
1775 /* Numeric Values */
1776 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001777 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001778 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1779 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001780 goto cifs_parse_mount_err;
1781 }
1782 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001783 break;
1784 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001785 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001786 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1787 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001788 goto cifs_parse_mount_err;
1789 }
1790 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001791 break;
1792 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001793 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001794 cifs_dbg(VFS, "%s: Invalid uid value\n",
1795 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001796 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001798 uid_specified = true;
1799 break;
1800 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001801 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001802 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1803 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001804 goto cifs_parse_mount_err;
1805 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001806 break;
1807 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001808 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001809 cifs_dbg(VFS, "%s: Invalid gid value\n",
1810 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001811 goto cifs_parse_mount_err;
1812 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001813 gid_specified = true;
1814 break;
1815 case Opt_file_mode:
1816 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001817 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1818 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001819 goto cifs_parse_mount_err;
1820 }
1821 vol->file_mode = option;
1822 break;
1823 case Opt_dirmode:
1824 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001825 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1826 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001827 goto cifs_parse_mount_err;
1828 }
1829 vol->dir_mode = option;
1830 break;
1831 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001832 if (get_option_ul(args, &option) ||
1833 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001834 cifs_dbg(VFS, "%s: Invalid port value\n",
1835 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001836 goto cifs_parse_mount_err;
1837 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001838 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001839 break;
1840 case Opt_rsize:
1841 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001842 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1843 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001844 goto cifs_parse_mount_err;
1845 }
1846 vol->rsize = option;
1847 break;
1848 case Opt_wsize:
1849 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001850 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1851 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001852 goto cifs_parse_mount_err;
1853 }
1854 vol->wsize = option;
1855 break;
1856 case Opt_actimeo:
1857 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001858 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1859 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001860 goto cifs_parse_mount_err;
1861 }
1862 vol->actimeo = HZ * option;
1863 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001864 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001865 goto cifs_parse_mount_err;
1866 }
1867 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001868 case Opt_echo_interval:
1869 if (get_option_ul(args, &option)) {
1870 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1871 __func__);
1872 goto cifs_parse_mount_err;
1873 }
1874 vol->echo_interval = option;
1875 break;
Steve French8b217fe2016-11-11 22:36:20 -06001876 case Opt_snapshot:
1877 if (get_option_ul(args, &option)) {
1878 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1879 __func__);
1880 goto cifs_parse_mount_err;
1881 }
1882 vol->snapshot_time = option;
1883 break;
Steve French141891f2016-09-23 00:44:16 -05001884 case Opt_max_credits:
1885 if (get_option_ul(args, &option) || (option < 20) ||
1886 (option > 60000)) {
1887 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1888 __func__);
1889 goto cifs_parse_mount_err;
1890 }
1891 vol->max_credits = option;
1892 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001893
1894 /* String Arguments */
1895
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001896 case Opt_blank_user:
1897 /* null user, ie. anonymous authentication */
1898 vol->nullauth = 1;
1899 vol->username = NULL;
1900 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001901 case Opt_user:
1902 string = match_strdup(args);
1903 if (string == NULL)
1904 goto out_nomem;
1905
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001906 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1907 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001908 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001909 goto cifs_parse_mount_err;
1910 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001911
1912 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001913 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001914 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001915 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001916 break;
1917 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001918 /* passwords have to be handled differently
1919 * to allow the character used for deliminator
1920 * to be passed within them
1921 */
1922
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001923 /*
1924 * Check if this is a case where the password
1925 * starts with a delimiter
1926 */
1927 tmp_end = strchr(data, '=');
1928 tmp_end++;
1929 if (!(tmp_end < end && tmp_end[1] == delim)) {
1930 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001931 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001932 vol->password = NULL;
1933 break;
1934 }
1935 /* Yes it is. Drop down to Opt_pass below.*/
1936 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001937 /* Obtain the value string */
1938 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001939 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001940
1941 /* Set tmp_end to end of the string */
1942 tmp_end = (char *) value + strlen(value);
1943
1944 /* Check if following character is the deliminator
1945 * If yes, we have encountered a double deliminator
1946 * reset the NULL character to the deliminator
1947 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301948 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001949 tmp_end[0] = delim;
1950
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301951 /* Keep iterating until we get to a single
1952 * deliminator OR the end
1953 */
1954 while ((tmp_end = strchr(tmp_end, delim))
1955 != NULL && (tmp_end[1] == delim)) {
1956 tmp_end = (char *) &tmp_end[2];
1957 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001958
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301959 /* Reset var options to point to next element */
1960 if (tmp_end) {
1961 tmp_end[0] = '\0';
1962 options = (char *) &tmp_end[1];
1963 } else
1964 /* Reached the end of the mount option
1965 * string */
1966 options = end;
1967 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001968
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001969 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001970 /* Now build new password string */
1971 temp_len = strlen(value);
1972 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1973 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001974 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001975 goto cifs_parse_mount_err;
1976 }
1977
1978 for (i = 0, j = 0; i < temp_len; i++, j++) {
1979 vol->password[j] = value[i];
1980 if ((value[i] == delim) &&
1981 value[i+1] == delim)
1982 /* skip the second deliminator */
1983 i++;
1984 }
1985 vol->password[j] = '\0';
1986 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001987 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001988 /* FIXME: should this be an error instead? */
1989 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001990 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001991 case Opt_ip:
1992 string = match_strdup(args);
1993 if (string == NULL)
1994 goto out_nomem;
1995
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001996 if (!cifs_convert_address(dstaddr, string,
1997 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001998 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001999 goto cifs_parse_mount_err;
2000 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002001 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002002 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002003 case Opt_domain:
2004 string = match_strdup(args);
2005 if (string == NULL)
2006 goto out_nomem;
2007
Chen Gang057d6332013-07-19 09:01:36 +08002008 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
2009 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002010 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002011 goto cifs_parse_mount_err;
2012 }
2013
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002014 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002015 vol->domainname = kstrdup(string, GFP_KERNEL);
2016 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002017 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002018 goto cifs_parse_mount_err;
2019 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002020 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002021 break;
2022 case Opt_srcaddr:
2023 string = match_strdup(args);
2024 if (string == NULL)
2025 goto out_nomem;
2026
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002027 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002028 (struct sockaddr *)&vol->srcaddr,
2029 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002030 pr_warn("CIFS: Could not parse srcaddr: %s\n",
2031 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002032 goto cifs_parse_mount_err;
2033 }
2034 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002035 case Opt_iocharset:
2036 string = match_strdup(args);
2037 if (string == NULL)
2038 goto out_nomem;
2039
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002040 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002041 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002042 goto cifs_parse_mount_err;
2043 }
2044
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002045 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002046 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002047 vol->iocharset = kstrdup(string,
2048 GFP_KERNEL);
2049 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002050 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002051 goto cifs_parse_mount_err;
2052 }
2053 }
2054 /* if iocharset not set then load_nls_default
2055 * is used by caller
2056 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002057 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002058 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002059 case Opt_netbiosname:
2060 string = match_strdup(args);
2061 if (string == NULL)
2062 goto out_nomem;
2063
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002064 memset(vol->source_rfc1001_name, 0x20,
2065 RFC1001_NAME_LEN);
2066 /*
2067 * FIXME: are there cases in which a comma can
2068 * be valid in workstation netbios name (and
2069 * need special handling)?
2070 */
2071 for (i = 0; i < RFC1001_NAME_LEN; i++) {
2072 /* don't ucase netbiosname for user */
2073 if (string[i] == 0)
2074 break;
2075 vol->source_rfc1001_name[i] = string[i];
2076 }
2077 /* The string has 16th byte zero still from
2078 * set at top of the function
2079 */
2080 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002081 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002082 break;
2083 case Opt_servern:
2084 /* servernetbiosname specified override *SMBSERVER */
2085 string = match_strdup(args);
2086 if (string == NULL)
2087 goto out_nomem;
2088
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002089 /* last byte, type, is 0x20 for servr type */
2090 memset(vol->target_rfc1001_name, 0x20,
2091 RFC1001_NAME_LEN_WITH_NULL);
2092
2093 /* BB are there cases in which a comma can be
2094 valid in this workstation netbios name
2095 (and need special handling)? */
2096
2097 /* user or mount helper must uppercase the
2098 netbios name */
2099 for (i = 0; i < 15; i++) {
2100 if (string[i] == 0)
2101 break;
2102 vol->target_rfc1001_name[i] = string[i];
2103 }
2104 /* The string has 16th byte zero still from
2105 set at top of the function */
2106 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002107 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002108 break;
2109 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002110 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002111 string = match_strdup(args);
2112 if (string == NULL)
2113 goto out_nomem;
2114
Steve French7e682f72017-08-31 21:34:24 -05002115 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002116 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002117 if (strlen(string) > 1) {
2118 pr_warn("Bad mount helper ver=%s. Did "
2119 "you want SMB1 (CIFS) dialect "
2120 "and mean to type vers=1.0 "
2121 "instead?\n", string);
2122 goto cifs_parse_mount_err;
2123 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002124 /* This is the default */
2125 break;
2126 }
2127 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002128 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002129 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002130 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002131 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002132 string = match_strdup(args);
2133 if (string == NULL)
2134 goto out_nomem;
2135
Steve Frenchc7c137b2018-06-06 17:59:29 -05002136 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002137 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002138 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002139 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002140 case Opt_sec:
2141 string = match_strdup(args);
2142 if (string == NULL)
2143 goto out_nomem;
2144
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002145 if (cifs_parse_security_flavors(string, vol) != 0)
2146 goto cifs_parse_mount_err;
2147 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002148 case Opt_cache:
2149 string = match_strdup(args);
2150 if (string == NULL)
2151 goto out_nomem;
2152
2153 if (cifs_parse_cache_flavor(string, vol) != 0)
2154 goto cifs_parse_mount_err;
2155 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002156 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002157 /*
2158 * An option we don't recognize. Save it off for later
2159 * if we haven't already found one
2160 */
2161 if (!invalid)
2162 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002165 /* Free up any allocated string */
2166 kfree(string);
2167 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002169
Jeff Laytond8162552012-03-23 14:40:56 -04002170 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002171 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002172 goto cifs_parse_mount_err;
2173 }
2174
Long Li8339dd32017-11-07 01:54:55 -07002175 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2176 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2177 goto cifs_parse_mount_err;
2178 }
2179
Jeff Layton8a8798a2012-01-17 16:09:15 -05002180#ifndef CONFIG_KEYS
2181 /* Muliuser mounts require CONFIG_KEYS support */
2182 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002183 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002184 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002185 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002186#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002187 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002188 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002189 goto cifs_parse_mount_err;
2190 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002191
Jeff Layton62a1a432012-12-10 06:10:45 -05002192 /* make sure UNC has a share name */
2193 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002194 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002195 goto cifs_parse_mount_err;
2196 }
2197
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002198 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002199 int len;
2200 const char *slash;
2201
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002202 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002203 /* Use the address part of the UNC. */
2204 slash = strchr(&vol->UNC[2], '\\');
2205 len = slash - &vol->UNC[2];
2206 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002207 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002208 goto cifs_parse_mount_err;
2209 }
2210 }
2211
2212 /* set the port that we got earlier */
2213 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002215 if (uid_specified)
2216 vol->override_uid = override_uid;
2217 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002218 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002219
2220 if (gid_specified)
2221 vol->override_gid = override_gid;
2222 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002223 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002224
Steve French7e682f72017-08-31 21:34:24 -05002225 if (got_version == false)
2226 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002227 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002228 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002229 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2230 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002231
Sean Finneyb9468452011-04-11 13:19:32 +00002232 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002234
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002235out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002236 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002237cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002238 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002239 kfree(mountdata_copy);
2240 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
Ben Greear3eb9a882010-09-01 17:06:02 -07002243/** Returns true if srcaddr isn't specified and rhs isn't
2244 * specified, or if srcaddr is specified and
2245 * matches the IP address of the rhs argument.
2246 */
Jeff Layton45151482010-07-06 20:43:02 -04002247static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002248srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2249{
2250 switch (srcaddr->sa_family) {
2251 case AF_UNSPEC:
2252 return (rhs->sa_family == AF_UNSPEC);
2253 case AF_INET: {
2254 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2255 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2256 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2257 }
2258 case AF_INET6: {
2259 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002260 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002261 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2262 }
2263 default:
2264 WARN_ON(1);
2265 return false; /* don't expect to be here */
2266 }
2267}
2268
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002269/*
2270 * If no port is specified in addr structure, we try to match with 445 port
2271 * and if it fails - with 139 ports. It should be called only if address
2272 * families of server and addr are equal.
2273 */
2274static bool
2275match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2276{
Steve French6da97912011-03-13 18:55:55 +00002277 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002278
2279 switch (addr->sa_family) {
2280 case AF_INET:
2281 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2282 port = ((struct sockaddr_in *) addr)->sin_port;
2283 break;
2284 case AF_INET6:
2285 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2286 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2287 break;
2288 default:
2289 WARN_ON(1);
2290 return false;
2291 }
2292
2293 if (!port) {
2294 port = htons(CIFS_PORT);
2295 if (port == *sport)
2296 return true;
2297
2298 port = htons(RFC1001_PORT);
2299 }
2300
2301 return port == *sport;
2302}
Ben Greear3eb9a882010-09-01 17:06:02 -07002303
2304static bool
2305match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2306 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Jeff Layton45151482010-07-06 20:43:02 -04002308 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002309 case AF_INET: {
2310 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2311 struct sockaddr_in *srv_addr4 =
2312 (struct sockaddr_in *)&server->dstaddr;
2313
2314 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002315 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002316 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002317 }
2318 case AF_INET6: {
2319 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2320 struct sockaddr_in6 *srv_addr6 =
2321 (struct sockaddr_in6 *)&server->dstaddr;
2322
Jeff Layton45151482010-07-06 20:43:02 -04002323 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002324 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002325 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002326 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002327 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002328 break;
2329 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002330 default:
2331 WARN_ON(1);
2332 return false; /* don't expect to be here */
2333 }
Jeff Layton45151482010-07-06 20:43:02 -04002334
Ben Greear3eb9a882010-09-01 17:06:02 -07002335 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2336 return false;
2337
Jeff Layton45151482010-07-06 20:43:02 -04002338 return true;
2339}
2340
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002341static bool
2342match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2343{
Jeff Layton3f618222013-06-12 19:52:14 -05002344 /*
2345 * The select_sectype function should either return the vol->sectype
2346 * that was specified, or "Unspecified" if that sectype was not
2347 * compatible with the given NEGOTIATE request.
2348 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302349 if (server->ops->select_sectype(server, vol->sectype)
2350 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002351 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002352
Jeff Layton3f618222013-06-12 19:52:14 -05002353 /*
2354 * Now check if signing mode is acceptable. No need to check
2355 * global_secflags at this point since if MUST_SIGN is set then
2356 * the server->sign had better be too.
2357 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002358 if (vol->sign && !server->sign)
2359 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002360
2361 return true;
2362}
2363
Jeff Layton9fa114f2012-11-26 11:09:57 -05002364static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002365{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002366 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2367
Jeff Laytona0b3df52013-05-24 07:40:59 -04002368 if (vol->nosharesock)
2369 return 0;
2370
Steve French9764c022017-09-17 10:41:35 -05002371 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002372 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2373 return 0;
2374
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002375 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2376 return 0;
2377
2378 if (!match_address(server, addr,
2379 (struct sockaddr *)&vol->srcaddr))
2380 return 0;
2381
2382 if (!match_port(server, addr))
2383 return 0;
2384
2385 if (!match_security(server, vol))
2386 return 0;
2387
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002388 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002389 return 0;
2390
Long Li8339dd32017-11-07 01:54:55 -07002391 if (server->rdma != vol->rdma)
2392 return 0;
2393
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002394 return 1;
2395}
2396
Jeff Layton45151482010-07-06 20:43:02 -04002397static struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002398cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002399{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002400 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302402 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002403 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002404 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002405 continue;
2406
Jeff Laytone7ddee92008-11-14 13:44:38 -05002407 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302408 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002409 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002410 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302412 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return NULL;
2414}
2415
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002416void
2417cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002419 struct task_struct *task;
2420
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302421 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002422 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302423 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002424 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002426
Rob Landleyf1d0c992011-01-22 15:44:05 -06002427 put_net(cifs_net_ns(server));
2428
Jeff Laytone7ddee92008-11-14 13:44:38 -05002429 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302430 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002431
Jeff Laytonc74093b2011-01-11 07:24:23 -05002432 cancel_delayed_work_sync(&server->echo);
2433
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002434 if (from_reconnect)
2435 /*
2436 * Avoid deadlock here: reconnect work calls
2437 * cifs_put_tcp_session() at its end. Need to be sure
2438 * that reconnect work does nothing with server pointer after
2439 * that step.
2440 */
2441 cancel_delayed_work(&server->reconnect);
2442 else
2443 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002444
Jeff Laytone7ddee92008-11-14 13:44:38 -05002445 spin_lock(&GlobalMid_Lock);
2446 server->tcpStatus = CifsExiting;
2447 spin_unlock(&GlobalMid_Lock);
2448
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002449 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302450 cifs_fscache_release_client_cookie(server);
2451
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002452 kfree(server->session_key.response);
2453 server->session_key.response = NULL;
2454 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002455
2456 task = xchg(&server->tsk, NULL);
2457 if (task)
Eric W. Biedermane6a13c72019-05-15 12:33:50 -05002458 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Jeff Layton63c038c2008-12-01 18:41:46 -05002461static struct TCP_Server_Info *
2462cifs_get_tcp_session(struct smb_vol *volume_info)
2463{
2464 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002465 int rc;
2466
Joe Perchesf96637b2013-05-04 22:12:25 -05002467 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002468
2469 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002470 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002471 if (tcp_ses)
2472 return tcp_ses;
2473
2474 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2475 if (!tcp_ses) {
2476 rc = -ENOMEM;
2477 goto out_err;
2478 }
2479
Jeff Layton23db65f2012-05-15 12:20:51 -04002480 tcp_ses->ops = volume_info->ops;
2481 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002482 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002483 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2484 if (IS_ERR(tcp_ses->hostname)) {
2485 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002486 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002487 }
2488
2489 tcp_ses->noblocksnd = volume_info->noblocksnd;
2490 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002491 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002492 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002493 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002494 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002495 init_waitqueue_head(&tcp_ses->response_q);
2496 init_waitqueue_head(&tcp_ses->request_q);
2497 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2498 mutex_init(&tcp_ses->srv_mutex);
2499 memcpy(tcp_ses->workstation_RFC1001_name,
2500 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2501 memcpy(tcp_ses->server_RFC1001_name,
2502 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002503 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002504 tcp_ses->sequence_number = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002505 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002506 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002507 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2508 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002509 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002510 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2511 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002512 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2513 sizeof(tcp_ses->srcaddr));
2514 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2515 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002516 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002517 /*
2518 * at this point we are the only ones with the pointer
2519 * to the struct since the kernel thread not created yet
2520 * no need to spinlock this init of tcpStatus or srv_count
2521 */
2522 tcp_ses->tcpStatus = CifsNew;
2523 ++tcp_ses->srv_count;
2524
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002525 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2526 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2527 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2528 else
2529 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002530 if (tcp_ses->rdma) {
2531#ifndef CONFIG_CIFS_SMB_DIRECT
2532 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2533 rc = -ENOENT;
2534 goto out_err_crypto_release;
2535#endif
2536 tcp_ses->smbd_conn = smbd_get_connection(
2537 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2538 if (tcp_ses->smbd_conn) {
2539 cifs_dbg(VFS, "RDMA transport established\n");
2540 rc = 0;
2541 goto smbd_connected;
2542 } else {
2543 rc = -ENOENT;
2544 goto out_err_crypto_release;
2545 }
2546 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002547 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002548 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002549 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002550 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002551 }
Long Li2f894642017-11-22 17:38:34 -07002552smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002553 /*
2554 * since we're in a cifs function already, we know that
2555 * this will succeed. No need for try_module_get().
2556 */
2557 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002558 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002559 tcp_ses, "cifsd");
2560 if (IS_ERR(tcp_ses->tsk)) {
2561 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002562 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002563 module_put(THIS_MODULE);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002564 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002565 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002566 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002567
2568 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302569 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002570 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302571 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002572
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302573 cifs_fscache_get_client_cookie(tcp_ses);
2574
Jeff Laytonc74093b2011-01-11 07:24:23 -05002575 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002576 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002577
Jeff Layton63c038c2008-12-01 18:41:46 -05002578 return tcp_ses;
2579
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002580out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002581 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002582
Rob Landleyf1d0c992011-01-22 15:44:05 -06002583 put_net(cifs_net_ns(tcp_ses));
2584
Jeff Layton63c038c2008-12-01 18:41:46 -05002585out_err:
2586 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002587 if (!IS_ERR(tcp_ses->hostname))
2588 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002589 if (tcp_ses->ssocket)
2590 sock_release(tcp_ses->ssocket);
2591 kfree(tcp_ses);
2592 }
2593 return ERR_PTR(rc);
2594}
2595
Steve French96daf2b2011-05-27 04:34:02 +00002596static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002597{
Jeff Layton3f618222013-06-12 19:52:14 -05002598 if (vol->sectype != Unspecified &&
2599 vol->sectype != ses->sectype)
2600 return 0;
2601
2602 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002603 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002604 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002605 return 0;
2606 break;
2607 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002608 /* NULL username means anonymous session */
2609 if (ses->user_name == NULL) {
2610 if (!vol->nullauth)
2611 return 0;
2612 break;
2613 }
2614
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002615 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002616 if (strncmp(ses->user_name,
2617 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002618 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002619 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002620 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002621 ses->password != NULL &&
2622 strncmp(ses->password,
2623 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002624 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002625 return 0;
2626 }
2627 return 1;
2628}
2629
Aurelien Aptelb327a712018-01-24 13:46:10 +01002630/**
2631 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2632 *
2633 * A new IPC connection is made and stored in the session
2634 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2635 */
2636static int
2637cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2638{
2639 int rc = 0, xid;
2640 struct cifs_tcon *tcon;
2641 struct nls_table *nls_codepage;
2642 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2643 bool seal = false;
2644
2645 /*
2646 * If the mount request that resulted in the creation of the
2647 * session requires encryption, force IPC to be encrypted too.
2648 */
2649 if (volume_info->seal) {
2650 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2651 seal = true;
2652 else {
2653 cifs_dbg(VFS,
2654 "IPC: server doesn't support encryption\n");
2655 return -EOPNOTSUPP;
2656 }
2657 }
2658
2659 tcon = tconInfoAlloc();
2660 if (tcon == NULL)
2661 return -ENOMEM;
2662
Thomas Werschlein395a2072018-08-30 18:29:20 +02002663 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002664
2665 /* cannot fail */
2666 nls_codepage = load_nls_default();
2667
2668 xid = get_xid();
2669 tcon->ses = ses;
2670 tcon->ipc = true;
2671 tcon->seal = seal;
2672 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2673 free_xid(xid);
2674
2675 if (rc) {
2676 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2677 tconInfoFree(tcon);
2678 goto out;
2679 }
2680
2681 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2682
2683 ses->tcon_ipc = tcon;
2684out:
2685 unload_nls(nls_codepage);
2686 return rc;
2687}
2688
2689/**
2690 * cifs_free_ipc - helper to release the session IPC tcon
2691 *
2692 * Needs to be called everytime a session is destroyed
2693 */
2694static int
2695cifs_free_ipc(struct cifs_ses *ses)
2696{
2697 int rc = 0, xid;
2698 struct cifs_tcon *tcon = ses->tcon_ipc;
2699
2700 if (tcon == NULL)
2701 return 0;
2702
2703 if (ses->server->ops->tree_disconnect) {
2704 xid = get_xid();
2705 rc = ses->server->ops->tree_disconnect(xid, tcon);
2706 free_xid(xid);
2707 }
2708
2709 if (rc)
2710 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2711
2712 tconInfoFree(tcon);
2713 ses->tcon_ipc = NULL;
2714 return rc;
2715}
2716
Steve French96daf2b2011-05-27 04:34:02 +00002717static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002718cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Steve French96daf2b2011-05-27 04:34:02 +00002720 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302722 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002723 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002724 if (ses->status == CifsExiting)
2725 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002726 if (!match_session(ses, vol))
2727 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002728 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302729 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002730 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302732 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return NULL;
2734}
2735
Jeff Layton14fbf502008-11-14 13:53:46 -05002736static void
Steve French96daf2b2011-05-27 04:34:02 +00002737cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002738{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002739 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002740 struct TCP_Server_Info *server = ses->server;
2741
Joe Perchesf96637b2013-05-04 22:12:25 -05002742 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002743
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302744 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002745 if (ses->status == CifsExiting) {
2746 spin_unlock(&cifs_tcp_ses_lock);
2747 return;
2748 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002749 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302750 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002751 return;
2752 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002753 if (ses->status == CifsGood)
2754 ses->status = CifsExiting;
2755 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002756
Aurelien Aptelb327a712018-01-24 13:46:10 +01002757 cifs_free_ipc(ses);
2758
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002759 if (ses->status == CifsExiting && server->ops->logoff) {
2760 xid = get_xid();
2761 rc = server->ops->logoff(xid, ses);
2762 if (rc)
2763 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2764 __func__, rc);
2765 _free_xid(xid);
2766 }
2767
2768 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002769 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302770 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002771
Jeff Layton14fbf502008-11-14 13:53:46 -05002772 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002773 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002774}
2775
Jeff Layton8a8798a2012-01-17 16:09:15 -05002776#ifdef CONFIG_KEYS
2777
Chen Gang057d6332013-07-19 09:01:36 +08002778/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2779#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002780
2781/* Populate username and pw fields from keyring if possible */
2782static int
2783cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2784{
2785 int rc = 0;
Ronnie Sahlberg325fa2a2019-08-22 08:09:50 +10002786 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002787 const char *delim, *payload;
2788 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002789 ssize_t len;
2790 struct key *key;
2791 struct TCP_Server_Info *server = ses->server;
2792 struct sockaddr_in *sa;
2793 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002794 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002795
2796 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2797 if (!desc)
2798 return -ENOMEM;
2799
2800 /* try to find an address key first */
2801 switch (server->dstaddr.ss_family) {
2802 case AF_INET:
2803 sa = (struct sockaddr_in *)&server->dstaddr;
2804 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2805 break;
2806 case AF_INET6:
2807 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2808 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2809 break;
2810 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002811 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2812 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002813 rc = -EINVAL;
2814 goto out_err;
2815 }
2816
Joe Perchesf96637b2013-05-04 22:12:25 -05002817 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002818 key = request_key(&key_type_logon, desc, "");
2819 if (IS_ERR(key)) {
2820 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002821 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002822 rc = PTR_ERR(key);
2823 goto out_err;
2824 }
2825
2826 /* didn't work, try to find a domain key */
2827 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002828 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002829 key = request_key(&key_type_logon, desc, "");
2830 if (IS_ERR(key)) {
2831 rc = PTR_ERR(key);
2832 goto out_err;
2833 }
Ronnie Sahlberg325fa2a2019-08-22 08:09:50 +10002834 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002835 }
2836
2837 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002838 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002839 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002840 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002841 goto out_key_put;
2842 }
2843
2844 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002845 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002846 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002847 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002848 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002849 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2850 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002851 rc = -EINVAL;
2852 goto out_key_put;
2853 }
2854
2855 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002856 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002857 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2858 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002859 rc = -EINVAL;
2860 goto out_key_put;
2861 }
2862
2863 vol->username = kstrndup(payload, len, GFP_KERNEL);
2864 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002865 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2866 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002867 rc = -ENOMEM;
2868 goto out_key_put;
2869 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002870 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002871
2872 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002873 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002874 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002875 rc = -EINVAL;
2876 kfree(vol->username);
2877 vol->username = NULL;
2878 goto out_key_put;
2879 }
2880
2881 ++delim;
2882 vol->password = kstrndup(delim, len, GFP_KERNEL);
2883 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002884 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2885 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002886 rc = -ENOMEM;
2887 kfree(vol->username);
2888 vol->username = NULL;
2889 goto out_key_put;
2890 }
2891
Ronnie Sahlberg325fa2a2019-08-22 08:09:50 +10002892 /*
2893 * If we have a domain key then we must set the domainName in the
2894 * for the request.
2895 */
2896 if (is_domain && ses->domainName) {
2897 vol->domainname = kstrndup(ses->domainName,
2898 strlen(ses->domainName),
2899 GFP_KERNEL);
2900 if (!vol->domainname) {
2901 cifs_dbg(FYI, "Unable to allocate %zd bytes for "
2902 "domain\n", len);
2903 rc = -ENOMEM;
2904 kfree(vol->username);
2905 vol->username = NULL;
Dan Carpenterb6294682019-08-27 13:59:17 +03002906 kzfree(vol->password);
Ronnie Sahlberg325fa2a2019-08-22 08:09:50 +10002907 vol->password = NULL;
2908 goto out_key_put;
2909 }
2910 }
2911
Jeff Layton8a8798a2012-01-17 16:09:15 -05002912out_key_put:
2913 up_read(&key->sem);
2914 key_put(key);
2915out_err:
2916 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002917 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002918 return rc;
2919}
2920#else /* ! CONFIG_KEYS */
2921static inline int
2922cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2923 struct cifs_ses *ses __attribute__((unused)))
2924{
2925 return -ENOSYS;
2926}
2927#endif /* CONFIG_KEYS */
2928
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002929/**
2930 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2931 *
2932 * This function assumes it is being called from cifs_mount() where we
2933 * already got a server reference (server refcount +1). See
2934 * cifs_get_tcon() for refcount explanations.
2935 */
Steve French96daf2b2011-05-27 04:34:02 +00002936static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002937cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2938{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002939 int rc = -ENOMEM;
2940 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002941 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002942 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2943 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002944
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002945 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002946
Jeff Layton4ff67b72010-07-06 20:43:02 -04002947 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002948 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002949 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2950 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002951
Jeff Layton36988c72010-04-24 07:57:43 -04002952 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002953 rc = cifs_negotiate_protocol(xid, ses);
2954 if (rc) {
2955 mutex_unlock(&ses->session_mutex);
2956 /* problem -- put our ses reference */
2957 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002958 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002959 return ERR_PTR(rc);
2960 }
Jeff Layton36988c72010-04-24 07:57:43 -04002961 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002962 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002963 rc = cifs_setup_session(xid, ses,
2964 volume_info->local_nls);
2965 if (rc) {
2966 mutex_unlock(&ses->session_mutex);
2967 /* problem -- put our reference */
2968 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002969 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002970 return ERR_PTR(rc);
2971 }
2972 }
2973 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002974
2975 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002976 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002977 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002978 return ses;
2979 }
2980
Joe Perchesf96637b2013-05-04 22:12:25 -05002981 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002982 ses = sesInfoAlloc();
2983 if (ses == NULL)
2984 goto get_ses_fail;
2985
2986 /* new SMB session uses our server ref */
2987 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002988 if (server->dstaddr.ss_family == AF_INET6)
2989 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002990 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002991 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002992
Steve French8727c8a2011-02-25 01:11:56 -06002993 if (volume_info->username) {
2994 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2995 if (!ses->user_name)
2996 goto get_ses_fail;
2997 }
Jeff Layton36988c72010-04-24 07:57:43 -04002998
2999 /* volume_info->password freed at unmount */
3000 if (volume_info->password) {
3001 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
3002 if (!ses->password)
3003 goto get_ses_fail;
3004 }
3005 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05003006 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
3007 if (!ses->domainName)
3008 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04003009 }
Germano Percossi39566442016-12-15 12:31:18 +05303010 if (volume_info->domainauto)
3011 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04003012 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04003013 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00003014
Jeff Layton28e11bd2013-05-26 07:01:00 -04003015 ses->sectype = volume_info->sectype;
3016 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04003017
3018 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04003019 rc = cifs_negotiate_protocol(xid, ses);
3020 if (!rc)
3021 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04003022 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00003023 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04003024 goto get_ses_fail;
3025
3026 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303027 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003028 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303029 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003030
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003031 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01003032
3033 cifs_setup_ipc(ses, volume_info);
3034
Jeff Layton36988c72010-04-24 07:57:43 -04003035 return ses;
3036
3037get_ses_fail:
3038 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003039 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003040 return ERR_PTR(rc);
3041}
3042
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003043static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003044{
3045 if (tcon->tidStatus == CifsExiting)
3046 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003047 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003048 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003049 if (tcon->seal != volume_info->seal)
3050 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003051 if (tcon->snapshot_time != volume_info->snapshot_time)
3052 return 0;
Steve French2e96c932019-09-11 21:46:20 -05003053 if (tcon->no_lease != volume_info->no_lease)
3054 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003055 return 1;
3056}
3057
Steve French96daf2b2011-05-27 04:34:02 +00003058static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06003059cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
3061 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00003062 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303064 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003065 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00003066 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003067 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05003068 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003069 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303070 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return tcon;
3072 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303073 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return NULL;
3075}
3076
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003077void
Steve French96daf2b2011-05-27 04:34:02 +00003078cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05003079{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003080 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003081 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003082
Aurelien Aptelb327a712018-01-24 13:46:10 +01003083 /*
3084 * IPC tcon share the lifetime of their session and are
3085 * destroyed in the session put function
3086 */
3087 if (tcon == NULL || tcon->ipc)
3088 return;
3089
3090 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05003091 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303092 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003093 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303094 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003095 return;
3096 }
3097
3098 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303099 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003100
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003101 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003102 if (ses->server->ops->tree_disconnect)
3103 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003104 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003105
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303106 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00003107 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003108 cifs_put_smb_ses(ses);
3109}
3110
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003111/**
3112 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
3113 *
3114 * - tcon refcount is the number of mount points using the tcon.
3115 * - ses refcount is the number of tcon using the session.
3116 *
3117 * 1. This function assumes it is being called from cifs_mount() where
3118 * we already got a session reference (ses refcount +1).
3119 *
3120 * 2. Since we're in the context of adding a mount point, the end
3121 * result should be either:
3122 *
3123 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3124 * its session refcount incremented (1 new tcon). This +1 was
3125 * already done in (1).
3126 *
3127 * b) an existing tcon with refcount+1 (add a mount point to it) and
3128 * identical ses refcount (no new tcon). Because of (1) we need to
3129 * decrement the ses refcount.
3130 */
Steve French96daf2b2011-05-27 04:34:02 +00003131static struct cifs_tcon *
3132cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003133{
3134 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003135 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003136
Steve French8b217fe2016-11-11 22:36:20 -06003137 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003138 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003139 /*
3140 * tcon has refcount already incremented but we need to
3141 * decrement extra ses reference gotten by caller (case b)
3142 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003143 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003144 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003145 return tcon;
3146 }
3147
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003148 if (!ses->server->ops->tree_connect) {
3149 rc = -ENOSYS;
3150 goto out_fail;
3151 }
3152
Jeff Laytond00c28d2010-04-24 07:57:44 -04003153 tcon = tconInfoAlloc();
3154 if (tcon == NULL) {
3155 rc = -ENOMEM;
3156 goto out_fail;
3157 }
3158
Steve French8b217fe2016-11-11 22:36:20 -06003159 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003160 if (ses->server->vals->protocol_id == 0) {
3161 cifs_dbg(VFS,
3162 "Use SMB2 or later for snapshot mount option\n");
3163 rc = -EOPNOTSUPP;
3164 goto out_fail;
3165 } else
3166 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003167 }
3168
Jeff Laytond00c28d2010-04-24 07:57:44 -04003169 tcon->ses = ses;
3170 if (volume_info->password) {
3171 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3172 if (!tcon->password) {
3173 rc = -ENOMEM;
3174 goto out_fail;
3175 }
3176 }
3177
Steve French23657ad2018-04-22 15:14:58 -05003178 if (volume_info->seal) {
3179 if (ses->server->vals->protocol_id == 0) {
3180 cifs_dbg(VFS,
3181 "SMB3 or later required for encryption\n");
3182 rc = -EOPNOTSUPP;
3183 goto out_fail;
3184 } else if (tcon->ses->server->capabilities &
3185 SMB2_GLOBAL_CAP_ENCRYPTION)
3186 tcon->seal = true;
3187 else {
3188 cifs_dbg(VFS, "Encryption is not supported on share\n");
3189 rc = -EOPNOTSUPP;
3190 goto out_fail;
3191 }
3192 }
3193
Steve French8505c8b2018-06-18 14:01:59 -05003194 if (volume_info->linux_ext) {
3195 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003196 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003197 printk_once(KERN_WARNING
3198 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003199 } else {
3200 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3201 rc = -EOPNOTSUPP;
3202 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003203 }
Steve Frenchb3266142018-05-20 23:41:10 -05003204 }
Steve Frenchb3266142018-05-20 23:41:10 -05003205
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003206 /*
3207 * BB Do we need to wrap session_mutex around this TCon call and Unix
3208 * SetFS as we do on SessSetup and reconnect?
3209 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003210 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003211 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3212 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003213 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003214 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003215 if (rc)
3216 goto out_fail;
3217
3218 if (volume_info->nodfs) {
3219 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
Joe Perchesf96637b2013-05-04 22:12:25 -05003220 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003221 }
Steve Frenchb618f002015-11-03 09:15:03 -06003222 tcon->use_persistent = false;
3223 /* check if SMB2 or later, CIFS does not support persistent handles */
3224 if (volume_info->persistent) {
3225 if (ses->server->vals->protocol_id == 0) {
3226 cifs_dbg(VFS,
3227 "SMB3 or later required for persistent handles\n");
3228 rc = -EOPNOTSUPP;
3229 goto out_fail;
3230 } else if (ses->server->capabilities &
3231 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3232 tcon->use_persistent = true;
3233 else /* persistent handles requested but not supported */ {
3234 cifs_dbg(VFS,
3235 "Persistent handles not supported on share\n");
3236 rc = -EOPNOTSUPP;
3237 goto out_fail;
3238 }
3239 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3240 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3241 && (volume_info->nopersistent == false)) {
3242 cifs_dbg(FYI, "enabling persistent handles\n");
3243 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003244 } else if (volume_info->resilient) {
3245 if (ses->server->vals->protocol_id == 0) {
3246 cifs_dbg(VFS,
3247 "SMB2.1 or later required for resilient handles\n");
3248 rc = -EOPNOTSUPP;
3249 goto out_fail;
3250 }
3251 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003252 }
3253
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003254 /*
3255 * We can have only one retry value for a connection to a share so for
3256 * resources mounted more than once to the same server share the last
3257 * value passed in for the retry flag is used.
3258 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003259 tcon->retry = volume_info->retry;
3260 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003261 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003262 tcon->local_lease = volume_info->local_lease;
Steve French2e96c932019-09-11 21:46:20 -05003263 tcon->no_lease = volume_info->no_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003264 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003265
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303266 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003267 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303268 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003269
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303270 cifs_fscache_get_super_cookie(tcon);
3271
Jeff Laytond00c28d2010-04-24 07:57:44 -04003272 return tcon;
3273
3274out_fail:
3275 tconInfoFree(tcon);
3276 return ERR_PTR(rc);
3277}
3278
Jeff Layton9d002df2010-10-06 19:51:11 -04003279void
3280cifs_put_tlink(struct tcon_link *tlink)
3281{
3282 if (!tlink || IS_ERR(tlink))
3283 return;
3284
3285 if (!atomic_dec_and_test(&tlink->tl_count) ||
3286 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3287 tlink->tl_time = jiffies;
3288 return;
3289 }
3290
3291 if (!IS_ERR(tlink_tcon(tlink)))
3292 cifs_put_tcon(tlink_tcon(tlink));
3293 kfree(tlink);
3294 return;
3295}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003296
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003297static int
3298compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3299{
3300 struct cifs_sb_info *old = CIFS_SB(sb);
3301 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)987564c2019-06-18 16:16:02 -03003302 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
3303 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003304
3305 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3306 return 0;
3307
Paulo Alcantara (SUSE)987564c2019-06-18 16:16:02 -03003308 if (old->mnt_cifs_serverino_autodisabled)
3309 newflags &= ~CIFS_MOUNT_SERVER_INUM;
3310
3311 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003312 return 0;
3313
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003314 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003315 * We want to share sb only if we don't specify an r/wsize or
3316 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003317 */
3318 if (new->wsize && new->wsize < old->wsize)
3319 return 0;
3320
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003321 if (new->rsize && new->rsize < old->rsize)
3322 return 0;
3323
Eric W. Biederman1f682332013-02-06 01:20:20 -08003324 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003325 return 0;
3326
3327 if (old->mnt_file_mode != new->mnt_file_mode ||
3328 old->mnt_dir_mode != new->mnt_dir_mode)
3329 return 0;
3330
3331 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3332 return 0;
3333
3334 if (old->actimeo != new->actimeo)
3335 return 0;
3336
3337 return 1;
3338}
3339
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003340static int
3341match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3342{
3343 struct cifs_sb_info *old = CIFS_SB(sb);
3344 struct cifs_sb_info *new = mnt_data->cifs_sb;
Ronnie Sahlberg35c9bcb2020-01-22 11:07:56 +10003345 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3346 old->prepath;
3347 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3348 new->prepath;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003349
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003350 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003351 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003352 else if (!old_set && !new_set)
3353 return 1;
3354
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003355 return 0;
3356}
3357
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003358int
3359cifs_match_super(struct super_block *sb, void *data)
3360{
3361 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3362 struct smb_vol *volume_info;
3363 struct cifs_sb_info *cifs_sb;
3364 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003365 struct cifs_ses *ses;
3366 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003367 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003368 int rc = 0;
3369
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003370 spin_lock(&cifs_tcp_ses_lock);
3371 cifs_sb = CIFS_SB(sb);
3372 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3373 if (IS_ERR(tlink)) {
3374 spin_unlock(&cifs_tcp_ses_lock);
3375 return rc;
3376 }
3377 tcon = tlink_tcon(tlink);
3378 ses = tcon->ses;
3379 tcp_srv = ses->server;
3380
3381 volume_info = mnt_data->vol;
3382
Jeff Layton9fa114f2012-11-26 11:09:57 -05003383 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003384 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003385 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003386 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003387 rc = 0;
3388 goto out;
3389 }
3390
3391 rc = compare_mount_options(sb, mnt_data);
3392out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003393 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003394 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003395 return rc;
3396}
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003399get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003400 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3401 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 int rc = 0;
3404
Aurelien Aptelb327a712018-01-24 13:46:10 +01003405 if (!ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003406 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003408 *num_referrals = 0;
3409 *referrals = NULL;
3410
Aurelien Aptelb327a712018-01-24 13:46:10 +01003411 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3412 referrals, num_referrals,
3413 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return rc;
3415}
3416
Jeff Layton09e50d52008-07-23 10:11:19 -04003417#ifdef CONFIG_DEBUG_LOCK_ALLOC
3418static struct lock_class_key cifs_key[2];
3419static struct lock_class_key cifs_slock_key[2];
3420
3421static inline void
3422cifs_reclassify_socket4(struct socket *sock)
3423{
3424 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003425 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003426 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3427 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3428}
3429
3430static inline void
3431cifs_reclassify_socket6(struct socket *sock)
3432{
3433 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003434 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003435 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3436 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3437}
3438#else
3439static inline void
3440cifs_reclassify_socket4(struct socket *sock)
3441{
3442}
3443
3444static inline void
3445cifs_reclassify_socket6(struct socket *sock)
3446{
3447}
3448#endif
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003451static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Steve French50c2f752007-07-13 00:33:32 +00003453 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Steve French50c2f752007-07-13 00:33:32 +00003455 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 /* mask a nibble at a time and encode */
3457 target[j] = 'A' + (0x0F & (source[i] >> 4));
3458 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003459 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
3461
3462}
3463
Ben Greear3eb9a882010-09-01 17:06:02 -07003464static int
3465bind_socket(struct TCP_Server_Info *server)
3466{
3467 int rc = 0;
3468 if (server->srcaddr.ss_family != AF_UNSPEC) {
3469 /* Bind to the specified local IP address */
3470 struct socket *socket = server->ssocket;
3471 rc = socket->ops->bind(socket,
3472 (struct sockaddr *) &server->srcaddr,
3473 sizeof(server->srcaddr));
3474 if (rc < 0) {
3475 struct sockaddr_in *saddr4;
3476 struct sockaddr_in6 *saddr6;
3477 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3478 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3479 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003480 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3481 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003482 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003483 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3484 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003485 }
3486 }
3487 return rc;
3488}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003491ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
3493 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003494 /*
3495 * some servers require RFC1001 sessinit before sending
3496 * negprot - BB check reconnection in case where second
3497 * sessinit is sent but no second negprot
3498 */
3499 struct rfc1002_session_packet *ses_init_buf;
3500 struct smb_hdr *smb_buf;
3501 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3502 GFP_KERNEL);
3503 if (ses_init_buf) {
3504 ses_init_buf->trailer.session_req.called_len = 32;
3505
Colin Ian King997152f2016-01-25 16:25:54 +00003506 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003507 rfc1002mangle(ses_init_buf->trailer.
3508 session_req.called_name,
3509 server->server_RFC1001_name,
3510 RFC1001_NAME_LEN_WITH_NULL);
3511 else
3512 rfc1002mangle(ses_init_buf->trailer.
3513 session_req.called_name,
3514 DEFAULT_CIFS_CALLED_NAME,
3515 RFC1001_NAME_LEN_WITH_NULL);
3516
3517 ses_init_buf->trailer.session_req.calling_len = 32;
3518
3519 /*
3520 * calling name ends in null (byte 16) from old smb
3521 * convention.
3522 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003523 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003524 rfc1002mangle(ses_init_buf->trailer.
3525 session_req.calling_name,
3526 server->workstation_RFC1001_name,
3527 RFC1001_NAME_LEN_WITH_NULL);
3528 else
3529 rfc1002mangle(ses_init_buf->trailer.
3530 session_req.calling_name,
3531 "LINUX_CIFS_CLNT",
3532 RFC1001_NAME_LEN_WITH_NULL);
3533
3534 ses_init_buf->trailer.session_req.scope1 = 0;
3535 ses_init_buf->trailer.session_req.scope2 = 0;
3536 smb_buf = (struct smb_hdr *)ses_init_buf;
3537
3538 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003539 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003540 rc = smb_send(server, smb_buf, 0x44);
3541 kfree(ses_init_buf);
3542 /*
3543 * RFC1001 layer in at least one server
3544 * requires very short break before negprot
3545 * presumably because not expecting negprot
3546 * to follow so fast. This is a simple
3547 * solution that works without
3548 * complicating the code and causes no
3549 * significant slowing down on mount
3550 * for everyone else
3551 */
3552 usleep_range(1000, 2000);
3553 }
3554 /*
3555 * else the negprot may still work without this
3556 * even though malloc failed
3557 */
3558
3559 return rc;
3560}
3561
3562static int
3563generic_ip_connect(struct TCP_Server_Info *server)
3564{
3565 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003566 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003567 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003568 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003569 struct sockaddr *saddr;
3570
3571 saddr = (struct sockaddr *) &server->dstaddr;
3572
3573 if (server->dstaddr.ss_family == AF_INET6) {
3574 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3575 slen = sizeof(struct sockaddr_in6);
3576 sfamily = AF_INET6;
3577 } else {
3578 sport = ((struct sockaddr_in *) saddr)->sin_port;
3579 slen = sizeof(struct sockaddr_in);
3580 sfamily = AF_INET;
3581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003583 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003584 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3585 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003587 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003588 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003591
3592 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003593 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003594 server->ssocket = socket;
3595 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003596 if (sfamily == AF_INET6)
3597 cifs_reclassify_socket6(socket);
3598 else
3599 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 }
3601
Ben Greear3eb9a882010-09-01 17:06:02 -07003602 rc = bind_socket(server);
3603 if (rc < 0)
3604 return rc;
3605
Jeff Laytond5c56052008-12-01 18:42:33 -05003606 /*
3607 * Eventually check for other socket options to change from
3608 * the default. sock_setsockopt not used because it expects
3609 * user space buffer
3610 */
3611 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003612 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003613
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003614 /* make the bufsizes depend on wsize/rsize and max requests */
3615 if (server->noautotune) {
3616 if (socket->sk->sk_sndbuf < (200 * 1024))
3617 socket->sk->sk_sndbuf = 200 * 1024;
3618 if (socket->sk->sk_rcvbuf < (140 * 1024))
3619 socket->sk->sk_rcvbuf = 140 * 1024;
3620 }
3621
Steve French6a5fa2362010-01-01 01:28:43 +00003622 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003623 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003624 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3625 (char *)&val, sizeof(val));
3626 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003627 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3628 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003629 }
3630
Joe Perchesf96637b2013-05-04 22:12:25 -05003631 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003632 socket->sk->sk_sndbuf,
3633 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3634
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003635 rc = socket->ops->connect(socket, saddr, slen, 0);
3636 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003637 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003638 sock_release(socket);
3639 server->ssocket = NULL;
3640 return rc;
3641 }
3642
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003643 if (sport == htons(RFC1001_PORT))
3644 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return rc;
3647}
3648
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003649static int
3650ip_connect(struct TCP_Server_Info *server)
3651{
Steve French6da97912011-03-13 18:55:55 +00003652 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003653 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3654 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3655
3656 if (server->dstaddr.ss_family == AF_INET6)
3657 sport = &addr6->sin6_port;
3658 else
3659 sport = &addr->sin_port;
3660
3661 if (*sport == 0) {
3662 int rc;
3663
3664 /* try with 445 port at first */
3665 *sport = htons(CIFS_PORT);
3666
3667 rc = generic_ip_connect(server);
3668 if (rc >= 0)
3669 return rc;
3670
3671 /* if it failed, try with 139 port */
3672 *sport = htons(RFC1001_PORT);
3673 }
3674
3675 return generic_ip_connect(server);
3676}
3677
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003678void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003679 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003680{
3681 /* if we are reconnecting then should we check to see if
3682 * any requested capabilities changed locally e.g. via
3683 * remount but we can not do much about it here
3684 * if they have (even if we could detect it by the following)
3685 * Perhaps we could add a backpointer to array of sb from tcon
3686 * or if we change to make all sb to same share the same
3687 * sb as NFS - then we only have one backpointer to sb.
3688 * What if we wanted to mount the server share twice once with
3689 * and once without posixacls or posix paths? */
3690 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003691
Steve Frenchc18c8422007-07-18 23:21:09 +00003692 if (vol_info && vol_info->no_linux_ext) {
3693 tcon->fsUnixInfo.Capability = 0;
3694 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003695 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003696 return;
3697 } else if (vol_info)
3698 tcon->unix_ext = 1; /* Unix Extensions supported */
3699
3700 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003701 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003702 return;
3703 }
Steve French50c2f752007-07-13 00:33:32 +00003704
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003705 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003706 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003707 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003708 /* check for reconnect case in which we do not
3709 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003710 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003711 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003712 originally at mount time */
3713 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3714 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003715 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3716 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003717 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003718 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003719 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003720 cifs_dbg(VFS, "possible reconnect error\n");
3721 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003722 }
Steve French8af18972007-02-14 04:42:51 +00003723 }
Steve French50c2f752007-07-13 00:33:32 +00003724
Steve French6848b732011-05-26 18:38:54 +00003725 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003726 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003727
Steve French8af18972007-02-14 04:42:51 +00003728 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003729 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003730 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003731 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003732 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003733 if (cifs_sb)
3734 cifs_sb->mnt_cifs_flags |=
3735 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003736 }
3737
Steve French75865f8c2007-06-24 18:30:48 +00003738 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003739 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003740 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003741 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003742 if (cifs_sb)
3743 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003744 CIFS_MOUNT_POSIX_PATHS;
3745 }
Steve French50c2f752007-07-13 00:33:32 +00003746
Joe Perchesf96637b2013-05-04 22:12:25 -05003747 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003748#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003749 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003750 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003751 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003752 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003753 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003754 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003755 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003756 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003757 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003758 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003759 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003760 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003761 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003762 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003763 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003764 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003765 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003766 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003767#endif /* CIFS_DEBUG2 */
3768 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003769 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003770 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003771 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003772 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 +00003773
Steve French8af18972007-02-14 04:42:51 +00003774 }
3775 }
3776}
3777
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003778int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003779 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003780{
Jeff Layton2de970f2010-10-06 19:51:12 -04003781 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3782
Al Viro2ced6f62011-06-17 09:20:04 -04003783 spin_lock_init(&cifs_sb->tlink_tree_lock);
3784 cifs_sb->tlink_tree = RB_ROOT;
3785
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003786 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003787 * Temporarily set r/wsize for matching superblock. If we end up using
3788 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003789 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003790 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003791 cifs_sb->wsize = pvolume_info->wsize;
3792
Steve French3b795212008-11-13 19:45:32 +00003793 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3794 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3795 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3796 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Frank Sorensonb6b77b52020-02-12 15:31:48 -06003797 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003798 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003799
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303800 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003801 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303802
Steve French3b795212008-11-13 19:45:32 +00003803 if (pvolume_info->noperm)
3804 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3805 if (pvolume_info->setuids)
3806 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003807 if (pvolume_info->setuidfromacl)
3808 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003809 if (pvolume_info->server_ino)
3810 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3811 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003812 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3813 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003814 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3815 if (pvolume_info->no_xattr)
3816 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3817 if (pvolume_info->sfu_emul)
3818 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3819 if (pvolume_info->nobrl)
3820 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003821 if (pvolume_info->nohandlecache)
3822 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003823 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003824 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003825 if (pvolume_info->mand_lock)
3826 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003827 if (pvolume_info->rwpidforward)
3828 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003829 if (pvolume_info->cifs_acl)
3830 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003831 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003832 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003833 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3834 }
3835 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003836 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003837 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3838 }
Steve French3b795212008-11-13 19:45:32 +00003839 if (pvolume_info->override_uid)
3840 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3841 if (pvolume_info->override_gid)
3842 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3843 if (pvolume_info->dynperm)
3844 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303845 if (pvolume_info->fsc)
3846 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003847 if (pvolume_info->multiuser)
3848 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3849 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003850 if (pvolume_info->strict_io)
3851 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003852 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003853 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003854 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3855 }
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003856 if (pvolume_info->mfsymlinks) {
3857 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003858 /*
3859 * Our SFU ("Services for Unix" emulation does not allow
3860 * creating symlinks but does allow reading existing SFU
3861 * symlinks (it does allow both creating and reading SFU
3862 * style mknod and FIFOs though). When "mfsymlinks" and
3863 * "sfu" are both enabled at the same time, it allows
3864 * reading both types of symlinks, but will only create
3865 * them with mfsymlinks format. This allows better
3866 * Apple compatibility (probably better for Samba too)
3867 * while still recognizing old Windows style symlinks.
3868 */
3869 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003870 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003871 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003872 }
Steve French3b795212008-11-13 19:45:32 +00003873
3874 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003875 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003876
3877 if (pvolume_info->prepath) {
3878 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3879 if (cifs_sb->prepath == NULL)
3880 return -ENOMEM;
3881 }
3882
3883 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003884}
3885
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003886static void
3887cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003888{
Sean Finneyb9468452011-04-11 13:19:32 +00003889 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003890 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003891 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003892 kfree(volume_info->domainname);
3893 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003894 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003895}
3896
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003897void
3898cifs_cleanup_volume_info(struct smb_vol *volume_info)
3899{
3900 if (!volume_info)
3901 return;
3902 cleanup_volume_info_contents(volume_info);
3903 kfree(volume_info);
3904}
3905
3906
Steve French2d6d5892009-04-09 00:36:44 +00003907#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003908/*
3909 * cifs_build_path_to_root returns full path to root when we do not have an
3910 * exiting connection (tcon)
3911 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003912static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003913build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003914 const struct cifs_sb_info *cifs_sb)
3915{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003916 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003917 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003918 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003919
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003920 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003921 if (full_path == NULL)
3922 return ERR_PTR(-ENOMEM);
3923
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003924 strncpy(full_path, vol->UNC, unc_len);
3925 pos = full_path + unc_len;
3926
3927 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003928 *pos = CIFS_DIR_SEP(cifs_sb);
3929 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003930 pos += pplen;
3931 }
3932
3933 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003934 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003935 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003936 return full_path;
3937}
Sean Finneydd613942011-04-11 13:19:30 +00003938
3939/*
3940 * Perform a dfs referral query for a share and (optionally) prefix
3941 *
Sean Finney046462a2011-04-11 13:19:33 +00003942 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3943 * to a string containing updated options for the submount. Otherwise it
3944 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003945 *
3946 * Returns the rc from get_dfs_path to the caller, which can be used to
3947 * determine whether there were referrals.
3948 */
3949static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003950expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003951 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003952 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003953{
3954 int rc;
3955 unsigned int num_referrals = 0;
3956 struct dfs_info3_param *referrals = NULL;
3957 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3958
3959 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3960 if (IS_ERR(full_path))
3961 return PTR_ERR(full_path);
3962
3963 /* For DFS paths, skip the first '\' of the UNC */
3964 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3965
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003966 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003967 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003968
3969 if (!rc && num_referrals > 0) {
3970 char *fake_devname = NULL;
3971
3972 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3973 full_path + 1, referrals,
3974 &fake_devname);
3975
3976 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003977
Sean Finneydd613942011-04-11 13:19:30 +00003978 if (IS_ERR(mdata)) {
3979 rc = PTR_ERR(mdata);
3980 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003981 } else {
3982 cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003983 rc = cifs_setup_volume_info(volume_info, mdata,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003984 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00003985 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003986 kfree(fake_devname);
3987 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003988 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003989 }
3990 kfree(full_path);
3991 return rc;
3992}
Steve French2d6d5892009-04-09 00:36:44 +00003993#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003994
Jeff Layton04db79b2011-07-06 08:10:38 -04003995static int
3996cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003997 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003999 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00004000
Steve Frenchc7c137b2018-06-06 17:59:29 -05004001 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04004002 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Jeff Layton7586b762008-12-01 18:41:49 -05004004 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004005 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05004006 kfree(volume_info->username);
4007 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05004008 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05004010 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05004012 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00004013 /* In userspace mount helper we can get user name from alternate
4014 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04004015 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 }
4017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05004019 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004020 /* load_nls_default cannot return null */
4021 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004023 volume_info->local_nls = load_nls(volume_info->iocharset);
4024 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004025 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004026 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04004027 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 }
4029 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004030
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004031 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04004032}
4033
4034struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05004035cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04004036{
4037 int rc;
4038 struct smb_vol *volume_info;
4039
Jeff Layton6ee95422012-11-26 11:09:57 -05004040 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04004041 if (!volume_info)
4042 return ERR_PTR(-ENOMEM);
4043
Steve Frenchc7c137b2018-06-06 17:59:29 -05004044 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04004045 if (rc) {
4046 cifs_cleanup_volume_info(volume_info);
4047 volume_info = ERR_PTR(rc);
4048 }
4049
4050 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004051}
4052
Aurelien Aptela6b50582016-05-25 19:59:09 +02004053static int
4054cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4055 unsigned int xid,
4056 struct cifs_tcon *tcon,
4057 struct cifs_sb_info *cifs_sb,
4058 char *full_path)
4059{
4060 int rc;
4061 char *s;
4062 char sep, tmp;
4063
4064 sep = CIFS_DIR_SEP(cifs_sb);
4065 s = full_path;
4066
4067 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4068 while (rc == 0) {
4069 /* skip separators */
4070 while (*s == sep)
4071 s++;
4072 if (!*s)
4073 break;
4074 /* next separator */
4075 while (*s && *s != sep)
4076 s++;
4077
4078 /*
4079 * temporarily null-terminate the path at the end of
4080 * the current component
4081 */
4082 tmp = *s;
4083 *s = 0;
4084 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4085 full_path);
4086 *s = tmp;
4087 }
4088 return rc;
4089}
4090
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004091int
Al Viro2c6292a2011-06-17 09:05:48 -04004092cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004093{
Jeff Layton1daaae82012-03-21 06:30:40 -04004094 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004095 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004096 struct cifs_ses *ses;
Steve French96daf2b2011-05-27 04:34:02 +00004097 struct cifs_tcon *tcon;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004098 struct TCP_Server_Info *server;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004099 char *full_path;
4100 struct tcon_link *tlink;
4101#ifdef CONFIG_CIFS_DFS_UPCALL
4102 int referral_walks_count = 0;
Jeff Layton20547492011-07-09 12:21:07 -04004103#endif
Al Virodd854462011-06-17 08:24:42 -04004104
Jeff Layton20547492011-07-09 12:21:07 -04004105#ifdef CONFIG_CIFS_DFS_UPCALL
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004106try_mount_again:
4107 /* cleanup activities if we're chasing a referral */
4108 if (referral_walks_count) {
4109 if (tcon)
4110 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004111 else if (ses)
4112 cifs_put_smb_ses(ses);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004113
Sachin Prabhu1dfd18d2015-06-16 16:36:17 +01004114 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
4115
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004116 free_xid(xid);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004117 }
4118#endif
Jeff Layton1daaae82012-03-21 06:30:40 -04004119 rc = 0;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004120 tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004121 ses = NULL;
4122 server = NULL;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004123 full_path = NULL;
4124 tlink = NULL;
4125
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004126 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
Jeff Layton63c038c2008-12-01 18:41:46 -05004128 /* get a reference to a tcp session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004129 server = cifs_get_tcp_session(volume_info);
4130 if (IS_ERR(server)) {
4131 rc = PTR_ERR(server);
Jeff Layton63c038c2008-12-01 18:41:46 -05004132 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 }
Steve French141891f2016-09-23 00:44:16 -05004134 if ((volume_info->max_credits < 20) ||
4135 (volume_info->max_credits > 60000))
4136 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
4137 else
4138 server->max_credits = volume_info->max_credits;
Jeff Layton36988c72010-04-24 07:57:43 -04004139 /* get a reference to a SMB session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004140 ses = cifs_get_smb_ses(server, volume_info);
4141 if (IS_ERR(ses)) {
4142 rc = PTR_ERR(ses);
4143 ses = NULL;
Jeff Layton36988c72010-04-24 07:57:43 -04004144 goto mount_fail_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 }
Steve French50c2f752007-07-13 00:33:32 +00004146
Steve Frenchb618f002015-11-03 09:15:03 -06004147 if ((volume_info->persistent == true) && ((ses->server->capabilities &
4148 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
4149 cifs_dbg(VFS, "persistent handles not supported by server\n");
4150 rc = -EOPNOTSUPP;
4151 goto mount_fail_check;
4152 }
Steve French592fafe2015-11-03 10:08:53 -06004153
Jeff Laytond00c28d2010-04-24 07:57:44 -04004154 /* search for existing tcon to this server share */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004155 tcon = cifs_get_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04004156 if (IS_ERR(tcon)) {
4157 rc = PTR_ERR(tcon);
4158 tcon = NULL;
Mark Syms40920c22016-11-29 11:36:46 +00004159 if (rc == -EACCES)
4160 goto mount_fail_check;
4161
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004162 goto remote_path_check;
Jeff Laytond00c28d2010-04-24 07:57:44 -04004163 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004164
Steve Frenchce558b02018-05-31 19:16:54 -05004165 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4166 if (tcon->posix_extensions)
4167 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve Frenchce558b02018-05-31 19:16:54 -05004168
Steve French6848b732011-05-26 18:38:54 +00004169 /* tell server which Unix caps we support */
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004170 if (cap_unix(tcon->ses)) {
Steve French6848b732011-05-26 18:38:54 +00004171 /* reset of caps checks mount to see if unix extensions
4172 disabled for just this mount */
Al Viro2c6292a2011-06-17 09:05:48 -04004173 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
Steve French6848b732011-05-26 18:38:54 +00004174 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4175 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4176 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
4177 rc = -EACCES;
4178 goto mount_fail_check;
4179 }
4180 } else
4181 tcon->unix_ext = 0; /* server does not support them */
4182
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004183 /* do not care if a following call succeed - informational */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004184 if (!tcon->pipe && server->ops->qfs_tcon)
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004185 server->ops->qfs_tcon(xid, tcon);
Steve Frenchd82c2df2008-11-15 00:07:26 +00004186
Pavel Shilovsky24985c52012-09-18 16:20:28 -07004187 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
4188 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04004189
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004190remote_path_check:
Sean Finneyc1508ca2011-04-11 13:19:31 +00004191#ifdef CONFIG_CIFS_DFS_UPCALL
4192 /*
4193 * Perform an unconditional check for whether there are DFS
4194 * referrals for this path without prefix, to provide support
4195 * for DFS referrals from w2k8 servers which don't seem to respond
4196 * with PATH_NOT_COVERED to requests that include the prefix.
4197 * Chase the referral if found, otherwise continue normally.
4198 */
4199 if (referral_walks_count == 0) {
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004200 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
4201 false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004202 if (!refrc) {
4203 referral_walks_count++;
4204 goto try_mount_again;
4205 }
4206 }
4207#endif
4208
Steve Frenchf87d39d2011-05-27 03:50:55 +00004209 /* check if a whole path is not remote */
Jeff Layton70945642011-03-14 13:48:08 -04004210 if (!rc && tcon) {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004211 if (!server->ops->is_path_accessible) {
4212 rc = -ENOSYS;
4213 goto mount_fail_check;
4214 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004215 /*
4216 * cifs_build_path_to_root works only when we have a valid tcon
4217 */
Sachin Prabhu374402a2016-12-15 12:31:19 +05304218 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
4219 tcon->Flags & SMB_SHARE_IS_IN_DFS);
Igor Mammedove4cce942009-02-10 14:10:26 +03004220 if (full_path == NULL) {
4221 rc = -ENOMEM;
4222 goto mount_fail_check;
4223 }
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004224 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4225 full_path);
Jeff Layton03ceace2010-12-06 21:07:33 -05004226 if (rc != 0 && rc != -EREMOTE) {
Igor Mammedove4cce942009-02-10 14:10:26 +03004227 kfree(full_path);
4228 goto mount_fail_check;
4229 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004230
Sachin Prabhud1713562016-09-06 13:22:34 +01004231 if (rc != -EREMOTE) {
4232 rc = cifs_are_all_path_components_accessible(server,
Aurelien Aptela6b50582016-05-25 19:59:09 +02004233 xid, tcon, cifs_sb,
4234 full_path);
Sachin Prabhud1713562016-09-06 13:22:34 +01004235 if (rc != 0) {
4236 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4237 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4238 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4239 rc = 0;
4240 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004241 }
Igor Mammedove4cce942009-02-10 14:10:26 +03004242 kfree(full_path);
4243 }
4244
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004245 /* get referral if needed */
4246 if (rc == -EREMOTE) {
Steve Frenchd036f502009-04-03 03:12:08 +00004247#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004248 if (referral_walks_count > MAX_NESTED_LINKS) {
4249 /*
4250 * BB: when we implement proper loop detection,
4251 * we will remove this check. But now we need it
4252 * to prevent an indefinite loop if 'DFS tree' is
4253 * misconfigured (i.e. has loops).
4254 */
4255 rc = -ELOOP;
4256 goto mount_fail_check;
4257 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004258
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004259 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
Jeff Layton7b91e262009-07-23 15:22:30 -04004260
Sean Finneydd613942011-04-11 13:19:30 +00004261 if (!rc) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004262 referral_walks_count++;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004263 goto try_mount_again;
4264 }
Sean Finneydd613942011-04-11 13:19:30 +00004265 goto mount_fail_check;
Steve Frenchd036f502009-04-03 03:12:08 +00004266#else /* No DFS support, return error on mount */
4267 rc = -EOPNOTSUPP;
4268#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004269 }
4270
Jeff Layton9d002df2010-10-06 19:51:11 -04004271 if (rc)
4272 goto mount_fail_check;
4273
4274 /* now, hang the tcon off of the superblock */
4275 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
4276 if (tlink == NULL) {
4277 rc = -ENOMEM;
4278 goto mount_fail_check;
4279 }
4280
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004281 tlink->tl_uid = ses->linux_uid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004282 tlink->tl_tcon = tcon;
4283 tlink->tl_time = jiffies;
4284 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4285 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4286
Jeff Layton413e6612010-10-28 13:33:38 -04004287 cifs_sb->master_tlink = tlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004288 spin_lock(&cifs_sb->tlink_tree_lock);
4289 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4290 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton413e6612010-10-28 13:33:38 -04004291
Jeff Laytonda472fc2012-03-23 14:40:53 -04004292 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004293 TLINK_IDLE_EXPIRE);
4294
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004295mount_fail_check:
4296 /* on error free sesinfo and tcon struct if needed */
4297 if (rc) {
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004298 /* If find_unc succeeded then rc == 0 so we can not end */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004299 /* up accidentally freeing someone elses tcon struct */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004300 if (tcon)
4301 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004302 else if (ses)
4303 cifs_put_smb_ses(ses);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004304 else
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004305 cifs_put_tcp_session(server, 0);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004306 }
4307
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004308out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004309 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return rc;
4311}
4312
Jeff Layton8d1bca32011-06-11 21:17:10 -04004313/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004314 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004317CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004318 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 const struct nls_table *nls_codepage)
4320{
4321 struct smb_hdr *smb_buffer;
4322 struct smb_hdr *smb_buffer_response;
4323 TCONX_REQ *pSMB;
4324 TCONX_RSP *pSMBr;
4325 unsigned char *bcc_ptr;
4326 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004327 int length;
4328 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
4330 if (ses == NULL)
4331 return -EIO;
4332
4333 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004334 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 smb_buffer_response = smb_buffer;
4338
4339 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4340 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004341
Pavel Shilovsky88257362012-05-23 14:01:59 +04004342 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 smb_buffer->Uid = ses->Suid;
4344 pSMB = (TCONX_REQ *) smb_buffer;
4345 pSMBr = (TCONX_RSP *) smb_buffer_response;
4346
4347 pSMB->AndXCommand = 0xFF;
4348 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004350 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004351 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004352 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004353 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004354 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004355 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004356 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004357 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4358 specified as required (when that support is added to
4359 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004360 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004361 by Samba (not sure whether other servers allow
4362 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004363#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004364 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004365 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004366 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004367 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004368 SECMODE_PW_ENCRYPT ? true : false,
4369 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004370 else
4371#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004372 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004373 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004374 if (rc) {
4375 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4376 __func__, rc);
4377 cifs_buf_release(smb_buffer);
4378 return rc;
4379 }
Steve Frencheeac8042006-01-13 21:34:58 -08004380
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004381 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004382 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004383 /* must align unicode strings */
4384 *bcc_ptr = 0; /* null byte password */
4385 bcc_ptr++;
4386 }
Steve Frencheeac8042006-01-13 21:34:58 -08004387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Jeff Layton38d77c52013-05-26 07:01:00 -04004389 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4391
4392 if (ses->capabilities & CAP_STATUS32) {
4393 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4394 }
4395 if (ses->capabilities & CAP_DFS) {
4396 smb_buffer->Flags2 |= SMBFLG2_DFS;
4397 }
4398 if (ses->capabilities & CAP_UNICODE) {
4399 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4400 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004401 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004402 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004403 (/* server len*/ + 256 /* share len */), nls_codepage);
4404 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 bcc_ptr += 2; /* skip trailing null */
4406 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 strcpy(bcc_ptr, tree);
4408 bcc_ptr += strlen(tree) + 1;
4409 }
4410 strcpy(bcc_ptr, "?????");
4411 bcc_ptr += strlen("?????");
4412 bcc_ptr += 1;
4413 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004414 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4415 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 pSMB->ByteCount = cpu_to_le16(count);
4417
Steve French133672e2007-11-13 22:41:37 +00004418 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004419 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004422 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004423 bool is_unicode;
4424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004426 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 tcon->tid = smb_buffer_response->Tid;
4428 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004429 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004430 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004431 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4432 is_unicode = true;
4433 else
4434 is_unicode = false;
4435
Jeff Laytoncc20c032009-04-30 07:16:21 -04004436
Steve French50c2f752007-07-13 00:33:32 +00004437 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004438 if (length == 3) {
4439 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4440 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004441 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004442 tcon->ipc = true;
4443 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004444 }
4445 } else if (length == 2) {
4446 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4447 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004448 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004449 }
4450 }
Steve French50c2f752007-07-13 00:33:32 +00004451 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004452 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004453 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004454
4455 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004456 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004457 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004458 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004459 nls_codepage);
4460
Joe Perchesf96637b2013-05-04 22:12:25 -05004461 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004462
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004463 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004464 (smb_buffer_response->WordCount == 7))
4465 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004466 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4467 else
4468 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004469 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 }
4471
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004472 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 return rc;
4474}
4475
Al Viro2e32cf52013-10-03 12:53:37 -04004476static void delayed_free(struct rcu_head *p)
4477{
4478 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4479 unload_nls(sbi->local_nls);
4480 kfree(sbi);
4481}
4482
Al Viro2a9b9952011-06-17 09:27:16 -04004483void
4484cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485{
Jeff Laytonb647c352010-10-28 11:16:44 -04004486 struct rb_root *root = &cifs_sb->tlink_tree;
4487 struct rb_node *node;
4488 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Jeff Layton2de970f2010-10-06 19:51:12 -04004490 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4491
Jeff Laytonb647c352010-10-28 11:16:44 -04004492 spin_lock(&cifs_sb->tlink_tree_lock);
4493 while ((node = rb_first(root))) {
4494 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4495 cifs_get_tlink(tlink);
4496 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4497 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004498
Jeff Laytonb647c352010-10-28 11:16:44 -04004499 spin_unlock(&cifs_sb->tlink_tree_lock);
4500 cifs_put_tlink(tlink);
4501 spin_lock(&cifs_sb->tlink_tree_lock);
4502 }
4503 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004504
Al Virod757d712011-06-17 09:42:43 -04004505 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004506 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004507 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004508}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004510int
4511cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
4513 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004514 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004516 if (!server->ops->need_neg || !server->ops->negotiate)
4517 return -ENOSYS;
4518
Jeff Layton198b5682010-04-24 07:57:48 -04004519 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004520 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Pavel Shilovsky45275782012-05-17 17:53:29 +04004523 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004524
4525 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004526 if (rc == 0) {
4527 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004528 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004529 server->tcpStatus = CifsGood;
4530 else
4531 rc = -EHOSTDOWN;
4532 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
Steve French26b994f2008-08-06 05:11:33 +00004534
Jeff Layton198b5682010-04-24 07:57:48 -04004535 return rc;
4536}
Steve French26b994f2008-08-06 05:11:33 +00004537
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004538int
4539cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4540 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004541{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004542 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004543 struct TCP_Server_Info *server = ses->server;
4544
Jeff Layton198b5682010-04-24 07:57:48 -04004545 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004546 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004547 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004548
Joe Perchesf96637b2013-05-04 22:12:25 -05004549 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004550 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004551
Shu Wangf5c4ba82017-09-08 18:48:33 +08004552 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05004553 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08004554 ses->auth_key.response);
4555 kfree(ses->auth_key.response);
4556 ses->auth_key.response = NULL;
4557 ses->auth_key.len = 0;
4558 }
4559
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004560 if (server->ops->sess_setup)
4561 rc = server->ops->sess_setup(xid, ses, nls_info);
4562
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004563 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004564 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 return rc;
4567}
4568
Jeff Layton8a8798a2012-01-17 16:09:15 -05004569static int
4570cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4571{
Jeff Layton3f618222013-06-12 19:52:14 -05004572 vol->sectype = ses->sectype;
4573
4574 /* krb5 is special, since we don't need username or pw */
4575 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004576 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004577
4578 return cifs_set_cifscreds(vol, ses);
4579}
4580
Steve French96daf2b2011-05-27 04:34:02 +00004581static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004582cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004583{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004584 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004585 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4586 struct cifs_ses *ses;
4587 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004588 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004589
4590 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004591 if (vol_info == NULL)
4592 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004593
Jeff Layton9d002df2010-10-06 19:51:11 -04004594 vol_info->local_nls = cifs_sb->local_nls;
4595 vol_info->linux_uid = fsuid;
4596 vol_info->cred_uid = fsuid;
4597 vol_info->UNC = master_tcon->treeName;
4598 vol_info->retry = master_tcon->retry;
4599 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05004600 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04004601 vol_info->local_lease = master_tcon->local_lease;
4602 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004603 vol_info->sectype = master_tcon->ses->sectype;
4604 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004605
Jeff Layton8a8798a2012-01-17 16:09:15 -05004606 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4607 if (rc) {
4608 tcon = ERR_PTR(rc);
4609 goto out;
4610 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004611
4612 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304613 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004614 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304615 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004616
4617 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4618 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004619 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004620 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004621 goto out;
4622 }
4623
4624 tcon = cifs_get_tcon(ses, vol_info);
4625 if (IS_ERR(tcon)) {
4626 cifs_put_smb_ses(ses);
4627 goto out;
4628 }
4629
Steve Frenchce558b02018-05-31 19:16:54 -05004630 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4631 if (tcon->posix_extensions)
4632 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05004633
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004634 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004635 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05004636
Jeff Layton9d002df2010-10-06 19:51:11 -04004637out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004638 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01004639 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004640 kfree(vol_info);
4641
4642 return tcon;
4643}
4644
Steve French96daf2b2011-05-27 04:34:02 +00004645struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004646cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4647{
4648 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4649}
4650
Jeff Laytonb647c352010-10-28 11:16:44 -04004651/* find and return a tlink with given uid */
4652static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004653tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004654{
4655 struct rb_node *node = root->rb_node;
4656 struct tcon_link *tlink;
4657
4658 while (node) {
4659 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4660
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004661 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004662 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004663 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004664 node = node->rb_right;
4665 else
4666 return tlink;
4667 }
4668 return NULL;
4669}
4670
4671/* insert a tcon_link into the tree */
4672static void
4673tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4674{
4675 struct rb_node **new = &(root->rb_node), *parent = NULL;
4676 struct tcon_link *tlink;
4677
4678 while (*new) {
4679 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4680 parent = *new;
4681
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004682 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004683 new = &((*new)->rb_left);
4684 else
4685 new = &((*new)->rb_right);
4686 }
4687
4688 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4689 rb_insert_color(&new_tlink->tl_rbnode, root);
4690}
4691
Jeff Layton9d002df2010-10-06 19:51:11 -04004692/*
4693 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4694 * current task.
4695 *
4696 * If the superblock doesn't refer to a multiuser mount, then just return
4697 * the master tcon for the mount.
4698 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304699 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004700 * exists, then check to see if it's pending construction. If it is then wait
4701 * for construction to complete. Once it's no longer pending, check to see if
4702 * it failed and either return an error or retry construction, depending on
4703 * the timeout.
4704 *
4705 * If one doesn't exist then insert a new tcon_link struct into the tree and
4706 * try to construct a new one.
4707 */
4708struct tcon_link *
4709cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4710{
4711 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004712 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004713 struct tcon_link *tlink, *newtlink;
4714
4715 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4716 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4717
4718 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004719 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004720 if (tlink)
4721 cifs_get_tlink(tlink);
4722 spin_unlock(&cifs_sb->tlink_tree_lock);
4723
4724 if (tlink == NULL) {
4725 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4726 if (newtlink == NULL)
4727 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004728 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004729 newtlink->tl_tcon = ERR_PTR(-EACCES);
4730 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4731 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4732 cifs_get_tlink(newtlink);
4733
Jeff Layton9d002df2010-10-06 19:51:11 -04004734 spin_lock(&cifs_sb->tlink_tree_lock);
4735 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004736 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004737 if (tlink) {
4738 cifs_get_tlink(tlink);
4739 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004740 kfree(newtlink);
4741 goto wait_for_construction;
4742 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004743 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004744 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4745 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004746 } else {
4747wait_for_construction:
4748 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004749 TASK_INTERRUPTIBLE);
4750 if (ret) {
4751 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004752 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004753 }
4754
4755 /* if it's good, return it */
4756 if (!IS_ERR(tlink->tl_tcon))
4757 return tlink;
4758
4759 /* return error if we tried this already recently */
4760 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4761 cifs_put_tlink(tlink);
4762 return ERR_PTR(-EACCES);
4763 }
4764
4765 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4766 goto wait_for_construction;
4767 }
4768
4769 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4770 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4771 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4772
4773 if (IS_ERR(tlink->tl_tcon)) {
4774 cifs_put_tlink(tlink);
4775 return ERR_PTR(-EACCES);
4776 }
4777
4778 return tlink;
4779}
Jeff Layton2de970f2010-10-06 19:51:12 -04004780
4781/*
4782 * periodic workqueue job that scans tcon_tree for a superblock and closes
4783 * out tcons.
4784 */
4785static void
4786cifs_prune_tlinks(struct work_struct *work)
4787{
4788 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4789 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004790 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004791 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004792 struct rb_node *tmp;
4793 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004794
Jeff Laytonb647c352010-10-28 11:16:44 -04004795 /*
4796 * Because we drop the spinlock in the loop in order to put the tlink
4797 * it's not guarded against removal of links from the tree. The only
4798 * places that remove entries from the tree are this function and
4799 * umounts. Because this function is non-reentrant and is canceled
4800 * before umount can proceed, this is safe.
4801 */
4802 spin_lock(&cifs_sb->tlink_tree_lock);
4803 node = rb_first(root);
4804 while (node != NULL) {
4805 tmp = node;
4806 node = rb_next(tmp);
4807 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4808
4809 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4810 atomic_read(&tlink->tl_count) != 0 ||
4811 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4812 continue;
4813
4814 cifs_get_tlink(tlink);
4815 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4816 rb_erase(tmp, root);
4817
Jeff Layton2de970f2010-10-06 19:51:12 -04004818 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004819 cifs_put_tlink(tlink);
4820 spin_lock(&cifs_sb->tlink_tree_lock);
4821 }
4822 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004823
Jeff Laytonda472fc2012-03-23 14:40:53 -04004824 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004825 TLINK_IDLE_EXPIRE);
4826}