blob: f4d12b79ceed4b37c0eb85c72ce42226290b0269 [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"
Paulo Alcantara1c780222018-11-14 16:24:03 -020059#include "dns_resolve.h"
60#ifdef CONFIG_CIFS_DFS_UPCALL
61#include "dfs_cache.h"
62#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050065extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jeff Layton2de970f2010-10-06 19:51:12 -040067/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040068#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040069#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040070
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040071enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040072 /* Mount options that take no arguments */
73 Opt_user_xattr, Opt_nouser_xattr,
74 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040075 Opt_forcegid, Opt_noforcegid,
Steve French3e7a02d2019-09-11 21:46:20 -050076 Opt_noblocksend, Opt_noautotune, Opt_nolease,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040077 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050078 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040079 Opt_mapchars, Opt_nomapchars, Opt_sfu,
80 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
Steve Frenchb3266142018-05-20 23:41:10 -050081 Opt_noposixpaths, Opt_nounix, Opt_unix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040082 Opt_nocase,
83 Opt_brl, Opt_nobrl,
Steve French3d4ef9a2018-04-25 22:19:09 -050084 Opt_handlecache, Opt_nohandlecache,
Steve French95932652016-09-23 01:36:34 -050085 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040086 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
87 Opt_nohard, Opt_nosoft,
88 Opt_nointr, Opt_intr,
89 Opt_nostrictsync, Opt_strictsync,
90 Opt_serverino, Opt_noserverino,
91 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
92 Opt_acl, Opt_noacl, Opt_locallease,
Steve French4f5c10f2019-09-03 21:18:49 -050093 Opt_sign, Opt_ignore_signature, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040094 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040095 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050096 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060097 Opt_resilient, Opt_noresilient,
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -030098 Opt_domainauto, Opt_rdma, Opt_modesid, Opt_rootfs,
Aurelien Aptelbcc88802019-09-20 04:32:20 +020099 Opt_multichannel, Opt_nomultichannel,
Steve French9fe5ff12019-06-24 20:39:04 -0500100 Opt_compress,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400101
102 /* Mount options which take numeric value */
103 Opt_backupuid, Opt_backupgid, Opt_uid,
104 Opt_cruid, Opt_gid, Opt_file_mode,
105 Opt_dirmode, Opt_port,
Steve French563317e2019-09-08 23:22:02 -0500106 Opt_min_enc_offload,
Steve Frenche8506d22019-02-28 21:32:15 -0600107 Opt_blocksize, Opt_rsize, Opt_wsize, Opt_actimeo,
Steve Frenchca567eb2019-03-29 16:31:07 -0500108 Opt_echo_interval, Opt_max_credits, Opt_handletimeout,
Aurelien Aptelbcc88802019-09-20 04:32:20 +0200109 Opt_snapshot, Opt_max_channels,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400110
111 /* Mount options which take string value */
112 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400113 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400114 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400115 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400116
117 /* Mount options to be ignored */
118 Opt_ignore,
119
120 /* Options which could be blank */
121 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100122 Opt_blank_user,
123 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400124
125 Opt_err
126};
127
128static const match_table_t cifs_mount_option_tokens = {
129
130 { Opt_user_xattr, "user_xattr" },
131 { Opt_nouser_xattr, "nouser_xattr" },
132 { Opt_forceuid, "forceuid" },
133 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400134 { Opt_forcegid, "forcegid" },
135 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400136 { Opt_noblocksend, "noblocksend" },
137 { Opt_noautotune, "noautotune" },
Steve French3e7a02d2019-09-11 21:46:20 -0500138 { Opt_nolease, "nolease" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400139 { Opt_hard, "hard" },
140 { Opt_soft, "soft" },
141 { Opt_perm, "perm" },
142 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500143 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400144 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500145 { Opt_mapposix, "mapposix" }, /* SFM style */
146 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400147 { Opt_sfu, "sfu" },
148 { Opt_nosfu, "nosfu" },
149 { Opt_nodfs, "nodfs" },
150 { Opt_posixpaths, "posixpaths" },
151 { Opt_noposixpaths, "noposixpaths" },
152 { Opt_nounix, "nounix" },
153 { Opt_nounix, "nolinux" },
Steve Frenchb3266142018-05-20 23:41:10 -0500154 { Opt_nounix, "noposix" },
155 { Opt_unix, "unix" },
156 { Opt_unix, "linux" },
157 { Opt_unix, "posix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400158 { Opt_nocase, "nocase" },
159 { Opt_nocase, "ignorecase" },
160 { Opt_brl, "brl" },
161 { Opt_nobrl, "nobrl" },
Steve French3d4ef9a2018-04-25 22:19:09 -0500162 { Opt_handlecache, "handlecache" },
163 { Opt_nohandlecache, "nohandlecache" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400164 { Opt_nobrl, "nolock" },
165 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400166 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400167 { Opt_setuids, "setuids" },
168 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500169 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400170 { Opt_dynperm, "dynperm" },
171 { Opt_nodynperm, "nodynperm" },
172 { Opt_nohard, "nohard" },
173 { Opt_nosoft, "nosoft" },
174 { Opt_nointr, "nointr" },
175 { Opt_intr, "intr" },
176 { Opt_nostrictsync, "nostrictsync" },
177 { Opt_strictsync, "strictsync" },
178 { Opt_serverino, "serverino" },
179 { Opt_noserverino, "noserverino" },
180 { Opt_rwpidforward, "rwpidforward" },
Steve French412094a2019-06-24 02:01:42 -0500181 { Opt_modesid, "modefromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400182 { Opt_cifsacl, "cifsacl" },
183 { Opt_nocifsacl, "nocifsacl" },
184 { Opt_acl, "acl" },
185 { Opt_noacl, "noacl" },
186 { Opt_locallease, "locallease" },
187 { Opt_sign, "sign" },
Steve French4f5c10f2019-09-03 21:18:49 -0500188 { Opt_ignore_signature, "signloosely" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400189 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400190 { Opt_noac, "noac" },
191 { Opt_fsc, "fsc" },
192 { Opt_mfsymlinks, "mfsymlinks" },
193 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400194 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400195 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500196 { Opt_persistent, "persistenthandles"},
197 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600198 { Opt_resilient, "resilienthandles"},
199 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530200 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700201 { Opt_rdma, "rdma"},
Aurelien Aptelbcc88802019-09-20 04:32:20 +0200202 { Opt_multichannel, "multichannel" },
203 { Opt_nomultichannel, "nomultichannel" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400204
205 { Opt_backupuid, "backupuid=%s" },
206 { Opt_backupgid, "backupgid=%s" },
207 { Opt_uid, "uid=%s" },
208 { Opt_cruid, "cruid=%s" },
209 { Opt_gid, "gid=%s" },
210 { Opt_file_mode, "file_mode=%s" },
211 { Opt_dirmode, "dirmode=%s" },
212 { Opt_dirmode, "dir_mode=%s" },
213 { Opt_port, "port=%s" },
Steve French563317e2019-09-08 23:22:02 -0500214 { Opt_min_enc_offload, "esize=%s" },
Steve Frenche8506d22019-02-28 21:32:15 -0600215 { Opt_blocksize, "bsize=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400216 { Opt_rsize, "rsize=%s" },
217 { Opt_wsize, "wsize=%s" },
218 { Opt_actimeo, "actimeo=%s" },
Steve Frenchca567eb2019-03-29 16:31:07 -0500219 { Opt_handletimeout, "handletimeout=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600220 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500221 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600222 { Opt_snapshot, "snapshot=%s" },
Aurelien Aptelbcc88802019-09-20 04:32:20 +0200223 { Opt_max_channels, "max_channels=%s" },
Steve French9fe5ff12019-06-24 20:39:04 -0500224 { Opt_compress, "compress=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400225
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100226 { Opt_blank_user, "user=" },
227 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400228 { Opt_user, "user=%s" },
229 { Opt_user, "username=%s" },
230 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100231 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400232 { Opt_pass, "pass=%s" },
233 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100234 { Opt_blank_ip, "ip=" },
235 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400236 { Opt_ip, "ip=%s" },
237 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400238 { Opt_ignore, "unc=%s" },
239 { Opt_ignore, "target=%s" },
240 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400241 { Opt_domain, "dom=%s" },
242 { Opt_domain, "domain=%s" },
243 { Opt_domain, "workgroup=%s" },
244 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400245 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400246 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400247 { Opt_netbiosname, "netbiosname=%s" },
248 { Opt_servern, "servern=%s" },
249 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400250 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400251 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400252 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400253
254 { Opt_ignore, "cred" },
255 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400256 { Opt_ignore, "cred=%s" },
257 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400258 { Opt_ignore, "guest" },
259 { Opt_ignore, "rw" },
260 { Opt_ignore, "ro" },
261 { Opt_ignore, "suid" },
262 { Opt_ignore, "nosuid" },
263 { Opt_ignore, "exec" },
264 { Opt_ignore, "noexec" },
265 { Opt_ignore, "nodev" },
266 { Opt_ignore, "noauto" },
267 { Opt_ignore, "dev" },
268 { Opt_ignore, "mand" },
269 { Opt_ignore, "nomand" },
Steve French9b9c5be2018-09-22 12:07:06 -0500270 { Opt_ignore, "relatime" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400271 { Opt_ignore, "_netdev" },
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -0300272 { Opt_rootfs, "rootfs" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400273
274 { Opt_err, NULL }
275};
276
277enum {
278 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
279 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400280 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
281 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400282 Opt_sec_none,
283
284 Opt_sec_err
285};
286
287static const match_table_t cifs_secflavor_tokens = {
288 { Opt_sec_krb5, "krb5" },
289 { Opt_sec_krb5i, "krb5i" },
290 { Opt_sec_krb5p, "krb5p" },
291 { Opt_sec_ntlmsspi, "ntlmsspi" },
292 { Opt_sec_ntlmssp, "ntlmssp" },
293 { Opt_ntlm, "ntlm" },
294 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400295 { Opt_sec_ntlmv2, "nontlm" },
296 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400297 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400298 { Opt_sec_lanman, "lanman" },
299 { Opt_sec_none, "none" },
300
301 { Opt_sec_err, NULL }
302};
303
Jeff Layton15b6a472012-05-16 07:50:15 -0400304/* cache flavors */
305enum {
306 Opt_cache_loose,
307 Opt_cache_strict,
308 Opt_cache_none,
Steve French83bbfa72019-08-27 23:58:54 -0500309 Opt_cache_ro,
Steve French41e033f2019-08-30 02:12:41 -0500310 Opt_cache_rw,
Jeff Layton15b6a472012-05-16 07:50:15 -0400311 Opt_cache_err
312};
313
314static const match_table_t cifs_cacheflavor_tokens = {
315 { Opt_cache_loose, "loose" },
316 { Opt_cache_strict, "strict" },
317 { Opt_cache_none, "none" },
Steve French83bbfa72019-08-27 23:58:54 -0500318 { Opt_cache_ro, "ro" },
Steve French41e033f2019-08-30 02:12:41 -0500319 { Opt_cache_rw, "singleclient" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400320 { Opt_cache_err, NULL }
321};
322
Jeff Layton23db65f2012-05-15 12:20:51 -0400323static const match_table_t cifs_smb_version_tokens = {
324 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600325 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000326 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500327 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500328 { Smb_302, SMB302_VERSION_STRING },
Kenneth D'souza4a3b38a2018-11-17 10:33:30 +0530329 { Smb_302, ALT_SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600330 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500331 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French9764c022017-09-17 10:41:35 -0500332 { Smb_3any, SMB3ANY_VERSION_STRING },
333 { Smb_default, SMBDEFAULT_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600334 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400335};
336
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300337static int ip_connect(struct TCP_Server_Info *server);
338static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400339static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400340static void cifs_prune_tlinks(struct work_struct *work);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200341static char *extract_hostname(const char *unc);
342
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200343/*
344 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
345 * get their ip addresses changed at some point.
346 *
347 * This should be called with server->srv_mutex held.
348 */
349#ifdef CONFIG_CIFS_DFS_UPCALL
350static int reconn_set_ipaddr(struct TCP_Server_Info *server)
351{
352 int rc;
353 int len;
354 char *unc, *ipaddr = NULL;
355
356 if (!server->hostname)
357 return -EINVAL;
358
359 len = strlen(server->hostname) + 3;
360
361 unc = kmalloc(len, GFP_KERNEL);
362 if (!unc) {
363 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
364 return -ENOMEM;
365 }
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000366 scnprintf(unc, len, "\\\\%s", server->hostname);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200367
368 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
369 kfree(unc);
370
371 if (rc < 0) {
372 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
373 __func__, server->hostname, rc);
374 return rc;
375 }
376
377 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
378 strlen(ipaddr));
379 kfree(ipaddr);
380
381 return !rc ? -1 : 0;
382}
383#else
384static inline int reconn_set_ipaddr(struct TCP_Server_Info *server)
385{
386 return 0;
387}
388#endif
389
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200390#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200391/* These functions must be called with server->srv_mutex held */
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200392static void reconn_inval_dfs_target(struct TCP_Server_Info *server,
393 struct cifs_sb_info *cifs_sb,
394 struct dfs_cache_tgt_list *tgt_list,
395 struct dfs_cache_tgt_iterator **tgt_it)
396{
397 const char *name;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200398
399 if (!cifs_sb || !cifs_sb->origin_fullpath || !tgt_list ||
400 !server->nr_targets)
401 return;
402
403 if (!*tgt_it) {
404 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
405 } else {
406 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
407 if (!*tgt_it)
408 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
409 }
410
411 cifs_dbg(FYI, "%s: UNC: %s\n", __func__, cifs_sb->origin_fullpath);
412
413 name = dfs_cache_get_tgt_name(*tgt_it);
414
415 kfree(server->hostname);
416
417 server->hostname = extract_hostname(name);
Dan Carpenter84288172019-01-05 15:25:29 +0300418 if (IS_ERR(server->hostname)) {
419 cifs_dbg(FYI,
420 "%s: failed to extract hostname from target: %ld\n",
421 __func__, PTR_ERR(server->hostname));
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200422 }
423}
424
425static inline int reconn_setup_dfs_targets(struct cifs_sb_info *cifs_sb,
426 struct dfs_cache_tgt_list *tl,
427 struct dfs_cache_tgt_iterator **it)
428{
429 if (!cifs_sb->origin_fullpath)
430 return -EOPNOTSUPP;
431 return dfs_cache_noreq_find(cifs_sb->origin_fullpath + 1, NULL, tl);
432}
433#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Jeff Laytond5c56052008-12-01 18:42:33 -0500435/*
436 * cifs tcp session reconnection
437 *
438 * mark tcp session as reconnecting so temporarily locked
439 * mark all smb sessions as reconnecting for tcp session
440 * reconnect tcp session
441 * wake up waiters on reconnection? - (not needed currently)
442 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400443int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444cifs_reconnect(struct TCP_Server_Info *server)
445{
446 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500447 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000448 struct cifs_ses *ses;
449 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000450 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400451 struct list_head retry_list;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200452#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300453 struct super_block *sb = NULL;
Paulo Alcantara23324402018-11-20 14:37:18 -0200454 struct cifs_sb_info *cifs_sb = NULL;
455 struct dfs_cache_tgt_list tgt_list = {0};
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200456 struct dfs_cache_tgt_iterator *tgt_it = NULL;
457#endif
Steve French50c2f752007-07-13 00:33:32 +0000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 spin_lock(&GlobalMid_Lock);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200460 server->nr_targets = 1;
461#ifdef CONFIG_CIFS_DFS_UPCALL
Ronnie Sahlberg61cabc72019-06-14 13:02:29 +1000462 spin_unlock(&GlobalMid_Lock);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300463 sb = cifs_get_tcp_super(server);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300464 if (IS_ERR(sb)) {
465 rc = PTR_ERR(sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200466 cifs_dbg(FYI, "%s: will not do DFS failover: rc = %d\n",
467 __func__, rc);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300468 sb = NULL;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200469 } else {
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300470 cifs_sb = CIFS_SB(sb);
471
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200472 rc = reconn_setup_dfs_targets(cifs_sb, &tgt_list, &tgt_it);
Steve French55a7f002019-01-01 17:19:45 -0600473 if (rc && (rc != -EOPNOTSUPP)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000474 cifs_server_dbg(VFS, "%s: no target servers for DFS failover\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200475 __func__);
476 } else {
477 server->nr_targets = dfs_cache_get_nr_tgts(&tgt_list);
478 }
479 }
480 cifs_dbg(FYI, "%s: will retry %d target(s)\n", __func__,
481 server->nr_targets);
Ronnie Sahlberg61cabc72019-06-14 13:02:29 +1000482 spin_lock(&GlobalMid_Lock);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200483#endif
Jeff Layton469ee612008-10-16 18:46:39 +0000484 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000485 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 next time through the loop */
487 spin_unlock(&GlobalMid_Lock);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300488#ifdef CONFIG_CIFS_DFS_UPCALL
489 dfs_cache_free_tgts(&tgt_list);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300490 cifs_put_tcp_super(sb);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300491#endif
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600492 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return rc;
494 } else
495 server->tcpStatus = CifsNeedReconnect;
496 spin_unlock(&GlobalMid_Lock);
497 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400498 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Steve French6e4d3bb2018-09-22 11:25:04 -0500500 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500501 trace_smb3_reconnect(server->CurrentMid, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /* before reconnecting the tcp session, mark the smb session (uid)
504 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500505 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
506 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530507 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500508 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000509 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500510 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500511 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000512 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500513 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100515 if (ses->tcon_ipc)
516 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530518 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500521 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500522 mutex_lock(&server->srv_mutex);
Long Li1d2a4f52019-05-13 21:01:28 -0700523 if (server->ssocket) {
524 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
525 server->ssocket->state, server->ssocket->flags);
526 kernel_sock_shutdown(server->ssocket, SHUT_WR);
527 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
528 server->ssocket->state, server->ssocket->flags);
529 sock_release(server->ssocket);
530 server->ssocket = NULL;
531 }
532 server->sequence_number = 0;
533 server->session_estab = false;
534 kfree(server->session_key.response);
535 server->session_key.response = NULL;
536 server->session_key.len = 0;
537 server->lstrp = jiffies;
Long Li214bab42019-04-05 21:36:35 +0000538
539 /* mark submitted MIDs for retry and issue callback */
540 INIT_LIST_HEAD(&retry_list);
541 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
542 spin_lock(&GlobalMid_Lock);
543 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
544 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700545 kref_get(&mid_entry->refcount);
Long Li214bab42019-04-05 21:36:35 +0000546 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
547 mid_entry->mid_state = MID_RETRY_NEEDED;
548 list_move(&mid_entry->qhead, &retry_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700549 mid_entry->mid_flags |= MID_DELETED;
Long Li214bab42019-04-05 21:36:35 +0000550 }
551 spin_unlock(&GlobalMid_Lock);
Long Li1d2a4f52019-05-13 21:01:28 -0700552 mutex_unlock(&server->srv_mutex);
Long Li214bab42019-04-05 21:36:35 +0000553
554 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
555 list_for_each_safe(tmp, tmp2, &retry_list) {
556 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
557 list_del_init(&mid_entry->qhead);
558 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700559 cifs_mid_q_entry_release(mid_entry);
Long Li214bab42019-04-05 21:36:35 +0000560 }
561
Long Li1d2a4f52019-05-13 21:01:28 -0700562 if (cifs_rdma_enabled(server)) {
563 mutex_lock(&server->srv_mutex);
Long Li050b8c32019-04-04 11:35:42 -0500564 smbd_destroy(server);
Long Li1d2a4f52019-05-13 21:01:28 -0700565 mutex_unlock(&server->srv_mutex);
566 }
Jeff Layton3c1105d2011-05-22 07:09:13 -0400567
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400568 do {
Steve French6c3d8902006-07-31 22:46:20 +0000569 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300570
Jeff Layton73e216a2013-09-05 08:38:10 -0400571 mutex_lock(&server->srv_mutex);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200572 /*
573 * Set up next DFS target server (if any) for reconnect. If DFS
574 * feature is disabled, then we will retry last server we
575 * connected to before.
576 */
Long Li781a8052017-11-22 17:38:36 -0700577 if (cifs_rdma_enabled(server))
578 rc = smbd_reconnect(server);
579 else
580 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000581 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500582 cifs_dbg(FYI, "reconnect error %d\n", rc);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200583#ifdef CONFIG_CIFS_DFS_UPCALL
584 reconn_inval_dfs_target(server, cifs_sb, &tgt_list,
585 &tgt_it);
586#endif
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200587 rc = reconn_set_ipaddr(server);
588 if (rc) {
589 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
590 __func__, rc);
591 }
Federico Sauter4afe2602015-03-17 17:45:28 +0100592 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700593 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 } else {
595 atomic_inc(&tcpSesReconnectCount);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800596 set_credits(server, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000598 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000599 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000600 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100601 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400603 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500604
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200605#ifdef CONFIG_CIFS_DFS_UPCALL
606 if (tgt_it) {
607 rc = dfs_cache_noreq_update_tgthint(cifs_sb->origin_fullpath + 1,
608 tgt_it);
609 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000610 cifs_server_dbg(VFS, "%s: failed to update DFS target hint: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200611 __func__, rc);
612 }
613 rc = dfs_cache_update_vol(cifs_sb->origin_fullpath, server);
614 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000615 cifs_server_dbg(VFS, "%s: failed to update vol info in DFS cache: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200616 __func__, rc);
617 }
Paulo Alcantara23324402018-11-20 14:37:18 -0200618 dfs_cache_free_tgts(&tgt_list);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300619
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200620 }
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300621
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300622 cifs_put_tcp_super(sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200623#endif
Sachin Prabhub8c60012016-10-20 19:52:24 -0400624 if (server->tcpStatus == CifsNeedNegotiate)
625 mod_delayed_work(cifsiod_wq, &server->echo, 0);
626
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600627 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return rc;
629}
630
Jeff Laytonc74093b2011-01-11 07:24:23 -0500631static void
632cifs_echo_request(struct work_struct *work)
633{
634 int rc;
635 struct TCP_Server_Info *server = container_of(work,
636 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400637 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500638
Jeff Layton247ec9b2011-02-04 17:09:50 -0500639 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400640 * If we need to renegotiate, set echo interval to zero to
641 * immediately call echo service where we can renegotiate.
642 */
643 if (server->tcpStatus == CifsNeedNegotiate)
644 echo_interval = 0;
645 else
646 echo_interval = server->echo_interval;
647
648 /*
649 * We cannot send an echo if it is disabled.
650 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500651 */
Steve French4fcd1812016-06-22 20:12:05 -0500652
653 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400654 server->tcpStatus == CifsExiting ||
655 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400656 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600657 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500658 goto requeue_echo;
659
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400660 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500661 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500662 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
663 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500664
665requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400666 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500667}
668
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400669static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400670allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400671{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400672 if (!server->bigbuf) {
673 server->bigbuf = (char *)cifs_buf_get();
674 if (!server->bigbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000675 cifs_server_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400676 msleep(3000);
677 /* retry will check if exiting */
678 return false;
679 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400680 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400681 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400682 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400683 }
684
Jeff Layton2a37ef92011-10-19 15:29:23 -0400685 if (!server->smallbuf) {
686 server->smallbuf = (char *)cifs_small_buf_get();
687 if (!server->smallbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000688 cifs_server_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400689 msleep(1000);
690 /* retry will check if exiting */
691 return false;
692 }
693 /* beginning of smb buffer is cleared in our buf_get */
694 } else {
695 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400696 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400697 }
698
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400699 return true;
700}
701
Jeff Laytonba749e62011-10-11 06:41:32 -0400702static bool
703server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400704{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300705 /*
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000706 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300707 * situations right:
708 * 1s client sends a normal SMB request
Ronnie Sahlbergbecc2ba2019-07-24 11:43:49 +1000709 * 2s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300710 * 30s echo workqueue job pops, and decides we got a response recently
711 * and don't need to send another
712 * ...
713 * 65s kernel_recvmsg times out, and we see that we haven't gotten
714 * a response in >60s.
715 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200716 if ((server->tcpStatus == CifsGood ||
717 server->tcpStatus == CifsNeedNegotiate) &&
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000718 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000719 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
720 (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400721 cifs_reconnect(server);
Jeff Laytonba749e62011-10-11 06:41:32 -0400722 return true;
723 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400724
Jeff Laytonba749e62011-10-11 06:41:32 -0400725 return false;
726}
727
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800728static inline bool
729zero_credits(struct TCP_Server_Info *server)
730{
731 int val;
732
733 spin_lock(&server->req_lock);
734 val = server->credits + server->echo_credits + server->oplock_credits;
735 if (server->in_flight == 0 && val == 0) {
736 spin_unlock(&server->req_lock);
737 return true;
738 }
739 spin_unlock(&server->req_lock);
740 return false;
741}
742
Al Viro71335662016-01-09 19:54:50 -0500743static int
744cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400745{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400746 int length = 0;
747 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400748
Al Viro71335662016-01-09 19:54:50 -0500749 smb_msg->msg_control = NULL;
750 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400751
Al Viro71335662016-01-09 19:54:50 -0500752 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500753 try_to_freeze();
754
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800755 /* reconnect if no credits and no requests in flight */
756 if (zero_credits(server)) {
757 cifs_reconnect(server);
758 return -ECONNABORTED;
759 }
760
Al Viro71335662016-01-09 19:54:50 -0500761 if (server_unresponsive(server))
762 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700763 if (cifs_rdma_enabled(server) && server->smbd_conn)
764 length = smbd_recv(server->smbd_conn, smb_msg);
765 else
766 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500767
768 if (server->tcpStatus == CifsExiting)
769 return -ESHUTDOWN;
770
771 if (server->tcpStatus == CifsNeedReconnect) {
772 cifs_reconnect(server);
773 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400774 }
775
Al Viro71335662016-01-09 19:54:50 -0500776 if (length == -ERESTARTSYS ||
777 length == -EAGAIN ||
778 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400779 /*
780 * Minimum sleep to prevent looping, allowing socket
781 * to clear and app threads to set tcpStatus
782 * CifsNeedReconnect if server hung.
783 */
784 usleep_range(1000, 2000);
785 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400786 continue;
Al Viro71335662016-01-09 19:54:50 -0500787 }
788
789 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500790 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400791 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500792 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400793 }
794 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400795 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400796}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400797
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400798int
799cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
800 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400801{
Al Viro71335662016-01-09 19:54:50 -0500802 struct msghdr smb_msg;
803 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100804 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400805
Al Viro71335662016-01-09 19:54:50 -0500806 return cifs_readv_from_socket(server, &smb_msg);
807}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400808
Al Viro71335662016-01-09 19:54:50 -0500809int
810cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700811 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500812{
813 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700814 struct bio_vec bv = {
815 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100816 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500817 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400818}
819
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400820static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400821is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400822{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400823 /*
824 * The first byte big endian of the length field,
825 * is actually not part of the length but the type
826 * with the most common, zero, as regular data.
827 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400828 switch (type) {
829 case RFC1002_SESSION_MESSAGE:
830 /* Regular SMB response */
831 return true;
832 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500833 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400834 break;
835 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500836 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400837 break;
838 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400839 /*
840 * We get this from Windows 98 instead of an error on
841 * SMB negprot response.
842 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500843 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400844 /* give server a second to clean up */
845 msleep(1000);
846 /*
847 * Always try 445 first on reconnect since we get NACK
848 * on some if we ever connected to port 139 (the NACK
849 * is since we do not begin with RFC1001 session
850 * initialize frame).
851 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400852 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400853 cifs_reconnect(server);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400854 break;
855 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000856 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400857 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400858 }
859
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400860 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400861}
862
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400863void
864dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400865{
866#ifdef CONFIG_CIFS_STATS2
867 mid->when_received = jiffies;
868#endif
869 spin_lock(&GlobalMid_Lock);
870 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400871 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400872 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400873 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000874 /*
875 * Trying to handle/dequeue a mid after the send_recv()
876 * function has finished processing it is a bug.
877 */
878 if (mid->mid_flags & MID_DELETED)
879 printk_once(KERN_WARNING
880 "trying to dequeue a deleted mid\n");
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700881 else {
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000882 list_del_init(&mid->qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700883 mid->mid_flags |= MID_DELETED;
884 }
Jeff Laytonea1f4502011-10-19 15:29:05 -0400885 spin_unlock(&GlobalMid_Lock);
886}
887
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800888static unsigned int
889smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
890{
891 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
892
893 /*
894 * SMB1 does not use credits.
895 */
896 if (server->vals->header_preamble_size)
897 return 0;
898
899 return le16_to_cpu(shdr->CreditRequest);
900}
901
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400902static void
903handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400904 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400905{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400906 if (server->ops->check_trans2 &&
907 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400908 return;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800909 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400910 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400911 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400912 /* Was previous buf put in mpx struct for multi-rsp? */
913 if (!mid->multiRsp) {
914 /* smb buffer will be freed by user thread */
915 if (server->large_buf)
916 server->bigbuf = NULL;
917 else
918 server->smallbuf = NULL;
919 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400920 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400921}
922
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400923static void clean_demultiplex_info(struct TCP_Server_Info *server)
924{
925 int length;
926
927 /* take it off the list, if it's not already */
928 spin_lock(&cifs_tcp_ses_lock);
929 list_del_init(&server->tcp_ses_list);
930 spin_unlock(&cifs_tcp_ses_lock);
931
932 spin_lock(&GlobalMid_Lock);
933 server->tcpStatus = CifsExiting;
934 spin_unlock(&GlobalMid_Lock);
935 wake_up_all(&server->response_q);
936
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400937 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300938 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400939 if (server->credits <= 0)
940 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300941 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400942 /*
943 * Although there should not be any requests blocked on this queue it
944 * can not hurt to be paranoid and try to wake up requests that may
945 * haven been blocked when more than 50 at time were on the wire to the
946 * same server - they now will see the session is in exit state and get
947 * out of SendReceive.
948 */
949 wake_up_all(&server->request_q);
950 /* give those requests time to exit */
951 msleep(125);
Long Li050b8c32019-04-04 11:35:42 -0500952 if (cifs_rdma_enabled(server))
953 smbd_destroy(server);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400954 if (server->ssocket) {
955 sock_release(server->ssocket);
956 server->ssocket = NULL;
957 }
958
959 if (!list_empty(&server->pending_mid_q)) {
960 struct list_head dispose_list;
961 struct mid_q_entry *mid_entry;
962 struct list_head *tmp, *tmp2;
963
964 INIT_LIST_HEAD(&dispose_list);
965 spin_lock(&GlobalMid_Lock);
966 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
967 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500968 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700969 kref_get(&mid_entry->refcount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400970 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400971 list_move(&mid_entry->qhead, &dispose_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700972 mid_entry->mid_flags |= MID_DELETED;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400973 }
974 spin_unlock(&GlobalMid_Lock);
975
976 /* now walk dispose list and issue callbacks */
977 list_for_each_safe(tmp, tmp2, &dispose_list) {
978 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500979 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400980 list_del_init(&mid_entry->qhead);
981 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700982 cifs_mid_q_entry_release(mid_entry);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400983 }
984 /* 1/8th of sec is more than enough time for them to exit */
985 msleep(125);
986 }
987
988 if (!list_empty(&server->pending_mid_q)) {
989 /*
990 * mpx threads have not exited yet give them at least the smb
991 * send timeout time for long ops.
992 *
993 * Due to delays on oplock break requests, we need to wait at
994 * least 45 seconds before giving up on a request getting a
995 * response and going ahead and killing cifsd.
996 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500997 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400998 msleep(46000);
999 /*
1000 * If threads still have not exited they are probably never
1001 * coming home not much else we can do but free the memory.
1002 */
1003 }
1004
1005 kfree(server->hostname);
1006 kfree(server);
1007
1008 length = atomic_dec_return(&tcpSesAllocCount);
1009 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -07001010 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001011}
1012
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001013static int
Jeff Laytone9097ab2011-10-19 15:29:40 -04001014standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1015{
1016 int length;
1017 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10001018 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001019
1020 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001021 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
1022 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001023 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001024 cifs_reconnect(server);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +04001025 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001026 }
1027
1028 /* switch to large buffer if too big for a small one */
1029 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
1030 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001031 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001032 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001033 }
1034
1035 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001036 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001037 pdu_length - HEADER_SIZE(server) + 1
1038 + server->vals->header_preamble_size);
1039
Jeff Laytone9097ab2011-10-19 15:29:40 -04001040 if (length < 0)
1041 return length;
1042 server->total_read += length;
1043
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001044 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001045
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08001046 return cifs_handle_standard(server, mid);
1047}
1048
1049int
1050cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1051{
1052 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
1053 int length;
1054
Jeff Laytone9097ab2011-10-19 15:29:40 -04001055 /*
1056 * We know that we received enough to get to the MID as we
1057 * checked the pdu_length earlier. Now check to see
1058 * if the rest of the header is OK. We borrow the length
1059 * var for the rest of the loop to avoid a new stack var.
1060 *
1061 * 48 bytes is enough to display the header and a little bit
1062 * into the payload for debugging purposes.
1063 */
Steve French373512e2015-12-18 13:05:30 -06001064 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001065 if (length != 0)
1066 cifs_dump_mem("Bad SMB: ", buf,
1067 min_t(unsigned int, server->total_read, 48));
1068
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001069 if (server->ops->is_session_expired &&
1070 server->ops->is_session_expired(buf)) {
1071 cifs_reconnect(server);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001072 return -1;
1073 }
1074
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001075 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08001076 server->ops->is_status_pending(buf, server))
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001077 return -1;
1078
Jeff Laytonff4fa4a2012-02-07 06:31:05 -05001079 if (!mid)
1080 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001081
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001082 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -05001083 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001084}
1085
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001086static void
1087smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
1088{
1089 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
1090
1091 /*
1092 * SMB1 does not use credits.
1093 */
1094 if (server->vals->header_preamble_size)
1095 return;
1096
1097 if (shdr->CreditRequest) {
1098 spin_lock(&server->req_lock);
1099 server->credits += le16_to_cpu(shdr->CreditRequest);
1100 spin_unlock(&server->req_lock);
1101 wake_up(&server->request_q);
1102 }
1103}
1104
1105
Jeff Laytone9097ab2011-10-19 15:29:40 -04001106static int
Al Viro7c97c202011-06-21 08:51:28 -04001107cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001109 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -04001110 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001111 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001112 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001113 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001114 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001115 struct mid_q_entry *mids[MAX_COMPOUND];
1116 char *bufs[MAX_COMPOUND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -05001119 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -04001120
1121 length = atomic_inc_return(&tcpSesAllocCount);
1122 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -07001123 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001125 set_freezable();
Eric W. Biederman33da8e72019-08-16 12:33:54 -05001126 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +00001127 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -07001128 if (try_to_freeze())
1129 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001130
Jeff Layton2a37ef92011-10-19 15:29:23 -04001131 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +04001132 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001133
Jeff Layton2a37ef92011-10-19 15:29:23 -04001134 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001135 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +00001136 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +00001137
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001138 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -04001139 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +00001140 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10001141
1142 if (server->vals->header_preamble_size == 0)
1143 server->total_read = 0;
1144 else
1145 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -07001146
Pavel Shilovsky98bac622011-08-01 13:19:42 +04001147 /*
1148 * The right amount was read from socket - 4 bytes,
1149 * so we can now interpret the length field.
1150 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001151 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -07001152
Joe Perchesf96637b2013-05-04 22:12:25 -05001153 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -04001154 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001155 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001156next_pdu:
1157 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -07001158
Jeff Layton89482a52011-10-19 15:28:57 -04001159 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001160 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001161 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001162 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001163 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001164 cifs_reconnect(server);
Jeff Layton89482a52011-10-19 15:28:57 -04001165 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -07001166 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001167
Jeff Layton89482a52011-10-19 15:28:57 -04001168 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001169 length = cifs_read_from_socket(server,
1170 buf + server->vals->header_preamble_size,
1171 HEADER_SIZE(server) - 1
1172 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001173 if (length < 0)
1174 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001175 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001176
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001177 if (server->ops->next_header) {
1178 next_offset = server->ops->next_header(buf);
1179 if (next_offset)
1180 server->pdu_size = next_offset;
1181 }
1182
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001183 memset(mids, 0, sizeof(mids));
1184 memset(bufs, 0, sizeof(bufs));
1185 num_mids = 0;
1186
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001187 if (server->ops->is_transform_hdr &&
1188 server->ops->receive_transform &&
1189 server->ops->is_transform_hdr(buf)) {
1190 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001191 mids,
1192 bufs,
1193 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001194 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001195 mids[0] = server->ops->find_mid(server, buf);
1196 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001197 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001198
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001199 if (!mids[0] || !mids[0]->receive)
1200 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001201 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001202 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001203 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001204
Lars Persson696e4202018-06-25 14:05:25 +02001205 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001206 for (i = 0; i < num_mids; i++)
1207 if (mids[i])
1208 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001209 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001210 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001211
Steve Frenchfda35942011-01-20 18:06:34 +00001212 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001213
1214 for (i = 0; i < num_mids; i++) {
1215 if (mids[i] != NULL) {
1216 mids[i]->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001217
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001218 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1219 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001220
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001221 cifs_mid_q_entry_release(mids[i]);
1222 } else if (server->ops->is_oplock_break &&
1223 server->ops->is_oplock_break(bufs[i],
1224 server)) {
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001225 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001226 cifs_dbg(FYI, "Received oplock break\n");
1227 } else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001228 cifs_server_dbg(VFS, "No task to wake, unknown frame "
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001229 "received! NumMids %d\n",
1230 atomic_read(&midCount));
1231 cifs_dump_mem("Received Data is: ", bufs[i],
1232 HEADER_SIZE(server));
Ronnie Sahlberg3e272572019-07-06 06:43:08 +10001233 smb2_add_credits_from_hdr(bufs[i], server);
Steve French39798772006-05-31 22:40:51 +00001234#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001235 if (server->ops->dump_detail)
1236 server->ops->dump_detail(bufs[i],
1237 server);
1238 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001239#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001240 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001241 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001242
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001243 if (pdu_length > server->pdu_size) {
1244 if (!allocate_buffers(server))
1245 continue;
1246 pdu_length -= server->pdu_size;
1247 server->total_read = 0;
1248 server->large_buf = false;
1249 buf = server->smallbuf;
1250 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001251 }
1252 } /* end while !EXITING */
1253
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001254 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001255 cifs_buf_release(server->bigbuf);
1256 if (server->smallbuf) /* no sense logging a debug message if NULL */
1257 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001259 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001260 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001261
1262 /* if server->tsk was NULL then wait for a signal before exiting */
1263 if (!task_to_wake) {
1264 set_current_state(TASK_INTERRUPTIBLE);
1265 while (!signal_pending(current)) {
1266 schedule();
1267 set_current_state(TASK_INTERRUPTIBLE);
1268 }
1269 set_current_state(TASK_RUNNING);
1270 }
1271
Jeff Layton0468a2c2008-12-01 07:09:35 -05001272 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Jeff Laytonc359cf32007-11-16 22:22:06 +00001275/* extract the host portion of the UNC string */
1276static char *
1277extract_hostname(const char *unc)
1278{
1279 const char *src;
1280 char *dst, *delim;
1281 unsigned int len;
1282
1283 /* skip double chars at beginning of string */
1284 /* BB: check validity of these bytes? */
Paulo Alcantarac34fea52018-11-14 14:03:40 -02001285 if (strlen(unc) < 3)
1286 return ERR_PTR(-EINVAL);
1287 for (src = unc; *src && *src == '\\'; src++)
1288 ;
1289 if (!*src)
1290 return ERR_PTR(-EINVAL);
Jeff Laytonc359cf32007-11-16 22:22:06 +00001291
1292 /* delimiter between hostname and sharename is always '\\' now */
1293 delim = strchr(src, '\\');
1294 if (!delim)
1295 return ERR_PTR(-EINVAL);
1296
1297 len = delim - src;
1298 dst = kmalloc((len + 1), GFP_KERNEL);
1299 if (dst == NULL)
1300 return ERR_PTR(-ENOMEM);
1301
1302 memcpy(dst, src, len);
1303 dst[len] = '\0';
1304
1305 return dst;
1306}
1307
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001308static int get_option_ul(substring_t args[], unsigned long *option)
1309{
1310 int rc;
1311 char *string;
1312
1313 string = match_strdup(args);
1314 if (string == NULL)
1315 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001316 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001317 kfree(string);
1318
1319 return rc;
1320}
1321
Eric W. Biederman3da46562013-02-06 01:37:39 -08001322static int get_option_uid(substring_t args[], kuid_t *result)
1323{
1324 unsigned long value;
1325 kuid_t uid;
1326 int rc;
1327
1328 rc = get_option_ul(args, &value);
1329 if (rc)
1330 return rc;
1331
1332 uid = make_kuid(current_user_ns(), value);
1333 if (!uid_valid(uid))
1334 return -EINVAL;
1335
1336 *result = uid;
1337 return 0;
1338}
1339
1340static int get_option_gid(substring_t args[], kgid_t *result)
1341{
1342 unsigned long value;
1343 kgid_t gid;
1344 int rc;
1345
1346 rc = get_option_ul(args, &value);
1347 if (rc)
1348 return rc;
1349
1350 gid = make_kgid(current_user_ns(), value);
1351 if (!gid_valid(gid))
1352 return -EINVAL;
1353
1354 *result = gid;
1355 return 0;
1356}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001357
1358static int cifs_parse_security_flavors(char *value,
1359 struct smb_vol *vol)
1360{
1361
1362 substring_t args[MAX_OPT_ARGS];
1363
Jeff Layton1e3cc572013-06-10 17:12:23 -05001364 /*
1365 * With mount options, the last one should win. Reset any existing
1366 * settings back to default.
1367 */
1368 vol->sectype = Unspecified;
1369 vol->sign = false;
1370
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001371 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001372 case Opt_sec_krb5p:
1373 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1374 return 1;
1375 case Opt_sec_krb5i:
1376 vol->sign = true;
1377 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001378 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001379 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001380 break;
1381 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001382 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001383 /* Fallthrough */
1384 case Opt_sec_ntlmssp:
1385 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001386 break;
1387 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001388 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001389 /* Fallthrough */
1390 case Opt_ntlm:
1391 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001392 break;
1393 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001394 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001395 /* Fallthrough */
1396 case Opt_sec_ntlmv2:
1397 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001398 break;
1399#ifdef CONFIG_CIFS_WEAK_PW_HASH
1400 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001401 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001402 break;
1403#endif
1404 case Opt_sec_none:
1405 vol->nullauth = 1;
1406 break;
1407 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001408 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001409 return 1;
1410 }
1411
1412 return 0;
1413}
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001416cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1417{
1418 substring_t args[MAX_OPT_ARGS];
1419
1420 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1421 case Opt_cache_loose:
1422 vol->direct_io = false;
1423 vol->strict_io = false;
Steve French83bbfa72019-08-27 23:58:54 -05001424 vol->cache_ro = false;
Steve French41e033f2019-08-30 02:12:41 -05001425 vol->cache_rw = false;
Jeff Layton15b6a472012-05-16 07:50:15 -04001426 break;
1427 case Opt_cache_strict:
1428 vol->direct_io = false;
1429 vol->strict_io = true;
Steve French83bbfa72019-08-27 23:58:54 -05001430 vol->cache_ro = false;
Steve French41e033f2019-08-30 02:12:41 -05001431 vol->cache_rw = false;
Jeff Layton15b6a472012-05-16 07:50:15 -04001432 break;
1433 case Opt_cache_none:
1434 vol->direct_io = true;
1435 vol->strict_io = false;
Steve French83bbfa72019-08-27 23:58:54 -05001436 vol->cache_ro = false;
Steve French41e033f2019-08-30 02:12:41 -05001437 vol->cache_rw = false;
Steve French83bbfa72019-08-27 23:58:54 -05001438 break;
1439 case Opt_cache_ro:
1440 vol->direct_io = false;
1441 vol->strict_io = false;
1442 vol->cache_ro = true;
Steve French41e033f2019-08-30 02:12:41 -05001443 vol->cache_rw = false;
1444 break;
1445 case Opt_cache_rw:
1446 vol->direct_io = false;
1447 vol->strict_io = false;
1448 vol->cache_ro = false;
1449 vol->cache_rw = true;
Jeff Layton15b6a472012-05-16 07:50:15 -04001450 break;
1451 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001452 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001453 return 1;
1454 }
1455 return 0;
1456}
1457
1458static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001459cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001460{
1461 substring_t args[MAX_OPT_ARGS];
1462
1463 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001464#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001465 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001466 if (disable_legacy_dialects) {
1467 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1468 return 1;
1469 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001470 if (is_smb3) {
1471 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1472 return 1;
1473 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001474 vol->ops = &smb1_operations;
1475 vol->vals = &smb1_values;
1476 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001477 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001478 if (disable_legacy_dialects) {
1479 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1480 return 1;
1481 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001482 if (is_smb3) {
1483 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1484 return 1;
1485 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001486 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001487 vol->vals = &smb20_values;
1488 break;
Steve French74204512018-06-19 14:34:08 -05001489#else
1490 case Smb_1:
1491 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1492 return 1;
1493 case Smb_20:
1494 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1495 return 1;
1496#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001497 case Smb_21:
1498 vol->ops = &smb21_operations;
1499 vol->vals = &smb21_values;
1500 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001501 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001502 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001503 vol->vals = &smb30_values;
1504 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001505 case Smb_302:
1506 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1507 vol->vals = &smb302_values;
1508 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001509 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001510 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001511 vol->vals = &smb311_values;
1512 break;
Steve French9764c022017-09-17 10:41:35 -05001513 case Smb_3any:
1514 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1515 vol->vals = &smb3any_values;
1516 break;
1517 case Smb_default:
1518 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1519 vol->vals = &smbdefault_values;
1520 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001521 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001522 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001523 return 1;
1524 }
1525 return 0;
1526}
1527
Jeff Laytond387a5c2012-12-10 06:10:46 -05001528/*
1529 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1530 * fields with the result. Returns 0 on success and an error otherwise.
1531 */
1532static int
1533cifs_parse_devname(const char *devname, struct smb_vol *vol)
1534{
1535 char *pos;
1536 const char *delims = "/\\";
1537 size_t len;
1538
Yao Liu68e26722019-01-28 19:47:28 +08001539 if (unlikely(!devname || !*devname)) {
1540 cifs_dbg(VFS, "Device name not specified.\n");
1541 return -EINVAL;
1542 }
1543
Jeff Laytond387a5c2012-12-10 06:10:46 -05001544 /* make sure we have a valid UNC double delimiter prefix */
1545 len = strspn(devname, delims);
1546 if (len != 2)
1547 return -EINVAL;
1548
1549 /* find delimiter between host and sharename */
1550 pos = strpbrk(devname + 2, delims);
1551 if (!pos)
1552 return -EINVAL;
1553
1554 /* skip past delimiter */
1555 ++pos;
1556
1557 /* now go until next delimiter or end of string */
1558 len = strcspn(pos, delims);
1559
1560 /* move "pos" up to delimiter or NULL */
1561 pos += len;
1562 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1563 if (!vol->UNC)
1564 return -ENOMEM;
1565
1566 convert_delimiter(vol->UNC, '\\');
1567
Sachin Prabhu11e31642016-02-08 13:44:01 +05301568 /* skip any delimiter */
1569 if (*pos == '/' || *pos == '\\')
1570 pos++;
1571
1572 /* If pos is NULL then no prepath */
1573 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001574 return 0;
1575
1576 vol->prepath = kstrdup(pos, GFP_KERNEL);
1577 if (!vol->prepath)
1578 return -ENOMEM;
1579
1580 return 0;
1581}
1582
Jeff Layton23db65f2012-05-15 12:20:51 -04001583static int
Sean Finneyb9468452011-04-11 13:19:32 +00001584cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001585 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001587 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001588 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 unsigned int temp_len, i, j;
1590 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001591 short int override_uid = -1;
1592 short int override_gid = -1;
1593 bool uid_specified = false;
1594 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001595 bool sloppy = false;
1596 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001597 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001598 char *string = NULL;
1599 char *tmp_end, *value;
1600 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001601 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001602 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001603 unsigned short port = 0;
1604 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001607 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001608 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Jeff Layton6ee95422012-11-26 11:09:57 -05001610 /* ensure we always start with zeroed-out smb_vol */
1611 memset(vol, 0, sizeof(*vol));
1612
Jeff Layton88463992010-11-22 15:31:03 -05001613 /*
1614 * does not have to be perfect mapping since field is
1615 * informational, only used for servers that do not support
1616 * port 445 and it can be overridden at mount time
1617 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001618 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1619 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001620 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1621
Jeff Layton1397f2e2011-01-07 11:30:28 -05001622 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001623 /* null target name indicates to use *SMBSERVR default called name
1624 if we end up sending RFC1001 session initialize */
1625 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001626 vol->cred_uid = current_uid();
1627 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001628 vol->linux_gid = current_gid();
Steve Frenche8506d22019-02-28 21:32:15 -06001629 vol->bsize = 1024 * 1024; /* can improve cp performance significantly */
Steve French2baa2682014-09-27 02:19:01 -05001630 /*
1631 * default to SFM style remapping of seven reserved characters
1632 * unless user overrides it or we negotiate CIFS POSIX where
1633 * it is unnecessary. Can not simultaneously use more than one mapping
1634 * since then readdir could list files that open could not open
1635 */
1636 vol->remap = true;
1637
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001638 /* default to only allowing write access to owner of the mount */
1639 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001642 /* default is always to request posix paths. */
1643 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001644 /* default to using server inode numbers where available */
1645 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001646
Jeff Layton1b359202012-09-19 15:20:27 -07001647 /* default is to use strict cifs caching semantics */
1648 vol->strict_io = true;
1649
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301650 vol->actimeo = CIFS_DEF_ACTIMEO;
1651
Steve Frenchca567eb2019-03-29 16:31:07 -05001652 /* Most clients set timeout to 0, allows server to use its default */
1653 vol->handle_timeout = 0; /* See MS-SMB2 spec section 2.2.14.2.12 */
1654
Steve French9764c022017-09-17 10:41:35 -05001655 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1656 vol->ops = &smb30_operations;
1657 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001658
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001659 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1660
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001661 /* default to no multichannel (single server connection) */
1662 vol->multichannel = false;
1663 vol->max_channels = 1;
1664
Sean Finneyb9468452011-04-11 13:19:32 +00001665 if (!mountdata)
1666 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Sean Finneyb9468452011-04-11 13:19:32 +00001668 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1669 if (!mountdata_copy)
1670 goto cifs_parse_mount_err;
1671
1672 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001673 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001674
Steve French50c2f752007-07-13 00:33:32 +00001675 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001676 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 separator[0] = options[4];
1678 options += 5;
1679 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001680 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001683 vol->backupuid_specified = false; /* no backup intent for a user */
1684 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001685
Jeff Layton37d4f992013-05-24 07:40:05 -04001686 switch (cifs_parse_devname(devname, vol)) {
1687 case 0:
1688 break;
1689 case -ENOMEM:
1690 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1691 goto cifs_parse_mount_err;
1692 case -EINVAL:
1693 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1694 goto cifs_parse_mount_err;
1695 default:
1696 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1697 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001698 }
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001701 substring_t args[MAX_OPT_ARGS];
1702 unsigned long option;
1703 int token;
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (!*data)
1706 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001708 token = match_token(data, cifs_mount_option_tokens, args);
1709
1710 switch (token) {
1711
1712 /* Ingnore the following */
1713 case Opt_ignore:
1714 break;
1715
1716 /* Boolean values */
1717 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001719 break;
1720 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001722 break;
1723 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001724 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001725 break;
1726 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001727 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001728 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001729 case Opt_forcegid:
1730 override_gid = 1;
1731 break;
1732 case Opt_noforcegid:
1733 override_gid = 0;
1734 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001735 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001736 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001737 break;
1738 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001739 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001740 break;
Steve French3e7a02d2019-09-11 21:46:20 -05001741 case Opt_nolease:
1742 vol->no_lease = 1;
1743 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001744 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001746 break;
1747 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001749 break;
1750 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001752 break;
1753 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001755 break;
1756 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001757 vol->sfu_remap = true;
1758 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001759 break;
1760 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001761 vol->sfu_remap = false;
1762 break;
1763 case Opt_mapposix:
1764 vol->remap = true;
1765 vol->sfu_remap = false; /* disable SFU mapping */
1766 break;
1767 case Opt_nomapposix:
1768 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001769 break;
1770 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001771 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001772 break;
1773 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001774 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001775 break;
1776 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001777 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001778 break;
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03001779 case Opt_rootfs:
1780#ifdef CONFIG_CIFS_ROOT
1781 vol->rootfs = true;
1782#endif
1783 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001784 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001785 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001786 break;
1787 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001788 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001789 break;
1790 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001791 if (vol->linux_ext)
1792 cifs_dbg(VFS,
1793 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001794 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001795 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001796 case Opt_unix:
1797 if (vol->no_linux_ext)
1798 cifs_dbg(VFS,
1799 "conflicting unix mount options\n");
1800 vol->linux_ext = 1;
1801 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001802 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001803 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001804 break;
1805 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001806 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001807 break;
1808 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001809 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001810 /*
1811 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001812 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001813 * local vfs will do advisory
1814 */
Steve French50c2f752007-07-13 00:33:32 +00001815 if (vol->file_mode ==
1816 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001817 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001818 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001819 case Opt_nohandlecache:
1820 vol->nohandlecache = 1;
1821 break;
1822 case Opt_handlecache:
1823 vol->nohandlecache = 0;
1824 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001825 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001826 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001827 break;
1828 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001830 break;
1831 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001833 break;
Steve French95932652016-09-23 01:36:34 -05001834 case Opt_setuidfromacl:
1835 vol->setuidfromacl = 1;
1836 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001837 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001838 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001839 break;
1840 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001841 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001842 break;
1843 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001845 break;
1846 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001848 break;
1849 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001851 break;
1852 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001854 break;
1855 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001856 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001857 break;
1858 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001859 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001860 break;
1861 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001863 break;
1864 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001866 break;
1867 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001868 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001869 break;
Steve French412094a2019-06-24 02:01:42 -05001870 case Opt_modesid:
1871 vol->mode_ace = 1;
1872 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001873 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001874 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001875 break;
1876 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001877 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001878 break;
1879 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001881 break;
1882 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001884 break;
1885 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001886 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001887 break;
1888 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001889 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001890 break;
Steve French4f5c10f2019-09-03 21:18:49 -05001891 case Opt_ignore_signature:
1892 vol->sign = true;
1893 vol->ignore_signature = true;
1894 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001895 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001896 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001897 * is a per tree connection (mount) not a per socket
1898 * or per-smb connection option in the protocol
1899 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1900 */
Steve French95b1cb92008-05-15 16:44:38 +00001901 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001902 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001903 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001904 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001905 break;
1906 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301907#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001908 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001909 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301910#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301911 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001912 break;
1913 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001914 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001915 break;
1916 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001917 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001918 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001919 case Opt_sloppy:
1920 sloppy = true;
1921 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001922 case Opt_nosharesock:
1923 vol->nosharesock = true;
1924 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001925 case Opt_nopersistent:
1926 vol->nopersistent = true;
1927 if (vol->persistent) {
1928 cifs_dbg(VFS,
1929 "persistenthandles mount options conflict\n");
1930 goto cifs_parse_mount_err;
1931 }
1932 break;
1933 case Opt_persistent:
1934 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001935 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001936 cifs_dbg(VFS,
1937 "persistenthandles mount options conflict\n");
1938 goto cifs_parse_mount_err;
1939 }
1940 break;
Steve French592fafe2015-11-03 10:08:53 -06001941 case Opt_resilient:
1942 vol->resilient = true;
1943 if (vol->persistent) {
1944 cifs_dbg(VFS,
1945 "persistenthandles mount options conflict\n");
1946 goto cifs_parse_mount_err;
1947 }
1948 break;
1949 case Opt_noresilient:
1950 vol->resilient = false; /* already the default */
1951 break;
Germano Percossi39566442016-12-15 12:31:18 +05301952 case Opt_domainauto:
1953 vol->domainauto = true;
1954 break;
Long Li8339dd32017-11-07 01:54:55 -07001955 case Opt_rdma:
1956 vol->rdma = true;
1957 break;
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001958 case Opt_multichannel:
1959 vol->multichannel = true;
1960 break;
1961 case Opt_nomultichannel:
1962 vol->multichannel = false;
1963 break;
Steve French9fe5ff12019-06-24 20:39:04 -05001964 case Opt_compress:
1965 vol->compression = UNKNOWN_TYPE;
1966 cifs_dbg(VFS,
1967 "SMB3 compression support is experimental\n");
1968 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001969
1970 /* Numeric Values */
1971 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001972 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001973 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1974 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001975 goto cifs_parse_mount_err;
1976 }
1977 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001978 break;
1979 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001980 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001981 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1982 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001983 goto cifs_parse_mount_err;
1984 }
1985 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001986 break;
1987 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001988 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001989 cifs_dbg(VFS, "%s: Invalid uid value\n",
1990 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001991 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001993 uid_specified = true;
1994 break;
1995 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001996 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001997 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1998 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001999 goto cifs_parse_mount_err;
2000 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002001 break;
2002 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08002003 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002004 cifs_dbg(VFS, "%s: Invalid gid value\n",
2005 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002006 goto cifs_parse_mount_err;
2007 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002008 gid_specified = true;
2009 break;
2010 case Opt_file_mode:
2011 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002012 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
2013 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002014 goto cifs_parse_mount_err;
2015 }
2016 vol->file_mode = option;
2017 break;
2018 case Opt_dirmode:
2019 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002020 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
2021 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002022 goto cifs_parse_mount_err;
2023 }
2024 vol->dir_mode = option;
2025 break;
2026 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002027 if (get_option_ul(args, &option) ||
2028 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002029 cifs_dbg(VFS, "%s: Invalid port value\n",
2030 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002031 goto cifs_parse_mount_err;
2032 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002033 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002034 break;
Steve French563317e2019-09-08 23:22:02 -05002035 case Opt_min_enc_offload:
2036 if (get_option_ul(args, &option)) {
2037 cifs_dbg(VFS, "Invalid minimum encrypted read offload size (esize)\n");
2038 goto cifs_parse_mount_err;
2039 }
2040 vol->min_offload = option;
2041 break;
Steve Frenche8506d22019-02-28 21:32:15 -06002042 case Opt_blocksize:
2043 if (get_option_ul(args, &option)) {
2044 cifs_dbg(VFS, "%s: Invalid blocksize value\n",
2045 __func__);
2046 goto cifs_parse_mount_err;
2047 }
2048 /*
2049 * inode blocksize realistically should never need to be
2050 * less than 16K or greater than 16M and default is 1MB.
2051 * Note that small inode block sizes (e.g. 64K) can lead
2052 * to very poor performance of common tools like cp and scp
2053 */
2054 if ((option < CIFS_MAX_MSGSIZE) ||
2055 (option > (4 * SMB3_DEFAULT_IOSIZE))) {
2056 cifs_dbg(VFS, "%s: Invalid blocksize\n",
2057 __func__);
2058 goto cifs_parse_mount_err;
2059 }
2060 vol->bsize = option;
2061 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002062 case Opt_rsize:
2063 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002064 cifs_dbg(VFS, "%s: Invalid rsize value\n",
2065 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002066 goto cifs_parse_mount_err;
2067 }
2068 vol->rsize = option;
2069 break;
2070 case Opt_wsize:
2071 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002072 cifs_dbg(VFS, "%s: Invalid wsize value\n",
2073 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002074 goto cifs_parse_mount_err;
2075 }
2076 vol->wsize = option;
2077 break;
2078 case Opt_actimeo:
2079 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002080 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
2081 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002082 goto cifs_parse_mount_err;
2083 }
2084 vol->actimeo = HZ * option;
2085 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002086 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002087 goto cifs_parse_mount_err;
2088 }
2089 break;
Steve Frenchca567eb2019-03-29 16:31:07 -05002090 case Opt_handletimeout:
2091 if (get_option_ul(args, &option)) {
2092 cifs_dbg(VFS, "%s: Invalid handletimeout value\n",
2093 __func__);
2094 goto cifs_parse_mount_err;
2095 }
2096 vol->handle_timeout = option;
2097 if (vol->handle_timeout > SMB3_MAX_HANDLE_TIMEOUT) {
2098 cifs_dbg(VFS, "Invalid handle cache timeout, longer than 16 minutes\n");
2099 goto cifs_parse_mount_err;
2100 }
2101 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002102 case Opt_echo_interval:
2103 if (get_option_ul(args, &option)) {
2104 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
2105 __func__);
2106 goto cifs_parse_mount_err;
2107 }
2108 vol->echo_interval = option;
2109 break;
Steve French8b217fe2016-11-11 22:36:20 -06002110 case Opt_snapshot:
2111 if (get_option_ul(args, &option)) {
2112 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
2113 __func__);
2114 goto cifs_parse_mount_err;
2115 }
2116 vol->snapshot_time = option;
2117 break;
Steve French141891f2016-09-23 00:44:16 -05002118 case Opt_max_credits:
2119 if (get_option_ul(args, &option) || (option < 20) ||
2120 (option > 60000)) {
2121 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
2122 __func__);
2123 goto cifs_parse_mount_err;
2124 }
2125 vol->max_credits = option;
2126 break;
Aurelien Aptelbcc88802019-09-20 04:32:20 +02002127 case Opt_max_channels:
2128 if (get_option_ul(args, &option) || option < 1 ||
2129 option > CIFS_MAX_CHANNELS) {
2130 cifs_dbg(VFS, "%s: Invalid max_channels value, needs to be 1-%d\n",
2131 __func__, CIFS_MAX_CHANNELS);
2132 goto cifs_parse_mount_err;
2133 }
2134 vol->max_channels = option;
2135 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002136
2137 /* String Arguments */
2138
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002139 case Opt_blank_user:
2140 /* null user, ie. anonymous authentication */
2141 vol->nullauth = 1;
2142 vol->username = NULL;
2143 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002144 case Opt_user:
2145 string = match_strdup(args);
2146 if (string == NULL)
2147 goto out_nomem;
2148
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002149 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
2150 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002151 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002152 goto cifs_parse_mount_err;
2153 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002154
2155 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002156 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05002157 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002158 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002159 break;
2160 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002161 /* passwords have to be handled differently
2162 * to allow the character used for deliminator
2163 * to be passed within them
2164 */
2165
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01002166 /*
2167 * Check if this is a case where the password
2168 * starts with a delimiter
2169 */
2170 tmp_end = strchr(data, '=');
2171 tmp_end++;
2172 if (!(tmp_end < end && tmp_end[1] == delim)) {
2173 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01002174 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01002175 vol->password = NULL;
2176 break;
2177 }
Gustavo A. R. Silva07fa6012018-11-27 10:01:51 +11002178 /* Fallthrough - to Opt_pass below.*/
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01002179 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002180 /* Obtain the value string */
2181 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01002182 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002183
2184 /* Set tmp_end to end of the string */
2185 tmp_end = (char *) value + strlen(value);
2186
2187 /* Check if following character is the deliminator
2188 * If yes, we have encountered a double deliminator
2189 * reset the NULL character to the deliminator
2190 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05302191 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002192 tmp_end[0] = delim;
2193
Suresh Jayaramane73f8432012-06-12 07:15:50 +05302194 /* Keep iterating until we get to a single
2195 * deliminator OR the end
2196 */
2197 while ((tmp_end = strchr(tmp_end, delim))
2198 != NULL && (tmp_end[1] == delim)) {
2199 tmp_end = (char *) &tmp_end[2];
2200 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002201
Suresh Jayaramane73f8432012-06-12 07:15:50 +05302202 /* Reset var options to point to next element */
2203 if (tmp_end) {
2204 tmp_end[0] = '\0';
2205 options = (char *) &tmp_end[1];
2206 } else
2207 /* Reached the end of the mount option
2208 * string */
2209 options = end;
2210 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002211
Aurelien Aptel97f4b722018-01-25 15:59:39 +01002212 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002213 /* Now build new password string */
2214 temp_len = strlen(value);
2215 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
2216 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002217 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002218 goto cifs_parse_mount_err;
2219 }
2220
2221 for (i = 0, j = 0; i < temp_len; i++, j++) {
2222 vol->password[j] = value[i];
2223 if ((value[i] == delim) &&
2224 value[i+1] == delim)
2225 /* skip the second deliminator */
2226 i++;
2227 }
2228 vol->password[j] = '\0';
2229 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002230 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002231 /* FIXME: should this be an error instead? */
2232 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002233 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002234 case Opt_ip:
2235 string = match_strdup(args);
2236 if (string == NULL)
2237 goto out_nomem;
2238
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002239 if (!cifs_convert_address(dstaddr, string,
2240 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002241 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002242 goto cifs_parse_mount_err;
2243 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002244 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002245 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002246 case Opt_domain:
2247 string = match_strdup(args);
2248 if (string == NULL)
2249 goto out_nomem;
2250
Chen Gang057d6332013-07-19 09:01:36 +08002251 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
2252 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002253 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002254 goto cifs_parse_mount_err;
2255 }
2256
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002257 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002258 vol->domainname = kstrdup(string, GFP_KERNEL);
2259 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002260 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002261 goto cifs_parse_mount_err;
2262 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002263 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002264 break;
2265 case Opt_srcaddr:
2266 string = match_strdup(args);
2267 if (string == NULL)
2268 goto out_nomem;
2269
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002270 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002271 (struct sockaddr *)&vol->srcaddr,
2272 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002273 pr_warn("CIFS: Could not parse srcaddr: %s\n",
2274 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002275 goto cifs_parse_mount_err;
2276 }
2277 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002278 case Opt_iocharset:
2279 string = match_strdup(args);
2280 if (string == NULL)
2281 goto out_nomem;
2282
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002283 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002284 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002285 goto cifs_parse_mount_err;
2286 }
2287
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002288 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002289 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002290 vol->iocharset = kstrdup(string,
2291 GFP_KERNEL);
2292 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002293 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002294 goto cifs_parse_mount_err;
2295 }
2296 }
2297 /* if iocharset not set then load_nls_default
2298 * is used by caller
2299 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002300 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002301 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002302 case Opt_netbiosname:
2303 string = match_strdup(args);
2304 if (string == NULL)
2305 goto out_nomem;
2306
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002307 memset(vol->source_rfc1001_name, 0x20,
2308 RFC1001_NAME_LEN);
2309 /*
2310 * FIXME: are there cases in which a comma can
2311 * be valid in workstation netbios name (and
2312 * need special handling)?
2313 */
2314 for (i = 0; i < RFC1001_NAME_LEN; i++) {
2315 /* don't ucase netbiosname for user */
2316 if (string[i] == 0)
2317 break;
2318 vol->source_rfc1001_name[i] = string[i];
2319 }
2320 /* The string has 16th byte zero still from
2321 * set at top of the function
2322 */
2323 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002324 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002325 break;
2326 case Opt_servern:
2327 /* servernetbiosname specified override *SMBSERVER */
2328 string = match_strdup(args);
2329 if (string == NULL)
2330 goto out_nomem;
2331
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002332 /* last byte, type, is 0x20 for servr type */
2333 memset(vol->target_rfc1001_name, 0x20,
2334 RFC1001_NAME_LEN_WITH_NULL);
2335
2336 /* BB are there cases in which a comma can be
2337 valid in this workstation netbios name
2338 (and need special handling)? */
2339
2340 /* user or mount helper must uppercase the
2341 netbios name */
2342 for (i = 0; i < 15; i++) {
2343 if (string[i] == 0)
2344 break;
2345 vol->target_rfc1001_name[i] = string[i];
2346 }
2347 /* The string has 16th byte zero still from
2348 set at top of the function */
2349 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002350 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002351 break;
2352 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002353 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002354 string = match_strdup(args);
2355 if (string == NULL)
2356 goto out_nomem;
2357
Steve French7e682f72017-08-31 21:34:24 -05002358 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002359 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002360 if (strlen(string) > 1) {
2361 pr_warn("Bad mount helper ver=%s. Did "
2362 "you want SMB1 (CIFS) dialect "
2363 "and mean to type vers=1.0 "
2364 "instead?\n", string);
2365 goto cifs_parse_mount_err;
2366 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002367 /* This is the default */
2368 break;
2369 }
2370 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002371 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002372 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002373 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002374 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002375 string = match_strdup(args);
2376 if (string == NULL)
2377 goto out_nomem;
2378
Steve Frenchc7c137b2018-06-06 17:59:29 -05002379 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002380 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002381 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002382 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002383 case Opt_sec:
2384 string = match_strdup(args);
2385 if (string == NULL)
2386 goto out_nomem;
2387
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002388 if (cifs_parse_security_flavors(string, vol) != 0)
2389 goto cifs_parse_mount_err;
2390 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002391 case Opt_cache:
2392 string = match_strdup(args);
2393 if (string == NULL)
2394 goto out_nomem;
2395
2396 if (cifs_parse_cache_flavor(string, vol) != 0)
2397 goto cifs_parse_mount_err;
2398 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002399 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002400 /*
2401 * An option we don't recognize. Save it off for later
2402 * if we haven't already found one
2403 */
2404 if (!invalid)
2405 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002408 /* Free up any allocated string */
2409 kfree(string);
2410 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002412
Jeff Laytond8162552012-03-23 14:40:56 -04002413 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002414 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002415 goto cifs_parse_mount_err;
2416 }
2417
Long Li8339dd32017-11-07 01:54:55 -07002418 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2419 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2420 goto cifs_parse_mount_err;
2421 }
2422
Jeff Layton8a8798a2012-01-17 16:09:15 -05002423#ifndef CONFIG_KEYS
2424 /* Muliuser mounts require CONFIG_KEYS support */
2425 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002426 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002427 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002428 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002429#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002430 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002431 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002432 goto cifs_parse_mount_err;
2433 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002434
Jeff Layton62a1a432012-12-10 06:10:45 -05002435 /* make sure UNC has a share name */
2436 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002437 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002438 goto cifs_parse_mount_err;
2439 }
2440
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002441 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002442 int len;
2443 const char *slash;
2444
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002445 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002446 /* Use the address part of the UNC. */
2447 slash = strchr(&vol->UNC[2], '\\');
2448 len = slash - &vol->UNC[2];
2449 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002450 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002451 goto cifs_parse_mount_err;
2452 }
2453 }
2454
2455 /* set the port that we got earlier */
2456 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002458 if (uid_specified)
2459 vol->override_uid = override_uid;
2460 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002461 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002462
2463 if (gid_specified)
2464 vol->override_gid = override_gid;
2465 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002466 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002467
Steve French7e682f72017-08-31 21:34:24 -05002468 if (got_version == false)
2469 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002470 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002471 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002472 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2473 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002474
Sean Finneyb9468452011-04-11 13:19:32 +00002475 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002477
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002478out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002479 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002480cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002481 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002482 kfree(mountdata_copy);
2483 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
Ben Greear3eb9a882010-09-01 17:06:02 -07002486/** Returns true if srcaddr isn't specified and rhs isn't
2487 * specified, or if srcaddr is specified and
2488 * matches the IP address of the rhs argument.
2489 */
Jeff Layton45151482010-07-06 20:43:02 -04002490static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002491srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2492{
2493 switch (srcaddr->sa_family) {
2494 case AF_UNSPEC:
2495 return (rhs->sa_family == AF_UNSPEC);
2496 case AF_INET: {
2497 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2498 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2499 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2500 }
2501 case AF_INET6: {
2502 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002503 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002504 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2505 }
2506 default:
2507 WARN_ON(1);
2508 return false; /* don't expect to be here */
2509 }
2510}
2511
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002512/*
2513 * If no port is specified in addr structure, we try to match with 445 port
2514 * and if it fails - with 139 ports. It should be called only if address
2515 * families of server and addr are equal.
2516 */
2517static bool
2518match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2519{
Steve French6da97912011-03-13 18:55:55 +00002520 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002521
Long Li3b249112019-05-15 14:09:04 -07002522 /* SMBDirect manages its own ports, don't match it here */
2523 if (server->rdma)
2524 return true;
2525
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002526 switch (addr->sa_family) {
2527 case AF_INET:
2528 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2529 port = ((struct sockaddr_in *) addr)->sin_port;
2530 break;
2531 case AF_INET6:
2532 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2533 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2534 break;
2535 default:
2536 WARN_ON(1);
2537 return false;
2538 }
2539
2540 if (!port) {
2541 port = htons(CIFS_PORT);
2542 if (port == *sport)
2543 return true;
2544
2545 port = htons(RFC1001_PORT);
2546 }
2547
2548 return port == *sport;
2549}
Ben Greear3eb9a882010-09-01 17:06:02 -07002550
2551static bool
2552match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2553 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Jeff Layton45151482010-07-06 20:43:02 -04002555 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002556 case AF_INET: {
2557 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2558 struct sockaddr_in *srv_addr4 =
2559 (struct sockaddr_in *)&server->dstaddr;
2560
2561 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002562 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002563 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002564 }
2565 case AF_INET6: {
2566 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2567 struct sockaddr_in6 *srv_addr6 =
2568 (struct sockaddr_in6 *)&server->dstaddr;
2569
Jeff Layton45151482010-07-06 20:43:02 -04002570 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002571 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002572 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002573 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002574 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002575 break;
2576 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002577 default:
2578 WARN_ON(1);
2579 return false; /* don't expect to be here */
2580 }
Jeff Layton45151482010-07-06 20:43:02 -04002581
Ben Greear3eb9a882010-09-01 17:06:02 -07002582 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2583 return false;
2584
Jeff Layton45151482010-07-06 20:43:02 -04002585 return true;
2586}
2587
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002588static bool
2589match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2590{
Jeff Layton3f618222013-06-12 19:52:14 -05002591 /*
2592 * The select_sectype function should either return the vol->sectype
2593 * that was specified, or "Unspecified" if that sectype was not
2594 * compatible with the given NEGOTIATE request.
2595 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302596 if (server->ops->select_sectype(server, vol->sectype)
2597 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002598 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002599
Jeff Layton3f618222013-06-12 19:52:14 -05002600 /*
2601 * Now check if signing mode is acceptable. No need to check
2602 * global_secflags at this point since if MUST_SIGN is set then
2603 * the server->sign had better be too.
2604 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002605 if (vol->sign && !server->sign)
2606 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002607
2608 return true;
2609}
2610
Jeff Layton9fa114f2012-11-26 11:09:57 -05002611static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002612{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002613 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2614
Jeff Laytona0b3df52013-05-24 07:40:59 -04002615 if (vol->nosharesock)
2616 return 0;
2617
Steve French43cdae82019-06-13 14:26:49 -05002618 /* If multidialect negotiation see if existing sessions match one */
2619 if (strcmp(vol->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
2620 if (server->vals->protocol_id < SMB30_PROT_ID)
2621 return 0;
2622 } else if (strcmp(vol->vals->version_string,
2623 SMBDEFAULT_VERSION_STRING) == 0) {
2624 if (server->vals->protocol_id < SMB21_PROT_ID)
2625 return 0;
2626 } else if ((server->vals != vol->vals) || (server->ops != vol->ops))
Jeff Layton23db65f2012-05-15 12:20:51 -04002627 return 0;
2628
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002629 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2630 return 0;
2631
2632 if (!match_address(server, addr,
2633 (struct sockaddr *)&vol->srcaddr))
2634 return 0;
2635
2636 if (!match_port(server, addr))
2637 return 0;
2638
2639 if (!match_security(server, vol))
2640 return 0;
2641
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002642 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002643 return 0;
2644
Long Li8339dd32017-11-07 01:54:55 -07002645 if (server->rdma != vol->rdma)
2646 return 0;
2647
Steve French4f5c10f2019-09-03 21:18:49 -05002648 if (server->ignore_signature != vol->ignore_signature)
2649 return 0;
2650
Steve French563317e2019-09-08 23:22:02 -05002651 if (server->min_offload != vol->min_offload)
2652 return 0;
2653
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002654 return 1;
2655}
2656
Paulo Alcantara54be1f62018-11-14 16:01:21 -02002657struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002658cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002659{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002660 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302662 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002663 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -03002664 /*
2665 * Skip ses channels since they're only handled in lower layers
2666 * (e.g. cifs_send_recv).
2667 */
2668 if (server->is_channel || !match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002669 continue;
2670
Jeff Laytone7ddee92008-11-14 13:44:38 -05002671 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302672 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002673 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002674 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302676 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 return NULL;
2678}
2679
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002680void
2681cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002683 struct task_struct *task;
2684
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302685 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002686 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302687 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002688 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002690
Rob Landleyf1d0c992011-01-22 15:44:05 -06002691 put_net(cifs_net_ns(server));
2692
Jeff Laytone7ddee92008-11-14 13:44:38 -05002693 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302694 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002695
Jeff Laytonc74093b2011-01-11 07:24:23 -05002696 cancel_delayed_work_sync(&server->echo);
2697
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002698 if (from_reconnect)
2699 /*
2700 * Avoid deadlock here: reconnect work calls
2701 * cifs_put_tcp_session() at its end. Need to be sure
2702 * that reconnect work does nothing with server pointer after
2703 * that step.
2704 */
2705 cancel_delayed_work(&server->reconnect);
2706 else
2707 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002708
Jeff Laytone7ddee92008-11-14 13:44:38 -05002709 spin_lock(&GlobalMid_Lock);
2710 server->tcpStatus = CifsExiting;
2711 spin_unlock(&GlobalMid_Lock);
2712
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002713 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302714 cifs_fscache_release_client_cookie(server);
2715
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002716 kfree(server->session_key.response);
2717 server->session_key.response = NULL;
2718 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002719
2720 task = xchg(&server->tsk, NULL);
2721 if (task)
Eric W. Biederman72abe3b2019-05-15 12:33:50 -05002722 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002725struct TCP_Server_Info *
Jeff Layton63c038c2008-12-01 18:41:46 -05002726cifs_get_tcp_session(struct smb_vol *volume_info)
2727{
2728 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002729 int rc;
2730
Joe Perchesf96637b2013-05-04 22:12:25 -05002731 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002732
2733 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002734 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002735 if (tcp_ses)
2736 return tcp_ses;
2737
2738 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2739 if (!tcp_ses) {
2740 rc = -ENOMEM;
2741 goto out_err;
2742 }
2743
Jeff Layton23db65f2012-05-15 12:20:51 -04002744 tcp_ses->ops = volume_info->ops;
2745 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002746 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002747 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2748 if (IS_ERR(tcp_ses->hostname)) {
2749 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002750 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002751 }
2752
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002753 tcp_ses->noblockcnt = volume_info->rootfs;
2754 tcp_ses->noblocksnd = volume_info->noblocksnd || volume_info->rootfs;
Jeff Layton63c038c2008-12-01 18:41:46 -05002755 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002756 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002757 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002758 tcp_ses->in_flight = 0;
Steve French1b63f182019-09-09 22:57:11 -05002759 tcp_ses->max_in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002760 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002761 init_waitqueue_head(&tcp_ses->response_q);
2762 init_waitqueue_head(&tcp_ses->request_q);
2763 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2764 mutex_init(&tcp_ses->srv_mutex);
2765 memcpy(tcp_ses->workstation_RFC1001_name,
2766 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2767 memcpy(tcp_ses->server_RFC1001_name,
2768 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002769 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002770 tcp_ses->sequence_number = 0;
Pavel Shilovsky5b964852019-01-18 11:30:26 -08002771 tcp_ses->reconnect_instance = 1;
Steve Frenchfda35942011-01-20 18:06:34 +00002772 tcp_ses->lstrp = jiffies;
Steve French9fe5ff12019-06-24 20:39:04 -05002773 tcp_ses->compress_algorithm = cpu_to_le16(volume_info->compression);
Jeff Layton58fa0152012-05-01 17:41:16 -04002774 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002775 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2776 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002777 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002778 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2779 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002780 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2781 sizeof(tcp_ses->srcaddr));
2782 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2783 sizeof(tcp_ses->dstaddr));
Aurelien Aptelbcc88802019-09-20 04:32:20 +02002784 if (volume_info->use_client_guid)
2785 memcpy(tcp_ses->client_guid, volume_info->client_guid,
2786 SMB2_CLIENT_GUID_SIZE);
2787 else
2788 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002789 /*
2790 * at this point we are the only ones with the pointer
2791 * to the struct since the kernel thread not created yet
2792 * no need to spinlock this init of tcpStatus or srv_count
2793 */
2794 tcp_ses->tcpStatus = CifsNew;
2795 ++tcp_ses->srv_count;
2796
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002797 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2798 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2799 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2800 else
2801 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002802 if (tcp_ses->rdma) {
2803#ifndef CONFIG_CIFS_SMB_DIRECT
2804 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2805 rc = -ENOENT;
2806 goto out_err_crypto_release;
2807#endif
2808 tcp_ses->smbd_conn = smbd_get_connection(
2809 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2810 if (tcp_ses->smbd_conn) {
2811 cifs_dbg(VFS, "RDMA transport established\n");
2812 rc = 0;
2813 goto smbd_connected;
2814 } else {
2815 rc = -ENOENT;
2816 goto out_err_crypto_release;
2817 }
2818 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002819 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002820 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002821 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002822 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002823 }
Long Li2f894642017-11-22 17:38:34 -07002824smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002825 /*
2826 * since we're in a cifs function already, we know that
2827 * this will succeed. No need for try_module_get().
2828 */
2829 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002830 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002831 tcp_ses, "cifsd");
2832 if (IS_ERR(tcp_ses->tsk)) {
2833 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002834 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002835 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002836 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002837 }
Steve French563317e2019-09-08 23:22:02 -05002838 tcp_ses->min_offload = volume_info->min_offload;
Steve Frenchfd88ce92011-04-12 01:01:14 +00002839 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002840
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02002841 tcp_ses->nr_targets = 1;
Steve French4f5c10f2019-09-03 21:18:49 -05002842 tcp_ses->ignore_signature = volume_info->ignore_signature;
Jeff Layton63c038c2008-12-01 18:41:46 -05002843 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302844 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002845 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302846 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002847
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302848 cifs_fscache_get_client_cookie(tcp_ses);
2849
Jeff Laytonc74093b2011-01-11 07:24:23 -05002850 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002851 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002852
Jeff Layton63c038c2008-12-01 18:41:46 -05002853 return tcp_ses;
2854
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002855out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002856 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002857
Rob Landleyf1d0c992011-01-22 15:44:05 -06002858 put_net(cifs_net_ns(tcp_ses));
2859
Jeff Layton63c038c2008-12-01 18:41:46 -05002860out_err:
2861 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002862 if (!IS_ERR(tcp_ses->hostname))
2863 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002864 if (tcp_ses->ssocket)
2865 sock_release(tcp_ses->ssocket);
2866 kfree(tcp_ses);
2867 }
2868 return ERR_PTR(rc);
2869}
2870
Steve French96daf2b2011-05-27 04:34:02 +00002871static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002872{
Jeff Layton3f618222013-06-12 19:52:14 -05002873 if (vol->sectype != Unspecified &&
2874 vol->sectype != ses->sectype)
2875 return 0;
2876
Aurelien Aptelbcc88802019-09-20 04:32:20 +02002877 /*
2878 * If an existing session is limited to less channels than
2879 * requested, it should not be reused
2880 */
2881 if (ses->chan_max < vol->max_channels)
2882 return 0;
2883
Jeff Layton3f618222013-06-12 19:52:14 -05002884 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002885 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002886 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002887 return 0;
2888 break;
2889 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002890 /* NULL username means anonymous session */
2891 if (ses->user_name == NULL) {
2892 if (!vol->nullauth)
2893 return 0;
2894 break;
2895 }
2896
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002897 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002898 if (strncmp(ses->user_name,
2899 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002900 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002901 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002902 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002903 ses->password != NULL &&
2904 strncmp(ses->password,
2905 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002906 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002907 return 0;
2908 }
2909 return 1;
2910}
2911
Aurelien Aptelb327a712018-01-24 13:46:10 +01002912/**
2913 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2914 *
2915 * A new IPC connection is made and stored in the session
2916 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2917 */
2918static int
2919cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2920{
2921 int rc = 0, xid;
2922 struct cifs_tcon *tcon;
2923 struct nls_table *nls_codepage;
2924 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2925 bool seal = false;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002926 struct TCP_Server_Info *server = ses->server;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002927
2928 /*
2929 * If the mount request that resulted in the creation of the
2930 * session requires encryption, force IPC to be encrypted too.
2931 */
2932 if (volume_info->seal) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002933 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
Aurelien Aptelb327a712018-01-24 13:46:10 +01002934 seal = true;
2935 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002936 cifs_server_dbg(VFS,
Aurelien Aptelb327a712018-01-24 13:46:10 +01002937 "IPC: server doesn't support encryption\n");
2938 return -EOPNOTSUPP;
2939 }
2940 }
2941
2942 tcon = tconInfoAlloc();
2943 if (tcon == NULL)
2944 return -ENOMEM;
2945
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002946 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002947
2948 /* cannot fail */
2949 nls_codepage = load_nls_default();
2950
2951 xid = get_xid();
2952 tcon->ses = ses;
2953 tcon->ipc = true;
2954 tcon->seal = seal;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002955 rc = server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002956 free_xid(xid);
2957
2958 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002959 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002960 tconInfoFree(tcon);
2961 goto out;
2962 }
2963
2964 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2965
2966 ses->tcon_ipc = tcon;
2967out:
2968 unload_nls(nls_codepage);
2969 return rc;
2970}
2971
2972/**
2973 * cifs_free_ipc - helper to release the session IPC tcon
2974 *
2975 * Needs to be called everytime a session is destroyed
2976 */
2977static int
2978cifs_free_ipc(struct cifs_ses *ses)
2979{
2980 int rc = 0, xid;
2981 struct cifs_tcon *tcon = ses->tcon_ipc;
2982
2983 if (tcon == NULL)
2984 return 0;
2985
2986 if (ses->server->ops->tree_disconnect) {
2987 xid = get_xid();
2988 rc = ses->server->ops->tree_disconnect(xid, tcon);
2989 free_xid(xid);
2990 }
2991
2992 if (rc)
2993 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2994
2995 tconInfoFree(tcon);
2996 ses->tcon_ipc = NULL;
2997 return rc;
2998}
2999
Steve French96daf2b2011-05-27 04:34:02 +00003000static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04003001cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
Steve French96daf2b2011-05-27 04:34:02 +00003003 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303005 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04003006 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003007 if (ses->status == CifsExiting)
3008 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003009 if (!match_session(ses, vol))
3010 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05003011 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303012 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003013 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303015 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return NULL;
3017}
3018
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003019void cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05003020{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003021 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05003022 struct TCP_Server_Info *server = ses->server;
3023
Joe Perchesf96637b2013-05-04 22:12:25 -05003024 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003025
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303026 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003027 if (ses->status == CifsExiting) {
3028 spin_unlock(&cifs_tcp_ses_lock);
3029 return;
3030 }
Jeff Layton14fbf502008-11-14 13:53:46 -05003031 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303032 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003033 return;
3034 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003035 if (ses->status == CifsGood)
3036 ses->status = CifsExiting;
3037 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003038
Aurelien Aptelb327a712018-01-24 13:46:10 +01003039 cifs_free_ipc(ses);
3040
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003041 if (ses->status == CifsExiting && server->ops->logoff) {
3042 xid = get_xid();
3043 rc = server->ops->logoff(xid, ses);
3044 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003045 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003046 __func__, rc);
3047 _free_xid(xid);
3048 }
3049
3050 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003051 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303052 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003053
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003054 /* close any extra channels */
3055 if (ses->chan_count > 1) {
3056 int i;
3057
3058 for (i = 1; i < ses->chan_count; i++)
3059 cifs_put_tcp_session(ses->chans[i].server, 0);
3060 }
3061
Jeff Layton14fbf502008-11-14 13:53:46 -05003062 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003063 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05003064}
3065
Jeff Layton8a8798a2012-01-17 16:09:15 -05003066#ifdef CONFIG_KEYS
3067
Chen Gang057d6332013-07-19 09:01:36 +08003068/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
3069#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003070
3071/* Populate username and pw fields from keyring if possible */
3072static int
3073cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
3074{
3075 int rc = 0;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003076 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01003077 const char *delim, *payload;
3078 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003079 ssize_t len;
3080 struct key *key;
3081 struct TCP_Server_Info *server = ses->server;
3082 struct sockaddr_in *sa;
3083 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01003084 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003085
3086 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
3087 if (!desc)
3088 return -ENOMEM;
3089
3090 /* try to find an address key first */
3091 switch (server->dstaddr.ss_family) {
3092 case AF_INET:
3093 sa = (struct sockaddr_in *)&server->dstaddr;
3094 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
3095 break;
3096 case AF_INET6:
3097 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
3098 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
3099 break;
3100 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003101 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
3102 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003103 rc = -EINVAL;
3104 goto out_err;
3105 }
3106
Joe Perchesf96637b2013-05-04 22:12:25 -05003107 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07003108 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05003109 if (IS_ERR(key)) {
3110 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003111 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05003112 rc = PTR_ERR(key);
3113 goto out_err;
3114 }
3115
3116 /* didn't work, try to find a domain key */
3117 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05003118 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07003119 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05003120 if (IS_ERR(key)) {
3121 rc = PTR_ERR(key);
3122 goto out_err;
3123 }
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003124 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003125 }
3126
3127 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00003128 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003129 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05003130 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003131 goto out_key_put;
3132 }
3133
3134 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01003135 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003136 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05003137 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003138 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003139 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
3140 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003141 rc = -EINVAL;
3142 goto out_key_put;
3143 }
3144
3145 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04003146 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003147 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
3148 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003149 rc = -EINVAL;
3150 goto out_key_put;
3151 }
3152
3153 vol->username = kstrndup(payload, len, GFP_KERNEL);
3154 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003155 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
3156 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003157 rc = -ENOMEM;
3158 goto out_key_put;
3159 }
Joe Perchesf96637b2013-05-04 22:12:25 -05003160 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003161
3162 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04003163 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003164 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003165 rc = -EINVAL;
3166 kfree(vol->username);
3167 vol->username = NULL;
3168 goto out_key_put;
3169 }
3170
3171 ++delim;
3172 vol->password = kstrndup(delim, len, GFP_KERNEL);
3173 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003174 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
3175 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003176 rc = -ENOMEM;
3177 kfree(vol->username);
3178 vol->username = NULL;
3179 goto out_key_put;
3180 }
3181
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003182 /*
3183 * If we have a domain key then we must set the domainName in the
3184 * for the request.
3185 */
3186 if (is_domain && ses->domainName) {
3187 vol->domainname = kstrndup(ses->domainName,
3188 strlen(ses->domainName),
3189 GFP_KERNEL);
3190 if (!vol->domainname) {
3191 cifs_dbg(FYI, "Unable to allocate %zd bytes for "
3192 "domain\n", len);
3193 rc = -ENOMEM;
3194 kfree(vol->username);
3195 vol->username = NULL;
Dan Carpenter478228e2019-08-27 13:59:17 +03003196 kzfree(vol->password);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003197 vol->password = NULL;
3198 goto out_key_put;
3199 }
3200 }
3201
Jeff Layton8a8798a2012-01-17 16:09:15 -05003202out_key_put:
3203 up_read(&key->sem);
3204 key_put(key);
3205out_err:
3206 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05003207 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003208 return rc;
3209}
3210#else /* ! CONFIG_KEYS */
3211static inline int
3212cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
3213 struct cifs_ses *ses __attribute__((unused)))
3214{
3215 return -ENOSYS;
3216}
3217#endif /* CONFIG_KEYS */
3218
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003219/**
3220 * cifs_get_smb_ses - get a session matching @volume_info data from @server
3221 *
3222 * This function assumes it is being called from cifs_mount() where we
3223 * already got a server reference (server refcount +1). See
3224 * cifs_get_tcon() for refcount explanations.
3225 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003226struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04003227cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
3228{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003229 int rc = -ENOMEM;
3230 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00003231 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003232 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3233 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04003234
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003235 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04003236
Jeff Layton4ff67b72010-07-06 20:43:02 -04003237 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04003238 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003239 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
3240 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04003241
Jeff Layton36988c72010-04-24 07:57:43 -04003242 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04003243 rc = cifs_negotiate_protocol(xid, ses);
3244 if (rc) {
3245 mutex_unlock(&ses->session_mutex);
3246 /* problem -- put our ses reference */
3247 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003248 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04003249 return ERR_PTR(rc);
3250 }
Jeff Layton36988c72010-04-24 07:57:43 -04003251 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003252 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04003253 rc = cifs_setup_session(xid, ses,
3254 volume_info->local_nls);
3255 if (rc) {
3256 mutex_unlock(&ses->session_mutex);
3257 /* problem -- put our reference */
3258 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003259 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003260 return ERR_PTR(rc);
3261 }
3262 }
3263 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04003264
3265 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003266 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003267 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003268 return ses;
3269 }
3270
Joe Perchesf96637b2013-05-04 22:12:25 -05003271 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04003272 ses = sesInfoAlloc();
3273 if (ses == NULL)
3274 goto get_ses_fail;
3275
3276 /* new SMB session uses our server ref */
3277 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003278 if (server->dstaddr.ss_family == AF_INET6)
3279 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04003280 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003281 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04003282
Steve French8727c8a2011-02-25 01:11:56 -06003283 if (volume_info->username) {
3284 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
3285 if (!ses->user_name)
3286 goto get_ses_fail;
3287 }
Jeff Layton36988c72010-04-24 07:57:43 -04003288
3289 /* volume_info->password freed at unmount */
3290 if (volume_info->password) {
3291 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
3292 if (!ses->password)
3293 goto get_ses_fail;
3294 }
3295 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05003296 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
3297 if (!ses->domainName)
3298 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04003299 }
Germano Percossi39566442016-12-15 12:31:18 +05303300 if (volume_info->domainauto)
3301 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04003302 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04003303 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00003304
Jeff Layton28e11bd2013-05-26 07:01:00 -04003305 ses->sectype = volume_info->sectype;
3306 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04003307 mutex_lock(&ses->session_mutex);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003308
3309 /* add server as first channel */
3310 ses->chans[0].server = server;
3311 ses->chan_count = 1;
3312 ses->chan_max = volume_info->multichannel ? volume_info->max_channels:1;
3313
Jeff Layton198b5682010-04-24 07:57:48 -04003314 rc = cifs_negotiate_protocol(xid, ses);
3315 if (!rc)
3316 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003317
3318 /* each channel uses a different signing key */
3319 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
3320 sizeof(ses->smb3signingkey));
3321
Jeff Layton36988c72010-04-24 07:57:43 -04003322 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00003323 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04003324 goto get_ses_fail;
3325
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003326 /* success, put it on the list and add it as first channel */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303327 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003328 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303329 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003330
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003331 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01003332
3333 cifs_setup_ipc(ses, volume_info);
3334
Jeff Layton36988c72010-04-24 07:57:43 -04003335 return ses;
3336
3337get_ses_fail:
3338 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003339 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003340 return ERR_PTR(rc);
3341}
3342
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003343static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003344{
3345 if (tcon->tidStatus == CifsExiting)
3346 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003347 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003348 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003349 if (tcon->seal != volume_info->seal)
3350 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003351 if (tcon->snapshot_time != volume_info->snapshot_time)
3352 return 0;
Steve Frenchca567eb2019-03-29 16:31:07 -05003353 if (tcon->handle_timeout != volume_info->handle_timeout)
3354 return 0;
Steve French3e7a02d2019-09-11 21:46:20 -05003355 if (tcon->no_lease != volume_info->no_lease)
3356 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003357 return 1;
3358}
3359
Steve French96daf2b2011-05-27 04:34:02 +00003360static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06003361cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
3363 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00003364 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303366 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003367 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00003368 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003369 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05003370 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003371 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303372 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 return tcon;
3374 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303375 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return NULL;
3377}
3378
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003379void
Steve French96daf2b2011-05-27 04:34:02 +00003380cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05003381{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003382 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003383 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003384
Aurelien Aptelb327a712018-01-24 13:46:10 +01003385 /*
3386 * IPC tcon share the lifetime of their session and are
3387 * destroyed in the session put function
3388 */
3389 if (tcon == NULL || tcon->ipc)
3390 return;
3391
3392 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05003393 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303394 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003395 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303396 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003397 return;
3398 }
3399
3400 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303401 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003402
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003403 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003404 if (ses->server->ops->tree_disconnect)
3405 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003406 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003407
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303408 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00003409 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003410 cifs_put_smb_ses(ses);
3411}
3412
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003413/**
3414 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
3415 *
3416 * - tcon refcount is the number of mount points using the tcon.
3417 * - ses refcount is the number of tcon using the session.
3418 *
3419 * 1. This function assumes it is being called from cifs_mount() where
3420 * we already got a session reference (ses refcount +1).
3421 *
3422 * 2. Since we're in the context of adding a mount point, the end
3423 * result should be either:
3424 *
3425 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3426 * its session refcount incremented (1 new tcon). This +1 was
3427 * already done in (1).
3428 *
3429 * b) an existing tcon with refcount+1 (add a mount point to it) and
3430 * identical ses refcount (no new tcon). Because of (1) we need to
3431 * decrement the ses refcount.
3432 */
Steve French96daf2b2011-05-27 04:34:02 +00003433static struct cifs_tcon *
3434cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003435{
3436 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003437 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003438
Steve French8b217fe2016-11-11 22:36:20 -06003439 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003440 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003441 /*
3442 * tcon has refcount already incremented but we need to
3443 * decrement extra ses reference gotten by caller (case b)
3444 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003445 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003446 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003447 return tcon;
3448 }
3449
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003450 if (!ses->server->ops->tree_connect) {
3451 rc = -ENOSYS;
3452 goto out_fail;
3453 }
3454
Jeff Laytond00c28d2010-04-24 07:57:44 -04003455 tcon = tconInfoAlloc();
3456 if (tcon == NULL) {
3457 rc = -ENOMEM;
3458 goto out_fail;
3459 }
3460
Steve French8b217fe2016-11-11 22:36:20 -06003461 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003462 if (ses->server->vals->protocol_id == 0) {
3463 cifs_dbg(VFS,
3464 "Use SMB2 or later for snapshot mount option\n");
3465 rc = -EOPNOTSUPP;
3466 goto out_fail;
3467 } else
3468 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003469 }
3470
Steve Frenchca567eb2019-03-29 16:31:07 -05003471 if (volume_info->handle_timeout) {
3472 if (ses->server->vals->protocol_id == 0) {
3473 cifs_dbg(VFS,
3474 "Use SMB2.1 or later for handle timeout option\n");
3475 rc = -EOPNOTSUPP;
3476 goto out_fail;
3477 } else
3478 tcon->handle_timeout = volume_info->handle_timeout;
3479 }
3480
Jeff Laytond00c28d2010-04-24 07:57:44 -04003481 tcon->ses = ses;
3482 if (volume_info->password) {
3483 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3484 if (!tcon->password) {
3485 rc = -ENOMEM;
3486 goto out_fail;
3487 }
3488 }
3489
Steve French23657ad2018-04-22 15:14:58 -05003490 if (volume_info->seal) {
3491 if (ses->server->vals->protocol_id == 0) {
3492 cifs_dbg(VFS,
3493 "SMB3 or later required for encryption\n");
3494 rc = -EOPNOTSUPP;
3495 goto out_fail;
3496 } else if (tcon->ses->server->capabilities &
3497 SMB2_GLOBAL_CAP_ENCRYPTION)
3498 tcon->seal = true;
3499 else {
3500 cifs_dbg(VFS, "Encryption is not supported on share\n");
3501 rc = -EOPNOTSUPP;
3502 goto out_fail;
3503 }
3504 }
3505
Steve French8505c8b2018-06-18 14:01:59 -05003506 if (volume_info->linux_ext) {
3507 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003508 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003509 printk_once(KERN_WARNING
3510 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003511 } else {
3512 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3513 rc = -EOPNOTSUPP;
3514 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003515 }
Steve Frenchb3266142018-05-20 23:41:10 -05003516 }
Steve Frenchb3266142018-05-20 23:41:10 -05003517
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003518 /*
3519 * BB Do we need to wrap session_mutex around this TCon call and Unix
3520 * SetFS as we do on SessSetup and reconnect?
3521 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003522 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003523 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3524 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003525 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003526 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003527 if (rc)
3528 goto out_fail;
3529
Steve Frenchb618f002015-11-03 09:15:03 -06003530 tcon->use_persistent = false;
3531 /* check if SMB2 or later, CIFS does not support persistent handles */
3532 if (volume_info->persistent) {
3533 if (ses->server->vals->protocol_id == 0) {
3534 cifs_dbg(VFS,
3535 "SMB3 or later required for persistent handles\n");
3536 rc = -EOPNOTSUPP;
3537 goto out_fail;
3538 } else if (ses->server->capabilities &
3539 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3540 tcon->use_persistent = true;
3541 else /* persistent handles requested but not supported */ {
3542 cifs_dbg(VFS,
3543 "Persistent handles not supported on share\n");
3544 rc = -EOPNOTSUPP;
3545 goto out_fail;
3546 }
3547 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3548 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3549 && (volume_info->nopersistent == false)) {
3550 cifs_dbg(FYI, "enabling persistent handles\n");
3551 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003552 } else if (volume_info->resilient) {
3553 if (ses->server->vals->protocol_id == 0) {
3554 cifs_dbg(VFS,
3555 "SMB2.1 or later required for resilient handles\n");
3556 rc = -EOPNOTSUPP;
3557 goto out_fail;
3558 }
3559 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003560 }
3561
Steve Frenchcae53f72019-09-03 17:49:46 -05003562 /* If the user really knows what they are doing they can override */
3563 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
3564 if (volume_info->cache_ro)
3565 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
3566 else if (volume_info->cache_rw)
3567 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
3568 }
3569
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003570 /*
3571 * We can have only one retry value for a connection to a share so for
3572 * resources mounted more than once to the same server share the last
3573 * value passed in for the retry flag is used.
3574 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003575 tcon->retry = volume_info->retry;
3576 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003577 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003578 tcon->local_lease = volume_info->local_lease;
Steve French3e7a02d2019-09-11 21:46:20 -05003579 tcon->no_lease = volume_info->no_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003580 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003581
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303582 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003583 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303584 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003585
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303586 cifs_fscache_get_super_cookie(tcon);
3587
Jeff Laytond00c28d2010-04-24 07:57:44 -04003588 return tcon;
3589
3590out_fail:
3591 tconInfoFree(tcon);
3592 return ERR_PTR(rc);
3593}
3594
Jeff Layton9d002df2010-10-06 19:51:11 -04003595void
3596cifs_put_tlink(struct tcon_link *tlink)
3597{
3598 if (!tlink || IS_ERR(tlink))
3599 return;
3600
3601 if (!atomic_dec_and_test(&tlink->tl_count) ||
3602 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3603 tlink->tl_time = jiffies;
3604 return;
3605 }
3606
3607 if (!IS_ERR(tlink_tcon(tlink)))
3608 cifs_put_tcon(tlink_tcon(tlink));
3609 kfree(tlink);
3610 return;
3611}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003612
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003613static int
3614compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3615{
3616 struct cifs_sb_info *old = CIFS_SB(sb);
3617 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03003618 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
3619 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003620
3621 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3622 return 0;
3623
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03003624 if (old->mnt_cifs_serverino_autodisabled)
3625 newflags &= ~CIFS_MOUNT_SERVER_INUM;
3626
3627 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003628 return 0;
3629
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003630 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003631 * We want to share sb only if we don't specify an r/wsize or
3632 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003633 */
3634 if (new->wsize && new->wsize < old->wsize)
3635 return 0;
3636
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003637 if (new->rsize && new->rsize < old->rsize)
3638 return 0;
3639
Eric W. Biederman1f682332013-02-06 01:20:20 -08003640 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003641 return 0;
3642
3643 if (old->mnt_file_mode != new->mnt_file_mode ||
3644 old->mnt_dir_mode != new->mnt_dir_mode)
3645 return 0;
3646
3647 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3648 return 0;
3649
3650 if (old->actimeo != new->actimeo)
3651 return 0;
3652
3653 return 1;
3654}
3655
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003656static int
3657match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3658{
3659 struct cifs_sb_info *old = CIFS_SB(sb);
3660 struct cifs_sb_info *new = mnt_data->cifs_sb;
Ronnie Sahlbergfe129262020-01-22 11:07:56 +10003661 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3662 old->prepath;
3663 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3664 new->prepath;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003665
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003666 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003667 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003668 else if (!old_set && !new_set)
3669 return 1;
3670
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003671 return 0;
3672}
3673
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003674int
3675cifs_match_super(struct super_block *sb, void *data)
3676{
3677 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3678 struct smb_vol *volume_info;
3679 struct cifs_sb_info *cifs_sb;
3680 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003681 struct cifs_ses *ses;
3682 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003683 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003684 int rc = 0;
3685
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003686 spin_lock(&cifs_tcp_ses_lock);
3687 cifs_sb = CIFS_SB(sb);
3688 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3689 if (IS_ERR(tlink)) {
3690 spin_unlock(&cifs_tcp_ses_lock);
3691 return rc;
3692 }
3693 tcon = tlink_tcon(tlink);
3694 ses = tcon->ses;
3695 tcp_srv = ses->server;
3696
3697 volume_info = mnt_data->vol;
3698
Jeff Layton9fa114f2012-11-26 11:09:57 -05003699 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003700 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003701 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003702 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003703 rc = 0;
3704 goto out;
3705 }
3706
3707 rc = compare_mount_options(sb, mnt_data);
3708out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003709 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003710 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003711 return rc;
3712}
3713
Jeff Layton09e50d52008-07-23 10:11:19 -04003714#ifdef CONFIG_DEBUG_LOCK_ALLOC
3715static struct lock_class_key cifs_key[2];
3716static struct lock_class_key cifs_slock_key[2];
3717
3718static inline void
3719cifs_reclassify_socket4(struct socket *sock)
3720{
3721 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003722 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003723 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3724 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3725}
3726
3727static inline void
3728cifs_reclassify_socket6(struct socket *sock)
3729{
3730 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003731 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003732 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3733 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3734}
3735#else
3736static inline void
3737cifs_reclassify_socket4(struct socket *sock)
3738{
3739}
3740
3741static inline void
3742cifs_reclassify_socket6(struct socket *sock)
3743{
3744}
3745#endif
3746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003748static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
Steve French50c2f752007-07-13 00:33:32 +00003750 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Steve French50c2f752007-07-13 00:33:32 +00003752 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 /* mask a nibble at a time and encode */
3754 target[j] = 'A' + (0x0F & (source[i] >> 4));
3755 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003756 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 }
3758
3759}
3760
Ben Greear3eb9a882010-09-01 17:06:02 -07003761static int
3762bind_socket(struct TCP_Server_Info *server)
3763{
3764 int rc = 0;
3765 if (server->srcaddr.ss_family != AF_UNSPEC) {
3766 /* Bind to the specified local IP address */
3767 struct socket *socket = server->ssocket;
3768 rc = socket->ops->bind(socket,
3769 (struct sockaddr *) &server->srcaddr,
3770 sizeof(server->srcaddr));
3771 if (rc < 0) {
3772 struct sockaddr_in *saddr4;
3773 struct sockaddr_in6 *saddr6;
3774 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3775 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3776 if (saddr6->sin6_family == AF_INET6)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003777 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003778 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003779 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003780 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003781 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003782 }
3783 }
3784 return rc;
3785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
3787static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003788ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
3790 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003791 /*
3792 * some servers require RFC1001 sessinit before sending
3793 * negprot - BB check reconnection in case where second
3794 * sessinit is sent but no second negprot
3795 */
3796 struct rfc1002_session_packet *ses_init_buf;
3797 struct smb_hdr *smb_buf;
3798 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3799 GFP_KERNEL);
3800 if (ses_init_buf) {
3801 ses_init_buf->trailer.session_req.called_len = 32;
3802
Colin Ian King997152f2016-01-25 16:25:54 +00003803 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003804 rfc1002mangle(ses_init_buf->trailer.
3805 session_req.called_name,
3806 server->server_RFC1001_name,
3807 RFC1001_NAME_LEN_WITH_NULL);
3808 else
3809 rfc1002mangle(ses_init_buf->trailer.
3810 session_req.called_name,
3811 DEFAULT_CIFS_CALLED_NAME,
3812 RFC1001_NAME_LEN_WITH_NULL);
3813
3814 ses_init_buf->trailer.session_req.calling_len = 32;
3815
3816 /*
3817 * calling name ends in null (byte 16) from old smb
3818 * convention.
3819 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003820 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003821 rfc1002mangle(ses_init_buf->trailer.
3822 session_req.calling_name,
3823 server->workstation_RFC1001_name,
3824 RFC1001_NAME_LEN_WITH_NULL);
3825 else
3826 rfc1002mangle(ses_init_buf->trailer.
3827 session_req.calling_name,
3828 "LINUX_CIFS_CLNT",
3829 RFC1001_NAME_LEN_WITH_NULL);
3830
3831 ses_init_buf->trailer.session_req.scope1 = 0;
3832 ses_init_buf->trailer.session_req.scope2 = 0;
3833 smb_buf = (struct smb_hdr *)ses_init_buf;
3834
3835 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003836 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003837 rc = smb_send(server, smb_buf, 0x44);
3838 kfree(ses_init_buf);
3839 /*
3840 * RFC1001 layer in at least one server
3841 * requires very short break before negprot
3842 * presumably because not expecting negprot
3843 * to follow so fast. This is a simple
3844 * solution that works without
3845 * complicating the code and causes no
3846 * significant slowing down on mount
3847 * for everyone else
3848 */
3849 usleep_range(1000, 2000);
3850 }
3851 /*
3852 * else the negprot may still work without this
3853 * even though malloc failed
3854 */
3855
3856 return rc;
3857}
3858
3859static int
3860generic_ip_connect(struct TCP_Server_Info *server)
3861{
3862 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003863 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003864 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003865 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003866 struct sockaddr *saddr;
3867
3868 saddr = (struct sockaddr *) &server->dstaddr;
3869
3870 if (server->dstaddr.ss_family == AF_INET6) {
3871 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3872 slen = sizeof(struct sockaddr_in6);
3873 sfamily = AF_INET6;
3874 } else {
3875 sport = ((struct sockaddr_in *) saddr)->sin_port;
3876 slen = sizeof(struct sockaddr_in);
3877 sfamily = AF_INET;
3878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003880 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003881 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3882 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 if (rc < 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003884 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003885 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003888
3889 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003890 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003891 server->ssocket = socket;
3892 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003893 if (sfamily == AF_INET6)
3894 cifs_reclassify_socket6(socket);
3895 else
3896 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 }
3898
Ben Greear3eb9a882010-09-01 17:06:02 -07003899 rc = bind_socket(server);
3900 if (rc < 0)
3901 return rc;
3902
Jeff Laytond5c56052008-12-01 18:42:33 -05003903 /*
3904 * Eventually check for other socket options to change from
3905 * the default. sock_setsockopt not used because it expects
3906 * user space buffer
3907 */
3908 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003909 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003910
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003911 /* make the bufsizes depend on wsize/rsize and max requests */
3912 if (server->noautotune) {
3913 if (socket->sk->sk_sndbuf < (200 * 1024))
3914 socket->sk->sk_sndbuf = 200 * 1024;
3915 if (socket->sk->sk_rcvbuf < (140 * 1024))
3916 socket->sk->sk_rcvbuf = 140 * 1024;
3917 }
3918
Steve French6a5fa2362010-01-01 01:28:43 +00003919 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003920 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003921 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3922 (char *)&val, sizeof(val));
3923 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003924 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3925 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003926 }
3927
Joe Perchesf96637b2013-05-04 22:12:25 -05003928 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003929 socket->sk->sk_sndbuf,
3930 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3931
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03003932 rc = socket->ops->connect(socket, saddr, slen,
3933 server->noblockcnt ? O_NONBLOCK : 0);
Paulo Alcantara (SUSE)d532cc72019-10-10 12:31:58 -03003934 /*
3935 * When mounting SMB root file systems, we do not want to block in
3936 * connect. Otherwise bail out and then let cifs_reconnect() perform
3937 * reconnect failover - if possible.
3938 */
3939 if (server->noblockcnt && rc == -EINPROGRESS)
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03003940 rc = 0;
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003941 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003942 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003943 sock_release(socket);
3944 server->ssocket = NULL;
3945 return rc;
3946 }
3947
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003948 if (sport == htons(RFC1001_PORT))
3949 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 return rc;
3952}
3953
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003954static int
3955ip_connect(struct TCP_Server_Info *server)
3956{
Steve French6da97912011-03-13 18:55:55 +00003957 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003958 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3959 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3960
3961 if (server->dstaddr.ss_family == AF_INET6)
3962 sport = &addr6->sin6_port;
3963 else
3964 sport = &addr->sin_port;
3965
3966 if (*sport == 0) {
3967 int rc;
3968
3969 /* try with 445 port at first */
3970 *sport = htons(CIFS_PORT);
3971
3972 rc = generic_ip_connect(server);
3973 if (rc >= 0)
3974 return rc;
3975
3976 /* if it failed, try with 139 port */
3977 *sport = htons(RFC1001_PORT);
3978 }
3979
3980 return generic_ip_connect(server);
3981}
3982
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003983void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003984 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003985{
3986 /* if we are reconnecting then should we check to see if
3987 * any requested capabilities changed locally e.g. via
3988 * remount but we can not do much about it here
3989 * if they have (even if we could detect it by the following)
3990 * Perhaps we could add a backpointer to array of sb from tcon
3991 * or if we change to make all sb to same share the same
3992 * sb as NFS - then we only have one backpointer to sb.
3993 * What if we wanted to mount the server share twice once with
3994 * and once without posixacls or posix paths? */
3995 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003996
Steve Frenchc18c8422007-07-18 23:21:09 +00003997 if (vol_info && vol_info->no_linux_ext) {
3998 tcon->fsUnixInfo.Capability = 0;
3999 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05004000 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00004001 return;
4002 } else if (vol_info)
4003 tcon->unix_ext = 1; /* Unix Extensions supported */
4004
4005 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004006 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00004007 return;
4008 }
Steve French50c2f752007-07-13 00:33:32 +00004009
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004010 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00004011 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05004012 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00004013 /* check for reconnect case in which we do not
4014 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004015 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00004016 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00004017 originally at mount time */
4018 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
4019 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00004020 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
4021 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004022 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00004023 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00004024 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004025 cifs_dbg(VFS, "possible reconnect error\n");
4026 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00004027 }
Steve French8af18972007-02-14 04:42:51 +00004028 }
Steve French50c2f752007-07-13 00:33:32 +00004029
Steve French6848b732011-05-26 18:38:54 +00004030 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004031 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00004032
Steve French8af18972007-02-14 04:42:51 +00004033 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00004034 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00004035 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00004036 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004037 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04004038 if (cifs_sb)
4039 cifs_sb->mnt_cifs_flags |=
4040 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00004041 }
4042
Steve French75865f8c2007-06-24 18:30:48 +00004043 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00004044 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00004045 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004046 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04004047 if (cifs_sb)
4048 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00004049 CIFS_MOUNT_POSIX_PATHS;
4050 }
Steve French50c2f752007-07-13 00:33:32 +00004051
Joe Perchesf96637b2013-05-04 22:12:25 -05004052 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00004053#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00004054 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004055 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004056 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004057 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004058 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004059 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004060 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004061 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004062 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004063 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004064 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004065 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004066 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004067 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00004068 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004069 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00004070 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004071 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00004072#endif /* CIFS_DEBUG2 */
4073 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00004074 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004075 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00004076 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004077 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 +00004078
Steve French8af18972007-02-14 04:42:51 +00004079 }
4080 }
4081}
4082
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01004083int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004084 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04004085{
Jeff Layton2de970f2010-10-06 19:51:12 -04004086 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
4087
Al Viro2ced6f62011-06-17 09:20:04 -04004088 spin_lock_init(&cifs_sb->tlink_tree_lock);
4089 cifs_sb->tlink_tree = RB_ROOT;
4090
Steve Frenche8506d22019-02-28 21:32:15 -06004091 cifs_sb->bsize = pvolume_info->bsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04004092 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04004093 * Temporarily set r/wsize for matching superblock. If we end up using
4094 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04004095 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04004096 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04004097 cifs_sb->wsize = pvolume_info->wsize;
4098
Steve French3b795212008-11-13 19:45:32 +00004099 cifs_sb->mnt_uid = pvolume_info->linux_uid;
4100 cifs_sb->mnt_gid = pvolume_info->linux_gid;
4101 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
4102 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Frank Sorensonf52aa792020-02-12 15:31:48 -06004103 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05004104 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00004105
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05304106 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004107 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05304108
Aurelien Aptel83930722018-09-20 18:10:25 -07004109 if (pvolume_info->nodfs)
4110 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
Steve French3b795212008-11-13 19:45:32 +00004111 if (pvolume_info->noperm)
4112 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
4113 if (pvolume_info->setuids)
4114 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05004115 if (pvolume_info->setuidfromacl)
4116 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00004117 if (pvolume_info->server_ino)
4118 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
4119 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05004120 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
4121 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00004122 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
4123 if (pvolume_info->no_xattr)
4124 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
4125 if (pvolume_info->sfu_emul)
4126 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
4127 if (pvolume_info->nobrl)
4128 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05004129 if (pvolume_info->nohandlecache)
4130 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00004131 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00004132 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00004133 if (pvolume_info->mand_lock)
4134 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00004135 if (pvolume_info->rwpidforward)
4136 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French412094a2019-06-24 02:01:42 -05004137 if (pvolume_info->mode_ace)
4138 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MODE_FROM_SID;
Steve French3b795212008-11-13 19:45:32 +00004139 if (pvolume_info->cifs_acl)
4140 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01004141 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05004142 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01004143 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
4144 }
4145 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05004146 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01004147 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
4148 }
Steve French3b795212008-11-13 19:45:32 +00004149 if (pvolume_info->override_uid)
4150 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
4151 if (pvolume_info->override_gid)
4152 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
4153 if (pvolume_info->dynperm)
4154 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05304155 if (pvolume_info->fsc)
4156 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04004157 if (pvolume_info->multiuser)
4158 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
4159 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05004160 if (pvolume_info->strict_io)
4161 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00004162 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004163 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00004164 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
4165 }
Steve French83bbfa72019-08-27 23:58:54 -05004166 if (pvolume_info->cache_ro) {
4167 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
4168 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
Steve French41e033f2019-08-30 02:12:41 -05004169 } else if (pvolume_info->cache_rw) {
4170 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
4171 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
4172 CIFS_MOUNT_RW_CACHE);
Steve French83bbfa72019-08-27 23:58:54 -05004173 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004174 if (pvolume_info->mfsymlinks) {
4175 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05004176 /*
4177 * Our SFU ("Services for Unix" emulation does not allow
4178 * creating symlinks but does allow reading existing SFU
4179 * symlinks (it does allow both creating and reading SFU
4180 * style mknod and FIFOs though). When "mfsymlinks" and
4181 * "sfu" are both enabled at the same time, it allows
4182 * reading both types of symlinks, but will only create
4183 * them with mfsymlinks format. This allows better
4184 * Apple compatibility (probably better for Samba too)
4185 * while still recognizing old Windows style symlinks.
4186 */
4187 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004188 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05004189 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004190 }
Steve French3b795212008-11-13 19:45:32 +00004191
4192 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05004193 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01004194
4195 if (pvolume_info->prepath) {
4196 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
4197 if (cifs_sb->prepath == NULL)
4198 return -ENOMEM;
4199 }
4200
4201 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04004202}
4203
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004204void
4205cifs_cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004206{
Sean Finneyb9468452011-04-11 13:19:32 +00004207 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004208 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02004209 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00004210 kfree(volume_info->domainname);
4211 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004212 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004213}
4214
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004215void
4216cifs_cleanup_volume_info(struct smb_vol *volume_info)
4217{
4218 if (!volume_info)
4219 return;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004220 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004221 kfree(volume_info);
4222}
4223
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004224/* Release all succeed connections */
4225static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
4226 unsigned int xid,
4227 struct TCP_Server_Info *server,
4228 struct cifs_ses *ses, struct cifs_tcon *tcon)
4229{
4230 int rc = 0;
4231
4232 if (tcon)
4233 cifs_put_tcon(tcon);
4234 else if (ses)
4235 cifs_put_smb_ses(ses);
4236 else if (server)
4237 cifs_put_tcp_session(server, 0);
4238 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
4239 free_xid(xid);
4240}
4241
4242/* Get connections for tcp, ses and tcon */
4243static int mount_get_conns(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
4244 unsigned int *xid,
4245 struct TCP_Server_Info **nserver,
4246 struct cifs_ses **nses, struct cifs_tcon **ntcon)
4247{
4248 int rc = 0;
4249 struct TCP_Server_Info *server;
4250 struct cifs_ses *ses;
4251 struct cifs_tcon *tcon;
4252
4253 *nserver = NULL;
4254 *nses = NULL;
4255 *ntcon = NULL;
4256
4257 *xid = get_xid();
4258
4259 /* get a reference to a tcp session */
4260 server = cifs_get_tcp_session(vol);
4261 if (IS_ERR(server)) {
4262 rc = PTR_ERR(server);
4263 return rc;
4264 }
4265
4266 *nserver = server;
4267
4268 if ((vol->max_credits < 20) || (vol->max_credits > 60000))
4269 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
4270 else
4271 server->max_credits = vol->max_credits;
4272
4273 /* get a reference to a SMB session */
4274 ses = cifs_get_smb_ses(server, vol);
4275 if (IS_ERR(ses)) {
4276 rc = PTR_ERR(ses);
4277 return rc;
4278 }
4279
4280 *nses = ses;
4281
4282 if ((vol->persistent == true) && (!(ses->server->capabilities &
4283 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10004284 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004285 return -EOPNOTSUPP;
4286 }
4287
4288 /* search for existing tcon to this server share */
4289 tcon = cifs_get_tcon(ses, vol);
4290 if (IS_ERR(tcon)) {
4291 rc = PTR_ERR(tcon);
4292 return rc;
4293 }
4294
4295 *ntcon = tcon;
4296
4297 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4298 if (tcon->posix_extensions)
4299 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4300
4301 /* tell server which Unix caps we support */
4302 if (cap_unix(tcon->ses)) {
4303 /*
4304 * reset of caps checks mount to see if unix extensions disabled
4305 * for just this mount.
4306 */
4307 reset_cifs_unix_caps(*xid, tcon, cifs_sb, vol);
4308 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4309 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4310 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
4311 return -EACCES;
4312 } else
4313 tcon->unix_ext = 0; /* server does not support them */
4314
4315 /* do not care if a following call succeed - informational */
Steve French1981eba2019-08-29 22:33:38 -05004316 if (!tcon->pipe && server->ops->qfs_tcon) {
Amir Goldstein0f060932020-02-03 21:46:43 +02004317 server->ops->qfs_tcon(*xid, tcon, cifs_sb);
Steve French1981eba2019-08-29 22:33:38 -05004318 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
4319 if (tcon->fsDevInfo.DeviceCharacteristics &
Steve French52870d52019-10-01 21:25:46 -05004320 cpu_to_le32(FILE_READ_ONLY_DEVICE))
Steve French1981eba2019-08-29 22:33:38 -05004321 cifs_dbg(VFS, "mounted to read only share\n");
Steve French41e033f2019-08-30 02:12:41 -05004322 else if ((cifs_sb->mnt_cifs_flags &
4323 CIFS_MOUNT_RW_CACHE) == 0)
Steve French1981eba2019-08-29 22:33:38 -05004324 cifs_dbg(VFS, "read only mount of RW share\n");
Steve French41e033f2019-08-30 02:12:41 -05004325 /* no need to log a RW mount of a typical RW share */
Steve French1981eba2019-08-29 22:33:38 -05004326 }
4327 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004328
4329 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, vol);
4330 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, vol);
4331
4332 return 0;
4333}
4334
4335static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
4336 struct cifs_tcon *tcon)
4337{
4338 struct tcon_link *tlink;
4339
4340 /* hang the tcon off of the superblock */
4341 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4342 if (tlink == NULL)
4343 return -ENOMEM;
4344
4345 tlink->tl_uid = ses->linux_uid;
4346 tlink->tl_tcon = tcon;
4347 tlink->tl_time = jiffies;
4348 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4349 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4350
4351 cifs_sb->master_tlink = tlink;
4352 spin_lock(&cifs_sb->tlink_tree_lock);
4353 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4354 spin_unlock(&cifs_sb->tlink_tree_lock);
4355
4356 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4357 TLINK_IDLE_EXPIRE);
4358 return 0;
4359}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004360
Steve French2d6d5892009-04-09 00:36:44 +00004361#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06004362/*
4363 * cifs_build_path_to_root returns full path to root when we do not have an
4364 * exiting connection (tcon)
4365 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004366static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004367build_unc_path_to_root(const struct smb_vol *vol,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004368 const struct cifs_sb_info *cifs_sb, bool useppath)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004369{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004370 char *full_path, *pos;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004371 unsigned int pplen = useppath && vol->prepath ?
4372 strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004373 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004374
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004375 if (unc_len > MAX_TREE_SIZE)
4376 return ERR_PTR(-EINVAL);
4377
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004378 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004379 if (full_path == NULL)
4380 return ERR_PTR(-ENOMEM);
4381
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004382 memcpy(full_path, vol->UNC, unc_len);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004383 pos = full_path + unc_len;
4384
4385 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04004386 *pos = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004387 memcpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004388 pos += pplen;
4389 }
4390
4391 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00004392 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05004393 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004394 return full_path;
4395}
Sean Finneydd613942011-04-11 13:19:30 +00004396
Paulo Alcantara1c780222018-11-14 16:24:03 -02004397/**
4398 * expand_dfs_referral - Perform a dfs referral query and update the cifs_sb
4399 *
Sean Finneydd613942011-04-11 13:19:30 +00004400 *
Sean Finney046462a2011-04-11 13:19:33 +00004401 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
4402 * to a string containing updated options for the submount. Otherwise it
4403 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00004404 *
4405 * Returns the rc from get_dfs_path to the caller, which can be used to
4406 * determine whether there were referrals.
4407 */
4408static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004409expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00004410 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00004411 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00004412{
4413 int rc;
Paulo Alcantara1c780222018-11-14 16:24:03 -02004414 struct dfs_info3_param referral = {0};
Sean Finneydd613942011-04-11 13:19:30 +00004415 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
4416
Aurelien Aptel83930722018-09-20 18:10:25 -07004417 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
4418 return -EREMOTE;
4419
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004420 full_path = build_unc_path_to_root(volume_info, cifs_sb, true);
Sean Finneydd613942011-04-11 13:19:30 +00004421 if (IS_ERR(full_path))
4422 return PTR_ERR(full_path);
4423
4424 /* For DFS paths, skip the first '\' of the UNC */
4425 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
4426
Paulo Alcantara1c780222018-11-14 16:24:03 -02004427 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
4428 ref_path, &referral, NULL);
4429 if (!rc) {
Sean Finneydd613942011-04-11 13:19:30 +00004430 char *fake_devname = NULL;
4431
4432 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02004433 full_path + 1, &referral,
Sean Finneydd613942011-04-11 13:19:30 +00004434 &fake_devname);
Paulo Alcantara1c780222018-11-14 16:24:03 -02004435 free_dfs_info_param(&referral);
Sean Finney046462a2011-04-11 13:19:33 +00004436
Sean Finneydd613942011-04-11 13:19:30 +00004437 if (IS_ERR(mdata)) {
4438 rc = PTR_ERR(mdata);
4439 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004440 } else {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004441 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004442 rc = cifs_setup_volume_info(volume_info, mdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02004443 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00004444 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004445 kfree(fake_devname);
4446 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00004447 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00004448 }
4449 kfree(full_path);
4450 return rc;
4451}
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004452
4453static inline int get_next_dfs_tgt(const char *path,
4454 struct dfs_cache_tgt_list *tgt_list,
4455 struct dfs_cache_tgt_iterator **tgt_it)
4456{
4457 if (!*tgt_it)
4458 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
4459 else
4460 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
4461 return !*tgt_it ? -EHOSTDOWN : 0;
4462}
4463
4464static int update_vol_info(const struct dfs_cache_tgt_iterator *tgt_it,
4465 struct smb_vol *fake_vol, struct smb_vol *vol)
4466{
4467 const char *tgt = dfs_cache_get_tgt_name(tgt_it);
4468 int len = strlen(tgt) + 2;
4469 char *new_unc;
4470
4471 new_unc = kmalloc(len, GFP_KERNEL);
4472 if (!new_unc)
4473 return -ENOMEM;
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10004474 scnprintf(new_unc, len, "\\%s", tgt);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004475
4476 kfree(vol->UNC);
4477 vol->UNC = new_unc;
4478
4479 if (fake_vol->prepath) {
4480 kfree(vol->prepath);
4481 vol->prepath = fake_vol->prepath;
4482 fake_vol->prepath = NULL;
4483 }
4484 memcpy(&vol->dstaddr, &fake_vol->dstaddr, sizeof(vol->dstaddr));
4485
4486 return 0;
4487}
4488
4489static int setup_dfs_tgt_conn(const char *path,
4490 const struct dfs_cache_tgt_iterator *tgt_it,
4491 struct cifs_sb_info *cifs_sb,
4492 struct smb_vol *vol,
4493 unsigned int *xid,
4494 struct TCP_Server_Info **server,
4495 struct cifs_ses **ses,
4496 struct cifs_tcon **tcon)
4497{
4498 int rc;
4499 struct dfs_info3_param ref = {0};
4500 char *mdata = NULL, *fake_devname = NULL;
Steve Frenchd0959b02019-10-05 10:53:58 -05004501 struct smb_vol fake_vol = {NULL};
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004502
4503 cifs_dbg(FYI, "%s: dfs path: %s\n", __func__, path);
4504
4505 rc = dfs_cache_get_tgt_referral(path, tgt_it, &ref);
4506 if (rc)
4507 return rc;
4508
4509 mdata = cifs_compose_mount_options(cifs_sb->mountdata, path, &ref,
4510 &fake_devname);
4511 free_dfs_info_param(&ref);
4512
4513 if (IS_ERR(mdata)) {
4514 rc = PTR_ERR(mdata);
4515 mdata = NULL;
4516 } else {
4517 cifs_dbg(FYI, "%s: fake_devname: %s\n", __func__, fake_devname);
4518 rc = cifs_setup_volume_info(&fake_vol, mdata, fake_devname,
4519 false);
4520 }
4521 kfree(mdata);
4522 kfree(fake_devname);
4523
4524 if (!rc) {
4525 /*
4526 * We use a 'fake_vol' here because we need pass it down to the
4527 * mount_{get,put} functions to test connection against new DFS
4528 * targets.
4529 */
4530 mount_put_conns(cifs_sb, *xid, *server, *ses, *tcon);
4531 rc = mount_get_conns(&fake_vol, cifs_sb, xid, server, ses,
4532 tcon);
4533 if (!rc) {
4534 /*
4535 * We were able to connect to new target server.
4536 * Update current volume info with new target server.
4537 */
4538 rc = update_vol_info(tgt_it, &fake_vol, vol);
4539 }
4540 }
4541 cifs_cleanup_volume_info_contents(&fake_vol);
4542 return rc;
4543}
4544
4545static int mount_do_dfs_failover(const char *path,
4546 struct cifs_sb_info *cifs_sb,
4547 struct smb_vol *vol,
4548 struct cifs_ses *root_ses,
4549 unsigned int *xid,
4550 struct TCP_Server_Info **server,
4551 struct cifs_ses **ses,
4552 struct cifs_tcon **tcon)
4553{
4554 int rc;
4555 struct dfs_cache_tgt_list tgt_list;
4556 struct dfs_cache_tgt_iterator *tgt_it = NULL;
4557
4558 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
4559 return -EOPNOTSUPP;
4560
4561 rc = dfs_cache_noreq_find(path, NULL, &tgt_list);
4562 if (rc)
4563 return rc;
4564
4565 for (;;) {
4566 /* Get next DFS target server - if any */
4567 rc = get_next_dfs_tgt(path, &tgt_list, &tgt_it);
4568 if (rc)
4569 break;
4570 /* Connect to next DFS target */
4571 rc = setup_dfs_tgt_conn(path, tgt_it, cifs_sb, vol, xid, server,
4572 ses, tcon);
4573 if (!rc || rc == -EACCES || rc == -EOPNOTSUPP)
4574 break;
4575 }
4576 if (!rc) {
4577 /*
4578 * Update DFS target hint in DFS referral cache with the target
4579 * server we successfully reconnected to.
4580 */
4581 rc = dfs_cache_update_tgthint(*xid, root_ses ? root_ses : *ses,
4582 cifs_sb->local_nls,
4583 cifs_remap(cifs_sb), path,
4584 tgt_it);
4585 }
4586 dfs_cache_free_tgts(&tgt_list);
4587 return rc;
4588}
Steve French2d6d5892009-04-09 00:36:44 +00004589#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004590
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004591int
Jeff Layton04db79b2011-07-06 08:10:38 -04004592cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05004593 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004595 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00004596
Steve Frenchc7c137b2018-06-06 17:59:29 -05004597 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04004598 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Jeff Layton7586b762008-12-01 18:41:49 -05004600 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004601 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05004602 kfree(volume_info->username);
4603 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05004604 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05004606 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05004608 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00004609 /* In userspace mount helper we can get user name from alternate
4610 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04004611 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 }
4613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05004615 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004616 /* load_nls_default cannot return null */
4617 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004619 volume_info->local_nls = load_nls(volume_info->iocharset);
4620 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004621 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004622 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04004623 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 }
4625 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004626
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004627 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04004628}
4629
4630struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05004631cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04004632{
4633 int rc;
4634 struct smb_vol *volume_info;
4635
Jeff Layton6ee95422012-11-26 11:09:57 -05004636 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04004637 if (!volume_info)
4638 return ERR_PTR(-ENOMEM);
4639
Steve Frenchc7c137b2018-06-06 17:59:29 -05004640 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04004641 if (rc) {
4642 cifs_cleanup_volume_info(volume_info);
4643 volume_info = ERR_PTR(rc);
4644 }
4645
4646 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004647}
4648
Aurelien Aptela6b50582016-05-25 19:59:09 +02004649static int
4650cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4651 unsigned int xid,
4652 struct cifs_tcon *tcon,
4653 struct cifs_sb_info *cifs_sb,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004654 char *full_path,
4655 int added_treename)
Aurelien Aptela6b50582016-05-25 19:59:09 +02004656{
4657 int rc;
4658 char *s;
4659 char sep, tmp;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004660 int skip = added_treename ? 1 : 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +02004661
4662 sep = CIFS_DIR_SEP(cifs_sb);
4663 s = full_path;
4664
4665 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4666 while (rc == 0) {
4667 /* skip separators */
4668 while (*s == sep)
4669 s++;
4670 if (!*s)
4671 break;
4672 /* next separator */
4673 while (*s && *s != sep)
4674 s++;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004675 /*
4676 * if the treename is added, we then have to skip the first
4677 * part within the separators
4678 */
4679 if (skip) {
4680 skip = 0;
4681 continue;
4682 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004683 /*
4684 * temporarily null-terminate the path at the end of
4685 * the current component
4686 */
4687 tmp = *s;
4688 *s = 0;
4689 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4690 full_path);
4691 *s = tmp;
4692 }
4693 return rc;
4694}
4695
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004696/*
4697 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
4698 * otherwise 0.
4699 */
4700static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
4701 const unsigned int xid,
4702 struct TCP_Server_Info *server,
4703 struct cifs_tcon *tcon)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004704{
Jeff Layton1daaae82012-03-21 06:30:40 -04004705 int rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004706 char *full_path;
4707
4708 if (!server->ops->is_path_accessible)
4709 return -EOPNOTSUPP;
4710
4711 /*
4712 * cifs_build_path_to_root works only when we have a valid tcon
4713 */
4714 full_path = cifs_build_path_to_root(vol, cifs_sb, tcon,
4715 tcon->Flags & SMB_SHARE_IS_IN_DFS);
4716 if (full_path == NULL)
4717 return -ENOMEM;
4718
4719 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
4720
4721 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4722 full_path);
4723 if (rc != 0 && rc != -EREMOTE) {
4724 kfree(full_path);
4725 return rc;
4726 }
4727
4728 if (rc != -EREMOTE) {
4729 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004730 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004731 if (rc != 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10004732 cifs_server_dbg(VFS, "cannot query dirs between root and final path, "
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004733 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4734 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4735 rc = 0;
4736 }
4737 }
4738
4739 kfree(full_path);
4740 return rc;
4741}
4742
4743#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004744static inline void set_root_tcon(struct cifs_sb_info *cifs_sb,
4745 struct cifs_tcon *tcon,
4746 struct cifs_tcon **root)
4747{
4748 spin_lock(&cifs_tcp_ses_lock);
4749 tcon->tc_count++;
4750 tcon->remap = cifs_remap(cifs_sb);
4751 spin_unlock(&cifs_tcp_ses_lock);
4752 *root = tcon;
4753}
4754
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004755int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4756{
4757 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004758 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004759 struct cifs_ses *ses;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004760 struct cifs_tcon *root_tcon = NULL;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004761 struct cifs_tcon *tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004762 struct TCP_Server_Info *server;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004763 char *root_path = NULL, *full_path = NULL;
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004764 char *old_mountdata, *origin_mountdata = NULL;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004765 int count;
Al Virodd854462011-06-17 08:24:42 -04004766
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004767 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4768 if (!rc && tcon) {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004769 /* If not a standalone DFS root, then check if path is remote */
4770 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls,
4771 cifs_remap(cifs_sb), vol->UNC + 1, NULL,
4772 NULL);
4773 if (rc) {
4774 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4775 if (!rc)
4776 goto out;
4777 if (rc != -EREMOTE)
4778 goto error;
4779 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004780 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004781 /*
4782 * If first DFS target server went offline and we failed to connect it,
4783 * server and ses pointers are NULL at this point, though we still have
4784 * chance to get a cached DFS referral in expand_dfs_referral() and
4785 * retry next target available in it.
4786 *
4787 * If a NULL ses ptr is passed to dfs_cache_find(), a lookup will be
4788 * performed against DFS path and *no* requests will be sent to server
4789 * for any new DFS referrals. Hence it's safe to skip checking whether
4790 * server or ses ptr is NULL.
4791 */
4792 if (rc == -EACCES || rc == -EOPNOTSUPP)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004793 goto error;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004794
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004795 root_path = build_unc_path_to_root(vol, cifs_sb, false);
4796 if (IS_ERR(root_path)) {
4797 rc = PTR_ERR(root_path);
4798 root_path = NULL;
4799 goto error;
4800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004802 full_path = build_unc_path_to_root(vol, cifs_sb, true);
4803 if (IS_ERR(full_path)) {
4804 rc = PTR_ERR(full_path);
4805 full_path = NULL;
4806 goto error;
4807 }
Sean Finneyc1508ca2011-04-11 13:19:31 +00004808 /*
4809 * Perform an unconditional check for whether there are DFS
4810 * referrals for this path without prefix, to provide support
4811 * for DFS referrals from w2k8 servers which don't seem to respond
4812 * with PATH_NOT_COVERED to requests that include the prefix.
4813 * Chase the referral if found, otherwise continue normally.
4814 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004815 old_mountdata = cifs_sb->mountdata;
4816 (void)expand_dfs_referral(xid, ses, vol, cifs_sb, false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004817
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004818 if (cifs_sb->mountdata == NULL) {
4819 rc = -ENOENT;
4820 goto error;
4821 }
4822
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004823 /* Save DFS root volume information for DFS refresh worker */
4824 origin_mountdata = kstrndup(cifs_sb->mountdata,
4825 strlen(cifs_sb->mountdata), GFP_KERNEL);
4826 if (!origin_mountdata) {
4827 rc = -ENOMEM;
4828 goto error;
4829 }
4830
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004831 if (cifs_sb->mountdata != old_mountdata) {
4832 /* If we were redirected, reconnect to new target server */
4833 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4834 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4835 }
4836 if (rc) {
4837 if (rc == -EACCES || rc == -EOPNOTSUPP)
4838 goto error;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004839 /* Perform DFS failover to any other DFS targets */
4840 rc = mount_do_dfs_failover(root_path + 1, cifs_sb, vol, NULL,
4841 &xid, &server, &ses, &tcon);
4842 if (rc)
4843 goto error;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004844 }
4845
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004846 kfree(root_path);
4847 root_path = build_unc_path_to_root(vol, cifs_sb, false);
4848 if (IS_ERR(root_path)) {
4849 rc = PTR_ERR(root_path);
4850 root_path = NULL;
4851 goto error;
4852 }
4853 /* Cache out resolved root server */
4854 (void)dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
4855 root_path + 1, NULL, NULL);
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004856 kfree(root_path);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004857 root_path = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004858
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004859 set_root_tcon(cifs_sb, tcon, &root_tcon);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004860
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004861 for (count = 1; ;) {
4862 if (!rc && tcon) {
4863 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4864 if (!rc || rc != -EREMOTE)
4865 break;
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004866 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004867 /*
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004868 * BB: when we implement proper loop detection,
4869 * we will remove this check. But now we need it
4870 * to prevent an indefinite loop if 'DFS tree' is
4871 * misconfigured (i.e. has loops).
Steve French6d3ea7e2012-11-28 22:34:41 -06004872 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004873 if (count++ > MAX_NESTED_LINKS) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004874 rc = -ELOOP;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004875 break;
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004876 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004877
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004878 kfree(full_path);
4879 full_path = build_unc_path_to_root(vol, cifs_sb, true);
4880 if (IS_ERR(full_path)) {
4881 rc = PTR_ERR(full_path);
4882 full_path = NULL;
4883 break;
4884 }
4885
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004886 old_mountdata = cifs_sb->mountdata;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004887 rc = expand_dfs_referral(xid, root_tcon->ses, vol, cifs_sb,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004888 true);
4889 if (rc)
4890 break;
Jeff Layton7b91e262009-07-23 15:22:30 -04004891
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004892 if (cifs_sb->mountdata != old_mountdata) {
4893 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4894 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses,
4895 &tcon);
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004896 /*
4897 * Ensure that DFS referrals go through new root server.
4898 */
4899 if (!rc && tcon &&
4900 (tcon->share_flags & (SHI1005_FLAGS_DFS |
4901 SHI1005_FLAGS_DFS_ROOT))) {
4902 cifs_put_tcon(root_tcon);
4903 set_root_tcon(cifs_sb, tcon, &root_tcon);
4904 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004905 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004906 if (rc) {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004907 if (rc == -EACCES || rc == -EOPNOTSUPP)
4908 break;
4909 /* Perform DFS failover to any other DFS targets */
4910 rc = mount_do_dfs_failover(full_path + 1, cifs_sb, vol,
4911 root_tcon->ses, &xid,
4912 &server, &ses, &tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004913 if (rc == -EACCES || rc == -EOPNOTSUPP || !server ||
4914 !ses)
4915 goto error;
4916 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004917 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004918 cifs_put_tcon(root_tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004919
Jeff Layton9d002df2010-10-06 19:51:11 -04004920 if (rc)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004921 goto error;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004922
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004923 spin_lock(&cifs_tcp_ses_lock);
4924 if (!tcon->dfs_path) {
4925 /* Save full path in new tcon to do failover when reconnecting tcons */
4926 tcon->dfs_path = full_path;
4927 full_path = NULL;
4928 tcon->remap = cifs_remap(cifs_sb);
4929 }
4930 cifs_sb->origin_fullpath = kstrndup(tcon->dfs_path,
YueHaibing2f0a6172018-12-18 01:34:39 +00004931 strlen(tcon->dfs_path),
4932 GFP_ATOMIC);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004933 if (!cifs_sb->origin_fullpath) {
4934 spin_unlock(&cifs_tcp_ses_lock);
4935 rc = -ENOMEM;
4936 goto error;
4937 }
4938 spin_unlock(&cifs_tcp_ses_lock);
4939
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004940 rc = dfs_cache_add_vol(origin_mountdata, vol, cifs_sb->origin_fullpath);
Paulo Alcantarae511d312018-11-14 17:16:44 -02004941 if (rc) {
4942 kfree(cifs_sb->origin_fullpath);
4943 goto error;
4944 }
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01004945 /*
4946 * After reconnecting to a different server, unique ids won't
4947 * match anymore, so we disable serverino. This prevents
4948 * dentry revalidation to think the dentry are stale (ESTALE).
4949 */
4950 cifs_autodisable_serverino(cifs_sb);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03004951 /*
4952 * Force the use of prefix path to support failover on DFS paths that
4953 * resolve to targets that have different prefix paths.
4954 */
4955 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4956 kfree(cifs_sb->prepath);
4957 cifs_sb->prepath = vol->prepath;
4958 vol->prepath = NULL;
4959
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004960out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004961 free_xid(xid);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02004962 cifs_try_adding_channels(ses);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004963 return mount_setup_tlink(cifs_sb, ses, tcon);
4964
4965error:
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004966 kfree(full_path);
4967 kfree(root_path);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004968 kfree(origin_mountdata);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004969 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 return rc;
4971}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004972#else
4973int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4974{
4975 int rc = 0;
4976 unsigned int xid;
4977 struct cifs_ses *ses;
4978 struct cifs_tcon *tcon;
4979 struct TCP_Server_Info *server;
4980
4981 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4982 if (rc)
4983 goto error;
4984
4985 if (tcon) {
4986 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4987 if (rc == -EREMOTE)
4988 rc = -EOPNOTSUPP;
4989 if (rc)
4990 goto error;
4991 }
4992
4993 free_xid(xid);
4994
4995 return mount_setup_tlink(cifs_sb, ses, tcon);
4996
4997error:
4998 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4999 return rc;
5000}
5001#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Jeff Layton8d1bca32011-06-11 21:17:10 -04005003/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01005004 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04005005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04005007CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00005008 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 const struct nls_table *nls_codepage)
5010{
5011 struct smb_hdr *smb_buffer;
5012 struct smb_hdr *smb_buffer_response;
5013 TCONX_REQ *pSMB;
5014 TCONX_RSP *pSMBr;
5015 unsigned char *bcc_ptr;
5016 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05005017 int length;
5018 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
5020 if (ses == NULL)
5021 return -EIO;
5022
5023 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00005024 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00005026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 smb_buffer_response = smb_buffer;
5028
5029 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
5030 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07005031
Pavel Shilovsky88257362012-05-23 14:01:59 +04005032 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 smb_buffer->Uid = ses->Suid;
5034 pSMB = (TCONX_REQ *) smb_buffer;
5035 pSMBr = (TCONX_RSP *) smb_buffer_response;
5036
5037 pSMB->AndXCommand = 0xFF;
5038 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01005040 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08005041 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00005042 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08005043 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00005044 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08005045 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06005046 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08005047 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
5048 specified as required (when that support is added to
5049 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00005050 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08005051 by Samba (not sure whether other servers allow
5052 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00005053#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04005054 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05005055 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05005056 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00005057 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05005058 SECMODE_PW_ENCRYPT ? true : false,
5059 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00005060 else
5061#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06005062 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05005063 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05005064 if (rc) {
5065 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
5066 __func__, rc);
5067 cifs_buf_release(smb_buffer);
5068 return rc;
5069 }
Steve Frencheeac8042006-01-13 21:34:58 -08005070
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06005071 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00005072 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00005073 /* must align unicode strings */
5074 *bcc_ptr = 0; /* null byte password */
5075 bcc_ptr++;
5076 }
Steve Frencheeac8042006-01-13 21:34:58 -08005077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Jeff Layton38d77c52013-05-26 07:01:00 -04005079 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
5081
5082 if (ses->capabilities & CAP_STATUS32) {
5083 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
5084 }
5085 if (ses->capabilities & CAP_DFS) {
5086 smb_buffer->Flags2 |= SMBFLG2_DFS;
5087 }
5088 if (ses->capabilities & CAP_UNICODE) {
5089 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
5090 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06005091 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00005092 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00005093 (/* server len*/ + 256 /* share len */), nls_codepage);
5094 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 bcc_ptr += 2; /* skip trailing null */
5096 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 strcpy(bcc_ptr, tree);
5098 bcc_ptr += strlen(tree) + 1;
5099 }
5100 strcpy(bcc_ptr, "?????");
5101 bcc_ptr += strlen("?????");
5102 bcc_ptr += 1;
5103 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005104 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
5105 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 pSMB->ByteCount = cpu_to_le16(count);
5107
Steve French133672e2007-11-13 22:41:37 +00005108 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05005109 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01005112 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00005113 bool is_unicode;
5114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00005116 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 tcon->tid = smb_buffer_response->Tid;
5118 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05005119 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04005120 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00005121 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
5122 is_unicode = true;
5123 else
5124 is_unicode = false;
5125
Jeff Laytoncc20c032009-04-30 07:16:21 -04005126
Steve French50c2f752007-07-13 00:33:32 +00005127 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00005128 if (length == 3) {
5129 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
5130 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005131 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01005132 tcon->ipc = true;
5133 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00005134 }
5135 } else if (length == 2) {
5136 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
5137 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05005138 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00005139 }
5140 }
Steve French50c2f752007-07-13 00:33:32 +00005141 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04005142 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05005143 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04005144
5145 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04005146 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06005147 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00005148 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04005149 nls_codepage);
5150
Joe Perchesf96637b2013-05-04 22:12:25 -05005151 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04005152
Steve Frenchfb8c4b12007-07-10 01:16:18 +00005153 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00005154 (smb_buffer_response->WordCount == 7))
5155 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00005156 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
5157 else
5158 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05005159 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 }
5161
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00005162 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 return rc;
5164}
5165
Al Viro2e32cf52013-10-03 12:53:37 -04005166static void delayed_free(struct rcu_head *p)
5167{
5168 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
5169 unload_nls(sbi->local_nls);
5170 kfree(sbi);
5171}
5172
Al Viro2a9b9952011-06-17 09:27:16 -04005173void
5174cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175{
Jeff Laytonb647c352010-10-28 11:16:44 -04005176 struct rb_root *root = &cifs_sb->tlink_tree;
5177 struct rb_node *node;
5178 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Jeff Layton2de970f2010-10-06 19:51:12 -04005180 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
5181
Jeff Laytonb647c352010-10-28 11:16:44 -04005182 spin_lock(&cifs_sb->tlink_tree_lock);
5183 while ((node = rb_first(root))) {
5184 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
5185 cifs_get_tlink(tlink);
5186 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
5187 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00005188
Jeff Laytonb647c352010-10-28 11:16:44 -04005189 spin_unlock(&cifs_sb->tlink_tree_lock);
5190 cifs_put_tlink(tlink);
5191 spin_lock(&cifs_sb->tlink_tree_lock);
5192 }
5193 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005194
Al Virod757d712011-06-17 09:42:43 -04005195 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02005196 kfree(cifs_sb->prepath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02005197#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarae511d312018-11-14 17:16:44 -02005198 dfs_cache_del_vol(cifs_sb->origin_fullpath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02005199 kfree(cifs_sb->origin_fullpath);
5200#endif
Al Viro2e32cf52013-10-03 12:53:37 -04005201 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00005202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005204int
5205cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206{
5207 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02005208 struct TCP_Server_Info *server = cifs_ses_server(ses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005210 if (!server->ops->need_neg || !server->ops->negotiate)
5211 return -ENOSYS;
5212
Jeff Layton198b5682010-04-24 07:57:48 -04005213 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005214 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04005215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005217 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04005218 if (rc == 0) {
5219 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04005220 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04005221 server->tcpStatus = CifsGood;
5222 else
5223 rc = -EHOSTDOWN;
5224 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 }
Steve French26b994f2008-08-06 05:11:33 +00005226
Jeff Layton198b5682010-04-24 07:57:48 -04005227 return rc;
5228}
Steve French26b994f2008-08-06 05:11:33 +00005229
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005230int
5231cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
5232 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04005233{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005234 int rc = -ENOSYS;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02005235 struct TCP_Server_Info *server = cifs_ses_server(ses);
Jeff Layton198b5682010-04-24 07:57:48 -04005236
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02005237 if (!ses->binding) {
5238 ses->capabilities = server->capabilities;
5239 if (linuxExtEnabled == 0)
5240 ses->capabilities &= (~server->vals->cap_unix);
5241
5242 if (ses->auth_key.response) {
5243 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
5244 ses->auth_key.response);
5245 kfree(ses->auth_key.response);
5246 ses->auth_key.response = NULL;
5247 ses->auth_key.len = 0;
5248 }
5249 }
Steve French20418ac2009-04-30 16:13:32 +00005250
Joe Perchesf96637b2013-05-04 22:12:25 -05005251 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00005252 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04005253
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005254 if (server->ops->sess_setup)
5255 rc = server->ops->sess_setup(xid, ses, nls_info);
5256
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05005257 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10005258 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05005259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 return rc;
5261}
5262
Jeff Layton8a8798a2012-01-17 16:09:15 -05005263static int
5264cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
5265{
Jeff Layton3f618222013-06-12 19:52:14 -05005266 vol->sectype = ses->sectype;
5267
5268 /* krb5 is special, since we don't need username or pw */
5269 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05005270 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05005271
5272 return cifs_set_cifscreds(vol, ses);
5273}
5274
Steve French96daf2b2011-05-27 04:34:02 +00005275static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005276cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04005277{
Jeff Layton8a8798a2012-01-17 16:09:15 -05005278 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00005279 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
5280 struct cifs_ses *ses;
5281 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04005282 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04005283
5284 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03005285 if (vol_info == NULL)
5286 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04005287
Jeff Layton9d002df2010-10-06 19:51:11 -04005288 vol_info->local_nls = cifs_sb->local_nls;
5289 vol_info->linux_uid = fsuid;
5290 vol_info->cred_uid = fsuid;
5291 vol_info->UNC = master_tcon->treeName;
5292 vol_info->retry = master_tcon->retry;
5293 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05005294 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04005295 vol_info->local_lease = master_tcon->local_lease;
5296 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04005297 vol_info->sectype = master_tcon->ses->sectype;
5298 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04005299
Jeff Layton8a8798a2012-01-17 16:09:15 -05005300 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
5301 if (rc) {
5302 tcon = ERR_PTR(rc);
5303 goto out;
5304 }
Jeff Layton9d002df2010-10-06 19:51:11 -04005305
5306 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05305307 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005308 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05305309 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005310
5311 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
5312 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00005313 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07005314 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04005315 goto out;
5316 }
5317
5318 tcon = cifs_get_tcon(ses, vol_info);
5319 if (IS_ERR(tcon)) {
5320 cifs_put_smb_ses(ses);
5321 goto out;
5322 }
5323
Steve Frenchce558b02018-05-31 19:16:54 -05005324 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
5325 if (tcon->posix_extensions)
5326 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05005327
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04005328 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04005329 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05005330
Jeff Layton9d002df2010-10-06 19:51:11 -04005331out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05005332 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01005333 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04005334 kfree(vol_info);
5335
5336 return tcon;
5337}
5338
Steve French96daf2b2011-05-27 04:34:02 +00005339struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04005340cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
5341{
5342 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
5343}
5344
Jeff Laytonb647c352010-10-28 11:16:44 -04005345/* find and return a tlink with given uid */
5346static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005347tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04005348{
5349 struct rb_node *node = root->rb_node;
5350 struct tcon_link *tlink;
5351
5352 while (node) {
5353 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
5354
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005355 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005356 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005357 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005358 node = node->rb_right;
5359 else
5360 return tlink;
5361 }
5362 return NULL;
5363}
5364
5365/* insert a tcon_link into the tree */
5366static void
5367tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
5368{
5369 struct rb_node **new = &(root->rb_node), *parent = NULL;
5370 struct tcon_link *tlink;
5371
5372 while (*new) {
5373 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
5374 parent = *new;
5375
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005376 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005377 new = &((*new)->rb_left);
5378 else
5379 new = &((*new)->rb_right);
5380 }
5381
5382 rb_link_node(&new_tlink->tl_rbnode, parent, new);
5383 rb_insert_color(&new_tlink->tl_rbnode, root);
5384}
5385
Jeff Layton9d002df2010-10-06 19:51:11 -04005386/*
5387 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
5388 * current task.
5389 *
5390 * If the superblock doesn't refer to a multiuser mount, then just return
5391 * the master tcon for the mount.
5392 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05305393 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04005394 * exists, then check to see if it's pending construction. If it is then wait
5395 * for construction to complete. Once it's no longer pending, check to see if
5396 * it failed and either return an error or retry construction, depending on
5397 * the timeout.
5398 *
5399 * If one doesn't exist then insert a new tcon_link struct into the tree and
5400 * try to construct a new one.
5401 */
5402struct tcon_link *
5403cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
5404{
5405 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005406 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04005407 struct tcon_link *tlink, *newtlink;
5408
5409 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
5410 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
5411
5412 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04005413 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04005414 if (tlink)
5415 cifs_get_tlink(tlink);
5416 spin_unlock(&cifs_sb->tlink_tree_lock);
5417
5418 if (tlink == NULL) {
5419 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
5420 if (newtlink == NULL)
5421 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04005422 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04005423 newtlink->tl_tcon = ERR_PTR(-EACCES);
5424 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
5425 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
5426 cifs_get_tlink(newtlink);
5427
Jeff Layton9d002df2010-10-06 19:51:11 -04005428 spin_lock(&cifs_sb->tlink_tree_lock);
5429 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04005430 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04005431 if (tlink) {
5432 cifs_get_tlink(tlink);
5433 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005434 kfree(newtlink);
5435 goto wait_for_construction;
5436 }
Jeff Layton9d002df2010-10-06 19:51:11 -04005437 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04005438 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
5439 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005440 } else {
5441wait_for_construction:
5442 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04005443 TASK_INTERRUPTIBLE);
5444 if (ret) {
5445 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10005446 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04005447 }
5448
5449 /* if it's good, return it */
5450 if (!IS_ERR(tlink->tl_tcon))
5451 return tlink;
5452
5453 /* return error if we tried this already recently */
5454 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
5455 cifs_put_tlink(tlink);
5456 return ERR_PTR(-EACCES);
5457 }
5458
5459 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
5460 goto wait_for_construction;
5461 }
5462
5463 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
5464 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
5465 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
5466
5467 if (IS_ERR(tlink->tl_tcon)) {
5468 cifs_put_tlink(tlink);
5469 return ERR_PTR(-EACCES);
5470 }
5471
5472 return tlink;
5473}
Jeff Layton2de970f2010-10-06 19:51:12 -04005474
5475/*
5476 * periodic workqueue job that scans tcon_tree for a superblock and closes
5477 * out tcons.
5478 */
5479static void
5480cifs_prune_tlinks(struct work_struct *work)
5481{
5482 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
5483 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04005484 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00005485 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04005486 struct rb_node *tmp;
5487 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04005488
Jeff Laytonb647c352010-10-28 11:16:44 -04005489 /*
5490 * Because we drop the spinlock in the loop in order to put the tlink
5491 * it's not guarded against removal of links from the tree. The only
5492 * places that remove entries from the tree are this function and
5493 * umounts. Because this function is non-reentrant and is canceled
5494 * before umount can proceed, this is safe.
5495 */
5496 spin_lock(&cifs_sb->tlink_tree_lock);
5497 node = rb_first(root);
5498 while (node != NULL) {
5499 tmp = node;
5500 node = rb_next(tmp);
5501 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
5502
5503 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
5504 atomic_read(&tlink->tl_count) != 0 ||
5505 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
5506 continue;
5507
5508 cifs_get_tlink(tlink);
5509 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
5510 rb_erase(tmp, root);
5511
Jeff Layton2de970f2010-10-06 19:51:12 -04005512 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04005513 cifs_put_tlink(tlink);
5514 spin_lock(&cifs_sb->tlink_tree_lock);
5515 }
5516 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04005517
Jeff Laytonda472fc2012-03-23 14:40:53 -04005518 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04005519 TLINK_IDLE_EXPIRE);
5520}