blob: 751bdde6515d5ac853d83d774ac80172bc14a159 [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>
24#include <linux/list.h>
25#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pagemap.h>
28#include <linux/ctype.h>
29#include <linux/utsname.h>
30#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070031#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070032#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000033#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070034#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040036#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040039#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040040#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050041#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000042#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040043#include <linux/parser.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "cifspdu.h"
46#include "cifsglob.h"
47#include "cifsproto.h"
48#include "cifs_unicode.h"
49#include "cifs_debug.h"
50#include "cifs_fs_sb.h"
Paulo Alcantarafb713a12018-11-20 15:16:36 -020051#include "dns_resolve.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "ntlmssp.h"
53#include "nterr.h"
54#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053055#include "fscache.h"
Pavel Shilovsky48f95262016-11-04 11:50:31 -070056#ifdef CONFIG_CIFS_SMB2
57#include "smb2proto.h"
58#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#define CIFS_PORT 445
61#define RFC1001_PORT 139
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063extern mempool_t *cifs_req_poolp;
64
Jeff Layton2de970f2010-10-06 19:51:12 -040065/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040066#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040067#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040068
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040069enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040070 /* Mount options that take no arguments */
71 Opt_user_xattr, Opt_nouser_xattr,
72 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040073 Opt_forcegid, Opt_noforcegid,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040074 Opt_noblocksend, Opt_noautotune,
75 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050076 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040077 Opt_mapchars, Opt_nomapchars, Opt_sfu,
78 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
79 Opt_noposixpaths, Opt_nounix,
80 Opt_nocase,
81 Opt_brl, Opt_nobrl,
Steve French95932652016-09-23 01:36:34 -050082 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040083 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
84 Opt_nohard, Opt_nosoft,
85 Opt_nointr, Opt_intr,
86 Opt_nostrictsync, Opt_strictsync,
87 Opt_serverino, Opt_noserverino,
88 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
89 Opt_acl, Opt_noacl, Opt_locallease,
Jeff Layton1b359202012-09-19 15:20:27 -070090 Opt_sign, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040091 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040092 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050093 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060094 Opt_resilient, Opt_noresilient,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040095
96 /* Mount options which take numeric value */
97 Opt_backupuid, Opt_backupgid, Opt_uid,
98 Opt_cruid, Opt_gid, Opt_file_mode,
99 Opt_dirmode, Opt_port,
100 Opt_rsize, Opt_wsize, Opt_actimeo,
Steve French141891f2016-09-23 00:44:16 -0500101 Opt_echo_interval, Opt_max_credits,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400102
103 /* Mount options which take string value */
104 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400105 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400106 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400107 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400108
109 /* Mount options to be ignored */
110 Opt_ignore,
111
112 /* Options which could be blank */
113 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100114 Opt_blank_user,
115 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400116
117 Opt_err
118};
119
120static const match_table_t cifs_mount_option_tokens = {
121
122 { Opt_user_xattr, "user_xattr" },
123 { Opt_nouser_xattr, "nouser_xattr" },
124 { Opt_forceuid, "forceuid" },
125 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400126 { Opt_forcegid, "forcegid" },
127 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400128 { Opt_noblocksend, "noblocksend" },
129 { Opt_noautotune, "noautotune" },
130 { Opt_hard, "hard" },
131 { Opt_soft, "soft" },
132 { Opt_perm, "perm" },
133 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500134 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400135 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500136 { Opt_mapposix, "mapposix" }, /* SFM style */
137 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400138 { Opt_sfu, "sfu" },
139 { Opt_nosfu, "nosfu" },
140 { Opt_nodfs, "nodfs" },
141 { Opt_posixpaths, "posixpaths" },
142 { Opt_noposixpaths, "noposixpaths" },
143 { Opt_nounix, "nounix" },
144 { Opt_nounix, "nolinux" },
145 { Opt_nocase, "nocase" },
146 { Opt_nocase, "ignorecase" },
147 { Opt_brl, "brl" },
148 { Opt_nobrl, "nobrl" },
149 { Opt_nobrl, "nolock" },
150 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400151 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400152 { Opt_setuids, "setuids" },
153 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500154 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400155 { Opt_dynperm, "dynperm" },
156 { Opt_nodynperm, "nodynperm" },
157 { Opt_nohard, "nohard" },
158 { Opt_nosoft, "nosoft" },
159 { Opt_nointr, "nointr" },
160 { Opt_intr, "intr" },
161 { Opt_nostrictsync, "nostrictsync" },
162 { Opt_strictsync, "strictsync" },
163 { Opt_serverino, "serverino" },
164 { Opt_noserverino, "noserverino" },
165 { Opt_rwpidforward, "rwpidforward" },
166 { Opt_cifsacl, "cifsacl" },
167 { Opt_nocifsacl, "nocifsacl" },
168 { Opt_acl, "acl" },
169 { Opt_noacl, "noacl" },
170 { Opt_locallease, "locallease" },
171 { Opt_sign, "sign" },
172 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400173 { Opt_noac, "noac" },
174 { Opt_fsc, "fsc" },
175 { Opt_mfsymlinks, "mfsymlinks" },
176 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400177 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400178 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500179 { Opt_persistent, "persistenthandles"},
180 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600181 { Opt_resilient, "resilienthandles"},
182 { Opt_noresilient, "noresilienthandles"},
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400183
184 { Opt_backupuid, "backupuid=%s" },
185 { Opt_backupgid, "backupgid=%s" },
186 { Opt_uid, "uid=%s" },
187 { Opt_cruid, "cruid=%s" },
188 { Opt_gid, "gid=%s" },
189 { Opt_file_mode, "file_mode=%s" },
190 { Opt_dirmode, "dirmode=%s" },
191 { Opt_dirmode, "dir_mode=%s" },
192 { Opt_port, "port=%s" },
193 { Opt_rsize, "rsize=%s" },
194 { Opt_wsize, "wsize=%s" },
195 { Opt_actimeo, "actimeo=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600196 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500197 { Opt_max_credits, "max_credits=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400198
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100199 { Opt_blank_user, "user=" },
200 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400201 { Opt_user, "user=%s" },
202 { Opt_user, "username=%s" },
203 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100204 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400205 { Opt_pass, "pass=%s" },
206 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100207 { Opt_blank_ip, "ip=" },
208 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400209 { Opt_ip, "ip=%s" },
210 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400211 { Opt_ignore, "unc=%s" },
212 { Opt_ignore, "target=%s" },
213 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400214 { Opt_domain, "dom=%s" },
215 { Opt_domain, "domain=%s" },
216 { Opt_domain, "workgroup=%s" },
217 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400218 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400219 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400220 { Opt_netbiosname, "netbiosname=%s" },
221 { Opt_servern, "servern=%s" },
222 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400223 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400224 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400225 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400226
227 { Opt_ignore, "cred" },
228 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400229 { Opt_ignore, "cred=%s" },
230 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400231 { Opt_ignore, "guest" },
232 { Opt_ignore, "rw" },
233 { Opt_ignore, "ro" },
234 { Opt_ignore, "suid" },
235 { Opt_ignore, "nosuid" },
236 { Opt_ignore, "exec" },
237 { Opt_ignore, "noexec" },
238 { Opt_ignore, "nodev" },
239 { Opt_ignore, "noauto" },
240 { Opt_ignore, "dev" },
241 { Opt_ignore, "mand" },
242 { Opt_ignore, "nomand" },
243 { Opt_ignore, "_netdev" },
244
245 { Opt_err, NULL }
246};
247
248enum {
249 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
250 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400251 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
252 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400253 Opt_sec_none,
254
255 Opt_sec_err
256};
257
258static const match_table_t cifs_secflavor_tokens = {
259 { Opt_sec_krb5, "krb5" },
260 { Opt_sec_krb5i, "krb5i" },
261 { Opt_sec_krb5p, "krb5p" },
262 { Opt_sec_ntlmsspi, "ntlmsspi" },
263 { Opt_sec_ntlmssp, "ntlmssp" },
264 { Opt_ntlm, "ntlm" },
265 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400266 { Opt_sec_ntlmv2, "nontlm" },
267 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400268 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400269 { Opt_sec_lanman, "lanman" },
270 { Opt_sec_none, "none" },
271
272 { Opt_sec_err, NULL }
273};
274
Jeff Layton15b6a472012-05-16 07:50:15 -0400275/* cache flavors */
276enum {
277 Opt_cache_loose,
278 Opt_cache_strict,
279 Opt_cache_none,
280 Opt_cache_err
281};
282
283static const match_table_t cifs_cacheflavor_tokens = {
284 { Opt_cache_loose, "loose" },
285 { Opt_cache_strict, "strict" },
286 { Opt_cache_none, "none" },
287 { Opt_cache_err, NULL }
288};
289
Jeff Layton23db65f2012-05-15 12:20:51 -0400290static const match_table_t cifs_smb_version_tokens = {
291 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600292 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000293 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500294 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500295 { Smb_302, SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600296#ifdef CONFIG_CIFS_SMB311
297 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500298 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600299#endif /* SMB311 */
300 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400301};
302
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300303static int ip_connect(struct TCP_Server_Info *server);
304static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400305static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400306static void cifs_prune_tlinks(struct work_struct *work);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -0400307static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
308 const char *devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Jeff Laytond5c56052008-12-01 18:42:33 -0500310/*
Paulo Alcantarafb713a12018-11-20 15:16:36 -0200311 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
312 * get their ip addresses changed at some point.
313 *
314 * This should be called with server->srv_mutex held.
315 */
316#ifdef CONFIG_CIFS_DFS_UPCALL
317static int reconn_set_ipaddr(struct TCP_Server_Info *server)
318{
319 int rc;
320 int len;
321 char *unc, *ipaddr = NULL;
322
323 if (!server->hostname)
324 return -EINVAL;
325
326 len = strlen(server->hostname) + 3;
327
328 unc = kmalloc(len, GFP_KERNEL);
329 if (!unc) {
330 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
331 return -ENOMEM;
332 }
333 snprintf(unc, len, "\\\\%s", server->hostname);
334
335 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
336 kfree(unc);
337
338 if (rc < 0) {
339 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
340 __func__, server->hostname, rc);
341 return rc;
342 }
343
344 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
345 strlen(ipaddr));
346 kfree(ipaddr);
347
348 return !rc ? -1 : 0;
349}
350#else
351static inline int reconn_set_ipaddr(struct TCP_Server_Info *server)
352{
353 return 0;
354}
355#endif
356
357/*
Jeff Laytond5c56052008-12-01 18:42:33 -0500358 * cifs tcp session reconnection
359 *
360 * mark tcp session as reconnecting so temporarily locked
361 * mark all smb sessions as reconnecting for tcp session
362 * reconnect tcp session
363 * wake up waiters on reconnection? - (not needed currently)
364 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400365int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366cifs_reconnect(struct TCP_Server_Info *server)
367{
368 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500369 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000370 struct cifs_ses *ses;
371 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000372 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400373 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000376 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000377 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 next time through the loop */
379 spin_unlock(&GlobalMid_Lock);
380 return rc;
381 } else
382 server->tcpStatus = CifsNeedReconnect;
383 spin_unlock(&GlobalMid_Lock);
384 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400385#ifdef CONFIG_CIFS_SMB2
386 server->max_read = 0;
387#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Joe Perchesf96637b2013-05-04 22:12:25 -0500389 cifs_dbg(FYI, "Reconnecting tcp session\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /* before reconnecting the tcp session, mark the smb session (uid)
392 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500393 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
394 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530395 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500396 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000397 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500398 ses->need_reconnect = true;
399 ses->ipc_tid = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500400 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000401 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500402 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530405 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500408 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500409 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000410 if (server->ssocket) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500411 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
412 server->ssocket->state, server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800413 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesf96637b2013-05-04 22:12:25 -0500414 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
415 server->ssocket->state, server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 sock_release(server->ssocket);
417 server->ssocket = NULL;
418 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500419 server->sequence_number = 0;
420 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500421 kfree(server->session_key.response);
422 server->session_key.response = NULL;
423 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000424 server->lstrp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500426 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400427 INIT_LIST_HEAD(&retry_list);
Joe Perchesf96637b2013-05-04 22:12:25 -0500428 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500430 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
431 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400432 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
433 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400434 list_move(&mid_entry->qhead, &retry_list);
435 }
436 spin_unlock(&GlobalMid_Lock);
Rabin Vincent820962d2015-12-23 07:32:41 +0100437 mutex_unlock(&server->srv_mutex);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400438
Joe Perchesf96637b2013-05-04 22:12:25 -0500439 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400440 list_for_each_safe(tmp, tmp2, &retry_list) {
441 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500442 list_del_init(&mid_entry->qhead);
443 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400446 do {
Steve French6c3d8902006-07-31 22:46:20 +0000447 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300448
449 /* we should try only the port we connected to before */
Jeff Layton73e216a2013-09-05 08:38:10 -0400450 mutex_lock(&server->srv_mutex);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300451 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000452 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500453 cifs_dbg(FYI, "reconnect error %d\n", rc);
Paulo Alcantarafb713a12018-11-20 15:16:36 -0200454 rc = reconn_set_ipaddr(server);
455 if (rc) {
456 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
457 __func__, rc);
458 }
Federico Sauter4afe2602015-03-17 17:45:28 +0100459 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700460 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 } else {
462 atomic_inc(&tcpSesReconnectCount);
463 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000464 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000465 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000466 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100467 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400469 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500470
Sachin Prabhub8c60012016-10-20 19:52:24 -0400471 if (server->tcpStatus == CifsNeedNegotiate)
472 mod_delayed_work(cifsiod_wq, &server->echo, 0);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return rc;
475}
476
Jeff Laytonc74093b2011-01-11 07:24:23 -0500477static void
478cifs_echo_request(struct work_struct *work)
479{
480 int rc;
481 struct TCP_Server_Info *server = container_of(work,
482 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400483 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500484
Jeff Layton247ec9b2011-02-04 17:09:50 -0500485 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400486 * If we need to renegotiate, set echo interval to zero to
487 * immediately call echo service where we can renegotiate.
488 */
489 if (server->tcpStatus == CifsNeedNegotiate)
490 echo_interval = 0;
491 else
492 echo_interval = server->echo_interval;
493
494 /*
495 * We cannot send an echo if it is disabled.
496 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500497 */
Steve French4fcd1812016-06-22 20:12:05 -0500498
499 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400500 server->tcpStatus == CifsExiting ||
501 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400502 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600503 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500504 goto requeue_echo;
505
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400506 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500507 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500508 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
509 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500510
511requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400512 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500513}
514
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400515static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400516allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400517{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400518 if (!server->bigbuf) {
519 server->bigbuf = (char *)cifs_buf_get();
520 if (!server->bigbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500521 cifs_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400522 msleep(3000);
523 /* retry will check if exiting */
524 return false;
525 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400526 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400527 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400528 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400529 }
530
Jeff Layton2a37ef92011-10-19 15:29:23 -0400531 if (!server->smallbuf) {
532 server->smallbuf = (char *)cifs_small_buf_get();
533 if (!server->smallbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500534 cifs_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400535 msleep(1000);
536 /* retry will check if exiting */
537 return false;
538 }
539 /* beginning of smb buffer is cleared in our buf_get */
540 } else {
541 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400542 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400543 }
544
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400545 return true;
546}
547
Jeff Laytonba749e62011-10-11 06:41:32 -0400548static bool
549server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400550{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300551 /*
552 * We need to wait 2 echo intervals to make sure we handle such
553 * situations right:
554 * 1s client sends a normal SMB request
555 * 2s client gets a response
556 * 30s echo workqueue job pops, and decides we got a response recently
557 * and don't need to send another
558 * ...
559 * 65s kernel_recvmsg times out, and we see that we haven't gotten
560 * a response in >60s.
561 */
562 if (server->tcpStatus == CifsGood &&
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600563 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
564 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
565 server->hostname, (2 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400566 cifs_reconnect(server);
567 wake_up(&server->response_q);
568 return true;
569 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400570
Jeff Laytonba749e62011-10-11 06:41:32 -0400571 return false;
572}
573
Al Viro71335662016-01-09 19:54:50 -0500574static int
575cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400576{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400577 int length = 0;
578 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400579
Al Viro71335662016-01-09 19:54:50 -0500580 smb_msg->msg_control = NULL;
581 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400582
Al Viro71335662016-01-09 19:54:50 -0500583 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500584 try_to_freeze();
585
Al Viro71335662016-01-09 19:54:50 -0500586 if (server_unresponsive(server))
587 return -ECONNABORTED;
588
589 length = sock_recvmsg(server->ssocket, smb_msg, 0);
590
591 if (server->tcpStatus == CifsExiting)
592 return -ESHUTDOWN;
593
594 if (server->tcpStatus == CifsNeedReconnect) {
595 cifs_reconnect(server);
596 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400597 }
598
Al Viro71335662016-01-09 19:54:50 -0500599 if (length == -ERESTARTSYS ||
600 length == -EAGAIN ||
601 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400602 /*
603 * Minimum sleep to prevent looping, allowing socket
604 * to clear and app threads to set tcpStatus
605 * CifsNeedReconnect if server hung.
606 */
607 usleep_range(1000, 2000);
608 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400609 continue;
Al Viro71335662016-01-09 19:54:50 -0500610 }
611
612 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500613 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400614 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500615 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400616 }
617 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400618 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400619}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400620
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400621int
622cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
623 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400624{
Al Viro71335662016-01-09 19:54:50 -0500625 struct msghdr smb_msg;
626 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
627 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400628
Al Viro71335662016-01-09 19:54:50 -0500629 return cifs_readv_from_socket(server, &smb_msg);
630}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400631
Al Viro71335662016-01-09 19:54:50 -0500632int
633cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
634 unsigned int to_read)
635{
636 struct msghdr smb_msg;
637 struct bio_vec bv = {.bv_page = page, .bv_len = to_read};
638 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
639 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400640}
641
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400642static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400643is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400644{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400645 /*
646 * The first byte big endian of the length field,
647 * is actually not part of the length but the type
648 * with the most common, zero, as regular data.
649 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400650 switch (type) {
651 case RFC1002_SESSION_MESSAGE:
652 /* Regular SMB response */
653 return true;
654 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500655 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400656 break;
657 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500658 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400659 break;
660 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400661 /*
662 * We get this from Windows 98 instead of an error on
663 * SMB negprot response.
664 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500665 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400666 /* give server a second to clean up */
667 msleep(1000);
668 /*
669 * Always try 445 first on reconnect since we get NACK
670 * on some if we ever connected to port 139 (the NACK
671 * is since we do not begin with RFC1001 session
672 * initialize frame).
673 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400674 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400675 cifs_reconnect(server);
676 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400677 break;
678 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500679 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400680 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400681 }
682
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400683 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400684}
685
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400686void
687dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400688{
689#ifdef CONFIG_CIFS_STATS2
690 mid->when_received = jiffies;
691#endif
692 spin_lock(&GlobalMid_Lock);
693 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400694 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400695 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400696 mid->mid_state = MID_RESPONSE_MALFORMED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400697 list_del_init(&mid->qhead);
698 spin_unlock(&GlobalMid_Lock);
699}
700
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400701static void
702handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400703 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400704{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400705 if (server->ops->check_trans2 &&
706 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400707 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400708 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400709 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400710 /* Was previous buf put in mpx struct for multi-rsp? */
711 if (!mid->multiRsp) {
712 /* smb buffer will be freed by user thread */
713 if (server->large_buf)
714 server->bigbuf = NULL;
715 else
716 server->smallbuf = NULL;
717 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400718 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400719}
720
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400721static void clean_demultiplex_info(struct TCP_Server_Info *server)
722{
723 int length;
724
725 /* take it off the list, if it's not already */
726 spin_lock(&cifs_tcp_ses_lock);
727 list_del_init(&server->tcp_ses_list);
728 spin_unlock(&cifs_tcp_ses_lock);
729
730 spin_lock(&GlobalMid_Lock);
731 server->tcpStatus = CifsExiting;
732 spin_unlock(&GlobalMid_Lock);
733 wake_up_all(&server->response_q);
734
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400735 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300736 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400737 if (server->credits <= 0)
738 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300739 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400740 /*
741 * Although there should not be any requests blocked on this queue it
742 * can not hurt to be paranoid and try to wake up requests that may
743 * haven been blocked when more than 50 at time were on the wire to the
744 * same server - they now will see the session is in exit state and get
745 * out of SendReceive.
746 */
747 wake_up_all(&server->request_q);
748 /* give those requests time to exit */
749 msleep(125);
750
751 if (server->ssocket) {
752 sock_release(server->ssocket);
753 server->ssocket = NULL;
754 }
755
756 if (!list_empty(&server->pending_mid_q)) {
757 struct list_head dispose_list;
758 struct mid_q_entry *mid_entry;
759 struct list_head *tmp, *tmp2;
760
761 INIT_LIST_HEAD(&dispose_list);
762 spin_lock(&GlobalMid_Lock);
763 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
764 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500765 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400766 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400767 list_move(&mid_entry->qhead, &dispose_list);
768 }
769 spin_unlock(&GlobalMid_Lock);
770
771 /* now walk dispose list and issue callbacks */
772 list_for_each_safe(tmp, tmp2, &dispose_list) {
773 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500774 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400775 list_del_init(&mid_entry->qhead);
776 mid_entry->callback(mid_entry);
777 }
778 /* 1/8th of sec is more than enough time for them to exit */
779 msleep(125);
780 }
781
782 if (!list_empty(&server->pending_mid_q)) {
783 /*
784 * mpx threads have not exited yet give them at least the smb
785 * send timeout time for long ops.
786 *
787 * Due to delays on oplock break requests, we need to wait at
788 * least 45 seconds before giving up on a request getting a
789 * response and going ahead and killing cifsd.
790 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500791 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400792 msleep(46000);
793 /*
794 * If threads still have not exited they are probably never
795 * coming home not much else we can do but free the memory.
796 */
797 }
798
799 kfree(server->hostname);
800 kfree(server);
801
802 length = atomic_dec_return(&tcpSesAllocCount);
803 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700804 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400805}
806
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400807static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400808standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
809{
810 int length;
811 char *buf = server->smallbuf;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400812 unsigned int pdu_length = get_rfc1002_length(buf);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400813
814 /* make sure this will fit in a large buffer */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400815 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500816 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400817 cifs_reconnect(server);
818 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400819 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400820 }
821
822 /* switch to large buffer if too big for a small one */
823 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
824 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400825 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400826 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400827 }
828
829 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400830 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
831 pdu_length - HEADER_SIZE(server) + 1 + 4);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400832 if (length < 0)
833 return length;
834 server->total_read += length;
835
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400836 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400837
838 /*
839 * We know that we received enough to get to the MID as we
840 * checked the pdu_length earlier. Now check to see
841 * if the rest of the header is OK. We borrow the length
842 * var for the rest of the loop to avoid a new stack var.
843 *
844 * 48 bytes is enough to display the header and a little bit
845 * into the payload for debugging purposes.
846 */
Steve French373512e2015-12-18 13:05:30 -0600847 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400848 if (length != 0)
849 cifs_dump_mem("Bad SMB: ", buf,
850 min_t(unsigned int, server->total_read, 48));
851
Pavel Shilovsky92d7d3e2017-07-08 14:32:00 -0700852 if (server->ops->is_session_expired &&
853 server->ops->is_session_expired(buf)) {
854 cifs_reconnect(server);
855 wake_up(&server->response_q);
856 return -1;
857 }
858
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700859 if (server->ops->is_status_pending &&
860 server->ops->is_status_pending(buf, server, length))
861 return -1;
862
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500863 if (!mid)
864 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400865
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400866 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500867 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400868}
869
870static int
Al Viro7c97c202011-06-21 08:51:28 -0400871cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 int length;
Al Viro7c97c202011-06-21 08:51:28 -0400874 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400875 unsigned int pdu_length;
876 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500877 struct task_struct *task_to_wake = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct mid_q_entry *mid_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500881 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400882
883 length = atomic_inc_return(&tcpSesAllocCount);
884 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700885 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700887 set_freezable();
Eric W. Biedermana3714572019-08-16 12:33:54 -0500888 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +0000889 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700890 if (try_to_freeze())
891 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700892
Jeff Layton2a37ef92011-10-19 15:29:23 -0400893 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400894 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700895
Jeff Layton2a37ef92011-10-19 15:29:23 -0400896 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400897 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000898 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000899
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400900 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400901 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000902 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400903 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700904
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400905 /*
906 * The right amount was read from socket - 4 bytes,
907 * so we can now interpret the length field.
908 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400909 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700910
Joe Perchesf96637b2013-05-04 22:12:25 -0500911 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400912 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000913 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700914
Jeff Layton89482a52011-10-19 15:28:57 -0400915 /* make sure we have enough to get to the MID */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400916 if (pdu_length < HEADER_SIZE(server) - 1 - 4) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500917 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
918 pdu_length);
Jeff Layton89482a52011-10-19 15:28:57 -0400919 cifs_reconnect(server);
920 wake_up(&server->response_q);
921 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700922 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400923
Jeff Layton89482a52011-10-19 15:28:57 -0400924 /* read down to the MID */
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400925 length = cifs_read_from_socket(server, buf + 4,
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400926 HEADER_SIZE(server) - 1 - 4);
Jeff Layton89482a52011-10-19 15:28:57 -0400927 if (length < 0)
928 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400929 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400930
Pavel Shilovsky8aa26f32012-05-17 13:25:35 +0400931 mid_entry = server->ops->find_mid(server, buf);
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400932
Jeff Layton44d22d82011-10-19 15:29:49 -0400933 if (!mid_entry || !mid_entry->receive)
934 length = standard_receive3(server, mid_entry);
935 else
936 length = mid_entry->receive(server, mid_entry);
937
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400938 if (length < 0)
Steve Frenche4eb2952005-04-28 22:41:09 -0700939 continue;
940
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400941 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -0400942 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700943
Steve Frenchfda35942011-01-20 18:06:34 +0000944 server->lstrp = jiffies;
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500945 if (mid_entry != NULL) {
Sachin Prabhud8fd99d2017-03-03 15:41:38 -0800946 if ((mid_entry->mid_flags & MID_WAIT_CANCELLED) &&
947 mid_entry->mid_state == MID_RESPONSE_RECEIVED &&
948 server->ops->handle_cancelled_mid)
949 server->ops->handle_cancelled_mid(
950 mid_entry->resp_buf,
951 server);
952
Jeff Layton2a37ef92011-10-19 15:29:23 -0400953 if (!mid_entry->multiRsp || mid_entry->multiEnd)
954 mid_entry->callback(mid_entry);
Sachin Prabhud8fd99d2017-03-03 15:41:38 -0800955 } else if (server->ops->is_oplock_break &&
956 server->ops->is_oplock_break(buf, server)) {
957 cifs_dbg(FYI, "Received oplock break\n");
958 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500959 cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
960 atomic_read(&midCount));
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400961 cifs_dump_mem("Received Data is: ", buf,
962 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +0000963#ifdef CONFIG_CIFS_DEBUG2
Pavel Shilovsky7f0adb52012-05-28 15:50:10 +0400964 if (server->ops->dump_detail)
965 server->ops->dump_detail(buf);
Steve French39798772006-05-31 22:40:51 +0000966 cifs_dump_mids(server);
967#endif /* CIFS_DEBUG2 */
Steve French50c2f752007-07-13 00:33:32 +0000968
Steve Frenche4eb2952005-04-28 22:41:09 -0700969 }
970 } /* end while !EXITING */
971
Justin P. Mattockfd62cb72011-02-24 22:15:02 -0800972 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -0400973 cifs_buf_release(server->bigbuf);
974 if (server->smallbuf) /* no sense logging a debug message if NULL */
975 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500977 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400978 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500979
980 /* if server->tsk was NULL then wait for a signal before exiting */
981 if (!task_to_wake) {
982 set_current_state(TASK_INTERRUPTIBLE);
983 while (!signal_pending(current)) {
984 schedule();
985 set_current_state(TASK_INTERRUPTIBLE);
986 }
987 set_current_state(TASK_RUNNING);
988 }
989
Jeff Layton0468a2c2008-12-01 07:09:35 -0500990 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Jeff Laytonc359cf32007-11-16 22:22:06 +0000993/* extract the host portion of the UNC string */
994static char *
995extract_hostname(const char *unc)
996{
997 const char *src;
998 char *dst, *delim;
999 unsigned int len;
1000
1001 /* skip double chars at beginning of string */
1002 /* BB: check validity of these bytes? */
1003 src = unc + 2;
1004
1005 /* delimiter between hostname and sharename is always '\\' now */
1006 delim = strchr(src, '\\');
1007 if (!delim)
1008 return ERR_PTR(-EINVAL);
1009
1010 len = delim - src;
1011 dst = kmalloc((len + 1), GFP_KERNEL);
1012 if (dst == NULL)
1013 return ERR_PTR(-ENOMEM);
1014
1015 memcpy(dst, src, len);
1016 dst[len] = '\0';
1017
1018 return dst;
1019}
1020
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001021static int get_option_ul(substring_t args[], unsigned long *option)
1022{
1023 int rc;
1024 char *string;
1025
1026 string = match_strdup(args);
1027 if (string == NULL)
1028 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001029 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001030 kfree(string);
1031
1032 return rc;
1033}
1034
Eric W. Biederman3da46562013-02-06 01:37:39 -08001035static int get_option_uid(substring_t args[], kuid_t *result)
1036{
1037 unsigned long value;
1038 kuid_t uid;
1039 int rc;
1040
1041 rc = get_option_ul(args, &value);
1042 if (rc)
1043 return rc;
1044
1045 uid = make_kuid(current_user_ns(), value);
1046 if (!uid_valid(uid))
1047 return -EINVAL;
1048
1049 *result = uid;
1050 return 0;
1051}
1052
1053static int get_option_gid(substring_t args[], kgid_t *result)
1054{
1055 unsigned long value;
1056 kgid_t gid;
1057 int rc;
1058
1059 rc = get_option_ul(args, &value);
1060 if (rc)
1061 return rc;
1062
1063 gid = make_kgid(current_user_ns(), value);
1064 if (!gid_valid(gid))
1065 return -EINVAL;
1066
1067 *result = gid;
1068 return 0;
1069}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001070
1071static int cifs_parse_security_flavors(char *value,
1072 struct smb_vol *vol)
1073{
1074
1075 substring_t args[MAX_OPT_ARGS];
1076
Jeff Layton1e3cc572013-06-10 17:12:23 -05001077 /*
1078 * With mount options, the last one should win. Reset any existing
1079 * settings back to default.
1080 */
1081 vol->sectype = Unspecified;
1082 vol->sign = false;
1083
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001084 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001085 case Opt_sec_krb5p:
1086 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1087 return 1;
1088 case Opt_sec_krb5i:
1089 vol->sign = true;
1090 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001091 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001092 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001093 break;
1094 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001095 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001096 /* Fallthrough */
1097 case Opt_sec_ntlmssp:
1098 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001099 break;
1100 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001101 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001102 /* Fallthrough */
1103 case Opt_ntlm:
1104 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001105 break;
1106 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001107 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001108 /* Fallthrough */
1109 case Opt_sec_ntlmv2:
1110 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001111 break;
1112#ifdef CONFIG_CIFS_WEAK_PW_HASH
1113 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001114 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001115 break;
1116#endif
1117 case Opt_sec_none:
1118 vol->nullauth = 1;
1119 break;
1120 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001121 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001122 return 1;
1123 }
1124
1125 return 0;
1126}
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001129cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1130{
1131 substring_t args[MAX_OPT_ARGS];
1132
1133 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1134 case Opt_cache_loose:
1135 vol->direct_io = false;
1136 vol->strict_io = false;
1137 break;
1138 case Opt_cache_strict:
1139 vol->direct_io = false;
1140 vol->strict_io = true;
1141 break;
1142 case Opt_cache_none:
1143 vol->direct_io = true;
1144 vol->strict_io = false;
1145 break;
1146 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001147 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001148 return 1;
1149 }
1150 return 0;
1151}
1152
1153static int
Jeff Layton23db65f2012-05-15 12:20:51 -04001154cifs_parse_smb_version(char *value, struct smb_vol *vol)
1155{
1156 substring_t args[MAX_OPT_ARGS];
1157
1158 switch (match_token(value, cifs_smb_version_tokens, args)) {
1159 case Smb_1:
1160 vol->ops = &smb1_operations;
1161 vol->vals = &smb1_values;
1162 break;
Steve French1080ef72011-02-24 18:07:19 +00001163#ifdef CONFIG_CIFS_SMB2
Steve Frenchdd446b12012-11-28 23:21:06 -06001164 case Smb_20:
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001165 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001166 vol->vals = &smb20_values;
1167 break;
Steve French1080ef72011-02-24 18:07:19 +00001168 case Smb_21:
1169 vol->ops = &smb21_operations;
1170 vol->vals = &smb21_values;
1171 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001172 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001173 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001174 vol->vals = &smb30_values;
1175 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001176 case Smb_302:
1177 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1178 vol->vals = &smb302_values;
1179 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001180#ifdef CONFIG_CIFS_SMB311
1181 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001182 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001183 vol->vals = &smb311_values;
1184 break;
1185#endif /* SMB311 */
Steve French1080ef72011-02-24 18:07:19 +00001186#endif
Jeff Layton23db65f2012-05-15 12:20:51 -04001187 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001188 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001189 return 1;
1190 }
1191 return 0;
1192}
1193
Jeff Laytond387a5c2012-12-10 06:10:46 -05001194/*
1195 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1196 * fields with the result. Returns 0 on success and an error otherwise.
1197 */
1198static int
1199cifs_parse_devname(const char *devname, struct smb_vol *vol)
1200{
1201 char *pos;
1202 const char *delims = "/\\";
1203 size_t len;
1204
Yao Liu9d5a48f2019-01-28 19:47:28 +08001205 if (unlikely(!devname || !*devname)) {
1206 cifs_dbg(VFS, "Device name not specified.\n");
1207 return -EINVAL;
1208 }
1209
Jeff Laytond387a5c2012-12-10 06:10:46 -05001210 /* make sure we have a valid UNC double delimiter prefix */
1211 len = strspn(devname, delims);
1212 if (len != 2)
1213 return -EINVAL;
1214
1215 /* find delimiter between host and sharename */
1216 pos = strpbrk(devname + 2, delims);
1217 if (!pos)
1218 return -EINVAL;
1219
1220 /* skip past delimiter */
1221 ++pos;
1222
1223 /* now go until next delimiter or end of string */
1224 len = strcspn(pos, delims);
1225
1226 /* move "pos" up to delimiter or NULL */
1227 pos += len;
1228 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1229 if (!vol->UNC)
1230 return -ENOMEM;
1231
1232 convert_delimiter(vol->UNC, '\\');
1233
Sachin Prabhu11e31642016-02-08 13:44:01 +05301234 /* skip any delimiter */
1235 if (*pos == '/' || *pos == '\\')
1236 pos++;
1237
1238 /* If pos is NULL then no prepath */
1239 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001240 return 0;
1241
1242 vol->prepath = kstrdup(pos, GFP_KERNEL);
1243 if (!vol->prepath)
1244 return -ENOMEM;
1245
1246 return 0;
1247}
1248
Jeff Layton23db65f2012-05-15 12:20:51 -04001249static int
Sean Finneyb9468452011-04-11 13:19:32 +00001250cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve French50c2f752007-07-13 00:33:32 +00001251 struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001253 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001254 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 unsigned int temp_len, i, j;
1256 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001257 short int override_uid = -1;
1258 short int override_gid = -1;
1259 bool uid_specified = false;
1260 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001261 bool sloppy = false;
1262 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001263 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001264 char *string = NULL;
1265 char *tmp_end, *value;
1266 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001267 bool got_ip = false;
1268 unsigned short port = 0;
1269 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001272 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001273 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Jeff Layton6ee95422012-11-26 11:09:57 -05001275 /* ensure we always start with zeroed-out smb_vol */
1276 memset(vol, 0, sizeof(*vol));
1277
Jeff Layton88463992010-11-22 15:31:03 -05001278 /*
1279 * does not have to be perfect mapping since field is
1280 * informational, only used for servers that do not support
1281 * port 445 and it can be overridden at mount time
1282 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001283 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1284 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001285 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1286
Jeff Layton1397f2e2011-01-07 11:30:28 -05001287 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001288 /* null target name indicates to use *SMBSERVR default called name
1289 if we end up sending RFC1001 session initialize */
1290 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001291 vol->cred_uid = current_uid();
1292 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001293 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001294
Steve French2baa2682014-09-27 02:19:01 -05001295 /*
1296 * default to SFM style remapping of seven reserved characters
1297 * unless user overrides it or we negotiate CIFS POSIX where
1298 * it is unnecessary. Can not simultaneously use more than one mapping
1299 * since then readdir could list files that open could not open
1300 */
1301 vol->remap = true;
1302
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001303 /* default to only allowing write access to owner of the mount */
1304 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001307 /* default is always to request posix paths. */
1308 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001309 /* default to using server inode numbers where available */
1310 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001311
Jeff Layton1b359202012-09-19 15:20:27 -07001312 /* default is to use strict cifs caching semantics */
1313 vol->strict_io = true;
1314
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301315 vol->actimeo = CIFS_DEF_ACTIMEO;
1316
Jeff Layton23db65f2012-05-15 12:20:51 -04001317 /* FIXME: add autonegotiation -- for now, SMB1 is default */
1318 vol->ops = &smb1_operations;
1319 vol->vals = &smb1_values;
1320
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001321 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1322
Sean Finneyb9468452011-04-11 13:19:32 +00001323 if (!mountdata)
1324 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Sean Finneyb9468452011-04-11 13:19:32 +00001326 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1327 if (!mountdata_copy)
1328 goto cifs_parse_mount_err;
1329
1330 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001331 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001332
Steve French50c2f752007-07-13 00:33:32 +00001333 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001334 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 separator[0] = options[4];
1336 options += 5;
1337 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001338 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001341 vol->backupuid_specified = false; /* no backup intent for a user */
1342 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001343
Jeff Layton37d4f992013-05-24 07:40:05 -04001344 switch (cifs_parse_devname(devname, vol)) {
1345 case 0:
1346 break;
1347 case -ENOMEM:
1348 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1349 goto cifs_parse_mount_err;
1350 case -EINVAL:
1351 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1352 goto cifs_parse_mount_err;
1353 default:
1354 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1355 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001356 }
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001359 substring_t args[MAX_OPT_ARGS];
1360 unsigned long option;
1361 int token;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (!*data)
1364 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001366 token = match_token(data, cifs_mount_option_tokens, args);
1367
1368 switch (token) {
1369
1370 /* Ingnore the following */
1371 case Opt_ignore:
1372 break;
1373
1374 /* Boolean values */
1375 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001377 break;
1378 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001380 break;
1381 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001382 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001383 break;
1384 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001385 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001386 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001387 case Opt_forcegid:
1388 override_gid = 1;
1389 break;
1390 case Opt_noforcegid:
1391 override_gid = 0;
1392 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001393 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001394 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001395 break;
1396 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001397 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001398 break;
1399 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001401 break;
1402 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001404 break;
1405 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001407 break;
1408 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001410 break;
1411 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001412 vol->sfu_remap = true;
1413 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001414 break;
1415 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001416 vol->sfu_remap = false;
1417 break;
1418 case Opt_mapposix:
1419 vol->remap = true;
1420 vol->sfu_remap = false; /* disable SFU mapping */
1421 break;
1422 case Opt_nomapposix:
1423 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001424 break;
1425 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001426 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001427 break;
1428 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001429 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001430 break;
1431 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001432 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001433 break;
1434 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001435 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001436 break;
1437 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001438 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001439 break;
1440 case Opt_nounix:
Steve Frenchc18c8422007-07-18 23:21:09 +00001441 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001442 break;
1443 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001444 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001445 break;
1446 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001447 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001448 break;
1449 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001450 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001451 /*
1452 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001453 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001454 * local vfs will do advisory
1455 */
Steve French50c2f752007-07-13 00:33:32 +00001456 if (vol->file_mode ==
1457 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001458 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001459 break;
1460 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001461 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001462 break;
1463 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001465 break;
1466 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001468 break;
Steve French95932652016-09-23 01:36:34 -05001469 case Opt_setuidfromacl:
1470 vol->setuidfromacl = 1;
1471 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001472 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001473 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001474 break;
1475 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001476 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001477 break;
1478 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001480 break;
1481 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001483 break;
1484 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001486 break;
1487 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001489 break;
1490 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001491 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001492 break;
1493 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001494 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001495 break;
1496 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001498 break;
1499 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001501 break;
1502 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001503 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001504 break;
1505 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001506 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001507 break;
1508 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001509 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001510 break;
1511 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001513 break;
1514 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001516 break;
1517 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001518 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001519 break;
1520 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001521 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001522 break;
1523 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001524 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001525 * is a per tree connection (mount) not a per socket
1526 * or per-smb connection option in the protocol
1527 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1528 */
Steve French95b1cb92008-05-15 16:44:38 +00001529 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001530 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001531 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001532 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001533 break;
1534 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301535#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001536 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001537 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301538#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301539 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001540 break;
1541 case Opt_mfsymlinks:
Stefan Metzmacher736a3322010-07-30 14:56:00 +02001542 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001543 break;
1544 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001545 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001546 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001547 case Opt_sloppy:
1548 sloppy = true;
1549 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001550 case Opt_nosharesock:
1551 vol->nosharesock = true;
1552 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001553 case Opt_nopersistent:
1554 vol->nopersistent = true;
1555 if (vol->persistent) {
1556 cifs_dbg(VFS,
1557 "persistenthandles mount options conflict\n");
1558 goto cifs_parse_mount_err;
1559 }
1560 break;
1561 case Opt_persistent:
1562 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001563 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001564 cifs_dbg(VFS,
1565 "persistenthandles mount options conflict\n");
1566 goto cifs_parse_mount_err;
1567 }
1568 break;
Steve French592fafe2015-11-03 10:08:53 -06001569 case Opt_resilient:
1570 vol->resilient = true;
1571 if (vol->persistent) {
1572 cifs_dbg(VFS,
1573 "persistenthandles mount options conflict\n");
1574 goto cifs_parse_mount_err;
1575 }
1576 break;
1577 case Opt_noresilient:
1578 vol->resilient = false; /* already the default */
1579 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001580
1581 /* Numeric Values */
1582 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001583 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001584 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1585 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001586 goto cifs_parse_mount_err;
1587 }
1588 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001589 break;
1590 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001591 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001592 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1593 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001594 goto cifs_parse_mount_err;
1595 }
1596 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001597 break;
1598 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001599 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001600 cifs_dbg(VFS, "%s: Invalid uid value\n",
1601 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001602 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001604 uid_specified = true;
1605 break;
1606 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001607 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001608 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1609 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001610 goto cifs_parse_mount_err;
1611 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001612 break;
1613 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001614 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001615 cifs_dbg(VFS, "%s: Invalid gid value\n",
1616 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001617 goto cifs_parse_mount_err;
1618 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001619 gid_specified = true;
1620 break;
1621 case Opt_file_mode:
1622 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001623 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1624 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001625 goto cifs_parse_mount_err;
1626 }
1627 vol->file_mode = option;
1628 break;
1629 case Opt_dirmode:
1630 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001631 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1632 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001633 goto cifs_parse_mount_err;
1634 }
1635 vol->dir_mode = option;
1636 break;
1637 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001638 if (get_option_ul(args, &option) ||
1639 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001640 cifs_dbg(VFS, "%s: Invalid port value\n",
1641 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001642 goto cifs_parse_mount_err;
1643 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001644 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001645 break;
1646 case Opt_rsize:
1647 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001648 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1649 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001650 goto cifs_parse_mount_err;
1651 }
1652 vol->rsize = option;
1653 break;
1654 case Opt_wsize:
1655 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001656 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1657 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001658 goto cifs_parse_mount_err;
1659 }
1660 vol->wsize = option;
1661 break;
1662 case Opt_actimeo:
1663 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001664 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1665 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001666 goto cifs_parse_mount_err;
1667 }
1668 vol->actimeo = HZ * option;
1669 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001670 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001671 goto cifs_parse_mount_err;
1672 }
1673 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001674 case Opt_echo_interval:
1675 if (get_option_ul(args, &option)) {
1676 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1677 __func__);
1678 goto cifs_parse_mount_err;
1679 }
1680 vol->echo_interval = option;
1681 break;
Steve French141891f2016-09-23 00:44:16 -05001682 case Opt_max_credits:
1683 if (get_option_ul(args, &option) || (option < 20) ||
1684 (option > 60000)) {
1685 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1686 __func__);
1687 goto cifs_parse_mount_err;
1688 }
1689 vol->max_credits = option;
1690 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001691
1692 /* String Arguments */
1693
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001694 case Opt_blank_user:
1695 /* null user, ie. anonymous authentication */
1696 vol->nullauth = 1;
1697 vol->username = NULL;
1698 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001699 case Opt_user:
1700 string = match_strdup(args);
1701 if (string == NULL)
1702 goto out_nomem;
1703
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001704 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1705 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001706 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001707 goto cifs_parse_mount_err;
1708 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001709
1710 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001711 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001712 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001713 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001714 break;
1715 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001716 /* passwords have to be handled differently
1717 * to allow the character used for deliminator
1718 * to be passed within them
1719 */
1720
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001721 /*
1722 * Check if this is a case where the password
1723 * starts with a delimiter
1724 */
1725 tmp_end = strchr(data, '=');
1726 tmp_end++;
1727 if (!(tmp_end < end && tmp_end[1] == delim)) {
1728 /* No it is not. Set the password to NULL */
Aurelien Aptel7e689162018-01-25 15:59:39 +01001729 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001730 vol->password = NULL;
1731 break;
1732 }
1733 /* Yes it is. Drop down to Opt_pass below.*/
1734 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001735 /* Obtain the value string */
1736 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001737 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001738
1739 /* Set tmp_end to end of the string */
1740 tmp_end = (char *) value + strlen(value);
1741
1742 /* Check if following character is the deliminator
1743 * If yes, we have encountered a double deliminator
1744 * reset the NULL character to the deliminator
1745 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301746 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001747 tmp_end[0] = delim;
1748
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301749 /* Keep iterating until we get to a single
1750 * deliminator OR the end
1751 */
1752 while ((tmp_end = strchr(tmp_end, delim))
1753 != NULL && (tmp_end[1] == delim)) {
1754 tmp_end = (char *) &tmp_end[2];
1755 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001756
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301757 /* Reset var options to point to next element */
1758 if (tmp_end) {
1759 tmp_end[0] = '\0';
1760 options = (char *) &tmp_end[1];
1761 } else
1762 /* Reached the end of the mount option
1763 * string */
1764 options = end;
1765 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001766
Aurelien Aptel7e689162018-01-25 15:59:39 +01001767 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001768 /* Now build new password string */
1769 temp_len = strlen(value);
1770 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1771 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001772 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001773 goto cifs_parse_mount_err;
1774 }
1775
1776 for (i = 0, j = 0; i < temp_len; i++, j++) {
1777 vol->password[j] = value[i];
1778 if ((value[i] == delim) &&
1779 value[i+1] == delim)
1780 /* skip the second deliminator */
1781 i++;
1782 }
1783 vol->password[j] = '\0';
1784 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001785 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001786 /* FIXME: should this be an error instead? */
1787 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001788 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001789 case Opt_ip:
1790 string = match_strdup(args);
1791 if (string == NULL)
1792 goto out_nomem;
1793
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001794 if (!cifs_convert_address(dstaddr, string,
1795 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001796 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001797 goto cifs_parse_mount_err;
1798 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001799 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001800 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001801 case Opt_domain:
1802 string = match_strdup(args);
1803 if (string == NULL)
1804 goto out_nomem;
1805
Chen Gang057d6332013-07-19 09:01:36 +08001806 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1807 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001808 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001809 goto cifs_parse_mount_err;
1810 }
1811
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001812 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001813 vol->domainname = kstrdup(string, GFP_KERNEL);
1814 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001815 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001816 goto cifs_parse_mount_err;
1817 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001818 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001819 break;
1820 case Opt_srcaddr:
1821 string = match_strdup(args);
1822 if (string == NULL)
1823 goto out_nomem;
1824
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001825 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001826 (struct sockaddr *)&vol->srcaddr,
1827 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001828 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1829 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001830 goto cifs_parse_mount_err;
1831 }
1832 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001833 case Opt_iocharset:
1834 string = match_strdup(args);
1835 if (string == NULL)
1836 goto out_nomem;
1837
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001838 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001839 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001840 goto cifs_parse_mount_err;
1841 }
1842
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001843 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001844 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001845 vol->iocharset = kstrdup(string,
1846 GFP_KERNEL);
1847 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001848 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001849 goto cifs_parse_mount_err;
1850 }
1851 }
1852 /* if iocharset not set then load_nls_default
1853 * is used by caller
1854 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001855 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001856 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001857 case Opt_netbiosname:
1858 string = match_strdup(args);
1859 if (string == NULL)
1860 goto out_nomem;
1861
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001862 memset(vol->source_rfc1001_name, 0x20,
1863 RFC1001_NAME_LEN);
1864 /*
1865 * FIXME: are there cases in which a comma can
1866 * be valid in workstation netbios name (and
1867 * need special handling)?
1868 */
1869 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1870 /* don't ucase netbiosname for user */
1871 if (string[i] == 0)
1872 break;
1873 vol->source_rfc1001_name[i] = string[i];
1874 }
1875 /* The string has 16th byte zero still from
1876 * set at top of the function
1877 */
1878 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001879 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001880 break;
1881 case Opt_servern:
1882 /* servernetbiosname specified override *SMBSERVER */
1883 string = match_strdup(args);
1884 if (string == NULL)
1885 goto out_nomem;
1886
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001887 /* last byte, type, is 0x20 for servr type */
1888 memset(vol->target_rfc1001_name, 0x20,
1889 RFC1001_NAME_LEN_WITH_NULL);
1890
1891 /* BB are there cases in which a comma can be
1892 valid in this workstation netbios name
1893 (and need special handling)? */
1894
1895 /* user or mount helper must uppercase the
1896 netbios name */
1897 for (i = 0; i < 15; i++) {
1898 if (string[i] == 0)
1899 break;
1900 vol->target_rfc1001_name[i] = string[i];
1901 }
1902 /* The string has 16th byte zero still from
1903 set at top of the function */
1904 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001905 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001906 break;
1907 case Opt_ver:
1908 string = match_strdup(args);
1909 if (string == NULL)
1910 goto out_nomem;
1911
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001912 if (strncasecmp(string, "1", 1) == 0) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001913 /* This is the default */
1914 break;
1915 }
1916 /* For all other value, error */
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001917 pr_warn("CIFS: Invalid version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001918 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04001919 case Opt_vers:
1920 string = match_strdup(args);
1921 if (string == NULL)
1922 goto out_nomem;
1923
1924 if (cifs_parse_smb_version(string, vol) != 0)
1925 goto cifs_parse_mount_err;
1926 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001927 case Opt_sec:
1928 string = match_strdup(args);
1929 if (string == NULL)
1930 goto out_nomem;
1931
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001932 if (cifs_parse_security_flavors(string, vol) != 0)
1933 goto cifs_parse_mount_err;
1934 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04001935 case Opt_cache:
1936 string = match_strdup(args);
1937 if (string == NULL)
1938 goto out_nomem;
1939
1940 if (cifs_parse_cache_flavor(string, vol) != 0)
1941 goto cifs_parse_mount_err;
1942 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001943 default:
Jeff Laytond8162552012-03-23 14:40:56 -04001944 /*
1945 * An option we don't recognize. Save it off for later
1946 * if we haven't already found one
1947 */
1948 if (!invalid)
1949 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001952 /* Free up any allocated string */
1953 kfree(string);
1954 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04001956
Jeff Laytond8162552012-03-23 14:40:56 -04001957 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001958 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04001959 goto cifs_parse_mount_err;
1960 }
1961
Jeff Layton8a8798a2012-01-17 16:09:15 -05001962#ifndef CONFIG_KEYS
1963 /* Muliuser mounts require CONFIG_KEYS support */
1964 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001965 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001966 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04001967 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05001968#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05001969 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04001970 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05001971 goto cifs_parse_mount_err;
1972 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04001973
Jeff Layton62a1a432012-12-10 06:10:45 -05001974 /* make sure UNC has a share name */
1975 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001976 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05001977 goto cifs_parse_mount_err;
1978 }
1979
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001980 if (!got_ip) {
1981 /* No ip= option specified? Try to get it from UNC */
1982 if (!cifs_convert_address(dstaddr, &vol->UNC[2],
1983 strlen(&vol->UNC[2]))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001984 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001985 goto cifs_parse_mount_err;
1986 }
1987 }
1988
1989 /* set the port that we got earlier */
1990 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001992 if (uid_specified)
1993 vol->override_uid = override_uid;
1994 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001995 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001996
1997 if (gid_specified)
1998 vol->override_gid = override_gid;
1999 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002000 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002001
Sean Finneyb9468452011-04-11 13:19:32 +00002002 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002004
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002005out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002006 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002007cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002008 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002009 kfree(mountdata_copy);
2010 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Ben Greear3eb9a882010-09-01 17:06:02 -07002013/** Returns true if srcaddr isn't specified and rhs isn't
2014 * specified, or if srcaddr is specified and
2015 * matches the IP address of the rhs argument.
2016 */
Jeff Layton45151482010-07-06 20:43:02 -04002017static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002018srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2019{
2020 switch (srcaddr->sa_family) {
2021 case AF_UNSPEC:
2022 return (rhs->sa_family == AF_UNSPEC);
2023 case AF_INET: {
2024 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2025 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2026 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2027 }
2028 case AF_INET6: {
2029 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002030 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002031 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2032 }
2033 default:
2034 WARN_ON(1);
2035 return false; /* don't expect to be here */
2036 }
2037}
2038
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002039/*
2040 * If no port is specified in addr structure, we try to match with 445 port
2041 * and if it fails - with 139 ports. It should be called only if address
2042 * families of server and addr are equal.
2043 */
2044static bool
2045match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2046{
Steve French6da97912011-03-13 18:55:55 +00002047 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002048
2049 switch (addr->sa_family) {
2050 case AF_INET:
2051 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2052 port = ((struct sockaddr_in *) addr)->sin_port;
2053 break;
2054 case AF_INET6:
2055 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2056 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2057 break;
2058 default:
2059 WARN_ON(1);
2060 return false;
2061 }
2062
2063 if (!port) {
2064 port = htons(CIFS_PORT);
2065 if (port == *sport)
2066 return true;
2067
2068 port = htons(RFC1001_PORT);
2069 }
2070
2071 return port == *sport;
2072}
Ben Greear3eb9a882010-09-01 17:06:02 -07002073
2074static bool
2075match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2076 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Jeff Layton45151482010-07-06 20:43:02 -04002078 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002079 case AF_INET: {
2080 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2081 struct sockaddr_in *srv_addr4 =
2082 (struct sockaddr_in *)&server->dstaddr;
2083
2084 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002085 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002086 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002087 }
2088 case AF_INET6: {
2089 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2090 struct sockaddr_in6 *srv_addr6 =
2091 (struct sockaddr_in6 *)&server->dstaddr;
2092
Jeff Layton45151482010-07-06 20:43:02 -04002093 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002094 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002095 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002096 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002097 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002098 break;
2099 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002100 default:
2101 WARN_ON(1);
2102 return false; /* don't expect to be here */
2103 }
Jeff Layton45151482010-07-06 20:43:02 -04002104
Ben Greear3eb9a882010-09-01 17:06:02 -07002105 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2106 return false;
2107
Jeff Layton45151482010-07-06 20:43:02 -04002108 return true;
2109}
2110
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002111static bool
2112match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2113{
Jeff Layton3f618222013-06-12 19:52:14 -05002114 /*
2115 * The select_sectype function should either return the vol->sectype
2116 * that was specified, or "Unspecified" if that sectype was not
2117 * compatible with the given NEGOTIATE request.
2118 */
2119 if (select_sectype(server, vol->sectype) == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002120 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002121
Jeff Layton3f618222013-06-12 19:52:14 -05002122 /*
2123 * Now check if signing mode is acceptable. No need to check
2124 * global_secflags at this point since if MUST_SIGN is set then
2125 * the server->sign had better be too.
2126 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002127 if (vol->sign && !server->sign)
2128 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002129
2130 return true;
2131}
2132
Jeff Layton9fa114f2012-11-26 11:09:57 -05002133static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002134{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002135 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2136
Jeff Laytona0b3df52013-05-24 07:40:59 -04002137 if (vol->nosharesock)
2138 return 0;
2139
Jeff Layton23db65f2012-05-15 12:20:51 -04002140 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2141 return 0;
2142
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002143 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2144 return 0;
2145
2146 if (!match_address(server, addr,
2147 (struct sockaddr *)&vol->srcaddr))
2148 return 0;
2149
2150 if (!match_port(server, addr))
2151 return 0;
2152
2153 if (!match_security(server, vol))
2154 return 0;
2155
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002156 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002157 return 0;
2158
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002159 return 1;
2160}
2161
Jeff Layton45151482010-07-06 20:43:02 -04002162static struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002163cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002164{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002165 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302167 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002168 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002169 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002170 continue;
2171
Jeff Laytone7ddee92008-11-14 13:44:38 -05002172 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302173 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002174 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002175 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302177 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return NULL;
2179}
2180
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002181void
2182cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002184 struct task_struct *task;
2185
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302186 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002187 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302188 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002191
Rob Landleyf1d0c992011-01-22 15:44:05 -06002192 put_net(cifs_net_ns(server));
2193
Jeff Laytone7ddee92008-11-14 13:44:38 -05002194 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302195 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002196
Jeff Laytonc74093b2011-01-11 07:24:23 -05002197 cancel_delayed_work_sync(&server->echo);
2198
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002199#ifdef CONFIG_CIFS_SMB2
2200 if (from_reconnect)
2201 /*
2202 * Avoid deadlock here: reconnect work calls
2203 * cifs_put_tcp_session() at its end. Need to be sure
2204 * that reconnect work does nothing with server pointer after
2205 * that step.
2206 */
2207 cancel_delayed_work(&server->reconnect);
2208 else
2209 cancel_delayed_work_sync(&server->reconnect);
2210#endif
2211
Jeff Laytone7ddee92008-11-14 13:44:38 -05002212 spin_lock(&GlobalMid_Lock);
2213 server->tcpStatus = CifsExiting;
2214 spin_unlock(&GlobalMid_Lock);
2215
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002216 cifs_crypto_shash_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302217 cifs_fscache_release_client_cookie(server);
2218
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002219 kfree(server->session_key.response);
2220 server->session_key.response = NULL;
2221 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002222
2223 task = xchg(&server->tsk, NULL);
2224 if (task)
Eric W. Biedermane038a1b62019-05-15 12:33:50 -05002225 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Jeff Layton63c038c2008-12-01 18:41:46 -05002228static struct TCP_Server_Info *
2229cifs_get_tcp_session(struct smb_vol *volume_info)
2230{
2231 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002232 int rc;
2233
Joe Perchesf96637b2013-05-04 22:12:25 -05002234 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002235
2236 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002237 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002238 if (tcp_ses)
2239 return tcp_ses;
2240
2241 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2242 if (!tcp_ses) {
2243 rc = -ENOMEM;
2244 goto out_err;
2245 }
2246
Jeff Layton23db65f2012-05-15 12:20:51 -04002247 tcp_ses->ops = volume_info->ops;
2248 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002249 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002250 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2251 if (IS_ERR(tcp_ses->hostname)) {
2252 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002253 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002254 }
2255
2256 tcp_ses->noblocksnd = volume_info->noblocksnd;
2257 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002258 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002259 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002260 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002261 init_waitqueue_head(&tcp_ses->response_q);
2262 init_waitqueue_head(&tcp_ses->request_q);
2263 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2264 mutex_init(&tcp_ses->srv_mutex);
2265 memcpy(tcp_ses->workstation_RFC1001_name,
2266 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2267 memcpy(tcp_ses->server_RFC1001_name,
2268 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002269 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002270 tcp_ses->sequence_number = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002271 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002272 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002273 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2274 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002275 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002276#ifdef CONFIG_CIFS_SMB2
2277 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2278 mutex_init(&tcp_ses->reconnect_mutex);
2279#endif
Jeff Layton9fa114f2012-11-26 11:09:57 -05002280 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2281 sizeof(tcp_ses->srcaddr));
2282 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2283 sizeof(tcp_ses->dstaddr));
Sachin Prabhu39552ea2014-05-13 00:48:12 +01002284#ifdef CONFIG_CIFS_SMB2
Steve Frenchfa70b872016-09-22 00:39:34 -05002285 generate_random_uuid(tcp_ses->client_guid);
Sachin Prabhu39552ea2014-05-13 00:48:12 +01002286#endif
Jeff Layton63c038c2008-12-01 18:41:46 -05002287 /*
2288 * at this point we are the only ones with the pointer
2289 * to the struct since the kernel thread not created yet
2290 * no need to spinlock this init of tcpStatus or srv_count
2291 */
2292 tcp_ses->tcpStatus = CifsNew;
2293 ++tcp_ses->srv_count;
2294
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002295 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2296 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2297 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2298 else
2299 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
2300
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002301 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002302 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002303 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002304 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002305 }
2306
2307 /*
2308 * since we're in a cifs function already, we know that
2309 * this will succeed. No need for try_module_get().
2310 */
2311 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002312 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002313 tcp_ses, "cifsd");
2314 if (IS_ERR(tcp_ses->tsk)) {
2315 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002316 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002317 module_put(THIS_MODULE);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002318 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002319 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002320 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002321
2322 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302323 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002324 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302325 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002326
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302327 cifs_fscache_get_client_cookie(tcp_ses);
2328
Jeff Laytonc74093b2011-01-11 07:24:23 -05002329 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002330 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002331
Jeff Layton63c038c2008-12-01 18:41:46 -05002332 return tcp_ses;
2333
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002334out_err_crypto_release:
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002335 cifs_crypto_shash_release(tcp_ses);
2336
Rob Landleyf1d0c992011-01-22 15:44:05 -06002337 put_net(cifs_net_ns(tcp_ses));
2338
Jeff Layton63c038c2008-12-01 18:41:46 -05002339out_err:
2340 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002341 if (!IS_ERR(tcp_ses->hostname))
2342 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002343 if (tcp_ses->ssocket)
2344 sock_release(tcp_ses->ssocket);
2345 kfree(tcp_ses);
2346 }
2347 return ERR_PTR(rc);
2348}
2349
Steve French96daf2b2011-05-27 04:34:02 +00002350static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002351{
Jeff Layton3f618222013-06-12 19:52:14 -05002352 if (vol->sectype != Unspecified &&
2353 vol->sectype != ses->sectype)
2354 return 0;
2355
2356 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002357 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002358 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002359 return 0;
2360 break;
2361 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002362 /* NULL username means anonymous session */
2363 if (ses->user_name == NULL) {
2364 if (!vol->nullauth)
2365 return 0;
2366 break;
2367 }
2368
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002369 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002370 if (strncmp(ses->user_name,
2371 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002372 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002373 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002374 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002375 ses->password != NULL &&
2376 strncmp(ses->password,
2377 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002378 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002379 return 0;
2380 }
2381 return 1;
2382}
2383
Steve French96daf2b2011-05-27 04:34:02 +00002384static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002385cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Steve French96daf2b2011-05-27 04:34:02 +00002387 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302389 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002390 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002391 if (ses->status == CifsExiting)
2392 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002393 if (!match_session(ses, vol))
2394 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002395 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302396 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002397 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302399 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return NULL;
2401}
2402
Jeff Layton14fbf502008-11-14 13:53:46 -05002403static void
Steve French96daf2b2011-05-27 04:34:02 +00002404cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002405{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002406 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002407 struct TCP_Server_Info *server = ses->server;
2408
Joe Perchesf96637b2013-05-04 22:12:25 -05002409 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002410
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302411 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002412 if (ses->status == CifsExiting) {
2413 spin_unlock(&cifs_tcp_ses_lock);
2414 return;
2415 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002416 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302417 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002418 return;
2419 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002420 if (ses->status == CifsGood)
2421 ses->status = CifsExiting;
2422 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002423
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002424 if (ses->status == CifsExiting && server->ops->logoff) {
2425 xid = get_xid();
2426 rc = server->ops->logoff(xid, ses);
2427 if (rc)
2428 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2429 __func__, rc);
2430 _free_xid(xid);
2431 }
2432
2433 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002434 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302435 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002436
Jeff Layton14fbf502008-11-14 13:53:46 -05002437 sesInfoFree(ses);
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002438 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002439}
2440
Jeff Layton8a8798a2012-01-17 16:09:15 -05002441#ifdef CONFIG_KEYS
2442
Chen Gang057d6332013-07-19 09:01:36 +08002443/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2444#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002445
2446/* Populate username and pw fields from keyring if possible */
2447static int
2448cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2449{
2450 int rc = 0;
Ronnie Sahlberg16764d72019-08-22 08:09:50 +10002451 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002452 const char *delim, *payload;
2453 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002454 ssize_t len;
2455 struct key *key;
2456 struct TCP_Server_Info *server = ses->server;
2457 struct sockaddr_in *sa;
2458 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002459 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002460
2461 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2462 if (!desc)
2463 return -ENOMEM;
2464
2465 /* try to find an address key first */
2466 switch (server->dstaddr.ss_family) {
2467 case AF_INET:
2468 sa = (struct sockaddr_in *)&server->dstaddr;
2469 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2470 break;
2471 case AF_INET6:
2472 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2473 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2474 break;
2475 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002476 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2477 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002478 rc = -EINVAL;
2479 goto out_err;
2480 }
2481
Joe Perchesf96637b2013-05-04 22:12:25 -05002482 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002483 key = request_key(&key_type_logon, desc, "");
2484 if (IS_ERR(key)) {
2485 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002486 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002487 rc = PTR_ERR(key);
2488 goto out_err;
2489 }
2490
2491 /* didn't work, try to find a domain key */
2492 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002493 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002494 key = request_key(&key_type_logon, desc, "");
2495 if (IS_ERR(key)) {
2496 rc = PTR_ERR(key);
2497 goto out_err;
2498 }
Ronnie Sahlberg16764d72019-08-22 08:09:50 +10002499 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002500 }
2501
2502 down_read(&key->sem);
David Howells146aa8b2015-10-21 14:04:48 +01002503 upayload = user_key_payload(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002504 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002505 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002506 goto out_key_put;
2507 }
2508
2509 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002510 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002511 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002512 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002513 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002514 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2515 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002516 rc = -EINVAL;
2517 goto out_key_put;
2518 }
2519
2520 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002521 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002522 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2523 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002524 rc = -EINVAL;
2525 goto out_key_put;
2526 }
2527
2528 vol->username = kstrndup(payload, len, GFP_KERNEL);
2529 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002530 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2531 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002532 rc = -ENOMEM;
2533 goto out_key_put;
2534 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002535 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002536
2537 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002538 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002539 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002540 rc = -EINVAL;
2541 kfree(vol->username);
2542 vol->username = NULL;
2543 goto out_key_put;
2544 }
2545
2546 ++delim;
2547 vol->password = kstrndup(delim, len, GFP_KERNEL);
2548 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002549 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2550 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002551 rc = -ENOMEM;
2552 kfree(vol->username);
2553 vol->username = NULL;
2554 goto out_key_put;
2555 }
2556
Ronnie Sahlberg16764d72019-08-22 08:09:50 +10002557 /*
2558 * If we have a domain key then we must set the domainName in the
2559 * for the request.
2560 */
2561 if (is_domain && ses->domainName) {
2562 vol->domainname = kstrndup(ses->domainName,
2563 strlen(ses->domainName),
2564 GFP_KERNEL);
2565 if (!vol->domainname) {
2566 cifs_dbg(FYI, "Unable to allocate %zd bytes for "
2567 "domain\n", len);
2568 rc = -ENOMEM;
2569 kfree(vol->username);
2570 vol->username = NULL;
Dan Carpenter3571c9b2019-08-27 13:59:17 +03002571 kzfree(vol->password);
Ronnie Sahlberg16764d72019-08-22 08:09:50 +10002572 vol->password = NULL;
2573 goto out_key_put;
2574 }
2575 }
2576
Jeff Layton8a8798a2012-01-17 16:09:15 -05002577out_key_put:
2578 up_read(&key->sem);
2579 key_put(key);
2580out_err:
2581 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002582 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002583 return rc;
2584}
2585#else /* ! CONFIG_KEYS */
2586static inline int
2587cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2588 struct cifs_ses *ses __attribute__((unused)))
2589{
2590 return -ENOSYS;
2591}
2592#endif /* CONFIG_KEYS */
2593
Steve French96daf2b2011-05-27 04:34:02 +00002594static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002595cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2596{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002597 int rc = -ENOMEM;
2598 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002599 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002600 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2601 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002602
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002603 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002604
Jeff Layton4ff67b72010-07-06 20:43:02 -04002605 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002606 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002607 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2608 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002609
Jeff Layton36988c72010-04-24 07:57:43 -04002610 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002611 rc = cifs_negotiate_protocol(xid, ses);
2612 if (rc) {
2613 mutex_unlock(&ses->session_mutex);
2614 /* problem -- put our ses reference */
2615 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002616 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002617 return ERR_PTR(rc);
2618 }
Jeff Layton36988c72010-04-24 07:57:43 -04002619 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002620 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002621 rc = cifs_setup_session(xid, ses,
2622 volume_info->local_nls);
2623 if (rc) {
2624 mutex_unlock(&ses->session_mutex);
2625 /* problem -- put our reference */
2626 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002627 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002628 return ERR_PTR(rc);
2629 }
2630 }
2631 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002632
2633 /* existing SMB ses has a server reference already */
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002634 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002635 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002636 return ses;
2637 }
2638
Joe Perchesf96637b2013-05-04 22:12:25 -05002639 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002640 ses = sesInfoAlloc();
2641 if (ses == NULL)
2642 goto get_ses_fail;
2643
2644 /* new SMB session uses our server ref */
2645 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002646 if (server->dstaddr.ss_family == AF_INET6)
2647 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002648 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002649 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002650
Steve French8727c8a2011-02-25 01:11:56 -06002651 if (volume_info->username) {
2652 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2653 if (!ses->user_name)
2654 goto get_ses_fail;
2655 }
Jeff Layton36988c72010-04-24 07:57:43 -04002656
2657 /* volume_info->password freed at unmount */
2658 if (volume_info->password) {
2659 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2660 if (!ses->password)
2661 goto get_ses_fail;
2662 }
2663 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002664 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2665 if (!ses->domainName)
2666 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002667 }
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002668 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002669 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002670
Jeff Layton28e11bd2013-05-26 07:01:00 -04002671 ses->sectype = volume_info->sectype;
2672 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002673
2674 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002675 rc = cifs_negotiate_protocol(xid, ses);
2676 if (!rc)
2677 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002678 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002679 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002680 goto get_ses_fail;
2681
2682 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302683 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002684 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302685 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002686
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002687 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002688 return ses;
2689
2690get_ses_fail:
2691 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002692 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002693 return ERR_PTR(rc);
2694}
2695
Steve French96daf2b2011-05-27 04:34:02 +00002696static int match_tcon(struct cifs_tcon *tcon, const char *unc)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002697{
2698 if (tcon->tidStatus == CifsExiting)
2699 return 0;
2700 if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
2701 return 0;
2702 return 1;
2703}
2704
Steve French96daf2b2011-05-27 04:34:02 +00002705static struct cifs_tcon *
2706cifs_find_tcon(struct cifs_ses *ses, const char *unc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
2708 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002709 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302711 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002712 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002713 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002714 if (!match_tcon(tcon, unc))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002715 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002716 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302717 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return tcon;
2719 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302720 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return NULL;
2722}
2723
Pavel Shilovsky48f95262016-11-04 11:50:31 -07002724void
Steve French96daf2b2011-05-27 04:34:02 +00002725cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002726{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002727 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002728 struct cifs_ses *ses = tcon->ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002729
Joe Perchesf96637b2013-05-04 22:12:25 -05002730 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302731 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002732 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302733 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002734 return;
2735 }
2736
2737 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302738 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002739
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002740 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002741 if (ses->server->ops->tree_disconnect)
2742 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002743 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002744
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302745 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002746 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002747 cifs_put_smb_ses(ses);
2748}
2749
Steve French96daf2b2011-05-27 04:34:02 +00002750static struct cifs_tcon *
2751cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002752{
2753 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002754 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002755
2756 tcon = cifs_find_tcon(ses, volume_info->UNC);
2757 if (tcon) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002758 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002759 /* existing tcon already has a reference */
2760 cifs_put_smb_ses(ses);
2761 if (tcon->seal != volume_info->seal)
Joe Perchesf96637b2013-05-04 22:12:25 -05002762 cifs_dbg(VFS, "transport encryption setting conflicts with existing tid\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002763 return tcon;
2764 }
2765
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002766 if (!ses->server->ops->tree_connect) {
2767 rc = -ENOSYS;
2768 goto out_fail;
2769 }
2770
Jeff Laytond00c28d2010-04-24 07:57:44 -04002771 tcon = tconInfoAlloc();
2772 if (tcon == NULL) {
2773 rc = -ENOMEM;
2774 goto out_fail;
2775 }
2776
2777 tcon->ses = ses;
2778 if (volume_info->password) {
2779 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
2780 if (!tcon->password) {
2781 rc = -ENOMEM;
2782 goto out_fail;
2783 }
2784 }
2785
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002786 /*
2787 * BB Do we need to wrap session_mutex around this TCon call and Unix
2788 * SetFS as we do on SessSetup and reconnect?
2789 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002790 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002791 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
2792 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002793 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05002794 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002795 if (rc)
2796 goto out_fail;
2797
2798 if (volume_info->nodfs) {
2799 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
Joe Perchesf96637b2013-05-04 22:12:25 -05002800 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002801 }
2802 tcon->seal = volume_info->seal;
Steve Frenchb618f002015-11-03 09:15:03 -06002803 tcon->use_persistent = false;
2804 /* check if SMB2 or later, CIFS does not support persistent handles */
2805 if (volume_info->persistent) {
2806 if (ses->server->vals->protocol_id == 0) {
2807 cifs_dbg(VFS,
2808 "SMB3 or later required for persistent handles\n");
2809 rc = -EOPNOTSUPP;
2810 goto out_fail;
Steve French592fafe2015-11-03 10:08:53 -06002811#ifdef CONFIG_CIFS_SMB2
Steve Frenchb618f002015-11-03 09:15:03 -06002812 } else if (ses->server->capabilities &
2813 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2814 tcon->use_persistent = true;
2815 else /* persistent handles requested but not supported */ {
2816 cifs_dbg(VFS,
2817 "Persistent handles not supported on share\n");
2818 rc = -EOPNOTSUPP;
2819 goto out_fail;
Steve French592fafe2015-11-03 10:08:53 -06002820#endif /* CONFIG_CIFS_SMB2 */
Steve Frenchb618f002015-11-03 09:15:03 -06002821 }
Steve French592fafe2015-11-03 10:08:53 -06002822#ifdef CONFIG_CIFS_SMB2
Steve Frenchb618f002015-11-03 09:15:03 -06002823 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2824 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2825 && (volume_info->nopersistent == false)) {
2826 cifs_dbg(FYI, "enabling persistent handles\n");
2827 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06002828#endif /* CONFIG_CIFS_SMB2 */
2829 } else if (volume_info->resilient) {
2830 if (ses->server->vals->protocol_id == 0) {
2831 cifs_dbg(VFS,
2832 "SMB2.1 or later required for resilient handles\n");
2833 rc = -EOPNOTSUPP;
2834 goto out_fail;
2835 }
2836 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06002837 }
2838
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002839 /*
2840 * We can have only one retry value for a connection to a share so for
2841 * resources mounted more than once to the same server share the last
2842 * value passed in for the retry flag is used.
2843 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04002844 tcon->retry = volume_info->retry;
2845 tcon->nocase = volume_info->nocase;
2846 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07002847 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002848
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302849 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002850 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302851 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002852
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302853 cifs_fscache_get_super_cookie(tcon);
2854
Jeff Laytond00c28d2010-04-24 07:57:44 -04002855 return tcon;
2856
2857out_fail:
2858 tconInfoFree(tcon);
2859 return ERR_PTR(rc);
2860}
2861
Jeff Layton9d002df2010-10-06 19:51:11 -04002862void
2863cifs_put_tlink(struct tcon_link *tlink)
2864{
2865 if (!tlink || IS_ERR(tlink))
2866 return;
2867
2868 if (!atomic_dec_and_test(&tlink->tl_count) ||
2869 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2870 tlink->tl_time = jiffies;
2871 return;
2872 }
2873
2874 if (!IS_ERR(tlink_tcon(tlink)))
2875 cifs_put_tcon(tlink_tcon(tlink));
2876 kfree(tlink);
2877 return;
2878}
Jeff Laytond00c28d2010-04-24 07:57:44 -04002879
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002880static inline struct tcon_link *
Pavel Shilovskycd518752011-06-09 12:58:53 +04002881cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
2882{
2883 return cifs_sb->master_tlink;
2884}
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002885
2886static int
2887compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2888{
2889 struct cifs_sb_info *old = CIFS_SB(sb);
2890 struct cifs_sb_info *new = mnt_data->cifs_sb;
2891
2892 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2893 return 0;
2894
2895 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
2896 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
2897 return 0;
2898
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002899 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002900 * We want to share sb only if we don't specify an r/wsize or
2901 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002902 */
2903 if (new->wsize && new->wsize < old->wsize)
2904 return 0;
2905
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002906 if (new->rsize && new->rsize < old->rsize)
2907 return 0;
2908
Eric W. Biederman1f682332013-02-06 01:20:20 -08002909 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002910 return 0;
2911
2912 if (old->mnt_file_mode != new->mnt_file_mode ||
2913 old->mnt_dir_mode != new->mnt_dir_mode)
2914 return 0;
2915
2916 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2917 return 0;
2918
2919 if (old->actimeo != new->actimeo)
2920 return 0;
2921
2922 return 1;
2923}
2924
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002925static int
2926match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2927{
2928 struct cifs_sb_info *old = CIFS_SB(sb);
2929 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhu4f5e1c42017-04-26 14:05:46 +01002930 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
2931 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002932
Sachin Prabhu4f5e1c42017-04-26 14:05:46 +01002933 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002934 return 1;
Sachin Prabhu4f5e1c42017-04-26 14:05:46 +01002935 else if (!old_set && !new_set)
2936 return 1;
2937
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002938 return 0;
2939}
2940
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002941int
2942cifs_match_super(struct super_block *sb, void *data)
2943{
2944 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
2945 struct smb_vol *volume_info;
2946 struct cifs_sb_info *cifs_sb;
2947 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00002948 struct cifs_ses *ses;
2949 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002950 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002951 int rc = 0;
2952
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002953 spin_lock(&cifs_tcp_ses_lock);
2954 cifs_sb = CIFS_SB(sb);
2955 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2956 if (IS_ERR(tlink)) {
2957 spin_unlock(&cifs_tcp_ses_lock);
2958 return rc;
2959 }
2960 tcon = tlink_tcon(tlink);
2961 ses = tcon->ses;
2962 tcp_srv = ses->server;
2963
2964 volume_info = mnt_data->vol;
2965
Jeff Layton9fa114f2012-11-26 11:09:57 -05002966 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002967 !match_session(ses, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002968 !match_tcon(tcon, volume_info->UNC) ||
2969 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002970 rc = 0;
2971 goto out;
2972 }
2973
2974 rc = compare_mount_options(sb, mnt_data);
2975out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002976 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04002977 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002978 return rc;
2979}
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04002982get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002983 const struct nls_table *nls_codepage, unsigned int *num_referrals,
2984 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 char *temp_unc;
2987 int rc = 0;
2988
Pavel Shilovskyb669f332012-05-27 20:21:53 +04002989 if (!ses->server->ops->tree_connect || !ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002990 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002992 *num_referrals = 0;
2993 *referrals = NULL;
2994
2995 if (ses->ipc_tid == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 temp_unc = kmalloc(2 /* for slashes */ +
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002997 strnlen(ses->serverName, SERVER_NAME_LEN_WITH_NULL * 2)
2998 + 1 + 4 /* slash IPC$ */ + 2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 if (temp_unc == NULL)
3000 return -ENOMEM;
3001 temp_unc[0] = '\\';
3002 temp_unc[1] = '\\';
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003003 strcpy(temp_unc + 2, ses->serverName);
3004 strcpy(temp_unc + 2 + strlen(ses->serverName), "\\IPC$");
3005 rc = ses->server->ops->tree_connect(xid, ses, temp_unc, NULL,
3006 nls_codepage);
Joe Perchesf96637b2013-05-04 22:12:25 -05003007 cifs_dbg(FYI, "Tcon rc = %d ipc_tid = %d\n", rc, ses->ipc_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 kfree(temp_unc);
3009 }
3010 if (rc == 0)
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003011 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3012 referrals, num_referrals,
3013 nls_codepage, remap);
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003014 /*
3015 * BB - map targetUNCs to dfs_info3 structures, here or in
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003016 * ses->server->ops->get_dfs_refer.
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019 return rc;
3020}
3021
Jeff Layton09e50d52008-07-23 10:11:19 -04003022#ifdef CONFIG_DEBUG_LOCK_ALLOC
3023static struct lock_class_key cifs_key[2];
3024static struct lock_class_key cifs_slock_key[2];
3025
3026static inline void
3027cifs_reclassify_socket4(struct socket *sock)
3028{
3029 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003030 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003031 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3032 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3033}
3034
3035static inline void
3036cifs_reclassify_socket6(struct socket *sock)
3037{
3038 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003039 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003040 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3041 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3042}
3043#else
3044static inline void
3045cifs_reclassify_socket4(struct socket *sock)
3046{
3047}
3048
3049static inline void
3050cifs_reclassify_socket6(struct socket *sock)
3051{
3052}
3053#endif
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003056static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Steve French50c2f752007-07-13 00:33:32 +00003058 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Steve French50c2f752007-07-13 00:33:32 +00003060 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 /* mask a nibble at a time and encode */
3062 target[j] = 'A' + (0x0F & (source[i] >> 4));
3063 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003064 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
3066
3067}
3068
Ben Greear3eb9a882010-09-01 17:06:02 -07003069static int
3070bind_socket(struct TCP_Server_Info *server)
3071{
3072 int rc = 0;
3073 if (server->srcaddr.ss_family != AF_UNSPEC) {
3074 /* Bind to the specified local IP address */
3075 struct socket *socket = server->ssocket;
3076 rc = socket->ops->bind(socket,
3077 (struct sockaddr *) &server->srcaddr,
3078 sizeof(server->srcaddr));
3079 if (rc < 0) {
3080 struct sockaddr_in *saddr4;
3081 struct sockaddr_in6 *saddr6;
3082 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3083 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3084 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003085 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3086 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003087 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003088 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3089 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003090 }
3091 }
3092 return rc;
3093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003096ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
3098 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003099 /*
3100 * some servers require RFC1001 sessinit before sending
3101 * negprot - BB check reconnection in case where second
3102 * sessinit is sent but no second negprot
3103 */
3104 struct rfc1002_session_packet *ses_init_buf;
3105 struct smb_hdr *smb_buf;
3106 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3107 GFP_KERNEL);
3108 if (ses_init_buf) {
3109 ses_init_buf->trailer.session_req.called_len = 32;
3110
Colin Ian King997152f2016-01-25 16:25:54 +00003111 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003112 rfc1002mangle(ses_init_buf->trailer.
3113 session_req.called_name,
3114 server->server_RFC1001_name,
3115 RFC1001_NAME_LEN_WITH_NULL);
3116 else
3117 rfc1002mangle(ses_init_buf->trailer.
3118 session_req.called_name,
3119 DEFAULT_CIFS_CALLED_NAME,
3120 RFC1001_NAME_LEN_WITH_NULL);
3121
3122 ses_init_buf->trailer.session_req.calling_len = 32;
3123
3124 /*
3125 * calling name ends in null (byte 16) from old smb
3126 * convention.
3127 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003128 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003129 rfc1002mangle(ses_init_buf->trailer.
3130 session_req.calling_name,
3131 server->workstation_RFC1001_name,
3132 RFC1001_NAME_LEN_WITH_NULL);
3133 else
3134 rfc1002mangle(ses_init_buf->trailer.
3135 session_req.calling_name,
3136 "LINUX_CIFS_CLNT",
3137 RFC1001_NAME_LEN_WITH_NULL);
3138
3139 ses_init_buf->trailer.session_req.scope1 = 0;
3140 ses_init_buf->trailer.session_req.scope2 = 0;
3141 smb_buf = (struct smb_hdr *)ses_init_buf;
3142
3143 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003144 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003145 rc = smb_send(server, smb_buf, 0x44);
3146 kfree(ses_init_buf);
3147 /*
3148 * RFC1001 layer in at least one server
3149 * requires very short break before negprot
3150 * presumably because not expecting negprot
3151 * to follow so fast. This is a simple
3152 * solution that works without
3153 * complicating the code and causes no
3154 * significant slowing down on mount
3155 * for everyone else
3156 */
3157 usleep_range(1000, 2000);
3158 }
3159 /*
3160 * else the negprot may still work without this
3161 * even though malloc failed
3162 */
3163
3164 return rc;
3165}
3166
3167static int
3168generic_ip_connect(struct TCP_Server_Info *server)
3169{
3170 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003171 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003172 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003173 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003174 struct sockaddr *saddr;
3175
3176 saddr = (struct sockaddr *) &server->dstaddr;
3177
3178 if (server->dstaddr.ss_family == AF_INET6) {
3179 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3180 slen = sizeof(struct sockaddr_in6);
3181 sfamily = AF_INET6;
3182 } else {
3183 sport = ((struct sockaddr_in *) saddr)->sin_port;
3184 slen = sizeof(struct sockaddr_in);
3185 sfamily = AF_INET;
3186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003188 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003189 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3190 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003192 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003193 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003196
3197 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003198 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003199 server->ssocket = socket;
3200 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003201 if (sfamily == AF_INET6)
3202 cifs_reclassify_socket6(socket);
3203 else
3204 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
3206
Ben Greear3eb9a882010-09-01 17:06:02 -07003207 rc = bind_socket(server);
3208 if (rc < 0)
3209 return rc;
3210
Jeff Laytond5c56052008-12-01 18:42:33 -05003211 /*
3212 * Eventually check for other socket options to change from
3213 * the default. sock_setsockopt not used because it expects
3214 * user space buffer
3215 */
3216 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003217 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003218
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003219 /* make the bufsizes depend on wsize/rsize and max requests */
3220 if (server->noautotune) {
3221 if (socket->sk->sk_sndbuf < (200 * 1024))
3222 socket->sk->sk_sndbuf = 200 * 1024;
3223 if (socket->sk->sk_rcvbuf < (140 * 1024))
3224 socket->sk->sk_rcvbuf = 140 * 1024;
3225 }
3226
Steve French6a5fa2362010-01-01 01:28:43 +00003227 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003228 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003229 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3230 (char *)&val, sizeof(val));
3231 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003232 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3233 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003234 }
3235
Joe Perchesf96637b2013-05-04 22:12:25 -05003236 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003237 socket->sk->sk_sndbuf,
3238 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3239
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003240 rc = socket->ops->connect(socket, saddr, slen, 0);
3241 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003242 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003243 sock_release(socket);
3244 server->ssocket = NULL;
3245 return rc;
3246 }
3247
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003248 if (sport == htons(RFC1001_PORT))
3249 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return rc;
3252}
3253
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003254static int
3255ip_connect(struct TCP_Server_Info *server)
3256{
Steve French6da97912011-03-13 18:55:55 +00003257 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003258 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3259 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3260
3261 if (server->dstaddr.ss_family == AF_INET6)
3262 sport = &addr6->sin6_port;
3263 else
3264 sport = &addr->sin_port;
3265
3266 if (*sport == 0) {
3267 int rc;
3268
3269 /* try with 445 port at first */
3270 *sport = htons(CIFS_PORT);
3271
3272 rc = generic_ip_connect(server);
3273 if (rc >= 0)
3274 return rc;
3275
3276 /* if it failed, try with 139 port */
3277 *sport = htons(RFC1001_PORT);
3278 }
3279
3280 return generic_ip_connect(server);
3281}
3282
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003283void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003284 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003285{
3286 /* if we are reconnecting then should we check to see if
3287 * any requested capabilities changed locally e.g. via
3288 * remount but we can not do much about it here
3289 * if they have (even if we could detect it by the following)
3290 * Perhaps we could add a backpointer to array of sb from tcon
3291 * or if we change to make all sb to same share the same
3292 * sb as NFS - then we only have one backpointer to sb.
3293 * What if we wanted to mount the server share twice once with
3294 * and once without posixacls or posix paths? */
3295 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003296
Steve Frenchc18c8422007-07-18 23:21:09 +00003297 if (vol_info && vol_info->no_linux_ext) {
3298 tcon->fsUnixInfo.Capability = 0;
3299 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003300 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003301 return;
3302 } else if (vol_info)
3303 tcon->unix_ext = 1; /* Unix Extensions supported */
3304
3305 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003306 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003307 return;
3308 }
Steve French50c2f752007-07-13 00:33:32 +00003309
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003310 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003311 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003312 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003313 /* check for reconnect case in which we do not
3314 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003315 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003316 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003317 originally at mount time */
3318 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3319 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003320 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3321 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003322 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003323 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003324 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003325 cifs_dbg(VFS, "possible reconnect error\n");
3326 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003327 }
Steve French8af18972007-02-14 04:42:51 +00003328 }
Steve French50c2f752007-07-13 00:33:32 +00003329
Steve French6848b732011-05-26 18:38:54 +00003330 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003331 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003332
Steve French8af18972007-02-14 04:42:51 +00003333 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003334 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003335 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003336 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003337 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003338 if (cifs_sb)
3339 cifs_sb->mnt_cifs_flags |=
3340 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003341 }
3342
Steve French75865f8c2007-06-24 18:30:48 +00003343 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003344 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003345 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003346 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003347 if (cifs_sb)
3348 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003349 CIFS_MOUNT_POSIX_PATHS;
3350 }
Steve French50c2f752007-07-13 00:33:32 +00003351
Joe Perchesf96637b2013-05-04 22:12:25 -05003352 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003353#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003354 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003355 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003356 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003357 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003358 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003359 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003360 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003361 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003362 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003363 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003364 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003365 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003366 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003367 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003368 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003369 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003370 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003371 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003372#endif /* CIFS_DEBUG2 */
3373 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003374 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003375 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003376 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003377 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 +00003378
Steve French8af18972007-02-14 04:42:51 +00003379 }
3380 }
3381}
3382
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003383int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003384 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003385{
Jeff Layton2de970f2010-10-06 19:51:12 -04003386 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3387
Al Viro2ced6f62011-06-17 09:20:04 -04003388 spin_lock_init(&cifs_sb->tlink_tree_lock);
3389 cifs_sb->tlink_tree = RB_ROOT;
3390
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003391 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003392 * Temporarily set r/wsize for matching superblock. If we end up using
3393 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003394 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003395 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003396 cifs_sb->wsize = pvolume_info->wsize;
3397
Steve French3b795212008-11-13 19:45:32 +00003398 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3399 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3400 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3401 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003402 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3403 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003404
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303405 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003406 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303407
Steve French3b795212008-11-13 19:45:32 +00003408 if (pvolume_info->noperm)
3409 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3410 if (pvolume_info->setuids)
3411 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003412 if (pvolume_info->setuidfromacl)
3413 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003414 if (pvolume_info->server_ino)
3415 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3416 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003417 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3418 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003419 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3420 if (pvolume_info->no_xattr)
3421 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3422 if (pvolume_info->sfu_emul)
3423 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3424 if (pvolume_info->nobrl)
3425 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve Frenchbe652442009-02-23 15:21:59 +00003426 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003427 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003428 if (pvolume_info->mand_lock)
3429 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003430 if (pvolume_info->rwpidforward)
3431 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003432 if (pvolume_info->cifs_acl)
3433 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003434 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003435 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003436 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3437 }
3438 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003439 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003440 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3441 }
Steve French3b795212008-11-13 19:45:32 +00003442 if (pvolume_info->override_uid)
3443 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3444 if (pvolume_info->override_gid)
3445 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3446 if (pvolume_info->dynperm)
3447 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303448 if (pvolume_info->fsc)
3449 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003450 if (pvolume_info->multiuser)
3451 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3452 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003453 if (pvolume_info->strict_io)
3454 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003455 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003456 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003457 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3458 }
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003459 if (pvolume_info->mfsymlinks) {
3460 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003461 /*
3462 * Our SFU ("Services for Unix" emulation does not allow
3463 * creating symlinks but does allow reading existing SFU
3464 * symlinks (it does allow both creating and reading SFU
3465 * style mknod and FIFOs though). When "mfsymlinks" and
3466 * "sfu" are both enabled at the same time, it allows
3467 * reading both types of symlinks, but will only create
3468 * them with mfsymlinks format. This allows better
3469 * Apple compatibility (probably better for Samba too)
3470 * while still recognizing old Windows style symlinks.
3471 */
3472 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003473 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003474 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003475 }
Steve French3b795212008-11-13 19:45:32 +00003476
3477 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003478 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003479
3480 if (pvolume_info->prepath) {
3481 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3482 if (cifs_sb->prepath == NULL)
3483 return -ENOMEM;
3484 }
3485
3486 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003487}
3488
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003489static void
3490cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003491{
Sean Finneyb9468452011-04-11 13:19:32 +00003492 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003493 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003494 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003495 kfree(volume_info->domainname);
3496 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003497 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003498}
3499
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003500void
3501cifs_cleanup_volume_info(struct smb_vol *volume_info)
3502{
3503 if (!volume_info)
3504 return;
3505 cleanup_volume_info_contents(volume_info);
3506 kfree(volume_info);
3507}
3508
3509
Steve French2d6d5892009-04-09 00:36:44 +00003510#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003511/*
3512 * cifs_build_path_to_root returns full path to root when we do not have an
3513 * exiting connection (tcon)
3514 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003515static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003516build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003517 const struct cifs_sb_info *cifs_sb)
3518{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003519 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003520 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003521 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003522
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003523 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003524 if (full_path == NULL)
3525 return ERR_PTR(-ENOMEM);
3526
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003527 strncpy(full_path, vol->UNC, unc_len);
3528 pos = full_path + unc_len;
3529
3530 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003531 *pos = CIFS_DIR_SEP(cifs_sb);
3532 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003533 pos += pplen;
3534 }
3535
3536 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003537 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003538 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003539 return full_path;
3540}
Sean Finneydd613942011-04-11 13:19:30 +00003541
3542/*
3543 * Perform a dfs referral query for a share and (optionally) prefix
3544 *
Sean Finney046462a2011-04-11 13:19:33 +00003545 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3546 * to a string containing updated options for the submount. Otherwise it
3547 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003548 *
3549 * Returns the rc from get_dfs_path to the caller, which can be used to
3550 * determine whether there were referrals.
3551 */
3552static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003553expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003554 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003555 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003556{
3557 int rc;
3558 unsigned int num_referrals = 0;
3559 struct dfs_info3_param *referrals = NULL;
3560 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3561
3562 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3563 if (IS_ERR(full_path))
3564 return PTR_ERR(full_path);
3565
3566 /* For DFS paths, skip the first '\' of the UNC */
3567 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3568
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003569 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003570 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003571
3572 if (!rc && num_referrals > 0) {
3573 char *fake_devname = NULL;
3574
3575 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3576 full_path + 1, referrals,
3577 &fake_devname);
3578
3579 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003580
Sean Finneydd613942011-04-11 13:19:30 +00003581 if (IS_ERR(mdata)) {
3582 rc = PTR_ERR(mdata);
3583 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003584 } else {
3585 cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003586 rc = cifs_setup_volume_info(volume_info, mdata,
3587 fake_devname);
Sean Finneydd613942011-04-11 13:19:30 +00003588 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003589 kfree(fake_devname);
3590 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003591 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003592 }
3593 kfree(full_path);
3594 return rc;
3595}
Steve French2d6d5892009-04-09 00:36:44 +00003596#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003597
Jeff Layton04db79b2011-07-06 08:10:38 -04003598static int
3599cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3600 const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003602 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003603
Jeff Layton04db79b2011-07-06 08:10:38 -04003604 if (cifs_parse_mount_options(mount_data, devname, volume_info))
3605 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Jeff Layton7586b762008-12-01 18:41:49 -05003607 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003608 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05003609 kfree(volume_info->username);
3610 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05003611 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05003613 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003615 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003616 /* In userspace mount helper we can get user name from alternate
3617 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003618 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
3620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05003622 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003623 /* load_nls_default cannot return null */
3624 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003626 volume_info->local_nls = load_nls(volume_info->iocharset);
3627 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003628 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003629 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04003630 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
3632 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003633
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003634 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04003635}
3636
3637struct smb_vol *
3638cifs_get_volume_info(char *mount_data, const char *devname)
3639{
3640 int rc;
3641 struct smb_vol *volume_info;
3642
Jeff Layton6ee95422012-11-26 11:09:57 -05003643 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04003644 if (!volume_info)
3645 return ERR_PTR(-ENOMEM);
3646
3647 rc = cifs_setup_volume_info(volume_info, mount_data, devname);
3648 if (rc) {
3649 cifs_cleanup_volume_info(volume_info);
3650 volume_info = ERR_PTR(rc);
3651 }
3652
3653 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003654}
3655
Aurelien Aptela6b50582016-05-25 19:59:09 +02003656static int
3657cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3658 unsigned int xid,
3659 struct cifs_tcon *tcon,
3660 struct cifs_sb_info *cifs_sb,
3661 char *full_path)
3662{
3663 int rc;
3664 char *s;
3665 char sep, tmp;
3666
3667 sep = CIFS_DIR_SEP(cifs_sb);
3668 s = full_path;
3669
3670 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3671 while (rc == 0) {
3672 /* skip separators */
3673 while (*s == sep)
3674 s++;
3675 if (!*s)
3676 break;
3677 /* next separator */
3678 while (*s && *s != sep)
3679 s++;
3680
3681 /*
3682 * temporarily null-terminate the path at the end of
3683 * the current component
3684 */
3685 tmp = *s;
3686 *s = 0;
3687 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3688 full_path);
3689 *s = tmp;
3690 }
3691 return rc;
3692}
3693
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003694int
Al Viro2c6292a2011-06-17 09:05:48 -04003695cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003696{
Jeff Layton1daaae82012-03-21 06:30:40 -04003697 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003698 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003699 struct cifs_ses *ses;
Steve French96daf2b2011-05-27 04:34:02 +00003700 struct cifs_tcon *tcon;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003701 struct TCP_Server_Info *server;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003702 char *full_path;
3703 struct tcon_link *tlink;
3704#ifdef CONFIG_CIFS_DFS_UPCALL
3705 int referral_walks_count = 0;
Jeff Layton20547492011-07-09 12:21:07 -04003706#endif
Al Virodd854462011-06-17 08:24:42 -04003707
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01003708 rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs");
Al Virodd854462011-06-17 08:24:42 -04003709 if (rc)
3710 return rc;
3711
Jeff Layton20547492011-07-09 12:21:07 -04003712#ifdef CONFIG_CIFS_DFS_UPCALL
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003713try_mount_again:
3714 /* cleanup activities if we're chasing a referral */
3715 if (referral_walks_count) {
3716 if (tcon)
3717 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003718 else if (ses)
3719 cifs_put_smb_ses(ses);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003720
Sachin Prabhu1dfd18d2015-06-16 16:36:17 +01003721 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3722
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003723 free_xid(xid);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003724 }
3725#endif
Jeff Layton1daaae82012-03-21 06:30:40 -04003726 rc = 0;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003727 tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003728 ses = NULL;
3729 server = NULL;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003730 full_path = NULL;
3731 tlink = NULL;
3732
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003733 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Jeff Layton63c038c2008-12-01 18:41:46 -05003735 /* get a reference to a tcp session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003736 server = cifs_get_tcp_session(volume_info);
3737 if (IS_ERR(server)) {
3738 rc = PTR_ERR(server);
Al Virodd854462011-06-17 08:24:42 -04003739 bdi_destroy(&cifs_sb->bdi);
Jeff Layton63c038c2008-12-01 18:41:46 -05003740 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 }
Steve French141891f2016-09-23 00:44:16 -05003742 if ((volume_info->max_credits < 20) ||
3743 (volume_info->max_credits > 60000))
3744 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3745 else
3746 server->max_credits = volume_info->max_credits;
Jeff Layton36988c72010-04-24 07:57:43 -04003747 /* get a reference to a SMB session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003748 ses = cifs_get_smb_ses(server, volume_info);
3749 if (IS_ERR(ses)) {
3750 rc = PTR_ERR(ses);
3751 ses = NULL;
Jeff Layton36988c72010-04-24 07:57:43 -04003752 goto mount_fail_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
Steve French50c2f752007-07-13 00:33:32 +00003754
Steve French592fafe2015-11-03 10:08:53 -06003755#ifdef CONFIG_CIFS_SMB2
Steve Frenchb618f002015-11-03 09:15:03 -06003756 if ((volume_info->persistent == true) && ((ses->server->capabilities &
3757 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
3758 cifs_dbg(VFS, "persistent handles not supported by server\n");
3759 rc = -EOPNOTSUPP;
3760 goto mount_fail_check;
3761 }
Steve French592fafe2015-11-03 10:08:53 -06003762#endif /* CONFIG_CIFS_SMB2*/
3763
Jeff Laytond00c28d2010-04-24 07:57:44 -04003764 /* search for existing tcon to this server share */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003765 tcon = cifs_get_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003766 if (IS_ERR(tcon)) {
3767 rc = PTR_ERR(tcon);
3768 tcon = NULL;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003769 goto remote_path_check;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003770 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003771
Steve French6848b732011-05-26 18:38:54 +00003772 /* tell server which Unix caps we support */
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003773 if (cap_unix(tcon->ses)) {
Steve French6848b732011-05-26 18:38:54 +00003774 /* reset of caps checks mount to see if unix extensions
3775 disabled for just this mount */
Al Viro2c6292a2011-06-17 09:05:48 -04003776 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
Steve French6848b732011-05-26 18:38:54 +00003777 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3778 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3779 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3780 rc = -EACCES;
3781 goto mount_fail_check;
3782 }
3783 } else
3784 tcon->unix_ext = 0; /* server does not support them */
3785
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003786 /* do not care if a following call succeed - informational */
3787 if (!tcon->ipc && server->ops->qfs_tcon)
3788 server->ops->qfs_tcon(xid, tcon);
Steve Frenchd82c2df2008-11-15 00:07:26 +00003789
Pavel Shilovsky24985c52012-09-18 16:20:28 -07003790 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
3791 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003792
Jeff Layton66bfaad2011-10-19 15:30:35 -04003793 /* tune readahead according to rsize */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003794 cifs_sb->bdi.ra_pages = cifs_sb->rsize / PAGE_SIZE;
Jeff Layton03ceace2010-12-06 21:07:33 -05003795
Igor Mammedove4cce942009-02-10 14:10:26 +03003796remote_path_check:
3797#ifdef CONFIG_CIFS_DFS_UPCALL
3798 /*
3799 * Perform an unconditional check for whether there are DFS
3800 * referrals for this path without prefix, to provide support
3801 * for DFS referrals from w2k8 servers which don't seem to respond
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003802 * with PATH_NOT_COVERED to requests that include the prefix.
3803 * Chase the referral if found, otherwise continue normally.
Steve Frenchd036f502009-04-03 03:12:08 +00003804 */
Igor Mammedov5c2503a2009-04-21 19:31:05 +04003805 if (referral_walks_count == 0) {
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003806 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
3807 false);
Steve Frencheeac8042006-01-13 21:34:58 -08003808 if (!refrc) {
Steve French4523cc32007-04-30 20:13:06 +00003809 referral_walks_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 goto try_mount_again;
3811 }
3812 }
3813#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003814
Steve Frenchf87d39d2011-05-27 03:50:55 +00003815 /* check if a whole path is not remote */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 if (!rc && tcon) {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003817 if (!server->ops->is_path_accessible) {
3818 rc = -ENOSYS;
3819 goto mount_fail_check;
3820 }
Steve French6d3ea7e2012-11-28 22:34:41 -06003821 /*
3822 * cifs_build_path_to_root works only when we have a valid tcon
3823 */
3824 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 if (full_path == NULL) {
3826 rc = -ENOMEM;
3827 goto mount_fail_check;
3828 }
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003829 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3830 full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 if (rc != 0 && rc != -EREMOTE) {
3832 kfree(full_path);
3833 goto mount_fail_check;
3834 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02003835
Sachin Prabhud1713562016-09-06 13:22:34 +01003836 if (rc != -EREMOTE) {
3837 rc = cifs_are_all_path_components_accessible(server,
Aurelien Aptela6b50582016-05-25 19:59:09 +02003838 xid, tcon, cifs_sb,
3839 full_path);
Sachin Prabhud1713562016-09-06 13:22:34 +01003840 if (rc != 0) {
3841 cifs_dbg(VFS, "cannot query dirs between root and final path, "
3842 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3843 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3844 rc = 0;
3845 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02003846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 kfree(full_path);
3848 }
3849
3850 /* get referral if needed */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003851 if (rc == -EREMOTE) {
3852#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov5c2503a2009-04-21 19:31:05 +04003853 if (referral_walks_count > MAX_NESTED_LINKS) {
3854 /*
3855 * BB: when we implement proper loop detection,
3856 * we will remove this check. But now we need it
3857 * to prevent an indefinite loop if 'DFS tree' is
3858 * misconfigured (i.e. has loops).
3859 */
3860 rc = -ELOOP;
3861 goto mount_fail_check;
3862 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003863
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003864 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
Jeff Layton7b91e262009-07-23 15:22:30 -04003865
Sean Finneydd613942011-04-11 13:19:30 +00003866 if (!rc) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04003867 referral_walks_count++;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003868 goto try_mount_again;
3869 }
Sean Finneydd613942011-04-11 13:19:30 +00003870 goto mount_fail_check;
Steve Frenchd036f502009-04-03 03:12:08 +00003871#else /* No DFS support, return error on mount */
3872 rc = -EOPNOTSUPP;
3873#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003874 }
3875
Jeff Layton9d002df2010-10-06 19:51:11 -04003876 if (rc)
3877 goto mount_fail_check;
3878
3879 /* now, hang the tcon off of the superblock */
3880 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
3881 if (tlink == NULL) {
3882 rc = -ENOMEM;
3883 goto mount_fail_check;
3884 }
3885
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003886 tlink->tl_uid = ses->linux_uid;
Jeff Layton9d002df2010-10-06 19:51:11 -04003887 tlink->tl_tcon = tcon;
3888 tlink->tl_time = jiffies;
3889 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3890 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3891
Jeff Layton413e6612010-10-28 13:33:38 -04003892 cifs_sb->master_tlink = tlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04003893 spin_lock(&cifs_sb->tlink_tree_lock);
3894 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3895 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton413e6612010-10-28 13:33:38 -04003896
Jeff Laytonda472fc2012-03-23 14:40:53 -04003897 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04003898 TLINK_IDLE_EXPIRE);
3899
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003900mount_fail_check:
3901 /* on error free sesinfo and tcon struct if needed */
3902 if (rc) {
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003903 /* If find_unc succeeded then rc == 0 so we can not end */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003904 /* up accidentally freeing someone elses tcon struct */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003905 if (tcon)
3906 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003907 else if (ses)
3908 cifs_put_smb_ses(ses);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003909 else
Pavel Shilovsky48f95262016-11-04 11:50:31 -07003910 cifs_put_tcp_session(server, 0);
Al Virodd854462011-06-17 08:24:42 -04003911 bdi_destroy(&cifs_sb->bdi);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003912 }
3913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003915 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 return rc;
3917}
3918
Jeff Layton8d1bca32011-06-11 21:17:10 -04003919/*
3920 * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
3921 * pointer may be NULL.
3922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003924CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00003925 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 const struct nls_table *nls_codepage)
3927{
3928 struct smb_hdr *smb_buffer;
3929 struct smb_hdr *smb_buffer_response;
3930 TCONX_REQ *pSMB;
3931 TCONX_RSP *pSMBr;
3932 unsigned char *bcc_ptr;
3933 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05003934 int length;
3935 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
3937 if (ses == NULL)
3938 return -EIO;
3939
3940 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00003941 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00003943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 smb_buffer_response = smb_buffer;
3945
3946 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3947 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003948
Pavel Shilovsky88257362012-05-23 14:01:59 +04003949 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 smb_buffer->Uid = ses->Suid;
3951 pSMB = (TCONX_REQ *) smb_buffer;
3952 pSMBr = (TCONX_RSP *) smb_buffer_response;
3953
3954 pSMB->AndXCommand = 0xFF;
3955 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 bcc_ptr = &pSMB->Password[0];
Jeff Layton8d1bca32011-06-11 21:17:10 -04003957 if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08003958 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003959 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003960 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003961 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003962 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06003963 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08003964 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
3965 specified as required (when that support is added to
3966 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00003967 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08003968 by Samba (not sure whether other servers allow
3969 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00003970#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04003971 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05003972 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05003973 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00003974 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05003975 SECMODE_PW_ENCRYPT ? true : false,
3976 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00003977 else
3978#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06003979 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05003980 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05003981 if (rc) {
3982 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
3983 __func__, rc);
3984 cifs_buf_release(smb_buffer);
3985 return rc;
3986 }
Steve Frencheeac8042006-01-13 21:34:58 -08003987
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06003988 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003989 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00003990 /* must align unicode strings */
3991 *bcc_ptr = 0; /* null byte password */
3992 bcc_ptr++;
3993 }
Steve Frencheeac8042006-01-13 21:34:58 -08003994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Jeff Layton38d77c52013-05-26 07:01:00 -04003996 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3998
3999 if (ses->capabilities & CAP_STATUS32) {
4000 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4001 }
4002 if (ses->capabilities & CAP_DFS) {
4003 smb_buffer->Flags2 |= SMBFLG2_DFS;
4004 }
4005 if (ses->capabilities & CAP_UNICODE) {
4006 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4007 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004008 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004009 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004010 (/* server len*/ + 256 /* share len */), nls_codepage);
4011 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 bcc_ptr += 2; /* skip trailing null */
4013 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 strcpy(bcc_ptr, tree);
4015 bcc_ptr += strlen(tree) + 1;
4016 }
4017 strcpy(bcc_ptr, "?????");
4018 bcc_ptr += strlen("?????");
4019 bcc_ptr += 1;
4020 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004021 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4022 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 pSMB->ByteCount = cpu_to_le16(count);
4024
Steve French133672e2007-11-13 22:41:37 +00004025 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004026 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 /* above now done in SendReceive */
4029 if ((rc == 0) && (tcon != NULL)) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004030 bool is_unicode;
4031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004033 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 tcon->tid = smb_buffer_response->Tid;
4035 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004036 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004037 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004038 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4039 is_unicode = true;
4040 else
4041 is_unicode = false;
4042
Jeff Laytoncc20c032009-04-30 07:16:21 -04004043
Steve French50c2f752007-07-13 00:33:32 +00004044 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004045 if (length == 3) {
4046 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4047 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004048 cifs_dbg(FYI, "IPC connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004049 tcon->ipc = 1;
4050 }
4051 } else if (length == 2) {
4052 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4053 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004054 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004055 }
4056 }
Steve French50c2f752007-07-13 00:33:32 +00004057 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004058 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004059 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004060
4061 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004062 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004063 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004064 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004065 nls_codepage);
4066
Joe Perchesf96637b2013-05-04 22:12:25 -05004067 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004068
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004069 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004070 (smb_buffer_response->WordCount == 7))
4071 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004072 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4073 else
4074 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004075 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 } else if ((rc == 0) && tcon == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00004077 /* all we need to save for IPC$ connection */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 ses->ipc_tid = smb_buffer_response->Tid;
4079 }
4080
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004081 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 return rc;
4083}
4084
Al Viro2e32cf52013-10-03 12:53:37 -04004085static void delayed_free(struct rcu_head *p)
4086{
4087 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4088 unload_nls(sbi->local_nls);
4089 kfree(sbi);
4090}
4091
Al Viro2a9b9952011-06-17 09:27:16 -04004092void
4093cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
Jeff Laytonb647c352010-10-28 11:16:44 -04004095 struct rb_root *root = &cifs_sb->tlink_tree;
4096 struct rb_node *node;
4097 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Jeff Layton2de970f2010-10-06 19:51:12 -04004099 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4100
Jeff Laytonb647c352010-10-28 11:16:44 -04004101 spin_lock(&cifs_sb->tlink_tree_lock);
4102 while ((node = rb_first(root))) {
4103 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4104 cifs_get_tlink(tlink);
4105 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4106 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004107
Jeff Laytonb647c352010-10-28 11:16:44 -04004108 spin_unlock(&cifs_sb->tlink_tree_lock);
4109 cifs_put_tlink(tlink);
4110 spin_lock(&cifs_sb->tlink_tree_lock);
4111 }
4112 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004113
Al Virodd854462011-06-17 08:24:42 -04004114 bdi_destroy(&cifs_sb->bdi);
Al Virod757d712011-06-17 09:42:43 -04004115 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004116 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004117 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004120int
4121cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
4123 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004124 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004126 if (!server->ops->need_neg || !server->ops->negotiate)
4127 return -ENOSYS;
4128
Jeff Layton198b5682010-04-24 07:57:48 -04004129 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004130 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Pavel Shilovsky45275782012-05-17 17:53:29 +04004133 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004134
4135 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004136 if (rc == 0) {
4137 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004138 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004139 server->tcpStatus = CifsGood;
4140 else
4141 rc = -EHOSTDOWN;
4142 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 }
Steve French26b994f2008-08-06 05:11:33 +00004144
Jeff Layton198b5682010-04-24 07:57:48 -04004145 return rc;
4146}
Steve French26b994f2008-08-06 05:11:33 +00004147
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004148int
4149cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4150 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004151{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004152 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004153 struct TCP_Server_Info *server = ses->server;
4154
Jeff Layton198b5682010-04-24 07:57:48 -04004155 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004156 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004157 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004158
Joe Perchesf96637b2013-05-04 22:12:25 -05004159 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004160 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004161
Shu Wangb6a77c72017-09-08 18:48:33 +08004162 if (ses->auth_key.response) {
4163 cifs_dbg(VFS, "Free previous auth_key.response = %p\n",
4164 ses->auth_key.response);
4165 kfree(ses->auth_key.response);
4166 ses->auth_key.response = NULL;
4167 ses->auth_key.len = 0;
4168 }
4169
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004170 if (server->ops->sess_setup)
4171 rc = server->ops->sess_setup(xid, ses, nls_info);
4172
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004173 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004174 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return rc;
4177}
4178
Jeff Layton8a8798a2012-01-17 16:09:15 -05004179static int
4180cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4181{
Jeff Layton3f618222013-06-12 19:52:14 -05004182 vol->sectype = ses->sectype;
4183
4184 /* krb5 is special, since we don't need username or pw */
4185 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004186 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004187
4188 return cifs_set_cifscreds(vol, ses);
4189}
4190
Steve French96daf2b2011-05-27 04:34:02 +00004191static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004192cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004193{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004194 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004195 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4196 struct cifs_ses *ses;
4197 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004198 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004199
4200 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004201 if (vol_info == NULL)
4202 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004203
Jeff Layton9d002df2010-10-06 19:51:11 -04004204 vol_info->local_nls = cifs_sb->local_nls;
4205 vol_info->linux_uid = fsuid;
4206 vol_info->cred_uid = fsuid;
4207 vol_info->UNC = master_tcon->treeName;
4208 vol_info->retry = master_tcon->retry;
4209 vol_info->nocase = master_tcon->nocase;
4210 vol_info->local_lease = master_tcon->local_lease;
4211 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004212 vol_info->sectype = master_tcon->ses->sectype;
4213 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004214
Jeff Layton8a8798a2012-01-17 16:09:15 -05004215 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4216 if (rc) {
4217 tcon = ERR_PTR(rc);
4218 goto out;
4219 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004220
4221 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304222 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004223 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304224 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004225
4226 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4227 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004228 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky48f95262016-11-04 11:50:31 -07004229 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004230 goto out;
4231 }
4232
4233 tcon = cifs_get_tcon(ses, vol_info);
4234 if (IS_ERR(tcon)) {
4235 cifs_put_smb_ses(ses);
4236 goto out;
4237 }
4238
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004239 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004240 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4241out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004242 kfree(vol_info->username);
Aurelien Aptel7e689162018-01-25 15:59:39 +01004243 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004244 kfree(vol_info);
4245
4246 return tcon;
4247}
4248
Steve French96daf2b2011-05-27 04:34:02 +00004249struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004250cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4251{
4252 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4253}
4254
Jeff Laytonb647c352010-10-28 11:16:44 -04004255/* find and return a tlink with given uid */
4256static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004257tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004258{
4259 struct rb_node *node = root->rb_node;
4260 struct tcon_link *tlink;
4261
4262 while (node) {
4263 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4264
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004265 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004266 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004267 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004268 node = node->rb_right;
4269 else
4270 return tlink;
4271 }
4272 return NULL;
4273}
4274
4275/* insert a tcon_link into the tree */
4276static void
4277tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4278{
4279 struct rb_node **new = &(root->rb_node), *parent = NULL;
4280 struct tcon_link *tlink;
4281
4282 while (*new) {
4283 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4284 parent = *new;
4285
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004286 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004287 new = &((*new)->rb_left);
4288 else
4289 new = &((*new)->rb_right);
4290 }
4291
4292 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4293 rb_insert_color(&new_tlink->tl_rbnode, root);
4294}
4295
Jeff Layton9d002df2010-10-06 19:51:11 -04004296/*
4297 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4298 * current task.
4299 *
4300 * If the superblock doesn't refer to a multiuser mount, then just return
4301 * the master tcon for the mount.
4302 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304303 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004304 * exists, then check to see if it's pending construction. If it is then wait
4305 * for construction to complete. Once it's no longer pending, check to see if
4306 * it failed and either return an error or retry construction, depending on
4307 * the timeout.
4308 *
4309 * If one doesn't exist then insert a new tcon_link struct into the tree and
4310 * try to construct a new one.
4311 */
4312struct tcon_link *
4313cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4314{
4315 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004316 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004317 struct tcon_link *tlink, *newtlink;
4318
4319 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4320 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4321
4322 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004323 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004324 if (tlink)
4325 cifs_get_tlink(tlink);
4326 spin_unlock(&cifs_sb->tlink_tree_lock);
4327
4328 if (tlink == NULL) {
4329 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4330 if (newtlink == NULL)
4331 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004332 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004333 newtlink->tl_tcon = ERR_PTR(-EACCES);
4334 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4335 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4336 cifs_get_tlink(newtlink);
4337
Jeff Layton9d002df2010-10-06 19:51:11 -04004338 spin_lock(&cifs_sb->tlink_tree_lock);
4339 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004340 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004341 if (tlink) {
4342 cifs_get_tlink(tlink);
4343 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004344 kfree(newtlink);
4345 goto wait_for_construction;
4346 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004347 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004348 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4349 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004350 } else {
4351wait_for_construction:
4352 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004353 TASK_INTERRUPTIBLE);
4354 if (ret) {
4355 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004356 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004357 }
4358
4359 /* if it's good, return it */
4360 if (!IS_ERR(tlink->tl_tcon))
4361 return tlink;
4362
4363 /* return error if we tried this already recently */
4364 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4365 cifs_put_tlink(tlink);
4366 return ERR_PTR(-EACCES);
4367 }
4368
4369 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4370 goto wait_for_construction;
4371 }
4372
4373 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4374 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4375 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4376
4377 if (IS_ERR(tlink->tl_tcon)) {
4378 cifs_put_tlink(tlink);
4379 return ERR_PTR(-EACCES);
4380 }
4381
4382 return tlink;
4383}
Jeff Layton2de970f2010-10-06 19:51:12 -04004384
4385/*
4386 * periodic workqueue job that scans tcon_tree for a superblock and closes
4387 * out tcons.
4388 */
4389static void
4390cifs_prune_tlinks(struct work_struct *work)
4391{
4392 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4393 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004394 struct rb_root *root = &cifs_sb->tlink_tree;
4395 struct rb_node *node = rb_first(root);
4396 struct rb_node *tmp;
4397 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004398
Jeff Laytonb647c352010-10-28 11:16:44 -04004399 /*
4400 * Because we drop the spinlock in the loop in order to put the tlink
4401 * it's not guarded against removal of links from the tree. The only
4402 * places that remove entries from the tree are this function and
4403 * umounts. Because this function is non-reentrant and is canceled
4404 * before umount can proceed, this is safe.
4405 */
4406 spin_lock(&cifs_sb->tlink_tree_lock);
4407 node = rb_first(root);
4408 while (node != NULL) {
4409 tmp = node;
4410 node = rb_next(tmp);
4411 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4412
4413 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4414 atomic_read(&tlink->tl_count) != 0 ||
4415 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4416 continue;
4417
4418 cifs_get_tlink(tlink);
4419 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4420 rb_erase(tmp, root);
4421
Jeff Layton2de970f2010-10-06 19:51:12 -04004422 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004423 cifs_put_tlink(tlink);
4424 spin_lock(&cifs_sb->tlink_tree_lock);
4425 }
4426 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004427
Jeff Laytonda472fc2012-03-23 14:40:53 -04004428 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004429 TLINK_IDLE_EXPIRE);
4430}