blob: e2196b36376569239b3a622f4f7d3b9b9e4f1bc9 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return rc;
493 } else
494 server->tcpStatus = CifsNeedReconnect;
495 spin_unlock(&GlobalMid_Lock);
496 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400497 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Steve French6e4d3bb2018-09-22 11:25:04 -0500499 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500500 trace_smb3_reconnect(server->CurrentMid, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* before reconnecting the tcp session, mark the smb session (uid)
503 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500504 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
505 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530506 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500507 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000508 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500509 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500510 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000511 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500512 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100514 if (ses->tcon_ipc)
515 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530517 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500520 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500521 mutex_lock(&server->srv_mutex);
Long Li1d2a4f52019-05-13 21:01:28 -0700522 if (server->ssocket) {
523 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
524 server->ssocket->state, server->ssocket->flags);
525 kernel_sock_shutdown(server->ssocket, SHUT_WR);
526 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
527 server->ssocket->state, server->ssocket->flags);
528 sock_release(server->ssocket);
529 server->ssocket = NULL;
530 }
531 server->sequence_number = 0;
532 server->session_estab = false;
533 kfree(server->session_key.response);
534 server->session_key.response = NULL;
535 server->session_key.len = 0;
536 server->lstrp = jiffies;
Long Li214bab42019-04-05 21:36:35 +0000537
538 /* mark submitted MIDs for retry and issue callback */
539 INIT_LIST_HEAD(&retry_list);
540 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
541 spin_lock(&GlobalMid_Lock);
542 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
543 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700544 kref_get(&mid_entry->refcount);
Long Li214bab42019-04-05 21:36:35 +0000545 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
546 mid_entry->mid_state = MID_RETRY_NEEDED;
547 list_move(&mid_entry->qhead, &retry_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700548 mid_entry->mid_flags |= MID_DELETED;
Long Li214bab42019-04-05 21:36:35 +0000549 }
550 spin_unlock(&GlobalMid_Lock);
Long Li1d2a4f52019-05-13 21:01:28 -0700551 mutex_unlock(&server->srv_mutex);
Long Li214bab42019-04-05 21:36:35 +0000552
553 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
554 list_for_each_safe(tmp, tmp2, &retry_list) {
555 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
556 list_del_init(&mid_entry->qhead);
557 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700558 cifs_mid_q_entry_release(mid_entry);
Long Li214bab42019-04-05 21:36:35 +0000559 }
560
Long Li1d2a4f52019-05-13 21:01:28 -0700561 if (cifs_rdma_enabled(server)) {
562 mutex_lock(&server->srv_mutex);
Long Li050b8c32019-04-04 11:35:42 -0500563 smbd_destroy(server);
Long Li1d2a4f52019-05-13 21:01:28 -0700564 mutex_unlock(&server->srv_mutex);
565 }
Jeff Layton3c1105d2011-05-22 07:09:13 -0400566
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400567 do {
Steve French6c3d8902006-07-31 22:46:20 +0000568 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300569
Jeff Layton73e216a2013-09-05 08:38:10 -0400570 mutex_lock(&server->srv_mutex);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200571 /*
572 * Set up next DFS target server (if any) for reconnect. If DFS
573 * feature is disabled, then we will retry last server we
574 * connected to before.
575 */
Long Li781a8052017-11-22 17:38:36 -0700576 if (cifs_rdma_enabled(server))
577 rc = smbd_reconnect(server);
578 else
579 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000580 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500581 cifs_dbg(FYI, "reconnect error %d\n", rc);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200582#ifdef CONFIG_CIFS_DFS_UPCALL
583 reconn_inval_dfs_target(server, cifs_sb, &tgt_list,
584 &tgt_it);
585#endif
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200586 rc = reconn_set_ipaddr(server);
587 if (rc) {
588 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
589 __func__, rc);
590 }
Federico Sauter4afe2602015-03-17 17:45:28 +0100591 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700592 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 } else {
594 atomic_inc(&tcpSesReconnectCount);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800595 set_credits(server, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000597 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000598 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000599 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100600 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400602 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500603
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200604#ifdef CONFIG_CIFS_DFS_UPCALL
605 if (tgt_it) {
606 rc = dfs_cache_noreq_update_tgthint(cifs_sb->origin_fullpath + 1,
607 tgt_it);
608 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000609 cifs_server_dbg(VFS, "%s: failed to update DFS target hint: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200610 __func__, rc);
611 }
612 rc = dfs_cache_update_vol(cifs_sb->origin_fullpath, server);
613 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000614 cifs_server_dbg(VFS, "%s: failed to update vol info in DFS cache: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200615 __func__, rc);
616 }
Paulo Alcantara23324402018-11-20 14:37:18 -0200617 dfs_cache_free_tgts(&tgt_list);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300618
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200619 }
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300620
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300621 cifs_put_tcp_super(sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200622#endif
Sachin Prabhub8c60012016-10-20 19:52:24 -0400623 if (server->tcpStatus == CifsNeedNegotiate)
624 mod_delayed_work(cifsiod_wq, &server->echo, 0);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return rc;
627}
628
Jeff Laytonc74093b2011-01-11 07:24:23 -0500629static void
630cifs_echo_request(struct work_struct *work)
631{
632 int rc;
633 struct TCP_Server_Info *server = container_of(work,
634 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400635 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500636
Jeff Layton247ec9b2011-02-04 17:09:50 -0500637 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400638 * If we need to renegotiate, set echo interval to zero to
639 * immediately call echo service where we can renegotiate.
640 */
641 if (server->tcpStatus == CifsNeedNegotiate)
642 echo_interval = 0;
643 else
644 echo_interval = server->echo_interval;
645
646 /*
647 * We cannot send an echo if it is disabled.
648 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500649 */
Steve French4fcd1812016-06-22 20:12:05 -0500650
651 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400652 server->tcpStatus == CifsExiting ||
653 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400654 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600655 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500656 goto requeue_echo;
657
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400658 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500659 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500660 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
661 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500662
663requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400664 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500665}
666
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400667static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400668allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400669{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400670 if (!server->bigbuf) {
671 server->bigbuf = (char *)cifs_buf_get();
672 if (!server->bigbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000673 cifs_server_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400674 msleep(3000);
675 /* retry will check if exiting */
676 return false;
677 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400678 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400679 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400680 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400681 }
682
Jeff Layton2a37ef92011-10-19 15:29:23 -0400683 if (!server->smallbuf) {
684 server->smallbuf = (char *)cifs_small_buf_get();
685 if (!server->smallbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000686 cifs_server_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400687 msleep(1000);
688 /* retry will check if exiting */
689 return false;
690 }
691 /* beginning of smb buffer is cleared in our buf_get */
692 } else {
693 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400694 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400695 }
696
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400697 return true;
698}
699
Jeff Laytonba749e62011-10-11 06:41:32 -0400700static bool
701server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400702{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300703 /*
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000704 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300705 * situations right:
706 * 1s client sends a normal SMB request
Ronnie Sahlbergbecc2ba2019-07-24 11:43:49 +1000707 * 2s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300708 * 30s echo workqueue job pops, and decides we got a response recently
709 * and don't need to send another
710 * ...
711 * 65s kernel_recvmsg times out, and we see that we haven't gotten
712 * a response in >60s.
713 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200714 if ((server->tcpStatus == CifsGood ||
715 server->tcpStatus == CifsNeedNegotiate) &&
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000716 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000717 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
718 (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400719 cifs_reconnect(server);
720 wake_up(&server->response_q);
721 return true;
722 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400723
Jeff Laytonba749e62011-10-11 06:41:32 -0400724 return false;
725}
726
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800727static inline bool
728zero_credits(struct TCP_Server_Info *server)
729{
730 int val;
731
732 spin_lock(&server->req_lock);
733 val = server->credits + server->echo_credits + server->oplock_credits;
734 if (server->in_flight == 0 && val == 0) {
735 spin_unlock(&server->req_lock);
736 return true;
737 }
738 spin_unlock(&server->req_lock);
739 return false;
740}
741
Al Viro71335662016-01-09 19:54:50 -0500742static int
743cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400744{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400745 int length = 0;
746 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400747
Al Viro71335662016-01-09 19:54:50 -0500748 smb_msg->msg_control = NULL;
749 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400750
Al Viro71335662016-01-09 19:54:50 -0500751 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500752 try_to_freeze();
753
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800754 /* reconnect if no credits and no requests in flight */
755 if (zero_credits(server)) {
756 cifs_reconnect(server);
757 return -ECONNABORTED;
758 }
759
Al Viro71335662016-01-09 19:54:50 -0500760 if (server_unresponsive(server))
761 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700762 if (cifs_rdma_enabled(server) && server->smbd_conn)
763 length = smbd_recv(server->smbd_conn, smb_msg);
764 else
765 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500766
767 if (server->tcpStatus == CifsExiting)
768 return -ESHUTDOWN;
769
770 if (server->tcpStatus == CifsNeedReconnect) {
771 cifs_reconnect(server);
772 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400773 }
774
Al Viro71335662016-01-09 19:54:50 -0500775 if (length == -ERESTARTSYS ||
776 length == -EAGAIN ||
777 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400778 /*
779 * Minimum sleep to prevent looping, allowing socket
780 * to clear and app threads to set tcpStatus
781 * CifsNeedReconnect if server hung.
782 */
783 usleep_range(1000, 2000);
784 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400785 continue;
Al Viro71335662016-01-09 19:54:50 -0500786 }
787
788 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500789 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400790 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500791 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400792 }
793 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400794 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400795}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400796
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400797int
798cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
799 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400800{
Al Viro71335662016-01-09 19:54:50 -0500801 struct msghdr smb_msg;
802 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100803 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400804
Al Viro71335662016-01-09 19:54:50 -0500805 return cifs_readv_from_socket(server, &smb_msg);
806}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400807
Al Viro71335662016-01-09 19:54:50 -0500808int
809cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700810 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500811{
812 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700813 struct bio_vec bv = {
814 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100815 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500816 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400817}
818
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400819static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400820is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400821{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400822 /*
823 * The first byte big endian of the length field,
824 * is actually not part of the length but the type
825 * with the most common, zero, as regular data.
826 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400827 switch (type) {
828 case RFC1002_SESSION_MESSAGE:
829 /* Regular SMB response */
830 return true;
831 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500832 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400833 break;
834 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500835 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400836 break;
837 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400838 /*
839 * We get this from Windows 98 instead of an error on
840 * SMB negprot response.
841 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500842 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400843 /* give server a second to clean up */
844 msleep(1000);
845 /*
846 * Always try 445 first on reconnect since we get NACK
847 * on some if we ever connected to port 139 (the NACK
848 * is since we do not begin with RFC1001 session
849 * initialize frame).
850 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400851 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400852 cifs_reconnect(server);
853 wake_up(&server->response_q);
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);
1025 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +04001026 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001027 }
1028
1029 /* switch to large buffer if too big for a small one */
1030 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
1031 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001032 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001033 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001034 }
1035
1036 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001037 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001038 pdu_length - HEADER_SIZE(server) + 1
1039 + server->vals->header_preamble_size);
1040
Jeff Laytone9097ab2011-10-19 15:29:40 -04001041 if (length < 0)
1042 return length;
1043 server->total_read += length;
1044
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001045 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001046
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08001047 return cifs_handle_standard(server, mid);
1048}
1049
1050int
1051cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1052{
1053 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
1054 int length;
1055
Jeff Laytone9097ab2011-10-19 15:29:40 -04001056 /*
1057 * We know that we received enough to get to the MID as we
1058 * checked the pdu_length earlier. Now check to see
1059 * if the rest of the header is OK. We borrow the length
1060 * var for the rest of the loop to avoid a new stack var.
1061 *
1062 * 48 bytes is enough to display the header and a little bit
1063 * into the payload for debugging purposes.
1064 */
Steve French373512e2015-12-18 13:05:30 -06001065 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001066 if (length != 0)
1067 cifs_dump_mem("Bad SMB: ", buf,
1068 min_t(unsigned int, server->total_read, 48));
1069
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001070 if (server->ops->is_session_expired &&
1071 server->ops->is_session_expired(buf)) {
1072 cifs_reconnect(server);
1073 wake_up(&server->response_q);
1074 return -1;
1075 }
1076
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001077 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08001078 server->ops->is_status_pending(buf, server))
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001079 return -1;
1080
Jeff Laytonff4fa4a2012-02-07 06:31:05 -05001081 if (!mid)
1082 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001083
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001084 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -05001085 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001086}
1087
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001088static void
1089smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
1090{
1091 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
1092
1093 /*
1094 * SMB1 does not use credits.
1095 */
1096 if (server->vals->header_preamble_size)
1097 return;
1098
1099 if (shdr->CreditRequest) {
1100 spin_lock(&server->req_lock);
1101 server->credits += le16_to_cpu(shdr->CreditRequest);
1102 spin_unlock(&server->req_lock);
1103 wake_up(&server->request_q);
1104 }
1105}
1106
1107
Jeff Laytone9097ab2011-10-19 15:29:40 -04001108static int
Al Viro7c97c202011-06-21 08:51:28 -04001109cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001111 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -04001112 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001113 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001114 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001115 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001116 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001117 struct mid_q_entry *mids[MAX_COMPOUND];
1118 char *bufs[MAX_COMPOUND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -05001121 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -04001122
1123 length = atomic_inc_return(&tcpSesAllocCount);
1124 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -07001125 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001127 set_freezable();
Eric W. Biederman33da8e72019-08-16 12:33:54 -05001128 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +00001129 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -07001130 if (try_to_freeze())
1131 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001132
Jeff Layton2a37ef92011-10-19 15:29:23 -04001133 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +04001134 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001135
Jeff Layton2a37ef92011-10-19 15:29:23 -04001136 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001137 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +00001138 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +00001139
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001140 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -04001141 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +00001142 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10001143
1144 if (server->vals->header_preamble_size == 0)
1145 server->total_read = 0;
1146 else
1147 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -07001148
Pavel Shilovsky98bac622011-08-01 13:19:42 +04001149 /*
1150 * The right amount was read from socket - 4 bytes,
1151 * so we can now interpret the length field.
1152 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001153 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -07001154
Joe Perchesf96637b2013-05-04 22:12:25 -05001155 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -04001156 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001157 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001158next_pdu:
1159 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -07001160
Jeff Layton89482a52011-10-19 15:28:57 -04001161 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001162 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001163 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001164 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001165 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001166 cifs_reconnect(server);
1167 wake_up(&server->response_q);
1168 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -07001169 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001170
Jeff Layton89482a52011-10-19 15:28:57 -04001171 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001172 length = cifs_read_from_socket(server,
1173 buf + server->vals->header_preamble_size,
1174 HEADER_SIZE(server) - 1
1175 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001176 if (length < 0)
1177 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001178 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001179
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001180 if (server->ops->next_header) {
1181 next_offset = server->ops->next_header(buf);
1182 if (next_offset)
1183 server->pdu_size = next_offset;
1184 }
1185
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001186 memset(mids, 0, sizeof(mids));
1187 memset(bufs, 0, sizeof(bufs));
1188 num_mids = 0;
1189
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001190 if (server->ops->is_transform_hdr &&
1191 server->ops->receive_transform &&
1192 server->ops->is_transform_hdr(buf)) {
1193 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001194 mids,
1195 bufs,
1196 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001197 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001198 mids[0] = server->ops->find_mid(server, buf);
1199 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001200 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001201
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001202 if (!mids[0] || !mids[0]->receive)
1203 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001204 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001205 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001206 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001207
Lars Persson696e4202018-06-25 14:05:25 +02001208 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001209 for (i = 0; i < num_mids; i++)
1210 if (mids[i])
1211 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001212 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001213 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001214
Steve Frenchfda35942011-01-20 18:06:34 +00001215 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001216
1217 for (i = 0; i < num_mids; i++) {
1218 if (mids[i] != NULL) {
1219 mids[i]->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001220
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001221 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1222 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001223
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001224 cifs_mid_q_entry_release(mids[i]);
1225 } else if (server->ops->is_oplock_break &&
1226 server->ops->is_oplock_break(bufs[i],
1227 server)) {
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001228 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001229 cifs_dbg(FYI, "Received oplock break\n");
1230 } else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001231 cifs_server_dbg(VFS, "No task to wake, unknown frame "
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001232 "received! NumMids %d\n",
1233 atomic_read(&midCount));
1234 cifs_dump_mem("Received Data is: ", bufs[i],
1235 HEADER_SIZE(server));
Ronnie Sahlberg3e272572019-07-06 06:43:08 +10001236 smb2_add_credits_from_hdr(bufs[i], server);
Steve French39798772006-05-31 22:40:51 +00001237#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001238 if (server->ops->dump_detail)
1239 server->ops->dump_detail(bufs[i],
1240 server);
1241 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001242#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001243 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001244 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001245
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001246 if (pdu_length > server->pdu_size) {
1247 if (!allocate_buffers(server))
1248 continue;
1249 pdu_length -= server->pdu_size;
1250 server->total_read = 0;
1251 server->large_buf = false;
1252 buf = server->smallbuf;
1253 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001254 }
1255 } /* end while !EXITING */
1256
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001257 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001258 cifs_buf_release(server->bigbuf);
1259 if (server->smallbuf) /* no sense logging a debug message if NULL */
1260 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001262 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001263 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001264
1265 /* if server->tsk was NULL then wait for a signal before exiting */
1266 if (!task_to_wake) {
1267 set_current_state(TASK_INTERRUPTIBLE);
1268 while (!signal_pending(current)) {
1269 schedule();
1270 set_current_state(TASK_INTERRUPTIBLE);
1271 }
1272 set_current_state(TASK_RUNNING);
1273 }
1274
Jeff Layton0468a2c2008-12-01 07:09:35 -05001275 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Jeff Laytonc359cf32007-11-16 22:22:06 +00001278/* extract the host portion of the UNC string */
1279static char *
1280extract_hostname(const char *unc)
1281{
1282 const char *src;
1283 char *dst, *delim;
1284 unsigned int len;
1285
1286 /* skip double chars at beginning of string */
1287 /* BB: check validity of these bytes? */
Paulo Alcantarac34fea52018-11-14 14:03:40 -02001288 if (strlen(unc) < 3)
1289 return ERR_PTR(-EINVAL);
1290 for (src = unc; *src && *src == '\\'; src++)
1291 ;
1292 if (!*src)
1293 return ERR_PTR(-EINVAL);
Jeff Laytonc359cf32007-11-16 22:22:06 +00001294
1295 /* delimiter between hostname and sharename is always '\\' now */
1296 delim = strchr(src, '\\');
1297 if (!delim)
1298 return ERR_PTR(-EINVAL);
1299
1300 len = delim - src;
1301 dst = kmalloc((len + 1), GFP_KERNEL);
1302 if (dst == NULL)
1303 return ERR_PTR(-ENOMEM);
1304
1305 memcpy(dst, src, len);
1306 dst[len] = '\0';
1307
1308 return dst;
1309}
1310
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001311static int get_option_ul(substring_t args[], unsigned long *option)
1312{
1313 int rc;
1314 char *string;
1315
1316 string = match_strdup(args);
1317 if (string == NULL)
1318 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001319 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001320 kfree(string);
1321
1322 return rc;
1323}
1324
Eric W. Biederman3da46562013-02-06 01:37:39 -08001325static int get_option_uid(substring_t args[], kuid_t *result)
1326{
1327 unsigned long value;
1328 kuid_t uid;
1329 int rc;
1330
1331 rc = get_option_ul(args, &value);
1332 if (rc)
1333 return rc;
1334
1335 uid = make_kuid(current_user_ns(), value);
1336 if (!uid_valid(uid))
1337 return -EINVAL;
1338
1339 *result = uid;
1340 return 0;
1341}
1342
1343static int get_option_gid(substring_t args[], kgid_t *result)
1344{
1345 unsigned long value;
1346 kgid_t gid;
1347 int rc;
1348
1349 rc = get_option_ul(args, &value);
1350 if (rc)
1351 return rc;
1352
1353 gid = make_kgid(current_user_ns(), value);
1354 if (!gid_valid(gid))
1355 return -EINVAL;
1356
1357 *result = gid;
1358 return 0;
1359}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001360
1361static int cifs_parse_security_flavors(char *value,
1362 struct smb_vol *vol)
1363{
1364
1365 substring_t args[MAX_OPT_ARGS];
1366
Jeff Layton1e3cc572013-06-10 17:12:23 -05001367 /*
1368 * With mount options, the last one should win. Reset any existing
1369 * settings back to default.
1370 */
1371 vol->sectype = Unspecified;
1372 vol->sign = false;
1373
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001374 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001375 case Opt_sec_krb5p:
1376 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1377 return 1;
1378 case Opt_sec_krb5i:
1379 vol->sign = true;
1380 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001381 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001382 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001383 break;
1384 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001385 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001386 /* Fallthrough */
1387 case Opt_sec_ntlmssp:
1388 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001389 break;
1390 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001391 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001392 /* Fallthrough */
1393 case Opt_ntlm:
1394 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001395 break;
1396 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001397 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001398 /* Fallthrough */
1399 case Opt_sec_ntlmv2:
1400 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001401 break;
1402#ifdef CONFIG_CIFS_WEAK_PW_HASH
1403 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001404 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001405 break;
1406#endif
1407 case Opt_sec_none:
1408 vol->nullauth = 1;
1409 break;
1410 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001411 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001412 return 1;
1413 }
1414
1415 return 0;
1416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001419cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1420{
1421 substring_t args[MAX_OPT_ARGS];
1422
1423 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1424 case Opt_cache_loose:
1425 vol->direct_io = false;
1426 vol->strict_io = false;
Steve French83bbfa72019-08-27 23:58:54 -05001427 vol->cache_ro = false;
Steve French41e033f2019-08-30 02:12:41 -05001428 vol->cache_rw = false;
Jeff Layton15b6a472012-05-16 07:50:15 -04001429 break;
1430 case Opt_cache_strict:
1431 vol->direct_io = false;
1432 vol->strict_io = true;
Steve French83bbfa72019-08-27 23:58:54 -05001433 vol->cache_ro = false;
Steve French41e033f2019-08-30 02:12:41 -05001434 vol->cache_rw = false;
Jeff Layton15b6a472012-05-16 07:50:15 -04001435 break;
1436 case Opt_cache_none:
1437 vol->direct_io = true;
1438 vol->strict_io = false;
Steve French83bbfa72019-08-27 23:58:54 -05001439 vol->cache_ro = false;
Steve French41e033f2019-08-30 02:12:41 -05001440 vol->cache_rw = false;
Steve French83bbfa72019-08-27 23:58:54 -05001441 break;
1442 case Opt_cache_ro:
1443 vol->direct_io = false;
1444 vol->strict_io = false;
1445 vol->cache_ro = true;
Steve French41e033f2019-08-30 02:12:41 -05001446 vol->cache_rw = false;
1447 break;
1448 case Opt_cache_rw:
1449 vol->direct_io = false;
1450 vol->strict_io = false;
1451 vol->cache_ro = false;
1452 vol->cache_rw = true;
Jeff Layton15b6a472012-05-16 07:50:15 -04001453 break;
1454 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001455 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001456 return 1;
1457 }
1458 return 0;
1459}
1460
1461static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001462cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001463{
1464 substring_t args[MAX_OPT_ARGS];
1465
1466 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001467#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001468 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001469 if (disable_legacy_dialects) {
1470 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1471 return 1;
1472 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001473 if (is_smb3) {
1474 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1475 return 1;
1476 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001477 vol->ops = &smb1_operations;
1478 vol->vals = &smb1_values;
1479 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001480 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001481 if (disable_legacy_dialects) {
1482 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1483 return 1;
1484 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001485 if (is_smb3) {
1486 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1487 return 1;
1488 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001489 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001490 vol->vals = &smb20_values;
1491 break;
Steve French74204512018-06-19 14:34:08 -05001492#else
1493 case Smb_1:
1494 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1495 return 1;
1496 case Smb_20:
1497 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1498 return 1;
1499#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001500 case Smb_21:
1501 vol->ops = &smb21_operations;
1502 vol->vals = &smb21_values;
1503 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001504 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001505 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001506 vol->vals = &smb30_values;
1507 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001508 case Smb_302:
1509 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1510 vol->vals = &smb302_values;
1511 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001512 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001513 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001514 vol->vals = &smb311_values;
1515 break;
Steve French9764c022017-09-17 10:41:35 -05001516 case Smb_3any:
1517 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1518 vol->vals = &smb3any_values;
1519 break;
1520 case Smb_default:
1521 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1522 vol->vals = &smbdefault_values;
1523 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001524 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001525 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001526 return 1;
1527 }
1528 return 0;
1529}
1530
Jeff Laytond387a5c2012-12-10 06:10:46 -05001531/*
1532 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1533 * fields with the result. Returns 0 on success and an error otherwise.
1534 */
1535static int
1536cifs_parse_devname(const char *devname, struct smb_vol *vol)
1537{
1538 char *pos;
1539 const char *delims = "/\\";
1540 size_t len;
1541
Yao Liu68e26722019-01-28 19:47:28 +08001542 if (unlikely(!devname || !*devname)) {
1543 cifs_dbg(VFS, "Device name not specified.\n");
1544 return -EINVAL;
1545 }
1546
Jeff Laytond387a5c2012-12-10 06:10:46 -05001547 /* make sure we have a valid UNC double delimiter prefix */
1548 len = strspn(devname, delims);
1549 if (len != 2)
1550 return -EINVAL;
1551
1552 /* find delimiter between host and sharename */
1553 pos = strpbrk(devname + 2, delims);
1554 if (!pos)
1555 return -EINVAL;
1556
1557 /* skip past delimiter */
1558 ++pos;
1559
1560 /* now go until next delimiter or end of string */
1561 len = strcspn(pos, delims);
1562
1563 /* move "pos" up to delimiter or NULL */
1564 pos += len;
1565 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1566 if (!vol->UNC)
1567 return -ENOMEM;
1568
1569 convert_delimiter(vol->UNC, '\\');
1570
Sachin Prabhu11e31642016-02-08 13:44:01 +05301571 /* skip any delimiter */
1572 if (*pos == '/' || *pos == '\\')
1573 pos++;
1574
1575 /* If pos is NULL then no prepath */
1576 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001577 return 0;
1578
1579 vol->prepath = kstrdup(pos, GFP_KERNEL);
1580 if (!vol->prepath)
1581 return -ENOMEM;
1582
1583 return 0;
1584}
1585
Jeff Layton23db65f2012-05-15 12:20:51 -04001586static int
Sean Finneyb9468452011-04-11 13:19:32 +00001587cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001588 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001590 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001591 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 unsigned int temp_len, i, j;
1593 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001594 short int override_uid = -1;
1595 short int override_gid = -1;
1596 bool uid_specified = false;
1597 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001598 bool sloppy = false;
1599 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001600 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001601 char *string = NULL;
1602 char *tmp_end, *value;
1603 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001604 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001605 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001606 unsigned short port = 0;
1607 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001610 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001611 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Jeff Layton6ee95422012-11-26 11:09:57 -05001613 /* ensure we always start with zeroed-out smb_vol */
1614 memset(vol, 0, sizeof(*vol));
1615
Jeff Layton88463992010-11-22 15:31:03 -05001616 /*
1617 * does not have to be perfect mapping since field is
1618 * informational, only used for servers that do not support
1619 * port 445 and it can be overridden at mount time
1620 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001621 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1622 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001623 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1624
Jeff Layton1397f2e2011-01-07 11:30:28 -05001625 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001626 /* null target name indicates to use *SMBSERVR default called name
1627 if we end up sending RFC1001 session initialize */
1628 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001629 vol->cred_uid = current_uid();
1630 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001631 vol->linux_gid = current_gid();
Steve Frenche8506d22019-02-28 21:32:15 -06001632 vol->bsize = 1024 * 1024; /* can improve cp performance significantly */
Steve French2baa2682014-09-27 02:19:01 -05001633 /*
1634 * default to SFM style remapping of seven reserved characters
1635 * unless user overrides it or we negotiate CIFS POSIX where
1636 * it is unnecessary. Can not simultaneously use more than one mapping
1637 * since then readdir could list files that open could not open
1638 */
1639 vol->remap = true;
1640
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001641 /* default to only allowing write access to owner of the mount */
1642 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001645 /* default is always to request posix paths. */
1646 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001647 /* default to using server inode numbers where available */
1648 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001649
Jeff Layton1b359202012-09-19 15:20:27 -07001650 /* default is to use strict cifs caching semantics */
1651 vol->strict_io = true;
1652
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301653 vol->actimeo = CIFS_DEF_ACTIMEO;
1654
Steve Frenchca567eb2019-03-29 16:31:07 -05001655 /* Most clients set timeout to 0, allows server to use its default */
1656 vol->handle_timeout = 0; /* See MS-SMB2 spec section 2.2.14.2.12 */
1657
Steve French9764c022017-09-17 10:41:35 -05001658 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1659 vol->ops = &smb30_operations;
1660 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001661
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001662 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1663
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001664 /* default to no multichannel (single server connection) */
1665 vol->multichannel = false;
1666 vol->max_channels = 1;
1667
Sean Finneyb9468452011-04-11 13:19:32 +00001668 if (!mountdata)
1669 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Sean Finneyb9468452011-04-11 13:19:32 +00001671 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1672 if (!mountdata_copy)
1673 goto cifs_parse_mount_err;
1674
1675 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001676 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001677
Steve French50c2f752007-07-13 00:33:32 +00001678 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001679 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 separator[0] = options[4];
1681 options += 5;
1682 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001683 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001686 vol->backupuid_specified = false; /* no backup intent for a user */
1687 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001688
Jeff Layton37d4f992013-05-24 07:40:05 -04001689 switch (cifs_parse_devname(devname, vol)) {
1690 case 0:
1691 break;
1692 case -ENOMEM:
1693 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1694 goto cifs_parse_mount_err;
1695 case -EINVAL:
1696 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1697 goto cifs_parse_mount_err;
1698 default:
1699 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1700 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001701 }
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001704 substring_t args[MAX_OPT_ARGS];
1705 unsigned long option;
1706 int token;
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (!*data)
1709 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001711 token = match_token(data, cifs_mount_option_tokens, args);
1712
1713 switch (token) {
1714
1715 /* Ingnore the following */
1716 case Opt_ignore:
1717 break;
1718
1719 /* Boolean values */
1720 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001722 break;
1723 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001725 break;
1726 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001727 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001728 break;
1729 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001730 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001731 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001732 case Opt_forcegid:
1733 override_gid = 1;
1734 break;
1735 case Opt_noforcegid:
1736 override_gid = 0;
1737 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001738 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001739 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001740 break;
1741 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001742 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001743 break;
Steve French3e7a02d2019-09-11 21:46:20 -05001744 case Opt_nolease:
1745 vol->no_lease = 1;
1746 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001747 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001749 break;
1750 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001752 break;
1753 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001755 break;
1756 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001758 break;
1759 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001760 vol->sfu_remap = true;
1761 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001762 break;
1763 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001764 vol->sfu_remap = false;
1765 break;
1766 case Opt_mapposix:
1767 vol->remap = true;
1768 vol->sfu_remap = false; /* disable SFU mapping */
1769 break;
1770 case Opt_nomapposix:
1771 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001772 break;
1773 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001774 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001775 break;
1776 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001777 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001778 break;
1779 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001780 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001781 break;
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03001782 case Opt_rootfs:
1783#ifdef CONFIG_CIFS_ROOT
1784 vol->rootfs = true;
1785#endif
1786 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001787 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001788 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001789 break;
1790 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001791 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001792 break;
1793 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001794 if (vol->linux_ext)
1795 cifs_dbg(VFS,
1796 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001797 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001798 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001799 case Opt_unix:
1800 if (vol->no_linux_ext)
1801 cifs_dbg(VFS,
1802 "conflicting unix mount options\n");
1803 vol->linux_ext = 1;
1804 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001805 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001806 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001807 break;
1808 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001809 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001810 break;
1811 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001812 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001813 /*
1814 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001815 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001816 * local vfs will do advisory
1817 */
Steve French50c2f752007-07-13 00:33:32 +00001818 if (vol->file_mode ==
1819 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001820 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001821 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001822 case Opt_nohandlecache:
1823 vol->nohandlecache = 1;
1824 break;
1825 case Opt_handlecache:
1826 vol->nohandlecache = 0;
1827 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001828 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001829 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001830 break;
1831 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001833 break;
1834 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001836 break;
Steve French95932652016-09-23 01:36:34 -05001837 case Opt_setuidfromacl:
1838 vol->setuidfromacl = 1;
1839 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001840 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001841 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001842 break;
1843 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001844 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001845 break;
1846 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001848 break;
1849 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001851 break;
1852 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001854 break;
1855 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001857 break;
1858 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001859 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001860 break;
1861 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001862 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001863 break;
1864 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001866 break;
1867 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001869 break;
1870 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001871 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001872 break;
Steve French412094a2019-06-24 02:01:42 -05001873 case Opt_modesid:
1874 vol->mode_ace = 1;
1875 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001876 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001877 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001878 break;
1879 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001880 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001881 break;
1882 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001884 break;
1885 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001887 break;
1888 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001889 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001890 break;
1891 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001892 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001893 break;
Steve French4f5c10f2019-09-03 21:18:49 -05001894 case Opt_ignore_signature:
1895 vol->sign = true;
1896 vol->ignore_signature = true;
1897 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001898 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001899 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001900 * is a per tree connection (mount) not a per socket
1901 * or per-smb connection option in the protocol
1902 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1903 */
Steve French95b1cb92008-05-15 16:44:38 +00001904 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001905 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001906 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001907 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001908 break;
1909 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301910#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001911 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001912 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301913#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301914 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001915 break;
1916 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001917 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001918 break;
1919 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001920 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001921 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001922 case Opt_sloppy:
1923 sloppy = true;
1924 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001925 case Opt_nosharesock:
1926 vol->nosharesock = true;
1927 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001928 case Opt_nopersistent:
1929 vol->nopersistent = true;
1930 if (vol->persistent) {
1931 cifs_dbg(VFS,
1932 "persistenthandles mount options conflict\n");
1933 goto cifs_parse_mount_err;
1934 }
1935 break;
1936 case Opt_persistent:
1937 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001938 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001939 cifs_dbg(VFS,
1940 "persistenthandles mount options conflict\n");
1941 goto cifs_parse_mount_err;
1942 }
1943 break;
Steve French592fafe2015-11-03 10:08:53 -06001944 case Opt_resilient:
1945 vol->resilient = true;
1946 if (vol->persistent) {
1947 cifs_dbg(VFS,
1948 "persistenthandles mount options conflict\n");
1949 goto cifs_parse_mount_err;
1950 }
1951 break;
1952 case Opt_noresilient:
1953 vol->resilient = false; /* already the default */
1954 break;
Germano Percossi39566442016-12-15 12:31:18 +05301955 case Opt_domainauto:
1956 vol->domainauto = true;
1957 break;
Long Li8339dd32017-11-07 01:54:55 -07001958 case Opt_rdma:
1959 vol->rdma = true;
1960 break;
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001961 case Opt_multichannel:
1962 vol->multichannel = true;
1963 break;
1964 case Opt_nomultichannel:
1965 vol->multichannel = false;
1966 break;
Steve French9fe5ff12019-06-24 20:39:04 -05001967 case Opt_compress:
1968 vol->compression = UNKNOWN_TYPE;
1969 cifs_dbg(VFS,
1970 "SMB3 compression support is experimental\n");
1971 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001972
1973 /* Numeric Values */
1974 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001975 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001976 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1977 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001978 goto cifs_parse_mount_err;
1979 }
1980 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001981 break;
1982 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001983 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001984 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1985 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001986 goto cifs_parse_mount_err;
1987 }
1988 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001989 break;
1990 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001991 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001992 cifs_dbg(VFS, "%s: Invalid uid value\n",
1993 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001994 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001996 uid_specified = true;
1997 break;
1998 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001999 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002000 cifs_dbg(VFS, "%s: Invalid cruid value\n",
2001 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002002 goto cifs_parse_mount_err;
2003 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002004 break;
2005 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08002006 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002007 cifs_dbg(VFS, "%s: Invalid gid value\n",
2008 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002009 goto cifs_parse_mount_err;
2010 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002011 gid_specified = true;
2012 break;
2013 case Opt_file_mode:
2014 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002015 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
2016 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002017 goto cifs_parse_mount_err;
2018 }
2019 vol->file_mode = option;
2020 break;
2021 case Opt_dirmode:
2022 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002023 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
2024 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002025 goto cifs_parse_mount_err;
2026 }
2027 vol->dir_mode = option;
2028 break;
2029 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002030 if (get_option_ul(args, &option) ||
2031 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002032 cifs_dbg(VFS, "%s: Invalid port value\n",
2033 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002034 goto cifs_parse_mount_err;
2035 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002036 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002037 break;
Steve French563317e2019-09-08 23:22:02 -05002038 case Opt_min_enc_offload:
2039 if (get_option_ul(args, &option)) {
2040 cifs_dbg(VFS, "Invalid minimum encrypted read offload size (esize)\n");
2041 goto cifs_parse_mount_err;
2042 }
2043 vol->min_offload = option;
2044 break;
Steve Frenche8506d22019-02-28 21:32:15 -06002045 case Opt_blocksize:
2046 if (get_option_ul(args, &option)) {
2047 cifs_dbg(VFS, "%s: Invalid blocksize value\n",
2048 __func__);
2049 goto cifs_parse_mount_err;
2050 }
2051 /*
2052 * inode blocksize realistically should never need to be
2053 * less than 16K or greater than 16M and default is 1MB.
2054 * Note that small inode block sizes (e.g. 64K) can lead
2055 * to very poor performance of common tools like cp and scp
2056 */
2057 if ((option < CIFS_MAX_MSGSIZE) ||
2058 (option > (4 * SMB3_DEFAULT_IOSIZE))) {
2059 cifs_dbg(VFS, "%s: Invalid blocksize\n",
2060 __func__);
2061 goto cifs_parse_mount_err;
2062 }
2063 vol->bsize = option;
2064 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002065 case Opt_rsize:
2066 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002067 cifs_dbg(VFS, "%s: Invalid rsize value\n",
2068 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002069 goto cifs_parse_mount_err;
2070 }
2071 vol->rsize = option;
2072 break;
2073 case Opt_wsize:
2074 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002075 cifs_dbg(VFS, "%s: Invalid wsize value\n",
2076 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002077 goto cifs_parse_mount_err;
2078 }
2079 vol->wsize = option;
2080 break;
2081 case Opt_actimeo:
2082 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002083 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
2084 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002085 goto cifs_parse_mount_err;
2086 }
2087 vol->actimeo = HZ * option;
2088 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002089 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002090 goto cifs_parse_mount_err;
2091 }
2092 break;
Steve Frenchca567eb2019-03-29 16:31:07 -05002093 case Opt_handletimeout:
2094 if (get_option_ul(args, &option)) {
2095 cifs_dbg(VFS, "%s: Invalid handletimeout value\n",
2096 __func__);
2097 goto cifs_parse_mount_err;
2098 }
2099 vol->handle_timeout = option;
2100 if (vol->handle_timeout > SMB3_MAX_HANDLE_TIMEOUT) {
2101 cifs_dbg(VFS, "Invalid handle cache timeout, longer than 16 minutes\n");
2102 goto cifs_parse_mount_err;
2103 }
2104 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002105 case Opt_echo_interval:
2106 if (get_option_ul(args, &option)) {
2107 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
2108 __func__);
2109 goto cifs_parse_mount_err;
2110 }
2111 vol->echo_interval = option;
2112 break;
Steve French8b217fe2016-11-11 22:36:20 -06002113 case Opt_snapshot:
2114 if (get_option_ul(args, &option)) {
2115 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
2116 __func__);
2117 goto cifs_parse_mount_err;
2118 }
2119 vol->snapshot_time = option;
2120 break;
Steve French141891f2016-09-23 00:44:16 -05002121 case Opt_max_credits:
2122 if (get_option_ul(args, &option) || (option < 20) ||
2123 (option > 60000)) {
2124 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
2125 __func__);
2126 goto cifs_parse_mount_err;
2127 }
2128 vol->max_credits = option;
2129 break;
Aurelien Aptelbcc88802019-09-20 04:32:20 +02002130 case Opt_max_channels:
2131 if (get_option_ul(args, &option) || option < 1 ||
2132 option > CIFS_MAX_CHANNELS) {
2133 cifs_dbg(VFS, "%s: Invalid max_channels value, needs to be 1-%d\n",
2134 __func__, CIFS_MAX_CHANNELS);
2135 goto cifs_parse_mount_err;
2136 }
2137 vol->max_channels = option;
2138 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002139
2140 /* String Arguments */
2141
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002142 case Opt_blank_user:
2143 /* null user, ie. anonymous authentication */
2144 vol->nullauth = 1;
2145 vol->username = NULL;
2146 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002147 case Opt_user:
2148 string = match_strdup(args);
2149 if (string == NULL)
2150 goto out_nomem;
2151
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002152 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
2153 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002154 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002155 goto cifs_parse_mount_err;
2156 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002157
2158 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002159 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05002160 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002161 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002162 break;
2163 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002164 /* passwords have to be handled differently
2165 * to allow the character used for deliminator
2166 * to be passed within them
2167 */
2168
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01002169 /*
2170 * Check if this is a case where the password
2171 * starts with a delimiter
2172 */
2173 tmp_end = strchr(data, '=');
2174 tmp_end++;
2175 if (!(tmp_end < end && tmp_end[1] == delim)) {
2176 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01002177 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01002178 vol->password = NULL;
2179 break;
2180 }
Gustavo A. R. Silva07fa6012018-11-27 10:01:51 +11002181 /* Fallthrough - to Opt_pass below.*/
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01002182 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002183 /* Obtain the value string */
2184 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01002185 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002186
2187 /* Set tmp_end to end of the string */
2188 tmp_end = (char *) value + strlen(value);
2189
2190 /* Check if following character is the deliminator
2191 * If yes, we have encountered a double deliminator
2192 * reset the NULL character to the deliminator
2193 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05302194 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002195 tmp_end[0] = delim;
2196
Suresh Jayaramane73f8432012-06-12 07:15:50 +05302197 /* Keep iterating until we get to a single
2198 * deliminator OR the end
2199 */
2200 while ((tmp_end = strchr(tmp_end, delim))
2201 != NULL && (tmp_end[1] == delim)) {
2202 tmp_end = (char *) &tmp_end[2];
2203 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002204
Suresh Jayaramane73f8432012-06-12 07:15:50 +05302205 /* Reset var options to point to next element */
2206 if (tmp_end) {
2207 tmp_end[0] = '\0';
2208 options = (char *) &tmp_end[1];
2209 } else
2210 /* Reached the end of the mount option
2211 * string */
2212 options = end;
2213 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002214
Aurelien Aptel97f4b722018-01-25 15:59:39 +01002215 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002216 /* Now build new password string */
2217 temp_len = strlen(value);
2218 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
2219 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002220 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002221 goto cifs_parse_mount_err;
2222 }
2223
2224 for (i = 0, j = 0; i < temp_len; i++, j++) {
2225 vol->password[j] = value[i];
2226 if ((value[i] == delim) &&
2227 value[i+1] == delim)
2228 /* skip the second deliminator */
2229 i++;
2230 }
2231 vol->password[j] = '\0';
2232 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002233 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002234 /* FIXME: should this be an error instead? */
2235 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002236 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002237 case Opt_ip:
2238 string = match_strdup(args);
2239 if (string == NULL)
2240 goto out_nomem;
2241
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002242 if (!cifs_convert_address(dstaddr, string,
2243 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002244 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002245 goto cifs_parse_mount_err;
2246 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002247 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002248 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002249 case Opt_domain:
2250 string = match_strdup(args);
2251 if (string == NULL)
2252 goto out_nomem;
2253
Chen Gang057d6332013-07-19 09:01:36 +08002254 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
2255 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002256 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002257 goto cifs_parse_mount_err;
2258 }
2259
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002260 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002261 vol->domainname = kstrdup(string, GFP_KERNEL);
2262 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002263 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002264 goto cifs_parse_mount_err;
2265 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002266 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002267 break;
2268 case Opt_srcaddr:
2269 string = match_strdup(args);
2270 if (string == NULL)
2271 goto out_nomem;
2272
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002273 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002274 (struct sockaddr *)&vol->srcaddr,
2275 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002276 pr_warn("CIFS: Could not parse srcaddr: %s\n",
2277 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002278 goto cifs_parse_mount_err;
2279 }
2280 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002281 case Opt_iocharset:
2282 string = match_strdup(args);
2283 if (string == NULL)
2284 goto out_nomem;
2285
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002286 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002287 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002288 goto cifs_parse_mount_err;
2289 }
2290
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002291 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002292 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002293 vol->iocharset = kstrdup(string,
2294 GFP_KERNEL);
2295 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002296 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002297 goto cifs_parse_mount_err;
2298 }
2299 }
2300 /* if iocharset not set then load_nls_default
2301 * is used by caller
2302 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002303 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002304 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002305 case Opt_netbiosname:
2306 string = match_strdup(args);
2307 if (string == NULL)
2308 goto out_nomem;
2309
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002310 memset(vol->source_rfc1001_name, 0x20,
2311 RFC1001_NAME_LEN);
2312 /*
2313 * FIXME: are there cases in which a comma can
2314 * be valid in workstation netbios name (and
2315 * need special handling)?
2316 */
2317 for (i = 0; i < RFC1001_NAME_LEN; i++) {
2318 /* don't ucase netbiosname for user */
2319 if (string[i] == 0)
2320 break;
2321 vol->source_rfc1001_name[i] = string[i];
2322 }
2323 /* The string has 16th byte zero still from
2324 * set at top of the function
2325 */
2326 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002327 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002328 break;
2329 case Opt_servern:
2330 /* servernetbiosname specified override *SMBSERVER */
2331 string = match_strdup(args);
2332 if (string == NULL)
2333 goto out_nomem;
2334
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002335 /* last byte, type, is 0x20 for servr type */
2336 memset(vol->target_rfc1001_name, 0x20,
2337 RFC1001_NAME_LEN_WITH_NULL);
2338
2339 /* BB are there cases in which a comma can be
2340 valid in this workstation netbios name
2341 (and need special handling)? */
2342
2343 /* user or mount helper must uppercase the
2344 netbios name */
2345 for (i = 0; i < 15; i++) {
2346 if (string[i] == 0)
2347 break;
2348 vol->target_rfc1001_name[i] = string[i];
2349 }
2350 /* The string has 16th byte zero still from
2351 set at top of the function */
2352 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002353 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002354 break;
2355 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002356 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002357 string = match_strdup(args);
2358 if (string == NULL)
2359 goto out_nomem;
2360
Steve French7e682f72017-08-31 21:34:24 -05002361 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002362 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002363 if (strlen(string) > 1) {
2364 pr_warn("Bad mount helper ver=%s. Did "
2365 "you want SMB1 (CIFS) dialect "
2366 "and mean to type vers=1.0 "
2367 "instead?\n", string);
2368 goto cifs_parse_mount_err;
2369 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002370 /* This is the default */
2371 break;
2372 }
2373 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002374 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002375 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002376 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002377 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002378 string = match_strdup(args);
2379 if (string == NULL)
2380 goto out_nomem;
2381
Steve Frenchc7c137b2018-06-06 17:59:29 -05002382 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002383 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002384 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002385 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002386 case Opt_sec:
2387 string = match_strdup(args);
2388 if (string == NULL)
2389 goto out_nomem;
2390
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002391 if (cifs_parse_security_flavors(string, vol) != 0)
2392 goto cifs_parse_mount_err;
2393 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002394 case Opt_cache:
2395 string = match_strdup(args);
2396 if (string == NULL)
2397 goto out_nomem;
2398
2399 if (cifs_parse_cache_flavor(string, vol) != 0)
2400 goto cifs_parse_mount_err;
2401 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002402 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002403 /*
2404 * An option we don't recognize. Save it off for later
2405 * if we haven't already found one
2406 */
2407 if (!invalid)
2408 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002411 /* Free up any allocated string */
2412 kfree(string);
2413 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002415
Jeff Laytond8162552012-03-23 14:40:56 -04002416 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002417 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002418 goto cifs_parse_mount_err;
2419 }
2420
Long Li8339dd32017-11-07 01:54:55 -07002421 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2422 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2423 goto cifs_parse_mount_err;
2424 }
2425
Jeff Layton8a8798a2012-01-17 16:09:15 -05002426#ifndef CONFIG_KEYS
2427 /* Muliuser mounts require CONFIG_KEYS support */
2428 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002429 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002430 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002431 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002432#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002433 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002434 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002435 goto cifs_parse_mount_err;
2436 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002437
Jeff Layton62a1a432012-12-10 06:10:45 -05002438 /* make sure UNC has a share name */
2439 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002440 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002441 goto cifs_parse_mount_err;
2442 }
2443
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002444 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002445 int len;
2446 const char *slash;
2447
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002448 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002449 /* Use the address part of the UNC. */
2450 slash = strchr(&vol->UNC[2], '\\');
2451 len = slash - &vol->UNC[2];
2452 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002453 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002454 goto cifs_parse_mount_err;
2455 }
2456 }
2457
2458 /* set the port that we got earlier */
2459 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002461 if (uid_specified)
2462 vol->override_uid = override_uid;
2463 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002464 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002465
2466 if (gid_specified)
2467 vol->override_gid = override_gid;
2468 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002469 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002470
Steve French7e682f72017-08-31 21:34:24 -05002471 if (got_version == false)
2472 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002473 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002474 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002475 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2476 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002477
Sean Finneyb9468452011-04-11 13:19:32 +00002478 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002480
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002481out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002482 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002483cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002484 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002485 kfree(mountdata_copy);
2486 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Ben Greear3eb9a882010-09-01 17:06:02 -07002489/** Returns true if srcaddr isn't specified and rhs isn't
2490 * specified, or if srcaddr is specified and
2491 * matches the IP address of the rhs argument.
2492 */
Jeff Layton45151482010-07-06 20:43:02 -04002493static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002494srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2495{
2496 switch (srcaddr->sa_family) {
2497 case AF_UNSPEC:
2498 return (rhs->sa_family == AF_UNSPEC);
2499 case AF_INET: {
2500 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2501 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2502 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2503 }
2504 case AF_INET6: {
2505 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002506 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002507 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2508 }
2509 default:
2510 WARN_ON(1);
2511 return false; /* don't expect to be here */
2512 }
2513}
2514
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002515/*
2516 * If no port is specified in addr structure, we try to match with 445 port
2517 * and if it fails - with 139 ports. It should be called only if address
2518 * families of server and addr are equal.
2519 */
2520static bool
2521match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2522{
Steve French6da97912011-03-13 18:55:55 +00002523 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002524
Long Li3b249112019-05-15 14:09:04 -07002525 /* SMBDirect manages its own ports, don't match it here */
2526 if (server->rdma)
2527 return true;
2528
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002529 switch (addr->sa_family) {
2530 case AF_INET:
2531 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2532 port = ((struct sockaddr_in *) addr)->sin_port;
2533 break;
2534 case AF_INET6:
2535 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2536 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2537 break;
2538 default:
2539 WARN_ON(1);
2540 return false;
2541 }
2542
2543 if (!port) {
2544 port = htons(CIFS_PORT);
2545 if (port == *sport)
2546 return true;
2547
2548 port = htons(RFC1001_PORT);
2549 }
2550
2551 return port == *sport;
2552}
Ben Greear3eb9a882010-09-01 17:06:02 -07002553
2554static bool
2555match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2556 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Jeff Layton45151482010-07-06 20:43:02 -04002558 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002559 case AF_INET: {
2560 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2561 struct sockaddr_in *srv_addr4 =
2562 (struct sockaddr_in *)&server->dstaddr;
2563
2564 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002565 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002566 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002567 }
2568 case AF_INET6: {
2569 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2570 struct sockaddr_in6 *srv_addr6 =
2571 (struct sockaddr_in6 *)&server->dstaddr;
2572
Jeff Layton45151482010-07-06 20:43:02 -04002573 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002574 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002575 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002576 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002577 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002578 break;
2579 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002580 default:
2581 WARN_ON(1);
2582 return false; /* don't expect to be here */
2583 }
Jeff Layton45151482010-07-06 20:43:02 -04002584
Ben Greear3eb9a882010-09-01 17:06:02 -07002585 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2586 return false;
2587
Jeff Layton45151482010-07-06 20:43:02 -04002588 return true;
2589}
2590
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002591static bool
2592match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2593{
Jeff Layton3f618222013-06-12 19:52:14 -05002594 /*
2595 * The select_sectype function should either return the vol->sectype
2596 * that was specified, or "Unspecified" if that sectype was not
2597 * compatible with the given NEGOTIATE request.
2598 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302599 if (server->ops->select_sectype(server, vol->sectype)
2600 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002601 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002602
Jeff Layton3f618222013-06-12 19:52:14 -05002603 /*
2604 * Now check if signing mode is acceptable. No need to check
2605 * global_secflags at this point since if MUST_SIGN is set then
2606 * the server->sign had better be too.
2607 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002608 if (vol->sign && !server->sign)
2609 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002610
2611 return true;
2612}
2613
Jeff Layton9fa114f2012-11-26 11:09:57 -05002614static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002615{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002616 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2617
Jeff Laytona0b3df52013-05-24 07:40:59 -04002618 if (vol->nosharesock)
2619 return 0;
2620
Steve French43cdae82019-06-13 14:26:49 -05002621 /* If multidialect negotiation see if existing sessions match one */
2622 if (strcmp(vol->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
2623 if (server->vals->protocol_id < SMB30_PROT_ID)
2624 return 0;
2625 } else if (strcmp(vol->vals->version_string,
2626 SMBDEFAULT_VERSION_STRING) == 0) {
2627 if (server->vals->protocol_id < SMB21_PROT_ID)
2628 return 0;
2629 } else if ((server->vals != vol->vals) || (server->ops != vol->ops))
Jeff Layton23db65f2012-05-15 12:20:51 -04002630 return 0;
2631
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002632 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2633 return 0;
2634
2635 if (!match_address(server, addr,
2636 (struct sockaddr *)&vol->srcaddr))
2637 return 0;
2638
2639 if (!match_port(server, addr))
2640 return 0;
2641
2642 if (!match_security(server, vol))
2643 return 0;
2644
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002645 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002646 return 0;
2647
Long Li8339dd32017-11-07 01:54:55 -07002648 if (server->rdma != vol->rdma)
2649 return 0;
2650
Steve French4f5c10f2019-09-03 21:18:49 -05002651 if (server->ignore_signature != vol->ignore_signature)
2652 return 0;
2653
Steve French563317e2019-09-08 23:22:02 -05002654 if (server->min_offload != vol->min_offload)
2655 return 0;
2656
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002657 return 1;
2658}
2659
Paulo Alcantara54be1f62018-11-14 16:01:21 -02002660struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002661cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002662{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002663 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302665 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002666 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -03002667 /*
2668 * Skip ses channels since they're only handled in lower layers
2669 * (e.g. cifs_send_recv).
2670 */
2671 if (server->is_channel || !match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002672 continue;
2673
Jeff Laytone7ddee92008-11-14 13:44:38 -05002674 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302675 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002676 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002677 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302679 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return NULL;
2681}
2682
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002683void
2684cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002686 struct task_struct *task;
2687
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302688 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002689 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302690 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002691 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002693
Rob Landleyf1d0c992011-01-22 15:44:05 -06002694 put_net(cifs_net_ns(server));
2695
Jeff Laytone7ddee92008-11-14 13:44:38 -05002696 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302697 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002698
Jeff Laytonc74093b2011-01-11 07:24:23 -05002699 cancel_delayed_work_sync(&server->echo);
2700
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002701 if (from_reconnect)
2702 /*
2703 * Avoid deadlock here: reconnect work calls
2704 * cifs_put_tcp_session() at its end. Need to be sure
2705 * that reconnect work does nothing with server pointer after
2706 * that step.
2707 */
2708 cancel_delayed_work(&server->reconnect);
2709 else
2710 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002711
Jeff Laytone7ddee92008-11-14 13:44:38 -05002712 spin_lock(&GlobalMid_Lock);
2713 server->tcpStatus = CifsExiting;
2714 spin_unlock(&GlobalMid_Lock);
2715
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002716 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302717 cifs_fscache_release_client_cookie(server);
2718
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002719 kfree(server->session_key.response);
2720 server->session_key.response = NULL;
2721 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002722
2723 task = xchg(&server->tsk, NULL);
2724 if (task)
Eric W. Biederman72abe3b2019-05-15 12:33:50 -05002725 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002728struct TCP_Server_Info *
Jeff Layton63c038c2008-12-01 18:41:46 -05002729cifs_get_tcp_session(struct smb_vol *volume_info)
2730{
2731 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002732 int rc;
2733
Joe Perchesf96637b2013-05-04 22:12:25 -05002734 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002735
2736 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002737 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002738 if (tcp_ses)
2739 return tcp_ses;
2740
2741 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2742 if (!tcp_ses) {
2743 rc = -ENOMEM;
2744 goto out_err;
2745 }
2746
Jeff Layton23db65f2012-05-15 12:20:51 -04002747 tcp_ses->ops = volume_info->ops;
2748 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002749 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002750 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2751 if (IS_ERR(tcp_ses->hostname)) {
2752 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002753 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002754 }
2755
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002756 tcp_ses->noblockcnt = volume_info->rootfs;
2757 tcp_ses->noblocksnd = volume_info->noblocksnd || volume_info->rootfs;
Jeff Layton63c038c2008-12-01 18:41:46 -05002758 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002759 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002760 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002761 tcp_ses->in_flight = 0;
Steve French1b63f182019-09-09 22:57:11 -05002762 tcp_ses->max_in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002763 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002764 init_waitqueue_head(&tcp_ses->response_q);
2765 init_waitqueue_head(&tcp_ses->request_q);
2766 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2767 mutex_init(&tcp_ses->srv_mutex);
2768 memcpy(tcp_ses->workstation_RFC1001_name,
2769 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2770 memcpy(tcp_ses->server_RFC1001_name,
2771 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002772 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002773 tcp_ses->sequence_number = 0;
Pavel Shilovsky5b964852019-01-18 11:30:26 -08002774 tcp_ses->reconnect_instance = 1;
Steve Frenchfda35942011-01-20 18:06:34 +00002775 tcp_ses->lstrp = jiffies;
Steve French9fe5ff12019-06-24 20:39:04 -05002776 tcp_ses->compress_algorithm = cpu_to_le16(volume_info->compression);
Jeff Layton58fa0152012-05-01 17:41:16 -04002777 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002778 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2779 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002780 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002781 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2782 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002783 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2784 sizeof(tcp_ses->srcaddr));
2785 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2786 sizeof(tcp_ses->dstaddr));
Aurelien Aptelbcc88802019-09-20 04:32:20 +02002787 if (volume_info->use_client_guid)
2788 memcpy(tcp_ses->client_guid, volume_info->client_guid,
2789 SMB2_CLIENT_GUID_SIZE);
2790 else
2791 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002792 /*
2793 * at this point we are the only ones with the pointer
2794 * to the struct since the kernel thread not created yet
2795 * no need to spinlock this init of tcpStatus or srv_count
2796 */
2797 tcp_ses->tcpStatus = CifsNew;
2798 ++tcp_ses->srv_count;
2799
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002800 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2801 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2802 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2803 else
2804 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002805 if (tcp_ses->rdma) {
2806#ifndef CONFIG_CIFS_SMB_DIRECT
2807 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2808 rc = -ENOENT;
2809 goto out_err_crypto_release;
2810#endif
2811 tcp_ses->smbd_conn = smbd_get_connection(
2812 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2813 if (tcp_ses->smbd_conn) {
2814 cifs_dbg(VFS, "RDMA transport established\n");
2815 rc = 0;
2816 goto smbd_connected;
2817 } else {
2818 rc = -ENOENT;
2819 goto out_err_crypto_release;
2820 }
2821 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002822 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002823 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002824 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002825 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002826 }
Long Li2f894642017-11-22 17:38:34 -07002827smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002828 /*
2829 * since we're in a cifs function already, we know that
2830 * this will succeed. No need for try_module_get().
2831 */
2832 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002833 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002834 tcp_ses, "cifsd");
2835 if (IS_ERR(tcp_ses->tsk)) {
2836 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002837 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002838 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002839 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002840 }
Steve French563317e2019-09-08 23:22:02 -05002841 tcp_ses->min_offload = volume_info->min_offload;
Steve Frenchfd88ce92011-04-12 01:01:14 +00002842 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002843
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02002844 tcp_ses->nr_targets = 1;
Steve French4f5c10f2019-09-03 21:18:49 -05002845 tcp_ses->ignore_signature = volume_info->ignore_signature;
Jeff Layton63c038c2008-12-01 18:41:46 -05002846 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302847 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002848 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302849 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002850
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302851 cifs_fscache_get_client_cookie(tcp_ses);
2852
Jeff Laytonc74093b2011-01-11 07:24:23 -05002853 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002854 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002855
Jeff Layton63c038c2008-12-01 18:41:46 -05002856 return tcp_ses;
2857
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002858out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002859 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002860
Rob Landleyf1d0c992011-01-22 15:44:05 -06002861 put_net(cifs_net_ns(tcp_ses));
2862
Jeff Layton63c038c2008-12-01 18:41:46 -05002863out_err:
2864 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002865 if (!IS_ERR(tcp_ses->hostname))
2866 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002867 if (tcp_ses->ssocket)
2868 sock_release(tcp_ses->ssocket);
2869 kfree(tcp_ses);
2870 }
2871 return ERR_PTR(rc);
2872}
2873
Steve French96daf2b2011-05-27 04:34:02 +00002874static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002875{
Jeff Layton3f618222013-06-12 19:52:14 -05002876 if (vol->sectype != Unspecified &&
2877 vol->sectype != ses->sectype)
2878 return 0;
2879
Aurelien Aptelbcc88802019-09-20 04:32:20 +02002880 /*
2881 * If an existing session is limited to less channels than
2882 * requested, it should not be reused
2883 */
2884 if (ses->chan_max < vol->max_channels)
2885 return 0;
2886
Jeff Layton3f618222013-06-12 19:52:14 -05002887 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002888 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002889 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002890 return 0;
2891 break;
2892 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002893 /* NULL username means anonymous session */
2894 if (ses->user_name == NULL) {
2895 if (!vol->nullauth)
2896 return 0;
2897 break;
2898 }
2899
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002900 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002901 if (strncmp(ses->user_name,
2902 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002903 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002904 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002905 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002906 ses->password != NULL &&
2907 strncmp(ses->password,
2908 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002909 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002910 return 0;
2911 }
2912 return 1;
2913}
2914
Aurelien Aptelb327a712018-01-24 13:46:10 +01002915/**
2916 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2917 *
2918 * A new IPC connection is made and stored in the session
2919 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2920 */
2921static int
2922cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2923{
2924 int rc = 0, xid;
2925 struct cifs_tcon *tcon;
2926 struct nls_table *nls_codepage;
2927 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2928 bool seal = false;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002929 struct TCP_Server_Info *server = ses->server;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002930
2931 /*
2932 * If the mount request that resulted in the creation of the
2933 * session requires encryption, force IPC to be encrypted too.
2934 */
2935 if (volume_info->seal) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002936 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
Aurelien Aptelb327a712018-01-24 13:46:10 +01002937 seal = true;
2938 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002939 cifs_server_dbg(VFS,
Aurelien Aptelb327a712018-01-24 13:46:10 +01002940 "IPC: server doesn't support encryption\n");
2941 return -EOPNOTSUPP;
2942 }
2943 }
2944
2945 tcon = tconInfoAlloc();
2946 if (tcon == NULL)
2947 return -ENOMEM;
2948
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002949 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002950
2951 /* cannot fail */
2952 nls_codepage = load_nls_default();
2953
2954 xid = get_xid();
2955 tcon->ses = ses;
2956 tcon->ipc = true;
2957 tcon->seal = seal;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002958 rc = server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002959 free_xid(xid);
2960
2961 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002962 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002963 tconInfoFree(tcon);
2964 goto out;
2965 }
2966
2967 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2968
2969 ses->tcon_ipc = tcon;
2970out:
2971 unload_nls(nls_codepage);
2972 return rc;
2973}
2974
2975/**
2976 * cifs_free_ipc - helper to release the session IPC tcon
2977 *
2978 * Needs to be called everytime a session is destroyed
2979 */
2980static int
2981cifs_free_ipc(struct cifs_ses *ses)
2982{
2983 int rc = 0, xid;
2984 struct cifs_tcon *tcon = ses->tcon_ipc;
2985
2986 if (tcon == NULL)
2987 return 0;
2988
2989 if (ses->server->ops->tree_disconnect) {
2990 xid = get_xid();
2991 rc = ses->server->ops->tree_disconnect(xid, tcon);
2992 free_xid(xid);
2993 }
2994
2995 if (rc)
2996 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2997
2998 tconInfoFree(tcon);
2999 ses->tcon_ipc = NULL;
3000 return rc;
3001}
3002
Steve French96daf2b2011-05-27 04:34:02 +00003003static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04003004cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Steve French96daf2b2011-05-27 04:34:02 +00003006 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303008 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04003009 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003010 if (ses->status == CifsExiting)
3011 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003012 if (!match_session(ses, vol))
3013 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05003014 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303015 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003016 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303018 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return NULL;
3020}
3021
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003022void cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05003023{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003024 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05003025 struct TCP_Server_Info *server = ses->server;
3026
Joe Perchesf96637b2013-05-04 22:12:25 -05003027 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003028
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303029 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003030 if (ses->status == CifsExiting) {
3031 spin_unlock(&cifs_tcp_ses_lock);
3032 return;
3033 }
Jeff Layton14fbf502008-11-14 13:53:46 -05003034 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303035 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003036 return;
3037 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003038 if (ses->status == CifsGood)
3039 ses->status = CifsExiting;
3040 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003041
Aurelien Aptelb327a712018-01-24 13:46:10 +01003042 cifs_free_ipc(ses);
3043
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003044 if (ses->status == CifsExiting && server->ops->logoff) {
3045 xid = get_xid();
3046 rc = server->ops->logoff(xid, ses);
3047 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003048 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05003049 __func__, rc);
3050 _free_xid(xid);
3051 }
3052
3053 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003054 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303055 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05003056
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003057 /* close any extra channels */
3058 if (ses->chan_count > 1) {
3059 int i;
3060
3061 for (i = 1; i < ses->chan_count; i++)
3062 cifs_put_tcp_session(ses->chans[i].server, 0);
3063 }
3064
Jeff Layton14fbf502008-11-14 13:53:46 -05003065 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003066 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05003067}
3068
Jeff Layton8a8798a2012-01-17 16:09:15 -05003069#ifdef CONFIG_KEYS
3070
Chen Gang057d6332013-07-19 09:01:36 +08003071/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
3072#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003073
3074/* Populate username and pw fields from keyring if possible */
3075static int
3076cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
3077{
3078 int rc = 0;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003079 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01003080 const char *delim, *payload;
3081 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003082 ssize_t len;
3083 struct key *key;
3084 struct TCP_Server_Info *server = ses->server;
3085 struct sockaddr_in *sa;
3086 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01003087 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003088
3089 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
3090 if (!desc)
3091 return -ENOMEM;
3092
3093 /* try to find an address key first */
3094 switch (server->dstaddr.ss_family) {
3095 case AF_INET:
3096 sa = (struct sockaddr_in *)&server->dstaddr;
3097 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
3098 break;
3099 case AF_INET6:
3100 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
3101 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
3102 break;
3103 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003104 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
3105 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003106 rc = -EINVAL;
3107 goto out_err;
3108 }
3109
Joe Perchesf96637b2013-05-04 22:12:25 -05003110 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07003111 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05003112 if (IS_ERR(key)) {
3113 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003114 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05003115 rc = PTR_ERR(key);
3116 goto out_err;
3117 }
3118
3119 /* didn't work, try to find a domain key */
3120 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05003121 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07003122 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05003123 if (IS_ERR(key)) {
3124 rc = PTR_ERR(key);
3125 goto out_err;
3126 }
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003127 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003128 }
3129
3130 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00003131 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003132 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05003133 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003134 goto out_key_put;
3135 }
3136
3137 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01003138 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003139 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05003140 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003141 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003142 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
3143 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003144 rc = -EINVAL;
3145 goto out_key_put;
3146 }
3147
3148 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04003149 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003150 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
3151 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003152 rc = -EINVAL;
3153 goto out_key_put;
3154 }
3155
3156 vol->username = kstrndup(payload, len, GFP_KERNEL);
3157 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003158 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
3159 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003160 rc = -ENOMEM;
3161 goto out_key_put;
3162 }
Joe Perchesf96637b2013-05-04 22:12:25 -05003163 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003164
3165 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04003166 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003167 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003168 rc = -EINVAL;
3169 kfree(vol->username);
3170 vol->username = NULL;
3171 goto out_key_put;
3172 }
3173
3174 ++delim;
3175 vol->password = kstrndup(delim, len, GFP_KERNEL);
3176 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003177 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
3178 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003179 rc = -ENOMEM;
3180 kfree(vol->username);
3181 vol->username = NULL;
3182 goto out_key_put;
3183 }
3184
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003185 /*
3186 * If we have a domain key then we must set the domainName in the
3187 * for the request.
3188 */
3189 if (is_domain && ses->domainName) {
3190 vol->domainname = kstrndup(ses->domainName,
3191 strlen(ses->domainName),
3192 GFP_KERNEL);
3193 if (!vol->domainname) {
3194 cifs_dbg(FYI, "Unable to allocate %zd bytes for "
3195 "domain\n", len);
3196 rc = -ENOMEM;
3197 kfree(vol->username);
3198 vol->username = NULL;
Dan Carpenter478228e2019-08-27 13:59:17 +03003199 kzfree(vol->password);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003200 vol->password = NULL;
3201 goto out_key_put;
3202 }
3203 }
3204
Jeff Layton8a8798a2012-01-17 16:09:15 -05003205out_key_put:
3206 up_read(&key->sem);
3207 key_put(key);
3208out_err:
3209 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05003210 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003211 return rc;
3212}
3213#else /* ! CONFIG_KEYS */
3214static inline int
3215cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
3216 struct cifs_ses *ses __attribute__((unused)))
3217{
3218 return -ENOSYS;
3219}
3220#endif /* CONFIG_KEYS */
3221
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003222/**
3223 * cifs_get_smb_ses - get a session matching @volume_info data from @server
3224 *
3225 * This function assumes it is being called from cifs_mount() where we
3226 * already got a server reference (server refcount +1). See
3227 * cifs_get_tcon() for refcount explanations.
3228 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003229struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04003230cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
3231{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003232 int rc = -ENOMEM;
3233 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00003234 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003235 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3236 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04003237
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003238 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04003239
Jeff Layton4ff67b72010-07-06 20:43:02 -04003240 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04003241 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003242 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
3243 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04003244
Jeff Layton36988c72010-04-24 07:57:43 -04003245 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04003246 rc = cifs_negotiate_protocol(xid, ses);
3247 if (rc) {
3248 mutex_unlock(&ses->session_mutex);
3249 /* problem -- put our ses reference */
3250 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003251 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04003252 return ERR_PTR(rc);
3253 }
Jeff Layton36988c72010-04-24 07:57:43 -04003254 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003255 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04003256 rc = cifs_setup_session(xid, ses,
3257 volume_info->local_nls);
3258 if (rc) {
3259 mutex_unlock(&ses->session_mutex);
3260 /* problem -- put our reference */
3261 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003262 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003263 return ERR_PTR(rc);
3264 }
3265 }
3266 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04003267
3268 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003269 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003270 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003271 return ses;
3272 }
3273
Joe Perchesf96637b2013-05-04 22:12:25 -05003274 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04003275 ses = sesInfoAlloc();
3276 if (ses == NULL)
3277 goto get_ses_fail;
3278
3279 /* new SMB session uses our server ref */
3280 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003281 if (server->dstaddr.ss_family == AF_INET6)
3282 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04003283 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003284 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04003285
Steve French8727c8a2011-02-25 01:11:56 -06003286 if (volume_info->username) {
3287 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
3288 if (!ses->user_name)
3289 goto get_ses_fail;
3290 }
Jeff Layton36988c72010-04-24 07:57:43 -04003291
3292 /* volume_info->password freed at unmount */
3293 if (volume_info->password) {
3294 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
3295 if (!ses->password)
3296 goto get_ses_fail;
3297 }
3298 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05003299 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
3300 if (!ses->domainName)
3301 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04003302 }
Germano Percossi39566442016-12-15 12:31:18 +05303303 if (volume_info->domainauto)
3304 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04003305 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04003306 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00003307
Jeff Layton28e11bd2013-05-26 07:01:00 -04003308 ses->sectype = volume_info->sectype;
3309 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04003310 mutex_lock(&ses->session_mutex);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003311
3312 /* add server as first channel */
3313 ses->chans[0].server = server;
3314 ses->chan_count = 1;
3315 ses->chan_max = volume_info->multichannel ? volume_info->max_channels:1;
3316
Jeff Layton198b5682010-04-24 07:57:48 -04003317 rc = cifs_negotiate_protocol(xid, ses);
3318 if (!rc)
3319 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003320
3321 /* each channel uses a different signing key */
3322 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
3323 sizeof(ses->smb3signingkey));
3324
Jeff Layton36988c72010-04-24 07:57:43 -04003325 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00003326 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04003327 goto get_ses_fail;
3328
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003329 /* success, put it on the list and add it as first channel */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303330 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003331 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303332 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003333
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003334 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01003335
3336 cifs_setup_ipc(ses, volume_info);
3337
Jeff Layton36988c72010-04-24 07:57:43 -04003338 return ses;
3339
3340get_ses_fail:
3341 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003342 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003343 return ERR_PTR(rc);
3344}
3345
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003346static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003347{
3348 if (tcon->tidStatus == CifsExiting)
3349 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003350 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003351 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003352 if (tcon->seal != volume_info->seal)
3353 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003354 if (tcon->snapshot_time != volume_info->snapshot_time)
3355 return 0;
Steve Frenchca567eb2019-03-29 16:31:07 -05003356 if (tcon->handle_timeout != volume_info->handle_timeout)
3357 return 0;
Steve French3e7a02d2019-09-11 21:46:20 -05003358 if (tcon->no_lease != volume_info->no_lease)
3359 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003360 return 1;
3361}
3362
Steve French96daf2b2011-05-27 04:34:02 +00003363static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06003364cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00003367 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303369 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003370 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00003371 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003372 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05003373 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003374 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303375 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return tcon;
3377 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303378 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return NULL;
3380}
3381
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003382void
Steve French96daf2b2011-05-27 04:34:02 +00003383cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05003384{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003385 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003386 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003387
Aurelien Aptelb327a712018-01-24 13:46:10 +01003388 /*
3389 * IPC tcon share the lifetime of their session and are
3390 * destroyed in the session put function
3391 */
3392 if (tcon == NULL || tcon->ipc)
3393 return;
3394
3395 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05003396 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303397 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003398 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303399 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003400 return;
3401 }
3402
3403 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303404 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003405
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003406 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003407 if (ses->server->ops->tree_disconnect)
3408 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003409 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003410
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303411 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00003412 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003413 cifs_put_smb_ses(ses);
3414}
3415
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003416/**
3417 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
3418 *
3419 * - tcon refcount is the number of mount points using the tcon.
3420 * - ses refcount is the number of tcon using the session.
3421 *
3422 * 1. This function assumes it is being called from cifs_mount() where
3423 * we already got a session reference (ses refcount +1).
3424 *
3425 * 2. Since we're in the context of adding a mount point, the end
3426 * result should be either:
3427 *
3428 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3429 * its session refcount incremented (1 new tcon). This +1 was
3430 * already done in (1).
3431 *
3432 * b) an existing tcon with refcount+1 (add a mount point to it) and
3433 * identical ses refcount (no new tcon). Because of (1) we need to
3434 * decrement the ses refcount.
3435 */
Steve French96daf2b2011-05-27 04:34:02 +00003436static struct cifs_tcon *
3437cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003438{
3439 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003440 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003441
Steve French8b217fe2016-11-11 22:36:20 -06003442 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003443 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003444 /*
3445 * tcon has refcount already incremented but we need to
3446 * decrement extra ses reference gotten by caller (case b)
3447 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003448 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003449 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003450 return tcon;
3451 }
3452
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003453 if (!ses->server->ops->tree_connect) {
3454 rc = -ENOSYS;
3455 goto out_fail;
3456 }
3457
Jeff Laytond00c28d2010-04-24 07:57:44 -04003458 tcon = tconInfoAlloc();
3459 if (tcon == NULL) {
3460 rc = -ENOMEM;
3461 goto out_fail;
3462 }
3463
Steve French8b217fe2016-11-11 22:36:20 -06003464 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003465 if (ses->server->vals->protocol_id == 0) {
3466 cifs_dbg(VFS,
3467 "Use SMB2 or later for snapshot mount option\n");
3468 rc = -EOPNOTSUPP;
3469 goto out_fail;
3470 } else
3471 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003472 }
3473
Steve Frenchca567eb2019-03-29 16:31:07 -05003474 if (volume_info->handle_timeout) {
3475 if (ses->server->vals->protocol_id == 0) {
3476 cifs_dbg(VFS,
3477 "Use SMB2.1 or later for handle timeout option\n");
3478 rc = -EOPNOTSUPP;
3479 goto out_fail;
3480 } else
3481 tcon->handle_timeout = volume_info->handle_timeout;
3482 }
3483
Jeff Laytond00c28d2010-04-24 07:57:44 -04003484 tcon->ses = ses;
3485 if (volume_info->password) {
3486 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3487 if (!tcon->password) {
3488 rc = -ENOMEM;
3489 goto out_fail;
3490 }
3491 }
3492
Steve French23657ad2018-04-22 15:14:58 -05003493 if (volume_info->seal) {
3494 if (ses->server->vals->protocol_id == 0) {
3495 cifs_dbg(VFS,
3496 "SMB3 or later required for encryption\n");
3497 rc = -EOPNOTSUPP;
3498 goto out_fail;
3499 } else if (tcon->ses->server->capabilities &
3500 SMB2_GLOBAL_CAP_ENCRYPTION)
3501 tcon->seal = true;
3502 else {
3503 cifs_dbg(VFS, "Encryption is not supported on share\n");
3504 rc = -EOPNOTSUPP;
3505 goto out_fail;
3506 }
3507 }
3508
Steve French8505c8b2018-06-18 14:01:59 -05003509 if (volume_info->linux_ext) {
3510 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003511 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003512 printk_once(KERN_WARNING
3513 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003514 } else {
3515 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3516 rc = -EOPNOTSUPP;
3517 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003518 }
Steve Frenchb3266142018-05-20 23:41:10 -05003519 }
Steve Frenchb3266142018-05-20 23:41:10 -05003520
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003521 /*
3522 * BB Do we need to wrap session_mutex around this TCon call and Unix
3523 * SetFS as we do on SessSetup and reconnect?
3524 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003525 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003526 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3527 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003528 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003529 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003530 if (rc)
3531 goto out_fail;
3532
Steve Frenchb618f002015-11-03 09:15:03 -06003533 tcon->use_persistent = false;
3534 /* check if SMB2 or later, CIFS does not support persistent handles */
3535 if (volume_info->persistent) {
3536 if (ses->server->vals->protocol_id == 0) {
3537 cifs_dbg(VFS,
3538 "SMB3 or later required for persistent handles\n");
3539 rc = -EOPNOTSUPP;
3540 goto out_fail;
3541 } else if (ses->server->capabilities &
3542 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3543 tcon->use_persistent = true;
3544 else /* persistent handles requested but not supported */ {
3545 cifs_dbg(VFS,
3546 "Persistent handles not supported on share\n");
3547 rc = -EOPNOTSUPP;
3548 goto out_fail;
3549 }
3550 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3551 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3552 && (volume_info->nopersistent == false)) {
3553 cifs_dbg(FYI, "enabling persistent handles\n");
3554 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003555 } else if (volume_info->resilient) {
3556 if (ses->server->vals->protocol_id == 0) {
3557 cifs_dbg(VFS,
3558 "SMB2.1 or later required for resilient handles\n");
3559 rc = -EOPNOTSUPP;
3560 goto out_fail;
3561 }
3562 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003563 }
3564
Steve Frenchcae53f72019-09-03 17:49:46 -05003565 /* If the user really knows what they are doing they can override */
3566 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
3567 if (volume_info->cache_ro)
3568 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
3569 else if (volume_info->cache_rw)
3570 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
3571 }
3572
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003573 /*
3574 * We can have only one retry value for a connection to a share so for
3575 * resources mounted more than once to the same server share the last
3576 * value passed in for the retry flag is used.
3577 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003578 tcon->retry = volume_info->retry;
3579 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003580 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003581 tcon->local_lease = volume_info->local_lease;
Steve French3e7a02d2019-09-11 21:46:20 -05003582 tcon->no_lease = volume_info->no_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003583 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003584
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303585 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003586 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303587 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003588
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303589 cifs_fscache_get_super_cookie(tcon);
3590
Jeff Laytond00c28d2010-04-24 07:57:44 -04003591 return tcon;
3592
3593out_fail:
3594 tconInfoFree(tcon);
3595 return ERR_PTR(rc);
3596}
3597
Jeff Layton9d002df2010-10-06 19:51:11 -04003598void
3599cifs_put_tlink(struct tcon_link *tlink)
3600{
3601 if (!tlink || IS_ERR(tlink))
3602 return;
3603
3604 if (!atomic_dec_and_test(&tlink->tl_count) ||
3605 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3606 tlink->tl_time = jiffies;
3607 return;
3608 }
3609
3610 if (!IS_ERR(tlink_tcon(tlink)))
3611 cifs_put_tcon(tlink_tcon(tlink));
3612 kfree(tlink);
3613 return;
3614}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003615
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003616static int
3617compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3618{
3619 struct cifs_sb_info *old = CIFS_SB(sb);
3620 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03003621 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
3622 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003623
3624 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3625 return 0;
3626
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03003627 if (old->mnt_cifs_serverino_autodisabled)
3628 newflags &= ~CIFS_MOUNT_SERVER_INUM;
3629
3630 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003631 return 0;
3632
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003633 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003634 * We want to share sb only if we don't specify an r/wsize or
3635 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003636 */
3637 if (new->wsize && new->wsize < old->wsize)
3638 return 0;
3639
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003640 if (new->rsize && new->rsize < old->rsize)
3641 return 0;
3642
Eric W. Biederman1f682332013-02-06 01:20:20 -08003643 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003644 return 0;
3645
3646 if (old->mnt_file_mode != new->mnt_file_mode ||
3647 old->mnt_dir_mode != new->mnt_dir_mode)
3648 return 0;
3649
3650 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3651 return 0;
3652
3653 if (old->actimeo != new->actimeo)
3654 return 0;
3655
3656 return 1;
3657}
3658
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003659static int
3660match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3661{
3662 struct cifs_sb_info *old = CIFS_SB(sb);
3663 struct cifs_sb_info *new = mnt_data->cifs_sb;
Ronnie Sahlbergfe129262020-01-22 11:07:56 +10003664 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3665 old->prepath;
3666 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3667 new->prepath;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003668
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003669 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003670 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003671 else if (!old_set && !new_set)
3672 return 1;
3673
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003674 return 0;
3675}
3676
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003677int
3678cifs_match_super(struct super_block *sb, void *data)
3679{
3680 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3681 struct smb_vol *volume_info;
3682 struct cifs_sb_info *cifs_sb;
3683 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003684 struct cifs_ses *ses;
3685 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003686 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003687 int rc = 0;
3688
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003689 spin_lock(&cifs_tcp_ses_lock);
3690 cifs_sb = CIFS_SB(sb);
3691 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3692 if (IS_ERR(tlink)) {
3693 spin_unlock(&cifs_tcp_ses_lock);
3694 return rc;
3695 }
3696 tcon = tlink_tcon(tlink);
3697 ses = tcon->ses;
3698 tcp_srv = ses->server;
3699
3700 volume_info = mnt_data->vol;
3701
Jeff Layton9fa114f2012-11-26 11:09:57 -05003702 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003703 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003704 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003705 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003706 rc = 0;
3707 goto out;
3708 }
3709
3710 rc = compare_mount_options(sb, mnt_data);
3711out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003712 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003713 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003714 return rc;
3715}
3716
Jeff Layton09e50d52008-07-23 10:11:19 -04003717#ifdef CONFIG_DEBUG_LOCK_ALLOC
3718static struct lock_class_key cifs_key[2];
3719static struct lock_class_key cifs_slock_key[2];
3720
3721static inline void
3722cifs_reclassify_socket4(struct socket *sock)
3723{
3724 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003725 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003726 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3727 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3728}
3729
3730static inline void
3731cifs_reclassify_socket6(struct socket *sock)
3732{
3733 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003734 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003735 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3736 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3737}
3738#else
3739static inline void
3740cifs_reclassify_socket4(struct socket *sock)
3741{
3742}
3743
3744static inline void
3745cifs_reclassify_socket6(struct socket *sock)
3746{
3747}
3748#endif
3749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003751static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
Steve French50c2f752007-07-13 00:33:32 +00003753 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Steve French50c2f752007-07-13 00:33:32 +00003755 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 /* mask a nibble at a time and encode */
3757 target[j] = 'A' + (0x0F & (source[i] >> 4));
3758 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003759 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 }
3761
3762}
3763
Ben Greear3eb9a882010-09-01 17:06:02 -07003764static int
3765bind_socket(struct TCP_Server_Info *server)
3766{
3767 int rc = 0;
3768 if (server->srcaddr.ss_family != AF_UNSPEC) {
3769 /* Bind to the specified local IP address */
3770 struct socket *socket = server->ssocket;
3771 rc = socket->ops->bind(socket,
3772 (struct sockaddr *) &server->srcaddr,
3773 sizeof(server->srcaddr));
3774 if (rc < 0) {
3775 struct sockaddr_in *saddr4;
3776 struct sockaddr_in6 *saddr6;
3777 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3778 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3779 if (saddr6->sin6_family == AF_INET6)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003780 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003781 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003782 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003783 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003784 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003785 }
3786 }
3787 return rc;
3788}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003791ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
3793 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003794 /*
3795 * some servers require RFC1001 sessinit before sending
3796 * negprot - BB check reconnection in case where second
3797 * sessinit is sent but no second negprot
3798 */
3799 struct rfc1002_session_packet *ses_init_buf;
3800 struct smb_hdr *smb_buf;
3801 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3802 GFP_KERNEL);
3803 if (ses_init_buf) {
3804 ses_init_buf->trailer.session_req.called_len = 32;
3805
Colin Ian King997152f2016-01-25 16:25:54 +00003806 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003807 rfc1002mangle(ses_init_buf->trailer.
3808 session_req.called_name,
3809 server->server_RFC1001_name,
3810 RFC1001_NAME_LEN_WITH_NULL);
3811 else
3812 rfc1002mangle(ses_init_buf->trailer.
3813 session_req.called_name,
3814 DEFAULT_CIFS_CALLED_NAME,
3815 RFC1001_NAME_LEN_WITH_NULL);
3816
3817 ses_init_buf->trailer.session_req.calling_len = 32;
3818
3819 /*
3820 * calling name ends in null (byte 16) from old smb
3821 * convention.
3822 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003823 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003824 rfc1002mangle(ses_init_buf->trailer.
3825 session_req.calling_name,
3826 server->workstation_RFC1001_name,
3827 RFC1001_NAME_LEN_WITH_NULL);
3828 else
3829 rfc1002mangle(ses_init_buf->trailer.
3830 session_req.calling_name,
3831 "LINUX_CIFS_CLNT",
3832 RFC1001_NAME_LEN_WITH_NULL);
3833
3834 ses_init_buf->trailer.session_req.scope1 = 0;
3835 ses_init_buf->trailer.session_req.scope2 = 0;
3836 smb_buf = (struct smb_hdr *)ses_init_buf;
3837
3838 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003839 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003840 rc = smb_send(server, smb_buf, 0x44);
3841 kfree(ses_init_buf);
3842 /*
3843 * RFC1001 layer in at least one server
3844 * requires very short break before negprot
3845 * presumably because not expecting negprot
3846 * to follow so fast. This is a simple
3847 * solution that works without
3848 * complicating the code and causes no
3849 * significant slowing down on mount
3850 * for everyone else
3851 */
3852 usleep_range(1000, 2000);
3853 }
3854 /*
3855 * else the negprot may still work without this
3856 * even though malloc failed
3857 */
3858
3859 return rc;
3860}
3861
3862static int
3863generic_ip_connect(struct TCP_Server_Info *server)
3864{
3865 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003866 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003867 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003868 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003869 struct sockaddr *saddr;
3870
3871 saddr = (struct sockaddr *) &server->dstaddr;
3872
3873 if (server->dstaddr.ss_family == AF_INET6) {
3874 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3875 slen = sizeof(struct sockaddr_in6);
3876 sfamily = AF_INET6;
3877 } else {
3878 sport = ((struct sockaddr_in *) saddr)->sin_port;
3879 slen = sizeof(struct sockaddr_in);
3880 sfamily = AF_INET;
3881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003883 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003884 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3885 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (rc < 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003887 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003888 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003891
3892 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003893 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003894 server->ssocket = socket;
3895 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003896 if (sfamily == AF_INET6)
3897 cifs_reclassify_socket6(socket);
3898 else
3899 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 }
3901
Ben Greear3eb9a882010-09-01 17:06:02 -07003902 rc = bind_socket(server);
3903 if (rc < 0)
3904 return rc;
3905
Jeff Laytond5c56052008-12-01 18:42:33 -05003906 /*
3907 * Eventually check for other socket options to change from
3908 * the default. sock_setsockopt not used because it expects
3909 * user space buffer
3910 */
3911 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003912 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003913
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003914 /* make the bufsizes depend on wsize/rsize and max requests */
3915 if (server->noautotune) {
3916 if (socket->sk->sk_sndbuf < (200 * 1024))
3917 socket->sk->sk_sndbuf = 200 * 1024;
3918 if (socket->sk->sk_rcvbuf < (140 * 1024))
3919 socket->sk->sk_rcvbuf = 140 * 1024;
3920 }
3921
Steve French6a5fa2362010-01-01 01:28:43 +00003922 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003923 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003924 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3925 (char *)&val, sizeof(val));
3926 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003927 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3928 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003929 }
3930
Joe Perchesf96637b2013-05-04 22:12:25 -05003931 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003932 socket->sk->sk_sndbuf,
3933 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3934
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03003935 rc = socket->ops->connect(socket, saddr, slen,
3936 server->noblockcnt ? O_NONBLOCK : 0);
Paulo Alcantara (SUSE)d532cc72019-10-10 12:31:58 -03003937 /*
3938 * When mounting SMB root file systems, we do not want to block in
3939 * connect. Otherwise bail out and then let cifs_reconnect() perform
3940 * reconnect failover - if possible.
3941 */
3942 if (server->noblockcnt && rc == -EINPROGRESS)
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03003943 rc = 0;
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003944 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003945 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003946 sock_release(socket);
3947 server->ssocket = NULL;
3948 return rc;
3949 }
3950
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003951 if (sport == htons(RFC1001_PORT))
3952 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return rc;
3955}
3956
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003957static int
3958ip_connect(struct TCP_Server_Info *server)
3959{
Steve French6da97912011-03-13 18:55:55 +00003960 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003961 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3962 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3963
3964 if (server->dstaddr.ss_family == AF_INET6)
3965 sport = &addr6->sin6_port;
3966 else
3967 sport = &addr->sin_port;
3968
3969 if (*sport == 0) {
3970 int rc;
3971
3972 /* try with 445 port at first */
3973 *sport = htons(CIFS_PORT);
3974
3975 rc = generic_ip_connect(server);
3976 if (rc >= 0)
3977 return rc;
3978
3979 /* if it failed, try with 139 port */
3980 *sport = htons(RFC1001_PORT);
3981 }
3982
3983 return generic_ip_connect(server);
3984}
3985
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003986void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003987 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003988{
3989 /* if we are reconnecting then should we check to see if
3990 * any requested capabilities changed locally e.g. via
3991 * remount but we can not do much about it here
3992 * if they have (even if we could detect it by the following)
3993 * Perhaps we could add a backpointer to array of sb from tcon
3994 * or if we change to make all sb to same share the same
3995 * sb as NFS - then we only have one backpointer to sb.
3996 * What if we wanted to mount the server share twice once with
3997 * and once without posixacls or posix paths? */
3998 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003999
Steve Frenchc18c8422007-07-18 23:21:09 +00004000 if (vol_info && vol_info->no_linux_ext) {
4001 tcon->fsUnixInfo.Capability = 0;
4002 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05004003 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00004004 return;
4005 } else if (vol_info)
4006 tcon->unix_ext = 1; /* Unix Extensions supported */
4007
4008 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004009 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00004010 return;
4011 }
Steve French50c2f752007-07-13 00:33:32 +00004012
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004013 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00004014 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05004015 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00004016 /* check for reconnect case in which we do not
4017 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004018 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00004019 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00004020 originally at mount time */
4021 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
4022 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00004023 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
4024 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004025 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00004026 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00004027 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004028 cifs_dbg(VFS, "possible reconnect error\n");
4029 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00004030 }
Steve French8af18972007-02-14 04:42:51 +00004031 }
Steve French50c2f752007-07-13 00:33:32 +00004032
Steve French6848b732011-05-26 18:38:54 +00004033 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004034 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00004035
Steve French8af18972007-02-14 04:42:51 +00004036 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00004037 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00004038 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00004039 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004040 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04004041 if (cifs_sb)
4042 cifs_sb->mnt_cifs_flags |=
4043 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00004044 }
4045
Steve French75865f8c2007-06-24 18:30:48 +00004046 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00004047 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00004048 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004049 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04004050 if (cifs_sb)
4051 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00004052 CIFS_MOUNT_POSIX_PATHS;
4053 }
Steve French50c2f752007-07-13 00:33:32 +00004054
Joe Perchesf96637b2013-05-04 22:12:25 -05004055 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00004056#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00004057 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004058 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004059 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004060 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004061 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004062 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004063 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004064 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004065 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004066 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004067 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004068 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00004069 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004070 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00004071 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004072 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00004073 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05004074 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00004075#endif /* CIFS_DEBUG2 */
4076 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00004077 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004078 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00004079 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05004080 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 +00004081
Steve French8af18972007-02-14 04:42:51 +00004082 }
4083 }
4084}
4085
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01004086int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004087 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04004088{
Jeff Layton2de970f2010-10-06 19:51:12 -04004089 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
4090
Al Viro2ced6f62011-06-17 09:20:04 -04004091 spin_lock_init(&cifs_sb->tlink_tree_lock);
4092 cifs_sb->tlink_tree = RB_ROOT;
4093
Steve Frenche8506d22019-02-28 21:32:15 -06004094 cifs_sb->bsize = pvolume_info->bsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04004095 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04004096 * Temporarily set r/wsize for matching superblock. If we end up using
4097 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04004098 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04004099 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04004100 cifs_sb->wsize = pvolume_info->wsize;
4101
Steve French3b795212008-11-13 19:45:32 +00004102 cifs_sb->mnt_uid = pvolume_info->linux_uid;
4103 cifs_sb->mnt_gid = pvolume_info->linux_gid;
4104 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
4105 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Frank Sorensonf52aa792020-02-12 15:31:48 -06004106 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05004107 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00004108
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05304109 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004110 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05304111
Aurelien Aptel83930722018-09-20 18:10:25 -07004112 if (pvolume_info->nodfs)
4113 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
Steve French3b795212008-11-13 19:45:32 +00004114 if (pvolume_info->noperm)
4115 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
4116 if (pvolume_info->setuids)
4117 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05004118 if (pvolume_info->setuidfromacl)
4119 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00004120 if (pvolume_info->server_ino)
4121 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
4122 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05004123 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
4124 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00004125 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
4126 if (pvolume_info->no_xattr)
4127 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
4128 if (pvolume_info->sfu_emul)
4129 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
4130 if (pvolume_info->nobrl)
4131 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05004132 if (pvolume_info->nohandlecache)
4133 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00004134 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00004135 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00004136 if (pvolume_info->mand_lock)
4137 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00004138 if (pvolume_info->rwpidforward)
4139 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French412094a2019-06-24 02:01:42 -05004140 if (pvolume_info->mode_ace)
4141 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MODE_FROM_SID;
Steve French3b795212008-11-13 19:45:32 +00004142 if (pvolume_info->cifs_acl)
4143 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01004144 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05004145 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01004146 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
4147 }
4148 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05004149 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01004150 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
4151 }
Steve French3b795212008-11-13 19:45:32 +00004152 if (pvolume_info->override_uid)
4153 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
4154 if (pvolume_info->override_gid)
4155 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
4156 if (pvolume_info->dynperm)
4157 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05304158 if (pvolume_info->fsc)
4159 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04004160 if (pvolume_info->multiuser)
4161 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
4162 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05004163 if (pvolume_info->strict_io)
4164 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00004165 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004166 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00004167 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
4168 }
Steve French83bbfa72019-08-27 23:58:54 -05004169 if (pvolume_info->cache_ro) {
4170 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
4171 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
Steve French41e033f2019-08-30 02:12:41 -05004172 } else if (pvolume_info->cache_rw) {
4173 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
4174 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
4175 CIFS_MOUNT_RW_CACHE);
Steve French83bbfa72019-08-27 23:58:54 -05004176 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004177 if (pvolume_info->mfsymlinks) {
4178 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05004179 /*
4180 * Our SFU ("Services for Unix" emulation does not allow
4181 * creating symlinks but does allow reading existing SFU
4182 * symlinks (it does allow both creating and reading SFU
4183 * style mknod and FIFOs though). When "mfsymlinks" and
4184 * "sfu" are both enabled at the same time, it allows
4185 * reading both types of symlinks, but will only create
4186 * them with mfsymlinks format. This allows better
4187 * Apple compatibility (probably better for Samba too)
4188 * while still recognizing old Windows style symlinks.
4189 */
4190 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004191 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05004192 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004193 }
Steve French3b795212008-11-13 19:45:32 +00004194
4195 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05004196 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01004197
4198 if (pvolume_info->prepath) {
4199 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
4200 if (cifs_sb->prepath == NULL)
4201 return -ENOMEM;
4202 }
4203
4204 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04004205}
4206
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004207void
4208cifs_cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004209{
Sean Finneyb9468452011-04-11 13:19:32 +00004210 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004211 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02004212 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00004213 kfree(volume_info->domainname);
4214 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004215 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004216}
4217
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004218void
4219cifs_cleanup_volume_info(struct smb_vol *volume_info)
4220{
4221 if (!volume_info)
4222 return;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004223 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004224 kfree(volume_info);
4225}
4226
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004227/* Release all succeed connections */
4228static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
4229 unsigned int xid,
4230 struct TCP_Server_Info *server,
4231 struct cifs_ses *ses, struct cifs_tcon *tcon)
4232{
4233 int rc = 0;
4234
4235 if (tcon)
4236 cifs_put_tcon(tcon);
4237 else if (ses)
4238 cifs_put_smb_ses(ses);
4239 else if (server)
4240 cifs_put_tcp_session(server, 0);
4241 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
4242 free_xid(xid);
4243}
4244
4245/* Get connections for tcp, ses and tcon */
4246static int mount_get_conns(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
4247 unsigned int *xid,
4248 struct TCP_Server_Info **nserver,
4249 struct cifs_ses **nses, struct cifs_tcon **ntcon)
4250{
4251 int rc = 0;
4252 struct TCP_Server_Info *server;
4253 struct cifs_ses *ses;
4254 struct cifs_tcon *tcon;
4255
4256 *nserver = NULL;
4257 *nses = NULL;
4258 *ntcon = NULL;
4259
4260 *xid = get_xid();
4261
4262 /* get a reference to a tcp session */
4263 server = cifs_get_tcp_session(vol);
4264 if (IS_ERR(server)) {
4265 rc = PTR_ERR(server);
4266 return rc;
4267 }
4268
4269 *nserver = server;
4270
4271 if ((vol->max_credits < 20) || (vol->max_credits > 60000))
4272 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
4273 else
4274 server->max_credits = vol->max_credits;
4275
4276 /* get a reference to a SMB session */
4277 ses = cifs_get_smb_ses(server, vol);
4278 if (IS_ERR(ses)) {
4279 rc = PTR_ERR(ses);
4280 return rc;
4281 }
4282
4283 *nses = ses;
4284
4285 if ((vol->persistent == true) && (!(ses->server->capabilities &
4286 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10004287 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004288 return -EOPNOTSUPP;
4289 }
4290
4291 /* search for existing tcon to this server share */
4292 tcon = cifs_get_tcon(ses, vol);
4293 if (IS_ERR(tcon)) {
4294 rc = PTR_ERR(tcon);
4295 return rc;
4296 }
4297
4298 *ntcon = tcon;
4299
4300 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4301 if (tcon->posix_extensions)
4302 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4303
4304 /* tell server which Unix caps we support */
4305 if (cap_unix(tcon->ses)) {
4306 /*
4307 * reset of caps checks mount to see if unix extensions disabled
4308 * for just this mount.
4309 */
4310 reset_cifs_unix_caps(*xid, tcon, cifs_sb, vol);
4311 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4312 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4313 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
4314 return -EACCES;
4315 } else
4316 tcon->unix_ext = 0; /* server does not support them */
4317
4318 /* do not care if a following call succeed - informational */
Steve French1981eba2019-08-29 22:33:38 -05004319 if (!tcon->pipe && server->ops->qfs_tcon) {
Amir Goldstein0f060932020-02-03 21:46:43 +02004320 server->ops->qfs_tcon(*xid, tcon, cifs_sb);
Steve French1981eba2019-08-29 22:33:38 -05004321 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
4322 if (tcon->fsDevInfo.DeviceCharacteristics &
Steve French52870d52019-10-01 21:25:46 -05004323 cpu_to_le32(FILE_READ_ONLY_DEVICE))
Steve French1981eba2019-08-29 22:33:38 -05004324 cifs_dbg(VFS, "mounted to read only share\n");
Steve French41e033f2019-08-30 02:12:41 -05004325 else if ((cifs_sb->mnt_cifs_flags &
4326 CIFS_MOUNT_RW_CACHE) == 0)
Steve French1981eba2019-08-29 22:33:38 -05004327 cifs_dbg(VFS, "read only mount of RW share\n");
Steve French41e033f2019-08-30 02:12:41 -05004328 /* no need to log a RW mount of a typical RW share */
Steve French1981eba2019-08-29 22:33:38 -05004329 }
4330 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004331
4332 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, vol);
4333 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, vol);
4334
4335 return 0;
4336}
4337
4338static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
4339 struct cifs_tcon *tcon)
4340{
4341 struct tcon_link *tlink;
4342
4343 /* hang the tcon off of the superblock */
4344 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4345 if (tlink == NULL)
4346 return -ENOMEM;
4347
4348 tlink->tl_uid = ses->linux_uid;
4349 tlink->tl_tcon = tcon;
4350 tlink->tl_time = jiffies;
4351 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4352 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4353
4354 cifs_sb->master_tlink = tlink;
4355 spin_lock(&cifs_sb->tlink_tree_lock);
4356 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4357 spin_unlock(&cifs_sb->tlink_tree_lock);
4358
4359 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4360 TLINK_IDLE_EXPIRE);
4361 return 0;
4362}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004363
Steve French2d6d5892009-04-09 00:36:44 +00004364#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06004365/*
4366 * cifs_build_path_to_root returns full path to root when we do not have an
4367 * exiting connection (tcon)
4368 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004369static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004370build_unc_path_to_root(const struct smb_vol *vol,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004371 const struct cifs_sb_info *cifs_sb, bool useppath)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004372{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004373 char *full_path, *pos;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004374 unsigned int pplen = useppath && vol->prepath ?
4375 strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004376 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004377
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004378 if (unc_len > MAX_TREE_SIZE)
4379 return ERR_PTR(-EINVAL);
4380
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004381 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004382 if (full_path == NULL)
4383 return ERR_PTR(-ENOMEM);
4384
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004385 memcpy(full_path, vol->UNC, unc_len);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004386 pos = full_path + unc_len;
4387
4388 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04004389 *pos = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004390 memcpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004391 pos += pplen;
4392 }
4393
4394 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00004395 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05004396 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004397 return full_path;
4398}
Sean Finneydd613942011-04-11 13:19:30 +00004399
Paulo Alcantara1c780222018-11-14 16:24:03 -02004400/**
4401 * expand_dfs_referral - Perform a dfs referral query and update the cifs_sb
4402 *
Sean Finneydd613942011-04-11 13:19:30 +00004403 *
Sean Finney046462a2011-04-11 13:19:33 +00004404 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
4405 * to a string containing updated options for the submount. Otherwise it
4406 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00004407 *
4408 * Returns the rc from get_dfs_path to the caller, which can be used to
4409 * determine whether there were referrals.
4410 */
4411static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004412expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00004413 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00004414 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00004415{
4416 int rc;
Paulo Alcantara1c780222018-11-14 16:24:03 -02004417 struct dfs_info3_param referral = {0};
Sean Finneydd613942011-04-11 13:19:30 +00004418 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
4419
Aurelien Aptel83930722018-09-20 18:10:25 -07004420 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
4421 return -EREMOTE;
4422
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004423 full_path = build_unc_path_to_root(volume_info, cifs_sb, true);
Sean Finneydd613942011-04-11 13:19:30 +00004424 if (IS_ERR(full_path))
4425 return PTR_ERR(full_path);
4426
4427 /* For DFS paths, skip the first '\' of the UNC */
4428 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
4429
Paulo Alcantara1c780222018-11-14 16:24:03 -02004430 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
4431 ref_path, &referral, NULL);
4432 if (!rc) {
Sean Finneydd613942011-04-11 13:19:30 +00004433 char *fake_devname = NULL;
4434
4435 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02004436 full_path + 1, &referral,
Sean Finneydd613942011-04-11 13:19:30 +00004437 &fake_devname);
Paulo Alcantara1c780222018-11-14 16:24:03 -02004438 free_dfs_info_param(&referral);
Sean Finney046462a2011-04-11 13:19:33 +00004439
Sean Finneydd613942011-04-11 13:19:30 +00004440 if (IS_ERR(mdata)) {
4441 rc = PTR_ERR(mdata);
4442 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004443 } else {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004444 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004445 rc = cifs_setup_volume_info(volume_info, mdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02004446 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00004447 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004448 kfree(fake_devname);
4449 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00004450 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00004451 }
4452 kfree(full_path);
4453 return rc;
4454}
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004455
4456static inline int get_next_dfs_tgt(const char *path,
4457 struct dfs_cache_tgt_list *tgt_list,
4458 struct dfs_cache_tgt_iterator **tgt_it)
4459{
4460 if (!*tgt_it)
4461 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
4462 else
4463 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
4464 return !*tgt_it ? -EHOSTDOWN : 0;
4465}
4466
4467static int update_vol_info(const struct dfs_cache_tgt_iterator *tgt_it,
4468 struct smb_vol *fake_vol, struct smb_vol *vol)
4469{
4470 const char *tgt = dfs_cache_get_tgt_name(tgt_it);
4471 int len = strlen(tgt) + 2;
4472 char *new_unc;
4473
4474 new_unc = kmalloc(len, GFP_KERNEL);
4475 if (!new_unc)
4476 return -ENOMEM;
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10004477 scnprintf(new_unc, len, "\\%s", tgt);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004478
4479 kfree(vol->UNC);
4480 vol->UNC = new_unc;
4481
4482 if (fake_vol->prepath) {
4483 kfree(vol->prepath);
4484 vol->prepath = fake_vol->prepath;
4485 fake_vol->prepath = NULL;
4486 }
4487 memcpy(&vol->dstaddr, &fake_vol->dstaddr, sizeof(vol->dstaddr));
4488
4489 return 0;
4490}
4491
4492static int setup_dfs_tgt_conn(const char *path,
4493 const struct dfs_cache_tgt_iterator *tgt_it,
4494 struct cifs_sb_info *cifs_sb,
4495 struct smb_vol *vol,
4496 unsigned int *xid,
4497 struct TCP_Server_Info **server,
4498 struct cifs_ses **ses,
4499 struct cifs_tcon **tcon)
4500{
4501 int rc;
4502 struct dfs_info3_param ref = {0};
4503 char *mdata = NULL, *fake_devname = NULL;
Steve Frenchd0959b02019-10-05 10:53:58 -05004504 struct smb_vol fake_vol = {NULL};
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004505
4506 cifs_dbg(FYI, "%s: dfs path: %s\n", __func__, path);
4507
4508 rc = dfs_cache_get_tgt_referral(path, tgt_it, &ref);
4509 if (rc)
4510 return rc;
4511
4512 mdata = cifs_compose_mount_options(cifs_sb->mountdata, path, &ref,
4513 &fake_devname);
4514 free_dfs_info_param(&ref);
4515
4516 if (IS_ERR(mdata)) {
4517 rc = PTR_ERR(mdata);
4518 mdata = NULL;
4519 } else {
4520 cifs_dbg(FYI, "%s: fake_devname: %s\n", __func__, fake_devname);
4521 rc = cifs_setup_volume_info(&fake_vol, mdata, fake_devname,
4522 false);
4523 }
4524 kfree(mdata);
4525 kfree(fake_devname);
4526
4527 if (!rc) {
4528 /*
4529 * We use a 'fake_vol' here because we need pass it down to the
4530 * mount_{get,put} functions to test connection against new DFS
4531 * targets.
4532 */
4533 mount_put_conns(cifs_sb, *xid, *server, *ses, *tcon);
4534 rc = mount_get_conns(&fake_vol, cifs_sb, xid, server, ses,
4535 tcon);
4536 if (!rc) {
4537 /*
4538 * We were able to connect to new target server.
4539 * Update current volume info with new target server.
4540 */
4541 rc = update_vol_info(tgt_it, &fake_vol, vol);
4542 }
4543 }
4544 cifs_cleanup_volume_info_contents(&fake_vol);
4545 return rc;
4546}
4547
4548static int mount_do_dfs_failover(const char *path,
4549 struct cifs_sb_info *cifs_sb,
4550 struct smb_vol *vol,
4551 struct cifs_ses *root_ses,
4552 unsigned int *xid,
4553 struct TCP_Server_Info **server,
4554 struct cifs_ses **ses,
4555 struct cifs_tcon **tcon)
4556{
4557 int rc;
4558 struct dfs_cache_tgt_list tgt_list;
4559 struct dfs_cache_tgt_iterator *tgt_it = NULL;
4560
4561 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
4562 return -EOPNOTSUPP;
4563
4564 rc = dfs_cache_noreq_find(path, NULL, &tgt_list);
4565 if (rc)
4566 return rc;
4567
4568 for (;;) {
4569 /* Get next DFS target server - if any */
4570 rc = get_next_dfs_tgt(path, &tgt_list, &tgt_it);
4571 if (rc)
4572 break;
4573 /* Connect to next DFS target */
4574 rc = setup_dfs_tgt_conn(path, tgt_it, cifs_sb, vol, xid, server,
4575 ses, tcon);
4576 if (!rc || rc == -EACCES || rc == -EOPNOTSUPP)
4577 break;
4578 }
4579 if (!rc) {
4580 /*
4581 * Update DFS target hint in DFS referral cache with the target
4582 * server we successfully reconnected to.
4583 */
4584 rc = dfs_cache_update_tgthint(*xid, root_ses ? root_ses : *ses,
4585 cifs_sb->local_nls,
4586 cifs_remap(cifs_sb), path,
4587 tgt_it);
4588 }
4589 dfs_cache_free_tgts(&tgt_list);
4590 return rc;
4591}
Steve French2d6d5892009-04-09 00:36:44 +00004592#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004593
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004594int
Jeff Layton04db79b2011-07-06 08:10:38 -04004595cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05004596 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004598 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00004599
Steve Frenchc7c137b2018-06-06 17:59:29 -05004600 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04004601 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
Jeff Layton7586b762008-12-01 18:41:49 -05004603 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004604 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05004605 kfree(volume_info->username);
4606 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05004607 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05004609 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05004611 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00004612 /* In userspace mount helper we can get user name from alternate
4613 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04004614 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
4616
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05004618 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004619 /* load_nls_default cannot return null */
4620 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004622 volume_info->local_nls = load_nls(volume_info->iocharset);
4623 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004624 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004625 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04004626 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 }
4628 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004629
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004630 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04004631}
4632
4633struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05004634cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04004635{
4636 int rc;
4637 struct smb_vol *volume_info;
4638
Jeff Layton6ee95422012-11-26 11:09:57 -05004639 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04004640 if (!volume_info)
4641 return ERR_PTR(-ENOMEM);
4642
Steve Frenchc7c137b2018-06-06 17:59:29 -05004643 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04004644 if (rc) {
4645 cifs_cleanup_volume_info(volume_info);
4646 volume_info = ERR_PTR(rc);
4647 }
4648
4649 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004650}
4651
Aurelien Aptela6b50582016-05-25 19:59:09 +02004652static int
4653cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4654 unsigned int xid,
4655 struct cifs_tcon *tcon,
4656 struct cifs_sb_info *cifs_sb,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004657 char *full_path,
4658 int added_treename)
Aurelien Aptela6b50582016-05-25 19:59:09 +02004659{
4660 int rc;
4661 char *s;
4662 char sep, tmp;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004663 int skip = added_treename ? 1 : 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +02004664
4665 sep = CIFS_DIR_SEP(cifs_sb);
4666 s = full_path;
4667
4668 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4669 while (rc == 0) {
4670 /* skip separators */
4671 while (*s == sep)
4672 s++;
4673 if (!*s)
4674 break;
4675 /* next separator */
4676 while (*s && *s != sep)
4677 s++;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004678 /*
4679 * if the treename is added, we then have to skip the first
4680 * part within the separators
4681 */
4682 if (skip) {
4683 skip = 0;
4684 continue;
4685 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004686 /*
4687 * temporarily null-terminate the path at the end of
4688 * the current component
4689 */
4690 tmp = *s;
4691 *s = 0;
4692 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4693 full_path);
4694 *s = tmp;
4695 }
4696 return rc;
4697}
4698
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004699/*
4700 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
4701 * otherwise 0.
4702 */
4703static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
4704 const unsigned int xid,
4705 struct TCP_Server_Info *server,
4706 struct cifs_tcon *tcon)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004707{
Jeff Layton1daaae82012-03-21 06:30:40 -04004708 int rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004709 char *full_path;
4710
4711 if (!server->ops->is_path_accessible)
4712 return -EOPNOTSUPP;
4713
4714 /*
4715 * cifs_build_path_to_root works only when we have a valid tcon
4716 */
4717 full_path = cifs_build_path_to_root(vol, cifs_sb, tcon,
4718 tcon->Flags & SMB_SHARE_IS_IN_DFS);
4719 if (full_path == NULL)
4720 return -ENOMEM;
4721
4722 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
4723
4724 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4725 full_path);
4726 if (rc != 0 && rc != -EREMOTE) {
4727 kfree(full_path);
4728 return rc;
4729 }
4730
4731 if (rc != -EREMOTE) {
4732 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004733 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004734 if (rc != 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10004735 cifs_server_dbg(VFS, "cannot query dirs between root and final path, "
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004736 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4737 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4738 rc = 0;
4739 }
4740 }
4741
4742 kfree(full_path);
4743 return rc;
4744}
4745
4746#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004747static inline void set_root_tcon(struct cifs_sb_info *cifs_sb,
4748 struct cifs_tcon *tcon,
4749 struct cifs_tcon **root)
4750{
4751 spin_lock(&cifs_tcp_ses_lock);
4752 tcon->tc_count++;
4753 tcon->remap = cifs_remap(cifs_sb);
4754 spin_unlock(&cifs_tcp_ses_lock);
4755 *root = tcon;
4756}
4757
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004758int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4759{
4760 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004761 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004762 struct cifs_ses *ses;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004763 struct cifs_tcon *root_tcon = NULL;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004764 struct cifs_tcon *tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004765 struct TCP_Server_Info *server;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004766 char *root_path = NULL, *full_path = NULL;
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004767 char *old_mountdata, *origin_mountdata = NULL;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004768 int count;
Al Virodd854462011-06-17 08:24:42 -04004769
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004770 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4771 if (!rc && tcon) {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004772 /* If not a standalone DFS root, then check if path is remote */
4773 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls,
4774 cifs_remap(cifs_sb), vol->UNC + 1, NULL,
4775 NULL);
4776 if (rc) {
4777 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4778 if (!rc)
4779 goto out;
4780 if (rc != -EREMOTE)
4781 goto error;
4782 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004783 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004784 /*
4785 * If first DFS target server went offline and we failed to connect it,
4786 * server and ses pointers are NULL at this point, though we still have
4787 * chance to get a cached DFS referral in expand_dfs_referral() and
4788 * retry next target available in it.
4789 *
4790 * If a NULL ses ptr is passed to dfs_cache_find(), a lookup will be
4791 * performed against DFS path and *no* requests will be sent to server
4792 * for any new DFS referrals. Hence it's safe to skip checking whether
4793 * server or ses ptr is NULL.
4794 */
4795 if (rc == -EACCES || rc == -EOPNOTSUPP)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004796 goto error;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004797
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004798 root_path = build_unc_path_to_root(vol, cifs_sb, false);
4799 if (IS_ERR(root_path)) {
4800 rc = PTR_ERR(root_path);
4801 root_path = NULL;
4802 goto error;
4803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004805 full_path = build_unc_path_to_root(vol, cifs_sb, true);
4806 if (IS_ERR(full_path)) {
4807 rc = PTR_ERR(full_path);
4808 full_path = NULL;
4809 goto error;
4810 }
Sean Finneyc1508ca2011-04-11 13:19:31 +00004811 /*
4812 * Perform an unconditional check for whether there are DFS
4813 * referrals for this path without prefix, to provide support
4814 * for DFS referrals from w2k8 servers which don't seem to respond
4815 * with PATH_NOT_COVERED to requests that include the prefix.
4816 * Chase the referral if found, otherwise continue normally.
4817 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004818 old_mountdata = cifs_sb->mountdata;
4819 (void)expand_dfs_referral(xid, ses, vol, cifs_sb, false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004820
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004821 if (cifs_sb->mountdata == NULL) {
4822 rc = -ENOENT;
4823 goto error;
4824 }
4825
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004826 /* Save DFS root volume information for DFS refresh worker */
4827 origin_mountdata = kstrndup(cifs_sb->mountdata,
4828 strlen(cifs_sb->mountdata), GFP_KERNEL);
4829 if (!origin_mountdata) {
4830 rc = -ENOMEM;
4831 goto error;
4832 }
4833
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004834 if (cifs_sb->mountdata != old_mountdata) {
4835 /* If we were redirected, reconnect to new target server */
4836 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4837 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4838 }
4839 if (rc) {
4840 if (rc == -EACCES || rc == -EOPNOTSUPP)
4841 goto error;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004842 /* Perform DFS failover to any other DFS targets */
4843 rc = mount_do_dfs_failover(root_path + 1, cifs_sb, vol, NULL,
4844 &xid, &server, &ses, &tcon);
4845 if (rc)
4846 goto error;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004847 }
4848
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004849 kfree(root_path);
4850 root_path = build_unc_path_to_root(vol, cifs_sb, false);
4851 if (IS_ERR(root_path)) {
4852 rc = PTR_ERR(root_path);
4853 root_path = NULL;
4854 goto error;
4855 }
4856 /* Cache out resolved root server */
4857 (void)dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
4858 root_path + 1, NULL, NULL);
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004859 kfree(root_path);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004860 root_path = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004861
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004862 set_root_tcon(cifs_sb, tcon, &root_tcon);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004863
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004864 for (count = 1; ;) {
4865 if (!rc && tcon) {
4866 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4867 if (!rc || rc != -EREMOTE)
4868 break;
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004869 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004870 /*
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004871 * BB: when we implement proper loop detection,
4872 * we will remove this check. But now we need it
4873 * to prevent an indefinite loop if 'DFS tree' is
4874 * misconfigured (i.e. has loops).
Steve French6d3ea7e2012-11-28 22:34:41 -06004875 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004876 if (count++ > MAX_NESTED_LINKS) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004877 rc = -ELOOP;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004878 break;
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004879 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004880
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004881 kfree(full_path);
4882 full_path = build_unc_path_to_root(vol, cifs_sb, true);
4883 if (IS_ERR(full_path)) {
4884 rc = PTR_ERR(full_path);
4885 full_path = NULL;
4886 break;
4887 }
4888
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004889 old_mountdata = cifs_sb->mountdata;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004890 rc = expand_dfs_referral(xid, root_tcon->ses, vol, cifs_sb,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004891 true);
4892 if (rc)
4893 break;
Jeff Layton7b91e262009-07-23 15:22:30 -04004894
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004895 if (cifs_sb->mountdata != old_mountdata) {
4896 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4897 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses,
4898 &tcon);
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004899 /*
4900 * Ensure that DFS referrals go through new root server.
4901 */
4902 if (!rc && tcon &&
4903 (tcon->share_flags & (SHI1005_FLAGS_DFS |
4904 SHI1005_FLAGS_DFS_ROOT))) {
4905 cifs_put_tcon(root_tcon);
4906 set_root_tcon(cifs_sb, tcon, &root_tcon);
4907 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004908 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004909 if (rc) {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004910 if (rc == -EACCES || rc == -EOPNOTSUPP)
4911 break;
4912 /* Perform DFS failover to any other DFS targets */
4913 rc = mount_do_dfs_failover(full_path + 1, cifs_sb, vol,
4914 root_tcon->ses, &xid,
4915 &server, &ses, &tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004916 if (rc == -EACCES || rc == -EOPNOTSUPP || !server ||
4917 !ses)
4918 goto error;
4919 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004920 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004921 cifs_put_tcon(root_tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004922
Jeff Layton9d002df2010-10-06 19:51:11 -04004923 if (rc)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004924 goto error;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004925
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004926 spin_lock(&cifs_tcp_ses_lock);
4927 if (!tcon->dfs_path) {
4928 /* Save full path in new tcon to do failover when reconnecting tcons */
4929 tcon->dfs_path = full_path;
4930 full_path = NULL;
4931 tcon->remap = cifs_remap(cifs_sb);
4932 }
4933 cifs_sb->origin_fullpath = kstrndup(tcon->dfs_path,
YueHaibing2f0a6172018-12-18 01:34:39 +00004934 strlen(tcon->dfs_path),
4935 GFP_ATOMIC);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004936 if (!cifs_sb->origin_fullpath) {
4937 spin_unlock(&cifs_tcp_ses_lock);
4938 rc = -ENOMEM;
4939 goto error;
4940 }
4941 spin_unlock(&cifs_tcp_ses_lock);
4942
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004943 rc = dfs_cache_add_vol(origin_mountdata, vol, cifs_sb->origin_fullpath);
Paulo Alcantarae511d312018-11-14 17:16:44 -02004944 if (rc) {
4945 kfree(cifs_sb->origin_fullpath);
4946 goto error;
4947 }
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01004948 /*
4949 * After reconnecting to a different server, unique ids won't
4950 * match anymore, so we disable serverino. This prevents
4951 * dentry revalidation to think the dentry are stale (ESTALE).
4952 */
4953 cifs_autodisable_serverino(cifs_sb);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03004954 /*
4955 * Force the use of prefix path to support failover on DFS paths that
4956 * resolve to targets that have different prefix paths.
4957 */
4958 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4959 kfree(cifs_sb->prepath);
4960 cifs_sb->prepath = vol->prepath;
4961 vol->prepath = NULL;
4962
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004963out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004964 free_xid(xid);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02004965 cifs_try_adding_channels(ses);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004966 return mount_setup_tlink(cifs_sb, ses, tcon);
4967
4968error:
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004969 kfree(full_path);
4970 kfree(root_path);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004971 kfree(origin_mountdata);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004972 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 return rc;
4974}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004975#else
4976int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4977{
4978 int rc = 0;
4979 unsigned int xid;
4980 struct cifs_ses *ses;
4981 struct cifs_tcon *tcon;
4982 struct TCP_Server_Info *server;
4983
4984 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4985 if (rc)
4986 goto error;
4987
4988 if (tcon) {
4989 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4990 if (rc == -EREMOTE)
4991 rc = -EOPNOTSUPP;
4992 if (rc)
4993 goto error;
4994 }
4995
4996 free_xid(xid);
4997
4998 return mount_setup_tlink(cifs_sb, ses, tcon);
4999
5000error:
5001 mount_put_conns(cifs_sb, xid, server, ses, tcon);
5002 return rc;
5003}
5004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Jeff Layton8d1bca32011-06-11 21:17:10 -04005006/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01005007 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04005008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04005010CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00005011 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 const struct nls_table *nls_codepage)
5013{
5014 struct smb_hdr *smb_buffer;
5015 struct smb_hdr *smb_buffer_response;
5016 TCONX_REQ *pSMB;
5017 TCONX_RSP *pSMBr;
5018 unsigned char *bcc_ptr;
5019 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05005020 int length;
5021 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
5023 if (ses == NULL)
5024 return -EIO;
5025
5026 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00005027 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00005029
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 smb_buffer_response = smb_buffer;
5031
5032 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
5033 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07005034
Pavel Shilovsky88257362012-05-23 14:01:59 +04005035 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 smb_buffer->Uid = ses->Suid;
5037 pSMB = (TCONX_REQ *) smb_buffer;
5038 pSMBr = (TCONX_RSP *) smb_buffer_response;
5039
5040 pSMB->AndXCommand = 0xFF;
5041 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01005043 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08005044 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00005045 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08005046 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00005047 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08005048 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06005049 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08005050 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
5051 specified as required (when that support is added to
5052 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00005053 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08005054 by Samba (not sure whether other servers allow
5055 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00005056#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04005057 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05005058 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05005059 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00005060 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05005061 SECMODE_PW_ENCRYPT ? true : false,
5062 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00005063 else
5064#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06005065 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05005066 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05005067 if (rc) {
5068 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
5069 __func__, rc);
5070 cifs_buf_release(smb_buffer);
5071 return rc;
5072 }
Steve Frencheeac8042006-01-13 21:34:58 -08005073
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06005074 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00005075 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00005076 /* must align unicode strings */
5077 *bcc_ptr = 0; /* null byte password */
5078 bcc_ptr++;
5079 }
Steve Frencheeac8042006-01-13 21:34:58 -08005080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
Jeff Layton38d77c52013-05-26 07:01:00 -04005082 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
5084
5085 if (ses->capabilities & CAP_STATUS32) {
5086 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
5087 }
5088 if (ses->capabilities & CAP_DFS) {
5089 smb_buffer->Flags2 |= SMBFLG2_DFS;
5090 }
5091 if (ses->capabilities & CAP_UNICODE) {
5092 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
5093 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06005094 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00005095 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00005096 (/* server len*/ + 256 /* share len */), nls_codepage);
5097 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 bcc_ptr += 2; /* skip trailing null */
5099 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 strcpy(bcc_ptr, tree);
5101 bcc_ptr += strlen(tree) + 1;
5102 }
5103 strcpy(bcc_ptr, "?????");
5104 bcc_ptr += strlen("?????");
5105 bcc_ptr += 1;
5106 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00005107 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
5108 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 pSMB->ByteCount = cpu_to_le16(count);
5110
Steve French133672e2007-11-13 22:41:37 +00005111 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05005112 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01005115 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00005116 bool is_unicode;
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00005119 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 tcon->tid = smb_buffer_response->Tid;
5121 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05005122 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04005123 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00005124 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
5125 is_unicode = true;
5126 else
5127 is_unicode = false;
5128
Jeff Laytoncc20c032009-04-30 07:16:21 -04005129
Steve French50c2f752007-07-13 00:33:32 +00005130 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00005131 if (length == 3) {
5132 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
5133 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005134 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01005135 tcon->ipc = true;
5136 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00005137 }
5138 } else if (length == 2) {
5139 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
5140 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05005141 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00005142 }
5143 }
Steve French50c2f752007-07-13 00:33:32 +00005144 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04005145 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05005146 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04005147
5148 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04005149 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06005150 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00005151 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04005152 nls_codepage);
5153
Joe Perchesf96637b2013-05-04 22:12:25 -05005154 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04005155
Steve Frenchfb8c4b12007-07-10 01:16:18 +00005156 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00005157 (smb_buffer_response->WordCount == 7))
5158 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00005159 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
5160 else
5161 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05005162 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 }
5164
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00005165 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 return rc;
5167}
5168
Al Viro2e32cf52013-10-03 12:53:37 -04005169static void delayed_free(struct rcu_head *p)
5170{
5171 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
5172 unload_nls(sbi->local_nls);
5173 kfree(sbi);
5174}
5175
Al Viro2a9b9952011-06-17 09:27:16 -04005176void
5177cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178{
Jeff Laytonb647c352010-10-28 11:16:44 -04005179 struct rb_root *root = &cifs_sb->tlink_tree;
5180 struct rb_node *node;
5181 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
Jeff Layton2de970f2010-10-06 19:51:12 -04005183 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
5184
Jeff Laytonb647c352010-10-28 11:16:44 -04005185 spin_lock(&cifs_sb->tlink_tree_lock);
5186 while ((node = rb_first(root))) {
5187 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
5188 cifs_get_tlink(tlink);
5189 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
5190 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00005191
Jeff Laytonb647c352010-10-28 11:16:44 -04005192 spin_unlock(&cifs_sb->tlink_tree_lock);
5193 cifs_put_tlink(tlink);
5194 spin_lock(&cifs_sb->tlink_tree_lock);
5195 }
5196 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005197
Al Virod757d712011-06-17 09:42:43 -04005198 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02005199 kfree(cifs_sb->prepath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02005200#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarae511d312018-11-14 17:16:44 -02005201 dfs_cache_del_vol(cifs_sb->origin_fullpath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02005202 kfree(cifs_sb->origin_fullpath);
5203#endif
Al Viro2e32cf52013-10-03 12:53:37 -04005204 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00005205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005207int
5208cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
5210 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02005211 struct TCP_Server_Info *server = cifs_ses_server(ses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005213 if (!server->ops->need_neg || !server->ops->negotiate)
5214 return -ENOSYS;
5215
Jeff Layton198b5682010-04-24 07:57:48 -04005216 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005217 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04005218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005220 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04005221 if (rc == 0) {
5222 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04005223 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04005224 server->tcpStatus = CifsGood;
5225 else
5226 rc = -EHOSTDOWN;
5227 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 }
Steve French26b994f2008-08-06 05:11:33 +00005229
Jeff Layton198b5682010-04-24 07:57:48 -04005230 return rc;
5231}
Steve French26b994f2008-08-06 05:11:33 +00005232
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005233int
5234cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
5235 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04005236{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005237 int rc = -ENOSYS;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02005238 struct TCP_Server_Info *server = cifs_ses_server(ses);
Jeff Layton198b5682010-04-24 07:57:48 -04005239
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02005240 if (!ses->binding) {
5241 ses->capabilities = server->capabilities;
5242 if (linuxExtEnabled == 0)
5243 ses->capabilities &= (~server->vals->cap_unix);
5244
5245 if (ses->auth_key.response) {
5246 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
5247 ses->auth_key.response);
5248 kfree(ses->auth_key.response);
5249 ses->auth_key.response = NULL;
5250 ses->auth_key.len = 0;
5251 }
5252 }
Steve French20418ac2009-04-30 16:13:32 +00005253
Joe Perchesf96637b2013-05-04 22:12:25 -05005254 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00005255 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04005256
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005257 if (server->ops->sess_setup)
5258 rc = server->ops->sess_setup(xid, ses, nls_info);
5259
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05005260 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10005261 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05005262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 return rc;
5264}
5265
Jeff Layton8a8798a2012-01-17 16:09:15 -05005266static int
5267cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
5268{
Jeff Layton3f618222013-06-12 19:52:14 -05005269 vol->sectype = ses->sectype;
5270
5271 /* krb5 is special, since we don't need username or pw */
5272 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05005273 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05005274
5275 return cifs_set_cifscreds(vol, ses);
5276}
5277
Steve French96daf2b2011-05-27 04:34:02 +00005278static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005279cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04005280{
Jeff Layton8a8798a2012-01-17 16:09:15 -05005281 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00005282 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
5283 struct cifs_ses *ses;
5284 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04005285 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04005286
5287 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03005288 if (vol_info == NULL)
5289 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04005290
Jeff Layton9d002df2010-10-06 19:51:11 -04005291 vol_info->local_nls = cifs_sb->local_nls;
5292 vol_info->linux_uid = fsuid;
5293 vol_info->cred_uid = fsuid;
5294 vol_info->UNC = master_tcon->treeName;
5295 vol_info->retry = master_tcon->retry;
5296 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05005297 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04005298 vol_info->local_lease = master_tcon->local_lease;
5299 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04005300 vol_info->sectype = master_tcon->ses->sectype;
5301 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04005302
Jeff Layton8a8798a2012-01-17 16:09:15 -05005303 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
5304 if (rc) {
5305 tcon = ERR_PTR(rc);
5306 goto out;
5307 }
Jeff Layton9d002df2010-10-06 19:51:11 -04005308
5309 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05305310 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005311 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05305312 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005313
5314 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
5315 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00005316 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07005317 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04005318 goto out;
5319 }
5320
5321 tcon = cifs_get_tcon(ses, vol_info);
5322 if (IS_ERR(tcon)) {
5323 cifs_put_smb_ses(ses);
5324 goto out;
5325 }
5326
Steve Frenchce558b02018-05-31 19:16:54 -05005327 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
5328 if (tcon->posix_extensions)
5329 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05005330
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04005331 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04005332 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05005333
Jeff Layton9d002df2010-10-06 19:51:11 -04005334out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05005335 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01005336 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04005337 kfree(vol_info);
5338
5339 return tcon;
5340}
5341
Steve French96daf2b2011-05-27 04:34:02 +00005342struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04005343cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
5344{
5345 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
5346}
5347
Jeff Laytonb647c352010-10-28 11:16:44 -04005348/* find and return a tlink with given uid */
5349static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005350tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04005351{
5352 struct rb_node *node = root->rb_node;
5353 struct tcon_link *tlink;
5354
5355 while (node) {
5356 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
5357
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005358 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005359 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005360 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005361 node = node->rb_right;
5362 else
5363 return tlink;
5364 }
5365 return NULL;
5366}
5367
5368/* insert a tcon_link into the tree */
5369static void
5370tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
5371{
5372 struct rb_node **new = &(root->rb_node), *parent = NULL;
5373 struct tcon_link *tlink;
5374
5375 while (*new) {
5376 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
5377 parent = *new;
5378
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005379 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005380 new = &((*new)->rb_left);
5381 else
5382 new = &((*new)->rb_right);
5383 }
5384
5385 rb_link_node(&new_tlink->tl_rbnode, parent, new);
5386 rb_insert_color(&new_tlink->tl_rbnode, root);
5387}
5388
Jeff Layton9d002df2010-10-06 19:51:11 -04005389/*
5390 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
5391 * current task.
5392 *
5393 * If the superblock doesn't refer to a multiuser mount, then just return
5394 * the master tcon for the mount.
5395 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05305396 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04005397 * exists, then check to see if it's pending construction. If it is then wait
5398 * for construction to complete. Once it's no longer pending, check to see if
5399 * it failed and either return an error or retry construction, depending on
5400 * the timeout.
5401 *
5402 * If one doesn't exist then insert a new tcon_link struct into the tree and
5403 * try to construct a new one.
5404 */
5405struct tcon_link *
5406cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
5407{
5408 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005409 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04005410 struct tcon_link *tlink, *newtlink;
5411
5412 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
5413 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
5414
5415 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04005416 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04005417 if (tlink)
5418 cifs_get_tlink(tlink);
5419 spin_unlock(&cifs_sb->tlink_tree_lock);
5420
5421 if (tlink == NULL) {
5422 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
5423 if (newtlink == NULL)
5424 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04005425 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04005426 newtlink->tl_tcon = ERR_PTR(-EACCES);
5427 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
5428 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
5429 cifs_get_tlink(newtlink);
5430
Jeff Layton9d002df2010-10-06 19:51:11 -04005431 spin_lock(&cifs_sb->tlink_tree_lock);
5432 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04005433 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04005434 if (tlink) {
5435 cifs_get_tlink(tlink);
5436 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005437 kfree(newtlink);
5438 goto wait_for_construction;
5439 }
Jeff Layton9d002df2010-10-06 19:51:11 -04005440 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04005441 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
5442 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005443 } else {
5444wait_for_construction:
5445 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04005446 TASK_INTERRUPTIBLE);
5447 if (ret) {
5448 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10005449 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04005450 }
5451
5452 /* if it's good, return it */
5453 if (!IS_ERR(tlink->tl_tcon))
5454 return tlink;
5455
5456 /* return error if we tried this already recently */
5457 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
5458 cifs_put_tlink(tlink);
5459 return ERR_PTR(-EACCES);
5460 }
5461
5462 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
5463 goto wait_for_construction;
5464 }
5465
5466 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
5467 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
5468 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
5469
5470 if (IS_ERR(tlink->tl_tcon)) {
5471 cifs_put_tlink(tlink);
5472 return ERR_PTR(-EACCES);
5473 }
5474
5475 return tlink;
5476}
Jeff Layton2de970f2010-10-06 19:51:12 -04005477
5478/*
5479 * periodic workqueue job that scans tcon_tree for a superblock and closes
5480 * out tcons.
5481 */
5482static void
5483cifs_prune_tlinks(struct work_struct *work)
5484{
5485 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
5486 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04005487 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00005488 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04005489 struct rb_node *tmp;
5490 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04005491
Jeff Laytonb647c352010-10-28 11:16:44 -04005492 /*
5493 * Because we drop the spinlock in the loop in order to put the tlink
5494 * it's not guarded against removal of links from the tree. The only
5495 * places that remove entries from the tree are this function and
5496 * umounts. Because this function is non-reentrant and is canceled
5497 * before umount can proceed, this is safe.
5498 */
5499 spin_lock(&cifs_sb->tlink_tree_lock);
5500 node = rb_first(root);
5501 while (node != NULL) {
5502 tmp = node;
5503 node = rb_next(tmp);
5504 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
5505
5506 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
5507 atomic_read(&tlink->tl_count) != 0 ||
5508 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
5509 continue;
5510
5511 cifs_get_tlink(tlink);
5512 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
5513 rb_erase(tmp, root);
5514
Jeff Layton2de970f2010-10-06 19:51:12 -04005515 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04005516 cifs_put_tlink(tlink);
5517 spin_lock(&cifs_sb->tlink_tree_lock);
5518 }
5519 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04005520
Jeff Laytonda472fc2012-03-23 14:40:53 -04005521 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04005522 TLINK_IDLE_EXPIRE);
5523}