blob: 966e493c82e5734671fea040fb450a3c1ed6a978 [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
933static int
Al Viro7c97c202011-06-21 08:51:28 -0400934cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000936 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400937 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400938 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000939 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400940 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500941 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000942 struct mid_q_entry *mids[MAX_COMPOUND];
943 char *bufs[MAX_COMPOUND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500946 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400947
948 length = atomic_inc_return(&tcpSesAllocCount);
949 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700950 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700952 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000953 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700954 if (try_to_freeze())
955 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700956
Jeff Layton2a37ef92011-10-19 15:29:23 -0400957 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400958 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700959
Jeff Layton2a37ef92011-10-19 15:29:23 -0400960 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400961 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000962 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000963
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400964 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400965 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000966 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000967
968 if (server->vals->header_preamble_size == 0)
969 server->total_read = 0;
970 else
971 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700972
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400973 /*
974 * The right amount was read from socket - 4 bytes,
975 * so we can now interpret the length field.
976 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400977 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700978
Joe Perchesf96637b2013-05-04 22:12:25 -0500979 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400980 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000981 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000982next_pdu:
983 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700984
Jeff Layton89482a52011-10-19 15:28:57 -0400985 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000986 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100987 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500988 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000989 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400990 cifs_reconnect(server);
991 wake_up(&server->response_q);
992 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700993 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400994
Jeff Layton89482a52011-10-19 15:28:57 -0400995 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100996 length = cifs_read_from_socket(server,
997 buf + server->vals->header_preamble_size,
998 HEADER_SIZE(server) - 1
999 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001000 if (length < 0)
1001 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001002 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001003
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001004 if (server->ops->next_header) {
1005 next_offset = server->ops->next_header(buf);
1006 if (next_offset)
1007 server->pdu_size = next_offset;
1008 }
1009
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001010 memset(mids, 0, sizeof(mids));
1011 memset(bufs, 0, sizeof(bufs));
1012 num_mids = 0;
1013
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001014 if (server->ops->is_transform_hdr &&
1015 server->ops->receive_transform &&
1016 server->ops->is_transform_hdr(buf)) {
1017 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001018 mids,
1019 bufs,
1020 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001021 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001022 mids[0] = server->ops->find_mid(server, buf);
1023 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001024 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001025
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001026 if (!mids[0] || !mids[0]->receive)
1027 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001028 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001029 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001030 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001031
Lars Persson696e4202018-06-25 14:05:25 +02001032 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001033 for (i = 0; i < num_mids; i++)
1034 if (mids[i])
1035 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001036 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001037 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001038
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001039 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -04001040 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -07001041
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001042
Steve Frenchfda35942011-01-20 18:06:34 +00001043 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001044
1045 for (i = 0; i < num_mids; i++) {
1046 if (mids[i] != NULL) {
1047 mids[i]->resp_buf_size = server->pdu_size;
1048 if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
1049 mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
1050 server->ops->handle_cancelled_mid)
1051 server->ops->handle_cancelled_mid(
1052 mids[i]->resp_buf,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001053 server);
1054
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001055 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1056 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001057
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001058 cifs_mid_q_entry_release(mids[i]);
1059 } else if (server->ops->is_oplock_break &&
1060 server->ops->is_oplock_break(bufs[i],
1061 server)) {
1062 cifs_dbg(FYI, "Received oplock break\n");
1063 } else {
1064 cifs_dbg(VFS, "No task to wake, unknown frame "
1065 "received! NumMids %d\n",
1066 atomic_read(&midCount));
1067 cifs_dump_mem("Received Data is: ", bufs[i],
1068 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +00001069#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001070 if (server->ops->dump_detail)
1071 server->ops->dump_detail(bufs[i],
1072 server);
1073 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001074#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001075 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001076 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001077
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001078 if (pdu_length > server->pdu_size) {
1079 if (!allocate_buffers(server))
1080 continue;
1081 pdu_length -= server->pdu_size;
1082 server->total_read = 0;
1083 server->large_buf = false;
1084 buf = server->smallbuf;
1085 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001086 }
1087 } /* end while !EXITING */
1088
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001089 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001090 cifs_buf_release(server->bigbuf);
1091 if (server->smallbuf) /* no sense logging a debug message if NULL */
1092 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001094 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001095 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001096
1097 /* if server->tsk was NULL then wait for a signal before exiting */
1098 if (!task_to_wake) {
1099 set_current_state(TASK_INTERRUPTIBLE);
1100 while (!signal_pending(current)) {
1101 schedule();
1102 set_current_state(TASK_INTERRUPTIBLE);
1103 }
1104 set_current_state(TASK_RUNNING);
1105 }
1106
Jeff Layton0468a2c2008-12-01 07:09:35 -05001107 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Jeff Laytonc359cf32007-11-16 22:22:06 +00001110/* extract the host portion of the UNC string */
1111static char *
1112extract_hostname(const char *unc)
1113{
1114 const char *src;
1115 char *dst, *delim;
1116 unsigned int len;
1117
1118 /* skip double chars at beginning of string */
1119 /* BB: check validity of these bytes? */
1120 src = unc + 2;
1121
1122 /* delimiter between hostname and sharename is always '\\' now */
1123 delim = strchr(src, '\\');
1124 if (!delim)
1125 return ERR_PTR(-EINVAL);
1126
1127 len = delim - src;
1128 dst = kmalloc((len + 1), GFP_KERNEL);
1129 if (dst == NULL)
1130 return ERR_PTR(-ENOMEM);
1131
1132 memcpy(dst, src, len);
1133 dst[len] = '\0';
1134
1135 return dst;
1136}
1137
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001138static int get_option_ul(substring_t args[], unsigned long *option)
1139{
1140 int rc;
1141 char *string;
1142
1143 string = match_strdup(args);
1144 if (string == NULL)
1145 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001146 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001147 kfree(string);
1148
1149 return rc;
1150}
1151
Eric W. Biederman3da46562013-02-06 01:37:39 -08001152static int get_option_uid(substring_t args[], kuid_t *result)
1153{
1154 unsigned long value;
1155 kuid_t uid;
1156 int rc;
1157
1158 rc = get_option_ul(args, &value);
1159 if (rc)
1160 return rc;
1161
1162 uid = make_kuid(current_user_ns(), value);
1163 if (!uid_valid(uid))
1164 return -EINVAL;
1165
1166 *result = uid;
1167 return 0;
1168}
1169
1170static int get_option_gid(substring_t args[], kgid_t *result)
1171{
1172 unsigned long value;
1173 kgid_t gid;
1174 int rc;
1175
1176 rc = get_option_ul(args, &value);
1177 if (rc)
1178 return rc;
1179
1180 gid = make_kgid(current_user_ns(), value);
1181 if (!gid_valid(gid))
1182 return -EINVAL;
1183
1184 *result = gid;
1185 return 0;
1186}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001187
1188static int cifs_parse_security_flavors(char *value,
1189 struct smb_vol *vol)
1190{
1191
1192 substring_t args[MAX_OPT_ARGS];
1193
Jeff Layton1e3cc572013-06-10 17:12:23 -05001194 /*
1195 * With mount options, the last one should win. Reset any existing
1196 * settings back to default.
1197 */
1198 vol->sectype = Unspecified;
1199 vol->sign = false;
1200
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001201 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001202 case Opt_sec_krb5p:
1203 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1204 return 1;
1205 case Opt_sec_krb5i:
1206 vol->sign = true;
1207 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001208 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001209 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001210 break;
1211 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001212 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001213 /* Fallthrough */
1214 case Opt_sec_ntlmssp:
1215 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001216 break;
1217 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001218 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001219 /* Fallthrough */
1220 case Opt_ntlm:
1221 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001222 break;
1223 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001224 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001225 /* Fallthrough */
1226 case Opt_sec_ntlmv2:
1227 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001228 break;
1229#ifdef CONFIG_CIFS_WEAK_PW_HASH
1230 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001231 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001232 break;
1233#endif
1234 case Opt_sec_none:
1235 vol->nullauth = 1;
1236 break;
1237 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001238 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001239 return 1;
1240 }
1241
1242 return 0;
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001246cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1247{
1248 substring_t args[MAX_OPT_ARGS];
1249
1250 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1251 case Opt_cache_loose:
1252 vol->direct_io = false;
1253 vol->strict_io = false;
1254 break;
1255 case Opt_cache_strict:
1256 vol->direct_io = false;
1257 vol->strict_io = true;
1258 break;
1259 case Opt_cache_none:
1260 vol->direct_io = true;
1261 vol->strict_io = false;
1262 break;
1263 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001264 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001265 return 1;
1266 }
1267 return 0;
1268}
1269
1270static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001271cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001272{
1273 substring_t args[MAX_OPT_ARGS];
1274
1275 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001276#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001277 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001278 if (disable_legacy_dialects) {
1279 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1280 return 1;
1281 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001282 if (is_smb3) {
1283 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1284 return 1;
1285 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001286 vol->ops = &smb1_operations;
1287 vol->vals = &smb1_values;
1288 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001289 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001290 if (disable_legacy_dialects) {
1291 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1292 return 1;
1293 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001294 if (is_smb3) {
1295 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1296 return 1;
1297 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001298 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001299 vol->vals = &smb20_values;
1300 break;
Steve French74204512018-06-19 14:34:08 -05001301#else
1302 case Smb_1:
1303 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1304 return 1;
1305 case Smb_20:
1306 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1307 return 1;
1308#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001309 case Smb_21:
1310 vol->ops = &smb21_operations;
1311 vol->vals = &smb21_values;
1312 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001313 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001314 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001315 vol->vals = &smb30_values;
1316 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001317 case Smb_302:
1318 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1319 vol->vals = &smb302_values;
1320 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001321 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001322 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001323 vol->vals = &smb311_values;
1324 break;
Steve French9764c022017-09-17 10:41:35 -05001325 case Smb_3any:
1326 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1327 vol->vals = &smb3any_values;
1328 break;
1329 case Smb_default:
1330 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1331 vol->vals = &smbdefault_values;
1332 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001333 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001334 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001335 return 1;
1336 }
1337 return 0;
1338}
1339
Jeff Laytond387a5c2012-12-10 06:10:46 -05001340/*
1341 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1342 * fields with the result. Returns 0 on success and an error otherwise.
1343 */
1344static int
1345cifs_parse_devname(const char *devname, struct smb_vol *vol)
1346{
1347 char *pos;
1348 const char *delims = "/\\";
1349 size_t len;
1350
Yao Liu36a32192019-01-28 19:47:28 +08001351 if (unlikely(!devname || !*devname)) {
1352 cifs_dbg(VFS, "Device name not specified.\n");
1353 return -EINVAL;
1354 }
1355
Jeff Laytond387a5c2012-12-10 06:10:46 -05001356 /* make sure we have a valid UNC double delimiter prefix */
1357 len = strspn(devname, delims);
1358 if (len != 2)
1359 return -EINVAL;
1360
1361 /* find delimiter between host and sharename */
1362 pos = strpbrk(devname + 2, delims);
1363 if (!pos)
1364 return -EINVAL;
1365
1366 /* skip past delimiter */
1367 ++pos;
1368
1369 /* now go until next delimiter or end of string */
1370 len = strcspn(pos, delims);
1371
1372 /* move "pos" up to delimiter or NULL */
1373 pos += len;
1374 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1375 if (!vol->UNC)
1376 return -ENOMEM;
1377
1378 convert_delimiter(vol->UNC, '\\');
1379
Sachin Prabhu11e31642016-02-08 13:44:01 +05301380 /* skip any delimiter */
1381 if (*pos == '/' || *pos == '\\')
1382 pos++;
1383
1384 /* If pos is NULL then no prepath */
1385 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001386 return 0;
1387
1388 vol->prepath = kstrdup(pos, GFP_KERNEL);
1389 if (!vol->prepath)
1390 return -ENOMEM;
1391
1392 return 0;
1393}
1394
Jeff Layton23db65f2012-05-15 12:20:51 -04001395static int
Sean Finneyb9468452011-04-11 13:19:32 +00001396cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001397 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001399 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001400 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 unsigned int temp_len, i, j;
1402 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001403 short int override_uid = -1;
1404 short int override_gid = -1;
1405 bool uid_specified = false;
1406 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001407 bool sloppy = false;
1408 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001409 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001410 char *string = NULL;
1411 char *tmp_end, *value;
1412 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001413 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001414 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001415 unsigned short port = 0;
1416 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001419 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001420 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Jeff Layton6ee95422012-11-26 11:09:57 -05001422 /* ensure we always start with zeroed-out smb_vol */
1423 memset(vol, 0, sizeof(*vol));
1424
Jeff Layton88463992010-11-22 15:31:03 -05001425 /*
1426 * does not have to be perfect mapping since field is
1427 * informational, only used for servers that do not support
1428 * port 445 and it can be overridden at mount time
1429 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001430 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1431 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001432 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1433
Jeff Layton1397f2e2011-01-07 11:30:28 -05001434 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001435 /* null target name indicates to use *SMBSERVR default called name
1436 if we end up sending RFC1001 session initialize */
1437 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001438 vol->cred_uid = current_uid();
1439 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001440 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001441
Steve French2baa2682014-09-27 02:19:01 -05001442 /*
1443 * default to SFM style remapping of seven reserved characters
1444 * unless user overrides it or we negotiate CIFS POSIX where
1445 * it is unnecessary. Can not simultaneously use more than one mapping
1446 * since then readdir could list files that open could not open
1447 */
1448 vol->remap = true;
1449
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001450 /* default to only allowing write access to owner of the mount */
1451 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001454 /* default is always to request posix paths. */
1455 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001456 /* default to using server inode numbers where available */
1457 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001458
Jeff Layton1b359202012-09-19 15:20:27 -07001459 /* default is to use strict cifs caching semantics */
1460 vol->strict_io = true;
1461
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301462 vol->actimeo = CIFS_DEF_ACTIMEO;
1463
Steve French9764c022017-09-17 10:41:35 -05001464 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1465 vol->ops = &smb30_operations;
1466 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001467
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001468 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1469
Sean Finneyb9468452011-04-11 13:19:32 +00001470 if (!mountdata)
1471 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Sean Finneyb9468452011-04-11 13:19:32 +00001473 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1474 if (!mountdata_copy)
1475 goto cifs_parse_mount_err;
1476
1477 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001478 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001479
Steve French50c2f752007-07-13 00:33:32 +00001480 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001481 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 separator[0] = options[4];
1483 options += 5;
1484 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001485 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001488 vol->backupuid_specified = false; /* no backup intent for a user */
1489 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001490
Jeff Layton37d4f992013-05-24 07:40:05 -04001491 switch (cifs_parse_devname(devname, vol)) {
1492 case 0:
1493 break;
1494 case -ENOMEM:
1495 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1496 goto cifs_parse_mount_err;
1497 case -EINVAL:
1498 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1499 goto cifs_parse_mount_err;
1500 default:
1501 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1502 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001503 }
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001506 substring_t args[MAX_OPT_ARGS];
1507 unsigned long option;
1508 int token;
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (!*data)
1511 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001513 token = match_token(data, cifs_mount_option_tokens, args);
1514
1515 switch (token) {
1516
1517 /* Ingnore the following */
1518 case Opt_ignore:
1519 break;
1520
1521 /* Boolean values */
1522 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001524 break;
1525 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001527 break;
1528 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001529 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001530 break;
1531 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001532 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001533 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001534 case Opt_forcegid:
1535 override_gid = 1;
1536 break;
1537 case Opt_noforcegid:
1538 override_gid = 0;
1539 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001540 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001541 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001542 break;
1543 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001544 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001545 break;
Steve French2e96c932019-09-11 21:46:20 -05001546 case Opt_nolease:
1547 vol->no_lease = 1;
1548 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001549 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001551 break;
1552 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001554 break;
1555 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001557 break;
1558 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001560 break;
1561 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001562 vol->sfu_remap = true;
1563 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001564 break;
1565 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001566 vol->sfu_remap = false;
1567 break;
1568 case Opt_mapposix:
1569 vol->remap = true;
1570 vol->sfu_remap = false; /* disable SFU mapping */
1571 break;
1572 case Opt_nomapposix:
1573 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001574 break;
1575 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001576 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001577 break;
1578 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001579 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001580 break;
1581 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001582 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001583 break;
1584 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001585 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001586 break;
1587 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001588 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001589 break;
1590 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001591 if (vol->linux_ext)
1592 cifs_dbg(VFS,
1593 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001594 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001595 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001596 case Opt_unix:
1597 if (vol->no_linux_ext)
1598 cifs_dbg(VFS,
1599 "conflicting unix mount options\n");
1600 vol->linux_ext = 1;
1601 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001602 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001603 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001604 break;
1605 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001606 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001607 break;
1608 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001609 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001610 /*
1611 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001612 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001613 * local vfs will do advisory
1614 */
Steve French50c2f752007-07-13 00:33:32 +00001615 if (vol->file_mode ==
1616 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001617 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001618 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001619 case Opt_nohandlecache:
1620 vol->nohandlecache = 1;
1621 break;
1622 case Opt_handlecache:
1623 vol->nohandlecache = 0;
1624 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001625 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001626 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001627 break;
1628 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001630 break;
1631 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001633 break;
Steve French95932652016-09-23 01:36:34 -05001634 case Opt_setuidfromacl:
1635 vol->setuidfromacl = 1;
1636 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001637 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001638 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001639 break;
1640 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001641 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001642 break;
1643 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001645 break;
1646 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001648 break;
1649 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001651 break;
1652 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001654 break;
1655 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001656 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001657 break;
1658 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001659 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001660 break;
1661 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001663 break;
1664 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001666 break;
1667 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001668 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001669 break;
1670 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001671 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001672 break;
1673 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001674 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001675 break;
1676 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001678 break;
1679 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001681 break;
1682 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001683 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001684 break;
1685 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001686 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001687 break;
1688 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001689 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001690 * is a per tree connection (mount) not a per socket
1691 * or per-smb connection option in the protocol
1692 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1693 */
Steve French95b1cb92008-05-15 16:44:38 +00001694 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001695 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001696 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001697 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001698 break;
1699 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301700#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001701 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001702 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301703#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301704 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001705 break;
1706 case Opt_mfsymlinks:
Stefan Metzmacher736a3322010-07-30 14:56:00 +02001707 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001708 break;
1709 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001710 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001711 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001712 case Opt_sloppy:
1713 sloppy = true;
1714 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001715 case Opt_nosharesock:
1716 vol->nosharesock = true;
1717 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001718 case Opt_nopersistent:
1719 vol->nopersistent = true;
1720 if (vol->persistent) {
1721 cifs_dbg(VFS,
1722 "persistenthandles mount options conflict\n");
1723 goto cifs_parse_mount_err;
1724 }
1725 break;
1726 case Opt_persistent:
1727 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001728 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001729 cifs_dbg(VFS,
1730 "persistenthandles mount options conflict\n");
1731 goto cifs_parse_mount_err;
1732 }
1733 break;
Steve French592fafe2015-11-03 10:08:53 -06001734 case Opt_resilient:
1735 vol->resilient = true;
1736 if (vol->persistent) {
1737 cifs_dbg(VFS,
1738 "persistenthandles mount options conflict\n");
1739 goto cifs_parse_mount_err;
1740 }
1741 break;
1742 case Opt_noresilient:
1743 vol->resilient = false; /* already the default */
1744 break;
Germano Percossi39566442016-12-15 12:31:18 +05301745 case Opt_domainauto:
1746 vol->domainauto = true;
1747 break;
Long Li8339dd32017-11-07 01:54:55 -07001748 case Opt_rdma:
1749 vol->rdma = true;
1750 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001751
1752 /* Numeric Values */
1753 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001754 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001755 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1756 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001757 goto cifs_parse_mount_err;
1758 }
1759 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001760 break;
1761 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001762 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001763 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1764 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001765 goto cifs_parse_mount_err;
1766 }
1767 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001768 break;
1769 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001770 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001771 cifs_dbg(VFS, "%s: Invalid uid value\n",
1772 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001773 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001775 uid_specified = true;
1776 break;
1777 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001778 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001779 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1780 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001781 goto cifs_parse_mount_err;
1782 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001783 break;
1784 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001785 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001786 cifs_dbg(VFS, "%s: Invalid gid value\n",
1787 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001788 goto cifs_parse_mount_err;
1789 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001790 gid_specified = true;
1791 break;
1792 case Opt_file_mode:
1793 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001794 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1795 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001796 goto cifs_parse_mount_err;
1797 }
1798 vol->file_mode = option;
1799 break;
1800 case Opt_dirmode:
1801 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001802 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1803 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001804 goto cifs_parse_mount_err;
1805 }
1806 vol->dir_mode = option;
1807 break;
1808 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001809 if (get_option_ul(args, &option) ||
1810 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001811 cifs_dbg(VFS, "%s: Invalid port value\n",
1812 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001813 goto cifs_parse_mount_err;
1814 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001815 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001816 break;
1817 case Opt_rsize:
1818 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001819 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1820 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001821 goto cifs_parse_mount_err;
1822 }
1823 vol->rsize = option;
1824 break;
1825 case Opt_wsize:
1826 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001827 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1828 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001829 goto cifs_parse_mount_err;
1830 }
1831 vol->wsize = option;
1832 break;
1833 case Opt_actimeo:
1834 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001835 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1836 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001837 goto cifs_parse_mount_err;
1838 }
1839 vol->actimeo = HZ * option;
1840 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001841 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001842 goto cifs_parse_mount_err;
1843 }
1844 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001845 case Opt_echo_interval:
1846 if (get_option_ul(args, &option)) {
1847 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1848 __func__);
1849 goto cifs_parse_mount_err;
1850 }
1851 vol->echo_interval = option;
1852 break;
Steve French8b217fe2016-11-11 22:36:20 -06001853 case Opt_snapshot:
1854 if (get_option_ul(args, &option)) {
1855 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1856 __func__);
1857 goto cifs_parse_mount_err;
1858 }
1859 vol->snapshot_time = option;
1860 break;
Steve French141891f2016-09-23 00:44:16 -05001861 case Opt_max_credits:
1862 if (get_option_ul(args, &option) || (option < 20) ||
1863 (option > 60000)) {
1864 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1865 __func__);
1866 goto cifs_parse_mount_err;
1867 }
1868 vol->max_credits = option;
1869 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001870
1871 /* String Arguments */
1872
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001873 case Opt_blank_user:
1874 /* null user, ie. anonymous authentication */
1875 vol->nullauth = 1;
1876 vol->username = NULL;
1877 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001878 case Opt_user:
1879 string = match_strdup(args);
1880 if (string == NULL)
1881 goto out_nomem;
1882
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001883 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1884 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001885 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001886 goto cifs_parse_mount_err;
1887 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001888
1889 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001890 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001891 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001892 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001893 break;
1894 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001895 /* passwords have to be handled differently
1896 * to allow the character used for deliminator
1897 * to be passed within them
1898 */
1899
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001900 /*
1901 * Check if this is a case where the password
1902 * starts with a delimiter
1903 */
1904 tmp_end = strchr(data, '=');
1905 tmp_end++;
1906 if (!(tmp_end < end && tmp_end[1] == delim)) {
1907 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001908 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001909 vol->password = NULL;
1910 break;
1911 }
1912 /* Yes it is. Drop down to Opt_pass below.*/
1913 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001914 /* Obtain the value string */
1915 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001916 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001917
1918 /* Set tmp_end to end of the string */
1919 tmp_end = (char *) value + strlen(value);
1920
1921 /* Check if following character is the deliminator
1922 * If yes, we have encountered a double deliminator
1923 * reset the NULL character to the deliminator
1924 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301925 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001926 tmp_end[0] = delim;
1927
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301928 /* Keep iterating until we get to a single
1929 * deliminator OR the end
1930 */
1931 while ((tmp_end = strchr(tmp_end, delim))
1932 != NULL && (tmp_end[1] == delim)) {
1933 tmp_end = (char *) &tmp_end[2];
1934 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001935
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301936 /* Reset var options to point to next element */
1937 if (tmp_end) {
1938 tmp_end[0] = '\0';
1939 options = (char *) &tmp_end[1];
1940 } else
1941 /* Reached the end of the mount option
1942 * string */
1943 options = end;
1944 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001945
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001946 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001947 /* Now build new password string */
1948 temp_len = strlen(value);
1949 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1950 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001951 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001952 goto cifs_parse_mount_err;
1953 }
1954
1955 for (i = 0, j = 0; i < temp_len; i++, j++) {
1956 vol->password[j] = value[i];
1957 if ((value[i] == delim) &&
1958 value[i+1] == delim)
1959 /* skip the second deliminator */
1960 i++;
1961 }
1962 vol->password[j] = '\0';
1963 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001964 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001965 /* FIXME: should this be an error instead? */
1966 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001967 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001968 case Opt_ip:
1969 string = match_strdup(args);
1970 if (string == NULL)
1971 goto out_nomem;
1972
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001973 if (!cifs_convert_address(dstaddr, string,
1974 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001975 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001976 goto cifs_parse_mount_err;
1977 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001978 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001979 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001980 case Opt_domain:
1981 string = match_strdup(args);
1982 if (string == NULL)
1983 goto out_nomem;
1984
Chen Gang057d6332013-07-19 09:01:36 +08001985 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1986 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001987 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001988 goto cifs_parse_mount_err;
1989 }
1990
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001991 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001992 vol->domainname = kstrdup(string, GFP_KERNEL);
1993 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001994 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001995 goto cifs_parse_mount_err;
1996 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001997 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001998 break;
1999 case Opt_srcaddr:
2000 string = match_strdup(args);
2001 if (string == NULL)
2002 goto out_nomem;
2003
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002004 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002005 (struct sockaddr *)&vol->srcaddr,
2006 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002007 pr_warn("CIFS: Could not parse srcaddr: %s\n",
2008 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002009 goto cifs_parse_mount_err;
2010 }
2011 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002012 case Opt_iocharset:
2013 string = match_strdup(args);
2014 if (string == NULL)
2015 goto out_nomem;
2016
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002017 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002018 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002019 goto cifs_parse_mount_err;
2020 }
2021
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002022 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002023 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002024 vol->iocharset = kstrdup(string,
2025 GFP_KERNEL);
2026 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002027 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002028 goto cifs_parse_mount_err;
2029 }
2030 }
2031 /* if iocharset not set then load_nls_default
2032 * is used by caller
2033 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002034 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002035 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002036 case Opt_netbiosname:
2037 string = match_strdup(args);
2038 if (string == NULL)
2039 goto out_nomem;
2040
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002041 memset(vol->source_rfc1001_name, 0x20,
2042 RFC1001_NAME_LEN);
2043 /*
2044 * FIXME: are there cases in which a comma can
2045 * be valid in workstation netbios name (and
2046 * need special handling)?
2047 */
2048 for (i = 0; i < RFC1001_NAME_LEN; i++) {
2049 /* don't ucase netbiosname for user */
2050 if (string[i] == 0)
2051 break;
2052 vol->source_rfc1001_name[i] = string[i];
2053 }
2054 /* The string has 16th byte zero still from
2055 * set at top of the function
2056 */
2057 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002058 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002059 break;
2060 case Opt_servern:
2061 /* servernetbiosname specified override *SMBSERVER */
2062 string = match_strdup(args);
2063 if (string == NULL)
2064 goto out_nomem;
2065
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002066 /* last byte, type, is 0x20 for servr type */
2067 memset(vol->target_rfc1001_name, 0x20,
2068 RFC1001_NAME_LEN_WITH_NULL);
2069
2070 /* BB are there cases in which a comma can be
2071 valid in this workstation netbios name
2072 (and need special handling)? */
2073
2074 /* user or mount helper must uppercase the
2075 netbios name */
2076 for (i = 0; i < 15; i++) {
2077 if (string[i] == 0)
2078 break;
2079 vol->target_rfc1001_name[i] = string[i];
2080 }
2081 /* The string has 16th byte zero still from
2082 set at top of the function */
2083 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002084 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002085 break;
2086 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002087 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002088 string = match_strdup(args);
2089 if (string == NULL)
2090 goto out_nomem;
2091
Steve French7e682f72017-08-31 21:34:24 -05002092 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002093 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002094 if (strlen(string) > 1) {
2095 pr_warn("Bad mount helper ver=%s. Did "
2096 "you want SMB1 (CIFS) dialect "
2097 "and mean to type vers=1.0 "
2098 "instead?\n", string);
2099 goto cifs_parse_mount_err;
2100 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002101 /* This is the default */
2102 break;
2103 }
2104 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002105 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002106 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002107 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002108 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002109 string = match_strdup(args);
2110 if (string == NULL)
2111 goto out_nomem;
2112
Steve Frenchc7c137b2018-06-06 17:59:29 -05002113 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002114 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002115 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002116 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002117 case Opt_sec:
2118 string = match_strdup(args);
2119 if (string == NULL)
2120 goto out_nomem;
2121
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002122 if (cifs_parse_security_flavors(string, vol) != 0)
2123 goto cifs_parse_mount_err;
2124 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002125 case Opt_cache:
2126 string = match_strdup(args);
2127 if (string == NULL)
2128 goto out_nomem;
2129
2130 if (cifs_parse_cache_flavor(string, vol) != 0)
2131 goto cifs_parse_mount_err;
2132 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002133 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002134 /*
2135 * An option we don't recognize. Save it off for later
2136 * if we haven't already found one
2137 */
2138 if (!invalid)
2139 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002140 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002142 /* Free up any allocated string */
2143 kfree(string);
2144 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002146
Jeff Laytond8162552012-03-23 14:40:56 -04002147 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002148 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002149 goto cifs_parse_mount_err;
2150 }
2151
Long Li8339dd32017-11-07 01:54:55 -07002152 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2153 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2154 goto cifs_parse_mount_err;
2155 }
2156
Jeff Layton8a8798a2012-01-17 16:09:15 -05002157#ifndef CONFIG_KEYS
2158 /* Muliuser mounts require CONFIG_KEYS support */
2159 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002160 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002161 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002162 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002163#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002164 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002165 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002166 goto cifs_parse_mount_err;
2167 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002168
Jeff Layton62a1a432012-12-10 06:10:45 -05002169 /* make sure UNC has a share name */
2170 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002171 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002172 goto cifs_parse_mount_err;
2173 }
2174
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002175 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002176 int len;
2177 const char *slash;
2178
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002179 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002180 /* Use the address part of the UNC. */
2181 slash = strchr(&vol->UNC[2], '\\');
2182 len = slash - &vol->UNC[2];
2183 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002184 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002185 goto cifs_parse_mount_err;
2186 }
2187 }
2188
2189 /* set the port that we got earlier */
2190 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002192 if (uid_specified)
2193 vol->override_uid = override_uid;
2194 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002195 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002196
2197 if (gid_specified)
2198 vol->override_gid = override_gid;
2199 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002200 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002201
Steve French7e682f72017-08-31 21:34:24 -05002202 if (got_version == false)
2203 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002204 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002205 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002206 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2207 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002208
Sean Finneyb9468452011-04-11 13:19:32 +00002209 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002211
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002212out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002213 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002214cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002215 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002216 kfree(mountdata_copy);
2217 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Ben Greear3eb9a882010-09-01 17:06:02 -07002220/** Returns true if srcaddr isn't specified and rhs isn't
2221 * specified, or if srcaddr is specified and
2222 * matches the IP address of the rhs argument.
2223 */
Jeff Layton45151482010-07-06 20:43:02 -04002224static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002225srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2226{
2227 switch (srcaddr->sa_family) {
2228 case AF_UNSPEC:
2229 return (rhs->sa_family == AF_UNSPEC);
2230 case AF_INET: {
2231 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2232 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2233 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2234 }
2235 case AF_INET6: {
2236 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002237 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002238 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2239 }
2240 default:
2241 WARN_ON(1);
2242 return false; /* don't expect to be here */
2243 }
2244}
2245
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002246/*
2247 * If no port is specified in addr structure, we try to match with 445 port
2248 * and if it fails - with 139 ports. It should be called only if address
2249 * families of server and addr are equal.
2250 */
2251static bool
2252match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2253{
Steve French6da97912011-03-13 18:55:55 +00002254 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002255
2256 switch (addr->sa_family) {
2257 case AF_INET:
2258 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2259 port = ((struct sockaddr_in *) addr)->sin_port;
2260 break;
2261 case AF_INET6:
2262 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2263 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2264 break;
2265 default:
2266 WARN_ON(1);
2267 return false;
2268 }
2269
2270 if (!port) {
2271 port = htons(CIFS_PORT);
2272 if (port == *sport)
2273 return true;
2274
2275 port = htons(RFC1001_PORT);
2276 }
2277
2278 return port == *sport;
2279}
Ben Greear3eb9a882010-09-01 17:06:02 -07002280
2281static bool
2282match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2283 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Jeff Layton45151482010-07-06 20:43:02 -04002285 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002286 case AF_INET: {
2287 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2288 struct sockaddr_in *srv_addr4 =
2289 (struct sockaddr_in *)&server->dstaddr;
2290
2291 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002292 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002293 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002294 }
2295 case AF_INET6: {
2296 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2297 struct sockaddr_in6 *srv_addr6 =
2298 (struct sockaddr_in6 *)&server->dstaddr;
2299
Jeff Layton45151482010-07-06 20:43:02 -04002300 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002301 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002302 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002303 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002304 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002305 break;
2306 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002307 default:
2308 WARN_ON(1);
2309 return false; /* don't expect to be here */
2310 }
Jeff Layton45151482010-07-06 20:43:02 -04002311
Ben Greear3eb9a882010-09-01 17:06:02 -07002312 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2313 return false;
2314
Jeff Layton45151482010-07-06 20:43:02 -04002315 return true;
2316}
2317
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002318static bool
2319match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2320{
Jeff Layton3f618222013-06-12 19:52:14 -05002321 /*
2322 * The select_sectype function should either return the vol->sectype
2323 * that was specified, or "Unspecified" if that sectype was not
2324 * compatible with the given NEGOTIATE request.
2325 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302326 if (server->ops->select_sectype(server, vol->sectype)
2327 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002328 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002329
Jeff Layton3f618222013-06-12 19:52:14 -05002330 /*
2331 * Now check if signing mode is acceptable. No need to check
2332 * global_secflags at this point since if MUST_SIGN is set then
2333 * the server->sign had better be too.
2334 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002335 if (vol->sign && !server->sign)
2336 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002337
2338 return true;
2339}
2340
Jeff Layton9fa114f2012-11-26 11:09:57 -05002341static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002342{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002343 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2344
Jeff Laytona0b3df52013-05-24 07:40:59 -04002345 if (vol->nosharesock)
2346 return 0;
2347
Steve French9764c022017-09-17 10:41:35 -05002348 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002349 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2350 return 0;
2351
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002352 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2353 return 0;
2354
2355 if (!match_address(server, addr,
2356 (struct sockaddr *)&vol->srcaddr))
2357 return 0;
2358
2359 if (!match_port(server, addr))
2360 return 0;
2361
2362 if (!match_security(server, vol))
2363 return 0;
2364
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002365 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002366 return 0;
2367
Long Li8339dd32017-11-07 01:54:55 -07002368 if (server->rdma != vol->rdma)
2369 return 0;
2370
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002371 return 1;
2372}
2373
Jeff Layton45151482010-07-06 20:43:02 -04002374static struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002375cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002376{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002377 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302379 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002380 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002381 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002382 continue;
2383
Jeff Laytone7ddee92008-11-14 13:44:38 -05002384 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302385 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002386 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002387 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302389 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return NULL;
2391}
2392
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002393void
2394cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002396 struct task_struct *task;
2397
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302398 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002399 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302400 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002401 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002403
Rob Landleyf1d0c992011-01-22 15:44:05 -06002404 put_net(cifs_net_ns(server));
2405
Jeff Laytone7ddee92008-11-14 13:44:38 -05002406 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302407 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002408
Jeff Laytonc74093b2011-01-11 07:24:23 -05002409 cancel_delayed_work_sync(&server->echo);
2410
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002411 if (from_reconnect)
2412 /*
2413 * Avoid deadlock here: reconnect work calls
2414 * cifs_put_tcp_session() at its end. Need to be sure
2415 * that reconnect work does nothing with server pointer after
2416 * that step.
2417 */
2418 cancel_delayed_work(&server->reconnect);
2419 else
2420 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002421
Jeff Laytone7ddee92008-11-14 13:44:38 -05002422 spin_lock(&GlobalMid_Lock);
2423 server->tcpStatus = CifsExiting;
2424 spin_unlock(&GlobalMid_Lock);
2425
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002426 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302427 cifs_fscache_release_client_cookie(server);
2428
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002429 kfree(server->session_key.response);
2430 server->session_key.response = NULL;
2431 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002432
2433 task = xchg(&server->tsk, NULL);
2434 if (task)
2435 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
Jeff Layton63c038c2008-12-01 18:41:46 -05002438static struct TCP_Server_Info *
2439cifs_get_tcp_session(struct smb_vol *volume_info)
2440{
2441 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002442 int rc;
2443
Joe Perchesf96637b2013-05-04 22:12:25 -05002444 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002445
2446 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002447 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002448 if (tcp_ses)
2449 return tcp_ses;
2450
2451 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2452 if (!tcp_ses) {
2453 rc = -ENOMEM;
2454 goto out_err;
2455 }
2456
Jeff Layton23db65f2012-05-15 12:20:51 -04002457 tcp_ses->ops = volume_info->ops;
2458 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002459 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002460 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2461 if (IS_ERR(tcp_ses->hostname)) {
2462 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002463 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002464 }
2465
2466 tcp_ses->noblocksnd = volume_info->noblocksnd;
2467 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002468 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002469 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002470 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002471 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002472 init_waitqueue_head(&tcp_ses->response_q);
2473 init_waitqueue_head(&tcp_ses->request_q);
2474 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2475 mutex_init(&tcp_ses->srv_mutex);
2476 memcpy(tcp_ses->workstation_RFC1001_name,
2477 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2478 memcpy(tcp_ses->server_RFC1001_name,
2479 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002480 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002481 tcp_ses->sequence_number = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002482 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002483 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002484 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2485 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002486 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002487 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2488 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002489 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2490 sizeof(tcp_ses->srcaddr));
2491 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2492 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002493 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002494 /*
2495 * at this point we are the only ones with the pointer
2496 * to the struct since the kernel thread not created yet
2497 * no need to spinlock this init of tcpStatus or srv_count
2498 */
2499 tcp_ses->tcpStatus = CifsNew;
2500 ++tcp_ses->srv_count;
2501
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002502 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2503 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2504 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2505 else
2506 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002507 if (tcp_ses->rdma) {
2508#ifndef CONFIG_CIFS_SMB_DIRECT
2509 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2510 rc = -ENOENT;
2511 goto out_err_crypto_release;
2512#endif
2513 tcp_ses->smbd_conn = smbd_get_connection(
2514 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2515 if (tcp_ses->smbd_conn) {
2516 cifs_dbg(VFS, "RDMA transport established\n");
2517 rc = 0;
2518 goto smbd_connected;
2519 } else {
2520 rc = -ENOENT;
2521 goto out_err_crypto_release;
2522 }
2523 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002524 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002525 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002526 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002527 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002528 }
Long Li2f894642017-11-22 17:38:34 -07002529smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002530 /*
2531 * since we're in a cifs function already, we know that
2532 * this will succeed. No need for try_module_get().
2533 */
2534 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002535 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002536 tcp_ses, "cifsd");
2537 if (IS_ERR(tcp_ses->tsk)) {
2538 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002539 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002540 module_put(THIS_MODULE);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002541 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002542 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002543 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002544
2545 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302546 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002547 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302548 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002549
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302550 cifs_fscache_get_client_cookie(tcp_ses);
2551
Jeff Laytonc74093b2011-01-11 07:24:23 -05002552 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002553 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002554
Jeff Layton63c038c2008-12-01 18:41:46 -05002555 return tcp_ses;
2556
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002557out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002558 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002559
Rob Landleyf1d0c992011-01-22 15:44:05 -06002560 put_net(cifs_net_ns(tcp_ses));
2561
Jeff Layton63c038c2008-12-01 18:41:46 -05002562out_err:
2563 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002564 if (!IS_ERR(tcp_ses->hostname))
2565 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002566 if (tcp_ses->ssocket)
2567 sock_release(tcp_ses->ssocket);
2568 kfree(tcp_ses);
2569 }
2570 return ERR_PTR(rc);
2571}
2572
Steve French96daf2b2011-05-27 04:34:02 +00002573static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002574{
Jeff Layton3f618222013-06-12 19:52:14 -05002575 if (vol->sectype != Unspecified &&
2576 vol->sectype != ses->sectype)
2577 return 0;
2578
2579 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002580 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002581 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002582 return 0;
2583 break;
2584 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002585 /* NULL username means anonymous session */
2586 if (ses->user_name == NULL) {
2587 if (!vol->nullauth)
2588 return 0;
2589 break;
2590 }
2591
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002592 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002593 if (strncmp(ses->user_name,
2594 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002595 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002596 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002597 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002598 ses->password != NULL &&
2599 strncmp(ses->password,
2600 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002601 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002602 return 0;
2603 }
2604 return 1;
2605}
2606
Aurelien Aptelb327a712018-01-24 13:46:10 +01002607/**
2608 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2609 *
2610 * A new IPC connection is made and stored in the session
2611 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2612 */
2613static int
2614cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2615{
2616 int rc = 0, xid;
2617 struct cifs_tcon *tcon;
2618 struct nls_table *nls_codepage;
2619 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2620 bool seal = false;
2621
2622 /*
2623 * If the mount request that resulted in the creation of the
2624 * session requires encryption, force IPC to be encrypted too.
2625 */
2626 if (volume_info->seal) {
2627 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2628 seal = true;
2629 else {
2630 cifs_dbg(VFS,
2631 "IPC: server doesn't support encryption\n");
2632 return -EOPNOTSUPP;
2633 }
2634 }
2635
2636 tcon = tconInfoAlloc();
2637 if (tcon == NULL)
2638 return -ENOMEM;
2639
Thomas Werschlein395a2072018-08-30 18:29:20 +02002640 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002641
2642 /* cannot fail */
2643 nls_codepage = load_nls_default();
2644
2645 xid = get_xid();
2646 tcon->ses = ses;
2647 tcon->ipc = true;
2648 tcon->seal = seal;
2649 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2650 free_xid(xid);
2651
2652 if (rc) {
2653 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2654 tconInfoFree(tcon);
2655 goto out;
2656 }
2657
2658 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2659
2660 ses->tcon_ipc = tcon;
2661out:
2662 unload_nls(nls_codepage);
2663 return rc;
2664}
2665
2666/**
2667 * cifs_free_ipc - helper to release the session IPC tcon
2668 *
2669 * Needs to be called everytime a session is destroyed
2670 */
2671static int
2672cifs_free_ipc(struct cifs_ses *ses)
2673{
2674 int rc = 0, xid;
2675 struct cifs_tcon *tcon = ses->tcon_ipc;
2676
2677 if (tcon == NULL)
2678 return 0;
2679
2680 if (ses->server->ops->tree_disconnect) {
2681 xid = get_xid();
2682 rc = ses->server->ops->tree_disconnect(xid, tcon);
2683 free_xid(xid);
2684 }
2685
2686 if (rc)
2687 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2688
2689 tconInfoFree(tcon);
2690 ses->tcon_ipc = NULL;
2691 return rc;
2692}
2693
Steve French96daf2b2011-05-27 04:34:02 +00002694static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002695cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Steve French96daf2b2011-05-27 04:34:02 +00002697 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302699 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002700 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002701 if (ses->status == CifsExiting)
2702 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002703 if (!match_session(ses, vol))
2704 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002705 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302706 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002707 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302709 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return NULL;
2711}
2712
Jeff Layton14fbf502008-11-14 13:53:46 -05002713static void
Steve French96daf2b2011-05-27 04:34:02 +00002714cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002715{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002716 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002717 struct TCP_Server_Info *server = ses->server;
2718
Joe Perchesf96637b2013-05-04 22:12:25 -05002719 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002720
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302721 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002722 if (ses->status == CifsExiting) {
2723 spin_unlock(&cifs_tcp_ses_lock);
2724 return;
2725 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002726 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302727 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002728 return;
2729 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002730 if (ses->status == CifsGood)
2731 ses->status = CifsExiting;
2732 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002733
Aurelien Aptelb327a712018-01-24 13:46:10 +01002734 cifs_free_ipc(ses);
2735
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002736 if (ses->status == CifsExiting && server->ops->logoff) {
2737 xid = get_xid();
2738 rc = server->ops->logoff(xid, ses);
2739 if (rc)
2740 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2741 __func__, rc);
2742 _free_xid(xid);
2743 }
2744
2745 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002746 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302747 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002748
Jeff Layton14fbf502008-11-14 13:53:46 -05002749 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002750 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002751}
2752
Jeff Layton8a8798a2012-01-17 16:09:15 -05002753#ifdef CONFIG_KEYS
2754
Chen Gang057d6332013-07-19 09:01:36 +08002755/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2756#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002757
2758/* Populate username and pw fields from keyring if possible */
2759static int
2760cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2761{
2762 int rc = 0;
Ronnie Sahlberg325fa2a2019-08-22 08:09:50 +10002763 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002764 const char *delim, *payload;
2765 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002766 ssize_t len;
2767 struct key *key;
2768 struct TCP_Server_Info *server = ses->server;
2769 struct sockaddr_in *sa;
2770 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002771 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002772
2773 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2774 if (!desc)
2775 return -ENOMEM;
2776
2777 /* try to find an address key first */
2778 switch (server->dstaddr.ss_family) {
2779 case AF_INET:
2780 sa = (struct sockaddr_in *)&server->dstaddr;
2781 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2782 break;
2783 case AF_INET6:
2784 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2785 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2786 break;
2787 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002788 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2789 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002790 rc = -EINVAL;
2791 goto out_err;
2792 }
2793
Joe Perchesf96637b2013-05-04 22:12:25 -05002794 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002795 key = request_key(&key_type_logon, desc, "");
2796 if (IS_ERR(key)) {
2797 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002798 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002799 rc = PTR_ERR(key);
2800 goto out_err;
2801 }
2802
2803 /* didn't work, try to find a domain key */
2804 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002805 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002806 key = request_key(&key_type_logon, desc, "");
2807 if (IS_ERR(key)) {
2808 rc = PTR_ERR(key);
2809 goto out_err;
2810 }
Ronnie Sahlberg325fa2a2019-08-22 08:09:50 +10002811 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002812 }
2813
2814 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002815 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002816 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002817 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002818 goto out_key_put;
2819 }
2820
2821 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002822 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002823 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002824 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002825 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002826 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2827 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002828 rc = -EINVAL;
2829 goto out_key_put;
2830 }
2831
2832 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002833 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002834 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2835 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002836 rc = -EINVAL;
2837 goto out_key_put;
2838 }
2839
2840 vol->username = kstrndup(payload, len, GFP_KERNEL);
2841 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002842 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2843 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002844 rc = -ENOMEM;
2845 goto out_key_put;
2846 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002847 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002848
2849 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002850 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002851 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002852 rc = -EINVAL;
2853 kfree(vol->username);
2854 vol->username = NULL;
2855 goto out_key_put;
2856 }
2857
2858 ++delim;
2859 vol->password = kstrndup(delim, len, GFP_KERNEL);
2860 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002861 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2862 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002863 rc = -ENOMEM;
2864 kfree(vol->username);
2865 vol->username = NULL;
2866 goto out_key_put;
2867 }
2868
Ronnie Sahlberg325fa2a2019-08-22 08:09:50 +10002869 /*
2870 * If we have a domain key then we must set the domainName in the
2871 * for the request.
2872 */
2873 if (is_domain && ses->domainName) {
2874 vol->domainname = kstrndup(ses->domainName,
2875 strlen(ses->domainName),
2876 GFP_KERNEL);
2877 if (!vol->domainname) {
2878 cifs_dbg(FYI, "Unable to allocate %zd bytes for "
2879 "domain\n", len);
2880 rc = -ENOMEM;
2881 kfree(vol->username);
2882 vol->username = NULL;
Dan Carpenterb6294682019-08-27 13:59:17 +03002883 kzfree(vol->password);
Ronnie Sahlberg325fa2a2019-08-22 08:09:50 +10002884 vol->password = NULL;
2885 goto out_key_put;
2886 }
2887 }
2888
Jeff Layton8a8798a2012-01-17 16:09:15 -05002889out_key_put:
2890 up_read(&key->sem);
2891 key_put(key);
2892out_err:
2893 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002894 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002895 return rc;
2896}
2897#else /* ! CONFIG_KEYS */
2898static inline int
2899cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2900 struct cifs_ses *ses __attribute__((unused)))
2901{
2902 return -ENOSYS;
2903}
2904#endif /* CONFIG_KEYS */
2905
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002906/**
2907 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2908 *
2909 * This function assumes it is being called from cifs_mount() where we
2910 * already got a server reference (server refcount +1). See
2911 * cifs_get_tcon() for refcount explanations.
2912 */
Steve French96daf2b2011-05-27 04:34:02 +00002913static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002914cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2915{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002916 int rc = -ENOMEM;
2917 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002918 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002919 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2920 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002921
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002922 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002923
Jeff Layton4ff67b72010-07-06 20:43:02 -04002924 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002925 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002926 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2927 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002928
Jeff Layton36988c72010-04-24 07:57:43 -04002929 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002930 rc = cifs_negotiate_protocol(xid, ses);
2931 if (rc) {
2932 mutex_unlock(&ses->session_mutex);
2933 /* problem -- put our ses reference */
2934 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002935 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002936 return ERR_PTR(rc);
2937 }
Jeff Layton36988c72010-04-24 07:57:43 -04002938 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002939 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002940 rc = cifs_setup_session(xid, ses,
2941 volume_info->local_nls);
2942 if (rc) {
2943 mutex_unlock(&ses->session_mutex);
2944 /* problem -- put our reference */
2945 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002946 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002947 return ERR_PTR(rc);
2948 }
2949 }
2950 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002951
2952 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002953 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002954 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002955 return ses;
2956 }
2957
Joe Perchesf96637b2013-05-04 22:12:25 -05002958 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002959 ses = sesInfoAlloc();
2960 if (ses == NULL)
2961 goto get_ses_fail;
2962
2963 /* new SMB session uses our server ref */
2964 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002965 if (server->dstaddr.ss_family == AF_INET6)
2966 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002967 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002968 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002969
Steve French8727c8a2011-02-25 01:11:56 -06002970 if (volume_info->username) {
2971 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2972 if (!ses->user_name)
2973 goto get_ses_fail;
2974 }
Jeff Layton36988c72010-04-24 07:57:43 -04002975
2976 /* volume_info->password freed at unmount */
2977 if (volume_info->password) {
2978 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2979 if (!ses->password)
2980 goto get_ses_fail;
2981 }
2982 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002983 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2984 if (!ses->domainName)
2985 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002986 }
Germano Percossi39566442016-12-15 12:31:18 +05302987 if (volume_info->domainauto)
2988 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002989 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002990 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002991
Jeff Layton28e11bd2013-05-26 07:01:00 -04002992 ses->sectype = volume_info->sectype;
2993 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002994
2995 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002996 rc = cifs_negotiate_protocol(xid, ses);
2997 if (!rc)
2998 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002999 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00003000 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04003001 goto get_ses_fail;
3002
3003 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303004 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003005 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303006 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003007
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003008 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01003009
3010 cifs_setup_ipc(ses, volume_info);
3011
Jeff Layton36988c72010-04-24 07:57:43 -04003012 return ses;
3013
3014get_ses_fail:
3015 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003016 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003017 return ERR_PTR(rc);
3018}
3019
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003020static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003021{
3022 if (tcon->tidStatus == CifsExiting)
3023 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003024 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003025 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003026 if (tcon->seal != volume_info->seal)
3027 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003028 if (tcon->snapshot_time != volume_info->snapshot_time)
3029 return 0;
Steve French2e96c932019-09-11 21:46:20 -05003030 if (tcon->no_lease != volume_info->no_lease)
3031 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003032 return 1;
3033}
3034
Steve French96daf2b2011-05-27 04:34:02 +00003035static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06003036cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00003039 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303041 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003042 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00003043 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003044 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05003045 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003046 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303047 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return tcon;
3049 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303050 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return NULL;
3052}
3053
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003054void
Steve French96daf2b2011-05-27 04:34:02 +00003055cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05003056{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003057 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003058 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003059
Aurelien Aptelb327a712018-01-24 13:46:10 +01003060 /*
3061 * IPC tcon share the lifetime of their session and are
3062 * destroyed in the session put function
3063 */
3064 if (tcon == NULL || tcon->ipc)
3065 return;
3066
3067 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05003068 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303069 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003070 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303071 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003072 return;
3073 }
3074
3075 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303076 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003077
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003078 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003079 if (ses->server->ops->tree_disconnect)
3080 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003081 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003082
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303083 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00003084 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003085 cifs_put_smb_ses(ses);
3086}
3087
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003088/**
3089 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
3090 *
3091 * - tcon refcount is the number of mount points using the tcon.
3092 * - ses refcount is the number of tcon using the session.
3093 *
3094 * 1. This function assumes it is being called from cifs_mount() where
3095 * we already got a session reference (ses refcount +1).
3096 *
3097 * 2. Since we're in the context of adding a mount point, the end
3098 * result should be either:
3099 *
3100 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3101 * its session refcount incremented (1 new tcon). This +1 was
3102 * already done in (1).
3103 *
3104 * b) an existing tcon with refcount+1 (add a mount point to it) and
3105 * identical ses refcount (no new tcon). Because of (1) we need to
3106 * decrement the ses refcount.
3107 */
Steve French96daf2b2011-05-27 04:34:02 +00003108static struct cifs_tcon *
3109cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003110{
3111 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003112 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003113
Steve French8b217fe2016-11-11 22:36:20 -06003114 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003115 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003116 /*
3117 * tcon has refcount already incremented but we need to
3118 * decrement extra ses reference gotten by caller (case b)
3119 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003120 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003121 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003122 return tcon;
3123 }
3124
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003125 if (!ses->server->ops->tree_connect) {
3126 rc = -ENOSYS;
3127 goto out_fail;
3128 }
3129
Jeff Laytond00c28d2010-04-24 07:57:44 -04003130 tcon = tconInfoAlloc();
3131 if (tcon == NULL) {
3132 rc = -ENOMEM;
3133 goto out_fail;
3134 }
3135
Steve French8b217fe2016-11-11 22:36:20 -06003136 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003137 if (ses->server->vals->protocol_id == 0) {
3138 cifs_dbg(VFS,
3139 "Use SMB2 or later for snapshot mount option\n");
3140 rc = -EOPNOTSUPP;
3141 goto out_fail;
3142 } else
3143 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003144 }
3145
Jeff Laytond00c28d2010-04-24 07:57:44 -04003146 tcon->ses = ses;
3147 if (volume_info->password) {
3148 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3149 if (!tcon->password) {
3150 rc = -ENOMEM;
3151 goto out_fail;
3152 }
3153 }
3154
Steve French23657ad2018-04-22 15:14:58 -05003155 if (volume_info->seal) {
3156 if (ses->server->vals->protocol_id == 0) {
3157 cifs_dbg(VFS,
3158 "SMB3 or later required for encryption\n");
3159 rc = -EOPNOTSUPP;
3160 goto out_fail;
3161 } else if (tcon->ses->server->capabilities &
3162 SMB2_GLOBAL_CAP_ENCRYPTION)
3163 tcon->seal = true;
3164 else {
3165 cifs_dbg(VFS, "Encryption is not supported on share\n");
3166 rc = -EOPNOTSUPP;
3167 goto out_fail;
3168 }
3169 }
3170
Steve French8505c8b2018-06-18 14:01:59 -05003171 if (volume_info->linux_ext) {
3172 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003173 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003174 printk_once(KERN_WARNING
3175 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003176 } else {
3177 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3178 rc = -EOPNOTSUPP;
3179 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003180 }
Steve Frenchb3266142018-05-20 23:41:10 -05003181 }
Steve Frenchb3266142018-05-20 23:41:10 -05003182
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003183 /*
3184 * BB Do we need to wrap session_mutex around this TCon call and Unix
3185 * SetFS as we do on SessSetup and reconnect?
3186 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003187 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003188 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3189 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003190 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003191 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003192 if (rc)
3193 goto out_fail;
3194
3195 if (volume_info->nodfs) {
3196 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
Joe Perchesf96637b2013-05-04 22:12:25 -05003197 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003198 }
Steve Frenchb618f002015-11-03 09:15:03 -06003199 tcon->use_persistent = false;
3200 /* check if SMB2 or later, CIFS does not support persistent handles */
3201 if (volume_info->persistent) {
3202 if (ses->server->vals->protocol_id == 0) {
3203 cifs_dbg(VFS,
3204 "SMB3 or later required for persistent handles\n");
3205 rc = -EOPNOTSUPP;
3206 goto out_fail;
3207 } else if (ses->server->capabilities &
3208 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3209 tcon->use_persistent = true;
3210 else /* persistent handles requested but not supported */ {
3211 cifs_dbg(VFS,
3212 "Persistent handles not supported on share\n");
3213 rc = -EOPNOTSUPP;
3214 goto out_fail;
3215 }
3216 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3217 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3218 && (volume_info->nopersistent == false)) {
3219 cifs_dbg(FYI, "enabling persistent handles\n");
3220 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003221 } else if (volume_info->resilient) {
3222 if (ses->server->vals->protocol_id == 0) {
3223 cifs_dbg(VFS,
3224 "SMB2.1 or later required for resilient handles\n");
3225 rc = -EOPNOTSUPP;
3226 goto out_fail;
3227 }
3228 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003229 }
3230
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003231 /*
3232 * We can have only one retry value for a connection to a share so for
3233 * resources mounted more than once to the same server share the last
3234 * value passed in for the retry flag is used.
3235 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003236 tcon->retry = volume_info->retry;
3237 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003238 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003239 tcon->local_lease = volume_info->local_lease;
Steve French2e96c932019-09-11 21:46:20 -05003240 tcon->no_lease = volume_info->no_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003241 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003242
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303243 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003244 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303245 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003246
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303247 cifs_fscache_get_super_cookie(tcon);
3248
Jeff Laytond00c28d2010-04-24 07:57:44 -04003249 return tcon;
3250
3251out_fail:
3252 tconInfoFree(tcon);
3253 return ERR_PTR(rc);
3254}
3255
Jeff Layton9d002df2010-10-06 19:51:11 -04003256void
3257cifs_put_tlink(struct tcon_link *tlink)
3258{
3259 if (!tlink || IS_ERR(tlink))
3260 return;
3261
3262 if (!atomic_dec_and_test(&tlink->tl_count) ||
3263 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3264 tlink->tl_time = jiffies;
3265 return;
3266 }
3267
3268 if (!IS_ERR(tlink_tcon(tlink)))
3269 cifs_put_tcon(tlink_tcon(tlink));
3270 kfree(tlink);
3271 return;
3272}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003273
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003274static int
3275compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3276{
3277 struct cifs_sb_info *old = CIFS_SB(sb);
3278 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)987564c2019-06-18 16:16:02 -03003279 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
3280 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003281
3282 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3283 return 0;
3284
Paulo Alcantara (SUSE)987564c2019-06-18 16:16:02 -03003285 if (old->mnt_cifs_serverino_autodisabled)
3286 newflags &= ~CIFS_MOUNT_SERVER_INUM;
3287
3288 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003289 return 0;
3290
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003291 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003292 * We want to share sb only if we don't specify an r/wsize or
3293 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003294 */
3295 if (new->wsize && new->wsize < old->wsize)
3296 return 0;
3297
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003298 if (new->rsize && new->rsize < old->rsize)
3299 return 0;
3300
Eric W. Biederman1f682332013-02-06 01:20:20 -08003301 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003302 return 0;
3303
3304 if (old->mnt_file_mode != new->mnt_file_mode ||
3305 old->mnt_dir_mode != new->mnt_dir_mode)
3306 return 0;
3307
3308 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3309 return 0;
3310
3311 if (old->actimeo != new->actimeo)
3312 return 0;
3313
3314 return 1;
3315}
3316
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003317static int
3318match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3319{
3320 struct cifs_sb_info *old = CIFS_SB(sb);
3321 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003322 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3323 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003324
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003325 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003326 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003327 else if (!old_set && !new_set)
3328 return 1;
3329
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003330 return 0;
3331}
3332
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003333int
3334cifs_match_super(struct super_block *sb, void *data)
3335{
3336 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3337 struct smb_vol *volume_info;
3338 struct cifs_sb_info *cifs_sb;
3339 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003340 struct cifs_ses *ses;
3341 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003342 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003343 int rc = 0;
3344
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003345 spin_lock(&cifs_tcp_ses_lock);
3346 cifs_sb = CIFS_SB(sb);
3347 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3348 if (IS_ERR(tlink)) {
3349 spin_unlock(&cifs_tcp_ses_lock);
3350 return rc;
3351 }
3352 tcon = tlink_tcon(tlink);
3353 ses = tcon->ses;
3354 tcp_srv = ses->server;
3355
3356 volume_info = mnt_data->vol;
3357
Jeff Layton9fa114f2012-11-26 11:09:57 -05003358 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003359 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003360 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003361 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003362 rc = 0;
3363 goto out;
3364 }
3365
3366 rc = compare_mount_options(sb, mnt_data);
3367out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003368 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003369 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003370 return rc;
3371}
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003374get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003375 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3376 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 int rc = 0;
3379
Aurelien Aptelb327a712018-01-24 13:46:10 +01003380 if (!ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003381 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003383 *num_referrals = 0;
3384 *referrals = NULL;
3385
Aurelien Aptelb327a712018-01-24 13:46:10 +01003386 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3387 referrals, num_referrals,
3388 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return rc;
3390}
3391
Jeff Layton09e50d52008-07-23 10:11:19 -04003392#ifdef CONFIG_DEBUG_LOCK_ALLOC
3393static struct lock_class_key cifs_key[2];
3394static struct lock_class_key cifs_slock_key[2];
3395
3396static inline void
3397cifs_reclassify_socket4(struct socket *sock)
3398{
3399 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003400 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003401 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3402 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3403}
3404
3405static inline void
3406cifs_reclassify_socket6(struct socket *sock)
3407{
3408 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003409 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003410 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3411 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3412}
3413#else
3414static inline void
3415cifs_reclassify_socket4(struct socket *sock)
3416{
3417}
3418
3419static inline void
3420cifs_reclassify_socket6(struct socket *sock)
3421{
3422}
3423#endif
3424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003426static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
Steve French50c2f752007-07-13 00:33:32 +00003428 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
Steve French50c2f752007-07-13 00:33:32 +00003430 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 /* mask a nibble at a time and encode */
3432 target[j] = 'A' + (0x0F & (source[i] >> 4));
3433 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003434 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 }
3436
3437}
3438
Ben Greear3eb9a882010-09-01 17:06:02 -07003439static int
3440bind_socket(struct TCP_Server_Info *server)
3441{
3442 int rc = 0;
3443 if (server->srcaddr.ss_family != AF_UNSPEC) {
3444 /* Bind to the specified local IP address */
3445 struct socket *socket = server->ssocket;
3446 rc = socket->ops->bind(socket,
3447 (struct sockaddr *) &server->srcaddr,
3448 sizeof(server->srcaddr));
3449 if (rc < 0) {
3450 struct sockaddr_in *saddr4;
3451 struct sockaddr_in6 *saddr6;
3452 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3453 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3454 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003455 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3456 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003457 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003458 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3459 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003460 }
3461 }
3462 return rc;
3463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
3465static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003466ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
3468 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003469 /*
3470 * some servers require RFC1001 sessinit before sending
3471 * negprot - BB check reconnection in case where second
3472 * sessinit is sent but no second negprot
3473 */
3474 struct rfc1002_session_packet *ses_init_buf;
3475 struct smb_hdr *smb_buf;
3476 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3477 GFP_KERNEL);
3478 if (ses_init_buf) {
3479 ses_init_buf->trailer.session_req.called_len = 32;
3480
Colin Ian King997152f2016-01-25 16:25:54 +00003481 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003482 rfc1002mangle(ses_init_buf->trailer.
3483 session_req.called_name,
3484 server->server_RFC1001_name,
3485 RFC1001_NAME_LEN_WITH_NULL);
3486 else
3487 rfc1002mangle(ses_init_buf->trailer.
3488 session_req.called_name,
3489 DEFAULT_CIFS_CALLED_NAME,
3490 RFC1001_NAME_LEN_WITH_NULL);
3491
3492 ses_init_buf->trailer.session_req.calling_len = 32;
3493
3494 /*
3495 * calling name ends in null (byte 16) from old smb
3496 * convention.
3497 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003498 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003499 rfc1002mangle(ses_init_buf->trailer.
3500 session_req.calling_name,
3501 server->workstation_RFC1001_name,
3502 RFC1001_NAME_LEN_WITH_NULL);
3503 else
3504 rfc1002mangle(ses_init_buf->trailer.
3505 session_req.calling_name,
3506 "LINUX_CIFS_CLNT",
3507 RFC1001_NAME_LEN_WITH_NULL);
3508
3509 ses_init_buf->trailer.session_req.scope1 = 0;
3510 ses_init_buf->trailer.session_req.scope2 = 0;
3511 smb_buf = (struct smb_hdr *)ses_init_buf;
3512
3513 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003514 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003515 rc = smb_send(server, smb_buf, 0x44);
3516 kfree(ses_init_buf);
3517 /*
3518 * RFC1001 layer in at least one server
3519 * requires very short break before negprot
3520 * presumably because not expecting negprot
3521 * to follow so fast. This is a simple
3522 * solution that works without
3523 * complicating the code and causes no
3524 * significant slowing down on mount
3525 * for everyone else
3526 */
3527 usleep_range(1000, 2000);
3528 }
3529 /*
3530 * else the negprot may still work without this
3531 * even though malloc failed
3532 */
3533
3534 return rc;
3535}
3536
3537static int
3538generic_ip_connect(struct TCP_Server_Info *server)
3539{
3540 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003541 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003542 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003543 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003544 struct sockaddr *saddr;
3545
3546 saddr = (struct sockaddr *) &server->dstaddr;
3547
3548 if (server->dstaddr.ss_family == AF_INET6) {
3549 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3550 slen = sizeof(struct sockaddr_in6);
3551 sfamily = AF_INET6;
3552 } else {
3553 sport = ((struct sockaddr_in *) saddr)->sin_port;
3554 slen = sizeof(struct sockaddr_in);
3555 sfamily = AF_INET;
3556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003558 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003559 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3560 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003562 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003563 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003566
3567 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003568 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003569 server->ssocket = socket;
3570 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003571 if (sfamily == AF_INET6)
3572 cifs_reclassify_socket6(socket);
3573 else
3574 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 }
3576
Ben Greear3eb9a882010-09-01 17:06:02 -07003577 rc = bind_socket(server);
3578 if (rc < 0)
3579 return rc;
3580
Jeff Laytond5c56052008-12-01 18:42:33 -05003581 /*
3582 * Eventually check for other socket options to change from
3583 * the default. sock_setsockopt not used because it expects
3584 * user space buffer
3585 */
3586 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003587 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003588
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003589 /* make the bufsizes depend on wsize/rsize and max requests */
3590 if (server->noautotune) {
3591 if (socket->sk->sk_sndbuf < (200 * 1024))
3592 socket->sk->sk_sndbuf = 200 * 1024;
3593 if (socket->sk->sk_rcvbuf < (140 * 1024))
3594 socket->sk->sk_rcvbuf = 140 * 1024;
3595 }
3596
Steve French6a5fa2362010-01-01 01:28:43 +00003597 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003598 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003599 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3600 (char *)&val, sizeof(val));
3601 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003602 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3603 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003604 }
3605
Joe Perchesf96637b2013-05-04 22:12:25 -05003606 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003607 socket->sk->sk_sndbuf,
3608 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3609
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003610 rc = socket->ops->connect(socket, saddr, slen, 0);
3611 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003612 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003613 sock_release(socket);
3614 server->ssocket = NULL;
3615 return rc;
3616 }
3617
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003618 if (sport == htons(RFC1001_PORT))
3619 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 return rc;
3622}
3623
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003624static int
3625ip_connect(struct TCP_Server_Info *server)
3626{
Steve French6da97912011-03-13 18:55:55 +00003627 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003628 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3629 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3630
3631 if (server->dstaddr.ss_family == AF_INET6)
3632 sport = &addr6->sin6_port;
3633 else
3634 sport = &addr->sin_port;
3635
3636 if (*sport == 0) {
3637 int rc;
3638
3639 /* try with 445 port at first */
3640 *sport = htons(CIFS_PORT);
3641
3642 rc = generic_ip_connect(server);
3643 if (rc >= 0)
3644 return rc;
3645
3646 /* if it failed, try with 139 port */
3647 *sport = htons(RFC1001_PORT);
3648 }
3649
3650 return generic_ip_connect(server);
3651}
3652
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003653void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003654 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003655{
3656 /* if we are reconnecting then should we check to see if
3657 * any requested capabilities changed locally e.g. via
3658 * remount but we can not do much about it here
3659 * if they have (even if we could detect it by the following)
3660 * Perhaps we could add a backpointer to array of sb from tcon
3661 * or if we change to make all sb to same share the same
3662 * sb as NFS - then we only have one backpointer to sb.
3663 * What if we wanted to mount the server share twice once with
3664 * and once without posixacls or posix paths? */
3665 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003666
Steve Frenchc18c8422007-07-18 23:21:09 +00003667 if (vol_info && vol_info->no_linux_ext) {
3668 tcon->fsUnixInfo.Capability = 0;
3669 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003670 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003671 return;
3672 } else if (vol_info)
3673 tcon->unix_ext = 1; /* Unix Extensions supported */
3674
3675 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003676 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003677 return;
3678 }
Steve French50c2f752007-07-13 00:33:32 +00003679
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003680 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003681 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003682 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003683 /* check for reconnect case in which we do not
3684 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003685 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003686 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003687 originally at mount time */
3688 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3689 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003690 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3691 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003692 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003693 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003694 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003695 cifs_dbg(VFS, "possible reconnect error\n");
3696 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003697 }
Steve French8af18972007-02-14 04:42:51 +00003698 }
Steve French50c2f752007-07-13 00:33:32 +00003699
Steve French6848b732011-05-26 18:38:54 +00003700 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003701 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003702
Steve French8af18972007-02-14 04:42:51 +00003703 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003704 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003705 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003706 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003707 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003708 if (cifs_sb)
3709 cifs_sb->mnt_cifs_flags |=
3710 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003711 }
3712
Steve French75865f8c2007-06-24 18:30:48 +00003713 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003714 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003715 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003716 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003717 if (cifs_sb)
3718 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003719 CIFS_MOUNT_POSIX_PATHS;
3720 }
Steve French50c2f752007-07-13 00:33:32 +00003721
Joe Perchesf96637b2013-05-04 22:12:25 -05003722 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003723#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003724 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003725 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003726 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003727 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003728 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003729 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003730 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003731 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003732 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003733 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003734 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003735 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003736 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003737 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003738 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003739 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003740 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003741 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003742#endif /* CIFS_DEBUG2 */
3743 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003744 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003745 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003746 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003747 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 +00003748
Steve French8af18972007-02-14 04:42:51 +00003749 }
3750 }
3751}
3752
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003753int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003754 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003755{
Jeff Layton2de970f2010-10-06 19:51:12 -04003756 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3757
Al Viro2ced6f62011-06-17 09:20:04 -04003758 spin_lock_init(&cifs_sb->tlink_tree_lock);
3759 cifs_sb->tlink_tree = RB_ROOT;
3760
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003761 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003762 * Temporarily set r/wsize for matching superblock. If we end up using
3763 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003764 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003765 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003766 cifs_sb->wsize = pvolume_info->wsize;
3767
Steve French3b795212008-11-13 19:45:32 +00003768 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3769 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3770 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3771 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003772 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3773 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003774
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303775 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003776 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303777
Steve French3b795212008-11-13 19:45:32 +00003778 if (pvolume_info->noperm)
3779 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3780 if (pvolume_info->setuids)
3781 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003782 if (pvolume_info->setuidfromacl)
3783 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003784 if (pvolume_info->server_ino)
3785 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3786 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003787 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3788 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003789 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3790 if (pvolume_info->no_xattr)
3791 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3792 if (pvolume_info->sfu_emul)
3793 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3794 if (pvolume_info->nobrl)
3795 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003796 if (pvolume_info->nohandlecache)
3797 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003798 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003799 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003800 if (pvolume_info->mand_lock)
3801 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003802 if (pvolume_info->rwpidforward)
3803 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003804 if (pvolume_info->cifs_acl)
3805 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003806 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003807 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003808 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3809 }
3810 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003811 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003812 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3813 }
Steve French3b795212008-11-13 19:45:32 +00003814 if (pvolume_info->override_uid)
3815 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3816 if (pvolume_info->override_gid)
3817 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3818 if (pvolume_info->dynperm)
3819 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303820 if (pvolume_info->fsc)
3821 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003822 if (pvolume_info->multiuser)
3823 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3824 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003825 if (pvolume_info->strict_io)
3826 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003827 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003828 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003829 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3830 }
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003831 if (pvolume_info->mfsymlinks) {
3832 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003833 /*
3834 * Our SFU ("Services for Unix" emulation does not allow
3835 * creating symlinks but does allow reading existing SFU
3836 * symlinks (it does allow both creating and reading SFU
3837 * style mknod and FIFOs though). When "mfsymlinks" and
3838 * "sfu" are both enabled at the same time, it allows
3839 * reading both types of symlinks, but will only create
3840 * them with mfsymlinks format. This allows better
3841 * Apple compatibility (probably better for Samba too)
3842 * while still recognizing old Windows style symlinks.
3843 */
3844 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003845 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003846 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003847 }
Steve French3b795212008-11-13 19:45:32 +00003848
3849 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003850 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003851
3852 if (pvolume_info->prepath) {
3853 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3854 if (cifs_sb->prepath == NULL)
3855 return -ENOMEM;
3856 }
3857
3858 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003859}
3860
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003861static void
3862cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003863{
Sean Finneyb9468452011-04-11 13:19:32 +00003864 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003865 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003866 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003867 kfree(volume_info->domainname);
3868 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003869 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003870}
3871
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003872void
3873cifs_cleanup_volume_info(struct smb_vol *volume_info)
3874{
3875 if (!volume_info)
3876 return;
3877 cleanup_volume_info_contents(volume_info);
3878 kfree(volume_info);
3879}
3880
3881
Steve French2d6d5892009-04-09 00:36:44 +00003882#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003883/*
3884 * cifs_build_path_to_root returns full path to root when we do not have an
3885 * exiting connection (tcon)
3886 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003887static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003888build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003889 const struct cifs_sb_info *cifs_sb)
3890{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003891 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003892 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003893 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003894
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003895 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003896 if (full_path == NULL)
3897 return ERR_PTR(-ENOMEM);
3898
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003899 strncpy(full_path, vol->UNC, unc_len);
3900 pos = full_path + unc_len;
3901
3902 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003903 *pos = CIFS_DIR_SEP(cifs_sb);
3904 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003905 pos += pplen;
3906 }
3907
3908 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003909 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003910 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003911 return full_path;
3912}
Sean Finneydd613942011-04-11 13:19:30 +00003913
3914/*
3915 * Perform a dfs referral query for a share and (optionally) prefix
3916 *
Sean Finney046462a2011-04-11 13:19:33 +00003917 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3918 * to a string containing updated options for the submount. Otherwise it
3919 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003920 *
3921 * Returns the rc from get_dfs_path to the caller, which can be used to
3922 * determine whether there were referrals.
3923 */
3924static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003925expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003926 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003927 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003928{
3929 int rc;
3930 unsigned int num_referrals = 0;
3931 struct dfs_info3_param *referrals = NULL;
3932 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3933
3934 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3935 if (IS_ERR(full_path))
3936 return PTR_ERR(full_path);
3937
3938 /* For DFS paths, skip the first '\' of the UNC */
3939 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3940
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003941 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003942 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003943
3944 if (!rc && num_referrals > 0) {
3945 char *fake_devname = NULL;
3946
3947 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3948 full_path + 1, referrals,
3949 &fake_devname);
3950
3951 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003952
Sean Finneydd613942011-04-11 13:19:30 +00003953 if (IS_ERR(mdata)) {
3954 rc = PTR_ERR(mdata);
3955 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003956 } else {
3957 cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003958 rc = cifs_setup_volume_info(volume_info, mdata,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003959 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00003960 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003961 kfree(fake_devname);
3962 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003963 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003964 }
3965 kfree(full_path);
3966 return rc;
3967}
Steve French2d6d5892009-04-09 00:36:44 +00003968#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003969
Jeff Layton04db79b2011-07-06 08:10:38 -04003970static int
3971cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003972 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003974 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003975
Steve Frenchc7c137b2018-06-06 17:59:29 -05003976 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04003977 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Jeff Layton7586b762008-12-01 18:41:49 -05003979 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003980 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05003981 kfree(volume_info->username);
3982 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05003983 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05003985 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003987 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003988 /* In userspace mount helper we can get user name from alternate
3989 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003990 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 }
3992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05003994 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003995 /* load_nls_default cannot return null */
3996 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003998 volume_info->local_nls = load_nls(volume_info->iocharset);
3999 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004000 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004001 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04004002 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 }
4004 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004005
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004006 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04004007}
4008
4009struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05004010cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04004011{
4012 int rc;
4013 struct smb_vol *volume_info;
4014
Jeff Layton6ee95422012-11-26 11:09:57 -05004015 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04004016 if (!volume_info)
4017 return ERR_PTR(-ENOMEM);
4018
Steve Frenchc7c137b2018-06-06 17:59:29 -05004019 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04004020 if (rc) {
4021 cifs_cleanup_volume_info(volume_info);
4022 volume_info = ERR_PTR(rc);
4023 }
4024
4025 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004026}
4027
Aurelien Aptela6b50582016-05-25 19:59:09 +02004028static int
4029cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4030 unsigned int xid,
4031 struct cifs_tcon *tcon,
4032 struct cifs_sb_info *cifs_sb,
4033 char *full_path)
4034{
4035 int rc;
4036 char *s;
4037 char sep, tmp;
4038
4039 sep = CIFS_DIR_SEP(cifs_sb);
4040 s = full_path;
4041
4042 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4043 while (rc == 0) {
4044 /* skip separators */
4045 while (*s == sep)
4046 s++;
4047 if (!*s)
4048 break;
4049 /* next separator */
4050 while (*s && *s != sep)
4051 s++;
4052
4053 /*
4054 * temporarily null-terminate the path at the end of
4055 * the current component
4056 */
4057 tmp = *s;
4058 *s = 0;
4059 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4060 full_path);
4061 *s = tmp;
4062 }
4063 return rc;
4064}
4065
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004066int
Al Viro2c6292a2011-06-17 09:05:48 -04004067cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004068{
Jeff Layton1daaae82012-03-21 06:30:40 -04004069 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004070 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004071 struct cifs_ses *ses;
Steve French96daf2b2011-05-27 04:34:02 +00004072 struct cifs_tcon *tcon;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004073 struct TCP_Server_Info *server;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004074 char *full_path;
4075 struct tcon_link *tlink;
4076#ifdef CONFIG_CIFS_DFS_UPCALL
4077 int referral_walks_count = 0;
Jeff Layton20547492011-07-09 12:21:07 -04004078#endif
Al Virodd854462011-06-17 08:24:42 -04004079
Jeff Layton20547492011-07-09 12:21:07 -04004080#ifdef CONFIG_CIFS_DFS_UPCALL
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004081try_mount_again:
4082 /* cleanup activities if we're chasing a referral */
4083 if (referral_walks_count) {
4084 if (tcon)
4085 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004086 else if (ses)
4087 cifs_put_smb_ses(ses);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004088
Sachin Prabhu1dfd18d2015-06-16 16:36:17 +01004089 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
4090
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004091 free_xid(xid);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004092 }
4093#endif
Jeff Layton1daaae82012-03-21 06:30:40 -04004094 rc = 0;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004095 tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004096 ses = NULL;
4097 server = NULL;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004098 full_path = NULL;
4099 tlink = NULL;
4100
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004101 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Jeff Layton63c038c2008-12-01 18:41:46 -05004103 /* get a reference to a tcp session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004104 server = cifs_get_tcp_session(volume_info);
4105 if (IS_ERR(server)) {
4106 rc = PTR_ERR(server);
Jeff Layton63c038c2008-12-01 18:41:46 -05004107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 }
Steve French141891f2016-09-23 00:44:16 -05004109 if ((volume_info->max_credits < 20) ||
4110 (volume_info->max_credits > 60000))
4111 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
4112 else
4113 server->max_credits = volume_info->max_credits;
Jeff Layton36988c72010-04-24 07:57:43 -04004114 /* get a reference to a SMB session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004115 ses = cifs_get_smb_ses(server, volume_info);
4116 if (IS_ERR(ses)) {
4117 rc = PTR_ERR(ses);
4118 ses = NULL;
Jeff Layton36988c72010-04-24 07:57:43 -04004119 goto mount_fail_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 }
Steve French50c2f752007-07-13 00:33:32 +00004121
Steve Frenchb618f002015-11-03 09:15:03 -06004122 if ((volume_info->persistent == true) && ((ses->server->capabilities &
4123 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
4124 cifs_dbg(VFS, "persistent handles not supported by server\n");
4125 rc = -EOPNOTSUPP;
4126 goto mount_fail_check;
4127 }
Steve French592fafe2015-11-03 10:08:53 -06004128
Jeff Laytond00c28d2010-04-24 07:57:44 -04004129 /* search for existing tcon to this server share */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004130 tcon = cifs_get_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04004131 if (IS_ERR(tcon)) {
4132 rc = PTR_ERR(tcon);
4133 tcon = NULL;
Mark Syms40920c22016-11-29 11:36:46 +00004134 if (rc == -EACCES)
4135 goto mount_fail_check;
4136
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004137 goto remote_path_check;
Jeff Laytond00c28d2010-04-24 07:57:44 -04004138 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004139
Steve Frenchce558b02018-05-31 19:16:54 -05004140 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4141 if (tcon->posix_extensions)
4142 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve Frenchce558b02018-05-31 19:16:54 -05004143
Steve French6848b732011-05-26 18:38:54 +00004144 /* tell server which Unix caps we support */
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004145 if (cap_unix(tcon->ses)) {
Steve French6848b732011-05-26 18:38:54 +00004146 /* reset of caps checks mount to see if unix extensions
4147 disabled for just this mount */
Al Viro2c6292a2011-06-17 09:05:48 -04004148 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
Steve French6848b732011-05-26 18:38:54 +00004149 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4150 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4151 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
4152 rc = -EACCES;
4153 goto mount_fail_check;
4154 }
4155 } else
4156 tcon->unix_ext = 0; /* server does not support them */
4157
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004158 /* do not care if a following call succeed - informational */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004159 if (!tcon->pipe && server->ops->qfs_tcon)
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004160 server->ops->qfs_tcon(xid, tcon);
Steve Frenchd82c2df2008-11-15 00:07:26 +00004161
Pavel Shilovsky24985c52012-09-18 16:20:28 -07004162 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
4163 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04004164
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004165remote_path_check:
Sean Finneyc1508ca2011-04-11 13:19:31 +00004166#ifdef CONFIG_CIFS_DFS_UPCALL
4167 /*
4168 * Perform an unconditional check for whether there are DFS
4169 * referrals for this path without prefix, to provide support
4170 * for DFS referrals from w2k8 servers which don't seem to respond
4171 * with PATH_NOT_COVERED to requests that include the prefix.
4172 * Chase the referral if found, otherwise continue normally.
4173 */
4174 if (referral_walks_count == 0) {
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004175 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
4176 false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004177 if (!refrc) {
4178 referral_walks_count++;
4179 goto try_mount_again;
4180 }
4181 }
4182#endif
4183
Steve Frenchf87d39d2011-05-27 03:50:55 +00004184 /* check if a whole path is not remote */
Jeff Layton70945642011-03-14 13:48:08 -04004185 if (!rc && tcon) {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004186 if (!server->ops->is_path_accessible) {
4187 rc = -ENOSYS;
4188 goto mount_fail_check;
4189 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004190 /*
4191 * cifs_build_path_to_root works only when we have a valid tcon
4192 */
Sachin Prabhu374402a2016-12-15 12:31:19 +05304193 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
4194 tcon->Flags & SMB_SHARE_IS_IN_DFS);
Igor Mammedove4cce942009-02-10 14:10:26 +03004195 if (full_path == NULL) {
4196 rc = -ENOMEM;
4197 goto mount_fail_check;
4198 }
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004199 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4200 full_path);
Jeff Layton03ceace2010-12-06 21:07:33 -05004201 if (rc != 0 && rc != -EREMOTE) {
Igor Mammedove4cce942009-02-10 14:10:26 +03004202 kfree(full_path);
4203 goto mount_fail_check;
4204 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004205
Sachin Prabhud1713562016-09-06 13:22:34 +01004206 if (rc != -EREMOTE) {
4207 rc = cifs_are_all_path_components_accessible(server,
Aurelien Aptela6b50582016-05-25 19:59:09 +02004208 xid, tcon, cifs_sb,
4209 full_path);
Sachin Prabhud1713562016-09-06 13:22:34 +01004210 if (rc != 0) {
4211 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4212 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4213 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4214 rc = 0;
4215 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004216 }
Igor Mammedove4cce942009-02-10 14:10:26 +03004217 kfree(full_path);
4218 }
4219
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004220 /* get referral if needed */
4221 if (rc == -EREMOTE) {
Steve Frenchd036f502009-04-03 03:12:08 +00004222#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004223 if (referral_walks_count > MAX_NESTED_LINKS) {
4224 /*
4225 * BB: when we implement proper loop detection,
4226 * we will remove this check. But now we need it
4227 * to prevent an indefinite loop if 'DFS tree' is
4228 * misconfigured (i.e. has loops).
4229 */
4230 rc = -ELOOP;
4231 goto mount_fail_check;
4232 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004233
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004234 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
Jeff Layton7b91e262009-07-23 15:22:30 -04004235
Sean Finneydd613942011-04-11 13:19:30 +00004236 if (!rc) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004237 referral_walks_count++;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004238 goto try_mount_again;
4239 }
Sean Finneydd613942011-04-11 13:19:30 +00004240 goto mount_fail_check;
Steve Frenchd036f502009-04-03 03:12:08 +00004241#else /* No DFS support, return error on mount */
4242 rc = -EOPNOTSUPP;
4243#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004244 }
4245
Jeff Layton9d002df2010-10-06 19:51:11 -04004246 if (rc)
4247 goto mount_fail_check;
4248
4249 /* now, hang the tcon off of the superblock */
4250 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
4251 if (tlink == NULL) {
4252 rc = -ENOMEM;
4253 goto mount_fail_check;
4254 }
4255
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004256 tlink->tl_uid = ses->linux_uid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004257 tlink->tl_tcon = tcon;
4258 tlink->tl_time = jiffies;
4259 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4260 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4261
Jeff Layton413e6612010-10-28 13:33:38 -04004262 cifs_sb->master_tlink = tlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004263 spin_lock(&cifs_sb->tlink_tree_lock);
4264 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4265 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton413e6612010-10-28 13:33:38 -04004266
Jeff Laytonda472fc2012-03-23 14:40:53 -04004267 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004268 TLINK_IDLE_EXPIRE);
4269
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004270mount_fail_check:
4271 /* on error free sesinfo and tcon struct if needed */
4272 if (rc) {
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004273 /* If find_unc succeeded then rc == 0 so we can not end */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004274 /* up accidentally freeing someone elses tcon struct */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004275 if (tcon)
4276 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004277 else if (ses)
4278 cifs_put_smb_ses(ses);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004279 else
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004280 cifs_put_tcp_session(server, 0);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004281 }
4282
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004283out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004284 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 return rc;
4286}
4287
Jeff Layton8d1bca32011-06-11 21:17:10 -04004288/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004289 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004292CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004293 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 const struct nls_table *nls_codepage)
4295{
4296 struct smb_hdr *smb_buffer;
4297 struct smb_hdr *smb_buffer_response;
4298 TCONX_REQ *pSMB;
4299 TCONX_RSP *pSMBr;
4300 unsigned char *bcc_ptr;
4301 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004302 int length;
4303 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305 if (ses == NULL)
4306 return -EIO;
4307
4308 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004309 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 smb_buffer_response = smb_buffer;
4313
4314 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4315 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004316
Pavel Shilovsky88257362012-05-23 14:01:59 +04004317 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 smb_buffer->Uid = ses->Suid;
4319 pSMB = (TCONX_REQ *) smb_buffer;
4320 pSMBr = (TCONX_RSP *) smb_buffer_response;
4321
4322 pSMB->AndXCommand = 0xFF;
4323 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004325 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004326 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004327 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004328 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004329 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004330 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004331 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004332 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4333 specified as required (when that support is added to
4334 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004335 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004336 by Samba (not sure whether other servers allow
4337 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004338#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004339 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004340 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004341 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004342 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004343 SECMODE_PW_ENCRYPT ? true : false,
4344 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004345 else
4346#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004347 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004348 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004349 if (rc) {
4350 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4351 __func__, rc);
4352 cifs_buf_release(smb_buffer);
4353 return rc;
4354 }
Steve Frencheeac8042006-01-13 21:34:58 -08004355
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004356 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004357 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004358 /* must align unicode strings */
4359 *bcc_ptr = 0; /* null byte password */
4360 bcc_ptr++;
4361 }
Steve Frencheeac8042006-01-13 21:34:58 -08004362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Jeff Layton38d77c52013-05-26 07:01:00 -04004364 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4366
4367 if (ses->capabilities & CAP_STATUS32) {
4368 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4369 }
4370 if (ses->capabilities & CAP_DFS) {
4371 smb_buffer->Flags2 |= SMBFLG2_DFS;
4372 }
4373 if (ses->capabilities & CAP_UNICODE) {
4374 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4375 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004376 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004377 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004378 (/* server len*/ + 256 /* share len */), nls_codepage);
4379 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 bcc_ptr += 2; /* skip trailing null */
4381 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 strcpy(bcc_ptr, tree);
4383 bcc_ptr += strlen(tree) + 1;
4384 }
4385 strcpy(bcc_ptr, "?????");
4386 bcc_ptr += strlen("?????");
4387 bcc_ptr += 1;
4388 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004389 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4390 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 pSMB->ByteCount = cpu_to_le16(count);
4392
Steve French133672e2007-11-13 22:41:37 +00004393 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004394 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004397 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004398 bool is_unicode;
4399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004401 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 tcon->tid = smb_buffer_response->Tid;
4403 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004404 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004405 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004406 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4407 is_unicode = true;
4408 else
4409 is_unicode = false;
4410
Jeff Laytoncc20c032009-04-30 07:16:21 -04004411
Steve French50c2f752007-07-13 00:33:32 +00004412 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004413 if (length == 3) {
4414 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4415 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004416 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004417 tcon->ipc = true;
4418 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004419 }
4420 } else if (length == 2) {
4421 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4422 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004423 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004424 }
4425 }
Steve French50c2f752007-07-13 00:33:32 +00004426 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004427 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004428 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004429
4430 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004431 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004432 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004433 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004434 nls_codepage);
4435
Joe Perchesf96637b2013-05-04 22:12:25 -05004436 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004437
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004438 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004439 (smb_buffer_response->WordCount == 7))
4440 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004441 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4442 else
4443 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004444 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
4446
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004447 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return rc;
4449}
4450
Al Viro2e32cf52013-10-03 12:53:37 -04004451static void delayed_free(struct rcu_head *p)
4452{
4453 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4454 unload_nls(sbi->local_nls);
4455 kfree(sbi);
4456}
4457
Al Viro2a9b9952011-06-17 09:27:16 -04004458void
4459cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460{
Jeff Laytonb647c352010-10-28 11:16:44 -04004461 struct rb_root *root = &cifs_sb->tlink_tree;
4462 struct rb_node *node;
4463 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Jeff Layton2de970f2010-10-06 19:51:12 -04004465 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4466
Jeff Laytonb647c352010-10-28 11:16:44 -04004467 spin_lock(&cifs_sb->tlink_tree_lock);
4468 while ((node = rb_first(root))) {
4469 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4470 cifs_get_tlink(tlink);
4471 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4472 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004473
Jeff Laytonb647c352010-10-28 11:16:44 -04004474 spin_unlock(&cifs_sb->tlink_tree_lock);
4475 cifs_put_tlink(tlink);
4476 spin_lock(&cifs_sb->tlink_tree_lock);
4477 }
4478 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004479
Al Virod757d712011-06-17 09:42:43 -04004480 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004481 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004482 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004485int
4486cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487{
4488 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004489 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004491 if (!server->ops->need_neg || !server->ops->negotiate)
4492 return -ENOSYS;
4493
Jeff Layton198b5682010-04-24 07:57:48 -04004494 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004495 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Pavel Shilovsky45275782012-05-17 17:53:29 +04004498 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004499
4500 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004501 if (rc == 0) {
4502 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004503 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004504 server->tcpStatus = CifsGood;
4505 else
4506 rc = -EHOSTDOWN;
4507 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 }
Steve French26b994f2008-08-06 05:11:33 +00004509
Jeff Layton198b5682010-04-24 07:57:48 -04004510 return rc;
4511}
Steve French26b994f2008-08-06 05:11:33 +00004512
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004513int
4514cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4515 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004516{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004517 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004518 struct TCP_Server_Info *server = ses->server;
4519
Jeff Layton198b5682010-04-24 07:57:48 -04004520 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004521 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004522 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004523
Joe Perchesf96637b2013-05-04 22:12:25 -05004524 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004525 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004526
Shu Wangf5c4ba82017-09-08 18:48:33 +08004527 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05004528 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08004529 ses->auth_key.response);
4530 kfree(ses->auth_key.response);
4531 ses->auth_key.response = NULL;
4532 ses->auth_key.len = 0;
4533 }
4534
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004535 if (server->ops->sess_setup)
4536 rc = server->ops->sess_setup(xid, ses, nls_info);
4537
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004538 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004539 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 return rc;
4542}
4543
Jeff Layton8a8798a2012-01-17 16:09:15 -05004544static int
4545cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4546{
Jeff Layton3f618222013-06-12 19:52:14 -05004547 vol->sectype = ses->sectype;
4548
4549 /* krb5 is special, since we don't need username or pw */
4550 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004551 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004552
4553 return cifs_set_cifscreds(vol, ses);
4554}
4555
Steve French96daf2b2011-05-27 04:34:02 +00004556static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004557cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004558{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004559 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004560 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4561 struct cifs_ses *ses;
4562 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004563 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004564
4565 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004566 if (vol_info == NULL)
4567 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004568
Jeff Layton9d002df2010-10-06 19:51:11 -04004569 vol_info->local_nls = cifs_sb->local_nls;
4570 vol_info->linux_uid = fsuid;
4571 vol_info->cred_uid = fsuid;
4572 vol_info->UNC = master_tcon->treeName;
4573 vol_info->retry = master_tcon->retry;
4574 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05004575 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04004576 vol_info->local_lease = master_tcon->local_lease;
4577 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004578 vol_info->sectype = master_tcon->ses->sectype;
4579 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004580
Jeff Layton8a8798a2012-01-17 16:09:15 -05004581 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4582 if (rc) {
4583 tcon = ERR_PTR(rc);
4584 goto out;
4585 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004586
4587 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304588 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004589 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304590 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004591
4592 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4593 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004594 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004595 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004596 goto out;
4597 }
4598
4599 tcon = cifs_get_tcon(ses, vol_info);
4600 if (IS_ERR(tcon)) {
4601 cifs_put_smb_ses(ses);
4602 goto out;
4603 }
4604
Steve Frenchce558b02018-05-31 19:16:54 -05004605 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4606 if (tcon->posix_extensions)
4607 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05004608
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004609 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004610 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05004611
Jeff Layton9d002df2010-10-06 19:51:11 -04004612out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004613 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01004614 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004615 kfree(vol_info);
4616
4617 return tcon;
4618}
4619
Steve French96daf2b2011-05-27 04:34:02 +00004620struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004621cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4622{
4623 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4624}
4625
Jeff Laytonb647c352010-10-28 11:16:44 -04004626/* find and return a tlink with given uid */
4627static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004628tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004629{
4630 struct rb_node *node = root->rb_node;
4631 struct tcon_link *tlink;
4632
4633 while (node) {
4634 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4635
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004636 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004637 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004638 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004639 node = node->rb_right;
4640 else
4641 return tlink;
4642 }
4643 return NULL;
4644}
4645
4646/* insert a tcon_link into the tree */
4647static void
4648tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4649{
4650 struct rb_node **new = &(root->rb_node), *parent = NULL;
4651 struct tcon_link *tlink;
4652
4653 while (*new) {
4654 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4655 parent = *new;
4656
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004657 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004658 new = &((*new)->rb_left);
4659 else
4660 new = &((*new)->rb_right);
4661 }
4662
4663 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4664 rb_insert_color(&new_tlink->tl_rbnode, root);
4665}
4666
Jeff Layton9d002df2010-10-06 19:51:11 -04004667/*
4668 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4669 * current task.
4670 *
4671 * If the superblock doesn't refer to a multiuser mount, then just return
4672 * the master tcon for the mount.
4673 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304674 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004675 * exists, then check to see if it's pending construction. If it is then wait
4676 * for construction to complete. Once it's no longer pending, check to see if
4677 * it failed and either return an error or retry construction, depending on
4678 * the timeout.
4679 *
4680 * If one doesn't exist then insert a new tcon_link struct into the tree and
4681 * try to construct a new one.
4682 */
4683struct tcon_link *
4684cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4685{
4686 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004687 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004688 struct tcon_link *tlink, *newtlink;
4689
4690 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4691 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4692
4693 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004694 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004695 if (tlink)
4696 cifs_get_tlink(tlink);
4697 spin_unlock(&cifs_sb->tlink_tree_lock);
4698
4699 if (tlink == NULL) {
4700 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4701 if (newtlink == NULL)
4702 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004703 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004704 newtlink->tl_tcon = ERR_PTR(-EACCES);
4705 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4706 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4707 cifs_get_tlink(newtlink);
4708
Jeff Layton9d002df2010-10-06 19:51:11 -04004709 spin_lock(&cifs_sb->tlink_tree_lock);
4710 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004711 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004712 if (tlink) {
4713 cifs_get_tlink(tlink);
4714 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004715 kfree(newtlink);
4716 goto wait_for_construction;
4717 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004718 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004719 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4720 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004721 } else {
4722wait_for_construction:
4723 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004724 TASK_INTERRUPTIBLE);
4725 if (ret) {
4726 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004727 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004728 }
4729
4730 /* if it's good, return it */
4731 if (!IS_ERR(tlink->tl_tcon))
4732 return tlink;
4733
4734 /* return error if we tried this already recently */
4735 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4736 cifs_put_tlink(tlink);
4737 return ERR_PTR(-EACCES);
4738 }
4739
4740 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4741 goto wait_for_construction;
4742 }
4743
4744 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4745 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4746 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4747
4748 if (IS_ERR(tlink->tl_tcon)) {
4749 cifs_put_tlink(tlink);
4750 return ERR_PTR(-EACCES);
4751 }
4752
4753 return tlink;
4754}
Jeff Layton2de970f2010-10-06 19:51:12 -04004755
4756/*
4757 * periodic workqueue job that scans tcon_tree for a superblock and closes
4758 * out tcons.
4759 */
4760static void
4761cifs_prune_tlinks(struct work_struct *work)
4762{
4763 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4764 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004765 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004766 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004767 struct rb_node *tmp;
4768 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004769
Jeff Laytonb647c352010-10-28 11:16:44 -04004770 /*
4771 * Because we drop the spinlock in the loop in order to put the tlink
4772 * it's not guarded against removal of links from the tree. The only
4773 * places that remove entries from the tree are this function and
4774 * umounts. Because this function is non-reentrant and is canceled
4775 * before umount can proceed, this is safe.
4776 */
4777 spin_lock(&cifs_sb->tlink_tree_lock);
4778 node = rb_first(root);
4779 while (node != NULL) {
4780 tmp = node;
4781 node = rb_next(tmp);
4782 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4783
4784 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4785 atomic_read(&tlink->tl_count) != 0 ||
4786 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4787 continue;
4788
4789 cifs_get_tlink(tlink);
4790 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4791 rb_erase(tmp, root);
4792
Jeff Layton2de970f2010-10-06 19:51:12 -04004793 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004794 cifs_put_tlink(tlink);
4795 spin_lock(&cifs_sb->tlink_tree_lock);
4796 }
4797 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004798
Jeff Laytonda472fc2012-03-23 14:40:53 -04004799 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004800 TLINK_IDLE_EXPIRE);
4801}