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