blob: 944188d6200c246da16e68364bae713737a9c6a2 [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" },
Steve French9b9c5be2018-09-22 12:07:06 -0500253 { Opt_ignore, "relatime" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400254 { Opt_ignore, "_netdev" },
255
256 { Opt_err, NULL }
257};
258
259enum {
260 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
261 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400262 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
263 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400264 Opt_sec_none,
265
266 Opt_sec_err
267};
268
269static const match_table_t cifs_secflavor_tokens = {
270 { Opt_sec_krb5, "krb5" },
271 { Opt_sec_krb5i, "krb5i" },
272 { Opt_sec_krb5p, "krb5p" },
273 { Opt_sec_ntlmsspi, "ntlmsspi" },
274 { Opt_sec_ntlmssp, "ntlmssp" },
275 { Opt_ntlm, "ntlm" },
276 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400277 { Opt_sec_ntlmv2, "nontlm" },
278 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400279 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400280 { Opt_sec_lanman, "lanman" },
281 { Opt_sec_none, "none" },
282
283 { Opt_sec_err, NULL }
284};
285
Jeff Layton15b6a472012-05-16 07:50:15 -0400286/* cache flavors */
287enum {
288 Opt_cache_loose,
289 Opt_cache_strict,
290 Opt_cache_none,
291 Opt_cache_err
292};
293
294static const match_table_t cifs_cacheflavor_tokens = {
295 { Opt_cache_loose, "loose" },
296 { Opt_cache_strict, "strict" },
297 { Opt_cache_none, "none" },
298 { Opt_cache_err, NULL }
299};
300
Jeff Layton23db65f2012-05-15 12:20:51 -0400301static const match_table_t cifs_smb_version_tokens = {
302 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600303 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000304 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500305 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500306 { Smb_302, SMB302_VERSION_STRING },
Kenneth D'souza4a3b38a2018-11-17 10:33:30 +0530307 { Smb_302, ALT_SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600308 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500309 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French9764c022017-09-17 10:41:35 -0500310 { Smb_3any, SMB3ANY_VERSION_STRING },
311 { Smb_default, SMBDEFAULT_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600312 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400313};
314
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300315static int ip_connect(struct TCP_Server_Info *server);
316static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400317static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400318static void cifs_prune_tlinks(struct work_struct *work);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -0400319static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -0500320 const char *devname, bool is_smb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Jeff Laytond5c56052008-12-01 18:42:33 -0500322/*
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
Steve French6e4d3bb2018-09-22 11:25:04 -0500352 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500353 trace_smb3_reconnect(server->CurrentMid, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* before reconnecting the tcp session, mark the smb session (uid)
356 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500357 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
358 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530359 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500360 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000361 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500362 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500363 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000364 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500365 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100367 if (ses->tcon_ipc)
368 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530370 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500373 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500374 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000375 if (server->ssocket) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500376 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
377 server->ssocket->state, server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800378 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesf96637b2013-05-04 22:12:25 -0500379 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
380 server->ssocket->state, server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 sock_release(server->ssocket);
382 server->ssocket = NULL;
383 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500384 server->sequence_number = 0;
385 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500386 kfree(server->session_key.response);
387 server->session_key.response = NULL;
388 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000389 server->lstrp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500391 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400392 INIT_LIST_HEAD(&retry_list);
Joe Perchesf96637b2013-05-04 22:12:25 -0500393 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500395 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
396 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400397 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
398 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400399 list_move(&mid_entry->qhead, &retry_list);
400 }
401 spin_unlock(&GlobalMid_Lock);
Rabin Vincent820962d2015-12-23 07:32:41 +0100402 mutex_unlock(&server->srv_mutex);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400403
Joe Perchesf96637b2013-05-04 22:12:25 -0500404 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400405 list_for_each_safe(tmp, tmp2, &retry_list) {
406 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500407 list_del_init(&mid_entry->qhead);
408 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400411 do {
Steve French6c3d8902006-07-31 22:46:20 +0000412 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300413
414 /* we should try only the port we connected to before */
Jeff Layton73e216a2013-09-05 08:38:10 -0400415 mutex_lock(&server->srv_mutex);
Long Li781a8052017-11-22 17:38:36 -0700416 if (cifs_rdma_enabled(server))
417 rc = smbd_reconnect(server);
418 else
419 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000420 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500421 cifs_dbg(FYI, "reconnect error %d\n", rc);
Federico Sauter4afe2602015-03-17 17:45:28 +0100422 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700423 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 } else {
425 atomic_inc(&tcpSesReconnectCount);
426 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000427 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000428 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000429 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100430 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400432 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500433
Sachin Prabhub8c60012016-10-20 19:52:24 -0400434 if (server->tcpStatus == CifsNeedNegotiate)
435 mod_delayed_work(cifsiod_wq, &server->echo, 0);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return rc;
438}
439
Jeff Laytonc74093b2011-01-11 07:24:23 -0500440static void
441cifs_echo_request(struct work_struct *work)
442{
443 int rc;
444 struct TCP_Server_Info *server = container_of(work,
445 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400446 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500447
Jeff Layton247ec9b2011-02-04 17:09:50 -0500448 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400449 * If we need to renegotiate, set echo interval to zero to
450 * immediately call echo service where we can renegotiate.
451 */
452 if (server->tcpStatus == CifsNeedNegotiate)
453 echo_interval = 0;
454 else
455 echo_interval = server->echo_interval;
456
457 /*
458 * We cannot send an echo if it is disabled.
459 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500460 */
Steve French4fcd1812016-06-22 20:12:05 -0500461
462 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400463 server->tcpStatus == CifsExiting ||
464 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400465 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600466 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500467 goto requeue_echo;
468
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400469 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500470 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500471 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
472 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500473
474requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400475 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500476}
477
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400478static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400479allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400480{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400481 if (!server->bigbuf) {
482 server->bigbuf = (char *)cifs_buf_get();
483 if (!server->bigbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500484 cifs_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400485 msleep(3000);
486 /* retry will check if exiting */
487 return false;
488 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400489 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400490 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400491 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400492 }
493
Jeff Layton2a37ef92011-10-19 15:29:23 -0400494 if (!server->smallbuf) {
495 server->smallbuf = (char *)cifs_small_buf_get();
496 if (!server->smallbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500497 cifs_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400498 msleep(1000);
499 /* retry will check if exiting */
500 return false;
501 }
502 /* beginning of smb buffer is cleared in our buf_get */
503 } else {
504 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400505 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400506 }
507
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400508 return true;
509}
510
Jeff Laytonba749e62011-10-11 06:41:32 -0400511static bool
512server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400513{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300514 /*
515 * We need to wait 2 echo intervals to make sure we handle such
516 * situations right:
517 * 1s client sends a normal SMB request
518 * 2s client gets a response
519 * 30s echo workqueue job pops, and decides we got a response recently
520 * and don't need to send another
521 * ...
522 * 65s kernel_recvmsg times out, and we see that we haven't gotten
523 * a response in >60s.
524 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200525 if ((server->tcpStatus == CifsGood ||
526 server->tcpStatus == CifsNeedNegotiate) &&
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600527 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
528 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
529 server->hostname, (2 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400530 cifs_reconnect(server);
531 wake_up(&server->response_q);
532 return true;
533 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400534
Jeff Laytonba749e62011-10-11 06:41:32 -0400535 return false;
536}
537
Al Viro71335662016-01-09 19:54:50 -0500538static int
539cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400540{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400541 int length = 0;
542 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400543
Al Viro71335662016-01-09 19:54:50 -0500544 smb_msg->msg_control = NULL;
545 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400546
Al Viro71335662016-01-09 19:54:50 -0500547 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500548 try_to_freeze();
549
Al Viro71335662016-01-09 19:54:50 -0500550 if (server_unresponsive(server))
551 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700552 if (cifs_rdma_enabled(server) && server->smbd_conn)
553 length = smbd_recv(server->smbd_conn, smb_msg);
554 else
555 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500556
557 if (server->tcpStatus == CifsExiting)
558 return -ESHUTDOWN;
559
560 if (server->tcpStatus == CifsNeedReconnect) {
561 cifs_reconnect(server);
562 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400563 }
564
Al Viro71335662016-01-09 19:54:50 -0500565 if (length == -ERESTARTSYS ||
566 length == -EAGAIN ||
567 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400568 /*
569 * Minimum sleep to prevent looping, allowing socket
570 * to clear and app threads to set tcpStatus
571 * CifsNeedReconnect if server hung.
572 */
573 usleep_range(1000, 2000);
574 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400575 continue;
Al Viro71335662016-01-09 19:54:50 -0500576 }
577
578 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500579 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400580 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500581 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400582 }
583 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400584 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400585}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400586
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400587int
588cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
589 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400590{
Al Viro71335662016-01-09 19:54:50 -0500591 struct msghdr smb_msg;
592 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100593 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400594
Al Viro71335662016-01-09 19:54:50 -0500595 return cifs_readv_from_socket(server, &smb_msg);
596}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400597
Al Viro71335662016-01-09 19:54:50 -0500598int
599cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700600 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500601{
602 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700603 struct bio_vec bv = {
604 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100605 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500606 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400607}
608
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400609static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400610is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400611{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400612 /*
613 * The first byte big endian of the length field,
614 * is actually not part of the length but the type
615 * with the most common, zero, as regular data.
616 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400617 switch (type) {
618 case RFC1002_SESSION_MESSAGE:
619 /* Regular SMB response */
620 return true;
621 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500622 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400623 break;
624 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500625 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400626 break;
627 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400628 /*
629 * We get this from Windows 98 instead of an error on
630 * SMB negprot response.
631 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500632 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400633 /* give server a second to clean up */
634 msleep(1000);
635 /*
636 * Always try 445 first on reconnect since we get NACK
637 * on some if we ever connected to port 139 (the NACK
638 * is since we do not begin with RFC1001 session
639 * initialize frame).
640 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400641 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400642 cifs_reconnect(server);
643 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400644 break;
645 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500646 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400647 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400648 }
649
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400650 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400651}
652
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400653void
654dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400655{
656#ifdef CONFIG_CIFS_STATS2
657 mid->when_received = jiffies;
658#endif
659 spin_lock(&GlobalMid_Lock);
660 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400661 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400662 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400663 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000664 /*
665 * Trying to handle/dequeue a mid after the send_recv()
666 * function has finished processing it is a bug.
667 */
668 if (mid->mid_flags & MID_DELETED)
669 printk_once(KERN_WARNING
670 "trying to dequeue a deleted mid\n");
671 else
672 list_del_init(&mid->qhead);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400673 spin_unlock(&GlobalMid_Lock);
674}
675
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400676static void
677handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400678 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400679{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400680 if (server->ops->check_trans2 &&
681 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400682 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400683 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400684 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400685 /* Was previous buf put in mpx struct for multi-rsp? */
686 if (!mid->multiRsp) {
687 /* smb buffer will be freed by user thread */
688 if (server->large_buf)
689 server->bigbuf = NULL;
690 else
691 server->smallbuf = NULL;
692 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400693 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400694}
695
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400696static void clean_demultiplex_info(struct TCP_Server_Info *server)
697{
698 int length;
699
700 /* take it off the list, if it's not already */
701 spin_lock(&cifs_tcp_ses_lock);
702 list_del_init(&server->tcp_ses_list);
703 spin_unlock(&cifs_tcp_ses_lock);
704
705 spin_lock(&GlobalMid_Lock);
706 server->tcpStatus = CifsExiting;
707 spin_unlock(&GlobalMid_Lock);
708 wake_up_all(&server->response_q);
709
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400710 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300711 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400712 if (server->credits <= 0)
713 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300714 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400715 /*
716 * Although there should not be any requests blocked on this queue it
717 * can not hurt to be paranoid and try to wake up requests that may
718 * haven been blocked when more than 50 at time were on the wire to the
719 * same server - they now will see the session is in exit state and get
720 * out of SendReceive.
721 */
722 wake_up_all(&server->request_q);
723 /* give those requests time to exit */
724 msleep(125);
Long Libce9ce72017-11-22 17:38:38 -0700725 if (cifs_rdma_enabled(server) && server->smbd_conn) {
726 smbd_destroy(server->smbd_conn);
727 server->smbd_conn = NULL;
728 }
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400729 if (server->ssocket) {
730 sock_release(server->ssocket);
731 server->ssocket = NULL;
732 }
733
734 if (!list_empty(&server->pending_mid_q)) {
735 struct list_head dispose_list;
736 struct mid_q_entry *mid_entry;
737 struct list_head *tmp, *tmp2;
738
739 INIT_LIST_HEAD(&dispose_list);
740 spin_lock(&GlobalMid_Lock);
741 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
742 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500743 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400744 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400745 list_move(&mid_entry->qhead, &dispose_list);
746 }
747 spin_unlock(&GlobalMid_Lock);
748
749 /* now walk dispose list and issue callbacks */
750 list_for_each_safe(tmp, tmp2, &dispose_list) {
751 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500752 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400753 list_del_init(&mid_entry->qhead);
754 mid_entry->callback(mid_entry);
755 }
756 /* 1/8th of sec is more than enough time for them to exit */
757 msleep(125);
758 }
759
760 if (!list_empty(&server->pending_mid_q)) {
761 /*
762 * mpx threads have not exited yet give them at least the smb
763 * send timeout time for long ops.
764 *
765 * Due to delays on oplock break requests, we need to wait at
766 * least 45 seconds before giving up on a request getting a
767 * response and going ahead and killing cifsd.
768 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500769 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400770 msleep(46000);
771 /*
772 * If threads still have not exited they are probably never
773 * coming home not much else we can do but free the memory.
774 */
775 }
776
777 kfree(server->hostname);
778 kfree(server);
779
780 length = atomic_dec_return(&tcpSesAllocCount);
781 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700782 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400783}
784
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400785static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400786standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
787{
788 int length;
789 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000790 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400791
792 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100793 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
794 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500795 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400796 cifs_reconnect(server);
797 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400798 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400799 }
800
801 /* switch to large buffer if too big for a small one */
802 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
803 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400804 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400805 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400806 }
807
808 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400809 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100810 pdu_length - HEADER_SIZE(server) + 1
811 + server->vals->header_preamble_size);
812
Jeff Laytone9097ab2011-10-19 15:29:40 -0400813 if (length < 0)
814 return length;
815 server->total_read += length;
816
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400817 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400818
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800819 return cifs_handle_standard(server, mid);
820}
821
822int
823cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
824{
825 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
826 int length;
827
Jeff Laytone9097ab2011-10-19 15:29:40 -0400828 /*
829 * We know that we received enough to get to the MID as we
830 * checked the pdu_length earlier. Now check to see
831 * if the rest of the header is OK. We borrow the length
832 * var for the rest of the loop to avoid a new stack var.
833 *
834 * 48 bytes is enough to display the header and a little bit
835 * into the payload for debugging purposes.
836 */
Steve French373512e2015-12-18 13:05:30 -0600837 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400838 if (length != 0)
839 cifs_dump_mem("Bad SMB: ", buf,
840 min_t(unsigned int, server->total_read, 48));
841
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700842 if (server->ops->is_session_expired &&
843 server->ops->is_session_expired(buf)) {
844 cifs_reconnect(server);
845 wake_up(&server->response_q);
846 return -1;
847 }
848
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700849 if (server->ops->is_status_pending &&
850 server->ops->is_status_pending(buf, server, length))
851 return -1;
852
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500853 if (!mid)
854 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400855
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400856 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500857 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400858}
859
860static int
Al Viro7c97c202011-06-21 08:51:28 -0400861cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000863 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400864 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400865 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000866 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400867 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500868 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000869 struct mid_q_entry *mids[MAX_COMPOUND];
870 char *bufs[MAX_COMPOUND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500873 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400874
875 length = atomic_inc_return(&tcpSesAllocCount);
876 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700877 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700879 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000880 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700881 if (try_to_freeze())
882 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700883
Jeff Layton2a37ef92011-10-19 15:29:23 -0400884 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400885 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700886
Jeff Layton2a37ef92011-10-19 15:29:23 -0400887 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400888 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000889 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000890
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400891 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400892 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000893 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000894
895 if (server->vals->header_preamble_size == 0)
896 server->total_read = 0;
897 else
898 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700899
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400900 /*
901 * The right amount was read from socket - 4 bytes,
902 * so we can now interpret the length field.
903 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400904 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700905
Joe Perchesf96637b2013-05-04 22:12:25 -0500906 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400907 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000908 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000909next_pdu:
910 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700911
Jeff Layton89482a52011-10-19 15:28:57 -0400912 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000913 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100914 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500915 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000916 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400917 cifs_reconnect(server);
918 wake_up(&server->response_q);
919 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700920 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400921
Jeff Layton89482a52011-10-19 15:28:57 -0400922 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100923 length = cifs_read_from_socket(server,
924 buf + server->vals->header_preamble_size,
925 HEADER_SIZE(server) - 1
926 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400927 if (length < 0)
928 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400929 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400930
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000931 if (server->ops->next_header) {
932 next_offset = server->ops->next_header(buf);
933 if (next_offset)
934 server->pdu_size = next_offset;
935 }
936
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000937 memset(mids, 0, sizeof(mids));
938 memset(bufs, 0, sizeof(bufs));
939 num_mids = 0;
940
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800941 if (server->ops->is_transform_hdr &&
942 server->ops->receive_transform &&
943 server->ops->is_transform_hdr(buf)) {
944 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000945 mids,
946 bufs,
947 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800948 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000949 mids[0] = server->ops->find_mid(server, buf);
950 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -0500951 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400952
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000953 if (!mids[0] || !mids[0]->receive)
954 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800955 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000956 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800957 }
Jeff Layton44d22d82011-10-19 15:29:49 -0400958
Lars Persson696e4202018-06-25 14:05:25 +0200959 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000960 for (i = 0; i < num_mids; i++)
961 if (mids[i])
962 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -0700963 continue;
Lars Persson696e4202018-06-25 14:05:25 +0200964 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700965
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400966 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -0400967 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700968
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000969
Steve Frenchfda35942011-01-20 18:06:34 +0000970 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000971
972 for (i = 0; i < num_mids; i++) {
973 if (mids[i] != NULL) {
974 mids[i]->resp_buf_size = server->pdu_size;
975 if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
976 mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
977 server->ops->handle_cancelled_mid)
978 server->ops->handle_cancelled_mid(
979 mids[i]->resp_buf,
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800980 server);
981
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000982 if (!mids[i]->multiRsp || mids[i]->multiEnd)
983 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +0200984
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000985 cifs_mid_q_entry_release(mids[i]);
986 } else if (server->ops->is_oplock_break &&
987 server->ops->is_oplock_break(bufs[i],
988 server)) {
989 cifs_dbg(FYI, "Received oplock break\n");
990 } else {
991 cifs_dbg(VFS, "No task to wake, unknown frame "
992 "received! NumMids %d\n",
993 atomic_read(&midCount));
994 cifs_dump_mem("Received Data is: ", bufs[i],
995 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +0000996#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000997 if (server->ops->dump_detail)
998 server->ops->dump_detail(bufs[i],
999 server);
1000 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001001#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001002 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001003 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001004
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001005 if (pdu_length > server->pdu_size) {
1006 if (!allocate_buffers(server))
1007 continue;
1008 pdu_length -= server->pdu_size;
1009 server->total_read = 0;
1010 server->large_buf = false;
1011 buf = server->smallbuf;
1012 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001013 }
1014 } /* end while !EXITING */
1015
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001016 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001017 cifs_buf_release(server->bigbuf);
1018 if (server->smallbuf) /* no sense logging a debug message if NULL */
1019 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001021 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001022 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001023
1024 /* if server->tsk was NULL then wait for a signal before exiting */
1025 if (!task_to_wake) {
1026 set_current_state(TASK_INTERRUPTIBLE);
1027 while (!signal_pending(current)) {
1028 schedule();
1029 set_current_state(TASK_INTERRUPTIBLE);
1030 }
1031 set_current_state(TASK_RUNNING);
1032 }
1033
Jeff Layton0468a2c2008-12-01 07:09:35 -05001034 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Jeff Laytonc359cf32007-11-16 22:22:06 +00001037/* extract the host portion of the UNC string */
1038static char *
1039extract_hostname(const char *unc)
1040{
1041 const char *src;
1042 char *dst, *delim;
1043 unsigned int len;
1044
1045 /* skip double chars at beginning of string */
1046 /* BB: check validity of these bytes? */
1047 src = unc + 2;
1048
1049 /* delimiter between hostname and sharename is always '\\' now */
1050 delim = strchr(src, '\\');
1051 if (!delim)
1052 return ERR_PTR(-EINVAL);
1053
1054 len = delim - src;
1055 dst = kmalloc((len + 1), GFP_KERNEL);
1056 if (dst == NULL)
1057 return ERR_PTR(-ENOMEM);
1058
1059 memcpy(dst, src, len);
1060 dst[len] = '\0';
1061
1062 return dst;
1063}
1064
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001065static int get_option_ul(substring_t args[], unsigned long *option)
1066{
1067 int rc;
1068 char *string;
1069
1070 string = match_strdup(args);
1071 if (string == NULL)
1072 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001073 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001074 kfree(string);
1075
1076 return rc;
1077}
1078
Eric W. Biederman3da46562013-02-06 01:37:39 -08001079static int get_option_uid(substring_t args[], kuid_t *result)
1080{
1081 unsigned long value;
1082 kuid_t uid;
1083 int rc;
1084
1085 rc = get_option_ul(args, &value);
1086 if (rc)
1087 return rc;
1088
1089 uid = make_kuid(current_user_ns(), value);
1090 if (!uid_valid(uid))
1091 return -EINVAL;
1092
1093 *result = uid;
1094 return 0;
1095}
1096
1097static int get_option_gid(substring_t args[], kgid_t *result)
1098{
1099 unsigned long value;
1100 kgid_t gid;
1101 int rc;
1102
1103 rc = get_option_ul(args, &value);
1104 if (rc)
1105 return rc;
1106
1107 gid = make_kgid(current_user_ns(), value);
1108 if (!gid_valid(gid))
1109 return -EINVAL;
1110
1111 *result = gid;
1112 return 0;
1113}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001114
1115static int cifs_parse_security_flavors(char *value,
1116 struct smb_vol *vol)
1117{
1118
1119 substring_t args[MAX_OPT_ARGS];
1120
Jeff Layton1e3cc572013-06-10 17:12:23 -05001121 /*
1122 * With mount options, the last one should win. Reset any existing
1123 * settings back to default.
1124 */
1125 vol->sectype = Unspecified;
1126 vol->sign = false;
1127
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001128 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001129 case Opt_sec_krb5p:
1130 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1131 return 1;
1132 case Opt_sec_krb5i:
1133 vol->sign = true;
1134 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001135 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001136 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001137 break;
1138 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001139 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001140 /* Fallthrough */
1141 case Opt_sec_ntlmssp:
1142 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001143 break;
1144 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001145 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001146 /* Fallthrough */
1147 case Opt_ntlm:
1148 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001149 break;
1150 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001151 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001152 /* Fallthrough */
1153 case Opt_sec_ntlmv2:
1154 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001155 break;
1156#ifdef CONFIG_CIFS_WEAK_PW_HASH
1157 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001158 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001159 break;
1160#endif
1161 case Opt_sec_none:
1162 vol->nullauth = 1;
1163 break;
1164 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001165 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001166 return 1;
1167 }
1168
1169 return 0;
1170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001173cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1174{
1175 substring_t args[MAX_OPT_ARGS];
1176
1177 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1178 case Opt_cache_loose:
1179 vol->direct_io = false;
1180 vol->strict_io = false;
1181 break;
1182 case Opt_cache_strict:
1183 vol->direct_io = false;
1184 vol->strict_io = true;
1185 break;
1186 case Opt_cache_none:
1187 vol->direct_io = true;
1188 vol->strict_io = false;
1189 break;
1190 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001191 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001192 return 1;
1193 }
1194 return 0;
1195}
1196
1197static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001198cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001199{
1200 substring_t args[MAX_OPT_ARGS];
1201
1202 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001203#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001204 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001205 if (disable_legacy_dialects) {
1206 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1207 return 1;
1208 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001209 if (is_smb3) {
1210 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1211 return 1;
1212 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001213 vol->ops = &smb1_operations;
1214 vol->vals = &smb1_values;
1215 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001216 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001217 if (disable_legacy_dialects) {
1218 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1219 return 1;
1220 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001221 if (is_smb3) {
1222 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1223 return 1;
1224 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001225 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001226 vol->vals = &smb20_values;
1227 break;
Steve French74204512018-06-19 14:34:08 -05001228#else
1229 case Smb_1:
1230 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1231 return 1;
1232 case Smb_20:
1233 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1234 return 1;
1235#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001236 case Smb_21:
1237 vol->ops = &smb21_operations;
1238 vol->vals = &smb21_values;
1239 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001240 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001241 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001242 vol->vals = &smb30_values;
1243 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001244 case Smb_302:
1245 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1246 vol->vals = &smb302_values;
1247 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001248 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001249 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001250 vol->vals = &smb311_values;
1251 break;
Steve French9764c022017-09-17 10:41:35 -05001252 case Smb_3any:
1253 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1254 vol->vals = &smb3any_values;
1255 break;
1256 case Smb_default:
1257 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1258 vol->vals = &smbdefault_values;
1259 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001260 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001261 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001262 return 1;
1263 }
1264 return 0;
1265}
1266
Jeff Laytond387a5c2012-12-10 06:10:46 -05001267/*
1268 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1269 * fields with the result. Returns 0 on success and an error otherwise.
1270 */
1271static int
1272cifs_parse_devname(const char *devname, struct smb_vol *vol)
1273{
1274 char *pos;
1275 const char *delims = "/\\";
1276 size_t len;
1277
1278 /* make sure we have a valid UNC double delimiter prefix */
1279 len = strspn(devname, delims);
1280 if (len != 2)
1281 return -EINVAL;
1282
1283 /* find delimiter between host and sharename */
1284 pos = strpbrk(devname + 2, delims);
1285 if (!pos)
1286 return -EINVAL;
1287
1288 /* skip past delimiter */
1289 ++pos;
1290
1291 /* now go until next delimiter or end of string */
1292 len = strcspn(pos, delims);
1293
1294 /* move "pos" up to delimiter or NULL */
1295 pos += len;
1296 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1297 if (!vol->UNC)
1298 return -ENOMEM;
1299
1300 convert_delimiter(vol->UNC, '\\');
1301
Sachin Prabhu11e31642016-02-08 13:44:01 +05301302 /* skip any delimiter */
1303 if (*pos == '/' || *pos == '\\')
1304 pos++;
1305
1306 /* If pos is NULL then no prepath */
1307 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001308 return 0;
1309
1310 vol->prepath = kstrdup(pos, GFP_KERNEL);
1311 if (!vol->prepath)
1312 return -ENOMEM;
1313
1314 return 0;
1315}
1316
Jeff Layton23db65f2012-05-15 12:20:51 -04001317static int
Sean Finneyb9468452011-04-11 13:19:32 +00001318cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001319 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001321 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001322 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 unsigned int temp_len, i, j;
1324 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001325 short int override_uid = -1;
1326 short int override_gid = -1;
1327 bool uid_specified = false;
1328 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001329 bool sloppy = false;
1330 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001331 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001332 char *string = NULL;
1333 char *tmp_end, *value;
1334 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001335 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001336 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001337 unsigned short port = 0;
1338 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001341 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001342 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Jeff Layton6ee95422012-11-26 11:09:57 -05001344 /* ensure we always start with zeroed-out smb_vol */
1345 memset(vol, 0, sizeof(*vol));
1346
Jeff Layton88463992010-11-22 15:31:03 -05001347 /*
1348 * does not have to be perfect mapping since field is
1349 * informational, only used for servers that do not support
1350 * port 445 and it can be overridden at mount time
1351 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001352 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1353 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001354 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1355
Jeff Layton1397f2e2011-01-07 11:30:28 -05001356 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001357 /* null target name indicates to use *SMBSERVR default called name
1358 if we end up sending RFC1001 session initialize */
1359 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001360 vol->cred_uid = current_uid();
1361 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001362 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001363
Steve French2baa2682014-09-27 02:19:01 -05001364 /*
1365 * default to SFM style remapping of seven reserved characters
1366 * unless user overrides it or we negotiate CIFS POSIX where
1367 * it is unnecessary. Can not simultaneously use more than one mapping
1368 * since then readdir could list files that open could not open
1369 */
1370 vol->remap = true;
1371
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001372 /* default to only allowing write access to owner of the mount */
1373 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001376 /* default is always to request posix paths. */
1377 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001378 /* default to using server inode numbers where available */
1379 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001380
Jeff Layton1b359202012-09-19 15:20:27 -07001381 /* default is to use strict cifs caching semantics */
1382 vol->strict_io = true;
1383
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301384 vol->actimeo = CIFS_DEF_ACTIMEO;
1385
Steve French9764c022017-09-17 10:41:35 -05001386 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1387 vol->ops = &smb30_operations;
1388 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001389
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001390 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1391
Sean Finneyb9468452011-04-11 13:19:32 +00001392 if (!mountdata)
1393 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Sean Finneyb9468452011-04-11 13:19:32 +00001395 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1396 if (!mountdata_copy)
1397 goto cifs_parse_mount_err;
1398
1399 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001400 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001401
Steve French50c2f752007-07-13 00:33:32 +00001402 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001403 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 separator[0] = options[4];
1405 options += 5;
1406 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001407 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001410 vol->backupuid_specified = false; /* no backup intent for a user */
1411 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001412
Jeff Layton37d4f992013-05-24 07:40:05 -04001413 switch (cifs_parse_devname(devname, vol)) {
1414 case 0:
1415 break;
1416 case -ENOMEM:
1417 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1418 goto cifs_parse_mount_err;
1419 case -EINVAL:
1420 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1421 goto cifs_parse_mount_err;
1422 default:
1423 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1424 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001425 }
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001428 substring_t args[MAX_OPT_ARGS];
1429 unsigned long option;
1430 int token;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (!*data)
1433 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001435 token = match_token(data, cifs_mount_option_tokens, args);
1436
1437 switch (token) {
1438
1439 /* Ingnore the following */
1440 case Opt_ignore:
1441 break;
1442
1443 /* Boolean values */
1444 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001446 break;
1447 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001449 break;
1450 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001451 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001452 break;
1453 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001454 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001455 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001456 case Opt_forcegid:
1457 override_gid = 1;
1458 break;
1459 case Opt_noforcegid:
1460 override_gid = 0;
1461 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001462 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001463 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001464 break;
1465 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001466 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001467 break;
1468 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001470 break;
1471 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001473 break;
1474 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001476 break;
1477 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001479 break;
1480 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001481 vol->sfu_remap = true;
1482 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001483 break;
1484 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001485 vol->sfu_remap = false;
1486 break;
1487 case Opt_mapposix:
1488 vol->remap = true;
1489 vol->sfu_remap = false; /* disable SFU mapping */
1490 break;
1491 case Opt_nomapposix:
1492 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001493 break;
1494 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001495 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001496 break;
1497 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001498 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001499 break;
1500 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001501 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001502 break;
1503 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001504 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001505 break;
1506 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001507 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001508 break;
1509 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001510 if (vol->linux_ext)
1511 cifs_dbg(VFS,
1512 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001513 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001514 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001515 case Opt_unix:
1516 if (vol->no_linux_ext)
1517 cifs_dbg(VFS,
1518 "conflicting unix mount options\n");
1519 vol->linux_ext = 1;
1520 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001521 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001522 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001523 break;
1524 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001525 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001526 break;
1527 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001528 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001529 /*
1530 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001531 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001532 * local vfs will do advisory
1533 */
Steve French50c2f752007-07-13 00:33:32 +00001534 if (vol->file_mode ==
1535 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001536 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001537 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001538 case Opt_nohandlecache:
1539 vol->nohandlecache = 1;
1540 break;
1541 case Opt_handlecache:
1542 vol->nohandlecache = 0;
1543 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001544 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001545 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001546 break;
1547 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001549 break;
1550 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001552 break;
Steve French95932652016-09-23 01:36:34 -05001553 case Opt_setuidfromacl:
1554 vol->setuidfromacl = 1;
1555 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001556 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001557 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001558 break;
1559 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001560 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001561 break;
1562 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001564 break;
1565 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001567 break;
1568 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001570 break;
1571 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001573 break;
1574 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001575 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001576 break;
1577 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001578 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001579 break;
1580 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001582 break;
1583 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001585 break;
1586 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001587 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001588 break;
1589 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001590 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001591 break;
1592 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001593 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001594 break;
1595 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001597 break;
1598 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001600 break;
1601 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001602 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001603 break;
1604 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001605 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001606 break;
1607 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001608 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001609 * is a per tree connection (mount) not a per socket
1610 * or per-smb connection option in the protocol
1611 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1612 */
Steve French95b1cb92008-05-15 16:44:38 +00001613 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001614 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001615 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001616 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001617 break;
1618 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301619#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001620 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001621 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301622#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301623 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001624 break;
1625 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001626 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001627 break;
1628 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001629 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001630 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001631 case Opt_sloppy:
1632 sloppy = true;
1633 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001634 case Opt_nosharesock:
1635 vol->nosharesock = true;
1636 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001637 case Opt_nopersistent:
1638 vol->nopersistent = true;
1639 if (vol->persistent) {
1640 cifs_dbg(VFS,
1641 "persistenthandles mount options conflict\n");
1642 goto cifs_parse_mount_err;
1643 }
1644 break;
1645 case Opt_persistent:
1646 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001647 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001648 cifs_dbg(VFS,
1649 "persistenthandles mount options conflict\n");
1650 goto cifs_parse_mount_err;
1651 }
1652 break;
Steve French592fafe2015-11-03 10:08:53 -06001653 case Opt_resilient:
1654 vol->resilient = true;
1655 if (vol->persistent) {
1656 cifs_dbg(VFS,
1657 "persistenthandles mount options conflict\n");
1658 goto cifs_parse_mount_err;
1659 }
1660 break;
1661 case Opt_noresilient:
1662 vol->resilient = false; /* already the default */
1663 break;
Germano Percossi39566442016-12-15 12:31:18 +05301664 case Opt_domainauto:
1665 vol->domainauto = true;
1666 break;
Long Li8339dd32017-11-07 01:54:55 -07001667 case Opt_rdma:
1668 vol->rdma = true;
1669 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001670
1671 /* Numeric Values */
1672 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001673 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001674 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1675 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001676 goto cifs_parse_mount_err;
1677 }
1678 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001679 break;
1680 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001681 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001682 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1683 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001684 goto cifs_parse_mount_err;
1685 }
1686 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001687 break;
1688 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001689 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001690 cifs_dbg(VFS, "%s: Invalid uid value\n",
1691 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001692 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001694 uid_specified = true;
1695 break;
1696 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001697 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001698 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1699 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001700 goto cifs_parse_mount_err;
1701 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001702 break;
1703 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001704 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001705 cifs_dbg(VFS, "%s: Invalid gid value\n",
1706 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001707 goto cifs_parse_mount_err;
1708 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001709 gid_specified = true;
1710 break;
1711 case Opt_file_mode:
1712 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001713 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1714 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001715 goto cifs_parse_mount_err;
1716 }
1717 vol->file_mode = option;
1718 break;
1719 case Opt_dirmode:
1720 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001721 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1722 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001723 goto cifs_parse_mount_err;
1724 }
1725 vol->dir_mode = option;
1726 break;
1727 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001728 if (get_option_ul(args, &option) ||
1729 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001730 cifs_dbg(VFS, "%s: Invalid port value\n",
1731 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001732 goto cifs_parse_mount_err;
1733 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001734 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001735 break;
1736 case Opt_rsize:
1737 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001738 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1739 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001740 goto cifs_parse_mount_err;
1741 }
1742 vol->rsize = option;
1743 break;
1744 case Opt_wsize:
1745 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001746 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1747 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001748 goto cifs_parse_mount_err;
1749 }
1750 vol->wsize = option;
1751 break;
1752 case Opt_actimeo:
1753 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001754 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1755 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001756 goto cifs_parse_mount_err;
1757 }
1758 vol->actimeo = HZ * option;
1759 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001760 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001761 goto cifs_parse_mount_err;
1762 }
1763 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001764 case Opt_echo_interval:
1765 if (get_option_ul(args, &option)) {
1766 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1767 __func__);
1768 goto cifs_parse_mount_err;
1769 }
1770 vol->echo_interval = option;
1771 break;
Steve French8b217fe2016-11-11 22:36:20 -06001772 case Opt_snapshot:
1773 if (get_option_ul(args, &option)) {
1774 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1775 __func__);
1776 goto cifs_parse_mount_err;
1777 }
1778 vol->snapshot_time = option;
1779 break;
Steve French141891f2016-09-23 00:44:16 -05001780 case Opt_max_credits:
1781 if (get_option_ul(args, &option) || (option < 20) ||
1782 (option > 60000)) {
1783 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1784 __func__);
1785 goto cifs_parse_mount_err;
1786 }
1787 vol->max_credits = option;
1788 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001789
1790 /* String Arguments */
1791
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001792 case Opt_blank_user:
1793 /* null user, ie. anonymous authentication */
1794 vol->nullauth = 1;
1795 vol->username = NULL;
1796 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001797 case Opt_user:
1798 string = match_strdup(args);
1799 if (string == NULL)
1800 goto out_nomem;
1801
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001802 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1803 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001804 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001805 goto cifs_parse_mount_err;
1806 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001807
1808 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001809 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001810 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001811 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001812 break;
1813 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001814 /* passwords have to be handled differently
1815 * to allow the character used for deliminator
1816 * to be passed within them
1817 */
1818
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001819 /*
1820 * Check if this is a case where the password
1821 * starts with a delimiter
1822 */
1823 tmp_end = strchr(data, '=');
1824 tmp_end++;
1825 if (!(tmp_end < end && tmp_end[1] == delim)) {
1826 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001827 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001828 vol->password = NULL;
1829 break;
1830 }
Gustavo A. R. Silva07fa6012018-11-27 10:01:51 +11001831 /* Fallthrough - to Opt_pass below.*/
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001832 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001833 /* Obtain the value string */
1834 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001835 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001836
1837 /* Set tmp_end to end of the string */
1838 tmp_end = (char *) value + strlen(value);
1839
1840 /* Check if following character is the deliminator
1841 * If yes, we have encountered a double deliminator
1842 * reset the NULL character to the deliminator
1843 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301844 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001845 tmp_end[0] = delim;
1846
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301847 /* Keep iterating until we get to a single
1848 * deliminator OR the end
1849 */
1850 while ((tmp_end = strchr(tmp_end, delim))
1851 != NULL && (tmp_end[1] == delim)) {
1852 tmp_end = (char *) &tmp_end[2];
1853 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001854
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301855 /* Reset var options to point to next element */
1856 if (tmp_end) {
1857 tmp_end[0] = '\0';
1858 options = (char *) &tmp_end[1];
1859 } else
1860 /* Reached the end of the mount option
1861 * string */
1862 options = end;
1863 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001864
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001865 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001866 /* Now build new password string */
1867 temp_len = strlen(value);
1868 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1869 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001870 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001871 goto cifs_parse_mount_err;
1872 }
1873
1874 for (i = 0, j = 0; i < temp_len; i++, j++) {
1875 vol->password[j] = value[i];
1876 if ((value[i] == delim) &&
1877 value[i+1] == delim)
1878 /* skip the second deliminator */
1879 i++;
1880 }
1881 vol->password[j] = '\0';
1882 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001883 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001884 /* FIXME: should this be an error instead? */
1885 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001886 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001887 case Opt_ip:
1888 string = match_strdup(args);
1889 if (string == NULL)
1890 goto out_nomem;
1891
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001892 if (!cifs_convert_address(dstaddr, string,
1893 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001894 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001895 goto cifs_parse_mount_err;
1896 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001897 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001898 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001899 case Opt_domain:
1900 string = match_strdup(args);
1901 if (string == NULL)
1902 goto out_nomem;
1903
Chen Gang057d6332013-07-19 09:01:36 +08001904 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1905 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001906 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001907 goto cifs_parse_mount_err;
1908 }
1909
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001910 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001911 vol->domainname = kstrdup(string, GFP_KERNEL);
1912 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001913 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001914 goto cifs_parse_mount_err;
1915 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001916 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001917 break;
1918 case Opt_srcaddr:
1919 string = match_strdup(args);
1920 if (string == NULL)
1921 goto out_nomem;
1922
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001923 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001924 (struct sockaddr *)&vol->srcaddr,
1925 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001926 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1927 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001928 goto cifs_parse_mount_err;
1929 }
1930 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001931 case Opt_iocharset:
1932 string = match_strdup(args);
1933 if (string == NULL)
1934 goto out_nomem;
1935
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001936 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001937 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001938 goto cifs_parse_mount_err;
1939 }
1940
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001941 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001942 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001943 vol->iocharset = kstrdup(string,
1944 GFP_KERNEL);
1945 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001946 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001947 goto cifs_parse_mount_err;
1948 }
1949 }
1950 /* if iocharset not set then load_nls_default
1951 * is used by caller
1952 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001953 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001954 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001955 case Opt_netbiosname:
1956 string = match_strdup(args);
1957 if (string == NULL)
1958 goto out_nomem;
1959
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001960 memset(vol->source_rfc1001_name, 0x20,
1961 RFC1001_NAME_LEN);
1962 /*
1963 * FIXME: are there cases in which a comma can
1964 * be valid in workstation netbios name (and
1965 * need special handling)?
1966 */
1967 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1968 /* don't ucase netbiosname for user */
1969 if (string[i] == 0)
1970 break;
1971 vol->source_rfc1001_name[i] = string[i];
1972 }
1973 /* The string has 16th byte zero still from
1974 * set at top of the function
1975 */
1976 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001977 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001978 break;
1979 case Opt_servern:
1980 /* servernetbiosname specified override *SMBSERVER */
1981 string = match_strdup(args);
1982 if (string == NULL)
1983 goto out_nomem;
1984
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001985 /* last byte, type, is 0x20 for servr type */
1986 memset(vol->target_rfc1001_name, 0x20,
1987 RFC1001_NAME_LEN_WITH_NULL);
1988
1989 /* BB are there cases in which a comma can be
1990 valid in this workstation netbios name
1991 (and need special handling)? */
1992
1993 /* user or mount helper must uppercase the
1994 netbios name */
1995 for (i = 0; i < 15; i++) {
1996 if (string[i] == 0)
1997 break;
1998 vol->target_rfc1001_name[i] = string[i];
1999 }
2000 /* The string has 16th byte zero still from
2001 set at top of the function */
2002 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002003 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002004 break;
2005 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002006 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002007 string = match_strdup(args);
2008 if (string == NULL)
2009 goto out_nomem;
2010
Steve French7e682f72017-08-31 21:34:24 -05002011 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002012 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002013 if (strlen(string) > 1) {
2014 pr_warn("Bad mount helper ver=%s. Did "
2015 "you want SMB1 (CIFS) dialect "
2016 "and mean to type vers=1.0 "
2017 "instead?\n", string);
2018 goto cifs_parse_mount_err;
2019 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002020 /* This is the default */
2021 break;
2022 }
2023 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002024 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002025 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002026 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002027 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002028 string = match_strdup(args);
2029 if (string == NULL)
2030 goto out_nomem;
2031
Steve Frenchc7c137b2018-06-06 17:59:29 -05002032 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002033 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002034 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002035 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002036 case Opt_sec:
2037 string = match_strdup(args);
2038 if (string == NULL)
2039 goto out_nomem;
2040
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002041 if (cifs_parse_security_flavors(string, vol) != 0)
2042 goto cifs_parse_mount_err;
2043 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002044 case Opt_cache:
2045 string = match_strdup(args);
2046 if (string == NULL)
2047 goto out_nomem;
2048
2049 if (cifs_parse_cache_flavor(string, vol) != 0)
2050 goto cifs_parse_mount_err;
2051 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002052 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002053 /*
2054 * An option we don't recognize. Save it off for later
2055 * if we haven't already found one
2056 */
2057 if (!invalid)
2058 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002061 /* Free up any allocated string */
2062 kfree(string);
2063 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002065
Jeff Laytond8162552012-03-23 14:40:56 -04002066 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002067 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002068 goto cifs_parse_mount_err;
2069 }
2070
Long Li8339dd32017-11-07 01:54:55 -07002071 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2072 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2073 goto cifs_parse_mount_err;
2074 }
2075
Jeff Layton8a8798a2012-01-17 16:09:15 -05002076#ifndef CONFIG_KEYS
2077 /* Muliuser mounts require CONFIG_KEYS support */
2078 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002079 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002080 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002081 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002082#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002083 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002084 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002085 goto cifs_parse_mount_err;
2086 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002087
Jeff Layton62a1a432012-12-10 06:10:45 -05002088 /* make sure UNC has a share name */
2089 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002090 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002091 goto cifs_parse_mount_err;
2092 }
2093
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002094 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002095 int len;
2096 const char *slash;
2097
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002098 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002099 /* Use the address part of the UNC. */
2100 slash = strchr(&vol->UNC[2], '\\');
2101 len = slash - &vol->UNC[2];
2102 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002103 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002104 goto cifs_parse_mount_err;
2105 }
2106 }
2107
2108 /* set the port that we got earlier */
2109 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002111 if (uid_specified)
2112 vol->override_uid = override_uid;
2113 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002114 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002115
2116 if (gid_specified)
2117 vol->override_gid = override_gid;
2118 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002119 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002120
Steve French7e682f72017-08-31 21:34:24 -05002121 if (got_version == false)
2122 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002123 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002124 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002125 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2126 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002127
Sean Finneyb9468452011-04-11 13:19:32 +00002128 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002130
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002131out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002132 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002133cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002134 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002135 kfree(mountdata_copy);
2136 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Ben Greear3eb9a882010-09-01 17:06:02 -07002139/** Returns true if srcaddr isn't specified and rhs isn't
2140 * specified, or if srcaddr is specified and
2141 * matches the IP address of the rhs argument.
2142 */
Jeff Layton45151482010-07-06 20:43:02 -04002143static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002144srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2145{
2146 switch (srcaddr->sa_family) {
2147 case AF_UNSPEC:
2148 return (rhs->sa_family == AF_UNSPEC);
2149 case AF_INET: {
2150 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2151 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2152 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2153 }
2154 case AF_INET6: {
2155 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002156 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002157 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2158 }
2159 default:
2160 WARN_ON(1);
2161 return false; /* don't expect to be here */
2162 }
2163}
2164
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002165/*
2166 * If no port is specified in addr structure, we try to match with 445 port
2167 * and if it fails - with 139 ports. It should be called only if address
2168 * families of server and addr are equal.
2169 */
2170static bool
2171match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2172{
Steve French6da97912011-03-13 18:55:55 +00002173 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002174
2175 switch (addr->sa_family) {
2176 case AF_INET:
2177 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2178 port = ((struct sockaddr_in *) addr)->sin_port;
2179 break;
2180 case AF_INET6:
2181 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2182 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2183 break;
2184 default:
2185 WARN_ON(1);
2186 return false;
2187 }
2188
2189 if (!port) {
2190 port = htons(CIFS_PORT);
2191 if (port == *sport)
2192 return true;
2193
2194 port = htons(RFC1001_PORT);
2195 }
2196
2197 return port == *sport;
2198}
Ben Greear3eb9a882010-09-01 17:06:02 -07002199
2200static bool
2201match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2202 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Jeff Layton45151482010-07-06 20:43:02 -04002204 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002205 case AF_INET: {
2206 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2207 struct sockaddr_in *srv_addr4 =
2208 (struct sockaddr_in *)&server->dstaddr;
2209
2210 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002211 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002212 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002213 }
2214 case AF_INET6: {
2215 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2216 struct sockaddr_in6 *srv_addr6 =
2217 (struct sockaddr_in6 *)&server->dstaddr;
2218
Jeff Layton45151482010-07-06 20:43:02 -04002219 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002220 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002221 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002222 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002223 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002224 break;
2225 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002226 default:
2227 WARN_ON(1);
2228 return false; /* don't expect to be here */
2229 }
Jeff Layton45151482010-07-06 20:43:02 -04002230
Ben Greear3eb9a882010-09-01 17:06:02 -07002231 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2232 return false;
2233
Jeff Layton45151482010-07-06 20:43:02 -04002234 return true;
2235}
2236
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002237static bool
2238match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2239{
Jeff Layton3f618222013-06-12 19:52:14 -05002240 /*
2241 * The select_sectype function should either return the vol->sectype
2242 * that was specified, or "Unspecified" if that sectype was not
2243 * compatible with the given NEGOTIATE request.
2244 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302245 if (server->ops->select_sectype(server, vol->sectype)
2246 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002247 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002248
Jeff Layton3f618222013-06-12 19:52:14 -05002249 /*
2250 * Now check if signing mode is acceptable. No need to check
2251 * global_secflags at this point since if MUST_SIGN is set then
2252 * the server->sign had better be too.
2253 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002254 if (vol->sign && !server->sign)
2255 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002256
2257 return true;
2258}
2259
Jeff Layton9fa114f2012-11-26 11:09:57 -05002260static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002261{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002262 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2263
Jeff Laytona0b3df52013-05-24 07:40:59 -04002264 if (vol->nosharesock)
2265 return 0;
2266
Steve French9764c022017-09-17 10:41:35 -05002267 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002268 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2269 return 0;
2270
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002271 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2272 return 0;
2273
2274 if (!match_address(server, addr,
2275 (struct sockaddr *)&vol->srcaddr))
2276 return 0;
2277
2278 if (!match_port(server, addr))
2279 return 0;
2280
2281 if (!match_security(server, vol))
2282 return 0;
2283
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002284 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002285 return 0;
2286
Long Li8339dd32017-11-07 01:54:55 -07002287 if (server->rdma != vol->rdma)
2288 return 0;
2289
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002290 return 1;
2291}
2292
Jeff Layton45151482010-07-06 20:43:02 -04002293static struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002294cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002295{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002296 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302298 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002299 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002300 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002301 continue;
2302
Jeff Laytone7ddee92008-11-14 13:44:38 -05002303 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302304 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002305 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002306 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302308 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return NULL;
2310}
2311
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002312void
2313cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002315 struct task_struct *task;
2316
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302317 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002318 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302319 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002320 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002322
Rob Landleyf1d0c992011-01-22 15:44:05 -06002323 put_net(cifs_net_ns(server));
2324
Jeff Laytone7ddee92008-11-14 13:44:38 -05002325 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302326 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002327
Jeff Laytonc74093b2011-01-11 07:24:23 -05002328 cancel_delayed_work_sync(&server->echo);
2329
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002330 if (from_reconnect)
2331 /*
2332 * Avoid deadlock here: reconnect work calls
2333 * cifs_put_tcp_session() at its end. Need to be sure
2334 * that reconnect work does nothing with server pointer after
2335 * that step.
2336 */
2337 cancel_delayed_work(&server->reconnect);
2338 else
2339 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002340
Jeff Laytone7ddee92008-11-14 13:44:38 -05002341 spin_lock(&GlobalMid_Lock);
2342 server->tcpStatus = CifsExiting;
2343 spin_unlock(&GlobalMid_Lock);
2344
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002345 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302346 cifs_fscache_release_client_cookie(server);
2347
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002348 kfree(server->session_key.response);
2349 server->session_key.response = NULL;
2350 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002351
2352 task = xchg(&server->tsk, NULL);
2353 if (task)
2354 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355}
2356
Jeff Layton63c038c2008-12-01 18:41:46 -05002357static struct TCP_Server_Info *
2358cifs_get_tcp_session(struct smb_vol *volume_info)
2359{
2360 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002361 int rc;
2362
Joe Perchesf96637b2013-05-04 22:12:25 -05002363 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002364
2365 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002366 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002367 if (tcp_ses)
2368 return tcp_ses;
2369
2370 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2371 if (!tcp_ses) {
2372 rc = -ENOMEM;
2373 goto out_err;
2374 }
2375
Jeff Layton23db65f2012-05-15 12:20:51 -04002376 tcp_ses->ops = volume_info->ops;
2377 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002378 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002379 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2380 if (IS_ERR(tcp_ses->hostname)) {
2381 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002382 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002383 }
2384
2385 tcp_ses->noblocksnd = volume_info->noblocksnd;
2386 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002387 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002388 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002389 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002390 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002391 init_waitqueue_head(&tcp_ses->response_q);
2392 init_waitqueue_head(&tcp_ses->request_q);
2393 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2394 mutex_init(&tcp_ses->srv_mutex);
2395 memcpy(tcp_ses->workstation_RFC1001_name,
2396 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2397 memcpy(tcp_ses->server_RFC1001_name,
2398 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002399 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002400 tcp_ses->sequence_number = 0;
Steve French9e1a37d2018-09-19 02:38:17 -05002401 tcp_ses->reconnect_instance = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002402 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002403 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002404 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2405 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002406 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002407 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2408 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002409 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2410 sizeof(tcp_ses->srcaddr));
2411 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2412 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002413 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002414 /*
2415 * at this point we are the only ones with the pointer
2416 * to the struct since the kernel thread not created yet
2417 * no need to spinlock this init of tcpStatus or srv_count
2418 */
2419 tcp_ses->tcpStatus = CifsNew;
2420 ++tcp_ses->srv_count;
2421
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002422 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2423 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2424 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2425 else
2426 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002427 if (tcp_ses->rdma) {
2428#ifndef CONFIG_CIFS_SMB_DIRECT
2429 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2430 rc = -ENOENT;
2431 goto out_err_crypto_release;
2432#endif
2433 tcp_ses->smbd_conn = smbd_get_connection(
2434 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2435 if (tcp_ses->smbd_conn) {
2436 cifs_dbg(VFS, "RDMA transport established\n");
2437 rc = 0;
2438 goto smbd_connected;
2439 } else {
2440 rc = -ENOENT;
2441 goto out_err_crypto_release;
2442 }
2443 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002444 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002445 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002446 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002447 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002448 }
Long Li2f894642017-11-22 17:38:34 -07002449smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002450 /*
2451 * since we're in a cifs function already, we know that
2452 * this will succeed. No need for try_module_get().
2453 */
2454 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002455 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002456 tcp_ses, "cifsd");
2457 if (IS_ERR(tcp_ses->tsk)) {
2458 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002459 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002460 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002461 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002462 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002463 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002464
2465 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302466 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002467 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302468 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002469
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302470 cifs_fscache_get_client_cookie(tcp_ses);
2471
Jeff Laytonc74093b2011-01-11 07:24:23 -05002472 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002473 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002474
Jeff Layton63c038c2008-12-01 18:41:46 -05002475 return tcp_ses;
2476
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002477out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002478 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002479
Rob Landleyf1d0c992011-01-22 15:44:05 -06002480 put_net(cifs_net_ns(tcp_ses));
2481
Jeff Layton63c038c2008-12-01 18:41:46 -05002482out_err:
2483 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002484 if (!IS_ERR(tcp_ses->hostname))
2485 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002486 if (tcp_ses->ssocket)
2487 sock_release(tcp_ses->ssocket);
2488 kfree(tcp_ses);
2489 }
2490 return ERR_PTR(rc);
2491}
2492
Steve French96daf2b2011-05-27 04:34:02 +00002493static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002494{
Jeff Layton3f618222013-06-12 19:52:14 -05002495 if (vol->sectype != Unspecified &&
2496 vol->sectype != ses->sectype)
2497 return 0;
2498
2499 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002500 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002501 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002502 return 0;
2503 break;
2504 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002505 /* NULL username means anonymous session */
2506 if (ses->user_name == NULL) {
2507 if (!vol->nullauth)
2508 return 0;
2509 break;
2510 }
2511
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002512 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002513 if (strncmp(ses->user_name,
2514 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002515 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002516 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002517 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002518 ses->password != NULL &&
2519 strncmp(ses->password,
2520 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002521 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002522 return 0;
2523 }
2524 return 1;
2525}
2526
Aurelien Aptelb327a712018-01-24 13:46:10 +01002527/**
2528 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2529 *
2530 * A new IPC connection is made and stored in the session
2531 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2532 */
2533static int
2534cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2535{
2536 int rc = 0, xid;
2537 struct cifs_tcon *tcon;
2538 struct nls_table *nls_codepage;
2539 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2540 bool seal = false;
2541
2542 /*
2543 * If the mount request that resulted in the creation of the
2544 * session requires encryption, force IPC to be encrypted too.
2545 */
2546 if (volume_info->seal) {
2547 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2548 seal = true;
2549 else {
2550 cifs_dbg(VFS,
2551 "IPC: server doesn't support encryption\n");
2552 return -EOPNOTSUPP;
2553 }
2554 }
2555
2556 tcon = tconInfoAlloc();
2557 if (tcon == NULL)
2558 return -ENOMEM;
2559
Thomas Werschlein395a2072018-08-30 18:29:20 +02002560 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002561
2562 /* cannot fail */
2563 nls_codepage = load_nls_default();
2564
2565 xid = get_xid();
2566 tcon->ses = ses;
2567 tcon->ipc = true;
2568 tcon->seal = seal;
2569 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2570 free_xid(xid);
2571
2572 if (rc) {
2573 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2574 tconInfoFree(tcon);
2575 goto out;
2576 }
2577
2578 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2579
2580 ses->tcon_ipc = tcon;
2581out:
2582 unload_nls(nls_codepage);
2583 return rc;
2584}
2585
2586/**
2587 * cifs_free_ipc - helper to release the session IPC tcon
2588 *
2589 * Needs to be called everytime a session is destroyed
2590 */
2591static int
2592cifs_free_ipc(struct cifs_ses *ses)
2593{
2594 int rc = 0, xid;
2595 struct cifs_tcon *tcon = ses->tcon_ipc;
2596
2597 if (tcon == NULL)
2598 return 0;
2599
2600 if (ses->server->ops->tree_disconnect) {
2601 xid = get_xid();
2602 rc = ses->server->ops->tree_disconnect(xid, tcon);
2603 free_xid(xid);
2604 }
2605
2606 if (rc)
2607 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2608
2609 tconInfoFree(tcon);
2610 ses->tcon_ipc = NULL;
2611 return rc;
2612}
2613
Steve French96daf2b2011-05-27 04:34:02 +00002614static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002615cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
Steve French96daf2b2011-05-27 04:34:02 +00002617 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302619 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002620 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002621 if (ses->status == CifsExiting)
2622 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002623 if (!match_session(ses, vol))
2624 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002625 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302626 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002627 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302629 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return NULL;
2631}
2632
Jeff Layton14fbf502008-11-14 13:53:46 -05002633static void
Steve French96daf2b2011-05-27 04:34:02 +00002634cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002635{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002636 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002637 struct TCP_Server_Info *server = ses->server;
2638
Joe Perchesf96637b2013-05-04 22:12:25 -05002639 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002640
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302641 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002642 if (ses->status == CifsExiting) {
2643 spin_unlock(&cifs_tcp_ses_lock);
2644 return;
2645 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002646 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302647 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002648 return;
2649 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002650 if (ses->status == CifsGood)
2651 ses->status = CifsExiting;
2652 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002653
Aurelien Aptelb327a712018-01-24 13:46:10 +01002654 cifs_free_ipc(ses);
2655
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002656 if (ses->status == CifsExiting && server->ops->logoff) {
2657 xid = get_xid();
2658 rc = server->ops->logoff(xid, ses);
2659 if (rc)
2660 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2661 __func__, rc);
2662 _free_xid(xid);
2663 }
2664
2665 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002666 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302667 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002668
Jeff Layton14fbf502008-11-14 13:53:46 -05002669 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002670 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002671}
2672
Jeff Layton8a8798a2012-01-17 16:09:15 -05002673#ifdef CONFIG_KEYS
2674
Chen Gang057d6332013-07-19 09:01:36 +08002675/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2676#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002677
2678/* Populate username and pw fields from keyring if possible */
2679static int
2680cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2681{
2682 int rc = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002683 const char *delim, *payload;
2684 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002685 ssize_t len;
2686 struct key *key;
2687 struct TCP_Server_Info *server = ses->server;
2688 struct sockaddr_in *sa;
2689 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002690 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002691
2692 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2693 if (!desc)
2694 return -ENOMEM;
2695
2696 /* try to find an address key first */
2697 switch (server->dstaddr.ss_family) {
2698 case AF_INET:
2699 sa = (struct sockaddr_in *)&server->dstaddr;
2700 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2701 break;
2702 case AF_INET6:
2703 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2704 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2705 break;
2706 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002707 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2708 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002709 rc = -EINVAL;
2710 goto out_err;
2711 }
2712
Joe Perchesf96637b2013-05-04 22:12:25 -05002713 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002714 key = request_key(&key_type_logon, desc, "");
2715 if (IS_ERR(key)) {
2716 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002717 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002718 rc = PTR_ERR(key);
2719 goto out_err;
2720 }
2721
2722 /* didn't work, try to find a domain key */
2723 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002724 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002725 key = request_key(&key_type_logon, desc, "");
2726 if (IS_ERR(key)) {
2727 rc = PTR_ERR(key);
2728 goto out_err;
2729 }
2730 }
2731
2732 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002733 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002734 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002735 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002736 goto out_key_put;
2737 }
2738
2739 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002740 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002741 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002742 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002743 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002744 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2745 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002746 rc = -EINVAL;
2747 goto out_key_put;
2748 }
2749
2750 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002751 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002752 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2753 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002754 rc = -EINVAL;
2755 goto out_key_put;
2756 }
2757
2758 vol->username = kstrndup(payload, len, GFP_KERNEL);
2759 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002760 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2761 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002762 rc = -ENOMEM;
2763 goto out_key_put;
2764 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002765 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002766
2767 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002768 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002769 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002770 rc = -EINVAL;
2771 kfree(vol->username);
2772 vol->username = NULL;
2773 goto out_key_put;
2774 }
2775
2776 ++delim;
2777 vol->password = kstrndup(delim, len, GFP_KERNEL);
2778 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002779 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2780 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002781 rc = -ENOMEM;
2782 kfree(vol->username);
2783 vol->username = NULL;
2784 goto out_key_put;
2785 }
2786
2787out_key_put:
2788 up_read(&key->sem);
2789 key_put(key);
2790out_err:
2791 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002792 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002793 return rc;
2794}
2795#else /* ! CONFIG_KEYS */
2796static inline int
2797cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2798 struct cifs_ses *ses __attribute__((unused)))
2799{
2800 return -ENOSYS;
2801}
2802#endif /* CONFIG_KEYS */
2803
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002804/**
2805 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2806 *
2807 * This function assumes it is being called from cifs_mount() where we
2808 * already got a server reference (server refcount +1). See
2809 * cifs_get_tcon() for refcount explanations.
2810 */
Steve French96daf2b2011-05-27 04:34:02 +00002811static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002812cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2813{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002814 int rc = -ENOMEM;
2815 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002816 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002817 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2818 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002819
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002820 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002821
Jeff Layton4ff67b72010-07-06 20:43:02 -04002822 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002823 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002824 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2825 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002826
Jeff Layton36988c72010-04-24 07:57:43 -04002827 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002828 rc = cifs_negotiate_protocol(xid, ses);
2829 if (rc) {
2830 mutex_unlock(&ses->session_mutex);
2831 /* problem -- put our ses reference */
2832 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002833 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002834 return ERR_PTR(rc);
2835 }
Jeff Layton36988c72010-04-24 07:57:43 -04002836 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002837 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002838 rc = cifs_setup_session(xid, ses,
2839 volume_info->local_nls);
2840 if (rc) {
2841 mutex_unlock(&ses->session_mutex);
2842 /* problem -- put our reference */
2843 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002844 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002845 return ERR_PTR(rc);
2846 }
2847 }
2848 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002849
2850 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002851 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002852 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002853 return ses;
2854 }
2855
Joe Perchesf96637b2013-05-04 22:12:25 -05002856 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002857 ses = sesInfoAlloc();
2858 if (ses == NULL)
2859 goto get_ses_fail;
2860
2861 /* new SMB session uses our server ref */
2862 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002863 if (server->dstaddr.ss_family == AF_INET6)
2864 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002865 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002866 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002867
Steve French8727c8a2011-02-25 01:11:56 -06002868 if (volume_info->username) {
2869 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2870 if (!ses->user_name)
2871 goto get_ses_fail;
2872 }
Jeff Layton36988c72010-04-24 07:57:43 -04002873
2874 /* volume_info->password freed at unmount */
2875 if (volume_info->password) {
2876 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2877 if (!ses->password)
2878 goto get_ses_fail;
2879 }
2880 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002881 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2882 if (!ses->domainName)
2883 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002884 }
Germano Percossi39566442016-12-15 12:31:18 +05302885 if (volume_info->domainauto)
2886 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002887 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002888 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002889
Jeff Layton28e11bd2013-05-26 07:01:00 -04002890 ses->sectype = volume_info->sectype;
2891 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002892
2893 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002894 rc = cifs_negotiate_protocol(xid, ses);
2895 if (!rc)
2896 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002897 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002898 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002899 goto get_ses_fail;
2900
2901 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302902 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002903 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302904 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002905
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002906 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002907
2908 cifs_setup_ipc(ses, volume_info);
2909
Jeff Layton36988c72010-04-24 07:57:43 -04002910 return ses;
2911
2912get_ses_fail:
2913 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002914 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002915 return ERR_PTR(rc);
2916}
2917
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002918static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002919{
2920 if (tcon->tidStatus == CifsExiting)
2921 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002922 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002923 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002924 if (tcon->seal != volume_info->seal)
2925 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002926 if (tcon->snapshot_time != volume_info->snapshot_time)
2927 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002928 return 1;
2929}
2930
Steve French96daf2b2011-05-27 04:34:02 +00002931static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06002932cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
2934 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002935 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302937 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002938 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002939 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002940 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002941 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002942 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302943 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return tcon;
2945 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302946 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return NULL;
2948}
2949
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002950void
Steve French96daf2b2011-05-27 04:34:02 +00002951cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002952{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002953 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002954 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002955
Aurelien Aptelb327a712018-01-24 13:46:10 +01002956 /*
2957 * IPC tcon share the lifetime of their session and are
2958 * destroyed in the session put function
2959 */
2960 if (tcon == NULL || tcon->ipc)
2961 return;
2962
2963 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002964 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302965 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002966 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302967 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002968 return;
2969 }
2970
2971 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302972 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002973
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002974 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002975 if (ses->server->ops->tree_disconnect)
2976 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002977 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002978
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302979 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002980 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002981 cifs_put_smb_ses(ses);
2982}
2983
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002984/**
2985 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
2986 *
2987 * - tcon refcount is the number of mount points using the tcon.
2988 * - ses refcount is the number of tcon using the session.
2989 *
2990 * 1. This function assumes it is being called from cifs_mount() where
2991 * we already got a session reference (ses refcount +1).
2992 *
2993 * 2. Since we're in the context of adding a mount point, the end
2994 * result should be either:
2995 *
2996 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2997 * its session refcount incremented (1 new tcon). This +1 was
2998 * already done in (1).
2999 *
3000 * b) an existing tcon with refcount+1 (add a mount point to it) and
3001 * identical ses refcount (no new tcon). Because of (1) we need to
3002 * decrement the ses refcount.
3003 */
Steve French96daf2b2011-05-27 04:34:02 +00003004static struct cifs_tcon *
3005cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003006{
3007 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003008 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003009
Steve French8b217fe2016-11-11 22:36:20 -06003010 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003011 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003012 /*
3013 * tcon has refcount already incremented but we need to
3014 * decrement extra ses reference gotten by caller (case b)
3015 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003016 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003017 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003018 return tcon;
3019 }
3020
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003021 if (!ses->server->ops->tree_connect) {
3022 rc = -ENOSYS;
3023 goto out_fail;
3024 }
3025
Jeff Laytond00c28d2010-04-24 07:57:44 -04003026 tcon = tconInfoAlloc();
3027 if (tcon == NULL) {
3028 rc = -ENOMEM;
3029 goto out_fail;
3030 }
3031
Steve French8b217fe2016-11-11 22:36:20 -06003032 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003033 if (ses->server->vals->protocol_id == 0) {
3034 cifs_dbg(VFS,
3035 "Use SMB2 or later for snapshot mount option\n");
3036 rc = -EOPNOTSUPP;
3037 goto out_fail;
3038 } else
3039 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003040 }
3041
Jeff Laytond00c28d2010-04-24 07:57:44 -04003042 tcon->ses = ses;
3043 if (volume_info->password) {
3044 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3045 if (!tcon->password) {
3046 rc = -ENOMEM;
3047 goto out_fail;
3048 }
3049 }
3050
Steve French23657ad2018-04-22 15:14:58 -05003051 if (volume_info->seal) {
3052 if (ses->server->vals->protocol_id == 0) {
3053 cifs_dbg(VFS,
3054 "SMB3 or later required for encryption\n");
3055 rc = -EOPNOTSUPP;
3056 goto out_fail;
3057 } else if (tcon->ses->server->capabilities &
3058 SMB2_GLOBAL_CAP_ENCRYPTION)
3059 tcon->seal = true;
3060 else {
3061 cifs_dbg(VFS, "Encryption is not supported on share\n");
3062 rc = -EOPNOTSUPP;
3063 goto out_fail;
3064 }
3065 }
3066
Steve French8505c8b2018-06-18 14:01:59 -05003067 if (volume_info->linux_ext) {
3068 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003069 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003070 printk_once(KERN_WARNING
3071 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003072 } else {
3073 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3074 rc = -EOPNOTSUPP;
3075 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003076 }
Steve Frenchb3266142018-05-20 23:41:10 -05003077 }
Steve Frenchb3266142018-05-20 23:41:10 -05003078
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003079 /*
3080 * BB Do we need to wrap session_mutex around this TCon call and Unix
3081 * SetFS as we do on SessSetup and reconnect?
3082 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003083 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003084 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3085 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003086 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003087 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003088 if (rc)
3089 goto out_fail;
3090
Steve Frenchb618f002015-11-03 09:15:03 -06003091 tcon->use_persistent = false;
3092 /* check if SMB2 or later, CIFS does not support persistent handles */
3093 if (volume_info->persistent) {
3094 if (ses->server->vals->protocol_id == 0) {
3095 cifs_dbg(VFS,
3096 "SMB3 or later required for persistent handles\n");
3097 rc = -EOPNOTSUPP;
3098 goto out_fail;
3099 } else if (ses->server->capabilities &
3100 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3101 tcon->use_persistent = true;
3102 else /* persistent handles requested but not supported */ {
3103 cifs_dbg(VFS,
3104 "Persistent handles not supported on share\n");
3105 rc = -EOPNOTSUPP;
3106 goto out_fail;
3107 }
3108 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3109 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3110 && (volume_info->nopersistent == false)) {
3111 cifs_dbg(FYI, "enabling persistent handles\n");
3112 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003113 } else if (volume_info->resilient) {
3114 if (ses->server->vals->protocol_id == 0) {
3115 cifs_dbg(VFS,
3116 "SMB2.1 or later required for resilient handles\n");
3117 rc = -EOPNOTSUPP;
3118 goto out_fail;
3119 }
3120 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003121 }
3122
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003123 /*
3124 * We can have only one retry value for a connection to a share so for
3125 * resources mounted more than once to the same server share the last
3126 * value passed in for the retry flag is used.
3127 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003128 tcon->retry = volume_info->retry;
3129 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003130 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003131 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003132 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003133
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303134 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003135 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303136 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003137
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303138 cifs_fscache_get_super_cookie(tcon);
3139
Jeff Laytond00c28d2010-04-24 07:57:44 -04003140 return tcon;
3141
3142out_fail:
3143 tconInfoFree(tcon);
3144 return ERR_PTR(rc);
3145}
3146
Jeff Layton9d002df2010-10-06 19:51:11 -04003147void
3148cifs_put_tlink(struct tcon_link *tlink)
3149{
3150 if (!tlink || IS_ERR(tlink))
3151 return;
3152
3153 if (!atomic_dec_and_test(&tlink->tl_count) ||
3154 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3155 tlink->tl_time = jiffies;
3156 return;
3157 }
3158
3159 if (!IS_ERR(tlink_tcon(tlink)))
3160 cifs_put_tcon(tlink_tcon(tlink));
3161 kfree(tlink);
3162 return;
3163}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003164
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003165static int
3166compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3167{
3168 struct cifs_sb_info *old = CIFS_SB(sb);
3169 struct cifs_sb_info *new = mnt_data->cifs_sb;
3170
3171 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3172 return 0;
3173
3174 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3175 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3176 return 0;
3177
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003178 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003179 * We want to share sb only if we don't specify an r/wsize or
3180 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003181 */
3182 if (new->wsize && new->wsize < old->wsize)
3183 return 0;
3184
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003185 if (new->rsize && new->rsize < old->rsize)
3186 return 0;
3187
Eric W. Biederman1f682332013-02-06 01:20:20 -08003188 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003189 return 0;
3190
3191 if (old->mnt_file_mode != new->mnt_file_mode ||
3192 old->mnt_dir_mode != new->mnt_dir_mode)
3193 return 0;
3194
3195 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3196 return 0;
3197
3198 if (old->actimeo != new->actimeo)
3199 return 0;
3200
3201 return 1;
3202}
3203
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003204static int
3205match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3206{
3207 struct cifs_sb_info *old = CIFS_SB(sb);
3208 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003209 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3210 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003211
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003212 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003213 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003214 else if (!old_set && !new_set)
3215 return 1;
3216
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003217 return 0;
3218}
3219
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003220int
3221cifs_match_super(struct super_block *sb, void *data)
3222{
3223 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3224 struct smb_vol *volume_info;
3225 struct cifs_sb_info *cifs_sb;
3226 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003227 struct cifs_ses *ses;
3228 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003229 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003230 int rc = 0;
3231
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003232 spin_lock(&cifs_tcp_ses_lock);
3233 cifs_sb = CIFS_SB(sb);
3234 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3235 if (IS_ERR(tlink)) {
3236 spin_unlock(&cifs_tcp_ses_lock);
3237 return rc;
3238 }
3239 tcon = tlink_tcon(tlink);
3240 ses = tcon->ses;
3241 tcp_srv = ses->server;
3242
3243 volume_info = mnt_data->vol;
3244
Jeff Layton9fa114f2012-11-26 11:09:57 -05003245 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003246 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003247 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003248 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003249 rc = 0;
3250 goto out;
3251 }
3252
3253 rc = compare_mount_options(sb, mnt_data);
3254out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003255 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003256 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003257 return rc;
3258}
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003261get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003262 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3263 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 int rc = 0;
3266
Aurelien Aptelb327a712018-01-24 13:46:10 +01003267 if (!ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003268 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003270 *num_referrals = 0;
3271 *referrals = NULL;
3272
Aurelien Aptelb327a712018-01-24 13:46:10 +01003273 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3274 referrals, num_referrals,
3275 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return rc;
3277}
3278
Jeff Layton09e50d52008-07-23 10:11:19 -04003279#ifdef CONFIG_DEBUG_LOCK_ALLOC
3280static struct lock_class_key cifs_key[2];
3281static struct lock_class_key cifs_slock_key[2];
3282
3283static inline void
3284cifs_reclassify_socket4(struct socket *sock)
3285{
3286 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003287 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003288 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3289 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3290}
3291
3292static inline void
3293cifs_reclassify_socket6(struct socket *sock)
3294{
3295 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003296 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003297 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3298 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3299}
3300#else
3301static inline void
3302cifs_reclassify_socket4(struct socket *sock)
3303{
3304}
3305
3306static inline void
3307cifs_reclassify_socket6(struct socket *sock)
3308{
3309}
3310#endif
3311
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003313static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
Steve French50c2f752007-07-13 00:33:32 +00003315 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Steve French50c2f752007-07-13 00:33:32 +00003317 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 /* mask a nibble at a time and encode */
3319 target[j] = 'A' + (0x0F & (source[i] >> 4));
3320 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003321 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
3323
3324}
3325
Ben Greear3eb9a882010-09-01 17:06:02 -07003326static int
3327bind_socket(struct TCP_Server_Info *server)
3328{
3329 int rc = 0;
3330 if (server->srcaddr.ss_family != AF_UNSPEC) {
3331 /* Bind to the specified local IP address */
3332 struct socket *socket = server->ssocket;
3333 rc = socket->ops->bind(socket,
3334 (struct sockaddr *) &server->srcaddr,
3335 sizeof(server->srcaddr));
3336 if (rc < 0) {
3337 struct sockaddr_in *saddr4;
3338 struct sockaddr_in6 *saddr6;
3339 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3340 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3341 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003342 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3343 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003344 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003345 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3346 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003347 }
3348 }
3349 return rc;
3350}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
3352static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003353ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
3355 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003356 /*
3357 * some servers require RFC1001 sessinit before sending
3358 * negprot - BB check reconnection in case where second
3359 * sessinit is sent but no second negprot
3360 */
3361 struct rfc1002_session_packet *ses_init_buf;
3362 struct smb_hdr *smb_buf;
3363 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3364 GFP_KERNEL);
3365 if (ses_init_buf) {
3366 ses_init_buf->trailer.session_req.called_len = 32;
3367
Colin Ian King997152f2016-01-25 16:25:54 +00003368 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003369 rfc1002mangle(ses_init_buf->trailer.
3370 session_req.called_name,
3371 server->server_RFC1001_name,
3372 RFC1001_NAME_LEN_WITH_NULL);
3373 else
3374 rfc1002mangle(ses_init_buf->trailer.
3375 session_req.called_name,
3376 DEFAULT_CIFS_CALLED_NAME,
3377 RFC1001_NAME_LEN_WITH_NULL);
3378
3379 ses_init_buf->trailer.session_req.calling_len = 32;
3380
3381 /*
3382 * calling name ends in null (byte 16) from old smb
3383 * convention.
3384 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003385 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003386 rfc1002mangle(ses_init_buf->trailer.
3387 session_req.calling_name,
3388 server->workstation_RFC1001_name,
3389 RFC1001_NAME_LEN_WITH_NULL);
3390 else
3391 rfc1002mangle(ses_init_buf->trailer.
3392 session_req.calling_name,
3393 "LINUX_CIFS_CLNT",
3394 RFC1001_NAME_LEN_WITH_NULL);
3395
3396 ses_init_buf->trailer.session_req.scope1 = 0;
3397 ses_init_buf->trailer.session_req.scope2 = 0;
3398 smb_buf = (struct smb_hdr *)ses_init_buf;
3399
3400 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003401 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003402 rc = smb_send(server, smb_buf, 0x44);
3403 kfree(ses_init_buf);
3404 /*
3405 * RFC1001 layer in at least one server
3406 * requires very short break before negprot
3407 * presumably because not expecting negprot
3408 * to follow so fast. This is a simple
3409 * solution that works without
3410 * complicating the code and causes no
3411 * significant slowing down on mount
3412 * for everyone else
3413 */
3414 usleep_range(1000, 2000);
3415 }
3416 /*
3417 * else the negprot may still work without this
3418 * even though malloc failed
3419 */
3420
3421 return rc;
3422}
3423
3424static int
3425generic_ip_connect(struct TCP_Server_Info *server)
3426{
3427 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003428 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003429 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003430 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003431 struct sockaddr *saddr;
3432
3433 saddr = (struct sockaddr *) &server->dstaddr;
3434
3435 if (server->dstaddr.ss_family == AF_INET6) {
3436 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3437 slen = sizeof(struct sockaddr_in6);
3438 sfamily = AF_INET6;
3439 } else {
3440 sport = ((struct sockaddr_in *) saddr)->sin_port;
3441 slen = sizeof(struct sockaddr_in);
3442 sfamily = AF_INET;
3443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003445 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003446 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3447 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003449 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003450 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003453
3454 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003455 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003456 server->ssocket = socket;
3457 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003458 if (sfamily == AF_INET6)
3459 cifs_reclassify_socket6(socket);
3460 else
3461 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 }
3463
Ben Greear3eb9a882010-09-01 17:06:02 -07003464 rc = bind_socket(server);
3465 if (rc < 0)
3466 return rc;
3467
Jeff Laytond5c56052008-12-01 18:42:33 -05003468 /*
3469 * Eventually check for other socket options to change from
3470 * the default. sock_setsockopt not used because it expects
3471 * user space buffer
3472 */
3473 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003474 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003475
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003476 /* make the bufsizes depend on wsize/rsize and max requests */
3477 if (server->noautotune) {
3478 if (socket->sk->sk_sndbuf < (200 * 1024))
3479 socket->sk->sk_sndbuf = 200 * 1024;
3480 if (socket->sk->sk_rcvbuf < (140 * 1024))
3481 socket->sk->sk_rcvbuf = 140 * 1024;
3482 }
3483
Steve French6a5fa2362010-01-01 01:28:43 +00003484 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003485 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003486 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3487 (char *)&val, sizeof(val));
3488 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003489 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3490 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003491 }
3492
Joe Perchesf96637b2013-05-04 22:12:25 -05003493 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003494 socket->sk->sk_sndbuf,
3495 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3496
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003497 rc = socket->ops->connect(socket, saddr, slen, 0);
3498 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003499 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003500 sock_release(socket);
3501 server->ssocket = NULL;
3502 return rc;
3503 }
3504
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003505 if (sport == htons(RFC1001_PORT))
3506 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return rc;
3509}
3510
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003511static int
3512ip_connect(struct TCP_Server_Info *server)
3513{
Steve French6da97912011-03-13 18:55:55 +00003514 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003515 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3516 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3517
3518 if (server->dstaddr.ss_family == AF_INET6)
3519 sport = &addr6->sin6_port;
3520 else
3521 sport = &addr->sin_port;
3522
3523 if (*sport == 0) {
3524 int rc;
3525
3526 /* try with 445 port at first */
3527 *sport = htons(CIFS_PORT);
3528
3529 rc = generic_ip_connect(server);
3530 if (rc >= 0)
3531 return rc;
3532
3533 /* if it failed, try with 139 port */
3534 *sport = htons(RFC1001_PORT);
3535 }
3536
3537 return generic_ip_connect(server);
3538}
3539
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003540void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003541 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003542{
3543 /* if we are reconnecting then should we check to see if
3544 * any requested capabilities changed locally e.g. via
3545 * remount but we can not do much about it here
3546 * if they have (even if we could detect it by the following)
3547 * Perhaps we could add a backpointer to array of sb from tcon
3548 * or if we change to make all sb to same share the same
3549 * sb as NFS - then we only have one backpointer to sb.
3550 * What if we wanted to mount the server share twice once with
3551 * and once without posixacls or posix paths? */
3552 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003553
Steve Frenchc18c8422007-07-18 23:21:09 +00003554 if (vol_info && vol_info->no_linux_ext) {
3555 tcon->fsUnixInfo.Capability = 0;
3556 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003557 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003558 return;
3559 } else if (vol_info)
3560 tcon->unix_ext = 1; /* Unix Extensions supported */
3561
3562 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003563 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003564 return;
3565 }
Steve French50c2f752007-07-13 00:33:32 +00003566
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003567 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003568 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003569 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003570 /* check for reconnect case in which we do not
3571 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003572 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003573 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003574 originally at mount time */
3575 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3576 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003577 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3578 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003579 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003580 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003581 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003582 cifs_dbg(VFS, "possible reconnect error\n");
3583 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003584 }
Steve French8af18972007-02-14 04:42:51 +00003585 }
Steve French50c2f752007-07-13 00:33:32 +00003586
Steve French6848b732011-05-26 18:38:54 +00003587 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003588 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003589
Steve French8af18972007-02-14 04:42:51 +00003590 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003591 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003592 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003593 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003594 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003595 if (cifs_sb)
3596 cifs_sb->mnt_cifs_flags |=
3597 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003598 }
3599
Steve French75865f8c2007-06-24 18:30:48 +00003600 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003601 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003602 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003603 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003604 if (cifs_sb)
3605 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003606 CIFS_MOUNT_POSIX_PATHS;
3607 }
Steve French50c2f752007-07-13 00:33:32 +00003608
Joe Perchesf96637b2013-05-04 22:12:25 -05003609 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003610#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003611 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003612 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003613 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003614 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003615 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003616 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003617 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003618 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003619 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003620 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003621 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003622 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003623 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003624 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003625 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003626 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003627 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003628 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003629#endif /* CIFS_DEBUG2 */
3630 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003631 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003632 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003633 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003634 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 +00003635
Steve French8af18972007-02-14 04:42:51 +00003636 }
3637 }
3638}
3639
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003640int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003641 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003642{
Jeff Layton2de970f2010-10-06 19:51:12 -04003643 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3644
Al Viro2ced6f62011-06-17 09:20:04 -04003645 spin_lock_init(&cifs_sb->tlink_tree_lock);
3646 cifs_sb->tlink_tree = RB_ROOT;
3647
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003648 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003649 * Temporarily set r/wsize for matching superblock. If we end up using
3650 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003651 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003652 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003653 cifs_sb->wsize = pvolume_info->wsize;
3654
Steve French3b795212008-11-13 19:45:32 +00003655 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3656 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3657 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3658 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003659 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3660 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003661
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303662 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003663 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303664
Aurelien Aptel83930722018-09-20 18:10:25 -07003665 if (pvolume_info->nodfs)
3666 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
Steve French3b795212008-11-13 19:45:32 +00003667 if (pvolume_info->noperm)
3668 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3669 if (pvolume_info->setuids)
3670 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003671 if (pvolume_info->setuidfromacl)
3672 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003673 if (pvolume_info->server_ino)
3674 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3675 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003676 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3677 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003678 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3679 if (pvolume_info->no_xattr)
3680 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3681 if (pvolume_info->sfu_emul)
3682 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3683 if (pvolume_info->nobrl)
3684 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003685 if (pvolume_info->nohandlecache)
3686 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003687 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003688 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003689 if (pvolume_info->mand_lock)
3690 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003691 if (pvolume_info->rwpidforward)
3692 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003693 if (pvolume_info->cifs_acl)
3694 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003695 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003696 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003697 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3698 }
3699 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003700 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003701 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3702 }
Steve French3b795212008-11-13 19:45:32 +00003703 if (pvolume_info->override_uid)
3704 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3705 if (pvolume_info->override_gid)
3706 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3707 if (pvolume_info->dynperm)
3708 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303709 if (pvolume_info->fsc)
3710 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003711 if (pvolume_info->multiuser)
3712 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3713 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003714 if (pvolume_info->strict_io)
3715 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003716 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003717 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003718 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3719 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003720 if (pvolume_info->mfsymlinks) {
3721 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003722 /*
3723 * Our SFU ("Services for Unix" emulation does not allow
3724 * creating symlinks but does allow reading existing SFU
3725 * symlinks (it does allow both creating and reading SFU
3726 * style mknod and FIFOs though). When "mfsymlinks" and
3727 * "sfu" are both enabled at the same time, it allows
3728 * reading both types of symlinks, but will only create
3729 * them with mfsymlinks format. This allows better
3730 * Apple compatibility (probably better for Samba too)
3731 * while still recognizing old Windows style symlinks.
3732 */
3733 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003734 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003735 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003736 }
Steve French3b795212008-11-13 19:45:32 +00003737
3738 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003739 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003740
3741 if (pvolume_info->prepath) {
3742 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3743 if (cifs_sb->prepath == NULL)
3744 return -ENOMEM;
3745 }
3746
3747 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003748}
3749
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003750void
3751cifs_cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003752{
Sean Finneyb9468452011-04-11 13:19:32 +00003753 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003754 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003755 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003756 kfree(volume_info->domainname);
3757 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003758 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003759}
3760
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003761void
3762cifs_cleanup_volume_info(struct smb_vol *volume_info)
3763{
3764 if (!volume_info)
3765 return;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003766 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003767 kfree(volume_info);
3768}
3769
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003770/* Release all succeed connections */
3771static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
3772 unsigned int xid,
3773 struct TCP_Server_Info *server,
3774 struct cifs_ses *ses, struct cifs_tcon *tcon)
3775{
3776 int rc = 0;
3777
3778 if (tcon)
3779 cifs_put_tcon(tcon);
3780 else if (ses)
3781 cifs_put_smb_ses(ses);
3782 else if (server)
3783 cifs_put_tcp_session(server, 0);
3784 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3785 free_xid(xid);
3786}
3787
3788/* Get connections for tcp, ses and tcon */
3789static int mount_get_conns(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
3790 unsigned int *xid,
3791 struct TCP_Server_Info **nserver,
3792 struct cifs_ses **nses, struct cifs_tcon **ntcon)
3793{
3794 int rc = 0;
3795 struct TCP_Server_Info *server;
3796 struct cifs_ses *ses;
3797 struct cifs_tcon *tcon;
3798
3799 *nserver = NULL;
3800 *nses = NULL;
3801 *ntcon = NULL;
3802
3803 *xid = get_xid();
3804
3805 /* get a reference to a tcp session */
3806 server = cifs_get_tcp_session(vol);
3807 if (IS_ERR(server)) {
3808 rc = PTR_ERR(server);
3809 return rc;
3810 }
3811
3812 *nserver = server;
3813
3814 if ((vol->max_credits < 20) || (vol->max_credits > 60000))
3815 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3816 else
3817 server->max_credits = vol->max_credits;
3818
3819 /* get a reference to a SMB session */
3820 ses = cifs_get_smb_ses(server, vol);
3821 if (IS_ERR(ses)) {
3822 rc = PTR_ERR(ses);
3823 return rc;
3824 }
3825
3826 *nses = ses;
3827
3828 if ((vol->persistent == true) && (!(ses->server->capabilities &
3829 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
3830 cifs_dbg(VFS, "persistent handles not supported by server\n");
3831 return -EOPNOTSUPP;
3832 }
3833
3834 /* search for existing tcon to this server share */
3835 tcon = cifs_get_tcon(ses, vol);
3836 if (IS_ERR(tcon)) {
3837 rc = PTR_ERR(tcon);
3838 return rc;
3839 }
3840
3841 *ntcon = tcon;
3842
3843 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3844 if (tcon->posix_extensions)
3845 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3846
3847 /* tell server which Unix caps we support */
3848 if (cap_unix(tcon->ses)) {
3849 /*
3850 * reset of caps checks mount to see if unix extensions disabled
3851 * for just this mount.
3852 */
3853 reset_cifs_unix_caps(*xid, tcon, cifs_sb, vol);
3854 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3855 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3856 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
3857 return -EACCES;
3858 } else
3859 tcon->unix_ext = 0; /* server does not support them */
3860
3861 /* do not care if a following call succeed - informational */
3862 if (!tcon->pipe && server->ops->qfs_tcon)
3863 server->ops->qfs_tcon(*xid, tcon);
3864
3865 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, vol);
3866 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, vol);
3867
3868 return 0;
3869}
3870
3871static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3872 struct cifs_tcon *tcon)
3873{
3874 struct tcon_link *tlink;
3875
3876 /* hang the tcon off of the superblock */
3877 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3878 if (tlink == NULL)
3879 return -ENOMEM;
3880
3881 tlink->tl_uid = ses->linux_uid;
3882 tlink->tl_tcon = tcon;
3883 tlink->tl_time = jiffies;
3884 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3885 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3886
3887 cifs_sb->master_tlink = tlink;
3888 spin_lock(&cifs_sb->tlink_tree_lock);
3889 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3890 spin_unlock(&cifs_sb->tlink_tree_lock);
3891
3892 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3893 TLINK_IDLE_EXPIRE);
3894 return 0;
3895}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003896
Steve French2d6d5892009-04-09 00:36:44 +00003897#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003898/*
3899 * cifs_build_path_to_root returns full path to root when we do not have an
3900 * exiting connection (tcon)
3901 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003902static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003903build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003904 const struct cifs_sb_info *cifs_sb)
3905{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003906 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003907 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003908 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003909
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003910 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003911 if (full_path == NULL)
3912 return ERR_PTR(-ENOMEM);
3913
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003914 strncpy(full_path, vol->UNC, unc_len);
3915 pos = full_path + unc_len;
3916
3917 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003918 *pos = CIFS_DIR_SEP(cifs_sb);
3919 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003920 pos += pplen;
3921 }
3922
3923 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003924 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003925 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003926 return full_path;
3927}
Sean Finneydd613942011-04-11 13:19:30 +00003928
3929/*
3930 * Perform a dfs referral query for a share and (optionally) prefix
3931 *
Sean Finney046462a2011-04-11 13:19:33 +00003932 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3933 * to a string containing updated options for the submount. Otherwise it
3934 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003935 *
3936 * Returns the rc from get_dfs_path to the caller, which can be used to
3937 * determine whether there were referrals.
3938 */
3939static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003940expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003941 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003942 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003943{
3944 int rc;
3945 unsigned int num_referrals = 0;
3946 struct dfs_info3_param *referrals = NULL;
3947 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3948
Aurelien Aptel83930722018-09-20 18:10:25 -07003949 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
3950 return -EREMOTE;
3951
Sean Finneydd613942011-04-11 13:19:30 +00003952 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3953 if (IS_ERR(full_path))
3954 return PTR_ERR(full_path);
3955
3956 /* For DFS paths, skip the first '\' of the UNC */
3957 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3958
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003959 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003960 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003961
3962 if (!rc && num_referrals > 0) {
3963 char *fake_devname = NULL;
3964
3965 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3966 full_path + 1, referrals,
3967 &fake_devname);
3968
3969 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003970
Sean Finneydd613942011-04-11 13:19:30 +00003971 if (IS_ERR(mdata)) {
3972 rc = PTR_ERR(mdata);
3973 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003974 } else {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003975 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003976 rc = cifs_setup_volume_info(volume_info, mdata,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003977 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00003978 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003979 kfree(fake_devname);
3980 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003981 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003982 }
3983 kfree(full_path);
3984 return rc;
3985}
Steve French2d6d5892009-04-09 00:36:44 +00003986#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003987
Jeff Layton04db79b2011-07-06 08:10:38 -04003988static int
3989cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003990 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003992 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003993
Steve Frenchc7c137b2018-06-06 17:59:29 -05003994 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04003995 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Jeff Layton7586b762008-12-01 18:41:49 -05003997 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003998 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05003999 kfree(volume_info->username);
4000 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05004001 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05004003 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05004005 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00004006 /* In userspace mount helper we can get user name from alternate
4007 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04004008 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
4010
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05004012 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004013 /* load_nls_default cannot return null */
4014 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004016 volume_info->local_nls = load_nls(volume_info->iocharset);
4017 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004018 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004019 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04004020 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
4022 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004023
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004024 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04004025}
4026
4027struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05004028cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04004029{
4030 int rc;
4031 struct smb_vol *volume_info;
4032
Jeff Layton6ee95422012-11-26 11:09:57 -05004033 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04004034 if (!volume_info)
4035 return ERR_PTR(-ENOMEM);
4036
Steve Frenchc7c137b2018-06-06 17:59:29 -05004037 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04004038 if (rc) {
4039 cifs_cleanup_volume_info(volume_info);
4040 volume_info = ERR_PTR(rc);
4041 }
4042
4043 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004044}
4045
Aurelien Aptela6b50582016-05-25 19:59:09 +02004046static int
4047cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4048 unsigned int xid,
4049 struct cifs_tcon *tcon,
4050 struct cifs_sb_info *cifs_sb,
4051 char *full_path)
4052{
4053 int rc;
4054 char *s;
4055 char sep, tmp;
4056
4057 sep = CIFS_DIR_SEP(cifs_sb);
4058 s = full_path;
4059
4060 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4061 while (rc == 0) {
4062 /* skip separators */
4063 while (*s == sep)
4064 s++;
4065 if (!*s)
4066 break;
4067 /* next separator */
4068 while (*s && *s != sep)
4069 s++;
4070
4071 /*
4072 * temporarily null-terminate the path at the end of
4073 * the current component
4074 */
4075 tmp = *s;
4076 *s = 0;
4077 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4078 full_path);
4079 *s = tmp;
4080 }
4081 return rc;
4082}
4083
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004084/*
4085 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
4086 * otherwise 0.
4087 */
4088static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
4089 const unsigned int xid,
4090 struct TCP_Server_Info *server,
4091 struct cifs_tcon *tcon)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004092{
Jeff Layton1daaae82012-03-21 06:30:40 -04004093 int rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004094 char *full_path;
4095
4096 if (!server->ops->is_path_accessible)
4097 return -EOPNOTSUPP;
4098
4099 /*
4100 * cifs_build_path_to_root works only when we have a valid tcon
4101 */
4102 full_path = cifs_build_path_to_root(vol, cifs_sb, tcon,
4103 tcon->Flags & SMB_SHARE_IS_IN_DFS);
4104 if (full_path == NULL)
4105 return -ENOMEM;
4106
4107 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
4108
4109 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4110 full_path);
4111 if (rc != 0 && rc != -EREMOTE) {
4112 kfree(full_path);
4113 return rc;
4114 }
4115
4116 if (rc != -EREMOTE) {
4117 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
4118 cifs_sb,
4119 full_path);
4120 if (rc != 0) {
4121 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4122 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4123 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4124 rc = 0;
4125 }
4126 }
4127
4128 kfree(full_path);
4129 return rc;
4130}
4131
4132#ifdef CONFIG_CIFS_DFS_UPCALL
4133int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4134{
4135 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004136 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004137 struct cifs_ses *ses;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004138 struct cifs_tcon *tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004139 struct TCP_Server_Info *server;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004140 char *old_mountdata;
4141 int count;
Al Virodd854462011-06-17 08:24:42 -04004142
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004143 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4144 if (!rc && tcon) {
4145 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4146 if (!rc)
4147 goto out;
4148 if (rc != -EREMOTE)
4149 goto error;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004150 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004151 if ((rc == -EACCES) || (rc == -EOPNOTSUPP) || (ses == NULL) || (server == NULL))
4152 goto error;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Sean Finneyc1508ca2011-04-11 13:19:31 +00004155 /*
4156 * Perform an unconditional check for whether there are DFS
4157 * referrals for this path without prefix, to provide support
4158 * for DFS referrals from w2k8 servers which don't seem to respond
4159 * with PATH_NOT_COVERED to requests that include the prefix.
4160 * Chase the referral if found, otherwise continue normally.
4161 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004162 old_mountdata = cifs_sb->mountdata;
4163 (void)expand_dfs_referral(xid, ses, vol, cifs_sb, false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004164
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004165 if (cifs_sb->mountdata == NULL) {
4166 rc = -ENOENT;
4167 goto error;
4168 }
4169
4170 if (cifs_sb->mountdata != old_mountdata) {
4171 /* If we were redirected, reconnect to new target server */
4172 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4173 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4174 }
4175 if (rc) {
4176 if (rc == -EACCES || rc == -EOPNOTSUPP)
4177 goto error;
4178 }
4179
4180 for (count = 1; ;) {
4181 if (!rc && tcon) {
4182 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4183 if (!rc || rc != -EREMOTE)
4184 break;
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004185 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004186 /*
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004187 * BB: when we implement proper loop detection,
4188 * we will remove this check. But now we need it
4189 * to prevent an indefinite loop if 'DFS tree' is
4190 * misconfigured (i.e. has loops).
Steve French6d3ea7e2012-11-28 22:34:41 -06004191 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004192 if (count++ > MAX_NESTED_LINKS) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004193 rc = -ELOOP;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004194 break;
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004195 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004196
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004197 old_mountdata = cifs_sb->mountdata;
4198 rc = expand_dfs_referral(xid, tcon->ses, vol, cifs_sb,
4199 true);
4200 if (rc)
4201 break;
Jeff Layton7b91e262009-07-23 15:22:30 -04004202
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004203 if (cifs_sb->mountdata != old_mountdata) {
4204 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4205 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses,
4206 &tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004207 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004208 if (rc) {
4209 if (rc == -EACCES || rc == -EOPNOTSUPP || !server ||
4210 !ses)
4211 goto error;
4212 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004213 }
4214
Jeff Layton9d002df2010-10-06 19:51:11 -04004215 if (rc)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004216 goto error;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004217
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004218out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004219 free_xid(xid);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004220 return mount_setup_tlink(cifs_sb, ses, tcon);
4221
4222error:
4223 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 return rc;
4225}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004226#else
4227int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4228{
4229 int rc = 0;
4230 unsigned int xid;
4231 struct cifs_ses *ses;
4232 struct cifs_tcon *tcon;
4233 struct TCP_Server_Info *server;
4234
4235 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4236 if (rc)
4237 goto error;
4238
4239 if (tcon) {
4240 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4241 if (rc == -EREMOTE)
4242 rc = -EOPNOTSUPP;
4243 if (rc)
4244 goto error;
4245 }
4246
4247 free_xid(xid);
4248
4249 return mount_setup_tlink(cifs_sb, ses, tcon);
4250
4251error:
4252 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4253 return rc;
4254}
4255#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Jeff Layton8d1bca32011-06-11 21:17:10 -04004257/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004258 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004261CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004262 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 const struct nls_table *nls_codepage)
4264{
4265 struct smb_hdr *smb_buffer;
4266 struct smb_hdr *smb_buffer_response;
4267 TCONX_REQ *pSMB;
4268 TCONX_RSP *pSMBr;
4269 unsigned char *bcc_ptr;
4270 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004271 int length;
4272 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 if (ses == NULL)
4275 return -EIO;
4276
4277 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004278 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 smb_buffer_response = smb_buffer;
4282
4283 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4284 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004285
Pavel Shilovsky88257362012-05-23 14:01:59 +04004286 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 smb_buffer->Uid = ses->Suid;
4288 pSMB = (TCONX_REQ *) smb_buffer;
4289 pSMBr = (TCONX_RSP *) smb_buffer_response;
4290
4291 pSMB->AndXCommand = 0xFF;
4292 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004294 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004295 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004296 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004297 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004298 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004299 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004300 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004301 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4302 specified as required (when that support is added to
4303 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004304 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004305 by Samba (not sure whether other servers allow
4306 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004307#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004308 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004309 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004310 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004311 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004312 SECMODE_PW_ENCRYPT ? true : false,
4313 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004314 else
4315#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004316 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004317 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004318 if (rc) {
4319 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4320 __func__, rc);
4321 cifs_buf_release(smb_buffer);
4322 return rc;
4323 }
Steve Frencheeac8042006-01-13 21:34:58 -08004324
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004325 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004326 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004327 /* must align unicode strings */
4328 *bcc_ptr = 0; /* null byte password */
4329 bcc_ptr++;
4330 }
Steve Frencheeac8042006-01-13 21:34:58 -08004331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Jeff Layton38d77c52013-05-26 07:01:00 -04004333 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4335
4336 if (ses->capabilities & CAP_STATUS32) {
4337 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4338 }
4339 if (ses->capabilities & CAP_DFS) {
4340 smb_buffer->Flags2 |= SMBFLG2_DFS;
4341 }
4342 if (ses->capabilities & CAP_UNICODE) {
4343 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4344 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004345 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004346 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004347 (/* server len*/ + 256 /* share len */), nls_codepage);
4348 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 bcc_ptr += 2; /* skip trailing null */
4350 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 strcpy(bcc_ptr, tree);
4352 bcc_ptr += strlen(tree) + 1;
4353 }
4354 strcpy(bcc_ptr, "?????");
4355 bcc_ptr += strlen("?????");
4356 bcc_ptr += 1;
4357 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004358 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4359 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 pSMB->ByteCount = cpu_to_le16(count);
4361
Steve French133672e2007-11-13 22:41:37 +00004362 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004363 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004366 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004367 bool is_unicode;
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004370 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 tcon->tid = smb_buffer_response->Tid;
4372 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004373 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004374 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004375 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4376 is_unicode = true;
4377 else
4378 is_unicode = false;
4379
Jeff Laytoncc20c032009-04-30 07:16:21 -04004380
Steve French50c2f752007-07-13 00:33:32 +00004381 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004382 if (length == 3) {
4383 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4384 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004385 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004386 tcon->ipc = true;
4387 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004388 }
4389 } else if (length == 2) {
4390 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4391 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004392 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004393 }
4394 }
Steve French50c2f752007-07-13 00:33:32 +00004395 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004396 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004397 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004398
4399 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004400 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004401 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004402 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004403 nls_codepage);
4404
Joe Perchesf96637b2013-05-04 22:12:25 -05004405 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004406
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004407 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004408 (smb_buffer_response->WordCount == 7))
4409 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004410 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4411 else
4412 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004413 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 }
4415
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004416 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 return rc;
4418}
4419
Al Viro2e32cf52013-10-03 12:53:37 -04004420static void delayed_free(struct rcu_head *p)
4421{
4422 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4423 unload_nls(sbi->local_nls);
4424 kfree(sbi);
4425}
4426
Al Viro2a9b9952011-06-17 09:27:16 -04004427void
4428cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
Jeff Laytonb647c352010-10-28 11:16:44 -04004430 struct rb_root *root = &cifs_sb->tlink_tree;
4431 struct rb_node *node;
4432 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Jeff Layton2de970f2010-10-06 19:51:12 -04004434 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4435
Jeff Laytonb647c352010-10-28 11:16:44 -04004436 spin_lock(&cifs_sb->tlink_tree_lock);
4437 while ((node = rb_first(root))) {
4438 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4439 cifs_get_tlink(tlink);
4440 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4441 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004442
Jeff Laytonb647c352010-10-28 11:16:44 -04004443 spin_unlock(&cifs_sb->tlink_tree_lock);
4444 cifs_put_tlink(tlink);
4445 spin_lock(&cifs_sb->tlink_tree_lock);
4446 }
4447 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004448
Al Virod757d712011-06-17 09:42:43 -04004449 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004450 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004451 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004454int
4455cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456{
4457 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004458 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004460 if (!server->ops->need_neg || !server->ops->negotiate)
4461 return -ENOSYS;
4462
Jeff Layton198b5682010-04-24 07:57:48 -04004463 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004464 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Pavel Shilovsky45275782012-05-17 17:53:29 +04004467 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004468
4469 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004470 if (rc == 0) {
4471 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004472 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004473 server->tcpStatus = CifsGood;
4474 else
4475 rc = -EHOSTDOWN;
4476 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 }
Steve French26b994f2008-08-06 05:11:33 +00004478
Jeff Layton198b5682010-04-24 07:57:48 -04004479 return rc;
4480}
Steve French26b994f2008-08-06 05:11:33 +00004481
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004482int
4483cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4484 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004485{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004486 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004487 struct TCP_Server_Info *server = ses->server;
4488
Jeff Layton198b5682010-04-24 07:57:48 -04004489 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004490 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004491 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004492
Joe Perchesf96637b2013-05-04 22:12:25 -05004493 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004494 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004495
Shu Wangf5c4ba82017-09-08 18:48:33 +08004496 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05004497 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08004498 ses->auth_key.response);
4499 kfree(ses->auth_key.response);
4500 ses->auth_key.response = NULL;
4501 ses->auth_key.len = 0;
4502 }
4503
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004504 if (server->ops->sess_setup)
4505 rc = server->ops->sess_setup(xid, ses, nls_info);
4506
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004507 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004508 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004509
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 return rc;
4511}
4512
Jeff Layton8a8798a2012-01-17 16:09:15 -05004513static int
4514cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4515{
Jeff Layton3f618222013-06-12 19:52:14 -05004516 vol->sectype = ses->sectype;
4517
4518 /* krb5 is special, since we don't need username or pw */
4519 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004520 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004521
4522 return cifs_set_cifscreds(vol, ses);
4523}
4524
Steve French96daf2b2011-05-27 04:34:02 +00004525static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004526cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004527{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004528 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004529 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4530 struct cifs_ses *ses;
4531 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004532 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004533
4534 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004535 if (vol_info == NULL)
4536 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004537
Jeff Layton9d002df2010-10-06 19:51:11 -04004538 vol_info->local_nls = cifs_sb->local_nls;
4539 vol_info->linux_uid = fsuid;
4540 vol_info->cred_uid = fsuid;
4541 vol_info->UNC = master_tcon->treeName;
4542 vol_info->retry = master_tcon->retry;
4543 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05004544 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04004545 vol_info->local_lease = master_tcon->local_lease;
4546 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004547 vol_info->sectype = master_tcon->ses->sectype;
4548 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004549
Jeff Layton8a8798a2012-01-17 16:09:15 -05004550 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4551 if (rc) {
4552 tcon = ERR_PTR(rc);
4553 goto out;
4554 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004555
4556 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304557 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004558 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304559 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004560
4561 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4562 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004563 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004564 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004565 goto out;
4566 }
4567
4568 tcon = cifs_get_tcon(ses, vol_info);
4569 if (IS_ERR(tcon)) {
4570 cifs_put_smb_ses(ses);
4571 goto out;
4572 }
4573
Steve Frenchce558b02018-05-31 19:16:54 -05004574 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4575 if (tcon->posix_extensions)
4576 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05004577
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004578 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004579 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05004580
Jeff Layton9d002df2010-10-06 19:51:11 -04004581out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004582 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01004583 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004584 kfree(vol_info);
4585
4586 return tcon;
4587}
4588
Steve French96daf2b2011-05-27 04:34:02 +00004589struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004590cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4591{
4592 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4593}
4594
Jeff Laytonb647c352010-10-28 11:16:44 -04004595/* find and return a tlink with given uid */
4596static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004597tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004598{
4599 struct rb_node *node = root->rb_node;
4600 struct tcon_link *tlink;
4601
4602 while (node) {
4603 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4604
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004605 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004606 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004607 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004608 node = node->rb_right;
4609 else
4610 return tlink;
4611 }
4612 return NULL;
4613}
4614
4615/* insert a tcon_link into the tree */
4616static void
4617tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4618{
4619 struct rb_node **new = &(root->rb_node), *parent = NULL;
4620 struct tcon_link *tlink;
4621
4622 while (*new) {
4623 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4624 parent = *new;
4625
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004626 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004627 new = &((*new)->rb_left);
4628 else
4629 new = &((*new)->rb_right);
4630 }
4631
4632 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4633 rb_insert_color(&new_tlink->tl_rbnode, root);
4634}
4635
Jeff Layton9d002df2010-10-06 19:51:11 -04004636/*
4637 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4638 * current task.
4639 *
4640 * If the superblock doesn't refer to a multiuser mount, then just return
4641 * the master tcon for the mount.
4642 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304643 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004644 * exists, then check to see if it's pending construction. If it is then wait
4645 * for construction to complete. Once it's no longer pending, check to see if
4646 * it failed and either return an error or retry construction, depending on
4647 * the timeout.
4648 *
4649 * If one doesn't exist then insert a new tcon_link struct into the tree and
4650 * try to construct a new one.
4651 */
4652struct tcon_link *
4653cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4654{
4655 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004656 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004657 struct tcon_link *tlink, *newtlink;
4658
4659 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4660 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4661
4662 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004663 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004664 if (tlink)
4665 cifs_get_tlink(tlink);
4666 spin_unlock(&cifs_sb->tlink_tree_lock);
4667
4668 if (tlink == NULL) {
4669 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4670 if (newtlink == NULL)
4671 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004672 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004673 newtlink->tl_tcon = ERR_PTR(-EACCES);
4674 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4675 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4676 cifs_get_tlink(newtlink);
4677
Jeff Layton9d002df2010-10-06 19:51:11 -04004678 spin_lock(&cifs_sb->tlink_tree_lock);
4679 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004680 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004681 if (tlink) {
4682 cifs_get_tlink(tlink);
4683 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004684 kfree(newtlink);
4685 goto wait_for_construction;
4686 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004687 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004688 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4689 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004690 } else {
4691wait_for_construction:
4692 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004693 TASK_INTERRUPTIBLE);
4694 if (ret) {
4695 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004696 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004697 }
4698
4699 /* if it's good, return it */
4700 if (!IS_ERR(tlink->tl_tcon))
4701 return tlink;
4702
4703 /* return error if we tried this already recently */
4704 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4705 cifs_put_tlink(tlink);
4706 return ERR_PTR(-EACCES);
4707 }
4708
4709 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4710 goto wait_for_construction;
4711 }
4712
4713 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4714 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4715 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4716
4717 if (IS_ERR(tlink->tl_tcon)) {
4718 cifs_put_tlink(tlink);
4719 return ERR_PTR(-EACCES);
4720 }
4721
4722 return tlink;
4723}
Jeff Layton2de970f2010-10-06 19:51:12 -04004724
4725/*
4726 * periodic workqueue job that scans tcon_tree for a superblock and closes
4727 * out tcons.
4728 */
4729static void
4730cifs_prune_tlinks(struct work_struct *work)
4731{
4732 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4733 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004734 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004735 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004736 struct rb_node *tmp;
4737 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004738
Jeff Laytonb647c352010-10-28 11:16:44 -04004739 /*
4740 * Because we drop the spinlock in the loop in order to put the tlink
4741 * it's not guarded against removal of links from the tree. The only
4742 * places that remove entries from the tree are this function and
4743 * umounts. Because this function is non-reentrant and is canceled
4744 * before umount can proceed, this is safe.
4745 */
4746 spin_lock(&cifs_sb->tlink_tree_lock);
4747 node = rb_first(root);
4748 while (node != NULL) {
4749 tmp = node;
4750 node = rb_next(tmp);
4751 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4752
4753 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4754 atomic_read(&tlink->tl_count) != 0 ||
4755 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4756 continue;
4757
4758 cifs_get_tlink(tlink);
4759 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4760 rb_erase(tmp, root);
4761
Jeff Layton2de970f2010-10-06 19:51:12 -04004762 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004763 cifs_put_tlink(tlink);
4764 spin_lock(&cifs_sb->tlink_tree_lock);
4765 }
4766 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004767
Jeff Laytonda472fc2012-03-23 14:40:53 -04004768 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004769 TLINK_IDLE_EXPIRE);
4770}