blob: d8bfa89161e24bae64b96f97baf7e6077b2c6daa [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;
64
Jeff Layton2de970f2010-10-06 19:51:12 -040065/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040066#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040067#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040068
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040069enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040070 /* Mount options that take no arguments */
71 Opt_user_xattr, Opt_nouser_xattr,
72 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040073 Opt_forcegid, Opt_noforcegid,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040074 Opt_noblocksend, Opt_noautotune,
75 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050076 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040077 Opt_mapchars, Opt_nomapchars, Opt_sfu,
78 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
79 Opt_noposixpaths, Opt_nounix,
80 Opt_nocase,
81 Opt_brl, Opt_nobrl,
Steve French95932652016-09-23 01:36:34 -050082 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040083 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
84 Opt_nohard, Opt_nosoft,
85 Opt_nointr, Opt_intr,
86 Opt_nostrictsync, Opt_strictsync,
87 Opt_serverino, Opt_noserverino,
88 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
89 Opt_acl, Opt_noacl, Opt_locallease,
Jeff Layton1b359202012-09-19 15:20:27 -070090 Opt_sign, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040091 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040092 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050093 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060094 Opt_resilient, Opt_noresilient,
Long Li8339dd32017-11-07 01:54:55 -070095 Opt_domainauto, Opt_rdma,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040096
97 /* Mount options which take numeric value */
98 Opt_backupuid, Opt_backupgid, Opt_uid,
99 Opt_cruid, Opt_gid, Opt_file_mode,
100 Opt_dirmode, Opt_port,
101 Opt_rsize, Opt_wsize, Opt_actimeo,
Steve French141891f2016-09-23 00:44:16 -0500102 Opt_echo_interval, Opt_max_credits,
Steve French8b217fe2016-11-11 22:36:20 -0600103 Opt_snapshot,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400104
105 /* Mount options which take string value */
106 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400107 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400108 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400109 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400110
111 /* Mount options to be ignored */
112 Opt_ignore,
113
114 /* Options which could be blank */
115 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100116 Opt_blank_user,
117 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400118
119 Opt_err
120};
121
122static const match_table_t cifs_mount_option_tokens = {
123
124 { Opt_user_xattr, "user_xattr" },
125 { Opt_nouser_xattr, "nouser_xattr" },
126 { Opt_forceuid, "forceuid" },
127 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400128 { Opt_forcegid, "forcegid" },
129 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400130 { Opt_noblocksend, "noblocksend" },
131 { Opt_noautotune, "noautotune" },
132 { Opt_hard, "hard" },
133 { Opt_soft, "soft" },
134 { Opt_perm, "perm" },
135 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500136 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400137 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500138 { Opt_mapposix, "mapposix" }, /* SFM style */
139 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400140 { Opt_sfu, "sfu" },
141 { Opt_nosfu, "nosfu" },
142 { Opt_nodfs, "nodfs" },
143 { Opt_posixpaths, "posixpaths" },
144 { Opt_noposixpaths, "noposixpaths" },
145 { Opt_nounix, "nounix" },
146 { Opt_nounix, "nolinux" },
147 { Opt_nocase, "nocase" },
148 { Opt_nocase, "ignorecase" },
149 { Opt_brl, "brl" },
150 { Opt_nobrl, "nobrl" },
151 { Opt_nobrl, "nolock" },
152 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400153 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400154 { Opt_setuids, "setuids" },
155 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500156 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400157 { Opt_dynperm, "dynperm" },
158 { Opt_nodynperm, "nodynperm" },
159 { Opt_nohard, "nohard" },
160 { Opt_nosoft, "nosoft" },
161 { Opt_nointr, "nointr" },
162 { Opt_intr, "intr" },
163 { Opt_nostrictsync, "nostrictsync" },
164 { Opt_strictsync, "strictsync" },
165 { Opt_serverino, "serverino" },
166 { Opt_noserverino, "noserverino" },
167 { Opt_rwpidforward, "rwpidforward" },
168 { Opt_cifsacl, "cifsacl" },
169 { Opt_nocifsacl, "nocifsacl" },
170 { Opt_acl, "acl" },
171 { Opt_noacl, "noacl" },
172 { Opt_locallease, "locallease" },
173 { Opt_sign, "sign" },
174 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400175 { Opt_noac, "noac" },
176 { Opt_fsc, "fsc" },
177 { Opt_mfsymlinks, "mfsymlinks" },
178 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400179 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400180 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500181 { Opt_persistent, "persistenthandles"},
182 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600183 { Opt_resilient, "resilienthandles"},
184 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530185 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700186 { Opt_rdma, "rdma"},
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400187
188 { Opt_backupuid, "backupuid=%s" },
189 { Opt_backupgid, "backupgid=%s" },
190 { Opt_uid, "uid=%s" },
191 { Opt_cruid, "cruid=%s" },
192 { Opt_gid, "gid=%s" },
193 { Opt_file_mode, "file_mode=%s" },
194 { Opt_dirmode, "dirmode=%s" },
195 { Opt_dirmode, "dir_mode=%s" },
196 { Opt_port, "port=%s" },
197 { Opt_rsize, "rsize=%s" },
198 { Opt_wsize, "wsize=%s" },
199 { Opt_actimeo, "actimeo=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600200 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500201 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600202 { Opt_snapshot, "snapshot=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400203
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100204 { Opt_blank_user, "user=" },
205 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400206 { Opt_user, "user=%s" },
207 { Opt_user, "username=%s" },
208 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100209 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400210 { Opt_pass, "pass=%s" },
211 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100212 { Opt_blank_ip, "ip=" },
213 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400214 { Opt_ip, "ip=%s" },
215 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400216 { Opt_ignore, "unc=%s" },
217 { Opt_ignore, "target=%s" },
218 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400219 { Opt_domain, "dom=%s" },
220 { Opt_domain, "domain=%s" },
221 { Opt_domain, "workgroup=%s" },
222 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400223 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400224 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400225 { Opt_netbiosname, "netbiosname=%s" },
226 { Opt_servern, "servern=%s" },
227 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400228 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400229 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400230 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400231
232 { Opt_ignore, "cred" },
233 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400234 { Opt_ignore, "cred=%s" },
235 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400236 { Opt_ignore, "guest" },
237 { Opt_ignore, "rw" },
238 { Opt_ignore, "ro" },
239 { Opt_ignore, "suid" },
240 { Opt_ignore, "nosuid" },
241 { Opt_ignore, "exec" },
242 { Opt_ignore, "noexec" },
243 { Opt_ignore, "nodev" },
244 { Opt_ignore, "noauto" },
245 { Opt_ignore, "dev" },
246 { Opt_ignore, "mand" },
247 { Opt_ignore, "nomand" },
248 { Opt_ignore, "_netdev" },
249
250 { Opt_err, NULL }
251};
252
253enum {
254 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
255 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400256 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
257 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400258 Opt_sec_none,
259
260 Opt_sec_err
261};
262
263static const match_table_t cifs_secflavor_tokens = {
264 { Opt_sec_krb5, "krb5" },
265 { Opt_sec_krb5i, "krb5i" },
266 { Opt_sec_krb5p, "krb5p" },
267 { Opt_sec_ntlmsspi, "ntlmsspi" },
268 { Opt_sec_ntlmssp, "ntlmssp" },
269 { Opt_ntlm, "ntlm" },
270 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400271 { Opt_sec_ntlmv2, "nontlm" },
272 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400273 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400274 { Opt_sec_lanman, "lanman" },
275 { Opt_sec_none, "none" },
276
277 { Opt_sec_err, NULL }
278};
279
Jeff Layton15b6a472012-05-16 07:50:15 -0400280/* cache flavors */
281enum {
282 Opt_cache_loose,
283 Opt_cache_strict,
284 Opt_cache_none,
285 Opt_cache_err
286};
287
288static const match_table_t cifs_cacheflavor_tokens = {
289 { Opt_cache_loose, "loose" },
290 { Opt_cache_strict, "strict" },
291 { Opt_cache_none, "none" },
292 { Opt_cache_err, NULL }
293};
294
Jeff Layton23db65f2012-05-15 12:20:51 -0400295static const match_table_t cifs_smb_version_tokens = {
296 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600297 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000298 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500299 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500300 { Smb_302, SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600301#ifdef CONFIG_CIFS_SMB311
302 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500303 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600304#endif /* SMB311 */
Steve French9764c022017-09-17 10:41:35 -0500305 { Smb_3any, SMB3ANY_VERSION_STRING },
306 { Smb_default, SMBDEFAULT_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600307 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400308};
309
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300310static int ip_connect(struct TCP_Server_Info *server);
311static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400312static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400313static void cifs_prune_tlinks(struct work_struct *work);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -0400314static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
315 const char *devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Jeff Laytond5c56052008-12-01 18:42:33 -0500317/*
318 * cifs tcp session reconnection
319 *
320 * mark tcp session as reconnecting so temporarily locked
321 * mark all smb sessions as reconnecting for tcp session
322 * reconnect tcp session
323 * wake up waiters on reconnection? - (not needed currently)
324 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400325int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326cifs_reconnect(struct TCP_Server_Info *server)
327{
328 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500329 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000330 struct cifs_ses *ses;
331 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000332 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400333 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000336 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000337 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 next time through the loop */
339 spin_unlock(&GlobalMid_Lock);
340 return rc;
341 } else
342 server->tcpStatus = CifsNeedReconnect;
343 spin_unlock(&GlobalMid_Lock);
344 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400345 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Joe Perchesf96637b2013-05-04 22:12:25 -0500347 cifs_dbg(FYI, "Reconnecting tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* before reconnecting the tcp session, mark the smb session (uid)
350 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500351 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
352 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530353 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500354 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000355 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500356 ses->need_reconnect = true;
357 ses->ipc_tid = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500358 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000359 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500360 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530363 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500366 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500367 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000368 if (server->ssocket) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500369 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
370 server->ssocket->state, server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800371 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesf96637b2013-05-04 22:12:25 -0500372 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
373 server->ssocket->state, server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 sock_release(server->ssocket);
375 server->ssocket = NULL;
376 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500377 server->sequence_number = 0;
378 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500379 kfree(server->session_key.response);
380 server->session_key.response = NULL;
381 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000382 server->lstrp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500384 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400385 INIT_LIST_HEAD(&retry_list);
Joe Perchesf96637b2013-05-04 22:12:25 -0500386 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500388 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
389 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400390 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
391 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400392 list_move(&mid_entry->qhead, &retry_list);
393 }
394 spin_unlock(&GlobalMid_Lock);
Rabin Vincent820962d2015-12-23 07:32:41 +0100395 mutex_unlock(&server->srv_mutex);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400396
Joe Perchesf96637b2013-05-04 22:12:25 -0500397 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400398 list_for_each_safe(tmp, tmp2, &retry_list) {
399 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500400 list_del_init(&mid_entry->qhead);
401 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400404 do {
Steve French6c3d8902006-07-31 22:46:20 +0000405 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300406
407 /* we should try only the port we connected to before */
Jeff Layton73e216a2013-09-05 08:38:10 -0400408 mutex_lock(&server->srv_mutex);
Long Li781a8052017-11-22 17:38:36 -0700409 if (cifs_rdma_enabled(server))
410 rc = smbd_reconnect(server);
411 else
412 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000413 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500414 cifs_dbg(FYI, "reconnect error %d\n", rc);
Federico Sauter4afe2602015-03-17 17:45:28 +0100415 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700416 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 } else {
418 atomic_inc(&tcpSesReconnectCount);
419 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000420 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000421 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000422 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100423 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400425 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500426
Sachin Prabhub8c60012016-10-20 19:52:24 -0400427 if (server->tcpStatus == CifsNeedNegotiate)
428 mod_delayed_work(cifsiod_wq, &server->echo, 0);
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return rc;
431}
432
Jeff Laytonc74093b2011-01-11 07:24:23 -0500433static void
434cifs_echo_request(struct work_struct *work)
435{
436 int rc;
437 struct TCP_Server_Info *server = container_of(work,
438 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400439 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500440
Jeff Layton247ec9b2011-02-04 17:09:50 -0500441 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400442 * If we need to renegotiate, set echo interval to zero to
443 * immediately call echo service where we can renegotiate.
444 */
445 if (server->tcpStatus == CifsNeedNegotiate)
446 echo_interval = 0;
447 else
448 echo_interval = server->echo_interval;
449
450 /*
451 * We cannot send an echo if it is disabled.
452 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500453 */
Steve French4fcd1812016-06-22 20:12:05 -0500454
455 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400456 server->tcpStatus == CifsExiting ||
457 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400458 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600459 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500460 goto requeue_echo;
461
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400462 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500463 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500464 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
465 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500466
467requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400468 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500469}
470
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400471static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400472allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400473{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400474 if (!server->bigbuf) {
475 server->bigbuf = (char *)cifs_buf_get();
476 if (!server->bigbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500477 cifs_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400478 msleep(3000);
479 /* retry will check if exiting */
480 return false;
481 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400482 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400483 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400484 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400485 }
486
Jeff Layton2a37ef92011-10-19 15:29:23 -0400487 if (!server->smallbuf) {
488 server->smallbuf = (char *)cifs_small_buf_get();
489 if (!server->smallbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500490 cifs_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400491 msleep(1000);
492 /* retry will check if exiting */
493 return false;
494 }
495 /* beginning of smb buffer is cleared in our buf_get */
496 } else {
497 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400498 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400499 }
500
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400501 return true;
502}
503
Jeff Laytonba749e62011-10-11 06:41:32 -0400504static bool
505server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400506{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300507 /*
508 * We need to wait 2 echo intervals to make sure we handle such
509 * situations right:
510 * 1s client sends a normal SMB request
511 * 2s client gets a response
512 * 30s echo workqueue job pops, and decides we got a response recently
513 * and don't need to send another
514 * ...
515 * 65s kernel_recvmsg times out, and we see that we haven't gotten
516 * a response in >60s.
517 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200518 if ((server->tcpStatus == CifsGood ||
519 server->tcpStatus == CifsNeedNegotiate) &&
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600520 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
521 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
522 server->hostname, (2 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400523 cifs_reconnect(server);
524 wake_up(&server->response_q);
525 return true;
526 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400527
Jeff Laytonba749e62011-10-11 06:41:32 -0400528 return false;
529}
530
Al Viro71335662016-01-09 19:54:50 -0500531static int
532cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400533{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400534 int length = 0;
535 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400536
Al Viro71335662016-01-09 19:54:50 -0500537 smb_msg->msg_control = NULL;
538 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400539
Al Viro71335662016-01-09 19:54:50 -0500540 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500541 try_to_freeze();
542
Al Viro71335662016-01-09 19:54:50 -0500543 if (server_unresponsive(server))
544 return -ECONNABORTED;
545
546 length = sock_recvmsg(server->ssocket, smb_msg, 0);
547
548 if (server->tcpStatus == CifsExiting)
549 return -ESHUTDOWN;
550
551 if (server->tcpStatus == CifsNeedReconnect) {
552 cifs_reconnect(server);
553 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400554 }
555
Al Viro71335662016-01-09 19:54:50 -0500556 if (length == -ERESTARTSYS ||
557 length == -EAGAIN ||
558 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400559 /*
560 * Minimum sleep to prevent looping, allowing socket
561 * to clear and app threads to set tcpStatus
562 * CifsNeedReconnect if server hung.
563 */
564 usleep_range(1000, 2000);
565 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400566 continue;
Al Viro71335662016-01-09 19:54:50 -0500567 }
568
569 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500570 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400571 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500572 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400573 }
574 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400575 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400576}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400577
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400578int
579cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
580 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400581{
Al Viro71335662016-01-09 19:54:50 -0500582 struct msghdr smb_msg;
583 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
584 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400585
Al Viro71335662016-01-09 19:54:50 -0500586 return cifs_readv_from_socket(server, &smb_msg);
587}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400588
Al Viro71335662016-01-09 19:54:50 -0500589int
590cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
591 unsigned int to_read)
592{
593 struct msghdr smb_msg;
594 struct bio_vec bv = {.bv_page = page, .bv_len = to_read};
595 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
596 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400597}
598
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400599static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400600is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400601{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400602 /*
603 * The first byte big endian of the length field,
604 * is actually not part of the length but the type
605 * with the most common, zero, as regular data.
606 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400607 switch (type) {
608 case RFC1002_SESSION_MESSAGE:
609 /* Regular SMB response */
610 return true;
611 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500612 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400613 break;
614 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500615 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400616 break;
617 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400618 /*
619 * We get this from Windows 98 instead of an error on
620 * SMB negprot response.
621 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500622 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400623 /* give server a second to clean up */
624 msleep(1000);
625 /*
626 * Always try 445 first on reconnect since we get NACK
627 * on some if we ever connected to port 139 (the NACK
628 * is since we do not begin with RFC1001 session
629 * initialize frame).
630 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400631 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400632 cifs_reconnect(server);
633 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400634 break;
635 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500636 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400637 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400638 }
639
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400640 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400641}
642
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400643void
644dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400645{
646#ifdef CONFIG_CIFS_STATS2
647 mid->when_received = jiffies;
648#endif
649 spin_lock(&GlobalMid_Lock);
650 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400651 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400652 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400653 mid->mid_state = MID_RESPONSE_MALFORMED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400654 list_del_init(&mid->qhead);
655 spin_unlock(&GlobalMid_Lock);
656}
657
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400658static void
659handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400660 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400661{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400662 if (server->ops->check_trans2 &&
663 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400664 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400665 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400666 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400667 /* Was previous buf put in mpx struct for multi-rsp? */
668 if (!mid->multiRsp) {
669 /* smb buffer will be freed by user thread */
670 if (server->large_buf)
671 server->bigbuf = NULL;
672 else
673 server->smallbuf = NULL;
674 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400675 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400676}
677
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400678static void clean_demultiplex_info(struct TCP_Server_Info *server)
679{
680 int length;
681
682 /* take it off the list, if it's not already */
683 spin_lock(&cifs_tcp_ses_lock);
684 list_del_init(&server->tcp_ses_list);
685 spin_unlock(&cifs_tcp_ses_lock);
686
687 spin_lock(&GlobalMid_Lock);
688 server->tcpStatus = CifsExiting;
689 spin_unlock(&GlobalMid_Lock);
690 wake_up_all(&server->response_q);
691
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400692 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300693 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400694 if (server->credits <= 0)
695 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300696 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400697 /*
698 * Although there should not be any requests blocked on this queue it
699 * can not hurt to be paranoid and try to wake up requests that may
700 * haven been blocked when more than 50 at time were on the wire to the
701 * same server - they now will see the session is in exit state and get
702 * out of SendReceive.
703 */
704 wake_up_all(&server->request_q);
705 /* give those requests time to exit */
706 msleep(125);
Long Libce9ce72017-11-22 17:38:38 -0700707 if (cifs_rdma_enabled(server) && server->smbd_conn) {
708 smbd_destroy(server->smbd_conn);
709 server->smbd_conn = NULL;
710 }
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400711 if (server->ssocket) {
712 sock_release(server->ssocket);
713 server->ssocket = NULL;
714 }
715
716 if (!list_empty(&server->pending_mid_q)) {
717 struct list_head dispose_list;
718 struct mid_q_entry *mid_entry;
719 struct list_head *tmp, *tmp2;
720
721 INIT_LIST_HEAD(&dispose_list);
722 spin_lock(&GlobalMid_Lock);
723 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
724 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500725 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400726 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400727 list_move(&mid_entry->qhead, &dispose_list);
728 }
729 spin_unlock(&GlobalMid_Lock);
730
731 /* now walk dispose list and issue callbacks */
732 list_for_each_safe(tmp, tmp2, &dispose_list) {
733 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500734 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400735 list_del_init(&mid_entry->qhead);
736 mid_entry->callback(mid_entry);
737 }
738 /* 1/8th of sec is more than enough time for them to exit */
739 msleep(125);
740 }
741
742 if (!list_empty(&server->pending_mid_q)) {
743 /*
744 * mpx threads have not exited yet give them at least the smb
745 * send timeout time for long ops.
746 *
747 * Due to delays on oplock break requests, we need to wait at
748 * least 45 seconds before giving up on a request getting a
749 * response and going ahead and killing cifsd.
750 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500751 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400752 msleep(46000);
753 /*
754 * If threads still have not exited they are probably never
755 * coming home not much else we can do but free the memory.
756 */
757 }
758
759 kfree(server->hostname);
760 kfree(server);
761
762 length = atomic_dec_return(&tcpSesAllocCount);
763 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700764 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400765}
766
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400767static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400768standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
769{
770 int length;
771 char *buf = server->smallbuf;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400772 unsigned int pdu_length = get_rfc1002_length(buf);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400773
774 /* make sure this will fit in a large buffer */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400775 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500776 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400777 cifs_reconnect(server);
778 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400779 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400780 }
781
782 /* switch to large buffer if too big for a small one */
783 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
784 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400785 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400786 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400787 }
788
789 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400790 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
791 pdu_length - HEADER_SIZE(server) + 1 + 4);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400792 if (length < 0)
793 return length;
794 server->total_read += length;
795
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400796 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400797
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800798 return cifs_handle_standard(server, mid);
799}
800
801int
802cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
803{
804 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
805 int length;
806
Jeff Laytone9097ab2011-10-19 15:29:40 -0400807 /*
808 * We know that we received enough to get to the MID as we
809 * checked the pdu_length earlier. Now check to see
810 * if the rest of the header is OK. We borrow the length
811 * var for the rest of the loop to avoid a new stack var.
812 *
813 * 48 bytes is enough to display the header and a little bit
814 * into the payload for debugging purposes.
815 */
Steve French373512e2015-12-18 13:05:30 -0600816 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400817 if (length != 0)
818 cifs_dump_mem("Bad SMB: ", buf,
819 min_t(unsigned int, server->total_read, 48));
820
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700821 if (server->ops->is_session_expired &&
822 server->ops->is_session_expired(buf)) {
823 cifs_reconnect(server);
824 wake_up(&server->response_q);
825 return -1;
826 }
827
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700828 if (server->ops->is_status_pending &&
829 server->ops->is_status_pending(buf, server, length))
830 return -1;
831
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500832 if (!mid)
833 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400834
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400835 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500836 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400837}
838
839static int
Al Viro7c97c202011-06-21 08:51:28 -0400840cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 int length;
Al Viro7c97c202011-06-21 08:51:28 -0400843 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400844 unsigned int pdu_length;
845 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500846 struct task_struct *task_to_wake = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct mid_q_entry *mid_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500850 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400851
852 length = atomic_inc_return(&tcpSesAllocCount);
853 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700854 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700856 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000857 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700858 if (try_to_freeze())
859 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700860
Jeff Layton2a37ef92011-10-19 15:29:23 -0400861 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400862 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700863
Jeff Layton2a37ef92011-10-19 15:29:23 -0400864 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400865 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000866 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000867
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400868 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400869 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000870 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400871 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700872
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400873 /*
874 * The right amount was read from socket - 4 bytes,
875 * so we can now interpret the length field.
876 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400877 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700878
Joe Perchesf96637b2013-05-04 22:12:25 -0500879 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400880 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000881 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700882
Jeff Layton89482a52011-10-19 15:28:57 -0400883 /* make sure we have enough to get to the MID */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400884 if (pdu_length < HEADER_SIZE(server) - 1 - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500885 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
886 pdu_length);
Jeff Layton89482a52011-10-19 15:28:57 -0400887 cifs_reconnect(server);
888 wake_up(&server->response_q);
889 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700890 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400891
Jeff Layton89482a52011-10-19 15:28:57 -0400892 /* read down to the MID */
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400893 length = cifs_read_from_socket(server, buf + 4,
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400894 HEADER_SIZE(server) - 1 - 4);
Jeff Layton89482a52011-10-19 15:28:57 -0400895 if (length < 0)
896 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400897 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400898
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800899 if (server->ops->is_transform_hdr &&
900 server->ops->receive_transform &&
901 server->ops->is_transform_hdr(buf)) {
902 length = server->ops->receive_transform(server,
903 &mid_entry);
904 } else {
905 mid_entry = server->ops->find_mid(server, buf);
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400906
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800907 if (!mid_entry || !mid_entry->receive)
908 length = standard_receive3(server, mid_entry);
909 else
910 length = mid_entry->receive(server, mid_entry);
911 }
Jeff Layton44d22d82011-10-19 15:29:49 -0400912
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400913 if (length < 0)
Steve Frenche4eb2952005-04-28 22:41:09 -0700914 continue;
915
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400916 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -0400917 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700918
Steve Frenchfda35942011-01-20 18:06:34 +0000919 server->lstrp = jiffies;
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500920 if (mid_entry != NULL) {
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800921 if ((mid_entry->mid_flags & MID_WAIT_CANCELLED) &&
922 mid_entry->mid_state == MID_RESPONSE_RECEIVED &&
923 server->ops->handle_cancelled_mid)
924 server->ops->handle_cancelled_mid(
925 mid_entry->resp_buf,
926 server);
927
Jeff Layton2a37ef92011-10-19 15:29:23 -0400928 if (!mid_entry->multiRsp || mid_entry->multiEnd)
929 mid_entry->callback(mid_entry);
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800930 } else if (server->ops->is_oplock_break &&
931 server->ops->is_oplock_break(buf, server)) {
932 cifs_dbg(FYI, "Received oplock break\n");
933 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500934 cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
935 atomic_read(&midCount));
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400936 cifs_dump_mem("Received Data is: ", buf,
937 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +0000938#ifdef CONFIG_CIFS_DEBUG2
Pavel Shilovsky7f0adb52012-05-28 15:50:10 +0400939 if (server->ops->dump_detail)
940 server->ops->dump_detail(buf);
Steve French39798772006-05-31 22:40:51 +0000941 cifs_dump_mids(server);
942#endif /* CIFS_DEBUG2 */
Steve French50c2f752007-07-13 00:33:32 +0000943
Steve Frenche4eb2952005-04-28 22:41:09 -0700944 }
945 } /* end while !EXITING */
946
Justin P. Mattockfd62cb72011-02-24 22:15:02 -0800947 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -0400948 cifs_buf_release(server->bigbuf);
949 if (server->smallbuf) /* no sense logging a debug message if NULL */
950 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500952 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400953 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500954
955 /* if server->tsk was NULL then wait for a signal before exiting */
956 if (!task_to_wake) {
957 set_current_state(TASK_INTERRUPTIBLE);
958 while (!signal_pending(current)) {
959 schedule();
960 set_current_state(TASK_INTERRUPTIBLE);
961 }
962 set_current_state(TASK_RUNNING);
963 }
964
Jeff Layton0468a2c2008-12-01 07:09:35 -0500965 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Jeff Laytonc359cf32007-11-16 22:22:06 +0000968/* extract the host portion of the UNC string */
969static char *
970extract_hostname(const char *unc)
971{
972 const char *src;
973 char *dst, *delim;
974 unsigned int len;
975
976 /* skip double chars at beginning of string */
977 /* BB: check validity of these bytes? */
978 src = unc + 2;
979
980 /* delimiter between hostname and sharename is always '\\' now */
981 delim = strchr(src, '\\');
982 if (!delim)
983 return ERR_PTR(-EINVAL);
984
985 len = delim - src;
986 dst = kmalloc((len + 1), GFP_KERNEL);
987 if (dst == NULL)
988 return ERR_PTR(-ENOMEM);
989
990 memcpy(dst, src, len);
991 dst[len] = '\0';
992
993 return dst;
994}
995
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400996static int get_option_ul(substring_t args[], unsigned long *option)
997{
998 int rc;
999 char *string;
1000
1001 string = match_strdup(args);
1002 if (string == NULL)
1003 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001004 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001005 kfree(string);
1006
1007 return rc;
1008}
1009
Eric W. Biederman3da46562013-02-06 01:37:39 -08001010static int get_option_uid(substring_t args[], kuid_t *result)
1011{
1012 unsigned long value;
1013 kuid_t uid;
1014 int rc;
1015
1016 rc = get_option_ul(args, &value);
1017 if (rc)
1018 return rc;
1019
1020 uid = make_kuid(current_user_ns(), value);
1021 if (!uid_valid(uid))
1022 return -EINVAL;
1023
1024 *result = uid;
1025 return 0;
1026}
1027
1028static int get_option_gid(substring_t args[], kgid_t *result)
1029{
1030 unsigned long value;
1031 kgid_t gid;
1032 int rc;
1033
1034 rc = get_option_ul(args, &value);
1035 if (rc)
1036 return rc;
1037
1038 gid = make_kgid(current_user_ns(), value);
1039 if (!gid_valid(gid))
1040 return -EINVAL;
1041
1042 *result = gid;
1043 return 0;
1044}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001045
1046static int cifs_parse_security_flavors(char *value,
1047 struct smb_vol *vol)
1048{
1049
1050 substring_t args[MAX_OPT_ARGS];
1051
Jeff Layton1e3cc572013-06-10 17:12:23 -05001052 /*
1053 * With mount options, the last one should win. Reset any existing
1054 * settings back to default.
1055 */
1056 vol->sectype = Unspecified;
1057 vol->sign = false;
1058
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001059 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001060 case Opt_sec_krb5p:
1061 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1062 return 1;
1063 case Opt_sec_krb5i:
1064 vol->sign = true;
1065 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001066 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001067 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001068 break;
1069 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001070 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001071 /* Fallthrough */
1072 case Opt_sec_ntlmssp:
1073 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001074 break;
1075 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001076 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001077 /* Fallthrough */
1078 case Opt_ntlm:
1079 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001080 break;
1081 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001082 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001083 /* Fallthrough */
1084 case Opt_sec_ntlmv2:
1085 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001086 break;
1087#ifdef CONFIG_CIFS_WEAK_PW_HASH
1088 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001089 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001090 break;
1091#endif
1092 case Opt_sec_none:
1093 vol->nullauth = 1;
1094 break;
1095 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001096 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001097 return 1;
1098 }
1099
1100 return 0;
1101}
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001104cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1105{
1106 substring_t args[MAX_OPT_ARGS];
1107
1108 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1109 case Opt_cache_loose:
1110 vol->direct_io = false;
1111 vol->strict_io = false;
1112 break;
1113 case Opt_cache_strict:
1114 vol->direct_io = false;
1115 vol->strict_io = true;
1116 break;
1117 case Opt_cache_none:
1118 vol->direct_io = true;
1119 vol->strict_io = false;
1120 break;
1121 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001122 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001123 return 1;
1124 }
1125 return 0;
1126}
1127
1128static int
Jeff Layton23db65f2012-05-15 12:20:51 -04001129cifs_parse_smb_version(char *value, struct smb_vol *vol)
1130{
1131 substring_t args[MAX_OPT_ARGS];
1132
1133 switch (match_token(value, cifs_smb_version_tokens, args)) {
1134 case Smb_1:
1135 vol->ops = &smb1_operations;
1136 vol->vals = &smb1_values;
1137 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001138 case Smb_20:
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001139 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001140 vol->vals = &smb20_values;
1141 break;
Steve French1080ef72011-02-24 18:07:19 +00001142 case Smb_21:
1143 vol->ops = &smb21_operations;
1144 vol->vals = &smb21_values;
1145 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001146 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001147 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001148 vol->vals = &smb30_values;
1149 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001150 case Smb_302:
1151 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1152 vol->vals = &smb302_values;
1153 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001154#ifdef CONFIG_CIFS_SMB311
1155 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001156 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001157 vol->vals = &smb311_values;
1158 break;
1159#endif /* SMB311 */
Steve French9764c022017-09-17 10:41:35 -05001160 case Smb_3any:
1161 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1162 vol->vals = &smb3any_values;
1163 break;
1164 case Smb_default:
1165 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1166 vol->vals = &smbdefault_values;
1167 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001168 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001169 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001170 return 1;
1171 }
1172 return 0;
1173}
1174
Jeff Laytond387a5c2012-12-10 06:10:46 -05001175/*
1176 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1177 * fields with the result. Returns 0 on success and an error otherwise.
1178 */
1179static int
1180cifs_parse_devname(const char *devname, struct smb_vol *vol)
1181{
1182 char *pos;
1183 const char *delims = "/\\";
1184 size_t len;
1185
1186 /* make sure we have a valid UNC double delimiter prefix */
1187 len = strspn(devname, delims);
1188 if (len != 2)
1189 return -EINVAL;
1190
1191 /* find delimiter between host and sharename */
1192 pos = strpbrk(devname + 2, delims);
1193 if (!pos)
1194 return -EINVAL;
1195
1196 /* skip past delimiter */
1197 ++pos;
1198
1199 /* now go until next delimiter or end of string */
1200 len = strcspn(pos, delims);
1201
1202 /* move "pos" up to delimiter or NULL */
1203 pos += len;
1204 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1205 if (!vol->UNC)
1206 return -ENOMEM;
1207
1208 convert_delimiter(vol->UNC, '\\');
1209
Sachin Prabhu11e31642016-02-08 13:44:01 +05301210 /* skip any delimiter */
1211 if (*pos == '/' || *pos == '\\')
1212 pos++;
1213
1214 /* If pos is NULL then no prepath */
1215 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001216 return 0;
1217
1218 vol->prepath = kstrdup(pos, GFP_KERNEL);
1219 if (!vol->prepath)
1220 return -ENOMEM;
1221
1222 return 0;
1223}
1224
Jeff Layton23db65f2012-05-15 12:20:51 -04001225static int
Sean Finneyb9468452011-04-11 13:19:32 +00001226cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve French50c2f752007-07-13 00:33:32 +00001227 struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001229 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001230 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 unsigned int temp_len, i, j;
1232 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001233 short int override_uid = -1;
1234 short int override_gid = -1;
1235 bool uid_specified = false;
1236 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001237 bool sloppy = false;
1238 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001239 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001240 char *string = NULL;
1241 char *tmp_end, *value;
1242 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001243 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001244 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001245 unsigned short port = 0;
1246 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001249 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001250 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jeff Layton6ee95422012-11-26 11:09:57 -05001252 /* ensure we always start with zeroed-out smb_vol */
1253 memset(vol, 0, sizeof(*vol));
1254
Jeff Layton88463992010-11-22 15:31:03 -05001255 /*
1256 * does not have to be perfect mapping since field is
1257 * informational, only used for servers that do not support
1258 * port 445 and it can be overridden at mount time
1259 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001260 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1261 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001262 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1263
Jeff Layton1397f2e2011-01-07 11:30:28 -05001264 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001265 /* null target name indicates to use *SMBSERVR default called name
1266 if we end up sending RFC1001 session initialize */
1267 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001268 vol->cred_uid = current_uid();
1269 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001270 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001271
Steve French2baa2682014-09-27 02:19:01 -05001272 /*
1273 * default to SFM style remapping of seven reserved characters
1274 * unless user overrides it or we negotiate CIFS POSIX where
1275 * it is unnecessary. Can not simultaneously use more than one mapping
1276 * since then readdir could list files that open could not open
1277 */
1278 vol->remap = true;
1279
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001280 /* default to only allowing write access to owner of the mount */
1281 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001284 /* default is always to request posix paths. */
1285 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001286 /* default to using server inode numbers where available */
1287 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001288
Jeff Layton1b359202012-09-19 15:20:27 -07001289 /* default is to use strict cifs caching semantics */
1290 vol->strict_io = true;
1291
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301292 vol->actimeo = CIFS_DEF_ACTIMEO;
1293
Steve French9764c022017-09-17 10:41:35 -05001294 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1295 vol->ops = &smb30_operations;
1296 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001297
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001298 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1299
Sean Finneyb9468452011-04-11 13:19:32 +00001300 if (!mountdata)
1301 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Sean Finneyb9468452011-04-11 13:19:32 +00001303 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1304 if (!mountdata_copy)
1305 goto cifs_parse_mount_err;
1306
1307 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001308 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001309
Steve French50c2f752007-07-13 00:33:32 +00001310 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001311 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 separator[0] = options[4];
1313 options += 5;
1314 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001315 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001318 vol->backupuid_specified = false; /* no backup intent for a user */
1319 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001320
Jeff Layton37d4f992013-05-24 07:40:05 -04001321 switch (cifs_parse_devname(devname, vol)) {
1322 case 0:
1323 break;
1324 case -ENOMEM:
1325 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1326 goto cifs_parse_mount_err;
1327 case -EINVAL:
1328 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1329 goto cifs_parse_mount_err;
1330 default:
1331 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1332 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001333 }
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001336 substring_t args[MAX_OPT_ARGS];
1337 unsigned long option;
1338 int token;
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (!*data)
1341 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001343 token = match_token(data, cifs_mount_option_tokens, args);
1344
1345 switch (token) {
1346
1347 /* Ingnore the following */
1348 case Opt_ignore:
1349 break;
1350
1351 /* Boolean values */
1352 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001354 break;
1355 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001357 break;
1358 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001359 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001360 break;
1361 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001362 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001363 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001364 case Opt_forcegid:
1365 override_gid = 1;
1366 break;
1367 case Opt_noforcegid:
1368 override_gid = 0;
1369 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001370 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001371 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001372 break;
1373 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001374 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001375 break;
1376 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001378 break;
1379 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001381 break;
1382 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001384 break;
1385 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001387 break;
1388 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001389 vol->sfu_remap = true;
1390 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001391 break;
1392 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001393 vol->sfu_remap = false;
1394 break;
1395 case Opt_mapposix:
1396 vol->remap = true;
1397 vol->sfu_remap = false; /* disable SFU mapping */
1398 break;
1399 case Opt_nomapposix:
1400 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001401 break;
1402 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001403 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001404 break;
1405 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001406 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001407 break;
1408 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001409 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001410 break;
1411 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001412 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001413 break;
1414 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001415 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001416 break;
1417 case Opt_nounix:
Steve Frenchc18c8422007-07-18 23:21:09 +00001418 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001419 break;
1420 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001421 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001422 break;
1423 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001424 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001425 break;
1426 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001427 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001428 /*
1429 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001430 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001431 * local vfs will do advisory
1432 */
Steve French50c2f752007-07-13 00:33:32 +00001433 if (vol->file_mode ==
1434 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001435 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001436 break;
1437 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001438 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001439 break;
1440 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001442 break;
1443 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001445 break;
Steve French95932652016-09-23 01:36:34 -05001446 case Opt_setuidfromacl:
1447 vol->setuidfromacl = 1;
1448 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001449 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001450 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001451 break;
1452 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001453 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001454 break;
1455 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001457 break;
1458 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001460 break;
1461 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001463 break;
1464 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001466 break;
1467 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001468 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001469 break;
1470 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001471 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001472 break;
1473 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001475 break;
1476 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001478 break;
1479 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001480 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001481 break;
1482 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001483 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001484 break;
1485 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001486 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001487 break;
1488 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001490 break;
1491 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001493 break;
1494 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001495 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001496 break;
1497 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001498 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001499 break;
1500 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001501 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001502 * is a per tree connection (mount) not a per socket
1503 * or per-smb connection option in the protocol
1504 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1505 */
Steve French95b1cb92008-05-15 16:44:38 +00001506 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001507 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001508 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001509 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001510 break;
1511 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301512#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001513 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001514 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301515#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301516 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001517 break;
1518 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001519 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001520 break;
1521 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001522 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001523 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001524 case Opt_sloppy:
1525 sloppy = true;
1526 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001527 case Opt_nosharesock:
1528 vol->nosharesock = true;
1529 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001530 case Opt_nopersistent:
1531 vol->nopersistent = true;
1532 if (vol->persistent) {
1533 cifs_dbg(VFS,
1534 "persistenthandles mount options conflict\n");
1535 goto cifs_parse_mount_err;
1536 }
1537 break;
1538 case Opt_persistent:
1539 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001540 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001541 cifs_dbg(VFS,
1542 "persistenthandles mount options conflict\n");
1543 goto cifs_parse_mount_err;
1544 }
1545 break;
Steve French592fafe2015-11-03 10:08:53 -06001546 case Opt_resilient:
1547 vol->resilient = true;
1548 if (vol->persistent) {
1549 cifs_dbg(VFS,
1550 "persistenthandles mount options conflict\n");
1551 goto cifs_parse_mount_err;
1552 }
1553 break;
1554 case Opt_noresilient:
1555 vol->resilient = false; /* already the default */
1556 break;
Germano Percossi39566442016-12-15 12:31:18 +05301557 case Opt_domainauto:
1558 vol->domainauto = true;
1559 break;
Long Li8339dd32017-11-07 01:54:55 -07001560 case Opt_rdma:
1561 vol->rdma = true;
1562 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001563
1564 /* Numeric Values */
1565 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001566 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001567 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1568 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001569 goto cifs_parse_mount_err;
1570 }
1571 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001572 break;
1573 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001574 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001575 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1576 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001577 goto cifs_parse_mount_err;
1578 }
1579 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001580 break;
1581 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001582 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001583 cifs_dbg(VFS, "%s: Invalid uid value\n",
1584 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001585 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001587 uid_specified = true;
1588 break;
1589 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001590 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001591 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1592 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001593 goto cifs_parse_mount_err;
1594 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001595 break;
1596 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001597 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001598 cifs_dbg(VFS, "%s: Invalid gid value\n",
1599 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001600 goto cifs_parse_mount_err;
1601 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001602 gid_specified = true;
1603 break;
1604 case Opt_file_mode:
1605 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001606 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1607 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001608 goto cifs_parse_mount_err;
1609 }
1610 vol->file_mode = option;
1611 break;
1612 case Opt_dirmode:
1613 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001614 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1615 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001616 goto cifs_parse_mount_err;
1617 }
1618 vol->dir_mode = option;
1619 break;
1620 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001621 if (get_option_ul(args, &option) ||
1622 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001623 cifs_dbg(VFS, "%s: Invalid port value\n",
1624 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001625 goto cifs_parse_mount_err;
1626 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001627 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001628 break;
1629 case Opt_rsize:
1630 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001631 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1632 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001633 goto cifs_parse_mount_err;
1634 }
1635 vol->rsize = option;
1636 break;
1637 case Opt_wsize:
1638 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001639 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1640 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001641 goto cifs_parse_mount_err;
1642 }
1643 vol->wsize = option;
1644 break;
1645 case Opt_actimeo:
1646 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001647 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1648 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001649 goto cifs_parse_mount_err;
1650 }
1651 vol->actimeo = HZ * option;
1652 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001653 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001654 goto cifs_parse_mount_err;
1655 }
1656 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001657 case Opt_echo_interval:
1658 if (get_option_ul(args, &option)) {
1659 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1660 __func__);
1661 goto cifs_parse_mount_err;
1662 }
1663 vol->echo_interval = option;
1664 break;
Steve French8b217fe2016-11-11 22:36:20 -06001665 case Opt_snapshot:
1666 if (get_option_ul(args, &option)) {
1667 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1668 __func__);
1669 goto cifs_parse_mount_err;
1670 }
1671 vol->snapshot_time = option;
1672 break;
Steve French141891f2016-09-23 00:44:16 -05001673 case Opt_max_credits:
1674 if (get_option_ul(args, &option) || (option < 20) ||
1675 (option > 60000)) {
1676 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1677 __func__);
1678 goto cifs_parse_mount_err;
1679 }
1680 vol->max_credits = option;
1681 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001682
1683 /* String Arguments */
1684
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001685 case Opt_blank_user:
1686 /* null user, ie. anonymous authentication */
1687 vol->nullauth = 1;
1688 vol->username = NULL;
1689 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001690 case Opt_user:
1691 string = match_strdup(args);
1692 if (string == NULL)
1693 goto out_nomem;
1694
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001695 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1696 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001697 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001698 goto cifs_parse_mount_err;
1699 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001700
1701 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001702 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001703 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001704 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001705 break;
1706 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001707 /* passwords have to be handled differently
1708 * to allow the character used for deliminator
1709 * to be passed within them
1710 */
1711
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001712 /*
1713 * Check if this is a case where the password
1714 * starts with a delimiter
1715 */
1716 tmp_end = strchr(data, '=');
1717 tmp_end++;
1718 if (!(tmp_end < end && tmp_end[1] == delim)) {
1719 /* No it is not. Set the password to NULL */
Namjae Jeond6ccf492014-08-21 19:11:20 +09001720 kfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001721 vol->password = NULL;
1722 break;
1723 }
1724 /* Yes it is. Drop down to Opt_pass below.*/
1725 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001726 /* Obtain the value string */
1727 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001728 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001729
1730 /* Set tmp_end to end of the string */
1731 tmp_end = (char *) value + strlen(value);
1732
1733 /* Check if following character is the deliminator
1734 * If yes, we have encountered a double deliminator
1735 * reset the NULL character to the deliminator
1736 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301737 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001738 tmp_end[0] = delim;
1739
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301740 /* Keep iterating until we get to a single
1741 * deliminator OR the end
1742 */
1743 while ((tmp_end = strchr(tmp_end, delim))
1744 != NULL && (tmp_end[1] == delim)) {
1745 tmp_end = (char *) &tmp_end[2];
1746 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001747
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301748 /* Reset var options to point to next element */
1749 if (tmp_end) {
1750 tmp_end[0] = '\0';
1751 options = (char *) &tmp_end[1];
1752 } else
1753 /* Reached the end of the mount option
1754 * string */
1755 options = end;
1756 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001757
Namjae Jeond6ccf492014-08-21 19:11:20 +09001758 kfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001759 /* Now build new password string */
1760 temp_len = strlen(value);
1761 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1762 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001763 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001764 goto cifs_parse_mount_err;
1765 }
1766
1767 for (i = 0, j = 0; i < temp_len; i++, j++) {
1768 vol->password[j] = value[i];
1769 if ((value[i] == delim) &&
1770 value[i+1] == delim)
1771 /* skip the second deliminator */
1772 i++;
1773 }
1774 vol->password[j] = '\0';
1775 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001776 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001777 /* FIXME: should this be an error instead? */
1778 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001779 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001780 case Opt_ip:
1781 string = match_strdup(args);
1782 if (string == NULL)
1783 goto out_nomem;
1784
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001785 if (!cifs_convert_address(dstaddr, string,
1786 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001787 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001788 goto cifs_parse_mount_err;
1789 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001790 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001791 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001792 case Opt_domain:
1793 string = match_strdup(args);
1794 if (string == NULL)
1795 goto out_nomem;
1796
Chen Gang057d6332013-07-19 09:01:36 +08001797 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1798 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001799 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001800 goto cifs_parse_mount_err;
1801 }
1802
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001803 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001804 vol->domainname = kstrdup(string, GFP_KERNEL);
1805 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001806 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001807 goto cifs_parse_mount_err;
1808 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001809 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001810 break;
1811 case Opt_srcaddr:
1812 string = match_strdup(args);
1813 if (string == NULL)
1814 goto out_nomem;
1815
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001816 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001817 (struct sockaddr *)&vol->srcaddr,
1818 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001819 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1820 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001821 goto cifs_parse_mount_err;
1822 }
1823 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001824 case Opt_iocharset:
1825 string = match_strdup(args);
1826 if (string == NULL)
1827 goto out_nomem;
1828
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001829 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001830 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001831 goto cifs_parse_mount_err;
1832 }
1833
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001834 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001835 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001836 vol->iocharset = kstrdup(string,
1837 GFP_KERNEL);
1838 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001839 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001840 goto cifs_parse_mount_err;
1841 }
1842 }
1843 /* if iocharset not set then load_nls_default
1844 * is used by caller
1845 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001846 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001847 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001848 case Opt_netbiosname:
1849 string = match_strdup(args);
1850 if (string == NULL)
1851 goto out_nomem;
1852
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001853 memset(vol->source_rfc1001_name, 0x20,
1854 RFC1001_NAME_LEN);
1855 /*
1856 * FIXME: are there cases in which a comma can
1857 * be valid in workstation netbios name (and
1858 * need special handling)?
1859 */
1860 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1861 /* don't ucase netbiosname for user */
1862 if (string[i] == 0)
1863 break;
1864 vol->source_rfc1001_name[i] = string[i];
1865 }
1866 /* The string has 16th byte zero still from
1867 * set at top of the function
1868 */
1869 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001870 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001871 break;
1872 case Opt_servern:
1873 /* servernetbiosname specified override *SMBSERVER */
1874 string = match_strdup(args);
1875 if (string == NULL)
1876 goto out_nomem;
1877
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001878 /* last byte, type, is 0x20 for servr type */
1879 memset(vol->target_rfc1001_name, 0x20,
1880 RFC1001_NAME_LEN_WITH_NULL);
1881
1882 /* BB are there cases in which a comma can be
1883 valid in this workstation netbios name
1884 (and need special handling)? */
1885
1886 /* user or mount helper must uppercase the
1887 netbios name */
1888 for (i = 0; i < 15; i++) {
1889 if (string[i] == 0)
1890 break;
1891 vol->target_rfc1001_name[i] = string[i];
1892 }
1893 /* The string has 16th byte zero still from
1894 set at top of the function */
1895 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001896 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001897 break;
1898 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05001899 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001900 string = match_strdup(args);
1901 if (string == NULL)
1902 goto out_nomem;
1903
Steve French7e682f72017-08-31 21:34:24 -05001904 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001905 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05001906 if (strlen(string) > 1) {
1907 pr_warn("Bad mount helper ver=%s. Did "
1908 "you want SMB1 (CIFS) dialect "
1909 "and mean to type vers=1.0 "
1910 "instead?\n", string);
1911 goto cifs_parse_mount_err;
1912 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001913 /* This is the default */
1914 break;
1915 }
1916 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05001917 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001918 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04001919 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05001920 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04001921 string = match_strdup(args);
1922 if (string == NULL)
1923 goto out_nomem;
1924
1925 if (cifs_parse_smb_version(string, vol) != 0)
1926 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05001927 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04001928 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001929 case Opt_sec:
1930 string = match_strdup(args);
1931 if (string == NULL)
1932 goto out_nomem;
1933
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001934 if (cifs_parse_security_flavors(string, vol) != 0)
1935 goto cifs_parse_mount_err;
1936 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04001937 case Opt_cache:
1938 string = match_strdup(args);
1939 if (string == NULL)
1940 goto out_nomem;
1941
1942 if (cifs_parse_cache_flavor(string, vol) != 0)
1943 goto cifs_parse_mount_err;
1944 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001945 default:
Jeff Laytond8162552012-03-23 14:40:56 -04001946 /*
1947 * An option we don't recognize. Save it off for later
1948 * if we haven't already found one
1949 */
1950 if (!invalid)
1951 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001952 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001954 /* Free up any allocated string */
1955 kfree(string);
1956 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04001958
Jeff Laytond8162552012-03-23 14:40:56 -04001959 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001960 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04001961 goto cifs_parse_mount_err;
1962 }
1963
Long Li8339dd32017-11-07 01:54:55 -07001964 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
1965 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
1966 goto cifs_parse_mount_err;
1967 }
1968
Jeff Layton8a8798a2012-01-17 16:09:15 -05001969#ifndef CONFIG_KEYS
1970 /* Muliuser mounts require CONFIG_KEYS support */
1971 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001972 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001973 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04001974 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05001975#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05001976 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04001977 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05001978 goto cifs_parse_mount_err;
1979 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04001980
Jeff Layton62a1a432012-12-10 06:10:45 -05001981 /* make sure UNC has a share name */
1982 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001983 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05001984 goto cifs_parse_mount_err;
1985 }
1986
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001987 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02001988 int len;
1989 const char *slash;
1990
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001991 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02001992 /* Use the address part of the UNC. */
1993 slash = strchr(&vol->UNC[2], '\\');
1994 len = slash - &vol->UNC[2];
1995 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001996 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001997 goto cifs_parse_mount_err;
1998 }
1999 }
2000
2001 /* set the port that we got earlier */
2002 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002004 if (uid_specified)
2005 vol->override_uid = override_uid;
2006 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002007 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002008
2009 if (gid_specified)
2010 vol->override_gid = override_gid;
2011 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002012 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002013
Steve French7e682f72017-08-31 21:34:24 -05002014 if (got_version == false)
2015 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002016 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002017 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002018 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2019 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002020
Sean Finneyb9468452011-04-11 13:19:32 +00002021 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002023
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002024out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002025 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002026cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002027 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002028 kfree(mountdata_copy);
2029 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Ben Greear3eb9a882010-09-01 17:06:02 -07002032/** Returns true if srcaddr isn't specified and rhs isn't
2033 * specified, or if srcaddr is specified and
2034 * matches the IP address of the rhs argument.
2035 */
Jeff Layton45151482010-07-06 20:43:02 -04002036static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002037srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2038{
2039 switch (srcaddr->sa_family) {
2040 case AF_UNSPEC:
2041 return (rhs->sa_family == AF_UNSPEC);
2042 case AF_INET: {
2043 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2044 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2045 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2046 }
2047 case AF_INET6: {
2048 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002049 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002050 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2051 }
2052 default:
2053 WARN_ON(1);
2054 return false; /* don't expect to be here */
2055 }
2056}
2057
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002058/*
2059 * If no port is specified in addr structure, we try to match with 445 port
2060 * and if it fails - with 139 ports. It should be called only if address
2061 * families of server and addr are equal.
2062 */
2063static bool
2064match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2065{
Steve French6da97912011-03-13 18:55:55 +00002066 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002067
2068 switch (addr->sa_family) {
2069 case AF_INET:
2070 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2071 port = ((struct sockaddr_in *) addr)->sin_port;
2072 break;
2073 case AF_INET6:
2074 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2075 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2076 break;
2077 default:
2078 WARN_ON(1);
2079 return false;
2080 }
2081
2082 if (!port) {
2083 port = htons(CIFS_PORT);
2084 if (port == *sport)
2085 return true;
2086
2087 port = htons(RFC1001_PORT);
2088 }
2089
2090 return port == *sport;
2091}
Ben Greear3eb9a882010-09-01 17:06:02 -07002092
2093static bool
2094match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2095 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Jeff Layton45151482010-07-06 20:43:02 -04002097 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002098 case AF_INET: {
2099 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2100 struct sockaddr_in *srv_addr4 =
2101 (struct sockaddr_in *)&server->dstaddr;
2102
2103 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002104 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002105 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002106 }
2107 case AF_INET6: {
2108 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2109 struct sockaddr_in6 *srv_addr6 =
2110 (struct sockaddr_in6 *)&server->dstaddr;
2111
Jeff Layton45151482010-07-06 20:43:02 -04002112 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002113 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002114 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002115 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002116 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002117 break;
2118 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002119 default:
2120 WARN_ON(1);
2121 return false; /* don't expect to be here */
2122 }
Jeff Layton45151482010-07-06 20:43:02 -04002123
Ben Greear3eb9a882010-09-01 17:06:02 -07002124 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2125 return false;
2126
Jeff Layton45151482010-07-06 20:43:02 -04002127 return true;
2128}
2129
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002130static bool
2131match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2132{
Jeff Layton3f618222013-06-12 19:52:14 -05002133 /*
2134 * The select_sectype function should either return the vol->sectype
2135 * that was specified, or "Unspecified" if that sectype was not
2136 * compatible with the given NEGOTIATE request.
2137 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302138 if (server->ops->select_sectype(server, vol->sectype)
2139 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002140 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002141
Jeff Layton3f618222013-06-12 19:52:14 -05002142 /*
2143 * Now check if signing mode is acceptable. No need to check
2144 * global_secflags at this point since if MUST_SIGN is set then
2145 * the server->sign had better be too.
2146 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002147 if (vol->sign && !server->sign)
2148 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002149
2150 return true;
2151}
2152
Jeff Layton9fa114f2012-11-26 11:09:57 -05002153static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002154{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002155 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2156
Jeff Laytona0b3df52013-05-24 07:40:59 -04002157 if (vol->nosharesock)
2158 return 0;
2159
Steve French9764c022017-09-17 10:41:35 -05002160 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002161 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2162 return 0;
2163
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002164 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2165 return 0;
2166
2167 if (!match_address(server, addr,
2168 (struct sockaddr *)&vol->srcaddr))
2169 return 0;
2170
2171 if (!match_port(server, addr))
2172 return 0;
2173
2174 if (!match_security(server, vol))
2175 return 0;
2176
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002177 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002178 return 0;
2179
Long Li8339dd32017-11-07 01:54:55 -07002180 if (server->rdma != vol->rdma)
2181 return 0;
2182
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002183 return 1;
2184}
2185
Jeff Layton45151482010-07-06 20:43:02 -04002186static struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002187cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002188{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002189 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302191 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002192 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002193 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002194 continue;
2195
Jeff Laytone7ddee92008-11-14 13:44:38 -05002196 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302197 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002198 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002199 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302201 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return NULL;
2203}
2204
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002205void
2206cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002208 struct task_struct *task;
2209
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302210 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002211 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302212 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002213 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002215
Rob Landleyf1d0c992011-01-22 15:44:05 -06002216 put_net(cifs_net_ns(server));
2217
Jeff Laytone7ddee92008-11-14 13:44:38 -05002218 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302219 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002220
Jeff Laytonc74093b2011-01-11 07:24:23 -05002221 cancel_delayed_work_sync(&server->echo);
2222
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002223 if (from_reconnect)
2224 /*
2225 * Avoid deadlock here: reconnect work calls
2226 * cifs_put_tcp_session() at its end. Need to be sure
2227 * that reconnect work does nothing with server pointer after
2228 * that step.
2229 */
2230 cancel_delayed_work(&server->reconnect);
2231 else
2232 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002233
Jeff Laytone7ddee92008-11-14 13:44:38 -05002234 spin_lock(&GlobalMid_Lock);
2235 server->tcpStatus = CifsExiting;
2236 spin_unlock(&GlobalMid_Lock);
2237
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002238 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302239 cifs_fscache_release_client_cookie(server);
2240
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002241 kfree(server->session_key.response);
2242 server->session_key.response = NULL;
2243 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002244
2245 task = xchg(&server->tsk, NULL);
2246 if (task)
2247 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
Jeff Layton63c038c2008-12-01 18:41:46 -05002250static struct TCP_Server_Info *
2251cifs_get_tcp_session(struct smb_vol *volume_info)
2252{
2253 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002254 int rc;
2255
Joe Perchesf96637b2013-05-04 22:12:25 -05002256 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002257
2258 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002259 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002260 if (tcp_ses)
2261 return tcp_ses;
2262
2263 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2264 if (!tcp_ses) {
2265 rc = -ENOMEM;
2266 goto out_err;
2267 }
2268
Jeff Layton23db65f2012-05-15 12:20:51 -04002269 tcp_ses->ops = volume_info->ops;
2270 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002271 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002272 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2273 if (IS_ERR(tcp_ses->hostname)) {
2274 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002275 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002276 }
2277
2278 tcp_ses->noblocksnd = volume_info->noblocksnd;
2279 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002280 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002281 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002282 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002283 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002284 init_waitqueue_head(&tcp_ses->response_q);
2285 init_waitqueue_head(&tcp_ses->request_q);
2286 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2287 mutex_init(&tcp_ses->srv_mutex);
2288 memcpy(tcp_ses->workstation_RFC1001_name,
2289 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2290 memcpy(tcp_ses->server_RFC1001_name,
2291 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002292 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002293 tcp_ses->sequence_number = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002294 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002295 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002296 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2297 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002298 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002299 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2300 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002301 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2302 sizeof(tcp_ses->srcaddr));
2303 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2304 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002305 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002306 /*
2307 * at this point we are the only ones with the pointer
2308 * to the struct since the kernel thread not created yet
2309 * no need to spinlock this init of tcpStatus or srv_count
2310 */
2311 tcp_ses->tcpStatus = CifsNew;
2312 ++tcp_ses->srv_count;
2313
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002314 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2315 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2316 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2317 else
2318 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002319 if (tcp_ses->rdma) {
2320#ifndef CONFIG_CIFS_SMB_DIRECT
2321 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2322 rc = -ENOENT;
2323 goto out_err_crypto_release;
2324#endif
2325 tcp_ses->smbd_conn = smbd_get_connection(
2326 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2327 if (tcp_ses->smbd_conn) {
2328 cifs_dbg(VFS, "RDMA transport established\n");
2329 rc = 0;
2330 goto smbd_connected;
2331 } else {
2332 rc = -ENOENT;
2333 goto out_err_crypto_release;
2334 }
2335 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002336 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002337 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002338 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002339 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002340 }
Long Li2f894642017-11-22 17:38:34 -07002341smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002342 /*
2343 * since we're in a cifs function already, we know that
2344 * this will succeed. No need for try_module_get().
2345 */
2346 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002347 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002348 tcp_ses, "cifsd");
2349 if (IS_ERR(tcp_ses->tsk)) {
2350 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002351 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002352 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002353 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002354 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002355 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002356
2357 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302358 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002359 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302360 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002361
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302362 cifs_fscache_get_client_cookie(tcp_ses);
2363
Jeff Laytonc74093b2011-01-11 07:24:23 -05002364 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002365 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002366
Jeff Layton63c038c2008-12-01 18:41:46 -05002367 return tcp_ses;
2368
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002369out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002370 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002371
Rob Landleyf1d0c992011-01-22 15:44:05 -06002372 put_net(cifs_net_ns(tcp_ses));
2373
Jeff Layton63c038c2008-12-01 18:41:46 -05002374out_err:
2375 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002376 if (!IS_ERR(tcp_ses->hostname))
2377 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002378 if (tcp_ses->ssocket)
2379 sock_release(tcp_ses->ssocket);
2380 kfree(tcp_ses);
2381 }
2382 return ERR_PTR(rc);
2383}
2384
Steve French96daf2b2011-05-27 04:34:02 +00002385static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002386{
Jeff Layton3f618222013-06-12 19:52:14 -05002387 if (vol->sectype != Unspecified &&
2388 vol->sectype != ses->sectype)
2389 return 0;
2390
2391 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002392 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002393 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002394 return 0;
2395 break;
2396 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002397 /* NULL username means anonymous session */
2398 if (ses->user_name == NULL) {
2399 if (!vol->nullauth)
2400 return 0;
2401 break;
2402 }
2403
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002404 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002405 if (strncmp(ses->user_name,
2406 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002407 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002408 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002409 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002410 ses->password != NULL &&
2411 strncmp(ses->password,
2412 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002413 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002414 return 0;
2415 }
2416 return 1;
2417}
2418
Steve French96daf2b2011-05-27 04:34:02 +00002419static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002420cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Steve French96daf2b2011-05-27 04:34:02 +00002422 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302424 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002425 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002426 if (ses->status == CifsExiting)
2427 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002428 if (!match_session(ses, vol))
2429 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002430 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302431 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002432 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302434 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return NULL;
2436}
2437
Jeff Layton14fbf502008-11-14 13:53:46 -05002438static void
Steve French96daf2b2011-05-27 04:34:02 +00002439cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002440{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002441 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002442 struct TCP_Server_Info *server = ses->server;
2443
Joe Perchesf96637b2013-05-04 22:12:25 -05002444 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002445
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302446 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002447 if (ses->status == CifsExiting) {
2448 spin_unlock(&cifs_tcp_ses_lock);
2449 return;
2450 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002451 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302452 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002453 return;
2454 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002455 if (ses->status == CifsGood)
2456 ses->status = CifsExiting;
2457 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002458
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002459 if (ses->status == CifsExiting && server->ops->logoff) {
2460 xid = get_xid();
2461 rc = server->ops->logoff(xid, ses);
2462 if (rc)
2463 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2464 __func__, rc);
2465 _free_xid(xid);
2466 }
2467
2468 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002469 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302470 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002471
Jeff Layton14fbf502008-11-14 13:53:46 -05002472 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002473 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002474}
2475
Jeff Layton8a8798a2012-01-17 16:09:15 -05002476#ifdef CONFIG_KEYS
2477
Chen Gang057d6332013-07-19 09:01:36 +08002478/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2479#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002480
2481/* Populate username and pw fields from keyring if possible */
2482static int
2483cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2484{
2485 int rc = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002486 const char *delim, *payload;
2487 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002488 ssize_t len;
2489 struct key *key;
2490 struct TCP_Server_Info *server = ses->server;
2491 struct sockaddr_in *sa;
2492 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002493 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002494
2495 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2496 if (!desc)
2497 return -ENOMEM;
2498
2499 /* try to find an address key first */
2500 switch (server->dstaddr.ss_family) {
2501 case AF_INET:
2502 sa = (struct sockaddr_in *)&server->dstaddr;
2503 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2504 break;
2505 case AF_INET6:
2506 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2507 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2508 break;
2509 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002510 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2511 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002512 rc = -EINVAL;
2513 goto out_err;
2514 }
2515
Joe Perchesf96637b2013-05-04 22:12:25 -05002516 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002517 key = request_key(&key_type_logon, desc, "");
2518 if (IS_ERR(key)) {
2519 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002520 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002521 rc = PTR_ERR(key);
2522 goto out_err;
2523 }
2524
2525 /* didn't work, try to find a domain key */
2526 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002527 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002528 key = request_key(&key_type_logon, desc, "");
2529 if (IS_ERR(key)) {
2530 rc = PTR_ERR(key);
2531 goto out_err;
2532 }
2533 }
2534
2535 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002536 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002537 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002538 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002539 goto out_key_put;
2540 }
2541
2542 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002543 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002544 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002545 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002546 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002547 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2548 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002549 rc = -EINVAL;
2550 goto out_key_put;
2551 }
2552
2553 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002554 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002555 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2556 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002557 rc = -EINVAL;
2558 goto out_key_put;
2559 }
2560
2561 vol->username = kstrndup(payload, len, GFP_KERNEL);
2562 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002563 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2564 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002565 rc = -ENOMEM;
2566 goto out_key_put;
2567 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002568 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002569
2570 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002571 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002572 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002573 rc = -EINVAL;
2574 kfree(vol->username);
2575 vol->username = NULL;
2576 goto out_key_put;
2577 }
2578
2579 ++delim;
2580 vol->password = kstrndup(delim, len, GFP_KERNEL);
2581 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002582 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2583 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002584 rc = -ENOMEM;
2585 kfree(vol->username);
2586 vol->username = NULL;
2587 goto out_key_put;
2588 }
2589
2590out_key_put:
2591 up_read(&key->sem);
2592 key_put(key);
2593out_err:
2594 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002595 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002596 return rc;
2597}
2598#else /* ! CONFIG_KEYS */
2599static inline int
2600cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2601 struct cifs_ses *ses __attribute__((unused)))
2602{
2603 return -ENOSYS;
2604}
2605#endif /* CONFIG_KEYS */
2606
Steve French96daf2b2011-05-27 04:34:02 +00002607static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002608cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2609{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002610 int rc = -ENOMEM;
2611 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002612 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002613 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2614 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002615
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002616 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002617
Jeff Layton4ff67b72010-07-06 20:43:02 -04002618 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002619 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002620 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2621 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002622
Jeff Layton36988c72010-04-24 07:57:43 -04002623 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002624 rc = cifs_negotiate_protocol(xid, ses);
2625 if (rc) {
2626 mutex_unlock(&ses->session_mutex);
2627 /* problem -- put our ses reference */
2628 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002629 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002630 return ERR_PTR(rc);
2631 }
Jeff Layton36988c72010-04-24 07:57:43 -04002632 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002633 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002634 rc = cifs_setup_session(xid, ses,
2635 volume_info->local_nls);
2636 if (rc) {
2637 mutex_unlock(&ses->session_mutex);
2638 /* problem -- put our reference */
2639 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002640 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002641 return ERR_PTR(rc);
2642 }
2643 }
2644 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002645
2646 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002647 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002648 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002649 return ses;
2650 }
2651
Joe Perchesf96637b2013-05-04 22:12:25 -05002652 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002653 ses = sesInfoAlloc();
2654 if (ses == NULL)
2655 goto get_ses_fail;
2656
2657 /* new SMB session uses our server ref */
2658 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002659 if (server->dstaddr.ss_family == AF_INET6)
2660 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002661 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002662 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002663
Steve French8727c8a2011-02-25 01:11:56 -06002664 if (volume_info->username) {
2665 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2666 if (!ses->user_name)
2667 goto get_ses_fail;
2668 }
Jeff Layton36988c72010-04-24 07:57:43 -04002669
2670 /* volume_info->password freed at unmount */
2671 if (volume_info->password) {
2672 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2673 if (!ses->password)
2674 goto get_ses_fail;
2675 }
2676 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002677 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2678 if (!ses->domainName)
2679 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002680 }
Germano Percossi39566442016-12-15 12:31:18 +05302681 if (volume_info->domainauto)
2682 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002683 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002684 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002685
Jeff Layton28e11bd2013-05-26 07:01:00 -04002686 ses->sectype = volume_info->sectype;
2687 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002688
2689 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002690 rc = cifs_negotiate_protocol(xid, ses);
2691 if (!rc)
2692 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002693 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002694 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002695 goto get_ses_fail;
2696
2697 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302698 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002699 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302700 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002701
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002702 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002703 return ses;
2704
2705get_ses_fail:
2706 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002707 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002708 return ERR_PTR(rc);
2709}
2710
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002711static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002712{
2713 if (tcon->tidStatus == CifsExiting)
2714 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002715 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002716 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002717 if (tcon->seal != volume_info->seal)
2718 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002719 if (tcon->snapshot_time != volume_info->snapshot_time)
2720 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002721 return 1;
2722}
2723
Steve French96daf2b2011-05-27 04:34:02 +00002724static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06002725cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002728 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302730 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002731 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002732 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002733 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002734 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002735 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302736 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return tcon;
2738 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302739 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 return NULL;
2741}
2742
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002743void
Steve French96daf2b2011-05-27 04:34:02 +00002744cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002745{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002746 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002747 struct cifs_ses *ses = tcon->ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002748
Joe Perchesf96637b2013-05-04 22:12:25 -05002749 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302750 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002751 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302752 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002753 return;
2754 }
2755
2756 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302757 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002758
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002759 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002760 if (ses->server->ops->tree_disconnect)
2761 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002762 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002763
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302764 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002765 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002766 cifs_put_smb_ses(ses);
2767}
2768
Steve French96daf2b2011-05-27 04:34:02 +00002769static struct cifs_tcon *
2770cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002771{
2772 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002773 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002774
Steve French8b217fe2016-11-11 22:36:20 -06002775 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002776 if (tcon) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002777 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002778 /* existing tcon already has a reference */
2779 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002780 return tcon;
2781 }
2782
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002783 if (!ses->server->ops->tree_connect) {
2784 rc = -ENOSYS;
2785 goto out_fail;
2786 }
2787
Jeff Laytond00c28d2010-04-24 07:57:44 -04002788 tcon = tconInfoAlloc();
2789 if (tcon == NULL) {
2790 rc = -ENOMEM;
2791 goto out_fail;
2792 }
2793
Steve French8b217fe2016-11-11 22:36:20 -06002794 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06002795 if (ses->server->vals->protocol_id == 0) {
2796 cifs_dbg(VFS,
2797 "Use SMB2 or later for snapshot mount option\n");
2798 rc = -EOPNOTSUPP;
2799 goto out_fail;
2800 } else
2801 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06002802 }
2803
Jeff Laytond00c28d2010-04-24 07:57:44 -04002804 tcon->ses = ses;
2805 if (volume_info->password) {
2806 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
2807 if (!tcon->password) {
2808 rc = -ENOMEM;
2809 goto out_fail;
2810 }
2811 }
2812
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002813 /*
2814 * BB Do we need to wrap session_mutex around this TCon call and Unix
2815 * SetFS as we do on SessSetup and reconnect?
2816 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002817 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002818 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
2819 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002820 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05002821 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002822 if (rc)
2823 goto out_fail;
2824
2825 if (volume_info->nodfs) {
2826 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
Joe Perchesf96637b2013-05-04 22:12:25 -05002827 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002828 }
Steve Frenchb618f002015-11-03 09:15:03 -06002829 tcon->use_persistent = false;
2830 /* check if SMB2 or later, CIFS does not support persistent handles */
2831 if (volume_info->persistent) {
2832 if (ses->server->vals->protocol_id == 0) {
2833 cifs_dbg(VFS,
2834 "SMB3 or later required for persistent handles\n");
2835 rc = -EOPNOTSUPP;
2836 goto out_fail;
2837 } else if (ses->server->capabilities &
2838 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2839 tcon->use_persistent = true;
2840 else /* persistent handles requested but not supported */ {
2841 cifs_dbg(VFS,
2842 "Persistent handles not supported on share\n");
2843 rc = -EOPNOTSUPP;
2844 goto out_fail;
2845 }
2846 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2847 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2848 && (volume_info->nopersistent == false)) {
2849 cifs_dbg(FYI, "enabling persistent handles\n");
2850 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06002851 } else if (volume_info->resilient) {
2852 if (ses->server->vals->protocol_id == 0) {
2853 cifs_dbg(VFS,
2854 "SMB2.1 or later required for resilient handles\n");
2855 rc = -EOPNOTSUPP;
2856 goto out_fail;
2857 }
2858 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06002859 }
2860
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002861 if (volume_info->seal) {
2862 if (ses->server->vals->protocol_id == 0) {
2863 cifs_dbg(VFS,
2864 "SMB3 or later required for encryption\n");
2865 rc = -EOPNOTSUPP;
2866 goto out_fail;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002867 } else if (tcon->ses->server->capabilities &
2868 SMB2_GLOBAL_CAP_ENCRYPTION)
2869 tcon->seal = true;
2870 else {
2871 cifs_dbg(VFS, "Encryption is not supported on share\n");
2872 rc = -EOPNOTSUPP;
2873 goto out_fail;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002874 }
2875 }
2876
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002877 /*
2878 * We can have only one retry value for a connection to a share so for
2879 * resources mounted more than once to the same server share the last
2880 * value passed in for the retry flag is used.
2881 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04002882 tcon->retry = volume_info->retry;
2883 tcon->nocase = volume_info->nocase;
2884 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07002885 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002886
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302887 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002888 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302889 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002890
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302891 cifs_fscache_get_super_cookie(tcon);
2892
Jeff Laytond00c28d2010-04-24 07:57:44 -04002893 return tcon;
2894
2895out_fail:
2896 tconInfoFree(tcon);
2897 return ERR_PTR(rc);
2898}
2899
Jeff Layton9d002df2010-10-06 19:51:11 -04002900void
2901cifs_put_tlink(struct tcon_link *tlink)
2902{
2903 if (!tlink || IS_ERR(tlink))
2904 return;
2905
2906 if (!atomic_dec_and_test(&tlink->tl_count) ||
2907 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2908 tlink->tl_time = jiffies;
2909 return;
2910 }
2911
2912 if (!IS_ERR(tlink_tcon(tlink)))
2913 cifs_put_tcon(tlink_tcon(tlink));
2914 kfree(tlink);
2915 return;
2916}
Jeff Laytond00c28d2010-04-24 07:57:44 -04002917
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002918static inline struct tcon_link *
Pavel Shilovskycd518752011-06-09 12:58:53 +04002919cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
2920{
2921 return cifs_sb->master_tlink;
2922}
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002923
2924static int
2925compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2926{
2927 struct cifs_sb_info *old = CIFS_SB(sb);
2928 struct cifs_sb_info *new = mnt_data->cifs_sb;
2929
2930 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2931 return 0;
2932
2933 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
2934 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
2935 return 0;
2936
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002937 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002938 * We want to share sb only if we don't specify an r/wsize or
2939 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002940 */
2941 if (new->wsize && new->wsize < old->wsize)
2942 return 0;
2943
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002944 if (new->rsize && new->rsize < old->rsize)
2945 return 0;
2946
Eric W. Biederman1f682332013-02-06 01:20:20 -08002947 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002948 return 0;
2949
2950 if (old->mnt_file_mode != new->mnt_file_mode ||
2951 old->mnt_dir_mode != new->mnt_dir_mode)
2952 return 0;
2953
2954 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2955 return 0;
2956
2957 if (old->actimeo != new->actimeo)
2958 return 0;
2959
2960 return 1;
2961}
2962
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002963static int
2964match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2965{
2966 struct cifs_sb_info *old = CIFS_SB(sb);
2967 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002968 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
2969 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002970
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002971 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002972 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002973 else if (!old_set && !new_set)
2974 return 1;
2975
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002976 return 0;
2977}
2978
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002979int
2980cifs_match_super(struct super_block *sb, void *data)
2981{
2982 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
2983 struct smb_vol *volume_info;
2984 struct cifs_sb_info *cifs_sb;
2985 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00002986 struct cifs_ses *ses;
2987 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002988 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002989 int rc = 0;
2990
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002991 spin_lock(&cifs_tcp_ses_lock);
2992 cifs_sb = CIFS_SB(sb);
2993 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2994 if (IS_ERR(tlink)) {
2995 spin_unlock(&cifs_tcp_ses_lock);
2996 return rc;
2997 }
2998 tcon = tlink_tcon(tlink);
2999 ses = tcon->ses;
3000 tcp_srv = ses->server;
3001
3002 volume_info = mnt_data->vol;
3003
Jeff Layton9fa114f2012-11-26 11:09:57 -05003004 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003005 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003006 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003007 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003008 rc = 0;
3009 goto out;
3010 }
3011
3012 rc = compare_mount_options(sb, mnt_data);
3013out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003014 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003015 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003016 return rc;
3017}
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003020get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003021 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3022 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 char *temp_unc;
3025 int rc = 0;
3026
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003027 if (!ses->server->ops->tree_connect || !ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003028 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003030 *num_referrals = 0;
3031 *referrals = NULL;
3032
3033 if (ses->ipc_tid == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 temp_unc = kmalloc(2 /* for slashes */ +
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003035 strnlen(ses->serverName, SERVER_NAME_LEN_WITH_NULL * 2)
3036 + 1 + 4 /* slash IPC$ */ + 2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 if (temp_unc == NULL)
3038 return -ENOMEM;
3039 temp_unc[0] = '\\';
3040 temp_unc[1] = '\\';
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003041 strcpy(temp_unc + 2, ses->serverName);
3042 strcpy(temp_unc + 2 + strlen(ses->serverName), "\\IPC$");
3043 rc = ses->server->ops->tree_connect(xid, ses, temp_unc, NULL,
3044 nls_codepage);
Joe Perchesf96637b2013-05-04 22:12:25 -05003045 cifs_dbg(FYI, "Tcon rc = %d ipc_tid = %d\n", rc, ses->ipc_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 kfree(temp_unc);
3047 }
3048 if (rc == 0)
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003049 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3050 referrals, num_referrals,
3051 nls_codepage, remap);
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003052 /*
3053 * BB - map targetUNCs to dfs_info3 structures, here or in
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003054 * ses->server->ops->get_dfs_refer.
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003055 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057 return rc;
3058}
3059
Jeff Layton09e50d52008-07-23 10:11:19 -04003060#ifdef CONFIG_DEBUG_LOCK_ALLOC
3061static struct lock_class_key cifs_key[2];
3062static struct lock_class_key cifs_slock_key[2];
3063
3064static inline void
3065cifs_reclassify_socket4(struct socket *sock)
3066{
3067 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003068 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003069 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3070 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3071}
3072
3073static inline void
3074cifs_reclassify_socket6(struct socket *sock)
3075{
3076 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003077 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003078 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3079 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3080}
3081#else
3082static inline void
3083cifs_reclassify_socket4(struct socket *sock)
3084{
3085}
3086
3087static inline void
3088cifs_reclassify_socket6(struct socket *sock)
3089{
3090}
3091#endif
3092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003094static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
Steve French50c2f752007-07-13 00:33:32 +00003096 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Steve French50c2f752007-07-13 00:33:32 +00003098 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 /* mask a nibble at a time and encode */
3100 target[j] = 'A' + (0x0F & (source[i] >> 4));
3101 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003102 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104
3105}
3106
Ben Greear3eb9a882010-09-01 17:06:02 -07003107static int
3108bind_socket(struct TCP_Server_Info *server)
3109{
3110 int rc = 0;
3111 if (server->srcaddr.ss_family != AF_UNSPEC) {
3112 /* Bind to the specified local IP address */
3113 struct socket *socket = server->ssocket;
3114 rc = socket->ops->bind(socket,
3115 (struct sockaddr *) &server->srcaddr,
3116 sizeof(server->srcaddr));
3117 if (rc < 0) {
3118 struct sockaddr_in *saddr4;
3119 struct sockaddr_in6 *saddr6;
3120 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3121 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3122 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003123 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3124 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003125 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003126 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3127 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003128 }
3129 }
3130 return rc;
3131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003134ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
3136 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003137 /*
3138 * some servers require RFC1001 sessinit before sending
3139 * negprot - BB check reconnection in case where second
3140 * sessinit is sent but no second negprot
3141 */
3142 struct rfc1002_session_packet *ses_init_buf;
3143 struct smb_hdr *smb_buf;
3144 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3145 GFP_KERNEL);
3146 if (ses_init_buf) {
3147 ses_init_buf->trailer.session_req.called_len = 32;
3148
Colin Ian King997152f2016-01-25 16:25:54 +00003149 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003150 rfc1002mangle(ses_init_buf->trailer.
3151 session_req.called_name,
3152 server->server_RFC1001_name,
3153 RFC1001_NAME_LEN_WITH_NULL);
3154 else
3155 rfc1002mangle(ses_init_buf->trailer.
3156 session_req.called_name,
3157 DEFAULT_CIFS_CALLED_NAME,
3158 RFC1001_NAME_LEN_WITH_NULL);
3159
3160 ses_init_buf->trailer.session_req.calling_len = 32;
3161
3162 /*
3163 * calling name ends in null (byte 16) from old smb
3164 * convention.
3165 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003166 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003167 rfc1002mangle(ses_init_buf->trailer.
3168 session_req.calling_name,
3169 server->workstation_RFC1001_name,
3170 RFC1001_NAME_LEN_WITH_NULL);
3171 else
3172 rfc1002mangle(ses_init_buf->trailer.
3173 session_req.calling_name,
3174 "LINUX_CIFS_CLNT",
3175 RFC1001_NAME_LEN_WITH_NULL);
3176
3177 ses_init_buf->trailer.session_req.scope1 = 0;
3178 ses_init_buf->trailer.session_req.scope2 = 0;
3179 smb_buf = (struct smb_hdr *)ses_init_buf;
3180
3181 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003182 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003183 rc = smb_send(server, smb_buf, 0x44);
3184 kfree(ses_init_buf);
3185 /*
3186 * RFC1001 layer in at least one server
3187 * requires very short break before negprot
3188 * presumably because not expecting negprot
3189 * to follow so fast. This is a simple
3190 * solution that works without
3191 * complicating the code and causes no
3192 * significant slowing down on mount
3193 * for everyone else
3194 */
3195 usleep_range(1000, 2000);
3196 }
3197 /*
3198 * else the negprot may still work without this
3199 * even though malloc failed
3200 */
3201
3202 return rc;
3203}
3204
3205static int
3206generic_ip_connect(struct TCP_Server_Info *server)
3207{
3208 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003209 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003210 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003211 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003212 struct sockaddr *saddr;
3213
3214 saddr = (struct sockaddr *) &server->dstaddr;
3215
3216 if (server->dstaddr.ss_family == AF_INET6) {
3217 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3218 slen = sizeof(struct sockaddr_in6);
3219 sfamily = AF_INET6;
3220 } else {
3221 sport = ((struct sockaddr_in *) saddr)->sin_port;
3222 slen = sizeof(struct sockaddr_in);
3223 sfamily = AF_INET;
3224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003226 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003227 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3228 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003230 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003231 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003234
3235 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003236 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003237 server->ssocket = socket;
3238 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003239 if (sfamily == AF_INET6)
3240 cifs_reclassify_socket6(socket);
3241 else
3242 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
3244
Ben Greear3eb9a882010-09-01 17:06:02 -07003245 rc = bind_socket(server);
3246 if (rc < 0)
3247 return rc;
3248
Jeff Laytond5c56052008-12-01 18:42:33 -05003249 /*
3250 * Eventually check for other socket options to change from
3251 * the default. sock_setsockopt not used because it expects
3252 * user space buffer
3253 */
3254 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003255 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003256
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003257 /* make the bufsizes depend on wsize/rsize and max requests */
3258 if (server->noautotune) {
3259 if (socket->sk->sk_sndbuf < (200 * 1024))
3260 socket->sk->sk_sndbuf = 200 * 1024;
3261 if (socket->sk->sk_rcvbuf < (140 * 1024))
3262 socket->sk->sk_rcvbuf = 140 * 1024;
3263 }
3264
Steve French6a5fa2362010-01-01 01:28:43 +00003265 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003266 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003267 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3268 (char *)&val, sizeof(val));
3269 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003270 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3271 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003272 }
3273
Joe Perchesf96637b2013-05-04 22:12:25 -05003274 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003275 socket->sk->sk_sndbuf,
3276 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3277
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003278 rc = socket->ops->connect(socket, saddr, slen, 0);
3279 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003280 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003281 sock_release(socket);
3282 server->ssocket = NULL;
3283 return rc;
3284 }
3285
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003286 if (sport == htons(RFC1001_PORT))
3287 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 return rc;
3290}
3291
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003292static int
3293ip_connect(struct TCP_Server_Info *server)
3294{
Steve French6da97912011-03-13 18:55:55 +00003295 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003296 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3297 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3298
3299 if (server->dstaddr.ss_family == AF_INET6)
3300 sport = &addr6->sin6_port;
3301 else
3302 sport = &addr->sin_port;
3303
3304 if (*sport == 0) {
3305 int rc;
3306
3307 /* try with 445 port at first */
3308 *sport = htons(CIFS_PORT);
3309
3310 rc = generic_ip_connect(server);
3311 if (rc >= 0)
3312 return rc;
3313
3314 /* if it failed, try with 139 port */
3315 *sport = htons(RFC1001_PORT);
3316 }
3317
3318 return generic_ip_connect(server);
3319}
3320
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003321void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003322 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003323{
3324 /* if we are reconnecting then should we check to see if
3325 * any requested capabilities changed locally e.g. via
3326 * remount but we can not do much about it here
3327 * if they have (even if we could detect it by the following)
3328 * Perhaps we could add a backpointer to array of sb from tcon
3329 * or if we change to make all sb to same share the same
3330 * sb as NFS - then we only have one backpointer to sb.
3331 * What if we wanted to mount the server share twice once with
3332 * and once without posixacls or posix paths? */
3333 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003334
Steve Frenchc18c8422007-07-18 23:21:09 +00003335 if (vol_info && vol_info->no_linux_ext) {
3336 tcon->fsUnixInfo.Capability = 0;
3337 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003338 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003339 return;
3340 } else if (vol_info)
3341 tcon->unix_ext = 1; /* Unix Extensions supported */
3342
3343 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003344 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003345 return;
3346 }
Steve French50c2f752007-07-13 00:33:32 +00003347
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003348 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003349 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003350 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003351 /* check for reconnect case in which we do not
3352 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003353 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003354 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003355 originally at mount time */
3356 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3357 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003358 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3359 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003360 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003361 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003362 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003363 cifs_dbg(VFS, "possible reconnect error\n");
3364 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003365 }
Steve French8af18972007-02-14 04:42:51 +00003366 }
Steve French50c2f752007-07-13 00:33:32 +00003367
Steve French6848b732011-05-26 18:38:54 +00003368 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003369 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003370
Steve French8af18972007-02-14 04:42:51 +00003371 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003372 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003373 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003374 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003375 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003376 if (cifs_sb)
3377 cifs_sb->mnt_cifs_flags |=
3378 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003379 }
3380
Steve French75865f8c2007-06-24 18:30:48 +00003381 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003382 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003383 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003384 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003385 if (cifs_sb)
3386 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003387 CIFS_MOUNT_POSIX_PATHS;
3388 }
Steve French50c2f752007-07-13 00:33:32 +00003389
Joe Perchesf96637b2013-05-04 22:12:25 -05003390 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003391#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003392 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003393 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003394 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003395 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003396 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003397 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003398 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003399 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003400 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003401 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003402 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003403 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003404 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003405 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003406 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003407 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003408 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003409 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003410#endif /* CIFS_DEBUG2 */
3411 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003412 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003413 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003414 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003415 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 +00003416
Steve French8af18972007-02-14 04:42:51 +00003417 }
3418 }
3419}
3420
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003421int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003422 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003423{
Jeff Layton2de970f2010-10-06 19:51:12 -04003424 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3425
Al Viro2ced6f62011-06-17 09:20:04 -04003426 spin_lock_init(&cifs_sb->tlink_tree_lock);
3427 cifs_sb->tlink_tree = RB_ROOT;
3428
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003429 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003430 * Temporarily set r/wsize for matching superblock. If we end up using
3431 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003432 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003433 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003434 cifs_sb->wsize = pvolume_info->wsize;
3435
Steve French3b795212008-11-13 19:45:32 +00003436 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3437 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3438 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3439 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003440 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3441 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003442
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303443 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003444 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303445
Steve French3b795212008-11-13 19:45:32 +00003446 if (pvolume_info->noperm)
3447 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3448 if (pvolume_info->setuids)
3449 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003450 if (pvolume_info->setuidfromacl)
3451 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003452 if (pvolume_info->server_ino)
3453 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3454 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003455 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3456 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003457 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3458 if (pvolume_info->no_xattr)
3459 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3460 if (pvolume_info->sfu_emul)
3461 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3462 if (pvolume_info->nobrl)
3463 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve Frenchbe652442009-02-23 15:21:59 +00003464 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003465 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003466 if (pvolume_info->mand_lock)
3467 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003468 if (pvolume_info->rwpidforward)
3469 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003470 if (pvolume_info->cifs_acl)
3471 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003472 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003473 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003474 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3475 }
3476 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003477 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003478 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3479 }
Steve French3b795212008-11-13 19:45:32 +00003480 if (pvolume_info->override_uid)
3481 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3482 if (pvolume_info->override_gid)
3483 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3484 if (pvolume_info->dynperm)
3485 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303486 if (pvolume_info->fsc)
3487 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003488 if (pvolume_info->multiuser)
3489 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3490 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003491 if (pvolume_info->strict_io)
3492 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003493 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003494 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003495 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3496 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003497 if (pvolume_info->mfsymlinks) {
3498 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003499 /*
3500 * Our SFU ("Services for Unix" emulation does not allow
3501 * creating symlinks but does allow reading existing SFU
3502 * symlinks (it does allow both creating and reading SFU
3503 * style mknod and FIFOs though). When "mfsymlinks" and
3504 * "sfu" are both enabled at the same time, it allows
3505 * reading both types of symlinks, but will only create
3506 * them with mfsymlinks format. This allows better
3507 * Apple compatibility (probably better for Samba too)
3508 * while still recognizing old Windows style symlinks.
3509 */
3510 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003511 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003512 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003513 }
Steve French3b795212008-11-13 19:45:32 +00003514
3515 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003516 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003517
3518 if (pvolume_info->prepath) {
3519 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3520 if (cifs_sb->prepath == NULL)
3521 return -ENOMEM;
3522 }
3523
3524 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003525}
3526
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003527static void
3528cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003529{
Sean Finneyb9468452011-04-11 13:19:32 +00003530 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003531 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003532 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003533 kfree(volume_info->domainname);
3534 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003535 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003536}
3537
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003538void
3539cifs_cleanup_volume_info(struct smb_vol *volume_info)
3540{
3541 if (!volume_info)
3542 return;
3543 cleanup_volume_info_contents(volume_info);
3544 kfree(volume_info);
3545}
3546
3547
Steve French2d6d5892009-04-09 00:36:44 +00003548#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003549/*
3550 * cifs_build_path_to_root returns full path to root when we do not have an
3551 * exiting connection (tcon)
3552 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003553static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003554build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003555 const struct cifs_sb_info *cifs_sb)
3556{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003557 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003558 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003559 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003560
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003561 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003562 if (full_path == NULL)
3563 return ERR_PTR(-ENOMEM);
3564
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003565 strncpy(full_path, vol->UNC, unc_len);
3566 pos = full_path + unc_len;
3567
3568 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003569 *pos = CIFS_DIR_SEP(cifs_sb);
3570 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003571 pos += pplen;
3572 }
3573
3574 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003575 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003576 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003577 return full_path;
3578}
Sean Finneydd613942011-04-11 13:19:30 +00003579
3580/*
3581 * Perform a dfs referral query for a share and (optionally) prefix
3582 *
Sean Finney046462a2011-04-11 13:19:33 +00003583 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3584 * to a string containing updated options for the submount. Otherwise it
3585 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003586 *
3587 * Returns the rc from get_dfs_path to the caller, which can be used to
3588 * determine whether there were referrals.
3589 */
3590static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003591expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003592 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003593 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003594{
3595 int rc;
3596 unsigned int num_referrals = 0;
3597 struct dfs_info3_param *referrals = NULL;
3598 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3599
3600 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3601 if (IS_ERR(full_path))
3602 return PTR_ERR(full_path);
3603
3604 /* For DFS paths, skip the first '\' of the UNC */
3605 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3606
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003607 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003608 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003609
3610 if (!rc && num_referrals > 0) {
3611 char *fake_devname = NULL;
3612
3613 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3614 full_path + 1, referrals,
3615 &fake_devname);
3616
3617 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003618
Sean Finneydd613942011-04-11 13:19:30 +00003619 if (IS_ERR(mdata)) {
3620 rc = PTR_ERR(mdata);
3621 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003622 } else {
3623 cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003624 rc = cifs_setup_volume_info(volume_info, mdata,
3625 fake_devname);
Sean Finneydd613942011-04-11 13:19:30 +00003626 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003627 kfree(fake_devname);
3628 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003629 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003630 }
3631 kfree(full_path);
3632 return rc;
3633}
Steve French2d6d5892009-04-09 00:36:44 +00003634#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003635
Jeff Layton04db79b2011-07-06 08:10:38 -04003636static int
3637cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3638 const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003640 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003641
Jeff Layton04db79b2011-07-06 08:10:38 -04003642 if (cifs_parse_mount_options(mount_data, devname, volume_info))
3643 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
Jeff Layton7586b762008-12-01 18:41:49 -05003645 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003646 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05003647 kfree(volume_info->username);
3648 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05003649 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05003651 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003653 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003654 /* In userspace mount helper we can get user name from alternate
3655 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003656 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05003660 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003661 /* load_nls_default cannot return null */
3662 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003664 volume_info->local_nls = load_nls(volume_info->iocharset);
3665 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003666 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003667 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04003668 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
3670 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003671
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003672 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04003673}
3674
3675struct smb_vol *
3676cifs_get_volume_info(char *mount_data, const char *devname)
3677{
3678 int rc;
3679 struct smb_vol *volume_info;
3680
Jeff Layton6ee95422012-11-26 11:09:57 -05003681 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04003682 if (!volume_info)
3683 return ERR_PTR(-ENOMEM);
3684
3685 rc = cifs_setup_volume_info(volume_info, mount_data, devname);
3686 if (rc) {
3687 cifs_cleanup_volume_info(volume_info);
3688 volume_info = ERR_PTR(rc);
3689 }
3690
3691 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003692}
3693
Aurelien Aptela6b50582016-05-25 19:59:09 +02003694static int
3695cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3696 unsigned int xid,
3697 struct cifs_tcon *tcon,
3698 struct cifs_sb_info *cifs_sb,
3699 char *full_path)
3700{
3701 int rc;
3702 char *s;
3703 char sep, tmp;
3704
3705 sep = CIFS_DIR_SEP(cifs_sb);
3706 s = full_path;
3707
3708 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3709 while (rc == 0) {
3710 /* skip separators */
3711 while (*s == sep)
3712 s++;
3713 if (!*s)
3714 break;
3715 /* next separator */
3716 while (*s && *s != sep)
3717 s++;
3718
3719 /*
3720 * temporarily null-terminate the path at the end of
3721 * the current component
3722 */
3723 tmp = *s;
3724 *s = 0;
3725 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3726 full_path);
3727 *s = tmp;
3728 }
3729 return rc;
3730}
3731
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003732int
Al Viro2c6292a2011-06-17 09:05:48 -04003733cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003734{
Jeff Layton1daaae82012-03-21 06:30:40 -04003735 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003736 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003737 struct cifs_ses *ses;
Steve French96daf2b2011-05-27 04:34:02 +00003738 struct cifs_tcon *tcon;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003739 struct TCP_Server_Info *server;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003740 char *full_path;
3741 struct tcon_link *tlink;
3742#ifdef CONFIG_CIFS_DFS_UPCALL
3743 int referral_walks_count = 0;
Jeff Layton20547492011-07-09 12:21:07 -04003744#endif
Al Virodd854462011-06-17 08:24:42 -04003745
Jeff Layton20547492011-07-09 12:21:07 -04003746#ifdef CONFIG_CIFS_DFS_UPCALL
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003747try_mount_again:
3748 /* cleanup activities if we're chasing a referral */
3749 if (referral_walks_count) {
3750 if (tcon)
3751 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003752 else if (ses)
3753 cifs_put_smb_ses(ses);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003754
Sachin Prabhu1dfd18d2015-06-16 16:36:17 +01003755 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3756
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003757 free_xid(xid);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003758 }
3759#endif
Jeff Layton1daaae82012-03-21 06:30:40 -04003760 rc = 0;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003761 tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003762 ses = NULL;
3763 server = NULL;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003764 full_path = NULL;
3765 tlink = NULL;
3766
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003767 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Jeff Layton63c038c2008-12-01 18:41:46 -05003769 /* get a reference to a tcp session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003770 server = cifs_get_tcp_session(volume_info);
3771 if (IS_ERR(server)) {
3772 rc = PTR_ERR(server);
Jeff Layton63c038c2008-12-01 18:41:46 -05003773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 }
Steve French141891f2016-09-23 00:44:16 -05003775 if ((volume_info->max_credits < 20) ||
3776 (volume_info->max_credits > 60000))
3777 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3778 else
3779 server->max_credits = volume_info->max_credits;
Jeff Layton36988c72010-04-24 07:57:43 -04003780 /* get a reference to a SMB session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003781 ses = cifs_get_smb_ses(server, volume_info);
3782 if (IS_ERR(ses)) {
3783 rc = PTR_ERR(ses);
3784 ses = NULL;
Jeff Layton36988c72010-04-24 07:57:43 -04003785 goto mount_fail_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 }
Steve French50c2f752007-07-13 00:33:32 +00003787
Steve Frenchb618f002015-11-03 09:15:03 -06003788 if ((volume_info->persistent == true) && ((ses->server->capabilities &
3789 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
3790 cifs_dbg(VFS, "persistent handles not supported by server\n");
3791 rc = -EOPNOTSUPP;
3792 goto mount_fail_check;
3793 }
Steve French592fafe2015-11-03 10:08:53 -06003794
Jeff Laytond00c28d2010-04-24 07:57:44 -04003795 /* search for existing tcon to this server share */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003796 tcon = cifs_get_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003797 if (IS_ERR(tcon)) {
3798 rc = PTR_ERR(tcon);
3799 tcon = NULL;
Mark Syms40920c22016-11-29 11:36:46 +00003800 if (rc == -EACCES)
3801 goto mount_fail_check;
3802
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003803 goto remote_path_check;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003804 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003805
Steve French6848b732011-05-26 18:38:54 +00003806 /* tell server which Unix caps we support */
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003807 if (cap_unix(tcon->ses)) {
Steve French6848b732011-05-26 18:38:54 +00003808 /* reset of caps checks mount to see if unix extensions
3809 disabled for just this mount */
Al Viro2c6292a2011-06-17 09:05:48 -04003810 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
Steve French6848b732011-05-26 18:38:54 +00003811 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3812 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3813 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3814 rc = -EACCES;
3815 goto mount_fail_check;
3816 }
3817 } else
3818 tcon->unix_ext = 0; /* server does not support them */
3819
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003820 /* do not care if a following call succeed - informational */
3821 if (!tcon->ipc && server->ops->qfs_tcon)
3822 server->ops->qfs_tcon(xid, tcon);
Steve Frenchd82c2df2008-11-15 00:07:26 +00003823
Pavel Shilovsky24985c52012-09-18 16:20:28 -07003824 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
3825 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003826
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003827remote_path_check:
Sean Finneyc1508ca2011-04-11 13:19:31 +00003828#ifdef CONFIG_CIFS_DFS_UPCALL
3829 /*
3830 * Perform an unconditional check for whether there are DFS
3831 * referrals for this path without prefix, to provide support
3832 * for DFS referrals from w2k8 servers which don't seem to respond
3833 * with PATH_NOT_COVERED to requests that include the prefix.
3834 * Chase the referral if found, otherwise continue normally.
3835 */
3836 if (referral_walks_count == 0) {
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003837 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
3838 false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00003839 if (!refrc) {
3840 referral_walks_count++;
3841 goto try_mount_again;
3842 }
3843 }
3844#endif
3845
Steve Frenchf87d39d2011-05-27 03:50:55 +00003846 /* check if a whole path is not remote */
Jeff Layton70945642011-03-14 13:48:08 -04003847 if (!rc && tcon) {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003848 if (!server->ops->is_path_accessible) {
3849 rc = -ENOSYS;
3850 goto mount_fail_check;
3851 }
Steve French6d3ea7e2012-11-28 22:34:41 -06003852 /*
3853 * cifs_build_path_to_root works only when we have a valid tcon
3854 */
Sachin Prabhu374402a2016-12-15 12:31:19 +05303855 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
3856 tcon->Flags & SMB_SHARE_IS_IN_DFS);
Igor Mammedove4cce942009-02-10 14:10:26 +03003857 if (full_path == NULL) {
3858 rc = -ENOMEM;
3859 goto mount_fail_check;
3860 }
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003861 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3862 full_path);
Jeff Layton03ceace2010-12-06 21:07:33 -05003863 if (rc != 0 && rc != -EREMOTE) {
Igor Mammedove4cce942009-02-10 14:10:26 +03003864 kfree(full_path);
3865 goto mount_fail_check;
3866 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02003867
Sachin Prabhud1713562016-09-06 13:22:34 +01003868 if (rc != -EREMOTE) {
3869 rc = cifs_are_all_path_components_accessible(server,
Aurelien Aptela6b50582016-05-25 19:59:09 +02003870 xid, tcon, cifs_sb,
3871 full_path);
Sachin Prabhud1713562016-09-06 13:22:34 +01003872 if (rc != 0) {
3873 cifs_dbg(VFS, "cannot query dirs between root and final path, "
3874 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3875 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3876 rc = 0;
3877 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02003878 }
Igor Mammedove4cce942009-02-10 14:10:26 +03003879 kfree(full_path);
3880 }
3881
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003882 /* get referral if needed */
3883 if (rc == -EREMOTE) {
Steve Frenchd036f502009-04-03 03:12:08 +00003884#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov5c2503a2009-04-21 19:31:05 +04003885 if (referral_walks_count > MAX_NESTED_LINKS) {
3886 /*
3887 * BB: when we implement proper loop detection,
3888 * we will remove this check. But now we need it
3889 * to prevent an indefinite loop if 'DFS tree' is
3890 * misconfigured (i.e. has loops).
3891 */
3892 rc = -ELOOP;
3893 goto mount_fail_check;
3894 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003895
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003896 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
Jeff Layton7b91e262009-07-23 15:22:30 -04003897
Sean Finneydd613942011-04-11 13:19:30 +00003898 if (!rc) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04003899 referral_walks_count++;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003900 goto try_mount_again;
3901 }
Sean Finneydd613942011-04-11 13:19:30 +00003902 goto mount_fail_check;
Steve Frenchd036f502009-04-03 03:12:08 +00003903#else /* No DFS support, return error on mount */
3904 rc = -EOPNOTSUPP;
3905#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003906 }
3907
Jeff Layton9d002df2010-10-06 19:51:11 -04003908 if (rc)
3909 goto mount_fail_check;
3910
3911 /* now, hang the tcon off of the superblock */
3912 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
3913 if (tlink == NULL) {
3914 rc = -ENOMEM;
3915 goto mount_fail_check;
3916 }
3917
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003918 tlink->tl_uid = ses->linux_uid;
Jeff Layton9d002df2010-10-06 19:51:11 -04003919 tlink->tl_tcon = tcon;
3920 tlink->tl_time = jiffies;
3921 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3922 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3923
Jeff Layton413e6612010-10-28 13:33:38 -04003924 cifs_sb->master_tlink = tlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04003925 spin_lock(&cifs_sb->tlink_tree_lock);
3926 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3927 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton413e6612010-10-28 13:33:38 -04003928
Jeff Laytonda472fc2012-03-23 14:40:53 -04003929 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04003930 TLINK_IDLE_EXPIRE);
3931
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003932mount_fail_check:
3933 /* on error free sesinfo and tcon struct if needed */
3934 if (rc) {
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003935 /* If find_unc succeeded then rc == 0 so we can not end */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003936 /* up accidentally freeing someone elses tcon struct */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003937 if (tcon)
3938 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003939 else if (ses)
3940 cifs_put_smb_ses(ses);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003941 else
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003942 cifs_put_tcp_session(server, 0);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003943 }
3944
Jeff Layton70fe7dc2007-11-16 22:21:07 +00003945out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003946 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 return rc;
3948}
3949
Jeff Layton8d1bca32011-06-11 21:17:10 -04003950/*
3951 * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
3952 * pointer may be NULL.
3953 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003955CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00003956 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 const struct nls_table *nls_codepage)
3958{
3959 struct smb_hdr *smb_buffer;
3960 struct smb_hdr *smb_buffer_response;
3961 TCONX_REQ *pSMB;
3962 TCONX_RSP *pSMBr;
3963 unsigned char *bcc_ptr;
3964 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05003965 int length;
3966 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
3968 if (ses == NULL)
3969 return -EIO;
3970
3971 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00003972 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00003974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 smb_buffer_response = smb_buffer;
3976
3977 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3978 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003979
Pavel Shilovsky88257362012-05-23 14:01:59 +04003980 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 smb_buffer->Uid = ses->Suid;
3982 pSMB = (TCONX_REQ *) smb_buffer;
3983 pSMBr = (TCONX_RSP *) smb_buffer_response;
3984
3985 pSMB->AndXCommand = 0xFF;
3986 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 bcc_ptr = &pSMB->Password[0];
Jeff Layton8d1bca32011-06-11 21:17:10 -04003988 if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08003989 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003990 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003991 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003992 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003993 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06003994 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08003995 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
3996 specified as required (when that support is added to
3997 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00003998 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08003999 by Samba (not sure whether other servers allow
4000 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004001#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004002 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004003 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004004 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004005 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004006 SECMODE_PW_ENCRYPT ? true : false,
4007 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004008 else
4009#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004010 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004011 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004012 if (rc) {
4013 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4014 __func__, rc);
4015 cifs_buf_release(smb_buffer);
4016 return rc;
4017 }
Steve Frencheeac8042006-01-13 21:34:58 -08004018
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004019 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004020 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004021 /* must align unicode strings */
4022 *bcc_ptr = 0; /* null byte password */
4023 bcc_ptr++;
4024 }
Steve Frencheeac8042006-01-13 21:34:58 -08004025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
Jeff Layton38d77c52013-05-26 07:01:00 -04004027 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4029
4030 if (ses->capabilities & CAP_STATUS32) {
4031 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4032 }
4033 if (ses->capabilities & CAP_DFS) {
4034 smb_buffer->Flags2 |= SMBFLG2_DFS;
4035 }
4036 if (ses->capabilities & CAP_UNICODE) {
4037 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4038 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004039 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004040 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004041 (/* server len*/ + 256 /* share len */), nls_codepage);
4042 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 bcc_ptr += 2; /* skip trailing null */
4044 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 strcpy(bcc_ptr, tree);
4046 bcc_ptr += strlen(tree) + 1;
4047 }
4048 strcpy(bcc_ptr, "?????");
4049 bcc_ptr += strlen("?????");
4050 bcc_ptr += 1;
4051 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004052 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4053 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 pSMB->ByteCount = cpu_to_le16(count);
4055
Steve French133672e2007-11-13 22:41:37 +00004056 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004057 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 /* above now done in SendReceive */
4060 if ((rc == 0) && (tcon != NULL)) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004061 bool is_unicode;
4062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004064 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 tcon->tid = smb_buffer_response->Tid;
4066 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004067 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004068 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004069 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4070 is_unicode = true;
4071 else
4072 is_unicode = false;
4073
Jeff Laytoncc20c032009-04-30 07:16:21 -04004074
Steve French50c2f752007-07-13 00:33:32 +00004075 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004076 if (length == 3) {
4077 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4078 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004079 cifs_dbg(FYI, "IPC connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004080 tcon->ipc = 1;
4081 }
4082 } else if (length == 2) {
4083 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4084 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004085 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004086 }
4087 }
Steve French50c2f752007-07-13 00:33:32 +00004088 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004089 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004090 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004091
4092 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004093 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004094 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004095 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004096 nls_codepage);
4097
Joe Perchesf96637b2013-05-04 22:12:25 -05004098 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004099
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004100 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004101 (smb_buffer_response->WordCount == 7))
4102 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004103 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4104 else
4105 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004106 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 } else if ((rc == 0) && tcon == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00004108 /* all we need to save for IPC$ connection */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 ses->ipc_tid = smb_buffer_response->Tid;
4110 }
4111
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004112 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 return rc;
4114}
4115
Al Viro2e32cf52013-10-03 12:53:37 -04004116static void delayed_free(struct rcu_head *p)
4117{
4118 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4119 unload_nls(sbi->local_nls);
4120 kfree(sbi);
4121}
4122
Al Viro2a9b9952011-06-17 09:27:16 -04004123void
4124cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
Jeff Laytonb647c352010-10-28 11:16:44 -04004126 struct rb_root *root = &cifs_sb->tlink_tree;
4127 struct rb_node *node;
4128 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Jeff Layton2de970f2010-10-06 19:51:12 -04004130 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4131
Jeff Laytonb647c352010-10-28 11:16:44 -04004132 spin_lock(&cifs_sb->tlink_tree_lock);
4133 while ((node = rb_first(root))) {
4134 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4135 cifs_get_tlink(tlink);
4136 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4137 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004138
Jeff Laytonb647c352010-10-28 11:16:44 -04004139 spin_unlock(&cifs_sb->tlink_tree_lock);
4140 cifs_put_tlink(tlink);
4141 spin_lock(&cifs_sb->tlink_tree_lock);
4142 }
4143 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004144
Al Virod757d712011-06-17 09:42:43 -04004145 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004146 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004147 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004150int
4151cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
4153 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004154 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004156 if (!server->ops->need_neg || !server->ops->negotiate)
4157 return -ENOSYS;
4158
Jeff Layton198b5682010-04-24 07:57:48 -04004159 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004160 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Pavel Shilovsky45275782012-05-17 17:53:29 +04004163 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004164
4165 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004166 if (rc == 0) {
4167 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004168 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004169 server->tcpStatus = CifsGood;
4170 else
4171 rc = -EHOSTDOWN;
4172 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 }
Steve French26b994f2008-08-06 05:11:33 +00004174
Jeff Layton198b5682010-04-24 07:57:48 -04004175 return rc;
4176}
Steve French26b994f2008-08-06 05:11:33 +00004177
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004178int
4179cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4180 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004181{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004182 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004183 struct TCP_Server_Info *server = ses->server;
4184
Jeff Layton198b5682010-04-24 07:57:48 -04004185 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004186 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004187 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004188
Joe Perchesf96637b2013-05-04 22:12:25 -05004189 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004190 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004191
Shu Wangf5c4ba82017-09-08 18:48:33 +08004192 if (ses->auth_key.response) {
4193 cifs_dbg(VFS, "Free previous auth_key.response = %p\n",
4194 ses->auth_key.response);
4195 kfree(ses->auth_key.response);
4196 ses->auth_key.response = NULL;
4197 ses->auth_key.len = 0;
4198 }
4199
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004200 if (server->ops->sess_setup)
4201 rc = server->ops->sess_setup(xid, ses, nls_info);
4202
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004203 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004204 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004205
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return rc;
4207}
4208
Jeff Layton8a8798a2012-01-17 16:09:15 -05004209static int
4210cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4211{
Jeff Layton3f618222013-06-12 19:52:14 -05004212 vol->sectype = ses->sectype;
4213
4214 /* krb5 is special, since we don't need username or pw */
4215 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004216 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004217
4218 return cifs_set_cifscreds(vol, ses);
4219}
4220
Steve French96daf2b2011-05-27 04:34:02 +00004221static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004222cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004223{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004224 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004225 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4226 struct cifs_ses *ses;
4227 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004228 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004229
4230 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004231 if (vol_info == NULL)
4232 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004233
Jeff Layton9d002df2010-10-06 19:51:11 -04004234 vol_info->local_nls = cifs_sb->local_nls;
4235 vol_info->linux_uid = fsuid;
4236 vol_info->cred_uid = fsuid;
4237 vol_info->UNC = master_tcon->treeName;
4238 vol_info->retry = master_tcon->retry;
4239 vol_info->nocase = master_tcon->nocase;
4240 vol_info->local_lease = master_tcon->local_lease;
4241 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004242 vol_info->sectype = master_tcon->ses->sectype;
4243 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004244
Jeff Layton8a8798a2012-01-17 16:09:15 -05004245 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4246 if (rc) {
4247 tcon = ERR_PTR(rc);
4248 goto out;
4249 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004250
4251 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304252 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004253 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304254 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004255
4256 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4257 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004258 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004259 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004260 goto out;
4261 }
4262
4263 tcon = cifs_get_tcon(ses, vol_info);
4264 if (IS_ERR(tcon)) {
4265 cifs_put_smb_ses(ses);
4266 goto out;
4267 }
4268
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004269 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004270 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4271out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004272 kfree(vol_info->username);
4273 kfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004274 kfree(vol_info);
4275
4276 return tcon;
4277}
4278
Steve French96daf2b2011-05-27 04:34:02 +00004279struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004280cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4281{
4282 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4283}
4284
Jeff Laytonb647c352010-10-28 11:16:44 -04004285/* find and return a tlink with given uid */
4286static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004287tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004288{
4289 struct rb_node *node = root->rb_node;
4290 struct tcon_link *tlink;
4291
4292 while (node) {
4293 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4294
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004295 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004296 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004297 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004298 node = node->rb_right;
4299 else
4300 return tlink;
4301 }
4302 return NULL;
4303}
4304
4305/* insert a tcon_link into the tree */
4306static void
4307tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4308{
4309 struct rb_node **new = &(root->rb_node), *parent = NULL;
4310 struct tcon_link *tlink;
4311
4312 while (*new) {
4313 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4314 parent = *new;
4315
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004316 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004317 new = &((*new)->rb_left);
4318 else
4319 new = &((*new)->rb_right);
4320 }
4321
4322 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4323 rb_insert_color(&new_tlink->tl_rbnode, root);
4324}
4325
Jeff Layton9d002df2010-10-06 19:51:11 -04004326/*
4327 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4328 * current task.
4329 *
4330 * If the superblock doesn't refer to a multiuser mount, then just return
4331 * the master tcon for the mount.
4332 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304333 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004334 * exists, then check to see if it's pending construction. If it is then wait
4335 * for construction to complete. Once it's no longer pending, check to see if
4336 * it failed and either return an error or retry construction, depending on
4337 * the timeout.
4338 *
4339 * If one doesn't exist then insert a new tcon_link struct into the tree and
4340 * try to construct a new one.
4341 */
4342struct tcon_link *
4343cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4344{
4345 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004346 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004347 struct tcon_link *tlink, *newtlink;
4348
4349 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4350 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4351
4352 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004353 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004354 if (tlink)
4355 cifs_get_tlink(tlink);
4356 spin_unlock(&cifs_sb->tlink_tree_lock);
4357
4358 if (tlink == NULL) {
4359 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4360 if (newtlink == NULL)
4361 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004362 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004363 newtlink->tl_tcon = ERR_PTR(-EACCES);
4364 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4365 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4366 cifs_get_tlink(newtlink);
4367
Jeff Layton9d002df2010-10-06 19:51:11 -04004368 spin_lock(&cifs_sb->tlink_tree_lock);
4369 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004370 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004371 if (tlink) {
4372 cifs_get_tlink(tlink);
4373 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004374 kfree(newtlink);
4375 goto wait_for_construction;
4376 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004377 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004378 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4379 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004380 } else {
4381wait_for_construction:
4382 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004383 TASK_INTERRUPTIBLE);
4384 if (ret) {
4385 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004386 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004387 }
4388
4389 /* if it's good, return it */
4390 if (!IS_ERR(tlink->tl_tcon))
4391 return tlink;
4392
4393 /* return error if we tried this already recently */
4394 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4395 cifs_put_tlink(tlink);
4396 return ERR_PTR(-EACCES);
4397 }
4398
4399 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4400 goto wait_for_construction;
4401 }
4402
4403 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4404 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4405 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4406
4407 if (IS_ERR(tlink->tl_tcon)) {
4408 cifs_put_tlink(tlink);
4409 return ERR_PTR(-EACCES);
4410 }
4411
4412 return tlink;
4413}
Jeff Layton2de970f2010-10-06 19:51:12 -04004414
4415/*
4416 * periodic workqueue job that scans tcon_tree for a superblock and closes
4417 * out tcons.
4418 */
4419static void
4420cifs_prune_tlinks(struct work_struct *work)
4421{
4422 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4423 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004424 struct rb_root *root = &cifs_sb->tlink_tree;
4425 struct rb_node *node = rb_first(root);
4426 struct rb_node *tmp;
4427 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004428
Jeff Laytonb647c352010-10-28 11:16:44 -04004429 /*
4430 * Because we drop the spinlock in the loop in order to put the tlink
4431 * it's not guarded against removal of links from the tree. The only
4432 * places that remove entries from the tree are this function and
4433 * umounts. Because this function is non-reentrant and is canceled
4434 * before umount can proceed, this is safe.
4435 */
4436 spin_lock(&cifs_sb->tlink_tree_lock);
4437 node = rb_first(root);
4438 while (node != NULL) {
4439 tmp = node;
4440 node = rb_next(tmp);
4441 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4442
4443 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4444 atomic_read(&tlink->tl_count) != 0 ||
4445 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4446 continue;
4447
4448 cifs_get_tlink(tlink);
4449 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4450 rb_erase(tmp, root);
4451
Jeff Layton2de970f2010-10-06 19:51:12 -04004452 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004453 cifs_put_tlink(tlink);
4454 spin_lock(&cifs_sb->tlink_tree_lock);
4455 }
4456 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004457
Jeff Laytonda472fc2012-03-23 14:40:53 -04004458 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004459 TLINK_IDLE_EXPIRE);
4460}