blob: ed3b6de88395ddae89f35f84a7c16248251a1930 [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
60#define CIFS_PORT 445
61#define RFC1001_PORT 139
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050064extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jeff Layton2de970f2010-10-06 19:51:12 -040066/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040067#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040068#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040069
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040070enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040071 /* Mount options that take no arguments */
72 Opt_user_xattr, Opt_nouser_xattr,
73 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040074 Opt_forcegid, Opt_noforcegid,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040075 Opt_noblocksend, Opt_noautotune,
76 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050077 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040078 Opt_mapchars, Opt_nomapchars, Opt_sfu,
79 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
80 Opt_noposixpaths, Opt_nounix,
81 Opt_nocase,
82 Opt_brl, Opt_nobrl,
Steve French3d4ef9a2018-04-25 22:19:09 -050083 Opt_handlecache, Opt_nohandlecache,
Steve French95932652016-09-23 01:36:34 -050084 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040085 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
86 Opt_nohard, Opt_nosoft,
87 Opt_nointr, Opt_intr,
88 Opt_nostrictsync, Opt_strictsync,
89 Opt_serverino, Opt_noserverino,
90 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
91 Opt_acl, Opt_noacl, Opt_locallease,
Jeff Layton1b359202012-09-19 15:20:27 -070092 Opt_sign, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040093 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040094 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050095 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060096 Opt_resilient, Opt_noresilient,
Long Li8339dd32017-11-07 01:54:55 -070097 Opt_domainauto, Opt_rdma,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040098
99 /* Mount options which take numeric value */
100 Opt_backupuid, Opt_backupgid, Opt_uid,
101 Opt_cruid, Opt_gid, Opt_file_mode,
102 Opt_dirmode, Opt_port,
103 Opt_rsize, Opt_wsize, Opt_actimeo,
Steve French141891f2016-09-23 00:44:16 -0500104 Opt_echo_interval, Opt_max_credits,
Steve French8b217fe2016-11-11 22:36:20 -0600105 Opt_snapshot,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400106
107 /* Mount options which take string value */
108 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400109 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400110 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400111 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400112
113 /* Mount options to be ignored */
114 Opt_ignore,
115
116 /* Options which could be blank */
117 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100118 Opt_blank_user,
119 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400120
121 Opt_err
122};
123
124static const match_table_t cifs_mount_option_tokens = {
125
126 { Opt_user_xattr, "user_xattr" },
127 { Opt_nouser_xattr, "nouser_xattr" },
128 { Opt_forceuid, "forceuid" },
129 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400130 { Opt_forcegid, "forcegid" },
131 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400132 { Opt_noblocksend, "noblocksend" },
133 { Opt_noautotune, "noautotune" },
134 { Opt_hard, "hard" },
135 { Opt_soft, "soft" },
136 { Opt_perm, "perm" },
137 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500138 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400139 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500140 { Opt_mapposix, "mapposix" }, /* SFM style */
141 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400142 { Opt_sfu, "sfu" },
143 { Opt_nosfu, "nosfu" },
144 { Opt_nodfs, "nodfs" },
145 { Opt_posixpaths, "posixpaths" },
146 { Opt_noposixpaths, "noposixpaths" },
147 { Opt_nounix, "nounix" },
148 { Opt_nounix, "nolinux" },
149 { Opt_nocase, "nocase" },
150 { Opt_nocase, "ignorecase" },
151 { Opt_brl, "brl" },
152 { Opt_nobrl, "nobrl" },
Steve French3d4ef9a2018-04-25 22:19:09 -0500153 { Opt_handlecache, "handlecache" },
154 { Opt_nohandlecache, "nohandlecache" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400155 { Opt_nobrl, "nolock" },
156 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400157 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400158 { Opt_setuids, "setuids" },
159 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500160 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400161 { Opt_dynperm, "dynperm" },
162 { Opt_nodynperm, "nodynperm" },
163 { Opt_nohard, "nohard" },
164 { Opt_nosoft, "nosoft" },
165 { Opt_nointr, "nointr" },
166 { Opt_intr, "intr" },
167 { Opt_nostrictsync, "nostrictsync" },
168 { Opt_strictsync, "strictsync" },
169 { Opt_serverino, "serverino" },
170 { Opt_noserverino, "noserverino" },
171 { Opt_rwpidforward, "rwpidforward" },
172 { Opt_cifsacl, "cifsacl" },
173 { Opt_nocifsacl, "nocifsacl" },
174 { Opt_acl, "acl" },
175 { Opt_noacl, "noacl" },
176 { Opt_locallease, "locallease" },
177 { Opt_sign, "sign" },
178 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400179 { Opt_noac, "noac" },
180 { Opt_fsc, "fsc" },
181 { Opt_mfsymlinks, "mfsymlinks" },
182 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400183 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400184 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500185 { Opt_persistent, "persistenthandles"},
186 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600187 { Opt_resilient, "resilienthandles"},
188 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530189 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700190 { Opt_rdma, "rdma"},
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400191
192 { Opt_backupuid, "backupuid=%s" },
193 { Opt_backupgid, "backupgid=%s" },
194 { Opt_uid, "uid=%s" },
195 { Opt_cruid, "cruid=%s" },
196 { Opt_gid, "gid=%s" },
197 { Opt_file_mode, "file_mode=%s" },
198 { Opt_dirmode, "dirmode=%s" },
199 { Opt_dirmode, "dir_mode=%s" },
200 { Opt_port, "port=%s" },
201 { Opt_rsize, "rsize=%s" },
202 { Opt_wsize, "wsize=%s" },
203 { Opt_actimeo, "actimeo=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600204 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500205 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600206 { Opt_snapshot, "snapshot=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400207
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100208 { Opt_blank_user, "user=" },
209 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400210 { Opt_user, "user=%s" },
211 { Opt_user, "username=%s" },
212 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100213 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400214 { Opt_pass, "pass=%s" },
215 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100216 { Opt_blank_ip, "ip=" },
217 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400218 { Opt_ip, "ip=%s" },
219 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400220 { Opt_ignore, "unc=%s" },
221 { Opt_ignore, "target=%s" },
222 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400223 { Opt_domain, "dom=%s" },
224 { Opt_domain, "domain=%s" },
225 { Opt_domain, "workgroup=%s" },
226 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400227 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400228 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400229 { Opt_netbiosname, "netbiosname=%s" },
230 { Opt_servern, "servern=%s" },
231 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400232 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400233 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400234 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400235
236 { Opt_ignore, "cred" },
237 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400238 { Opt_ignore, "cred=%s" },
239 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400240 { Opt_ignore, "guest" },
241 { Opt_ignore, "rw" },
242 { Opt_ignore, "ro" },
243 { Opt_ignore, "suid" },
244 { Opt_ignore, "nosuid" },
245 { Opt_ignore, "exec" },
246 { Opt_ignore, "noexec" },
247 { Opt_ignore, "nodev" },
248 { Opt_ignore, "noauto" },
249 { Opt_ignore, "dev" },
250 { Opt_ignore, "mand" },
251 { Opt_ignore, "nomand" },
252 { Opt_ignore, "_netdev" },
253
254 { Opt_err, NULL }
255};
256
257enum {
258 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
259 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400260 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
261 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400262 Opt_sec_none,
263
264 Opt_sec_err
265};
266
267static const match_table_t cifs_secflavor_tokens = {
268 { Opt_sec_krb5, "krb5" },
269 { Opt_sec_krb5i, "krb5i" },
270 { Opt_sec_krb5p, "krb5p" },
271 { Opt_sec_ntlmsspi, "ntlmsspi" },
272 { Opt_sec_ntlmssp, "ntlmssp" },
273 { Opt_ntlm, "ntlm" },
274 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400275 { Opt_sec_ntlmv2, "nontlm" },
276 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400277 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400278 { Opt_sec_lanman, "lanman" },
279 { Opt_sec_none, "none" },
280
281 { Opt_sec_err, NULL }
282};
283
Jeff Layton15b6a472012-05-16 07:50:15 -0400284/* cache flavors */
285enum {
286 Opt_cache_loose,
287 Opt_cache_strict,
288 Opt_cache_none,
289 Opt_cache_err
290};
291
292static const match_table_t cifs_cacheflavor_tokens = {
293 { Opt_cache_loose, "loose" },
294 { Opt_cache_strict, "strict" },
295 { Opt_cache_none, "none" },
296 { Opt_cache_err, NULL }
297};
298
Jeff Layton23db65f2012-05-15 12:20:51 -0400299static const match_table_t cifs_smb_version_tokens = {
300 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600301 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000302 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500303 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500304 { Smb_302, SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600305#ifdef CONFIG_CIFS_SMB311
306 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500307 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600308#endif /* SMB311 */
Steve French9764c022017-09-17 10:41:35 -0500309 { Smb_3any, SMB3ANY_VERSION_STRING },
310 { Smb_default, SMBDEFAULT_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600311 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400312};
313
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300314static int ip_connect(struct TCP_Server_Info *server);
315static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400316static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400317static void cifs_prune_tlinks(struct work_struct *work);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -0400318static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
319 const char *devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Jeff Laytond5c56052008-12-01 18:42:33 -0500321/*
322 * cifs tcp session reconnection
323 *
324 * mark tcp session as reconnecting so temporarily locked
325 * mark all smb sessions as reconnecting for tcp session
326 * reconnect tcp session
327 * wake up waiters on reconnection? - (not needed currently)
328 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400329int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330cifs_reconnect(struct TCP_Server_Info *server)
331{
332 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500333 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000334 struct cifs_ses *ses;
335 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000336 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400337 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000340 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000341 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 next time through the loop */
343 spin_unlock(&GlobalMid_Lock);
344 return rc;
345 } else
346 server->tcpStatus = CifsNeedReconnect;
347 spin_unlock(&GlobalMid_Lock);
348 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400349 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Joe Perchesf96637b2013-05-04 22:12:25 -0500351 cifs_dbg(FYI, "Reconnecting tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* before reconnecting the tcp session, mark the smb session (uid)
354 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500355 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
356 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530357 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500358 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000359 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500360 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500361 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000362 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500363 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100365 if (ses->tcon_ipc)
366 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530368 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500371 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500372 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000373 if (server->ssocket) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500374 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
375 server->ssocket->state, server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800376 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesf96637b2013-05-04 22:12:25 -0500377 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
378 server->ssocket->state, server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 sock_release(server->ssocket);
380 server->ssocket = NULL;
381 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500382 server->sequence_number = 0;
383 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500384 kfree(server->session_key.response);
385 server->session_key.response = NULL;
386 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000387 server->lstrp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500389 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400390 INIT_LIST_HEAD(&retry_list);
Joe Perchesf96637b2013-05-04 22:12:25 -0500391 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500393 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
394 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400395 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
396 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400397 list_move(&mid_entry->qhead, &retry_list);
398 }
399 spin_unlock(&GlobalMid_Lock);
Rabin Vincent820962d2015-12-23 07:32:41 +0100400 mutex_unlock(&server->srv_mutex);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400401
Joe Perchesf96637b2013-05-04 22:12:25 -0500402 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400403 list_for_each_safe(tmp, tmp2, &retry_list) {
404 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500405 list_del_init(&mid_entry->qhead);
406 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400409 do {
Steve French6c3d8902006-07-31 22:46:20 +0000410 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300411
412 /* we should try only the port we connected to before */
Jeff Layton73e216a2013-09-05 08:38:10 -0400413 mutex_lock(&server->srv_mutex);
Long Li781a8052017-11-22 17:38:36 -0700414 if (cifs_rdma_enabled(server))
415 rc = smbd_reconnect(server);
416 else
417 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000418 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500419 cifs_dbg(FYI, "reconnect error %d\n", rc);
Federico Sauter4afe2602015-03-17 17:45:28 +0100420 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700421 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } else {
423 atomic_inc(&tcpSesReconnectCount);
424 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000425 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000426 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000427 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100428 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400430 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500431
Sachin Prabhub8c60012016-10-20 19:52:24 -0400432 if (server->tcpStatus == CifsNeedNegotiate)
433 mod_delayed_work(cifsiod_wq, &server->echo, 0);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return rc;
436}
437
Jeff Laytonc74093b2011-01-11 07:24:23 -0500438static void
439cifs_echo_request(struct work_struct *work)
440{
441 int rc;
442 struct TCP_Server_Info *server = container_of(work,
443 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400444 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500445
Jeff Layton247ec9b2011-02-04 17:09:50 -0500446 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400447 * If we need to renegotiate, set echo interval to zero to
448 * immediately call echo service where we can renegotiate.
449 */
450 if (server->tcpStatus == CifsNeedNegotiate)
451 echo_interval = 0;
452 else
453 echo_interval = server->echo_interval;
454
455 /*
456 * We cannot send an echo if it is disabled.
457 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500458 */
Steve French4fcd1812016-06-22 20:12:05 -0500459
460 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400461 server->tcpStatus == CifsExiting ||
462 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400463 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600464 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500465 goto requeue_echo;
466
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400467 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500468 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500469 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
470 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500471
472requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400473 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500474}
475
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400476static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400477allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400478{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400479 if (!server->bigbuf) {
480 server->bigbuf = (char *)cifs_buf_get();
481 if (!server->bigbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500482 cifs_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400483 msleep(3000);
484 /* retry will check if exiting */
485 return false;
486 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400487 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400488 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400489 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400490 }
491
Jeff Layton2a37ef92011-10-19 15:29:23 -0400492 if (!server->smallbuf) {
493 server->smallbuf = (char *)cifs_small_buf_get();
494 if (!server->smallbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500495 cifs_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400496 msleep(1000);
497 /* retry will check if exiting */
498 return false;
499 }
500 /* beginning of smb buffer is cleared in our buf_get */
501 } else {
502 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400503 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400504 }
505
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400506 return true;
507}
508
Jeff Laytonba749e62011-10-11 06:41:32 -0400509static bool
510server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400511{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300512 /*
513 * We need to wait 2 echo intervals to make sure we handle such
514 * situations right:
515 * 1s client sends a normal SMB request
516 * 2s client gets a response
517 * 30s echo workqueue job pops, and decides we got a response recently
518 * and don't need to send another
519 * ...
520 * 65s kernel_recvmsg times out, and we see that we haven't gotten
521 * a response in >60s.
522 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200523 if ((server->tcpStatus == CifsGood ||
524 server->tcpStatus == CifsNeedNegotiate) &&
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600525 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
526 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
527 server->hostname, (2 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400528 cifs_reconnect(server);
529 wake_up(&server->response_q);
530 return true;
531 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400532
Jeff Laytonba749e62011-10-11 06:41:32 -0400533 return false;
534}
535
Al Viro71335662016-01-09 19:54:50 -0500536static int
537cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400538{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400539 int length = 0;
540 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400541
Al Viro71335662016-01-09 19:54:50 -0500542 smb_msg->msg_control = NULL;
543 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400544
Al Viro71335662016-01-09 19:54:50 -0500545 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500546 try_to_freeze();
547
Al Viro71335662016-01-09 19:54:50 -0500548 if (server_unresponsive(server))
549 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700550 if (cifs_rdma_enabled(server) && server->smbd_conn)
551 length = smbd_recv(server->smbd_conn, smb_msg);
552 else
553 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500554
555 if (server->tcpStatus == CifsExiting)
556 return -ESHUTDOWN;
557
558 if (server->tcpStatus == CifsNeedReconnect) {
559 cifs_reconnect(server);
560 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400561 }
562
Al Viro71335662016-01-09 19:54:50 -0500563 if (length == -ERESTARTSYS ||
564 length == -EAGAIN ||
565 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400566 /*
567 * Minimum sleep to prevent looping, allowing socket
568 * to clear and app threads to set tcpStatus
569 * CifsNeedReconnect if server hung.
570 */
571 usleep_range(1000, 2000);
572 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400573 continue;
Al Viro71335662016-01-09 19:54:50 -0500574 }
575
576 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500577 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400578 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500579 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400580 }
581 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400582 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400583}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400584
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400585int
586cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
587 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400588{
Al Viro71335662016-01-09 19:54:50 -0500589 struct msghdr smb_msg;
590 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
591 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400592
Al Viro71335662016-01-09 19:54:50 -0500593 return cifs_readv_from_socket(server, &smb_msg);
594}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400595
Al Viro71335662016-01-09 19:54:50 -0500596int
597cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
598 unsigned int to_read)
599{
600 struct msghdr smb_msg;
601 struct bio_vec bv = {.bv_page = page, .bv_len = to_read};
602 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
603 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400604}
605
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400606static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400607is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400608{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400609 /*
610 * The first byte big endian of the length field,
611 * is actually not part of the length but the type
612 * with the most common, zero, as regular data.
613 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400614 switch (type) {
615 case RFC1002_SESSION_MESSAGE:
616 /* Regular SMB response */
617 return true;
618 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500619 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400620 break;
621 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500622 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400623 break;
624 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400625 /*
626 * We get this from Windows 98 instead of an error on
627 * SMB negprot response.
628 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500629 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400630 /* give server a second to clean up */
631 msleep(1000);
632 /*
633 * Always try 445 first on reconnect since we get NACK
634 * on some if we ever connected to port 139 (the NACK
635 * is since we do not begin with RFC1001 session
636 * initialize frame).
637 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400638 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400639 cifs_reconnect(server);
640 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400641 break;
642 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500643 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400644 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400645 }
646
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400647 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400648}
649
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400650void
651dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400652{
653#ifdef CONFIG_CIFS_STATS2
654 mid->when_received = jiffies;
655#endif
656 spin_lock(&GlobalMid_Lock);
657 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400658 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400659 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400660 mid->mid_state = MID_RESPONSE_MALFORMED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400661 list_del_init(&mid->qhead);
662 spin_unlock(&GlobalMid_Lock);
663}
664
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400665static void
666handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400667 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400668{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400669 if (server->ops->check_trans2 &&
670 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400671 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400672 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400673 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400674 /* Was previous buf put in mpx struct for multi-rsp? */
675 if (!mid->multiRsp) {
676 /* smb buffer will be freed by user thread */
677 if (server->large_buf)
678 server->bigbuf = NULL;
679 else
680 server->smallbuf = NULL;
681 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400682 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400683}
684
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400685static void clean_demultiplex_info(struct TCP_Server_Info *server)
686{
687 int length;
688
689 /* take it off the list, if it's not already */
690 spin_lock(&cifs_tcp_ses_lock);
691 list_del_init(&server->tcp_ses_list);
692 spin_unlock(&cifs_tcp_ses_lock);
693
694 spin_lock(&GlobalMid_Lock);
695 server->tcpStatus = CifsExiting;
696 spin_unlock(&GlobalMid_Lock);
697 wake_up_all(&server->response_q);
698
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400699 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300700 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400701 if (server->credits <= 0)
702 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300703 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400704 /*
705 * Although there should not be any requests blocked on this queue it
706 * can not hurt to be paranoid and try to wake up requests that may
707 * haven been blocked when more than 50 at time were on the wire to the
708 * same server - they now will see the session is in exit state and get
709 * out of SendReceive.
710 */
711 wake_up_all(&server->request_q);
712 /* give those requests time to exit */
713 msleep(125);
Long Libce9ce72017-11-22 17:38:38 -0700714 if (cifs_rdma_enabled(server) && server->smbd_conn) {
715 smbd_destroy(server->smbd_conn);
716 server->smbd_conn = NULL;
717 }
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400718 if (server->ssocket) {
719 sock_release(server->ssocket);
720 server->ssocket = NULL;
721 }
722
723 if (!list_empty(&server->pending_mid_q)) {
724 struct list_head dispose_list;
725 struct mid_q_entry *mid_entry;
726 struct list_head *tmp, *tmp2;
727
728 INIT_LIST_HEAD(&dispose_list);
729 spin_lock(&GlobalMid_Lock);
730 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
731 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500732 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400733 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400734 list_move(&mid_entry->qhead, &dispose_list);
735 }
736 spin_unlock(&GlobalMid_Lock);
737
738 /* now walk dispose list and issue callbacks */
739 list_for_each_safe(tmp, tmp2, &dispose_list) {
740 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500741 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400742 list_del_init(&mid_entry->qhead);
743 mid_entry->callback(mid_entry);
744 }
745 /* 1/8th of sec is more than enough time for them to exit */
746 msleep(125);
747 }
748
749 if (!list_empty(&server->pending_mid_q)) {
750 /*
751 * mpx threads have not exited yet give them at least the smb
752 * send timeout time for long ops.
753 *
754 * Due to delays on oplock break requests, we need to wait at
755 * least 45 seconds before giving up on a request getting a
756 * response and going ahead and killing cifsd.
757 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500758 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400759 msleep(46000);
760 /*
761 * If threads still have not exited they are probably never
762 * coming home not much else we can do but free the memory.
763 */
764 }
765
766 kfree(server->hostname);
767 kfree(server);
768
769 length = atomic_dec_return(&tcpSesAllocCount);
770 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700771 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400772}
773
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400774static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400775standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
776{
777 int length;
778 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000779 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400780
781 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100782 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
783 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500784 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400785 cifs_reconnect(server);
786 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400787 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400788 }
789
790 /* switch to large buffer if too big for a small one */
791 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
792 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400793 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400794 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400795 }
796
797 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400798 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100799 pdu_length - HEADER_SIZE(server) + 1
800 + server->vals->header_preamble_size);
801
Jeff Laytone9097ab2011-10-19 15:29:40 -0400802 if (length < 0)
803 return length;
804 server->total_read += length;
805
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400806 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400807
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800808 return cifs_handle_standard(server, mid);
809}
810
811int
812cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
813{
814 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
815 int length;
816
Jeff Laytone9097ab2011-10-19 15:29:40 -0400817 /*
818 * We know that we received enough to get to the MID as we
819 * checked the pdu_length earlier. Now check to see
820 * if the rest of the header is OK. We borrow the length
821 * var for the rest of the loop to avoid a new stack var.
822 *
823 * 48 bytes is enough to display the header and a little bit
824 * into the payload for debugging purposes.
825 */
Steve French373512e2015-12-18 13:05:30 -0600826 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400827 if (length != 0)
828 cifs_dump_mem("Bad SMB: ", buf,
829 min_t(unsigned int, server->total_read, 48));
830
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700831 if (server->ops->is_session_expired &&
832 server->ops->is_session_expired(buf)) {
833 cifs_reconnect(server);
834 wake_up(&server->response_q);
835 return -1;
836 }
837
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700838 if (server->ops->is_status_pending &&
839 server->ops->is_status_pending(buf, server, length))
840 return -1;
841
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500842 if (!mid)
843 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400844
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400845 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500846 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400847}
848
849static int
Al Viro7c97c202011-06-21 08:51:28 -0400850cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 int length;
Al Viro7c97c202011-06-21 08:51:28 -0400853 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400854 unsigned int pdu_length;
855 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500856 struct task_struct *task_to_wake = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 struct mid_q_entry *mid_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500860 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400861
862 length = atomic_inc_return(&tcpSesAllocCount);
863 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700864 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700866 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000867 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700868 if (try_to_freeze())
869 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700870
Jeff Layton2a37ef92011-10-19 15:29:23 -0400871 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400872 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700873
Jeff Layton2a37ef92011-10-19 15:29:23 -0400874 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400875 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000876 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000877
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400878 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400879 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000880 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400881 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700882
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400883 /*
884 * The right amount was read from socket - 4 bytes,
885 * so we can now interpret the length field.
886 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400887 pdu_length = get_rfc1002_length(buf);
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000888 server->pdu_size = pdu_length;
Steve French46810cb2005-04-28 22:41:09 -0700889
Joe Perchesf96637b2013-05-04 22:12:25 -0500890 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400891 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000892 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700893
Jeff Layton89482a52011-10-19 15:28:57 -0400894 /* make sure we have enough to get to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100895 if (pdu_length < HEADER_SIZE(server) - 1 -
896 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500897 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
898 pdu_length);
Jeff Layton89482a52011-10-19 15:28:57 -0400899 cifs_reconnect(server);
900 wake_up(&server->response_q);
901 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700902 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400903
Jeff Layton89482a52011-10-19 15:28:57 -0400904 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100905 length = cifs_read_from_socket(server,
906 buf + server->vals->header_preamble_size,
907 HEADER_SIZE(server) - 1
908 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400909 if (length < 0)
910 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400911 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400912
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800913 if (server->ops->is_transform_hdr &&
914 server->ops->receive_transform &&
915 server->ops->is_transform_hdr(buf)) {
916 length = server->ops->receive_transform(server,
917 &mid_entry);
918 } else {
919 mid_entry = server->ops->find_mid(server, buf);
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400920
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800921 if (!mid_entry || !mid_entry->receive)
922 length = standard_receive3(server, mid_entry);
923 else
924 length = mid_entry->receive(server, mid_entry);
925 }
Jeff Layton44d22d82011-10-19 15:29:49 -0400926
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400927 if (length < 0)
Steve Frenche4eb2952005-04-28 22:41:09 -0700928 continue;
929
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400930 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -0400931 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700932
Steve Frenchfda35942011-01-20 18:06:34 +0000933 server->lstrp = jiffies;
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500934 if (mid_entry != NULL) {
Ronnie Sahlberge19b2bc2018-04-09 18:06:28 +1000935 mid_entry->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800936 if ((mid_entry->mid_flags & MID_WAIT_CANCELLED) &&
937 mid_entry->mid_state == MID_RESPONSE_RECEIVED &&
938 server->ops->handle_cancelled_mid)
939 server->ops->handle_cancelled_mid(
940 mid_entry->resp_buf,
941 server);
942
Jeff Layton2a37ef92011-10-19 15:29:23 -0400943 if (!mid_entry->multiRsp || mid_entry->multiEnd)
944 mid_entry->callback(mid_entry);
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800945 } else if (server->ops->is_oplock_break &&
946 server->ops->is_oplock_break(buf, server)) {
947 cifs_dbg(FYI, "Received oplock break\n");
948 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500949 cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
950 atomic_read(&midCount));
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400951 cifs_dump_mem("Received Data is: ", buf,
952 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +0000953#ifdef CONFIG_CIFS_DEBUG2
Pavel Shilovsky7f0adb52012-05-28 15:50:10 +0400954 if (server->ops->dump_detail)
Steve French71992e622018-05-06 15:58:51 -0500955 server->ops->dump_detail(buf, server);
Steve French39798772006-05-31 22:40:51 +0000956 cifs_dump_mids(server);
957#endif /* CIFS_DEBUG2 */
Steve French50c2f752007-07-13 00:33:32 +0000958
Steve Frenche4eb2952005-04-28 22:41:09 -0700959 }
960 } /* end while !EXITING */
961
Justin P. Mattockfd62cb72011-02-24 22:15:02 -0800962 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -0400963 cifs_buf_release(server->bigbuf);
964 if (server->smallbuf) /* no sense logging a debug message if NULL */
965 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500967 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400968 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500969
970 /* if server->tsk was NULL then wait for a signal before exiting */
971 if (!task_to_wake) {
972 set_current_state(TASK_INTERRUPTIBLE);
973 while (!signal_pending(current)) {
974 schedule();
975 set_current_state(TASK_INTERRUPTIBLE);
976 }
977 set_current_state(TASK_RUNNING);
978 }
979
Jeff Layton0468a2c2008-12-01 07:09:35 -0500980 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Jeff Laytonc359cf32007-11-16 22:22:06 +0000983/* extract the host portion of the UNC string */
984static char *
985extract_hostname(const char *unc)
986{
987 const char *src;
988 char *dst, *delim;
989 unsigned int len;
990
991 /* skip double chars at beginning of string */
992 /* BB: check validity of these bytes? */
993 src = unc + 2;
994
995 /* delimiter between hostname and sharename is always '\\' now */
996 delim = strchr(src, '\\');
997 if (!delim)
998 return ERR_PTR(-EINVAL);
999
1000 len = delim - src;
1001 dst = kmalloc((len + 1), GFP_KERNEL);
1002 if (dst == NULL)
1003 return ERR_PTR(-ENOMEM);
1004
1005 memcpy(dst, src, len);
1006 dst[len] = '\0';
1007
1008 return dst;
1009}
1010
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001011static int get_option_ul(substring_t args[], unsigned long *option)
1012{
1013 int rc;
1014 char *string;
1015
1016 string = match_strdup(args);
1017 if (string == NULL)
1018 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001019 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001020 kfree(string);
1021
1022 return rc;
1023}
1024
Eric W. Biederman3da46562013-02-06 01:37:39 -08001025static int get_option_uid(substring_t args[], kuid_t *result)
1026{
1027 unsigned long value;
1028 kuid_t uid;
1029 int rc;
1030
1031 rc = get_option_ul(args, &value);
1032 if (rc)
1033 return rc;
1034
1035 uid = make_kuid(current_user_ns(), value);
1036 if (!uid_valid(uid))
1037 return -EINVAL;
1038
1039 *result = uid;
1040 return 0;
1041}
1042
1043static int get_option_gid(substring_t args[], kgid_t *result)
1044{
1045 unsigned long value;
1046 kgid_t gid;
1047 int rc;
1048
1049 rc = get_option_ul(args, &value);
1050 if (rc)
1051 return rc;
1052
1053 gid = make_kgid(current_user_ns(), value);
1054 if (!gid_valid(gid))
1055 return -EINVAL;
1056
1057 *result = gid;
1058 return 0;
1059}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001060
1061static int cifs_parse_security_flavors(char *value,
1062 struct smb_vol *vol)
1063{
1064
1065 substring_t args[MAX_OPT_ARGS];
1066
Jeff Layton1e3cc572013-06-10 17:12:23 -05001067 /*
1068 * With mount options, the last one should win. Reset any existing
1069 * settings back to default.
1070 */
1071 vol->sectype = Unspecified;
1072 vol->sign = false;
1073
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001074 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001075 case Opt_sec_krb5p:
1076 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1077 return 1;
1078 case Opt_sec_krb5i:
1079 vol->sign = true;
1080 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001081 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001082 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001083 break;
1084 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001085 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001086 /* Fallthrough */
1087 case Opt_sec_ntlmssp:
1088 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001089 break;
1090 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001091 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001092 /* Fallthrough */
1093 case Opt_ntlm:
1094 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001095 break;
1096 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001097 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001098 /* Fallthrough */
1099 case Opt_sec_ntlmv2:
1100 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001101 break;
1102#ifdef CONFIG_CIFS_WEAK_PW_HASH
1103 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001104 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001105 break;
1106#endif
1107 case Opt_sec_none:
1108 vol->nullauth = 1;
1109 break;
1110 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001111 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001112 return 1;
1113 }
1114
1115 return 0;
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001119cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1120{
1121 substring_t args[MAX_OPT_ARGS];
1122
1123 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1124 case Opt_cache_loose:
1125 vol->direct_io = false;
1126 vol->strict_io = false;
1127 break;
1128 case Opt_cache_strict:
1129 vol->direct_io = false;
1130 vol->strict_io = true;
1131 break;
1132 case Opt_cache_none:
1133 vol->direct_io = true;
1134 vol->strict_io = false;
1135 break;
1136 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001137 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001138 return 1;
1139 }
1140 return 0;
1141}
1142
1143static int
Jeff Layton23db65f2012-05-15 12:20:51 -04001144cifs_parse_smb_version(char *value, struct smb_vol *vol)
1145{
1146 substring_t args[MAX_OPT_ARGS];
1147
1148 switch (match_token(value, cifs_smb_version_tokens, args)) {
1149 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001150 if (disable_legacy_dialects) {
1151 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1152 return 1;
1153 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001154 vol->ops = &smb1_operations;
1155 vol->vals = &smb1_values;
1156 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001157 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001158 if (disable_legacy_dialects) {
1159 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1160 return 1;
1161 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001162 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001163 vol->vals = &smb20_values;
1164 break;
Steve French1080ef72011-02-24 18:07:19 +00001165 case Smb_21:
1166 vol->ops = &smb21_operations;
1167 vol->vals = &smb21_values;
1168 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001169 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001170 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001171 vol->vals = &smb30_values;
1172 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001173 case Smb_302:
1174 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1175 vol->vals = &smb302_values;
1176 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001177#ifdef CONFIG_CIFS_SMB311
1178 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001179 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001180 vol->vals = &smb311_values;
1181 break;
1182#endif /* SMB311 */
Steve French9764c022017-09-17 10:41:35 -05001183 case Smb_3any:
1184 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1185 vol->vals = &smb3any_values;
1186 break;
1187 case Smb_default:
1188 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1189 vol->vals = &smbdefault_values;
1190 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001191 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001192 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001193 return 1;
1194 }
1195 return 0;
1196}
1197
Jeff Laytond387a5c2012-12-10 06:10:46 -05001198/*
1199 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1200 * fields with the result. Returns 0 on success and an error otherwise.
1201 */
1202static int
1203cifs_parse_devname(const char *devname, struct smb_vol *vol)
1204{
1205 char *pos;
1206 const char *delims = "/\\";
1207 size_t len;
1208
1209 /* make sure we have a valid UNC double delimiter prefix */
1210 len = strspn(devname, delims);
1211 if (len != 2)
1212 return -EINVAL;
1213
1214 /* find delimiter between host and sharename */
1215 pos = strpbrk(devname + 2, delims);
1216 if (!pos)
1217 return -EINVAL;
1218
1219 /* skip past delimiter */
1220 ++pos;
1221
1222 /* now go until next delimiter or end of string */
1223 len = strcspn(pos, delims);
1224
1225 /* move "pos" up to delimiter or NULL */
1226 pos += len;
1227 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1228 if (!vol->UNC)
1229 return -ENOMEM;
1230
1231 convert_delimiter(vol->UNC, '\\');
1232
Sachin Prabhu11e31642016-02-08 13:44:01 +05301233 /* skip any delimiter */
1234 if (*pos == '/' || *pos == '\\')
1235 pos++;
1236
1237 /* If pos is NULL then no prepath */
1238 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001239 return 0;
1240
1241 vol->prepath = kstrdup(pos, GFP_KERNEL);
1242 if (!vol->prepath)
1243 return -ENOMEM;
1244
1245 return 0;
1246}
1247
Jeff Layton23db65f2012-05-15 12:20:51 -04001248static int
Sean Finneyb9468452011-04-11 13:19:32 +00001249cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve French50c2f752007-07-13 00:33:32 +00001250 struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001252 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001253 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 unsigned int temp_len, i, j;
1255 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001256 short int override_uid = -1;
1257 short int override_gid = -1;
1258 bool uid_specified = false;
1259 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001260 bool sloppy = false;
1261 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001262 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001263 char *string = NULL;
1264 char *tmp_end, *value;
1265 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001266 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001267 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001268 unsigned short port = 0;
1269 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001272 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001273 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Jeff Layton6ee95422012-11-26 11:09:57 -05001275 /* ensure we always start with zeroed-out smb_vol */
1276 memset(vol, 0, sizeof(*vol));
1277
Jeff Layton88463992010-11-22 15:31:03 -05001278 /*
1279 * does not have to be perfect mapping since field is
1280 * informational, only used for servers that do not support
1281 * port 445 and it can be overridden at mount time
1282 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001283 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1284 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001285 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1286
Jeff Layton1397f2e2011-01-07 11:30:28 -05001287 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001288 /* null target name indicates to use *SMBSERVR default called name
1289 if we end up sending RFC1001 session initialize */
1290 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001291 vol->cred_uid = current_uid();
1292 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001293 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001294
Steve French2baa2682014-09-27 02:19:01 -05001295 /*
1296 * default to SFM style remapping of seven reserved characters
1297 * unless user overrides it or we negotiate CIFS POSIX where
1298 * it is unnecessary. Can not simultaneously use more than one mapping
1299 * since then readdir could list files that open could not open
1300 */
1301 vol->remap = true;
1302
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001303 /* default to only allowing write access to owner of the mount */
1304 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001307 /* default is always to request posix paths. */
1308 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001309 /* default to using server inode numbers where available */
1310 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001311
Jeff Layton1b359202012-09-19 15:20:27 -07001312 /* default is to use strict cifs caching semantics */
1313 vol->strict_io = true;
1314
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301315 vol->actimeo = CIFS_DEF_ACTIMEO;
1316
Steve French9764c022017-09-17 10:41:35 -05001317 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1318 vol->ops = &smb30_operations;
1319 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001320
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001321 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1322
Sean Finneyb9468452011-04-11 13:19:32 +00001323 if (!mountdata)
1324 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Sean Finneyb9468452011-04-11 13:19:32 +00001326 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1327 if (!mountdata_copy)
1328 goto cifs_parse_mount_err;
1329
1330 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001331 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001332
Steve French50c2f752007-07-13 00:33:32 +00001333 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001334 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 separator[0] = options[4];
1336 options += 5;
1337 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001338 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001341 vol->backupuid_specified = false; /* no backup intent for a user */
1342 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001343
Jeff Layton37d4f992013-05-24 07:40:05 -04001344 switch (cifs_parse_devname(devname, vol)) {
1345 case 0:
1346 break;
1347 case -ENOMEM:
1348 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1349 goto cifs_parse_mount_err;
1350 case -EINVAL:
1351 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1352 goto cifs_parse_mount_err;
1353 default:
1354 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1355 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001356 }
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001359 substring_t args[MAX_OPT_ARGS];
1360 unsigned long option;
1361 int token;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (!*data)
1364 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001366 token = match_token(data, cifs_mount_option_tokens, args);
1367
1368 switch (token) {
1369
1370 /* Ingnore the following */
1371 case Opt_ignore:
1372 break;
1373
1374 /* Boolean values */
1375 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001377 break;
1378 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001380 break;
1381 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001382 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001383 break;
1384 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001385 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001386 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001387 case Opt_forcegid:
1388 override_gid = 1;
1389 break;
1390 case Opt_noforcegid:
1391 override_gid = 0;
1392 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001393 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001394 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001395 break;
1396 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001397 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001398 break;
1399 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001401 break;
1402 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001404 break;
1405 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001407 break;
1408 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001410 break;
1411 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001412 vol->sfu_remap = true;
1413 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001414 break;
1415 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001416 vol->sfu_remap = false;
1417 break;
1418 case Opt_mapposix:
1419 vol->remap = true;
1420 vol->sfu_remap = false; /* disable SFU mapping */
1421 break;
1422 case Opt_nomapposix:
1423 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001424 break;
1425 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001426 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001427 break;
1428 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001429 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001430 break;
1431 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001432 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001433 break;
1434 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001435 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001436 break;
1437 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001438 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001439 break;
1440 case Opt_nounix:
Steve Frenchc18c8422007-07-18 23:21:09 +00001441 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001442 break;
1443 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001444 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001445 break;
1446 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001447 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001448 break;
1449 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001450 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001451 /*
1452 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001453 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001454 * local vfs will do advisory
1455 */
Steve French50c2f752007-07-13 00:33:32 +00001456 if (vol->file_mode ==
1457 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001458 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001459 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001460 case Opt_nohandlecache:
1461 vol->nohandlecache = 1;
1462 break;
1463 case Opt_handlecache:
1464 vol->nohandlecache = 0;
1465 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001466 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001467 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001468 break;
1469 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001471 break;
1472 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001474 break;
Steve French95932652016-09-23 01:36:34 -05001475 case Opt_setuidfromacl:
1476 vol->setuidfromacl = 1;
1477 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001478 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001479 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001480 break;
1481 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001482 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001483 break;
1484 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001486 break;
1487 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001489 break;
1490 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001492 break;
1493 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001495 break;
1496 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001497 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001498 break;
1499 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001500 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001501 break;
1502 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001504 break;
1505 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001507 break;
1508 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001509 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001510 break;
1511 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001512 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001513 break;
1514 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001515 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001516 break;
1517 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001519 break;
1520 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001522 break;
1523 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001524 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001525 break;
1526 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001527 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001528 break;
1529 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001530 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001531 * is a per tree connection (mount) not a per socket
1532 * or per-smb connection option in the protocol
1533 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1534 */
Steve French95b1cb92008-05-15 16:44:38 +00001535 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001536 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001537 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001538 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001539 break;
1540 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301541#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001542 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001543 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301544#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301545 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001546 break;
1547 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001548 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001549 break;
1550 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001551 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001552 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001553 case Opt_sloppy:
1554 sloppy = true;
1555 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001556 case Opt_nosharesock:
1557 vol->nosharesock = true;
1558 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001559 case Opt_nopersistent:
1560 vol->nopersistent = true;
1561 if (vol->persistent) {
1562 cifs_dbg(VFS,
1563 "persistenthandles mount options conflict\n");
1564 goto cifs_parse_mount_err;
1565 }
1566 break;
1567 case Opt_persistent:
1568 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001569 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001570 cifs_dbg(VFS,
1571 "persistenthandles mount options conflict\n");
1572 goto cifs_parse_mount_err;
1573 }
1574 break;
Steve French592fafe2015-11-03 10:08:53 -06001575 case Opt_resilient:
1576 vol->resilient = true;
1577 if (vol->persistent) {
1578 cifs_dbg(VFS,
1579 "persistenthandles mount options conflict\n");
1580 goto cifs_parse_mount_err;
1581 }
1582 break;
1583 case Opt_noresilient:
1584 vol->resilient = false; /* already the default */
1585 break;
Germano Percossi39566442016-12-15 12:31:18 +05301586 case Opt_domainauto:
1587 vol->domainauto = true;
1588 break;
Long Li8339dd32017-11-07 01:54:55 -07001589 case Opt_rdma:
1590 vol->rdma = true;
1591 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001592
1593 /* Numeric Values */
1594 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001595 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001596 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1597 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001598 goto cifs_parse_mount_err;
1599 }
1600 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001601 break;
1602 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001603 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001604 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1605 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001606 goto cifs_parse_mount_err;
1607 }
1608 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001609 break;
1610 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001611 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001612 cifs_dbg(VFS, "%s: Invalid uid value\n",
1613 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001614 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001616 uid_specified = true;
1617 break;
1618 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001619 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001620 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1621 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001622 goto cifs_parse_mount_err;
1623 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001624 break;
1625 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001626 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001627 cifs_dbg(VFS, "%s: Invalid gid value\n",
1628 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001629 goto cifs_parse_mount_err;
1630 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001631 gid_specified = true;
1632 break;
1633 case Opt_file_mode:
1634 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001635 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1636 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001637 goto cifs_parse_mount_err;
1638 }
1639 vol->file_mode = option;
1640 break;
1641 case Opt_dirmode:
1642 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001643 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1644 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001645 goto cifs_parse_mount_err;
1646 }
1647 vol->dir_mode = option;
1648 break;
1649 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001650 if (get_option_ul(args, &option) ||
1651 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001652 cifs_dbg(VFS, "%s: Invalid port value\n",
1653 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001654 goto cifs_parse_mount_err;
1655 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001656 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001657 break;
1658 case Opt_rsize:
1659 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001660 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1661 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001662 goto cifs_parse_mount_err;
1663 }
1664 vol->rsize = option;
1665 break;
1666 case Opt_wsize:
1667 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001668 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1669 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001670 goto cifs_parse_mount_err;
1671 }
1672 vol->wsize = option;
1673 break;
1674 case Opt_actimeo:
1675 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001676 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1677 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001678 goto cifs_parse_mount_err;
1679 }
1680 vol->actimeo = HZ * option;
1681 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001682 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001683 goto cifs_parse_mount_err;
1684 }
1685 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001686 case Opt_echo_interval:
1687 if (get_option_ul(args, &option)) {
1688 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1689 __func__);
1690 goto cifs_parse_mount_err;
1691 }
1692 vol->echo_interval = option;
1693 break;
Steve French8b217fe2016-11-11 22:36:20 -06001694 case Opt_snapshot:
1695 if (get_option_ul(args, &option)) {
1696 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1697 __func__);
1698 goto cifs_parse_mount_err;
1699 }
1700 vol->snapshot_time = option;
1701 break;
Steve French141891f2016-09-23 00:44:16 -05001702 case Opt_max_credits:
1703 if (get_option_ul(args, &option) || (option < 20) ||
1704 (option > 60000)) {
1705 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1706 __func__);
1707 goto cifs_parse_mount_err;
1708 }
1709 vol->max_credits = option;
1710 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001711
1712 /* String Arguments */
1713
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001714 case Opt_blank_user:
1715 /* null user, ie. anonymous authentication */
1716 vol->nullauth = 1;
1717 vol->username = NULL;
1718 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001719 case Opt_user:
1720 string = match_strdup(args);
1721 if (string == NULL)
1722 goto out_nomem;
1723
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001724 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1725 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001726 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001727 goto cifs_parse_mount_err;
1728 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001729
1730 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001731 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001732 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001733 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001734 break;
1735 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001736 /* passwords have to be handled differently
1737 * to allow the character used for deliminator
1738 * to be passed within them
1739 */
1740
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001741 /*
1742 * Check if this is a case where the password
1743 * starts with a delimiter
1744 */
1745 tmp_end = strchr(data, '=');
1746 tmp_end++;
1747 if (!(tmp_end < end && tmp_end[1] == delim)) {
1748 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001749 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001750 vol->password = NULL;
1751 break;
1752 }
1753 /* Yes it is. Drop down to Opt_pass below.*/
1754 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001755 /* Obtain the value string */
1756 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001757 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001758
1759 /* Set tmp_end to end of the string */
1760 tmp_end = (char *) value + strlen(value);
1761
1762 /* Check if following character is the deliminator
1763 * If yes, we have encountered a double deliminator
1764 * reset the NULL character to the deliminator
1765 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301766 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001767 tmp_end[0] = delim;
1768
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301769 /* Keep iterating until we get to a single
1770 * deliminator OR the end
1771 */
1772 while ((tmp_end = strchr(tmp_end, delim))
1773 != NULL && (tmp_end[1] == delim)) {
1774 tmp_end = (char *) &tmp_end[2];
1775 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001776
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301777 /* Reset var options to point to next element */
1778 if (tmp_end) {
1779 tmp_end[0] = '\0';
1780 options = (char *) &tmp_end[1];
1781 } else
1782 /* Reached the end of the mount option
1783 * string */
1784 options = end;
1785 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001786
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001787 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001788 /* Now build new password string */
1789 temp_len = strlen(value);
1790 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1791 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001792 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001793 goto cifs_parse_mount_err;
1794 }
1795
1796 for (i = 0, j = 0; i < temp_len; i++, j++) {
1797 vol->password[j] = value[i];
1798 if ((value[i] == delim) &&
1799 value[i+1] == delim)
1800 /* skip the second deliminator */
1801 i++;
1802 }
1803 vol->password[j] = '\0';
1804 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001805 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001806 /* FIXME: should this be an error instead? */
1807 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001808 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001809 case Opt_ip:
1810 string = match_strdup(args);
1811 if (string == NULL)
1812 goto out_nomem;
1813
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001814 if (!cifs_convert_address(dstaddr, string,
1815 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001816 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001817 goto cifs_parse_mount_err;
1818 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001819 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001820 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001821 case Opt_domain:
1822 string = match_strdup(args);
1823 if (string == NULL)
1824 goto out_nomem;
1825
Chen Gang057d6332013-07-19 09:01:36 +08001826 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1827 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001828 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001829 goto cifs_parse_mount_err;
1830 }
1831
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001832 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001833 vol->domainname = kstrdup(string, GFP_KERNEL);
1834 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001835 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001836 goto cifs_parse_mount_err;
1837 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001838 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001839 break;
1840 case Opt_srcaddr:
1841 string = match_strdup(args);
1842 if (string == NULL)
1843 goto out_nomem;
1844
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001845 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001846 (struct sockaddr *)&vol->srcaddr,
1847 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001848 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1849 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001850 goto cifs_parse_mount_err;
1851 }
1852 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001853 case Opt_iocharset:
1854 string = match_strdup(args);
1855 if (string == NULL)
1856 goto out_nomem;
1857
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001858 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001859 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001860 goto cifs_parse_mount_err;
1861 }
1862
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001863 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001864 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001865 vol->iocharset = kstrdup(string,
1866 GFP_KERNEL);
1867 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001868 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001869 goto cifs_parse_mount_err;
1870 }
1871 }
1872 /* if iocharset not set then load_nls_default
1873 * is used by caller
1874 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001875 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001876 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001877 case Opt_netbiosname:
1878 string = match_strdup(args);
1879 if (string == NULL)
1880 goto out_nomem;
1881
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001882 memset(vol->source_rfc1001_name, 0x20,
1883 RFC1001_NAME_LEN);
1884 /*
1885 * FIXME: are there cases in which a comma can
1886 * be valid in workstation netbios name (and
1887 * need special handling)?
1888 */
1889 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1890 /* don't ucase netbiosname for user */
1891 if (string[i] == 0)
1892 break;
1893 vol->source_rfc1001_name[i] = string[i];
1894 }
1895 /* The string has 16th byte zero still from
1896 * set at top of the function
1897 */
1898 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001899 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001900 break;
1901 case Opt_servern:
1902 /* servernetbiosname specified override *SMBSERVER */
1903 string = match_strdup(args);
1904 if (string == NULL)
1905 goto out_nomem;
1906
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001907 /* last byte, type, is 0x20 for servr type */
1908 memset(vol->target_rfc1001_name, 0x20,
1909 RFC1001_NAME_LEN_WITH_NULL);
1910
1911 /* BB are there cases in which a comma can be
1912 valid in this workstation netbios name
1913 (and need special handling)? */
1914
1915 /* user or mount helper must uppercase the
1916 netbios name */
1917 for (i = 0; i < 15; i++) {
1918 if (string[i] == 0)
1919 break;
1920 vol->target_rfc1001_name[i] = string[i];
1921 }
1922 /* The string has 16th byte zero still from
1923 set at top of the function */
1924 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001925 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001926 break;
1927 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05001928 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001929 string = match_strdup(args);
1930 if (string == NULL)
1931 goto out_nomem;
1932
Steve French7e682f72017-08-31 21:34:24 -05001933 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001934 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05001935 if (strlen(string) > 1) {
1936 pr_warn("Bad mount helper ver=%s. Did "
1937 "you want SMB1 (CIFS) dialect "
1938 "and mean to type vers=1.0 "
1939 "instead?\n", string);
1940 goto cifs_parse_mount_err;
1941 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001942 /* This is the default */
1943 break;
1944 }
1945 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05001946 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001947 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04001948 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05001949 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04001950 string = match_strdup(args);
1951 if (string == NULL)
1952 goto out_nomem;
1953
1954 if (cifs_parse_smb_version(string, vol) != 0)
1955 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05001956 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04001957 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001958 case Opt_sec:
1959 string = match_strdup(args);
1960 if (string == NULL)
1961 goto out_nomem;
1962
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001963 if (cifs_parse_security_flavors(string, vol) != 0)
1964 goto cifs_parse_mount_err;
1965 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04001966 case Opt_cache:
1967 string = match_strdup(args);
1968 if (string == NULL)
1969 goto out_nomem;
1970
1971 if (cifs_parse_cache_flavor(string, vol) != 0)
1972 goto cifs_parse_mount_err;
1973 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001974 default:
Jeff Laytond8162552012-03-23 14:40:56 -04001975 /*
1976 * An option we don't recognize. Save it off for later
1977 * if we haven't already found one
1978 */
1979 if (!invalid)
1980 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001983 /* Free up any allocated string */
1984 kfree(string);
1985 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04001987
Jeff Laytond8162552012-03-23 14:40:56 -04001988 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001989 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04001990 goto cifs_parse_mount_err;
1991 }
1992
Long Li8339dd32017-11-07 01:54:55 -07001993 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
1994 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
1995 goto cifs_parse_mount_err;
1996 }
1997
Jeff Layton8a8798a2012-01-17 16:09:15 -05001998#ifndef CONFIG_KEYS
1999 /* Muliuser mounts require CONFIG_KEYS support */
2000 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002001 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002002 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002003 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002004#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002005 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002006 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002007 goto cifs_parse_mount_err;
2008 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002009
Jeff Layton62a1a432012-12-10 06:10:45 -05002010 /* make sure UNC has a share name */
2011 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002012 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002013 goto cifs_parse_mount_err;
2014 }
2015
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002016 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002017 int len;
2018 const char *slash;
2019
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002020 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002021 /* Use the address part of the UNC. */
2022 slash = strchr(&vol->UNC[2], '\\');
2023 len = slash - &vol->UNC[2];
2024 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002025 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002026 goto cifs_parse_mount_err;
2027 }
2028 }
2029
2030 /* set the port that we got earlier */
2031 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002033 if (uid_specified)
2034 vol->override_uid = override_uid;
2035 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002036 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002037
2038 if (gid_specified)
2039 vol->override_gid = override_gid;
2040 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002041 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002042
Steve French7e682f72017-08-31 21:34:24 -05002043 if (got_version == false)
2044 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002045 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002046 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002047 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2048 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002049
Sean Finneyb9468452011-04-11 13:19:32 +00002050 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002052
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002053out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002054 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002055cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002056 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002057 kfree(mountdata_copy);
2058 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Ben Greear3eb9a882010-09-01 17:06:02 -07002061/** Returns true if srcaddr isn't specified and rhs isn't
2062 * specified, or if srcaddr is specified and
2063 * matches the IP address of the rhs argument.
2064 */
Jeff Layton45151482010-07-06 20:43:02 -04002065static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002066srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2067{
2068 switch (srcaddr->sa_family) {
2069 case AF_UNSPEC:
2070 return (rhs->sa_family == AF_UNSPEC);
2071 case AF_INET: {
2072 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2073 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2074 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2075 }
2076 case AF_INET6: {
2077 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002078 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002079 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2080 }
2081 default:
2082 WARN_ON(1);
2083 return false; /* don't expect to be here */
2084 }
2085}
2086
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002087/*
2088 * If no port is specified in addr structure, we try to match with 445 port
2089 * and if it fails - with 139 ports. It should be called only if address
2090 * families of server and addr are equal.
2091 */
2092static bool
2093match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2094{
Steve French6da97912011-03-13 18:55:55 +00002095 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002096
2097 switch (addr->sa_family) {
2098 case AF_INET:
2099 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2100 port = ((struct sockaddr_in *) addr)->sin_port;
2101 break;
2102 case AF_INET6:
2103 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2104 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2105 break;
2106 default:
2107 WARN_ON(1);
2108 return false;
2109 }
2110
2111 if (!port) {
2112 port = htons(CIFS_PORT);
2113 if (port == *sport)
2114 return true;
2115
2116 port = htons(RFC1001_PORT);
2117 }
2118
2119 return port == *sport;
2120}
Ben Greear3eb9a882010-09-01 17:06:02 -07002121
2122static bool
2123match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2124 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Jeff Layton45151482010-07-06 20:43:02 -04002126 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002127 case AF_INET: {
2128 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2129 struct sockaddr_in *srv_addr4 =
2130 (struct sockaddr_in *)&server->dstaddr;
2131
2132 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002133 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002134 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002135 }
2136 case AF_INET6: {
2137 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2138 struct sockaddr_in6 *srv_addr6 =
2139 (struct sockaddr_in6 *)&server->dstaddr;
2140
Jeff Layton45151482010-07-06 20:43:02 -04002141 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002142 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002143 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002144 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002145 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002146 break;
2147 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002148 default:
2149 WARN_ON(1);
2150 return false; /* don't expect to be here */
2151 }
Jeff Layton45151482010-07-06 20:43:02 -04002152
Ben Greear3eb9a882010-09-01 17:06:02 -07002153 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2154 return false;
2155
Jeff Layton45151482010-07-06 20:43:02 -04002156 return true;
2157}
2158
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002159static bool
2160match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2161{
Jeff Layton3f618222013-06-12 19:52:14 -05002162 /*
2163 * The select_sectype function should either return the vol->sectype
2164 * that was specified, or "Unspecified" if that sectype was not
2165 * compatible with the given NEGOTIATE request.
2166 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302167 if (server->ops->select_sectype(server, vol->sectype)
2168 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002169 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002170
Jeff Layton3f618222013-06-12 19:52:14 -05002171 /*
2172 * Now check if signing mode is acceptable. No need to check
2173 * global_secflags at this point since if MUST_SIGN is set then
2174 * the server->sign had better be too.
2175 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002176 if (vol->sign && !server->sign)
2177 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002178
2179 return true;
2180}
2181
Jeff Layton9fa114f2012-11-26 11:09:57 -05002182static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002183{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002184 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2185
Jeff Laytona0b3df52013-05-24 07:40:59 -04002186 if (vol->nosharesock)
2187 return 0;
2188
Steve French9764c022017-09-17 10:41:35 -05002189 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002190 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2191 return 0;
2192
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002193 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2194 return 0;
2195
2196 if (!match_address(server, addr,
2197 (struct sockaddr *)&vol->srcaddr))
2198 return 0;
2199
2200 if (!match_port(server, addr))
2201 return 0;
2202
2203 if (!match_security(server, vol))
2204 return 0;
2205
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002206 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002207 return 0;
2208
Long Li8339dd32017-11-07 01:54:55 -07002209 if (server->rdma != vol->rdma)
2210 return 0;
2211
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002212 return 1;
2213}
2214
Jeff Layton45151482010-07-06 20:43:02 -04002215static struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002216cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002217{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002218 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302220 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002221 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002222 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002223 continue;
2224
Jeff Laytone7ddee92008-11-14 13:44:38 -05002225 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302226 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002227 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002228 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302230 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return NULL;
2232}
2233
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002234void
2235cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002237 struct task_struct *task;
2238
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302239 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002240 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302241 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002242 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002244
Rob Landleyf1d0c992011-01-22 15:44:05 -06002245 put_net(cifs_net_ns(server));
2246
Jeff Laytone7ddee92008-11-14 13:44:38 -05002247 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302248 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002249
Jeff Laytonc74093b2011-01-11 07:24:23 -05002250 cancel_delayed_work_sync(&server->echo);
2251
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002252 if (from_reconnect)
2253 /*
2254 * Avoid deadlock here: reconnect work calls
2255 * cifs_put_tcp_session() at its end. Need to be sure
2256 * that reconnect work does nothing with server pointer after
2257 * that step.
2258 */
2259 cancel_delayed_work(&server->reconnect);
2260 else
2261 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002262
Jeff Laytone7ddee92008-11-14 13:44:38 -05002263 spin_lock(&GlobalMid_Lock);
2264 server->tcpStatus = CifsExiting;
2265 spin_unlock(&GlobalMid_Lock);
2266
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002267 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302268 cifs_fscache_release_client_cookie(server);
2269
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002270 kfree(server->session_key.response);
2271 server->session_key.response = NULL;
2272 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002273
2274 task = xchg(&server->tsk, NULL);
2275 if (task)
2276 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
Jeff Layton63c038c2008-12-01 18:41:46 -05002279static struct TCP_Server_Info *
2280cifs_get_tcp_session(struct smb_vol *volume_info)
2281{
2282 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002283 int rc;
2284
Joe Perchesf96637b2013-05-04 22:12:25 -05002285 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002286
2287 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002288 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002289 if (tcp_ses)
2290 return tcp_ses;
2291
2292 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2293 if (!tcp_ses) {
2294 rc = -ENOMEM;
2295 goto out_err;
2296 }
2297
Jeff Layton23db65f2012-05-15 12:20:51 -04002298 tcp_ses->ops = volume_info->ops;
2299 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002300 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002301 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2302 if (IS_ERR(tcp_ses->hostname)) {
2303 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002304 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002305 }
2306
2307 tcp_ses->noblocksnd = volume_info->noblocksnd;
2308 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002309 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002310 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002311 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002312 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002313 init_waitqueue_head(&tcp_ses->response_q);
2314 init_waitqueue_head(&tcp_ses->request_q);
2315 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2316 mutex_init(&tcp_ses->srv_mutex);
2317 memcpy(tcp_ses->workstation_RFC1001_name,
2318 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2319 memcpy(tcp_ses->server_RFC1001_name,
2320 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002321 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002322 tcp_ses->sequence_number = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002323 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002324 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002325 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2326 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002327 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002328 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2329 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002330 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2331 sizeof(tcp_ses->srcaddr));
2332 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2333 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002334 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002335 /*
2336 * at this point we are the only ones with the pointer
2337 * to the struct since the kernel thread not created yet
2338 * no need to spinlock this init of tcpStatus or srv_count
2339 */
2340 tcp_ses->tcpStatus = CifsNew;
2341 ++tcp_ses->srv_count;
2342
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002343 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2344 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2345 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2346 else
2347 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002348 if (tcp_ses->rdma) {
2349#ifndef CONFIG_CIFS_SMB_DIRECT
2350 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2351 rc = -ENOENT;
2352 goto out_err_crypto_release;
2353#endif
2354 tcp_ses->smbd_conn = smbd_get_connection(
2355 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2356 if (tcp_ses->smbd_conn) {
2357 cifs_dbg(VFS, "RDMA transport established\n");
2358 rc = 0;
2359 goto smbd_connected;
2360 } else {
2361 rc = -ENOENT;
2362 goto out_err_crypto_release;
2363 }
2364 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002365 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002366 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002367 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002368 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002369 }
Long Li2f894642017-11-22 17:38:34 -07002370smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002371 /*
2372 * since we're in a cifs function already, we know that
2373 * this will succeed. No need for try_module_get().
2374 */
2375 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002376 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002377 tcp_ses, "cifsd");
2378 if (IS_ERR(tcp_ses->tsk)) {
2379 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002380 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002381 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002382 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002383 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002384 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002385
2386 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302387 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002388 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302389 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002390
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302391 cifs_fscache_get_client_cookie(tcp_ses);
2392
Jeff Laytonc74093b2011-01-11 07:24:23 -05002393 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002394 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002395
Jeff Layton63c038c2008-12-01 18:41:46 -05002396 return tcp_ses;
2397
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002398out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002399 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002400
Rob Landleyf1d0c992011-01-22 15:44:05 -06002401 put_net(cifs_net_ns(tcp_ses));
2402
Jeff Layton63c038c2008-12-01 18:41:46 -05002403out_err:
2404 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002405 if (!IS_ERR(tcp_ses->hostname))
2406 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002407 if (tcp_ses->ssocket)
2408 sock_release(tcp_ses->ssocket);
2409 kfree(tcp_ses);
2410 }
2411 return ERR_PTR(rc);
2412}
2413
Steve French96daf2b2011-05-27 04:34:02 +00002414static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002415{
Jeff Layton3f618222013-06-12 19:52:14 -05002416 if (vol->sectype != Unspecified &&
2417 vol->sectype != ses->sectype)
2418 return 0;
2419
2420 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002421 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002422 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002423 return 0;
2424 break;
2425 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002426 /* NULL username means anonymous session */
2427 if (ses->user_name == NULL) {
2428 if (!vol->nullauth)
2429 return 0;
2430 break;
2431 }
2432
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002433 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002434 if (strncmp(ses->user_name,
2435 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002436 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002437 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002438 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002439 ses->password != NULL &&
2440 strncmp(ses->password,
2441 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002442 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002443 return 0;
2444 }
2445 return 1;
2446}
2447
Aurelien Aptelb327a712018-01-24 13:46:10 +01002448/**
2449 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2450 *
2451 * A new IPC connection is made and stored in the session
2452 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2453 */
2454static int
2455cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2456{
2457 int rc = 0, xid;
2458 struct cifs_tcon *tcon;
2459 struct nls_table *nls_codepage;
2460 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2461 bool seal = false;
2462
2463 /*
2464 * If the mount request that resulted in the creation of the
2465 * session requires encryption, force IPC to be encrypted too.
2466 */
2467 if (volume_info->seal) {
2468 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2469 seal = true;
2470 else {
2471 cifs_dbg(VFS,
2472 "IPC: server doesn't support encryption\n");
2473 return -EOPNOTSUPP;
2474 }
2475 }
2476
2477 tcon = tconInfoAlloc();
2478 if (tcon == NULL)
2479 return -ENOMEM;
2480
2481 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->serverName);
2482
2483 /* cannot fail */
2484 nls_codepage = load_nls_default();
2485
2486 xid = get_xid();
2487 tcon->ses = ses;
2488 tcon->ipc = true;
2489 tcon->seal = seal;
2490 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2491 free_xid(xid);
2492
2493 if (rc) {
2494 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2495 tconInfoFree(tcon);
2496 goto out;
2497 }
2498
2499 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2500
2501 ses->tcon_ipc = tcon;
2502out:
2503 unload_nls(nls_codepage);
2504 return rc;
2505}
2506
2507/**
2508 * cifs_free_ipc - helper to release the session IPC tcon
2509 *
2510 * Needs to be called everytime a session is destroyed
2511 */
2512static int
2513cifs_free_ipc(struct cifs_ses *ses)
2514{
2515 int rc = 0, xid;
2516 struct cifs_tcon *tcon = ses->tcon_ipc;
2517
2518 if (tcon == NULL)
2519 return 0;
2520
2521 if (ses->server->ops->tree_disconnect) {
2522 xid = get_xid();
2523 rc = ses->server->ops->tree_disconnect(xid, tcon);
2524 free_xid(xid);
2525 }
2526
2527 if (rc)
2528 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2529
2530 tconInfoFree(tcon);
2531 ses->tcon_ipc = NULL;
2532 return rc;
2533}
2534
Steve French96daf2b2011-05-27 04:34:02 +00002535static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002536cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Steve French96daf2b2011-05-27 04:34:02 +00002538 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302540 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002541 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002542 if (ses->status == CifsExiting)
2543 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002544 if (!match_session(ses, vol))
2545 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002546 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302547 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002548 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302550 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 return NULL;
2552}
2553
Jeff Layton14fbf502008-11-14 13:53:46 -05002554static void
Steve French96daf2b2011-05-27 04:34:02 +00002555cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002556{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002557 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002558 struct TCP_Server_Info *server = ses->server;
2559
Joe Perchesf96637b2013-05-04 22:12:25 -05002560 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002561
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302562 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002563 if (ses->status == CifsExiting) {
2564 spin_unlock(&cifs_tcp_ses_lock);
2565 return;
2566 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002567 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302568 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002569 return;
2570 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002571 if (ses->status == CifsGood)
2572 ses->status = CifsExiting;
2573 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002574
Aurelien Aptelb327a712018-01-24 13:46:10 +01002575 cifs_free_ipc(ses);
2576
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002577 if (ses->status == CifsExiting && server->ops->logoff) {
2578 xid = get_xid();
2579 rc = server->ops->logoff(xid, ses);
2580 if (rc)
2581 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2582 __func__, rc);
2583 _free_xid(xid);
2584 }
2585
2586 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002587 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302588 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002589
Jeff Layton14fbf502008-11-14 13:53:46 -05002590 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002591 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002592}
2593
Jeff Layton8a8798a2012-01-17 16:09:15 -05002594#ifdef CONFIG_KEYS
2595
Chen Gang057d6332013-07-19 09:01:36 +08002596/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2597#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002598
2599/* Populate username and pw fields from keyring if possible */
2600static int
2601cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2602{
2603 int rc = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002604 const char *delim, *payload;
2605 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002606 ssize_t len;
2607 struct key *key;
2608 struct TCP_Server_Info *server = ses->server;
2609 struct sockaddr_in *sa;
2610 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002611 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002612
2613 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2614 if (!desc)
2615 return -ENOMEM;
2616
2617 /* try to find an address key first */
2618 switch (server->dstaddr.ss_family) {
2619 case AF_INET:
2620 sa = (struct sockaddr_in *)&server->dstaddr;
2621 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2622 break;
2623 case AF_INET6:
2624 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2625 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2626 break;
2627 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002628 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2629 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002630 rc = -EINVAL;
2631 goto out_err;
2632 }
2633
Joe Perchesf96637b2013-05-04 22:12:25 -05002634 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002635 key = request_key(&key_type_logon, desc, "");
2636 if (IS_ERR(key)) {
2637 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002638 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002639 rc = PTR_ERR(key);
2640 goto out_err;
2641 }
2642
2643 /* didn't work, try to find a domain key */
2644 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002645 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002646 key = request_key(&key_type_logon, desc, "");
2647 if (IS_ERR(key)) {
2648 rc = PTR_ERR(key);
2649 goto out_err;
2650 }
2651 }
2652
2653 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002654 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002655 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002656 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002657 goto out_key_put;
2658 }
2659
2660 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002661 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002662 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002663 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002664 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002665 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2666 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002667 rc = -EINVAL;
2668 goto out_key_put;
2669 }
2670
2671 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002672 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002673 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2674 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002675 rc = -EINVAL;
2676 goto out_key_put;
2677 }
2678
2679 vol->username = kstrndup(payload, len, GFP_KERNEL);
2680 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002681 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2682 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002683 rc = -ENOMEM;
2684 goto out_key_put;
2685 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002686 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002687
2688 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002689 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002690 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002691 rc = -EINVAL;
2692 kfree(vol->username);
2693 vol->username = NULL;
2694 goto out_key_put;
2695 }
2696
2697 ++delim;
2698 vol->password = kstrndup(delim, len, GFP_KERNEL);
2699 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002700 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2701 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002702 rc = -ENOMEM;
2703 kfree(vol->username);
2704 vol->username = NULL;
2705 goto out_key_put;
2706 }
2707
2708out_key_put:
2709 up_read(&key->sem);
2710 key_put(key);
2711out_err:
2712 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002713 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002714 return rc;
2715}
2716#else /* ! CONFIG_KEYS */
2717static inline int
2718cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2719 struct cifs_ses *ses __attribute__((unused)))
2720{
2721 return -ENOSYS;
2722}
2723#endif /* CONFIG_KEYS */
2724
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002725/**
2726 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2727 *
2728 * This function assumes it is being called from cifs_mount() where we
2729 * already got a server reference (server refcount +1). See
2730 * cifs_get_tcon() for refcount explanations.
2731 */
Steve French96daf2b2011-05-27 04:34:02 +00002732static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002733cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2734{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002735 int rc = -ENOMEM;
2736 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002737 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002738 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2739 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002740
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002741 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002742
Jeff Layton4ff67b72010-07-06 20:43:02 -04002743 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002744 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002745 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2746 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002747
Jeff Layton36988c72010-04-24 07:57:43 -04002748 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002749 rc = cifs_negotiate_protocol(xid, ses);
2750 if (rc) {
2751 mutex_unlock(&ses->session_mutex);
2752 /* problem -- put our ses reference */
2753 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002754 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002755 return ERR_PTR(rc);
2756 }
Jeff Layton36988c72010-04-24 07:57:43 -04002757 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002758 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002759 rc = cifs_setup_session(xid, ses,
2760 volume_info->local_nls);
2761 if (rc) {
2762 mutex_unlock(&ses->session_mutex);
2763 /* problem -- put our reference */
2764 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002765 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002766 return ERR_PTR(rc);
2767 }
2768 }
2769 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002770
2771 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002772 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002773 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002774 return ses;
2775 }
2776
Joe Perchesf96637b2013-05-04 22:12:25 -05002777 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002778 ses = sesInfoAlloc();
2779 if (ses == NULL)
2780 goto get_ses_fail;
2781
2782 /* new SMB session uses our server ref */
2783 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002784 if (server->dstaddr.ss_family == AF_INET6)
2785 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002786 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002787 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002788
Steve French8727c8a2011-02-25 01:11:56 -06002789 if (volume_info->username) {
2790 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2791 if (!ses->user_name)
2792 goto get_ses_fail;
2793 }
Jeff Layton36988c72010-04-24 07:57:43 -04002794
2795 /* volume_info->password freed at unmount */
2796 if (volume_info->password) {
2797 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2798 if (!ses->password)
2799 goto get_ses_fail;
2800 }
2801 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002802 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2803 if (!ses->domainName)
2804 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002805 }
Germano Percossi39566442016-12-15 12:31:18 +05302806 if (volume_info->domainauto)
2807 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002808 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002809 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002810
Jeff Layton28e11bd2013-05-26 07:01:00 -04002811 ses->sectype = volume_info->sectype;
2812 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002813
2814 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002815 rc = cifs_negotiate_protocol(xid, ses);
2816 if (!rc)
2817 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002818 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002819 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002820 goto get_ses_fail;
2821
2822 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302823 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002824 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302825 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002826
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002827 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002828
2829 cifs_setup_ipc(ses, volume_info);
2830
Jeff Layton36988c72010-04-24 07:57:43 -04002831 return ses;
2832
2833get_ses_fail:
2834 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002835 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002836 return ERR_PTR(rc);
2837}
2838
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002839static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002840{
2841 if (tcon->tidStatus == CifsExiting)
2842 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002843 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002844 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002845 if (tcon->seal != volume_info->seal)
2846 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002847 if (tcon->snapshot_time != volume_info->snapshot_time)
2848 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002849 return 1;
2850}
2851
Steve French96daf2b2011-05-27 04:34:02 +00002852static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06002853cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002856 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302858 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002859 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002860 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002861 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002862 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002863 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302864 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return tcon;
2866 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302867 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return NULL;
2869}
2870
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002871void
Steve French96daf2b2011-05-27 04:34:02 +00002872cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002873{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002874 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002875 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002876
Aurelien Aptelb327a712018-01-24 13:46:10 +01002877 /*
2878 * IPC tcon share the lifetime of their session and are
2879 * destroyed in the session put function
2880 */
2881 if (tcon == NULL || tcon->ipc)
2882 return;
2883
2884 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002885 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302886 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002887 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302888 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002889 return;
2890 }
2891
2892 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302893 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002894
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002895 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002896 if (ses->server->ops->tree_disconnect)
2897 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002898 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002899
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302900 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002901 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002902 cifs_put_smb_ses(ses);
2903}
2904
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002905/**
2906 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
2907 *
2908 * - tcon refcount is the number of mount points using the tcon.
2909 * - ses refcount is the number of tcon using the session.
2910 *
2911 * 1. This function assumes it is being called from cifs_mount() where
2912 * we already got a session reference (ses refcount +1).
2913 *
2914 * 2. Since we're in the context of adding a mount point, the end
2915 * result should be either:
2916 *
2917 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2918 * its session refcount incremented (1 new tcon). This +1 was
2919 * already done in (1).
2920 *
2921 * b) an existing tcon with refcount+1 (add a mount point to it) and
2922 * identical ses refcount (no new tcon). Because of (1) we need to
2923 * decrement the ses refcount.
2924 */
Steve French96daf2b2011-05-27 04:34:02 +00002925static struct cifs_tcon *
2926cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002927{
2928 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002929 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002930
Steve French8b217fe2016-11-11 22:36:20 -06002931 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002932 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002933 /*
2934 * tcon has refcount already incremented but we need to
2935 * decrement extra ses reference gotten by caller (case b)
2936 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002937 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002938 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002939 return tcon;
2940 }
2941
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002942 if (!ses->server->ops->tree_connect) {
2943 rc = -ENOSYS;
2944 goto out_fail;
2945 }
2946
Jeff Laytond00c28d2010-04-24 07:57:44 -04002947 tcon = tconInfoAlloc();
2948 if (tcon == NULL) {
2949 rc = -ENOMEM;
2950 goto out_fail;
2951 }
2952
Steve French8b217fe2016-11-11 22:36:20 -06002953 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06002954 if (ses->server->vals->protocol_id == 0) {
2955 cifs_dbg(VFS,
2956 "Use SMB2 or later for snapshot mount option\n");
2957 rc = -EOPNOTSUPP;
2958 goto out_fail;
2959 } else
2960 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06002961 }
2962
Jeff Laytond00c28d2010-04-24 07:57:44 -04002963 tcon->ses = ses;
2964 if (volume_info->password) {
2965 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
2966 if (!tcon->password) {
2967 rc = -ENOMEM;
2968 goto out_fail;
2969 }
2970 }
2971
Steve French23657ad2018-04-22 15:14:58 -05002972 if (volume_info->seal) {
2973 if (ses->server->vals->protocol_id == 0) {
2974 cifs_dbg(VFS,
2975 "SMB3 or later required for encryption\n");
2976 rc = -EOPNOTSUPP;
2977 goto out_fail;
2978 } else if (tcon->ses->server->capabilities &
2979 SMB2_GLOBAL_CAP_ENCRYPTION)
2980 tcon->seal = true;
2981 else {
2982 cifs_dbg(VFS, "Encryption is not supported on share\n");
2983 rc = -EOPNOTSUPP;
2984 goto out_fail;
2985 }
2986 }
2987
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002988 /*
2989 * BB Do we need to wrap session_mutex around this TCon call and Unix
2990 * SetFS as we do on SessSetup and reconnect?
2991 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002992 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002993 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
2994 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002995 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05002996 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002997 if (rc)
2998 goto out_fail;
2999
3000 if (volume_info->nodfs) {
3001 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
Joe Perchesf96637b2013-05-04 22:12:25 -05003002 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003003 }
Steve Frenchb618f002015-11-03 09:15:03 -06003004 tcon->use_persistent = false;
3005 /* check if SMB2 or later, CIFS does not support persistent handles */
3006 if (volume_info->persistent) {
3007 if (ses->server->vals->protocol_id == 0) {
3008 cifs_dbg(VFS,
3009 "SMB3 or later required for persistent handles\n");
3010 rc = -EOPNOTSUPP;
3011 goto out_fail;
3012 } else if (ses->server->capabilities &
3013 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3014 tcon->use_persistent = true;
3015 else /* persistent handles requested but not supported */ {
3016 cifs_dbg(VFS,
3017 "Persistent handles not supported on share\n");
3018 rc = -EOPNOTSUPP;
3019 goto out_fail;
3020 }
3021 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3022 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3023 && (volume_info->nopersistent == false)) {
3024 cifs_dbg(FYI, "enabling persistent handles\n");
3025 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003026 } else if (volume_info->resilient) {
3027 if (ses->server->vals->protocol_id == 0) {
3028 cifs_dbg(VFS,
3029 "SMB2.1 or later required for resilient handles\n");
3030 rc = -EOPNOTSUPP;
3031 goto out_fail;
3032 }
3033 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003034 }
3035
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003036 /*
3037 * We can have only one retry value for a connection to a share so for
3038 * resources mounted more than once to the same server share the last
3039 * value passed in for the retry flag is used.
3040 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003041 tcon->retry = volume_info->retry;
3042 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003043 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003044 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003045 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003046
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303047 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003048 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303049 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003050
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303051 cifs_fscache_get_super_cookie(tcon);
3052
Jeff Laytond00c28d2010-04-24 07:57:44 -04003053 return tcon;
3054
3055out_fail:
3056 tconInfoFree(tcon);
3057 return ERR_PTR(rc);
3058}
3059
Jeff Layton9d002df2010-10-06 19:51:11 -04003060void
3061cifs_put_tlink(struct tcon_link *tlink)
3062{
3063 if (!tlink || IS_ERR(tlink))
3064 return;
3065
3066 if (!atomic_dec_and_test(&tlink->tl_count) ||
3067 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3068 tlink->tl_time = jiffies;
3069 return;
3070 }
3071
3072 if (!IS_ERR(tlink_tcon(tlink)))
3073 cifs_put_tcon(tlink_tcon(tlink));
3074 kfree(tlink);
3075 return;
3076}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003077
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003078static inline struct tcon_link *
Pavel Shilovskycd518752011-06-09 12:58:53 +04003079cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
3080{
3081 return cifs_sb->master_tlink;
3082}
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003083
3084static int
3085compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3086{
3087 struct cifs_sb_info *old = CIFS_SB(sb);
3088 struct cifs_sb_info *new = mnt_data->cifs_sb;
3089
3090 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3091 return 0;
3092
3093 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3094 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3095 return 0;
3096
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003097 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003098 * We want to share sb only if we don't specify an r/wsize or
3099 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003100 */
3101 if (new->wsize && new->wsize < old->wsize)
3102 return 0;
3103
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003104 if (new->rsize && new->rsize < old->rsize)
3105 return 0;
3106
Eric W. Biederman1f682332013-02-06 01:20:20 -08003107 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003108 return 0;
3109
3110 if (old->mnt_file_mode != new->mnt_file_mode ||
3111 old->mnt_dir_mode != new->mnt_dir_mode)
3112 return 0;
3113
3114 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3115 return 0;
3116
3117 if (old->actimeo != new->actimeo)
3118 return 0;
3119
3120 return 1;
3121}
3122
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003123static int
3124match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3125{
3126 struct cifs_sb_info *old = CIFS_SB(sb);
3127 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003128 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3129 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003130
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003131 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003132 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003133 else if (!old_set && !new_set)
3134 return 1;
3135
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003136 return 0;
3137}
3138
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003139int
3140cifs_match_super(struct super_block *sb, void *data)
3141{
3142 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3143 struct smb_vol *volume_info;
3144 struct cifs_sb_info *cifs_sb;
3145 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003146 struct cifs_ses *ses;
3147 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003148 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003149 int rc = 0;
3150
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003151 spin_lock(&cifs_tcp_ses_lock);
3152 cifs_sb = CIFS_SB(sb);
3153 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3154 if (IS_ERR(tlink)) {
3155 spin_unlock(&cifs_tcp_ses_lock);
3156 return rc;
3157 }
3158 tcon = tlink_tcon(tlink);
3159 ses = tcon->ses;
3160 tcp_srv = ses->server;
3161
3162 volume_info = mnt_data->vol;
3163
Jeff Layton9fa114f2012-11-26 11:09:57 -05003164 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003165 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003166 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003167 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003168 rc = 0;
3169 goto out;
3170 }
3171
3172 rc = compare_mount_options(sb, mnt_data);
3173out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003174 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003175 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003176 return rc;
3177}
3178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003180get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003181 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3182 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 int rc = 0;
3185
Aurelien Aptelb327a712018-01-24 13:46:10 +01003186 if (!ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003187 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003189 *num_referrals = 0;
3190 *referrals = NULL;
3191
Aurelien Aptelb327a712018-01-24 13:46:10 +01003192 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3193 referrals, num_referrals,
3194 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 return rc;
3196}
3197
Jeff Layton09e50d52008-07-23 10:11:19 -04003198#ifdef CONFIG_DEBUG_LOCK_ALLOC
3199static struct lock_class_key cifs_key[2];
3200static struct lock_class_key cifs_slock_key[2];
3201
3202static inline void
3203cifs_reclassify_socket4(struct socket *sock)
3204{
3205 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003206 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003207 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3208 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3209}
3210
3211static inline void
3212cifs_reclassify_socket6(struct socket *sock)
3213{
3214 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003215 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003216 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3217 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3218}
3219#else
3220static inline void
3221cifs_reclassify_socket4(struct socket *sock)
3222{
3223}
3224
3225static inline void
3226cifs_reclassify_socket6(struct socket *sock)
3227{
3228}
3229#endif
3230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003232static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
Steve French50c2f752007-07-13 00:33:32 +00003234 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Steve French50c2f752007-07-13 00:33:32 +00003236 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 /* mask a nibble at a time and encode */
3238 target[j] = 'A' + (0x0F & (source[i] >> 4));
3239 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003240 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
3242
3243}
3244
Ben Greear3eb9a882010-09-01 17:06:02 -07003245static int
3246bind_socket(struct TCP_Server_Info *server)
3247{
3248 int rc = 0;
3249 if (server->srcaddr.ss_family != AF_UNSPEC) {
3250 /* Bind to the specified local IP address */
3251 struct socket *socket = server->ssocket;
3252 rc = socket->ops->bind(socket,
3253 (struct sockaddr *) &server->srcaddr,
3254 sizeof(server->srcaddr));
3255 if (rc < 0) {
3256 struct sockaddr_in *saddr4;
3257 struct sockaddr_in6 *saddr6;
3258 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3259 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3260 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003261 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3262 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003263 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003264 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3265 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003266 }
3267 }
3268 return rc;
3269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003272ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
3274 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003275 /*
3276 * some servers require RFC1001 sessinit before sending
3277 * negprot - BB check reconnection in case where second
3278 * sessinit is sent but no second negprot
3279 */
3280 struct rfc1002_session_packet *ses_init_buf;
3281 struct smb_hdr *smb_buf;
3282 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3283 GFP_KERNEL);
3284 if (ses_init_buf) {
3285 ses_init_buf->trailer.session_req.called_len = 32;
3286
Colin Ian King997152f2016-01-25 16:25:54 +00003287 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003288 rfc1002mangle(ses_init_buf->trailer.
3289 session_req.called_name,
3290 server->server_RFC1001_name,
3291 RFC1001_NAME_LEN_WITH_NULL);
3292 else
3293 rfc1002mangle(ses_init_buf->trailer.
3294 session_req.called_name,
3295 DEFAULT_CIFS_CALLED_NAME,
3296 RFC1001_NAME_LEN_WITH_NULL);
3297
3298 ses_init_buf->trailer.session_req.calling_len = 32;
3299
3300 /*
3301 * calling name ends in null (byte 16) from old smb
3302 * convention.
3303 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003304 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003305 rfc1002mangle(ses_init_buf->trailer.
3306 session_req.calling_name,
3307 server->workstation_RFC1001_name,
3308 RFC1001_NAME_LEN_WITH_NULL);
3309 else
3310 rfc1002mangle(ses_init_buf->trailer.
3311 session_req.calling_name,
3312 "LINUX_CIFS_CLNT",
3313 RFC1001_NAME_LEN_WITH_NULL);
3314
3315 ses_init_buf->trailer.session_req.scope1 = 0;
3316 ses_init_buf->trailer.session_req.scope2 = 0;
3317 smb_buf = (struct smb_hdr *)ses_init_buf;
3318
3319 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003320 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003321 rc = smb_send(server, smb_buf, 0x44);
3322 kfree(ses_init_buf);
3323 /*
3324 * RFC1001 layer in at least one server
3325 * requires very short break before negprot
3326 * presumably because not expecting negprot
3327 * to follow so fast. This is a simple
3328 * solution that works without
3329 * complicating the code and causes no
3330 * significant slowing down on mount
3331 * for everyone else
3332 */
3333 usleep_range(1000, 2000);
3334 }
3335 /*
3336 * else the negprot may still work without this
3337 * even though malloc failed
3338 */
3339
3340 return rc;
3341}
3342
3343static int
3344generic_ip_connect(struct TCP_Server_Info *server)
3345{
3346 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003347 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003348 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003349 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003350 struct sockaddr *saddr;
3351
3352 saddr = (struct sockaddr *) &server->dstaddr;
3353
3354 if (server->dstaddr.ss_family == AF_INET6) {
3355 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3356 slen = sizeof(struct sockaddr_in6);
3357 sfamily = AF_INET6;
3358 } else {
3359 sport = ((struct sockaddr_in *) saddr)->sin_port;
3360 slen = sizeof(struct sockaddr_in);
3361 sfamily = AF_INET;
3362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003364 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003365 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3366 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003368 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003369 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003372
3373 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003374 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003375 server->ssocket = socket;
3376 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003377 if (sfamily == AF_INET6)
3378 cifs_reclassify_socket6(socket);
3379 else
3380 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 }
3382
Ben Greear3eb9a882010-09-01 17:06:02 -07003383 rc = bind_socket(server);
3384 if (rc < 0)
3385 return rc;
3386
Jeff Laytond5c56052008-12-01 18:42:33 -05003387 /*
3388 * Eventually check for other socket options to change from
3389 * the default. sock_setsockopt not used because it expects
3390 * user space buffer
3391 */
3392 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003393 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003394
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003395 /* make the bufsizes depend on wsize/rsize and max requests */
3396 if (server->noautotune) {
3397 if (socket->sk->sk_sndbuf < (200 * 1024))
3398 socket->sk->sk_sndbuf = 200 * 1024;
3399 if (socket->sk->sk_rcvbuf < (140 * 1024))
3400 socket->sk->sk_rcvbuf = 140 * 1024;
3401 }
3402
Steve French6a5fa2362010-01-01 01:28:43 +00003403 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003404 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003405 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3406 (char *)&val, sizeof(val));
3407 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003408 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3409 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003410 }
3411
Joe Perchesf96637b2013-05-04 22:12:25 -05003412 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003413 socket->sk->sk_sndbuf,
3414 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3415
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003416 rc = socket->ops->connect(socket, saddr, slen, 0);
3417 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003418 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003419 sock_release(socket);
3420 server->ssocket = NULL;
3421 return rc;
3422 }
3423
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003424 if (sport == htons(RFC1001_PORT))
3425 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return rc;
3428}
3429
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003430static int
3431ip_connect(struct TCP_Server_Info *server)
3432{
Steve French6da97912011-03-13 18:55:55 +00003433 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003434 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3435 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3436
3437 if (server->dstaddr.ss_family == AF_INET6)
3438 sport = &addr6->sin6_port;
3439 else
3440 sport = &addr->sin_port;
3441
3442 if (*sport == 0) {
3443 int rc;
3444
3445 /* try with 445 port at first */
3446 *sport = htons(CIFS_PORT);
3447
3448 rc = generic_ip_connect(server);
3449 if (rc >= 0)
3450 return rc;
3451
3452 /* if it failed, try with 139 port */
3453 *sport = htons(RFC1001_PORT);
3454 }
3455
3456 return generic_ip_connect(server);
3457}
3458
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003459void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003460 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003461{
3462 /* if we are reconnecting then should we check to see if
3463 * any requested capabilities changed locally e.g. via
3464 * remount but we can not do much about it here
3465 * if they have (even if we could detect it by the following)
3466 * Perhaps we could add a backpointer to array of sb from tcon
3467 * or if we change to make all sb to same share the same
3468 * sb as NFS - then we only have one backpointer to sb.
3469 * What if we wanted to mount the server share twice once with
3470 * and once without posixacls or posix paths? */
3471 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003472
Steve Frenchc18c8422007-07-18 23:21:09 +00003473 if (vol_info && vol_info->no_linux_ext) {
3474 tcon->fsUnixInfo.Capability = 0;
3475 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003476 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003477 return;
3478 } else if (vol_info)
3479 tcon->unix_ext = 1; /* Unix Extensions supported */
3480
3481 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003482 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003483 return;
3484 }
Steve French50c2f752007-07-13 00:33:32 +00003485
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003486 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003487 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003488 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003489 /* check for reconnect case in which we do not
3490 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003491 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003492 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003493 originally at mount time */
3494 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3495 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003496 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3497 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003498 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003499 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003500 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003501 cifs_dbg(VFS, "possible reconnect error\n");
3502 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003503 }
Steve French8af18972007-02-14 04:42:51 +00003504 }
Steve French50c2f752007-07-13 00:33:32 +00003505
Steve French6848b732011-05-26 18:38:54 +00003506 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003507 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003508
Steve French8af18972007-02-14 04:42:51 +00003509 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003510 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003511 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003512 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003513 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003514 if (cifs_sb)
3515 cifs_sb->mnt_cifs_flags |=
3516 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003517 }
3518
Steve French75865f8c2007-06-24 18:30:48 +00003519 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003520 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003521 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003522 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003523 if (cifs_sb)
3524 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003525 CIFS_MOUNT_POSIX_PATHS;
3526 }
Steve French50c2f752007-07-13 00:33:32 +00003527
Joe Perchesf96637b2013-05-04 22:12:25 -05003528 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003529#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003530 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003531 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003532 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003533 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003534 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003535 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003536 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003537 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003538 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003539 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003540 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003541 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003542 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003543 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003544 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003545 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003546 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003547 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003548#endif /* CIFS_DEBUG2 */
3549 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003550 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003551 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003552 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003553 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 +00003554
Steve French8af18972007-02-14 04:42:51 +00003555 }
3556 }
3557}
3558
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003559int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003560 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003561{
Jeff Layton2de970f2010-10-06 19:51:12 -04003562 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3563
Al Viro2ced6f62011-06-17 09:20:04 -04003564 spin_lock_init(&cifs_sb->tlink_tree_lock);
3565 cifs_sb->tlink_tree = RB_ROOT;
3566
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003567 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003568 * Temporarily set r/wsize for matching superblock. If we end up using
3569 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003570 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003571 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003572 cifs_sb->wsize = pvolume_info->wsize;
3573
Steve French3b795212008-11-13 19:45:32 +00003574 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3575 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3576 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3577 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003578 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3579 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003580
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303581 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003582 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303583
Steve French3b795212008-11-13 19:45:32 +00003584 if (pvolume_info->noperm)
3585 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3586 if (pvolume_info->setuids)
3587 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003588 if (pvolume_info->setuidfromacl)
3589 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003590 if (pvolume_info->server_ino)
3591 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3592 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003593 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3594 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003595 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3596 if (pvolume_info->no_xattr)
3597 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3598 if (pvolume_info->sfu_emul)
3599 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3600 if (pvolume_info->nobrl)
3601 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003602 if (pvolume_info->nohandlecache)
3603 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003604 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003605 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003606 if (pvolume_info->mand_lock)
3607 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003608 if (pvolume_info->rwpidforward)
3609 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003610 if (pvolume_info->cifs_acl)
3611 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003612 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003613 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003614 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3615 }
3616 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003617 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003618 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3619 }
Steve French3b795212008-11-13 19:45:32 +00003620 if (pvolume_info->override_uid)
3621 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3622 if (pvolume_info->override_gid)
3623 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3624 if (pvolume_info->dynperm)
3625 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303626 if (pvolume_info->fsc)
3627 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003628 if (pvolume_info->multiuser)
3629 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3630 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003631 if (pvolume_info->strict_io)
3632 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003633 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003634 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003635 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3636 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003637 if (pvolume_info->mfsymlinks) {
3638 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003639 /*
3640 * Our SFU ("Services for Unix" emulation does not allow
3641 * creating symlinks but does allow reading existing SFU
3642 * symlinks (it does allow both creating and reading SFU
3643 * style mknod and FIFOs though). When "mfsymlinks" and
3644 * "sfu" are both enabled at the same time, it allows
3645 * reading both types of symlinks, but will only create
3646 * them with mfsymlinks format. This allows better
3647 * Apple compatibility (probably better for Samba too)
3648 * while still recognizing old Windows style symlinks.
3649 */
3650 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003651 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003652 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003653 }
Steve French3b795212008-11-13 19:45:32 +00003654
3655 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003656 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003657
3658 if (pvolume_info->prepath) {
3659 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3660 if (cifs_sb->prepath == NULL)
3661 return -ENOMEM;
3662 }
3663
3664 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003665}
3666
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003667static void
3668cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003669{
Sean Finneyb9468452011-04-11 13:19:32 +00003670 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003671 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003672 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003673 kfree(volume_info->domainname);
3674 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003675 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003676}
3677
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003678void
3679cifs_cleanup_volume_info(struct smb_vol *volume_info)
3680{
3681 if (!volume_info)
3682 return;
3683 cleanup_volume_info_contents(volume_info);
3684 kfree(volume_info);
3685}
3686
3687
Steve French2d6d5892009-04-09 00:36:44 +00003688#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003689/*
3690 * cifs_build_path_to_root returns full path to root when we do not have an
3691 * exiting connection (tcon)
3692 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003693static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003694build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003695 const struct cifs_sb_info *cifs_sb)
3696{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003697 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003698 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003699 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003700
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003701 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003702 if (full_path == NULL)
3703 return ERR_PTR(-ENOMEM);
3704
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003705 strncpy(full_path, vol->UNC, unc_len);
3706 pos = full_path + unc_len;
3707
3708 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003709 *pos = CIFS_DIR_SEP(cifs_sb);
3710 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003711 pos += pplen;
3712 }
3713
3714 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003715 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003716 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003717 return full_path;
3718}
Sean Finneydd613942011-04-11 13:19:30 +00003719
3720/*
3721 * Perform a dfs referral query for a share and (optionally) prefix
3722 *
Sean Finney046462a2011-04-11 13:19:33 +00003723 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3724 * to a string containing updated options for the submount. Otherwise it
3725 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003726 *
3727 * Returns the rc from get_dfs_path to the caller, which can be used to
3728 * determine whether there were referrals.
3729 */
3730static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003731expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003732 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003733 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003734{
3735 int rc;
3736 unsigned int num_referrals = 0;
3737 struct dfs_info3_param *referrals = NULL;
3738 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3739
3740 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3741 if (IS_ERR(full_path))
3742 return PTR_ERR(full_path);
3743
3744 /* For DFS paths, skip the first '\' of the UNC */
3745 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3746
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003747 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003748 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003749
3750 if (!rc && num_referrals > 0) {
3751 char *fake_devname = NULL;
3752
3753 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3754 full_path + 1, referrals,
3755 &fake_devname);
3756
3757 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003758
Sean Finneydd613942011-04-11 13:19:30 +00003759 if (IS_ERR(mdata)) {
3760 rc = PTR_ERR(mdata);
3761 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003762 } else {
3763 cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003764 rc = cifs_setup_volume_info(volume_info, mdata,
3765 fake_devname);
Sean Finneydd613942011-04-11 13:19:30 +00003766 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003767 kfree(fake_devname);
3768 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003769 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003770 }
3771 kfree(full_path);
3772 return rc;
3773}
Steve French2d6d5892009-04-09 00:36:44 +00003774#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003775
Jeff Layton04db79b2011-07-06 08:10:38 -04003776static int
3777cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3778 const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003780 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003781
Jeff Layton04db79b2011-07-06 08:10:38 -04003782 if (cifs_parse_mount_options(mount_data, devname, volume_info))
3783 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Jeff Layton7586b762008-12-01 18:41:49 -05003785 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003786 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05003787 kfree(volume_info->username);
3788 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05003789 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05003791 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003793 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003794 /* In userspace mount helper we can get user name from alternate
3795 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003796 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
3798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05003800 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003801 /* load_nls_default cannot return null */
3802 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003804 volume_info->local_nls = load_nls(volume_info->iocharset);
3805 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003806 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003807 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04003808 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 }
3810 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003811
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003812 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04003813}
3814
3815struct smb_vol *
3816cifs_get_volume_info(char *mount_data, const char *devname)
3817{
3818 int rc;
3819 struct smb_vol *volume_info;
3820
Jeff Layton6ee95422012-11-26 11:09:57 -05003821 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04003822 if (!volume_info)
3823 return ERR_PTR(-ENOMEM);
3824
3825 rc = cifs_setup_volume_info(volume_info, mount_data, devname);
3826 if (rc) {
3827 cifs_cleanup_volume_info(volume_info);
3828 volume_info = ERR_PTR(rc);
3829 }
3830
3831 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003832}
3833
Aurelien Aptela6b50582016-05-25 19:59:09 +02003834static int
3835cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3836 unsigned int xid,
3837 struct cifs_tcon *tcon,
3838 struct cifs_sb_info *cifs_sb,
3839 char *full_path)
3840{
3841 int rc;
3842 char *s;
3843 char sep, tmp;
3844
3845 sep = CIFS_DIR_SEP(cifs_sb);
3846 s = full_path;
3847
3848 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3849 while (rc == 0) {
3850 /* skip separators */
3851 while (*s == sep)
3852 s++;
3853 if (!*s)
3854 break;
3855 /* next separator */
3856 while (*s && *s != sep)
3857 s++;
3858
3859 /*
3860 * temporarily null-terminate the path at the end of
3861 * the current component
3862 */
3863 tmp = *s;
3864 *s = 0;
3865 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3866 full_path);
3867 *s = tmp;
3868 }
3869 return rc;
3870}
3871
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003872int
Al Viro2c6292a2011-06-17 09:05:48 -04003873cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003874{
Jeff Layton1daaae82012-03-21 06:30:40 -04003875 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003876 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003877 struct cifs_ses *ses;
Steve French96daf2b2011-05-27 04:34:02 +00003878 struct cifs_tcon *tcon;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003879 struct TCP_Server_Info *server;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003880 char *full_path;
3881 struct tcon_link *tlink;
3882#ifdef CONFIG_CIFS_DFS_UPCALL
3883 int referral_walks_count = 0;
Jeff Layton20547492011-07-09 12:21:07 -04003884#endif
Al Virodd854462011-06-17 08:24:42 -04003885
Jeff Layton20547492011-07-09 12:21:07 -04003886#ifdef CONFIG_CIFS_DFS_UPCALL
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003887try_mount_again:
3888 /* cleanup activities if we're chasing a referral */
3889 if (referral_walks_count) {
3890 if (tcon)
3891 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003892 else if (ses)
3893 cifs_put_smb_ses(ses);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003894
Sachin Prabhu1dfd18d2015-06-16 16:36:17 +01003895 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3896
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003897 free_xid(xid);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003898 }
3899#endif
Jeff Layton1daaae82012-03-21 06:30:40 -04003900 rc = 0;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003901 tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003902 ses = NULL;
3903 server = NULL;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003904 full_path = NULL;
3905 tlink = NULL;
3906
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003907 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Jeff Layton63c038c2008-12-01 18:41:46 -05003909 /* get a reference to a tcp session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003910 server = cifs_get_tcp_session(volume_info);
3911 if (IS_ERR(server)) {
3912 rc = PTR_ERR(server);
Jeff Layton63c038c2008-12-01 18:41:46 -05003913 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 }
Steve French141891f2016-09-23 00:44:16 -05003915 if ((volume_info->max_credits < 20) ||
3916 (volume_info->max_credits > 60000))
3917 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3918 else
3919 server->max_credits = volume_info->max_credits;
Jeff Layton36988c72010-04-24 07:57:43 -04003920 /* get a reference to a SMB session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003921 ses = cifs_get_smb_ses(server, volume_info);
3922 if (IS_ERR(ses)) {
3923 rc = PTR_ERR(ses);
3924 ses = NULL;
Jeff Layton36988c72010-04-24 07:57:43 -04003925 goto mount_fail_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
Steve French50c2f752007-07-13 00:33:32 +00003927
Steve Frenchb618f002015-11-03 09:15:03 -06003928 if ((volume_info->persistent == true) && ((ses->server->capabilities &
3929 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
3930 cifs_dbg(VFS, "persistent handles not supported by server\n");
3931 rc = -EOPNOTSUPP;
3932 goto mount_fail_check;
3933 }
Steve French592fafe2015-11-03 10:08:53 -06003934
Jeff Laytond00c28d2010-04-24 07:57:44 -04003935 /* search for existing tcon to this server share */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003936 tcon = cifs_get_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003937 if (IS_ERR(tcon)) {
3938 rc = PTR_ERR(tcon);
3939 tcon = NULL;
Mark Syms40920c22016-11-29 11:36:46 +00003940 if (rc == -EACCES)
3941 goto mount_fail_check;
3942
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003943 goto remote_path_check;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003944 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003945
Steve French6848b732011-05-26 18:38:54 +00003946 /* tell server which Unix caps we support */
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003947 if (cap_unix(tcon->ses)) {
Steve French6848b732011-05-26 18:38:54 +00003948 /* reset of caps checks mount to see if unix extensions
3949 disabled for just this mount */
Al Viro2c6292a2011-06-17 09:05:48 -04003950 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
Steve French6848b732011-05-26 18:38:54 +00003951 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3952 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3953 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3954 rc = -EACCES;
3955 goto mount_fail_check;
3956 }
3957 } else
3958 tcon->unix_ext = 0; /* server does not support them */
3959
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003960 /* do not care if a following call succeed - informational */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003961 if (!tcon->pipe && server->ops->qfs_tcon)
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003962 server->ops->qfs_tcon(xid, tcon);
Steve Frenchd82c2df2008-11-15 00:07:26 +00003963
Pavel Shilovsky24985c52012-09-18 16:20:28 -07003964 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
3965 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003966
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003967remote_path_check:
Sean Finneyc1508ca2011-04-11 13:19:31 +00003968#ifdef CONFIG_CIFS_DFS_UPCALL
3969 /*
3970 * Perform an unconditional check for whether there are DFS
3971 * referrals for this path without prefix, to provide support
3972 * for DFS referrals from w2k8 servers which don't seem to respond
3973 * with PATH_NOT_COVERED to requests that include the prefix.
3974 * Chase the referral if found, otherwise continue normally.
3975 */
3976 if (referral_walks_count == 0) {
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003977 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
3978 false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00003979 if (!refrc) {
3980 referral_walks_count++;
3981 goto try_mount_again;
3982 }
3983 }
3984#endif
3985
Steve Frenchf87d39d2011-05-27 03:50:55 +00003986 /* check if a whole path is not remote */
Jeff Layton70945642011-03-14 13:48:08 -04003987 if (!rc && tcon) {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003988 if (!server->ops->is_path_accessible) {
3989 rc = -ENOSYS;
3990 goto mount_fail_check;
3991 }
Steve French6d3ea7e2012-11-28 22:34:41 -06003992 /*
3993 * cifs_build_path_to_root works only when we have a valid tcon
3994 */
Sachin Prabhu374402a2016-12-15 12:31:19 +05303995 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
3996 tcon->Flags & SMB_SHARE_IS_IN_DFS);
Igor Mammedove4cce942009-02-10 14:10:26 +03003997 if (full_path == NULL) {
3998 rc = -ENOMEM;
3999 goto mount_fail_check;
4000 }
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004001 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4002 full_path);
Jeff Layton03ceace2010-12-06 21:07:33 -05004003 if (rc != 0 && rc != -EREMOTE) {
Igor Mammedove4cce942009-02-10 14:10:26 +03004004 kfree(full_path);
4005 goto mount_fail_check;
4006 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004007
Sachin Prabhud1713562016-09-06 13:22:34 +01004008 if (rc != -EREMOTE) {
4009 rc = cifs_are_all_path_components_accessible(server,
Aurelien Aptela6b50582016-05-25 19:59:09 +02004010 xid, tcon, cifs_sb,
4011 full_path);
Sachin Prabhud1713562016-09-06 13:22:34 +01004012 if (rc != 0) {
4013 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4014 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4015 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4016 rc = 0;
4017 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004018 }
Igor Mammedove4cce942009-02-10 14:10:26 +03004019 kfree(full_path);
4020 }
4021
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004022 /* get referral if needed */
4023 if (rc == -EREMOTE) {
Steve Frenchd036f502009-04-03 03:12:08 +00004024#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004025 if (referral_walks_count > MAX_NESTED_LINKS) {
4026 /*
4027 * BB: when we implement proper loop detection,
4028 * we will remove this check. But now we need it
4029 * to prevent an indefinite loop if 'DFS tree' is
4030 * misconfigured (i.e. has loops).
4031 */
4032 rc = -ELOOP;
4033 goto mount_fail_check;
4034 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004035
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004036 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
Jeff Layton7b91e262009-07-23 15:22:30 -04004037
Sean Finneydd613942011-04-11 13:19:30 +00004038 if (!rc) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004039 referral_walks_count++;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004040 goto try_mount_again;
4041 }
Sean Finneydd613942011-04-11 13:19:30 +00004042 goto mount_fail_check;
Steve Frenchd036f502009-04-03 03:12:08 +00004043#else /* No DFS support, return error on mount */
4044 rc = -EOPNOTSUPP;
4045#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004046 }
4047
Jeff Layton9d002df2010-10-06 19:51:11 -04004048 if (rc)
4049 goto mount_fail_check;
4050
4051 /* now, hang the tcon off of the superblock */
4052 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
4053 if (tlink == NULL) {
4054 rc = -ENOMEM;
4055 goto mount_fail_check;
4056 }
4057
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004058 tlink->tl_uid = ses->linux_uid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004059 tlink->tl_tcon = tcon;
4060 tlink->tl_time = jiffies;
4061 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4062 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4063
Jeff Layton413e6612010-10-28 13:33:38 -04004064 cifs_sb->master_tlink = tlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004065 spin_lock(&cifs_sb->tlink_tree_lock);
4066 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4067 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton413e6612010-10-28 13:33:38 -04004068
Jeff Laytonda472fc2012-03-23 14:40:53 -04004069 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004070 TLINK_IDLE_EXPIRE);
4071
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004072mount_fail_check:
4073 /* on error free sesinfo and tcon struct if needed */
4074 if (rc) {
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004075 /* If find_unc succeeded then rc == 0 so we can not end */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004076 /* up accidentally freeing someone elses tcon struct */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004077 if (tcon)
4078 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004079 else if (ses)
4080 cifs_put_smb_ses(ses);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004081 else
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004082 cifs_put_tcp_session(server, 0);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004083 }
4084
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004085out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004086 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 return rc;
4088}
4089
Jeff Layton8d1bca32011-06-11 21:17:10 -04004090/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004091 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004094CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004095 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 const struct nls_table *nls_codepage)
4097{
4098 struct smb_hdr *smb_buffer;
4099 struct smb_hdr *smb_buffer_response;
4100 TCONX_REQ *pSMB;
4101 TCONX_RSP *pSMBr;
4102 unsigned char *bcc_ptr;
4103 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004104 int length;
4105 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
4107 if (ses == NULL)
4108 return -EIO;
4109
4110 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004111 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 smb_buffer_response = smb_buffer;
4115
4116 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4117 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004118
Pavel Shilovsky88257362012-05-23 14:01:59 +04004119 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 smb_buffer->Uid = ses->Suid;
4121 pSMB = (TCONX_REQ *) smb_buffer;
4122 pSMBr = (TCONX_RSP *) smb_buffer_response;
4123
4124 pSMB->AndXCommand = 0xFF;
4125 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004127 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004128 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004129 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004130 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004131 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004132 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004133 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004134 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4135 specified as required (when that support is added to
4136 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004137 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004138 by Samba (not sure whether other servers allow
4139 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004140#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004141 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004142 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004143 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004144 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004145 SECMODE_PW_ENCRYPT ? true : false,
4146 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004147 else
4148#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004149 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004150 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004151 if (rc) {
4152 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4153 __func__, rc);
4154 cifs_buf_release(smb_buffer);
4155 return rc;
4156 }
Steve Frencheeac8042006-01-13 21:34:58 -08004157
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004158 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004159 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004160 /* must align unicode strings */
4161 *bcc_ptr = 0; /* null byte password */
4162 bcc_ptr++;
4163 }
Steve Frencheeac8042006-01-13 21:34:58 -08004164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Jeff Layton38d77c52013-05-26 07:01:00 -04004166 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4168
4169 if (ses->capabilities & CAP_STATUS32) {
4170 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4171 }
4172 if (ses->capabilities & CAP_DFS) {
4173 smb_buffer->Flags2 |= SMBFLG2_DFS;
4174 }
4175 if (ses->capabilities & CAP_UNICODE) {
4176 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4177 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004178 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004179 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004180 (/* server len*/ + 256 /* share len */), nls_codepage);
4181 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 bcc_ptr += 2; /* skip trailing null */
4183 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 strcpy(bcc_ptr, tree);
4185 bcc_ptr += strlen(tree) + 1;
4186 }
4187 strcpy(bcc_ptr, "?????");
4188 bcc_ptr += strlen("?????");
4189 bcc_ptr += 1;
4190 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004191 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4192 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 pSMB->ByteCount = cpu_to_le16(count);
4194
Steve French133672e2007-11-13 22:41:37 +00004195 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004196 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004199 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004200 bool is_unicode;
4201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004203 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 tcon->tid = smb_buffer_response->Tid;
4205 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004206 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004207 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004208 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4209 is_unicode = true;
4210 else
4211 is_unicode = false;
4212
Jeff Laytoncc20c032009-04-30 07:16:21 -04004213
Steve French50c2f752007-07-13 00:33:32 +00004214 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004215 if (length == 3) {
4216 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4217 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004218 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004219 tcon->ipc = true;
4220 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004221 }
4222 } else if (length == 2) {
4223 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4224 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004225 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004226 }
4227 }
Steve French50c2f752007-07-13 00:33:32 +00004228 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004229 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004230 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004231
4232 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004233 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004234 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004235 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004236 nls_codepage);
4237
Joe Perchesf96637b2013-05-04 22:12:25 -05004238 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004239
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004240 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004241 (smb_buffer_response->WordCount == 7))
4242 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004243 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4244 else
4245 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004246 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 }
4248
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004249 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return rc;
4251}
4252
Al Viro2e32cf52013-10-03 12:53:37 -04004253static void delayed_free(struct rcu_head *p)
4254{
4255 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4256 unload_nls(sbi->local_nls);
4257 kfree(sbi);
4258}
4259
Al Viro2a9b9952011-06-17 09:27:16 -04004260void
4261cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
Jeff Laytonb647c352010-10-28 11:16:44 -04004263 struct rb_root *root = &cifs_sb->tlink_tree;
4264 struct rb_node *node;
4265 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Jeff Layton2de970f2010-10-06 19:51:12 -04004267 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4268
Jeff Laytonb647c352010-10-28 11:16:44 -04004269 spin_lock(&cifs_sb->tlink_tree_lock);
4270 while ((node = rb_first(root))) {
4271 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4272 cifs_get_tlink(tlink);
4273 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4274 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004275
Jeff Laytonb647c352010-10-28 11:16:44 -04004276 spin_unlock(&cifs_sb->tlink_tree_lock);
4277 cifs_put_tlink(tlink);
4278 spin_lock(&cifs_sb->tlink_tree_lock);
4279 }
4280 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004281
Al Virod757d712011-06-17 09:42:43 -04004282 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004283 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004284 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004285}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004287int
4288cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
4290 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004291 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004293 if (!server->ops->need_neg || !server->ops->negotiate)
4294 return -ENOSYS;
4295
Jeff Layton198b5682010-04-24 07:57:48 -04004296 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004297 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Pavel Shilovsky45275782012-05-17 17:53:29 +04004300 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004301
4302 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004303 if (rc == 0) {
4304 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004305 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004306 server->tcpStatus = CifsGood;
4307 else
4308 rc = -EHOSTDOWN;
4309 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 }
Steve French26b994f2008-08-06 05:11:33 +00004311
Jeff Layton198b5682010-04-24 07:57:48 -04004312 return rc;
4313}
Steve French26b994f2008-08-06 05:11:33 +00004314
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004315int
4316cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4317 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004318{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004319 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004320 struct TCP_Server_Info *server = ses->server;
4321
Jeff Layton198b5682010-04-24 07:57:48 -04004322 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004323 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004324 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004325
Joe Perchesf96637b2013-05-04 22:12:25 -05004326 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004327 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004328
Shu Wangf5c4ba82017-09-08 18:48:33 +08004329 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05004330 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08004331 ses->auth_key.response);
4332 kfree(ses->auth_key.response);
4333 ses->auth_key.response = NULL;
4334 ses->auth_key.len = 0;
4335 }
4336
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004337 if (server->ops->sess_setup)
4338 rc = server->ops->sess_setup(xid, ses, nls_info);
4339
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004340 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004341 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return rc;
4344}
4345
Jeff Layton8a8798a2012-01-17 16:09:15 -05004346static int
4347cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4348{
Jeff Layton3f618222013-06-12 19:52:14 -05004349 vol->sectype = ses->sectype;
4350
4351 /* krb5 is special, since we don't need username or pw */
4352 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004353 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004354
4355 return cifs_set_cifscreds(vol, ses);
4356}
4357
Steve French96daf2b2011-05-27 04:34:02 +00004358static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004359cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004360{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004361 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004362 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4363 struct cifs_ses *ses;
4364 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004365 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004366
4367 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004368 if (vol_info == NULL)
4369 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004370
Jeff Layton9d002df2010-10-06 19:51:11 -04004371 vol_info->local_nls = cifs_sb->local_nls;
4372 vol_info->linux_uid = fsuid;
4373 vol_info->cred_uid = fsuid;
4374 vol_info->UNC = master_tcon->treeName;
4375 vol_info->retry = master_tcon->retry;
4376 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05004377 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04004378 vol_info->local_lease = master_tcon->local_lease;
4379 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004380 vol_info->sectype = master_tcon->ses->sectype;
4381 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004382
Jeff Layton8a8798a2012-01-17 16:09:15 -05004383 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4384 if (rc) {
4385 tcon = ERR_PTR(rc);
4386 goto out;
4387 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004388
4389 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304390 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004391 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304392 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004393
4394 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4395 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004396 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004397 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004398 goto out;
4399 }
4400
4401 tcon = cifs_get_tcon(ses, vol_info);
4402 if (IS_ERR(tcon)) {
4403 cifs_put_smb_ses(ses);
4404 goto out;
4405 }
4406
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004407 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004408 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4409out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004410 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01004411 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004412 kfree(vol_info);
4413
4414 return tcon;
4415}
4416
Steve French96daf2b2011-05-27 04:34:02 +00004417struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004418cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4419{
4420 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4421}
4422
Jeff Laytonb647c352010-10-28 11:16:44 -04004423/* find and return a tlink with given uid */
4424static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004425tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004426{
4427 struct rb_node *node = root->rb_node;
4428 struct tcon_link *tlink;
4429
4430 while (node) {
4431 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4432
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004433 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004434 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004435 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004436 node = node->rb_right;
4437 else
4438 return tlink;
4439 }
4440 return NULL;
4441}
4442
4443/* insert a tcon_link into the tree */
4444static void
4445tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4446{
4447 struct rb_node **new = &(root->rb_node), *parent = NULL;
4448 struct tcon_link *tlink;
4449
4450 while (*new) {
4451 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4452 parent = *new;
4453
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004454 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004455 new = &((*new)->rb_left);
4456 else
4457 new = &((*new)->rb_right);
4458 }
4459
4460 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4461 rb_insert_color(&new_tlink->tl_rbnode, root);
4462}
4463
Jeff Layton9d002df2010-10-06 19:51:11 -04004464/*
4465 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4466 * current task.
4467 *
4468 * If the superblock doesn't refer to a multiuser mount, then just return
4469 * the master tcon for the mount.
4470 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304471 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004472 * exists, then check to see if it's pending construction. If it is then wait
4473 * for construction to complete. Once it's no longer pending, check to see if
4474 * it failed and either return an error or retry construction, depending on
4475 * the timeout.
4476 *
4477 * If one doesn't exist then insert a new tcon_link struct into the tree and
4478 * try to construct a new one.
4479 */
4480struct tcon_link *
4481cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4482{
4483 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004484 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004485 struct tcon_link *tlink, *newtlink;
4486
4487 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4488 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4489
4490 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004491 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004492 if (tlink)
4493 cifs_get_tlink(tlink);
4494 spin_unlock(&cifs_sb->tlink_tree_lock);
4495
4496 if (tlink == NULL) {
4497 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4498 if (newtlink == NULL)
4499 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004500 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004501 newtlink->tl_tcon = ERR_PTR(-EACCES);
4502 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4503 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4504 cifs_get_tlink(newtlink);
4505
Jeff Layton9d002df2010-10-06 19:51:11 -04004506 spin_lock(&cifs_sb->tlink_tree_lock);
4507 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004508 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004509 if (tlink) {
4510 cifs_get_tlink(tlink);
4511 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004512 kfree(newtlink);
4513 goto wait_for_construction;
4514 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004515 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004516 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4517 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004518 } else {
4519wait_for_construction:
4520 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004521 TASK_INTERRUPTIBLE);
4522 if (ret) {
4523 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004524 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004525 }
4526
4527 /* if it's good, return it */
4528 if (!IS_ERR(tlink->tl_tcon))
4529 return tlink;
4530
4531 /* return error if we tried this already recently */
4532 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4533 cifs_put_tlink(tlink);
4534 return ERR_PTR(-EACCES);
4535 }
4536
4537 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4538 goto wait_for_construction;
4539 }
4540
4541 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4542 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4543 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4544
4545 if (IS_ERR(tlink->tl_tcon)) {
4546 cifs_put_tlink(tlink);
4547 return ERR_PTR(-EACCES);
4548 }
4549
4550 return tlink;
4551}
Jeff Layton2de970f2010-10-06 19:51:12 -04004552
4553/*
4554 * periodic workqueue job that scans tcon_tree for a superblock and closes
4555 * out tcons.
4556 */
4557static void
4558cifs_prune_tlinks(struct work_struct *work)
4559{
4560 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4561 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004562 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004563 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004564 struct rb_node *tmp;
4565 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004566
Jeff Laytonb647c352010-10-28 11:16:44 -04004567 /*
4568 * Because we drop the spinlock in the loop in order to put the tlink
4569 * it's not guarded against removal of links from the tree. The only
4570 * places that remove entries from the tree are this function and
4571 * umounts. Because this function is non-reentrant and is canceled
4572 * before umount can proceed, this is safe.
4573 */
4574 spin_lock(&cifs_sb->tlink_tree_lock);
4575 node = rb_first(root);
4576 while (node != NULL) {
4577 tmp = node;
4578 node = rb_next(tmp);
4579 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4580
4581 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4582 atomic_read(&tlink->tl_count) != 0 ||
4583 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4584 continue;
4585
4586 cifs_get_tlink(tlink);
4587 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4588 rb_erase(tmp, root);
4589
Jeff Layton2de970f2010-10-06 19:51:12 -04004590 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004591 cifs_put_tlink(tlink);
4592 spin_lock(&cifs_sb->tlink_tree_lock);
4593 }
4594 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004595
Jeff Laytonda472fc2012-03-23 14:40:53 -04004596 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004597 TLINK_IDLE_EXPIRE);
4598}