blob: 5feb3bf97d70be017a6b464f68d6f9e0ea5558ba [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? */
Paulo Alcantarac34fea52018-11-14 14:03:40 -02001047 if (strlen(unc) < 3)
1048 return ERR_PTR(-EINVAL);
1049 for (src = unc; *src && *src == '\\'; src++)
1050 ;
1051 if (!*src)
1052 return ERR_PTR(-EINVAL);
Jeff Laytonc359cf32007-11-16 22:22:06 +00001053
1054 /* delimiter between hostname and sharename is always '\\' now */
1055 delim = strchr(src, '\\');
1056 if (!delim)
1057 return ERR_PTR(-EINVAL);
1058
1059 len = delim - src;
1060 dst = kmalloc((len + 1), GFP_KERNEL);
1061 if (dst == NULL)
1062 return ERR_PTR(-ENOMEM);
1063
1064 memcpy(dst, src, len);
1065 dst[len] = '\0';
1066
1067 return dst;
1068}
1069
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001070static int get_option_ul(substring_t args[], unsigned long *option)
1071{
1072 int rc;
1073 char *string;
1074
1075 string = match_strdup(args);
1076 if (string == NULL)
1077 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001078 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001079 kfree(string);
1080
1081 return rc;
1082}
1083
Eric W. Biederman3da46562013-02-06 01:37:39 -08001084static int get_option_uid(substring_t args[], kuid_t *result)
1085{
1086 unsigned long value;
1087 kuid_t uid;
1088 int rc;
1089
1090 rc = get_option_ul(args, &value);
1091 if (rc)
1092 return rc;
1093
1094 uid = make_kuid(current_user_ns(), value);
1095 if (!uid_valid(uid))
1096 return -EINVAL;
1097
1098 *result = uid;
1099 return 0;
1100}
1101
1102static int get_option_gid(substring_t args[], kgid_t *result)
1103{
1104 unsigned long value;
1105 kgid_t gid;
1106 int rc;
1107
1108 rc = get_option_ul(args, &value);
1109 if (rc)
1110 return rc;
1111
1112 gid = make_kgid(current_user_ns(), value);
1113 if (!gid_valid(gid))
1114 return -EINVAL;
1115
1116 *result = gid;
1117 return 0;
1118}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001119
1120static int cifs_parse_security_flavors(char *value,
1121 struct smb_vol *vol)
1122{
1123
1124 substring_t args[MAX_OPT_ARGS];
1125
Jeff Layton1e3cc572013-06-10 17:12:23 -05001126 /*
1127 * With mount options, the last one should win. Reset any existing
1128 * settings back to default.
1129 */
1130 vol->sectype = Unspecified;
1131 vol->sign = false;
1132
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001133 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001134 case Opt_sec_krb5p:
1135 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1136 return 1;
1137 case Opt_sec_krb5i:
1138 vol->sign = true;
1139 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001140 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001141 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001142 break;
1143 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001144 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001145 /* Fallthrough */
1146 case Opt_sec_ntlmssp:
1147 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001148 break;
1149 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001150 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001151 /* Fallthrough */
1152 case Opt_ntlm:
1153 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001154 break;
1155 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001156 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001157 /* Fallthrough */
1158 case Opt_sec_ntlmv2:
1159 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001160 break;
1161#ifdef CONFIG_CIFS_WEAK_PW_HASH
1162 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001163 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001164 break;
1165#endif
1166 case Opt_sec_none:
1167 vol->nullauth = 1;
1168 break;
1169 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001170 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001171 return 1;
1172 }
1173
1174 return 0;
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001178cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1179{
1180 substring_t args[MAX_OPT_ARGS];
1181
1182 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1183 case Opt_cache_loose:
1184 vol->direct_io = false;
1185 vol->strict_io = false;
1186 break;
1187 case Opt_cache_strict:
1188 vol->direct_io = false;
1189 vol->strict_io = true;
1190 break;
1191 case Opt_cache_none:
1192 vol->direct_io = true;
1193 vol->strict_io = false;
1194 break;
1195 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001196 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001197 return 1;
1198 }
1199 return 0;
1200}
1201
1202static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001203cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001204{
1205 substring_t args[MAX_OPT_ARGS];
1206
1207 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001208#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001209 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001210 if (disable_legacy_dialects) {
1211 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1212 return 1;
1213 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001214 if (is_smb3) {
1215 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1216 return 1;
1217 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001218 vol->ops = &smb1_operations;
1219 vol->vals = &smb1_values;
1220 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001221 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001222 if (disable_legacy_dialects) {
1223 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1224 return 1;
1225 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001226 if (is_smb3) {
1227 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1228 return 1;
1229 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001230 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001231 vol->vals = &smb20_values;
1232 break;
Steve French74204512018-06-19 14:34:08 -05001233#else
1234 case Smb_1:
1235 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1236 return 1;
1237 case Smb_20:
1238 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1239 return 1;
1240#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001241 case Smb_21:
1242 vol->ops = &smb21_operations;
1243 vol->vals = &smb21_values;
1244 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001245 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001246 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001247 vol->vals = &smb30_values;
1248 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001249 case Smb_302:
1250 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1251 vol->vals = &smb302_values;
1252 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001253 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001254 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001255 vol->vals = &smb311_values;
1256 break;
Steve French9764c022017-09-17 10:41:35 -05001257 case Smb_3any:
1258 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1259 vol->vals = &smb3any_values;
1260 break;
1261 case Smb_default:
1262 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1263 vol->vals = &smbdefault_values;
1264 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001265 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001266 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001267 return 1;
1268 }
1269 return 0;
1270}
1271
Jeff Laytond387a5c2012-12-10 06:10:46 -05001272/*
1273 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1274 * fields with the result. Returns 0 on success and an error otherwise.
1275 */
1276static int
1277cifs_parse_devname(const char *devname, struct smb_vol *vol)
1278{
1279 char *pos;
1280 const char *delims = "/\\";
1281 size_t len;
1282
1283 /* make sure we have a valid UNC double delimiter prefix */
1284 len = strspn(devname, delims);
1285 if (len != 2)
1286 return -EINVAL;
1287
1288 /* find delimiter between host and sharename */
1289 pos = strpbrk(devname + 2, delims);
1290 if (!pos)
1291 return -EINVAL;
1292
1293 /* skip past delimiter */
1294 ++pos;
1295
1296 /* now go until next delimiter or end of string */
1297 len = strcspn(pos, delims);
1298
1299 /* move "pos" up to delimiter or NULL */
1300 pos += len;
1301 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1302 if (!vol->UNC)
1303 return -ENOMEM;
1304
1305 convert_delimiter(vol->UNC, '\\');
1306
Sachin Prabhu11e31642016-02-08 13:44:01 +05301307 /* skip any delimiter */
1308 if (*pos == '/' || *pos == '\\')
1309 pos++;
1310
1311 /* If pos is NULL then no prepath */
1312 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001313 return 0;
1314
1315 vol->prepath = kstrdup(pos, GFP_KERNEL);
1316 if (!vol->prepath)
1317 return -ENOMEM;
1318
1319 return 0;
1320}
1321
Jeff Layton23db65f2012-05-15 12:20:51 -04001322static int
Sean Finneyb9468452011-04-11 13:19:32 +00001323cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001324 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001326 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001327 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 unsigned int temp_len, i, j;
1329 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001330 short int override_uid = -1;
1331 short int override_gid = -1;
1332 bool uid_specified = false;
1333 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001334 bool sloppy = false;
1335 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001336 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001337 char *string = NULL;
1338 char *tmp_end, *value;
1339 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001340 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001341 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001342 unsigned short port = 0;
1343 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001346 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001347 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Jeff Layton6ee95422012-11-26 11:09:57 -05001349 /* ensure we always start with zeroed-out smb_vol */
1350 memset(vol, 0, sizeof(*vol));
1351
Jeff Layton88463992010-11-22 15:31:03 -05001352 /*
1353 * does not have to be perfect mapping since field is
1354 * informational, only used for servers that do not support
1355 * port 445 and it can be overridden at mount time
1356 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001357 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1358 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001359 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1360
Jeff Layton1397f2e2011-01-07 11:30:28 -05001361 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001362 /* null target name indicates to use *SMBSERVR default called name
1363 if we end up sending RFC1001 session initialize */
1364 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001365 vol->cred_uid = current_uid();
1366 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001367 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001368
Steve French2baa2682014-09-27 02:19:01 -05001369 /*
1370 * default to SFM style remapping of seven reserved characters
1371 * unless user overrides it or we negotiate CIFS POSIX where
1372 * it is unnecessary. Can not simultaneously use more than one mapping
1373 * since then readdir could list files that open could not open
1374 */
1375 vol->remap = true;
1376
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001377 /* default to only allowing write access to owner of the mount */
1378 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001381 /* default is always to request posix paths. */
1382 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001383 /* default to using server inode numbers where available */
1384 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001385
Jeff Layton1b359202012-09-19 15:20:27 -07001386 /* default is to use strict cifs caching semantics */
1387 vol->strict_io = true;
1388
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301389 vol->actimeo = CIFS_DEF_ACTIMEO;
1390
Steve French9764c022017-09-17 10:41:35 -05001391 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1392 vol->ops = &smb30_operations;
1393 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001394
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001395 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1396
Sean Finneyb9468452011-04-11 13:19:32 +00001397 if (!mountdata)
1398 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Sean Finneyb9468452011-04-11 13:19:32 +00001400 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1401 if (!mountdata_copy)
1402 goto cifs_parse_mount_err;
1403
1404 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001405 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001406
Steve French50c2f752007-07-13 00:33:32 +00001407 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001408 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 separator[0] = options[4];
1410 options += 5;
1411 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001412 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001415 vol->backupuid_specified = false; /* no backup intent for a user */
1416 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001417
Jeff Layton37d4f992013-05-24 07:40:05 -04001418 switch (cifs_parse_devname(devname, vol)) {
1419 case 0:
1420 break;
1421 case -ENOMEM:
1422 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1423 goto cifs_parse_mount_err;
1424 case -EINVAL:
1425 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1426 goto cifs_parse_mount_err;
1427 default:
1428 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1429 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001430 }
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001433 substring_t args[MAX_OPT_ARGS];
1434 unsigned long option;
1435 int token;
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (!*data)
1438 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001440 token = match_token(data, cifs_mount_option_tokens, args);
1441
1442 switch (token) {
1443
1444 /* Ingnore the following */
1445 case Opt_ignore:
1446 break;
1447
1448 /* Boolean values */
1449 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001451 break;
1452 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001454 break;
1455 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001456 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001457 break;
1458 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001459 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001460 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001461 case Opt_forcegid:
1462 override_gid = 1;
1463 break;
1464 case Opt_noforcegid:
1465 override_gid = 0;
1466 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001467 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001468 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001469 break;
1470 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001471 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001472 break;
1473 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001475 break;
1476 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001478 break;
1479 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001481 break;
1482 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001484 break;
1485 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001486 vol->sfu_remap = true;
1487 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001488 break;
1489 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001490 vol->sfu_remap = false;
1491 break;
1492 case Opt_mapposix:
1493 vol->remap = true;
1494 vol->sfu_remap = false; /* disable SFU mapping */
1495 break;
1496 case Opt_nomapposix:
1497 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001498 break;
1499 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001500 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001501 break;
1502 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001503 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001504 break;
1505 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001506 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001507 break;
1508 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001509 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001510 break;
1511 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001512 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001513 break;
1514 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001515 if (vol->linux_ext)
1516 cifs_dbg(VFS,
1517 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001518 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001519 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001520 case Opt_unix:
1521 if (vol->no_linux_ext)
1522 cifs_dbg(VFS,
1523 "conflicting unix mount options\n");
1524 vol->linux_ext = 1;
1525 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001526 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001527 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001528 break;
1529 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001530 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001531 break;
1532 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001533 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001534 /*
1535 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001536 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001537 * local vfs will do advisory
1538 */
Steve French50c2f752007-07-13 00:33:32 +00001539 if (vol->file_mode ==
1540 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001541 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001542 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001543 case Opt_nohandlecache:
1544 vol->nohandlecache = 1;
1545 break;
1546 case Opt_handlecache:
1547 vol->nohandlecache = 0;
1548 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001549 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001550 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001551 break;
1552 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001554 break;
1555 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001557 break;
Steve French95932652016-09-23 01:36:34 -05001558 case Opt_setuidfromacl:
1559 vol->setuidfromacl = 1;
1560 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001561 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001562 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001563 break;
1564 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001565 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001566 break;
1567 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001569 break;
1570 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001572 break;
1573 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001575 break;
1576 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001578 break;
1579 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001580 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001581 break;
1582 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001583 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001584 break;
1585 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001587 break;
1588 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001590 break;
1591 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001592 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001593 break;
1594 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001595 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001596 break;
1597 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001598 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001599 break;
1600 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001602 break;
1603 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001605 break;
1606 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001607 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001608 break;
1609 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001610 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001611 break;
1612 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001613 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001614 * is a per tree connection (mount) not a per socket
1615 * or per-smb connection option in the protocol
1616 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1617 */
Steve French95b1cb92008-05-15 16:44:38 +00001618 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001619 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001620 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001621 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001622 break;
1623 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301624#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001625 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001626 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301627#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301628 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001629 break;
1630 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001631 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001632 break;
1633 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001634 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001635 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001636 case Opt_sloppy:
1637 sloppy = true;
1638 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001639 case Opt_nosharesock:
1640 vol->nosharesock = true;
1641 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001642 case Opt_nopersistent:
1643 vol->nopersistent = true;
1644 if (vol->persistent) {
1645 cifs_dbg(VFS,
1646 "persistenthandles mount options conflict\n");
1647 goto cifs_parse_mount_err;
1648 }
1649 break;
1650 case Opt_persistent:
1651 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001652 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001653 cifs_dbg(VFS,
1654 "persistenthandles mount options conflict\n");
1655 goto cifs_parse_mount_err;
1656 }
1657 break;
Steve French592fafe2015-11-03 10:08:53 -06001658 case Opt_resilient:
1659 vol->resilient = true;
1660 if (vol->persistent) {
1661 cifs_dbg(VFS,
1662 "persistenthandles mount options conflict\n");
1663 goto cifs_parse_mount_err;
1664 }
1665 break;
1666 case Opt_noresilient:
1667 vol->resilient = false; /* already the default */
1668 break;
Germano Percossi39566442016-12-15 12:31:18 +05301669 case Opt_domainauto:
1670 vol->domainauto = true;
1671 break;
Long Li8339dd32017-11-07 01:54:55 -07001672 case Opt_rdma:
1673 vol->rdma = true;
1674 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001675
1676 /* Numeric Values */
1677 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001678 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001679 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1680 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001681 goto cifs_parse_mount_err;
1682 }
1683 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001684 break;
1685 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001686 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001687 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1688 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001689 goto cifs_parse_mount_err;
1690 }
1691 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001692 break;
1693 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001694 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001695 cifs_dbg(VFS, "%s: Invalid uid value\n",
1696 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001697 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001699 uid_specified = true;
1700 break;
1701 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001702 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001703 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1704 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001705 goto cifs_parse_mount_err;
1706 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001707 break;
1708 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001709 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001710 cifs_dbg(VFS, "%s: Invalid gid value\n",
1711 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001712 goto cifs_parse_mount_err;
1713 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001714 gid_specified = true;
1715 break;
1716 case Opt_file_mode:
1717 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001718 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1719 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001720 goto cifs_parse_mount_err;
1721 }
1722 vol->file_mode = option;
1723 break;
1724 case Opt_dirmode:
1725 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001726 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1727 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001728 goto cifs_parse_mount_err;
1729 }
1730 vol->dir_mode = option;
1731 break;
1732 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001733 if (get_option_ul(args, &option) ||
1734 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001735 cifs_dbg(VFS, "%s: Invalid port value\n",
1736 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001737 goto cifs_parse_mount_err;
1738 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001739 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001740 break;
1741 case Opt_rsize:
1742 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001743 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1744 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001745 goto cifs_parse_mount_err;
1746 }
1747 vol->rsize = option;
1748 break;
1749 case Opt_wsize:
1750 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001751 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1752 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001753 goto cifs_parse_mount_err;
1754 }
1755 vol->wsize = option;
1756 break;
1757 case Opt_actimeo:
1758 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001759 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1760 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001761 goto cifs_parse_mount_err;
1762 }
1763 vol->actimeo = HZ * option;
1764 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001765 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001766 goto cifs_parse_mount_err;
1767 }
1768 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001769 case Opt_echo_interval:
1770 if (get_option_ul(args, &option)) {
1771 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1772 __func__);
1773 goto cifs_parse_mount_err;
1774 }
1775 vol->echo_interval = option;
1776 break;
Steve French8b217fe2016-11-11 22:36:20 -06001777 case Opt_snapshot:
1778 if (get_option_ul(args, &option)) {
1779 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1780 __func__);
1781 goto cifs_parse_mount_err;
1782 }
1783 vol->snapshot_time = option;
1784 break;
Steve French141891f2016-09-23 00:44:16 -05001785 case Opt_max_credits:
1786 if (get_option_ul(args, &option) || (option < 20) ||
1787 (option > 60000)) {
1788 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1789 __func__);
1790 goto cifs_parse_mount_err;
1791 }
1792 vol->max_credits = option;
1793 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001794
1795 /* String Arguments */
1796
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001797 case Opt_blank_user:
1798 /* null user, ie. anonymous authentication */
1799 vol->nullauth = 1;
1800 vol->username = NULL;
1801 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001802 case Opt_user:
1803 string = match_strdup(args);
1804 if (string == NULL)
1805 goto out_nomem;
1806
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001807 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1808 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001809 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001810 goto cifs_parse_mount_err;
1811 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001812
1813 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001814 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001815 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001816 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001817 break;
1818 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001819 /* passwords have to be handled differently
1820 * to allow the character used for deliminator
1821 * to be passed within them
1822 */
1823
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001824 /*
1825 * Check if this is a case where the password
1826 * starts with a delimiter
1827 */
1828 tmp_end = strchr(data, '=');
1829 tmp_end++;
1830 if (!(tmp_end < end && tmp_end[1] == delim)) {
1831 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001832 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001833 vol->password = NULL;
1834 break;
1835 }
Gustavo A. R. Silva07fa6012018-11-27 10:01:51 +11001836 /* Fallthrough - to Opt_pass below.*/
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001837 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001838 /* Obtain the value string */
1839 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001840 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001841
1842 /* Set tmp_end to end of the string */
1843 tmp_end = (char *) value + strlen(value);
1844
1845 /* Check if following character is the deliminator
1846 * If yes, we have encountered a double deliminator
1847 * reset the NULL character to the deliminator
1848 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301849 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001850 tmp_end[0] = delim;
1851
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301852 /* Keep iterating until we get to a single
1853 * deliminator OR the end
1854 */
1855 while ((tmp_end = strchr(tmp_end, delim))
1856 != NULL && (tmp_end[1] == delim)) {
1857 tmp_end = (char *) &tmp_end[2];
1858 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001859
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301860 /* Reset var options to point to next element */
1861 if (tmp_end) {
1862 tmp_end[0] = '\0';
1863 options = (char *) &tmp_end[1];
1864 } else
1865 /* Reached the end of the mount option
1866 * string */
1867 options = end;
1868 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001869
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001870 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001871 /* Now build new password string */
1872 temp_len = strlen(value);
1873 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1874 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001875 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001876 goto cifs_parse_mount_err;
1877 }
1878
1879 for (i = 0, j = 0; i < temp_len; i++, j++) {
1880 vol->password[j] = value[i];
1881 if ((value[i] == delim) &&
1882 value[i+1] == delim)
1883 /* skip the second deliminator */
1884 i++;
1885 }
1886 vol->password[j] = '\0';
1887 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001888 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001889 /* FIXME: should this be an error instead? */
1890 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001891 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001892 case Opt_ip:
1893 string = match_strdup(args);
1894 if (string == NULL)
1895 goto out_nomem;
1896
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001897 if (!cifs_convert_address(dstaddr, string,
1898 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001899 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001900 goto cifs_parse_mount_err;
1901 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001902 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001903 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001904 case Opt_domain:
1905 string = match_strdup(args);
1906 if (string == NULL)
1907 goto out_nomem;
1908
Chen Gang057d6332013-07-19 09:01:36 +08001909 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1910 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001911 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001912 goto cifs_parse_mount_err;
1913 }
1914
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001915 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001916 vol->domainname = kstrdup(string, GFP_KERNEL);
1917 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001918 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001919 goto cifs_parse_mount_err;
1920 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001921 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001922 break;
1923 case Opt_srcaddr:
1924 string = match_strdup(args);
1925 if (string == NULL)
1926 goto out_nomem;
1927
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001928 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001929 (struct sockaddr *)&vol->srcaddr,
1930 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001931 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1932 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001933 goto cifs_parse_mount_err;
1934 }
1935 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001936 case Opt_iocharset:
1937 string = match_strdup(args);
1938 if (string == NULL)
1939 goto out_nomem;
1940
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001941 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001942 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001943 goto cifs_parse_mount_err;
1944 }
1945
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001946 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001947 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001948 vol->iocharset = kstrdup(string,
1949 GFP_KERNEL);
1950 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001951 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001952 goto cifs_parse_mount_err;
1953 }
1954 }
1955 /* if iocharset not set then load_nls_default
1956 * is used by caller
1957 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001958 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001959 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001960 case Opt_netbiosname:
1961 string = match_strdup(args);
1962 if (string == NULL)
1963 goto out_nomem;
1964
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001965 memset(vol->source_rfc1001_name, 0x20,
1966 RFC1001_NAME_LEN);
1967 /*
1968 * FIXME: are there cases in which a comma can
1969 * be valid in workstation netbios name (and
1970 * need special handling)?
1971 */
1972 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1973 /* don't ucase netbiosname for user */
1974 if (string[i] == 0)
1975 break;
1976 vol->source_rfc1001_name[i] = string[i];
1977 }
1978 /* The string has 16th byte zero still from
1979 * set at top of the function
1980 */
1981 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001982 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001983 break;
1984 case Opt_servern:
1985 /* servernetbiosname specified override *SMBSERVER */
1986 string = match_strdup(args);
1987 if (string == NULL)
1988 goto out_nomem;
1989
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001990 /* last byte, type, is 0x20 for servr type */
1991 memset(vol->target_rfc1001_name, 0x20,
1992 RFC1001_NAME_LEN_WITH_NULL);
1993
1994 /* BB are there cases in which a comma can be
1995 valid in this workstation netbios name
1996 (and need special handling)? */
1997
1998 /* user or mount helper must uppercase the
1999 netbios name */
2000 for (i = 0; i < 15; i++) {
2001 if (string[i] == 0)
2002 break;
2003 vol->target_rfc1001_name[i] = string[i];
2004 }
2005 /* The string has 16th byte zero still from
2006 set at top of the function */
2007 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002008 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002009 break;
2010 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002011 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002012 string = match_strdup(args);
2013 if (string == NULL)
2014 goto out_nomem;
2015
Steve French7e682f72017-08-31 21:34:24 -05002016 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002017 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002018 if (strlen(string) > 1) {
2019 pr_warn("Bad mount helper ver=%s. Did "
2020 "you want SMB1 (CIFS) dialect "
2021 "and mean to type vers=1.0 "
2022 "instead?\n", string);
2023 goto cifs_parse_mount_err;
2024 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002025 /* This is the default */
2026 break;
2027 }
2028 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002029 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002030 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002031 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002032 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002033 string = match_strdup(args);
2034 if (string == NULL)
2035 goto out_nomem;
2036
Steve Frenchc7c137b2018-06-06 17:59:29 -05002037 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002038 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002039 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002040 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002041 case Opt_sec:
2042 string = match_strdup(args);
2043 if (string == NULL)
2044 goto out_nomem;
2045
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002046 if (cifs_parse_security_flavors(string, vol) != 0)
2047 goto cifs_parse_mount_err;
2048 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002049 case Opt_cache:
2050 string = match_strdup(args);
2051 if (string == NULL)
2052 goto out_nomem;
2053
2054 if (cifs_parse_cache_flavor(string, vol) != 0)
2055 goto cifs_parse_mount_err;
2056 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002057 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002058 /*
2059 * An option we don't recognize. Save it off for later
2060 * if we haven't already found one
2061 */
2062 if (!invalid)
2063 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002066 /* Free up any allocated string */
2067 kfree(string);
2068 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002070
Jeff Laytond8162552012-03-23 14:40:56 -04002071 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002072 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002073 goto cifs_parse_mount_err;
2074 }
2075
Long Li8339dd32017-11-07 01:54:55 -07002076 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2077 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2078 goto cifs_parse_mount_err;
2079 }
2080
Jeff Layton8a8798a2012-01-17 16:09:15 -05002081#ifndef CONFIG_KEYS
2082 /* Muliuser mounts require CONFIG_KEYS support */
2083 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002084 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002085 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002086 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002087#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002088 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002089 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002090 goto cifs_parse_mount_err;
2091 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002092
Jeff Layton62a1a432012-12-10 06:10:45 -05002093 /* make sure UNC has a share name */
2094 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002095 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002096 goto cifs_parse_mount_err;
2097 }
2098
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002099 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002100 int len;
2101 const char *slash;
2102
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002103 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002104 /* Use the address part of the UNC. */
2105 slash = strchr(&vol->UNC[2], '\\');
2106 len = slash - &vol->UNC[2];
2107 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002108 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002109 goto cifs_parse_mount_err;
2110 }
2111 }
2112
2113 /* set the port that we got earlier */
2114 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002116 if (uid_specified)
2117 vol->override_uid = override_uid;
2118 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002119 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002120
2121 if (gid_specified)
2122 vol->override_gid = override_gid;
2123 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002124 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002125
Steve French7e682f72017-08-31 21:34:24 -05002126 if (got_version == false)
2127 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002128 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002129 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002130 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2131 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002132
Sean Finneyb9468452011-04-11 13:19:32 +00002133 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002135
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002136out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002137 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002138cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002139 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002140 kfree(mountdata_copy);
2141 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Ben Greear3eb9a882010-09-01 17:06:02 -07002144/** Returns true if srcaddr isn't specified and rhs isn't
2145 * specified, or if srcaddr is specified and
2146 * matches the IP address of the rhs argument.
2147 */
Jeff Layton45151482010-07-06 20:43:02 -04002148static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002149srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2150{
2151 switch (srcaddr->sa_family) {
2152 case AF_UNSPEC:
2153 return (rhs->sa_family == AF_UNSPEC);
2154 case AF_INET: {
2155 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2156 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2157 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2158 }
2159 case AF_INET6: {
2160 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002161 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002162 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2163 }
2164 default:
2165 WARN_ON(1);
2166 return false; /* don't expect to be here */
2167 }
2168}
2169
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002170/*
2171 * If no port is specified in addr structure, we try to match with 445 port
2172 * and if it fails - with 139 ports. It should be called only if address
2173 * families of server and addr are equal.
2174 */
2175static bool
2176match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2177{
Steve French6da97912011-03-13 18:55:55 +00002178 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002179
2180 switch (addr->sa_family) {
2181 case AF_INET:
2182 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2183 port = ((struct sockaddr_in *) addr)->sin_port;
2184 break;
2185 case AF_INET6:
2186 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2187 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2188 break;
2189 default:
2190 WARN_ON(1);
2191 return false;
2192 }
2193
2194 if (!port) {
2195 port = htons(CIFS_PORT);
2196 if (port == *sport)
2197 return true;
2198
2199 port = htons(RFC1001_PORT);
2200 }
2201
2202 return port == *sport;
2203}
Ben Greear3eb9a882010-09-01 17:06:02 -07002204
2205static bool
2206match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2207 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Jeff Layton45151482010-07-06 20:43:02 -04002209 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002210 case AF_INET: {
2211 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2212 struct sockaddr_in *srv_addr4 =
2213 (struct sockaddr_in *)&server->dstaddr;
2214
2215 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002216 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002217 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002218 }
2219 case AF_INET6: {
2220 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2221 struct sockaddr_in6 *srv_addr6 =
2222 (struct sockaddr_in6 *)&server->dstaddr;
2223
Jeff Layton45151482010-07-06 20:43:02 -04002224 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002225 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002226 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002227 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002228 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002229 break;
2230 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002231 default:
2232 WARN_ON(1);
2233 return false; /* don't expect to be here */
2234 }
Jeff Layton45151482010-07-06 20:43:02 -04002235
Ben Greear3eb9a882010-09-01 17:06:02 -07002236 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2237 return false;
2238
Jeff Layton45151482010-07-06 20:43:02 -04002239 return true;
2240}
2241
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002242static bool
2243match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2244{
Jeff Layton3f618222013-06-12 19:52:14 -05002245 /*
2246 * The select_sectype function should either return the vol->sectype
2247 * that was specified, or "Unspecified" if that sectype was not
2248 * compatible with the given NEGOTIATE request.
2249 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302250 if (server->ops->select_sectype(server, vol->sectype)
2251 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002252 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002253
Jeff Layton3f618222013-06-12 19:52:14 -05002254 /*
2255 * Now check if signing mode is acceptable. No need to check
2256 * global_secflags at this point since if MUST_SIGN is set then
2257 * the server->sign had better be too.
2258 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002259 if (vol->sign && !server->sign)
2260 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002261
2262 return true;
2263}
2264
Jeff Layton9fa114f2012-11-26 11:09:57 -05002265static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002266{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002267 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2268
Jeff Laytona0b3df52013-05-24 07:40:59 -04002269 if (vol->nosharesock)
2270 return 0;
2271
Steve French9764c022017-09-17 10:41:35 -05002272 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002273 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2274 return 0;
2275
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002276 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2277 return 0;
2278
2279 if (!match_address(server, addr,
2280 (struct sockaddr *)&vol->srcaddr))
2281 return 0;
2282
2283 if (!match_port(server, addr))
2284 return 0;
2285
2286 if (!match_security(server, vol))
2287 return 0;
2288
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002289 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002290 return 0;
2291
Long Li8339dd32017-11-07 01:54:55 -07002292 if (server->rdma != vol->rdma)
2293 return 0;
2294
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002295 return 1;
2296}
2297
Paulo Alcantara54be1f62018-11-14 16:01:21 -02002298struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002299cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002300{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002301 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302303 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002304 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002305 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002306 continue;
2307
Jeff Laytone7ddee92008-11-14 13:44:38 -05002308 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302309 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002310 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002311 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302313 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return NULL;
2315}
2316
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002317void
2318cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002320 struct task_struct *task;
2321
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302322 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002323 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302324 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002325 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002327
Rob Landleyf1d0c992011-01-22 15:44:05 -06002328 put_net(cifs_net_ns(server));
2329
Jeff Laytone7ddee92008-11-14 13:44:38 -05002330 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302331 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002332
Jeff Laytonc74093b2011-01-11 07:24:23 -05002333 cancel_delayed_work_sync(&server->echo);
2334
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002335 if (from_reconnect)
2336 /*
2337 * Avoid deadlock here: reconnect work calls
2338 * cifs_put_tcp_session() at its end. Need to be sure
2339 * that reconnect work does nothing with server pointer after
2340 * that step.
2341 */
2342 cancel_delayed_work(&server->reconnect);
2343 else
2344 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002345
Jeff Laytone7ddee92008-11-14 13:44:38 -05002346 spin_lock(&GlobalMid_Lock);
2347 server->tcpStatus = CifsExiting;
2348 spin_unlock(&GlobalMid_Lock);
2349
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002350 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302351 cifs_fscache_release_client_cookie(server);
2352
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002353 kfree(server->session_key.response);
2354 server->session_key.response = NULL;
2355 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002356
2357 task = xchg(&server->tsk, NULL);
2358 if (task)
2359 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Jeff Layton63c038c2008-12-01 18:41:46 -05002362static struct TCP_Server_Info *
2363cifs_get_tcp_session(struct smb_vol *volume_info)
2364{
2365 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002366 int rc;
2367
Joe Perchesf96637b2013-05-04 22:12:25 -05002368 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002369
2370 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002371 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002372 if (tcp_ses)
2373 return tcp_ses;
2374
2375 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2376 if (!tcp_ses) {
2377 rc = -ENOMEM;
2378 goto out_err;
2379 }
2380
Jeff Layton23db65f2012-05-15 12:20:51 -04002381 tcp_ses->ops = volume_info->ops;
2382 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002383 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002384 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2385 if (IS_ERR(tcp_ses->hostname)) {
2386 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002387 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002388 }
2389
2390 tcp_ses->noblocksnd = volume_info->noblocksnd;
2391 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002392 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002393 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002394 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002395 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002396 init_waitqueue_head(&tcp_ses->response_q);
2397 init_waitqueue_head(&tcp_ses->request_q);
2398 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2399 mutex_init(&tcp_ses->srv_mutex);
2400 memcpy(tcp_ses->workstation_RFC1001_name,
2401 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2402 memcpy(tcp_ses->server_RFC1001_name,
2403 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002404 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002405 tcp_ses->sequence_number = 0;
Steve French9e1a37d2018-09-19 02:38:17 -05002406 tcp_ses->reconnect_instance = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002407 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002408 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002409 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2410 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002411 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002412 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2413 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002414 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2415 sizeof(tcp_ses->srcaddr));
2416 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2417 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002418 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002419 /*
2420 * at this point we are the only ones with the pointer
2421 * to the struct since the kernel thread not created yet
2422 * no need to spinlock this init of tcpStatus or srv_count
2423 */
2424 tcp_ses->tcpStatus = CifsNew;
2425 ++tcp_ses->srv_count;
2426
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002427 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2428 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2429 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2430 else
2431 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002432 if (tcp_ses->rdma) {
2433#ifndef CONFIG_CIFS_SMB_DIRECT
2434 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2435 rc = -ENOENT;
2436 goto out_err_crypto_release;
2437#endif
2438 tcp_ses->smbd_conn = smbd_get_connection(
2439 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2440 if (tcp_ses->smbd_conn) {
2441 cifs_dbg(VFS, "RDMA transport established\n");
2442 rc = 0;
2443 goto smbd_connected;
2444 } else {
2445 rc = -ENOENT;
2446 goto out_err_crypto_release;
2447 }
2448 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002449 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002450 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002451 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002452 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002453 }
Long Li2f894642017-11-22 17:38:34 -07002454smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002455 /*
2456 * since we're in a cifs function already, we know that
2457 * this will succeed. No need for try_module_get().
2458 */
2459 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002460 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002461 tcp_ses, "cifsd");
2462 if (IS_ERR(tcp_ses->tsk)) {
2463 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002464 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002465 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002466 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002467 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002468 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002469
2470 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302471 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002472 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302473 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002474
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302475 cifs_fscache_get_client_cookie(tcp_ses);
2476
Jeff Laytonc74093b2011-01-11 07:24:23 -05002477 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002478 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002479
Jeff Layton63c038c2008-12-01 18:41:46 -05002480 return tcp_ses;
2481
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002482out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002483 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002484
Rob Landleyf1d0c992011-01-22 15:44:05 -06002485 put_net(cifs_net_ns(tcp_ses));
2486
Jeff Layton63c038c2008-12-01 18:41:46 -05002487out_err:
2488 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002489 if (!IS_ERR(tcp_ses->hostname))
2490 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002491 if (tcp_ses->ssocket)
2492 sock_release(tcp_ses->ssocket);
2493 kfree(tcp_ses);
2494 }
2495 return ERR_PTR(rc);
2496}
2497
Steve French96daf2b2011-05-27 04:34:02 +00002498static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002499{
Jeff Layton3f618222013-06-12 19:52:14 -05002500 if (vol->sectype != Unspecified &&
2501 vol->sectype != ses->sectype)
2502 return 0;
2503
2504 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002505 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002506 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002507 return 0;
2508 break;
2509 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002510 /* NULL username means anonymous session */
2511 if (ses->user_name == NULL) {
2512 if (!vol->nullauth)
2513 return 0;
2514 break;
2515 }
2516
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002517 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002518 if (strncmp(ses->user_name,
2519 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002520 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002521 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002522 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002523 ses->password != NULL &&
2524 strncmp(ses->password,
2525 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002526 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002527 return 0;
2528 }
2529 return 1;
2530}
2531
Aurelien Aptelb327a712018-01-24 13:46:10 +01002532/**
2533 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2534 *
2535 * A new IPC connection is made and stored in the session
2536 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2537 */
2538static int
2539cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2540{
2541 int rc = 0, xid;
2542 struct cifs_tcon *tcon;
2543 struct nls_table *nls_codepage;
2544 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2545 bool seal = false;
2546
2547 /*
2548 * If the mount request that resulted in the creation of the
2549 * session requires encryption, force IPC to be encrypted too.
2550 */
2551 if (volume_info->seal) {
2552 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2553 seal = true;
2554 else {
2555 cifs_dbg(VFS,
2556 "IPC: server doesn't support encryption\n");
2557 return -EOPNOTSUPP;
2558 }
2559 }
2560
2561 tcon = tconInfoAlloc();
2562 if (tcon == NULL)
2563 return -ENOMEM;
2564
Thomas Werschlein395a2072018-08-30 18:29:20 +02002565 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002566
2567 /* cannot fail */
2568 nls_codepage = load_nls_default();
2569
2570 xid = get_xid();
2571 tcon->ses = ses;
2572 tcon->ipc = true;
2573 tcon->seal = seal;
2574 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2575 free_xid(xid);
2576
2577 if (rc) {
2578 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2579 tconInfoFree(tcon);
2580 goto out;
2581 }
2582
2583 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2584
2585 ses->tcon_ipc = tcon;
2586out:
2587 unload_nls(nls_codepage);
2588 return rc;
2589}
2590
2591/**
2592 * cifs_free_ipc - helper to release the session IPC tcon
2593 *
2594 * Needs to be called everytime a session is destroyed
2595 */
2596static int
2597cifs_free_ipc(struct cifs_ses *ses)
2598{
2599 int rc = 0, xid;
2600 struct cifs_tcon *tcon = ses->tcon_ipc;
2601
2602 if (tcon == NULL)
2603 return 0;
2604
2605 if (ses->server->ops->tree_disconnect) {
2606 xid = get_xid();
2607 rc = ses->server->ops->tree_disconnect(xid, tcon);
2608 free_xid(xid);
2609 }
2610
2611 if (rc)
2612 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2613
2614 tconInfoFree(tcon);
2615 ses->tcon_ipc = NULL;
2616 return rc;
2617}
2618
Steve French96daf2b2011-05-27 04:34:02 +00002619static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002620cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
Steve French96daf2b2011-05-27 04:34:02 +00002622 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302624 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002625 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002626 if (ses->status == CifsExiting)
2627 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002628 if (!match_session(ses, vol))
2629 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002630 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302631 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002632 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302634 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return NULL;
2636}
2637
Jeff Layton14fbf502008-11-14 13:53:46 -05002638static void
Steve French96daf2b2011-05-27 04:34:02 +00002639cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002640{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002641 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002642 struct TCP_Server_Info *server = ses->server;
2643
Joe Perchesf96637b2013-05-04 22:12:25 -05002644 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002645
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302646 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002647 if (ses->status == CifsExiting) {
2648 spin_unlock(&cifs_tcp_ses_lock);
2649 return;
2650 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002651 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302652 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002653 return;
2654 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002655 if (ses->status == CifsGood)
2656 ses->status = CifsExiting;
2657 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002658
Aurelien Aptelb327a712018-01-24 13:46:10 +01002659 cifs_free_ipc(ses);
2660
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002661 if (ses->status == CifsExiting && server->ops->logoff) {
2662 xid = get_xid();
2663 rc = server->ops->logoff(xid, ses);
2664 if (rc)
2665 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2666 __func__, rc);
2667 _free_xid(xid);
2668 }
2669
2670 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002671 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302672 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002673
Jeff Layton14fbf502008-11-14 13:53:46 -05002674 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002675 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002676}
2677
Jeff Layton8a8798a2012-01-17 16:09:15 -05002678#ifdef CONFIG_KEYS
2679
Chen Gang057d6332013-07-19 09:01:36 +08002680/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2681#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002682
2683/* Populate username and pw fields from keyring if possible */
2684static int
2685cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2686{
2687 int rc = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002688 const char *delim, *payload;
2689 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002690 ssize_t len;
2691 struct key *key;
2692 struct TCP_Server_Info *server = ses->server;
2693 struct sockaddr_in *sa;
2694 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002695 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002696
2697 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2698 if (!desc)
2699 return -ENOMEM;
2700
2701 /* try to find an address key first */
2702 switch (server->dstaddr.ss_family) {
2703 case AF_INET:
2704 sa = (struct sockaddr_in *)&server->dstaddr;
2705 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2706 break;
2707 case AF_INET6:
2708 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2709 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2710 break;
2711 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002712 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2713 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002714 rc = -EINVAL;
2715 goto out_err;
2716 }
2717
Joe Perchesf96637b2013-05-04 22:12:25 -05002718 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002719 key = request_key(&key_type_logon, desc, "");
2720 if (IS_ERR(key)) {
2721 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002722 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002723 rc = PTR_ERR(key);
2724 goto out_err;
2725 }
2726
2727 /* didn't work, try to find a domain key */
2728 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002729 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002730 key = request_key(&key_type_logon, desc, "");
2731 if (IS_ERR(key)) {
2732 rc = PTR_ERR(key);
2733 goto out_err;
2734 }
2735 }
2736
2737 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002738 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002739 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002740 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002741 goto out_key_put;
2742 }
2743
2744 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002745 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002746 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002747 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002748 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002749 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2750 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002751 rc = -EINVAL;
2752 goto out_key_put;
2753 }
2754
2755 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002756 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002757 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2758 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002759 rc = -EINVAL;
2760 goto out_key_put;
2761 }
2762
2763 vol->username = kstrndup(payload, len, GFP_KERNEL);
2764 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002765 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2766 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002767 rc = -ENOMEM;
2768 goto out_key_put;
2769 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002770 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002771
2772 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002773 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002774 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002775 rc = -EINVAL;
2776 kfree(vol->username);
2777 vol->username = NULL;
2778 goto out_key_put;
2779 }
2780
2781 ++delim;
2782 vol->password = kstrndup(delim, len, GFP_KERNEL);
2783 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002784 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2785 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002786 rc = -ENOMEM;
2787 kfree(vol->username);
2788 vol->username = NULL;
2789 goto out_key_put;
2790 }
2791
2792out_key_put:
2793 up_read(&key->sem);
2794 key_put(key);
2795out_err:
2796 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002797 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002798 return rc;
2799}
2800#else /* ! CONFIG_KEYS */
2801static inline int
2802cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2803 struct cifs_ses *ses __attribute__((unused)))
2804{
2805 return -ENOSYS;
2806}
2807#endif /* CONFIG_KEYS */
2808
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002809/**
2810 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2811 *
2812 * This function assumes it is being called from cifs_mount() where we
2813 * already got a server reference (server refcount +1). See
2814 * cifs_get_tcon() for refcount explanations.
2815 */
Steve French96daf2b2011-05-27 04:34:02 +00002816static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002817cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2818{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002819 int rc = -ENOMEM;
2820 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002821 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002822 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2823 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002824
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002825 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002826
Jeff Layton4ff67b72010-07-06 20:43:02 -04002827 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002828 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002829 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2830 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002831
Jeff Layton36988c72010-04-24 07:57:43 -04002832 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002833 rc = cifs_negotiate_protocol(xid, ses);
2834 if (rc) {
2835 mutex_unlock(&ses->session_mutex);
2836 /* problem -- put our ses reference */
2837 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002838 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002839 return ERR_PTR(rc);
2840 }
Jeff Layton36988c72010-04-24 07:57:43 -04002841 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002842 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002843 rc = cifs_setup_session(xid, ses,
2844 volume_info->local_nls);
2845 if (rc) {
2846 mutex_unlock(&ses->session_mutex);
2847 /* problem -- put our reference */
2848 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002849 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002850 return ERR_PTR(rc);
2851 }
2852 }
2853 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002854
2855 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002856 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002857 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002858 return ses;
2859 }
2860
Joe Perchesf96637b2013-05-04 22:12:25 -05002861 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002862 ses = sesInfoAlloc();
2863 if (ses == NULL)
2864 goto get_ses_fail;
2865
2866 /* new SMB session uses our server ref */
2867 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002868 if (server->dstaddr.ss_family == AF_INET6)
2869 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002870 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002871 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002872
Steve French8727c8a2011-02-25 01:11:56 -06002873 if (volume_info->username) {
2874 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2875 if (!ses->user_name)
2876 goto get_ses_fail;
2877 }
Jeff Layton36988c72010-04-24 07:57:43 -04002878
2879 /* volume_info->password freed at unmount */
2880 if (volume_info->password) {
2881 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2882 if (!ses->password)
2883 goto get_ses_fail;
2884 }
2885 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002886 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2887 if (!ses->domainName)
2888 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002889 }
Germano Percossi39566442016-12-15 12:31:18 +05302890 if (volume_info->domainauto)
2891 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002892 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002893 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002894
Jeff Layton28e11bd2013-05-26 07:01:00 -04002895 ses->sectype = volume_info->sectype;
2896 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002897
2898 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002899 rc = cifs_negotiate_protocol(xid, ses);
2900 if (!rc)
2901 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002902 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002903 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002904 goto get_ses_fail;
2905
2906 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302907 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002908 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302909 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002910
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002911 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002912
2913 cifs_setup_ipc(ses, volume_info);
2914
Jeff Layton36988c72010-04-24 07:57:43 -04002915 return ses;
2916
2917get_ses_fail:
2918 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002919 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002920 return ERR_PTR(rc);
2921}
2922
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002923static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002924{
2925 if (tcon->tidStatus == CifsExiting)
2926 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002927 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002928 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002929 if (tcon->seal != volume_info->seal)
2930 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002931 if (tcon->snapshot_time != volume_info->snapshot_time)
2932 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002933 return 1;
2934}
2935
Steve French96daf2b2011-05-27 04:34:02 +00002936static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06002937cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002940 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302942 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002943 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002944 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002945 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002946 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002947 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302948 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return tcon;
2950 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302951 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 return NULL;
2953}
2954
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002955void
Steve French96daf2b2011-05-27 04:34:02 +00002956cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002957{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002958 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002959 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002960
Aurelien Aptelb327a712018-01-24 13:46:10 +01002961 /*
2962 * IPC tcon share the lifetime of their session and are
2963 * destroyed in the session put function
2964 */
2965 if (tcon == NULL || tcon->ipc)
2966 return;
2967
2968 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002969 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302970 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002971 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302972 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002973 return;
2974 }
2975
2976 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302977 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002978
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002979 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002980 if (ses->server->ops->tree_disconnect)
2981 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002982 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002983
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302984 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002985 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002986 cifs_put_smb_ses(ses);
2987}
2988
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002989/**
2990 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
2991 *
2992 * - tcon refcount is the number of mount points using the tcon.
2993 * - ses refcount is the number of tcon using the session.
2994 *
2995 * 1. This function assumes it is being called from cifs_mount() where
2996 * we already got a session reference (ses refcount +1).
2997 *
2998 * 2. Since we're in the context of adding a mount point, the end
2999 * result should be either:
3000 *
3001 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3002 * its session refcount incremented (1 new tcon). This +1 was
3003 * already done in (1).
3004 *
3005 * b) an existing tcon with refcount+1 (add a mount point to it) and
3006 * identical ses refcount (no new tcon). Because of (1) we need to
3007 * decrement the ses refcount.
3008 */
Steve French96daf2b2011-05-27 04:34:02 +00003009static struct cifs_tcon *
3010cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003011{
3012 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003013 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003014
Steve French8b217fe2016-11-11 22:36:20 -06003015 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003016 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003017 /*
3018 * tcon has refcount already incremented but we need to
3019 * decrement extra ses reference gotten by caller (case b)
3020 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003021 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003022 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003023 return tcon;
3024 }
3025
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003026 if (!ses->server->ops->tree_connect) {
3027 rc = -ENOSYS;
3028 goto out_fail;
3029 }
3030
Jeff Laytond00c28d2010-04-24 07:57:44 -04003031 tcon = tconInfoAlloc();
3032 if (tcon == NULL) {
3033 rc = -ENOMEM;
3034 goto out_fail;
3035 }
3036
Steve French8b217fe2016-11-11 22:36:20 -06003037 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003038 if (ses->server->vals->protocol_id == 0) {
3039 cifs_dbg(VFS,
3040 "Use SMB2 or later for snapshot mount option\n");
3041 rc = -EOPNOTSUPP;
3042 goto out_fail;
3043 } else
3044 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003045 }
3046
Jeff Laytond00c28d2010-04-24 07:57:44 -04003047 tcon->ses = ses;
3048 if (volume_info->password) {
3049 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3050 if (!tcon->password) {
3051 rc = -ENOMEM;
3052 goto out_fail;
3053 }
3054 }
3055
Steve French23657ad2018-04-22 15:14:58 -05003056 if (volume_info->seal) {
3057 if (ses->server->vals->protocol_id == 0) {
3058 cifs_dbg(VFS,
3059 "SMB3 or later required for encryption\n");
3060 rc = -EOPNOTSUPP;
3061 goto out_fail;
3062 } else if (tcon->ses->server->capabilities &
3063 SMB2_GLOBAL_CAP_ENCRYPTION)
3064 tcon->seal = true;
3065 else {
3066 cifs_dbg(VFS, "Encryption is not supported on share\n");
3067 rc = -EOPNOTSUPP;
3068 goto out_fail;
3069 }
3070 }
3071
Steve French8505c8b2018-06-18 14:01:59 -05003072 if (volume_info->linux_ext) {
3073 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003074 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003075 printk_once(KERN_WARNING
3076 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003077 } else {
3078 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3079 rc = -EOPNOTSUPP;
3080 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003081 }
Steve Frenchb3266142018-05-20 23:41:10 -05003082 }
Steve Frenchb3266142018-05-20 23:41:10 -05003083
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003084 /*
3085 * BB Do we need to wrap session_mutex around this TCon call and Unix
3086 * SetFS as we do on SessSetup and reconnect?
3087 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003088 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003089 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3090 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003091 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003092 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003093 if (rc)
3094 goto out_fail;
3095
Steve Frenchb618f002015-11-03 09:15:03 -06003096 tcon->use_persistent = false;
3097 /* check if SMB2 or later, CIFS does not support persistent handles */
3098 if (volume_info->persistent) {
3099 if (ses->server->vals->protocol_id == 0) {
3100 cifs_dbg(VFS,
3101 "SMB3 or later required for persistent handles\n");
3102 rc = -EOPNOTSUPP;
3103 goto out_fail;
3104 } else if (ses->server->capabilities &
3105 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3106 tcon->use_persistent = true;
3107 else /* persistent handles requested but not supported */ {
3108 cifs_dbg(VFS,
3109 "Persistent handles not supported on share\n");
3110 rc = -EOPNOTSUPP;
3111 goto out_fail;
3112 }
3113 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3114 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3115 && (volume_info->nopersistent == false)) {
3116 cifs_dbg(FYI, "enabling persistent handles\n");
3117 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003118 } else if (volume_info->resilient) {
3119 if (ses->server->vals->protocol_id == 0) {
3120 cifs_dbg(VFS,
3121 "SMB2.1 or later required for resilient handles\n");
3122 rc = -EOPNOTSUPP;
3123 goto out_fail;
3124 }
3125 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003126 }
3127
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003128 /*
3129 * We can have only one retry value for a connection to a share so for
3130 * resources mounted more than once to the same server share the last
3131 * value passed in for the retry flag is used.
3132 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003133 tcon->retry = volume_info->retry;
3134 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003135 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003136 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003137 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003138
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303139 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003140 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303141 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003142
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303143 cifs_fscache_get_super_cookie(tcon);
3144
Jeff Laytond00c28d2010-04-24 07:57:44 -04003145 return tcon;
3146
3147out_fail:
3148 tconInfoFree(tcon);
3149 return ERR_PTR(rc);
3150}
3151
Jeff Layton9d002df2010-10-06 19:51:11 -04003152void
3153cifs_put_tlink(struct tcon_link *tlink)
3154{
3155 if (!tlink || IS_ERR(tlink))
3156 return;
3157
3158 if (!atomic_dec_and_test(&tlink->tl_count) ||
3159 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3160 tlink->tl_time = jiffies;
3161 return;
3162 }
3163
3164 if (!IS_ERR(tlink_tcon(tlink)))
3165 cifs_put_tcon(tlink_tcon(tlink));
3166 kfree(tlink);
3167 return;
3168}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003169
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003170static int
3171compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3172{
3173 struct cifs_sb_info *old = CIFS_SB(sb);
3174 struct cifs_sb_info *new = mnt_data->cifs_sb;
3175
3176 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3177 return 0;
3178
3179 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3180 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3181 return 0;
3182
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003183 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003184 * We want to share sb only if we don't specify an r/wsize or
3185 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003186 */
3187 if (new->wsize && new->wsize < old->wsize)
3188 return 0;
3189
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003190 if (new->rsize && new->rsize < old->rsize)
3191 return 0;
3192
Eric W. Biederman1f682332013-02-06 01:20:20 -08003193 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003194 return 0;
3195
3196 if (old->mnt_file_mode != new->mnt_file_mode ||
3197 old->mnt_dir_mode != new->mnt_dir_mode)
3198 return 0;
3199
3200 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3201 return 0;
3202
3203 if (old->actimeo != new->actimeo)
3204 return 0;
3205
3206 return 1;
3207}
3208
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003209static int
3210match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3211{
3212 struct cifs_sb_info *old = CIFS_SB(sb);
3213 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003214 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3215 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003216
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003217 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003218 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003219 else if (!old_set && !new_set)
3220 return 1;
3221
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003222 return 0;
3223}
3224
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003225int
3226cifs_match_super(struct super_block *sb, void *data)
3227{
3228 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3229 struct smb_vol *volume_info;
3230 struct cifs_sb_info *cifs_sb;
3231 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003232 struct cifs_ses *ses;
3233 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003234 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003235 int rc = 0;
3236
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003237 spin_lock(&cifs_tcp_ses_lock);
3238 cifs_sb = CIFS_SB(sb);
3239 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3240 if (IS_ERR(tlink)) {
3241 spin_unlock(&cifs_tcp_ses_lock);
3242 return rc;
3243 }
3244 tcon = tlink_tcon(tlink);
3245 ses = tcon->ses;
3246 tcp_srv = ses->server;
3247
3248 volume_info = mnt_data->vol;
3249
Jeff Layton9fa114f2012-11-26 11:09:57 -05003250 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003251 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003252 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003253 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003254 rc = 0;
3255 goto out;
3256 }
3257
3258 rc = compare_mount_options(sb, mnt_data);
3259out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003260 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003261 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003262 return rc;
3263}
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003266get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003267 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3268 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 int rc = 0;
3271
Aurelien Aptelb327a712018-01-24 13:46:10 +01003272 if (!ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003273 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003275 *num_referrals = 0;
3276 *referrals = NULL;
3277
Aurelien Aptelb327a712018-01-24 13:46:10 +01003278 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3279 referrals, num_referrals,
3280 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return rc;
3282}
3283
Jeff Layton09e50d52008-07-23 10:11:19 -04003284#ifdef CONFIG_DEBUG_LOCK_ALLOC
3285static struct lock_class_key cifs_key[2];
3286static struct lock_class_key cifs_slock_key[2];
3287
3288static inline void
3289cifs_reclassify_socket4(struct socket *sock)
3290{
3291 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003292 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003293 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3294 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3295}
3296
3297static inline void
3298cifs_reclassify_socket6(struct socket *sock)
3299{
3300 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003301 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003302 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3303 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3304}
3305#else
3306static inline void
3307cifs_reclassify_socket4(struct socket *sock)
3308{
3309}
3310
3311static inline void
3312cifs_reclassify_socket6(struct socket *sock)
3313{
3314}
3315#endif
3316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003318static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
Steve French50c2f752007-07-13 00:33:32 +00003320 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
Steve French50c2f752007-07-13 00:33:32 +00003322 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 /* mask a nibble at a time and encode */
3324 target[j] = 'A' + (0x0F & (source[i] >> 4));
3325 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003326 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 }
3328
3329}
3330
Ben Greear3eb9a882010-09-01 17:06:02 -07003331static int
3332bind_socket(struct TCP_Server_Info *server)
3333{
3334 int rc = 0;
3335 if (server->srcaddr.ss_family != AF_UNSPEC) {
3336 /* Bind to the specified local IP address */
3337 struct socket *socket = server->ssocket;
3338 rc = socket->ops->bind(socket,
3339 (struct sockaddr *) &server->srcaddr,
3340 sizeof(server->srcaddr));
3341 if (rc < 0) {
3342 struct sockaddr_in *saddr4;
3343 struct sockaddr_in6 *saddr6;
3344 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3345 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3346 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003347 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3348 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003349 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003350 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3351 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003352 }
3353 }
3354 return rc;
3355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003358ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
3360 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003361 /*
3362 * some servers require RFC1001 sessinit before sending
3363 * negprot - BB check reconnection in case where second
3364 * sessinit is sent but no second negprot
3365 */
3366 struct rfc1002_session_packet *ses_init_buf;
3367 struct smb_hdr *smb_buf;
3368 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3369 GFP_KERNEL);
3370 if (ses_init_buf) {
3371 ses_init_buf->trailer.session_req.called_len = 32;
3372
Colin Ian King997152f2016-01-25 16:25:54 +00003373 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003374 rfc1002mangle(ses_init_buf->trailer.
3375 session_req.called_name,
3376 server->server_RFC1001_name,
3377 RFC1001_NAME_LEN_WITH_NULL);
3378 else
3379 rfc1002mangle(ses_init_buf->trailer.
3380 session_req.called_name,
3381 DEFAULT_CIFS_CALLED_NAME,
3382 RFC1001_NAME_LEN_WITH_NULL);
3383
3384 ses_init_buf->trailer.session_req.calling_len = 32;
3385
3386 /*
3387 * calling name ends in null (byte 16) from old smb
3388 * convention.
3389 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003390 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003391 rfc1002mangle(ses_init_buf->trailer.
3392 session_req.calling_name,
3393 server->workstation_RFC1001_name,
3394 RFC1001_NAME_LEN_WITH_NULL);
3395 else
3396 rfc1002mangle(ses_init_buf->trailer.
3397 session_req.calling_name,
3398 "LINUX_CIFS_CLNT",
3399 RFC1001_NAME_LEN_WITH_NULL);
3400
3401 ses_init_buf->trailer.session_req.scope1 = 0;
3402 ses_init_buf->trailer.session_req.scope2 = 0;
3403 smb_buf = (struct smb_hdr *)ses_init_buf;
3404
3405 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003406 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003407 rc = smb_send(server, smb_buf, 0x44);
3408 kfree(ses_init_buf);
3409 /*
3410 * RFC1001 layer in at least one server
3411 * requires very short break before negprot
3412 * presumably because not expecting negprot
3413 * to follow so fast. This is a simple
3414 * solution that works without
3415 * complicating the code and causes no
3416 * significant slowing down on mount
3417 * for everyone else
3418 */
3419 usleep_range(1000, 2000);
3420 }
3421 /*
3422 * else the negprot may still work without this
3423 * even though malloc failed
3424 */
3425
3426 return rc;
3427}
3428
3429static int
3430generic_ip_connect(struct TCP_Server_Info *server)
3431{
3432 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003433 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003434 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003435 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003436 struct sockaddr *saddr;
3437
3438 saddr = (struct sockaddr *) &server->dstaddr;
3439
3440 if (server->dstaddr.ss_family == AF_INET6) {
3441 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3442 slen = sizeof(struct sockaddr_in6);
3443 sfamily = AF_INET6;
3444 } else {
3445 sport = ((struct sockaddr_in *) saddr)->sin_port;
3446 slen = sizeof(struct sockaddr_in);
3447 sfamily = AF_INET;
3448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003450 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003451 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3452 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003454 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003455 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003458
3459 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003460 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003461 server->ssocket = socket;
3462 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003463 if (sfamily == AF_INET6)
3464 cifs_reclassify_socket6(socket);
3465 else
3466 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
3468
Ben Greear3eb9a882010-09-01 17:06:02 -07003469 rc = bind_socket(server);
3470 if (rc < 0)
3471 return rc;
3472
Jeff Laytond5c56052008-12-01 18:42:33 -05003473 /*
3474 * Eventually check for other socket options to change from
3475 * the default. sock_setsockopt not used because it expects
3476 * user space buffer
3477 */
3478 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003479 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003480
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003481 /* make the bufsizes depend on wsize/rsize and max requests */
3482 if (server->noautotune) {
3483 if (socket->sk->sk_sndbuf < (200 * 1024))
3484 socket->sk->sk_sndbuf = 200 * 1024;
3485 if (socket->sk->sk_rcvbuf < (140 * 1024))
3486 socket->sk->sk_rcvbuf = 140 * 1024;
3487 }
3488
Steve French6a5fa2362010-01-01 01:28:43 +00003489 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003490 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003491 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3492 (char *)&val, sizeof(val));
3493 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003494 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3495 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003496 }
3497
Joe Perchesf96637b2013-05-04 22:12:25 -05003498 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003499 socket->sk->sk_sndbuf,
3500 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3501
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003502 rc = socket->ops->connect(socket, saddr, slen, 0);
3503 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003504 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003505 sock_release(socket);
3506 server->ssocket = NULL;
3507 return rc;
3508 }
3509
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003510 if (sport == htons(RFC1001_PORT))
3511 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 return rc;
3514}
3515
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003516static int
3517ip_connect(struct TCP_Server_Info *server)
3518{
Steve French6da97912011-03-13 18:55:55 +00003519 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003520 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3521 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3522
3523 if (server->dstaddr.ss_family == AF_INET6)
3524 sport = &addr6->sin6_port;
3525 else
3526 sport = &addr->sin_port;
3527
3528 if (*sport == 0) {
3529 int rc;
3530
3531 /* try with 445 port at first */
3532 *sport = htons(CIFS_PORT);
3533
3534 rc = generic_ip_connect(server);
3535 if (rc >= 0)
3536 return rc;
3537
3538 /* if it failed, try with 139 port */
3539 *sport = htons(RFC1001_PORT);
3540 }
3541
3542 return generic_ip_connect(server);
3543}
3544
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003545void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003546 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003547{
3548 /* if we are reconnecting then should we check to see if
3549 * any requested capabilities changed locally e.g. via
3550 * remount but we can not do much about it here
3551 * if they have (even if we could detect it by the following)
3552 * Perhaps we could add a backpointer to array of sb from tcon
3553 * or if we change to make all sb to same share the same
3554 * sb as NFS - then we only have one backpointer to sb.
3555 * What if we wanted to mount the server share twice once with
3556 * and once without posixacls or posix paths? */
3557 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003558
Steve Frenchc18c8422007-07-18 23:21:09 +00003559 if (vol_info && vol_info->no_linux_ext) {
3560 tcon->fsUnixInfo.Capability = 0;
3561 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003562 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003563 return;
3564 } else if (vol_info)
3565 tcon->unix_ext = 1; /* Unix Extensions supported */
3566
3567 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003568 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003569 return;
3570 }
Steve French50c2f752007-07-13 00:33:32 +00003571
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003572 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003573 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003574 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003575 /* check for reconnect case in which we do not
3576 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003577 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003578 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003579 originally at mount time */
3580 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3581 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003582 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3583 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003584 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003585 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003586 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003587 cifs_dbg(VFS, "possible reconnect error\n");
3588 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003589 }
Steve French8af18972007-02-14 04:42:51 +00003590 }
Steve French50c2f752007-07-13 00:33:32 +00003591
Steve French6848b732011-05-26 18:38:54 +00003592 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003593 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003594
Steve French8af18972007-02-14 04:42:51 +00003595 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003596 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003597 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003598 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003599 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003600 if (cifs_sb)
3601 cifs_sb->mnt_cifs_flags |=
3602 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003603 }
3604
Steve French75865f8c2007-06-24 18:30:48 +00003605 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003606 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003607 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003608 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003609 if (cifs_sb)
3610 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003611 CIFS_MOUNT_POSIX_PATHS;
3612 }
Steve French50c2f752007-07-13 00:33:32 +00003613
Joe Perchesf96637b2013-05-04 22:12:25 -05003614 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003615#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003616 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003617 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003618 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003619 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003620 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003621 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003622 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003623 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003624 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003625 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003626 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003627 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003628 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003629 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003630 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003631 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003632 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003633 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003634#endif /* CIFS_DEBUG2 */
3635 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003636 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003637 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003638 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003639 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 +00003640
Steve French8af18972007-02-14 04:42:51 +00003641 }
3642 }
3643}
3644
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003645int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003646 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003647{
Jeff Layton2de970f2010-10-06 19:51:12 -04003648 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3649
Al Viro2ced6f62011-06-17 09:20:04 -04003650 spin_lock_init(&cifs_sb->tlink_tree_lock);
3651 cifs_sb->tlink_tree = RB_ROOT;
3652
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003653 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003654 * Temporarily set r/wsize for matching superblock. If we end up using
3655 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003656 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003657 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003658 cifs_sb->wsize = pvolume_info->wsize;
3659
Steve French3b795212008-11-13 19:45:32 +00003660 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3661 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3662 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3663 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003664 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3665 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003666
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303667 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003668 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303669
Aurelien Aptel83930722018-09-20 18:10:25 -07003670 if (pvolume_info->nodfs)
3671 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
Steve French3b795212008-11-13 19:45:32 +00003672 if (pvolume_info->noperm)
3673 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3674 if (pvolume_info->setuids)
3675 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003676 if (pvolume_info->setuidfromacl)
3677 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003678 if (pvolume_info->server_ino)
3679 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3680 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003681 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3682 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003683 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3684 if (pvolume_info->no_xattr)
3685 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3686 if (pvolume_info->sfu_emul)
3687 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3688 if (pvolume_info->nobrl)
3689 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003690 if (pvolume_info->nohandlecache)
3691 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003692 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003693 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003694 if (pvolume_info->mand_lock)
3695 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003696 if (pvolume_info->rwpidforward)
3697 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003698 if (pvolume_info->cifs_acl)
3699 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003700 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003701 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003702 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3703 }
3704 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003705 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003706 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3707 }
Steve French3b795212008-11-13 19:45:32 +00003708 if (pvolume_info->override_uid)
3709 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3710 if (pvolume_info->override_gid)
3711 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3712 if (pvolume_info->dynperm)
3713 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303714 if (pvolume_info->fsc)
3715 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003716 if (pvolume_info->multiuser)
3717 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3718 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003719 if (pvolume_info->strict_io)
3720 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003721 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003722 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003723 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3724 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003725 if (pvolume_info->mfsymlinks) {
3726 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003727 /*
3728 * Our SFU ("Services for Unix" emulation does not allow
3729 * creating symlinks but does allow reading existing SFU
3730 * symlinks (it does allow both creating and reading SFU
3731 * style mknod and FIFOs though). When "mfsymlinks" and
3732 * "sfu" are both enabled at the same time, it allows
3733 * reading both types of symlinks, but will only create
3734 * them with mfsymlinks format. This allows better
3735 * Apple compatibility (probably better for Samba too)
3736 * while still recognizing old Windows style symlinks.
3737 */
3738 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003739 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003740 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003741 }
Steve French3b795212008-11-13 19:45:32 +00003742
3743 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003744 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003745
3746 if (pvolume_info->prepath) {
3747 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3748 if (cifs_sb->prepath == NULL)
3749 return -ENOMEM;
3750 }
3751
3752 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003753}
3754
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003755void
3756cifs_cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003757{
Sean Finneyb9468452011-04-11 13:19:32 +00003758 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003759 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003760 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003761 kfree(volume_info->domainname);
3762 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003763 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003764}
3765
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003766void
3767cifs_cleanup_volume_info(struct smb_vol *volume_info)
3768{
3769 if (!volume_info)
3770 return;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003771 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003772 kfree(volume_info);
3773}
3774
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003775/* Release all succeed connections */
3776static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
3777 unsigned int xid,
3778 struct TCP_Server_Info *server,
3779 struct cifs_ses *ses, struct cifs_tcon *tcon)
3780{
3781 int rc = 0;
3782
3783 if (tcon)
3784 cifs_put_tcon(tcon);
3785 else if (ses)
3786 cifs_put_smb_ses(ses);
3787 else if (server)
3788 cifs_put_tcp_session(server, 0);
3789 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3790 free_xid(xid);
3791}
3792
3793/* Get connections for tcp, ses and tcon */
3794static int mount_get_conns(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
3795 unsigned int *xid,
3796 struct TCP_Server_Info **nserver,
3797 struct cifs_ses **nses, struct cifs_tcon **ntcon)
3798{
3799 int rc = 0;
3800 struct TCP_Server_Info *server;
3801 struct cifs_ses *ses;
3802 struct cifs_tcon *tcon;
3803
3804 *nserver = NULL;
3805 *nses = NULL;
3806 *ntcon = NULL;
3807
3808 *xid = get_xid();
3809
3810 /* get a reference to a tcp session */
3811 server = cifs_get_tcp_session(vol);
3812 if (IS_ERR(server)) {
3813 rc = PTR_ERR(server);
3814 return rc;
3815 }
3816
3817 *nserver = server;
3818
3819 if ((vol->max_credits < 20) || (vol->max_credits > 60000))
3820 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3821 else
3822 server->max_credits = vol->max_credits;
3823
3824 /* get a reference to a SMB session */
3825 ses = cifs_get_smb_ses(server, vol);
3826 if (IS_ERR(ses)) {
3827 rc = PTR_ERR(ses);
3828 return rc;
3829 }
3830
3831 *nses = ses;
3832
3833 if ((vol->persistent == true) && (!(ses->server->capabilities &
3834 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
3835 cifs_dbg(VFS, "persistent handles not supported by server\n");
3836 return -EOPNOTSUPP;
3837 }
3838
3839 /* search for existing tcon to this server share */
3840 tcon = cifs_get_tcon(ses, vol);
3841 if (IS_ERR(tcon)) {
3842 rc = PTR_ERR(tcon);
3843 return rc;
3844 }
3845
3846 *ntcon = tcon;
3847
3848 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3849 if (tcon->posix_extensions)
3850 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3851
3852 /* tell server which Unix caps we support */
3853 if (cap_unix(tcon->ses)) {
3854 /*
3855 * reset of caps checks mount to see if unix extensions disabled
3856 * for just this mount.
3857 */
3858 reset_cifs_unix_caps(*xid, tcon, cifs_sb, vol);
3859 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3860 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3861 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
3862 return -EACCES;
3863 } else
3864 tcon->unix_ext = 0; /* server does not support them */
3865
3866 /* do not care if a following call succeed - informational */
3867 if (!tcon->pipe && server->ops->qfs_tcon)
3868 server->ops->qfs_tcon(*xid, tcon);
3869
3870 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, vol);
3871 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, vol);
3872
3873 return 0;
3874}
3875
3876static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3877 struct cifs_tcon *tcon)
3878{
3879 struct tcon_link *tlink;
3880
3881 /* hang the tcon off of the superblock */
3882 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3883 if (tlink == NULL)
3884 return -ENOMEM;
3885
3886 tlink->tl_uid = ses->linux_uid;
3887 tlink->tl_tcon = tcon;
3888 tlink->tl_time = jiffies;
3889 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3890 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3891
3892 cifs_sb->master_tlink = tlink;
3893 spin_lock(&cifs_sb->tlink_tree_lock);
3894 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3895 spin_unlock(&cifs_sb->tlink_tree_lock);
3896
3897 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3898 TLINK_IDLE_EXPIRE);
3899 return 0;
3900}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003901
Steve French2d6d5892009-04-09 00:36:44 +00003902#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003903/*
3904 * cifs_build_path_to_root returns full path to root when we do not have an
3905 * exiting connection (tcon)
3906 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003907static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003908build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003909 const struct cifs_sb_info *cifs_sb)
3910{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003911 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003912 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003913 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003914
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003915 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003916 if (full_path == NULL)
3917 return ERR_PTR(-ENOMEM);
3918
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003919 strncpy(full_path, vol->UNC, unc_len);
3920 pos = full_path + unc_len;
3921
3922 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003923 *pos = CIFS_DIR_SEP(cifs_sb);
3924 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003925 pos += pplen;
3926 }
3927
3928 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003929 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003930 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003931 return full_path;
3932}
Sean Finneydd613942011-04-11 13:19:30 +00003933
3934/*
3935 * Perform a dfs referral query for a share and (optionally) prefix
3936 *
Sean Finney046462a2011-04-11 13:19:33 +00003937 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3938 * to a string containing updated options for the submount. Otherwise it
3939 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003940 *
3941 * Returns the rc from get_dfs_path to the caller, which can be used to
3942 * determine whether there were referrals.
3943 */
3944static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003945expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003946 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003947 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003948{
3949 int rc;
3950 unsigned int num_referrals = 0;
3951 struct dfs_info3_param *referrals = NULL;
3952 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3953
Aurelien Aptel83930722018-09-20 18:10:25 -07003954 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
3955 return -EREMOTE;
3956
Sean Finneydd613942011-04-11 13:19:30 +00003957 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3958 if (IS_ERR(full_path))
3959 return PTR_ERR(full_path);
3960
3961 /* For DFS paths, skip the first '\' of the UNC */
3962 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3963
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003964 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003965 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003966
3967 if (!rc && num_referrals > 0) {
3968 char *fake_devname = NULL;
3969
3970 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3971 full_path + 1, referrals,
3972 &fake_devname);
3973
3974 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003975
Sean Finneydd613942011-04-11 13:19:30 +00003976 if (IS_ERR(mdata)) {
3977 rc = PTR_ERR(mdata);
3978 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003979 } else {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003980 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003981 rc = cifs_setup_volume_info(volume_info, mdata,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003982 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00003983 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003984 kfree(fake_devname);
3985 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003986 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003987 }
3988 kfree(full_path);
3989 return rc;
3990}
Steve French2d6d5892009-04-09 00:36:44 +00003991#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003992
Jeff Layton04db79b2011-07-06 08:10:38 -04003993static int
3994cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003995 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003997 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003998
Steve Frenchc7c137b2018-06-06 17:59:29 -05003999 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04004000 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Jeff Layton7586b762008-12-01 18:41:49 -05004002 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004003 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05004004 kfree(volume_info->username);
4005 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05004006 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05004008 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05004010 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00004011 /* In userspace mount helper we can get user name from alternate
4012 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04004013 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 }
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05004017 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004018 /* load_nls_default cannot return null */
4019 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004021 volume_info->local_nls = load_nls(volume_info->iocharset);
4022 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004023 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004024 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04004025 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 }
4027 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004028
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004029 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04004030}
4031
4032struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05004033cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04004034{
4035 int rc;
4036 struct smb_vol *volume_info;
4037
Jeff Layton6ee95422012-11-26 11:09:57 -05004038 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04004039 if (!volume_info)
4040 return ERR_PTR(-ENOMEM);
4041
Steve Frenchc7c137b2018-06-06 17:59:29 -05004042 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04004043 if (rc) {
4044 cifs_cleanup_volume_info(volume_info);
4045 volume_info = ERR_PTR(rc);
4046 }
4047
4048 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004049}
4050
Aurelien Aptela6b50582016-05-25 19:59:09 +02004051static int
4052cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4053 unsigned int xid,
4054 struct cifs_tcon *tcon,
4055 struct cifs_sb_info *cifs_sb,
4056 char *full_path)
4057{
4058 int rc;
4059 char *s;
4060 char sep, tmp;
4061
4062 sep = CIFS_DIR_SEP(cifs_sb);
4063 s = full_path;
4064
4065 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4066 while (rc == 0) {
4067 /* skip separators */
4068 while (*s == sep)
4069 s++;
4070 if (!*s)
4071 break;
4072 /* next separator */
4073 while (*s && *s != sep)
4074 s++;
4075
4076 /*
4077 * temporarily null-terminate the path at the end of
4078 * the current component
4079 */
4080 tmp = *s;
4081 *s = 0;
4082 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4083 full_path);
4084 *s = tmp;
4085 }
4086 return rc;
4087}
4088
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004089/*
4090 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
4091 * otherwise 0.
4092 */
4093static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
4094 const unsigned int xid,
4095 struct TCP_Server_Info *server,
4096 struct cifs_tcon *tcon)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004097{
Jeff Layton1daaae82012-03-21 06:30:40 -04004098 int rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004099 char *full_path;
4100
4101 if (!server->ops->is_path_accessible)
4102 return -EOPNOTSUPP;
4103
4104 /*
4105 * cifs_build_path_to_root works only when we have a valid tcon
4106 */
4107 full_path = cifs_build_path_to_root(vol, cifs_sb, tcon,
4108 tcon->Flags & SMB_SHARE_IS_IN_DFS);
4109 if (full_path == NULL)
4110 return -ENOMEM;
4111
4112 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
4113
4114 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4115 full_path);
4116 if (rc != 0 && rc != -EREMOTE) {
4117 kfree(full_path);
4118 return rc;
4119 }
4120
4121 if (rc != -EREMOTE) {
4122 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
4123 cifs_sb,
4124 full_path);
4125 if (rc != 0) {
4126 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4127 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4128 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4129 rc = 0;
4130 }
4131 }
4132
4133 kfree(full_path);
4134 return rc;
4135}
4136
4137#ifdef CONFIG_CIFS_DFS_UPCALL
4138int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4139{
4140 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004141 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004142 struct cifs_ses *ses;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004143 struct cifs_tcon *tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004144 struct TCP_Server_Info *server;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004145 char *old_mountdata;
4146 int count;
Al Virodd854462011-06-17 08:24:42 -04004147
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004148 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4149 if (!rc && tcon) {
4150 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4151 if (!rc)
4152 goto out;
4153 if (rc != -EREMOTE)
4154 goto error;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004155 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004156 if ((rc == -EACCES) || (rc == -EOPNOTSUPP) || (ses == NULL) || (server == NULL))
4157 goto error;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Sean Finneyc1508ca2011-04-11 13:19:31 +00004160 /*
4161 * Perform an unconditional check for whether there are DFS
4162 * referrals for this path without prefix, to provide support
4163 * for DFS referrals from w2k8 servers which don't seem to respond
4164 * with PATH_NOT_COVERED to requests that include the prefix.
4165 * Chase the referral if found, otherwise continue normally.
4166 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004167 old_mountdata = cifs_sb->mountdata;
4168 (void)expand_dfs_referral(xid, ses, vol, cifs_sb, false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004169
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004170 if (cifs_sb->mountdata == NULL) {
4171 rc = -ENOENT;
4172 goto error;
4173 }
4174
4175 if (cifs_sb->mountdata != old_mountdata) {
4176 /* If we were redirected, reconnect to new target server */
4177 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4178 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4179 }
4180 if (rc) {
4181 if (rc == -EACCES || rc == -EOPNOTSUPP)
4182 goto error;
4183 }
4184
4185 for (count = 1; ;) {
4186 if (!rc && tcon) {
4187 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4188 if (!rc || rc != -EREMOTE)
4189 break;
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004190 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004191 /*
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004192 * BB: when we implement proper loop detection,
4193 * we will remove this check. But now we need it
4194 * to prevent an indefinite loop if 'DFS tree' is
4195 * misconfigured (i.e. has loops).
Steve French6d3ea7e2012-11-28 22:34:41 -06004196 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004197 if (count++ > MAX_NESTED_LINKS) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004198 rc = -ELOOP;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004199 break;
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004200 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004201
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004202 old_mountdata = cifs_sb->mountdata;
4203 rc = expand_dfs_referral(xid, tcon->ses, vol, cifs_sb,
4204 true);
4205 if (rc)
4206 break;
Jeff Layton7b91e262009-07-23 15:22:30 -04004207
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004208 if (cifs_sb->mountdata != old_mountdata) {
4209 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4210 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses,
4211 &tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004212 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004213 if (rc) {
4214 if (rc == -EACCES || rc == -EOPNOTSUPP || !server ||
4215 !ses)
4216 goto error;
4217 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004218 }
4219
Jeff Layton9d002df2010-10-06 19:51:11 -04004220 if (rc)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004221 goto error;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004222
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01004223 /*
4224 * After reconnecting to a different server, unique ids won't
4225 * match anymore, so we disable serverino. This prevents
4226 * dentry revalidation to think the dentry are stale (ESTALE).
4227 */
4228 cifs_autodisable_serverino(cifs_sb);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004229out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004230 free_xid(xid);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004231 return mount_setup_tlink(cifs_sb, ses, tcon);
4232
4233error:
4234 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return rc;
4236}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004237#else
4238int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4239{
4240 int rc = 0;
4241 unsigned int xid;
4242 struct cifs_ses *ses;
4243 struct cifs_tcon *tcon;
4244 struct TCP_Server_Info *server;
4245
4246 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4247 if (rc)
4248 goto error;
4249
4250 if (tcon) {
4251 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4252 if (rc == -EREMOTE)
4253 rc = -EOPNOTSUPP;
4254 if (rc)
4255 goto error;
4256 }
4257
4258 free_xid(xid);
4259
4260 return mount_setup_tlink(cifs_sb, ses, tcon);
4261
4262error:
4263 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4264 return rc;
4265}
4266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Jeff Layton8d1bca32011-06-11 21:17:10 -04004268/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004269 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004272CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004273 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 const struct nls_table *nls_codepage)
4275{
4276 struct smb_hdr *smb_buffer;
4277 struct smb_hdr *smb_buffer_response;
4278 TCONX_REQ *pSMB;
4279 TCONX_RSP *pSMBr;
4280 unsigned char *bcc_ptr;
4281 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004282 int length;
4283 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 if (ses == NULL)
4286 return -EIO;
4287
4288 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004289 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 smb_buffer_response = smb_buffer;
4293
4294 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4295 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004296
Pavel Shilovsky88257362012-05-23 14:01:59 +04004297 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 smb_buffer->Uid = ses->Suid;
4299 pSMB = (TCONX_REQ *) smb_buffer;
4300 pSMBr = (TCONX_RSP *) smb_buffer_response;
4301
4302 pSMB->AndXCommand = 0xFF;
4303 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004305 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004306 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004307 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004308 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004309 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004310 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004311 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004312 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4313 specified as required (when that support is added to
4314 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004315 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004316 by Samba (not sure whether other servers allow
4317 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004318#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004319 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004320 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004321 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004322 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004323 SECMODE_PW_ENCRYPT ? true : false,
4324 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004325 else
4326#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004327 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004328 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004329 if (rc) {
4330 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4331 __func__, rc);
4332 cifs_buf_release(smb_buffer);
4333 return rc;
4334 }
Steve Frencheeac8042006-01-13 21:34:58 -08004335
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004336 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004337 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004338 /* must align unicode strings */
4339 *bcc_ptr = 0; /* null byte password */
4340 bcc_ptr++;
4341 }
Steve Frencheeac8042006-01-13 21:34:58 -08004342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Jeff Layton38d77c52013-05-26 07:01:00 -04004344 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4346
4347 if (ses->capabilities & CAP_STATUS32) {
4348 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4349 }
4350 if (ses->capabilities & CAP_DFS) {
4351 smb_buffer->Flags2 |= SMBFLG2_DFS;
4352 }
4353 if (ses->capabilities & CAP_UNICODE) {
4354 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4355 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004356 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004357 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004358 (/* server len*/ + 256 /* share len */), nls_codepage);
4359 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 bcc_ptr += 2; /* skip trailing null */
4361 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 strcpy(bcc_ptr, tree);
4363 bcc_ptr += strlen(tree) + 1;
4364 }
4365 strcpy(bcc_ptr, "?????");
4366 bcc_ptr += strlen("?????");
4367 bcc_ptr += 1;
4368 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004369 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4370 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 pSMB->ByteCount = cpu_to_le16(count);
4372
Steve French133672e2007-11-13 22:41:37 +00004373 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004374 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004377 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004378 bool is_unicode;
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004381 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 tcon->tid = smb_buffer_response->Tid;
4383 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004384 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004385 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004386 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4387 is_unicode = true;
4388 else
4389 is_unicode = false;
4390
Jeff Laytoncc20c032009-04-30 07:16:21 -04004391
Steve French50c2f752007-07-13 00:33:32 +00004392 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004393 if (length == 3) {
4394 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4395 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004396 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004397 tcon->ipc = true;
4398 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004399 }
4400 } else if (length == 2) {
4401 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4402 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004403 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004404 }
4405 }
Steve French50c2f752007-07-13 00:33:32 +00004406 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004407 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004408 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004409
4410 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004411 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004412 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004413 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004414 nls_codepage);
4415
Joe Perchesf96637b2013-05-04 22:12:25 -05004416 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004417
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004418 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004419 (smb_buffer_response->WordCount == 7))
4420 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004421 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4422 else
4423 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004424 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
4426
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004427 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 return rc;
4429}
4430
Al Viro2e32cf52013-10-03 12:53:37 -04004431static void delayed_free(struct rcu_head *p)
4432{
4433 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4434 unload_nls(sbi->local_nls);
4435 kfree(sbi);
4436}
4437
Al Viro2a9b9952011-06-17 09:27:16 -04004438void
4439cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440{
Jeff Laytonb647c352010-10-28 11:16:44 -04004441 struct rb_root *root = &cifs_sb->tlink_tree;
4442 struct rb_node *node;
4443 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Jeff Layton2de970f2010-10-06 19:51:12 -04004445 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4446
Jeff Laytonb647c352010-10-28 11:16:44 -04004447 spin_lock(&cifs_sb->tlink_tree_lock);
4448 while ((node = rb_first(root))) {
4449 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4450 cifs_get_tlink(tlink);
4451 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4452 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004453
Jeff Laytonb647c352010-10-28 11:16:44 -04004454 spin_unlock(&cifs_sb->tlink_tree_lock);
4455 cifs_put_tlink(tlink);
4456 spin_lock(&cifs_sb->tlink_tree_lock);
4457 }
4458 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004459
Al Virod757d712011-06-17 09:42:43 -04004460 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004461 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004462 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004465int
4466cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
4468 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004469 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004471 if (!server->ops->need_neg || !server->ops->negotiate)
4472 return -ENOSYS;
4473
Jeff Layton198b5682010-04-24 07:57:48 -04004474 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004475 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Pavel Shilovsky45275782012-05-17 17:53:29 +04004478 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004479
4480 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004481 if (rc == 0) {
4482 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004483 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004484 server->tcpStatus = CifsGood;
4485 else
4486 rc = -EHOSTDOWN;
4487 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 }
Steve French26b994f2008-08-06 05:11:33 +00004489
Jeff Layton198b5682010-04-24 07:57:48 -04004490 return rc;
4491}
Steve French26b994f2008-08-06 05:11:33 +00004492
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004493int
4494cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4495 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004496{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004497 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004498 struct TCP_Server_Info *server = ses->server;
4499
Jeff Layton198b5682010-04-24 07:57:48 -04004500 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004501 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004502 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004503
Joe Perchesf96637b2013-05-04 22:12:25 -05004504 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004505 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004506
Shu Wangf5c4ba82017-09-08 18:48:33 +08004507 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05004508 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08004509 ses->auth_key.response);
4510 kfree(ses->auth_key.response);
4511 ses->auth_key.response = NULL;
4512 ses->auth_key.len = 0;
4513 }
4514
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004515 if (server->ops->sess_setup)
4516 rc = server->ops->sess_setup(xid, ses, nls_info);
4517
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004518 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004519 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 return rc;
4522}
4523
Jeff Layton8a8798a2012-01-17 16:09:15 -05004524static int
4525cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4526{
Jeff Layton3f618222013-06-12 19:52:14 -05004527 vol->sectype = ses->sectype;
4528
4529 /* krb5 is special, since we don't need username or pw */
4530 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004531 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004532
4533 return cifs_set_cifscreds(vol, ses);
4534}
4535
Steve French96daf2b2011-05-27 04:34:02 +00004536static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004537cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004538{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004539 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004540 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4541 struct cifs_ses *ses;
4542 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004543 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004544
4545 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004546 if (vol_info == NULL)
4547 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004548
Jeff Layton9d002df2010-10-06 19:51:11 -04004549 vol_info->local_nls = cifs_sb->local_nls;
4550 vol_info->linux_uid = fsuid;
4551 vol_info->cred_uid = fsuid;
4552 vol_info->UNC = master_tcon->treeName;
4553 vol_info->retry = master_tcon->retry;
4554 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05004555 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04004556 vol_info->local_lease = master_tcon->local_lease;
4557 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004558 vol_info->sectype = master_tcon->ses->sectype;
4559 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004560
Jeff Layton8a8798a2012-01-17 16:09:15 -05004561 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4562 if (rc) {
4563 tcon = ERR_PTR(rc);
4564 goto out;
4565 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004566
4567 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304568 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004569 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304570 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004571
4572 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4573 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004574 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004575 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004576 goto out;
4577 }
4578
4579 tcon = cifs_get_tcon(ses, vol_info);
4580 if (IS_ERR(tcon)) {
4581 cifs_put_smb_ses(ses);
4582 goto out;
4583 }
4584
Steve Frenchce558b02018-05-31 19:16:54 -05004585 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4586 if (tcon->posix_extensions)
4587 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05004588
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004589 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004590 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05004591
Jeff Layton9d002df2010-10-06 19:51:11 -04004592out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004593 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01004594 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004595 kfree(vol_info);
4596
4597 return tcon;
4598}
4599
Steve French96daf2b2011-05-27 04:34:02 +00004600struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004601cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4602{
4603 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4604}
4605
Jeff Laytonb647c352010-10-28 11:16:44 -04004606/* find and return a tlink with given uid */
4607static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004608tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004609{
4610 struct rb_node *node = root->rb_node;
4611 struct tcon_link *tlink;
4612
4613 while (node) {
4614 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4615
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004616 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004617 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004618 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004619 node = node->rb_right;
4620 else
4621 return tlink;
4622 }
4623 return NULL;
4624}
4625
4626/* insert a tcon_link into the tree */
4627static void
4628tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4629{
4630 struct rb_node **new = &(root->rb_node), *parent = NULL;
4631 struct tcon_link *tlink;
4632
4633 while (*new) {
4634 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4635 parent = *new;
4636
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004637 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004638 new = &((*new)->rb_left);
4639 else
4640 new = &((*new)->rb_right);
4641 }
4642
4643 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4644 rb_insert_color(&new_tlink->tl_rbnode, root);
4645}
4646
Jeff Layton9d002df2010-10-06 19:51:11 -04004647/*
4648 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4649 * current task.
4650 *
4651 * If the superblock doesn't refer to a multiuser mount, then just return
4652 * the master tcon for the mount.
4653 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304654 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004655 * exists, then check to see if it's pending construction. If it is then wait
4656 * for construction to complete. Once it's no longer pending, check to see if
4657 * it failed and either return an error or retry construction, depending on
4658 * the timeout.
4659 *
4660 * If one doesn't exist then insert a new tcon_link struct into the tree and
4661 * try to construct a new one.
4662 */
4663struct tcon_link *
4664cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4665{
4666 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004667 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004668 struct tcon_link *tlink, *newtlink;
4669
4670 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4671 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4672
4673 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004674 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004675 if (tlink)
4676 cifs_get_tlink(tlink);
4677 spin_unlock(&cifs_sb->tlink_tree_lock);
4678
4679 if (tlink == NULL) {
4680 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4681 if (newtlink == NULL)
4682 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004683 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004684 newtlink->tl_tcon = ERR_PTR(-EACCES);
4685 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4686 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4687 cifs_get_tlink(newtlink);
4688
Jeff Layton9d002df2010-10-06 19:51:11 -04004689 spin_lock(&cifs_sb->tlink_tree_lock);
4690 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004691 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004692 if (tlink) {
4693 cifs_get_tlink(tlink);
4694 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004695 kfree(newtlink);
4696 goto wait_for_construction;
4697 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004698 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004699 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4700 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004701 } else {
4702wait_for_construction:
4703 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004704 TASK_INTERRUPTIBLE);
4705 if (ret) {
4706 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004707 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004708 }
4709
4710 /* if it's good, return it */
4711 if (!IS_ERR(tlink->tl_tcon))
4712 return tlink;
4713
4714 /* return error if we tried this already recently */
4715 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4716 cifs_put_tlink(tlink);
4717 return ERR_PTR(-EACCES);
4718 }
4719
4720 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4721 goto wait_for_construction;
4722 }
4723
4724 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4725 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4726 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4727
4728 if (IS_ERR(tlink->tl_tcon)) {
4729 cifs_put_tlink(tlink);
4730 return ERR_PTR(-EACCES);
4731 }
4732
4733 return tlink;
4734}
Jeff Layton2de970f2010-10-06 19:51:12 -04004735
4736/*
4737 * periodic workqueue job that scans tcon_tree for a superblock and closes
4738 * out tcons.
4739 */
4740static void
4741cifs_prune_tlinks(struct work_struct *work)
4742{
4743 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4744 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004745 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004746 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004747 struct rb_node *tmp;
4748 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004749
Jeff Laytonb647c352010-10-28 11:16:44 -04004750 /*
4751 * Because we drop the spinlock in the loop in order to put the tlink
4752 * it's not guarded against removal of links from the tree. The only
4753 * places that remove entries from the tree are this function and
4754 * umounts. Because this function is non-reentrant and is canceled
4755 * before umount can proceed, this is safe.
4756 */
4757 spin_lock(&cifs_sb->tlink_tree_lock);
4758 node = rb_first(root);
4759 while (node != NULL) {
4760 tmp = node;
4761 node = rb_next(tmp);
4762 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4763
4764 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4765 atomic_read(&tlink->tl_count) != 0 ||
4766 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4767 continue;
4768
4769 cifs_get_tlink(tlink);
4770 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4771 rb_erase(tmp, root);
4772
Jeff Layton2de970f2010-10-06 19:51:12 -04004773 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004774 cifs_put_tlink(tlink);
4775 spin_lock(&cifs_sb->tlink_tree_lock);
4776 }
4777 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004778
Jeff Laytonda472fc2012-03-23 14:40:53 -04004779 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004780 TLINK_IDLE_EXPIRE);
4781}