blob: 842f45859968b3b21e9926c6cf6449b8f93ca87c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/connect.c
3 *
Steve French1080ef72011-02-24 18:07:19 +00004 * Copyright (C) International Business Machines Corp., 2002,2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
Steve Frenchfb8c4b12007-07-10 01:16:18 +000019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010024#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/list.h>
26#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/pagemap.h>
29#include <linux/ctype.h>
30#include <linux/utsname.h>
31#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070032#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070033#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000034#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070035#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040037#include <linux/namei.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020038#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040041#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040042#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050043#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000044#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040045#include <linux/parser.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060046#include <linux/bvec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "cifspdu.h"
48#include "cifsglob.h"
49#include "cifsproto.h"
50#include "cifs_unicode.h"
51#include "cifs_debug.h"
52#include "cifs_fs_sb.h"
53#include "ntlmssp.h"
54#include "nterr.h"
55#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053056#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070057#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070058#include "smbdirect.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050061extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jeff Layton2de970f2010-10-06 19:51:12 -040063/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040064#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040065#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040066
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040067enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040068 /* Mount options that take no arguments */
69 Opt_user_xattr, Opt_nouser_xattr,
70 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040071 Opt_forcegid, Opt_noforcegid,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040072 Opt_noblocksend, Opt_noautotune,
73 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050074 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040075 Opt_mapchars, Opt_nomapchars, Opt_sfu,
76 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
Steve Frenchb3266142018-05-20 23:41:10 -050077 Opt_noposixpaths, Opt_nounix, Opt_unix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040078 Opt_nocase,
79 Opt_brl, Opt_nobrl,
Steve French3d4ef9a2018-04-25 22:19:09 -050080 Opt_handlecache, Opt_nohandlecache,
Steve French95932652016-09-23 01:36:34 -050081 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040082 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
83 Opt_nohard, Opt_nosoft,
84 Opt_nointr, Opt_intr,
85 Opt_nostrictsync, Opt_strictsync,
86 Opt_serverino, Opt_noserverino,
87 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
88 Opt_acl, Opt_noacl, Opt_locallease,
Jeff Layton1b359202012-09-19 15:20:27 -070089 Opt_sign, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040090 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040091 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050092 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060093 Opt_resilient, Opt_noresilient,
Long Li8339dd32017-11-07 01:54:55 -070094 Opt_domainauto, Opt_rdma,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040095
96 /* Mount options which take numeric value */
97 Opt_backupuid, Opt_backupgid, Opt_uid,
98 Opt_cruid, Opt_gid, Opt_file_mode,
99 Opt_dirmode, Opt_port,
100 Opt_rsize, Opt_wsize, Opt_actimeo,
Steve French141891f2016-09-23 00:44:16 -0500101 Opt_echo_interval, Opt_max_credits,
Steve French8b217fe2016-11-11 22:36:20 -0600102 Opt_snapshot,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400103
104 /* Mount options which take string value */
105 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400106 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400107 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400108 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400109
110 /* Mount options to be ignored */
111 Opt_ignore,
112
113 /* Options which could be blank */
114 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100115 Opt_blank_user,
116 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400117
118 Opt_err
119};
120
121static const match_table_t cifs_mount_option_tokens = {
122
123 { Opt_user_xattr, "user_xattr" },
124 { Opt_nouser_xattr, "nouser_xattr" },
125 { Opt_forceuid, "forceuid" },
126 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400127 { Opt_forcegid, "forcegid" },
128 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400129 { Opt_noblocksend, "noblocksend" },
130 { Opt_noautotune, "noautotune" },
131 { Opt_hard, "hard" },
132 { Opt_soft, "soft" },
133 { Opt_perm, "perm" },
134 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500135 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400136 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500137 { Opt_mapposix, "mapposix" }, /* SFM style */
138 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400139 { Opt_sfu, "sfu" },
140 { Opt_nosfu, "nosfu" },
141 { Opt_nodfs, "nodfs" },
142 { Opt_posixpaths, "posixpaths" },
143 { Opt_noposixpaths, "noposixpaths" },
144 { Opt_nounix, "nounix" },
145 { Opt_nounix, "nolinux" },
Steve Frenchb3266142018-05-20 23:41:10 -0500146 { Opt_nounix, "noposix" },
147 { Opt_unix, "unix" },
148 { Opt_unix, "linux" },
149 { Opt_unix, "posix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400150 { Opt_nocase, "nocase" },
151 { Opt_nocase, "ignorecase" },
152 { Opt_brl, "brl" },
153 { Opt_nobrl, "nobrl" },
Steve French3d4ef9a2018-04-25 22:19:09 -0500154 { Opt_handlecache, "handlecache" },
155 { Opt_nohandlecache, "nohandlecache" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400156 { Opt_nobrl, "nolock" },
157 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400158 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400159 { Opt_setuids, "setuids" },
160 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500161 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400162 { Opt_dynperm, "dynperm" },
163 { Opt_nodynperm, "nodynperm" },
164 { Opt_nohard, "nohard" },
165 { Opt_nosoft, "nosoft" },
166 { Opt_nointr, "nointr" },
167 { Opt_intr, "intr" },
168 { Opt_nostrictsync, "nostrictsync" },
169 { Opt_strictsync, "strictsync" },
170 { Opt_serverino, "serverino" },
171 { Opt_noserverino, "noserverino" },
172 { Opt_rwpidforward, "rwpidforward" },
173 { Opt_cifsacl, "cifsacl" },
174 { Opt_nocifsacl, "nocifsacl" },
175 { Opt_acl, "acl" },
176 { Opt_noacl, "noacl" },
177 { Opt_locallease, "locallease" },
178 { Opt_sign, "sign" },
179 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400180 { Opt_noac, "noac" },
181 { Opt_fsc, "fsc" },
182 { Opt_mfsymlinks, "mfsymlinks" },
183 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400184 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400185 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500186 { Opt_persistent, "persistenthandles"},
187 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600188 { Opt_resilient, "resilienthandles"},
189 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530190 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700191 { Opt_rdma, "rdma"},
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400192
193 { Opt_backupuid, "backupuid=%s" },
194 { Opt_backupgid, "backupgid=%s" },
195 { Opt_uid, "uid=%s" },
196 { Opt_cruid, "cruid=%s" },
197 { Opt_gid, "gid=%s" },
198 { Opt_file_mode, "file_mode=%s" },
199 { Opt_dirmode, "dirmode=%s" },
200 { Opt_dirmode, "dir_mode=%s" },
201 { Opt_port, "port=%s" },
202 { Opt_rsize, "rsize=%s" },
203 { Opt_wsize, "wsize=%s" },
204 { Opt_actimeo, "actimeo=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600205 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500206 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600207 { Opt_snapshot, "snapshot=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400208
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100209 { Opt_blank_user, "user=" },
210 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400211 { Opt_user, "user=%s" },
212 { Opt_user, "username=%s" },
213 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100214 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400215 { Opt_pass, "pass=%s" },
216 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100217 { Opt_blank_ip, "ip=" },
218 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400219 { Opt_ip, "ip=%s" },
220 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400221 { Opt_ignore, "unc=%s" },
222 { Opt_ignore, "target=%s" },
223 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400224 { Opt_domain, "dom=%s" },
225 { Opt_domain, "domain=%s" },
226 { Opt_domain, "workgroup=%s" },
227 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400228 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400229 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400230 { Opt_netbiosname, "netbiosname=%s" },
231 { Opt_servern, "servern=%s" },
232 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400233 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400234 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400235 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400236
237 { Opt_ignore, "cred" },
238 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400239 { Opt_ignore, "cred=%s" },
240 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400241 { Opt_ignore, "guest" },
242 { Opt_ignore, "rw" },
243 { Opt_ignore, "ro" },
244 { Opt_ignore, "suid" },
245 { Opt_ignore, "nosuid" },
246 { Opt_ignore, "exec" },
247 { Opt_ignore, "noexec" },
248 { Opt_ignore, "nodev" },
249 { Opt_ignore, "noauto" },
250 { Opt_ignore, "dev" },
251 { Opt_ignore, "mand" },
252 { Opt_ignore, "nomand" },
253 { Opt_ignore, "_netdev" },
254
255 { Opt_err, NULL }
256};
257
258enum {
259 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
260 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400261 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
262 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400263 Opt_sec_none,
264
265 Opt_sec_err
266};
267
268static const match_table_t cifs_secflavor_tokens = {
269 { Opt_sec_krb5, "krb5" },
270 { Opt_sec_krb5i, "krb5i" },
271 { Opt_sec_krb5p, "krb5p" },
272 { Opt_sec_ntlmsspi, "ntlmsspi" },
273 { Opt_sec_ntlmssp, "ntlmssp" },
274 { Opt_ntlm, "ntlm" },
275 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400276 { Opt_sec_ntlmv2, "nontlm" },
277 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400278 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400279 { Opt_sec_lanman, "lanman" },
280 { Opt_sec_none, "none" },
281
282 { Opt_sec_err, NULL }
283};
284
Jeff Layton15b6a472012-05-16 07:50:15 -0400285/* cache flavors */
286enum {
287 Opt_cache_loose,
288 Opt_cache_strict,
289 Opt_cache_none,
290 Opt_cache_err
291};
292
293static const match_table_t cifs_cacheflavor_tokens = {
294 { Opt_cache_loose, "loose" },
295 { Opt_cache_strict, "strict" },
296 { Opt_cache_none, "none" },
297 { Opt_cache_err, NULL }
298};
299
Jeff Layton23db65f2012-05-15 12:20:51 -0400300static const match_table_t cifs_smb_version_tokens = {
301 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600302 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000303 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500304 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500305 { Smb_302, SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600306#ifdef CONFIG_CIFS_SMB311
307 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500308 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600309#endif /* SMB311 */
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/*
323 * cifs tcp session reconnection
324 *
325 * mark tcp session as reconnecting so temporarily locked
326 * mark all smb sessions as reconnecting for tcp session
327 * reconnect tcp session
328 * wake up waiters on reconnection? - (not needed currently)
329 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400330int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331cifs_reconnect(struct TCP_Server_Info *server)
332{
333 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500334 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000335 struct cifs_ses *ses;
336 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000337 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400338 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000341 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000342 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 next time through the loop */
344 spin_unlock(&GlobalMid_Lock);
345 return rc;
346 } else
347 server->tcpStatus = CifsNeedReconnect;
348 spin_unlock(&GlobalMid_Lock);
349 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400350 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Joe Perchesf96637b2013-05-04 22:12:25 -0500352 cifs_dbg(FYI, "Reconnecting tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* before reconnecting the tcp session, mark the smb session (uid)
355 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500356 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
357 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530358 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500359 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000360 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500361 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500362 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000363 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500364 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100366 if (ses->tcon_ipc)
367 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530369 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500372 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500373 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000374 if (server->ssocket) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500375 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
376 server->ssocket->state, server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800377 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesf96637b2013-05-04 22:12:25 -0500378 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
379 server->ssocket->state, server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 sock_release(server->ssocket);
381 server->ssocket = NULL;
382 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500383 server->sequence_number = 0;
384 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500385 kfree(server->session_key.response);
386 server->session_key.response = NULL;
387 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000388 server->lstrp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500390 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400391 INIT_LIST_HEAD(&retry_list);
Joe Perchesf96637b2013-05-04 22:12:25 -0500392 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500394 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
395 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400396 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
397 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400398 list_move(&mid_entry->qhead, &retry_list);
399 }
400 spin_unlock(&GlobalMid_Lock);
Rabin Vincent820962d2015-12-23 07:32:41 +0100401 mutex_unlock(&server->srv_mutex);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400402
Joe Perchesf96637b2013-05-04 22:12:25 -0500403 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400404 list_for_each_safe(tmp, tmp2, &retry_list) {
405 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500406 list_del_init(&mid_entry->qhead);
407 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400410 do {
Steve French6c3d8902006-07-31 22:46:20 +0000411 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300412
413 /* we should try only the port we connected to before */
Jeff Layton73e216a2013-09-05 08:38:10 -0400414 mutex_lock(&server->srv_mutex);
Long Li781a8052017-11-22 17:38:36 -0700415 if (cifs_rdma_enabled(server))
416 rc = smbd_reconnect(server);
417 else
418 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000419 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500420 cifs_dbg(FYI, "reconnect error %d\n", rc);
Federico Sauter4afe2602015-03-17 17:45:28 +0100421 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700422 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 } else {
424 atomic_inc(&tcpSesReconnectCount);
425 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000426 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000427 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000428 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100429 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400431 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500432
Sachin Prabhub8c60012016-10-20 19:52:24 -0400433 if (server->tcpStatus == CifsNeedNegotiate)
434 mod_delayed_work(cifsiod_wq, &server->echo, 0);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return rc;
437}
438
Jeff Laytonc74093b2011-01-11 07:24:23 -0500439static void
440cifs_echo_request(struct work_struct *work)
441{
442 int rc;
443 struct TCP_Server_Info *server = container_of(work,
444 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400445 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500446
Jeff Layton247ec9b2011-02-04 17:09:50 -0500447 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400448 * If we need to renegotiate, set echo interval to zero to
449 * immediately call echo service where we can renegotiate.
450 */
451 if (server->tcpStatus == CifsNeedNegotiate)
452 echo_interval = 0;
453 else
454 echo_interval = server->echo_interval;
455
456 /*
457 * We cannot send an echo if it is disabled.
458 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500459 */
Steve French4fcd1812016-06-22 20:12:05 -0500460
461 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400462 server->tcpStatus == CifsExiting ||
463 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400464 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600465 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500466 goto requeue_echo;
467
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400468 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500469 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500470 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
471 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500472
473requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400474 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500475}
476
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400477static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400478allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400479{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400480 if (!server->bigbuf) {
481 server->bigbuf = (char *)cifs_buf_get();
482 if (!server->bigbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500483 cifs_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400484 msleep(3000);
485 /* retry will check if exiting */
486 return false;
487 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400488 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400489 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400490 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400491 }
492
Jeff Layton2a37ef92011-10-19 15:29:23 -0400493 if (!server->smallbuf) {
494 server->smallbuf = (char *)cifs_small_buf_get();
495 if (!server->smallbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500496 cifs_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400497 msleep(1000);
498 /* retry will check if exiting */
499 return false;
500 }
501 /* beginning of smb buffer is cleared in our buf_get */
502 } else {
503 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400504 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400505 }
506
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400507 return true;
508}
509
Jeff Laytonba749e62011-10-11 06:41:32 -0400510static bool
511server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400512{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300513 /*
514 * We need to wait 2 echo intervals to make sure we handle such
515 * situations right:
516 * 1s client sends a normal SMB request
517 * 2s client gets a response
518 * 30s echo workqueue job pops, and decides we got a response recently
519 * and don't need to send another
520 * ...
521 * 65s kernel_recvmsg times out, and we see that we haven't gotten
522 * a response in >60s.
523 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200524 if ((server->tcpStatus == CifsGood ||
525 server->tcpStatus == CifsNeedNegotiate) &&
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600526 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
527 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
528 server->hostname, (2 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400529 cifs_reconnect(server);
530 wake_up(&server->response_q);
531 return true;
532 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400533
Jeff Laytonba749e62011-10-11 06:41:32 -0400534 return false;
535}
536
Al Viro71335662016-01-09 19:54:50 -0500537static int
538cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400539{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400540 int length = 0;
541 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400542
Al Viro71335662016-01-09 19:54:50 -0500543 smb_msg->msg_control = NULL;
544 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400545
Al Viro71335662016-01-09 19:54:50 -0500546 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500547 try_to_freeze();
548
Al Viro71335662016-01-09 19:54:50 -0500549 if (server_unresponsive(server))
550 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700551 if (cifs_rdma_enabled(server) && server->smbd_conn)
552 length = smbd_recv(server->smbd_conn, smb_msg);
553 else
554 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500555
556 if (server->tcpStatus == CifsExiting)
557 return -ESHUTDOWN;
558
559 if (server->tcpStatus == CifsNeedReconnect) {
560 cifs_reconnect(server);
561 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400562 }
563
Al Viro71335662016-01-09 19:54:50 -0500564 if (length == -ERESTARTSYS ||
565 length == -EAGAIN ||
566 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400567 /*
568 * Minimum sleep to prevent looping, allowing socket
569 * to clear and app threads to set tcpStatus
570 * CifsNeedReconnect if server hung.
571 */
572 usleep_range(1000, 2000);
573 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400574 continue;
Al Viro71335662016-01-09 19:54:50 -0500575 }
576
577 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500578 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400579 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500580 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400581 }
582 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400583 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400584}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400585
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400586int
587cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
588 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400589{
Al Viro71335662016-01-09 19:54:50 -0500590 struct msghdr smb_msg;
591 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
592 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400593
Al Viro71335662016-01-09 19:54:50 -0500594 return cifs_readv_from_socket(server, &smb_msg);
595}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400596
Al Viro71335662016-01-09 19:54:50 -0500597int
598cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700599 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500600{
601 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700602 struct bio_vec bv = {
603 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
Al Viro71335662016-01-09 19:54:50 -0500604 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
605 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400606}
607
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400608static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400609is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400610{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400611 /*
612 * The first byte big endian of the length field,
613 * is actually not part of the length but the type
614 * with the most common, zero, as regular data.
615 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400616 switch (type) {
617 case RFC1002_SESSION_MESSAGE:
618 /* Regular SMB response */
619 return true;
620 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500621 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400622 break;
623 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500624 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400625 break;
626 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400627 /*
628 * We get this from Windows 98 instead of an error on
629 * SMB negprot response.
630 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500631 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400632 /* give server a second to clean up */
633 msleep(1000);
634 /*
635 * Always try 445 first on reconnect since we get NACK
636 * on some if we ever connected to port 139 (the NACK
637 * is since we do not begin with RFC1001 session
638 * initialize frame).
639 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400640 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400641 cifs_reconnect(server);
642 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400643 break;
644 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500645 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400646 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400647 }
648
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400649 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400650}
651
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400652void
653dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400654{
655#ifdef CONFIG_CIFS_STATS2
656 mid->when_received = jiffies;
657#endif
658 spin_lock(&GlobalMid_Lock);
659 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400660 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400661 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400662 mid->mid_state = MID_RESPONSE_MALFORMED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400663 list_del_init(&mid->qhead);
664 spin_unlock(&GlobalMid_Lock);
665}
666
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400667static void
668handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400669 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400670{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400671 if (server->ops->check_trans2 &&
672 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400673 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400674 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400675 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400676 /* Was previous buf put in mpx struct for multi-rsp? */
677 if (!mid->multiRsp) {
678 /* smb buffer will be freed by user thread */
679 if (server->large_buf)
680 server->bigbuf = NULL;
681 else
682 server->smallbuf = NULL;
683 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400684 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400685}
686
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400687static void clean_demultiplex_info(struct TCP_Server_Info *server)
688{
689 int length;
690
691 /* take it off the list, if it's not already */
692 spin_lock(&cifs_tcp_ses_lock);
693 list_del_init(&server->tcp_ses_list);
694 spin_unlock(&cifs_tcp_ses_lock);
695
696 spin_lock(&GlobalMid_Lock);
697 server->tcpStatus = CifsExiting;
698 spin_unlock(&GlobalMid_Lock);
699 wake_up_all(&server->response_q);
700
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400701 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300702 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400703 if (server->credits <= 0)
704 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300705 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400706 /*
707 * Although there should not be any requests blocked on this queue it
708 * can not hurt to be paranoid and try to wake up requests that may
709 * haven been blocked when more than 50 at time were on the wire to the
710 * same server - they now will see the session is in exit state and get
711 * out of SendReceive.
712 */
713 wake_up_all(&server->request_q);
714 /* give those requests time to exit */
715 msleep(125);
Long Libce9ce72017-11-22 17:38:38 -0700716 if (cifs_rdma_enabled(server) && server->smbd_conn) {
717 smbd_destroy(server->smbd_conn);
718 server->smbd_conn = NULL;
719 }
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400720 if (server->ssocket) {
721 sock_release(server->ssocket);
722 server->ssocket = NULL;
723 }
724
725 if (!list_empty(&server->pending_mid_q)) {
726 struct list_head dispose_list;
727 struct mid_q_entry *mid_entry;
728 struct list_head *tmp, *tmp2;
729
730 INIT_LIST_HEAD(&dispose_list);
731 spin_lock(&GlobalMid_Lock);
732 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
733 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500734 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400735 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400736 list_move(&mid_entry->qhead, &dispose_list);
737 }
738 spin_unlock(&GlobalMid_Lock);
739
740 /* now walk dispose list and issue callbacks */
741 list_for_each_safe(tmp, tmp2, &dispose_list) {
742 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500743 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400744 list_del_init(&mid_entry->qhead);
745 mid_entry->callback(mid_entry);
746 }
747 /* 1/8th of sec is more than enough time for them to exit */
748 msleep(125);
749 }
750
751 if (!list_empty(&server->pending_mid_q)) {
752 /*
753 * mpx threads have not exited yet give them at least the smb
754 * send timeout time for long ops.
755 *
756 * Due to delays on oplock break requests, we need to wait at
757 * least 45 seconds before giving up on a request getting a
758 * response and going ahead and killing cifsd.
759 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500760 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400761 msleep(46000);
762 /*
763 * If threads still have not exited they are probably never
764 * coming home not much else we can do but free the memory.
765 */
766 }
767
768 kfree(server->hostname);
769 kfree(server);
770
771 length = atomic_dec_return(&tcpSesAllocCount);
772 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700773 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400774}
775
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400776static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400777standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
778{
779 int length;
780 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000781 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400782
783 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100784 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
785 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500786 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400787 cifs_reconnect(server);
788 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400789 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400790 }
791
792 /* switch to large buffer if too big for a small one */
793 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
794 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400795 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400796 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400797 }
798
799 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400800 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100801 pdu_length - HEADER_SIZE(server) + 1
802 + server->vals->header_preamble_size);
803
Jeff Laytone9097ab2011-10-19 15:29:40 -0400804 if (length < 0)
805 return length;
806 server->total_read += length;
807
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400808 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400809
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800810 return cifs_handle_standard(server, mid);
811}
812
813int
814cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
815{
816 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
817 int length;
818
Jeff Laytone9097ab2011-10-19 15:29:40 -0400819 /*
820 * We know that we received enough to get to the MID as we
821 * checked the pdu_length earlier. Now check to see
822 * if the rest of the header is OK. We borrow the length
823 * var for the rest of the loop to avoid a new stack var.
824 *
825 * 48 bytes is enough to display the header and a little bit
826 * into the payload for debugging purposes.
827 */
Steve French373512e2015-12-18 13:05:30 -0600828 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400829 if (length != 0)
830 cifs_dump_mem("Bad SMB: ", buf,
831 min_t(unsigned int, server->total_read, 48));
832
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700833 if (server->ops->is_session_expired &&
834 server->ops->is_session_expired(buf)) {
835 cifs_reconnect(server);
836 wake_up(&server->response_q);
837 return -1;
838 }
839
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700840 if (server->ops->is_status_pending &&
841 server->ops->is_status_pending(buf, server, length))
842 return -1;
843
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500844 if (!mid)
845 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400846
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400847 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500848 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400849}
850
851static int
Al Viro7c97c202011-06-21 08:51:28 -0400852cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 int length;
Al Viro7c97c202011-06-21 08:51:28 -0400855 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400856 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000857 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400858 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500859 struct task_struct *task_to_wake = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct mid_q_entry *mid_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500863 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400864
865 length = atomic_inc_return(&tcpSesAllocCount);
866 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700867 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700869 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000870 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700871 if (try_to_freeze())
872 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700873
Jeff Layton2a37ef92011-10-19 15:29:23 -0400874 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400875 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700876
Jeff Layton2a37ef92011-10-19 15:29:23 -0400877 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400878 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000879 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000880
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400881 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400882 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000883 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000884
885 if (server->vals->header_preamble_size == 0)
886 server->total_read = 0;
887 else
888 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700889
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400890 /*
891 * The right amount was read from socket - 4 bytes,
892 * so we can now interpret the length field.
893 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400894 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700895
Joe Perchesf96637b2013-05-04 22:12:25 -0500896 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400897 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000898 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000899next_pdu:
900 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700901
Jeff Layton89482a52011-10-19 15:28:57 -0400902 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000903 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100904 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500905 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000906 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400907 cifs_reconnect(server);
908 wake_up(&server->response_q);
909 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700910 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400911
Jeff Layton89482a52011-10-19 15:28:57 -0400912 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100913 length = cifs_read_from_socket(server,
914 buf + server->vals->header_preamble_size,
915 HEADER_SIZE(server) - 1
916 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400917 if (length < 0)
918 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400919 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400920
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000921 if (server->ops->next_header) {
922 next_offset = server->ops->next_header(buf);
923 if (next_offset)
924 server->pdu_size = next_offset;
925 }
926
Lars Persson696e4202018-06-25 14:05:25 +0200927 mid_entry = NULL;
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800928 if (server->ops->is_transform_hdr &&
929 server->ops->receive_transform &&
930 server->ops->is_transform_hdr(buf)) {
931 length = server->ops->receive_transform(server,
932 &mid_entry);
933 } else {
934 mid_entry = server->ops->find_mid(server, buf);
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400935
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800936 if (!mid_entry || !mid_entry->receive)
937 length = standard_receive3(server, mid_entry);
938 else
939 length = mid_entry->receive(server, mid_entry);
940 }
Jeff Layton44d22d82011-10-19 15:29:49 -0400941
Lars Persson696e4202018-06-25 14:05:25 +0200942 if (length < 0) {
943 if (mid_entry)
944 cifs_mid_q_entry_release(mid_entry);
Steve Frenche4eb2952005-04-28 22:41:09 -0700945 continue;
Lars Persson696e4202018-06-25 14:05:25 +0200946 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700947
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400948 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -0400949 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700950
Steve Frenchfda35942011-01-20 18:06:34 +0000951 server->lstrp = jiffies;
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500952 if (mid_entry != NULL) {
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000953 mid_entry->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800954 if ((mid_entry->mid_flags & MID_WAIT_CANCELLED) &&
955 mid_entry->mid_state == MID_RESPONSE_RECEIVED &&
956 server->ops->handle_cancelled_mid)
957 server->ops->handle_cancelled_mid(
958 mid_entry->resp_buf,
959 server);
960
Jeff Layton2a37ef92011-10-19 15:29:23 -0400961 if (!mid_entry->multiRsp || mid_entry->multiEnd)
962 mid_entry->callback(mid_entry);
Lars Persson696e4202018-06-25 14:05:25 +0200963
964 cifs_mid_q_entry_release(mid_entry);
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800965 } else if (server->ops->is_oplock_break &&
966 server->ops->is_oplock_break(buf, server)) {
967 cifs_dbg(FYI, "Received oplock break\n");
968 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500969 cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
970 atomic_read(&midCount));
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400971 cifs_dump_mem("Received Data is: ", buf,
972 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +0000973#ifdef CONFIG_CIFS_DEBUG2
Pavel Shilovsky7f0adb52012-05-28 15:50:10 +0400974 if (server->ops->dump_detail)
Steve French71992e622018-05-06 15:58:51 -0500975 server->ops->dump_detail(buf, server);
Steve French39798772006-05-31 22:40:51 +0000976 cifs_dump_mids(server);
977#endif /* CIFS_DEBUG2 */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000978 }
979 if (pdu_length > server->pdu_size) {
980 if (!allocate_buffers(server))
981 continue;
982 pdu_length -= server->pdu_size;
983 server->total_read = 0;
984 server->large_buf = false;
985 buf = server->smallbuf;
986 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -0700987 }
988 } /* end while !EXITING */
989
Justin P. Mattockfd62cb72011-02-24 22:15:02 -0800990 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -0400991 cifs_buf_release(server->bigbuf);
992 if (server->smallbuf) /* no sense logging a debug message if NULL */
993 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500995 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400996 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500997
998 /* if server->tsk was NULL then wait for a signal before exiting */
999 if (!task_to_wake) {
1000 set_current_state(TASK_INTERRUPTIBLE);
1001 while (!signal_pending(current)) {
1002 schedule();
1003 set_current_state(TASK_INTERRUPTIBLE);
1004 }
1005 set_current_state(TASK_RUNNING);
1006 }
1007
Jeff Layton0468a2c2008-12-01 07:09:35 -05001008 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Jeff Laytonc359cf32007-11-16 22:22:06 +00001011/* extract the host portion of the UNC string */
1012static char *
1013extract_hostname(const char *unc)
1014{
1015 const char *src;
1016 char *dst, *delim;
1017 unsigned int len;
1018
1019 /* skip double chars at beginning of string */
1020 /* BB: check validity of these bytes? */
1021 src = unc + 2;
1022
1023 /* delimiter between hostname and sharename is always '\\' now */
1024 delim = strchr(src, '\\');
1025 if (!delim)
1026 return ERR_PTR(-EINVAL);
1027
1028 len = delim - src;
1029 dst = kmalloc((len + 1), GFP_KERNEL);
1030 if (dst == NULL)
1031 return ERR_PTR(-ENOMEM);
1032
1033 memcpy(dst, src, len);
1034 dst[len] = '\0';
1035
1036 return dst;
1037}
1038
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001039static int get_option_ul(substring_t args[], unsigned long *option)
1040{
1041 int rc;
1042 char *string;
1043
1044 string = match_strdup(args);
1045 if (string == NULL)
1046 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001047 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001048 kfree(string);
1049
1050 return rc;
1051}
1052
Eric W. Biederman3da46562013-02-06 01:37:39 -08001053static int get_option_uid(substring_t args[], kuid_t *result)
1054{
1055 unsigned long value;
1056 kuid_t uid;
1057 int rc;
1058
1059 rc = get_option_ul(args, &value);
1060 if (rc)
1061 return rc;
1062
1063 uid = make_kuid(current_user_ns(), value);
1064 if (!uid_valid(uid))
1065 return -EINVAL;
1066
1067 *result = uid;
1068 return 0;
1069}
1070
1071static int get_option_gid(substring_t args[], kgid_t *result)
1072{
1073 unsigned long value;
1074 kgid_t gid;
1075 int rc;
1076
1077 rc = get_option_ul(args, &value);
1078 if (rc)
1079 return rc;
1080
1081 gid = make_kgid(current_user_ns(), value);
1082 if (!gid_valid(gid))
1083 return -EINVAL;
1084
1085 *result = gid;
1086 return 0;
1087}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001088
1089static int cifs_parse_security_flavors(char *value,
1090 struct smb_vol *vol)
1091{
1092
1093 substring_t args[MAX_OPT_ARGS];
1094
Jeff Layton1e3cc572013-06-10 17:12:23 -05001095 /*
1096 * With mount options, the last one should win. Reset any existing
1097 * settings back to default.
1098 */
1099 vol->sectype = Unspecified;
1100 vol->sign = false;
1101
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001102 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001103 case Opt_sec_krb5p:
1104 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1105 return 1;
1106 case Opt_sec_krb5i:
1107 vol->sign = true;
1108 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001109 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001110 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001111 break;
1112 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001113 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001114 /* Fallthrough */
1115 case Opt_sec_ntlmssp:
1116 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001117 break;
1118 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001119 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001120 /* Fallthrough */
1121 case Opt_ntlm:
1122 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001123 break;
1124 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001125 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001126 /* Fallthrough */
1127 case Opt_sec_ntlmv2:
1128 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001129 break;
1130#ifdef CONFIG_CIFS_WEAK_PW_HASH
1131 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001132 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001133 break;
1134#endif
1135 case Opt_sec_none:
1136 vol->nullauth = 1;
1137 break;
1138 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001139 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001140 return 1;
1141 }
1142
1143 return 0;
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001147cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1148{
1149 substring_t args[MAX_OPT_ARGS];
1150
1151 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1152 case Opt_cache_loose:
1153 vol->direct_io = false;
1154 vol->strict_io = false;
1155 break;
1156 case Opt_cache_strict:
1157 vol->direct_io = false;
1158 vol->strict_io = true;
1159 break;
1160 case Opt_cache_none:
1161 vol->direct_io = true;
1162 vol->strict_io = false;
1163 break;
1164 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001165 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001166 return 1;
1167 }
1168 return 0;
1169}
1170
1171static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001172cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001173{
1174 substring_t args[MAX_OPT_ARGS];
1175
1176 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001177#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001178 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001179 if (disable_legacy_dialects) {
1180 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1181 return 1;
1182 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001183 if (is_smb3) {
1184 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1185 return 1;
1186 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001187 vol->ops = &smb1_operations;
1188 vol->vals = &smb1_values;
1189 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001190 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001191 if (disable_legacy_dialects) {
1192 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1193 return 1;
1194 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001195 if (is_smb3) {
1196 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1197 return 1;
1198 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001199 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001200 vol->vals = &smb20_values;
1201 break;
Steve French74204512018-06-19 14:34:08 -05001202#else
1203 case Smb_1:
1204 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1205 return 1;
1206 case Smb_20:
1207 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1208 return 1;
1209#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001210 case Smb_21:
1211 vol->ops = &smb21_operations;
1212 vol->vals = &smb21_values;
1213 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001214 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001215 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001216 vol->vals = &smb30_values;
1217 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001218 case Smb_302:
1219 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1220 vol->vals = &smb302_values;
1221 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001222#ifdef CONFIG_CIFS_SMB311
1223 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001224 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001225 vol->vals = &smb311_values;
1226 break;
1227#endif /* SMB311 */
Steve French9764c022017-09-17 10:41:35 -05001228 case Smb_3any:
1229 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1230 vol->vals = &smb3any_values;
1231 break;
1232 case Smb_default:
1233 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1234 vol->vals = &smbdefault_values;
1235 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001236 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001237 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001238 return 1;
1239 }
1240 return 0;
1241}
1242
Jeff Laytond387a5c2012-12-10 06:10:46 -05001243/*
1244 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1245 * fields with the result. Returns 0 on success and an error otherwise.
1246 */
1247static int
1248cifs_parse_devname(const char *devname, struct smb_vol *vol)
1249{
1250 char *pos;
1251 const char *delims = "/\\";
1252 size_t len;
1253
1254 /* make sure we have a valid UNC double delimiter prefix */
1255 len = strspn(devname, delims);
1256 if (len != 2)
1257 return -EINVAL;
1258
1259 /* find delimiter between host and sharename */
1260 pos = strpbrk(devname + 2, delims);
1261 if (!pos)
1262 return -EINVAL;
1263
1264 /* skip past delimiter */
1265 ++pos;
1266
1267 /* now go until next delimiter or end of string */
1268 len = strcspn(pos, delims);
1269
1270 /* move "pos" up to delimiter or NULL */
1271 pos += len;
1272 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1273 if (!vol->UNC)
1274 return -ENOMEM;
1275
1276 convert_delimiter(vol->UNC, '\\');
1277
Sachin Prabhu11e31642016-02-08 13:44:01 +05301278 /* skip any delimiter */
1279 if (*pos == '/' || *pos == '\\')
1280 pos++;
1281
1282 /* If pos is NULL then no prepath */
1283 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001284 return 0;
1285
1286 vol->prepath = kstrdup(pos, GFP_KERNEL);
1287 if (!vol->prepath)
1288 return -ENOMEM;
1289
1290 return 0;
1291}
1292
Jeff Layton23db65f2012-05-15 12:20:51 -04001293static int
Sean Finneyb9468452011-04-11 13:19:32 +00001294cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001295 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001297 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001298 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 unsigned int temp_len, i, j;
1300 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001301 short int override_uid = -1;
1302 short int override_gid = -1;
1303 bool uid_specified = false;
1304 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001305 bool sloppy = false;
1306 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001307 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001308 char *string = NULL;
1309 char *tmp_end, *value;
1310 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001311 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001312 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001313 unsigned short port = 0;
1314 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001317 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001318 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Jeff Layton6ee95422012-11-26 11:09:57 -05001320 /* ensure we always start with zeroed-out smb_vol */
1321 memset(vol, 0, sizeof(*vol));
1322
Jeff Layton88463992010-11-22 15:31:03 -05001323 /*
1324 * does not have to be perfect mapping since field is
1325 * informational, only used for servers that do not support
1326 * port 445 and it can be overridden at mount time
1327 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001328 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1329 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001330 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1331
Jeff Layton1397f2e2011-01-07 11:30:28 -05001332 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001333 /* null target name indicates to use *SMBSERVR default called name
1334 if we end up sending RFC1001 session initialize */
1335 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001336 vol->cred_uid = current_uid();
1337 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001338 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001339
Steve French2baa2682014-09-27 02:19:01 -05001340 /*
1341 * default to SFM style remapping of seven reserved characters
1342 * unless user overrides it or we negotiate CIFS POSIX where
1343 * it is unnecessary. Can not simultaneously use more than one mapping
1344 * since then readdir could list files that open could not open
1345 */
1346 vol->remap = true;
1347
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001348 /* default to only allowing write access to owner of the mount */
1349 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001352 /* default is always to request posix paths. */
1353 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001354 /* default to using server inode numbers where available */
1355 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001356
Jeff Layton1b359202012-09-19 15:20:27 -07001357 /* default is to use strict cifs caching semantics */
1358 vol->strict_io = true;
1359
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301360 vol->actimeo = CIFS_DEF_ACTIMEO;
1361
Steve French9764c022017-09-17 10:41:35 -05001362 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1363 vol->ops = &smb30_operations;
1364 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001365
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001366 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1367
Sean Finneyb9468452011-04-11 13:19:32 +00001368 if (!mountdata)
1369 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Sean Finneyb9468452011-04-11 13:19:32 +00001371 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1372 if (!mountdata_copy)
1373 goto cifs_parse_mount_err;
1374
1375 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001376 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001377
Steve French50c2f752007-07-13 00:33:32 +00001378 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001379 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 separator[0] = options[4];
1381 options += 5;
1382 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001383 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001386 vol->backupuid_specified = false; /* no backup intent for a user */
1387 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001388
Jeff Layton37d4f992013-05-24 07:40:05 -04001389 switch (cifs_parse_devname(devname, vol)) {
1390 case 0:
1391 break;
1392 case -ENOMEM:
1393 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1394 goto cifs_parse_mount_err;
1395 case -EINVAL:
1396 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1397 goto cifs_parse_mount_err;
1398 default:
1399 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1400 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001401 }
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001404 substring_t args[MAX_OPT_ARGS];
1405 unsigned long option;
1406 int token;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (!*data)
1409 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001411 token = match_token(data, cifs_mount_option_tokens, args);
1412
1413 switch (token) {
1414
1415 /* Ingnore the following */
1416 case Opt_ignore:
1417 break;
1418
1419 /* Boolean values */
1420 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001422 break;
1423 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001425 break;
1426 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001427 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001428 break;
1429 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001430 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001431 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001432 case Opt_forcegid:
1433 override_gid = 1;
1434 break;
1435 case Opt_noforcegid:
1436 override_gid = 0;
1437 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001438 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001439 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001440 break;
1441 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001442 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001443 break;
1444 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001446 break;
1447 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001449 break;
1450 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001452 break;
1453 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001455 break;
1456 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001457 vol->sfu_remap = true;
1458 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001459 break;
1460 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001461 vol->sfu_remap = false;
1462 break;
1463 case Opt_mapposix:
1464 vol->remap = true;
1465 vol->sfu_remap = false; /* disable SFU mapping */
1466 break;
1467 case Opt_nomapposix:
1468 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001469 break;
1470 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001471 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001472 break;
1473 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001474 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001475 break;
1476 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001477 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001478 break;
1479 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001480 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001481 break;
1482 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001483 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001484 break;
1485 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001486 if (vol->linux_ext)
1487 cifs_dbg(VFS,
1488 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001489 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001490 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001491 case Opt_unix:
1492 if (vol->no_linux_ext)
1493 cifs_dbg(VFS,
1494 "conflicting unix mount options\n");
1495 vol->linux_ext = 1;
1496 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001497 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001498 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001499 break;
1500 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001501 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001502 break;
1503 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001504 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001505 /*
1506 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001507 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001508 * local vfs will do advisory
1509 */
Steve French50c2f752007-07-13 00:33:32 +00001510 if (vol->file_mode ==
1511 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001512 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001513 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001514 case Opt_nohandlecache:
1515 vol->nohandlecache = 1;
1516 break;
1517 case Opt_handlecache:
1518 vol->nohandlecache = 0;
1519 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001520 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001521 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001522 break;
1523 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001525 break;
1526 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001528 break;
Steve French95932652016-09-23 01:36:34 -05001529 case Opt_setuidfromacl:
1530 vol->setuidfromacl = 1;
1531 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001532 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001533 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001534 break;
1535 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001536 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001537 break;
1538 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001540 break;
1541 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001543 break;
1544 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001546 break;
1547 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001549 break;
1550 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001551 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001552 break;
1553 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001554 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001555 break;
1556 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001558 break;
1559 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001561 break;
1562 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001563 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001564 break;
1565 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001566 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001567 break;
1568 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001569 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001570 break;
1571 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001573 break;
1574 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001576 break;
1577 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001578 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001579 break;
1580 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001581 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001582 break;
1583 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001584 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001585 * is a per tree connection (mount) not a per socket
1586 * or per-smb connection option in the protocol
1587 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1588 */
Steve French95b1cb92008-05-15 16:44:38 +00001589 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001590 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001591 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001592 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001593 break;
1594 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301595#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001596 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001597 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301598#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301599 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001600 break;
1601 case Opt_mfsymlinks:
Stefan Metzmacher736a3322010-07-30 14:56:00 +02001602 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001603 break;
1604 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001605 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001606 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001607 case Opt_sloppy:
1608 sloppy = true;
1609 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001610 case Opt_nosharesock:
1611 vol->nosharesock = true;
1612 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001613 case Opt_nopersistent:
1614 vol->nopersistent = true;
1615 if (vol->persistent) {
1616 cifs_dbg(VFS,
1617 "persistenthandles mount options conflict\n");
1618 goto cifs_parse_mount_err;
1619 }
1620 break;
1621 case Opt_persistent:
1622 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001623 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001624 cifs_dbg(VFS,
1625 "persistenthandles mount options conflict\n");
1626 goto cifs_parse_mount_err;
1627 }
1628 break;
Steve French592fafe2015-11-03 10:08:53 -06001629 case Opt_resilient:
1630 vol->resilient = true;
1631 if (vol->persistent) {
1632 cifs_dbg(VFS,
1633 "persistenthandles mount options conflict\n");
1634 goto cifs_parse_mount_err;
1635 }
1636 break;
1637 case Opt_noresilient:
1638 vol->resilient = false; /* already the default */
1639 break;
Germano Percossi39566442016-12-15 12:31:18 +05301640 case Opt_domainauto:
1641 vol->domainauto = true;
1642 break;
Long Li8339dd32017-11-07 01:54:55 -07001643 case Opt_rdma:
1644 vol->rdma = true;
1645 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001646
1647 /* Numeric Values */
1648 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001649 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001650 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1651 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001652 goto cifs_parse_mount_err;
1653 }
1654 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001655 break;
1656 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001657 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001658 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1659 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001660 goto cifs_parse_mount_err;
1661 }
1662 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001663 break;
1664 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001665 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001666 cifs_dbg(VFS, "%s: Invalid uid value\n",
1667 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001668 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001670 uid_specified = true;
1671 break;
1672 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001673 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001674 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1675 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001676 goto cifs_parse_mount_err;
1677 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001678 break;
1679 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001680 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001681 cifs_dbg(VFS, "%s: Invalid gid value\n",
1682 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001683 goto cifs_parse_mount_err;
1684 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001685 gid_specified = true;
1686 break;
1687 case Opt_file_mode:
1688 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001689 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1690 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001691 goto cifs_parse_mount_err;
1692 }
1693 vol->file_mode = option;
1694 break;
1695 case Opt_dirmode:
1696 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001697 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1698 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001699 goto cifs_parse_mount_err;
1700 }
1701 vol->dir_mode = option;
1702 break;
1703 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001704 if (get_option_ul(args, &option) ||
1705 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001706 cifs_dbg(VFS, "%s: Invalid port value\n",
1707 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001708 goto cifs_parse_mount_err;
1709 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001710 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001711 break;
1712 case Opt_rsize:
1713 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001714 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1715 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001716 goto cifs_parse_mount_err;
1717 }
1718 vol->rsize = option;
1719 break;
1720 case Opt_wsize:
1721 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001722 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1723 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001724 goto cifs_parse_mount_err;
1725 }
1726 vol->wsize = option;
1727 break;
1728 case Opt_actimeo:
1729 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001730 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1731 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001732 goto cifs_parse_mount_err;
1733 }
1734 vol->actimeo = HZ * option;
1735 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001736 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001737 goto cifs_parse_mount_err;
1738 }
1739 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001740 case Opt_echo_interval:
1741 if (get_option_ul(args, &option)) {
1742 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1743 __func__);
1744 goto cifs_parse_mount_err;
1745 }
1746 vol->echo_interval = option;
1747 break;
Steve French8b217fe2016-11-11 22:36:20 -06001748 case Opt_snapshot:
1749 if (get_option_ul(args, &option)) {
1750 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1751 __func__);
1752 goto cifs_parse_mount_err;
1753 }
1754 vol->snapshot_time = option;
1755 break;
Steve French141891f2016-09-23 00:44:16 -05001756 case Opt_max_credits:
1757 if (get_option_ul(args, &option) || (option < 20) ||
1758 (option > 60000)) {
1759 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1760 __func__);
1761 goto cifs_parse_mount_err;
1762 }
1763 vol->max_credits = option;
1764 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001765
1766 /* String Arguments */
1767
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001768 case Opt_blank_user:
1769 /* null user, ie. anonymous authentication */
1770 vol->nullauth = 1;
1771 vol->username = NULL;
1772 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001773 case Opt_user:
1774 string = match_strdup(args);
1775 if (string == NULL)
1776 goto out_nomem;
1777
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001778 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1779 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001780 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001781 goto cifs_parse_mount_err;
1782 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001783
1784 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001785 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001786 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001787 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001788 break;
1789 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001790 /* passwords have to be handled differently
1791 * to allow the character used for deliminator
1792 * to be passed within them
1793 */
1794
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001795 /*
1796 * Check if this is a case where the password
1797 * starts with a delimiter
1798 */
1799 tmp_end = strchr(data, '=');
1800 tmp_end++;
1801 if (!(tmp_end < end && tmp_end[1] == delim)) {
1802 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001803 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001804 vol->password = NULL;
1805 break;
1806 }
1807 /* Yes it is. Drop down to Opt_pass below.*/
1808 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001809 /* Obtain the value string */
1810 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001811 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001812
1813 /* Set tmp_end to end of the string */
1814 tmp_end = (char *) value + strlen(value);
1815
1816 /* Check if following character is the deliminator
1817 * If yes, we have encountered a double deliminator
1818 * reset the NULL character to the deliminator
1819 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301820 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001821 tmp_end[0] = delim;
1822
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301823 /* Keep iterating until we get to a single
1824 * deliminator OR the end
1825 */
1826 while ((tmp_end = strchr(tmp_end, delim))
1827 != NULL && (tmp_end[1] == delim)) {
1828 tmp_end = (char *) &tmp_end[2];
1829 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001830
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301831 /* Reset var options to point to next element */
1832 if (tmp_end) {
1833 tmp_end[0] = '\0';
1834 options = (char *) &tmp_end[1];
1835 } else
1836 /* Reached the end of the mount option
1837 * string */
1838 options = end;
1839 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001840
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001841 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001842 /* Now build new password string */
1843 temp_len = strlen(value);
1844 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1845 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001846 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001847 goto cifs_parse_mount_err;
1848 }
1849
1850 for (i = 0, j = 0; i < temp_len; i++, j++) {
1851 vol->password[j] = value[i];
1852 if ((value[i] == delim) &&
1853 value[i+1] == delim)
1854 /* skip the second deliminator */
1855 i++;
1856 }
1857 vol->password[j] = '\0';
1858 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001859 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001860 /* FIXME: should this be an error instead? */
1861 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001862 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001863 case Opt_ip:
1864 string = match_strdup(args);
1865 if (string == NULL)
1866 goto out_nomem;
1867
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001868 if (!cifs_convert_address(dstaddr, string,
1869 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001870 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001871 goto cifs_parse_mount_err;
1872 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001873 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001874 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001875 case Opt_domain:
1876 string = match_strdup(args);
1877 if (string == NULL)
1878 goto out_nomem;
1879
Chen Gang057d6332013-07-19 09:01:36 +08001880 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1881 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001882 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001883 goto cifs_parse_mount_err;
1884 }
1885
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001886 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001887 vol->domainname = kstrdup(string, GFP_KERNEL);
1888 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001889 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001890 goto cifs_parse_mount_err;
1891 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001892 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001893 break;
1894 case Opt_srcaddr:
1895 string = match_strdup(args);
1896 if (string == NULL)
1897 goto out_nomem;
1898
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001899 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001900 (struct sockaddr *)&vol->srcaddr,
1901 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001902 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1903 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001904 goto cifs_parse_mount_err;
1905 }
1906 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001907 case Opt_iocharset:
1908 string = match_strdup(args);
1909 if (string == NULL)
1910 goto out_nomem;
1911
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001912 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001913 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001914 goto cifs_parse_mount_err;
1915 }
1916
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001917 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001918 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001919 vol->iocharset = kstrdup(string,
1920 GFP_KERNEL);
1921 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001922 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001923 goto cifs_parse_mount_err;
1924 }
1925 }
1926 /* if iocharset not set then load_nls_default
1927 * is used by caller
1928 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001929 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001930 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001931 case Opt_netbiosname:
1932 string = match_strdup(args);
1933 if (string == NULL)
1934 goto out_nomem;
1935
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001936 memset(vol->source_rfc1001_name, 0x20,
1937 RFC1001_NAME_LEN);
1938 /*
1939 * FIXME: are there cases in which a comma can
1940 * be valid in workstation netbios name (and
1941 * need special handling)?
1942 */
1943 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1944 /* don't ucase netbiosname for user */
1945 if (string[i] == 0)
1946 break;
1947 vol->source_rfc1001_name[i] = string[i];
1948 }
1949 /* The string has 16th byte zero still from
1950 * set at top of the function
1951 */
1952 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001953 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001954 break;
1955 case Opt_servern:
1956 /* servernetbiosname specified override *SMBSERVER */
1957 string = match_strdup(args);
1958 if (string == NULL)
1959 goto out_nomem;
1960
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001961 /* last byte, type, is 0x20 for servr type */
1962 memset(vol->target_rfc1001_name, 0x20,
1963 RFC1001_NAME_LEN_WITH_NULL);
1964
1965 /* BB are there cases in which a comma can be
1966 valid in this workstation netbios name
1967 (and need special handling)? */
1968
1969 /* user or mount helper must uppercase the
1970 netbios name */
1971 for (i = 0; i < 15; i++) {
1972 if (string[i] == 0)
1973 break;
1974 vol->target_rfc1001_name[i] = string[i];
1975 }
1976 /* The string has 16th byte zero still from
1977 set at top of the function */
1978 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001979 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001980 break;
1981 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05001982 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001983 string = match_strdup(args);
1984 if (string == NULL)
1985 goto out_nomem;
1986
Steve French7e682f72017-08-31 21:34:24 -05001987 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001988 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05001989 if (strlen(string) > 1) {
1990 pr_warn("Bad mount helper ver=%s. Did "
1991 "you want SMB1 (CIFS) dialect "
1992 "and mean to type vers=1.0 "
1993 "instead?\n", string);
1994 goto cifs_parse_mount_err;
1995 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001996 /* This is the default */
1997 break;
1998 }
1999 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002000 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002001 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002002 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002003 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002004 string = match_strdup(args);
2005 if (string == NULL)
2006 goto out_nomem;
2007
Steve Frenchc7c137b2018-06-06 17:59:29 -05002008 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002009 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002010 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002011 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002012 case Opt_sec:
2013 string = match_strdup(args);
2014 if (string == NULL)
2015 goto out_nomem;
2016
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002017 if (cifs_parse_security_flavors(string, vol) != 0)
2018 goto cifs_parse_mount_err;
2019 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002020 case Opt_cache:
2021 string = match_strdup(args);
2022 if (string == NULL)
2023 goto out_nomem;
2024
2025 if (cifs_parse_cache_flavor(string, vol) != 0)
2026 goto cifs_parse_mount_err;
2027 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002028 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002029 /*
2030 * An option we don't recognize. Save it off for later
2031 * if we haven't already found one
2032 */
2033 if (!invalid)
2034 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002037 /* Free up any allocated string */
2038 kfree(string);
2039 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002041
Jeff Laytond8162552012-03-23 14:40:56 -04002042 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002043 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002044 goto cifs_parse_mount_err;
2045 }
2046
Long Li8339dd32017-11-07 01:54:55 -07002047 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2048 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2049 goto cifs_parse_mount_err;
2050 }
2051
Jeff Layton8a8798a2012-01-17 16:09:15 -05002052#ifndef CONFIG_KEYS
2053 /* Muliuser mounts require CONFIG_KEYS support */
2054 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002055 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002056 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002057 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002058#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002059 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002060 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002061 goto cifs_parse_mount_err;
2062 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002063
Jeff Layton62a1a432012-12-10 06:10:45 -05002064 /* make sure UNC has a share name */
2065 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002066 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002067 goto cifs_parse_mount_err;
2068 }
2069
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002070 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002071 int len;
2072 const char *slash;
2073
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002074 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002075 /* Use the address part of the UNC. */
2076 slash = strchr(&vol->UNC[2], '\\');
2077 len = slash - &vol->UNC[2];
2078 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002079 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002080 goto cifs_parse_mount_err;
2081 }
2082 }
2083
2084 /* set the port that we got earlier */
2085 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002087 if (uid_specified)
2088 vol->override_uid = override_uid;
2089 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002090 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002091
2092 if (gid_specified)
2093 vol->override_gid = override_gid;
2094 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002095 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002096
Steve French7e682f72017-08-31 21:34:24 -05002097 if (got_version == false)
2098 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002099 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002100 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002101 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2102 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002103
Sean Finneyb9468452011-04-11 13:19:32 +00002104 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002106
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002107out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002108 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002109cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002110 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002111 kfree(mountdata_copy);
2112 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Ben Greear3eb9a882010-09-01 17:06:02 -07002115/** Returns true if srcaddr isn't specified and rhs isn't
2116 * specified, or if srcaddr is specified and
2117 * matches the IP address of the rhs argument.
2118 */
Jeff Layton45151482010-07-06 20:43:02 -04002119static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002120srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2121{
2122 switch (srcaddr->sa_family) {
2123 case AF_UNSPEC:
2124 return (rhs->sa_family == AF_UNSPEC);
2125 case AF_INET: {
2126 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2127 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2128 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2129 }
2130 case AF_INET6: {
2131 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002132 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002133 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2134 }
2135 default:
2136 WARN_ON(1);
2137 return false; /* don't expect to be here */
2138 }
2139}
2140
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002141/*
2142 * If no port is specified in addr structure, we try to match with 445 port
2143 * and if it fails - with 139 ports. It should be called only if address
2144 * families of server and addr are equal.
2145 */
2146static bool
2147match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2148{
Steve French6da97912011-03-13 18:55:55 +00002149 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002150
2151 switch (addr->sa_family) {
2152 case AF_INET:
2153 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2154 port = ((struct sockaddr_in *) addr)->sin_port;
2155 break;
2156 case AF_INET6:
2157 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2158 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2159 break;
2160 default:
2161 WARN_ON(1);
2162 return false;
2163 }
2164
2165 if (!port) {
2166 port = htons(CIFS_PORT);
2167 if (port == *sport)
2168 return true;
2169
2170 port = htons(RFC1001_PORT);
2171 }
2172
2173 return port == *sport;
2174}
Ben Greear3eb9a882010-09-01 17:06:02 -07002175
2176static bool
2177match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2178 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Jeff Layton45151482010-07-06 20:43:02 -04002180 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002181 case AF_INET: {
2182 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2183 struct sockaddr_in *srv_addr4 =
2184 (struct sockaddr_in *)&server->dstaddr;
2185
2186 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002187 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002188 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002189 }
2190 case AF_INET6: {
2191 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2192 struct sockaddr_in6 *srv_addr6 =
2193 (struct sockaddr_in6 *)&server->dstaddr;
2194
Jeff Layton45151482010-07-06 20:43:02 -04002195 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002196 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002197 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002198 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002199 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002200 break;
2201 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002202 default:
2203 WARN_ON(1);
2204 return false; /* don't expect to be here */
2205 }
Jeff Layton45151482010-07-06 20:43:02 -04002206
Ben Greear3eb9a882010-09-01 17:06:02 -07002207 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2208 return false;
2209
Jeff Layton45151482010-07-06 20:43:02 -04002210 return true;
2211}
2212
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002213static bool
2214match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2215{
Jeff Layton3f618222013-06-12 19:52:14 -05002216 /*
2217 * The select_sectype function should either return the vol->sectype
2218 * that was specified, or "Unspecified" if that sectype was not
2219 * compatible with the given NEGOTIATE request.
2220 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302221 if (server->ops->select_sectype(server, vol->sectype)
2222 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002223 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002224
Jeff Layton3f618222013-06-12 19:52:14 -05002225 /*
2226 * Now check if signing mode is acceptable. No need to check
2227 * global_secflags at this point since if MUST_SIGN is set then
2228 * the server->sign had better be too.
2229 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002230 if (vol->sign && !server->sign)
2231 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002232
2233 return true;
2234}
2235
Jeff Layton9fa114f2012-11-26 11:09:57 -05002236static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002237{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002238 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2239
Jeff Laytona0b3df52013-05-24 07:40:59 -04002240 if (vol->nosharesock)
2241 return 0;
2242
Steve French9764c022017-09-17 10:41:35 -05002243 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002244 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2245 return 0;
2246
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002247 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2248 return 0;
2249
2250 if (!match_address(server, addr,
2251 (struct sockaddr *)&vol->srcaddr))
2252 return 0;
2253
2254 if (!match_port(server, addr))
2255 return 0;
2256
2257 if (!match_security(server, vol))
2258 return 0;
2259
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002260 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002261 return 0;
2262
Long Li8339dd32017-11-07 01:54:55 -07002263 if (server->rdma != vol->rdma)
2264 return 0;
2265
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002266 return 1;
2267}
2268
Jeff Layton45151482010-07-06 20:43:02 -04002269static struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002270cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002271{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002272 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302274 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002275 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002276 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002277 continue;
2278
Jeff Laytone7ddee92008-11-14 13:44:38 -05002279 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302280 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002281 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002282 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302284 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return NULL;
2286}
2287
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002288void
2289cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002291 struct task_struct *task;
2292
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302293 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002294 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302295 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002296 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002298
Rob Landleyf1d0c992011-01-22 15:44:05 -06002299 put_net(cifs_net_ns(server));
2300
Jeff Laytone7ddee92008-11-14 13:44:38 -05002301 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302302 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002303
Jeff Laytonc74093b2011-01-11 07:24:23 -05002304 cancel_delayed_work_sync(&server->echo);
2305
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002306 if (from_reconnect)
2307 /*
2308 * Avoid deadlock here: reconnect work calls
2309 * cifs_put_tcp_session() at its end. Need to be sure
2310 * that reconnect work does nothing with server pointer after
2311 * that step.
2312 */
2313 cancel_delayed_work(&server->reconnect);
2314 else
2315 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002316
Jeff Laytone7ddee92008-11-14 13:44:38 -05002317 spin_lock(&GlobalMid_Lock);
2318 server->tcpStatus = CifsExiting;
2319 spin_unlock(&GlobalMid_Lock);
2320
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002321 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302322 cifs_fscache_release_client_cookie(server);
2323
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002324 kfree(server->session_key.response);
2325 server->session_key.response = NULL;
2326 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002327
2328 task = xchg(&server->tsk, NULL);
2329 if (task)
2330 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Jeff Layton63c038c2008-12-01 18:41:46 -05002333static struct TCP_Server_Info *
2334cifs_get_tcp_session(struct smb_vol *volume_info)
2335{
2336 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002337 int rc;
2338
Joe Perchesf96637b2013-05-04 22:12:25 -05002339 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002340
2341 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002342 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002343 if (tcp_ses)
2344 return tcp_ses;
2345
2346 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2347 if (!tcp_ses) {
2348 rc = -ENOMEM;
2349 goto out_err;
2350 }
2351
Jeff Layton23db65f2012-05-15 12:20:51 -04002352 tcp_ses->ops = volume_info->ops;
2353 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002354 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002355 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2356 if (IS_ERR(tcp_ses->hostname)) {
2357 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002358 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002359 }
2360
2361 tcp_ses->noblocksnd = volume_info->noblocksnd;
2362 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002363 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002364 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002365 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002366 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002367 init_waitqueue_head(&tcp_ses->response_q);
2368 init_waitqueue_head(&tcp_ses->request_q);
2369 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2370 mutex_init(&tcp_ses->srv_mutex);
2371 memcpy(tcp_ses->workstation_RFC1001_name,
2372 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2373 memcpy(tcp_ses->server_RFC1001_name,
2374 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002375 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002376 tcp_ses->sequence_number = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002377 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002378 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002379 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2380 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002381 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002382 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2383 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002384 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2385 sizeof(tcp_ses->srcaddr));
2386 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2387 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002388 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002389 /*
2390 * at this point we are the only ones with the pointer
2391 * to the struct since the kernel thread not created yet
2392 * no need to spinlock this init of tcpStatus or srv_count
2393 */
2394 tcp_ses->tcpStatus = CifsNew;
2395 ++tcp_ses->srv_count;
2396
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002397 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2398 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2399 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2400 else
2401 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002402 if (tcp_ses->rdma) {
2403#ifndef CONFIG_CIFS_SMB_DIRECT
2404 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2405 rc = -ENOENT;
2406 goto out_err_crypto_release;
2407#endif
2408 tcp_ses->smbd_conn = smbd_get_connection(
2409 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2410 if (tcp_ses->smbd_conn) {
2411 cifs_dbg(VFS, "RDMA transport established\n");
2412 rc = 0;
2413 goto smbd_connected;
2414 } else {
2415 rc = -ENOENT;
2416 goto out_err_crypto_release;
2417 }
2418 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002419 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002420 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002421 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002422 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002423 }
Long Li2f894642017-11-22 17:38:34 -07002424smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002425 /*
2426 * since we're in a cifs function already, we know that
2427 * this will succeed. No need for try_module_get().
2428 */
2429 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002430 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002431 tcp_ses, "cifsd");
2432 if (IS_ERR(tcp_ses->tsk)) {
2433 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002434 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002435 module_put(THIS_MODULE);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002436 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002437 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002438 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002439
2440 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302441 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002442 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302443 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002444
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302445 cifs_fscache_get_client_cookie(tcp_ses);
2446
Jeff Laytonc74093b2011-01-11 07:24:23 -05002447 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002448 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002449
Jeff Layton63c038c2008-12-01 18:41:46 -05002450 return tcp_ses;
2451
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002452out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002453 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002454
Rob Landleyf1d0c992011-01-22 15:44:05 -06002455 put_net(cifs_net_ns(tcp_ses));
2456
Jeff Layton63c038c2008-12-01 18:41:46 -05002457out_err:
2458 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002459 if (!IS_ERR(tcp_ses->hostname))
2460 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002461 if (tcp_ses->ssocket)
2462 sock_release(tcp_ses->ssocket);
2463 kfree(tcp_ses);
2464 }
2465 return ERR_PTR(rc);
2466}
2467
Steve French96daf2b2011-05-27 04:34:02 +00002468static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002469{
Jeff Layton3f618222013-06-12 19:52:14 -05002470 if (vol->sectype != Unspecified &&
2471 vol->sectype != ses->sectype)
2472 return 0;
2473
2474 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002475 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002476 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002477 return 0;
2478 break;
2479 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002480 /* NULL username means anonymous session */
2481 if (ses->user_name == NULL) {
2482 if (!vol->nullauth)
2483 return 0;
2484 break;
2485 }
2486
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002487 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002488 if (strncmp(ses->user_name,
2489 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002490 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002491 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002492 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002493 ses->password != NULL &&
2494 strncmp(ses->password,
2495 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002496 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002497 return 0;
2498 }
2499 return 1;
2500}
2501
Aurelien Aptelb327a712018-01-24 13:46:10 +01002502/**
2503 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2504 *
2505 * A new IPC connection is made and stored in the session
2506 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2507 */
2508static int
2509cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2510{
2511 int rc = 0, xid;
2512 struct cifs_tcon *tcon;
2513 struct nls_table *nls_codepage;
2514 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2515 bool seal = false;
2516
2517 /*
2518 * If the mount request that resulted in the creation of the
2519 * session requires encryption, force IPC to be encrypted too.
2520 */
2521 if (volume_info->seal) {
2522 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2523 seal = true;
2524 else {
2525 cifs_dbg(VFS,
2526 "IPC: server doesn't support encryption\n");
2527 return -EOPNOTSUPP;
2528 }
2529 }
2530
2531 tcon = tconInfoAlloc();
2532 if (tcon == NULL)
2533 return -ENOMEM;
2534
2535 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->serverName);
2536
2537 /* cannot fail */
2538 nls_codepage = load_nls_default();
2539
2540 xid = get_xid();
2541 tcon->ses = ses;
2542 tcon->ipc = true;
2543 tcon->seal = seal;
2544 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2545 free_xid(xid);
2546
2547 if (rc) {
2548 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2549 tconInfoFree(tcon);
2550 goto out;
2551 }
2552
2553 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2554
2555 ses->tcon_ipc = tcon;
2556out:
2557 unload_nls(nls_codepage);
2558 return rc;
2559}
2560
2561/**
2562 * cifs_free_ipc - helper to release the session IPC tcon
2563 *
2564 * Needs to be called everytime a session is destroyed
2565 */
2566static int
2567cifs_free_ipc(struct cifs_ses *ses)
2568{
2569 int rc = 0, xid;
2570 struct cifs_tcon *tcon = ses->tcon_ipc;
2571
2572 if (tcon == NULL)
2573 return 0;
2574
2575 if (ses->server->ops->tree_disconnect) {
2576 xid = get_xid();
2577 rc = ses->server->ops->tree_disconnect(xid, tcon);
2578 free_xid(xid);
2579 }
2580
2581 if (rc)
2582 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2583
2584 tconInfoFree(tcon);
2585 ses->tcon_ipc = NULL;
2586 return rc;
2587}
2588
Steve French96daf2b2011-05-27 04:34:02 +00002589static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002590cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Steve French96daf2b2011-05-27 04:34:02 +00002592 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302594 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002595 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002596 if (ses->status == CifsExiting)
2597 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002598 if (!match_session(ses, vol))
2599 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002600 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302601 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002602 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302604 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return NULL;
2606}
2607
Jeff Layton14fbf502008-11-14 13:53:46 -05002608static void
Steve French96daf2b2011-05-27 04:34:02 +00002609cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002610{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002611 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002612 struct TCP_Server_Info *server = ses->server;
2613
Joe Perchesf96637b2013-05-04 22:12:25 -05002614 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002615
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302616 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002617 if (ses->status == CifsExiting) {
2618 spin_unlock(&cifs_tcp_ses_lock);
2619 return;
2620 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002621 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302622 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002623 return;
2624 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002625 if (ses->status == CifsGood)
2626 ses->status = CifsExiting;
2627 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002628
Aurelien Aptelb327a712018-01-24 13:46:10 +01002629 cifs_free_ipc(ses);
2630
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002631 if (ses->status == CifsExiting && server->ops->logoff) {
2632 xid = get_xid();
2633 rc = server->ops->logoff(xid, ses);
2634 if (rc)
2635 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2636 __func__, rc);
2637 _free_xid(xid);
2638 }
2639
2640 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002641 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302642 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002643
Jeff Layton14fbf502008-11-14 13:53:46 -05002644 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002645 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002646}
2647
Jeff Layton8a8798a2012-01-17 16:09:15 -05002648#ifdef CONFIG_KEYS
2649
Chen Gang057d6332013-07-19 09:01:36 +08002650/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2651#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002652
2653/* Populate username and pw fields from keyring if possible */
2654static int
2655cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2656{
2657 int rc = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002658 const char *delim, *payload;
2659 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002660 ssize_t len;
2661 struct key *key;
2662 struct TCP_Server_Info *server = ses->server;
2663 struct sockaddr_in *sa;
2664 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002665 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002666
2667 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2668 if (!desc)
2669 return -ENOMEM;
2670
2671 /* try to find an address key first */
2672 switch (server->dstaddr.ss_family) {
2673 case AF_INET:
2674 sa = (struct sockaddr_in *)&server->dstaddr;
2675 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2676 break;
2677 case AF_INET6:
2678 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2679 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2680 break;
2681 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002682 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2683 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002684 rc = -EINVAL;
2685 goto out_err;
2686 }
2687
Joe Perchesf96637b2013-05-04 22:12:25 -05002688 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002689 key = request_key(&key_type_logon, desc, "");
2690 if (IS_ERR(key)) {
2691 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002692 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002693 rc = PTR_ERR(key);
2694 goto out_err;
2695 }
2696
2697 /* didn't work, try to find a domain key */
2698 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002699 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002700 key = request_key(&key_type_logon, desc, "");
2701 if (IS_ERR(key)) {
2702 rc = PTR_ERR(key);
2703 goto out_err;
2704 }
2705 }
2706
2707 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002708 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002709 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002710 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002711 goto out_key_put;
2712 }
2713
2714 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002715 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002716 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002717 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002718 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002719 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2720 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002721 rc = -EINVAL;
2722 goto out_key_put;
2723 }
2724
2725 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002726 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002727 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2728 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002729 rc = -EINVAL;
2730 goto out_key_put;
2731 }
2732
2733 vol->username = kstrndup(payload, len, GFP_KERNEL);
2734 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002735 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2736 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002737 rc = -ENOMEM;
2738 goto out_key_put;
2739 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002740 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002741
2742 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002743 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002744 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002745 rc = -EINVAL;
2746 kfree(vol->username);
2747 vol->username = NULL;
2748 goto out_key_put;
2749 }
2750
2751 ++delim;
2752 vol->password = kstrndup(delim, len, GFP_KERNEL);
2753 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002754 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2755 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002756 rc = -ENOMEM;
2757 kfree(vol->username);
2758 vol->username = NULL;
2759 goto out_key_put;
2760 }
2761
2762out_key_put:
2763 up_read(&key->sem);
2764 key_put(key);
2765out_err:
2766 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002767 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002768 return rc;
2769}
2770#else /* ! CONFIG_KEYS */
2771static inline int
2772cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2773 struct cifs_ses *ses __attribute__((unused)))
2774{
2775 return -ENOSYS;
2776}
2777#endif /* CONFIG_KEYS */
2778
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002779/**
2780 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2781 *
2782 * This function assumes it is being called from cifs_mount() where we
2783 * already got a server reference (server refcount +1). See
2784 * cifs_get_tcon() for refcount explanations.
2785 */
Steve French96daf2b2011-05-27 04:34:02 +00002786static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002787cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2788{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002789 int rc = -ENOMEM;
2790 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002791 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002792 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2793 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002794
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002795 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002796
Jeff Layton4ff67b72010-07-06 20:43:02 -04002797 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002798 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002799 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2800 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002801
Jeff Layton36988c72010-04-24 07:57:43 -04002802 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002803 rc = cifs_negotiate_protocol(xid, ses);
2804 if (rc) {
2805 mutex_unlock(&ses->session_mutex);
2806 /* problem -- put our ses reference */
2807 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002808 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002809 return ERR_PTR(rc);
2810 }
Jeff Layton36988c72010-04-24 07:57:43 -04002811 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002812 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002813 rc = cifs_setup_session(xid, ses,
2814 volume_info->local_nls);
2815 if (rc) {
2816 mutex_unlock(&ses->session_mutex);
2817 /* problem -- put our reference */
2818 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002819 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002820 return ERR_PTR(rc);
2821 }
2822 }
2823 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002824
2825 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002826 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002827 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002828 return ses;
2829 }
2830
Joe Perchesf96637b2013-05-04 22:12:25 -05002831 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002832 ses = sesInfoAlloc();
2833 if (ses == NULL)
2834 goto get_ses_fail;
2835
2836 /* new SMB session uses our server ref */
2837 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002838 if (server->dstaddr.ss_family == AF_INET6)
2839 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002840 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002841 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002842
Steve French8727c8a2011-02-25 01:11:56 -06002843 if (volume_info->username) {
2844 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2845 if (!ses->user_name)
2846 goto get_ses_fail;
2847 }
Jeff Layton36988c72010-04-24 07:57:43 -04002848
2849 /* volume_info->password freed at unmount */
2850 if (volume_info->password) {
2851 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2852 if (!ses->password)
2853 goto get_ses_fail;
2854 }
2855 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002856 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2857 if (!ses->domainName)
2858 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002859 }
Germano Percossi39566442016-12-15 12:31:18 +05302860 if (volume_info->domainauto)
2861 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002862 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002863 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002864
Jeff Layton28e11bd2013-05-26 07:01:00 -04002865 ses->sectype = volume_info->sectype;
2866 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002867
2868 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002869 rc = cifs_negotiate_protocol(xid, ses);
2870 if (!rc)
2871 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002872 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002873 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002874 goto get_ses_fail;
2875
2876 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302877 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002878 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302879 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002880
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002881 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002882
2883 cifs_setup_ipc(ses, volume_info);
2884
Jeff Layton36988c72010-04-24 07:57:43 -04002885 return ses;
2886
2887get_ses_fail:
2888 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002889 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002890 return ERR_PTR(rc);
2891}
2892
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002893static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002894{
2895 if (tcon->tidStatus == CifsExiting)
2896 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002897 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002898 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002899 if (tcon->seal != volume_info->seal)
2900 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002901 if (tcon->snapshot_time != volume_info->snapshot_time)
2902 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002903 return 1;
2904}
2905
Steve French96daf2b2011-05-27 04:34:02 +00002906static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06002907cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
2909 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002910 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302912 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002913 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002914 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002915 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002916 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002917 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302918 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return tcon;
2920 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302921 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return NULL;
2923}
2924
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002925void
Steve French96daf2b2011-05-27 04:34:02 +00002926cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002927{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002928 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002929 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002930
Aurelien Aptelb327a712018-01-24 13:46:10 +01002931 /*
2932 * IPC tcon share the lifetime of their session and are
2933 * destroyed in the session put function
2934 */
2935 if (tcon == NULL || tcon->ipc)
2936 return;
2937
2938 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002939 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302940 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002941 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302942 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002943 return;
2944 }
2945
2946 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302947 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002948
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002949 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002950 if (ses->server->ops->tree_disconnect)
2951 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002952 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002953
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302954 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002955 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002956 cifs_put_smb_ses(ses);
2957}
2958
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002959/**
2960 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
2961 *
2962 * - tcon refcount is the number of mount points using the tcon.
2963 * - ses refcount is the number of tcon using the session.
2964 *
2965 * 1. This function assumes it is being called from cifs_mount() where
2966 * we already got a session reference (ses refcount +1).
2967 *
2968 * 2. Since we're in the context of adding a mount point, the end
2969 * result should be either:
2970 *
2971 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2972 * its session refcount incremented (1 new tcon). This +1 was
2973 * already done in (1).
2974 *
2975 * b) an existing tcon with refcount+1 (add a mount point to it) and
2976 * identical ses refcount (no new tcon). Because of (1) we need to
2977 * decrement the ses refcount.
2978 */
Steve French96daf2b2011-05-27 04:34:02 +00002979static struct cifs_tcon *
2980cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002981{
2982 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002983 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002984
Steve French8b217fe2016-11-11 22:36:20 -06002985 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002986 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002987 /*
2988 * tcon has refcount already incremented but we need to
2989 * decrement extra ses reference gotten by caller (case b)
2990 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002991 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002992 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002993 return tcon;
2994 }
2995
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002996 if (!ses->server->ops->tree_connect) {
2997 rc = -ENOSYS;
2998 goto out_fail;
2999 }
3000
Jeff Laytond00c28d2010-04-24 07:57:44 -04003001 tcon = tconInfoAlloc();
3002 if (tcon == NULL) {
3003 rc = -ENOMEM;
3004 goto out_fail;
3005 }
3006
Steve French8b217fe2016-11-11 22:36:20 -06003007 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003008 if (ses->server->vals->protocol_id == 0) {
3009 cifs_dbg(VFS,
3010 "Use SMB2 or later for snapshot mount option\n");
3011 rc = -EOPNOTSUPP;
3012 goto out_fail;
3013 } else
3014 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003015 }
3016
Jeff Laytond00c28d2010-04-24 07:57:44 -04003017 tcon->ses = ses;
3018 if (volume_info->password) {
3019 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3020 if (!tcon->password) {
3021 rc = -ENOMEM;
3022 goto out_fail;
3023 }
3024 }
3025
Steve French23657ad2018-04-22 15:14:58 -05003026 if (volume_info->seal) {
3027 if (ses->server->vals->protocol_id == 0) {
3028 cifs_dbg(VFS,
3029 "SMB3 or later required for encryption\n");
3030 rc = -EOPNOTSUPP;
3031 goto out_fail;
3032 } else if (tcon->ses->server->capabilities &
3033 SMB2_GLOBAL_CAP_ENCRYPTION)
3034 tcon->seal = true;
3035 else {
3036 cifs_dbg(VFS, "Encryption is not supported on share\n");
3037 rc = -EOPNOTSUPP;
3038 goto out_fail;
3039 }
3040 }
3041
Steve Frenchb3266142018-05-20 23:41:10 -05003042#ifdef CONFIG_CIFS_SMB311
Steve French8505c8b2018-06-18 14:01:59 -05003043 if (volume_info->linux_ext) {
3044 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003045 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003046 printk_once(KERN_WARNING
3047 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003048 } else {
3049 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3050 rc = -EOPNOTSUPP;
3051 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003052 }
Steve Frenchb3266142018-05-20 23:41:10 -05003053 }
3054#endif /* 311 */
3055
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003056 /*
3057 * BB Do we need to wrap session_mutex around this TCon call and Unix
3058 * SetFS as we do on SessSetup and reconnect?
3059 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003060 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003061 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3062 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003063 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003064 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003065 if (rc)
3066 goto out_fail;
3067
3068 if (volume_info->nodfs) {
3069 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
Joe Perchesf96637b2013-05-04 22:12:25 -05003070 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003071 }
Steve Frenchb618f002015-11-03 09:15:03 -06003072 tcon->use_persistent = false;
3073 /* check if SMB2 or later, CIFS does not support persistent handles */
3074 if (volume_info->persistent) {
3075 if (ses->server->vals->protocol_id == 0) {
3076 cifs_dbg(VFS,
3077 "SMB3 or later required for persistent handles\n");
3078 rc = -EOPNOTSUPP;
3079 goto out_fail;
3080 } else if (ses->server->capabilities &
3081 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3082 tcon->use_persistent = true;
3083 else /* persistent handles requested but not supported */ {
3084 cifs_dbg(VFS,
3085 "Persistent handles not supported on share\n");
3086 rc = -EOPNOTSUPP;
3087 goto out_fail;
3088 }
3089 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3090 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3091 && (volume_info->nopersistent == false)) {
3092 cifs_dbg(FYI, "enabling persistent handles\n");
3093 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003094 } else if (volume_info->resilient) {
3095 if (ses->server->vals->protocol_id == 0) {
3096 cifs_dbg(VFS,
3097 "SMB2.1 or later required for resilient handles\n");
3098 rc = -EOPNOTSUPP;
3099 goto out_fail;
3100 }
3101 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003102 }
3103
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003104 /*
3105 * We can have only one retry value for a connection to a share so for
3106 * resources mounted more than once to the same server share the last
3107 * value passed in for the retry flag is used.
3108 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003109 tcon->retry = volume_info->retry;
3110 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003111 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003112 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003113 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003114
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303115 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003116 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303117 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003118
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303119 cifs_fscache_get_super_cookie(tcon);
3120
Jeff Laytond00c28d2010-04-24 07:57:44 -04003121 return tcon;
3122
3123out_fail:
3124 tconInfoFree(tcon);
3125 return ERR_PTR(rc);
3126}
3127
Jeff Layton9d002df2010-10-06 19:51:11 -04003128void
3129cifs_put_tlink(struct tcon_link *tlink)
3130{
3131 if (!tlink || IS_ERR(tlink))
3132 return;
3133
3134 if (!atomic_dec_and_test(&tlink->tl_count) ||
3135 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3136 tlink->tl_time = jiffies;
3137 return;
3138 }
3139
3140 if (!IS_ERR(tlink_tcon(tlink)))
3141 cifs_put_tcon(tlink_tcon(tlink));
3142 kfree(tlink);
3143 return;
3144}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003145
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003146static int
3147compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3148{
3149 struct cifs_sb_info *old = CIFS_SB(sb);
3150 struct cifs_sb_info *new = mnt_data->cifs_sb;
3151
3152 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3153 return 0;
3154
3155 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3156 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3157 return 0;
3158
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003159 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003160 * We want to share sb only if we don't specify an r/wsize or
3161 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003162 */
3163 if (new->wsize && new->wsize < old->wsize)
3164 return 0;
3165
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003166 if (new->rsize && new->rsize < old->rsize)
3167 return 0;
3168
Eric W. Biederman1f682332013-02-06 01:20:20 -08003169 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003170 return 0;
3171
3172 if (old->mnt_file_mode != new->mnt_file_mode ||
3173 old->mnt_dir_mode != new->mnt_dir_mode)
3174 return 0;
3175
3176 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3177 return 0;
3178
3179 if (old->actimeo != new->actimeo)
3180 return 0;
3181
3182 return 1;
3183}
3184
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003185static int
3186match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3187{
3188 struct cifs_sb_info *old = CIFS_SB(sb);
3189 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003190 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3191 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003192
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003193 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003194 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003195 else if (!old_set && !new_set)
3196 return 1;
3197
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003198 return 0;
3199}
3200
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003201int
3202cifs_match_super(struct super_block *sb, void *data)
3203{
3204 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3205 struct smb_vol *volume_info;
3206 struct cifs_sb_info *cifs_sb;
3207 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003208 struct cifs_ses *ses;
3209 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003210 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003211 int rc = 0;
3212
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003213 spin_lock(&cifs_tcp_ses_lock);
3214 cifs_sb = CIFS_SB(sb);
3215 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3216 if (IS_ERR(tlink)) {
3217 spin_unlock(&cifs_tcp_ses_lock);
3218 return rc;
3219 }
3220 tcon = tlink_tcon(tlink);
3221 ses = tcon->ses;
3222 tcp_srv = ses->server;
3223
3224 volume_info = mnt_data->vol;
3225
Jeff Layton9fa114f2012-11-26 11:09:57 -05003226 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003227 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003228 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003229 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003230 rc = 0;
3231 goto out;
3232 }
3233
3234 rc = compare_mount_options(sb, mnt_data);
3235out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003236 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003237 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003238 return rc;
3239}
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003242get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003243 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3244 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 int rc = 0;
3247
Aurelien Aptelb327a712018-01-24 13:46:10 +01003248 if (!ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003249 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003251 *num_referrals = 0;
3252 *referrals = NULL;
3253
Aurelien Aptelb327a712018-01-24 13:46:10 +01003254 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3255 referrals, num_referrals,
3256 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return rc;
3258}
3259
Jeff Layton09e50d52008-07-23 10:11:19 -04003260#ifdef CONFIG_DEBUG_LOCK_ALLOC
3261static struct lock_class_key cifs_key[2];
3262static struct lock_class_key cifs_slock_key[2];
3263
3264static inline void
3265cifs_reclassify_socket4(struct socket *sock)
3266{
3267 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003268 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003269 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3270 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3271}
3272
3273static inline void
3274cifs_reclassify_socket6(struct socket *sock)
3275{
3276 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003277 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003278 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3279 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3280}
3281#else
3282static inline void
3283cifs_reclassify_socket4(struct socket *sock)
3284{
3285}
3286
3287static inline void
3288cifs_reclassify_socket6(struct socket *sock)
3289{
3290}
3291#endif
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003294static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Steve French50c2f752007-07-13 00:33:32 +00003296 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Steve French50c2f752007-07-13 00:33:32 +00003298 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 /* mask a nibble at a time and encode */
3300 target[j] = 'A' + (0x0F & (source[i] >> 4));
3301 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003302 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
3304
3305}
3306
Ben Greear3eb9a882010-09-01 17:06:02 -07003307static int
3308bind_socket(struct TCP_Server_Info *server)
3309{
3310 int rc = 0;
3311 if (server->srcaddr.ss_family != AF_UNSPEC) {
3312 /* Bind to the specified local IP address */
3313 struct socket *socket = server->ssocket;
3314 rc = socket->ops->bind(socket,
3315 (struct sockaddr *) &server->srcaddr,
3316 sizeof(server->srcaddr));
3317 if (rc < 0) {
3318 struct sockaddr_in *saddr4;
3319 struct sockaddr_in6 *saddr6;
3320 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3321 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3322 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003323 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3324 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003325 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003326 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3327 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003328 }
3329 }
3330 return rc;
3331}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
3333static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003334ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
3336 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003337 /*
3338 * some servers require RFC1001 sessinit before sending
3339 * negprot - BB check reconnection in case where second
3340 * sessinit is sent but no second negprot
3341 */
3342 struct rfc1002_session_packet *ses_init_buf;
3343 struct smb_hdr *smb_buf;
3344 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3345 GFP_KERNEL);
3346 if (ses_init_buf) {
3347 ses_init_buf->trailer.session_req.called_len = 32;
3348
Colin Ian King997152f2016-01-25 16:25:54 +00003349 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003350 rfc1002mangle(ses_init_buf->trailer.
3351 session_req.called_name,
3352 server->server_RFC1001_name,
3353 RFC1001_NAME_LEN_WITH_NULL);
3354 else
3355 rfc1002mangle(ses_init_buf->trailer.
3356 session_req.called_name,
3357 DEFAULT_CIFS_CALLED_NAME,
3358 RFC1001_NAME_LEN_WITH_NULL);
3359
3360 ses_init_buf->trailer.session_req.calling_len = 32;
3361
3362 /*
3363 * calling name ends in null (byte 16) from old smb
3364 * convention.
3365 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003366 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003367 rfc1002mangle(ses_init_buf->trailer.
3368 session_req.calling_name,
3369 server->workstation_RFC1001_name,
3370 RFC1001_NAME_LEN_WITH_NULL);
3371 else
3372 rfc1002mangle(ses_init_buf->trailer.
3373 session_req.calling_name,
3374 "LINUX_CIFS_CLNT",
3375 RFC1001_NAME_LEN_WITH_NULL);
3376
3377 ses_init_buf->trailer.session_req.scope1 = 0;
3378 ses_init_buf->trailer.session_req.scope2 = 0;
3379 smb_buf = (struct smb_hdr *)ses_init_buf;
3380
3381 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003382 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003383 rc = smb_send(server, smb_buf, 0x44);
3384 kfree(ses_init_buf);
3385 /*
3386 * RFC1001 layer in at least one server
3387 * requires very short break before negprot
3388 * presumably because not expecting negprot
3389 * to follow so fast. This is a simple
3390 * solution that works without
3391 * complicating the code and causes no
3392 * significant slowing down on mount
3393 * for everyone else
3394 */
3395 usleep_range(1000, 2000);
3396 }
3397 /*
3398 * else the negprot may still work without this
3399 * even though malloc failed
3400 */
3401
3402 return rc;
3403}
3404
3405static int
3406generic_ip_connect(struct TCP_Server_Info *server)
3407{
3408 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003409 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003410 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003411 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003412 struct sockaddr *saddr;
3413
3414 saddr = (struct sockaddr *) &server->dstaddr;
3415
3416 if (server->dstaddr.ss_family == AF_INET6) {
3417 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3418 slen = sizeof(struct sockaddr_in6);
3419 sfamily = AF_INET6;
3420 } else {
3421 sport = ((struct sockaddr_in *) saddr)->sin_port;
3422 slen = sizeof(struct sockaddr_in);
3423 sfamily = AF_INET;
3424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003426 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003427 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3428 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003430 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003431 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003434
3435 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003436 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003437 server->ssocket = socket;
3438 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003439 if (sfamily == AF_INET6)
3440 cifs_reclassify_socket6(socket);
3441 else
3442 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
3444
Ben Greear3eb9a882010-09-01 17:06:02 -07003445 rc = bind_socket(server);
3446 if (rc < 0)
3447 return rc;
3448
Jeff Laytond5c56052008-12-01 18:42:33 -05003449 /*
3450 * Eventually check for other socket options to change from
3451 * the default. sock_setsockopt not used because it expects
3452 * user space buffer
3453 */
3454 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003455 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003456
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003457 /* make the bufsizes depend on wsize/rsize and max requests */
3458 if (server->noautotune) {
3459 if (socket->sk->sk_sndbuf < (200 * 1024))
3460 socket->sk->sk_sndbuf = 200 * 1024;
3461 if (socket->sk->sk_rcvbuf < (140 * 1024))
3462 socket->sk->sk_rcvbuf = 140 * 1024;
3463 }
3464
Steve French6a5fa2362010-01-01 01:28:43 +00003465 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003466 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003467 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3468 (char *)&val, sizeof(val));
3469 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003470 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3471 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003472 }
3473
Joe Perchesf96637b2013-05-04 22:12:25 -05003474 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003475 socket->sk->sk_sndbuf,
3476 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3477
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003478 rc = socket->ops->connect(socket, saddr, slen, 0);
3479 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003480 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003481 sock_release(socket);
3482 server->ssocket = NULL;
3483 return rc;
3484 }
3485
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003486 if (sport == htons(RFC1001_PORT))
3487 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return rc;
3490}
3491
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003492static int
3493ip_connect(struct TCP_Server_Info *server)
3494{
Steve French6da97912011-03-13 18:55:55 +00003495 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003496 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3497 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3498
3499 if (server->dstaddr.ss_family == AF_INET6)
3500 sport = &addr6->sin6_port;
3501 else
3502 sport = &addr->sin_port;
3503
3504 if (*sport == 0) {
3505 int rc;
3506
3507 /* try with 445 port at first */
3508 *sport = htons(CIFS_PORT);
3509
3510 rc = generic_ip_connect(server);
3511 if (rc >= 0)
3512 return rc;
3513
3514 /* if it failed, try with 139 port */
3515 *sport = htons(RFC1001_PORT);
3516 }
3517
3518 return generic_ip_connect(server);
3519}
3520
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003521void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003522 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003523{
3524 /* if we are reconnecting then should we check to see if
3525 * any requested capabilities changed locally e.g. via
3526 * remount but we can not do much about it here
3527 * if they have (even if we could detect it by the following)
3528 * Perhaps we could add a backpointer to array of sb from tcon
3529 * or if we change to make all sb to same share the same
3530 * sb as NFS - then we only have one backpointer to sb.
3531 * What if we wanted to mount the server share twice once with
3532 * and once without posixacls or posix paths? */
3533 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003534
Steve Frenchc18c8422007-07-18 23:21:09 +00003535 if (vol_info && vol_info->no_linux_ext) {
3536 tcon->fsUnixInfo.Capability = 0;
3537 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003538 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003539 return;
3540 } else if (vol_info)
3541 tcon->unix_ext = 1; /* Unix Extensions supported */
3542
3543 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003544 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003545 return;
3546 }
Steve French50c2f752007-07-13 00:33:32 +00003547
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003548 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003549 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003550 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003551 /* check for reconnect case in which we do not
3552 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003553 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003554 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003555 originally at mount time */
3556 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3557 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003558 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3559 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003560 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003561 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003562 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003563 cifs_dbg(VFS, "possible reconnect error\n");
3564 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003565 }
Steve French8af18972007-02-14 04:42:51 +00003566 }
Steve French50c2f752007-07-13 00:33:32 +00003567
Steve French6848b732011-05-26 18:38:54 +00003568 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003569 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003570
Steve French8af18972007-02-14 04:42:51 +00003571 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003572 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003573 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003574 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003575 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003576 if (cifs_sb)
3577 cifs_sb->mnt_cifs_flags |=
3578 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003579 }
3580
Steve French75865f8c2007-06-24 18:30:48 +00003581 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003582 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003583 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003584 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003585 if (cifs_sb)
3586 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003587 CIFS_MOUNT_POSIX_PATHS;
3588 }
Steve French50c2f752007-07-13 00:33:32 +00003589
Joe Perchesf96637b2013-05-04 22:12:25 -05003590 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003591#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003592 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003593 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003594 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003595 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003596 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003597 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003598 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003599 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003600 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003601 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003602 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003603 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003604 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003605 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003606 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003607 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003608 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003609 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003610#endif /* CIFS_DEBUG2 */
3611 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003612 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003613 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003614 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003615 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 +00003616
Steve French8af18972007-02-14 04:42:51 +00003617 }
3618 }
3619}
3620
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003621int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003622 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003623{
Jeff Layton2de970f2010-10-06 19:51:12 -04003624 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3625
Al Viro2ced6f62011-06-17 09:20:04 -04003626 spin_lock_init(&cifs_sb->tlink_tree_lock);
3627 cifs_sb->tlink_tree = RB_ROOT;
3628
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003629 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003630 * Temporarily set r/wsize for matching superblock. If we end up using
3631 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003632 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003633 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003634 cifs_sb->wsize = pvolume_info->wsize;
3635
Steve French3b795212008-11-13 19:45:32 +00003636 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3637 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3638 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3639 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003640 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3641 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003642
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303643 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003644 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303645
Steve French3b795212008-11-13 19:45:32 +00003646 if (pvolume_info->noperm)
3647 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3648 if (pvolume_info->setuids)
3649 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003650 if (pvolume_info->setuidfromacl)
3651 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003652 if (pvolume_info->server_ino)
3653 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3654 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003655 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3656 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003657 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3658 if (pvolume_info->no_xattr)
3659 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3660 if (pvolume_info->sfu_emul)
3661 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3662 if (pvolume_info->nobrl)
3663 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003664 if (pvolume_info->nohandlecache)
3665 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003666 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003667 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003668 if (pvolume_info->mand_lock)
3669 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003670 if (pvolume_info->rwpidforward)
3671 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003672 if (pvolume_info->cifs_acl)
3673 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003674 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003675 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003676 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3677 }
3678 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003679 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003680 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3681 }
Steve French3b795212008-11-13 19:45:32 +00003682 if (pvolume_info->override_uid)
3683 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3684 if (pvolume_info->override_gid)
3685 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3686 if (pvolume_info->dynperm)
3687 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303688 if (pvolume_info->fsc)
3689 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003690 if (pvolume_info->multiuser)
3691 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3692 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003693 if (pvolume_info->strict_io)
3694 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003695 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003696 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003697 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3698 }
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003699 if (pvolume_info->mfsymlinks) {
3700 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003701 /*
3702 * Our SFU ("Services for Unix" emulation does not allow
3703 * creating symlinks but does allow reading existing SFU
3704 * symlinks (it does allow both creating and reading SFU
3705 * style mknod and FIFOs though). When "mfsymlinks" and
3706 * "sfu" are both enabled at the same time, it allows
3707 * reading both types of symlinks, but will only create
3708 * them with mfsymlinks format. This allows better
3709 * Apple compatibility (probably better for Samba too)
3710 * while still recognizing old Windows style symlinks.
3711 */
3712 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003713 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003714 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003715 }
Steve French3b795212008-11-13 19:45:32 +00003716
3717 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003718 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003719
3720 if (pvolume_info->prepath) {
3721 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3722 if (cifs_sb->prepath == NULL)
3723 return -ENOMEM;
3724 }
3725
3726 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003727}
3728
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003729static void
3730cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003731{
Sean Finneyb9468452011-04-11 13:19:32 +00003732 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003733 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003734 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003735 kfree(volume_info->domainname);
3736 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003737 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003738}
3739
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003740void
3741cifs_cleanup_volume_info(struct smb_vol *volume_info)
3742{
3743 if (!volume_info)
3744 return;
3745 cleanup_volume_info_contents(volume_info);
3746 kfree(volume_info);
3747}
3748
3749
Steve French2d6d5892009-04-09 00:36:44 +00003750#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003751/*
3752 * cifs_build_path_to_root returns full path to root when we do not have an
3753 * exiting connection (tcon)
3754 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003755static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003756build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003757 const struct cifs_sb_info *cifs_sb)
3758{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003759 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003760 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003761 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003762
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003763 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003764 if (full_path == NULL)
3765 return ERR_PTR(-ENOMEM);
3766
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003767 strncpy(full_path, vol->UNC, unc_len);
3768 pos = full_path + unc_len;
3769
3770 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003771 *pos = CIFS_DIR_SEP(cifs_sb);
3772 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003773 pos += pplen;
3774 }
3775
3776 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003777 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003778 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003779 return full_path;
3780}
Sean Finneydd613942011-04-11 13:19:30 +00003781
3782/*
3783 * Perform a dfs referral query for a share and (optionally) prefix
3784 *
Sean Finney046462a2011-04-11 13:19:33 +00003785 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3786 * to a string containing updated options for the submount. Otherwise it
3787 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003788 *
3789 * Returns the rc from get_dfs_path to the caller, which can be used to
3790 * determine whether there were referrals.
3791 */
3792static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003793expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003794 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003795 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003796{
3797 int rc;
3798 unsigned int num_referrals = 0;
3799 struct dfs_info3_param *referrals = NULL;
3800 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3801
3802 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3803 if (IS_ERR(full_path))
3804 return PTR_ERR(full_path);
3805
3806 /* For DFS paths, skip the first '\' of the UNC */
3807 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3808
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003809 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003810 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003811
3812 if (!rc && num_referrals > 0) {
3813 char *fake_devname = NULL;
3814
3815 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3816 full_path + 1, referrals,
3817 &fake_devname);
3818
3819 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003820
Sean Finneydd613942011-04-11 13:19:30 +00003821 if (IS_ERR(mdata)) {
3822 rc = PTR_ERR(mdata);
3823 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003824 } else {
3825 cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003826 rc = cifs_setup_volume_info(volume_info, mdata,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003827 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00003828 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003829 kfree(fake_devname);
3830 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003831 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003832 }
3833 kfree(full_path);
3834 return rc;
3835}
Steve French2d6d5892009-04-09 00:36:44 +00003836#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003837
Jeff Layton04db79b2011-07-06 08:10:38 -04003838static int
3839cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003840 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003842 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003843
Steve Frenchc7c137b2018-06-06 17:59:29 -05003844 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04003845 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Jeff Layton7586b762008-12-01 18:41:49 -05003847 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003848 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05003849 kfree(volume_info->username);
3850 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05003851 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05003853 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003855 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003856 /* In userspace mount helper we can get user name from alternate
3857 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003858 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 }
3860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05003862 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003863 /* load_nls_default cannot return null */
3864 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003866 volume_info->local_nls = load_nls(volume_info->iocharset);
3867 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003868 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003869 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04003870 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 }
3872 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003873
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003874 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04003875}
3876
3877struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05003878cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04003879{
3880 int rc;
3881 struct smb_vol *volume_info;
3882
Jeff Layton6ee95422012-11-26 11:09:57 -05003883 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04003884 if (!volume_info)
3885 return ERR_PTR(-ENOMEM);
3886
Steve Frenchc7c137b2018-06-06 17:59:29 -05003887 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04003888 if (rc) {
3889 cifs_cleanup_volume_info(volume_info);
3890 volume_info = ERR_PTR(rc);
3891 }
3892
3893 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003894}
3895
Aurelien Aptela6b50582016-05-25 19:59:09 +02003896static int
3897cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3898 unsigned int xid,
3899 struct cifs_tcon *tcon,
3900 struct cifs_sb_info *cifs_sb,
3901 char *full_path)
3902{
3903 int rc;
3904 char *s;
3905 char sep, tmp;
3906
3907 sep = CIFS_DIR_SEP(cifs_sb);
3908 s = full_path;
3909
3910 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3911 while (rc == 0) {
3912 /* skip separators */
3913 while (*s == sep)
3914 s++;
3915 if (!*s)
3916 break;
3917 /* next separator */
3918 while (*s && *s != sep)
3919 s++;
3920
3921 /*
3922 * temporarily null-terminate the path at the end of
3923 * the current component
3924 */
3925 tmp = *s;
3926 *s = 0;
3927 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3928 full_path);
3929 *s = tmp;
3930 }
3931 return rc;
3932}
3933
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003934int
Al Viro2c6292a2011-06-17 09:05:48 -04003935cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003936{
Jeff Layton1daaae82012-03-21 06:30:40 -04003937 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003938 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003939 struct cifs_ses *ses;
Steve French96daf2b2011-05-27 04:34:02 +00003940 struct cifs_tcon *tcon;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003941 struct TCP_Server_Info *server;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003942 char *full_path;
3943 struct tcon_link *tlink;
3944#ifdef CONFIG_CIFS_DFS_UPCALL
3945 int referral_walks_count = 0;
Jeff Layton20547492011-07-09 12:21:07 -04003946#endif
Al Virodd854462011-06-17 08:24:42 -04003947
Jeff Layton20547492011-07-09 12:21:07 -04003948#ifdef CONFIG_CIFS_DFS_UPCALL
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003949try_mount_again:
3950 /* cleanup activities if we're chasing a referral */
3951 if (referral_walks_count) {
3952 if (tcon)
3953 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003954 else if (ses)
3955 cifs_put_smb_ses(ses);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003956
Sachin Prabhu1dfd18d2015-06-16 16:36:17 +01003957 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3958
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003959 free_xid(xid);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003960 }
3961#endif
Jeff Layton1daaae82012-03-21 06:30:40 -04003962 rc = 0;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003963 tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003964 ses = NULL;
3965 server = NULL;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003966 full_path = NULL;
3967 tlink = NULL;
3968
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003969 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Jeff Layton63c038c2008-12-01 18:41:46 -05003971 /* get a reference to a tcp session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003972 server = cifs_get_tcp_session(volume_info);
3973 if (IS_ERR(server)) {
3974 rc = PTR_ERR(server);
Jeff Layton63c038c2008-12-01 18:41:46 -05003975 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 }
Steve French141891f2016-09-23 00:44:16 -05003977 if ((volume_info->max_credits < 20) ||
3978 (volume_info->max_credits > 60000))
3979 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3980 else
3981 server->max_credits = volume_info->max_credits;
Jeff Layton36988c72010-04-24 07:57:43 -04003982 /* get a reference to a SMB session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003983 ses = cifs_get_smb_ses(server, volume_info);
3984 if (IS_ERR(ses)) {
3985 rc = PTR_ERR(ses);
3986 ses = NULL;
Jeff Layton36988c72010-04-24 07:57:43 -04003987 goto mount_fail_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
Steve French50c2f752007-07-13 00:33:32 +00003989
Steve Frenchb618f002015-11-03 09:15:03 -06003990 if ((volume_info->persistent == true) && ((ses->server->capabilities &
3991 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
3992 cifs_dbg(VFS, "persistent handles not supported by server\n");
3993 rc = -EOPNOTSUPP;
3994 goto mount_fail_check;
3995 }
Steve French592fafe2015-11-03 10:08:53 -06003996
Jeff Laytond00c28d2010-04-24 07:57:44 -04003997 /* search for existing tcon to this server share */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003998 tcon = cifs_get_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003999 if (IS_ERR(tcon)) {
4000 rc = PTR_ERR(tcon);
4001 tcon = NULL;
Mark Syms40920c22016-11-29 11:36:46 +00004002 if (rc == -EACCES)
4003 goto mount_fail_check;
4004
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004005 goto remote_path_check;
Jeff Laytond00c28d2010-04-24 07:57:44 -04004006 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004007
Steve Frenchce558b02018-05-31 19:16:54 -05004008#ifdef CONFIG_CIFS_SMB311
4009 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4010 if (tcon->posix_extensions)
4011 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4012#endif /* SMB3.11 */
4013
Steve French6848b732011-05-26 18:38:54 +00004014 /* tell server which Unix caps we support */
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004015 if (cap_unix(tcon->ses)) {
Steve French6848b732011-05-26 18:38:54 +00004016 /* reset of caps checks mount to see if unix extensions
4017 disabled for just this mount */
Al Viro2c6292a2011-06-17 09:05:48 -04004018 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
Steve French6848b732011-05-26 18:38:54 +00004019 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4020 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4021 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
4022 rc = -EACCES;
4023 goto mount_fail_check;
4024 }
4025 } else
4026 tcon->unix_ext = 0; /* server does not support them */
4027
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004028 /* do not care if a following call succeed - informational */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004029 if (!tcon->pipe && server->ops->qfs_tcon)
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004030 server->ops->qfs_tcon(xid, tcon);
Steve Frenchd82c2df2008-11-15 00:07:26 +00004031
Pavel Shilovsky24985c52012-09-18 16:20:28 -07004032 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
4033 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04004034
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004035remote_path_check:
Sean Finneyc1508ca2011-04-11 13:19:31 +00004036#ifdef CONFIG_CIFS_DFS_UPCALL
4037 /*
4038 * Perform an unconditional check for whether there are DFS
4039 * referrals for this path without prefix, to provide support
4040 * for DFS referrals from w2k8 servers which don't seem to respond
4041 * with PATH_NOT_COVERED to requests that include the prefix.
4042 * Chase the referral if found, otherwise continue normally.
4043 */
4044 if (referral_walks_count == 0) {
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004045 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
4046 false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004047 if (!refrc) {
4048 referral_walks_count++;
4049 goto try_mount_again;
4050 }
4051 }
4052#endif
4053
Steve Frenchf87d39d2011-05-27 03:50:55 +00004054 /* check if a whole path is not remote */
Jeff Layton70945642011-03-14 13:48:08 -04004055 if (!rc && tcon) {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004056 if (!server->ops->is_path_accessible) {
4057 rc = -ENOSYS;
4058 goto mount_fail_check;
4059 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004060 /*
4061 * cifs_build_path_to_root works only when we have a valid tcon
4062 */
Sachin Prabhu374402a2016-12-15 12:31:19 +05304063 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
4064 tcon->Flags & SMB_SHARE_IS_IN_DFS);
Igor Mammedove4cce942009-02-10 14:10:26 +03004065 if (full_path == NULL) {
4066 rc = -ENOMEM;
4067 goto mount_fail_check;
4068 }
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004069 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4070 full_path);
Jeff Layton03ceace2010-12-06 21:07:33 -05004071 if (rc != 0 && rc != -EREMOTE) {
Igor Mammedove4cce942009-02-10 14:10:26 +03004072 kfree(full_path);
4073 goto mount_fail_check;
4074 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004075
Sachin Prabhud1713562016-09-06 13:22:34 +01004076 if (rc != -EREMOTE) {
4077 rc = cifs_are_all_path_components_accessible(server,
Aurelien Aptela6b50582016-05-25 19:59:09 +02004078 xid, tcon, cifs_sb,
4079 full_path);
Sachin Prabhud1713562016-09-06 13:22:34 +01004080 if (rc != 0) {
4081 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4082 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4083 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4084 rc = 0;
4085 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004086 }
Igor Mammedove4cce942009-02-10 14:10:26 +03004087 kfree(full_path);
4088 }
4089
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004090 /* get referral if needed */
4091 if (rc == -EREMOTE) {
Steve Frenchd036f502009-04-03 03:12:08 +00004092#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004093 if (referral_walks_count > MAX_NESTED_LINKS) {
4094 /*
4095 * BB: when we implement proper loop detection,
4096 * we will remove this check. But now we need it
4097 * to prevent an indefinite loop if 'DFS tree' is
4098 * misconfigured (i.e. has loops).
4099 */
4100 rc = -ELOOP;
4101 goto mount_fail_check;
4102 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004103
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004104 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
Jeff Layton7b91e262009-07-23 15:22:30 -04004105
Sean Finneydd613942011-04-11 13:19:30 +00004106 if (!rc) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004107 referral_walks_count++;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004108 goto try_mount_again;
4109 }
Sean Finneydd613942011-04-11 13:19:30 +00004110 goto mount_fail_check;
Steve Frenchd036f502009-04-03 03:12:08 +00004111#else /* No DFS support, return error on mount */
4112 rc = -EOPNOTSUPP;
4113#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004114 }
4115
Jeff Layton9d002df2010-10-06 19:51:11 -04004116 if (rc)
4117 goto mount_fail_check;
4118
4119 /* now, hang the tcon off of the superblock */
4120 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
4121 if (tlink == NULL) {
4122 rc = -ENOMEM;
4123 goto mount_fail_check;
4124 }
4125
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004126 tlink->tl_uid = ses->linux_uid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004127 tlink->tl_tcon = tcon;
4128 tlink->tl_time = jiffies;
4129 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4130 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4131
Jeff Layton413e6612010-10-28 13:33:38 -04004132 cifs_sb->master_tlink = tlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004133 spin_lock(&cifs_sb->tlink_tree_lock);
4134 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4135 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton413e6612010-10-28 13:33:38 -04004136
Jeff Laytonda472fc2012-03-23 14:40:53 -04004137 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004138 TLINK_IDLE_EXPIRE);
4139
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004140mount_fail_check:
4141 /* on error free sesinfo and tcon struct if needed */
4142 if (rc) {
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004143 /* If find_unc succeeded then rc == 0 so we can not end */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004144 /* up accidentally freeing someone elses tcon struct */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004145 if (tcon)
4146 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004147 else if (ses)
4148 cifs_put_smb_ses(ses);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004149 else
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004150 cifs_put_tcp_session(server, 0);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004151 }
4152
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004153out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004154 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 return rc;
4156}
4157
Jeff Layton8d1bca32011-06-11 21:17:10 -04004158/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004159 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004162CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004163 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 const struct nls_table *nls_codepage)
4165{
4166 struct smb_hdr *smb_buffer;
4167 struct smb_hdr *smb_buffer_response;
4168 TCONX_REQ *pSMB;
4169 TCONX_RSP *pSMBr;
4170 unsigned char *bcc_ptr;
4171 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004172 int length;
4173 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 if (ses == NULL)
4176 return -EIO;
4177
4178 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004179 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 smb_buffer_response = smb_buffer;
4183
4184 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4185 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004186
Pavel Shilovsky88257362012-05-23 14:01:59 +04004187 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 smb_buffer->Uid = ses->Suid;
4189 pSMB = (TCONX_REQ *) smb_buffer;
4190 pSMBr = (TCONX_RSP *) smb_buffer_response;
4191
4192 pSMB->AndXCommand = 0xFF;
4193 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004195 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004196 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004197 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004198 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004199 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004200 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004201 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004202 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4203 specified as required (when that support is added to
4204 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004205 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004206 by Samba (not sure whether other servers allow
4207 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004208#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004209 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004210 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004211 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004212 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004213 SECMODE_PW_ENCRYPT ? true : false,
4214 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004215 else
4216#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004217 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004218 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004219 if (rc) {
4220 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4221 __func__, rc);
4222 cifs_buf_release(smb_buffer);
4223 return rc;
4224 }
Steve Frencheeac8042006-01-13 21:34:58 -08004225
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004226 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004227 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004228 /* must align unicode strings */
4229 *bcc_ptr = 0; /* null byte password */
4230 bcc_ptr++;
4231 }
Steve Frencheeac8042006-01-13 21:34:58 -08004232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Jeff Layton38d77c52013-05-26 07:01:00 -04004234 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4236
4237 if (ses->capabilities & CAP_STATUS32) {
4238 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4239 }
4240 if (ses->capabilities & CAP_DFS) {
4241 smb_buffer->Flags2 |= SMBFLG2_DFS;
4242 }
4243 if (ses->capabilities & CAP_UNICODE) {
4244 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4245 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004246 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004247 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004248 (/* server len*/ + 256 /* share len */), nls_codepage);
4249 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 bcc_ptr += 2; /* skip trailing null */
4251 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 strcpy(bcc_ptr, tree);
4253 bcc_ptr += strlen(tree) + 1;
4254 }
4255 strcpy(bcc_ptr, "?????");
4256 bcc_ptr += strlen("?????");
4257 bcc_ptr += 1;
4258 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004259 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4260 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 pSMB->ByteCount = cpu_to_le16(count);
4262
Steve French133672e2007-11-13 22:41:37 +00004263 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004264 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004267 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004268 bool is_unicode;
4269
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004271 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 tcon->tid = smb_buffer_response->Tid;
4273 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004274 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004275 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004276 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4277 is_unicode = true;
4278 else
4279 is_unicode = false;
4280
Jeff Laytoncc20c032009-04-30 07:16:21 -04004281
Steve French50c2f752007-07-13 00:33:32 +00004282 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004283 if (length == 3) {
4284 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4285 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004286 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004287 tcon->ipc = true;
4288 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004289 }
4290 } else if (length == 2) {
4291 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4292 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004293 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004294 }
4295 }
Steve French50c2f752007-07-13 00:33:32 +00004296 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004297 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004298 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004299
4300 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004301 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004302 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004303 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004304 nls_codepage);
4305
Joe Perchesf96637b2013-05-04 22:12:25 -05004306 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004307
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004308 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004309 (smb_buffer_response->WordCount == 7))
4310 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004311 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4312 else
4313 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004314 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 }
4316
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004317 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 return rc;
4319}
4320
Al Viro2e32cf52013-10-03 12:53:37 -04004321static void delayed_free(struct rcu_head *p)
4322{
4323 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4324 unload_nls(sbi->local_nls);
4325 kfree(sbi);
4326}
4327
Al Viro2a9b9952011-06-17 09:27:16 -04004328void
4329cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
Jeff Laytonb647c352010-10-28 11:16:44 -04004331 struct rb_root *root = &cifs_sb->tlink_tree;
4332 struct rb_node *node;
4333 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Jeff Layton2de970f2010-10-06 19:51:12 -04004335 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4336
Jeff Laytonb647c352010-10-28 11:16:44 -04004337 spin_lock(&cifs_sb->tlink_tree_lock);
4338 while ((node = rb_first(root))) {
4339 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4340 cifs_get_tlink(tlink);
4341 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4342 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004343
Jeff Laytonb647c352010-10-28 11:16:44 -04004344 spin_unlock(&cifs_sb->tlink_tree_lock);
4345 cifs_put_tlink(tlink);
4346 spin_lock(&cifs_sb->tlink_tree_lock);
4347 }
4348 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004349
Al Virod757d712011-06-17 09:42:43 -04004350 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004351 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004352 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004353}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004355int
4356cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357{
4358 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004359 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004361 if (!server->ops->need_neg || !server->ops->negotiate)
4362 return -ENOSYS;
4363
Jeff Layton198b5682010-04-24 07:57:48 -04004364 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004365 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Pavel Shilovsky45275782012-05-17 17:53:29 +04004368 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004369
4370 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004371 if (rc == 0) {
4372 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004373 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004374 server->tcpStatus = CifsGood;
4375 else
4376 rc = -EHOSTDOWN;
4377 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 }
Steve French26b994f2008-08-06 05:11:33 +00004379
Jeff Layton198b5682010-04-24 07:57:48 -04004380 return rc;
4381}
Steve French26b994f2008-08-06 05:11:33 +00004382
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004383int
4384cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4385 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004386{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004387 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004388 struct TCP_Server_Info *server = ses->server;
4389
Jeff Layton198b5682010-04-24 07:57:48 -04004390 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004391 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004392 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004393
Joe Perchesf96637b2013-05-04 22:12:25 -05004394 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004395 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004396
Shu Wangf5c4ba82017-09-08 18:48:33 +08004397 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05004398 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08004399 ses->auth_key.response);
4400 kfree(ses->auth_key.response);
4401 ses->auth_key.response = NULL;
4402 ses->auth_key.len = 0;
4403 }
4404
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004405 if (server->ops->sess_setup)
4406 rc = server->ops->sess_setup(xid, ses, nls_info);
4407
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004408 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004409 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 return rc;
4412}
4413
Jeff Layton8a8798a2012-01-17 16:09:15 -05004414static int
4415cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4416{
Jeff Layton3f618222013-06-12 19:52:14 -05004417 vol->sectype = ses->sectype;
4418
4419 /* krb5 is special, since we don't need username or pw */
4420 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004421 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004422
4423 return cifs_set_cifscreds(vol, ses);
4424}
4425
Steve French96daf2b2011-05-27 04:34:02 +00004426static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004427cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004428{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004429 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004430 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4431 struct cifs_ses *ses;
4432 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004433 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004434
4435 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004436 if (vol_info == NULL)
4437 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004438
Jeff Layton9d002df2010-10-06 19:51:11 -04004439 vol_info->local_nls = cifs_sb->local_nls;
4440 vol_info->linux_uid = fsuid;
4441 vol_info->cred_uid = fsuid;
4442 vol_info->UNC = master_tcon->treeName;
4443 vol_info->retry = master_tcon->retry;
4444 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05004445 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04004446 vol_info->local_lease = master_tcon->local_lease;
4447 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004448 vol_info->sectype = master_tcon->ses->sectype;
4449 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004450
Jeff Layton8a8798a2012-01-17 16:09:15 -05004451 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4452 if (rc) {
4453 tcon = ERR_PTR(rc);
4454 goto out;
4455 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004456
4457 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304458 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004459 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304460 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004461
4462 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4463 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004464 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004465 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004466 goto out;
4467 }
4468
4469 tcon = cifs_get_tcon(ses, vol_info);
4470 if (IS_ERR(tcon)) {
4471 cifs_put_smb_ses(ses);
4472 goto out;
4473 }
4474
Steve Frenchce558b02018-05-31 19:16:54 -05004475#ifdef CONFIG_CIFS_SMB311
4476 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4477 if (tcon->posix_extensions)
4478 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4479#endif /* SMB3.11 */
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004480 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004481 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05004482
Jeff Layton9d002df2010-10-06 19:51:11 -04004483out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004484 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01004485 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004486 kfree(vol_info);
4487
4488 return tcon;
4489}
4490
Steve French96daf2b2011-05-27 04:34:02 +00004491struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004492cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4493{
4494 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4495}
4496
Jeff Laytonb647c352010-10-28 11:16:44 -04004497/* find and return a tlink with given uid */
4498static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004499tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004500{
4501 struct rb_node *node = root->rb_node;
4502 struct tcon_link *tlink;
4503
4504 while (node) {
4505 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4506
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004507 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004508 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004509 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004510 node = node->rb_right;
4511 else
4512 return tlink;
4513 }
4514 return NULL;
4515}
4516
4517/* insert a tcon_link into the tree */
4518static void
4519tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4520{
4521 struct rb_node **new = &(root->rb_node), *parent = NULL;
4522 struct tcon_link *tlink;
4523
4524 while (*new) {
4525 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4526 parent = *new;
4527
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004528 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004529 new = &((*new)->rb_left);
4530 else
4531 new = &((*new)->rb_right);
4532 }
4533
4534 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4535 rb_insert_color(&new_tlink->tl_rbnode, root);
4536}
4537
Jeff Layton9d002df2010-10-06 19:51:11 -04004538/*
4539 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4540 * current task.
4541 *
4542 * If the superblock doesn't refer to a multiuser mount, then just return
4543 * the master tcon for the mount.
4544 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304545 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004546 * exists, then check to see if it's pending construction. If it is then wait
4547 * for construction to complete. Once it's no longer pending, check to see if
4548 * it failed and either return an error or retry construction, depending on
4549 * the timeout.
4550 *
4551 * If one doesn't exist then insert a new tcon_link struct into the tree and
4552 * try to construct a new one.
4553 */
4554struct tcon_link *
4555cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4556{
4557 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004558 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004559 struct tcon_link *tlink, *newtlink;
4560
4561 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4562 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4563
4564 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004565 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004566 if (tlink)
4567 cifs_get_tlink(tlink);
4568 spin_unlock(&cifs_sb->tlink_tree_lock);
4569
4570 if (tlink == NULL) {
4571 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4572 if (newtlink == NULL)
4573 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004574 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004575 newtlink->tl_tcon = ERR_PTR(-EACCES);
4576 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4577 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4578 cifs_get_tlink(newtlink);
4579
Jeff Layton9d002df2010-10-06 19:51:11 -04004580 spin_lock(&cifs_sb->tlink_tree_lock);
4581 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004582 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004583 if (tlink) {
4584 cifs_get_tlink(tlink);
4585 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004586 kfree(newtlink);
4587 goto wait_for_construction;
4588 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004589 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004590 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4591 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004592 } else {
4593wait_for_construction:
4594 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004595 TASK_INTERRUPTIBLE);
4596 if (ret) {
4597 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004598 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004599 }
4600
4601 /* if it's good, return it */
4602 if (!IS_ERR(tlink->tl_tcon))
4603 return tlink;
4604
4605 /* return error if we tried this already recently */
4606 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4607 cifs_put_tlink(tlink);
4608 return ERR_PTR(-EACCES);
4609 }
4610
4611 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4612 goto wait_for_construction;
4613 }
4614
4615 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4616 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4617 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4618
4619 if (IS_ERR(tlink->tl_tcon)) {
4620 cifs_put_tlink(tlink);
4621 return ERR_PTR(-EACCES);
4622 }
4623
4624 return tlink;
4625}
Jeff Layton2de970f2010-10-06 19:51:12 -04004626
4627/*
4628 * periodic workqueue job that scans tcon_tree for a superblock and closes
4629 * out tcons.
4630 */
4631static void
4632cifs_prune_tlinks(struct work_struct *work)
4633{
4634 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4635 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004636 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004637 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004638 struct rb_node *tmp;
4639 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004640
Jeff Laytonb647c352010-10-28 11:16:44 -04004641 /*
4642 * Because we drop the spinlock in the loop in order to put the tlink
4643 * it's not guarded against removal of links from the tree. The only
4644 * places that remove entries from the tree are this function and
4645 * umounts. Because this function is non-reentrant and is canceled
4646 * before umount can proceed, this is safe.
4647 */
4648 spin_lock(&cifs_sb->tlink_tree_lock);
4649 node = rb_first(root);
4650 while (node != NULL) {
4651 tmp = node;
4652 node = rb_next(tmp);
4653 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4654
4655 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4656 atomic_read(&tlink->tl_count) != 0 ||
4657 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4658 continue;
4659
4660 cifs_get_tlink(tlink);
4661 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4662 rb_erase(tmp, root);
4663
Jeff Layton2de970f2010-10-06 19:51:12 -04004664 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004665 cifs_put_tlink(tlink);
4666 spin_lock(&cifs_sb->tlink_tree_lock);
4667 }
4668 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004669
Jeff Laytonda472fc2012-03-23 14:40:53 -04004670 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004671 TLINK_IDLE_EXPIRE);
4672}