cifs: Make sure all data pages are signed correctly
[linux-2.6-block.git] / fs / cifs / connect.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/connect.c
3 *
1080ef75 4 * Copyright (C) International Business Machines Corp., 2002,2011
1da177e4
LT
5 * 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
fb8c4b14 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1da177e4
LT
20 */
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
3f07c014 24#include <linux/sched/signal.h>
1da177e4
LT
25#include <linux/list.h>
26#include <linux/wait.h>
5a0e3ad6 27#include <linux/slab.h>
1da177e4
LT
28#include <linux/pagemap.h>
29#include <linux/ctype.h>
30#include <linux/utsname.h>
31#include <linux/mempool.h>
b8643e1b 32#include <linux/delay.h>
f191401f 33#include <linux/completion.h>
aaf737ad 34#include <linux/kthread.h>
0ae0efad 35#include <linux/pagevec.h>
7dfb7103 36#include <linux/freezer.h>
5c2503a8 37#include <linux/namei.h>
c6e970a0 38#include <linux/uuid.h>
7c0f6ba6 39#include <linux/uaccess.h>
1da177e4 40#include <asm/processor.h>
50b64e3b 41#include <linux/inet.h>
143cb494 42#include <linux/module.h>
8a8798a5 43#include <keys/user-type.h>
0e2bedaa 44#include <net/ipv6.h>
8830d7e0 45#include <linux/parser.h>
2f8b5444 46#include <linux/bvec.h>
1da177e4
LT
47#include "cifspdu.h"
48#include "cifsglob.h"
49#include "cifsproto.h"
50#include "cifs_unicode.h"
51#include "cifs_debug.h"
52#include "cifs_fs_sb.h"
53#include "ntlmssp.h"
54#include "nterr.h"
55#include "rfc1002pdu.h"
488f1d2d 56#include "fscache.h"
53e0e11e 57#include "smb2proto.h"
2f894646 58#include "smbdirect.h"
1da177e4 59
1da177e4 60extern mempool_t *cifs_req_poolp;
f92a720e 61extern bool disable_legacy_dialects;
1da177e4 62
2de970ff 63/* FIXME: should these be tunable? */
9d002df4 64#define TLINK_ERROR_EXPIRE (1 * HZ)
2de970ff 65#define TLINK_IDLE_EXPIRE (600 * HZ)
9d002df4 66
8830d7e0 67enum {
8830d7e0
SP
68 /* Mount options that take no arguments */
69 Opt_user_xattr, Opt_nouser_xattr,
70 Opt_forceuid, Opt_noforceuid,
72bd481f 71 Opt_forcegid, Opt_noforcegid,
8830d7e0
SP
72 Opt_noblocksend, Opt_noautotune,
73 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
2baa2682 74 Opt_mapposix, Opt_nomapposix,
8830d7e0
SP
75 Opt_mapchars, Opt_nomapchars, Opt_sfu,
76 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
b326614e 77 Opt_noposixpaths, Opt_nounix, Opt_unix,
8830d7e0
SP
78 Opt_nocase,
79 Opt_brl, Opt_nobrl,
3d4ef9a1 80 Opt_handlecache, Opt_nohandlecache,
95932655 81 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
8830d7e0
SP
82 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
83 Opt_nohard, Opt_nosoft,
84 Opt_nointr, Opt_intr,
85 Opt_nostrictsync, Opt_strictsync,
86 Opt_serverino, Opt_noserverino,
87 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
88 Opt_acl, Opt_noacl, Opt_locallease,
1b359204 89 Opt_sign, Opt_seal, Opt_noac,
8830d7e0 90 Opt_fsc, Opt_mfsymlinks,
a0b3df5c 91 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
b2a30774 92 Opt_persistent, Opt_nopersistent,
592fafe6 93 Opt_resilient, Opt_noresilient,
8339dd32 94 Opt_domainauto, Opt_rdma,
8830d7e0
SP
95
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,
141891f4 101 Opt_echo_interval, Opt_max_credits,
8b217fe7 102 Opt_snapshot,
8830d7e0
SP
103
104 /* Mount options which take string value */
105 Opt_user, Opt_pass, Opt_ip,
73a999fa 106 Opt_domain, Opt_srcaddr, Opt_iocharset,
8830d7e0 107 Opt_netbiosname, Opt_servern,
23db65f5 108 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
8830d7e0
SP
109
110 /* Mount options to be ignored */
111 Opt_ignore,
112
113 /* Options which could be blank */
114 Opt_blank_pass,
4fe9e963
SP
115 Opt_blank_user,
116 Opt_blank_ip,
8830d7e0
SP
117
118 Opt_err
119};
120
121static const match_table_t cifs_mount_option_tokens = {
122
123 { Opt_user_xattr, "user_xattr" },
124 { Opt_nouser_xattr, "nouser_xattr" },
125 { Opt_forceuid, "forceuid" },
126 { Opt_noforceuid, "noforceuid" },
72bd481f
JL
127 { Opt_forcegid, "forcegid" },
128 { Opt_noforcegid, "noforcegid" },
8830d7e0
SP
129 { Opt_noblocksend, "noblocksend" },
130 { Opt_noautotune, "noautotune" },
131 { Opt_hard, "hard" },
132 { Opt_soft, "soft" },
133 { Opt_perm, "perm" },
134 { Opt_noperm, "noperm" },
2baa2682 135 { Opt_mapchars, "mapchars" }, /* SFU style */
8830d7e0 136 { Opt_nomapchars, "nomapchars" },
2baa2682
SF
137 { Opt_mapposix, "mapposix" }, /* SFM style */
138 { Opt_nomapposix, "nomapposix" },
8830d7e0
SP
139 { Opt_sfu, "sfu" },
140 { Opt_nosfu, "nosfu" },
141 { Opt_nodfs, "nodfs" },
142 { Opt_posixpaths, "posixpaths" },
143 { Opt_noposixpaths, "noposixpaths" },
144 { Opt_nounix, "nounix" },
145 { Opt_nounix, "nolinux" },
b326614e
SF
146 { Opt_nounix, "noposix" },
147 { Opt_unix, "unix" },
148 { Opt_unix, "linux" },
149 { Opt_unix, "posix" },
8830d7e0
SP
150 { Opt_nocase, "nocase" },
151 { Opt_nocase, "ignorecase" },
152 { Opt_brl, "brl" },
153 { Opt_nobrl, "nobrl" },
3d4ef9a1
SF
154 { Opt_handlecache, "handlecache" },
155 { Opt_nohandlecache, "nohandlecache" },
8830d7e0
SP
156 { Opt_nobrl, "nolock" },
157 { Opt_forcemandatorylock, "forcemandatorylock" },
5cfdddcf 158 { Opt_forcemandatorylock, "forcemand" },
8830d7e0
SP
159 { Opt_setuids, "setuids" },
160 { Opt_nosetuids, "nosetuids" },
95932655 161 { Opt_setuidfromacl, "idsfromsid" },
8830d7e0
SP
162 { Opt_dynperm, "dynperm" },
163 { Opt_nodynperm, "nodynperm" },
164 { Opt_nohard, "nohard" },
165 { Opt_nosoft, "nosoft" },
166 { Opt_nointr, "nointr" },
167 { Opt_intr, "intr" },
168 { Opt_nostrictsync, "nostrictsync" },
169 { Opt_strictsync, "strictsync" },
170 { Opt_serverino, "serverino" },
171 { Opt_noserverino, "noserverino" },
172 { Opt_rwpidforward, "rwpidforward" },
173 { Opt_cifsacl, "cifsacl" },
174 { Opt_nocifsacl, "nocifsacl" },
175 { Opt_acl, "acl" },
176 { Opt_noacl, "noacl" },
177 { Opt_locallease, "locallease" },
178 { Opt_sign, "sign" },
179 { Opt_seal, "seal" },
8830d7e0
SP
180 { Opt_noac, "noac" },
181 { Opt_fsc, "fsc" },
182 { Opt_mfsymlinks, "mfsymlinks" },
183 { Opt_multiuser, "multiuser" },
d8162558 184 { Opt_sloppy, "sloppy" },
a0b3df5c 185 { Opt_nosharesock, "nosharesock" },
b2a30774
SF
186 { Opt_persistent, "persistenthandles"},
187 { Opt_nopersistent, "nopersistenthandles"},
592fafe6
SF
188 { Opt_resilient, "resilienthandles"},
189 { Opt_noresilient, "noresilienthandles"},
39566443 190 { Opt_domainauto, "domainauto"},
8339dd32 191 { Opt_rdma, "rdma"},
8830d7e0
SP
192
193 { Opt_backupuid, "backupuid=%s" },
194 { Opt_backupgid, "backupgid=%s" },
195 { Opt_uid, "uid=%s" },
196 { Opt_cruid, "cruid=%s" },
197 { Opt_gid, "gid=%s" },
198 { Opt_file_mode, "file_mode=%s" },
199 { Opt_dirmode, "dirmode=%s" },
200 { Opt_dirmode, "dir_mode=%s" },
201 { Opt_port, "port=%s" },
202 { Opt_rsize, "rsize=%s" },
203 { Opt_wsize, "wsize=%s" },
204 { Opt_actimeo, "actimeo=%s" },
adfeb3e0 205 { Opt_echo_interval, "echo_interval=%s" },
141891f4 206 { Opt_max_credits, "max_credits=%s" },
8b217fe7 207 { Opt_snapshot, "snapshot=%s" },
8830d7e0 208
4fe9e963
SP
209 { Opt_blank_user, "user=" },
210 { Opt_blank_user, "username=" },
8830d7e0
SP
211 { Opt_user, "user=%s" },
212 { Opt_user, "username=%s" },
213 { Opt_blank_pass, "pass=" },
3c15b4cf 214 { Opt_blank_pass, "password=" },
8830d7e0
SP
215 { Opt_pass, "pass=%s" },
216 { Opt_pass, "password=%s" },
4fe9e963
SP
217 { Opt_blank_ip, "ip=" },
218 { Opt_blank_ip, "addr=" },
8830d7e0
SP
219 { Opt_ip, "ip=%s" },
220 { Opt_ip, "addr=%s" },
73a999fa
JL
221 { Opt_ignore, "unc=%s" },
222 { Opt_ignore, "target=%s" },
223 { Opt_ignore, "path=%s" },
8830d7e0
SP
224 { Opt_domain, "dom=%s" },
225 { Opt_domain, "domain=%s" },
226 { Opt_domain, "workgroup=%s" },
227 { Opt_srcaddr, "srcaddr=%s" },
73a999fa 228 { Opt_ignore, "prefixpath=%s" },
8830d7e0 229 { Opt_iocharset, "iocharset=%s" },
8830d7e0
SP
230 { Opt_netbiosname, "netbiosname=%s" },
231 { Opt_servern, "servern=%s" },
232 { Opt_ver, "ver=%s" },
23db65f5 233 { Opt_vers, "vers=%s" },
8830d7e0 234 { Opt_sec, "sec=%s" },
15b6a473 235 { Opt_cache, "cache=%s" },
8830d7e0
SP
236
237 { Opt_ignore, "cred" },
238 { Opt_ignore, "credentials" },
a557b976
JL
239 { Opt_ignore, "cred=%s" },
240 { Opt_ignore, "credentials=%s" },
8830d7e0
SP
241 { Opt_ignore, "guest" },
242 { Opt_ignore, "rw" },
243 { Opt_ignore, "ro" },
244 { Opt_ignore, "suid" },
245 { Opt_ignore, "nosuid" },
246 { Opt_ignore, "exec" },
247 { Opt_ignore, "noexec" },
248 { Opt_ignore, "nodev" },
249 { Opt_ignore, "noauto" },
250 { Opt_ignore, "dev" },
251 { Opt_ignore, "mand" },
252 { Opt_ignore, "nomand" },
253 { Opt_ignore, "_netdev" },
254
255 { Opt_err, NULL }
256};
257
258enum {
259 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
260 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
7659624f
JL
261 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
262 Opt_sec_ntlmv2i, Opt_sec_lanman,
8830d7e0
SP
263 Opt_sec_none,
264
265 Opt_sec_err
266};
267
268static const match_table_t cifs_secflavor_tokens = {
269 { Opt_sec_krb5, "krb5" },
270 { Opt_sec_krb5i, "krb5i" },
271 { Opt_sec_krb5p, "krb5p" },
272 { Opt_sec_ntlmsspi, "ntlmsspi" },
273 { Opt_sec_ntlmssp, "ntlmssp" },
274 { Opt_ntlm, "ntlm" },
275 { Opt_sec_ntlmi, "ntlmi" },
7659624f
JL
276 { Opt_sec_ntlmv2, "nontlm" },
277 { Opt_sec_ntlmv2, "ntlmv2" },
8830d7e0 278 { Opt_sec_ntlmv2i, "ntlmv2i" },
8830d7e0
SP
279 { Opt_sec_lanman, "lanman" },
280 { Opt_sec_none, "none" },
281
282 { Opt_sec_err, NULL }
283};
284
15b6a473
JL
285/* cache flavors */
286enum {
287 Opt_cache_loose,
288 Opt_cache_strict,
289 Opt_cache_none,
290 Opt_cache_err
291};
292
293static const match_table_t cifs_cacheflavor_tokens = {
294 { Opt_cache_loose, "loose" },
295 { Opt_cache_strict, "strict" },
296 { Opt_cache_none, "none" },
297 { Opt_cache_err, NULL }
298};
299
23db65f5
JL
300static const match_table_t cifs_smb_version_tokens = {
301 { Smb_1, SMB1_VERSION_STRING },
dd446b16 302 { Smb_20, SMB20_VERSION_STRING},
1080ef75 303 { Smb_21, SMB21_VERSION_STRING },
e4aa25e7 304 { Smb_30, SMB30_VERSION_STRING },
20b6d8b4 305 { Smb_302, SMB302_VERSION_STRING },
5f7fbf73
SF
306#ifdef CONFIG_CIFS_SMB311
307 { Smb_311, SMB311_VERSION_STRING },
aab1893d 308 { Smb_311, ALT_SMB311_VERSION_STRING },
5f7fbf73 309#endif /* SMB311 */
9764c02f
SF
310 { Smb_3any, SMB3ANY_VERSION_STRING },
311 { Smb_default, SMBDEFAULT_VERSION_STRING },
5f7fbf73 312 { Smb_version_err, NULL }
23db65f5
JL
313};
314
a9f1b85e
PS
315static int ip_connect(struct TCP_Server_Info *server);
316static int generic_ip_connect(struct TCP_Server_Info *server);
b647c35f 317static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
2de970ff 318static void cifs_prune_tlinks(struct work_struct *work);
b9bce2e9 319static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
c7c137b9 320 const char *devname, bool is_smb3);
1da177e4 321
d5c5605c
JL
322/*
323 * cifs tcp session reconnection
324 *
325 * mark tcp session as reconnecting so temporarily locked
326 * mark all smb sessions as reconnecting for tcp session
327 * reconnect tcp session
328 * wake up waiters on reconnection? - (not needed currently)
329 */
28ea5290 330int
1da177e4
LT
331cifs_reconnect(struct TCP_Server_Info *server)
332{
333 int rc = 0;
f1987b44 334 struct list_head *tmp, *tmp2;
96daf2b0
SF
335 struct cifs_ses *ses;
336 struct cifs_tcon *tcon;
fb8c4b14 337 struct mid_q_entry *mid_entry;
3c1105df 338 struct list_head retry_list;
50c2f753 339
1da177e4 340 spin_lock(&GlobalMid_Lock);
469ee614 341 if (server->tcpStatus == CifsExiting) {
fb8c4b14 342 /* the demux thread will exit normally
1da177e4
LT
343 next time through the loop */
344 spin_unlock(&GlobalMid_Lock);
345 return rc;
346 } else
347 server->tcpStatus = CifsNeedReconnect;
348 spin_unlock(&GlobalMid_Lock);
349 server->maxBuf = 0;
aa24d1e9 350 server->max_read = 0;
1da177e4 351
f96637be 352 cifs_dbg(FYI, "Reconnecting tcp session\n");
1da177e4
LT
353
354 /* before reconnecting the tcp session, mark the smb session (uid)
355 and the tid bad so they are not used until reconnected */
f96637be
JP
356 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
357 __func__);
3f9bcca7 358 spin_lock(&cifs_tcp_ses_lock);
14fbf50d 359 list_for_each(tmp, &server->smb_ses_list) {
96daf2b0 360 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
14fbf50d 361 ses->need_reconnect = true;
f1987b44 362 list_for_each(tmp2, &ses->tcon_list) {
96daf2b0 363 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
3b795210 364 tcon->need_reconnect = true;
1da177e4 365 }
b327a717
AA
366 if (ses->tcon_ipc)
367 ses->tcon_ipc->need_reconnect = true;
1da177e4 368 }
3f9bcca7 369 spin_unlock(&cifs_tcp_ses_lock);
2b84a36c 370
1da177e4 371 /* do not want to be sending data on a socket we are freeing */
f96637be 372 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
72ca545b 373 mutex_lock(&server->srv_mutex);
fb8c4b14 374 if (server->ssocket) {
f96637be
JP
375 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
376 server->ssocket->state, server->ssocket->flags);
91cf45f0 377 kernel_sock_shutdown(server->ssocket, SHUT_WR);
f96637be
JP
378 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
379 server->ssocket->state, server->ssocket->flags);
1da177e4
LT
380 sock_release(server->ssocket);
381 server->ssocket = NULL;
382 }
5d0d2882
SP
383 server->sequence_number = 0;
384 server->session_estab = false;
21e73393
SP
385 kfree(server->session_key.response);
386 server->session_key.response = NULL;
387 server->session_key.len = 0;
fda35943 388 server->lstrp = jiffies;
1da177e4 389
2b84a36c 390 /* mark submitted MIDs for retry and issue callback */
3c1105df 391 INIT_LIST_HEAD(&retry_list);
f96637be 392 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
1da177e4 393 spin_lock(&GlobalMid_Lock);
2b84a36c
JL
394 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
395 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
7c9421e1
PS
396 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
397 mid_entry->mid_state = MID_RETRY_NEEDED;
3c1105df
JL
398 list_move(&mid_entry->qhead, &retry_list);
399 }
400 spin_unlock(&GlobalMid_Lock);
820962dc 401 mutex_unlock(&server->srv_mutex);
3c1105df 402
f96637be 403 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
3c1105df
JL
404 list_for_each_safe(tmp, tmp2, &retry_list) {
405 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
2b84a36c
JL
406 list_del_init(&mid_entry->qhead);
407 mid_entry->callback(mid_entry);
1da177e4 408 }
1da177e4 409
7fdbaa1b 410 do {
6c3d8909 411 try_to_freeze();
a9f1b85e
PS
412
413 /* we should try only the port we connected to before */
73e216a8 414 mutex_lock(&server->srv_mutex);
781a8050
LL
415 if (cifs_rdma_enabled(server))
416 rc = smbd_reconnect(server);
417 else
418 rc = generic_ip_connect(server);
fb8c4b14 419 if (rc) {
f96637be 420 cifs_dbg(FYI, "reconnect error %d\n", rc);
4afe260b 421 mutex_unlock(&server->srv_mutex);
0cb766ae 422 msleep(3000);
1da177e4
LT
423 } else {
424 atomic_inc(&tcpSesReconnectCount);
425 spin_lock(&GlobalMid_Lock);
469ee614 426 if (server->tcpStatus != CifsExiting)
fd88ce93 427 server->tcpStatus = CifsNeedNegotiate;
fb8c4b14 428 spin_unlock(&GlobalMid_Lock);
4afe260b 429 mutex_unlock(&server->srv_mutex);
1da177e4 430 }
7fdbaa1b 431 } while (server->tcpStatus == CifsNeedReconnect);
2b84a36c 432
b8c60012
SP
433 if (server->tcpStatus == CifsNeedNegotiate)
434 mod_delayed_work(cifsiod_wq, &server->echo, 0);
435
1da177e4
LT
436 return rc;
437}
438
c74093b6
JL
439static void
440cifs_echo_request(struct work_struct *work)
441{
442 int rc;
443 struct TCP_Server_Info *server = container_of(work,
444 struct TCP_Server_Info, echo.work);
b8c60012
SP
445 unsigned long echo_interval;
446
447 /*
448 * If we need to renegotiate, set echo interval to zero to
449 * immediately call echo service where we can renegotiate.
450 */
451 if (server->tcpStatus == CifsNeedNegotiate)
452 echo_interval = 0;
453 else
454 echo_interval = server->echo_interval;
c74093b6 455
247ec9b4 456 /*
b8c60012
SP
457 * We cannot send an echo if it is disabled.
458 * Also, no need to ping if we got a response recently.
247ec9b4 459 */
4fcd1813
SF
460
461 if (server->tcpStatus == CifsNeedReconnect ||
b8c60012
SP
462 server->tcpStatus == CifsExiting ||
463 server->tcpStatus == CifsNew ||
f6d76178 464 (server->ops->can_echo && !server->ops->can_echo(server)) ||
adfeb3e0 465 time_before(jiffies, server->lstrp + echo_interval - HZ))
c74093b6
JL
466 goto requeue_echo;
467
f6d76178 468 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
c74093b6 469 if (rc)
f96637be
JP
470 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
471 server->hostname);
c74093b6
JL
472
473requeue_echo:
b8c60012 474 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
c74093b6
JL
475}
476
3d9c2472 477static bool
2a37ef94 478allocate_buffers(struct TCP_Server_Info *server)
3d9c2472 479{
2a37ef94
JL
480 if (!server->bigbuf) {
481 server->bigbuf = (char *)cifs_buf_get();
482 if (!server->bigbuf) {
f96637be 483 cifs_dbg(VFS, "No memory for large SMB response\n");
3d9c2472
PS
484 msleep(3000);
485 /* retry will check if exiting */
486 return false;
487 }
2a37ef94 488 } else if (server->large_buf) {
3d9c2472 489 /* we are reusing a dirty large buf, clear its start */
1887f601 490 memset(server->bigbuf, 0, HEADER_SIZE(server));
3d9c2472
PS
491 }
492
2a37ef94
JL
493 if (!server->smallbuf) {
494 server->smallbuf = (char *)cifs_small_buf_get();
495 if (!server->smallbuf) {
f96637be 496 cifs_dbg(VFS, "No memory for SMB response\n");
3d9c2472
PS
497 msleep(1000);
498 /* retry will check if exiting */
499 return false;
500 }
501 /* beginning of smb buffer is cleared in our buf_get */
502 } else {
503 /* if existing small buf clear beginning */
1887f601 504 memset(server->smallbuf, 0, HEADER_SIZE(server));
3d9c2472
PS
505 }
506
3d9c2472
PS
507 return true;
508}
509
ba749e6d
JL
510static bool
511server_unresponsive(struct TCP_Server_Info *server)
512{
6dae51a5
PS
513 /*
514 * We need to wait 2 echo intervals to make sure we handle such
515 * situations right:
516 * 1s client sends a normal SMB request
517 * 2s client gets a response
518 * 30s echo workqueue job pops, and decides we got a response recently
519 * and don't need to send another
520 * ...
521 * 65s kernel_recvmsg times out, and we see that we haven't gotten
522 * a response in >60s.
523 */
76e75270
SC
524 if ((server->tcpStatus == CifsGood ||
525 server->tcpStatus == CifsNeedNegotiate) &&
adfeb3e0
SF
526 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
527 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
528 server->hostname, (2 * server->echo_interval) / HZ);
ba749e6d
JL
529 cifs_reconnect(server);
530 wake_up(&server->response_q);
531 return true;
532 }
533
534 return false;
535}
536
71335664
AV
537static int
538cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
e7015fb1 539{
a52c1eb7
JL
540 int length = 0;
541 int total_read;
e7015fb1 542
71335664
AV
543 smb_msg->msg_control = NULL;
544 smb_msg->msg_controllen = 0;
e831e6cf 545
71335664 546 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
95edcff4
JL
547 try_to_freeze();
548
71335664
AV
549 if (server_unresponsive(server))
550 return -ECONNABORTED;
2fef137a
LL
551 if (cifs_rdma_enabled(server) && server->smbd_conn)
552 length = smbd_recv(server->smbd_conn, smb_msg);
553 else
554 length = sock_recvmsg(server->ssocket, smb_msg, 0);
42c4dfc2 555
71335664
AV
556 if (server->tcpStatus == CifsExiting)
557 return -ESHUTDOWN;
e7015fb1 558
71335664 559 if (server->tcpStatus == CifsNeedReconnect) {
e7015fb1 560 cifs_reconnect(server);
71335664
AV
561 return -ECONNABORTED;
562 }
563
564 if (length == -ERESTARTSYS ||
565 length == -EAGAIN ||
566 length == -EINTR) {
e7015fb1
PS
567 /*
568 * Minimum sleep to prevent looping, allowing socket
569 * to clear and app threads to set tcpStatus
570 * CifsNeedReconnect if server hung.
571 */
572 usleep_range(1000, 2000);
573 length = 0;
a52c1eb7 574 continue;
71335664
AV
575 }
576
577 if (length <= 0) {
09aab880 578 cifs_dbg(FYI, "Received no data or error: %d\n", length);
e7015fb1 579 cifs_reconnect(server);
71335664 580 return -ECONNABORTED;
e7015fb1
PS
581 }
582 }
a52c1eb7 583 return total_read;
e7015fb1 584}
e7015fb1 585
e28bc5b1
JL
586int
587cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
588 unsigned int to_read)
42c4dfc2 589{
71335664
AV
590 struct msghdr smb_msg;
591 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
592 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
42c4dfc2 593
71335664
AV
594 return cifs_readv_from_socket(server, &smb_msg);
595}
42c4dfc2 596
71335664
AV
597int
598cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
1dbe3466 599 unsigned int page_offset, unsigned int to_read)
71335664
AV
600{
601 struct msghdr smb_msg;
1dbe3466
LL
602 struct bio_vec bv = {
603 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
71335664
AV
604 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
605 return cifs_readv_from_socket(server, &smb_msg);
e7015fb1
PS
606}
607
98bac62c 608static bool
fe11e4cc 609is_smb_response(struct TCP_Server_Info *server, unsigned char type)
98bac62c 610{
98bac62c
PS
611 /*
612 * The first byte big endian of the length field,
613 * is actually not part of the length but the type
614 * with the most common, zero, as regular data.
615 */
fe11e4cc
JL
616 switch (type) {
617 case RFC1002_SESSION_MESSAGE:
618 /* Regular SMB response */
619 return true;
620 case RFC1002_SESSION_KEEP_ALIVE:
f96637be 621 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
fe11e4cc
JL
622 break;
623 case RFC1002_POSITIVE_SESSION_RESPONSE:
f96637be 624 cifs_dbg(FYI, "RFC 1002 positive session response\n");
fe11e4cc
JL
625 break;
626 case RFC1002_NEGATIVE_SESSION_RESPONSE:
98bac62c
PS
627 /*
628 * We get this from Windows 98 instead of an error on
629 * SMB negprot response.
630 */
f96637be 631 cifs_dbg(FYI, "RFC 1002 negative session response\n");
98bac62c
PS
632 /* give server a second to clean up */
633 msleep(1000);
634 /*
635 * Always try 445 first on reconnect since we get NACK
636 * on some if we ever connected to port 139 (the NACK
637 * is since we do not begin with RFC1001 session
638 * initialize frame).
639 */
fe11e4cc 640 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
98bac62c
PS
641 cifs_reconnect(server);
642 wake_up(&server->response_q);
fe11e4cc
JL
643 break;
644 default:
f96637be 645 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
98bac62c 646 cifs_reconnect(server);
98bac62c
PS
647 }
648
fe11e4cc 649 return false;
98bac62c
PS
650}
651
e28bc5b1
JL
652void
653dequeue_mid(struct mid_q_entry *mid, bool malformed)
ea1f4502 654{
ad69bae1 655#ifdef CONFIG_CIFS_STATS2
ea1f4502 656 mid->when_received = jiffies;
ad69bae1 657#endif
ea1f4502
JL
658 spin_lock(&GlobalMid_Lock);
659 if (!malformed)
7c9421e1 660 mid->mid_state = MID_RESPONSE_RECEIVED;
ea1f4502 661 else
7c9421e1 662 mid->mid_state = MID_RESPONSE_MALFORMED;
ea1f4502 663 list_del_init(&mid->qhead);
ad69bae1 664 spin_unlock(&GlobalMid_Lock);
ea1f4502 665}
ad69bae1 666
c8054ebd
JL
667static void
668handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
d4e4854f 669 char *buf, int malformed)
ea1f4502 670{
316cf94a
PS
671 if (server->ops->check_trans2 &&
672 server->ops->check_trans2(mid, server, buf, malformed))
c8054ebd 673 return;
ea1f4502 674 mid->resp_buf = buf;
7c9421e1 675 mid->large_buf = server->large_buf;
2a37ef94
JL
676 /* Was previous buf put in mpx struct for multi-rsp? */
677 if (!mid->multiRsp) {
678 /* smb buffer will be freed by user thread */
679 if (server->large_buf)
680 server->bigbuf = NULL;
681 else
682 server->smallbuf = NULL;
683 }
ffc00e27 684 dequeue_mid(mid, malformed);
ad69bae1
PS
685}
686
762dfd10
PS
687static void clean_demultiplex_info(struct TCP_Server_Info *server)
688{
689 int length;
690
691 /* take it off the list, if it's not already */
692 spin_lock(&cifs_tcp_ses_lock);
693 list_del_init(&server->tcp_ses_list);
694 spin_unlock(&cifs_tcp_ses_lock);
695
696 spin_lock(&GlobalMid_Lock);
697 server->tcpStatus = CifsExiting;
698 spin_unlock(&GlobalMid_Lock);
699 wake_up_all(&server->response_q);
700
2d86dbc9 701 /* check if we have blocked requests that need to free */
fc40f9cf 702 spin_lock(&server->req_lock);
2d86dbc9
PS
703 if (server->credits <= 0)
704 server->credits = 1;
fc40f9cf 705 spin_unlock(&server->req_lock);
762dfd10
PS
706 /*
707 * Although there should not be any requests blocked on this queue it
708 * can not hurt to be paranoid and try to wake up requests that may
709 * haven been blocked when more than 50 at time were on the wire to the
710 * same server - they now will see the session is in exit state and get
711 * out of SendReceive.
712 */
713 wake_up_all(&server->request_q);
714 /* give those requests time to exit */
715 msleep(125);
bce9ce7c
LL
716 if (cifs_rdma_enabled(server) && server->smbd_conn) {
717 smbd_destroy(server->smbd_conn);
718 server->smbd_conn = NULL;
719 }
762dfd10
PS
720 if (server->ssocket) {
721 sock_release(server->ssocket);
722 server->ssocket = NULL;
723 }
724
725 if (!list_empty(&server->pending_mid_q)) {
726 struct list_head dispose_list;
727 struct mid_q_entry *mid_entry;
728 struct list_head *tmp, *tmp2;
729
730 INIT_LIST_HEAD(&dispose_list);
731 spin_lock(&GlobalMid_Lock);
732 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
733 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
f96637be 734 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
7c9421e1 735 mid_entry->mid_state = MID_SHUTDOWN;
762dfd10
PS
736 list_move(&mid_entry->qhead, &dispose_list);
737 }
738 spin_unlock(&GlobalMid_Lock);
739
740 /* now walk dispose list and issue callbacks */
741 list_for_each_safe(tmp, tmp2, &dispose_list) {
742 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
f96637be 743 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
762dfd10
PS
744 list_del_init(&mid_entry->qhead);
745 mid_entry->callback(mid_entry);
746 }
747 /* 1/8th of sec is more than enough time for them to exit */
748 msleep(125);
749 }
750
751 if (!list_empty(&server->pending_mid_q)) {
752 /*
753 * mpx threads have not exited yet give them at least the smb
754 * send timeout time for long ops.
755 *
756 * Due to delays on oplock break requests, we need to wait at
757 * least 45 seconds before giving up on a request getting a
758 * response and going ahead and killing cifsd.
759 */
f96637be 760 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
762dfd10
PS
761 msleep(46000);
762 /*
763 * If threads still have not exited they are probably never
764 * coming home not much else we can do but free the memory.
765 */
766 }
767
768 kfree(server->hostname);
769 kfree(server);
770
771 length = atomic_dec_return(&tcpSesAllocCount);
772 if (length > 0)
11d83360 773 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
762dfd10
PS
774}
775
e9097ab4
JL
776static int
777standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
778{
779 int length;
780 char *buf = server->smallbuf;
2e96467d 781 unsigned int pdu_length = server->pdu_size;
e9097ab4
JL
782
783 /* make sure this will fit in a large buffer */
93012bf9
RS
784 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
785 server->vals->header_preamble_size) {
f96637be 786 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
e9097ab4
JL
787 cifs_reconnect(server);
788 wake_up(&server->response_q);
3fabaa27 789 return -ECONNABORTED;
e9097ab4
JL
790 }
791
792 /* switch to large buffer if too big for a small one */
793 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
794 server->large_buf = true;
d4e4854f 795 memcpy(server->bigbuf, buf, server->total_read);
e9097ab4 796 buf = server->bigbuf;
e9097ab4
JL
797 }
798
799 /* now read the rest */
1887f601 800 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
93012bf9
RS
801 pdu_length - HEADER_SIZE(server) + 1
802 + server->vals->header_preamble_size);
803
e9097ab4
JL
804 if (length < 0)
805 return length;
806 server->total_read += length;
807
d4e4854f 808 dump_smb(buf, server->total_read);
e9097ab4 809
4326ed2f
PS
810 return cifs_handle_standard(server, mid);
811}
812
813int
814cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
815{
816 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
817 int length;
818
e9097ab4
JL
819 /*
820 * We know that we received enough to get to the MID as we
821 * checked the pdu_length earlier. Now check to see
822 * if the rest of the header is OK. We borrow the length
823 * var for the rest of the loop to avoid a new stack var.
824 *
825 * 48 bytes is enough to display the header and a little bit
826 * into the payload for debugging purposes.
827 */
373512ec 828 length = server->ops->check_message(buf, server->total_read, server);
e9097ab4
JL
829 if (length != 0)
830 cifs_dump_mem("Bad SMB: ", buf,
831 min_t(unsigned int, server->total_read, 48));
832
511c54a2
PS
833 if (server->ops->is_session_expired &&
834 server->ops->is_session_expired(buf)) {
835 cifs_reconnect(server);
836 wake_up(&server->response_q);
837 return -1;
838 }
839
2e44b288
PS
840 if (server->ops->is_status_pending &&
841 server->ops->is_status_pending(buf, server, length))
842 return -1;
843
ff4fa4a2
JL
844 if (!mid)
845 return length;
e9097ab4 846
d4e4854f 847 handle_mid(mid, server, buf, length);
ff4fa4a2 848 return 0;
e9097ab4
JL
849}
850
1da177e4 851static int
7c97c200 852cifs_demultiplex_thread(void *p)
1da177e4
LT
853{
854 int length;
7c97c200 855 struct TCP_Server_Info *server = p;
2a37ef94 856 unsigned int pdu_length;
8ce79ec3 857 unsigned int next_offset;
2a37ef94 858 char *buf = NULL;
a5c3e1c7 859 struct task_struct *task_to_wake = NULL;
1da177e4 860 struct mid_q_entry *mid_entry;
1da177e4 861
1da177e4 862 current->flags |= PF_MEMALLOC;
f96637be 863 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
93d0ec85
JL
864
865 length = atomic_inc_return(&tcpSesAllocCount);
866 if (length > 1)
11d83360 867 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
1da177e4 868
83144186 869 set_freezable();
469ee614 870 while (server->tcpStatus != CifsExiting) {
ede1327e
SF
871 if (try_to_freeze())
872 continue;
b8643e1b 873
2a37ef94 874 if (!allocate_buffers(server))
3d9c2472 875 continue;
b8643e1b 876
2a37ef94 877 server->large_buf = false;
2a37ef94 878 buf = server->smallbuf;
f01d5e14 879 pdu_length = 4; /* enough to get RFC1001 header */
fda35943 880
e28bc5b1 881 length = cifs_read_from_socket(server, buf, pdu_length);
a52c1eb7 882 if (length < 0)
1da177e4 883 continue;
977b6170
RS
884
885 if (server->vals->header_preamble_size == 0)
886 server->total_read = 0;
887 else
888 server->total_read = length;
1da177e4 889
98bac62c
PS
890 /*
891 * The right amount was read from socket - 4 bytes,
892 * so we can now interpret the length field.
893 */
d4e4854f 894 pdu_length = get_rfc1002_length(buf);
70ca734a 895
f96637be 896 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
fe11e4cc 897 if (!is_smb_response(server, buf[0]))
fb8c4b14 898 continue;
8ce79ec3
RS
899next_pdu:
900 server->pdu_size = pdu_length;
e4eb295d 901
89482a56 902 /* make sure we have enough to get to the MID */
8ce79ec3 903 if (server->pdu_size < HEADER_SIZE(server) - 1 -
93012bf9 904 server->vals->header_preamble_size) {
f96637be 905 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
8ce79ec3 906 server->pdu_size);
89482a56
JL
907 cifs_reconnect(server);
908 wake_up(&server->response_q);
909 continue;
e4eb295d 910 }
e7015fb1 911
89482a56 912 /* read down to the MID */
93012bf9
RS
913 length = cifs_read_from_socket(server,
914 buf + server->vals->header_preamble_size,
915 HEADER_SIZE(server) - 1
916 - server->vals->header_preamble_size);
89482a56 917 if (length < 0)
e4eb295d 918 continue;
2a37ef94 919 server->total_read += length;
1da177e4 920
8ce79ec3
RS
921 if (server->ops->next_header) {
922 next_offset = server->ops->next_header(buf);
923 if (next_offset)
924 server->pdu_size = next_offset;
925 }
926
696e420b 927 mid_entry = NULL;
9bb17e09
PS
928 if (server->ops->is_transform_hdr &&
929 server->ops->receive_transform &&
930 server->ops->is_transform_hdr(buf)) {
931 length = server->ops->receive_transform(server,
932 &mid_entry);
933 } else {
934 mid_entry = server->ops->find_mid(server, buf);
50c2f753 935
9bb17e09
PS
936 if (!mid_entry || !mid_entry->receive)
937 length = standard_receive3(server, mid_entry);
938 else
939 length = mid_entry->receive(server, mid_entry);
940 }
71823baf 941
696e420b
LP
942 if (length < 0) {
943 if (mid_entry)
944 cifs_mid_q_entry_release(mid_entry);
fe11e4cc 945 continue;
696e420b 946 }
1da177e4 947
d4e4854f 948 if (server->large_buf)
2a37ef94 949 buf = server->bigbuf;
fda35943 950
fda35943 951 server->lstrp = jiffies;
2b84a36c 952 if (mid_entry != NULL) {
e19b2bc0 953 mid_entry->resp_buf_size = server->pdu_size;
38bd4906
SP
954 if ((mid_entry->mid_flags & MID_WAIT_CANCELLED) &&
955 mid_entry->mid_state == MID_RESPONSE_RECEIVED &&
956 server->ops->handle_cancelled_mid)
957 server->ops->handle_cancelled_mid(
958 mid_entry->resp_buf,
959 server);
960
2a37ef94
JL
961 if (!mid_entry->multiRsp || mid_entry->multiEnd)
962 mid_entry->callback(mid_entry);
696e420b
LP
963
964 cifs_mid_q_entry_release(mid_entry);
38bd4906
SP
965 } else if (server->ops->is_oplock_break &&
966 server->ops->is_oplock_break(buf, server)) {
967 cifs_dbg(FYI, "Received oplock break\n");
968 } else {
f96637be
JP
969 cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
970 atomic_read(&midCount));
1887f601
PS
971 cifs_dump_mem("Received Data is: ", buf,
972 HEADER_SIZE(server));
3979877e 973#ifdef CONFIG_CIFS_DEBUG2
7f0adb53 974 if (server->ops->dump_detail)
71992e62 975 server->ops->dump_detail(buf, server);
3979877e
SF
976 cifs_dump_mids(server);
977#endif /* CIFS_DEBUG2 */
8ce79ec3
RS
978 }
979 if (pdu_length > server->pdu_size) {
980 if (!allocate_buffers(server))
981 continue;
982 pdu_length -= server->pdu_size;
983 server->total_read = 0;
984 server->large_buf = false;
985 buf = server->smallbuf;
986 goto next_pdu;
e4eb295d
SF
987 }
988 } /* end while !EXITING */
989
fd62cb7e 990 /* buffer usually freed in free_mid - need to free it here on exit */
2a37ef94
JL
991 cifs_buf_release(server->bigbuf);
992 if (server->smallbuf) /* no sense logging a debug message if NULL */
993 cifs_small_buf_release(server->smallbuf);
1da177e4 994
a5c3e1c7 995 task_to_wake = xchg(&server->tsk, NULL);
762dfd10 996 clean_demultiplex_info(server);
a5c3e1c7
SF
997
998 /* if server->tsk was NULL then wait for a signal before exiting */
999 if (!task_to_wake) {
1000 set_current_state(TASK_INTERRUPTIBLE);
1001 while (!signal_pending(current)) {
1002 schedule();
1003 set_current_state(TASK_INTERRUPTIBLE);
1004 }
1005 set_current_state(TASK_RUNNING);
1006 }
1007
0468a2cf 1008 module_put_and_exit(0);
1da177e4
LT
1009}
1010
c359cf3c
JL
1011/* extract the host portion of the UNC string */
1012static char *
1013extract_hostname(const char *unc)
1014{
1015 const char *src;
1016 char *dst, *delim;
1017 unsigned int len;
1018
1019 /* skip double chars at beginning of string */
1020 /* BB: check validity of these bytes? */
1021 src = unc + 2;
1022
1023 /* delimiter between hostname and sharename is always '\\' now */
1024 delim = strchr(src, '\\');
1025 if (!delim)
1026 return ERR_PTR(-EINVAL);
1027
1028 len = delim - src;
1029 dst = kmalloc((len + 1), GFP_KERNEL);
1030 if (dst == NULL)
1031 return ERR_PTR(-ENOMEM);
1032
1033 memcpy(dst, src, len);
1034 dst[len] = '\0';
1035
1036 return dst;
1037}
1038
8830d7e0
SP
1039static int get_option_ul(substring_t args[], unsigned long *option)
1040{
1041 int rc;
1042 char *string;
1043
1044 string = match_strdup(args);
1045 if (string == NULL)
1046 return -ENOMEM;
bfa890a3 1047 rc = kstrtoul(string, 0, option);
8830d7e0
SP
1048 kfree(string);
1049
1050 return rc;
1051}
1052
3da46565
EB
1053static int get_option_uid(substring_t args[], kuid_t *result)
1054{
1055 unsigned long value;
1056 kuid_t uid;
1057 int rc;
1058
1059 rc = get_option_ul(args, &value);
1060 if (rc)
1061 return rc;
1062
1063 uid = make_kuid(current_user_ns(), value);
1064 if (!uid_valid(uid))
1065 return -EINVAL;
1066
1067 *result = uid;
1068 return 0;
1069}
1070
1071static int get_option_gid(substring_t args[], kgid_t *result)
1072{
1073 unsigned long value;
1074 kgid_t gid;
1075 int rc;
1076
1077 rc = get_option_ul(args, &value);
1078 if (rc)
1079 return rc;
1080
1081 gid = make_kgid(current_user_ns(), value);
1082 if (!gid_valid(gid))
1083 return -EINVAL;
1084
1085 *result = gid;
1086 return 0;
1087}
8830d7e0
SP
1088
1089static int cifs_parse_security_flavors(char *value,
1090 struct smb_vol *vol)
1091{
1092
1093 substring_t args[MAX_OPT_ARGS];
1094
1e3cc57e
JL
1095 /*
1096 * With mount options, the last one should win. Reset any existing
1097 * settings back to default.
1098 */
1099 vol->sectype = Unspecified;
1100 vol->sign = false;
1101
8830d7e0 1102 switch (match_token(value, cifs_secflavor_tokens, args)) {
3f618223
JL
1103 case Opt_sec_krb5p:
1104 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1105 return 1;
1106 case Opt_sec_krb5i:
1107 vol->sign = true;
1108 /* Fallthrough */
8830d7e0 1109 case Opt_sec_krb5:
1e3cc57e 1110 vol->sectype = Kerberos;
8830d7e0 1111 break;
3f618223 1112 case Opt_sec_ntlmsspi:
1e3cc57e 1113 vol->sign = true;
3f618223 1114 /* Fallthrough */
8830d7e0 1115 case Opt_sec_ntlmssp:
1e3cc57e 1116 vol->sectype = RawNTLMSSP;
8830d7e0 1117 break;
3f618223 1118 case Opt_sec_ntlmi:
1e3cc57e 1119 vol->sign = true;
3f618223 1120 /* Fallthrough */
8830d7e0 1121 case Opt_ntlm:
1e3cc57e 1122 vol->sectype = NTLM;
8830d7e0 1123 break;
3f618223 1124 case Opt_sec_ntlmv2i:
1e3cc57e 1125 vol->sign = true;
3f618223 1126 /* Fallthrough */
7659624f 1127 case Opt_sec_ntlmv2:
1e3cc57e 1128 vol->sectype = NTLMv2;
8830d7e0
SP
1129 break;
1130#ifdef CONFIG_CIFS_WEAK_PW_HASH
1131 case Opt_sec_lanman:
1e3cc57e 1132 vol->sectype = LANMAN;
8830d7e0
SP
1133 break;
1134#endif
1135 case Opt_sec_none:
1136 vol->nullauth = 1;
1137 break;
1138 default:
f96637be 1139 cifs_dbg(VFS, "bad security option: %s\n", value);
8830d7e0
SP
1140 return 1;
1141 }
1142
1143 return 0;
1144}
1145
15b6a473
JL
1146static int
1147cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1148{
1149 substring_t args[MAX_OPT_ARGS];
1150
1151 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1152 case Opt_cache_loose:
1153 vol->direct_io = false;
1154 vol->strict_io = false;
1155 break;
1156 case Opt_cache_strict:
1157 vol->direct_io = false;
1158 vol->strict_io = true;
1159 break;
1160 case Opt_cache_none:
1161 vol->direct_io = true;
1162 vol->strict_io = false;
1163 break;
1164 default:
f96637be 1165 cifs_dbg(VFS, "bad cache= option: %s\n", value);
15b6a473
JL
1166 return 1;
1167 }
1168 return 0;
1169}
1170
23db65f5 1171static int
c7c137b9 1172cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
23db65f5
JL
1173{
1174 substring_t args[MAX_OPT_ARGS];
1175
1176 switch (match_token(value, cifs_smb_version_tokens, args)) {
7420451f 1177#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
23db65f5 1178 case Smb_1:
f92a720e
SF
1179 if (disable_legacy_dialects) {
1180 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1181 return 1;
1182 }
c7c137b9
SF
1183 if (is_smb3) {
1184 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1185 return 1;
1186 }
23db65f5
JL
1187 vol->ops = &smb1_operations;
1188 vol->vals = &smb1_values;
1189 break;
dd446b16 1190 case Smb_20:
f92a720e
SF
1191 if (disable_legacy_dialects) {
1192 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1193 return 1;
1194 }
c7c137b9
SF
1195 if (is_smb3) {
1196 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1197 return 1;
1198 }
53ef1016 1199 vol->ops = &smb20_operations;
dd446b16
SF
1200 vol->vals = &smb20_values;
1201 break;
7420451f
SF
1202#else
1203 case Smb_1:
1204 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1205 return 1;
1206 case Smb_20:
1207 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1208 return 1;
1209#endif /* CIFS_ALLOW_INSECURE_LEGACY */
1080ef75
SF
1210 case Smb_21:
1211 vol->ops = &smb21_operations;
1212 vol->vals = &smb21_values;
1213 break;
e4aa25e7 1214 case Smb_30:
38107d45 1215 vol->ops = &smb30_operations;
e4aa25e7
SF
1216 vol->vals = &smb30_values;
1217 break;
20b6d8b4
SF
1218 case Smb_302:
1219 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1220 vol->vals = &smb302_values;
1221 break;
5f7fbf73
SF
1222#ifdef CONFIG_CIFS_SMB311
1223 case Smb_311:
aab1893d 1224 vol->ops = &smb311_operations;
5f7fbf73
SF
1225 vol->vals = &smb311_values;
1226 break;
1227#endif /* SMB311 */
9764c02f
SF
1228 case Smb_3any:
1229 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1230 vol->vals = &smb3any_values;
1231 break;
1232 case Smb_default:
1233 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1234 vol->vals = &smbdefault_values;
1235 break;
23db65f5 1236 default:
f96637be 1237 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
23db65f5
JL
1238 return 1;
1239 }
1240 return 0;
1241}
1242
d387a5c5
JL
1243/*
1244 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1245 * fields with the result. Returns 0 on success and an error otherwise.
1246 */
1247static int
1248cifs_parse_devname(const char *devname, struct smb_vol *vol)
1249{
1250 char *pos;
1251 const char *delims = "/\\";
1252 size_t len;
1253
1254 /* make sure we have a valid UNC double delimiter prefix */
1255 len = strspn(devname, delims);
1256 if (len != 2)
1257 return -EINVAL;
1258
1259 /* find delimiter between host and sharename */
1260 pos = strpbrk(devname + 2, delims);
1261 if (!pos)
1262 return -EINVAL;
1263
1264 /* skip past delimiter */
1265 ++pos;
1266
1267 /* now go until next delimiter or end of string */
1268 len = strcspn(pos, delims);
1269
1270 /* move "pos" up to delimiter or NULL */
1271 pos += len;
1272 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1273 if (!vol->UNC)
1274 return -ENOMEM;
1275
1276 convert_delimiter(vol->UNC, '\\');
1277
11e31647
SP
1278 /* skip any delimiter */
1279 if (*pos == '/' || *pos == '\\')
1280 pos++;
1281
1282 /* If pos is NULL then no prepath */
1283 if (!*pos)
d387a5c5
JL
1284 return 0;
1285
1286 vol->prepath = kstrdup(pos, GFP_KERNEL);
1287 if (!vol->prepath)
1288 return -ENOMEM;
1289
1290 return 0;
1291}
1292
1da177e4 1293static int
b946845a 1294cifs_parse_mount_options(const char *mountdata, const char *devname,
c7c137b9 1295 struct smb_vol *vol, bool is_smb3)
1da177e4 1296{
8830d7e0 1297 char *data, *end;
957df453 1298 char *mountdata_copy = NULL, *options;
1da177e4
LT
1299 unsigned int temp_len, i, j;
1300 char separator[2];
9b9d6b24
JL
1301 short int override_uid = -1;
1302 short int override_gid = -1;
1303 bool uid_specified = false;
1304 bool gid_specified = false;
d8162558
JL
1305 bool sloppy = false;
1306 char *invalid = NULL;
88463999 1307 char *nodename = utsname()->nodename;
8830d7e0
SP
1308 char *string = NULL;
1309 char *tmp_end, *value;
1310 char delim;
b979aaa1 1311 bool got_ip = false;
7e682f76 1312 bool got_version = false;
b979aaa1
JL
1313 unsigned short port = 0;
1314 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
1da177e4
LT
1315
1316 separator[0] = ',';
50c2f753 1317 separator[1] = 0;
8830d7e0 1318 delim = separator[0];
1da177e4 1319
6ee9542a
JL
1320 /* ensure we always start with zeroed-out smb_vol */
1321 memset(vol, 0, sizeof(*vol));
1322
88463999
JL
1323 /*
1324 * does not have to be perfect mapping since field is
1325 * informational, only used for servers that do not support
1326 * port 445 and it can be overridden at mount time
1327 */
1397f2ee
JL
1328 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1329 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
88463999
JL
1330 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1331
1397f2ee 1332 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
a10faeb2
SF
1333 /* null target name indicates to use *SMBSERVR default called name
1334 if we end up sending RFC1001 session initialize */
1335 vol->target_rfc1001_name[0] = 0;
3e4b3e1f
JL
1336 vol->cred_uid = current_uid();
1337 vol->linux_uid = current_uid();
a001e5b5 1338 vol->linux_gid = current_gid();
f55ed1a8 1339
2baa2682
SF
1340 /*
1341 * default to SFM style remapping of seven reserved characters
1342 * unless user overrides it or we negotiate CIFS POSIX where
1343 * it is unnecessary. Can not simultaneously use more than one mapping
1344 * since then readdir could list files that open could not open
1345 */
1346 vol->remap = true;
1347
f55ed1a8
JL
1348 /* default to only allowing write access to owner of the mount */
1349 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1da177e4
LT
1350
1351 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
ac67055e
JA
1352 /* default is always to request posix paths. */
1353 vol->posix_paths = 1;
a0c9217f
JL
1354 /* default to using server inode numbers where available */
1355 vol->server_ino = 1;
ac67055e 1356
1b359204
JL
1357 /* default is to use strict cifs caching semantics */
1358 vol->strict_io = true;
1359
6d20e840
SJ
1360 vol->actimeo = CIFS_DEF_ACTIMEO;
1361
9764c02f
SF
1362 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1363 vol->ops = &smb30_operations;
1364 vol->vals = &smbdefault_values;
23db65f5 1365
b782fcc1
RV
1366 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1367
b946845a
SF
1368 if (!mountdata)
1369 goto cifs_parse_mount_err;
1370
1371 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1372 if (!mountdata_copy)
1373 goto cifs_parse_mount_err;
1da177e4 1374
b946845a 1375 options = mountdata_copy;
4906e50b 1376 end = options + strlen(options);
8830d7e0 1377
50c2f753 1378 if (strncmp(options, "sep=", 4) == 0) {
fb8c4b14 1379 if (options[4] != 0) {
1da177e4
LT
1380 separator[0] = options[4];
1381 options += 5;
1382 } else {
f96637be 1383 cifs_dbg(FYI, "Null separator not allowed\n");
1da177e4
LT
1384 }
1385 }
3d3ea8e6
SP
1386 vol->backupuid_specified = false; /* no backup intent for a user */
1387 vol->backupgid_specified = false; /* no backup intent for a group */
50c2f753 1388
37d4f99b
JL
1389 switch (cifs_parse_devname(devname, vol)) {
1390 case 0:
1391 break;
1392 case -ENOMEM:
1393 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1394 goto cifs_parse_mount_err;
1395 case -EINVAL:
1396 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1397 goto cifs_parse_mount_err;
1398 default:
1399 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1400 goto cifs_parse_mount_err;
d387a5c5
JL
1401 }
1402
1da177e4 1403 while ((data = strsep(&options, separator)) != NULL) {
8830d7e0
SP
1404 substring_t args[MAX_OPT_ARGS];
1405 unsigned long option;
1406 int token;
1407
1da177e4
LT
1408 if (!*data)
1409 continue;
1da177e4 1410
8830d7e0
SP
1411 token = match_token(data, cifs_mount_option_tokens, args);
1412
1413 switch (token) {
1414
1415 /* Ingnore the following */
1416 case Opt_ignore:
1417 break;
1418
1419 /* Boolean values */
1420 case Opt_user_xattr:
1da177e4 1421 vol->no_xattr = 0;
8830d7e0
SP
1422 break;
1423 case Opt_nouser_xattr:
1da177e4 1424 vol->no_xattr = 1;
8830d7e0
SP
1425 break;
1426 case Opt_forceuid:
9b9d6b24 1427 override_uid = 1;
8830d7e0
SP
1428 break;
1429 case Opt_noforceuid:
9b9d6b24 1430 override_uid = 0;
8830d7e0 1431 break;
72bd481f
JL
1432 case Opt_forcegid:
1433 override_gid = 1;
1434 break;
1435 case Opt_noforcegid:
1436 override_gid = 0;
1437 break;
8830d7e0 1438 case Opt_noblocksend:
edf1ae40 1439 vol->noblocksnd = 1;
8830d7e0
SP
1440 break;
1441 case Opt_noautotune:
edf1ae40 1442 vol->noautotune = 1;
8830d7e0
SP
1443 break;
1444 case Opt_hard:
1da177e4 1445 vol->retry = 1;
8830d7e0
SP
1446 break;
1447 case Opt_soft:
1da177e4 1448 vol->retry = 0;
8830d7e0
SP
1449 break;
1450 case Opt_perm:
1da177e4 1451 vol->noperm = 0;
8830d7e0
SP
1452 break;
1453 case Opt_noperm:
1da177e4 1454 vol->noperm = 1;
8830d7e0
SP
1455 break;
1456 case Opt_mapchars:
2baa2682
SF
1457 vol->sfu_remap = true;
1458 vol->remap = false; /* disable SFM mapping */
8830d7e0
SP
1459 break;
1460 case Opt_nomapchars:
2baa2682
SF
1461 vol->sfu_remap = false;
1462 break;
1463 case Opt_mapposix:
1464 vol->remap = true;
1465 vol->sfu_remap = false; /* disable SFU mapping */
1466 break;
1467 case Opt_nomapposix:
1468 vol->remap = false;
8830d7e0
SP
1469 break;
1470 case Opt_sfu:
50c2f753 1471 vol->sfu_emul = 1;
8830d7e0
SP
1472 break;
1473 case Opt_nosfu:
50c2f753 1474 vol->sfu_emul = 0;
8830d7e0
SP
1475 break;
1476 case Opt_nodfs:
2c1b8615 1477 vol->nodfs = 1;
8830d7e0
SP
1478 break;
1479 case Opt_posixpaths:
ac67055e 1480 vol->posix_paths = 1;
8830d7e0
SP
1481 break;
1482 case Opt_noposixpaths:
ac67055e 1483 vol->posix_paths = 0;
8830d7e0
SP
1484 break;
1485 case Opt_nounix:
b326614e
SF
1486 if (vol->linux_ext)
1487 cifs_dbg(VFS,
1488 "conflicting unix mount options\n");
c18c842b 1489 vol->no_linux_ext = 1;
8830d7e0 1490 break;
b326614e
SF
1491 case Opt_unix:
1492 if (vol->no_linux_ext)
1493 cifs_dbg(VFS,
1494 "conflicting unix mount options\n");
1495 vol->linux_ext = 1;
1496 break;
8830d7e0 1497 case Opt_nocase:
50c2f753 1498 vol->nocase = 1;
8830d7e0
SP
1499 break;
1500 case Opt_brl:
c46fa8ac 1501 vol->nobrl = 0;
8830d7e0
SP
1502 break;
1503 case Opt_nobrl:
c46fa8ac 1504 vol->nobrl = 1;
5cfdddcf
PS
1505 /*
1506 * turn off mandatory locking in mode
8830d7e0 1507 * if remote locking is turned off since the
5cfdddcf
PS
1508 * local vfs will do advisory
1509 */
50c2f753
SF
1510 if (vol->file_mode ==
1511 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
d3485d37 1512 vol->file_mode = S_IALLUGO;
8830d7e0 1513 break;
3d4ef9a1
SF
1514 case Opt_nohandlecache:
1515 vol->nohandlecache = 1;
1516 break;
1517 case Opt_handlecache:
1518 vol->nohandlecache = 0;
1519 break;
8830d7e0 1520 case Opt_forcemandatorylock:
13a6e42a 1521 vol->mand_lock = 1;
8830d7e0
SP
1522 break;
1523 case Opt_setuids:
1da177e4 1524 vol->setuids = 1;
8830d7e0
SP
1525 break;
1526 case Opt_nosetuids:
1da177e4 1527 vol->setuids = 0;
8830d7e0 1528 break;
95932655
SF
1529 case Opt_setuidfromacl:
1530 vol->setuidfromacl = 1;
1531 break;
8830d7e0 1532 case Opt_dynperm:
d0a9c078 1533 vol->dynperm = true;
8830d7e0
SP
1534 break;
1535 case Opt_nodynperm:
d0a9c078 1536 vol->dynperm = false;
8830d7e0
SP
1537 break;
1538 case Opt_nohard:
1da177e4 1539 vol->retry = 0;
8830d7e0
SP
1540 break;
1541 case Opt_nosoft:
1da177e4 1542 vol->retry = 1;
8830d7e0
SP
1543 break;
1544 case Opt_nointr:
1da177e4 1545 vol->intr = 0;
8830d7e0
SP
1546 break;
1547 case Opt_intr:
1da177e4 1548 vol->intr = 1;
8830d7e0
SP
1549 break;
1550 case Opt_nostrictsync:
be652445 1551 vol->nostrictsync = 1;
8830d7e0
SP
1552 break;
1553 case Opt_strictsync:
be652445 1554 vol->nostrictsync = 0;
8830d7e0
SP
1555 break;
1556 case Opt_serverino:
1da177e4 1557 vol->server_ino = 1;
8830d7e0
SP
1558 break;
1559 case Opt_noserverino:
1da177e4 1560 vol->server_ino = 0;
8830d7e0
SP
1561 break;
1562 case Opt_rwpidforward:
d4ffff1f 1563 vol->rwpidforward = 1;
8830d7e0
SP
1564 break;
1565 case Opt_cifsacl:
0a4b92c0 1566 vol->cifs_acl = 1;
8830d7e0
SP
1567 break;
1568 case Opt_nocifsacl:
0a4b92c0 1569 vol->cifs_acl = 0;
8830d7e0
SP
1570 break;
1571 case Opt_acl:
1da177e4 1572 vol->no_psx_acl = 0;
8830d7e0
SP
1573 break;
1574 case Opt_noacl:
1da177e4 1575 vol->no_psx_acl = 1;
8830d7e0
SP
1576 break;
1577 case Opt_locallease:
84210e91 1578 vol->local_lease = 1;
8830d7e0
SP
1579 break;
1580 case Opt_sign:
1e3cc57e 1581 vol->sign = true;
8830d7e0
SP
1582 break;
1583 case Opt_seal:
95b1cb90 1584 /* we do not do the following in secFlags because seal
8830d7e0
SP
1585 * is a per tree connection (mount) not a per socket
1586 * or per-smb connection option in the protocol
1587 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1588 */
95b1cb90 1589 vol->seal = 1;
8830d7e0 1590 break;
8830d7e0 1591 case Opt_noac:
0b456f04 1592 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
8830d7e0
SP
1593 break;
1594 case Opt_fsc:
607a569d 1595#ifndef CONFIG_CIFS_FSCACHE
f96637be 1596 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
b946845a 1597 goto cifs_parse_mount_err;
607a569d 1598#endif
fa1df75d 1599 vol->fsc = true;
8830d7e0
SP
1600 break;
1601 case Opt_mfsymlinks:
736a3320 1602 vol->mfsymlinks = true;
8830d7e0
SP
1603 break;
1604 case Opt_multiuser:
0eb8a132 1605 vol->multiuser = true;
8830d7e0 1606 break;
d8162558
JL
1607 case Opt_sloppy:
1608 sloppy = true;
1609 break;
a0b3df5c
JL
1610 case Opt_nosharesock:
1611 vol->nosharesock = true;
1612 break;
b2a30774
SF
1613 case Opt_nopersistent:
1614 vol->nopersistent = true;
1615 if (vol->persistent) {
1616 cifs_dbg(VFS,
1617 "persistenthandles mount options conflict\n");
1618 goto cifs_parse_mount_err;
1619 }
1620 break;
1621 case Opt_persistent:
1622 vol->persistent = true;
592fafe6 1623 if ((vol->nopersistent) || (vol->resilient)) {
b2a30774
SF
1624 cifs_dbg(VFS,
1625 "persistenthandles mount options conflict\n");
1626 goto cifs_parse_mount_err;
1627 }
1628 break;
592fafe6
SF
1629 case Opt_resilient:
1630 vol->resilient = true;
1631 if (vol->persistent) {
1632 cifs_dbg(VFS,
1633 "persistenthandles mount options conflict\n");
1634 goto cifs_parse_mount_err;
1635 }
1636 break;
1637 case Opt_noresilient:
1638 vol->resilient = false; /* already the default */
1639 break;
39566443
GP
1640 case Opt_domainauto:
1641 vol->domainauto = true;
1642 break;
8339dd32
LL
1643 case Opt_rdma:
1644 vol->rdma = true;
1645 break;
8830d7e0
SP
1646
1647 /* Numeric Values */
1648 case Opt_backupuid:
3da46565 1649 if (get_option_uid(args, &vol->backupuid)) {
f96637be
JP
1650 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1651 __func__);
3d3ea8e6
SP
1652 goto cifs_parse_mount_err;
1653 }
1654 vol->backupuid_specified = true;
8830d7e0
SP
1655 break;
1656 case Opt_backupgid:
3da46565 1657 if (get_option_gid(args, &vol->backupgid)) {
f96637be
JP
1658 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1659 __func__);
3d3ea8e6
SP
1660 goto cifs_parse_mount_err;
1661 }
1662 vol->backupgid_specified = true;
8830d7e0
SP
1663 break;
1664 case Opt_uid:
3da46565 1665 if (get_option_uid(args, &vol->linux_uid)) {
f96637be
JP
1666 cifs_dbg(VFS, "%s: Invalid uid value\n",
1667 __func__);
8830d7e0
SP
1668 goto cifs_parse_mount_err;
1669 }
8830d7e0
SP
1670 uid_specified = true;
1671 break;
1672 case Opt_cruid:
3da46565 1673 if (get_option_uid(args, &vol->cred_uid)) {
f96637be
JP
1674 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1675 __func__);
8830d7e0
SP
1676 goto cifs_parse_mount_err;
1677 }
8830d7e0
SP
1678 break;
1679 case Opt_gid:
3da46565 1680 if (get_option_gid(args, &vol->linux_gid)) {
f96637be
JP
1681 cifs_dbg(VFS, "%s: Invalid gid value\n",
1682 __func__);
8830d7e0
SP
1683 goto cifs_parse_mount_err;
1684 }
8830d7e0
SP
1685 gid_specified = true;
1686 break;
1687 case Opt_file_mode:
1688 if (get_option_ul(args, &option)) {
f96637be
JP
1689 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1690 __func__);
8830d7e0
SP
1691 goto cifs_parse_mount_err;
1692 }
1693 vol->file_mode = option;
1694 break;
1695 case Opt_dirmode:
1696 if (get_option_ul(args, &option)) {
f96637be
JP
1697 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1698 __func__);
8830d7e0
SP
1699 goto cifs_parse_mount_err;
1700 }
1701 vol->dir_mode = option;
1702 break;
1703 case Opt_port:
b979aaa1
JL
1704 if (get_option_ul(args, &option) ||
1705 option > USHRT_MAX) {
f96637be
JP
1706 cifs_dbg(VFS, "%s: Invalid port value\n",
1707 __func__);
8830d7e0
SP
1708 goto cifs_parse_mount_err;
1709 }
b979aaa1 1710 port = (unsigned short)option;
8830d7e0
SP
1711 break;
1712 case Opt_rsize:
1713 if (get_option_ul(args, &option)) {
f96637be
JP
1714 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1715 __func__);
b946845a 1716 goto cifs_parse_mount_err;
8830d7e0
SP
1717 }
1718 vol->rsize = option;
1719 break;
1720 case Opt_wsize:
1721 if (get_option_ul(args, &option)) {
f96637be
JP
1722 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1723 __func__);
8830d7e0
SP
1724 goto cifs_parse_mount_err;
1725 }
1726 vol->wsize = option;
1727 break;
1728 case Opt_actimeo:
1729 if (get_option_ul(args, &option)) {
f96637be
JP
1730 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1731 __func__);
8830d7e0
SP
1732 goto cifs_parse_mount_err;
1733 }
1734 vol->actimeo = HZ * option;
1735 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
f96637be 1736 cifs_dbg(VFS, "attribute cache timeout too large\n");
8830d7e0
SP
1737 goto cifs_parse_mount_err;
1738 }
1739 break;
adfeb3e0
SF
1740 case Opt_echo_interval:
1741 if (get_option_ul(args, &option)) {
1742 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1743 __func__);
1744 goto cifs_parse_mount_err;
1745 }
1746 vol->echo_interval = option;
1747 break;
8b217fe7
SF
1748 case Opt_snapshot:
1749 if (get_option_ul(args, &option)) {
1750 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1751 __func__);
1752 goto cifs_parse_mount_err;
1753 }
1754 vol->snapshot_time = option;
1755 break;
141891f4
SF
1756 case Opt_max_credits:
1757 if (get_option_ul(args, &option) || (option < 20) ||
1758 (option > 60000)) {
1759 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1760 __func__);
1761 goto cifs_parse_mount_err;
1762 }
1763 vol->max_credits = option;
1764 break;
8830d7e0
SP
1765
1766 /* String Arguments */
1767
4fe9e963
SP
1768 case Opt_blank_user:
1769 /* null user, ie. anonymous authentication */
1770 vol->nullauth = 1;
1771 vol->username = NULL;
1772 break;
8830d7e0
SP
1773 case Opt_user:
1774 string = match_strdup(args);
1775 if (string == NULL)
1776 goto out_nomem;
1777
8c3a2b4c
SL
1778 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1779 CIFS_MAX_USERNAME_LEN) {
0b456f04 1780 pr_warn("CIFS: username too long\n");
8830d7e0
SP
1781 goto cifs_parse_mount_err;
1782 }
2bd50fb3
TK
1783
1784 kfree(vol->username);
8830d7e0 1785 vol->username = kstrdup(string, GFP_KERNEL);
f96637be 1786 if (!vol->username)
8830d7e0 1787 goto cifs_parse_mount_err;
8830d7e0
SP
1788 break;
1789 case Opt_blank_pass:
8830d7e0
SP
1790 /* passwords have to be handled differently
1791 * to allow the character used for deliminator
1792 * to be passed within them
1793 */
1794
c369c9a4
SP
1795 /*
1796 * Check if this is a case where the password
1797 * starts with a delimiter
1798 */
1799 tmp_end = strchr(data, '=');
1800 tmp_end++;
1801 if (!(tmp_end < end && tmp_end[1] == delim)) {
1802 /* No it is not. Set the password to NULL */
97f4b727 1803 kzfree(vol->password);
c369c9a4
SP
1804 vol->password = NULL;
1805 break;
1806 }
1807 /* Yes it is. Drop down to Opt_pass below.*/
1808 case Opt_pass:
8830d7e0
SP
1809 /* Obtain the value string */
1810 value = strchr(data, '=');
10238074 1811 value++;
8830d7e0
SP
1812
1813 /* Set tmp_end to end of the string */
1814 tmp_end = (char *) value + strlen(value);
1815
1816 /* Check if following character is the deliminator
1817 * If yes, we have encountered a double deliminator
1818 * reset the NULL character to the deliminator
1819 */
e73f843a 1820 if (tmp_end < end && tmp_end[1] == delim) {
8830d7e0
SP
1821 tmp_end[0] = delim;
1822
e73f843a
SJ
1823 /* Keep iterating until we get to a single
1824 * deliminator OR the end
1825 */
1826 while ((tmp_end = strchr(tmp_end, delim))
1827 != NULL && (tmp_end[1] == delim)) {
1828 tmp_end = (char *) &tmp_end[2];
1829 }
1830
1831 /* Reset var options to point to next element */
1832 if (tmp_end) {
1833 tmp_end[0] = '\0';
1834 options = (char *) &tmp_end[1];
1835 } else
1836 /* Reached the end of the mount option
1837 * string */
1838 options = end;
8830d7e0
SP
1839 }
1840
97f4b727 1841 kzfree(vol->password);
8830d7e0
SP
1842 /* Now build new password string */
1843 temp_len = strlen(value);
1844 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1845 if (vol->password == NULL) {
0b456f04 1846 pr_warn("CIFS: no memory for password\n");
8830d7e0
SP
1847 goto cifs_parse_mount_err;
1848 }
1849
1850 for (i = 0, j = 0; i < temp_len; i++, j++) {
1851 vol->password[j] = value[i];
1852 if ((value[i] == delim) &&
1853 value[i+1] == delim)
1854 /* skip the second deliminator */
1855 i++;
1856 }
1857 vol->password[j] = '\0';
1858 break;
4fe9e963 1859 case Opt_blank_ip:
b979aaa1
JL
1860 /* FIXME: should this be an error instead? */
1861 got_ip = false;
4fe9e963 1862 break;
8830d7e0
SP
1863 case Opt_ip:
1864 string = match_strdup(args);
1865 if (string == NULL)
1866 goto out_nomem;
1867
b979aaa1
JL
1868 if (!cifs_convert_address(dstaddr, string,
1869 strlen(string))) {
0b456f04 1870 pr_err("CIFS: bad ip= option (%s).\n", string);
8830d7e0
SP
1871 goto cifs_parse_mount_err;
1872 }
b979aaa1 1873 got_ip = true;
8830d7e0 1874 break;
8830d7e0
SP
1875 case Opt_domain:
1876 string = match_strdup(args);
1877 if (string == NULL)
1878 goto out_nomem;
1879
057d6332
CG
1880 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1881 == CIFS_MAX_DOMAINNAME_LEN) {
0b456f04 1882 pr_warn("CIFS: domain name too long\n");
8830d7e0
SP
1883 goto cifs_parse_mount_err;
1884 }
1885
2bd50fb3 1886 kfree(vol->domainname);
8830d7e0
SP
1887 vol->domainname = kstrdup(string, GFP_KERNEL);
1888 if (!vol->domainname) {
0b456f04 1889 pr_warn("CIFS: no memory for domainname\n");
8830d7e0
SP
1890 goto cifs_parse_mount_err;
1891 }
f96637be 1892 cifs_dbg(FYI, "Domain name set\n");
8830d7e0
SP
1893 break;
1894 case Opt_srcaddr:
1895 string = match_strdup(args);
1896 if (string == NULL)
1897 goto out_nomem;
1898
4fe9e963 1899 if (!cifs_convert_address(
8830d7e0
SP
1900 (struct sockaddr *)&vol->srcaddr,
1901 string, strlen(string))) {
0b456f04
AS
1902 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1903 string);
8830d7e0
SP
1904 goto cifs_parse_mount_err;
1905 }
1906 break;
8830d7e0
SP
1907 case Opt_iocharset:
1908 string = match_strdup(args);
1909 if (string == NULL)
1910 goto out_nomem;
1911
4fe9e963 1912 if (strnlen(string, 1024) >= 65) {
0b456f04 1913 pr_warn("CIFS: iocharset name too long.\n");
8830d7e0
SP
1914 goto cifs_parse_mount_err;
1915 }
1916
87e747cd 1917 if (strncasecmp(string, "default", 7) != 0) {
2bd50fb3 1918 kfree(vol->iocharset);
8830d7e0
SP
1919 vol->iocharset = kstrdup(string,
1920 GFP_KERNEL);
1921 if (!vol->iocharset) {
0b456f04 1922 pr_warn("CIFS: no memory for charset\n");
8830d7e0
SP
1923 goto cifs_parse_mount_err;
1924 }
1925 }
1926 /* if iocharset not set then load_nls_default
1927 * is used by caller
1928 */
f96637be 1929 cifs_dbg(FYI, "iocharset set to %s\n", string);
8830d7e0 1930 break;
8830d7e0
SP
1931 case Opt_netbiosname:
1932 string = match_strdup(args);
1933 if (string == NULL)
1934 goto out_nomem;
1935
8830d7e0
SP
1936 memset(vol->source_rfc1001_name, 0x20,
1937 RFC1001_NAME_LEN);
1938 /*
1939 * FIXME: are there cases in which a comma can
1940 * be valid in workstation netbios name (and
1941 * need special handling)?
1942 */
1943 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1944 /* don't ucase netbiosname for user */
1945 if (string[i] == 0)
1946 break;
1947 vol->source_rfc1001_name[i] = string[i];
1948 }
1949 /* The string has 16th byte zero still from
1950 * set at top of the function
1951 */
1952 if (i == RFC1001_NAME_LEN && string[i] != 0)
0b456f04 1953 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
8830d7e0
SP
1954 break;
1955 case Opt_servern:
1956 /* servernetbiosname specified override *SMBSERVER */
1957 string = match_strdup(args);
1958 if (string == NULL)
1959 goto out_nomem;
1960
8830d7e0
SP
1961 /* last byte, type, is 0x20 for servr type */
1962 memset(vol->target_rfc1001_name, 0x20,
1963 RFC1001_NAME_LEN_WITH_NULL);
1964
1965 /* BB are there cases in which a comma can be
1966 valid in this workstation netbios name
1967 (and need special handling)? */
1968
1969 /* user or mount helper must uppercase the
1970 netbios name */
1971 for (i = 0; i < 15; i++) {
1972 if (string[i] == 0)
1973 break;
1974 vol->target_rfc1001_name[i] = string[i];
1975 }
1976 /* The string has 16th byte zero still from
1977 set at top of the function */
1978 if (i == RFC1001_NAME_LEN && string[i] != 0)
0b456f04 1979 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
8830d7e0
SP
1980 break;
1981 case Opt_ver:
7e682f76 1982 /* version of mount userspace tools, not dialect */
8830d7e0
SP
1983 string = match_strdup(args);
1984 if (string == NULL)
1985 goto out_nomem;
1986
7e682f76 1987 /* If interface changes in mount.cifs bump to new ver */
87e747cd 1988 if (strncasecmp(string, "1", 1) == 0) {
7e682f76
SF
1989 if (strlen(string) > 1) {
1990 pr_warn("Bad mount helper ver=%s. Did "
1991 "you want SMB1 (CIFS) dialect "
1992 "and mean to type vers=1.0 "
1993 "instead?\n", string);
1994 goto cifs_parse_mount_err;
1995 }
8830d7e0
SP
1996 /* This is the default */
1997 break;
1998 }
1999 /* For all other value, error */
7e682f76 2000 pr_warn("CIFS: Invalid mount helper version specified\n");
b946845a 2001 goto cifs_parse_mount_err;
23db65f5 2002 case Opt_vers:
7e682f76 2003 /* protocol version (dialect) */
23db65f5
JL
2004 string = match_strdup(args);
2005 if (string == NULL)
2006 goto out_nomem;
2007
c7c137b9 2008 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
23db65f5 2009 goto cifs_parse_mount_err;
7e682f76 2010 got_version = true;
23db65f5 2011 break;
8830d7e0
SP
2012 case Opt_sec:
2013 string = match_strdup(args);
2014 if (string == NULL)
2015 goto out_nomem;
2016
8830d7e0
SP
2017 if (cifs_parse_security_flavors(string, vol) != 0)
2018 goto cifs_parse_mount_err;
2019 break;
15b6a473
JL
2020 case Opt_cache:
2021 string = match_strdup(args);
2022 if (string == NULL)
2023 goto out_nomem;
2024
2025 if (cifs_parse_cache_flavor(string, vol) != 0)
2026 goto cifs_parse_mount_err;
2027 break;
8830d7e0 2028 default:
d8162558
JL
2029 /*
2030 * An option we don't recognize. Save it off for later
2031 * if we haven't already found one
2032 */
2033 if (!invalid)
2034 invalid = data;
8830d7e0 2035 break;
1da177e4 2036 }
8830d7e0
SP
2037 /* Free up any allocated string */
2038 kfree(string);
2039 string = NULL;
1da177e4 2040 }
0eb8a132 2041
d8162558 2042 if (!sloppy && invalid) {
0b456f04 2043 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
d8162558
JL
2044 goto cifs_parse_mount_err;
2045 }
2046
8339dd32
LL
2047 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2048 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2049 goto cifs_parse_mount_err;
2050 }
2051
8a8798a5
JL
2052#ifndef CONFIG_KEYS
2053 /* Muliuser mounts require CONFIG_KEYS support */
2054 if (vol->multiuser) {
f96637be 2055 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
b946845a 2056 goto cifs_parse_mount_err;
0eb8a132 2057 }
8a8798a5 2058#endif
e5e69abd 2059 if (!vol->UNC) {
37d4f99b 2060 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
e5e69abd
JL
2061 goto cifs_parse_mount_err;
2062 }
0eb8a132 2063
62a1a439
JL
2064 /* make sure UNC has a share name */
2065 if (!strchr(vol->UNC + 3, '\\')) {
f96637be 2066 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
62a1a439
JL
2067 goto cifs_parse_mount_err;
2068 }
2069
b979aaa1 2070 if (!got_ip) {
29bb3158
DP
2071 int len;
2072 const char *slash;
2073
b979aaa1 2074 /* No ip= option specified? Try to get it from UNC */
29bb3158
DP
2075 /* Use the address part of the UNC. */
2076 slash = strchr(&vol->UNC[2], '\\');
2077 len = slash - &vol->UNC[2];
2078 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
0b456f04 2079 pr_err("Unable to determine destination address.\n");
b979aaa1
JL
2080 goto cifs_parse_mount_err;
2081 }
2082 }
2083
2084 /* set the port that we got earlier */
2085 cifs_set_port(dstaddr, port);
1da177e4 2086
9b9d6b24
JL
2087 if (uid_specified)
2088 vol->override_uid = override_uid;
2089 else if (override_uid == 1)
0b456f04 2090 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
9b9d6b24
JL
2091
2092 if (gid_specified)
2093 vol->override_gid = override_gid;
2094 else if (override_gid == 1)
0b456f04 2095 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
9b9d6b24 2096
7e682f76
SF
2097 if (got_version == false)
2098 pr_warn("No dialect specified on mount. Default has changed to "
9764c02f 2099 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
7e682f76 2100 "(SMB1). To use the less secure SMB1 dialect to access "
9764c02f
SF
2101 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2102 " on mount.\n");
7e682f76 2103
b946845a 2104 kfree(mountdata_copy);
1da177e4 2105 return 0;
b946845a 2106
8830d7e0 2107out_nomem:
0b456f04 2108 pr_warn("Could not allocate temporary buffer\n");
b946845a 2109cifs_parse_mount_err:
8830d7e0 2110 kfree(string);
b946845a
SF
2111 kfree(mountdata_copy);
2112 return 1;
1da177e4
LT
2113}
2114
3eb9a889
BG
2115/** Returns true if srcaddr isn't specified and rhs isn't
2116 * specified, or if srcaddr is specified and
2117 * matches the IP address of the rhs argument.
2118 */
2119static bool
2120srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2121{
2122 switch (srcaddr->sa_family) {
2123 case AF_UNSPEC:
2124 return (rhs->sa_family == AF_UNSPEC);
2125 case AF_INET: {
2126 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2127 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2128 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2129 }
2130 case AF_INET6: {
2131 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
e3e2775c 2132 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
3eb9a889
BG
2133 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2134 }
2135 default:
2136 WARN_ON(1);
2137 return false; /* don't expect to be here */
2138 }
2139}
2140
4b886136
PS
2141/*
2142 * If no port is specified in addr structure, we try to match with 445 port
2143 * and if it fails - with 139 ports. It should be called only if address
2144 * families of server and addr are equal.
2145 */
2146static bool
2147match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2148{
6da97910 2149 __be16 port, *sport;
4b886136
PS
2150
2151 switch (addr->sa_family) {
2152 case AF_INET:
2153 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2154 port = ((struct sockaddr_in *) addr)->sin_port;
2155 break;
2156 case AF_INET6:
2157 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2158 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2159 break;
2160 default:
2161 WARN_ON(1);
2162 return false;
2163 }
2164
2165 if (!port) {
2166 port = htons(CIFS_PORT);
2167 if (port == *sport)
2168 return true;
2169
2170 port = htons(RFC1001_PORT);
2171 }
2172
2173 return port == *sport;
2174}
3eb9a889 2175
4515148e 2176static bool
3eb9a889
BG
2177match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2178 struct sockaddr *srcaddr)
4515148e 2179{
4515148e 2180 switch (addr->sa_family) {
a9f1b85e
PS
2181 case AF_INET: {
2182 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2183 struct sockaddr_in *srv_addr4 =
2184 (struct sockaddr_in *)&server->dstaddr;
2185
2186 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
4515148e 2187 return false;
4515148e 2188 break;
a9f1b85e
PS
2189 }
2190 case AF_INET6: {
2191 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2192 struct sockaddr_in6 *srv_addr6 =
2193 (struct sockaddr_in6 *)&server->dstaddr;
2194
4515148e 2195 if (!ipv6_addr_equal(&addr6->sin6_addr,
a9f1b85e 2196 &srv_addr6->sin6_addr))
4515148e 2197 return false;
a9f1b85e 2198 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
4515148e 2199 return false;
4515148e
JL
2200 break;
2201 }
a9f1b85e
PS
2202 default:
2203 WARN_ON(1);
2204 return false; /* don't expect to be here */
2205 }
4515148e 2206
3eb9a889
BG
2207 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2208 return false;
2209
4515148e
JL
2210 return true;
2211}
2212
daf5b0b6
JL
2213static bool
2214match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2215{
3f618223
JL
2216 /*
2217 * The select_sectype function should either return the vol->sectype
2218 * that was specified, or "Unspecified" if that sectype was not
2219 * compatible with the given NEGOTIATE request.
2220 */
ef65aaed
SP
2221 if (server->ops->select_sectype(server, vol->sectype)
2222 == Unspecified)
daf5b0b6 2223 return false;
daf5b0b6 2224
3f618223
JL
2225 /*
2226 * Now check if signing mode is acceptable. No need to check
2227 * global_secflags at this point since if MUST_SIGN is set then
2228 * the server->sign had better be too.
2229 */
38d77c50
JL
2230 if (vol->sign && !server->sign)
2231 return false;
daf5b0b6
JL
2232
2233 return true;
2234}
2235
9fa114f7 2236static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
37bb04e5 2237{
9fa114f7
JL
2238 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2239
a0b3df5c
JL
2240 if (vol->nosharesock)
2241 return 0;
2242
9764c02f 2243 /* BB update this for smb3any and default case */
23db65f5
JL
2244 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2245 return 0;
2246
37bb04e5
PS
2247 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2248 return 0;
2249
2250 if (!match_address(server, addr,
2251 (struct sockaddr *)&vol->srcaddr))
2252 return 0;
2253
2254 if (!match_port(server, addr))
2255 return 0;
2256
2257 if (!match_security(server, vol))
2258 return 0;
2259
b782fcc1 2260 if (server->echo_interval != vol->echo_interval * HZ)
adfeb3e0
SF
2261 return 0;
2262
8339dd32
LL
2263 if (server->rdma != vol->rdma)
2264 return 0;
2265
37bb04e5
PS
2266 return 1;
2267}
2268
e7ddee90 2269static struct TCP_Server_Info *
9fa114f7 2270cifs_find_tcp_session(struct smb_vol *vol)
1da177e4 2271{
e7ddee90 2272 struct TCP_Server_Info *server;
e7ddee90 2273
3f9bcca7 2274 spin_lock(&cifs_tcp_ses_lock);
4515148e 2275 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
9fa114f7 2276 if (!match_server(server, vol))
daf5b0b6
JL
2277 continue;
2278
e7ddee90 2279 ++server->srv_count;
3f9bcca7 2280 spin_unlock(&cifs_tcp_ses_lock);
f96637be 2281 cifs_dbg(FYI, "Existing tcp session with server found\n");
e7ddee90 2282 return server;
1da177e4 2283 }
3f9bcca7 2284 spin_unlock(&cifs_tcp_ses_lock);
1da177e4
LT
2285 return NULL;
2286}
1b20d672 2287
53e0e11e
PS
2288void
2289cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
1da177e4 2290{
a5c3e1c7
SF
2291 struct task_struct *task;
2292
3f9bcca7 2293 spin_lock(&cifs_tcp_ses_lock);
e7ddee90 2294 if (--server->srv_count > 0) {
3f9bcca7 2295 spin_unlock(&cifs_tcp_ses_lock);
e7ddee90 2296 return;
1da177e4 2297 }
1b20d672 2298
f1d0c998
RL
2299 put_net(cifs_net_ns(server));
2300
e7ddee90 2301 list_del_init(&server->tcp_ses_list);
3f9bcca7 2302 spin_unlock(&cifs_tcp_ses_lock);
dea570e0 2303
c74093b6
JL
2304 cancel_delayed_work_sync(&server->echo);
2305
53e0e11e
PS
2306 if (from_reconnect)
2307 /*
2308 * Avoid deadlock here: reconnect work calls
2309 * cifs_put_tcp_session() at its end. Need to be sure
2310 * that reconnect work does nothing with server pointer after
2311 * that step.
2312 */
2313 cancel_delayed_work(&server->reconnect);
2314 else
2315 cancel_delayed_work_sync(&server->reconnect);
53e0e11e 2316
e7ddee90
JL
2317 spin_lock(&GlobalMid_Lock);
2318 server->tcpStatus = CifsExiting;
2319 spin_unlock(&GlobalMid_Lock);
dea570e0 2320
026e93dc 2321 cifs_crypto_secmech_release(server);
488f1d2d
SJ
2322 cifs_fscache_release_client_cookie(server);
2323
21e73393
SP
2324 kfree(server->session_key.response);
2325 server->session_key.response = NULL;
2326 server->session_key.len = 0;
a5c3e1c7
SF
2327
2328 task = xchg(&server->tsk, NULL);
2329 if (task)
2330 force_sig(SIGKILL, task);
1da177e4
LT
2331}
2332
63c038c2
JL
2333static struct TCP_Server_Info *
2334cifs_get_tcp_session(struct smb_vol *volume_info)
2335{
2336 struct TCP_Server_Info *tcp_ses = NULL;
63c038c2
JL
2337 int rc;
2338
f96637be 2339 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
63c038c2
JL
2340
2341 /* see if we already have a matching tcp_ses */
9fa114f7 2342 tcp_ses = cifs_find_tcp_session(volume_info);
63c038c2
JL
2343 if (tcp_ses)
2344 return tcp_ses;
2345
2346 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2347 if (!tcp_ses) {
2348 rc = -ENOMEM;
2349 goto out_err;
2350 }
2351
23db65f5
JL
2352 tcp_ses->ops = volume_info->ops;
2353 tcp_ses->vals = volume_info->vals;
f1d0c998 2354 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
63c038c2
JL
2355 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2356 if (IS_ERR(tcp_ses->hostname)) {
2357 rc = PTR_ERR(tcp_ses->hostname);
f7c5445a 2358 goto out_err_crypto_release;
63c038c2
JL
2359 }
2360
2361 tcp_ses->noblocksnd = volume_info->noblocksnd;
2362 tcp_ses->noautotune = volume_info->noautotune;
6a5fa236 2363 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
8339dd32 2364 tcp_ses->rdma = volume_info->rdma;
fc40f9cf 2365 tcp_ses->in_flight = 0;
2d86dbc9 2366 tcp_ses->credits = 1;
63c038c2
JL
2367 init_waitqueue_head(&tcp_ses->response_q);
2368 init_waitqueue_head(&tcp_ses->request_q);
2369 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2370 mutex_init(&tcp_ses->srv_mutex);
2371 memcpy(tcp_ses->workstation_RFC1001_name,
2372 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2373 memcpy(tcp_ses->server_RFC1001_name,
2374 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
5d0d2882 2375 tcp_ses->session_estab = false;
63c038c2 2376 tcp_ses->sequence_number = 0;
fda35943 2377 tcp_ses->lstrp = jiffies;
58fa015f 2378 spin_lock_init(&tcp_ses->req_lock);
63c038c2
JL
2379 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2380 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
c74093b6 2381 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
53e0e11e
PS
2382 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2383 mutex_init(&tcp_ses->reconnect_mutex);
9fa114f7
JL
2384 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2385 sizeof(tcp_ses->srcaddr));
2386 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2387 sizeof(tcp_ses->dstaddr));
fa70b87c 2388 generate_random_uuid(tcp_ses->client_guid);
63c038c2
JL
2389 /*
2390 * at this point we are the only ones with the pointer
2391 * to the struct since the kernel thread not created yet
2392 * no need to spinlock this init of tcpStatus or srv_count
2393 */
2394 tcp_ses->tcpStatus = CifsNew;
2395 ++tcp_ses->srv_count;
2396
adfeb3e0
SF
2397 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2398 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2399 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2400 else
2401 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
2f894646
LL
2402 if (tcp_ses->rdma) {
2403#ifndef CONFIG_CIFS_SMB_DIRECT
2404 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2405 rc = -ENOENT;
2406 goto out_err_crypto_release;
2407#endif
2408 tcp_ses->smbd_conn = smbd_get_connection(
2409 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2410 if (tcp_ses->smbd_conn) {
2411 cifs_dbg(VFS, "RDMA transport established\n");
2412 rc = 0;
2413 goto smbd_connected;
2414 } else {
2415 rc = -ENOENT;
2416 goto out_err_crypto_release;
2417 }
2418 }
a9f1b85e 2419 rc = ip_connect(tcp_ses);
63c038c2 2420 if (rc < 0) {
f96637be 2421 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
f7c5445a 2422 goto out_err_crypto_release;
63c038c2 2423 }
2f894646 2424smbd_connected:
63c038c2
JL
2425 /*
2426 * since we're in a cifs function already, we know that
2427 * this will succeed. No need for try_module_get().
2428 */
2429 __module_get(THIS_MODULE);
7c97c200 2430 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
63c038c2
JL
2431 tcp_ses, "cifsd");
2432 if (IS_ERR(tcp_ses->tsk)) {
2433 rc = PTR_ERR(tcp_ses->tsk);
f96637be 2434 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
63c038c2 2435 module_put(THIS_MODULE);
f7c5445a 2436 goto out_err_crypto_release;
63c038c2 2437 }
fd88ce93 2438 tcp_ses->tcpStatus = CifsNeedNegotiate;
63c038c2
JL
2439
2440 /* thread spawned, put it on the list */
3f9bcca7 2441 spin_lock(&cifs_tcp_ses_lock);
63c038c2 2442 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
3f9bcca7 2443 spin_unlock(&cifs_tcp_ses_lock);
63c038c2 2444
488f1d2d
SJ
2445 cifs_fscache_get_client_cookie(tcp_ses);
2446
c74093b6 2447 /* queue echo request delayed work */
adfeb3e0 2448 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
c74093b6 2449
63c038c2
JL
2450 return tcp_ses;
2451
f7c5445a 2452out_err_crypto_release:
026e93dc 2453 cifs_crypto_secmech_release(tcp_ses);
d2b91521 2454
f1d0c998
RL
2455 put_net(cifs_net_ns(tcp_ses));
2456
63c038c2
JL
2457out_err:
2458 if (tcp_ses) {
8347a5cd
SF
2459 if (!IS_ERR(tcp_ses->hostname))
2460 kfree(tcp_ses->hostname);
63c038c2
JL
2461 if (tcp_ses->ssocket)
2462 sock_release(tcp_ses->ssocket);
2463 kfree(tcp_ses);
2464 }
2465 return ERR_PTR(rc);
2466}
2467
96daf2b0 2468static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
37bb04e5 2469{
3f618223
JL
2470 if (vol->sectype != Unspecified &&
2471 vol->sectype != ses->sectype)
2472 return 0;
2473
2474 switch (ses->sectype) {
37bb04e5 2475 case Kerberos:
64ed39dd 2476 if (!uid_eq(vol->cred_uid, ses->cred_uid))
37bb04e5
PS
2477 return 0;
2478 break;
2479 default:
04febabc
JL
2480 /* NULL username means anonymous session */
2481 if (ses->user_name == NULL) {
2482 if (!vol->nullauth)
2483 return 0;
2484 break;
2485 }
2486
37bb04e5 2487 /* anything else takes username/password */
04febabc
JL
2488 if (strncmp(ses->user_name,
2489 vol->username ? vol->username : "",
8c3a2b4c 2490 CIFS_MAX_USERNAME_LEN))
37bb04e5 2491 return 0;
08b37d51 2492 if ((vol->username && strlen(vol->username) != 0) &&
37bb04e5
PS
2493 ses->password != NULL &&
2494 strncmp(ses->password,
2495 vol->password ? vol->password : "",
8c3a2b4c 2496 CIFS_MAX_PASSWORD_LEN))
37bb04e5
PS
2497 return 0;
2498 }
2499 return 1;
2500}
2501
b327a717
AA
2502/**
2503 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2504 *
2505 * A new IPC connection is made and stored in the session
2506 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2507 */
2508static int
2509cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2510{
2511 int rc = 0, xid;
2512 struct cifs_tcon *tcon;
2513 struct nls_table *nls_codepage;
2514 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2515 bool seal = false;
2516
2517 /*
2518 * If the mount request that resulted in the creation of the
2519 * session requires encryption, force IPC to be encrypted too.
2520 */
2521 if (volume_info->seal) {
2522 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2523 seal = true;
2524 else {
2525 cifs_dbg(VFS,
2526 "IPC: server doesn't support encryption\n");
2527 return -EOPNOTSUPP;
2528 }
2529 }
2530
2531 tcon = tconInfoAlloc();
2532 if (tcon == NULL)
2533 return -ENOMEM;
2534
2535 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->serverName);
2536
2537 /* cannot fail */
2538 nls_codepage = load_nls_default();
2539
2540 xid = get_xid();
2541 tcon->ses = ses;
2542 tcon->ipc = true;
2543 tcon->seal = seal;
2544 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2545 free_xid(xid);
2546
2547 if (rc) {
2548 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2549 tconInfoFree(tcon);
2550 goto out;
2551 }
2552
2553 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2554
2555 ses->tcon_ipc = tcon;
2556out:
2557 unload_nls(nls_codepage);
2558 return rc;
2559}
2560
2561/**
2562 * cifs_free_ipc - helper to release the session IPC tcon
2563 *
2564 * Needs to be called everytime a session is destroyed
2565 */
2566static int
2567cifs_free_ipc(struct cifs_ses *ses)
2568{
2569 int rc = 0, xid;
2570 struct cifs_tcon *tcon = ses->tcon_ipc;
2571
2572 if (tcon == NULL)
2573 return 0;
2574
2575 if (ses->server->ops->tree_disconnect) {
2576 xid = get_xid();
2577 rc = ses->server->ops->tree_disconnect(xid, tcon);
2578 free_xid(xid);
2579 }
2580
2581 if (rc)
2582 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2583
2584 tconInfoFree(tcon);
2585 ses->tcon_ipc = NULL;
2586 return rc;
2587}
2588
96daf2b0 2589static struct cifs_ses *
4ff67b72 2590cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
1da177e4 2591{
96daf2b0 2592 struct cifs_ses *ses;
dea570e0 2593
3f9bcca7 2594 spin_lock(&cifs_tcp_ses_lock);
4ff67b72 2595 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
7f48558e
SP
2596 if (ses->status == CifsExiting)
2597 continue;
37bb04e5
PS
2598 if (!match_session(ses, vol))
2599 continue;
14fbf50d 2600 ++ses->ses_count;
3f9bcca7 2601 spin_unlock(&cifs_tcp_ses_lock);
14fbf50d
JL
2602 return ses;
2603 }
3f9bcca7 2604 spin_unlock(&cifs_tcp_ses_lock);
14fbf50d
JL
2605 return NULL;
2606}
dea570e0 2607
14fbf50d 2608static void
96daf2b0 2609cifs_put_smb_ses(struct cifs_ses *ses)
14fbf50d 2610{
7f48558e 2611 unsigned int rc, xid;
14fbf50d 2612 struct TCP_Server_Info *server = ses->server;
dea570e0 2613
f96637be 2614 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
7f48558e 2615
3f9bcca7 2616 spin_lock(&cifs_tcp_ses_lock);
7f48558e
SP
2617 if (ses->status == CifsExiting) {
2618 spin_unlock(&cifs_tcp_ses_lock);
2619 return;
2620 }
14fbf50d 2621 if (--ses->ses_count > 0) {
3f9bcca7 2622 spin_unlock(&cifs_tcp_ses_lock);
14fbf50d
JL
2623 return;
2624 }
7f48558e
SP
2625 if (ses->status == CifsGood)
2626 ses->status = CifsExiting;
3f9bcca7 2627 spin_unlock(&cifs_tcp_ses_lock);
dea570e0 2628
b327a717
AA
2629 cifs_free_ipc(ses);
2630
7f48558e 2631 if (ses->status == CifsExiting && server->ops->logoff) {
6d5786a3 2632 xid = get_xid();
7f48558e
SP
2633 rc = server->ops->logoff(xid, ses);
2634 if (rc)
2635 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2636 __func__, rc);
6d5786a3 2637 _free_xid(xid);
14fbf50d 2638 }
7f48558e
SP
2639
2640 spin_lock(&cifs_tcp_ses_lock);
2641 list_del_init(&ses->smb_ses_list);
2642 spin_unlock(&cifs_tcp_ses_lock);
2643
14fbf50d 2644 sesInfoFree(ses);
53e0e11e 2645 cifs_put_tcp_session(server, 0);
14fbf50d 2646}
dea570e0 2647
8a8798a5
JL
2648#ifdef CONFIG_KEYS
2649
057d6332
CG
2650/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2651#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
8a8798a5
JL
2652
2653/* Populate username and pw fields from keyring if possible */
2654static int
2655cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2656{
2657 int rc = 0;
146aa8b1
DH
2658 const char *delim, *payload;
2659 char *desc;
8a8798a5
JL
2660 ssize_t len;
2661 struct key *key;
2662 struct TCP_Server_Info *server = ses->server;
2663 struct sockaddr_in *sa;
2664 struct sockaddr_in6 *sa6;
146aa8b1 2665 const struct user_key_payload *upayload;
8a8798a5
JL
2666
2667 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2668 if (!desc)
2669 return -ENOMEM;
2670
2671 /* try to find an address key first */
2672 switch (server->dstaddr.ss_family) {
2673 case AF_INET:
2674 sa = (struct sockaddr_in *)&server->dstaddr;
2675 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2676 break;
2677 case AF_INET6:
2678 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2679 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2680 break;
2681 default:
f96637be
JP
2682 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2683 server->dstaddr.ss_family);
8a8798a5
JL
2684 rc = -EINVAL;
2685 goto out_err;
2686 }
2687
f96637be 2688 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
8a8798a5
JL
2689 key = request_key(&key_type_logon, desc, "");
2690 if (IS_ERR(key)) {
2691 if (!ses->domainName) {
f96637be 2692 cifs_dbg(FYI, "domainName is NULL\n");
8a8798a5
JL
2693 rc = PTR_ERR(key);
2694 goto out_err;
2695 }
2696
2697 /* didn't work, try to find a domain key */
2698 sprintf(desc, "cifs:d:%s", ses->domainName);
f96637be 2699 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
8a8798a5
JL
2700 key = request_key(&key_type_logon, desc, "");
2701 if (IS_ERR(key)) {
2702 rc = PTR_ERR(key);
2703 goto out_err;
2704 }
2705 }
2706
2707 down_read(&key->sem);
0837e49a 2708 upayload = user_key_payload_locked(key);
8a8798a5 2709 if (IS_ERR_OR_NULL(upayload)) {
4edc53c1 2710 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
8a8798a5
JL
2711 goto out_key_put;
2712 }
2713
2714 /* find first : in payload */
146aa8b1 2715 payload = upayload->data;
8a8798a5 2716 delim = strnchr(payload, upayload->datalen, ':');
f96637be 2717 cifs_dbg(FYI, "payload=%s\n", payload);
8a8798a5 2718 if (!delim) {
f96637be
JP
2719 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2720 upayload->datalen);
8a8798a5
JL
2721 rc = -EINVAL;
2722 goto out_key_put;
2723 }
2724
2725 len = delim - payload;
8c3a2b4c 2726 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
f96637be
JP
2727 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2728 len);
8a8798a5
JL
2729 rc = -EINVAL;
2730 goto out_key_put;
2731 }
2732
2733 vol->username = kstrndup(payload, len, GFP_KERNEL);
2734 if (!vol->username) {
f96637be
JP
2735 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2736 len);
8a8798a5
JL
2737 rc = -ENOMEM;
2738 goto out_key_put;
2739 }
f96637be 2740 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
8a8798a5
JL
2741
2742 len = key->datalen - (len + 1);
8c3a2b4c 2743 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
f96637be 2744 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
8a8798a5
JL
2745 rc = -EINVAL;
2746 kfree(vol->username);
2747 vol->username = NULL;
2748 goto out_key_put;
2749 }
2750
2751 ++delim;
2752 vol->password = kstrndup(delim, len, GFP_KERNEL);
2753 if (!vol->password) {
f96637be
JP
2754 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2755 len);
8a8798a5
JL
2756 rc = -ENOMEM;
2757 kfree(vol->username);
2758 vol->username = NULL;
2759 goto out_key_put;
2760 }
2761
2762out_key_put:
2763 up_read(&key->sem);
2764 key_put(key);
2765out_err:
2766 kfree(desc);
f96637be 2767 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
8a8798a5
JL
2768 return rc;
2769}
2770#else /* ! CONFIG_KEYS */
2771static inline int
2772cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2773 struct cifs_ses *ses __attribute__((unused)))
2774{
2775 return -ENOSYS;
2776}
2777#endif /* CONFIG_KEYS */
2778
4a1360d0
AA
2779/**
2780 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2781 *
2782 * This function assumes it is being called from cifs_mount() where we
2783 * already got a server reference (server refcount +1). See
2784 * cifs_get_tcon() for refcount explanations.
2785 */
96daf2b0 2786static struct cifs_ses *
36988c76
JL
2787cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2788{
286170aa
PS
2789 int rc = -ENOMEM;
2790 unsigned int xid;
96daf2b0 2791 struct cifs_ses *ses;
a9f1b85e
PS
2792 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2793 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
36988c76 2794
6d5786a3 2795 xid = get_xid();
36988c76 2796
4ff67b72 2797 ses = cifs_find_smb_ses(server, volume_info);
36988c76 2798 if (ses) {
f96637be
JP
2799 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2800 ses->status);
36988c76 2801
36988c76 2802 mutex_lock(&ses->session_mutex);
198b5682
JL
2803 rc = cifs_negotiate_protocol(xid, ses);
2804 if (rc) {
2805 mutex_unlock(&ses->session_mutex);
2806 /* problem -- put our ses reference */
2807 cifs_put_smb_ses(ses);
6d5786a3 2808 free_xid(xid);
198b5682
JL
2809 return ERR_PTR(rc);
2810 }
36988c76 2811 if (ses->need_reconnect) {
f96637be 2812 cifs_dbg(FYI, "Session needs reconnect\n");
36988c76
JL
2813 rc = cifs_setup_session(xid, ses,
2814 volume_info->local_nls);
2815 if (rc) {
2816 mutex_unlock(&ses->session_mutex);
2817 /* problem -- put our reference */
2818 cifs_put_smb_ses(ses);
6d5786a3 2819 free_xid(xid);
36988c76
JL
2820 return ERR_PTR(rc);
2821 }
2822 }
2823 mutex_unlock(&ses->session_mutex);
460cf341
JL
2824
2825 /* existing SMB ses has a server reference already */
53e0e11e 2826 cifs_put_tcp_session(server, 0);
6d5786a3 2827 free_xid(xid);
36988c76
JL
2828 return ses;
2829 }
2830
f96637be 2831 cifs_dbg(FYI, "Existing smb sess not found\n");
36988c76
JL
2832 ses = sesInfoAlloc();
2833 if (ses == NULL)
2834 goto get_ses_fail;
2835
2836 /* new SMB session uses our server ref */
2837 ses->server = server;
a9f1b85e
PS
2838 if (server->dstaddr.ss_family == AF_INET6)
2839 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
36988c76 2840 else
a9f1b85e 2841 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
36988c76 2842
8727c8a8
SF
2843 if (volume_info->username) {
2844 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2845 if (!ses->user_name)
2846 goto get_ses_fail;
2847 }
36988c76
JL
2848
2849 /* volume_info->password freed at unmount */
2850 if (volume_info->password) {
2851 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2852 if (!ses->password)
2853 goto get_ses_fail;
2854 }
2855 if (volume_info->domainname) {
d3686d54
SP
2856 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2857 if (!ses->domainName)
2858 goto get_ses_fail;
36988c76 2859 }
39566443
GP
2860 if (volume_info->domainauto)
2861 ses->domainAuto = volume_info->domainauto;
3e4b3e1f 2862 ses->cred_uid = volume_info->cred_uid;
36988c76 2863 ses->linux_uid = volume_info->linux_uid;
d9b94201 2864
28e11bd8
JL
2865 ses->sectype = volume_info->sectype;
2866 ses->sign = volume_info->sign;
36988c76
JL
2867
2868 mutex_lock(&ses->session_mutex);
198b5682
JL
2869 rc = cifs_negotiate_protocol(xid, ses);
2870 if (!rc)
2871 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
36988c76 2872 mutex_unlock(&ses->session_mutex);
c8e56f1f 2873 if (rc)
36988c76
JL
2874 goto get_ses_fail;
2875
2876 /* success, put it on the list */
3f9bcca7 2877 spin_lock(&cifs_tcp_ses_lock);
36988c76 2878 list_add(&ses->smb_ses_list, &server->smb_ses_list);
3f9bcca7 2879 spin_unlock(&cifs_tcp_ses_lock);
36988c76 2880
6d5786a3 2881 free_xid(xid);
b327a717
AA
2882
2883 cifs_setup_ipc(ses, volume_info);
2884
36988c76
JL
2885 return ses;
2886
2887get_ses_fail:
2888 sesInfoFree(ses);
6d5786a3 2889 free_xid(xid);
36988c76
JL
2890 return ERR_PTR(rc);
2891}
2892
ae6f8dd4 2893static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
37bb04e5
PS
2894{
2895 if (tcon->tidStatus == CifsExiting)
2896 return 0;
ae6f8dd4 2897 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
37bb04e5 2898 return 0;
ae6f8dd4
PS
2899 if (tcon->seal != volume_info->seal)
2900 return 0;
ae6f8dd4
PS
2901 if (tcon->snapshot_time != volume_info->snapshot_time)
2902 return 0;
37bb04e5
PS
2903 return 1;
2904}
2905
96daf2b0 2906static struct cifs_tcon *
8b217fe7 2907cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
f1987b44
JL
2908{
2909 struct list_head *tmp;
96daf2b0 2910 struct cifs_tcon *tcon;
f1987b44 2911
3f9bcca7 2912 spin_lock(&cifs_tcp_ses_lock);
f1987b44 2913 list_for_each(tmp, &ses->tcon_list) {
96daf2b0 2914 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
ae6f8dd4 2915 if (!match_tcon(tcon, volume_info))
f1987b44 2916 continue;
f1987b44 2917 ++tcon->tc_count;
3f9bcca7 2918 spin_unlock(&cifs_tcp_ses_lock);
dea570e0 2919 return tcon;
1da177e4 2920 }
3f9bcca7 2921 spin_unlock(&cifs_tcp_ses_lock);
1da177e4
LT
2922 return NULL;
2923}
2924
53e0e11e 2925void
96daf2b0 2926cifs_put_tcon(struct cifs_tcon *tcon)
f1987b44 2927{
2e6e02ab 2928 unsigned int xid;
b327a717 2929 struct cifs_ses *ses;
f1987b44 2930
b327a717
AA
2931 /*
2932 * IPC tcon share the lifetime of their session and are
2933 * destroyed in the session put function
2934 */
2935 if (tcon == NULL || tcon->ipc)
2936 return;
2937
2938 ses = tcon->ses;
f96637be 2939 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
3f9bcca7 2940 spin_lock(&cifs_tcp_ses_lock);
f1987b44 2941 if (--tcon->tc_count > 0) {
3f9bcca7 2942 spin_unlock(&cifs_tcp_ses_lock);
f1987b44
JL
2943 return;
2944 }
2945
2946 list_del_init(&tcon->tcon_list);
3f9bcca7 2947 spin_unlock(&cifs_tcp_ses_lock);
f1987b44 2948
6d5786a3 2949 xid = get_xid();
2e6e02ab
PS
2950 if (ses->server->ops->tree_disconnect)
2951 ses->server->ops->tree_disconnect(xid, tcon);
6d5786a3 2952 _free_xid(xid);
f1987b44 2953
d03382ce 2954 cifs_fscache_release_super_cookie(tcon);
9f841593 2955 tconInfoFree(tcon);
f1987b44
JL
2956 cifs_put_smb_ses(ses);
2957}
2958
4a1360d0
AA
2959/**
2960 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
2961 *
2962 * - tcon refcount is the number of mount points using the tcon.
2963 * - ses refcount is the number of tcon using the session.
2964 *
2965 * 1. This function assumes it is being called from cifs_mount() where
2966 * we already got a session reference (ses refcount +1).
2967 *
2968 * 2. Since we're in the context of adding a mount point, the end
2969 * result should be either:
2970 *
2971 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2972 * its session refcount incremented (1 new tcon). This +1 was
2973 * already done in (1).
2974 *
2975 * b) an existing tcon with refcount+1 (add a mount point to it) and
2976 * identical ses refcount (no new tcon). Because of (1) we need to
2977 * decrement the ses refcount.
2978 */
96daf2b0
SF
2979static struct cifs_tcon *
2980cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
d00c28de
JL
2981{
2982 int rc, xid;
96daf2b0 2983 struct cifs_tcon *tcon;
d00c28de 2984
8b217fe7 2985 tcon = cifs_find_tcon(ses, volume_info);
d00c28de 2986 if (tcon) {
4a1360d0
AA
2987 /*
2988 * tcon has refcount already incremented but we need to
2989 * decrement extra ses reference gotten by caller (case b)
2990 */
f96637be 2991 cifs_dbg(FYI, "Found match on UNC path\n");
d00c28de 2992 cifs_put_smb_ses(ses);
d00c28de
JL
2993 return tcon;
2994 }
2995
2e6e02ab
PS
2996 if (!ses->server->ops->tree_connect) {
2997 rc = -ENOSYS;
2998 goto out_fail;
2999 }
3000
d00c28de
JL
3001 tcon = tconInfoAlloc();
3002 if (tcon == NULL) {
3003 rc = -ENOMEM;
3004 goto out_fail;
3005 }
3006
8b217fe7 3007 if (volume_info->snapshot_time) {
8b217fe7
SF
3008 if (ses->server->vals->protocol_id == 0) {
3009 cifs_dbg(VFS,
3010 "Use SMB2 or later for snapshot mount option\n");
3011 rc = -EOPNOTSUPP;
3012 goto out_fail;
3013 } else
3014 tcon->snapshot_time = volume_info->snapshot_time;
8b217fe7
SF
3015 }
3016
d00c28de
JL
3017 tcon->ses = ses;
3018 if (volume_info->password) {
3019 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3020 if (!tcon->password) {
3021 rc = -ENOMEM;
3022 goto out_fail;
3023 }
3024 }
3025
23657ad7
SF
3026 if (volume_info->seal) {
3027 if (ses->server->vals->protocol_id == 0) {
3028 cifs_dbg(VFS,
3029 "SMB3 or later required for encryption\n");
3030 rc = -EOPNOTSUPP;
3031 goto out_fail;
3032 } else if (tcon->ses->server->capabilities &
3033 SMB2_GLOBAL_CAP_ENCRYPTION)
3034 tcon->seal = true;
3035 else {
3036 cifs_dbg(VFS, "Encryption is not supported on share\n");
3037 rc = -EOPNOTSUPP;
3038 goto out_fail;
3039 }
3040 }
3041
b326614e 3042#ifdef CONFIG_CIFS_SMB311
8505c8bf
SF
3043 if (volume_info->linux_ext) {
3044 if (ses->server->posix_ext_supported) {
b326614e 3045 tcon->posix_extensions = true;
2fbb5644
SF
3046 printk_once(KERN_WARNING
3047 "SMB3.11 POSIX Extensions are experimental\n");
8505c8bf
SF
3048 } else {
3049 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3050 rc = -EOPNOTSUPP;
3051 goto out_fail;
2fbb5644 3052 }
b326614e
SF
3053 }
3054#endif /* 311 */
3055
2e6e02ab
PS
3056 /*
3057 * BB Do we need to wrap session_mutex around this TCon call and Unix
3058 * SetFS as we do on SessSetup and reconnect?
3059 */
6d5786a3 3060 xid = get_xid();
2e6e02ab
PS
3061 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3062 volume_info->local_nls);
6d5786a3 3063 free_xid(xid);
f96637be 3064 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
d00c28de
JL
3065 if (rc)
3066 goto out_fail;
3067
3068 if (volume_info->nodfs) {
3069 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
f96637be 3070 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
d00c28de 3071 }
b618f001
SF
3072 tcon->use_persistent = false;
3073 /* check if SMB2 or later, CIFS does not support persistent handles */
3074 if (volume_info->persistent) {
3075 if (ses->server->vals->protocol_id == 0) {
3076 cifs_dbg(VFS,
3077 "SMB3 or later required for persistent handles\n");
3078 rc = -EOPNOTSUPP;
3079 goto out_fail;
3080 } else if (ses->server->capabilities &
3081 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3082 tcon->use_persistent = true;
3083 else /* persistent handles requested but not supported */ {
3084 cifs_dbg(VFS,
3085 "Persistent handles not supported on share\n");
3086 rc = -EOPNOTSUPP;
3087 goto out_fail;
3088 }
3089 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3090 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3091 && (volume_info->nopersistent == false)) {
3092 cifs_dbg(FYI, "enabling persistent handles\n");
3093 tcon->use_persistent = true;
592fafe6
SF
3094 } else if (volume_info->resilient) {
3095 if (ses->server->vals->protocol_id == 0) {
3096 cifs_dbg(VFS,
3097 "SMB2.1 or later required for resilient handles\n");
3098 rc = -EOPNOTSUPP;
3099 goto out_fail;
3100 }
3101 tcon->use_resilient = true;
b618f001
SF
3102 }
3103
2e6e02ab
PS
3104 /*
3105 * We can have only one retry value for a connection to a share so for
3106 * resources mounted more than once to the same server share the last
3107 * value passed in for the retry flag is used.
3108 */
d00c28de
JL
3109 tcon->retry = volume_info->retry;
3110 tcon->nocase = volume_info->nocase;
3d4ef9a1 3111 tcon->nohandlecache = volume_info->nohandlecache;
d00c28de 3112 tcon->local_lease = volume_info->local_lease;
233839b1 3113 INIT_LIST_HEAD(&tcon->pending_opens);
d00c28de 3114
3f9bcca7 3115 spin_lock(&cifs_tcp_ses_lock);
d00c28de 3116 list_add(&tcon->tcon_list, &ses->tcon_list);
3f9bcca7 3117 spin_unlock(&cifs_tcp_ses_lock);
d00c28de 3118
d03382ce
SJ
3119 cifs_fscache_get_super_cookie(tcon);
3120
d00c28de
JL
3121 return tcon;
3122
3123out_fail:
3124 tconInfoFree(tcon);
3125 return ERR_PTR(rc);
3126}
3127
9d002df4
JL
3128void
3129cifs_put_tlink(struct tcon_link *tlink)
3130{
3131 if (!tlink || IS_ERR(tlink))
3132 return;
3133
3134 if (!atomic_dec_and_test(&tlink->tl_count) ||
3135 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3136 tlink->tl_time = jiffies;
3137 return;
3138 }
3139
3140 if (!IS_ERR(tlink_tcon(tlink)))
3141 cifs_put_tcon(tlink_tcon(tlink));
3142 kfree(tlink);
3143 return;
3144}
d00c28de 3145
25c7f41e
PS
3146static int
3147compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3148{
3149 struct cifs_sb_info *old = CIFS_SB(sb);
3150 struct cifs_sb_info *new = mnt_data->cifs_sb;
3151
3152 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3153 return 0;
3154
3155 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3156 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3157 return 0;
3158
25c7f41e 3159 /*
5eba8ab3
JL
3160 * We want to share sb only if we don't specify an r/wsize or
3161 * specified r/wsize is greater than or equal to existing one.
25c7f41e
PS
3162 */
3163 if (new->wsize && new->wsize < old->wsize)
3164 return 0;
3165
5eba8ab3
JL
3166 if (new->rsize && new->rsize < old->rsize)
3167 return 0;
3168
1f68233c 3169 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
25c7f41e
PS
3170 return 0;
3171
3172 if (old->mnt_file_mode != new->mnt_file_mode ||
3173 old->mnt_dir_mode != new->mnt_dir_mode)
3174 return 0;
3175
3176 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3177 return 0;
3178
3179 if (old->actimeo != new->actimeo)
3180 return 0;
3181
3182 return 1;
3183}
3184
c1d8b24d
SP
3185static int
3186match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3187{
3188 struct cifs_sb_info *old = CIFS_SB(sb);
3189 struct cifs_sb_info *new = mnt_data->cifs_sb;
cd8c4296
SP
3190 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3191 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
c1d8b24d 3192
cd8c4296 3193 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
c1d8b24d 3194 return 1;
cd8c4296
SP
3195 else if (!old_set && !new_set)
3196 return 1;
3197
c1d8b24d
SP
3198 return 0;
3199}
3200
25c7f41e
PS
3201int
3202cifs_match_super(struct super_block *sb, void *data)
3203{
3204 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3205 struct smb_vol *volume_info;
3206 struct cifs_sb_info *cifs_sb;
3207 struct TCP_Server_Info *tcp_srv;
96daf2b0
SF
3208 struct cifs_ses *ses;
3209 struct cifs_tcon *tcon;
25c7f41e 3210 struct tcon_link *tlink;
25c7f41e
PS
3211 int rc = 0;
3212
25c7f41e
PS
3213 spin_lock(&cifs_tcp_ses_lock);
3214 cifs_sb = CIFS_SB(sb);
3215 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3216 if (IS_ERR(tlink)) {
3217 spin_unlock(&cifs_tcp_ses_lock);
3218 return rc;
3219 }
3220 tcon = tlink_tcon(tlink);
3221 ses = tcon->ses;
3222 tcp_srv = ses->server;
3223
3224 volume_info = mnt_data->vol;
3225
9fa114f7 3226 if (!match_server(tcp_srv, volume_info) ||
25c7f41e 3227 !match_session(ses, volume_info) ||
ae6f8dd4 3228 !match_tcon(tcon, volume_info) ||
c1d8b24d 3229 !match_prepath(sb, mnt_data)) {
25c7f41e
PS
3230 rc = 0;
3231 goto out;
3232 }
3233
3234 rc = compare_mount_options(sb, mnt_data);
3235out:
25c7f41e 3236 spin_unlock(&cifs_tcp_ses_lock);
f484b5d0 3237 cifs_put_tlink(tlink);
25c7f41e
PS
3238 return rc;
3239}
3240
1da177e4 3241int
b669f33c 3242get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
2e6e02ab
PS
3243 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3244 struct dfs_info3_param **referrals, int remap)
1da177e4 3245{
1da177e4
LT
3246 int rc = 0;
3247
b327a717 3248 if (!ses->server->ops->get_dfs_refer)
2e6e02ab
PS
3249 return -ENOSYS;
3250
3251 *num_referrals = 0;
3252 *referrals = NULL;
1da177e4 3253
b327a717
AA
3254 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3255 referrals, num_referrals,
3256 nls_codepage, remap);
1da177e4
LT
3257 return rc;
3258}
3259
09e50d55
JL
3260#ifdef CONFIG_DEBUG_LOCK_ALLOC
3261static struct lock_class_key cifs_key[2];
3262static struct lock_class_key cifs_slock_key[2];
3263
3264static inline void
3265cifs_reclassify_socket4(struct socket *sock)
3266{
3267 struct sock *sk = sock->sk;
fafc4e1e 3268 BUG_ON(!sock_allow_reclassification(sk));
09e50d55
JL
3269 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3270 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3271}
3272
3273static inline void
3274cifs_reclassify_socket6(struct socket *sock)
3275{
3276 struct sock *sk = sock->sk;
fafc4e1e 3277 BUG_ON(!sock_allow_reclassification(sk));
09e50d55
JL
3278 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3279 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3280}
3281#else
3282static inline void
3283cifs_reclassify_socket4(struct socket *sock)
3284{
3285}
3286
3287static inline void
3288cifs_reclassify_socket6(struct socket *sock)
3289{
3290}
3291#endif
3292
1da177e4 3293/* See RFC1001 section 14 on representation of Netbios names */
50c2f753 3294static void rfc1002mangle(char *target, char *source, unsigned int length)
1da177e4 3295{
50c2f753 3296 unsigned int i, j;
1da177e4 3297
50c2f753 3298 for (i = 0, j = 0; i < (length); i++) {
1da177e4
LT
3299 /* mask a nibble at a time and encode */
3300 target[j] = 'A' + (0x0F & (source[i] >> 4));
3301 target[j+1] = 'A' + (0x0F & source[i]);
50c2f753 3302 j += 2;
1da177e4
LT
3303 }
3304
3305}
3306
3eb9a889
BG
3307static int
3308bind_socket(struct TCP_Server_Info *server)
3309{
3310 int rc = 0;
3311 if (server->srcaddr.ss_family != AF_UNSPEC) {
3312 /* Bind to the specified local IP address */
3313 struct socket *socket = server->ssocket;
3314 rc = socket->ops->bind(socket,
3315 (struct sockaddr *) &server->srcaddr,
3316 sizeof(server->srcaddr));
3317 if (rc < 0) {
3318 struct sockaddr_in *saddr4;
3319 struct sockaddr_in6 *saddr6;
3320 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3321 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3322 if (saddr6->sin6_family == AF_INET6)
f96637be
JP
3323 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3324 &saddr6->sin6_addr, rc);
3eb9a889 3325 else
f96637be
JP
3326 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3327 &saddr4->sin_addr.s_addr, rc);
3eb9a889
BG
3328 }
3329 }
3330 return rc;
3331}
1da177e4
LT
3332
3333static int
a9f1b85e 3334ip_rfc1001_connect(struct TCP_Server_Info *server)
1da177e4
LT
3335{
3336 int rc = 0;
a9f1b85e
PS
3337 /*
3338 * some servers require RFC1001 sessinit before sending
3339 * negprot - BB check reconnection in case where second
3340 * sessinit is sent but no second negprot
3341 */
3342 struct rfc1002_session_packet *ses_init_buf;
3343 struct smb_hdr *smb_buf;
3344 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3345 GFP_KERNEL);
3346 if (ses_init_buf) {
3347 ses_init_buf->trailer.session_req.called_len = 32;
3348
997152f6 3349 if (server->server_RFC1001_name[0] != 0)
a9f1b85e
PS
3350 rfc1002mangle(ses_init_buf->trailer.
3351 session_req.called_name,
3352 server->server_RFC1001_name,
3353 RFC1001_NAME_LEN_WITH_NULL);
3354 else
3355 rfc1002mangle(ses_init_buf->trailer.
3356 session_req.called_name,
3357 DEFAULT_CIFS_CALLED_NAME,
3358 RFC1001_NAME_LEN_WITH_NULL);
3359
3360 ses_init_buf->trailer.session_req.calling_len = 32;
3361
3362 /*
3363 * calling name ends in null (byte 16) from old smb
3364 * convention.
3365 */
c85c35f8 3366 if (server->workstation_RFC1001_name[0] != 0)
a9f1b85e
PS
3367 rfc1002mangle(ses_init_buf->trailer.
3368 session_req.calling_name,
3369 server->workstation_RFC1001_name,
3370 RFC1001_NAME_LEN_WITH_NULL);
3371 else
3372 rfc1002mangle(ses_init_buf->trailer.
3373 session_req.calling_name,
3374 "LINUX_CIFS_CLNT",
3375 RFC1001_NAME_LEN_WITH_NULL);
3376
3377 ses_init_buf->trailer.session_req.scope1 = 0;
3378 ses_init_buf->trailer.session_req.scope2 = 0;
3379 smb_buf = (struct smb_hdr *)ses_init_buf;
3380
3381 /* sizeof RFC1002_SESSION_REQUEST with no scope */
be8e3b00 3382 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
a9f1b85e
PS
3383 rc = smb_send(server, smb_buf, 0x44);
3384 kfree(ses_init_buf);
3385 /*
3386 * RFC1001 layer in at least one server
3387 * requires very short break before negprot
3388 * presumably because not expecting negprot
3389 * to follow so fast. This is a simple
3390 * solution that works without
3391 * complicating the code and causes no
3392 * significant slowing down on mount
3393 * for everyone else
3394 */
3395 usleep_range(1000, 2000);
3396 }
3397 /*
3398 * else the negprot may still work without this
3399 * even though malloc failed
3400 */
3401
3402 return rc;
3403}
3404
3405static int
3406generic_ip_connect(struct TCP_Server_Info *server)
3407{
3408 int rc = 0;
6da97910 3409 __be16 sport;
a9f1b85e 3410 int slen, sfamily;
bcf4b106 3411 struct socket *socket = server->ssocket;
a9f1b85e
PS
3412 struct sockaddr *saddr;
3413
3414 saddr = (struct sockaddr *) &server->dstaddr;
3415
3416 if (server->dstaddr.ss_family == AF_INET6) {
3417 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3418 slen = sizeof(struct sockaddr_in6);
3419 sfamily = AF_INET6;
3420 } else {
3421 sport = ((struct sockaddr_in *) saddr)->sin_port;
3422 slen = sizeof(struct sockaddr_in);
3423 sfamily = AF_INET;
3424 }
1da177e4 3425
bcf4b106 3426 if (socket == NULL) {
f1d0c998
RL
3427 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3428 IPPROTO_TCP, &socket, 1);
1da177e4 3429 if (rc < 0) {
f96637be 3430 cifs_dbg(VFS, "Error %d creating socket\n", rc);
a9f1b85e 3431 server->ssocket = NULL;
1da177e4 3432 return rc;
1da177e4 3433 }
bcf4b106
JL
3434
3435 /* BB other socket options to set KEEPALIVE, NODELAY? */
f96637be 3436 cifs_dbg(FYI, "Socket created\n");
bcf4b106
JL
3437 server->ssocket = socket;
3438 socket->sk->sk_allocation = GFP_NOFS;
a9f1b85e
PS
3439 if (sfamily == AF_INET6)
3440 cifs_reclassify_socket6(socket);
3441 else
3442 cifs_reclassify_socket4(socket);
1da177e4
LT
3443 }
3444
3eb9a889
BG
3445 rc = bind_socket(server);
3446 if (rc < 0)
3447 return rc;
3448
bcf4b106
JL
3449 /*
3450 * Eventually check for other socket options to change from
a9f1b85e
PS
3451 * the default. sock_setsockopt not used because it expects
3452 * user space buffer
bcf4b106
JL
3453 */
3454 socket->sk->sk_rcvtimeo = 7 * HZ;
da505c38 3455 socket->sk->sk_sndtimeo = 5 * HZ;
edf1ae40 3456
b387eaeb 3457 /* make the bufsizes depend on wsize/rsize and max requests */
bcf4b106
JL
3458 if (server->noautotune) {
3459 if (socket->sk->sk_sndbuf < (200 * 1024))
3460 socket->sk->sk_sndbuf = 200 * 1024;
3461 if (socket->sk->sk_rcvbuf < (140 * 1024))
3462 socket->sk->sk_rcvbuf = 140 * 1024;
edf1ae40 3463 }
1da177e4 3464
6a5fa236 3465 if (server->tcp_nodelay) {
a9f1b85e 3466 int val = 1;
6a5fa236
SF
3467 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3468 (char *)&val, sizeof(val));
3469 if (rc)
f96637be
JP
3470 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3471 rc);
6a5fa236
SF
3472 }
3473
f96637be 3474 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
bcf4b106 3475 socket->sk->sk_sndbuf,
b6b38f70 3476 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
bcf4b106 3477
ee1b3ea9
JL
3478 rc = socket->ops->connect(socket, saddr, slen, 0);
3479 if (rc < 0) {
f96637be 3480 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
ee1b3ea9
JL
3481 sock_release(socket);
3482 server->ssocket = NULL;
3483 return rc;
3484 }
3485
a9f1b85e
PS
3486 if (sport == htons(RFC1001_PORT))
3487 rc = ip_rfc1001_connect(server);
50c2f753 3488
1da177e4
LT
3489 return rc;
3490}
3491
3492static int
a9f1b85e 3493ip_connect(struct TCP_Server_Info *server)
1da177e4 3494{
6da97910 3495 __be16 *sport;
a9f1b85e
PS
3496 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3497 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
1da177e4 3498
a9f1b85e
PS
3499 if (server->dstaddr.ss_family == AF_INET6)
3500 sport = &addr6->sin6_port;
3501 else
3502 sport = &addr->sin_port;
1da177e4 3503
a9f1b85e
PS
3504 if (*sport == 0) {
3505 int rc;
1da177e4 3506
a9f1b85e
PS
3507 /* try with 445 port at first */
3508 *sport = htons(CIFS_PORT);
3eb9a889 3509
a9f1b85e 3510 rc = generic_ip_connect(server);
1da177e4 3511 if (rc >= 0)
a9f1b85e 3512 return rc;
6a5fa236 3513
a9f1b85e
PS
3514 /* if it failed, try with 139 port */
3515 *sport = htons(RFC1001_PORT);
6a5fa236
SF
3516 }
3517
a9f1b85e 3518 return generic_ip_connect(server);
1da177e4
LT
3519}
3520
6d5786a3 3521void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
2c6292ae 3522 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
8af18971
SF
3523{
3524 /* if we are reconnecting then should we check to see if
3525 * any requested capabilities changed locally e.g. via
3526 * remount but we can not do much about it here
3527 * if they have (even if we could detect it by the following)
3528 * Perhaps we could add a backpointer to array of sb from tcon
3529 * or if we change to make all sb to same share the same
3530 * sb as NFS - then we only have one backpointer to sb.
3531 * What if we wanted to mount the server share twice once with
3532 * and once without posixacls or posix paths? */
3533 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
50c2f753 3534
c18c842b
SF
3535 if (vol_info && vol_info->no_linux_ext) {
3536 tcon->fsUnixInfo.Capability = 0;
3537 tcon->unix_ext = 0; /* Unix Extensions disabled */
f96637be 3538 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
c18c842b
SF
3539 return;
3540 } else if (vol_info)
3541 tcon->unix_ext = 1; /* Unix Extensions supported */
3542
3543 if (tcon->unix_ext == 0) {
f96637be 3544 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
c18c842b
SF
3545 return;
3546 }
50c2f753 3547
fb8c4b14 3548 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
8af18971 3549 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
f96637be 3550 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
8af18971
SF
3551 /* check for reconnect case in which we do not
3552 want to change the mount behavior if we can avoid it */
fb8c4b14 3553 if (vol_info == NULL) {
50c2f753 3554 /* turn off POSIX ACL and PATHNAMES if not set
8af18971
SF
3555 originally at mount time */
3556 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3557 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
11b6d645
IM
3558 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3559 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
f96637be 3560 cifs_dbg(VFS, "POSIXPATH support change\n");
8af18971 3561 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
11b6d645 3562 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
f96637be
JP
3563 cifs_dbg(VFS, "possible reconnect error\n");
3564 cifs_dbg(VFS, "server disabled POSIX path support\n");
11b6d645 3565 }
8af18971 3566 }
50c2f753 3567
6848b733 3568 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
f96637be 3569 cifs_dbg(VFS, "per-share encryption not supported yet\n");
6848b733 3570
8af18971 3571 cap &= CIFS_UNIX_CAP_MASK;
75865f8c 3572 if (vol_info && vol_info->no_psx_acl)
8af18971 3573 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
75865f8c 3574 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
f96637be 3575 cifs_dbg(FYI, "negotiated posix acl support\n");
2c6292ae
AV
3576 if (cifs_sb)
3577 cifs_sb->mnt_cifs_flags |=
3578 CIFS_MOUNT_POSIXACL;
8af18971
SF
3579 }
3580
75865f8c 3581 if (vol_info && vol_info->posix_paths == 0)
8af18971 3582 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
75865f8c 3583 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
f96637be 3584 cifs_dbg(FYI, "negotiate posix pathnames\n");
2c6292ae
AV
3585 if (cifs_sb)
3586 cifs_sb->mnt_cifs_flags |=
8af18971
SF
3587 CIFS_MOUNT_POSIX_PATHS;
3588 }
50c2f753 3589
f96637be 3590 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
8af18971 3591#ifdef CONFIG_CIFS_DEBUG2
75865f8c 3592 if (cap & CIFS_UNIX_FCNTL_CAP)
f96637be 3593 cifs_dbg(FYI, "FCNTL cap\n");
75865f8c 3594 if (cap & CIFS_UNIX_EXTATTR_CAP)
f96637be 3595 cifs_dbg(FYI, "EXTATTR cap\n");
75865f8c 3596 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
f96637be 3597 cifs_dbg(FYI, "POSIX path cap\n");
75865f8c 3598 if (cap & CIFS_UNIX_XATTR_CAP)
f96637be 3599 cifs_dbg(FYI, "XATTR cap\n");
75865f8c 3600 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
f96637be 3601 cifs_dbg(FYI, "POSIX ACL cap\n");
75865f8c 3602 if (cap & CIFS_UNIX_LARGE_READ_CAP)
f96637be 3603 cifs_dbg(FYI, "very large read cap\n");
75865f8c 3604 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
f96637be 3605 cifs_dbg(FYI, "very large write cap\n");
6848b733 3606 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
f96637be 3607 cifs_dbg(FYI, "transport encryption cap\n");
6848b733 3608 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
f96637be 3609 cifs_dbg(FYI, "mandatory transport encryption cap\n");
8af18971
SF
3610#endif /* CIFS_DEBUG2 */
3611 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
442aa310 3612 if (vol_info == NULL) {
f96637be 3613 cifs_dbg(FYI, "resetting capabilities failed\n");
442aa310 3614 } else
f96637be 3615 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");
5a44b319 3616
8af18971
SF
3617 }
3618 }
3619}
3620
4214ebf4 3621int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
724d9f1c 3622 struct cifs_sb_info *cifs_sb)
b1c8d2b4 3623{
2de970ff
JL
3624 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3625
2ced6f69
AV
3626 spin_lock_init(&cifs_sb->tlink_tree_lock);
3627 cifs_sb->tlink_tree = RB_ROOT;
3628
25c7f41e 3629 /*
5eba8ab3
JL
3630 * Temporarily set r/wsize for matching superblock. If we end up using
3631 * new sb then client will later negotiate it downward if needed.
25c7f41e 3632 */
5eba8ab3 3633 cifs_sb->rsize = pvolume_info->rsize;
25c7f41e
PS
3634 cifs_sb->wsize = pvolume_info->wsize;
3635
3b795210
SF
3636 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3637 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3638 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3639 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
f96637be
JP
3640 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3641 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
3b795210 3642
6d20e840 3643 cifs_sb->actimeo = pvolume_info->actimeo;
724d9f1c 3644 cifs_sb->local_nls = pvolume_info->local_nls;
6d20e840 3645
3b795210
SF
3646 if (pvolume_info->noperm)
3647 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3648 if (pvolume_info->setuids)
3649 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
95932655
SF
3650 if (pvolume_info->setuidfromacl)
3651 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
3b795210
SF
3652 if (pvolume_info->server_ino)
3653 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3654 if (pvolume_info->remap)
2baa2682
SF
3655 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3656 if (pvolume_info->sfu_remap)
3b795210
SF
3657 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3658 if (pvolume_info->no_xattr)
3659 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3660 if (pvolume_info->sfu_emul)
3661 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3662 if (pvolume_info->nobrl)
3663 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
3d4ef9a1
SF
3664 if (pvolume_info->nohandlecache)
3665 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
be652445 3666 if (pvolume_info->nostrictsync)
4717bed6 3667 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
13a6e42a
SF
3668 if (pvolume_info->mand_lock)
3669 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
d4ffff1f
PS
3670 if (pvolume_info->rwpidforward)
3671 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
3b795210
SF
3672 if (pvolume_info->cifs_acl)
3673 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
3c7c87fd 3674 if (pvolume_info->backupuid_specified) {
3d3ea8e6 3675 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
3c7c87fd
SP
3676 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3677 }
3678 if (pvolume_info->backupgid_specified) {
3d3ea8e6 3679 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
3c7c87fd
SP
3680 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3681 }
3b795210
SF
3682 if (pvolume_info->override_uid)
3683 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3684 if (pvolume_info->override_gid)
3685 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3686 if (pvolume_info->dynperm)
3687 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
fa1df75d
SJ
3688 if (pvolume_info->fsc)
3689 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
0eb8a132
JL
3690 if (pvolume_info->multiuser)
3691 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3692 CIFS_MOUNT_NO_PERM);
d39454ff
PS
3693 if (pvolume_info->strict_io)
3694 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
3b795210 3695 if (pvolume_info->direct_io) {
f96637be 3696 cifs_dbg(FYI, "mounting share using direct i/o\n");
3b795210
SF
3697 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3698 }
736a3320
SM
3699 if (pvolume_info->mfsymlinks) {
3700 if (pvolume_info->sfu_emul) {
db8b631d
SF
3701 /*
3702 * Our SFU ("Services for Unix" emulation does not allow
3703 * creating symlinks but does allow reading existing SFU
3704 * symlinks (it does allow both creating and reading SFU
3705 * style mknod and FIFOs though). When "mfsymlinks" and
3706 * "sfu" are both enabled at the same time, it allows
3707 * reading both types of symlinks, but will only create
3708 * them with mfsymlinks format. This allows better
3709 * Apple compatibility (probably better for Samba too)
3710 * while still recognizing old Windows style symlinks.
3711 */
3712 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
736a3320 3713 }
db8b631d 3714 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
736a3320 3715 }
3b795210
SF
3716
3717 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
f96637be 3718 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
4214ebf4
SP
3719
3720 if (pvolume_info->prepath) {
3721 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3722 if (cifs_sb->prepath == NULL)
3723 return -ENOMEM;
3724 }
3725
3726 return 0;
b1c8d2b4
JL
3727}
3728
b9bce2e9
JL
3729static void
3730cleanup_volume_info_contents(struct smb_vol *volume_info)
1bfe73c2 3731{
b946845a 3732 kfree(volume_info->username);
1bfe73c2 3733 kzfree(volume_info->password);
95c75454 3734 kfree(volume_info->UNC);
b946845a
SF
3735 kfree(volume_info->domainname);
3736 kfree(volume_info->iocharset);
1bfe73c2 3737 kfree(volume_info->prepath);
b9bce2e9
JL
3738}
3739
3740void
3741cifs_cleanup_volume_info(struct smb_vol *volume_info)
3742{
3743 if (!volume_info)
3744 return;
3745 cleanup_volume_info_contents(volume_info);
1bfe73c2 3746 kfree(volume_info);
1bfe73c2
IM
3747}
3748
b9bce2e9 3749
2d6d589d 3750#ifdef CONFIG_CIFS_DFS_UPCALL
6d3ea7e4
SF
3751/*
3752 * cifs_build_path_to_root returns full path to root when we do not have an
3753 * exiting connection (tcon)
3754 */
1bfe73c2 3755static char *
b2a0fa15 3756build_unc_path_to_root(const struct smb_vol *vol,
1bfe73c2
IM
3757 const struct cifs_sb_info *cifs_sb)
3758{
b2a0fa15 3759 char *full_path, *pos;
839db3d1 3760 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
b2a0fa15 3761 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
1bfe73c2 3762
b2a0fa15 3763 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
1bfe73c2
IM
3764 if (full_path == NULL)
3765 return ERR_PTR(-ENOMEM);
3766
b2a0fa15
JL
3767 strncpy(full_path, vol->UNC, unc_len);
3768 pos = full_path + unc_len;
3769
3770 if (pplen) {
1fc29bac
JL
3771 *pos = CIFS_DIR_SEP(cifs_sb);
3772 strncpy(pos + 1, vol->prepath, pplen);
b2a0fa15
JL
3773 pos += pplen;
3774 }
3775
3776 *pos = '\0'; /* add trailing null */
f87d39d9 3777 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
f96637be 3778 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
1bfe73c2
IM
3779 return full_path;
3780}
dd613945
SF
3781
3782/*
3783 * Perform a dfs referral query for a share and (optionally) prefix
3784 *
046462ab
SF
3785 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3786 * to a string containing updated options for the submount. Otherwise it
3787 * will be left untouched.
dd613945
SF
3788 *
3789 * Returns the rc from get_dfs_path to the caller, which can be used to
3790 * determine whether there were referrals.
3791 */
3792static int
b669f33c 3793expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
dd613945 3794 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
046462ab 3795 int check_prefix)
dd613945
SF
3796{
3797 int rc;
3798 unsigned int num_referrals = 0;
3799 struct dfs_info3_param *referrals = NULL;
3800 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3801
3802 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3803 if (IS_ERR(full_path))
3804 return PTR_ERR(full_path);
3805
3806 /* For DFS paths, skip the first '\' of the UNC */
3807 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3808
b669f33c 3809 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
2baa2682 3810 &num_referrals, &referrals, cifs_remap(cifs_sb));
dd613945
SF
3811
3812 if (!rc && num_referrals > 0) {
3813 char *fake_devname = NULL;
3814
3815 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3816 full_path + 1, referrals,
3817 &fake_devname);
3818
3819 free_dfs_info_array(referrals, num_referrals);
046462ab 3820
dd613945
SF
3821 if (IS_ERR(mdata)) {
3822 rc = PTR_ERR(mdata);
3823 mdata = NULL;
b9bce2e9
JL
3824 } else {
3825 cleanup_volume_info_contents(volume_info);
b9bce2e9 3826 rc = cifs_setup_volume_info(volume_info, mdata,
c7c137b9 3827 fake_devname, false);
dd613945 3828 }
b9bce2e9
JL
3829 kfree(fake_devname);
3830 kfree(cifs_sb->mountdata);
046462ab 3831 cifs_sb->mountdata = mdata;
dd613945
SF
3832 }
3833 kfree(full_path);
3834 return rc;
3835}
2d6d589d 3836#endif
1bfe73c2 3837
04db79b0
JL
3838static int
3839cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
c7c137b9 3840 const char *devname, bool is_smb3)
1da177e4 3841{
724d9f1c 3842 int rc = 0;
1da177e4 3843
c7c137b9 3844 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
04db79b0 3845 return -EINVAL;
1da177e4 3846
7586b765 3847 if (volume_info->nullauth) {
f96637be 3848 cifs_dbg(FYI, "Anonymous login\n");
04febabc
JL
3849 kfree(volume_info->username);
3850 volume_info->username = NULL;
7586b765 3851 } else if (volume_info->username) {
1da177e4 3852 /* BB fixme parse for domain name here */
f96637be 3853 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
1da177e4 3854 } else {
f96637be 3855 cifs_dbg(VFS, "No username specified\n");
50c2f753
SF
3856 /* In userspace mount helper we can get user name from alternate
3857 locations such as env variables and files on disk */
04db79b0 3858 return -EINVAL;
1da177e4
LT
3859 }
3860
1da177e4 3861 /* this is needed for ASCII cp to Unicode converts */
7586b765 3862 if (volume_info->iocharset == NULL) {
a5fc4ce0
JL
3863 /* load_nls_default cannot return null */
3864 volume_info->local_nls = load_nls_default();
1da177e4 3865 } else {
a5fc4ce0
JL
3866 volume_info->local_nls = load_nls(volume_info->iocharset);
3867 if (volume_info->local_nls == NULL) {
f96637be 3868 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
b6b38f70 3869 volume_info->iocharset);
04db79b0 3870 return -ELIBACC;
1da177e4
LT
3871 }
3872 }
724d9f1c 3873
724d9f1c
PS
3874 return rc;
3875}
3876
04db79b0 3877struct smb_vol *
c7c137b9 3878cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
04db79b0
JL
3879{
3880 int rc;
3881 struct smb_vol *volume_info;
3882
6ee9542a 3883 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
04db79b0
JL
3884 if (!volume_info)
3885 return ERR_PTR(-ENOMEM);
3886
c7c137b9 3887 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
04db79b0
JL
3888 if (rc) {
3889 cifs_cleanup_volume_info(volume_info);
3890 volume_info = ERR_PTR(rc);
3891 }
3892
3893 return volume_info;
3894}
3895
a6b5058f
AA
3896static int
3897cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3898 unsigned int xid,
3899 struct cifs_tcon *tcon,
3900 struct cifs_sb_info *cifs_sb,
3901 char *full_path)
3902{
3903 int rc;
3904 char *s;
3905 char sep, tmp;
3906
3907 sep = CIFS_DIR_SEP(cifs_sb);
3908 s = full_path;
3909
3910 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3911 while (rc == 0) {
3912 /* skip separators */
3913 while (*s == sep)
3914 s++;
3915 if (!*s)
3916 break;
3917 /* next separator */
3918 while (*s && *s != sep)
3919 s++;
3920
3921 /*
3922 * temporarily null-terminate the path at the end of
3923 * the current component
3924 */
3925 tmp = *s;
3926 *s = 0;
3927 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3928 full_path);
3929 *s = tmp;
3930 }
3931 return rc;
3932}
3933
724d9f1c 3934int
2c6292ae 3935cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
724d9f1c 3936{
1daaae8f 3937 int rc;
6d5786a3 3938 unsigned int xid;
af4281dc 3939 struct cifs_ses *ses;
96daf2b0 3940 struct cifs_tcon *tcon;
af4281dc 3941 struct TCP_Server_Info *server;
724d9f1c
PS
3942 char *full_path;
3943 struct tcon_link *tlink;
3944#ifdef CONFIG_CIFS_DFS_UPCALL
3945 int referral_walks_count = 0;
20547490 3946#endif
dd854466 3947
20547490 3948#ifdef CONFIG_CIFS_DFS_UPCALL
724d9f1c
PS
3949try_mount_again:
3950 /* cleanup activities if we're chasing a referral */
3951 if (referral_walks_count) {
3952 if (tcon)
3953 cifs_put_tcon(tcon);
af4281dc
PS
3954 else if (ses)
3955 cifs_put_smb_ses(ses);
724d9f1c 3956
1dfd18d0
SP
3957 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3958
6d5786a3 3959 free_xid(xid);
724d9f1c
PS
3960 }
3961#endif
1daaae8f 3962 rc = 0;
724d9f1c 3963 tcon = NULL;
af4281dc
PS
3964 ses = NULL;
3965 server = NULL;
724d9f1c
PS
3966 full_path = NULL;
3967 tlink = NULL;
3968
6d5786a3 3969 xid = get_xid();
1da177e4 3970
63c038c2 3971 /* get a reference to a tcp session */
af4281dc
PS
3972 server = cifs_get_tcp_session(volume_info);
3973 if (IS_ERR(server)) {
3974 rc = PTR_ERR(server);
63c038c2 3975 goto out;
1da177e4 3976 }
141891f4
SF
3977 if ((volume_info->max_credits < 20) ||
3978 (volume_info->max_credits > 60000))
3979 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3980 else
3981 server->max_credits = volume_info->max_credits;
36988c76 3982 /* get a reference to a SMB session */
af4281dc
PS
3983 ses = cifs_get_smb_ses(server, volume_info);
3984 if (IS_ERR(ses)) {
3985 rc = PTR_ERR(ses);
3986 ses = NULL;
36988c76 3987 goto mount_fail_check;
1da177e4 3988 }
50c2f753 3989
b618f001
SF
3990 if ((volume_info->persistent == true) && ((ses->server->capabilities &
3991 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
3992 cifs_dbg(VFS, "persistent handles not supported by server\n");
3993 rc = -EOPNOTSUPP;
3994 goto mount_fail_check;
3995 }
592fafe6 3996
d00c28de 3997 /* search for existing tcon to this server share */
af4281dc 3998 tcon = cifs_get_tcon(ses, volume_info);
d00c28de
JL
3999 if (IS_ERR(tcon)) {
4000 rc = PTR_ERR(tcon);
4001 tcon = NULL;
40920c2b
MS
4002 if (rc == -EACCES)
4003 goto mount_fail_check;
4004
1bfe73c2 4005 goto remote_path_check;
d00c28de 4006 }
1bfe73c2 4007
ce558b0e
SF
4008#ifdef CONFIG_CIFS_SMB311
4009 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4010 if (tcon->posix_extensions)
4011 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4012#endif /* SMB3.11 */
4013
d82c2df5 4014 /* tell server which Unix caps we support */
29e20f9c 4015 if (cap_unix(tcon->ses)) {
d82c2df5
SF
4016 /* reset of caps checks mount to see if unix extensions
4017 disabled for just this mount */
2c6292ae 4018 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
6848b733
SF
4019 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4020 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4021 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
4022 rc = -EACCES;
4023 goto mount_fail_check;
4024 }
4025 } else
d82c2df5 4026 tcon->unix_ext = 0; /* server does not support them */
c18c842b 4027
af4281dc 4028 /* do not care if a following call succeed - informational */
b327a717 4029 if (!tcon->pipe && server->ops->qfs_tcon)
af4281dc 4030 server->ops->qfs_tcon(xid, tcon);
6848b733 4031
24985c53
PS
4032 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
4033 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
f7910cbd 4034
1bfe73c2 4035remote_path_check:
c1508ca2
SF
4036#ifdef CONFIG_CIFS_DFS_UPCALL
4037 /*
4038 * Perform an unconditional check for whether there are DFS
4039 * referrals for this path without prefix, to provide support
4040 * for DFS referrals from w2k8 servers which don't seem to respond
4041 * with PATH_NOT_COVERED to requests that include the prefix.
4042 * Chase the referral if found, otherwise continue normally.
4043 */
4044 if (referral_walks_count == 0) {
af4281dc
PS
4045 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
4046 false);
c1508ca2
SF
4047 if (!refrc) {
4048 referral_walks_count++;
4049 goto try_mount_again;
4050 }
4051 }
4052#endif
4053
f87d39d9 4054 /* check if a whole path is not remote */
70945643 4055 if (!rc && tcon) {
68889f26
PS
4056 if (!server->ops->is_path_accessible) {
4057 rc = -ENOSYS;
4058 goto mount_fail_check;
4059 }
6d3ea7e4
SF
4060 /*
4061 * cifs_build_path_to_root works only when we have a valid tcon
4062 */
374402a2
SP
4063 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
4064 tcon->Flags & SMB_SHARE_IS_IN_DFS);
e4cce94c
IM
4065 if (full_path == NULL) {
4066 rc = -ENOMEM;
4067 goto mount_fail_check;
4068 }
68889f26
PS
4069 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4070 full_path);
03ceace5 4071 if (rc != 0 && rc != -EREMOTE) {
e4cce94c
IM
4072 kfree(full_path);
4073 goto mount_fail_check;
4074 }
a6b5058f 4075
d171356f
SP
4076 if (rc != -EREMOTE) {
4077 rc = cifs_are_all_path_components_accessible(server,
a6b5058f
AA
4078 xid, tcon, cifs_sb,
4079 full_path);
d171356f
SP
4080 if (rc != 0) {
4081 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4082 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4083 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4084 rc = 0;
4085 }
a6b5058f 4086 }
e4cce94c
IM
4087 kfree(full_path);
4088 }
4089
1bfe73c2
IM
4090 /* get referral if needed */
4091 if (rc == -EREMOTE) {
d036f50f 4092#ifdef CONFIG_CIFS_DFS_UPCALL
5c2503a8
IM
4093 if (referral_walks_count > MAX_NESTED_LINKS) {
4094 /*
4095 * BB: when we implement proper loop detection,
4096 * we will remove this check. But now we need it
4097 * to prevent an indefinite loop if 'DFS tree' is
4098 * misconfigured (i.e. has loops).
4099 */
4100 rc = -ELOOP;
4101 goto mount_fail_check;
4102 }
1bfe73c2 4103
af4281dc 4104 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
7b91e266 4105
dd613945 4106 if (!rc) {
5c2503a8 4107 referral_walks_count++;
1bfe73c2
IM
4108 goto try_mount_again;
4109 }
dd613945 4110 goto mount_fail_check;
d036f50f
SF
4111#else /* No DFS support, return error on mount */
4112 rc = -EOPNOTSUPP;
4113#endif
1bfe73c2
IM
4114 }
4115
9d002df4
JL
4116 if (rc)
4117 goto mount_fail_check;
4118
4119 /* now, hang the tcon off of the superblock */
4120 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
4121 if (tlink == NULL) {
4122 rc = -ENOMEM;
4123 goto mount_fail_check;
4124 }
4125
af4281dc 4126 tlink->tl_uid = ses->linux_uid;
9d002df4
JL
4127 tlink->tl_tcon = tcon;
4128 tlink->tl_time = jiffies;
4129 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4130 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4131
b647c35f 4132 cifs_sb->master_tlink = tlink;
9d002df4 4133 spin_lock(&cifs_sb->tlink_tree_lock);
b647c35f 4134 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
9d002df4 4135 spin_unlock(&cifs_sb->tlink_tree_lock);
413e661c 4136
da472fc8 4137 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
2de970ff
JL
4138 TLINK_IDLE_EXPIRE);
4139
1bfe73c2
IM
4140mount_fail_check:
4141 /* on error free sesinfo and tcon struct if needed */
4142 if (rc) {
1bfe73c2 4143 /* If find_unc succeeded then rc == 0 so we can not end */
25985edc 4144 /* up accidentally freeing someone elses tcon struct */
1bfe73c2
IM
4145 if (tcon)
4146 cifs_put_tcon(tcon);
af4281dc
PS
4147 else if (ses)
4148 cifs_put_smb_ses(ses);
1bfe73c2 4149 else
53e0e11e 4150 cifs_put_tcp_session(server, 0);
1bfe73c2
IM
4151 }
4152
70fe7dc0 4153out:
6d5786a3 4154 free_xid(xid);
1da177e4
LT
4155 return rc;
4156}
4157
8d1bca32 4158/*
b327a717 4159 * Issue a TREE_CONNECT request.
8d1bca32 4160 */
1da177e4 4161int
2e6e02ab 4162CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
96daf2b0 4163 const char *tree, struct cifs_tcon *tcon,
1da177e4
LT
4164 const struct nls_table *nls_codepage)
4165{
4166 struct smb_hdr *smb_buffer;
4167 struct smb_hdr *smb_buffer_response;
4168 TCONX_REQ *pSMB;
4169 TCONX_RSP *pSMBr;
4170 unsigned char *bcc_ptr;
4171 int rc = 0;
690c522f
JL
4172 int length;
4173 __u16 bytes_left, count;
1da177e4
LT
4174
4175 if (ses == NULL)
4176 return -EIO;
4177
4178 smb_buffer = cifs_buf_get();
ca43e3be 4179 if (smb_buffer == NULL)
1da177e4 4180 return -ENOMEM;
ca43e3be 4181
1da177e4
LT
4182 smb_buffer_response = smb_buffer;
4183
4184 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4185 NULL /*no tid */ , 4 /*wct */ );
1982c344 4186
88257360 4187 smb_buffer->Mid = get_next_mid(ses->server);
1da177e4
LT
4188 smb_buffer->Uid = ses->Suid;
4189 pSMB = (TCONX_REQ *) smb_buffer;
4190 pSMBr = (TCONX_RSP *) smb_buffer_response;
4191
4192 pSMB->AndXCommand = 0xFF;
4193 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
1da177e4 4194 bcc_ptr = &pSMB->Password[0];
b327a717 4195 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
eeac8047 4196 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
7c7b25bc 4197 *bcc_ptr = 0; /* password is null byte */
eeac8047 4198 bcc_ptr++; /* skip password */
7c7b25bc 4199 /* already aligned so no need to do it below */
eeac8047 4200 } else {
540b2e37 4201 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
eeac8047
SF
4202 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4203 specified as required (when that support is added to
4204 the vfs in the future) as only NTLM or the much
7c7b25bc 4205 weaker LANMAN (which we do not send by default) is accepted
eeac8047
SF
4206 by Samba (not sure whether other servers allow
4207 NTLMv2 password here) */
7c7b25bc 4208#ifdef CONFIG_CIFS_WEAK_PW_HASH
04912d6a 4209 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
3f618223 4210 (ses->sectype == LANMAN))
d3ba50b1 4211 calc_lanman_hash(tcon->password, ses->server->cryptkey,
96daf2b0 4212 ses->server->sec_mode &
4e53a3fb
JL
4213 SECMODE_PW_ENCRYPT ? true : false,
4214 bcc_ptr);
7c7b25bc
SF
4215 else
4216#endif /* CIFS_WEAK_PW_HASH */
ee2c9258 4217 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
9ef5992e 4218 bcc_ptr, nls_codepage);
f3a31a2b
SF
4219 if (rc) {
4220 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4221 __func__, rc);
4222 cifs_buf_release(smb_buffer);
4223 return rc;
4224 }
eeac8047 4225
540b2e37 4226 bcc_ptr += CIFS_AUTH_RESP_SIZE;
fb8c4b14 4227 if (ses->capabilities & CAP_UNICODE) {
7c7b25bc
SF
4228 /* must align unicode strings */
4229 *bcc_ptr = 0; /* null byte password */
4230 bcc_ptr++;
4231 }
eeac8047 4232 }
1da177e4 4233
38d77c50 4234 if (ses->server->sign)
1da177e4
LT
4235 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4236
4237 if (ses->capabilities & CAP_STATUS32) {
4238 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4239 }
4240 if (ses->capabilities & CAP_DFS) {
4241 smb_buffer->Flags2 |= SMBFLG2_DFS;
4242 }
4243 if (ses->capabilities & CAP_UNICODE) {
4244 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4245 length =
acbbb76a 4246 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
50c2f753 4247 6 /* max utf8 char length in bytes */ *
a878fb22
SF
4248 (/* server len*/ + 256 /* share len */), nls_codepage);
4249 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
1da177e4
LT
4250 bcc_ptr += 2; /* skip trailing null */
4251 } else { /* ASCII */
1da177e4
LT
4252 strcpy(bcc_ptr, tree);
4253 bcc_ptr += strlen(tree) + 1;
4254 }
4255 strcpy(bcc_ptr, "?????");
4256 bcc_ptr += strlen("?????");
4257 bcc_ptr += 1;
4258 count = bcc_ptr - &pSMB->Password[0];
be8e3b00
SF
4259 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4260 pSMB->hdr.smb_buf_length) + count);
1da177e4
LT
4261 pSMB->ByteCount = cpu_to_le16(count);
4262
133672ef 4263 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
7749981e 4264 0);
1da177e4 4265
1da177e4 4266 /* above now done in SendReceive */
b327a717 4267 if (rc == 0) {
0e0d2cf3
SF
4268 bool is_unicode;
4269
1da177e4 4270 tcon->tidStatus = CifsGood;
3b795210 4271 tcon->need_reconnect = false;
1da177e4
LT
4272 tcon->tid = smb_buffer_response->Tid;
4273 bcc_ptr = pByteArea(smb_buffer_response);
690c522f 4274 bytes_left = get_bcc(smb_buffer_response);
cc20c031 4275 length = strnlen(bcc_ptr, bytes_left - 2);
0e0d2cf3
SF
4276 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4277 is_unicode = true;
4278 else
4279 is_unicode = false;
4280
cc20c031 4281
50c2f753 4282 /* skip service field (NB: this field is always ASCII) */
7f8ed420
SF
4283 if (length == 3) {
4284 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4285 (bcc_ptr[2] == 'C')) {
f96637be 4286 cifs_dbg(FYI, "IPC connection\n");
b327a717
AA
4287 tcon->ipc = true;
4288 tcon->pipe = true;
7f8ed420
SF
4289 }
4290 } else if (length == 2) {
4291 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4292 /* the most common case */
f96637be 4293 cifs_dbg(FYI, "disk share connection\n");
7f8ed420
SF
4294 }
4295 }
50c2f753 4296 bcc_ptr += length + 1;
cc20c031 4297 bytes_left -= (length + 1);
46b51d08 4298 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
cc20c031
JL
4299
4300 /* mostly informational -- no need to fail on error here */
90a98b2f 4301 kfree(tcon->nativeFileSystem);
acbbb76a 4302 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
0e0d2cf3 4303 bytes_left, is_unicode,
cc20c031
JL
4304 nls_codepage);
4305
f96637be 4306 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
cc20c031 4307
fb8c4b14 4308 if ((smb_buffer_response->WordCount == 3) ||
1a4e15a0
SF
4309 (smb_buffer_response->WordCount == 7))
4310 /* field is in same location */
3979877e
SF
4311 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4312 else
4313 tcon->Flags = 0;
f96637be 4314 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
1da177e4
LT
4315 }
4316
a8a11d39 4317 cifs_buf_release(smb_buffer);
1da177e4
LT
4318 return rc;
4319}
4320
2e32cf5e
AV
4321static void delayed_free(struct rcu_head *p)
4322{
4323 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4324 unload_nls(sbi->local_nls);
4325 kfree(sbi);
4326}
4327
2a9b9951
AV
4328void
4329cifs_umount(struct cifs_sb_info *cifs_sb)
1da177e4 4330{
b647c35f
JL
4331 struct rb_root *root = &cifs_sb->tlink_tree;
4332 struct rb_node *node;
4333 struct tcon_link *tlink;
9d002df4 4334
2de970ff
JL
4335 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4336
b647c35f
JL
4337 spin_lock(&cifs_sb->tlink_tree_lock);
4338 while ((node = rb_first(root))) {
4339 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4340 cifs_get_tlink(tlink);
4341 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4342 rb_erase(node, root);
1da177e4 4343
b647c35f
JL
4344 spin_unlock(&cifs_sb->tlink_tree_lock);
4345 cifs_put_tlink(tlink);
4346 spin_lock(&cifs_sb->tlink_tree_lock);
4347 }
4348 spin_unlock(&cifs_sb->tlink_tree_lock);
50c2f753 4349
d757d71b 4350 kfree(cifs_sb->mountdata);
a6b5058f 4351 kfree(cifs_sb->prepath);
2e32cf5e 4352 call_rcu(&cifs_sb->rcu, delayed_free);
50c2f753 4353}
1da177e4 4354
286170aa
PS
4355int
4356cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
1da177e4
LT
4357{
4358 int rc = 0;
198b5682 4359 struct TCP_Server_Info *server = ses->server;
1da177e4 4360
286170aa
PS
4361 if (!server->ops->need_neg || !server->ops->negotiate)
4362 return -ENOSYS;
4363
198b5682 4364 /* only send once per connect */
286170aa 4365 if (!server->ops->need_neg(server))
198b5682
JL
4366 return 0;
4367
45275789 4368 set_credits(server, 1);
286170aa
PS
4369
4370 rc = server->ops->negotiate(xid, ses);
198b5682
JL
4371 if (rc == 0) {
4372 spin_lock(&GlobalMid_Lock);
7fdbaa1b 4373 if (server->tcpStatus == CifsNeedNegotiate)
198b5682
JL
4374 server->tcpStatus = CifsGood;
4375 else
4376 rc = -EHOSTDOWN;
4377 spin_unlock(&GlobalMid_Lock);
198b5682
JL
4378 }
4379
4380 return rc;
4381}
4382
58c45c58
PS
4383int
4384cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4385 struct nls_table *nls_info)
198b5682 4386{
58c45c58 4387 int rc = -ENOSYS;
198b5682 4388 struct TCP_Server_Info *server = ses->server;
26b994fa 4389
198b5682 4390 ses->capabilities = server->capabilities;
26b994fa 4391 if (linuxExtEnabled == 0)
29e20f9c 4392 ses->capabilities &= (~server->vals->cap_unix);
20418acd 4393
f96637be 4394 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
96daf2b0 4395 server->sec_mode, server->capabilities, server->timeAdj);
cb7691b6 4396
f5c4ba81 4397 if (ses->auth_key.response) {
2a18287b 4398 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
f5c4ba81
SW
4399 ses->auth_key.response);
4400 kfree(ses->auth_key.response);
4401 ses->auth_key.response = NULL;
4402 ses->auth_key.len = 0;
4403 }
4404
58c45c58
PS
4405 if (server->ops->sess_setup)
4406 rc = server->ops->sess_setup(xid, ses, nls_info);
4407
d4e63bd6 4408 if (rc)
f96637be 4409 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
21e73393 4410
1da177e4
LT
4411 return rc;
4412}
4413
8a8798a5
JL
4414static int
4415cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4416{
3f618223
JL
4417 vol->sectype = ses->sectype;
4418
4419 /* krb5 is special, since we don't need username or pw */
4420 if (vol->sectype == Kerberos)
8a8798a5 4421 return 0;
8a8798a5
JL
4422
4423 return cifs_set_cifscreds(vol, ses);
4424}
4425
96daf2b0 4426static struct cifs_tcon *
6d4a0832 4427cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
9d002df4 4428{
8a8798a5 4429 int rc;
96daf2b0
SF
4430 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4431 struct cifs_ses *ses;
4432 struct cifs_tcon *tcon = NULL;
9d002df4 4433 struct smb_vol *vol_info;
9d002df4
JL
4434
4435 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
803ab977
DC
4436 if (vol_info == NULL)
4437 return ERR_PTR(-ENOMEM);
9d002df4 4438
9d002df4
JL
4439 vol_info->local_nls = cifs_sb->local_nls;
4440 vol_info->linux_uid = fsuid;
4441 vol_info->cred_uid = fsuid;
4442 vol_info->UNC = master_tcon->treeName;
4443 vol_info->retry = master_tcon->retry;
4444 vol_info->nocase = master_tcon->nocase;
3d4ef9a1 4445 vol_info->nohandlecache = master_tcon->nohandlecache;
9d002df4
JL
4446 vol_info->local_lease = master_tcon->local_lease;
4447 vol_info->no_linux_ext = !master_tcon->unix_ext;
28e11bd8
JL
4448 vol_info->sectype = master_tcon->ses->sectype;
4449 vol_info->sign = master_tcon->ses->sign;
9d002df4 4450
8a8798a5
JL
4451 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4452 if (rc) {
4453 tcon = ERR_PTR(rc);
4454 goto out;
4455 }
9d002df4
JL
4456
4457 /* get a reference for the same TCP session */
3f9bcca7 4458 spin_lock(&cifs_tcp_ses_lock);
9d002df4 4459 ++master_tcon->ses->server->srv_count;
3f9bcca7 4460 spin_unlock(&cifs_tcp_ses_lock);
9d002df4
JL
4461
4462 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4463 if (IS_ERR(ses)) {
96daf2b0 4464 tcon = (struct cifs_tcon *)ses;
53e0e11e 4465 cifs_put_tcp_session(master_tcon->ses->server, 0);
9d002df4
JL
4466 goto out;
4467 }
4468
4469 tcon = cifs_get_tcon(ses, vol_info);
4470 if (IS_ERR(tcon)) {
4471 cifs_put_smb_ses(ses);
4472 goto out;
4473 }
4474
ce558b0e
SF
4475#ifdef CONFIG_CIFS_SMB311
4476 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4477 if (tcon->posix_extensions)
4478 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4479#endif /* SMB3.11 */
29e20f9c 4480 if (cap_unix(ses))
9d002df4 4481 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
b326614e 4482
9d002df4 4483out:
8a8798a5 4484 kfree(vol_info->username);
97f4b727 4485 kzfree(vol_info->password);
9d002df4
JL
4486 kfree(vol_info);
4487
4488 return tcon;
4489}
4490
96daf2b0 4491struct cifs_tcon *
9d002df4
JL
4492cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4493{
4494 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4495}
4496
b647c35f
JL
4497/* find and return a tlink with given uid */
4498static struct tcon_link *
6d4a0832 4499tlink_rb_search(struct rb_root *root, kuid_t uid)
b647c35f
JL
4500{
4501 struct rb_node *node = root->rb_node;
4502 struct tcon_link *tlink;
4503
4504 while (node) {
4505 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4506
6d4a0832 4507 if (uid_gt(tlink->tl_uid, uid))
b647c35f 4508 node = node->rb_left;
6d4a0832 4509 else if (uid_lt(tlink->tl_uid, uid))
b647c35f
JL
4510 node = node->rb_right;
4511 else
4512 return tlink;
4513 }
4514 return NULL;
4515}
4516
4517/* insert a tcon_link into the tree */
4518static void
4519tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4520{
4521 struct rb_node **new = &(root->rb_node), *parent = NULL;
4522 struct tcon_link *tlink;
4523
4524 while (*new) {
4525 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4526 parent = *new;
4527
6d4a0832 4528 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
b647c35f
JL
4529 new = &((*new)->rb_left);
4530 else
4531 new = &((*new)->rb_right);
4532 }
4533
4534 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4535 rb_insert_color(&new_tlink->tl_rbnode, root);
4536}
4537
9d002df4
JL
4538/*
4539 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4540 * current task.
4541 *
4542 * If the superblock doesn't refer to a multiuser mount, then just return
4543 * the master tcon for the mount.
4544 *
6ef933a3 4545 * First, search the rbtree for an existing tcon for this fsuid. If one
9d002df4
JL
4546 * exists, then check to see if it's pending construction. If it is then wait
4547 * for construction to complete. Once it's no longer pending, check to see if
4548 * it failed and either return an error or retry construction, depending on
4549 * the timeout.
4550 *
4551 * If one doesn't exist then insert a new tcon_link struct into the tree and
4552 * try to construct a new one.
4553 */
4554struct tcon_link *
4555cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4556{
4557 int ret;
6d4a0832 4558 kuid_t fsuid = current_fsuid();
9d002df4
JL
4559 struct tcon_link *tlink, *newtlink;
4560
4561 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4562 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4563
4564 spin_lock(&cifs_sb->tlink_tree_lock);
b647c35f 4565 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
9d002df4
JL
4566 if (tlink)
4567 cifs_get_tlink(tlink);
4568 spin_unlock(&cifs_sb->tlink_tree_lock);
4569
4570 if (tlink == NULL) {
4571 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4572 if (newtlink == NULL)
4573 return ERR_PTR(-ENOMEM);
b647c35f 4574 newtlink->tl_uid = fsuid;
9d002df4
JL
4575 newtlink->tl_tcon = ERR_PTR(-EACCES);
4576 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4577 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4578 cifs_get_tlink(newtlink);
4579
9d002df4
JL
4580 spin_lock(&cifs_sb->tlink_tree_lock);
4581 /* was one inserted after previous search? */
b647c35f 4582 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
9d002df4
JL
4583 if (tlink) {
4584 cifs_get_tlink(tlink);
4585 spin_unlock(&cifs_sb->tlink_tree_lock);
9d002df4
JL
4586 kfree(newtlink);
4587 goto wait_for_construction;
4588 }
9d002df4 4589 tlink = newtlink;
b647c35f
JL
4590 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4591 spin_unlock(&cifs_sb->tlink_tree_lock);
9d002df4
JL
4592 } else {
4593wait_for_construction:
4594 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
9d002df4
JL
4595 TASK_INTERRUPTIBLE);
4596 if (ret) {
4597 cifs_put_tlink(tlink);
74316201 4598 return ERR_PTR(-ERESTARTSYS);
9d002df4
JL
4599 }
4600
4601 /* if it's good, return it */
4602 if (!IS_ERR(tlink->tl_tcon))
4603 return tlink;
4604
4605 /* return error if we tried this already recently */
4606 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4607 cifs_put_tlink(tlink);
4608 return ERR_PTR(-EACCES);
4609 }
4610
4611 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4612 goto wait_for_construction;
4613 }
4614
4615 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4616 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4617 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4618
4619 if (IS_ERR(tlink->tl_tcon)) {
4620 cifs_put_tlink(tlink);
4621 return ERR_PTR(-EACCES);
4622 }
4623
4624 return tlink;
4625}
2de970ff
JL
4626
4627/*
4628 * periodic workqueue job that scans tcon_tree for a superblock and closes
4629 * out tcons.
4630 */
4631static void
4632cifs_prune_tlinks(struct work_struct *work)
4633{
4634 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4635 prune_tlinks.work);
b647c35f 4636 struct rb_root *root = &cifs_sb->tlink_tree;
37e12f55 4637 struct rb_node *node;
b647c35f
JL
4638 struct rb_node *tmp;
4639 struct tcon_link *tlink;
2de970ff 4640
b647c35f
JL
4641 /*
4642 * Because we drop the spinlock in the loop in order to put the tlink
4643 * it's not guarded against removal of links from the tree. The only
4644 * places that remove entries from the tree are this function and
4645 * umounts. Because this function is non-reentrant and is canceled
4646 * before umount can proceed, this is safe.
4647 */
4648 spin_lock(&cifs_sb->tlink_tree_lock);
4649 node = rb_first(root);
4650 while (node != NULL) {
4651 tmp = node;
4652 node = rb_next(tmp);
4653 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4654
4655 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4656 atomic_read(&tlink->tl_count) != 0 ||
4657 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4658 continue;
2de970ff 4659
b647c35f
JL
4660 cifs_get_tlink(tlink);
4661 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4662 rb_erase(tmp, root);
4663
4664 spin_unlock(&cifs_sb->tlink_tree_lock);
4665 cifs_put_tlink(tlink);
4666 spin_lock(&cifs_sb->tlink_tree_lock);
4667 }
4668 spin_unlock(&cifs_sb->tlink_tree_lock);
2de970ff 4669
da472fc8 4670 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
2de970ff
JL
4671 TLINK_IDLE_EXPIRE);
4672}