xref: /openbmc/linux/fs/smb/client/connect.c (revision aad29a73199b7fbccfbabea3f1ee627ad1924f52)
138c8a9a5SSteve French // SPDX-License-Identifier: LGPL-2.1
238c8a9a5SSteve French /*
338c8a9a5SSteve French  *
438c8a9a5SSteve French  *   Copyright (C) International Business Machines  Corp., 2002,2011
538c8a9a5SSteve French  *   Author(s): Steve French (sfrench@us.ibm.com)
638c8a9a5SSteve French  *
738c8a9a5SSteve French  */
838c8a9a5SSteve French #include <linux/fs.h>
938c8a9a5SSteve French #include <linux/net.h>
1038c8a9a5SSteve French #include <linux/string.h>
1138c8a9a5SSteve French #include <linux/sched/mm.h>
1238c8a9a5SSteve French #include <linux/sched/signal.h>
1338c8a9a5SSteve French #include <linux/list.h>
1438c8a9a5SSteve French #include <linux/wait.h>
1538c8a9a5SSteve French #include <linux/slab.h>
1638c8a9a5SSteve French #include <linux/pagemap.h>
1738c8a9a5SSteve French #include <linux/ctype.h>
1838c8a9a5SSteve French #include <linux/utsname.h>
1938c8a9a5SSteve French #include <linux/mempool.h>
2038c8a9a5SSteve French #include <linux/delay.h>
2138c8a9a5SSteve French #include <linux/completion.h>
2238c8a9a5SSteve French #include <linux/kthread.h>
2338c8a9a5SSteve French #include <linux/pagevec.h>
2438c8a9a5SSteve French #include <linux/freezer.h>
2538c8a9a5SSteve French #include <linux/namei.h>
2638c8a9a5SSteve French #include <linux/uuid.h>
2738c8a9a5SSteve French #include <linux/uaccess.h>
2838c8a9a5SSteve French #include <asm/processor.h>
2938c8a9a5SSteve French #include <linux/inet.h>
3038c8a9a5SSteve French #include <linux/module.h>
3138c8a9a5SSteve French #include <keys/user-type.h>
3238c8a9a5SSteve French #include <net/ipv6.h>
3338c8a9a5SSteve French #include <linux/parser.h>
3438c8a9a5SSteve French #include <linux/bvec.h>
3538c8a9a5SSteve French #include "cifspdu.h"
3638c8a9a5SSteve French #include "cifsglob.h"
3738c8a9a5SSteve French #include "cifsproto.h"
3838c8a9a5SSteve French #include "cifs_unicode.h"
3938c8a9a5SSteve French #include "cifs_debug.h"
4038c8a9a5SSteve French #include "cifs_fs_sb.h"
4138c8a9a5SSteve French #include "ntlmssp.h"
4238c8a9a5SSteve French #include "nterr.h"
4338c8a9a5SSteve French #include "rfc1002pdu.h"
4438c8a9a5SSteve French #include "fscache.h"
4538c8a9a5SSteve French #include "smb2proto.h"
4638c8a9a5SSteve French #include "smbdirect.h"
4738c8a9a5SSteve French #include "dns_resolve.h"
4838c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
4938c8a9a5SSteve French #include "dfs.h"
5038c8a9a5SSteve French #include "dfs_cache.h"
5138c8a9a5SSteve French #endif
5238c8a9a5SSteve French #include "fs_context.h"
5338c8a9a5SSteve French #include "cifs_swn.h"
5438c8a9a5SSteve French 
5538c8a9a5SSteve French /* FIXME: should these be tunable? */
5638c8a9a5SSteve French #define TLINK_ERROR_EXPIRE	(1 * HZ)
5738c8a9a5SSteve French #define TLINK_IDLE_EXPIRE	(600 * HZ)
5838c8a9a5SSteve French 
5938c8a9a5SSteve French /* Drop the connection to not overload the server */
6069cba9d3SShyam Prasad N #define MAX_STATUS_IO_TIMEOUT   5
6138c8a9a5SSteve French 
6238c8a9a5SSteve French static int ip_connect(struct TCP_Server_Info *server);
6338c8a9a5SSteve French static int generic_ip_connect(struct TCP_Server_Info *server);
6438c8a9a5SSteve French static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
6538c8a9a5SSteve French static void cifs_prune_tlinks(struct work_struct *work);
6638c8a9a5SSteve French 
6738c8a9a5SSteve French /*
6838c8a9a5SSteve French  * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
6938c8a9a5SSteve French  * get their ip addresses changed at some point.
7038c8a9a5SSteve French  *
7138c8a9a5SSteve French  * This should be called with server->srv_mutex held.
7238c8a9a5SSteve French  */
reconn_set_ipaddr_from_hostname(struct TCP_Server_Info * server)7338c8a9a5SSteve French static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
7438c8a9a5SSteve French {
7538c8a9a5SSteve French 	int rc;
7638c8a9a5SSteve French 	int len;
7738c8a9a5SSteve French 	char *unc;
7838c8a9a5SSteve French 	struct sockaddr_storage ss;
7938c8a9a5SSteve French 
8038c8a9a5SSteve French 	if (!server->hostname)
8138c8a9a5SSteve French 		return -EINVAL;
8238c8a9a5SSteve French 
8338c8a9a5SSteve French 	/* if server hostname isn't populated, there's nothing to do here */
8438c8a9a5SSteve French 	if (server->hostname[0] == '\0')
8538c8a9a5SSteve French 		return 0;
8638c8a9a5SSteve French 
8738c8a9a5SSteve French 	len = strlen(server->hostname) + 3;
8838c8a9a5SSteve French 
8938c8a9a5SSteve French 	unc = kmalloc(len, GFP_KERNEL);
9038c8a9a5SSteve French 	if (!unc) {
9138c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
9238c8a9a5SSteve French 		return -ENOMEM;
9338c8a9a5SSteve French 	}
9438c8a9a5SSteve French 	scnprintf(unc, len, "\\\\%s", server->hostname);
9538c8a9a5SSteve French 
9638c8a9a5SSteve French 	spin_lock(&server->srv_lock);
9738c8a9a5SSteve French 	ss = server->dstaddr;
9838c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
9938c8a9a5SSteve French 
10038c8a9a5SSteve French 	rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
10138c8a9a5SSteve French 	kfree(unc);
10238c8a9a5SSteve French 
10338c8a9a5SSteve French 	if (rc < 0) {
10438c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
10538c8a9a5SSteve French 			 __func__, server->hostname, rc);
10638c8a9a5SSteve French 	} else {
10738c8a9a5SSteve French 		spin_lock(&server->srv_lock);
10838c8a9a5SSteve French 		memcpy(&server->dstaddr, &ss, sizeof(server->dstaddr));
10938c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
11038c8a9a5SSteve French 		rc = 0;
11138c8a9a5SSteve French 	}
11238c8a9a5SSteve French 
11338c8a9a5SSteve French 	return rc;
11438c8a9a5SSteve French }
11538c8a9a5SSteve French 
smb2_query_server_interfaces(struct work_struct * work)11638c8a9a5SSteve French static void smb2_query_server_interfaces(struct work_struct *work)
11738c8a9a5SSteve French {
11838c8a9a5SSteve French 	int rc;
119407ced4eSShyam Prasad N 	int xid;
12038c8a9a5SSteve French 	struct cifs_tcon *tcon = container_of(work,
12138c8a9a5SSteve French 					struct cifs_tcon,
12238c8a9a5SSteve French 					query_interfaces.work);
12366c2940cSShyam Prasad N 	struct TCP_Server_Info *server = tcon->ses->server;
12438c8a9a5SSteve French 
12538c8a9a5SSteve French 	/*
12638c8a9a5SSteve French 	 * query server network interfaces, in case they change
12738c8a9a5SSteve French 	 */
12866c2940cSShyam Prasad N 	if (!server->ops->query_server_interfaces)
12966c2940cSShyam Prasad N 		return;
13066c2940cSShyam Prasad N 
131407ced4eSShyam Prasad N 	xid = get_xid();
13266c2940cSShyam Prasad N 	rc = server->ops->query_server_interfaces(xid, tcon, false);
133407ced4eSShyam Prasad N 	free_xid(xid);
134407ced4eSShyam Prasad N 
13538c8a9a5SSteve French 	if (rc) {
1365075e9f4SShyam Prasad N 		if (rc == -EOPNOTSUPP)
1375075e9f4SShyam Prasad N 			return;
1385075e9f4SShyam Prasad N 
13938c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
14038c8a9a5SSteve French 				__func__, rc);
14138c8a9a5SSteve French 	}
14238c8a9a5SSteve French 
14338c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
14438c8a9a5SSteve French 			   (SMB_INTERFACE_POLL_INTERVAL * HZ));
14538c8a9a5SSteve French }
14638c8a9a5SSteve French 
14738c8a9a5SSteve French /*
14838c8a9a5SSteve French  * Update the tcpStatus for the server.
14938c8a9a5SSteve French  * This is used to signal the cifsd thread to call cifs_reconnect
15038c8a9a5SSteve French  * ONLY cifsd thread should call cifs_reconnect. For any other
15138c8a9a5SSteve French  * thread, use this function
15238c8a9a5SSteve French  *
15338c8a9a5SSteve French  * @server: the tcp ses for which reconnect is needed
15438c8a9a5SSteve French  * @all_channels: if this needs to be done for all channels
15538c8a9a5SSteve French  */
15638c8a9a5SSteve French void
cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info * server,bool all_channels)15738c8a9a5SSteve French cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
15838c8a9a5SSteve French 				bool all_channels)
15938c8a9a5SSteve French {
16038c8a9a5SSteve French 	struct TCP_Server_Info *pserver;
16138c8a9a5SSteve French 	struct cifs_ses *ses;
16238c8a9a5SSteve French 	int i;
16338c8a9a5SSteve French 
16438c8a9a5SSteve French 	/* If server is a channel, select the primary channel */
165b3773b19SSteve French 	pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
16638c8a9a5SSteve French 
167e42c5730SShyam Prasad N 	/* if we need to signal just this channel */
16838c8a9a5SSteve French 	if (!all_channels) {
169e42c5730SShyam Prasad N 		spin_lock(&server->srv_lock);
170e42c5730SShyam Prasad N 		if (server->tcpStatus != CifsExiting)
171e42c5730SShyam Prasad N 			server->tcpStatus = CifsNeedReconnect;
172e42c5730SShyam Prasad N 		spin_unlock(&server->srv_lock);
17338c8a9a5SSteve French 		return;
17438c8a9a5SSteve French 	}
17538c8a9a5SSteve French 
17638c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
17738c8a9a5SSteve French 	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
1782cfff217SPaulo Alcantara 		if (cifs_ses_exiting(ses))
1792cfff217SPaulo Alcantara 			continue;
18038c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
18138c8a9a5SSteve French 		for (i = 0; i < ses->chan_count; i++) {
182c395f798SShyam Prasad N 			if (!ses->chans[i].server)
183c395f798SShyam Prasad N 				continue;
184c395f798SShyam Prasad N 
18538c8a9a5SSteve French 			spin_lock(&ses->chans[i].server->srv_lock);
186c395f798SShyam Prasad N 			if (ses->chans[i].server->tcpStatus != CifsExiting)
18738c8a9a5SSteve French 				ses->chans[i].server->tcpStatus = CifsNeedReconnect;
18838c8a9a5SSteve French 			spin_unlock(&ses->chans[i].server->srv_lock);
18938c8a9a5SSteve French 		}
19038c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
19138c8a9a5SSteve French 	}
19238c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
19338c8a9a5SSteve French }
19438c8a9a5SSteve French 
19538c8a9a5SSteve French /*
19638c8a9a5SSteve French  * Mark all sessions and tcons for reconnect.
19738c8a9a5SSteve French  * IMPORTANT: make sure that this gets called only from
19838c8a9a5SSteve French  * cifsd thread. For any other thread, use
19938c8a9a5SSteve French  * cifs_signal_cifsd_for_reconnect
20038c8a9a5SSteve French  *
20138c8a9a5SSteve French  * @server: the tcp ses for which reconnect is needed
20238c8a9a5SSteve French  * @server needs to be previously set to CifsNeedReconnect.
20338c8a9a5SSteve French  * @mark_smb_session: whether even sessions need to be marked
20438c8a9a5SSteve French  */
20538c8a9a5SSteve French void
cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info * server,bool mark_smb_session)20638c8a9a5SSteve French cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
20738c8a9a5SSteve French 				      bool mark_smb_session)
20838c8a9a5SSteve French {
20938c8a9a5SSteve French 	struct TCP_Server_Info *pserver;
21038c8a9a5SSteve French 	struct cifs_ses *ses, *nses;
21138c8a9a5SSteve French 	struct cifs_tcon *tcon;
21238c8a9a5SSteve French 
21338c8a9a5SSteve French 	/*
21438c8a9a5SSteve French 	 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
21538c8a9a5SSteve French 	 * are not used until reconnected.
21638c8a9a5SSteve French 	 */
21738c8a9a5SSteve French 	cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__);
21838c8a9a5SSteve French 
21938c8a9a5SSteve French 	/* If server is a channel, select the primary channel */
220b3773b19SSteve French 	pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
22138c8a9a5SSteve French 
22214bafd19SShyam Prasad N 	/*
22314bafd19SShyam Prasad N 	 * if the server has been marked for termination, there is a
22414bafd19SShyam Prasad N 	 * chance that the remaining channels all need reconnect. To be
22514bafd19SShyam Prasad N 	 * on the safer side, mark the session and trees for reconnect
22614bafd19SShyam Prasad N 	 * for this scenario. This might cause a few redundant session
22714bafd19SShyam Prasad N 	 * setup and tree connect requests, but it is better than not doing
22814bafd19SShyam Prasad N 	 * a tree connect when needed, and all following requests failing
22914bafd19SShyam Prasad N 	 */
23014bafd19SShyam Prasad N 	if (server->terminate) {
23114bafd19SShyam Prasad N 		mark_smb_session = true;
23214bafd19SShyam Prasad N 		server = pserver;
23314bafd19SShyam Prasad N 	}
23438c8a9a5SSteve French 
23538c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
23638c8a9a5SSteve French 	list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) {
2376202996aSPaulo Alcantara 		spin_lock(&ses->ses_lock);
2386202996aSPaulo Alcantara 		if (ses->ses_status == SES_EXITING) {
2396202996aSPaulo Alcantara 			spin_unlock(&ses->ses_lock);
2406202996aSPaulo Alcantara 			continue;
2416202996aSPaulo Alcantara 		}
2426202996aSPaulo Alcantara 		spin_unlock(&ses->ses_lock);
2436202996aSPaulo Alcantara 
24438c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
245dd40cbafSShyam Prasad N 		if (cifs_ses_get_chan_index(ses, server) ==
246dd40cbafSShyam Prasad N 		    CIFS_INVAL_CHAN_INDEX) {
247dd40cbafSShyam Prasad N 			spin_unlock(&ses->chan_lock);
248dd40cbafSShyam Prasad N 			continue;
249dd40cbafSShyam Prasad N 		}
250dd40cbafSShyam Prasad N 
25140fc7dfdSShyam Prasad N 		if (!cifs_chan_is_iface_active(ses, server)) {
25240fc7dfdSShyam Prasad N 			spin_unlock(&ses->chan_lock);
25340fc7dfdSShyam Prasad N 			cifs_chan_update_iface(ses, server);
25440fc7dfdSShyam Prasad N 			spin_lock(&ses->chan_lock);
25540fc7dfdSShyam Prasad N 		}
25640fc7dfdSShyam Prasad N 
25738c8a9a5SSteve French 		if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) {
25838c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
25938c8a9a5SSteve French 			continue;
26038c8a9a5SSteve French 		}
26138c8a9a5SSteve French 
26238c8a9a5SSteve French 		if (mark_smb_session)
26338c8a9a5SSteve French 			CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses);
26438c8a9a5SSteve French 		else
26538c8a9a5SSteve French 			cifs_chan_set_need_reconnect(ses, server);
26638c8a9a5SSteve French 
26738c8a9a5SSteve French 		cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
26838c8a9a5SSteve French 			 __func__, ses->chans_need_reconnect);
26938c8a9a5SSteve French 
27038c8a9a5SSteve French 		/* If all channels need reconnect, then tcon needs reconnect */
27138c8a9a5SSteve French 		if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
27238c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
27338c8a9a5SSteve French 			continue;
27438c8a9a5SSteve French 		}
27538c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
27638c8a9a5SSteve French 
27738c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
27838c8a9a5SSteve French 		ses->ses_status = SES_NEED_RECON;
27938c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
28038c8a9a5SSteve French 
28138c8a9a5SSteve French 		list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
28238c8a9a5SSteve French 			tcon->need_reconnect = true;
28338c8a9a5SSteve French 			spin_lock(&tcon->tc_lock);
28438c8a9a5SSteve French 			tcon->status = TID_NEED_RECON;
28538c8a9a5SSteve French 			spin_unlock(&tcon->tc_lock);
2863e161536SShyam Prasad N 
2873e161536SShyam Prasad N 			cancel_delayed_work(&tcon->query_interfaces);
28838c8a9a5SSteve French 		}
28938c8a9a5SSteve French 		if (ses->tcon_ipc) {
29038c8a9a5SSteve French 			ses->tcon_ipc->need_reconnect = true;
29138c8a9a5SSteve French 			spin_lock(&ses->tcon_ipc->tc_lock);
29238c8a9a5SSteve French 			ses->tcon_ipc->status = TID_NEED_RECON;
29338c8a9a5SSteve French 			spin_unlock(&ses->tcon_ipc->tc_lock);
29438c8a9a5SSteve French 		}
29538c8a9a5SSteve French 	}
29638c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
29738c8a9a5SSteve French }
29838c8a9a5SSteve French 
29938c8a9a5SSteve French static void
cifs_abort_connection(struct TCP_Server_Info * server)30038c8a9a5SSteve French cifs_abort_connection(struct TCP_Server_Info *server)
30138c8a9a5SSteve French {
30238c8a9a5SSteve French 	struct mid_q_entry *mid, *nmid;
30338c8a9a5SSteve French 	struct list_head retry_list;
30438c8a9a5SSteve French 
30538c8a9a5SSteve French 	server->maxBuf = 0;
30638c8a9a5SSteve French 	server->max_read = 0;
30738c8a9a5SSteve French 
30838c8a9a5SSteve French 	/* do not want to be sending data on a socket we are freeing */
30938c8a9a5SSteve French 	cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
31038c8a9a5SSteve French 	cifs_server_lock(server);
31138c8a9a5SSteve French 	if (server->ssocket) {
31238c8a9a5SSteve French 		cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
31338c8a9a5SSteve French 			 server->ssocket->flags);
31438c8a9a5SSteve French 		kernel_sock_shutdown(server->ssocket, SHUT_WR);
31538c8a9a5SSteve French 		cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
31638c8a9a5SSteve French 			 server->ssocket->flags);
31738c8a9a5SSteve French 		sock_release(server->ssocket);
31838c8a9a5SSteve French 		server->ssocket = NULL;
31938c8a9a5SSteve French 	}
32038c8a9a5SSteve French 	server->sequence_number = 0;
32138c8a9a5SSteve French 	server->session_estab = false;
32238c8a9a5SSteve French 	kfree_sensitive(server->session_key.response);
32338c8a9a5SSteve French 	server->session_key.response = NULL;
32438c8a9a5SSteve French 	server->session_key.len = 0;
32538c8a9a5SSteve French 	server->lstrp = jiffies;
32638c8a9a5SSteve French 
32738c8a9a5SSteve French 	/* mark submitted MIDs for retry and issue callback */
32838c8a9a5SSteve French 	INIT_LIST_HEAD(&retry_list);
32938c8a9a5SSteve French 	cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
33038c8a9a5SSteve French 	spin_lock(&server->mid_lock);
33138c8a9a5SSteve French 	list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
33238c8a9a5SSteve French 		kref_get(&mid->refcount);
33338c8a9a5SSteve French 		if (mid->mid_state == MID_REQUEST_SUBMITTED)
33438c8a9a5SSteve French 			mid->mid_state = MID_RETRY_NEEDED;
33538c8a9a5SSteve French 		list_move(&mid->qhead, &retry_list);
33638c8a9a5SSteve French 		mid->mid_flags |= MID_DELETED;
33738c8a9a5SSteve French 	}
33838c8a9a5SSteve French 	spin_unlock(&server->mid_lock);
33938c8a9a5SSteve French 	cifs_server_unlock(server);
34038c8a9a5SSteve French 
34138c8a9a5SSteve French 	cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
34238c8a9a5SSteve French 	list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
34338c8a9a5SSteve French 		list_del_init(&mid->qhead);
34438c8a9a5SSteve French 		mid->callback(mid);
34538c8a9a5SSteve French 		release_mid(mid);
34638c8a9a5SSteve French 	}
34738c8a9a5SSteve French 
34838c8a9a5SSteve French 	if (cifs_rdma_enabled(server)) {
34938c8a9a5SSteve French 		cifs_server_lock(server);
35038c8a9a5SSteve French 		smbd_destroy(server);
35138c8a9a5SSteve French 		cifs_server_unlock(server);
35238c8a9a5SSteve French 	}
35338c8a9a5SSteve French }
35438c8a9a5SSteve French 
cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info * server,int num_targets)35538c8a9a5SSteve French static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
35638c8a9a5SSteve French {
35738c8a9a5SSteve French 	spin_lock(&server->srv_lock);
35838c8a9a5SSteve French 	server->nr_targets = num_targets;
35938c8a9a5SSteve French 	if (server->tcpStatus == CifsExiting) {
36038c8a9a5SSteve French 		/* the demux thread will exit normally next time through the loop */
36138c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
36238c8a9a5SSteve French 		wake_up(&server->response_q);
36338c8a9a5SSteve French 		return false;
36438c8a9a5SSteve French 	}
36538c8a9a5SSteve French 
36638c8a9a5SSteve French 	cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
36738c8a9a5SSteve French 	trace_smb3_reconnect(server->CurrentMid, server->conn_id,
36838c8a9a5SSteve French 			     server->hostname);
36938c8a9a5SSteve French 	server->tcpStatus = CifsNeedReconnect;
37038c8a9a5SSteve French 
37138c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
37238c8a9a5SSteve French 	return true;
37338c8a9a5SSteve French }
37438c8a9a5SSteve French 
37538c8a9a5SSteve French /*
37638c8a9a5SSteve French  * cifs tcp session reconnection
37738c8a9a5SSteve French  *
37838c8a9a5SSteve French  * mark tcp session as reconnecting so temporarily locked
37938c8a9a5SSteve French  * mark all smb sessions as reconnecting for tcp session
38038c8a9a5SSteve French  * reconnect tcp session
38138c8a9a5SSteve French  * wake up waiters on reconnection? - (not needed currently)
38238c8a9a5SSteve French  *
38338c8a9a5SSteve French  * if mark_smb_session is passed as true, unconditionally mark
38438c8a9a5SSteve French  * the smb session (and tcon) for reconnect as well. This value
38538c8a9a5SSteve French  * doesn't really matter for non-multichannel scenario.
38638c8a9a5SSteve French  *
38738c8a9a5SSteve French  */
__cifs_reconnect(struct TCP_Server_Info * server,bool mark_smb_session)38838c8a9a5SSteve French static int __cifs_reconnect(struct TCP_Server_Info *server,
38938c8a9a5SSteve French 			    bool mark_smb_session)
39038c8a9a5SSteve French {
39138c8a9a5SSteve French 	int rc = 0;
39238c8a9a5SSteve French 
39338c8a9a5SSteve French 	if (!cifs_tcp_ses_needs_reconnect(server, 1))
39438c8a9a5SSteve French 		return 0;
39538c8a9a5SSteve French 
39638c8a9a5SSteve French 	cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
39738c8a9a5SSteve French 
39838c8a9a5SSteve French 	cifs_abort_connection(server);
39938c8a9a5SSteve French 
40038c8a9a5SSteve French 	do {
40138c8a9a5SSteve French 		try_to_freeze();
40238c8a9a5SSteve French 		cifs_server_lock(server);
40338c8a9a5SSteve French 
40438c8a9a5SSteve French 		if (!cifs_swn_set_server_dstaddr(server)) {
40538c8a9a5SSteve French 			/* resolve the hostname again to make sure that IP address is up-to-date */
40638c8a9a5SSteve French 			rc = reconn_set_ipaddr_from_hostname(server);
40738c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
40838c8a9a5SSteve French 		}
40938c8a9a5SSteve French 
41038c8a9a5SSteve French 		if (cifs_rdma_enabled(server))
41138c8a9a5SSteve French 			rc = smbd_reconnect(server);
41238c8a9a5SSteve French 		else
41338c8a9a5SSteve French 			rc = generic_ip_connect(server);
41438c8a9a5SSteve French 		if (rc) {
41538c8a9a5SSteve French 			cifs_server_unlock(server);
41638c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
41738c8a9a5SSteve French 			msleep(3000);
41838c8a9a5SSteve French 		} else {
41938c8a9a5SSteve French 			atomic_inc(&tcpSesReconnectCount);
42038c8a9a5SSteve French 			set_credits(server, 1);
42138c8a9a5SSteve French 			spin_lock(&server->srv_lock);
42238c8a9a5SSteve French 			if (server->tcpStatus != CifsExiting)
42338c8a9a5SSteve French 				server->tcpStatus = CifsNeedNegotiate;
42438c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
42538c8a9a5SSteve French 			cifs_swn_reset_server_dstaddr(server);
42638c8a9a5SSteve French 			cifs_server_unlock(server);
4273a0ef868SShyam Prasad N 			mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
42838c8a9a5SSteve French 		}
42938c8a9a5SSteve French 	} while (server->tcpStatus == CifsNeedReconnect);
43038c8a9a5SSteve French 
43138c8a9a5SSteve French 	spin_lock(&server->srv_lock);
43238c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedNegotiate)
43338c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
43438c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
43538c8a9a5SSteve French 
43638c8a9a5SSteve French 	wake_up(&server->response_q);
43738c8a9a5SSteve French 	return rc;
43838c8a9a5SSteve French }
43938c8a9a5SSteve French 
44038c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
__reconnect_target_unlocked(struct TCP_Server_Info * server,const char * target)44138c8a9a5SSteve French static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
44238c8a9a5SSteve French {
44338c8a9a5SSteve French 	int rc;
44438c8a9a5SSteve French 	char *hostname;
44538c8a9a5SSteve French 
44638c8a9a5SSteve French 	if (!cifs_swn_set_server_dstaddr(server)) {
44738c8a9a5SSteve French 		if (server->hostname != target) {
44838c8a9a5SSteve French 			hostname = extract_hostname(target);
44938c8a9a5SSteve French 			if (!IS_ERR(hostname)) {
45038c8a9a5SSteve French 				spin_lock(&server->srv_lock);
45138c8a9a5SSteve French 				kfree(server->hostname);
45238c8a9a5SSteve French 				server->hostname = hostname;
45338c8a9a5SSteve French 				spin_unlock(&server->srv_lock);
45438c8a9a5SSteve French 			} else {
45538c8a9a5SSteve French 				cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
45638c8a9a5SSteve French 					 __func__, PTR_ERR(hostname));
45738c8a9a5SSteve French 				cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
45838c8a9a5SSteve French 					 server->hostname);
45938c8a9a5SSteve French 			}
46038c8a9a5SSteve French 		}
46138c8a9a5SSteve French 		/* resolve the hostname again to make sure that IP address is up-to-date. */
46238c8a9a5SSteve French 		rc = reconn_set_ipaddr_from_hostname(server);
46338c8a9a5SSteve French 		cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
46438c8a9a5SSteve French 	}
46538c8a9a5SSteve French 	/* Reconnect the socket */
46638c8a9a5SSteve French 	if (cifs_rdma_enabled(server))
46738c8a9a5SSteve French 		rc = smbd_reconnect(server);
46838c8a9a5SSteve French 	else
46938c8a9a5SSteve French 		rc = generic_ip_connect(server);
47038c8a9a5SSteve French 
47138c8a9a5SSteve French 	return rc;
47238c8a9a5SSteve French }
47338c8a9a5SSteve French 
reconnect_target_unlocked(struct TCP_Server_Info * server,struct dfs_cache_tgt_list * tl,struct dfs_cache_tgt_iterator ** target_hint)47438c8a9a5SSteve French static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
47538c8a9a5SSteve French 				     struct dfs_cache_tgt_iterator **target_hint)
47638c8a9a5SSteve French {
47738c8a9a5SSteve French 	int rc;
47838c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *tit;
47938c8a9a5SSteve French 
48038c8a9a5SSteve French 	*target_hint = NULL;
48138c8a9a5SSteve French 
48238c8a9a5SSteve French 	/* If dfs target list is empty, then reconnect to last server */
48338c8a9a5SSteve French 	tit = dfs_cache_get_tgt_iterator(tl);
48438c8a9a5SSteve French 	if (!tit)
48538c8a9a5SSteve French 		return __reconnect_target_unlocked(server, server->hostname);
48638c8a9a5SSteve French 
48738c8a9a5SSteve French 	/* Otherwise, try every dfs target in @tl */
48838c8a9a5SSteve French 	for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
48938c8a9a5SSteve French 		rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
49038c8a9a5SSteve French 		if (!rc) {
49138c8a9a5SSteve French 			*target_hint = tit;
49238c8a9a5SSteve French 			break;
49338c8a9a5SSteve French 		}
49438c8a9a5SSteve French 	}
49538c8a9a5SSteve French 	return rc;
49638c8a9a5SSteve French }
49738c8a9a5SSteve French 
reconnect_dfs_server(struct TCP_Server_Info * server)49838c8a9a5SSteve French static int reconnect_dfs_server(struct TCP_Server_Info *server)
49938c8a9a5SSteve French {
50038c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *target_hint = NULL;
501da70c1d3SPierre Mariani 
5023fea12f3SPaulo Alcantara 	DFS_CACHE_TGT_LIST(tl);
50338c8a9a5SSteve French 	int num_targets = 0;
5043fea12f3SPaulo Alcantara 	int rc = 0;
50538c8a9a5SSteve French 
50638c8a9a5SSteve French 	/*
50738c8a9a5SSteve French 	 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
50838c8a9a5SSteve French 	 *
50938c8a9a5SSteve French 	 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
51038c8a9a5SSteve French 	 * targets (server->nr_targets).  It's also possible that the cached referral was cleared
51138c8a9a5SSteve French 	 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
51238c8a9a5SSteve French 	 * refreshing the referral, so, in this case, default it to 1.
51338c8a9a5SSteve French 	 */
51438c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
51538c8a9a5SSteve French 	if (!dfs_cache_noreq_find(server->leaf_fullpath + 1, NULL, &tl))
51638c8a9a5SSteve French 		num_targets = dfs_cache_get_nr_tgts(&tl);
51738c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
51838c8a9a5SSteve French 	if (!num_targets)
51938c8a9a5SSteve French 		num_targets = 1;
52038c8a9a5SSteve French 
52138c8a9a5SSteve French 	if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
52238c8a9a5SSteve French 		return 0;
52338c8a9a5SSteve French 
52438c8a9a5SSteve French 	/*
52538c8a9a5SSteve French 	 * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a
52638c8a9a5SSteve French 	 * different server or share during failover.  It could be improved by adding some logic to
52738c8a9a5SSteve French 	 * only do that in case it connects to a different server or share, though.
52838c8a9a5SSteve French 	 */
52938c8a9a5SSteve French 	cifs_mark_tcp_ses_conns_for_reconnect(server, true);
53038c8a9a5SSteve French 
53138c8a9a5SSteve French 	cifs_abort_connection(server);
53238c8a9a5SSteve French 
53338c8a9a5SSteve French 	do {
53438c8a9a5SSteve French 		try_to_freeze();
53538c8a9a5SSteve French 		cifs_server_lock(server);
53638c8a9a5SSteve French 
53738c8a9a5SSteve French 		rc = reconnect_target_unlocked(server, &tl, &target_hint);
53838c8a9a5SSteve French 		if (rc) {
53938c8a9a5SSteve French 			/* Failed to reconnect socket */
54038c8a9a5SSteve French 			cifs_server_unlock(server);
54138c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
54238c8a9a5SSteve French 			msleep(3000);
54338c8a9a5SSteve French 			continue;
54438c8a9a5SSteve French 		}
54538c8a9a5SSteve French 		/*
54638c8a9a5SSteve French 		 * Socket was created.  Update tcp session status to CifsNeedNegotiate so that a
54738c8a9a5SSteve French 		 * process waiting for reconnect will know it needs to re-establish session and tcon
54838c8a9a5SSteve French 		 * through the reconnected target server.
54938c8a9a5SSteve French 		 */
55038c8a9a5SSteve French 		atomic_inc(&tcpSesReconnectCount);
55138c8a9a5SSteve French 		set_credits(server, 1);
55238c8a9a5SSteve French 		spin_lock(&server->srv_lock);
55338c8a9a5SSteve French 		if (server->tcpStatus != CifsExiting)
55438c8a9a5SSteve French 			server->tcpStatus = CifsNeedNegotiate;
55538c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
55638c8a9a5SSteve French 		cifs_swn_reset_server_dstaddr(server);
55738c8a9a5SSteve French 		cifs_server_unlock(server);
5583a0ef868SShyam Prasad N 		mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
55938c8a9a5SSteve French 	} while (server->tcpStatus == CifsNeedReconnect);
56038c8a9a5SSteve French 
56138c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
56238c8a9a5SSteve French 	dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, target_hint);
56338c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
56438c8a9a5SSteve French 	dfs_cache_free_tgts(&tl);
56538c8a9a5SSteve French 
56638c8a9a5SSteve French 	/* Need to set up echo worker again once connection has been established */
56738c8a9a5SSteve French 	spin_lock(&server->srv_lock);
56838c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedNegotiate)
56938c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
57038c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
57138c8a9a5SSteve French 
57238c8a9a5SSteve French 	wake_up(&server->response_q);
57338c8a9a5SSteve French 	return rc;
57438c8a9a5SSteve French }
57538c8a9a5SSteve French 
cifs_reconnect(struct TCP_Server_Info * server,bool mark_smb_session)57638c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
57738c8a9a5SSteve French {
57838c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
57938c8a9a5SSteve French 	if (!server->leaf_fullpath) {
58038c8a9a5SSteve French 		mutex_unlock(&server->refpath_lock);
58138c8a9a5SSteve French 		return __cifs_reconnect(server, mark_smb_session);
58238c8a9a5SSteve French 	}
58338c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
58438c8a9a5SSteve French 
58538c8a9a5SSteve French 	return reconnect_dfs_server(server);
58638c8a9a5SSteve French }
58738c8a9a5SSteve French #else
cifs_reconnect(struct TCP_Server_Info * server,bool mark_smb_session)58838c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
58938c8a9a5SSteve French {
59038c8a9a5SSteve French 	return __cifs_reconnect(server, mark_smb_session);
59138c8a9a5SSteve French }
59238c8a9a5SSteve French #endif
59338c8a9a5SSteve French 
59438c8a9a5SSteve French static void
cifs_echo_request(struct work_struct * work)59538c8a9a5SSteve French cifs_echo_request(struct work_struct *work)
59638c8a9a5SSteve French {
59738c8a9a5SSteve French 	int rc;
59838c8a9a5SSteve French 	struct TCP_Server_Info *server = container_of(work,
59938c8a9a5SSteve French 					struct TCP_Server_Info, echo.work);
60038c8a9a5SSteve French 
60138c8a9a5SSteve French 	/*
60238c8a9a5SSteve French 	 * We cannot send an echo if it is disabled.
60338c8a9a5SSteve French 	 * Also, no need to ping if we got a response recently.
60438c8a9a5SSteve French 	 */
60538c8a9a5SSteve French 
60638c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedReconnect ||
60738c8a9a5SSteve French 	    server->tcpStatus == CifsExiting ||
60838c8a9a5SSteve French 	    server->tcpStatus == CifsNew ||
60938c8a9a5SSteve French 	    (server->ops->can_echo && !server->ops->can_echo(server)) ||
61038c8a9a5SSteve French 	    time_before(jiffies, server->lstrp + server->echo_interval - HZ))
61138c8a9a5SSteve French 		goto requeue_echo;
61238c8a9a5SSteve French 
61338c8a9a5SSteve French 	rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
61438c8a9a5SSteve French 	cifs_server_dbg(FYI, "send echo request: rc = %d\n", rc);
61538c8a9a5SSteve French 
61638c8a9a5SSteve French 	/* Check witness registrations */
61738c8a9a5SSteve French 	cifs_swn_check();
61838c8a9a5SSteve French 
61938c8a9a5SSteve French requeue_echo:
62038c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
62138c8a9a5SSteve French }
62238c8a9a5SSteve French 
62338c8a9a5SSteve French static bool
allocate_buffers(struct TCP_Server_Info * server)62438c8a9a5SSteve French allocate_buffers(struct TCP_Server_Info *server)
62538c8a9a5SSteve French {
62638c8a9a5SSteve French 	if (!server->bigbuf) {
62738c8a9a5SSteve French 		server->bigbuf = (char *)cifs_buf_get();
62838c8a9a5SSteve French 		if (!server->bigbuf) {
62938c8a9a5SSteve French 			cifs_server_dbg(VFS, "No memory for large SMB response\n");
63038c8a9a5SSteve French 			msleep(3000);
63138c8a9a5SSteve French 			/* retry will check if exiting */
63238c8a9a5SSteve French 			return false;
63338c8a9a5SSteve French 		}
63438c8a9a5SSteve French 	} else if (server->large_buf) {
63538c8a9a5SSteve French 		/* we are reusing a dirty large buf, clear its start */
63638c8a9a5SSteve French 		memset(server->bigbuf, 0, HEADER_SIZE(server));
63738c8a9a5SSteve French 	}
63838c8a9a5SSteve French 
63938c8a9a5SSteve French 	if (!server->smallbuf) {
64038c8a9a5SSteve French 		server->smallbuf = (char *)cifs_small_buf_get();
64138c8a9a5SSteve French 		if (!server->smallbuf) {
64238c8a9a5SSteve French 			cifs_server_dbg(VFS, "No memory for SMB response\n");
64338c8a9a5SSteve French 			msleep(1000);
64438c8a9a5SSteve French 			/* retry will check if exiting */
64538c8a9a5SSteve French 			return false;
64638c8a9a5SSteve French 		}
64738c8a9a5SSteve French 		/* beginning of smb buffer is cleared in our buf_get */
64838c8a9a5SSteve French 	} else {
64938c8a9a5SSteve French 		/* if existing small buf clear beginning */
65038c8a9a5SSteve French 		memset(server->smallbuf, 0, HEADER_SIZE(server));
65138c8a9a5SSteve French 	}
65238c8a9a5SSteve French 
65338c8a9a5SSteve French 	return true;
65438c8a9a5SSteve French }
65538c8a9a5SSteve French 
65638c8a9a5SSteve French static bool
server_unresponsive(struct TCP_Server_Info * server)65738c8a9a5SSteve French server_unresponsive(struct TCP_Server_Info *server)
65838c8a9a5SSteve French {
65938c8a9a5SSteve French 	/*
660fabc4ed2SPaulo Alcantara 	 * If we're in the process of mounting a share or reconnecting a session
661fabc4ed2SPaulo Alcantara 	 * and the server abruptly shut down (e.g. socket wasn't closed, packet
662fabc4ed2SPaulo Alcantara 	 * had been ACK'ed but no SMB response), don't wait longer than 20s to
663fabc4ed2SPaulo Alcantara 	 * negotiate protocol.
664fabc4ed2SPaulo Alcantara 	 */
665fabc4ed2SPaulo Alcantara 	spin_lock(&server->srv_lock);
666fabc4ed2SPaulo Alcantara 	if (server->tcpStatus == CifsInNegotiate &&
667fabc4ed2SPaulo Alcantara 	    time_after(jiffies, server->lstrp + 20 * HZ)) {
668fabc4ed2SPaulo Alcantara 		spin_unlock(&server->srv_lock);
669fabc4ed2SPaulo Alcantara 		cifs_reconnect(server, false);
670fabc4ed2SPaulo Alcantara 		return true;
671fabc4ed2SPaulo Alcantara 	}
672fabc4ed2SPaulo Alcantara 	/*
67338c8a9a5SSteve French 	 * We need to wait 3 echo intervals to make sure we handle such
67438c8a9a5SSteve French 	 * situations right:
67538c8a9a5SSteve French 	 * 1s  client sends a normal SMB request
67638c8a9a5SSteve French 	 * 2s  client gets a response
67738c8a9a5SSteve French 	 * 30s echo workqueue job pops, and decides we got a response recently
67838c8a9a5SSteve French 	 *     and don't need to send another
67938c8a9a5SSteve French 	 * ...
68038c8a9a5SSteve French 	 * 65s kernel_recvmsg times out, and we see that we haven't gotten
68138c8a9a5SSteve French 	 *     a response in >60s.
68238c8a9a5SSteve French 	 */
68338c8a9a5SSteve French 	if ((server->tcpStatus == CifsGood ||
68438c8a9a5SSteve French 	    server->tcpStatus == CifsNeedNegotiate) &&
68538c8a9a5SSteve French 	    (!server->ops->can_echo || server->ops->can_echo(server)) &&
68638c8a9a5SSteve French 	    time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
68738c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
68838c8a9a5SSteve French 		cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
68938c8a9a5SSteve French 			 (3 * server->echo_interval) / HZ);
69038c8a9a5SSteve French 		cifs_reconnect(server, false);
69138c8a9a5SSteve French 		return true;
69238c8a9a5SSteve French 	}
69338c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
69438c8a9a5SSteve French 
69538c8a9a5SSteve French 	return false;
69638c8a9a5SSteve French }
69738c8a9a5SSteve French 
69838c8a9a5SSteve French static inline bool
zero_credits(struct TCP_Server_Info * server)69938c8a9a5SSteve French zero_credits(struct TCP_Server_Info *server)
70038c8a9a5SSteve French {
70138c8a9a5SSteve French 	int val;
70238c8a9a5SSteve French 
70338c8a9a5SSteve French 	spin_lock(&server->req_lock);
70438c8a9a5SSteve French 	val = server->credits + server->echo_credits + server->oplock_credits;
70538c8a9a5SSteve French 	if (server->in_flight == 0 && val == 0) {
70638c8a9a5SSteve French 		spin_unlock(&server->req_lock);
70738c8a9a5SSteve French 		return true;
70838c8a9a5SSteve French 	}
70938c8a9a5SSteve French 	spin_unlock(&server->req_lock);
71038c8a9a5SSteve French 	return false;
71138c8a9a5SSteve French }
71238c8a9a5SSteve French 
71338c8a9a5SSteve French static int
cifs_readv_from_socket(struct TCP_Server_Info * server,struct msghdr * smb_msg)71438c8a9a5SSteve French cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
71538c8a9a5SSteve French {
71638c8a9a5SSteve French 	int length = 0;
71738c8a9a5SSteve French 	int total_read;
71838c8a9a5SSteve French 
71938c8a9a5SSteve French 	for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
72038c8a9a5SSteve French 		try_to_freeze();
72138c8a9a5SSteve French 
72238c8a9a5SSteve French 		/* reconnect if no credits and no requests in flight */
72338c8a9a5SSteve French 		if (zero_credits(server)) {
72438c8a9a5SSteve French 			cifs_reconnect(server, false);
72538c8a9a5SSteve French 			return -ECONNABORTED;
72638c8a9a5SSteve French 		}
72738c8a9a5SSteve French 
72838c8a9a5SSteve French 		if (server_unresponsive(server))
72938c8a9a5SSteve French 			return -ECONNABORTED;
73038c8a9a5SSteve French 		if (cifs_rdma_enabled(server) && server->smbd_conn)
73138c8a9a5SSteve French 			length = smbd_recv(server->smbd_conn, smb_msg);
73238c8a9a5SSteve French 		else
73338c8a9a5SSteve French 			length = sock_recvmsg(server->ssocket, smb_msg, 0);
73438c8a9a5SSteve French 
73538c8a9a5SSteve French 		spin_lock(&server->srv_lock);
73638c8a9a5SSteve French 		if (server->tcpStatus == CifsExiting) {
73738c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
73838c8a9a5SSteve French 			return -ESHUTDOWN;
73938c8a9a5SSteve French 		}
74038c8a9a5SSteve French 
74138c8a9a5SSteve French 		if (server->tcpStatus == CifsNeedReconnect) {
74238c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
74338c8a9a5SSteve French 			cifs_reconnect(server, false);
74438c8a9a5SSteve French 			return -ECONNABORTED;
74538c8a9a5SSteve French 		}
74638c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
74738c8a9a5SSteve French 
74838c8a9a5SSteve French 		if (length == -ERESTARTSYS ||
74938c8a9a5SSteve French 		    length == -EAGAIN ||
75038c8a9a5SSteve French 		    length == -EINTR) {
75138c8a9a5SSteve French 			/*
75238c8a9a5SSteve French 			 * Minimum sleep to prevent looping, allowing socket
75338c8a9a5SSteve French 			 * to clear and app threads to set tcpStatus
75438c8a9a5SSteve French 			 * CifsNeedReconnect if server hung.
75538c8a9a5SSteve French 			 */
75638c8a9a5SSteve French 			usleep_range(1000, 2000);
75738c8a9a5SSteve French 			length = 0;
75838c8a9a5SSteve French 			continue;
75938c8a9a5SSteve French 		}
76038c8a9a5SSteve French 
76138c8a9a5SSteve French 		if (length <= 0) {
76238c8a9a5SSteve French 			cifs_dbg(FYI, "Received no data or error: %d\n", length);
76338c8a9a5SSteve French 			cifs_reconnect(server, false);
76438c8a9a5SSteve French 			return -ECONNABORTED;
76538c8a9a5SSteve French 		}
76638c8a9a5SSteve French 	}
76738c8a9a5SSteve French 	return total_read;
76838c8a9a5SSteve French }
76938c8a9a5SSteve French 
77038c8a9a5SSteve French int
cifs_read_from_socket(struct TCP_Server_Info * server,char * buf,unsigned int to_read)77138c8a9a5SSteve French cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
77238c8a9a5SSteve French 		      unsigned int to_read)
77338c8a9a5SSteve French {
77438c8a9a5SSteve French 	struct msghdr smb_msg = {};
77538c8a9a5SSteve French 	struct kvec iov = {.iov_base = buf, .iov_len = to_read};
776da70c1d3SPierre Mariani 
77738c8a9a5SSteve French 	iov_iter_kvec(&smb_msg.msg_iter, ITER_DEST, &iov, 1, to_read);
77838c8a9a5SSteve French 
77938c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
78038c8a9a5SSteve French }
78138c8a9a5SSteve French 
78238c8a9a5SSteve French ssize_t
cifs_discard_from_socket(struct TCP_Server_Info * server,size_t to_read)78338c8a9a5SSteve French cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
78438c8a9a5SSteve French {
78538c8a9a5SSteve French 	struct msghdr smb_msg = {};
78638c8a9a5SSteve French 
78738c8a9a5SSteve French 	/*
78838c8a9a5SSteve French 	 *  iov_iter_discard already sets smb_msg.type and count and iov_offset
78938c8a9a5SSteve French 	 *  and cifs_readv_from_socket sets msg_control and msg_controllen
79038c8a9a5SSteve French 	 *  so little to initialize in struct msghdr
79138c8a9a5SSteve French 	 */
79238c8a9a5SSteve French 	iov_iter_discard(&smb_msg.msg_iter, ITER_DEST, to_read);
79338c8a9a5SSteve French 
79438c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
79538c8a9a5SSteve French }
79638c8a9a5SSteve French 
79738c8a9a5SSteve French int
cifs_read_page_from_socket(struct TCP_Server_Info * server,struct page * page,unsigned int page_offset,unsigned int to_read)79838c8a9a5SSteve French cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
79938c8a9a5SSteve French 	unsigned int page_offset, unsigned int to_read)
80038c8a9a5SSteve French {
80138c8a9a5SSteve French 	struct msghdr smb_msg = {};
80238c8a9a5SSteve French 	struct bio_vec bv;
80338c8a9a5SSteve French 
80438c8a9a5SSteve French 	bvec_set_page(&bv, page, to_read, page_offset);
80538c8a9a5SSteve French 	iov_iter_bvec(&smb_msg.msg_iter, ITER_DEST, &bv, 1, to_read);
80638c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
80738c8a9a5SSteve French }
80838c8a9a5SSteve French 
80938c8a9a5SSteve French int
cifs_read_iter_from_socket(struct TCP_Server_Info * server,struct iov_iter * iter,unsigned int to_read)81038c8a9a5SSteve French cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter *iter,
81138c8a9a5SSteve French 			   unsigned int to_read)
81238c8a9a5SSteve French {
81338c8a9a5SSteve French 	struct msghdr smb_msg = { .msg_iter = *iter };
81438c8a9a5SSteve French 	int ret;
81538c8a9a5SSteve French 
81638c8a9a5SSteve French 	iov_iter_truncate(&smb_msg.msg_iter, to_read);
81738c8a9a5SSteve French 	ret = cifs_readv_from_socket(server, &smb_msg);
81838c8a9a5SSteve French 	if (ret > 0)
81938c8a9a5SSteve French 		iov_iter_advance(iter, ret);
82038c8a9a5SSteve French 	return ret;
82138c8a9a5SSteve French }
82238c8a9a5SSteve French 
82338c8a9a5SSteve French static bool
is_smb_response(struct TCP_Server_Info * server,unsigned char type)82438c8a9a5SSteve French is_smb_response(struct TCP_Server_Info *server, unsigned char type)
82538c8a9a5SSteve French {
82638c8a9a5SSteve French 	/*
82738c8a9a5SSteve French 	 * The first byte big endian of the length field,
82838c8a9a5SSteve French 	 * is actually not part of the length but the type
82938c8a9a5SSteve French 	 * with the most common, zero, as regular data.
83038c8a9a5SSteve French 	 */
83138c8a9a5SSteve French 	switch (type) {
83238c8a9a5SSteve French 	case RFC1002_SESSION_MESSAGE:
83338c8a9a5SSteve French 		/* Regular SMB response */
83438c8a9a5SSteve French 		return true;
83538c8a9a5SSteve French 	case RFC1002_SESSION_KEEP_ALIVE:
83638c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 session keep alive\n");
83738c8a9a5SSteve French 		break;
83838c8a9a5SSteve French 	case RFC1002_POSITIVE_SESSION_RESPONSE:
83938c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 positive session response\n");
84038c8a9a5SSteve French 		break;
84138c8a9a5SSteve French 	case RFC1002_NEGATIVE_SESSION_RESPONSE:
84238c8a9a5SSteve French 		/*
84338c8a9a5SSteve French 		 * We get this from Windows 98 instead of an error on
84438c8a9a5SSteve French 		 * SMB negprot response.
84538c8a9a5SSteve French 		 */
84638c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 negative session response\n");
84738c8a9a5SSteve French 		/* give server a second to clean up */
84838c8a9a5SSteve French 		msleep(1000);
84938c8a9a5SSteve French 		/*
85038c8a9a5SSteve French 		 * Always try 445 first on reconnect since we get NACK
85138c8a9a5SSteve French 		 * on some if we ever connected to port 139 (the NACK
85238c8a9a5SSteve French 		 * is since we do not begin with RFC1001 session
85338c8a9a5SSteve French 		 * initialize frame).
85438c8a9a5SSteve French 		 */
85538c8a9a5SSteve French 		cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
85638c8a9a5SSteve French 		cifs_reconnect(server, true);
85738c8a9a5SSteve French 		break;
85838c8a9a5SSteve French 	default:
85938c8a9a5SSteve French 		cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
86038c8a9a5SSteve French 		cifs_reconnect(server, true);
86138c8a9a5SSteve French 	}
86238c8a9a5SSteve French 
86338c8a9a5SSteve French 	return false;
86438c8a9a5SSteve French }
86538c8a9a5SSteve French 
86638c8a9a5SSteve French void
dequeue_mid(struct mid_q_entry * mid,bool malformed)86738c8a9a5SSteve French dequeue_mid(struct mid_q_entry *mid, bool malformed)
86838c8a9a5SSteve French {
86938c8a9a5SSteve French #ifdef CONFIG_CIFS_STATS2
87038c8a9a5SSteve French 	mid->when_received = jiffies;
87138c8a9a5SSteve French #endif
87238c8a9a5SSteve French 	spin_lock(&mid->server->mid_lock);
87338c8a9a5SSteve French 	if (!malformed)
87438c8a9a5SSteve French 		mid->mid_state = MID_RESPONSE_RECEIVED;
87538c8a9a5SSteve French 	else
87638c8a9a5SSteve French 		mid->mid_state = MID_RESPONSE_MALFORMED;
87738c8a9a5SSteve French 	/*
87838c8a9a5SSteve French 	 * Trying to handle/dequeue a mid after the send_recv()
87938c8a9a5SSteve French 	 * function has finished processing it is a bug.
88038c8a9a5SSteve French 	 */
88138c8a9a5SSteve French 	if (mid->mid_flags & MID_DELETED) {
88238c8a9a5SSteve French 		spin_unlock(&mid->server->mid_lock);
88338c8a9a5SSteve French 		pr_warn_once("trying to dequeue a deleted mid\n");
88438c8a9a5SSteve French 	} else {
88538c8a9a5SSteve French 		list_del_init(&mid->qhead);
88638c8a9a5SSteve French 		mid->mid_flags |= MID_DELETED;
88738c8a9a5SSteve French 		spin_unlock(&mid->server->mid_lock);
88838c8a9a5SSteve French 	}
88938c8a9a5SSteve French }
89038c8a9a5SSteve French 
89138c8a9a5SSteve French static unsigned int
smb2_get_credits_from_hdr(char * buffer,struct TCP_Server_Info * server)89238c8a9a5SSteve French smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
89338c8a9a5SSteve French {
89438c8a9a5SSteve French 	struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
89538c8a9a5SSteve French 
89638c8a9a5SSteve French 	/*
89738c8a9a5SSteve French 	 * SMB1 does not use credits.
89838c8a9a5SSteve French 	 */
89938c8a9a5SSteve French 	if (is_smb1(server))
90038c8a9a5SSteve French 		return 0;
90138c8a9a5SSteve French 
90238c8a9a5SSteve French 	return le16_to_cpu(shdr->CreditRequest);
90338c8a9a5SSteve French }
90438c8a9a5SSteve French 
90538c8a9a5SSteve French static void
handle_mid(struct mid_q_entry * mid,struct TCP_Server_Info * server,char * buf,int malformed)90638c8a9a5SSteve French handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
90738c8a9a5SSteve French 	   char *buf, int malformed)
90838c8a9a5SSteve French {
90938c8a9a5SSteve French 	if (server->ops->check_trans2 &&
91038c8a9a5SSteve French 	    server->ops->check_trans2(mid, server, buf, malformed))
91138c8a9a5SSteve French 		return;
91238c8a9a5SSteve French 	mid->credits_received = smb2_get_credits_from_hdr(buf, server);
91338c8a9a5SSteve French 	mid->resp_buf = buf;
91438c8a9a5SSteve French 	mid->large_buf = server->large_buf;
91538c8a9a5SSteve French 	/* Was previous buf put in mpx struct for multi-rsp? */
91638c8a9a5SSteve French 	if (!mid->multiRsp) {
91738c8a9a5SSteve French 		/* smb buffer will be freed by user thread */
91838c8a9a5SSteve French 		if (server->large_buf)
91938c8a9a5SSteve French 			server->bigbuf = NULL;
92038c8a9a5SSteve French 		else
92138c8a9a5SSteve French 			server->smallbuf = NULL;
92238c8a9a5SSteve French 	}
92338c8a9a5SSteve French 	dequeue_mid(mid, malformed);
92438c8a9a5SSteve French }
92538c8a9a5SSteve French 
92638c8a9a5SSteve French int
cifs_enable_signing(struct TCP_Server_Info * server,bool mnt_sign_required)92738c8a9a5SSteve French cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
92838c8a9a5SSteve French {
92938c8a9a5SSteve French 	bool srv_sign_required = server->sec_mode & server->vals->signing_required;
93038c8a9a5SSteve French 	bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
93138c8a9a5SSteve French 	bool mnt_sign_enabled;
93238c8a9a5SSteve French 
93338c8a9a5SSteve French 	/*
93438c8a9a5SSteve French 	 * Is signing required by mnt options? If not then check
93538c8a9a5SSteve French 	 * global_secflags to see if it is there.
93638c8a9a5SSteve French 	 */
93738c8a9a5SSteve French 	if (!mnt_sign_required)
93838c8a9a5SSteve French 		mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
93938c8a9a5SSteve French 						CIFSSEC_MUST_SIGN);
94038c8a9a5SSteve French 
94138c8a9a5SSteve French 	/*
94238c8a9a5SSteve French 	 * If signing is required then it's automatically enabled too,
94338c8a9a5SSteve French 	 * otherwise, check to see if the secflags allow it.
94438c8a9a5SSteve French 	 */
94538c8a9a5SSteve French 	mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
94638c8a9a5SSteve French 				(global_secflags & CIFSSEC_MAY_SIGN);
94738c8a9a5SSteve French 
94838c8a9a5SSteve French 	/* If server requires signing, does client allow it? */
94938c8a9a5SSteve French 	if (srv_sign_required) {
95038c8a9a5SSteve French 		if (!mnt_sign_enabled) {
95138c8a9a5SSteve French 			cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n");
95238c8a9a5SSteve French 			return -EOPNOTSUPP;
95338c8a9a5SSteve French 		}
95438c8a9a5SSteve French 		server->sign = true;
95538c8a9a5SSteve French 	}
95638c8a9a5SSteve French 
95738c8a9a5SSteve French 	/* If client requires signing, does server allow it? */
95838c8a9a5SSteve French 	if (mnt_sign_required) {
95938c8a9a5SSteve French 		if (!srv_sign_enabled) {
96038c8a9a5SSteve French 			cifs_dbg(VFS, "Server does not support signing!\n");
96138c8a9a5SSteve French 			return -EOPNOTSUPP;
96238c8a9a5SSteve French 		}
96338c8a9a5SSteve French 		server->sign = true;
96438c8a9a5SSteve French 	}
96538c8a9a5SSteve French 
96638c8a9a5SSteve French 	if (cifs_rdma_enabled(server) && server->sign)
96738c8a9a5SSteve French 		cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n");
96838c8a9a5SSteve French 
96938c8a9a5SSteve French 	return 0;
97038c8a9a5SSteve French }
97138c8a9a5SSteve French 
972946ad1b8SPaulo Alcantara static noinline_for_stack void
clean_demultiplex_info(struct TCP_Server_Info * server)973946ad1b8SPaulo Alcantara clean_demultiplex_info(struct TCP_Server_Info *server)
97438c8a9a5SSteve French {
97538c8a9a5SSteve French 	int length;
97638c8a9a5SSteve French 
97738c8a9a5SSteve French 	/* take it off the list, if it's not already */
97838c8a9a5SSteve French 	spin_lock(&server->srv_lock);
97938c8a9a5SSteve French 	list_del_init(&server->tcp_ses_list);
98038c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
98138c8a9a5SSteve French 
98238c8a9a5SSteve French 	cancel_delayed_work_sync(&server->echo);
98338c8a9a5SSteve French 
98438c8a9a5SSteve French 	spin_lock(&server->srv_lock);
98538c8a9a5SSteve French 	server->tcpStatus = CifsExiting;
98638c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
98738c8a9a5SSteve French 	wake_up_all(&server->response_q);
98838c8a9a5SSteve French 
98938c8a9a5SSteve French 	/* check if we have blocked requests that need to free */
99038c8a9a5SSteve French 	spin_lock(&server->req_lock);
99138c8a9a5SSteve French 	if (server->credits <= 0)
99238c8a9a5SSteve French 		server->credits = 1;
99338c8a9a5SSteve French 	spin_unlock(&server->req_lock);
99438c8a9a5SSteve French 	/*
99538c8a9a5SSteve French 	 * Although there should not be any requests blocked on this queue it
99638c8a9a5SSteve French 	 * can not hurt to be paranoid and try to wake up requests that may
99738c8a9a5SSteve French 	 * haven been blocked when more than 50 at time were on the wire to the
99838c8a9a5SSteve French 	 * same server - they now will see the session is in exit state and get
99938c8a9a5SSteve French 	 * out of SendReceive.
100038c8a9a5SSteve French 	 */
100138c8a9a5SSteve French 	wake_up_all(&server->request_q);
100238c8a9a5SSteve French 	/* give those requests time to exit */
100338c8a9a5SSteve French 	msleep(125);
100438c8a9a5SSteve French 	if (cifs_rdma_enabled(server))
100538c8a9a5SSteve French 		smbd_destroy(server);
1006906807c7SEnzo Matsumiya 
100738c8a9a5SSteve French 	if (server->ssocket) {
100838c8a9a5SSteve French 		sock_release(server->ssocket);
100938c8a9a5SSteve French 		server->ssocket = NULL;
1010906807c7SEnzo Matsumiya 
1011906807c7SEnzo Matsumiya 		/* Release netns reference for the socket. */
1012906807c7SEnzo Matsumiya 		put_net(cifs_net_ns(server));
101338c8a9a5SSteve French 	}
101438c8a9a5SSteve French 
101538c8a9a5SSteve French 	if (!list_empty(&server->pending_mid_q)) {
101638c8a9a5SSteve French 		struct list_head dispose_list;
101738c8a9a5SSteve French 		struct mid_q_entry *mid_entry;
101838c8a9a5SSteve French 		struct list_head *tmp, *tmp2;
101938c8a9a5SSteve French 
102038c8a9a5SSteve French 		INIT_LIST_HEAD(&dispose_list);
102138c8a9a5SSteve French 		spin_lock(&server->mid_lock);
102238c8a9a5SSteve French 		list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
102338c8a9a5SSteve French 			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
102438c8a9a5SSteve French 			cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
102538c8a9a5SSteve French 			kref_get(&mid_entry->refcount);
102638c8a9a5SSteve French 			mid_entry->mid_state = MID_SHUTDOWN;
102738c8a9a5SSteve French 			list_move(&mid_entry->qhead, &dispose_list);
102838c8a9a5SSteve French 			mid_entry->mid_flags |= MID_DELETED;
102938c8a9a5SSteve French 		}
103038c8a9a5SSteve French 		spin_unlock(&server->mid_lock);
103138c8a9a5SSteve French 
103238c8a9a5SSteve French 		/* now walk dispose list and issue callbacks */
103338c8a9a5SSteve French 		list_for_each_safe(tmp, tmp2, &dispose_list) {
103438c8a9a5SSteve French 			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
103538c8a9a5SSteve French 			cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
103638c8a9a5SSteve French 			list_del_init(&mid_entry->qhead);
103738c8a9a5SSteve French 			mid_entry->callback(mid_entry);
103838c8a9a5SSteve French 			release_mid(mid_entry);
103938c8a9a5SSteve French 		}
104038c8a9a5SSteve French 		/* 1/8th of sec is more than enough time for them to exit */
104138c8a9a5SSteve French 		msleep(125);
104238c8a9a5SSteve French 	}
104338c8a9a5SSteve French 
104438c8a9a5SSteve French 	if (!list_empty(&server->pending_mid_q)) {
104538c8a9a5SSteve French 		/*
104638c8a9a5SSteve French 		 * mpx threads have not exited yet give them at least the smb
104738c8a9a5SSteve French 		 * send timeout time for long ops.
104838c8a9a5SSteve French 		 *
104938c8a9a5SSteve French 		 * Due to delays on oplock break requests, we need to wait at
105038c8a9a5SSteve French 		 * least 45 seconds before giving up on a request getting a
105138c8a9a5SSteve French 		 * response and going ahead and killing cifsd.
105238c8a9a5SSteve French 		 */
105338c8a9a5SSteve French 		cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
105438c8a9a5SSteve French 		msleep(46000);
105538c8a9a5SSteve French 		/*
105638c8a9a5SSteve French 		 * If threads still have not exited they are probably never
105738c8a9a5SSteve French 		 * coming home not much else we can do but free the memory.
105838c8a9a5SSteve French 		 */
105938c8a9a5SSteve French 	}
106038c8a9a5SSteve French 
1061906807c7SEnzo Matsumiya 	/* Release netns reference for this server. */
1062e8c71494SKuniyuki Iwashima 	put_net(cifs_net_ns(server));
106338c8a9a5SSteve French 	kfree(server->leaf_fullpath);
1064*1ea68070SPaulo Alcantara 	kfree(server->hostname);
106538c8a9a5SSteve French 	kfree(server);
106638c8a9a5SSteve French 
106738c8a9a5SSteve French 	length = atomic_dec_return(&tcpSesAllocCount);
106838c8a9a5SSteve French 	if (length > 0)
106938c8a9a5SSteve French 		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
107038c8a9a5SSteve French }
107138c8a9a5SSteve French 
standard_receive3(struct TCP_Server_Info * server,struct mid_q_entry * mid)107238c8a9a5SSteve French static int
107338c8a9a5SSteve French standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
107438c8a9a5SSteve French {
107538c8a9a5SSteve French 	int length;
107638c8a9a5SSteve French 	char *buf = server->smallbuf;
107738c8a9a5SSteve French 	unsigned int pdu_length = server->pdu_size;
107838c8a9a5SSteve French 
107938c8a9a5SSteve French 	/* make sure this will fit in a large buffer */
108038c8a9a5SSteve French 	if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
108138c8a9a5SSteve French 	    HEADER_PREAMBLE_SIZE(server)) {
108238c8a9a5SSteve French 		cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
108338c8a9a5SSteve French 		cifs_reconnect(server, true);
108438c8a9a5SSteve French 		return -ECONNABORTED;
108538c8a9a5SSteve French 	}
108638c8a9a5SSteve French 
108738c8a9a5SSteve French 	/* switch to large buffer if too big for a small one */
108838c8a9a5SSteve French 	if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
108938c8a9a5SSteve French 		server->large_buf = true;
109038c8a9a5SSteve French 		memcpy(server->bigbuf, buf, server->total_read);
109138c8a9a5SSteve French 		buf = server->bigbuf;
109238c8a9a5SSteve French 	}
109338c8a9a5SSteve French 
109438c8a9a5SSteve French 	/* now read the rest */
109538c8a9a5SSteve French 	length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
109638c8a9a5SSteve French 				       pdu_length - MID_HEADER_SIZE(server));
109738c8a9a5SSteve French 
109838c8a9a5SSteve French 	if (length < 0)
109938c8a9a5SSteve French 		return length;
110038c8a9a5SSteve French 	server->total_read += length;
110138c8a9a5SSteve French 
110238c8a9a5SSteve French 	dump_smb(buf, server->total_read);
110338c8a9a5SSteve French 
110438c8a9a5SSteve French 	return cifs_handle_standard(server, mid);
110538c8a9a5SSteve French }
110638c8a9a5SSteve French 
cifs_handle_standard(struct TCP_Server_Info * server,struct mid_q_entry * mid)110738c8a9a5SSteve French int
110838c8a9a5SSteve French cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
110938c8a9a5SSteve French {
111038c8a9a5SSteve French 	char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
111138c8a9a5SSteve French 	int rc;
111238c8a9a5SSteve French 
111338c8a9a5SSteve French 	/*
111438c8a9a5SSteve French 	 * We know that we received enough to get to the MID as we
111538c8a9a5SSteve French 	 * checked the pdu_length earlier. Now check to see
111638c8a9a5SSteve French 	 * if the rest of the header is OK.
111738c8a9a5SSteve French 	 *
111838c8a9a5SSteve French 	 * 48 bytes is enough to display the header and a little bit
111938c8a9a5SSteve French 	 * into the payload for debugging purposes.
112038c8a9a5SSteve French 	 */
112138c8a9a5SSteve French 	rc = server->ops->check_message(buf, server->total_read, server);
112238c8a9a5SSteve French 	if (rc)
112338c8a9a5SSteve French 		cifs_dump_mem("Bad SMB: ", buf,
112438c8a9a5SSteve French 			min_t(unsigned int, server->total_read, 48));
112538c8a9a5SSteve French 
112638c8a9a5SSteve French 	if (server->ops->is_session_expired &&
112738c8a9a5SSteve French 	    server->ops->is_session_expired(buf)) {
112838c8a9a5SSteve French 		cifs_reconnect(server, true);
112938c8a9a5SSteve French 		return -1;
113038c8a9a5SSteve French 	}
113138c8a9a5SSteve French 
113238c8a9a5SSteve French 	if (server->ops->is_status_pending &&
113338c8a9a5SSteve French 	    server->ops->is_status_pending(buf, server))
113438c8a9a5SSteve French 		return -1;
113538c8a9a5SSteve French 
113638c8a9a5SSteve French 	if (!mid)
113738c8a9a5SSteve French 		return rc;
113838c8a9a5SSteve French 
113938c8a9a5SSteve French 	handle_mid(mid, server, buf, rc);
114038c8a9a5SSteve French 	return 0;
114138c8a9a5SSteve French }
114238c8a9a5SSteve French 
smb2_add_credits_from_hdr(char * buffer,struct TCP_Server_Info * server)114338c8a9a5SSteve French static void
114438c8a9a5SSteve French smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
114538c8a9a5SSteve French {
114638c8a9a5SSteve French 	struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
114738c8a9a5SSteve French 	int scredits, in_flight;
114838c8a9a5SSteve French 
114938c8a9a5SSteve French 	/*
115038c8a9a5SSteve French 	 * SMB1 does not use credits.
115138c8a9a5SSteve French 	 */
115238c8a9a5SSteve French 	if (is_smb1(server))
115338c8a9a5SSteve French 		return;
115438c8a9a5SSteve French 
115538c8a9a5SSteve French 	if (shdr->CreditRequest) {
115638c8a9a5SSteve French 		spin_lock(&server->req_lock);
115738c8a9a5SSteve French 		server->credits += le16_to_cpu(shdr->CreditRequest);
115838c8a9a5SSteve French 		scredits = server->credits;
115938c8a9a5SSteve French 		in_flight = server->in_flight;
116038c8a9a5SSteve French 		spin_unlock(&server->req_lock);
116138c8a9a5SSteve French 		wake_up(&server->request_q);
116238c8a9a5SSteve French 
116338c8a9a5SSteve French 		trace_smb3_hdr_credits(server->CurrentMid,
116438c8a9a5SSteve French 				server->conn_id, server->hostname, scredits,
116538c8a9a5SSteve French 				le16_to_cpu(shdr->CreditRequest), in_flight);
116638c8a9a5SSteve French 		cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
116738c8a9a5SSteve French 				__func__, le16_to_cpu(shdr->CreditRequest),
116838c8a9a5SSteve French 				scredits);
116938c8a9a5SSteve French 	}
117038c8a9a5SSteve French }
117138c8a9a5SSteve French 
117238c8a9a5SSteve French 
cifs_demultiplex_thread(void * p)117338c8a9a5SSteve French static int
117438c8a9a5SSteve French cifs_demultiplex_thread(void *p)
117538c8a9a5SSteve French {
117638c8a9a5SSteve French 	int i, num_mids, length;
117738c8a9a5SSteve French 	struct TCP_Server_Info *server = p;
117838c8a9a5SSteve French 	unsigned int pdu_length;
117938c8a9a5SSteve French 	unsigned int next_offset;
118038c8a9a5SSteve French 	char *buf = NULL;
118138c8a9a5SSteve French 	struct task_struct *task_to_wake = NULL;
118238c8a9a5SSteve French 	struct mid_q_entry *mids[MAX_COMPOUND];
118338c8a9a5SSteve French 	char *bufs[MAX_COMPOUND];
118438c8a9a5SSteve French 	unsigned int noreclaim_flag, num_io_timeout = 0;
118569cba9d3SShyam Prasad N 	bool pending_reconnect = false;
118638c8a9a5SSteve French 
118738c8a9a5SSteve French 	noreclaim_flag = memalloc_noreclaim_save();
118838c8a9a5SSteve French 	cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
118938c8a9a5SSteve French 
119038c8a9a5SSteve French 	length = atomic_inc_return(&tcpSesAllocCount);
119138c8a9a5SSteve French 	if (length > 1)
119238c8a9a5SSteve French 		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
119338c8a9a5SSteve French 
119438c8a9a5SSteve French 	set_freezable();
119538c8a9a5SSteve French 	allow_kernel_signal(SIGKILL);
119638c8a9a5SSteve French 	while (server->tcpStatus != CifsExiting) {
119738c8a9a5SSteve French 		if (try_to_freeze())
119838c8a9a5SSteve French 			continue;
119938c8a9a5SSteve French 
120038c8a9a5SSteve French 		if (!allocate_buffers(server))
120138c8a9a5SSteve French 			continue;
120238c8a9a5SSteve French 
120338c8a9a5SSteve French 		server->large_buf = false;
120438c8a9a5SSteve French 		buf = server->smallbuf;
120538c8a9a5SSteve French 		pdu_length = 4; /* enough to get RFC1001 header */
120638c8a9a5SSteve French 
120738c8a9a5SSteve French 		length = cifs_read_from_socket(server, buf, pdu_length);
120838c8a9a5SSteve French 		if (length < 0)
120938c8a9a5SSteve French 			continue;
121038c8a9a5SSteve French 
121138c8a9a5SSteve French 		if (is_smb1(server))
121238c8a9a5SSteve French 			server->total_read = length;
121338c8a9a5SSteve French 		else
121438c8a9a5SSteve French 			server->total_read = 0;
121538c8a9a5SSteve French 
121638c8a9a5SSteve French 		/*
121738c8a9a5SSteve French 		 * The right amount was read from socket - 4 bytes,
121838c8a9a5SSteve French 		 * so we can now interpret the length field.
121938c8a9a5SSteve French 		 */
122038c8a9a5SSteve French 		pdu_length = get_rfc1002_length(buf);
122138c8a9a5SSteve French 
122238c8a9a5SSteve French 		cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
122338c8a9a5SSteve French 		if (!is_smb_response(server, buf[0]))
122438c8a9a5SSteve French 			continue;
122569cba9d3SShyam Prasad N 
122669cba9d3SShyam Prasad N 		pending_reconnect = false;
122738c8a9a5SSteve French next_pdu:
122838c8a9a5SSteve French 		server->pdu_size = pdu_length;
122938c8a9a5SSteve French 
123038c8a9a5SSteve French 		/* make sure we have enough to get to the MID */
123138c8a9a5SSteve French 		if (server->pdu_size < MID_HEADER_SIZE(server)) {
123238c8a9a5SSteve French 			cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
123338c8a9a5SSteve French 				 server->pdu_size);
123438c8a9a5SSteve French 			cifs_reconnect(server, true);
123538c8a9a5SSteve French 			continue;
123638c8a9a5SSteve French 		}
123738c8a9a5SSteve French 
123838c8a9a5SSteve French 		/* read down to the MID */
123938c8a9a5SSteve French 		length = cifs_read_from_socket(server,
124038c8a9a5SSteve French 			     buf + HEADER_PREAMBLE_SIZE(server),
124138c8a9a5SSteve French 			     MID_HEADER_SIZE(server));
124238c8a9a5SSteve French 		if (length < 0)
124338c8a9a5SSteve French 			continue;
124438c8a9a5SSteve French 		server->total_read += length;
124538c8a9a5SSteve French 
124638c8a9a5SSteve French 		if (server->ops->next_header) {
1247c0e98de9SPaulo Alcantara 			if (server->ops->next_header(server, buf, &next_offset)) {
1248c0e98de9SPaulo Alcantara 				cifs_dbg(VFS, "%s: malformed response (next_offset=%u)\n",
1249c0e98de9SPaulo Alcantara 					 __func__, next_offset);
1250c0e98de9SPaulo Alcantara 				cifs_reconnect(server, true);
1251c0e98de9SPaulo Alcantara 				continue;
1252c0e98de9SPaulo Alcantara 			}
125338c8a9a5SSteve French 			if (next_offset)
125438c8a9a5SSteve French 				server->pdu_size = next_offset;
125538c8a9a5SSteve French 		}
125638c8a9a5SSteve French 
125738c8a9a5SSteve French 		memset(mids, 0, sizeof(mids));
125838c8a9a5SSteve French 		memset(bufs, 0, sizeof(bufs));
125938c8a9a5SSteve French 		num_mids = 0;
126038c8a9a5SSteve French 
126138c8a9a5SSteve French 		if (server->ops->is_transform_hdr &&
126238c8a9a5SSteve French 		    server->ops->receive_transform &&
126338c8a9a5SSteve French 		    server->ops->is_transform_hdr(buf)) {
126438c8a9a5SSteve French 			length = server->ops->receive_transform(server,
126538c8a9a5SSteve French 								mids,
126638c8a9a5SSteve French 								bufs,
126738c8a9a5SSteve French 								&num_mids);
126838c8a9a5SSteve French 		} else {
126938c8a9a5SSteve French 			mids[0] = server->ops->find_mid(server, buf);
127038c8a9a5SSteve French 			bufs[0] = buf;
127138c8a9a5SSteve French 			num_mids = 1;
127238c8a9a5SSteve French 
127338c8a9a5SSteve French 			if (!mids[0] || !mids[0]->receive)
127438c8a9a5SSteve French 				length = standard_receive3(server, mids[0]);
127538c8a9a5SSteve French 			else
127638c8a9a5SSteve French 				length = mids[0]->receive(server, mids[0]);
127738c8a9a5SSteve French 		}
127838c8a9a5SSteve French 
127938c8a9a5SSteve French 		if (length < 0) {
128038c8a9a5SSteve French 			for (i = 0; i < num_mids; i++)
128138c8a9a5SSteve French 				if (mids[i])
128238c8a9a5SSteve French 					release_mid(mids[i]);
128338c8a9a5SSteve French 			continue;
128438c8a9a5SSteve French 		}
128538c8a9a5SSteve French 
128638c8a9a5SSteve French 		if (server->ops->is_status_io_timeout &&
128738c8a9a5SSteve French 		    server->ops->is_status_io_timeout(buf)) {
128838c8a9a5SSteve French 			num_io_timeout++;
128969cba9d3SShyam Prasad N 			if (num_io_timeout > MAX_STATUS_IO_TIMEOUT) {
129069cba9d3SShyam Prasad N 				cifs_server_dbg(VFS,
129169cba9d3SShyam Prasad N 						"Number of request timeouts exceeded %d. Reconnecting",
129269cba9d3SShyam Prasad N 						MAX_STATUS_IO_TIMEOUT);
129369cba9d3SShyam Prasad N 
129469cba9d3SShyam Prasad N 				pending_reconnect = true;
129538c8a9a5SSteve French 				num_io_timeout = 0;
129638c8a9a5SSteve French 			}
129738c8a9a5SSteve French 		}
129838c8a9a5SSteve French 
129938c8a9a5SSteve French 		server->lstrp = jiffies;
130038c8a9a5SSteve French 
130138c8a9a5SSteve French 		for (i = 0; i < num_mids; i++) {
130238c8a9a5SSteve French 			if (mids[i] != NULL) {
130338c8a9a5SSteve French 				mids[i]->resp_buf_size = server->pdu_size;
130438c8a9a5SSteve French 
1305c071b34fSShyam Prasad N 				if (bufs[i] != NULL) {
1306c071b34fSShyam Prasad N 					if (server->ops->is_network_name_deleted &&
130738c8a9a5SSteve French 					    server->ops->is_network_name_deleted(bufs[i],
1308c071b34fSShyam Prasad N 										 server)) {
1309c071b34fSShyam Prasad N 						cifs_server_dbg(FYI,
1310c071b34fSShyam Prasad N 								"Share deleted. Reconnect needed");
1311c071b34fSShyam Prasad N 					}
1312c071b34fSShyam Prasad N 				}
131338c8a9a5SSteve French 
131438c8a9a5SSteve French 				if (!mids[i]->multiRsp || mids[i]->multiEnd)
131538c8a9a5SSteve French 					mids[i]->callback(mids[i]);
131638c8a9a5SSteve French 
131738c8a9a5SSteve French 				release_mid(mids[i]);
131838c8a9a5SSteve French 			} else if (server->ops->is_oplock_break &&
131938c8a9a5SSteve French 				   server->ops->is_oplock_break(bufs[i],
132038c8a9a5SSteve French 								server)) {
132138c8a9a5SSteve French 				smb2_add_credits_from_hdr(bufs[i], server);
132238c8a9a5SSteve French 				cifs_dbg(FYI, "Received oplock break\n");
132338c8a9a5SSteve French 			} else {
132438c8a9a5SSteve French 				cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
132538c8a9a5SSteve French 						atomic_read(&mid_count));
132638c8a9a5SSteve French 				cifs_dump_mem("Received Data is: ", bufs[i],
132738c8a9a5SSteve French 					      HEADER_SIZE(server));
132838c8a9a5SSteve French 				smb2_add_credits_from_hdr(bufs[i], server);
132938c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2
133038c8a9a5SSteve French 				if (server->ops->dump_detail)
133138c8a9a5SSteve French 					server->ops->dump_detail(bufs[i],
133238c8a9a5SSteve French 								 server);
133338c8a9a5SSteve French 				cifs_dump_mids(server);
133438c8a9a5SSteve French #endif /* CIFS_DEBUG2 */
133538c8a9a5SSteve French 			}
133638c8a9a5SSteve French 		}
133738c8a9a5SSteve French 
133838c8a9a5SSteve French 		if (pdu_length > server->pdu_size) {
133938c8a9a5SSteve French 			if (!allocate_buffers(server))
134038c8a9a5SSteve French 				continue;
134138c8a9a5SSteve French 			pdu_length -= server->pdu_size;
134238c8a9a5SSteve French 			server->total_read = 0;
134338c8a9a5SSteve French 			server->large_buf = false;
134438c8a9a5SSteve French 			buf = server->smallbuf;
134538c8a9a5SSteve French 			goto next_pdu;
134638c8a9a5SSteve French 		}
134769cba9d3SShyam Prasad N 
134869cba9d3SShyam Prasad N 		/* do this reconnect at the very end after processing all MIDs */
134969cba9d3SShyam Prasad N 		if (pending_reconnect)
135069cba9d3SShyam Prasad N 			cifs_reconnect(server, true);
135169cba9d3SShyam Prasad N 
135238c8a9a5SSteve French 	} /* end while !EXITING */
135338c8a9a5SSteve French 
135438c8a9a5SSteve French 	/* buffer usually freed in free_mid - need to free it here on exit */
135538c8a9a5SSteve French 	cifs_buf_release(server->bigbuf);
135638c8a9a5SSteve French 	if (server->smallbuf) /* no sense logging a debug message if NULL */
135738c8a9a5SSteve French 		cifs_small_buf_release(server->smallbuf);
135838c8a9a5SSteve French 
135938c8a9a5SSteve French 	task_to_wake = xchg(&server->tsk, NULL);
136038c8a9a5SSteve French 	clean_demultiplex_info(server);
136138c8a9a5SSteve French 
136238c8a9a5SSteve French 	/* if server->tsk was NULL then wait for a signal before exiting */
136338c8a9a5SSteve French 	if (!task_to_wake) {
136438c8a9a5SSteve French 		set_current_state(TASK_INTERRUPTIBLE);
136538c8a9a5SSteve French 		while (!signal_pending(current)) {
136638c8a9a5SSteve French 			schedule();
136738c8a9a5SSteve French 			set_current_state(TASK_INTERRUPTIBLE);
136838c8a9a5SSteve French 		}
136938c8a9a5SSteve French 		set_current_state(TASK_RUNNING);
137038c8a9a5SSteve French 	}
137138c8a9a5SSteve French 
137238c8a9a5SSteve French 	memalloc_noreclaim_restore(noreclaim_flag);
137338c8a9a5SSteve French 	module_put_and_kthread_exit(0);
137438c8a9a5SSteve French }
137538c8a9a5SSteve French 
cifs_ipaddr_cmp(struct sockaddr * srcaddr,struct sockaddr * rhs)13762991b774SShyam Prasad N int
13772991b774SShyam Prasad N cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs)
13782991b774SShyam Prasad N {
13792991b774SShyam Prasad N 	struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
13802991b774SShyam Prasad N 	struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
13812991b774SShyam Prasad N 	struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
13822991b774SShyam Prasad N 	struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
13832991b774SShyam Prasad N 
13842991b774SShyam Prasad N 	switch (srcaddr->sa_family) {
13852991b774SShyam Prasad N 	case AF_UNSPEC:
13862991b774SShyam Prasad N 		switch (rhs->sa_family) {
13872991b774SShyam Prasad N 		case AF_UNSPEC:
13882991b774SShyam Prasad N 			return 0;
13892991b774SShyam Prasad N 		case AF_INET:
13902991b774SShyam Prasad N 		case AF_INET6:
13912991b774SShyam Prasad N 			return 1;
13922991b774SShyam Prasad N 		default:
13932991b774SShyam Prasad N 			return -1;
13942991b774SShyam Prasad N 		}
13952991b774SShyam Prasad N 	case AF_INET: {
13962991b774SShyam Prasad N 		switch (rhs->sa_family) {
13972991b774SShyam Prasad N 		case AF_UNSPEC:
13982991b774SShyam Prasad N 			return -1;
13992991b774SShyam Prasad N 		case AF_INET:
14002991b774SShyam Prasad N 			return memcmp(saddr4, vaddr4,
14012991b774SShyam Prasad N 				      sizeof(struct sockaddr_in));
14022991b774SShyam Prasad N 		case AF_INET6:
14032991b774SShyam Prasad N 			return 1;
14042991b774SShyam Prasad N 		default:
14052991b774SShyam Prasad N 			return -1;
14062991b774SShyam Prasad N 		}
14072991b774SShyam Prasad N 	}
14082991b774SShyam Prasad N 	case AF_INET6: {
14092991b774SShyam Prasad N 		switch (rhs->sa_family) {
14102991b774SShyam Prasad N 		case AF_UNSPEC:
14112991b774SShyam Prasad N 		case AF_INET:
14122991b774SShyam Prasad N 			return -1;
14132991b774SShyam Prasad N 		case AF_INET6:
14142991b774SShyam Prasad N 			return memcmp(saddr6,
14152991b774SShyam Prasad N 				      vaddr6,
14162991b774SShyam Prasad N 				      sizeof(struct sockaddr_in6));
14172991b774SShyam Prasad N 		default:
14182991b774SShyam Prasad N 			return -1;
14192991b774SShyam Prasad N 		}
14202991b774SShyam Prasad N 	}
14212991b774SShyam Prasad N 	default:
14222991b774SShyam Prasad N 		return -1; /* don't expect to be here */
14232991b774SShyam Prasad N 	}
14242991b774SShyam Prasad N }
14252991b774SShyam Prasad N 
142638c8a9a5SSteve French /*
142738c8a9a5SSteve French  * Returns true if srcaddr isn't specified and rhs isn't specified, or
142838c8a9a5SSteve French  * if srcaddr is specified and matches the IP address of the rhs argument
142938c8a9a5SSteve French  */
cifs_match_ipaddr(struct sockaddr * srcaddr,struct sockaddr * rhs)143038c8a9a5SSteve French bool
143138c8a9a5SSteve French cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
143238c8a9a5SSteve French {
143338c8a9a5SSteve French 	switch (srcaddr->sa_family) {
143438c8a9a5SSteve French 	case AF_UNSPEC:
143538c8a9a5SSteve French 		return (rhs->sa_family == AF_UNSPEC);
143638c8a9a5SSteve French 	case AF_INET: {
143738c8a9a5SSteve French 		struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
143838c8a9a5SSteve French 		struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1439da70c1d3SPierre Mariani 
144038c8a9a5SSteve French 		return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
144138c8a9a5SSteve French 	}
144238c8a9a5SSteve French 	case AF_INET6: {
144338c8a9a5SSteve French 		struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
144438c8a9a5SSteve French 		struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
1445da70c1d3SPierre Mariani 
144638c8a9a5SSteve French 		return (ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr)
144738c8a9a5SSteve French 			&& saddr6->sin6_scope_id == vaddr6->sin6_scope_id);
144838c8a9a5SSteve French 	}
144938c8a9a5SSteve French 	default:
145038c8a9a5SSteve French 		WARN_ON(1);
145138c8a9a5SSteve French 		return false; /* don't expect to be here */
145238c8a9a5SSteve French 	}
145338c8a9a5SSteve French }
145438c8a9a5SSteve French 
145538c8a9a5SSteve French /*
145638c8a9a5SSteve French  * If no port is specified in addr structure, we try to match with 445 port
145738c8a9a5SSteve French  * and if it fails - with 139 ports. It should be called only if address
145838c8a9a5SSteve French  * families of server and addr are equal.
145938c8a9a5SSteve French  */
match_port(struct TCP_Server_Info * server,struct sockaddr * addr)146038c8a9a5SSteve French static bool
146138c8a9a5SSteve French match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
146238c8a9a5SSteve French {
146338c8a9a5SSteve French 	__be16 port, *sport;
146438c8a9a5SSteve French 
146538c8a9a5SSteve French 	/* SMBDirect manages its own ports, don't match it here */
146638c8a9a5SSteve French 	if (server->rdma)
146738c8a9a5SSteve French 		return true;
146838c8a9a5SSteve French 
146938c8a9a5SSteve French 	switch (addr->sa_family) {
147038c8a9a5SSteve French 	case AF_INET:
147138c8a9a5SSteve French 		sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
147238c8a9a5SSteve French 		port = ((struct sockaddr_in *) addr)->sin_port;
147338c8a9a5SSteve French 		break;
147438c8a9a5SSteve French 	case AF_INET6:
147538c8a9a5SSteve French 		sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
147638c8a9a5SSteve French 		port = ((struct sockaddr_in6 *) addr)->sin6_port;
147738c8a9a5SSteve French 		break;
147838c8a9a5SSteve French 	default:
147938c8a9a5SSteve French 		WARN_ON(1);
148038c8a9a5SSteve French 		return false;
148138c8a9a5SSteve French 	}
148238c8a9a5SSteve French 
148338c8a9a5SSteve French 	if (!port) {
148438c8a9a5SSteve French 		port = htons(CIFS_PORT);
148538c8a9a5SSteve French 		if (port == *sport)
148638c8a9a5SSteve French 			return true;
148738c8a9a5SSteve French 
148838c8a9a5SSteve French 		port = htons(RFC1001_PORT);
148938c8a9a5SSteve French 	}
149038c8a9a5SSteve French 
149138c8a9a5SSteve French 	return port == *sport;
149238c8a9a5SSteve French }
match_server_address(struct TCP_Server_Info * server,struct sockaddr * addr)149338c8a9a5SSteve French 
149438c8a9a5SSteve French static bool match_server_address(struct TCP_Server_Info *server, struct sockaddr *addr)
149538c8a9a5SSteve French {
149638c8a9a5SSteve French 	if (!cifs_match_ipaddr(addr, (struct sockaddr *)&server->dstaddr))
149738c8a9a5SSteve French 		return false;
149838c8a9a5SSteve French 
149938c8a9a5SSteve French 	return true;
150038c8a9a5SSteve French }
150138c8a9a5SSteve French 
match_security(struct TCP_Server_Info * server,struct smb3_fs_context * ctx)150238c8a9a5SSteve French static bool
150338c8a9a5SSteve French match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
150438c8a9a5SSteve French {
150538c8a9a5SSteve French 	/*
150638c8a9a5SSteve French 	 * The select_sectype function should either return the ctx->sectype
150738c8a9a5SSteve French 	 * that was specified, or "Unspecified" if that sectype was not
150838c8a9a5SSteve French 	 * compatible with the given NEGOTIATE request.
150938c8a9a5SSteve French 	 */
151038c8a9a5SSteve French 	if (server->ops->select_sectype(server, ctx->sectype)
151138c8a9a5SSteve French 	     == Unspecified)
151238c8a9a5SSteve French 		return false;
151338c8a9a5SSteve French 
151438c8a9a5SSteve French 	/*
151538c8a9a5SSteve French 	 * Now check if signing mode is acceptable. No need to check
151638c8a9a5SSteve French 	 * global_secflags at this point since if MUST_SIGN is set then
151738c8a9a5SSteve French 	 * the server->sign had better be too.
151838c8a9a5SSteve French 	 */
151938c8a9a5SSteve French 	if (ctx->sign && !server->sign)
152038c8a9a5SSteve French 		return false;
152138c8a9a5SSteve French 
152238c8a9a5SSteve French 	return true;
152338c8a9a5SSteve French }
152438c8a9a5SSteve French 
match_server(struct TCP_Server_Info * server,struct smb3_fs_context * ctx,bool match_super)152538c8a9a5SSteve French /* this function must be called with srv_lock held */
15263ae872deSPaulo Alcantara static int match_server(struct TCP_Server_Info *server,
15273ae872deSPaulo Alcantara 			struct smb3_fs_context *ctx,
15283ae872deSPaulo Alcantara 			bool match_super)
152938c8a9a5SSteve French {
153038c8a9a5SSteve French 	struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
153138c8a9a5SSteve French 
153238c8a9a5SSteve French 	lockdep_assert_held(&server->srv_lock);
153338c8a9a5SSteve French 
153438c8a9a5SSteve French 	if (ctx->nosharesock)
153538c8a9a5SSteve French 		return 0;
153638c8a9a5SSteve French 
153738c8a9a5SSteve French 	/* this server does not share socket */
153838c8a9a5SSteve French 	if (server->nosharesock)
153938c8a9a5SSteve French 		return 0;
154038c8a9a5SSteve French 
154138c8a9a5SSteve French 	/* If multidialect negotiation see if existing sessions match one */
154238c8a9a5SSteve French 	if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
154338c8a9a5SSteve French 		if (server->vals->protocol_id < SMB30_PROT_ID)
154438c8a9a5SSteve French 			return 0;
154538c8a9a5SSteve French 	} else if (strcmp(ctx->vals->version_string,
154638c8a9a5SSteve French 		   SMBDEFAULT_VERSION_STRING) == 0) {
154738c8a9a5SSteve French 		if (server->vals->protocol_id < SMB21_PROT_ID)
154838c8a9a5SSteve French 			return 0;
154938c8a9a5SSteve French 	} else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
155038c8a9a5SSteve French 		return 0;
155138c8a9a5SSteve French 
155238c8a9a5SSteve French 	if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
155338c8a9a5SSteve French 		return 0;
155438c8a9a5SSteve French 
155538c8a9a5SSteve French 	if (!cifs_match_ipaddr((struct sockaddr *)&ctx->srcaddr,
155638c8a9a5SSteve French 			       (struct sockaddr *)&server->srcaddr))
155738c8a9a5SSteve French 		return 0;
155838c8a9a5SSteve French 	/*
15593ae872deSPaulo Alcantara 	 * When matching cifs.ko superblocks (@match_super == true), we can't
15603ae872deSPaulo Alcantara 	 * really match either @server->leaf_fullpath or @server->dstaddr
15613ae872deSPaulo Alcantara 	 * directly since this @server might belong to a completely different
15623ae872deSPaulo Alcantara 	 * server -- in case of domain-based DFS referrals or DFS links -- as
15633ae872deSPaulo Alcantara 	 * provided earlier by mount(2) through 'source' and 'ip' options.
15643ae872deSPaulo Alcantara 	 *
15653ae872deSPaulo Alcantara 	 * Otherwise, match the DFS referral in @server->leaf_fullpath or the
15663ae872deSPaulo Alcantara 	 * destination address in @server->dstaddr.
15673ae872deSPaulo Alcantara 	 *
15683ae872deSPaulo Alcantara 	 * When using 'nodfs' mount option, we avoid sharing it with DFS
15693ae872deSPaulo Alcantara 	 * connections as they might failover.
157038c8a9a5SSteve French 	 */
15713ae872deSPaulo Alcantara 	if (!match_super) {
157238c8a9a5SSteve French 		if (!ctx->nodfs) {
15733ae872deSPaulo Alcantara 			if (server->leaf_fullpath) {
157438c8a9a5SSteve French 				if (!ctx->leaf_fullpath ||
157538c8a9a5SSteve French 				    strcasecmp(server->leaf_fullpath,
157638c8a9a5SSteve French 					       ctx->leaf_fullpath))
157738c8a9a5SSteve French 					return 0;
157838c8a9a5SSteve French 			} else if (ctx->leaf_fullpath) {
157938c8a9a5SSteve French 				return 0;
158038c8a9a5SSteve French 			}
15813ae872deSPaulo Alcantara 		} else if (server->leaf_fullpath) {
158238c8a9a5SSteve French 			return 0;
158338c8a9a5SSteve French 		}
15843ae872deSPaulo Alcantara 	}
158538c8a9a5SSteve French 
158638c8a9a5SSteve French 	/*
158738c8a9a5SSteve French 	 * Match for a regular connection (address/hostname/port) which has no
158838c8a9a5SSteve French 	 * DFS referrals set.
158938c8a9a5SSteve French 	 */
15903ae872deSPaulo Alcantara 	if (!server->leaf_fullpath &&
159138c8a9a5SSteve French 	    (strcasecmp(server->hostname, ctx->server_hostname) ||
159238c8a9a5SSteve French 	     !match_server_address(server, addr) ||
159338c8a9a5SSteve French 	     !match_port(server, addr)))
159438c8a9a5SSteve French 		return 0;
159538c8a9a5SSteve French 
159638c8a9a5SSteve French 	if (!match_security(server, ctx))
159738c8a9a5SSteve French 		return 0;
159838c8a9a5SSteve French 
159938c8a9a5SSteve French 	if (server->echo_interval != ctx->echo_interval * HZ)
160038c8a9a5SSteve French 		return 0;
160138c8a9a5SSteve French 
160238c8a9a5SSteve French 	if (server->rdma != ctx->rdma)
160338c8a9a5SSteve French 		return 0;
160438c8a9a5SSteve French 
160538c8a9a5SSteve French 	if (server->ignore_signature != ctx->ignore_signature)
160638c8a9a5SSteve French 		return 0;
160738c8a9a5SSteve French 
160838c8a9a5SSteve French 	if (server->min_offload != ctx->min_offload)
160938c8a9a5SSteve French 		return 0;
161038c8a9a5SSteve French 
1611b4ca2942SShyam Prasad N 	if (server->retrans != ctx->retrans)
1612b4ca2942SShyam Prasad N 		return 0;
1613b4ca2942SShyam Prasad N 
161438c8a9a5SSteve French 	return 1;
161538c8a9a5SSteve French }
161638c8a9a5SSteve French 
cifs_find_tcp_session(struct smb3_fs_context * ctx)161738c8a9a5SSteve French struct TCP_Server_Info *
161838c8a9a5SSteve French cifs_find_tcp_session(struct smb3_fs_context *ctx)
161938c8a9a5SSteve French {
162038c8a9a5SSteve French 	struct TCP_Server_Info *server;
162138c8a9a5SSteve French 
162238c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
162338c8a9a5SSteve French 	list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
162438c8a9a5SSteve French 		spin_lock(&server->srv_lock);
162538c8a9a5SSteve French 		/*
162638c8a9a5SSteve French 		 * Skip ses channels since they're only handled in lower layers
162738c8a9a5SSteve French 		 * (e.g. cifs_send_recv).
162838c8a9a5SSteve French 		 */
1629b3773b19SSteve French 		if (SERVER_IS_CHAN(server) ||
16303ae872deSPaulo Alcantara 		    !match_server(server, ctx, false)) {
163138c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
163238c8a9a5SSteve French 			continue;
163338c8a9a5SSteve French 		}
163438c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
163538c8a9a5SSteve French 
163638c8a9a5SSteve French 		++server->srv_count;
163738c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
163838c8a9a5SSteve French 		cifs_dbg(FYI, "Existing tcp session with server found\n");
163938c8a9a5SSteve French 		return server;
164038c8a9a5SSteve French 	}
164138c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
164238c8a9a5SSteve French 	return NULL;
164338c8a9a5SSteve French }
164438c8a9a5SSteve French 
cifs_put_tcp_session(struct TCP_Server_Info * server,int from_reconnect)164538c8a9a5SSteve French void
164638c8a9a5SSteve French cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
164738c8a9a5SSteve French {
164838c8a9a5SSteve French 	struct task_struct *task;
164938c8a9a5SSteve French 
165038c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
165138c8a9a5SSteve French 	if (--server->srv_count > 0) {
165238c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
165338c8a9a5SSteve French 		return;
165438c8a9a5SSteve French 	}
165538c8a9a5SSteve French 
165638c8a9a5SSteve French 	/* srv_count can never go negative */
165738c8a9a5SSteve French 	WARN_ON(server->srv_count < 0);
165838c8a9a5SSteve French 
165938c8a9a5SSteve French 	list_del_init(&server->tcp_ses_list);
166038c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
166138c8a9a5SSteve French 
166238c8a9a5SSteve French 	cancel_delayed_work_sync(&server->echo);
166338c8a9a5SSteve French 
16643a0ef868SShyam Prasad N 	if (from_reconnect)
166538c8a9a5SSteve French 		/*
166638c8a9a5SSteve French 		 * Avoid deadlock here: reconnect work calls
166738c8a9a5SSteve French 		 * cifs_put_tcp_session() at its end. Need to be sure
166838c8a9a5SSteve French 		 * that reconnect work does nothing with server pointer after
166938c8a9a5SSteve French 		 * that step.
167038c8a9a5SSteve French 		 */
16713a0ef868SShyam Prasad N 		cancel_delayed_work(&server->reconnect);
16723a0ef868SShyam Prasad N 	else
16733a0ef868SShyam Prasad N 		cancel_delayed_work_sync(&server->reconnect);
167438c8a9a5SSteve French 
167507e86408SShyam Prasad N 	/* For secondary channels, we pick up ref-count on the primary server */
167607e86408SShyam Prasad N 	if (SERVER_IS_CHAN(server))
167707e86408SShyam Prasad N 		cifs_put_tcp_session(server->primary_server, from_reconnect);
167807e86408SShyam Prasad N 
167938c8a9a5SSteve French 	spin_lock(&server->srv_lock);
168038c8a9a5SSteve French 	server->tcpStatus = CifsExiting;
168138c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
168238c8a9a5SSteve French 
168338c8a9a5SSteve French 	cifs_crypto_secmech_release(server);
168438c8a9a5SSteve French 
168538c8a9a5SSteve French 	kfree_sensitive(server->session_key.response);
168638c8a9a5SSteve French 	server->session_key.response = NULL;
168738c8a9a5SSteve French 	server->session_key.len = 0;
168838c8a9a5SSteve French 
168938c8a9a5SSteve French 	task = xchg(&server->tsk, NULL);
169038c8a9a5SSteve French 	if (task)
169138c8a9a5SSteve French 		send_sig(SIGKILL, task, 1);
169238c8a9a5SSteve French }
169338c8a9a5SSteve French 
169438c8a9a5SSteve French struct TCP_Server_Info *
169538c8a9a5SSteve French cifs_get_tcp_session(struct smb3_fs_context *ctx,
cifs_get_tcp_session(struct smb3_fs_context * ctx,struct TCP_Server_Info * primary_server)169638c8a9a5SSteve French 		     struct TCP_Server_Info *primary_server)
169738c8a9a5SSteve French {
169838c8a9a5SSteve French 	struct TCP_Server_Info *tcp_ses = NULL;
169938c8a9a5SSteve French 	int rc;
170038c8a9a5SSteve French 
170138c8a9a5SSteve French 	cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
170238c8a9a5SSteve French 
170338c8a9a5SSteve French 	/* see if we already have a matching tcp_ses */
170438c8a9a5SSteve French 	tcp_ses = cifs_find_tcp_session(ctx);
170538c8a9a5SSteve French 	if (tcp_ses)
170638c8a9a5SSteve French 		return tcp_ses;
170738c8a9a5SSteve French 
170838c8a9a5SSteve French 	tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
170938c8a9a5SSteve French 	if (!tcp_ses) {
171038c8a9a5SSteve French 		rc = -ENOMEM;
171138c8a9a5SSteve French 		goto out_err;
171238c8a9a5SSteve French 	}
171338c8a9a5SSteve French 
171438c8a9a5SSteve French 	tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
171538c8a9a5SSteve French 	if (!tcp_ses->hostname) {
171638c8a9a5SSteve French 		rc = -ENOMEM;
171738c8a9a5SSteve French 		goto out_err;
171838c8a9a5SSteve French 	}
171938c8a9a5SSteve French 
172038c8a9a5SSteve French 	if (ctx->leaf_fullpath) {
172138c8a9a5SSteve French 		tcp_ses->leaf_fullpath = kstrdup(ctx->leaf_fullpath, GFP_KERNEL);
172238c8a9a5SSteve French 		if (!tcp_ses->leaf_fullpath) {
172338c8a9a5SSteve French 			rc = -ENOMEM;
172438c8a9a5SSteve French 			goto out_err;
172538c8a9a5SSteve French 		}
172638c8a9a5SSteve French 	}
172738c8a9a5SSteve French 
172838c8a9a5SSteve French 	if (ctx->nosharesock)
172938c8a9a5SSteve French 		tcp_ses->nosharesock = true;
173038c8a9a5SSteve French 
173138c8a9a5SSteve French 	tcp_ses->ops = ctx->ops;
173238c8a9a5SSteve French 	tcp_ses->vals = ctx->vals;
1733906807c7SEnzo Matsumiya 
1734906807c7SEnzo Matsumiya 	/* Grab netns reference for this server. */
173538c8a9a5SSteve French 	cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
173638c8a9a5SSteve French 
173738c8a9a5SSteve French 	tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
173838c8a9a5SSteve French 	tcp_ses->noblockcnt = ctx->rootfs;
173938c8a9a5SSteve French 	tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
174038c8a9a5SSteve French 	tcp_ses->noautotune = ctx->noautotune;
174138c8a9a5SSteve French 	tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
174238c8a9a5SSteve French 	tcp_ses->rdma = ctx->rdma;
174338c8a9a5SSteve French 	tcp_ses->in_flight = 0;
174438c8a9a5SSteve French 	tcp_ses->max_in_flight = 0;
174538c8a9a5SSteve French 	tcp_ses->credits = 1;
174638c8a9a5SSteve French 	if (primary_server) {
174738c8a9a5SSteve French 		spin_lock(&cifs_tcp_ses_lock);
174838c8a9a5SSteve French 		++primary_server->srv_count;
174938c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
175038c8a9a5SSteve French 		tcp_ses->primary_server = primary_server;
175138c8a9a5SSteve French 	}
175238c8a9a5SSteve French 	init_waitqueue_head(&tcp_ses->response_q);
175338c8a9a5SSteve French 	init_waitqueue_head(&tcp_ses->request_q);
175438c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
175538c8a9a5SSteve French 	mutex_init(&tcp_ses->_srv_mutex);
175638c8a9a5SSteve French 	memcpy(tcp_ses->workstation_RFC1001_name,
175738c8a9a5SSteve French 		ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
175838c8a9a5SSteve French 	memcpy(tcp_ses->server_RFC1001_name,
175938c8a9a5SSteve French 		ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
176038c8a9a5SSteve French 	tcp_ses->session_estab = false;
176138c8a9a5SSteve French 	tcp_ses->sequence_number = 0;
176209ee7a3bSSteve French 	tcp_ses->channel_sequence_num = 0; /* only tracked for primary channel */
176338c8a9a5SSteve French 	tcp_ses->reconnect_instance = 1;
176438c8a9a5SSteve French 	tcp_ses->lstrp = jiffies;
1765e8aee4f4SEnzo Matsumiya 	tcp_ses->compression.requested = ctx->compress;
176638c8a9a5SSteve French 	spin_lock_init(&tcp_ses->req_lock);
176738c8a9a5SSteve French 	spin_lock_init(&tcp_ses->srv_lock);
176838c8a9a5SSteve French 	spin_lock_init(&tcp_ses->mid_lock);
176938c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
177038c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
177138c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
177238c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
177338c8a9a5SSteve French 	mutex_init(&tcp_ses->reconnect_mutex);
177438c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
177538c8a9a5SSteve French 	mutex_init(&tcp_ses->refpath_lock);
177638c8a9a5SSteve French #endif
177738c8a9a5SSteve French 	memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
177838c8a9a5SSteve French 	       sizeof(tcp_ses->srcaddr));
177938c8a9a5SSteve French 	memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
178038c8a9a5SSteve French 		sizeof(tcp_ses->dstaddr));
178138c8a9a5SSteve French 	if (ctx->use_client_guid)
178238c8a9a5SSteve French 		memcpy(tcp_ses->client_guid, ctx->client_guid,
178338c8a9a5SSteve French 		       SMB2_CLIENT_GUID_SIZE);
178438c8a9a5SSteve French 	else
178538c8a9a5SSteve French 		generate_random_uuid(tcp_ses->client_guid);
178638c8a9a5SSteve French 	/*
178738c8a9a5SSteve French 	 * at this point we are the only ones with the pointer
178838c8a9a5SSteve French 	 * to the struct since the kernel thread not created yet
178938c8a9a5SSteve French 	 * no need to spinlock this init of tcpStatus or srv_count
179038c8a9a5SSteve French 	 */
179138c8a9a5SSteve French 	tcp_ses->tcpStatus = CifsNew;
179238c8a9a5SSteve French 	++tcp_ses->srv_count;
179338c8a9a5SSteve French 
179438c8a9a5SSteve French 	if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
179538c8a9a5SSteve French 		ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
179638c8a9a5SSteve French 		tcp_ses->echo_interval = ctx->echo_interval * HZ;
179738c8a9a5SSteve French 	else
179838c8a9a5SSteve French 		tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
179938c8a9a5SSteve French 	if (tcp_ses->rdma) {
180038c8a9a5SSteve French #ifndef CONFIG_CIFS_SMB_DIRECT
180138c8a9a5SSteve French 		cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
180238c8a9a5SSteve French 		rc = -ENOENT;
180338c8a9a5SSteve French 		goto out_err_crypto_release;
180438c8a9a5SSteve French #endif
180538c8a9a5SSteve French 		tcp_ses->smbd_conn = smbd_get_connection(
180638c8a9a5SSteve French 			tcp_ses, (struct sockaddr *)&ctx->dstaddr);
180738c8a9a5SSteve French 		if (tcp_ses->smbd_conn) {
180838c8a9a5SSteve French 			cifs_dbg(VFS, "RDMA transport established\n");
180938c8a9a5SSteve French 			rc = 0;
181038c8a9a5SSteve French 			goto smbd_connected;
181138c8a9a5SSteve French 		} else {
181238c8a9a5SSteve French 			rc = -ENOENT;
181338c8a9a5SSteve French 			goto out_err_crypto_release;
181438c8a9a5SSteve French 		}
181538c8a9a5SSteve French 	}
181638c8a9a5SSteve French 	rc = ip_connect(tcp_ses);
181738c8a9a5SSteve French 	if (rc < 0) {
181838c8a9a5SSteve French 		cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
181938c8a9a5SSteve French 		goto out_err_crypto_release;
182038c8a9a5SSteve French 	}
182138c8a9a5SSteve French smbd_connected:
182238c8a9a5SSteve French 	/*
182338c8a9a5SSteve French 	 * since we're in a cifs function already, we know that
182438c8a9a5SSteve French 	 * this will succeed. No need for try_module_get().
182538c8a9a5SSteve French 	 */
182638c8a9a5SSteve French 	__module_get(THIS_MODULE);
182738c8a9a5SSteve French 	tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
182838c8a9a5SSteve French 				  tcp_ses, "cifsd");
182938c8a9a5SSteve French 	if (IS_ERR(tcp_ses->tsk)) {
183038c8a9a5SSteve French 		rc = PTR_ERR(tcp_ses->tsk);
183138c8a9a5SSteve French 		cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
183238c8a9a5SSteve French 		module_put(THIS_MODULE);
183338c8a9a5SSteve French 		goto out_err_crypto_release;
183438c8a9a5SSteve French 	}
183538c8a9a5SSteve French 	tcp_ses->min_offload = ctx->min_offload;
1836b4ca2942SShyam Prasad N 	tcp_ses->retrans = ctx->retrans;
183738c8a9a5SSteve French 	/*
183838c8a9a5SSteve French 	 * at this point we are the only ones with the pointer
183938c8a9a5SSteve French 	 * to the struct since the kernel thread not created yet
184038c8a9a5SSteve French 	 * no need to spinlock this update of tcpStatus
184138c8a9a5SSteve French 	 */
184238c8a9a5SSteve French 	spin_lock(&tcp_ses->srv_lock);
184338c8a9a5SSteve French 	tcp_ses->tcpStatus = CifsNeedNegotiate;
184438c8a9a5SSteve French 	spin_unlock(&tcp_ses->srv_lock);
184538c8a9a5SSteve French 
184638c8a9a5SSteve French 	if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
184738c8a9a5SSteve French 		tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
184838c8a9a5SSteve French 	else
184938c8a9a5SSteve French 		tcp_ses->max_credits = ctx->max_credits;
185038c8a9a5SSteve French 
185138c8a9a5SSteve French 	tcp_ses->nr_targets = 1;
185238c8a9a5SSteve French 	tcp_ses->ignore_signature = ctx->ignore_signature;
185338c8a9a5SSteve French 	/* thread spawned, put it on the list */
185438c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
185538c8a9a5SSteve French 	list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
185638c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
185738c8a9a5SSteve French 
185838c8a9a5SSteve French 	/* queue echo request delayed work */
185938c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
186038c8a9a5SSteve French 
186138c8a9a5SSteve French 	return tcp_ses;
186238c8a9a5SSteve French 
186338c8a9a5SSteve French out_err_crypto_release:
186438c8a9a5SSteve French 	cifs_crypto_secmech_release(tcp_ses);
186538c8a9a5SSteve French 
1866906807c7SEnzo Matsumiya 	/* Release netns reference for this server. */
186738c8a9a5SSteve French 	put_net(cifs_net_ns(tcp_ses));
186838c8a9a5SSteve French 
186938c8a9a5SSteve French out_err:
187038c8a9a5SSteve French 	if (tcp_ses) {
1871b3773b19SSteve French 		if (SERVER_IS_CHAN(tcp_ses))
187238c8a9a5SSteve French 			cifs_put_tcp_session(tcp_ses->primary_server, false);
187338c8a9a5SSteve French 		kfree(tcp_ses->hostname);
187438c8a9a5SSteve French 		kfree(tcp_ses->leaf_fullpath);
1875906807c7SEnzo Matsumiya 		if (tcp_ses->ssocket) {
187638c8a9a5SSteve French 			sock_release(tcp_ses->ssocket);
1877906807c7SEnzo Matsumiya 			put_net(cifs_net_ns(tcp_ses));
1878906807c7SEnzo Matsumiya 		}
187938c8a9a5SSteve French 		kfree(tcp_ses);
188038c8a9a5SSteve French 	}
188138c8a9a5SSteve French 	return ERR_PTR(rc);
188238c8a9a5SSteve French }
188338c8a9a5SSteve French 
188438c8a9a5SSteve French /* this function must be called with ses_lock and chan_lock held */
188538c8a9a5SSteve French static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
match_session(struct cifs_ses * ses,struct smb3_fs_context * ctx)188638c8a9a5SSteve French {
188738c8a9a5SSteve French 	if (ctx->sectype != Unspecified &&
188838c8a9a5SSteve French 	    ctx->sectype != ses->sectype)
188938c8a9a5SSteve French 		return 0;
189038c8a9a5SSteve French 
1891645f332cSPaulo Alcantara 	if (ctx->dfs_root_ses != ses->dfs_root_ses)
1892645f332cSPaulo Alcantara 		return 0;
1893645f332cSPaulo Alcantara 
189438c8a9a5SSteve French 	/*
189538c8a9a5SSteve French 	 * If an existing session is limited to less channels than
189638c8a9a5SSteve French 	 * requested, it should not be reused
189738c8a9a5SSteve French 	 */
189838c8a9a5SSteve French 	if (ses->chan_max < ctx->max_channels)
189938c8a9a5SSteve French 		return 0;
190038c8a9a5SSteve French 
190138c8a9a5SSteve French 	switch (ses->sectype) {
190238c8a9a5SSteve French 	case Kerberos:
190338c8a9a5SSteve French 		if (!uid_eq(ctx->cred_uid, ses->cred_uid))
190438c8a9a5SSteve French 			return 0;
190538c8a9a5SSteve French 		break;
190638c8a9a5SSteve French 	default:
190738c8a9a5SSteve French 		/* NULL username means anonymous session */
190838c8a9a5SSteve French 		if (ses->user_name == NULL) {
190938c8a9a5SSteve French 			if (!ctx->nullauth)
191038c8a9a5SSteve French 				return 0;
191138c8a9a5SSteve French 			break;
191238c8a9a5SSteve French 		}
191338c8a9a5SSteve French 
191438c8a9a5SSteve French 		/* anything else takes username/password */
191538c8a9a5SSteve French 		if (strncmp(ses->user_name,
191638c8a9a5SSteve French 			    ctx->username ? ctx->username : "",
191738c8a9a5SSteve French 			    CIFS_MAX_USERNAME_LEN))
191838c8a9a5SSteve French 			return 0;
191938c8a9a5SSteve French 		if ((ctx->username && strlen(ctx->username) != 0) &&
1920185f065fSMeetakshi Setiya 		    ses->password != NULL) {
1921185f065fSMeetakshi Setiya 
1922185f065fSMeetakshi Setiya 			/* New mount can only share sessions with an existing mount if:
1923185f065fSMeetakshi Setiya 			 * 1. Both password and password2 match, or
1924185f065fSMeetakshi Setiya 			 * 2. password2 of the old mount matches password of the new mount
1925185f065fSMeetakshi Setiya 			 *    and password of the old mount matches password2 of the new
1926185f065fSMeetakshi Setiya 			 *	  mount
1927185f065fSMeetakshi Setiya 			 */
1928185f065fSMeetakshi Setiya 			if (ses->password2 != NULL && ctx->password2 != NULL) {
1929185f065fSMeetakshi Setiya 				if (!((strncmp(ses->password, ctx->password ?
1930185f065fSMeetakshi Setiya 					ctx->password : "", CIFS_MAX_PASSWORD_LEN) == 0 &&
1931185f065fSMeetakshi Setiya 					strncmp(ses->password2, ctx->password2,
1932185f065fSMeetakshi Setiya 					CIFS_MAX_PASSWORD_LEN) == 0) ||
1933185f065fSMeetakshi Setiya 					(strncmp(ses->password, ctx->password2,
1934185f065fSMeetakshi Setiya 					CIFS_MAX_PASSWORD_LEN) == 0 &&
1935185f065fSMeetakshi Setiya 					strncmp(ses->password2, ctx->password ?
1936185f065fSMeetakshi Setiya 					ctx->password : "", CIFS_MAX_PASSWORD_LEN) == 0)))
193738c8a9a5SSteve French 					return 0;
1938185f065fSMeetakshi Setiya 
1939185f065fSMeetakshi Setiya 			} else if ((ses->password2 == NULL && ctx->password2 != NULL) ||
1940185f065fSMeetakshi Setiya 				(ses->password2 != NULL && ctx->password2 == NULL)) {
1941185f065fSMeetakshi Setiya 				return 0;
1942185f065fSMeetakshi Setiya 
1943185f065fSMeetakshi Setiya 			} else {
1944185f065fSMeetakshi Setiya 				if (strncmp(ses->password, ctx->password ?
1945185f065fSMeetakshi Setiya 					ctx->password : "", CIFS_MAX_PASSWORD_LEN))
1946185f065fSMeetakshi Setiya 					return 0;
1947185f065fSMeetakshi Setiya 			}
1948185f065fSMeetakshi Setiya 		}
194938c8a9a5SSteve French 	}
1950a43f95fdSWinston Wen 
1951a43f95fdSWinston Wen 	if (strcmp(ctx->local_nls->charset, ses->local_nls->charset))
1952a43f95fdSWinston Wen 		return 0;
1953a43f95fdSWinston Wen 
195438c8a9a5SSteve French 	return 1;
195538c8a9a5SSteve French }
195638c8a9a5SSteve French 
195738c8a9a5SSteve French /**
195838c8a9a5SSteve French  * cifs_setup_ipc - helper to setup the IPC tcon for the session
195938c8a9a5SSteve French  * @ses: smb session to issue the request on
196038c8a9a5SSteve French  * @ctx: the superblock configuration context to use for building the
196138c8a9a5SSteve French  *       new tree connection for the IPC (interprocess communication RPC)
196238c8a9a5SSteve French  *
196338c8a9a5SSteve French  * A new IPC connection is made and stored in the session
196438c8a9a5SSteve French  * tcon_ipc. The IPC tcon has the same lifetime as the session.
196538c8a9a5SSteve French  */
196638c8a9a5SSteve French static int
196738c8a9a5SSteve French cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
cifs_setup_ipc(struct cifs_ses * ses,struct smb3_fs_context * ctx)196838c8a9a5SSteve French {
196938c8a9a5SSteve French 	int rc = 0, xid;
197038c8a9a5SSteve French 	struct cifs_tcon *tcon;
197138c8a9a5SSteve French 	char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
197238c8a9a5SSteve French 	bool seal = false;
197338c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
197438c8a9a5SSteve French 
197538c8a9a5SSteve French 	/*
197638c8a9a5SSteve French 	 * If the mount request that resulted in the creation of the
197738c8a9a5SSteve French 	 * session requires encryption, force IPC to be encrypted too.
197838c8a9a5SSteve French 	 */
197938c8a9a5SSteve French 	if (ctx->seal) {
198038c8a9a5SSteve French 		if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
198138c8a9a5SSteve French 			seal = true;
198238c8a9a5SSteve French 		else {
198338c8a9a5SSteve French 			cifs_server_dbg(VFS,
198438c8a9a5SSteve French 				 "IPC: server doesn't support encryption\n");
198538c8a9a5SSteve French 			return -EOPNOTSUPP;
198638c8a9a5SSteve French 		}
198738c8a9a5SSteve French 	}
198838c8a9a5SSteve French 
19892da338ffSSteve French 	/* no need to setup directory caching on IPC share, so pass in false */
199007e76ea1SDavid Howells 	tcon = tcon_info_alloc(false, netfs_trace_tcon_ref_new_ipc);
199138c8a9a5SSteve French 	if (tcon == NULL)
199238c8a9a5SSteve French 		return -ENOMEM;
199338c8a9a5SSteve French 
199438c8a9a5SSteve French 	spin_lock(&server->srv_lock);
199538c8a9a5SSteve French 	scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
199638c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
199738c8a9a5SSteve French 
199838c8a9a5SSteve French 	xid = get_xid();
199938c8a9a5SSteve French 	tcon->ses = ses;
200038c8a9a5SSteve French 	tcon->ipc = true;
200138c8a9a5SSteve French 	tcon->seal = seal;
200238c8a9a5SSteve French 	rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
200338c8a9a5SSteve French 	free_xid(xid);
200438c8a9a5SSteve French 
200538c8a9a5SSteve French 	if (rc) {
200638c8a9a5SSteve French 		cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
200707e76ea1SDavid Howells 		tconInfoFree(tcon, netfs_trace_tcon_ref_free_ipc_fail);
200838c8a9a5SSteve French 		goto out;
200938c8a9a5SSteve French 	}
201038c8a9a5SSteve French 
201138c8a9a5SSteve French 	cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid);
201238c8a9a5SSteve French 
201338c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
201438c8a9a5SSteve French 	tcon->status = TID_GOOD;
201538c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
201638c8a9a5SSteve French 	ses->tcon_ipc = tcon;
201738c8a9a5SSteve French out:
201838c8a9a5SSteve French 	return rc;
201938c8a9a5SSteve French }
202038c8a9a5SSteve French 
202138c8a9a5SSteve French static struct cifs_ses *
202238c8a9a5SSteve French cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
cifs_find_smb_ses(struct TCP_Server_Info * server,struct smb3_fs_context * ctx)202338c8a9a5SSteve French {
202438c8a9a5SSteve French 	struct cifs_ses *ses, *ret = NULL;
202538c8a9a5SSteve French 
202638c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
202738c8a9a5SSteve French 	list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
202838c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
202938c8a9a5SSteve French 		if (ses->ses_status == SES_EXITING) {
203038c8a9a5SSteve French 			spin_unlock(&ses->ses_lock);
203138c8a9a5SSteve French 			continue;
203238c8a9a5SSteve French 		}
203338c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
203438c8a9a5SSteve French 		if (match_session(ses, ctx)) {
203538c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
203638c8a9a5SSteve French 			spin_unlock(&ses->ses_lock);
203738c8a9a5SSteve French 			ret = ses;
203838c8a9a5SSteve French 			break;
203938c8a9a5SSteve French 		}
204038c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
204138c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
204238c8a9a5SSteve French 	}
204338c8a9a5SSteve French 	if (ret)
204438c8a9a5SSteve French 		cifs_smb_ses_inc_refcount(ret);
204538c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
204638c8a9a5SSteve French 	return ret;
204738c8a9a5SSteve French }
204838c8a9a5SSteve French 
204938c8a9a5SSteve French void __cifs_put_smb_ses(struct cifs_ses *ses)
__cifs_put_smb_ses(struct cifs_ses * ses)205038c8a9a5SSteve French {
205138c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
20526202996aSPaulo Alcantara 	struct cifs_tcon *tcon;
2053fe1bad73SPaulo Alcantara 	unsigned int xid;
2054fe1bad73SPaulo Alcantara 	size_t i;
20556202996aSPaulo Alcantara 	bool do_logoff;
2056fe1bad73SPaulo Alcantara 	int rc;
205738c8a9a5SSteve French 
205838c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
20596202996aSPaulo Alcantara 	spin_lock(&ses->ses_lock);
20606202996aSPaulo Alcantara 	cifs_dbg(FYI, "%s: id=0x%llx ses_count=%d ses_status=%u ipc=%s\n",
20616202996aSPaulo Alcantara 		 __func__, ses->Suid, ses->ses_count, ses->ses_status,
20626202996aSPaulo Alcantara 		 ses->tcon_ipc ? ses->tcon_ipc->tree_name : "none");
20636202996aSPaulo Alcantara 	if (ses->ses_status == SES_EXITING || --ses->ses_count > 0) {
20646202996aSPaulo Alcantara 		spin_unlock(&ses->ses_lock);
206538c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
206638c8a9a5SSteve French 		return;
206738c8a9a5SSteve French 	}
206838c8a9a5SSteve French 	/* ses_count can never go negative */
206938c8a9a5SSteve French 	WARN_ON(ses->ses_count < 0);
207038c8a9a5SSteve French 
20716202996aSPaulo Alcantara 	spin_lock(&ses->chan_lock);
20726202996aSPaulo Alcantara 	cifs_chan_clear_need_reconnect(ses, server);
20736202996aSPaulo Alcantara 	spin_unlock(&ses->chan_lock);
20746202996aSPaulo Alcantara 
20756202996aSPaulo Alcantara 	do_logoff = ses->ses_status == SES_GOOD && server->ops->logoff;
20766202996aSPaulo Alcantara 	ses->ses_status = SES_EXITING;
20776202996aSPaulo Alcantara 	tcon = ses->tcon_ipc;
20786202996aSPaulo Alcantara 	ses->tcon_ipc = NULL;
207938c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
20806202996aSPaulo Alcantara 	spin_unlock(&cifs_tcp_ses_lock);
20816202996aSPaulo Alcantara 
20826202996aSPaulo Alcantara 	/*
20836202996aSPaulo Alcantara 	 * On session close, the IPC is closed and the server must release all
20846202996aSPaulo Alcantara 	 * tcons of the session.  No need to send a tree disconnect here.
20856202996aSPaulo Alcantara 	 *
20866202996aSPaulo Alcantara 	 * Besides, it will make the server to not close durable and resilient
20876202996aSPaulo Alcantara 	 * files on session close, as specified in MS-SMB2 3.3.5.6 Receiving an
20886202996aSPaulo Alcantara 	 * SMB2 LOGOFF Request.
20896202996aSPaulo Alcantara 	 */
209007e76ea1SDavid Howells 	tconInfoFree(tcon, netfs_trace_tcon_ref_free_ipc);
20916202996aSPaulo Alcantara 	if (do_logoff) {
209238c8a9a5SSteve French 		xid = get_xid();
209338c8a9a5SSteve French 		rc = server->ops->logoff(xid, ses);
209438c8a9a5SSteve French 		if (rc)
209538c8a9a5SSteve French 			cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
209638c8a9a5SSteve French 				__func__, rc);
209738c8a9a5SSteve French 		_free_xid(xid);
209838c8a9a5SSteve French 	}
209938c8a9a5SSteve French 
210038c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
210138c8a9a5SSteve French 	list_del_init(&ses->smb_ses_list);
210238c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
210338c8a9a5SSteve French 
210438c8a9a5SSteve French 	/* close any extra channels */
2105fe1bad73SPaulo Alcantara 	for (i = 1; i < ses->chan_count; i++) {
210638c8a9a5SSteve French 		if (ses->chans[i].iface) {
210738c8a9a5SSteve French 			kref_put(&ses->chans[i].iface->refcount, release_iface);
210838c8a9a5SSteve French 			ses->chans[i].iface = NULL;
210938c8a9a5SSteve French 		}
211038c8a9a5SSteve French 		cifs_put_tcp_session(ses->chans[i].server, 0);
211138c8a9a5SSteve French 		ses->chans[i].server = NULL;
211238c8a9a5SSteve French 	}
211338c8a9a5SSteve French 
21144875ef3eSShyam Prasad N 	/* we now account for primary channel in iface->refcount */
21154875ef3eSShyam Prasad N 	if (ses->chans[0].iface) {
21164875ef3eSShyam Prasad N 		kref_put(&ses->chans[0].iface->refcount, release_iface);
21174875ef3eSShyam Prasad N 		ses->chans[0].server = NULL;
21184875ef3eSShyam Prasad N 	}
21194875ef3eSShyam Prasad N 
212038c8a9a5SSteve French 	sesInfoFree(ses);
212138c8a9a5SSteve French 	cifs_put_tcp_session(server, 0);
212238c8a9a5SSteve French }
212338c8a9a5SSteve French 
212438c8a9a5SSteve French #ifdef CONFIG_KEYS
212538c8a9a5SSteve French 
212638c8a9a5SSteve French /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
212738c8a9a5SSteve French #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
212838c8a9a5SSteve French 
212938c8a9a5SSteve French /* Populate username and pw fields from keyring if possible */
213038c8a9a5SSteve French static int
213138c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
cifs_set_cifscreds(struct smb3_fs_context * ctx,struct cifs_ses * ses)213238c8a9a5SSteve French {
213338c8a9a5SSteve French 	int rc = 0;
213438c8a9a5SSteve French 	int is_domain = 0;
213538c8a9a5SSteve French 	const char *delim, *payload;
213638c8a9a5SSteve French 	char *desc;
213738c8a9a5SSteve French 	ssize_t len;
213838c8a9a5SSteve French 	struct key *key;
213938c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
214038c8a9a5SSteve French 	struct sockaddr_in *sa;
214138c8a9a5SSteve French 	struct sockaddr_in6 *sa6;
214238c8a9a5SSteve French 	const struct user_key_payload *upayload;
214338c8a9a5SSteve French 
214438c8a9a5SSteve French 	desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
214538c8a9a5SSteve French 	if (!desc)
214638c8a9a5SSteve French 		return -ENOMEM;
214738c8a9a5SSteve French 
214838c8a9a5SSteve French 	/* try to find an address key first */
214938c8a9a5SSteve French 	switch (server->dstaddr.ss_family) {
215038c8a9a5SSteve French 	case AF_INET:
215138c8a9a5SSteve French 		sa = (struct sockaddr_in *)&server->dstaddr;
215238c8a9a5SSteve French 		sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
215338c8a9a5SSteve French 		break;
215438c8a9a5SSteve French 	case AF_INET6:
215538c8a9a5SSteve French 		sa6 = (struct sockaddr_in6 *)&server->dstaddr;
215638c8a9a5SSteve French 		sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
215738c8a9a5SSteve French 		break;
215838c8a9a5SSteve French 	default:
215938c8a9a5SSteve French 		cifs_dbg(FYI, "Bad ss_family (%hu)\n",
216038c8a9a5SSteve French 			 server->dstaddr.ss_family);
216138c8a9a5SSteve French 		rc = -EINVAL;
216238c8a9a5SSteve French 		goto out_err;
216338c8a9a5SSteve French 	}
216438c8a9a5SSteve French 
216538c8a9a5SSteve French 	cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
216638c8a9a5SSteve French 	key = request_key(&key_type_logon, desc, "");
216738c8a9a5SSteve French 	if (IS_ERR(key)) {
216838c8a9a5SSteve French 		if (!ses->domainName) {
216938c8a9a5SSteve French 			cifs_dbg(FYI, "domainName is NULL\n");
217038c8a9a5SSteve French 			rc = PTR_ERR(key);
217138c8a9a5SSteve French 			goto out_err;
217238c8a9a5SSteve French 		}
217338c8a9a5SSteve French 
217438c8a9a5SSteve French 		/* didn't work, try to find a domain key */
217538c8a9a5SSteve French 		sprintf(desc, "cifs:d:%s", ses->domainName);
217638c8a9a5SSteve French 		cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
217738c8a9a5SSteve French 		key = request_key(&key_type_logon, desc, "");
217838c8a9a5SSteve French 		if (IS_ERR(key)) {
217938c8a9a5SSteve French 			rc = PTR_ERR(key);
218038c8a9a5SSteve French 			goto out_err;
218138c8a9a5SSteve French 		}
218238c8a9a5SSteve French 		is_domain = 1;
218338c8a9a5SSteve French 	}
218438c8a9a5SSteve French 
218538c8a9a5SSteve French 	down_read(&key->sem);
218638c8a9a5SSteve French 	upayload = user_key_payload_locked(key);
218738c8a9a5SSteve French 	if (IS_ERR_OR_NULL(upayload)) {
218838c8a9a5SSteve French 		rc = upayload ? PTR_ERR(upayload) : -EINVAL;
218938c8a9a5SSteve French 		goto out_key_put;
219038c8a9a5SSteve French 	}
219138c8a9a5SSteve French 
219238c8a9a5SSteve French 	/* find first : in payload */
219338c8a9a5SSteve French 	payload = upayload->data;
219438c8a9a5SSteve French 	delim = strnchr(payload, upayload->datalen, ':');
219538c8a9a5SSteve French 	cifs_dbg(FYI, "payload=%s\n", payload);
219638c8a9a5SSteve French 	if (!delim) {
219738c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
219838c8a9a5SSteve French 			 upayload->datalen);
219938c8a9a5SSteve French 		rc = -EINVAL;
220038c8a9a5SSteve French 		goto out_key_put;
220138c8a9a5SSteve French 	}
220238c8a9a5SSteve French 
220338c8a9a5SSteve French 	len = delim - payload;
220438c8a9a5SSteve French 	if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
220538c8a9a5SSteve French 		cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
220638c8a9a5SSteve French 			 len);
220738c8a9a5SSteve French 		rc = -EINVAL;
220838c8a9a5SSteve French 		goto out_key_put;
220938c8a9a5SSteve French 	}
221038c8a9a5SSteve French 
221138c8a9a5SSteve French 	ctx->username = kstrndup(payload, len, GFP_KERNEL);
221238c8a9a5SSteve French 	if (!ctx->username) {
221338c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
221438c8a9a5SSteve French 			 len);
221538c8a9a5SSteve French 		rc = -ENOMEM;
221638c8a9a5SSteve French 		goto out_key_put;
221738c8a9a5SSteve French 	}
221838c8a9a5SSteve French 	cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
221938c8a9a5SSteve French 
222038c8a9a5SSteve French 	len = key->datalen - (len + 1);
222138c8a9a5SSteve French 	if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
222238c8a9a5SSteve French 		cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
222338c8a9a5SSteve French 		rc = -EINVAL;
222438c8a9a5SSteve French 		kfree(ctx->username);
222538c8a9a5SSteve French 		ctx->username = NULL;
222638c8a9a5SSteve French 		goto out_key_put;
222738c8a9a5SSteve French 	}
222838c8a9a5SSteve French 
222938c8a9a5SSteve French 	++delim;
223008bedfbcSSteve French 	/* BB consider adding support for password2 (Key Rotation) for multiuser in future */
223138c8a9a5SSteve French 	ctx->password = kstrndup(delim, len, GFP_KERNEL);
223238c8a9a5SSteve French 	if (!ctx->password) {
223338c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
223438c8a9a5SSteve French 			 len);
223538c8a9a5SSteve French 		rc = -ENOMEM;
223638c8a9a5SSteve French 		kfree(ctx->username);
223738c8a9a5SSteve French 		ctx->username = NULL;
223838c8a9a5SSteve French 		goto out_key_put;
223938c8a9a5SSteve French 	}
224038c8a9a5SSteve French 
224138c8a9a5SSteve French 	/*
224238c8a9a5SSteve French 	 * If we have a domain key then we must set the domainName in the
224338c8a9a5SSteve French 	 * for the request.
224438c8a9a5SSteve French 	 */
224538c8a9a5SSteve French 	if (is_domain && ses->domainName) {
224638c8a9a5SSteve French 		ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
224738c8a9a5SSteve French 		if (!ctx->domainname) {
224838c8a9a5SSteve French 			cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
224938c8a9a5SSteve French 				 len);
225038c8a9a5SSteve French 			rc = -ENOMEM;
225138c8a9a5SSteve French 			kfree(ctx->username);
225238c8a9a5SSteve French 			ctx->username = NULL;
225338c8a9a5SSteve French 			kfree_sensitive(ctx->password);
225408bedfbcSSteve French 			/* no need to free ctx->password2 since not allocated in this path */
225538c8a9a5SSteve French 			ctx->password = NULL;
225638c8a9a5SSteve French 			goto out_key_put;
225738c8a9a5SSteve French 		}
225838c8a9a5SSteve French 	}
225938c8a9a5SSteve French 
226038c8a9a5SSteve French 	strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name));
226138c8a9a5SSteve French 
226238c8a9a5SSteve French out_key_put:
226338c8a9a5SSteve French 	up_read(&key->sem);
226438c8a9a5SSteve French 	key_put(key);
226538c8a9a5SSteve French out_err:
226638c8a9a5SSteve French 	kfree(desc);
226738c8a9a5SSteve French 	cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
226838c8a9a5SSteve French 	return rc;
226938c8a9a5SSteve French }
227038c8a9a5SSteve French #else /* ! CONFIG_KEYS */
227138c8a9a5SSteve French static inline int
227238c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
cifs_set_cifscreds(struct smb3_fs_context * ctx,struct cifs_ses * ses)227338c8a9a5SSteve French 		   struct cifs_ses *ses __attribute__((unused)))
227438c8a9a5SSteve French {
227538c8a9a5SSteve French 	return -ENOSYS;
227638c8a9a5SSteve French }
227738c8a9a5SSteve French #endif /* CONFIG_KEYS */
227838c8a9a5SSteve French 
227938c8a9a5SSteve French /**
228038c8a9a5SSteve French  * cifs_get_smb_ses - get a session matching @ctx data from @server
228138c8a9a5SSteve French  * @server: server to setup the session to
228238c8a9a5SSteve French  * @ctx: superblock configuration context to use to setup the session
228338c8a9a5SSteve French  *
228438c8a9a5SSteve French  * This function assumes it is being called from cifs_mount() where we
228538c8a9a5SSteve French  * already got a server reference (server refcount +1). See
228638c8a9a5SSteve French  * cifs_get_tcon() for refcount explanations.
228738c8a9a5SSteve French  */
228838c8a9a5SSteve French struct cifs_ses *
228938c8a9a5SSteve French cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
cifs_get_smb_ses(struct TCP_Server_Info * server,struct smb3_fs_context * ctx)229038c8a9a5SSteve French {
229138c8a9a5SSteve French 	int rc = 0;
2292185f065fSMeetakshi Setiya 	int retries = 0;
229338c8a9a5SSteve French 	unsigned int xid;
229438c8a9a5SSteve French 	struct cifs_ses *ses;
229538c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
229638c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
229738c8a9a5SSteve French 
229838c8a9a5SSteve French 	xid = get_xid();
229938c8a9a5SSteve French 
230038c8a9a5SSteve French 	ses = cifs_find_smb_ses(server, ctx);
230138c8a9a5SSteve French 	if (ses) {
230238c8a9a5SSteve French 		cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
230338c8a9a5SSteve French 			 ses->ses_status);
230438c8a9a5SSteve French 
230538c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
230638c8a9a5SSteve French 		if (cifs_chan_needs_reconnect(ses, server)) {
230738c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
230838c8a9a5SSteve French 			cifs_dbg(FYI, "Session needs reconnect\n");
230938c8a9a5SSteve French 
231038c8a9a5SSteve French 			mutex_lock(&ses->session_mutex);
2311185f065fSMeetakshi Setiya 
2312185f065fSMeetakshi Setiya retry_old_session:
231338c8a9a5SSteve French 			rc = cifs_negotiate_protocol(xid, ses, server);
231438c8a9a5SSteve French 			if (rc) {
231538c8a9a5SSteve French 				mutex_unlock(&ses->session_mutex);
231638c8a9a5SSteve French 				/* problem -- put our ses reference */
231738c8a9a5SSteve French 				cifs_put_smb_ses(ses);
231838c8a9a5SSteve French 				free_xid(xid);
231938c8a9a5SSteve French 				return ERR_PTR(rc);
232038c8a9a5SSteve French 			}
232138c8a9a5SSteve French 
232238c8a9a5SSteve French 			rc = cifs_setup_session(xid, ses, server,
232338c8a9a5SSteve French 						ctx->local_nls);
232438c8a9a5SSteve French 			if (rc) {
2325185f065fSMeetakshi Setiya 				if (((rc == -EACCES) || (rc == -EKEYEXPIRED) ||
2326185f065fSMeetakshi Setiya 					(rc == -EKEYREVOKED)) && !retries && ses->password2) {
2327185f065fSMeetakshi Setiya 					retries++;
2328185f065fSMeetakshi Setiya 					cifs_dbg(FYI, "Session reconnect failed, retrying with alternate password\n");
2329185f065fSMeetakshi Setiya 					swap(ses->password, ses->password2);
2330185f065fSMeetakshi Setiya 					goto retry_old_session;
2331185f065fSMeetakshi Setiya 				}
233238c8a9a5SSteve French 				mutex_unlock(&ses->session_mutex);
233338c8a9a5SSteve French 				/* problem -- put our reference */
233438c8a9a5SSteve French 				cifs_put_smb_ses(ses);
233538c8a9a5SSteve French 				free_xid(xid);
233638c8a9a5SSteve French 				return ERR_PTR(rc);
233738c8a9a5SSteve French 			}
233838c8a9a5SSteve French 			mutex_unlock(&ses->session_mutex);
233938c8a9a5SSteve French 
234038c8a9a5SSteve French 			spin_lock(&ses->chan_lock);
234138c8a9a5SSteve French 		}
234238c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
234338c8a9a5SSteve French 
234438c8a9a5SSteve French 		/* existing SMB ses has a server reference already */
234538c8a9a5SSteve French 		cifs_put_tcp_session(server, 0);
234638c8a9a5SSteve French 		free_xid(xid);
234738c8a9a5SSteve French 		return ses;
234838c8a9a5SSteve French 	}
234938c8a9a5SSteve French 
235038c8a9a5SSteve French 	rc = -ENOMEM;
235138c8a9a5SSteve French 
235238c8a9a5SSteve French 	cifs_dbg(FYI, "Existing smb sess not found\n");
235338c8a9a5SSteve French 	ses = sesInfoAlloc();
235438c8a9a5SSteve French 	if (ses == NULL)
235538c8a9a5SSteve French 		goto get_ses_fail;
235638c8a9a5SSteve French 
235738c8a9a5SSteve French 	/* new SMB session uses our server ref */
235838c8a9a5SSteve French 	ses->server = server;
235938c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6)
236038c8a9a5SSteve French 		sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
236138c8a9a5SSteve French 	else
236238c8a9a5SSteve French 		sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
236338c8a9a5SSteve French 
236438c8a9a5SSteve French 	if (ctx->username) {
236538c8a9a5SSteve French 		ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
236638c8a9a5SSteve French 		if (!ses->user_name)
236738c8a9a5SSteve French 			goto get_ses_fail;
236838c8a9a5SSteve French 	}
236938c8a9a5SSteve French 
237038c8a9a5SSteve French 	/* ctx->password freed at unmount */
237138c8a9a5SSteve French 	if (ctx->password) {
237238c8a9a5SSteve French 		ses->password = kstrdup(ctx->password, GFP_KERNEL);
237338c8a9a5SSteve French 		if (!ses->password)
237438c8a9a5SSteve French 			goto get_ses_fail;
237538c8a9a5SSteve French 	}
237608bedfbcSSteve French 	/* ctx->password freed at unmount */
237708bedfbcSSteve French 	if (ctx->password2) {
237808bedfbcSSteve French 		ses->password2 = kstrdup(ctx->password2, GFP_KERNEL);
237908bedfbcSSteve French 		if (!ses->password2)
238008bedfbcSSteve French 			goto get_ses_fail;
238108bedfbcSSteve French 	}
238238c8a9a5SSteve French 	if (ctx->domainname) {
238338c8a9a5SSteve French 		ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
238438c8a9a5SSteve French 		if (!ses->domainName)
238538c8a9a5SSteve French 			goto get_ses_fail;
238638c8a9a5SSteve French 	}
238738c8a9a5SSteve French 
238838c8a9a5SSteve French 	strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name));
238938c8a9a5SSteve French 
239038c8a9a5SSteve French 	if (ctx->domainauto)
239138c8a9a5SSteve French 		ses->domainAuto = ctx->domainauto;
239238c8a9a5SSteve French 	ses->cred_uid = ctx->cred_uid;
239338c8a9a5SSteve French 	ses->linux_uid = ctx->linux_uid;
239438c8a9a5SSteve French 
239538c8a9a5SSteve French 	ses->sectype = ctx->sectype;
239638c8a9a5SSteve French 	ses->sign = ctx->sign;
2397a43f95fdSWinston Wen 	ses->local_nls = load_nls(ctx->local_nls->charset);
239838c8a9a5SSteve French 
239938c8a9a5SSteve French 	/* add server as first channel */
240038c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
240138c8a9a5SSteve French 	ses->chans[0].server = server;
240238c8a9a5SSteve French 	ses->chan_count = 1;
240338c8a9a5SSteve French 	ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
240438c8a9a5SSteve French 	ses->chans_need_reconnect = 1;
240538c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
240638c8a9a5SSteve French 
2407185f065fSMeetakshi Setiya retry_new_session:
240838c8a9a5SSteve French 	mutex_lock(&ses->session_mutex);
240938c8a9a5SSteve French 	rc = cifs_negotiate_protocol(xid, ses, server);
241038c8a9a5SSteve French 	if (!rc)
241138c8a9a5SSteve French 		rc = cifs_setup_session(xid, ses, server, ctx->local_nls);
241238c8a9a5SSteve French 	mutex_unlock(&ses->session_mutex);
241338c8a9a5SSteve French 
241438c8a9a5SSteve French 	/* each channel uses a different signing key */
241538c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
241638c8a9a5SSteve French 	memcpy(ses->chans[0].signkey, ses->smb3signingkey,
241738c8a9a5SSteve French 	       sizeof(ses->smb3signingkey));
241838c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
241938c8a9a5SSteve French 
2420185f065fSMeetakshi Setiya 	if (rc) {
2421185f065fSMeetakshi Setiya 		if (((rc == -EACCES) || (rc == -EKEYEXPIRED) ||
2422185f065fSMeetakshi Setiya 			(rc == -EKEYREVOKED)) && !retries && ses->password2) {
2423185f065fSMeetakshi Setiya 			retries++;
2424185f065fSMeetakshi Setiya 			cifs_dbg(FYI, "Session setup failed, retrying with alternate password\n");
2425185f065fSMeetakshi Setiya 			swap(ses->password, ses->password2);
2426185f065fSMeetakshi Setiya 			goto retry_new_session;
2427185f065fSMeetakshi Setiya 		} else
242838c8a9a5SSteve French 			goto get_ses_fail;
2429185f065fSMeetakshi Setiya 	}
243038c8a9a5SSteve French 
243138c8a9a5SSteve French 	/*
243238c8a9a5SSteve French 	 * success, put it on the list and add it as first channel
243338c8a9a5SSteve French 	 * note: the session becomes active soon after this. So you'll
243438c8a9a5SSteve French 	 * need to lock before changing something in the session.
243538c8a9a5SSteve French 	 */
243638c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
2437645f332cSPaulo Alcantara 	if (ctx->dfs_root_ses)
2438645f332cSPaulo Alcantara 		cifs_smb_ses_inc_refcount(ctx->dfs_root_ses);
243938c8a9a5SSteve French 	ses->dfs_root_ses = ctx->dfs_root_ses;
244038c8a9a5SSteve French 	list_add(&ses->smb_ses_list, &server->smb_ses_list);
244138c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
244238c8a9a5SSteve French 
244338c8a9a5SSteve French 	cifs_setup_ipc(ses, ctx);
244438c8a9a5SSteve French 
244538c8a9a5SSteve French 	free_xid(xid);
244638c8a9a5SSteve French 
244738c8a9a5SSteve French 	return ses;
244838c8a9a5SSteve French 
244938c8a9a5SSteve French get_ses_fail:
245038c8a9a5SSteve French 	sesInfoFree(ses);
245138c8a9a5SSteve French 	free_xid(xid);
245238c8a9a5SSteve French 	return ERR_PTR(rc);
245338c8a9a5SSteve French }
245438c8a9a5SSteve French 
245538c8a9a5SSteve French /* this function must be called with tc_lock held */
245638c8a9a5SSteve French static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
match_tcon(struct cifs_tcon * tcon,struct smb3_fs_context * ctx)245738c8a9a5SSteve French {
245838c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
245938c8a9a5SSteve French 
246038c8a9a5SSteve French 	if (tcon->status == TID_EXITING)
246138c8a9a5SSteve French 		return 0;
24623ae872deSPaulo Alcantara 
24633ae872deSPaulo Alcantara 	if (tcon->origin_fullpath) {
24643ae872deSPaulo Alcantara 		if (!ctx->source ||
24653ae872deSPaulo Alcantara 		    !dfs_src_pathname_equal(ctx->source,
24663ae872deSPaulo Alcantara 					    tcon->origin_fullpath))
246738c8a9a5SSteve French 			return 0;
24683ae872deSPaulo Alcantara 	} else if (!server->leaf_fullpath &&
24693ae872deSPaulo Alcantara 		   strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE)) {
24703ae872deSPaulo Alcantara 		return 0;
24713ae872deSPaulo Alcantara 	}
247238c8a9a5SSteve French 	if (tcon->seal != ctx->seal)
247338c8a9a5SSteve French 		return 0;
247438c8a9a5SSteve French 	if (tcon->snapshot_time != ctx->snapshot_time)
247538c8a9a5SSteve French 		return 0;
247638c8a9a5SSteve French 	if (tcon->handle_timeout != ctx->handle_timeout)
247738c8a9a5SSteve French 		return 0;
247838c8a9a5SSteve French 	if (tcon->no_lease != ctx->no_lease)
247938c8a9a5SSteve French 		return 0;
248038c8a9a5SSteve French 	if (tcon->nodelete != ctx->nodelete)
248138c8a9a5SSteve French 		return 0;
248238c8a9a5SSteve French 	return 1;
248338c8a9a5SSteve French }
248438c8a9a5SSteve French 
248538c8a9a5SSteve French static struct cifs_tcon *
248638c8a9a5SSteve French cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
cifs_find_tcon(struct cifs_ses * ses,struct smb3_fs_context * ctx)248738c8a9a5SSteve French {
248838c8a9a5SSteve French 	struct cifs_tcon *tcon;
248938c8a9a5SSteve French 
249038c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
249138c8a9a5SSteve French 	list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
249238c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
249338c8a9a5SSteve French 		if (!match_tcon(tcon, ctx)) {
249438c8a9a5SSteve French 			spin_unlock(&tcon->tc_lock);
249538c8a9a5SSteve French 			continue;
249638c8a9a5SSteve French 		}
249738c8a9a5SSteve French 		++tcon->tc_count;
249807e76ea1SDavid Howells 		trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
249907e76ea1SDavid Howells 				    netfs_trace_tcon_ref_get_find);
250038c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
250138c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
250238c8a9a5SSteve French 		return tcon;
250338c8a9a5SSteve French 	}
250438c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
250538c8a9a5SSteve French 	return NULL;
250638c8a9a5SSteve French }
250738c8a9a5SSteve French 
250838c8a9a5SSteve French void
250907e76ea1SDavid Howells cifs_put_tcon(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace)
cifs_put_tcon(struct cifs_tcon * tcon,enum smb3_tcon_ref_trace trace)251038c8a9a5SSteve French {
251138c8a9a5SSteve French 	unsigned int xid;
251238c8a9a5SSteve French 	struct cifs_ses *ses;
251338c8a9a5SSteve French 
251438c8a9a5SSteve French 	/*
251538c8a9a5SSteve French 	 * IPC tcon share the lifetime of their session and are
251638c8a9a5SSteve French 	 * destroyed in the session put function
251738c8a9a5SSteve French 	 */
251838c8a9a5SSteve French 	if (tcon == NULL || tcon->ipc)
251938c8a9a5SSteve French 		return;
252038c8a9a5SSteve French 
252138c8a9a5SSteve French 	ses = tcon->ses;
252238c8a9a5SSteve French 	cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
252338c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
252438c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
252507e76ea1SDavid Howells 	trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count - 1, trace);
252638c8a9a5SSteve French 	if (--tcon->tc_count > 0) {
252738c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
252838c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
252938c8a9a5SSteve French 		return;
253038c8a9a5SSteve French 	}
253138c8a9a5SSteve French 
253238c8a9a5SSteve French 	/* tc_count can never go negative */
253338c8a9a5SSteve French 	WARN_ON(tcon->tc_count < 0);
253438c8a9a5SSteve French 
253538c8a9a5SSteve French 	list_del_init(&tcon->tcon_list);
253638c8a9a5SSteve French 	tcon->status = TID_EXITING;
253738c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
253838c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
253938c8a9a5SSteve French 
254038c8a9a5SSteve French 	/* cancel polling of interfaces */
254138c8a9a5SSteve French 	cancel_delayed_work_sync(&tcon->query_interfaces);
254238c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
254338c8a9a5SSteve French 	cancel_delayed_work_sync(&tcon->dfs_cache_work);
254438c8a9a5SSteve French #endif
254538c8a9a5SSteve French 
254638c8a9a5SSteve French 	if (tcon->use_witness) {
254738c8a9a5SSteve French 		int rc;
254838c8a9a5SSteve French 
254938c8a9a5SSteve French 		rc = cifs_swn_unregister(tcon);
255038c8a9a5SSteve French 		if (rc < 0) {
255138c8a9a5SSteve French 			cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
255238c8a9a5SSteve French 					__func__, rc);
255338c8a9a5SSteve French 		}
255438c8a9a5SSteve French 	}
255538c8a9a5SSteve French 
255638c8a9a5SSteve French 	xid = get_xid();
255738c8a9a5SSteve French 	if (ses->server->ops->tree_disconnect)
255838c8a9a5SSteve French 		ses->server->ops->tree_disconnect(xid, tcon);
255938c8a9a5SSteve French 	_free_xid(xid);
256038c8a9a5SSteve French 
256138c8a9a5SSteve French 	cifs_fscache_release_super_cookie(tcon);
256207e76ea1SDavid Howells 	tconInfoFree(tcon, netfs_trace_tcon_ref_free);
256338c8a9a5SSteve French 	cifs_put_smb_ses(ses);
256438c8a9a5SSteve French }
256538c8a9a5SSteve French 
256638c8a9a5SSteve French /**
256738c8a9a5SSteve French  * cifs_get_tcon - get a tcon matching @ctx data from @ses
256838c8a9a5SSteve French  * @ses: smb session to issue the request on
256938c8a9a5SSteve French  * @ctx: the superblock configuration context to use for building the
257038c8a9a5SSteve French  *
257138c8a9a5SSteve French  * - tcon refcount is the number of mount points using the tcon.
257238c8a9a5SSteve French  * - ses refcount is the number of tcon using the session.
257338c8a9a5SSteve French  *
257438c8a9a5SSteve French  * 1. This function assumes it is being called from cifs_mount() where
257538c8a9a5SSteve French  *    we already got a session reference (ses refcount +1).
257638c8a9a5SSteve French  *
257738c8a9a5SSteve French  * 2. Since we're in the context of adding a mount point, the end
257838c8a9a5SSteve French  *    result should be either:
257938c8a9a5SSteve French  *
258038c8a9a5SSteve French  * a) a new tcon already allocated with refcount=1 (1 mount point) and
258138c8a9a5SSteve French  *    its session refcount incremented (1 new tcon). This +1 was
258238c8a9a5SSteve French  *    already done in (1).
258338c8a9a5SSteve French  *
258438c8a9a5SSteve French  * b) an existing tcon with refcount+1 (add a mount point to it) and
258538c8a9a5SSteve French  *    identical ses refcount (no new tcon). Because of (1) we need to
258638c8a9a5SSteve French  *    decrement the ses refcount.
258738c8a9a5SSteve French  */
258838c8a9a5SSteve French static struct cifs_tcon *
258938c8a9a5SSteve French cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
cifs_get_tcon(struct cifs_ses * ses,struct smb3_fs_context * ctx)259038c8a9a5SSteve French {
259138c8a9a5SSteve French 	struct cifs_tcon *tcon;
25923b8bb317SPaulo Alcantara 	bool nohandlecache;
25933b8bb317SPaulo Alcantara 	int rc, xid;
259438c8a9a5SSteve French 
259538c8a9a5SSteve French 	tcon = cifs_find_tcon(ses, ctx);
259638c8a9a5SSteve French 	if (tcon) {
259738c8a9a5SSteve French 		/*
259838c8a9a5SSteve French 		 * tcon has refcount already incremented but we need to
259938c8a9a5SSteve French 		 * decrement extra ses reference gotten by caller (case b)
260038c8a9a5SSteve French 		 */
260138c8a9a5SSteve French 		cifs_dbg(FYI, "Found match on UNC path\n");
260238c8a9a5SSteve French 		cifs_put_smb_ses(ses);
260338c8a9a5SSteve French 		return tcon;
260438c8a9a5SSteve French 	}
260538c8a9a5SSteve French 
260638c8a9a5SSteve French 	if (!ses->server->ops->tree_connect) {
260738c8a9a5SSteve French 		rc = -ENOSYS;
260838c8a9a5SSteve French 		goto out_fail;
260938c8a9a5SSteve French 	}
261038c8a9a5SSteve French 
26113b8bb317SPaulo Alcantara 	if (ses->server->dialect >= SMB20_PROT_ID &&
26123b8bb317SPaulo Alcantara 	    (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING))
2613e0614826SHenrique Carvalho 		nohandlecache = ctx->nohandlecache || !dir_cache_timeout;
26142da338ffSSteve French 	else
26153b8bb317SPaulo Alcantara 		nohandlecache = true;
261607e76ea1SDavid Howells 	tcon = tcon_info_alloc(!nohandlecache, netfs_trace_tcon_ref_new);
261738c8a9a5SSteve French 	if (tcon == NULL) {
261838c8a9a5SSteve French 		rc = -ENOMEM;
261938c8a9a5SSteve French 		goto out_fail;
262038c8a9a5SSteve French 	}
26213b8bb317SPaulo Alcantara 	tcon->nohandlecache = nohandlecache;
262238c8a9a5SSteve French 
262338c8a9a5SSteve French 	if (ctx->snapshot_time) {
262438c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
262538c8a9a5SSteve French 			cifs_dbg(VFS,
262638c8a9a5SSteve French 			     "Use SMB2 or later for snapshot mount option\n");
262738c8a9a5SSteve French 			rc = -EOPNOTSUPP;
262838c8a9a5SSteve French 			goto out_fail;
262938c8a9a5SSteve French 		} else
263038c8a9a5SSteve French 			tcon->snapshot_time = ctx->snapshot_time;
263138c8a9a5SSteve French 	}
263238c8a9a5SSteve French 
263338c8a9a5SSteve French 	if (ctx->handle_timeout) {
263438c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
263538c8a9a5SSteve French 			cifs_dbg(VFS,
263638c8a9a5SSteve French 			     "Use SMB2.1 or later for handle timeout option\n");
263738c8a9a5SSteve French 			rc = -EOPNOTSUPP;
263838c8a9a5SSteve French 			goto out_fail;
263938c8a9a5SSteve French 		} else
264038c8a9a5SSteve French 			tcon->handle_timeout = ctx->handle_timeout;
264138c8a9a5SSteve French 	}
264238c8a9a5SSteve French 
264338c8a9a5SSteve French 	tcon->ses = ses;
264438c8a9a5SSteve French 	if (ctx->password) {
264538c8a9a5SSteve French 		tcon->password = kstrdup(ctx->password, GFP_KERNEL);
264638c8a9a5SSteve French 		if (!tcon->password) {
264738c8a9a5SSteve French 			rc = -ENOMEM;
264838c8a9a5SSteve French 			goto out_fail;
264938c8a9a5SSteve French 		}
265038c8a9a5SSteve French 	}
265138c8a9a5SSteve French 
265238c8a9a5SSteve French 	if (ctx->seal) {
265338c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
265438c8a9a5SSteve French 			cifs_dbg(VFS,
265538c8a9a5SSteve French 				 "SMB3 or later required for encryption\n");
265638c8a9a5SSteve French 			rc = -EOPNOTSUPP;
265738c8a9a5SSteve French 			goto out_fail;
265838c8a9a5SSteve French 		} else if (tcon->ses->server->capabilities &
265938c8a9a5SSteve French 					SMB2_GLOBAL_CAP_ENCRYPTION)
266038c8a9a5SSteve French 			tcon->seal = true;
266138c8a9a5SSteve French 		else {
266238c8a9a5SSteve French 			cifs_dbg(VFS, "Encryption is not supported on share\n");
266338c8a9a5SSteve French 			rc = -EOPNOTSUPP;
266438c8a9a5SSteve French 			goto out_fail;
266538c8a9a5SSteve French 		}
266638c8a9a5SSteve French 	}
266738c8a9a5SSteve French 
266838c8a9a5SSteve French 	if (ctx->linux_ext) {
266938c8a9a5SSteve French 		if (ses->server->posix_ext_supported) {
267038c8a9a5SSteve French 			tcon->posix_extensions = true;
267138c8a9a5SSteve French 			pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
267238c8a9a5SSteve French 		} else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
267338c8a9a5SSteve French 		    (strcmp(ses->server->vals->version_string,
267438c8a9a5SSteve French 		     SMB3ANY_VERSION_STRING) == 0) ||
267538c8a9a5SSteve French 		    (strcmp(ses->server->vals->version_string,
267638c8a9a5SSteve French 		     SMBDEFAULT_VERSION_STRING) == 0)) {
267738c8a9a5SSteve French 			cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
267838c8a9a5SSteve French 			rc = -EOPNOTSUPP;
267938c8a9a5SSteve French 			goto out_fail;
2680f2d6c5b8SSteve French 		} else if (ses->server->vals->protocol_id == SMB10_PROT_ID)
2681f2d6c5b8SSteve French 			if (cap_unix(ses))
2682f2d6c5b8SSteve French 				cifs_dbg(FYI, "Unix Extensions requested on SMB1 mount\n");
2683f2d6c5b8SSteve French 			else {
2684f2d6c5b8SSteve French 				cifs_dbg(VFS, "SMB1 Unix Extensions not supported by server\n");
2685f2d6c5b8SSteve French 				rc = -EOPNOTSUPP;
2686f2d6c5b8SSteve French 				goto out_fail;
268738c8a9a5SSteve French 		} else {
2688da70c1d3SPierre Mariani 			cifs_dbg(VFS,
2689da70c1d3SPierre Mariani 				"Check vers= mount option. SMB3.11 disabled but required for POSIX extensions\n");
269038c8a9a5SSteve French 			rc = -EOPNOTSUPP;
269138c8a9a5SSteve French 			goto out_fail;
269238c8a9a5SSteve French 		}
269338c8a9a5SSteve French 	}
269438c8a9a5SSteve French 
269538c8a9a5SSteve French 	xid = get_xid();
269638c8a9a5SSteve French 	rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
269738c8a9a5SSteve French 					    ctx->local_nls);
269838c8a9a5SSteve French 	free_xid(xid);
269938c8a9a5SSteve French 	cifs_dbg(FYI, "Tcon rc = %d\n", rc);
270038c8a9a5SSteve French 	if (rc)
270138c8a9a5SSteve French 		goto out_fail;
270238c8a9a5SSteve French 
270338c8a9a5SSteve French 	tcon->use_persistent = false;
270438c8a9a5SSteve French 	/* check if SMB2 or later, CIFS does not support persistent handles */
270538c8a9a5SSteve French 	if (ctx->persistent) {
270638c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
270738c8a9a5SSteve French 			cifs_dbg(VFS,
270838c8a9a5SSteve French 			     "SMB3 or later required for persistent handles\n");
270938c8a9a5SSteve French 			rc = -EOPNOTSUPP;
271038c8a9a5SSteve French 			goto out_fail;
271138c8a9a5SSteve French 		} else if (ses->server->capabilities &
271238c8a9a5SSteve French 			   SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
271338c8a9a5SSteve French 			tcon->use_persistent = true;
271438c8a9a5SSteve French 		else /* persistent handles requested but not supported */ {
271538c8a9a5SSteve French 			cifs_dbg(VFS,
271638c8a9a5SSteve French 				"Persistent handles not supported on share\n");
271738c8a9a5SSteve French 			rc = -EOPNOTSUPP;
271838c8a9a5SSteve French 			goto out_fail;
271938c8a9a5SSteve French 		}
272038c8a9a5SSteve French 	} else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
272138c8a9a5SSteve French 	     && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
272238c8a9a5SSteve French 	     && (ctx->nopersistent == false)) {
272338c8a9a5SSteve French 		cifs_dbg(FYI, "enabling persistent handles\n");
272438c8a9a5SSteve French 		tcon->use_persistent = true;
272538c8a9a5SSteve French 	} else if (ctx->resilient) {
272638c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
272738c8a9a5SSteve French 			cifs_dbg(VFS,
272838c8a9a5SSteve French 			     "SMB2.1 or later required for resilient handles\n");
272938c8a9a5SSteve French 			rc = -EOPNOTSUPP;
273038c8a9a5SSteve French 			goto out_fail;
273138c8a9a5SSteve French 		}
273238c8a9a5SSteve French 		tcon->use_resilient = true;
273338c8a9a5SSteve French 	}
273438c8a9a5SSteve French 
273538c8a9a5SSteve French 	tcon->use_witness = false;
273638c8a9a5SSteve French 	if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
273738c8a9a5SSteve French 		if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
273838c8a9a5SSteve French 			if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
273938c8a9a5SSteve French 				/*
274038c8a9a5SSteve French 				 * Set witness in use flag in first place
274138c8a9a5SSteve French 				 * to retry registration in the echo task
274238c8a9a5SSteve French 				 */
274338c8a9a5SSteve French 				tcon->use_witness = true;
274438c8a9a5SSteve French 				/* And try to register immediately */
274538c8a9a5SSteve French 				rc = cifs_swn_register(tcon);
274638c8a9a5SSteve French 				if (rc < 0) {
274738c8a9a5SSteve French 					cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
274838c8a9a5SSteve French 					goto out_fail;
274938c8a9a5SSteve French 				}
275038c8a9a5SSteve French 			} else {
275138c8a9a5SSteve French 				/* TODO: try to extend for non-cluster uses (eg multichannel) */
275238c8a9a5SSteve French 				cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
275338c8a9a5SSteve French 				rc = -EOPNOTSUPP;
275438c8a9a5SSteve French 				goto out_fail;
275538c8a9a5SSteve French 			}
275638c8a9a5SSteve French 		} else {
275738c8a9a5SSteve French 			cifs_dbg(VFS, "SMB3 or later required for witness option\n");
275838c8a9a5SSteve French 			rc = -EOPNOTSUPP;
275938c8a9a5SSteve French 			goto out_fail;
276038c8a9a5SSteve French 		}
276138c8a9a5SSteve French 	}
276238c8a9a5SSteve French 
276338c8a9a5SSteve French 	/* If the user really knows what they are doing they can override */
276438c8a9a5SSteve French 	if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
276538c8a9a5SSteve French 		if (ctx->cache_ro)
276638c8a9a5SSteve French 			cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
276738c8a9a5SSteve French 		else if (ctx->cache_rw)
276838c8a9a5SSteve French 			cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
276938c8a9a5SSteve French 	}
277038c8a9a5SSteve French 
277138c8a9a5SSteve French 	if (ctx->no_lease) {
277238c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
277338c8a9a5SSteve French 			cifs_dbg(VFS,
277438c8a9a5SSteve French 				"SMB2 or later required for nolease option\n");
277538c8a9a5SSteve French 			rc = -EOPNOTSUPP;
277638c8a9a5SSteve French 			goto out_fail;
277738c8a9a5SSteve French 		} else
277838c8a9a5SSteve French 			tcon->no_lease = ctx->no_lease;
277938c8a9a5SSteve French 	}
278038c8a9a5SSteve French 
278138c8a9a5SSteve French 	/*
278238c8a9a5SSteve French 	 * We can have only one retry value for a connection to a share so for
278338c8a9a5SSteve French 	 * resources mounted more than once to the same server share the last
278438c8a9a5SSteve French 	 * value passed in for the retry flag is used.
278538c8a9a5SSteve French 	 */
278638c8a9a5SSteve French 	tcon->retry = ctx->retry;
278738c8a9a5SSteve French 	tcon->nocase = ctx->nocase;
278838c8a9a5SSteve French 	tcon->broken_sparse_sup = ctx->no_sparse;
27896a50d71dSSteve French 	tcon->max_cached_dirs = ctx->max_cached_dirs;
279038c8a9a5SSteve French 	tcon->nodelete = ctx->nodelete;
279138c8a9a5SSteve French 	tcon->local_lease = ctx->local_lease;
279238c8a9a5SSteve French 	INIT_LIST_HEAD(&tcon->pending_opens);
279338c8a9a5SSteve French 	tcon->status = TID_GOOD;
279438c8a9a5SSteve French 
279538c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcon->query_interfaces,
279638c8a9a5SSteve French 			  smb2_query_server_interfaces);
279738c8a9a5SSteve French 	if (ses->server->dialect >= SMB30_PROT_ID &&
279838c8a9a5SSteve French 	    (ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
279938c8a9a5SSteve French 		/* schedule query interfaces poll */
280038c8a9a5SSteve French 		queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
280138c8a9a5SSteve French 				   (SMB_INTERFACE_POLL_INTERVAL * HZ));
280238c8a9a5SSteve French 	}
280338c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
280438c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcon->dfs_cache_work, dfs_cache_refresh);
280538c8a9a5SSteve French #endif
280638c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
280738c8a9a5SSteve French 	list_add(&tcon->tcon_list, &ses->tcon_list);
280838c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
280938c8a9a5SSteve French 
281038c8a9a5SSteve French 	return tcon;
281138c8a9a5SSteve French 
281238c8a9a5SSteve French out_fail:
281307e76ea1SDavid Howells 	tconInfoFree(tcon, netfs_trace_tcon_ref_free_fail);
281438c8a9a5SSteve French 	return ERR_PTR(rc);
281538c8a9a5SSteve French }
281638c8a9a5SSteve French 
281738c8a9a5SSteve French void
281838c8a9a5SSteve French cifs_put_tlink(struct tcon_link *tlink)
cifs_put_tlink(struct tcon_link * tlink)281938c8a9a5SSteve French {
282038c8a9a5SSteve French 	if (!tlink || IS_ERR(tlink))
282138c8a9a5SSteve French 		return;
282238c8a9a5SSteve French 
282338c8a9a5SSteve French 	if (!atomic_dec_and_test(&tlink->tl_count) ||
282438c8a9a5SSteve French 	    test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
282538c8a9a5SSteve French 		tlink->tl_time = jiffies;
282638c8a9a5SSteve French 		return;
282738c8a9a5SSteve French 	}
282838c8a9a5SSteve French 
282938c8a9a5SSteve French 	if (!IS_ERR(tlink_tcon(tlink)))
283007e76ea1SDavid Howells 		cifs_put_tcon(tlink_tcon(tlink), netfs_trace_tcon_ref_put_tlink);
283138c8a9a5SSteve French 	kfree(tlink);
283238c8a9a5SSteve French }
283338c8a9a5SSteve French 
283438c8a9a5SSteve French static int
283538c8a9a5SSteve French compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
compare_mount_options(struct super_block * sb,struct cifs_mnt_data * mnt_data)283638c8a9a5SSteve French {
283738c8a9a5SSteve French 	struct cifs_sb_info *old = CIFS_SB(sb);
283838c8a9a5SSteve French 	struct cifs_sb_info *new = mnt_data->cifs_sb;
283938c8a9a5SSteve French 	unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
284038c8a9a5SSteve French 	unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
284138c8a9a5SSteve French 
284238c8a9a5SSteve French 	if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
284338c8a9a5SSteve French 		return 0;
284438c8a9a5SSteve French 
284538c8a9a5SSteve French 	if (old->mnt_cifs_serverino_autodisabled)
284638c8a9a5SSteve French 		newflags &= ~CIFS_MOUNT_SERVER_INUM;
284738c8a9a5SSteve French 
284838c8a9a5SSteve French 	if (oldflags != newflags)
284938c8a9a5SSteve French 		return 0;
285038c8a9a5SSteve French 
285138c8a9a5SSteve French 	/*
285238c8a9a5SSteve French 	 * We want to share sb only if we don't specify an r/wsize or
285338c8a9a5SSteve French 	 * specified r/wsize is greater than or equal to existing one.
285438c8a9a5SSteve French 	 */
285538c8a9a5SSteve French 	if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
285638c8a9a5SSteve French 		return 0;
285738c8a9a5SSteve French 
285838c8a9a5SSteve French 	if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
285938c8a9a5SSteve French 		return 0;
286038c8a9a5SSteve French 
286138c8a9a5SSteve French 	if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
286238c8a9a5SSteve French 	    !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
286338c8a9a5SSteve French 		return 0;
286438c8a9a5SSteve French 
286538c8a9a5SSteve French 	if (old->ctx->file_mode != new->ctx->file_mode ||
286638c8a9a5SSteve French 	    old->ctx->dir_mode != new->ctx->dir_mode)
286738c8a9a5SSteve French 		return 0;
286838c8a9a5SSteve French 
286938c8a9a5SSteve French 	if (strcmp(old->local_nls->charset, new->local_nls->charset))
287038c8a9a5SSteve French 		return 0;
287138c8a9a5SSteve French 
287238c8a9a5SSteve French 	if (old->ctx->acregmax != new->ctx->acregmax)
287338c8a9a5SSteve French 		return 0;
287438c8a9a5SSteve French 	if (old->ctx->acdirmax != new->ctx->acdirmax)
287538c8a9a5SSteve French 		return 0;
287638c8a9a5SSteve French 	if (old->ctx->closetimeo != new->ctx->closetimeo)
287738c8a9a5SSteve French 		return 0;
2878c1468c7eSPaulo Alcantara 	if (old->ctx->reparse_type != new->ctx->reparse_type)
2879c1468c7eSPaulo Alcantara 		return 0;
288038c8a9a5SSteve French 
288138c8a9a5SSteve French 	return 1;
288238c8a9a5SSteve French }
288338c8a9a5SSteve French 
288438c8a9a5SSteve French static int match_prepath(struct super_block *sb,
match_prepath(struct super_block * sb,struct cifs_tcon * tcon,struct cifs_mnt_data * mnt_data)28853ae872deSPaulo Alcantara 			 struct cifs_tcon *tcon,
288638c8a9a5SSteve French 			 struct cifs_mnt_data *mnt_data)
288738c8a9a5SSteve French {
288838c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_data->ctx;
288938c8a9a5SSteve French 	struct cifs_sb_info *old = CIFS_SB(sb);
289038c8a9a5SSteve French 	struct cifs_sb_info *new = mnt_data->cifs_sb;
289138c8a9a5SSteve French 	bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
289238c8a9a5SSteve French 		old->prepath;
289338c8a9a5SSteve French 	bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
289438c8a9a5SSteve French 		new->prepath;
289538c8a9a5SSteve French 
28963ae872deSPaulo Alcantara 	if (tcon->origin_fullpath &&
28973ae872deSPaulo Alcantara 	    dfs_src_pathname_equal(tcon->origin_fullpath, ctx->source))
289838c8a9a5SSteve French 		return 1;
289938c8a9a5SSteve French 
290038c8a9a5SSteve French 	if (old_set && new_set && !strcmp(new->prepath, old->prepath))
290138c8a9a5SSteve French 		return 1;
290238c8a9a5SSteve French 	else if (!old_set && !new_set)
290338c8a9a5SSteve French 		return 1;
290438c8a9a5SSteve French 
290538c8a9a5SSteve French 	return 0;
290638c8a9a5SSteve French }
290738c8a9a5SSteve French 
290838c8a9a5SSteve French int
290938c8a9a5SSteve French cifs_match_super(struct super_block *sb, void *data)
cifs_match_super(struct super_block * sb,void * data)291038c8a9a5SSteve French {
291138c8a9a5SSteve French 	struct cifs_mnt_data *mnt_data = data;
291238c8a9a5SSteve French 	struct smb3_fs_context *ctx;
291338c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb;
291438c8a9a5SSteve French 	struct TCP_Server_Info *tcp_srv;
291538c8a9a5SSteve French 	struct cifs_ses *ses;
291638c8a9a5SSteve French 	struct cifs_tcon *tcon;
291738c8a9a5SSteve French 	struct tcon_link *tlink;
291838c8a9a5SSteve French 	int rc = 0;
291938c8a9a5SSteve French 
292038c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
292138c8a9a5SSteve French 	cifs_sb = CIFS_SB(sb);
292238c8a9a5SSteve French 
292338c8a9a5SSteve French 	/* We do not want to use a superblock that has been shutdown */
292438c8a9a5SSteve French 	if (CIFS_MOUNT_SHUTDOWN & cifs_sb->mnt_cifs_flags) {
292538c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
292638c8a9a5SSteve French 		return 0;
292738c8a9a5SSteve French 	}
292838c8a9a5SSteve French 
292938c8a9a5SSteve French 	tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2930fc1bd51dSPaulo Alcantara 	if (IS_ERR_OR_NULL(tlink)) {
2931fc1bd51dSPaulo Alcantara 		pr_warn_once("%s: skip super matching due to bad tlink(%p)\n",
2932fc1bd51dSPaulo Alcantara 			     __func__, tlink);
293338c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
293438c8a9a5SSteve French 		return 0;
293538c8a9a5SSteve French 	}
293638c8a9a5SSteve French 	tcon = tlink_tcon(tlink);
293738c8a9a5SSteve French 	ses = tcon->ses;
293838c8a9a5SSteve French 	tcp_srv = ses->server;
293938c8a9a5SSteve French 
294038c8a9a5SSteve French 	ctx = mnt_data->ctx;
294138c8a9a5SSteve French 
294238c8a9a5SSteve French 	spin_lock(&tcp_srv->srv_lock);
294338c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
294438c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
294538c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
29463ae872deSPaulo Alcantara 	if (!match_server(tcp_srv, ctx, true) ||
294738c8a9a5SSteve French 	    !match_session(ses, ctx) ||
294838c8a9a5SSteve French 	    !match_tcon(tcon, ctx) ||
29493ae872deSPaulo Alcantara 	    !match_prepath(sb, tcon, mnt_data)) {
295038c8a9a5SSteve French 		rc = 0;
295138c8a9a5SSteve French 		goto out;
295238c8a9a5SSteve French 	}
295338c8a9a5SSteve French 
295438c8a9a5SSteve French 	rc = compare_mount_options(sb, mnt_data);
295538c8a9a5SSteve French out:
295638c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
295738c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
295838c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
295938c8a9a5SSteve French 	spin_unlock(&tcp_srv->srv_lock);
296038c8a9a5SSteve French 
296138c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
296238c8a9a5SSteve French 	cifs_put_tlink(tlink);
296338c8a9a5SSteve French 	return rc;
296438c8a9a5SSteve French }
296538c8a9a5SSteve French 
296638c8a9a5SSteve French #ifdef CONFIG_DEBUG_LOCK_ALLOC
296738c8a9a5SSteve French static struct lock_class_key cifs_key[2];
296838c8a9a5SSteve French static struct lock_class_key cifs_slock_key[2];
296938c8a9a5SSteve French 
297038c8a9a5SSteve French static inline void
297138c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock)
cifs_reclassify_socket4(struct socket * sock)297238c8a9a5SSteve French {
297338c8a9a5SSteve French 	struct sock *sk = sock->sk;
2974da70c1d3SPierre Mariani 
297538c8a9a5SSteve French 	BUG_ON(!sock_allow_reclassification(sk));
297638c8a9a5SSteve French 	sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
297738c8a9a5SSteve French 		&cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
297838c8a9a5SSteve French }
297938c8a9a5SSteve French 
298038c8a9a5SSteve French static inline void
298138c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock)
cifs_reclassify_socket6(struct socket * sock)298238c8a9a5SSteve French {
298338c8a9a5SSteve French 	struct sock *sk = sock->sk;
2984da70c1d3SPierre Mariani 
298538c8a9a5SSteve French 	BUG_ON(!sock_allow_reclassification(sk));
298638c8a9a5SSteve French 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
298738c8a9a5SSteve French 		&cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
298838c8a9a5SSteve French }
298938c8a9a5SSteve French #else
299038c8a9a5SSteve French static inline void
299138c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock)
cifs_reclassify_socket4(struct socket * sock)299238c8a9a5SSteve French {
299338c8a9a5SSteve French }
299438c8a9a5SSteve French 
299538c8a9a5SSteve French static inline void
299638c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock)
cifs_reclassify_socket6(struct socket * sock)299738c8a9a5SSteve French {
299838c8a9a5SSteve French }
299938c8a9a5SSteve French #endif
300038c8a9a5SSteve French 
300138c8a9a5SSteve French /* See RFC1001 section 14 on representation of Netbios names */
300238c8a9a5SSteve French static void rfc1002mangle(char *target, char *source, unsigned int length)
rfc1002mangle(char * target,char * source,unsigned int length)300338c8a9a5SSteve French {
300438c8a9a5SSteve French 	unsigned int i, j;
300538c8a9a5SSteve French 
300638c8a9a5SSteve French 	for (i = 0, j = 0; i < (length); i++) {
300738c8a9a5SSteve French 		/* mask a nibble at a time and encode */
300838c8a9a5SSteve French 		target[j] = 'A' + (0x0F & (source[i] >> 4));
300938c8a9a5SSteve French 		target[j+1] = 'A' + (0x0F & source[i]);
301038c8a9a5SSteve French 		j += 2;
301138c8a9a5SSteve French 	}
301238c8a9a5SSteve French 
301338c8a9a5SSteve French }
301438c8a9a5SSteve French 
301538c8a9a5SSteve French static int
301638c8a9a5SSteve French bind_socket(struct TCP_Server_Info *server)
bind_socket(struct TCP_Server_Info * server)301738c8a9a5SSteve French {
301838c8a9a5SSteve French 	int rc = 0;
3019da70c1d3SPierre Mariani 
302038c8a9a5SSteve French 	if (server->srcaddr.ss_family != AF_UNSPEC) {
302138c8a9a5SSteve French 		/* Bind to the specified local IP address */
302238c8a9a5SSteve French 		struct socket *socket = server->ssocket;
3023da70c1d3SPierre Mariani 
3024cedc019bSJordan Rife 		rc = kernel_bind(socket,
302538c8a9a5SSteve French 				 (struct sockaddr *) &server->srcaddr,
302638c8a9a5SSteve French 				 sizeof(server->srcaddr));
302738c8a9a5SSteve French 		if (rc < 0) {
302838c8a9a5SSteve French 			struct sockaddr_in *saddr4;
302938c8a9a5SSteve French 			struct sockaddr_in6 *saddr6;
3030da70c1d3SPierre Mariani 
303138c8a9a5SSteve French 			saddr4 = (struct sockaddr_in *)&server->srcaddr;
303238c8a9a5SSteve French 			saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
303338c8a9a5SSteve French 			if (saddr6->sin6_family == AF_INET6)
303438c8a9a5SSteve French 				cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
303538c8a9a5SSteve French 					 &saddr6->sin6_addr, rc);
303638c8a9a5SSteve French 			else
303738c8a9a5SSteve French 				cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
303838c8a9a5SSteve French 					 &saddr4->sin_addr.s_addr, rc);
303938c8a9a5SSteve French 		}
304038c8a9a5SSteve French 	}
304138c8a9a5SSteve French 	return rc;
304238c8a9a5SSteve French }
304338c8a9a5SSteve French 
304438c8a9a5SSteve French static int
304538c8a9a5SSteve French ip_rfc1001_connect(struct TCP_Server_Info *server)
ip_rfc1001_connect(struct TCP_Server_Info * server)304638c8a9a5SSteve French {
304738c8a9a5SSteve French 	int rc = 0;
304838c8a9a5SSteve French 	/*
304938c8a9a5SSteve French 	 * some servers require RFC1001 sessinit before sending
305038c8a9a5SSteve French 	 * negprot - BB check reconnection in case where second
305138c8a9a5SSteve French 	 * sessinit is sent but no second negprot
305238c8a9a5SSteve French 	 */
305338c8a9a5SSteve French 	struct rfc1002_session_packet req = {};
305438c8a9a5SSteve French 	struct smb_hdr *smb_buf = (struct smb_hdr *)&req;
305538c8a9a5SSteve French 	unsigned int len;
305638c8a9a5SSteve French 
305738c8a9a5SSteve French 	req.trailer.session_req.called_len = sizeof(req.trailer.session_req.called_name);
305838c8a9a5SSteve French 
305938c8a9a5SSteve French 	if (server->server_RFC1001_name[0] != 0)
306038c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.called_name,
306138c8a9a5SSteve French 			      server->server_RFC1001_name,
306238c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
306338c8a9a5SSteve French 	else
306438c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.called_name,
306538c8a9a5SSteve French 			      DEFAULT_CIFS_CALLED_NAME,
306638c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
306738c8a9a5SSteve French 
306838c8a9a5SSteve French 	req.trailer.session_req.calling_len = sizeof(req.trailer.session_req.calling_name);
306938c8a9a5SSteve French 
307038c8a9a5SSteve French 	/* calling name ends in null (byte 16) from old smb convention */
307138c8a9a5SSteve French 	if (server->workstation_RFC1001_name[0] != 0)
307238c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.calling_name,
307338c8a9a5SSteve French 			      server->workstation_RFC1001_name,
307438c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
307538c8a9a5SSteve French 	else
307638c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.calling_name,
307738c8a9a5SSteve French 			      "LINUX_CIFS_CLNT",
307838c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
307938c8a9a5SSteve French 
308038c8a9a5SSteve French 	/*
308138c8a9a5SSteve French 	 * As per rfc1002, @len must be the number of bytes that follows the
308238c8a9a5SSteve French 	 * length field of a rfc1002 session request payload.
308338c8a9a5SSteve French 	 */
308438c8a9a5SSteve French 	len = sizeof(req) - offsetof(struct rfc1002_session_packet, trailer.session_req);
308538c8a9a5SSteve French 
308638c8a9a5SSteve French 	smb_buf->smb_buf_length = cpu_to_be32((RFC1002_SESSION_REQUEST << 24) | len);
308738c8a9a5SSteve French 	rc = smb_send(server, smb_buf, len);
308838c8a9a5SSteve French 	/*
308938c8a9a5SSteve French 	 * RFC1001 layer in at least one server requires very short break before
309038c8a9a5SSteve French 	 * negprot presumably because not expecting negprot to follow so fast.
309138c8a9a5SSteve French 	 * This is a simple solution that works without complicating the code
309238c8a9a5SSteve French 	 * and causes no significant slowing down on mount for everyone else
309338c8a9a5SSteve French 	 */
309438c8a9a5SSteve French 	usleep_range(1000, 2000);
309538c8a9a5SSteve French 
309638c8a9a5SSteve French 	return rc;
309738c8a9a5SSteve French }
309838c8a9a5SSteve French 
309938c8a9a5SSteve French static int
310038c8a9a5SSteve French generic_ip_connect(struct TCP_Server_Info *server)
generic_ip_connect(struct TCP_Server_Info * server)310138c8a9a5SSteve French {
310238c8a9a5SSteve French 	struct sockaddr *saddr;
3103f0b6a834SPaulo Alcantara 	struct socket *socket;
3104f0b6a834SPaulo Alcantara 	int slen, sfamily;
3105f0b6a834SPaulo Alcantara 	__be16 sport;
3106f0b6a834SPaulo Alcantara 	int rc = 0;
310738c8a9a5SSteve French 
310838c8a9a5SSteve French 	saddr = (struct sockaddr *) &server->dstaddr;
310938c8a9a5SSteve French 
311038c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6) {
311138c8a9a5SSteve French 		struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
311238c8a9a5SSteve French 
311338c8a9a5SSteve French 		sport = ipv6->sin6_port;
311438c8a9a5SSteve French 		slen = sizeof(struct sockaddr_in6);
311538c8a9a5SSteve French 		sfamily = AF_INET6;
311638c8a9a5SSteve French 		cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
311738c8a9a5SSteve French 				ntohs(sport));
311838c8a9a5SSteve French 	} else {
311938c8a9a5SSteve French 		struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
312038c8a9a5SSteve French 
312138c8a9a5SSteve French 		sport = ipv4->sin_port;
312238c8a9a5SSteve French 		slen = sizeof(struct sockaddr_in);
312338c8a9a5SSteve French 		sfamily = AF_INET;
312438c8a9a5SSteve French 		cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
312538c8a9a5SSteve French 				ntohs(sport));
312638c8a9a5SSteve French 	}
312738c8a9a5SSteve French 
3128f0b6a834SPaulo Alcantara 	if (server->ssocket) {
3129f0b6a834SPaulo Alcantara 		socket = server->ssocket;
3130f0b6a834SPaulo Alcantara 	} else {
3131e8c71494SKuniyuki Iwashima 		struct net *net = cifs_net_ns(server);
3132e8c71494SKuniyuki Iwashima 
3133906807c7SEnzo Matsumiya 		rc = sock_create_kern(net, sfamily, SOCK_STREAM, IPPROTO_TCP, &server->ssocket);
313438c8a9a5SSteve French 		if (rc < 0) {
313538c8a9a5SSteve French 			cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
313638c8a9a5SSteve French 			return rc;
313738c8a9a5SSteve French 		}
313838c8a9a5SSteve French 
3139906807c7SEnzo Matsumiya 		/*
3140906807c7SEnzo Matsumiya 		 * Grab netns reference for the socket.
3141906807c7SEnzo Matsumiya 		 *
3142906807c7SEnzo Matsumiya 		 * It'll be released here, on error, or in clean_demultiplex_info() upon server
3143906807c7SEnzo Matsumiya 		 * teardown.
3144906807c7SEnzo Matsumiya 		 */
3145906807c7SEnzo Matsumiya 		get_net(net);
3146e8c71494SKuniyuki Iwashima 
314738c8a9a5SSteve French 		/* BB other socket options to set KEEPALIVE, NODELAY? */
314838c8a9a5SSteve French 		cifs_dbg(FYI, "Socket created\n");
3149f0b6a834SPaulo Alcantara 		socket = server->ssocket;
315038c8a9a5SSteve French 		socket->sk->sk_allocation = GFP_NOFS;
315138c8a9a5SSteve French 		socket->sk->sk_use_task_frag = false;
315238c8a9a5SSteve French 		if (sfamily == AF_INET6)
315338c8a9a5SSteve French 			cifs_reclassify_socket6(socket);
315438c8a9a5SSteve French 		else
315538c8a9a5SSteve French 			cifs_reclassify_socket4(socket);
315638c8a9a5SSteve French 	}
315738c8a9a5SSteve French 
315838c8a9a5SSteve French 	rc = bind_socket(server);
3159906807c7SEnzo Matsumiya 	if (rc < 0) {
3160906807c7SEnzo Matsumiya 		put_net(cifs_net_ns(server));
316138c8a9a5SSteve French 		return rc;
3162906807c7SEnzo Matsumiya 	}
316338c8a9a5SSteve French 
316438c8a9a5SSteve French 	/*
316538c8a9a5SSteve French 	 * Eventually check for other socket options to change from
316638c8a9a5SSteve French 	 * the default. sock_setsockopt not used because it expects
316738c8a9a5SSteve French 	 * user space buffer
316838c8a9a5SSteve French 	 */
316938c8a9a5SSteve French 	socket->sk->sk_rcvtimeo = 7 * HZ;
317038c8a9a5SSteve French 	socket->sk->sk_sndtimeo = 5 * HZ;
317138c8a9a5SSteve French 
317238c8a9a5SSteve French 	/* make the bufsizes depend on wsize/rsize and max requests */
317338c8a9a5SSteve French 	if (server->noautotune) {
317438c8a9a5SSteve French 		if (socket->sk->sk_sndbuf < (200 * 1024))
317538c8a9a5SSteve French 			socket->sk->sk_sndbuf = 200 * 1024;
317638c8a9a5SSteve French 		if (socket->sk->sk_rcvbuf < (140 * 1024))
317738c8a9a5SSteve French 			socket->sk->sk_rcvbuf = 140 * 1024;
317838c8a9a5SSteve French 	}
317938c8a9a5SSteve French 
318038c8a9a5SSteve French 	if (server->tcp_nodelay)
318138c8a9a5SSteve French 		tcp_sock_set_nodelay(socket->sk);
318238c8a9a5SSteve French 
318338c8a9a5SSteve French 	cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
318438c8a9a5SSteve French 		 socket->sk->sk_sndbuf,
318538c8a9a5SSteve French 		 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
318638c8a9a5SSteve French 
3187cedc019bSJordan Rife 	rc = kernel_connect(socket, saddr, slen,
318838c8a9a5SSteve French 			    server->noblockcnt ? O_NONBLOCK : 0);
318938c8a9a5SSteve French 	/*
319038c8a9a5SSteve French 	 * When mounting SMB root file systems, we do not want to block in
319138c8a9a5SSteve French 	 * connect. Otherwise bail out and then let cifs_reconnect() perform
319238c8a9a5SSteve French 	 * reconnect failover - if possible.
319338c8a9a5SSteve French 	 */
319438c8a9a5SSteve French 	if (server->noblockcnt && rc == -EINPROGRESS)
319538c8a9a5SSteve French 		rc = 0;
319638c8a9a5SSteve French 	if (rc < 0) {
319738c8a9a5SSteve French 		cifs_dbg(FYI, "Error %d connecting to server\n", rc);
319838c8a9a5SSteve French 		trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
3199906807c7SEnzo Matsumiya 		put_net(cifs_net_ns(server));
320038c8a9a5SSteve French 		sock_release(socket);
320138c8a9a5SSteve French 		server->ssocket = NULL;
320238c8a9a5SSteve French 		return rc;
320338c8a9a5SSteve French 	}
320438c8a9a5SSteve French 	trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
320538c8a9a5SSteve French 	if (sport == htons(RFC1001_PORT))
320638c8a9a5SSteve French 		rc = ip_rfc1001_connect(server);
320738c8a9a5SSteve French 
3208906807c7SEnzo Matsumiya 	if (rc < 0)
3209906807c7SEnzo Matsumiya 		put_net(cifs_net_ns(server));
3210906807c7SEnzo Matsumiya 
321138c8a9a5SSteve French 	return rc;
321238c8a9a5SSteve French }
321338c8a9a5SSteve French 
321438c8a9a5SSteve French static int
321538c8a9a5SSteve French ip_connect(struct TCP_Server_Info *server)
ip_connect(struct TCP_Server_Info * server)321638c8a9a5SSteve French {
321738c8a9a5SSteve French 	__be16 *sport;
321838c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
321938c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
322038c8a9a5SSteve French 
322138c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6)
322238c8a9a5SSteve French 		sport = &addr6->sin6_port;
322338c8a9a5SSteve French 	else
322438c8a9a5SSteve French 		sport = &addr->sin_port;
322538c8a9a5SSteve French 
322638c8a9a5SSteve French 	if (*sport == 0) {
322738c8a9a5SSteve French 		int rc;
322838c8a9a5SSteve French 
322938c8a9a5SSteve French 		/* try with 445 port at first */
323038c8a9a5SSteve French 		*sport = htons(CIFS_PORT);
323138c8a9a5SSteve French 
323238c8a9a5SSteve French 		rc = generic_ip_connect(server);
323338c8a9a5SSteve French 		if (rc >= 0)
323438c8a9a5SSteve French 			return rc;
323538c8a9a5SSteve French 
323638c8a9a5SSteve French 		/* if it failed, try with 139 port */
323738c8a9a5SSteve French 		*sport = htons(RFC1001_PORT);
323838c8a9a5SSteve French 	}
323938c8a9a5SSteve French 
324038c8a9a5SSteve French 	return generic_ip_connect(server);
324138c8a9a5SSteve French }
324238c8a9a5SSteve French 
324338c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
324438c8a9a5SSteve French void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
reset_cifs_unix_caps(unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,struct smb3_fs_context * ctx)324538c8a9a5SSteve French 			  struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
324638c8a9a5SSteve French {
324738c8a9a5SSteve French 	/*
324838c8a9a5SSteve French 	 * If we are reconnecting then should we check to see if
324938c8a9a5SSteve French 	 * any requested capabilities changed locally e.g. via
325038c8a9a5SSteve French 	 * remount but we can not do much about it here
325138c8a9a5SSteve French 	 * if they have (even if we could detect it by the following)
325238c8a9a5SSteve French 	 * Perhaps we could add a backpointer to array of sb from tcon
325338c8a9a5SSteve French 	 * or if we change to make all sb to same share the same
325438c8a9a5SSteve French 	 * sb as NFS - then we only have one backpointer to sb.
325538c8a9a5SSteve French 	 * What if we wanted to mount the server share twice once with
325638c8a9a5SSteve French 	 * and once without posixacls or posix paths?
325738c8a9a5SSteve French 	 */
325838c8a9a5SSteve French 	__u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
325938c8a9a5SSteve French 
326038c8a9a5SSteve French 	if (ctx && ctx->no_linux_ext) {
326138c8a9a5SSteve French 		tcon->fsUnixInfo.Capability = 0;
326238c8a9a5SSteve French 		tcon->unix_ext = 0; /* Unix Extensions disabled */
326338c8a9a5SSteve French 		cifs_dbg(FYI, "Linux protocol extensions disabled\n");
326438c8a9a5SSteve French 		return;
326538c8a9a5SSteve French 	} else if (ctx)
326638c8a9a5SSteve French 		tcon->unix_ext = 1; /* Unix Extensions supported */
326738c8a9a5SSteve French 
326838c8a9a5SSteve French 	if (!tcon->unix_ext) {
326938c8a9a5SSteve French 		cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
327038c8a9a5SSteve French 		return;
327138c8a9a5SSteve French 	}
327238c8a9a5SSteve French 
327338c8a9a5SSteve French 	if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
327438c8a9a5SSteve French 		__u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3275da70c1d3SPierre Mariani 
327638c8a9a5SSteve French 		cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
327738c8a9a5SSteve French 		/*
327838c8a9a5SSteve French 		 * check for reconnect case in which we do not
327938c8a9a5SSteve French 		 * want to change the mount behavior if we can avoid it
328038c8a9a5SSteve French 		 */
328138c8a9a5SSteve French 		if (ctx == NULL) {
328238c8a9a5SSteve French 			/*
328338c8a9a5SSteve French 			 * turn off POSIX ACL and PATHNAMES if not set
328438c8a9a5SSteve French 			 * originally at mount time
328538c8a9a5SSteve French 			 */
328638c8a9a5SSteve French 			if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
328738c8a9a5SSteve French 				cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
328838c8a9a5SSteve French 			if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
328938c8a9a5SSteve French 				if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
329038c8a9a5SSteve French 					cifs_dbg(VFS, "POSIXPATH support change\n");
329138c8a9a5SSteve French 				cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
329238c8a9a5SSteve French 			} else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
329338c8a9a5SSteve French 				cifs_dbg(VFS, "possible reconnect error\n");
329438c8a9a5SSteve French 				cifs_dbg(VFS, "server disabled POSIX path support\n");
329538c8a9a5SSteve French 			}
329638c8a9a5SSteve French 		}
329738c8a9a5SSteve French 
329838c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
329938c8a9a5SSteve French 			cifs_dbg(VFS, "per-share encryption not supported yet\n");
330038c8a9a5SSteve French 
330138c8a9a5SSteve French 		cap &= CIFS_UNIX_CAP_MASK;
330238c8a9a5SSteve French 		if (ctx && ctx->no_psx_acl)
330338c8a9a5SSteve French 			cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
330438c8a9a5SSteve French 		else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
330538c8a9a5SSteve French 			cifs_dbg(FYI, "negotiated posix acl support\n");
330638c8a9a5SSteve French 			if (cifs_sb)
330738c8a9a5SSteve French 				cifs_sb->mnt_cifs_flags |=
330838c8a9a5SSteve French 					CIFS_MOUNT_POSIXACL;
330938c8a9a5SSteve French 		}
331038c8a9a5SSteve French 
331138c8a9a5SSteve French 		if (ctx && ctx->posix_paths == 0)
331238c8a9a5SSteve French 			cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
331338c8a9a5SSteve French 		else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
331438c8a9a5SSteve French 			cifs_dbg(FYI, "negotiate posix pathnames\n");
331538c8a9a5SSteve French 			if (cifs_sb)
331638c8a9a5SSteve French 				cifs_sb->mnt_cifs_flags |=
331738c8a9a5SSteve French 					CIFS_MOUNT_POSIX_PATHS;
331838c8a9a5SSteve French 		}
331938c8a9a5SSteve French 
332038c8a9a5SSteve French 		cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
332138c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2
332238c8a9a5SSteve French 		if (cap & CIFS_UNIX_FCNTL_CAP)
332338c8a9a5SSteve French 			cifs_dbg(FYI, "FCNTL cap\n");
332438c8a9a5SSteve French 		if (cap & CIFS_UNIX_EXTATTR_CAP)
332538c8a9a5SSteve French 			cifs_dbg(FYI, "EXTATTR cap\n");
332638c8a9a5SSteve French 		if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
332738c8a9a5SSteve French 			cifs_dbg(FYI, "POSIX path cap\n");
332838c8a9a5SSteve French 		if (cap & CIFS_UNIX_XATTR_CAP)
332938c8a9a5SSteve French 			cifs_dbg(FYI, "XATTR cap\n");
333038c8a9a5SSteve French 		if (cap & CIFS_UNIX_POSIX_ACL_CAP)
333138c8a9a5SSteve French 			cifs_dbg(FYI, "POSIX ACL cap\n");
333238c8a9a5SSteve French 		if (cap & CIFS_UNIX_LARGE_READ_CAP)
333338c8a9a5SSteve French 			cifs_dbg(FYI, "very large read cap\n");
333438c8a9a5SSteve French 		if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
333538c8a9a5SSteve French 			cifs_dbg(FYI, "very large write cap\n");
333638c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
333738c8a9a5SSteve French 			cifs_dbg(FYI, "transport encryption cap\n");
333838c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
333938c8a9a5SSteve French 			cifs_dbg(FYI, "mandatory transport encryption cap\n");
334038c8a9a5SSteve French #endif /* CIFS_DEBUG2 */
334138c8a9a5SSteve French 		if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
334238c8a9a5SSteve French 			if (ctx == NULL)
334338c8a9a5SSteve French 				cifs_dbg(FYI, "resetting capabilities failed\n");
334438c8a9a5SSteve French 			else
334538c8a9a5SSteve French 				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");
334638c8a9a5SSteve French 
334738c8a9a5SSteve French 		}
334838c8a9a5SSteve French 	}
334938c8a9a5SSteve French }
335038c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
335138c8a9a5SSteve French 
335238c8a9a5SSteve French int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
cifs_setup_cifs_sb(struct cifs_sb_info * cifs_sb)335338c8a9a5SSteve French {
335438c8a9a5SSteve French 	struct smb3_fs_context *ctx = cifs_sb->ctx;
335538c8a9a5SSteve French 
335638c8a9a5SSteve French 	INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
335738c8a9a5SSteve French 
335838c8a9a5SSteve French 	spin_lock_init(&cifs_sb->tlink_tree_lock);
335938c8a9a5SSteve French 	cifs_sb->tlink_tree = RB_ROOT;
336038c8a9a5SSteve French 
336138c8a9a5SSteve French 	cifs_dbg(FYI, "file mode: %04ho  dir mode: %04ho\n",
336238c8a9a5SSteve French 		 ctx->file_mode, ctx->dir_mode);
336338c8a9a5SSteve French 
336438c8a9a5SSteve French 	/* this is needed for ASCII cp to Unicode converts */
336538c8a9a5SSteve French 	if (ctx->iocharset == NULL) {
336638c8a9a5SSteve French 		/* load_nls_default cannot return null */
336738c8a9a5SSteve French 		cifs_sb->local_nls = load_nls_default();
336838c8a9a5SSteve French 	} else {
336938c8a9a5SSteve French 		cifs_sb->local_nls = load_nls(ctx->iocharset);
337038c8a9a5SSteve French 		if (cifs_sb->local_nls == NULL) {
337138c8a9a5SSteve French 			cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
337238c8a9a5SSteve French 				 ctx->iocharset);
337338c8a9a5SSteve French 			return -ELIBACC;
337438c8a9a5SSteve French 		}
337538c8a9a5SSteve French 	}
337638c8a9a5SSteve French 	ctx->local_nls = cifs_sb->local_nls;
337738c8a9a5SSteve French 
337838c8a9a5SSteve French 	smb3_update_mnt_flags(cifs_sb);
337938c8a9a5SSteve French 
338038c8a9a5SSteve French 	if (ctx->direct_io)
338138c8a9a5SSteve French 		cifs_dbg(FYI, "mounting share using direct i/o\n");
338238c8a9a5SSteve French 	if (ctx->cache_ro) {
338338c8a9a5SSteve French 		cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
338438c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
338538c8a9a5SSteve French 	} else if (ctx->cache_rw) {
338638c8a9a5SSteve French 		cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
338738c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
338838c8a9a5SSteve French 					    CIFS_MOUNT_RW_CACHE);
338938c8a9a5SSteve French 	}
339038c8a9a5SSteve French 
339138c8a9a5SSteve French 	if ((ctx->cifs_acl) && (ctx->dynperm))
339238c8a9a5SSteve French 		cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
339338c8a9a5SSteve French 
339438c8a9a5SSteve French 	if (ctx->prepath) {
339538c8a9a5SSteve French 		cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
339638c8a9a5SSteve French 		if (cifs_sb->prepath == NULL)
339738c8a9a5SSteve French 			return -ENOMEM;
339838c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
339938c8a9a5SSteve French 	}
340038c8a9a5SSteve French 
340138c8a9a5SSteve French 	return 0;
340238c8a9a5SSteve French }
340338c8a9a5SSteve French 
340438c8a9a5SSteve French /* Release all succeed connections */
340538c8a9a5SSteve French void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx)
cifs_mount_put_conns(struct cifs_mount_ctx * mnt_ctx)340638c8a9a5SSteve French {
340738c8a9a5SSteve French 	int rc = 0;
340838c8a9a5SSteve French 
340938c8a9a5SSteve French 	if (mnt_ctx->tcon)
341007e76ea1SDavid Howells 		cifs_put_tcon(mnt_ctx->tcon, netfs_trace_tcon_ref_put_mnt_ctx);
341138c8a9a5SSteve French 	else if (mnt_ctx->ses)
341238c8a9a5SSteve French 		cifs_put_smb_ses(mnt_ctx->ses);
341338c8a9a5SSteve French 	else if (mnt_ctx->server)
341438c8a9a5SSteve French 		cifs_put_tcp_session(mnt_ctx->server, 0);
3415645f332cSPaulo Alcantara 	mnt_ctx->ses = NULL;
3416645f332cSPaulo Alcantara 	mnt_ctx->tcon = NULL;
3417645f332cSPaulo Alcantara 	mnt_ctx->server = NULL;
341838c8a9a5SSteve French 	mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
341938c8a9a5SSteve French 	free_xid(mnt_ctx->xid);
342038c8a9a5SSteve French }
342138c8a9a5SSteve French 
342238c8a9a5SSteve French int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx)
cifs_mount_get_session(struct cifs_mount_ctx * mnt_ctx)342338c8a9a5SSteve French {
342438c8a9a5SSteve French 	struct TCP_Server_Info *server = NULL;
342538c8a9a5SSteve French 	struct smb3_fs_context *ctx;
342638c8a9a5SSteve French 	struct cifs_ses *ses = NULL;
342738c8a9a5SSteve French 	unsigned int xid;
342838c8a9a5SSteve French 	int rc = 0;
342938c8a9a5SSteve French 
343038c8a9a5SSteve French 	xid = get_xid();
343138c8a9a5SSteve French 
343238c8a9a5SSteve French 	if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->fs_ctx)) {
343338c8a9a5SSteve French 		rc = -EINVAL;
343438c8a9a5SSteve French 		goto out;
343538c8a9a5SSteve French 	}
343638c8a9a5SSteve French 	ctx = mnt_ctx->fs_ctx;
343738c8a9a5SSteve French 
343838c8a9a5SSteve French 	/* get a reference to a tcp session */
343938c8a9a5SSteve French 	server = cifs_get_tcp_session(ctx, NULL);
344038c8a9a5SSteve French 	if (IS_ERR(server)) {
344138c8a9a5SSteve French 		rc = PTR_ERR(server);
344238c8a9a5SSteve French 		server = NULL;
344338c8a9a5SSteve French 		goto out;
344438c8a9a5SSteve French 	}
344538c8a9a5SSteve French 
344638c8a9a5SSteve French 	/* get a reference to a SMB session */
344738c8a9a5SSteve French 	ses = cifs_get_smb_ses(server, ctx);
344838c8a9a5SSteve French 	if (IS_ERR(ses)) {
344938c8a9a5SSteve French 		rc = PTR_ERR(ses);
345038c8a9a5SSteve French 		ses = NULL;
345138c8a9a5SSteve French 		goto out;
345238c8a9a5SSteve French 	}
345338c8a9a5SSteve French 
345438c8a9a5SSteve French 	if ((ctx->persistent == true) && (!(ses->server->capabilities &
345538c8a9a5SSteve French 					    SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
345638c8a9a5SSteve French 		cifs_server_dbg(VFS, "persistent handles not supported by server\n");
345738c8a9a5SSteve French 		rc = -EOPNOTSUPP;
345838c8a9a5SSteve French 	}
345938c8a9a5SSteve French 
346038c8a9a5SSteve French out:
346138c8a9a5SSteve French 	mnt_ctx->xid = xid;
346238c8a9a5SSteve French 	mnt_ctx->server = server;
346338c8a9a5SSteve French 	mnt_ctx->ses = ses;
346438c8a9a5SSteve French 	mnt_ctx->tcon = NULL;
346538c8a9a5SSteve French 
346638c8a9a5SSteve French 	return rc;
346738c8a9a5SSteve French }
346838c8a9a5SSteve French 
346938c8a9a5SSteve French int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx)
cifs_mount_get_tcon(struct cifs_mount_ctx * mnt_ctx)347038c8a9a5SSteve French {
347138c8a9a5SSteve French 	struct TCP_Server_Info *server;
347238c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb;
347338c8a9a5SSteve French 	struct smb3_fs_context *ctx;
347438c8a9a5SSteve French 	struct cifs_tcon *tcon = NULL;
347538c8a9a5SSteve French 	int rc = 0;
347638c8a9a5SSteve French 
347738c8a9a5SSteve French 	if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->server || !mnt_ctx->ses || !mnt_ctx->fs_ctx ||
347838c8a9a5SSteve French 			 !mnt_ctx->cifs_sb)) {
347938c8a9a5SSteve French 		rc = -EINVAL;
348038c8a9a5SSteve French 		goto out;
348138c8a9a5SSteve French 	}
348238c8a9a5SSteve French 	server = mnt_ctx->server;
348338c8a9a5SSteve French 	ctx = mnt_ctx->fs_ctx;
348438c8a9a5SSteve French 	cifs_sb = mnt_ctx->cifs_sb;
348538c8a9a5SSteve French 
348638c8a9a5SSteve French 	/* search for existing tcon to this server share */
348738c8a9a5SSteve French 	tcon = cifs_get_tcon(mnt_ctx->ses, ctx);
348838c8a9a5SSteve French 	if (IS_ERR(tcon)) {
348938c8a9a5SSteve French 		rc = PTR_ERR(tcon);
349038c8a9a5SSteve French 		tcon = NULL;
349138c8a9a5SSteve French 		goto out;
349238c8a9a5SSteve French 	}
349338c8a9a5SSteve French 
349438c8a9a5SSteve French 	/* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
349538c8a9a5SSteve French 	if (tcon->posix_extensions)
349638c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
349738c8a9a5SSteve French 
349838c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
349938c8a9a5SSteve French 	/* tell server which Unix caps we support */
350038c8a9a5SSteve French 	if (cap_unix(tcon->ses)) {
350138c8a9a5SSteve French 		/*
350238c8a9a5SSteve French 		 * reset of caps checks mount to see if unix extensions disabled
350338c8a9a5SSteve French 		 * for just this mount.
350438c8a9a5SSteve French 		 */
350538c8a9a5SSteve French 		reset_cifs_unix_caps(mnt_ctx->xid, tcon, cifs_sb, ctx);
350638c8a9a5SSteve French 		spin_lock(&tcon->ses->server->srv_lock);
350738c8a9a5SSteve French 		if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
350838c8a9a5SSteve French 		    (le64_to_cpu(tcon->fsUnixInfo.Capability) &
350938c8a9a5SSteve French 		     CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
351038c8a9a5SSteve French 			spin_unlock(&tcon->ses->server->srv_lock);
351138c8a9a5SSteve French 			rc = -EACCES;
351238c8a9a5SSteve French 			goto out;
351338c8a9a5SSteve French 		}
351438c8a9a5SSteve French 		spin_unlock(&tcon->ses->server->srv_lock);
351538c8a9a5SSteve French 	} else
351638c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
351738c8a9a5SSteve French 		tcon->unix_ext = 0; /* server does not support them */
351838c8a9a5SSteve French 
351938c8a9a5SSteve French 	/* do not care if a following call succeed - informational */
352038c8a9a5SSteve French 	if (!tcon->pipe && server->ops->qfs_tcon) {
352138c8a9a5SSteve French 		server->ops->qfs_tcon(mnt_ctx->xid, tcon, cifs_sb);
352238c8a9a5SSteve French 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
352338c8a9a5SSteve French 			if (tcon->fsDevInfo.DeviceCharacteristics &
352438c8a9a5SSteve French 			    cpu_to_le32(FILE_READ_ONLY_DEVICE))
352538c8a9a5SSteve French 				cifs_dbg(VFS, "mounted to read only share\n");
352638c8a9a5SSteve French 			else if ((cifs_sb->mnt_cifs_flags &
352738c8a9a5SSteve French 				  CIFS_MOUNT_RW_CACHE) == 0)
352838c8a9a5SSteve French 				cifs_dbg(VFS, "read only mount of RW share\n");
352938c8a9a5SSteve French 			/* no need to log a RW mount of a typical RW share */
353038c8a9a5SSteve French 		}
353138c8a9a5SSteve French 	}
353238c8a9a5SSteve French 
353338c8a9a5SSteve French 	/*
353438c8a9a5SSteve French 	 * Clamp the rsize/wsize mount arguments if they are too big for the server
353538c8a9a5SSteve French 	 * and set the rsize/wsize to the negotiated values if not passed in by
353638c8a9a5SSteve French 	 * the user on mount
353738c8a9a5SSteve French 	 */
353838c8a9a5SSteve French 	if ((cifs_sb->ctx->wsize == 0) ||
35394145ccffSSteve French 	    (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx))) {
35404145ccffSSteve French 		cifs_sb->ctx->wsize =
35414145ccffSSteve French 			round_down(server->ops->negotiate_wsize(tcon, ctx), PAGE_SIZE);
35424145ccffSSteve French 		/*
35434145ccffSSteve French 		 * in the very unlikely event that the server sent a max write size under PAGE_SIZE,
35444145ccffSSteve French 		 * (which would get rounded down to 0) then reset wsize to absolute minimum eg 4096
35454145ccffSSteve French 		 */
35464145ccffSSteve French 		if (cifs_sb->ctx->wsize == 0) {
35474145ccffSSteve French 			cifs_sb->ctx->wsize = PAGE_SIZE;
35484145ccffSSteve French 			cifs_dbg(VFS, "wsize too small, reset to minimum ie PAGE_SIZE, usually 4096\n");
35494145ccffSSteve French 		}
35504145ccffSSteve French 	}
355138c8a9a5SSteve French 	if ((cifs_sb->ctx->rsize == 0) ||
355238c8a9a5SSteve French 	    (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
355338c8a9a5SSteve French 		cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
355438c8a9a5SSteve French 
355538c8a9a5SSteve French 	/*
355638c8a9a5SSteve French 	 * The cookie is initialized from volume info returned above.
355738c8a9a5SSteve French 	 * Inside cifs_fscache_get_super_cookie it checks
355838c8a9a5SSteve French 	 * that we do not get super cookie twice.
355938c8a9a5SSteve French 	 */
356038c8a9a5SSteve French 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
356138c8a9a5SSteve French 		cifs_fscache_get_super_cookie(tcon);
356238c8a9a5SSteve French 
356338c8a9a5SSteve French out:
356438c8a9a5SSteve French 	mnt_ctx->tcon = tcon;
356538c8a9a5SSteve French 	return rc;
356638c8a9a5SSteve French }
356738c8a9a5SSteve French 
356838c8a9a5SSteve French static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
mount_setup_tlink(struct cifs_sb_info * cifs_sb,struct cifs_ses * ses,struct cifs_tcon * tcon)356938c8a9a5SSteve French 			     struct cifs_tcon *tcon)
357038c8a9a5SSteve French {
357138c8a9a5SSteve French 	struct tcon_link *tlink;
357238c8a9a5SSteve French 
357338c8a9a5SSteve French 	/* hang the tcon off of the superblock */
357438c8a9a5SSteve French 	tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
357538c8a9a5SSteve French 	if (tlink == NULL)
357638c8a9a5SSteve French 		return -ENOMEM;
357738c8a9a5SSteve French 
357838c8a9a5SSteve French 	tlink->tl_uid = ses->linux_uid;
357938c8a9a5SSteve French 	tlink->tl_tcon = tcon;
358038c8a9a5SSteve French 	tlink->tl_time = jiffies;
358138c8a9a5SSteve French 	set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
358238c8a9a5SSteve French 	set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
358338c8a9a5SSteve French 
358438c8a9a5SSteve French 	cifs_sb->master_tlink = tlink;
358538c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
358638c8a9a5SSteve French 	tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
358738c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
358838c8a9a5SSteve French 
358938c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
359038c8a9a5SSteve French 				TLINK_IDLE_EXPIRE);
359138c8a9a5SSteve French 	return 0;
359238c8a9a5SSteve French }
359338c8a9a5SSteve French 
359438c8a9a5SSteve French static int
359538c8a9a5SSteve French cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
cifs_are_all_path_components_accessible(struct TCP_Server_Info * server,unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,char * full_path,int added_treename)359638c8a9a5SSteve French 					unsigned int xid,
359738c8a9a5SSteve French 					struct cifs_tcon *tcon,
359838c8a9a5SSteve French 					struct cifs_sb_info *cifs_sb,
359938c8a9a5SSteve French 					char *full_path,
360038c8a9a5SSteve French 					int added_treename)
360138c8a9a5SSteve French {
360238c8a9a5SSteve French 	int rc;
360338c8a9a5SSteve French 	char *s;
360438c8a9a5SSteve French 	char sep, tmp;
360538c8a9a5SSteve French 	int skip = added_treename ? 1 : 0;
360638c8a9a5SSteve French 
360738c8a9a5SSteve French 	sep = CIFS_DIR_SEP(cifs_sb);
360838c8a9a5SSteve French 	s = full_path;
360938c8a9a5SSteve French 
361038c8a9a5SSteve French 	rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
361138c8a9a5SSteve French 	while (rc == 0) {
361238c8a9a5SSteve French 		/* skip separators */
361338c8a9a5SSteve French 		while (*s == sep)
361438c8a9a5SSteve French 			s++;
361538c8a9a5SSteve French 		if (!*s)
361638c8a9a5SSteve French 			break;
361738c8a9a5SSteve French 		/* next separator */
361838c8a9a5SSteve French 		while (*s && *s != sep)
361938c8a9a5SSteve French 			s++;
362038c8a9a5SSteve French 		/*
362138c8a9a5SSteve French 		 * if the treename is added, we then have to skip the first
362238c8a9a5SSteve French 		 * part within the separators
362338c8a9a5SSteve French 		 */
362438c8a9a5SSteve French 		if (skip) {
362538c8a9a5SSteve French 			skip = 0;
362638c8a9a5SSteve French 			continue;
362738c8a9a5SSteve French 		}
362838c8a9a5SSteve French 		/*
362938c8a9a5SSteve French 		 * temporarily null-terminate the path at the end of
363038c8a9a5SSteve French 		 * the current component
363138c8a9a5SSteve French 		 */
363238c8a9a5SSteve French 		tmp = *s;
363338c8a9a5SSteve French 		*s = 0;
363438c8a9a5SSteve French 		rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
363538c8a9a5SSteve French 						     full_path);
363638c8a9a5SSteve French 		*s = tmp;
363738c8a9a5SSteve French 	}
363838c8a9a5SSteve French 	return rc;
363938c8a9a5SSteve French }
364038c8a9a5SSteve French 
364138c8a9a5SSteve French /*
364238c8a9a5SSteve French  * Check if path is remote (i.e. a DFS share).
364338c8a9a5SSteve French  *
364438c8a9a5SSteve French  * Return -EREMOTE if it is, otherwise 0 or -errno.
364538c8a9a5SSteve French  */
364638c8a9a5SSteve French int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx)
cifs_is_path_remote(struct cifs_mount_ctx * mnt_ctx)364738c8a9a5SSteve French {
364838c8a9a5SSteve French 	int rc;
364938c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
365038c8a9a5SSteve French 	struct TCP_Server_Info *server = mnt_ctx->server;
365138c8a9a5SSteve French 	unsigned int xid = mnt_ctx->xid;
365238c8a9a5SSteve French 	struct cifs_tcon *tcon = mnt_ctx->tcon;
365338c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
365438c8a9a5SSteve French 	char *full_path;
365538c8a9a5SSteve French 
365638c8a9a5SSteve French 	if (!server->ops->is_path_accessible)
365738c8a9a5SSteve French 		return -EOPNOTSUPP;
365838c8a9a5SSteve French 
365938c8a9a5SSteve French 	/*
366038c8a9a5SSteve French 	 * cifs_build_path_to_root works only when we have a valid tcon
366138c8a9a5SSteve French 	 */
366238c8a9a5SSteve French 	full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
366338c8a9a5SSteve French 					    tcon->Flags & SMB_SHARE_IS_IN_DFS);
366438c8a9a5SSteve French 	if (full_path == NULL)
366538c8a9a5SSteve French 		return -ENOMEM;
366638c8a9a5SSteve French 
366738c8a9a5SSteve French 	cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
366838c8a9a5SSteve French 
366938c8a9a5SSteve French 	rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
367038c8a9a5SSteve French 					     full_path);
367138c8a9a5SSteve French 	if (rc != 0 && rc != -EREMOTE)
367238c8a9a5SSteve French 		goto out;
367338c8a9a5SSteve French 
367438c8a9a5SSteve French 	if (rc != -EREMOTE) {
367538c8a9a5SSteve French 		rc = cifs_are_all_path_components_accessible(server, xid, tcon,
367638c8a9a5SSteve French 			cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
367738c8a9a5SSteve French 		if (rc != 0) {
367838c8a9a5SSteve French 			cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
367938c8a9a5SSteve French 			cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
368038c8a9a5SSteve French 			rc = 0;
368138c8a9a5SSteve French 		}
368238c8a9a5SSteve French 	}
368338c8a9a5SSteve French 
368438c8a9a5SSteve French out:
368538c8a9a5SSteve French 	kfree(full_path);
368638c8a9a5SSteve French 	return rc;
368738c8a9a5SSteve French }
368838c8a9a5SSteve French 
368938c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
369038c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
cifs_mount(struct cifs_sb_info * cifs_sb,struct smb3_fs_context * ctx)369138c8a9a5SSteve French {
369238c8a9a5SSteve French 	struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
369338c8a9a5SSteve French 	bool isdfs;
369438c8a9a5SSteve French 	int rc;
369538c8a9a5SSteve French 
369638c8a9a5SSteve French 	rc = dfs_mount_share(&mnt_ctx, &isdfs);
369738c8a9a5SSteve French 	if (rc)
369838c8a9a5SSteve French 		goto error;
369938c8a9a5SSteve French 	if (!isdfs)
370038c8a9a5SSteve French 		goto out;
370138c8a9a5SSteve French 
370238c8a9a5SSteve French 	/*
370338c8a9a5SSteve French 	 * After reconnecting to a different server, unique ids won't match anymore, so we disable
370438c8a9a5SSteve French 	 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
370538c8a9a5SSteve French 	 */
370638c8a9a5SSteve French 	cifs_autodisable_serverino(cifs_sb);
370738c8a9a5SSteve French 	/*
370838c8a9a5SSteve French 	 * Force the use of prefix path to support failover on DFS paths that resolve to targets
370938c8a9a5SSteve French 	 * that have different prefix paths.
371038c8a9a5SSteve French 	 */
371138c8a9a5SSteve French 	cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
371238c8a9a5SSteve French 	kfree(cifs_sb->prepath);
371338c8a9a5SSteve French 	cifs_sb->prepath = ctx->prepath;
371438c8a9a5SSteve French 	ctx->prepath = NULL;
371538c8a9a5SSteve French 
371638c8a9a5SSteve French out:
3717739bf98cSShyam Prasad N 	cifs_try_adding_channels(mnt_ctx.ses);
371838c8a9a5SSteve French 	rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
371938c8a9a5SSteve French 	if (rc)
372038c8a9a5SSteve French 		goto error;
372138c8a9a5SSteve French 
372238c8a9a5SSteve French 	free_xid(mnt_ctx.xid);
372338c8a9a5SSteve French 	return rc;
372438c8a9a5SSteve French 
372538c8a9a5SSteve French error:
372638c8a9a5SSteve French 	cifs_mount_put_conns(&mnt_ctx);
372738c8a9a5SSteve French 	return rc;
372838c8a9a5SSteve French }
372938c8a9a5SSteve French #else
373038c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
cifs_mount(struct cifs_sb_info * cifs_sb,struct smb3_fs_context * ctx)373138c8a9a5SSteve French {
373238c8a9a5SSteve French 	int rc = 0;
373338c8a9a5SSteve French 	struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
373438c8a9a5SSteve French 
373538c8a9a5SSteve French 	rc = cifs_mount_get_session(&mnt_ctx);
373638c8a9a5SSteve French 	if (rc)
373738c8a9a5SSteve French 		goto error;
373838c8a9a5SSteve French 
373938c8a9a5SSteve French 	rc = cifs_mount_get_tcon(&mnt_ctx);
3740645f332cSPaulo Alcantara 	if (!rc) {
3741645f332cSPaulo Alcantara 		/*
3742645f332cSPaulo Alcantara 		 * Prevent superblock from being created with any missing
3743645f332cSPaulo Alcantara 		 * connections.
3744645f332cSPaulo Alcantara 		 */
3745645f332cSPaulo Alcantara 		if (WARN_ON(!mnt_ctx.server))
3746645f332cSPaulo Alcantara 			rc = -EHOSTDOWN;
3747645f332cSPaulo Alcantara 		else if (WARN_ON(!mnt_ctx.ses))
3748645f332cSPaulo Alcantara 			rc = -EACCES;
3749645f332cSPaulo Alcantara 		else if (WARN_ON(!mnt_ctx.tcon))
3750645f332cSPaulo Alcantara 			rc = -ENOENT;
3751645f332cSPaulo Alcantara 	}
375238c8a9a5SSteve French 	if (rc)
375338c8a9a5SSteve French 		goto error;
375438c8a9a5SSteve French 
375538c8a9a5SSteve French 	rc = cifs_is_path_remote(&mnt_ctx);
375638c8a9a5SSteve French 	if (rc == -EREMOTE)
375738c8a9a5SSteve French 		rc = -EOPNOTSUPP;
375838c8a9a5SSteve French 	if (rc)
375938c8a9a5SSteve French 		goto error;
376038c8a9a5SSteve French 
376138c8a9a5SSteve French 	rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
376238c8a9a5SSteve French 	if (rc)
376338c8a9a5SSteve French 		goto error;
376438c8a9a5SSteve French 
376538c8a9a5SSteve French 	free_xid(mnt_ctx.xid);
376638c8a9a5SSteve French 	return rc;
376738c8a9a5SSteve French 
376838c8a9a5SSteve French error:
376938c8a9a5SSteve French 	cifs_mount_put_conns(&mnt_ctx);
377038c8a9a5SSteve French 	return rc;
377138c8a9a5SSteve French }
377238c8a9a5SSteve French #endif
377338c8a9a5SSteve French 
377431553d20SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
377538c8a9a5SSteve French /*
377638c8a9a5SSteve French  * Issue a TREE_CONNECT request.
377738c8a9a5SSteve French  */
377838c8a9a5SSteve French int
377938c8a9a5SSteve French CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
CIFSTCon(const unsigned int xid,struct cifs_ses * ses,const char * tree,struct cifs_tcon * tcon,const struct nls_table * nls_codepage)378038c8a9a5SSteve French 	 const char *tree, struct cifs_tcon *tcon,
378138c8a9a5SSteve French 	 const struct nls_table *nls_codepage)
378238c8a9a5SSteve French {
378338c8a9a5SSteve French 	struct smb_hdr *smb_buffer;
378438c8a9a5SSteve French 	struct smb_hdr *smb_buffer_response;
378538c8a9a5SSteve French 	TCONX_REQ *pSMB;
378638c8a9a5SSteve French 	TCONX_RSP *pSMBr;
378738c8a9a5SSteve French 	unsigned char *bcc_ptr;
378838c8a9a5SSteve French 	int rc = 0;
378938c8a9a5SSteve French 	int length;
379038c8a9a5SSteve French 	__u16 bytes_left, count;
379138c8a9a5SSteve French 
379238c8a9a5SSteve French 	if (ses == NULL)
379338c8a9a5SSteve French 		return -EIO;
379438c8a9a5SSteve French 
379538c8a9a5SSteve French 	smb_buffer = cifs_buf_get();
379638c8a9a5SSteve French 	if (smb_buffer == NULL)
379738c8a9a5SSteve French 		return -ENOMEM;
379838c8a9a5SSteve French 
379938c8a9a5SSteve French 	smb_buffer_response = smb_buffer;
380038c8a9a5SSteve French 
380138c8a9a5SSteve French 	header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
380238c8a9a5SSteve French 			NULL /*no tid */, 4 /*wct */);
380338c8a9a5SSteve French 
380438c8a9a5SSteve French 	smb_buffer->Mid = get_next_mid(ses->server);
380538c8a9a5SSteve French 	smb_buffer->Uid = ses->Suid;
380638c8a9a5SSteve French 	pSMB = (TCONX_REQ *) smb_buffer;
380738c8a9a5SSteve French 	pSMBr = (TCONX_RSP *) smb_buffer_response;
380838c8a9a5SSteve French 
380938c8a9a5SSteve French 	pSMB->AndXCommand = 0xFF;
381038c8a9a5SSteve French 	pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
381138c8a9a5SSteve French 	bcc_ptr = &pSMB->Password[0];
381238c8a9a5SSteve French 
381338c8a9a5SSteve French 	pSMB->PasswordLength = cpu_to_le16(1);	/* minimum */
381438c8a9a5SSteve French 	*bcc_ptr = 0; /* password is null byte */
381538c8a9a5SSteve French 	bcc_ptr++;              /* skip password */
381638c8a9a5SSteve French 	/* already aligned so no need to do it below */
381738c8a9a5SSteve French 
381838c8a9a5SSteve French 	if (ses->server->sign)
381938c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
382038c8a9a5SSteve French 
3821da70c1d3SPierre Mariani 	if (ses->capabilities & CAP_STATUS32)
382238c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3823da70c1d3SPierre Mariani 
3824da70c1d3SPierre Mariani 	if (ses->capabilities & CAP_DFS)
382538c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_DFS;
3826da70c1d3SPierre Mariani 
382738c8a9a5SSteve French 	if (ses->capabilities & CAP_UNICODE) {
382838c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_UNICODE;
382938c8a9a5SSteve French 		length =
383038c8a9a5SSteve French 		    cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
383138c8a9a5SSteve French 			6 /* max utf8 char length in bytes */ *
383238c8a9a5SSteve French 			(/* server len*/ + 256 /* share len */), nls_codepage);
383338c8a9a5SSteve French 		bcc_ptr += 2 * length;	/* convert num 16 bit words to bytes */
383438c8a9a5SSteve French 		bcc_ptr += 2;	/* skip trailing null */
383538c8a9a5SSteve French 	} else {		/* ASCII */
383638c8a9a5SSteve French 		strcpy(bcc_ptr, tree);
383738c8a9a5SSteve French 		bcc_ptr += strlen(tree) + 1;
383838c8a9a5SSteve French 	}
383938c8a9a5SSteve French 	strcpy(bcc_ptr, "?????");
384038c8a9a5SSteve French 	bcc_ptr += strlen("?????");
384138c8a9a5SSteve French 	bcc_ptr += 1;
384238c8a9a5SSteve French 	count = bcc_ptr - &pSMB->Password[0];
384338c8a9a5SSteve French 	be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
384438c8a9a5SSteve French 	pSMB->ByteCount = cpu_to_le16(count);
384538c8a9a5SSteve French 
384638c8a9a5SSteve French 	rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
384738c8a9a5SSteve French 			 0);
384838c8a9a5SSteve French 
384938c8a9a5SSteve French 	/* above now done in SendReceive */
385038c8a9a5SSteve French 	if (rc == 0) {
385138c8a9a5SSteve French 		bool is_unicode;
385238c8a9a5SSteve French 
385338c8a9a5SSteve French 		tcon->tid = smb_buffer_response->Tid;
385438c8a9a5SSteve French 		bcc_ptr = pByteArea(smb_buffer_response);
385538c8a9a5SSteve French 		bytes_left = get_bcc(smb_buffer_response);
385638c8a9a5SSteve French 		length = strnlen(bcc_ptr, bytes_left - 2);
385738c8a9a5SSteve French 		if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
385838c8a9a5SSteve French 			is_unicode = true;
385938c8a9a5SSteve French 		else
386038c8a9a5SSteve French 			is_unicode = false;
386138c8a9a5SSteve French 
386238c8a9a5SSteve French 
386338c8a9a5SSteve French 		/* skip service field (NB: this field is always ASCII) */
386438c8a9a5SSteve French 		if (length == 3) {
386538c8a9a5SSteve French 			if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
386638c8a9a5SSteve French 			    (bcc_ptr[2] == 'C')) {
386738c8a9a5SSteve French 				cifs_dbg(FYI, "IPC connection\n");
386838c8a9a5SSteve French 				tcon->ipc = true;
386938c8a9a5SSteve French 				tcon->pipe = true;
387038c8a9a5SSteve French 			}
387138c8a9a5SSteve French 		} else if (length == 2) {
387238c8a9a5SSteve French 			if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
387338c8a9a5SSteve French 				/* the most common case */
387438c8a9a5SSteve French 				cifs_dbg(FYI, "disk share connection\n");
387538c8a9a5SSteve French 			}
387638c8a9a5SSteve French 		}
387738c8a9a5SSteve French 		bcc_ptr += length + 1;
387838c8a9a5SSteve French 		bytes_left -= (length + 1);
387938c8a9a5SSteve French 		strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
388038c8a9a5SSteve French 
388138c8a9a5SSteve French 		/* mostly informational -- no need to fail on error here */
388238c8a9a5SSteve French 		kfree(tcon->nativeFileSystem);
388338c8a9a5SSteve French 		tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
388438c8a9a5SSteve French 						      bytes_left, is_unicode,
388538c8a9a5SSteve French 						      nls_codepage);
388638c8a9a5SSteve French 
388738c8a9a5SSteve French 		cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
388838c8a9a5SSteve French 
388938c8a9a5SSteve French 		if ((smb_buffer_response->WordCount == 3) ||
389038c8a9a5SSteve French 			 (smb_buffer_response->WordCount == 7))
389138c8a9a5SSteve French 			/* field is in same location */
389238c8a9a5SSteve French 			tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
389338c8a9a5SSteve French 		else
389438c8a9a5SSteve French 			tcon->Flags = 0;
389538c8a9a5SSteve French 		cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
389638c8a9a5SSteve French 
389731553d20SSteve French 		/*
389831553d20SSteve French 		 * reset_cifs_unix_caps calls QFSInfo which requires
389931553d20SSteve French 		 * need_reconnect to be false, but we would not need to call
390031553d20SSteve French 		 * reset_caps if this were not a reconnect case so must check
390131553d20SSteve French 		 * need_reconnect flag here.  The caller will also clear
390231553d20SSteve French 		 * need_reconnect when tcon was successful but needed to be
390331553d20SSteve French 		 * cleared earlier in the case of unix extensions reconnect
390431553d20SSteve French 		 */
390531553d20SSteve French 		if (tcon->need_reconnect && tcon->unix_ext) {
390631553d20SSteve French 			cifs_dbg(FYI, "resetting caps for %s\n", tcon->tree_name);
390731553d20SSteve French 			tcon->need_reconnect = false;
390831553d20SSteve French 			reset_cifs_unix_caps(xid, tcon, NULL, NULL);
390931553d20SSteve French 		}
391031553d20SSteve French 	}
391138c8a9a5SSteve French 	cifs_buf_release(smb_buffer);
391238c8a9a5SSteve French 	return rc;
391338c8a9a5SSteve French }
391431553d20SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
391538c8a9a5SSteve French 
391638c8a9a5SSteve French static void delayed_free(struct rcu_head *p)
delayed_free(struct rcu_head * p)391738c8a9a5SSteve French {
391838c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
391938c8a9a5SSteve French 
392038c8a9a5SSteve French 	unload_nls(cifs_sb->local_nls);
392138c8a9a5SSteve French 	smb3_cleanup_fs_context(cifs_sb->ctx);
392238c8a9a5SSteve French 	kfree(cifs_sb);
392338c8a9a5SSteve French }
392438c8a9a5SSteve French 
392538c8a9a5SSteve French void
392638c8a9a5SSteve French cifs_umount(struct cifs_sb_info *cifs_sb)
cifs_umount(struct cifs_sb_info * cifs_sb)392738c8a9a5SSteve French {
392838c8a9a5SSteve French 	struct rb_root *root = &cifs_sb->tlink_tree;
392938c8a9a5SSteve French 	struct rb_node *node;
393038c8a9a5SSteve French 	struct tcon_link *tlink;
393138c8a9a5SSteve French 
393238c8a9a5SSteve French 	cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
393338c8a9a5SSteve French 
393438c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
393538c8a9a5SSteve French 	while ((node = rb_first(root))) {
393638c8a9a5SSteve French 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
393738c8a9a5SSteve French 		cifs_get_tlink(tlink);
393838c8a9a5SSteve French 		clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
393938c8a9a5SSteve French 		rb_erase(node, root);
394038c8a9a5SSteve French 
394138c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
394238c8a9a5SSteve French 		cifs_put_tlink(tlink);
394338c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
394438c8a9a5SSteve French 	}
394538c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
394638c8a9a5SSteve French 
394738c8a9a5SSteve French 	kfree(cifs_sb->prepath);
394838c8a9a5SSteve French 	call_rcu(&cifs_sb->rcu, delayed_free);
394938c8a9a5SSteve French }
395038c8a9a5SSteve French 
395138c8a9a5SSteve French int
395238c8a9a5SSteve French cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
cifs_negotiate_protocol(const unsigned int xid,struct cifs_ses * ses,struct TCP_Server_Info * server)395338c8a9a5SSteve French 			struct TCP_Server_Info *server)
395438c8a9a5SSteve French {
395538c8a9a5SSteve French 	int rc = 0;
395638c8a9a5SSteve French 
395738c8a9a5SSteve French 	if (!server->ops->need_neg || !server->ops->negotiate)
395838c8a9a5SSteve French 		return -ENOSYS;
395938c8a9a5SSteve French 
396038c8a9a5SSteve French 	/* only send once per connect */
396138c8a9a5SSteve French 	spin_lock(&server->srv_lock);
396238c8a9a5SSteve French 	if (server->tcpStatus != CifsGood &&
396338c8a9a5SSteve French 	    server->tcpStatus != CifsNew &&
396438c8a9a5SSteve French 	    server->tcpStatus != CifsNeedNegotiate) {
396538c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
396638c8a9a5SSteve French 		return -EHOSTDOWN;
396738c8a9a5SSteve French 	}
396838c8a9a5SSteve French 
396938c8a9a5SSteve French 	if (!server->ops->need_neg(server) &&
397038c8a9a5SSteve French 	    server->tcpStatus == CifsGood) {
397138c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
397238c8a9a5SSteve French 		return 0;
397338c8a9a5SSteve French 	}
397438c8a9a5SSteve French 
397538c8a9a5SSteve French 	server->tcpStatus = CifsInNegotiate;
397638c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
397738c8a9a5SSteve French 
397838c8a9a5SSteve French 	rc = server->ops->negotiate(xid, ses, server);
397938c8a9a5SSteve French 	if (rc == 0) {
398038c8a9a5SSteve French 		spin_lock(&server->srv_lock);
398138c8a9a5SSteve French 		if (server->tcpStatus == CifsInNegotiate)
398238c8a9a5SSteve French 			server->tcpStatus = CifsGood;
398338c8a9a5SSteve French 		else
398438c8a9a5SSteve French 			rc = -EHOSTDOWN;
398538c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
398638c8a9a5SSteve French 	} else {
398738c8a9a5SSteve French 		spin_lock(&server->srv_lock);
398838c8a9a5SSteve French 		if (server->tcpStatus == CifsInNegotiate)
398938c8a9a5SSteve French 			server->tcpStatus = CifsNeedNegotiate;
399038c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
399138c8a9a5SSteve French 	}
399238c8a9a5SSteve French 
399338c8a9a5SSteve French 	return rc;
399438c8a9a5SSteve French }
399538c8a9a5SSteve French 
399638c8a9a5SSteve French int
399738c8a9a5SSteve French cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
cifs_setup_session(const unsigned int xid,struct cifs_ses * ses,struct TCP_Server_Info * server,struct nls_table * nls_info)399838c8a9a5SSteve French 		   struct TCP_Server_Info *server,
399938c8a9a5SSteve French 		   struct nls_table *nls_info)
400038c8a9a5SSteve French {
400138c8a9a5SSteve French 	int rc = -ENOSYS;
4002b3773b19SSteve French 	struct TCP_Server_Info *pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
400338c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr;
400438c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr;
400538c8a9a5SSteve French 	bool is_binding = false;
400638c8a9a5SSteve French 
400738c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
400838c8a9a5SSteve French 	cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
400938c8a9a5SSteve French 		 __func__, ses->chans_need_reconnect);
401038c8a9a5SSteve French 
401138c8a9a5SSteve French 	if (ses->ses_status != SES_GOOD &&
401238c8a9a5SSteve French 	    ses->ses_status != SES_NEW &&
401338c8a9a5SSteve French 	    ses->ses_status != SES_NEED_RECON) {
401438c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
401538c8a9a5SSteve French 		return -EHOSTDOWN;
401638c8a9a5SSteve French 	}
401738c8a9a5SSteve French 
401838c8a9a5SSteve French 	/* only send once per connect */
401938c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
402038c8a9a5SSteve French 	if (CIFS_ALL_CHANS_GOOD(ses)) {
402138c8a9a5SSteve French 		if (ses->ses_status == SES_NEED_RECON)
402238c8a9a5SSteve French 			ses->ses_status = SES_GOOD;
402338c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
402438c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
402538c8a9a5SSteve French 		return 0;
402638c8a9a5SSteve French 	}
402738c8a9a5SSteve French 
402838c8a9a5SSteve French 	cifs_chan_set_in_reconnect(ses, server);
402938c8a9a5SSteve French 	is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
403038c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
403138c8a9a5SSteve French 
4032aabf4851SShyam Prasad N 	if (!is_binding) {
403338c8a9a5SSteve French 		ses->ses_status = SES_IN_SETUP;
4034aabf4851SShyam Prasad N 
4035aabf4851SShyam Prasad N 		/* force iface_list refresh */
4036aabf4851SShyam Prasad N 		ses->iface_last_update = 0;
4037aabf4851SShyam Prasad N 	}
403838c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
403938c8a9a5SSteve French 
404038c8a9a5SSteve French 	/* update ses ip_addr only for primary chan */
404138c8a9a5SSteve French 	if (server == pserver) {
404238c8a9a5SSteve French 		if (server->dstaddr.ss_family == AF_INET6)
404338c8a9a5SSteve French 			scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr);
404438c8a9a5SSteve French 		else
404538c8a9a5SSteve French 			scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr);
404638c8a9a5SSteve French 	}
404738c8a9a5SSteve French 
404838c8a9a5SSteve French 	if (!is_binding) {
404938c8a9a5SSteve French 		ses->capabilities = server->capabilities;
405038c8a9a5SSteve French 		if (!linuxExtEnabled)
405138c8a9a5SSteve French 			ses->capabilities &= (~server->vals->cap_unix);
405238c8a9a5SSteve French 
405338c8a9a5SSteve French 		if (ses->auth_key.response) {
405438c8a9a5SSteve French 			cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
405538c8a9a5SSteve French 				 ses->auth_key.response);
405638c8a9a5SSteve French 			kfree_sensitive(ses->auth_key.response);
405738c8a9a5SSteve French 			ses->auth_key.response = NULL;
405838c8a9a5SSteve French 			ses->auth_key.len = 0;
405938c8a9a5SSteve French 		}
406038c8a9a5SSteve French 	}
406138c8a9a5SSteve French 
406238c8a9a5SSteve French 	cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
406338c8a9a5SSteve French 		 server->sec_mode, server->capabilities, server->timeAdj);
406438c8a9a5SSteve French 
406538c8a9a5SSteve French 	if (server->ops->sess_setup)
406638c8a9a5SSteve French 		rc = server->ops->sess_setup(xid, ses, server, nls_info);
406738c8a9a5SSteve French 
406838c8a9a5SSteve French 	if (rc) {
406938c8a9a5SSteve French 		cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
407038c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
407138c8a9a5SSteve French 		if (ses->ses_status == SES_IN_SETUP)
407238c8a9a5SSteve French 			ses->ses_status = SES_NEED_RECON;
407338c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
407438c8a9a5SSteve French 		cifs_chan_clear_in_reconnect(ses, server);
407538c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
407638c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
407738c8a9a5SSteve French 	} else {
407838c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
407938c8a9a5SSteve French 		if (ses->ses_status == SES_IN_SETUP)
408038c8a9a5SSteve French 			ses->ses_status = SES_GOOD;
408138c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
408238c8a9a5SSteve French 		cifs_chan_clear_in_reconnect(ses, server);
408338c8a9a5SSteve French 		cifs_chan_clear_need_reconnect(ses, server);
408438c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
408538c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
408638c8a9a5SSteve French 	}
408738c8a9a5SSteve French 
408838c8a9a5SSteve French 	return rc;
408938c8a9a5SSteve French }
409038c8a9a5SSteve French 
409138c8a9a5SSteve French static int
409238c8a9a5SSteve French cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
cifs_set_vol_auth(struct smb3_fs_context * ctx,struct cifs_ses * ses)409338c8a9a5SSteve French {
409438c8a9a5SSteve French 	ctx->sectype = ses->sectype;
409538c8a9a5SSteve French 
409638c8a9a5SSteve French 	/* krb5 is special, since we don't need username or pw */
409738c8a9a5SSteve French 	if (ctx->sectype == Kerberos)
409838c8a9a5SSteve French 		return 0;
409938c8a9a5SSteve French 
410038c8a9a5SSteve French 	return cifs_set_cifscreds(ctx, ses);
410138c8a9a5SSteve French }
410238c8a9a5SSteve French 
410338c8a9a5SSteve French static struct cifs_tcon *
4104ba55f8a9SPaulo Alcantara __cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
__cifs_construct_tcon(struct cifs_sb_info * cifs_sb,kuid_t fsuid)410538c8a9a5SSteve French {
410638c8a9a5SSteve French 	int rc;
410738c8a9a5SSteve French 	struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
410838c8a9a5SSteve French 	struct cifs_ses *ses;
410938c8a9a5SSteve French 	struct cifs_tcon *tcon = NULL;
411038c8a9a5SSteve French 	struct smb3_fs_context *ctx;
41119b2ee27eSPaulo Alcantara 	char *origin_fullpath = NULL;
411238c8a9a5SSteve French 
411338c8a9a5SSteve French 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
411438c8a9a5SSteve French 	if (ctx == NULL)
411538c8a9a5SSteve French 		return ERR_PTR(-ENOMEM);
411638c8a9a5SSteve French 
411738c8a9a5SSteve French 	ctx->local_nls = cifs_sb->local_nls;
411838c8a9a5SSteve French 	ctx->linux_uid = fsuid;
411938c8a9a5SSteve French 	ctx->cred_uid = fsuid;
412038c8a9a5SSteve French 	ctx->UNC = master_tcon->tree_name;
412138c8a9a5SSteve French 	ctx->retry = master_tcon->retry;
412238c8a9a5SSteve French 	ctx->nocase = master_tcon->nocase;
412338c8a9a5SSteve French 	ctx->nohandlecache = master_tcon->nohandlecache;
412438c8a9a5SSteve French 	ctx->local_lease = master_tcon->local_lease;
412538c8a9a5SSteve French 	ctx->no_lease = master_tcon->no_lease;
412638c8a9a5SSteve French 	ctx->resilient = master_tcon->use_resilient;
412738c8a9a5SSteve French 	ctx->persistent = master_tcon->use_persistent;
412838c8a9a5SSteve French 	ctx->handle_timeout = master_tcon->handle_timeout;
412938c8a9a5SSteve French 	ctx->no_linux_ext = !master_tcon->unix_ext;
413038c8a9a5SSteve French 	ctx->linux_ext = master_tcon->posix_extensions;
413138c8a9a5SSteve French 	ctx->sectype = master_tcon->ses->sectype;
413238c8a9a5SSteve French 	ctx->sign = master_tcon->ses->sign;
413338c8a9a5SSteve French 	ctx->seal = master_tcon->seal;
413438c8a9a5SSteve French 	ctx->witness = master_tcon->use_witness;
41359b2ee27eSPaulo Alcantara 	ctx->dfs_root_ses = master_tcon->ses->dfs_root_ses;
413638c8a9a5SSteve French 
413738c8a9a5SSteve French 	rc = cifs_set_vol_auth(ctx, master_tcon->ses);
413838c8a9a5SSteve French 	if (rc) {
413938c8a9a5SSteve French 		tcon = ERR_PTR(rc);
414038c8a9a5SSteve French 		goto out;
414138c8a9a5SSteve French 	}
414238c8a9a5SSteve French 
414338c8a9a5SSteve French 	/* get a reference for the same TCP session */
414438c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
414538c8a9a5SSteve French 	++master_tcon->ses->server->srv_count;
414638c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
414738c8a9a5SSteve French 
414838c8a9a5SSteve French 	ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
414938c8a9a5SSteve French 	if (IS_ERR(ses)) {
415038c8a9a5SSteve French 		tcon = (struct cifs_tcon *)ses;
415138c8a9a5SSteve French 		cifs_put_tcp_session(master_tcon->ses->server, 0);
415238c8a9a5SSteve French 		goto out;
415338c8a9a5SSteve French 	}
415438c8a9a5SSteve French 
41559b2ee27eSPaulo Alcantara #ifdef CONFIG_CIFS_DFS_UPCALL
41569b2ee27eSPaulo Alcantara 	spin_lock(&master_tcon->tc_lock);
41579b2ee27eSPaulo Alcantara 	if (master_tcon->origin_fullpath) {
41589b2ee27eSPaulo Alcantara 		spin_unlock(&master_tcon->tc_lock);
41599b2ee27eSPaulo Alcantara 		origin_fullpath = dfs_get_path(cifs_sb, cifs_sb->ctx->source);
41609b2ee27eSPaulo Alcantara 		if (IS_ERR(origin_fullpath)) {
41619b2ee27eSPaulo Alcantara 			tcon = ERR_CAST(origin_fullpath);
41629b2ee27eSPaulo Alcantara 			origin_fullpath = NULL;
41639b2ee27eSPaulo Alcantara 			cifs_put_smb_ses(ses);
41649b2ee27eSPaulo Alcantara 			goto out;
41659b2ee27eSPaulo Alcantara 		}
41669b2ee27eSPaulo Alcantara 	} else {
41679b2ee27eSPaulo Alcantara 		spin_unlock(&master_tcon->tc_lock);
41689b2ee27eSPaulo Alcantara 	}
41699b2ee27eSPaulo Alcantara #endif
41709b2ee27eSPaulo Alcantara 
417138c8a9a5SSteve French 	tcon = cifs_get_tcon(ses, ctx);
417238c8a9a5SSteve French 	if (IS_ERR(tcon)) {
417338c8a9a5SSteve French 		cifs_put_smb_ses(ses);
417438c8a9a5SSteve French 		goto out;
417538c8a9a5SSteve French 	}
417638c8a9a5SSteve French 
41779b2ee27eSPaulo Alcantara #ifdef CONFIG_CIFS_DFS_UPCALL
41789b2ee27eSPaulo Alcantara 	if (origin_fullpath) {
41799b2ee27eSPaulo Alcantara 		spin_lock(&tcon->tc_lock);
41809b2ee27eSPaulo Alcantara 		tcon->origin_fullpath = origin_fullpath;
41819b2ee27eSPaulo Alcantara 		spin_unlock(&tcon->tc_lock);
41829b2ee27eSPaulo Alcantara 		origin_fullpath = NULL;
41839b2ee27eSPaulo Alcantara 		queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work,
41849b2ee27eSPaulo Alcantara 				   dfs_cache_get_ttl() * HZ);
41859b2ee27eSPaulo Alcantara 	}
41869b2ee27eSPaulo Alcantara #endif
41879b2ee27eSPaulo Alcantara 
418838c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
418938c8a9a5SSteve French 	if (cap_unix(ses))
419038c8a9a5SSteve French 		reset_cifs_unix_caps(0, tcon, NULL, ctx);
419138c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
419238c8a9a5SSteve French 
419338c8a9a5SSteve French out:
419438c8a9a5SSteve French 	kfree(ctx->username);
419538c8a9a5SSteve French 	kfree_sensitive(ctx->password);
41969b2ee27eSPaulo Alcantara 	kfree(origin_fullpath);
419738c8a9a5SSteve French 	kfree(ctx);
419838c8a9a5SSteve French 
419938c8a9a5SSteve French 	return tcon;
420038c8a9a5SSteve French }
420138c8a9a5SSteve French 
4202ba55f8a9SPaulo Alcantara static struct cifs_tcon *
4203ba55f8a9SPaulo Alcantara cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
cifs_construct_tcon(struct cifs_sb_info * cifs_sb,kuid_t fsuid)4204ba55f8a9SPaulo Alcantara {
4205ba55f8a9SPaulo Alcantara 	struct cifs_tcon *ret;
4206ba55f8a9SPaulo Alcantara 
4207ba55f8a9SPaulo Alcantara 	cifs_mount_lock();
4208ba55f8a9SPaulo Alcantara 	ret = __cifs_construct_tcon(cifs_sb, fsuid);
4209ba55f8a9SPaulo Alcantara 	cifs_mount_unlock();
4210ba55f8a9SPaulo Alcantara 	return ret;
4211ba55f8a9SPaulo Alcantara }
4212ba55f8a9SPaulo Alcantara 
421338c8a9a5SSteve French struct cifs_tcon *
421438c8a9a5SSteve French cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
cifs_sb_master_tcon(struct cifs_sb_info * cifs_sb)421538c8a9a5SSteve French {
421638c8a9a5SSteve French 	return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
421738c8a9a5SSteve French }
421838c8a9a5SSteve French 
421938c8a9a5SSteve French /* find and return a tlink with given uid */
422038c8a9a5SSteve French static struct tcon_link *
422138c8a9a5SSteve French tlink_rb_search(struct rb_root *root, kuid_t uid)
tlink_rb_search(struct rb_root * root,kuid_t uid)422238c8a9a5SSteve French {
422338c8a9a5SSteve French 	struct rb_node *node = root->rb_node;
422438c8a9a5SSteve French 	struct tcon_link *tlink;
422538c8a9a5SSteve French 
422638c8a9a5SSteve French 	while (node) {
422738c8a9a5SSteve French 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
422838c8a9a5SSteve French 
422938c8a9a5SSteve French 		if (uid_gt(tlink->tl_uid, uid))
423038c8a9a5SSteve French 			node = node->rb_left;
423138c8a9a5SSteve French 		else if (uid_lt(tlink->tl_uid, uid))
423238c8a9a5SSteve French 			node = node->rb_right;
423338c8a9a5SSteve French 		else
423438c8a9a5SSteve French 			return tlink;
423538c8a9a5SSteve French 	}
423638c8a9a5SSteve French 	return NULL;
423738c8a9a5SSteve French }
423838c8a9a5SSteve French 
423938c8a9a5SSteve French /* insert a tcon_link into the tree */
424038c8a9a5SSteve French static void
424138c8a9a5SSteve French tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
tlink_rb_insert(struct rb_root * root,struct tcon_link * new_tlink)424238c8a9a5SSteve French {
424338c8a9a5SSteve French 	struct rb_node **new = &(root->rb_node), *parent = NULL;
424438c8a9a5SSteve French 	struct tcon_link *tlink;
424538c8a9a5SSteve French 
424638c8a9a5SSteve French 	while (*new) {
424738c8a9a5SSteve French 		tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
424838c8a9a5SSteve French 		parent = *new;
424938c8a9a5SSteve French 
425038c8a9a5SSteve French 		if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
425138c8a9a5SSteve French 			new = &((*new)->rb_left);
425238c8a9a5SSteve French 		else
425338c8a9a5SSteve French 			new = &((*new)->rb_right);
425438c8a9a5SSteve French 	}
425538c8a9a5SSteve French 
425638c8a9a5SSteve French 	rb_link_node(&new_tlink->tl_rbnode, parent, new);
425738c8a9a5SSteve French 	rb_insert_color(&new_tlink->tl_rbnode, root);
425838c8a9a5SSteve French }
425938c8a9a5SSteve French 
426038c8a9a5SSteve French /*
426138c8a9a5SSteve French  * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
426238c8a9a5SSteve French  * current task.
426338c8a9a5SSteve French  *
426438c8a9a5SSteve French  * If the superblock doesn't refer to a multiuser mount, then just return
426538c8a9a5SSteve French  * the master tcon for the mount.
426638c8a9a5SSteve French  *
426738c8a9a5SSteve French  * First, search the rbtree for an existing tcon for this fsuid. If one
426838c8a9a5SSteve French  * exists, then check to see if it's pending construction. If it is then wait
426938c8a9a5SSteve French  * for construction to complete. Once it's no longer pending, check to see if
427038c8a9a5SSteve French  * it failed and either return an error or retry construction, depending on
427138c8a9a5SSteve French  * the timeout.
427238c8a9a5SSteve French  *
427338c8a9a5SSteve French  * If one doesn't exist then insert a new tcon_link struct into the tree and
427438c8a9a5SSteve French  * try to construct a new one.
427538c8a9a5SSteve French  */
427638c8a9a5SSteve French struct tcon_link *
427738c8a9a5SSteve French cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
cifs_sb_tlink(struct cifs_sb_info * cifs_sb)427838c8a9a5SSteve French {
427938c8a9a5SSteve French 	int ret;
428038c8a9a5SSteve French 	kuid_t fsuid = current_fsuid();
428138c8a9a5SSteve French 	struct tcon_link *tlink, *newtlink;
428238c8a9a5SSteve French 
428338c8a9a5SSteve French 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
428438c8a9a5SSteve French 		return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
428538c8a9a5SSteve French 
428638c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
428738c8a9a5SSteve French 	tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
428838c8a9a5SSteve French 	if (tlink)
428938c8a9a5SSteve French 		cifs_get_tlink(tlink);
429038c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
429138c8a9a5SSteve French 
429238c8a9a5SSteve French 	if (tlink == NULL) {
429338c8a9a5SSteve French 		newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
429438c8a9a5SSteve French 		if (newtlink == NULL)
429538c8a9a5SSteve French 			return ERR_PTR(-ENOMEM);
429638c8a9a5SSteve French 		newtlink->tl_uid = fsuid;
429738c8a9a5SSteve French 		newtlink->tl_tcon = ERR_PTR(-EACCES);
429838c8a9a5SSteve French 		set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
429938c8a9a5SSteve French 		set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
430038c8a9a5SSteve French 		cifs_get_tlink(newtlink);
430138c8a9a5SSteve French 
430238c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
430338c8a9a5SSteve French 		/* was one inserted after previous search? */
430438c8a9a5SSteve French 		tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
430538c8a9a5SSteve French 		if (tlink) {
430638c8a9a5SSteve French 			cifs_get_tlink(tlink);
430738c8a9a5SSteve French 			spin_unlock(&cifs_sb->tlink_tree_lock);
430838c8a9a5SSteve French 			kfree(newtlink);
430938c8a9a5SSteve French 			goto wait_for_construction;
431038c8a9a5SSteve French 		}
431138c8a9a5SSteve French 		tlink = newtlink;
431238c8a9a5SSteve French 		tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
431338c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
431438c8a9a5SSteve French 	} else {
431538c8a9a5SSteve French wait_for_construction:
431638c8a9a5SSteve French 		ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
431738c8a9a5SSteve French 				  TASK_INTERRUPTIBLE);
431838c8a9a5SSteve French 		if (ret) {
431938c8a9a5SSteve French 			cifs_put_tlink(tlink);
432038c8a9a5SSteve French 			return ERR_PTR(-ERESTARTSYS);
432138c8a9a5SSteve French 		}
432238c8a9a5SSteve French 
432338c8a9a5SSteve French 		/* if it's good, return it */
432438c8a9a5SSteve French 		if (!IS_ERR(tlink->tl_tcon))
432538c8a9a5SSteve French 			return tlink;
432638c8a9a5SSteve French 
432738c8a9a5SSteve French 		/* return error if we tried this already recently */
432838c8a9a5SSteve French 		if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
432938c8a9a5SSteve French 			cifs_put_tlink(tlink);
433038c8a9a5SSteve French 			return ERR_PTR(-EACCES);
433138c8a9a5SSteve French 		}
433238c8a9a5SSteve French 
433338c8a9a5SSteve French 		if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
433438c8a9a5SSteve French 			goto wait_for_construction;
433538c8a9a5SSteve French 	}
433638c8a9a5SSteve French 
433738c8a9a5SSteve French 	tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
433838c8a9a5SSteve French 	clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
433938c8a9a5SSteve French 	wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
434038c8a9a5SSteve French 
434138c8a9a5SSteve French 	if (IS_ERR(tlink->tl_tcon)) {
434238c8a9a5SSteve French 		cifs_put_tlink(tlink);
434338c8a9a5SSteve French 		return ERR_PTR(-EACCES);
434438c8a9a5SSteve French 	}
434538c8a9a5SSteve French 
434638c8a9a5SSteve French 	return tlink;
434738c8a9a5SSteve French }
434838c8a9a5SSteve French 
434938c8a9a5SSteve French /*
435038c8a9a5SSteve French  * periodic workqueue job that scans tcon_tree for a superblock and closes
435138c8a9a5SSteve French  * out tcons.
435238c8a9a5SSteve French  */
435338c8a9a5SSteve French static void
435438c8a9a5SSteve French cifs_prune_tlinks(struct work_struct *work)
cifs_prune_tlinks(struct work_struct * work)435538c8a9a5SSteve French {
435638c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
435738c8a9a5SSteve French 						    prune_tlinks.work);
435838c8a9a5SSteve French 	struct rb_root *root = &cifs_sb->tlink_tree;
435938c8a9a5SSteve French 	struct rb_node *node;
436038c8a9a5SSteve French 	struct rb_node *tmp;
436138c8a9a5SSteve French 	struct tcon_link *tlink;
436238c8a9a5SSteve French 
436338c8a9a5SSteve French 	/*
436438c8a9a5SSteve French 	 * Because we drop the spinlock in the loop in order to put the tlink
436538c8a9a5SSteve French 	 * it's not guarded against removal of links from the tree. The only
436638c8a9a5SSteve French 	 * places that remove entries from the tree are this function and
436738c8a9a5SSteve French 	 * umounts. Because this function is non-reentrant and is canceled
436838c8a9a5SSteve French 	 * before umount can proceed, this is safe.
436938c8a9a5SSteve French 	 */
437038c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
437138c8a9a5SSteve French 	node = rb_first(root);
437238c8a9a5SSteve French 	while (node != NULL) {
437338c8a9a5SSteve French 		tmp = node;
437438c8a9a5SSteve French 		node = rb_next(tmp);
437538c8a9a5SSteve French 		tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
437638c8a9a5SSteve French 
437738c8a9a5SSteve French 		if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
437838c8a9a5SSteve French 		    atomic_read(&tlink->tl_count) != 0 ||
437938c8a9a5SSteve French 		    time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
438038c8a9a5SSteve French 			continue;
438138c8a9a5SSteve French 
438238c8a9a5SSteve French 		cifs_get_tlink(tlink);
438338c8a9a5SSteve French 		clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
438438c8a9a5SSteve French 		rb_erase(tmp, root);
438538c8a9a5SSteve French 
438638c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
438738c8a9a5SSteve French 		cifs_put_tlink(tlink);
438838c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
438938c8a9a5SSteve French 	}
439038c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
439138c8a9a5SSteve French 
439238c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
439338c8a9a5SSteve French 				TLINK_IDLE_EXPIRE);
439438c8a9a5SSteve French }
439538c8a9a5SSteve French 
439638c8a9a5SSteve French #ifndef CONFIG_CIFS_DFS_UPCALL
439738c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
cifs_tree_connect(const unsigned int xid,struct cifs_tcon * tcon,const struct nls_table * nlsc)439838c8a9a5SSteve French {
439938c8a9a5SSteve French 	int rc;
440038c8a9a5SSteve French 	const struct smb_version_operations *ops = tcon->ses->server->ops;
440138c8a9a5SSteve French 
440238c8a9a5SSteve French 	/* only send once per connect */
440338c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
4404cd743cfeSShyam Prasad N 
4405cd743cfeSShyam Prasad N 	/* if tcon is marked for needing reconnect, update state */
4406cd743cfeSShyam Prasad N 	if (tcon->need_reconnect)
4407cd743cfeSShyam Prasad N 		tcon->status = TID_NEED_TCON;
4408cd743cfeSShyam Prasad N 
440991f4480cSShyam Prasad N 	if (tcon->status == TID_GOOD) {
441091f4480cSShyam Prasad N 		spin_unlock(&tcon->tc_lock);
441191f4480cSShyam Prasad N 		return 0;
441291f4480cSShyam Prasad N 	}
441391f4480cSShyam Prasad N 
441438c8a9a5SSteve French 	if (tcon->status != TID_NEW &&
441538c8a9a5SSteve French 	    tcon->status != TID_NEED_TCON) {
441638c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
441738c8a9a5SSteve French 		return -EHOSTDOWN;
441838c8a9a5SSteve French 	}
441938c8a9a5SSteve French 
442038c8a9a5SSteve French 	tcon->status = TID_IN_TCON;
442138c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
442238c8a9a5SSteve French 
442338c8a9a5SSteve French 	rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, nlsc);
442438c8a9a5SSteve French 	if (rc) {
442538c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
442638c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
442738c8a9a5SSteve French 			tcon->status = TID_NEED_TCON;
442838c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
442938c8a9a5SSteve French 	} else {
443038c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
443138c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
443238c8a9a5SSteve French 			tcon->status = TID_GOOD;
443338c8a9a5SSteve French 		tcon->need_reconnect = false;
443438c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
443538c8a9a5SSteve French 	}
443638c8a9a5SSteve French 
443738c8a9a5SSteve French 	return rc;
443838c8a9a5SSteve French }
443938c8a9a5SSteve French #endif
4440