xref: /openbmc/linux/fs/smb/client/connect.c (revision 6a50d71d0ffff6791737eb502b27f74fb87d0cae)
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 extern mempool_t *cifs_req_poolp;
5638c8a9a5SSteve French extern bool disable_legacy_dialects;
5738c8a9a5SSteve French 
5838c8a9a5SSteve French /* FIXME: should these be tunable? */
5938c8a9a5SSteve French #define TLINK_ERROR_EXPIRE	(1 * HZ)
6038c8a9a5SSteve French #define TLINK_IDLE_EXPIRE	(600 * HZ)
6138c8a9a5SSteve French 
6238c8a9a5SSteve French /* Drop the connection to not overload the server */
6369cba9d3SShyam Prasad N #define MAX_STATUS_IO_TIMEOUT   5
6438c8a9a5SSteve French 
6538c8a9a5SSteve French static int ip_connect(struct TCP_Server_Info *server);
6638c8a9a5SSteve French static int generic_ip_connect(struct TCP_Server_Info *server);
6738c8a9a5SSteve French static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
6838c8a9a5SSteve French static void cifs_prune_tlinks(struct work_struct *work);
6938c8a9a5SSteve French 
7038c8a9a5SSteve French /*
7138c8a9a5SSteve French  * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
7238c8a9a5SSteve French  * get their ip addresses changed at some point.
7338c8a9a5SSteve French  *
7438c8a9a5SSteve French  * This should be called with server->srv_mutex held.
7538c8a9a5SSteve French  */
7638c8a9a5SSteve French static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
7738c8a9a5SSteve French {
7838c8a9a5SSteve French 	int rc;
7938c8a9a5SSteve French 	int len;
8038c8a9a5SSteve French 	char *unc;
8138c8a9a5SSteve French 	struct sockaddr_storage ss;
8238c8a9a5SSteve French 
8338c8a9a5SSteve French 	if (!server->hostname)
8438c8a9a5SSteve French 		return -EINVAL;
8538c8a9a5SSteve French 
8638c8a9a5SSteve French 	/* if server hostname isn't populated, there's nothing to do here */
8738c8a9a5SSteve French 	if (server->hostname[0] == '\0')
8838c8a9a5SSteve French 		return 0;
8938c8a9a5SSteve French 
9038c8a9a5SSteve French 	len = strlen(server->hostname) + 3;
9138c8a9a5SSteve French 
9238c8a9a5SSteve French 	unc = kmalloc(len, GFP_KERNEL);
9338c8a9a5SSteve French 	if (!unc) {
9438c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
9538c8a9a5SSteve French 		return -ENOMEM;
9638c8a9a5SSteve French 	}
9738c8a9a5SSteve French 	scnprintf(unc, len, "\\\\%s", server->hostname);
9838c8a9a5SSteve French 
9938c8a9a5SSteve French 	spin_lock(&server->srv_lock);
10038c8a9a5SSteve French 	ss = server->dstaddr;
10138c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
10238c8a9a5SSteve French 
10338c8a9a5SSteve French 	rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
10438c8a9a5SSteve French 	kfree(unc);
10538c8a9a5SSteve French 
10638c8a9a5SSteve French 	if (rc < 0) {
10738c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
10838c8a9a5SSteve French 			 __func__, server->hostname, rc);
10938c8a9a5SSteve French 	} else {
11038c8a9a5SSteve French 		spin_lock(&server->srv_lock);
11138c8a9a5SSteve French 		memcpy(&server->dstaddr, &ss, sizeof(server->dstaddr));
11238c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
11338c8a9a5SSteve French 		rc = 0;
11438c8a9a5SSteve French 	}
11538c8a9a5SSteve French 
11638c8a9a5SSteve French 	return rc;
11738c8a9a5SSteve French }
11838c8a9a5SSteve French 
11938c8a9a5SSteve French static void smb2_query_server_interfaces(struct work_struct *work)
12038c8a9a5SSteve French {
12138c8a9a5SSteve French 	int rc;
12238c8a9a5SSteve French 	struct cifs_tcon *tcon = container_of(work,
12338c8a9a5SSteve French 					struct cifs_tcon,
12438c8a9a5SSteve French 					query_interfaces.work);
12538c8a9a5SSteve French 
12638c8a9a5SSteve French 	/*
12738c8a9a5SSteve French 	 * query server network interfaces, in case they change
12838c8a9a5SSteve French 	 */
12938c8a9a5SSteve French 	rc = SMB3_request_interfaces(0, tcon, false);
13038c8a9a5SSteve French 	if (rc) {
13138c8a9a5SSteve French 		cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
13238c8a9a5SSteve French 				__func__, rc);
13338c8a9a5SSteve French 	}
13438c8a9a5SSteve French 
13538c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
13638c8a9a5SSteve French 			   (SMB_INTERFACE_POLL_INTERVAL * HZ));
13738c8a9a5SSteve French }
13838c8a9a5SSteve French 
13938c8a9a5SSteve French /*
14038c8a9a5SSteve French  * Update the tcpStatus for the server.
14138c8a9a5SSteve French  * This is used to signal the cifsd thread to call cifs_reconnect
14238c8a9a5SSteve French  * ONLY cifsd thread should call cifs_reconnect. For any other
14338c8a9a5SSteve French  * thread, use this function
14438c8a9a5SSteve French  *
14538c8a9a5SSteve French  * @server: the tcp ses for which reconnect is needed
14638c8a9a5SSteve French  * @all_channels: if this needs to be done for all channels
14738c8a9a5SSteve French  */
14838c8a9a5SSteve French void
14938c8a9a5SSteve French cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
15038c8a9a5SSteve French 				bool all_channels)
15138c8a9a5SSteve French {
15238c8a9a5SSteve French 	struct TCP_Server_Info *pserver;
15338c8a9a5SSteve French 	struct cifs_ses *ses;
15438c8a9a5SSteve French 	int i;
15538c8a9a5SSteve French 
15638c8a9a5SSteve French 	/* If server is a channel, select the primary channel */
157b3773b19SSteve French 	pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
15838c8a9a5SSteve French 
15938c8a9a5SSteve French 	spin_lock(&pserver->srv_lock);
16038c8a9a5SSteve French 	if (!all_channels) {
16138c8a9a5SSteve French 		pserver->tcpStatus = CifsNeedReconnect;
16238c8a9a5SSteve French 		spin_unlock(&pserver->srv_lock);
16338c8a9a5SSteve French 		return;
16438c8a9a5SSteve French 	}
16538c8a9a5SSteve French 	spin_unlock(&pserver->srv_lock);
16638c8a9a5SSteve French 
16738c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
16838c8a9a5SSteve French 	list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
16938c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
17038c8a9a5SSteve French 		for (i = 0; i < ses->chan_count; i++) {
17138c8a9a5SSteve French 			spin_lock(&ses->chans[i].server->srv_lock);
17238c8a9a5SSteve French 			ses->chans[i].server->tcpStatus = CifsNeedReconnect;
17338c8a9a5SSteve French 			spin_unlock(&ses->chans[i].server->srv_lock);
17438c8a9a5SSteve French 		}
17538c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
17638c8a9a5SSteve French 	}
17738c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
17838c8a9a5SSteve French }
17938c8a9a5SSteve French 
18038c8a9a5SSteve French /*
18138c8a9a5SSteve French  * Mark all sessions and tcons for reconnect.
18238c8a9a5SSteve French  * IMPORTANT: make sure that this gets called only from
18338c8a9a5SSteve French  * cifsd thread. For any other thread, use
18438c8a9a5SSteve French  * cifs_signal_cifsd_for_reconnect
18538c8a9a5SSteve French  *
18638c8a9a5SSteve French  * @server: the tcp ses for which reconnect is needed
18738c8a9a5SSteve French  * @server needs to be previously set to CifsNeedReconnect.
18838c8a9a5SSteve French  * @mark_smb_session: whether even sessions need to be marked
18938c8a9a5SSteve French  */
19038c8a9a5SSteve French void
19138c8a9a5SSteve French cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
19238c8a9a5SSteve French 				      bool mark_smb_session)
19338c8a9a5SSteve French {
19438c8a9a5SSteve French 	struct TCP_Server_Info *pserver;
19538c8a9a5SSteve French 	struct cifs_ses *ses, *nses;
19638c8a9a5SSteve French 	struct cifs_tcon *tcon;
19738c8a9a5SSteve French 
19838c8a9a5SSteve French 	/*
19938c8a9a5SSteve French 	 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
20038c8a9a5SSteve French 	 * are not used until reconnected.
20138c8a9a5SSteve French 	 */
20238c8a9a5SSteve French 	cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__);
20338c8a9a5SSteve French 
20438c8a9a5SSteve French 	/* If server is a channel, select the primary channel */
205b3773b19SSteve French 	pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
20638c8a9a5SSteve French 
20738c8a9a5SSteve French 
20838c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
20938c8a9a5SSteve French 	list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) {
21038c8a9a5SSteve French 		/* check if iface is still active */
21138c8a9a5SSteve French 		if (!cifs_chan_is_iface_active(ses, server))
21238c8a9a5SSteve French 			cifs_chan_update_iface(ses, server);
21338c8a9a5SSteve French 
21438c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
21538c8a9a5SSteve French 		if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) {
21638c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
21738c8a9a5SSteve French 			continue;
21838c8a9a5SSteve French 		}
21938c8a9a5SSteve French 
22038c8a9a5SSteve French 		if (mark_smb_session)
22138c8a9a5SSteve French 			CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses);
22238c8a9a5SSteve French 		else
22338c8a9a5SSteve French 			cifs_chan_set_need_reconnect(ses, server);
22438c8a9a5SSteve French 
22538c8a9a5SSteve French 		cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
22638c8a9a5SSteve French 			 __func__, ses->chans_need_reconnect);
22738c8a9a5SSteve French 
22838c8a9a5SSteve French 		/* If all channels need reconnect, then tcon needs reconnect */
22938c8a9a5SSteve French 		if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
23038c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
23138c8a9a5SSteve French 			continue;
23238c8a9a5SSteve French 		}
23338c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
23438c8a9a5SSteve French 
23538c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
23638c8a9a5SSteve French 		ses->ses_status = SES_NEED_RECON;
23738c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
23838c8a9a5SSteve French 
23938c8a9a5SSteve French 		list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
24038c8a9a5SSteve French 			tcon->need_reconnect = true;
24138c8a9a5SSteve French 			spin_lock(&tcon->tc_lock);
24238c8a9a5SSteve French 			tcon->status = TID_NEED_RECON;
24338c8a9a5SSteve French 			spin_unlock(&tcon->tc_lock);
24438c8a9a5SSteve French 		}
24538c8a9a5SSteve French 		if (ses->tcon_ipc) {
24638c8a9a5SSteve French 			ses->tcon_ipc->need_reconnect = true;
24738c8a9a5SSteve French 			spin_lock(&ses->tcon_ipc->tc_lock);
24838c8a9a5SSteve French 			ses->tcon_ipc->status = TID_NEED_RECON;
24938c8a9a5SSteve French 			spin_unlock(&ses->tcon_ipc->tc_lock);
25038c8a9a5SSteve French 		}
25138c8a9a5SSteve French 	}
25238c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
25338c8a9a5SSteve French }
25438c8a9a5SSteve French 
25538c8a9a5SSteve French static void
25638c8a9a5SSteve French cifs_abort_connection(struct TCP_Server_Info *server)
25738c8a9a5SSteve French {
25838c8a9a5SSteve French 	struct mid_q_entry *mid, *nmid;
25938c8a9a5SSteve French 	struct list_head retry_list;
26038c8a9a5SSteve French 
26138c8a9a5SSteve French 	server->maxBuf = 0;
26238c8a9a5SSteve French 	server->max_read = 0;
26338c8a9a5SSteve French 
26438c8a9a5SSteve French 	/* do not want to be sending data on a socket we are freeing */
26538c8a9a5SSteve French 	cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
26638c8a9a5SSteve French 	cifs_server_lock(server);
26738c8a9a5SSteve French 	if (server->ssocket) {
26838c8a9a5SSteve French 		cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
26938c8a9a5SSteve French 			 server->ssocket->flags);
27038c8a9a5SSteve French 		kernel_sock_shutdown(server->ssocket, SHUT_WR);
27138c8a9a5SSteve French 		cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
27238c8a9a5SSteve French 			 server->ssocket->flags);
27338c8a9a5SSteve French 		sock_release(server->ssocket);
27438c8a9a5SSteve French 		server->ssocket = NULL;
27538c8a9a5SSteve French 	}
27638c8a9a5SSteve French 	server->sequence_number = 0;
27738c8a9a5SSteve French 	server->session_estab = false;
27838c8a9a5SSteve French 	kfree_sensitive(server->session_key.response);
27938c8a9a5SSteve French 	server->session_key.response = NULL;
28038c8a9a5SSteve French 	server->session_key.len = 0;
28138c8a9a5SSteve French 	server->lstrp = jiffies;
28238c8a9a5SSteve French 
28338c8a9a5SSteve French 	/* mark submitted MIDs for retry and issue callback */
28438c8a9a5SSteve French 	INIT_LIST_HEAD(&retry_list);
28538c8a9a5SSteve French 	cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
28638c8a9a5SSteve French 	spin_lock(&server->mid_lock);
28738c8a9a5SSteve French 	list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
28838c8a9a5SSteve French 		kref_get(&mid->refcount);
28938c8a9a5SSteve French 		if (mid->mid_state == MID_REQUEST_SUBMITTED)
29038c8a9a5SSteve French 			mid->mid_state = MID_RETRY_NEEDED;
29138c8a9a5SSteve French 		list_move(&mid->qhead, &retry_list);
29238c8a9a5SSteve French 		mid->mid_flags |= MID_DELETED;
29338c8a9a5SSteve French 	}
29438c8a9a5SSteve French 	spin_unlock(&server->mid_lock);
29538c8a9a5SSteve French 	cifs_server_unlock(server);
29638c8a9a5SSteve French 
29738c8a9a5SSteve French 	cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
29838c8a9a5SSteve French 	list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
29938c8a9a5SSteve French 		list_del_init(&mid->qhead);
30038c8a9a5SSteve French 		mid->callback(mid);
30138c8a9a5SSteve French 		release_mid(mid);
30238c8a9a5SSteve French 	}
30338c8a9a5SSteve French 
30438c8a9a5SSteve French 	if (cifs_rdma_enabled(server)) {
30538c8a9a5SSteve French 		cifs_server_lock(server);
30638c8a9a5SSteve French 		smbd_destroy(server);
30738c8a9a5SSteve French 		cifs_server_unlock(server);
30838c8a9a5SSteve French 	}
30938c8a9a5SSteve French }
31038c8a9a5SSteve French 
31138c8a9a5SSteve French static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
31238c8a9a5SSteve French {
31338c8a9a5SSteve French 	spin_lock(&server->srv_lock);
31438c8a9a5SSteve French 	server->nr_targets = num_targets;
31538c8a9a5SSteve French 	if (server->tcpStatus == CifsExiting) {
31638c8a9a5SSteve French 		/* the demux thread will exit normally next time through the loop */
31738c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
31838c8a9a5SSteve French 		wake_up(&server->response_q);
31938c8a9a5SSteve French 		return false;
32038c8a9a5SSteve French 	}
32138c8a9a5SSteve French 
32238c8a9a5SSteve French 	cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
32338c8a9a5SSteve French 	trace_smb3_reconnect(server->CurrentMid, server->conn_id,
32438c8a9a5SSteve French 			     server->hostname);
32538c8a9a5SSteve French 	server->tcpStatus = CifsNeedReconnect;
32638c8a9a5SSteve French 
32738c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
32838c8a9a5SSteve French 	return true;
32938c8a9a5SSteve French }
33038c8a9a5SSteve French 
33138c8a9a5SSteve French /*
33238c8a9a5SSteve French  * cifs tcp session reconnection
33338c8a9a5SSteve French  *
33438c8a9a5SSteve French  * mark tcp session as reconnecting so temporarily locked
33538c8a9a5SSteve French  * mark all smb sessions as reconnecting for tcp session
33638c8a9a5SSteve French  * reconnect tcp session
33738c8a9a5SSteve French  * wake up waiters on reconnection? - (not needed currently)
33838c8a9a5SSteve French  *
33938c8a9a5SSteve French  * if mark_smb_session is passed as true, unconditionally mark
34038c8a9a5SSteve French  * the smb session (and tcon) for reconnect as well. This value
34138c8a9a5SSteve French  * doesn't really matter for non-multichannel scenario.
34238c8a9a5SSteve French  *
34338c8a9a5SSteve French  */
34438c8a9a5SSteve French static int __cifs_reconnect(struct TCP_Server_Info *server,
34538c8a9a5SSteve French 			    bool mark_smb_session)
34638c8a9a5SSteve French {
34738c8a9a5SSteve French 	int rc = 0;
34838c8a9a5SSteve French 
34938c8a9a5SSteve French 	if (!cifs_tcp_ses_needs_reconnect(server, 1))
35038c8a9a5SSteve French 		return 0;
35138c8a9a5SSteve French 
35238c8a9a5SSteve French 	cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
35338c8a9a5SSteve French 
35438c8a9a5SSteve French 	cifs_abort_connection(server);
35538c8a9a5SSteve French 
35638c8a9a5SSteve French 	do {
35738c8a9a5SSteve French 		try_to_freeze();
35838c8a9a5SSteve French 		cifs_server_lock(server);
35938c8a9a5SSteve French 
36038c8a9a5SSteve French 		if (!cifs_swn_set_server_dstaddr(server)) {
36138c8a9a5SSteve French 			/* resolve the hostname again to make sure that IP address is up-to-date */
36238c8a9a5SSteve French 			rc = reconn_set_ipaddr_from_hostname(server);
36338c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
36438c8a9a5SSteve French 		}
36538c8a9a5SSteve French 
36638c8a9a5SSteve French 		if (cifs_rdma_enabled(server))
36738c8a9a5SSteve French 			rc = smbd_reconnect(server);
36838c8a9a5SSteve French 		else
36938c8a9a5SSteve French 			rc = generic_ip_connect(server);
37038c8a9a5SSteve French 		if (rc) {
37138c8a9a5SSteve French 			cifs_server_unlock(server);
37238c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
37338c8a9a5SSteve French 			msleep(3000);
37438c8a9a5SSteve French 		} else {
37538c8a9a5SSteve French 			atomic_inc(&tcpSesReconnectCount);
37638c8a9a5SSteve French 			set_credits(server, 1);
37738c8a9a5SSteve French 			spin_lock(&server->srv_lock);
37838c8a9a5SSteve French 			if (server->tcpStatus != CifsExiting)
37938c8a9a5SSteve French 				server->tcpStatus = CifsNeedNegotiate;
38038c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
38138c8a9a5SSteve French 			cifs_swn_reset_server_dstaddr(server);
38238c8a9a5SSteve French 			cifs_server_unlock(server);
38338c8a9a5SSteve French 			mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
38438c8a9a5SSteve French 		}
38538c8a9a5SSteve French 	} while (server->tcpStatus == CifsNeedReconnect);
38638c8a9a5SSteve French 
38738c8a9a5SSteve French 	spin_lock(&server->srv_lock);
38838c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedNegotiate)
38938c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
39038c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
39138c8a9a5SSteve French 
39238c8a9a5SSteve French 	wake_up(&server->response_q);
39338c8a9a5SSteve French 	return rc;
39438c8a9a5SSteve French }
39538c8a9a5SSteve French 
39638c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
39738c8a9a5SSteve French static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
39838c8a9a5SSteve French {
39938c8a9a5SSteve French 	int rc;
40038c8a9a5SSteve French 	char *hostname;
40138c8a9a5SSteve French 
40238c8a9a5SSteve French 	if (!cifs_swn_set_server_dstaddr(server)) {
40338c8a9a5SSteve French 		if (server->hostname != target) {
40438c8a9a5SSteve French 			hostname = extract_hostname(target);
40538c8a9a5SSteve French 			if (!IS_ERR(hostname)) {
40638c8a9a5SSteve French 				spin_lock(&server->srv_lock);
40738c8a9a5SSteve French 				kfree(server->hostname);
40838c8a9a5SSteve French 				server->hostname = hostname;
40938c8a9a5SSteve French 				spin_unlock(&server->srv_lock);
41038c8a9a5SSteve French 			} else {
41138c8a9a5SSteve French 				cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
41238c8a9a5SSteve French 					 __func__, PTR_ERR(hostname));
41338c8a9a5SSteve French 				cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
41438c8a9a5SSteve French 					 server->hostname);
41538c8a9a5SSteve French 			}
41638c8a9a5SSteve French 		}
41738c8a9a5SSteve French 		/* resolve the hostname again to make sure that IP address is up-to-date. */
41838c8a9a5SSteve French 		rc = reconn_set_ipaddr_from_hostname(server);
41938c8a9a5SSteve French 		cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
42038c8a9a5SSteve French 	}
42138c8a9a5SSteve French 	/* Reconnect the socket */
42238c8a9a5SSteve French 	if (cifs_rdma_enabled(server))
42338c8a9a5SSteve French 		rc = smbd_reconnect(server);
42438c8a9a5SSteve French 	else
42538c8a9a5SSteve French 		rc = generic_ip_connect(server);
42638c8a9a5SSteve French 
42738c8a9a5SSteve French 	return rc;
42838c8a9a5SSteve French }
42938c8a9a5SSteve French 
43038c8a9a5SSteve French static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
43138c8a9a5SSteve French 				     struct dfs_cache_tgt_iterator **target_hint)
43238c8a9a5SSteve French {
43338c8a9a5SSteve French 	int rc;
43438c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *tit;
43538c8a9a5SSteve French 
43638c8a9a5SSteve French 	*target_hint = NULL;
43738c8a9a5SSteve French 
43838c8a9a5SSteve French 	/* If dfs target list is empty, then reconnect to last server */
43938c8a9a5SSteve French 	tit = dfs_cache_get_tgt_iterator(tl);
44038c8a9a5SSteve French 	if (!tit)
44138c8a9a5SSteve French 		return __reconnect_target_unlocked(server, server->hostname);
44238c8a9a5SSteve French 
44338c8a9a5SSteve French 	/* Otherwise, try every dfs target in @tl */
44438c8a9a5SSteve French 	for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
44538c8a9a5SSteve French 		rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
44638c8a9a5SSteve French 		if (!rc) {
44738c8a9a5SSteve French 			*target_hint = tit;
44838c8a9a5SSteve French 			break;
44938c8a9a5SSteve French 		}
45038c8a9a5SSteve French 	}
45138c8a9a5SSteve French 	return rc;
45238c8a9a5SSteve French }
45338c8a9a5SSteve French 
45438c8a9a5SSteve French static int reconnect_dfs_server(struct TCP_Server_Info *server)
45538c8a9a5SSteve French {
45638c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *target_hint = NULL;
4573fea12f3SPaulo Alcantara 	DFS_CACHE_TGT_LIST(tl);
45838c8a9a5SSteve French 	int num_targets = 0;
4593fea12f3SPaulo Alcantara 	int rc = 0;
46038c8a9a5SSteve French 
46138c8a9a5SSteve French 	/*
46238c8a9a5SSteve French 	 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
46338c8a9a5SSteve French 	 *
46438c8a9a5SSteve French 	 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
46538c8a9a5SSteve French 	 * targets (server->nr_targets).  It's also possible that the cached referral was cleared
46638c8a9a5SSteve French 	 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
46738c8a9a5SSteve French 	 * refreshing the referral, so, in this case, default it to 1.
46838c8a9a5SSteve French 	 */
46938c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
47038c8a9a5SSteve French 	if (!dfs_cache_noreq_find(server->leaf_fullpath + 1, NULL, &tl))
47138c8a9a5SSteve French 		num_targets = dfs_cache_get_nr_tgts(&tl);
47238c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
47338c8a9a5SSteve French 	if (!num_targets)
47438c8a9a5SSteve French 		num_targets = 1;
47538c8a9a5SSteve French 
47638c8a9a5SSteve French 	if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
47738c8a9a5SSteve French 		return 0;
47838c8a9a5SSteve French 
47938c8a9a5SSteve French 	/*
48038c8a9a5SSteve French 	 * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a
48138c8a9a5SSteve French 	 * different server or share during failover.  It could be improved by adding some logic to
48238c8a9a5SSteve French 	 * only do that in case it connects to a different server or share, though.
48338c8a9a5SSteve French 	 */
48438c8a9a5SSteve French 	cifs_mark_tcp_ses_conns_for_reconnect(server, true);
48538c8a9a5SSteve French 
48638c8a9a5SSteve French 	cifs_abort_connection(server);
48738c8a9a5SSteve French 
48838c8a9a5SSteve French 	do {
48938c8a9a5SSteve French 		try_to_freeze();
49038c8a9a5SSteve French 		cifs_server_lock(server);
49138c8a9a5SSteve French 
49238c8a9a5SSteve French 		rc = reconnect_target_unlocked(server, &tl, &target_hint);
49338c8a9a5SSteve French 		if (rc) {
49438c8a9a5SSteve French 			/* Failed to reconnect socket */
49538c8a9a5SSteve French 			cifs_server_unlock(server);
49638c8a9a5SSteve French 			cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
49738c8a9a5SSteve French 			msleep(3000);
49838c8a9a5SSteve French 			continue;
49938c8a9a5SSteve French 		}
50038c8a9a5SSteve French 		/*
50138c8a9a5SSteve French 		 * Socket was created.  Update tcp session status to CifsNeedNegotiate so that a
50238c8a9a5SSteve French 		 * process waiting for reconnect will know it needs to re-establish session and tcon
50338c8a9a5SSteve French 		 * through the reconnected target server.
50438c8a9a5SSteve French 		 */
50538c8a9a5SSteve French 		atomic_inc(&tcpSesReconnectCount);
50638c8a9a5SSteve French 		set_credits(server, 1);
50738c8a9a5SSteve French 		spin_lock(&server->srv_lock);
50838c8a9a5SSteve French 		if (server->tcpStatus != CifsExiting)
50938c8a9a5SSteve French 			server->tcpStatus = CifsNeedNegotiate;
51038c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
51138c8a9a5SSteve French 		cifs_swn_reset_server_dstaddr(server);
51238c8a9a5SSteve French 		cifs_server_unlock(server);
51338c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
51438c8a9a5SSteve French 	} while (server->tcpStatus == CifsNeedReconnect);
51538c8a9a5SSteve French 
51638c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
51738c8a9a5SSteve French 	dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, target_hint);
51838c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
51938c8a9a5SSteve French 	dfs_cache_free_tgts(&tl);
52038c8a9a5SSteve French 
52138c8a9a5SSteve French 	/* Need to set up echo worker again once connection has been established */
52238c8a9a5SSteve French 	spin_lock(&server->srv_lock);
52338c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedNegotiate)
52438c8a9a5SSteve French 		mod_delayed_work(cifsiod_wq, &server->echo, 0);
52538c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
52638c8a9a5SSteve French 
52738c8a9a5SSteve French 	wake_up(&server->response_q);
52838c8a9a5SSteve French 	return rc;
52938c8a9a5SSteve French }
53038c8a9a5SSteve French 
53138c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
53238c8a9a5SSteve French {
53338c8a9a5SSteve French 	mutex_lock(&server->refpath_lock);
53438c8a9a5SSteve French 	if (!server->leaf_fullpath) {
53538c8a9a5SSteve French 		mutex_unlock(&server->refpath_lock);
53638c8a9a5SSteve French 		return __cifs_reconnect(server, mark_smb_session);
53738c8a9a5SSteve French 	}
53838c8a9a5SSteve French 	mutex_unlock(&server->refpath_lock);
53938c8a9a5SSteve French 
54038c8a9a5SSteve French 	return reconnect_dfs_server(server);
54138c8a9a5SSteve French }
54238c8a9a5SSteve French #else
54338c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
54438c8a9a5SSteve French {
54538c8a9a5SSteve French 	return __cifs_reconnect(server, mark_smb_session);
54638c8a9a5SSteve French }
54738c8a9a5SSteve French #endif
54838c8a9a5SSteve French 
54938c8a9a5SSteve French static void
55038c8a9a5SSteve French cifs_echo_request(struct work_struct *work)
55138c8a9a5SSteve French {
55238c8a9a5SSteve French 	int rc;
55338c8a9a5SSteve French 	struct TCP_Server_Info *server = container_of(work,
55438c8a9a5SSteve French 					struct TCP_Server_Info, echo.work);
55538c8a9a5SSteve French 
55638c8a9a5SSteve French 	/*
55738c8a9a5SSteve French 	 * We cannot send an echo if it is disabled.
55838c8a9a5SSteve French 	 * Also, no need to ping if we got a response recently.
55938c8a9a5SSteve French 	 */
56038c8a9a5SSteve French 
56138c8a9a5SSteve French 	if (server->tcpStatus == CifsNeedReconnect ||
56238c8a9a5SSteve French 	    server->tcpStatus == CifsExiting ||
56338c8a9a5SSteve French 	    server->tcpStatus == CifsNew ||
56438c8a9a5SSteve French 	    (server->ops->can_echo && !server->ops->can_echo(server)) ||
56538c8a9a5SSteve French 	    time_before(jiffies, server->lstrp + server->echo_interval - HZ))
56638c8a9a5SSteve French 		goto requeue_echo;
56738c8a9a5SSteve French 
56838c8a9a5SSteve French 	rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
56938c8a9a5SSteve French 	cifs_server_dbg(FYI, "send echo request: rc = %d\n", rc);
57038c8a9a5SSteve French 
57138c8a9a5SSteve French 	/* Check witness registrations */
57238c8a9a5SSteve French 	cifs_swn_check();
57338c8a9a5SSteve French 
57438c8a9a5SSteve French requeue_echo:
57538c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
57638c8a9a5SSteve French }
57738c8a9a5SSteve French 
57838c8a9a5SSteve French static bool
57938c8a9a5SSteve French allocate_buffers(struct TCP_Server_Info *server)
58038c8a9a5SSteve French {
58138c8a9a5SSteve French 	if (!server->bigbuf) {
58238c8a9a5SSteve French 		server->bigbuf = (char *)cifs_buf_get();
58338c8a9a5SSteve French 		if (!server->bigbuf) {
58438c8a9a5SSteve French 			cifs_server_dbg(VFS, "No memory for large SMB response\n");
58538c8a9a5SSteve French 			msleep(3000);
58638c8a9a5SSteve French 			/* retry will check if exiting */
58738c8a9a5SSteve French 			return false;
58838c8a9a5SSteve French 		}
58938c8a9a5SSteve French 	} else if (server->large_buf) {
59038c8a9a5SSteve French 		/* we are reusing a dirty large buf, clear its start */
59138c8a9a5SSteve French 		memset(server->bigbuf, 0, HEADER_SIZE(server));
59238c8a9a5SSteve French 	}
59338c8a9a5SSteve French 
59438c8a9a5SSteve French 	if (!server->smallbuf) {
59538c8a9a5SSteve French 		server->smallbuf = (char *)cifs_small_buf_get();
59638c8a9a5SSteve French 		if (!server->smallbuf) {
59738c8a9a5SSteve French 			cifs_server_dbg(VFS, "No memory for SMB response\n");
59838c8a9a5SSteve French 			msleep(1000);
59938c8a9a5SSteve French 			/* retry will check if exiting */
60038c8a9a5SSteve French 			return false;
60138c8a9a5SSteve French 		}
60238c8a9a5SSteve French 		/* beginning of smb buffer is cleared in our buf_get */
60338c8a9a5SSteve French 	} else {
60438c8a9a5SSteve French 		/* if existing small buf clear beginning */
60538c8a9a5SSteve French 		memset(server->smallbuf, 0, HEADER_SIZE(server));
60638c8a9a5SSteve French 	}
60738c8a9a5SSteve French 
60838c8a9a5SSteve French 	return true;
60938c8a9a5SSteve French }
61038c8a9a5SSteve French 
61138c8a9a5SSteve French static bool
61238c8a9a5SSteve French server_unresponsive(struct TCP_Server_Info *server)
61338c8a9a5SSteve French {
61438c8a9a5SSteve French 	/*
61538c8a9a5SSteve French 	 * We need to wait 3 echo intervals to make sure we handle such
61638c8a9a5SSteve French 	 * situations right:
61738c8a9a5SSteve French 	 * 1s  client sends a normal SMB request
61838c8a9a5SSteve French 	 * 2s  client gets a response
61938c8a9a5SSteve French 	 * 30s echo workqueue job pops, and decides we got a response recently
62038c8a9a5SSteve French 	 *     and don't need to send another
62138c8a9a5SSteve French 	 * ...
62238c8a9a5SSteve French 	 * 65s kernel_recvmsg times out, and we see that we haven't gotten
62338c8a9a5SSteve French 	 *     a response in >60s.
62438c8a9a5SSteve French 	 */
62538c8a9a5SSteve French 	spin_lock(&server->srv_lock);
62638c8a9a5SSteve French 	if ((server->tcpStatus == CifsGood ||
62738c8a9a5SSteve French 	    server->tcpStatus == CifsNeedNegotiate) &&
62838c8a9a5SSteve French 	    (!server->ops->can_echo || server->ops->can_echo(server)) &&
62938c8a9a5SSteve French 	    time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
63038c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
63138c8a9a5SSteve French 		cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
63238c8a9a5SSteve French 			 (3 * server->echo_interval) / HZ);
63338c8a9a5SSteve French 		cifs_reconnect(server, false);
63438c8a9a5SSteve French 		return true;
63538c8a9a5SSteve French 	}
63638c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
63738c8a9a5SSteve French 
63838c8a9a5SSteve French 	return false;
63938c8a9a5SSteve French }
64038c8a9a5SSteve French 
64138c8a9a5SSteve French static inline bool
64238c8a9a5SSteve French zero_credits(struct TCP_Server_Info *server)
64338c8a9a5SSteve French {
64438c8a9a5SSteve French 	int val;
64538c8a9a5SSteve French 
64638c8a9a5SSteve French 	spin_lock(&server->req_lock);
64738c8a9a5SSteve French 	val = server->credits + server->echo_credits + server->oplock_credits;
64838c8a9a5SSteve French 	if (server->in_flight == 0 && val == 0) {
64938c8a9a5SSteve French 		spin_unlock(&server->req_lock);
65038c8a9a5SSteve French 		return true;
65138c8a9a5SSteve French 	}
65238c8a9a5SSteve French 	spin_unlock(&server->req_lock);
65338c8a9a5SSteve French 	return false;
65438c8a9a5SSteve French }
65538c8a9a5SSteve French 
65638c8a9a5SSteve French static int
65738c8a9a5SSteve French cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
65838c8a9a5SSteve French {
65938c8a9a5SSteve French 	int length = 0;
66038c8a9a5SSteve French 	int total_read;
66138c8a9a5SSteve French 
66238c8a9a5SSteve French 	for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
66338c8a9a5SSteve French 		try_to_freeze();
66438c8a9a5SSteve French 
66538c8a9a5SSteve French 		/* reconnect if no credits and no requests in flight */
66638c8a9a5SSteve French 		if (zero_credits(server)) {
66738c8a9a5SSteve French 			cifs_reconnect(server, false);
66838c8a9a5SSteve French 			return -ECONNABORTED;
66938c8a9a5SSteve French 		}
67038c8a9a5SSteve French 
67138c8a9a5SSteve French 		if (server_unresponsive(server))
67238c8a9a5SSteve French 			return -ECONNABORTED;
67338c8a9a5SSteve French 		if (cifs_rdma_enabled(server) && server->smbd_conn)
67438c8a9a5SSteve French 			length = smbd_recv(server->smbd_conn, smb_msg);
67538c8a9a5SSteve French 		else
67638c8a9a5SSteve French 			length = sock_recvmsg(server->ssocket, smb_msg, 0);
67738c8a9a5SSteve French 
67838c8a9a5SSteve French 		spin_lock(&server->srv_lock);
67938c8a9a5SSteve French 		if (server->tcpStatus == CifsExiting) {
68038c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
68138c8a9a5SSteve French 			return -ESHUTDOWN;
68238c8a9a5SSteve French 		}
68338c8a9a5SSteve French 
68438c8a9a5SSteve French 		if (server->tcpStatus == CifsNeedReconnect) {
68538c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
68638c8a9a5SSteve French 			cifs_reconnect(server, false);
68738c8a9a5SSteve French 			return -ECONNABORTED;
68838c8a9a5SSteve French 		}
68938c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
69038c8a9a5SSteve French 
69138c8a9a5SSteve French 		if (length == -ERESTARTSYS ||
69238c8a9a5SSteve French 		    length == -EAGAIN ||
69338c8a9a5SSteve French 		    length == -EINTR) {
69438c8a9a5SSteve French 			/*
69538c8a9a5SSteve French 			 * Minimum sleep to prevent looping, allowing socket
69638c8a9a5SSteve French 			 * to clear and app threads to set tcpStatus
69738c8a9a5SSteve French 			 * CifsNeedReconnect if server hung.
69838c8a9a5SSteve French 			 */
69938c8a9a5SSteve French 			usleep_range(1000, 2000);
70038c8a9a5SSteve French 			length = 0;
70138c8a9a5SSteve French 			continue;
70238c8a9a5SSteve French 		}
70338c8a9a5SSteve French 
70438c8a9a5SSteve French 		if (length <= 0) {
70538c8a9a5SSteve French 			cifs_dbg(FYI, "Received no data or error: %d\n", length);
70638c8a9a5SSteve French 			cifs_reconnect(server, false);
70738c8a9a5SSteve French 			return -ECONNABORTED;
70838c8a9a5SSteve French 		}
70938c8a9a5SSteve French 	}
71038c8a9a5SSteve French 	return total_read;
71138c8a9a5SSteve French }
71238c8a9a5SSteve French 
71338c8a9a5SSteve French int
71438c8a9a5SSteve French cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
71538c8a9a5SSteve French 		      unsigned int to_read)
71638c8a9a5SSteve French {
71738c8a9a5SSteve French 	struct msghdr smb_msg = {};
71838c8a9a5SSteve French 	struct kvec iov = {.iov_base = buf, .iov_len = to_read};
71938c8a9a5SSteve French 	iov_iter_kvec(&smb_msg.msg_iter, ITER_DEST, &iov, 1, to_read);
72038c8a9a5SSteve French 
72138c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
72238c8a9a5SSteve French }
72338c8a9a5SSteve French 
72438c8a9a5SSteve French ssize_t
72538c8a9a5SSteve French cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
72638c8a9a5SSteve French {
72738c8a9a5SSteve French 	struct msghdr smb_msg = {};
72838c8a9a5SSteve French 
72938c8a9a5SSteve French 	/*
73038c8a9a5SSteve French 	 *  iov_iter_discard already sets smb_msg.type and count and iov_offset
73138c8a9a5SSteve French 	 *  and cifs_readv_from_socket sets msg_control and msg_controllen
73238c8a9a5SSteve French 	 *  so little to initialize in struct msghdr
73338c8a9a5SSteve French 	 */
73438c8a9a5SSteve French 	iov_iter_discard(&smb_msg.msg_iter, ITER_DEST, to_read);
73538c8a9a5SSteve French 
73638c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
73738c8a9a5SSteve French }
73838c8a9a5SSteve French 
73938c8a9a5SSteve French int
74038c8a9a5SSteve French cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
74138c8a9a5SSteve French 	unsigned int page_offset, unsigned int to_read)
74238c8a9a5SSteve French {
74338c8a9a5SSteve French 	struct msghdr smb_msg = {};
74438c8a9a5SSteve French 	struct bio_vec bv;
74538c8a9a5SSteve French 
74638c8a9a5SSteve French 	bvec_set_page(&bv, page, to_read, page_offset);
74738c8a9a5SSteve French 	iov_iter_bvec(&smb_msg.msg_iter, ITER_DEST, &bv, 1, to_read);
74838c8a9a5SSteve French 	return cifs_readv_from_socket(server, &smb_msg);
74938c8a9a5SSteve French }
75038c8a9a5SSteve French 
75138c8a9a5SSteve French int
75238c8a9a5SSteve French cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter *iter,
75338c8a9a5SSteve French 			   unsigned int to_read)
75438c8a9a5SSteve French {
75538c8a9a5SSteve French 	struct msghdr smb_msg = { .msg_iter = *iter };
75638c8a9a5SSteve French 	int ret;
75738c8a9a5SSteve French 
75838c8a9a5SSteve French 	iov_iter_truncate(&smb_msg.msg_iter, to_read);
75938c8a9a5SSteve French 	ret = cifs_readv_from_socket(server, &smb_msg);
76038c8a9a5SSteve French 	if (ret > 0)
76138c8a9a5SSteve French 		iov_iter_advance(iter, ret);
76238c8a9a5SSteve French 	return ret;
76338c8a9a5SSteve French }
76438c8a9a5SSteve French 
76538c8a9a5SSteve French static bool
76638c8a9a5SSteve French is_smb_response(struct TCP_Server_Info *server, unsigned char type)
76738c8a9a5SSteve French {
76838c8a9a5SSteve French 	/*
76938c8a9a5SSteve French 	 * The first byte big endian of the length field,
77038c8a9a5SSteve French 	 * is actually not part of the length but the type
77138c8a9a5SSteve French 	 * with the most common, zero, as regular data.
77238c8a9a5SSteve French 	 */
77338c8a9a5SSteve French 	switch (type) {
77438c8a9a5SSteve French 	case RFC1002_SESSION_MESSAGE:
77538c8a9a5SSteve French 		/* Regular SMB response */
77638c8a9a5SSteve French 		return true;
77738c8a9a5SSteve French 	case RFC1002_SESSION_KEEP_ALIVE:
77838c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 session keep alive\n");
77938c8a9a5SSteve French 		break;
78038c8a9a5SSteve French 	case RFC1002_POSITIVE_SESSION_RESPONSE:
78138c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 positive session response\n");
78238c8a9a5SSteve French 		break;
78338c8a9a5SSteve French 	case RFC1002_NEGATIVE_SESSION_RESPONSE:
78438c8a9a5SSteve French 		/*
78538c8a9a5SSteve French 		 * We get this from Windows 98 instead of an error on
78638c8a9a5SSteve French 		 * SMB negprot response.
78738c8a9a5SSteve French 		 */
78838c8a9a5SSteve French 		cifs_dbg(FYI, "RFC 1002 negative session response\n");
78938c8a9a5SSteve French 		/* give server a second to clean up */
79038c8a9a5SSteve French 		msleep(1000);
79138c8a9a5SSteve French 		/*
79238c8a9a5SSteve French 		 * Always try 445 first on reconnect since we get NACK
79338c8a9a5SSteve French 		 * on some if we ever connected to port 139 (the NACK
79438c8a9a5SSteve French 		 * is since we do not begin with RFC1001 session
79538c8a9a5SSteve French 		 * initialize frame).
79638c8a9a5SSteve French 		 */
79738c8a9a5SSteve French 		cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
79838c8a9a5SSteve French 		cifs_reconnect(server, true);
79938c8a9a5SSteve French 		break;
80038c8a9a5SSteve French 	default:
80138c8a9a5SSteve French 		cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
80238c8a9a5SSteve French 		cifs_reconnect(server, true);
80338c8a9a5SSteve French 	}
80438c8a9a5SSteve French 
80538c8a9a5SSteve French 	return false;
80638c8a9a5SSteve French }
80738c8a9a5SSteve French 
80838c8a9a5SSteve French void
80938c8a9a5SSteve French dequeue_mid(struct mid_q_entry *mid, bool malformed)
81038c8a9a5SSteve French {
81138c8a9a5SSteve French #ifdef CONFIG_CIFS_STATS2
81238c8a9a5SSteve French 	mid->when_received = jiffies;
81338c8a9a5SSteve French #endif
81438c8a9a5SSteve French 	spin_lock(&mid->server->mid_lock);
81538c8a9a5SSteve French 	if (!malformed)
81638c8a9a5SSteve French 		mid->mid_state = MID_RESPONSE_RECEIVED;
81738c8a9a5SSteve French 	else
81838c8a9a5SSteve French 		mid->mid_state = MID_RESPONSE_MALFORMED;
81938c8a9a5SSteve French 	/*
82038c8a9a5SSteve French 	 * Trying to handle/dequeue a mid after the send_recv()
82138c8a9a5SSteve French 	 * function has finished processing it is a bug.
82238c8a9a5SSteve French 	 */
82338c8a9a5SSteve French 	if (mid->mid_flags & MID_DELETED) {
82438c8a9a5SSteve French 		spin_unlock(&mid->server->mid_lock);
82538c8a9a5SSteve French 		pr_warn_once("trying to dequeue a deleted mid\n");
82638c8a9a5SSteve French 	} else {
82738c8a9a5SSteve French 		list_del_init(&mid->qhead);
82838c8a9a5SSteve French 		mid->mid_flags |= MID_DELETED;
82938c8a9a5SSteve French 		spin_unlock(&mid->server->mid_lock);
83038c8a9a5SSteve French 	}
83138c8a9a5SSteve French }
83238c8a9a5SSteve French 
83338c8a9a5SSteve French static unsigned int
83438c8a9a5SSteve French smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
83538c8a9a5SSteve French {
83638c8a9a5SSteve French 	struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
83738c8a9a5SSteve French 
83838c8a9a5SSteve French 	/*
83938c8a9a5SSteve French 	 * SMB1 does not use credits.
84038c8a9a5SSteve French 	 */
84138c8a9a5SSteve French 	if (is_smb1(server))
84238c8a9a5SSteve French 		return 0;
84338c8a9a5SSteve French 
84438c8a9a5SSteve French 	return le16_to_cpu(shdr->CreditRequest);
84538c8a9a5SSteve French }
84638c8a9a5SSteve French 
84738c8a9a5SSteve French static void
84838c8a9a5SSteve French handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
84938c8a9a5SSteve French 	   char *buf, int malformed)
85038c8a9a5SSteve French {
85138c8a9a5SSteve French 	if (server->ops->check_trans2 &&
85238c8a9a5SSteve French 	    server->ops->check_trans2(mid, server, buf, malformed))
85338c8a9a5SSteve French 		return;
85438c8a9a5SSteve French 	mid->credits_received = smb2_get_credits_from_hdr(buf, server);
85538c8a9a5SSteve French 	mid->resp_buf = buf;
85638c8a9a5SSteve French 	mid->large_buf = server->large_buf;
85738c8a9a5SSteve French 	/* Was previous buf put in mpx struct for multi-rsp? */
85838c8a9a5SSteve French 	if (!mid->multiRsp) {
85938c8a9a5SSteve French 		/* smb buffer will be freed by user thread */
86038c8a9a5SSteve French 		if (server->large_buf)
86138c8a9a5SSteve French 			server->bigbuf = NULL;
86238c8a9a5SSteve French 		else
86338c8a9a5SSteve French 			server->smallbuf = NULL;
86438c8a9a5SSteve French 	}
86538c8a9a5SSteve French 	dequeue_mid(mid, malformed);
86638c8a9a5SSteve French }
86738c8a9a5SSteve French 
86838c8a9a5SSteve French int
86938c8a9a5SSteve French cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
87038c8a9a5SSteve French {
87138c8a9a5SSteve French 	bool srv_sign_required = server->sec_mode & server->vals->signing_required;
87238c8a9a5SSteve French 	bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
87338c8a9a5SSteve French 	bool mnt_sign_enabled;
87438c8a9a5SSteve French 
87538c8a9a5SSteve French 	/*
87638c8a9a5SSteve French 	 * Is signing required by mnt options? If not then check
87738c8a9a5SSteve French 	 * global_secflags to see if it is there.
87838c8a9a5SSteve French 	 */
87938c8a9a5SSteve French 	if (!mnt_sign_required)
88038c8a9a5SSteve French 		mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
88138c8a9a5SSteve French 						CIFSSEC_MUST_SIGN);
88238c8a9a5SSteve French 
88338c8a9a5SSteve French 	/*
88438c8a9a5SSteve French 	 * If signing is required then it's automatically enabled too,
88538c8a9a5SSteve French 	 * otherwise, check to see if the secflags allow it.
88638c8a9a5SSteve French 	 */
88738c8a9a5SSteve French 	mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
88838c8a9a5SSteve French 				(global_secflags & CIFSSEC_MAY_SIGN);
88938c8a9a5SSteve French 
89038c8a9a5SSteve French 	/* If server requires signing, does client allow it? */
89138c8a9a5SSteve French 	if (srv_sign_required) {
89238c8a9a5SSteve French 		if (!mnt_sign_enabled) {
89338c8a9a5SSteve French 			cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n");
89438c8a9a5SSteve French 			return -EOPNOTSUPP;
89538c8a9a5SSteve French 		}
89638c8a9a5SSteve French 		server->sign = true;
89738c8a9a5SSteve French 	}
89838c8a9a5SSteve French 
89938c8a9a5SSteve French 	/* If client requires signing, does server allow it? */
90038c8a9a5SSteve French 	if (mnt_sign_required) {
90138c8a9a5SSteve French 		if (!srv_sign_enabled) {
90238c8a9a5SSteve French 			cifs_dbg(VFS, "Server does not support signing!\n");
90338c8a9a5SSteve French 			return -EOPNOTSUPP;
90438c8a9a5SSteve French 		}
90538c8a9a5SSteve French 		server->sign = true;
90638c8a9a5SSteve French 	}
90738c8a9a5SSteve French 
90838c8a9a5SSteve French 	if (cifs_rdma_enabled(server) && server->sign)
90938c8a9a5SSteve French 		cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n");
91038c8a9a5SSteve French 
91138c8a9a5SSteve French 	return 0;
91238c8a9a5SSteve French }
91338c8a9a5SSteve French 
914946ad1b8SPaulo Alcantara static noinline_for_stack void
915946ad1b8SPaulo Alcantara clean_demultiplex_info(struct TCP_Server_Info *server)
91638c8a9a5SSteve French {
91738c8a9a5SSteve French 	int length;
91838c8a9a5SSteve French 
91938c8a9a5SSteve French 	/* take it off the list, if it's not already */
92038c8a9a5SSteve French 	spin_lock(&server->srv_lock);
92138c8a9a5SSteve French 	list_del_init(&server->tcp_ses_list);
92238c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
92338c8a9a5SSteve French 
92438c8a9a5SSteve French 	cancel_delayed_work_sync(&server->echo);
92538c8a9a5SSteve French 
92638c8a9a5SSteve French 	spin_lock(&server->srv_lock);
92738c8a9a5SSteve French 	server->tcpStatus = CifsExiting;
92838c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
92938c8a9a5SSteve French 	wake_up_all(&server->response_q);
93038c8a9a5SSteve French 
93138c8a9a5SSteve French 	/* check if we have blocked requests that need to free */
93238c8a9a5SSteve French 	spin_lock(&server->req_lock);
93338c8a9a5SSteve French 	if (server->credits <= 0)
93438c8a9a5SSteve French 		server->credits = 1;
93538c8a9a5SSteve French 	spin_unlock(&server->req_lock);
93638c8a9a5SSteve French 	/*
93738c8a9a5SSteve French 	 * Although there should not be any requests blocked on this queue it
93838c8a9a5SSteve French 	 * can not hurt to be paranoid and try to wake up requests that may
93938c8a9a5SSteve French 	 * haven been blocked when more than 50 at time were on the wire to the
94038c8a9a5SSteve French 	 * same server - they now will see the session is in exit state and get
94138c8a9a5SSteve French 	 * out of SendReceive.
94238c8a9a5SSteve French 	 */
94338c8a9a5SSteve French 	wake_up_all(&server->request_q);
94438c8a9a5SSteve French 	/* give those requests time to exit */
94538c8a9a5SSteve French 	msleep(125);
94638c8a9a5SSteve French 	if (cifs_rdma_enabled(server))
94738c8a9a5SSteve French 		smbd_destroy(server);
94838c8a9a5SSteve French 	if (server->ssocket) {
94938c8a9a5SSteve French 		sock_release(server->ssocket);
95038c8a9a5SSteve French 		server->ssocket = NULL;
95138c8a9a5SSteve French 	}
95238c8a9a5SSteve French 
95338c8a9a5SSteve French 	if (!list_empty(&server->pending_mid_q)) {
95438c8a9a5SSteve French 		struct list_head dispose_list;
95538c8a9a5SSteve French 		struct mid_q_entry *mid_entry;
95638c8a9a5SSteve French 		struct list_head *tmp, *tmp2;
95738c8a9a5SSteve French 
95838c8a9a5SSteve French 		INIT_LIST_HEAD(&dispose_list);
95938c8a9a5SSteve French 		spin_lock(&server->mid_lock);
96038c8a9a5SSteve French 		list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
96138c8a9a5SSteve French 			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
96238c8a9a5SSteve French 			cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
96338c8a9a5SSteve French 			kref_get(&mid_entry->refcount);
96438c8a9a5SSteve French 			mid_entry->mid_state = MID_SHUTDOWN;
96538c8a9a5SSteve French 			list_move(&mid_entry->qhead, &dispose_list);
96638c8a9a5SSteve French 			mid_entry->mid_flags |= MID_DELETED;
96738c8a9a5SSteve French 		}
96838c8a9a5SSteve French 		spin_unlock(&server->mid_lock);
96938c8a9a5SSteve French 
97038c8a9a5SSteve French 		/* now walk dispose list and issue callbacks */
97138c8a9a5SSteve French 		list_for_each_safe(tmp, tmp2, &dispose_list) {
97238c8a9a5SSteve French 			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
97338c8a9a5SSteve French 			cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
97438c8a9a5SSteve French 			list_del_init(&mid_entry->qhead);
97538c8a9a5SSteve French 			mid_entry->callback(mid_entry);
97638c8a9a5SSteve French 			release_mid(mid_entry);
97738c8a9a5SSteve French 		}
97838c8a9a5SSteve French 		/* 1/8th of sec is more than enough time for them to exit */
97938c8a9a5SSteve French 		msleep(125);
98038c8a9a5SSteve French 	}
98138c8a9a5SSteve French 
98238c8a9a5SSteve French 	if (!list_empty(&server->pending_mid_q)) {
98338c8a9a5SSteve French 		/*
98438c8a9a5SSteve French 		 * mpx threads have not exited yet give them at least the smb
98538c8a9a5SSteve French 		 * send timeout time for long ops.
98638c8a9a5SSteve French 		 *
98738c8a9a5SSteve French 		 * Due to delays on oplock break requests, we need to wait at
98838c8a9a5SSteve French 		 * least 45 seconds before giving up on a request getting a
98938c8a9a5SSteve French 		 * response and going ahead and killing cifsd.
99038c8a9a5SSteve French 		 */
99138c8a9a5SSteve French 		cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
99238c8a9a5SSteve French 		msleep(46000);
99338c8a9a5SSteve French 		/*
99438c8a9a5SSteve French 		 * If threads still have not exited they are probably never
99538c8a9a5SSteve French 		 * coming home not much else we can do but free the memory.
99638c8a9a5SSteve French 		 */
99738c8a9a5SSteve French 	}
99838c8a9a5SSteve French 
99938c8a9a5SSteve French 	kfree(server->leaf_fullpath);
100038c8a9a5SSteve French 	kfree(server);
100138c8a9a5SSteve French 
100238c8a9a5SSteve French 	length = atomic_dec_return(&tcpSesAllocCount);
100338c8a9a5SSteve French 	if (length > 0)
100438c8a9a5SSteve French 		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
100538c8a9a5SSteve French }
100638c8a9a5SSteve French 
100738c8a9a5SSteve French static int
100838c8a9a5SSteve French standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
100938c8a9a5SSteve French {
101038c8a9a5SSteve French 	int length;
101138c8a9a5SSteve French 	char *buf = server->smallbuf;
101238c8a9a5SSteve French 	unsigned int pdu_length = server->pdu_size;
101338c8a9a5SSteve French 
101438c8a9a5SSteve French 	/* make sure this will fit in a large buffer */
101538c8a9a5SSteve French 	if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
101638c8a9a5SSteve French 	    HEADER_PREAMBLE_SIZE(server)) {
101738c8a9a5SSteve French 		cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
101838c8a9a5SSteve French 		cifs_reconnect(server, true);
101938c8a9a5SSteve French 		return -ECONNABORTED;
102038c8a9a5SSteve French 	}
102138c8a9a5SSteve French 
102238c8a9a5SSteve French 	/* switch to large buffer if too big for a small one */
102338c8a9a5SSteve French 	if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
102438c8a9a5SSteve French 		server->large_buf = true;
102538c8a9a5SSteve French 		memcpy(server->bigbuf, buf, server->total_read);
102638c8a9a5SSteve French 		buf = server->bigbuf;
102738c8a9a5SSteve French 	}
102838c8a9a5SSteve French 
102938c8a9a5SSteve French 	/* now read the rest */
103038c8a9a5SSteve French 	length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
103138c8a9a5SSteve French 				       pdu_length - MID_HEADER_SIZE(server));
103238c8a9a5SSteve French 
103338c8a9a5SSteve French 	if (length < 0)
103438c8a9a5SSteve French 		return length;
103538c8a9a5SSteve French 	server->total_read += length;
103638c8a9a5SSteve French 
103738c8a9a5SSteve French 	dump_smb(buf, server->total_read);
103838c8a9a5SSteve French 
103938c8a9a5SSteve French 	return cifs_handle_standard(server, mid);
104038c8a9a5SSteve French }
104138c8a9a5SSteve French 
104238c8a9a5SSteve French int
104338c8a9a5SSteve French cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
104438c8a9a5SSteve French {
104538c8a9a5SSteve French 	char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
104638c8a9a5SSteve French 	int rc;
104738c8a9a5SSteve French 
104838c8a9a5SSteve French 	/*
104938c8a9a5SSteve French 	 * We know that we received enough to get to the MID as we
105038c8a9a5SSteve French 	 * checked the pdu_length earlier. Now check to see
105138c8a9a5SSteve French 	 * if the rest of the header is OK.
105238c8a9a5SSteve French 	 *
105338c8a9a5SSteve French 	 * 48 bytes is enough to display the header and a little bit
105438c8a9a5SSteve French 	 * into the payload for debugging purposes.
105538c8a9a5SSteve French 	 */
105638c8a9a5SSteve French 	rc = server->ops->check_message(buf, server->total_read, server);
105738c8a9a5SSteve French 	if (rc)
105838c8a9a5SSteve French 		cifs_dump_mem("Bad SMB: ", buf,
105938c8a9a5SSteve French 			min_t(unsigned int, server->total_read, 48));
106038c8a9a5SSteve French 
106138c8a9a5SSteve French 	if (server->ops->is_session_expired &&
106238c8a9a5SSteve French 	    server->ops->is_session_expired(buf)) {
106338c8a9a5SSteve French 		cifs_reconnect(server, true);
106438c8a9a5SSteve French 		return -1;
106538c8a9a5SSteve French 	}
106638c8a9a5SSteve French 
106738c8a9a5SSteve French 	if (server->ops->is_status_pending &&
106838c8a9a5SSteve French 	    server->ops->is_status_pending(buf, server))
106938c8a9a5SSteve French 		return -1;
107038c8a9a5SSteve French 
107138c8a9a5SSteve French 	if (!mid)
107238c8a9a5SSteve French 		return rc;
107338c8a9a5SSteve French 
107438c8a9a5SSteve French 	handle_mid(mid, server, buf, rc);
107538c8a9a5SSteve French 	return 0;
107638c8a9a5SSteve French }
107738c8a9a5SSteve French 
107838c8a9a5SSteve French static void
107938c8a9a5SSteve French smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
108038c8a9a5SSteve French {
108138c8a9a5SSteve French 	struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
108238c8a9a5SSteve French 	int scredits, in_flight;
108338c8a9a5SSteve French 
108438c8a9a5SSteve French 	/*
108538c8a9a5SSteve French 	 * SMB1 does not use credits.
108638c8a9a5SSteve French 	 */
108738c8a9a5SSteve French 	if (is_smb1(server))
108838c8a9a5SSteve French 		return;
108938c8a9a5SSteve French 
109038c8a9a5SSteve French 	if (shdr->CreditRequest) {
109138c8a9a5SSteve French 		spin_lock(&server->req_lock);
109238c8a9a5SSteve French 		server->credits += le16_to_cpu(shdr->CreditRequest);
109338c8a9a5SSteve French 		scredits = server->credits;
109438c8a9a5SSteve French 		in_flight = server->in_flight;
109538c8a9a5SSteve French 		spin_unlock(&server->req_lock);
109638c8a9a5SSteve French 		wake_up(&server->request_q);
109738c8a9a5SSteve French 
109838c8a9a5SSteve French 		trace_smb3_hdr_credits(server->CurrentMid,
109938c8a9a5SSteve French 				server->conn_id, server->hostname, scredits,
110038c8a9a5SSteve French 				le16_to_cpu(shdr->CreditRequest), in_flight);
110138c8a9a5SSteve French 		cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
110238c8a9a5SSteve French 				__func__, le16_to_cpu(shdr->CreditRequest),
110338c8a9a5SSteve French 				scredits);
110438c8a9a5SSteve French 	}
110538c8a9a5SSteve French }
110638c8a9a5SSteve French 
110738c8a9a5SSteve French 
110838c8a9a5SSteve French static int
110938c8a9a5SSteve French cifs_demultiplex_thread(void *p)
111038c8a9a5SSteve French {
111138c8a9a5SSteve French 	int i, num_mids, length;
111238c8a9a5SSteve French 	struct TCP_Server_Info *server = p;
111338c8a9a5SSteve French 	unsigned int pdu_length;
111438c8a9a5SSteve French 	unsigned int next_offset;
111538c8a9a5SSteve French 	char *buf = NULL;
111638c8a9a5SSteve French 	struct task_struct *task_to_wake = NULL;
111738c8a9a5SSteve French 	struct mid_q_entry *mids[MAX_COMPOUND];
111838c8a9a5SSteve French 	char *bufs[MAX_COMPOUND];
111938c8a9a5SSteve French 	unsigned int noreclaim_flag, num_io_timeout = 0;
112069cba9d3SShyam Prasad N 	bool pending_reconnect = false;
112138c8a9a5SSteve French 
112238c8a9a5SSteve French 	noreclaim_flag = memalloc_noreclaim_save();
112338c8a9a5SSteve French 	cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
112438c8a9a5SSteve French 
112538c8a9a5SSteve French 	length = atomic_inc_return(&tcpSesAllocCount);
112638c8a9a5SSteve French 	if (length > 1)
112738c8a9a5SSteve French 		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
112838c8a9a5SSteve French 
112938c8a9a5SSteve French 	set_freezable();
113038c8a9a5SSteve French 	allow_kernel_signal(SIGKILL);
113138c8a9a5SSteve French 	while (server->tcpStatus != CifsExiting) {
113238c8a9a5SSteve French 		if (try_to_freeze())
113338c8a9a5SSteve French 			continue;
113438c8a9a5SSteve French 
113538c8a9a5SSteve French 		if (!allocate_buffers(server))
113638c8a9a5SSteve French 			continue;
113738c8a9a5SSteve French 
113838c8a9a5SSteve French 		server->large_buf = false;
113938c8a9a5SSteve French 		buf = server->smallbuf;
114038c8a9a5SSteve French 		pdu_length = 4; /* enough to get RFC1001 header */
114138c8a9a5SSteve French 
114238c8a9a5SSteve French 		length = cifs_read_from_socket(server, buf, pdu_length);
114338c8a9a5SSteve French 		if (length < 0)
114438c8a9a5SSteve French 			continue;
114538c8a9a5SSteve French 
114638c8a9a5SSteve French 		if (is_smb1(server))
114738c8a9a5SSteve French 			server->total_read = length;
114838c8a9a5SSteve French 		else
114938c8a9a5SSteve French 			server->total_read = 0;
115038c8a9a5SSteve French 
115138c8a9a5SSteve French 		/*
115238c8a9a5SSteve French 		 * The right amount was read from socket - 4 bytes,
115338c8a9a5SSteve French 		 * so we can now interpret the length field.
115438c8a9a5SSteve French 		 */
115538c8a9a5SSteve French 		pdu_length = get_rfc1002_length(buf);
115638c8a9a5SSteve French 
115738c8a9a5SSteve French 		cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
115838c8a9a5SSteve French 		if (!is_smb_response(server, buf[0]))
115938c8a9a5SSteve French 			continue;
116069cba9d3SShyam Prasad N 
116169cba9d3SShyam Prasad N 		pending_reconnect = false;
116238c8a9a5SSteve French next_pdu:
116338c8a9a5SSteve French 		server->pdu_size = pdu_length;
116438c8a9a5SSteve French 
116538c8a9a5SSteve French 		/* make sure we have enough to get to the MID */
116638c8a9a5SSteve French 		if (server->pdu_size < MID_HEADER_SIZE(server)) {
116738c8a9a5SSteve French 			cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
116838c8a9a5SSteve French 				 server->pdu_size);
116938c8a9a5SSteve French 			cifs_reconnect(server, true);
117038c8a9a5SSteve French 			continue;
117138c8a9a5SSteve French 		}
117238c8a9a5SSteve French 
117338c8a9a5SSteve French 		/* read down to the MID */
117438c8a9a5SSteve French 		length = cifs_read_from_socket(server,
117538c8a9a5SSteve French 			     buf + HEADER_PREAMBLE_SIZE(server),
117638c8a9a5SSteve French 			     MID_HEADER_SIZE(server));
117738c8a9a5SSteve French 		if (length < 0)
117838c8a9a5SSteve French 			continue;
117938c8a9a5SSteve French 		server->total_read += length;
118038c8a9a5SSteve French 
118138c8a9a5SSteve French 		if (server->ops->next_header) {
118238c8a9a5SSteve French 			next_offset = server->ops->next_header(buf);
118338c8a9a5SSteve French 			if (next_offset)
118438c8a9a5SSteve French 				server->pdu_size = next_offset;
118538c8a9a5SSteve French 		}
118638c8a9a5SSteve French 
118738c8a9a5SSteve French 		memset(mids, 0, sizeof(mids));
118838c8a9a5SSteve French 		memset(bufs, 0, sizeof(bufs));
118938c8a9a5SSteve French 		num_mids = 0;
119038c8a9a5SSteve French 
119138c8a9a5SSteve French 		if (server->ops->is_transform_hdr &&
119238c8a9a5SSteve French 		    server->ops->receive_transform &&
119338c8a9a5SSteve French 		    server->ops->is_transform_hdr(buf)) {
119438c8a9a5SSteve French 			length = server->ops->receive_transform(server,
119538c8a9a5SSteve French 								mids,
119638c8a9a5SSteve French 								bufs,
119738c8a9a5SSteve French 								&num_mids);
119838c8a9a5SSteve French 		} else {
119938c8a9a5SSteve French 			mids[0] = server->ops->find_mid(server, buf);
120038c8a9a5SSteve French 			bufs[0] = buf;
120138c8a9a5SSteve French 			num_mids = 1;
120238c8a9a5SSteve French 
120338c8a9a5SSteve French 			if (!mids[0] || !mids[0]->receive)
120438c8a9a5SSteve French 				length = standard_receive3(server, mids[0]);
120538c8a9a5SSteve French 			else
120638c8a9a5SSteve French 				length = mids[0]->receive(server, mids[0]);
120738c8a9a5SSteve French 		}
120838c8a9a5SSteve French 
120938c8a9a5SSteve French 		if (length < 0) {
121038c8a9a5SSteve French 			for (i = 0; i < num_mids; i++)
121138c8a9a5SSteve French 				if (mids[i])
121238c8a9a5SSteve French 					release_mid(mids[i]);
121338c8a9a5SSteve French 			continue;
121438c8a9a5SSteve French 		}
121538c8a9a5SSteve French 
121638c8a9a5SSteve French 		if (server->ops->is_status_io_timeout &&
121738c8a9a5SSteve French 		    server->ops->is_status_io_timeout(buf)) {
121838c8a9a5SSteve French 			num_io_timeout++;
121969cba9d3SShyam Prasad N 			if (num_io_timeout > MAX_STATUS_IO_TIMEOUT) {
122069cba9d3SShyam Prasad N 				cifs_server_dbg(VFS,
122169cba9d3SShyam Prasad N 						"Number of request timeouts exceeded %d. Reconnecting",
122269cba9d3SShyam Prasad N 						MAX_STATUS_IO_TIMEOUT);
122369cba9d3SShyam Prasad N 
122469cba9d3SShyam Prasad N 				pending_reconnect = true;
122538c8a9a5SSteve French 				num_io_timeout = 0;
122638c8a9a5SSteve French 			}
122738c8a9a5SSteve French 		}
122838c8a9a5SSteve French 
122938c8a9a5SSteve French 		server->lstrp = jiffies;
123038c8a9a5SSteve French 
123138c8a9a5SSteve French 		for (i = 0; i < num_mids; i++) {
123238c8a9a5SSteve French 			if (mids[i] != NULL) {
123338c8a9a5SSteve French 				mids[i]->resp_buf_size = server->pdu_size;
123438c8a9a5SSteve French 
1235c071b34fSShyam Prasad N 				if (bufs[i] != NULL) {
1236c071b34fSShyam Prasad N 					if (server->ops->is_network_name_deleted &&
123738c8a9a5SSteve French 					    server->ops->is_network_name_deleted(bufs[i],
1238c071b34fSShyam Prasad N 										 server)) {
1239c071b34fSShyam Prasad N 						cifs_server_dbg(FYI,
1240c071b34fSShyam Prasad N 								"Share deleted. Reconnect needed");
1241c071b34fSShyam Prasad N 					}
1242c071b34fSShyam Prasad N 				}
124338c8a9a5SSteve French 
124438c8a9a5SSteve French 				if (!mids[i]->multiRsp || mids[i]->multiEnd)
124538c8a9a5SSteve French 					mids[i]->callback(mids[i]);
124638c8a9a5SSteve French 
124738c8a9a5SSteve French 				release_mid(mids[i]);
124838c8a9a5SSteve French 			} else if (server->ops->is_oplock_break &&
124938c8a9a5SSteve French 				   server->ops->is_oplock_break(bufs[i],
125038c8a9a5SSteve French 								server)) {
125138c8a9a5SSteve French 				smb2_add_credits_from_hdr(bufs[i], server);
125238c8a9a5SSteve French 				cifs_dbg(FYI, "Received oplock break\n");
125338c8a9a5SSteve French 			} else {
125438c8a9a5SSteve French 				cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
125538c8a9a5SSteve French 						atomic_read(&mid_count));
125638c8a9a5SSteve French 				cifs_dump_mem("Received Data is: ", bufs[i],
125738c8a9a5SSteve French 					      HEADER_SIZE(server));
125838c8a9a5SSteve French 				smb2_add_credits_from_hdr(bufs[i], server);
125938c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2
126038c8a9a5SSteve French 				if (server->ops->dump_detail)
126138c8a9a5SSteve French 					server->ops->dump_detail(bufs[i],
126238c8a9a5SSteve French 								 server);
126338c8a9a5SSteve French 				cifs_dump_mids(server);
126438c8a9a5SSteve French #endif /* CIFS_DEBUG2 */
126538c8a9a5SSteve French 			}
126638c8a9a5SSteve French 		}
126738c8a9a5SSteve French 
126838c8a9a5SSteve French 		if (pdu_length > server->pdu_size) {
126938c8a9a5SSteve French 			if (!allocate_buffers(server))
127038c8a9a5SSteve French 				continue;
127138c8a9a5SSteve French 			pdu_length -= server->pdu_size;
127238c8a9a5SSteve French 			server->total_read = 0;
127338c8a9a5SSteve French 			server->large_buf = false;
127438c8a9a5SSteve French 			buf = server->smallbuf;
127538c8a9a5SSteve French 			goto next_pdu;
127638c8a9a5SSteve French 		}
127769cba9d3SShyam Prasad N 
127869cba9d3SShyam Prasad N 		/* do this reconnect at the very end after processing all MIDs */
127969cba9d3SShyam Prasad N 		if (pending_reconnect)
128069cba9d3SShyam Prasad N 			cifs_reconnect(server, true);
128169cba9d3SShyam Prasad N 
128238c8a9a5SSteve French 	} /* end while !EXITING */
128338c8a9a5SSteve French 
128438c8a9a5SSteve French 	/* buffer usually freed in free_mid - need to free it here on exit */
128538c8a9a5SSteve French 	cifs_buf_release(server->bigbuf);
128638c8a9a5SSteve French 	if (server->smallbuf) /* no sense logging a debug message if NULL */
128738c8a9a5SSteve French 		cifs_small_buf_release(server->smallbuf);
128838c8a9a5SSteve French 
128938c8a9a5SSteve French 	task_to_wake = xchg(&server->tsk, NULL);
129038c8a9a5SSteve French 	clean_demultiplex_info(server);
129138c8a9a5SSteve French 
129238c8a9a5SSteve French 	/* if server->tsk was NULL then wait for a signal before exiting */
129338c8a9a5SSteve French 	if (!task_to_wake) {
129438c8a9a5SSteve French 		set_current_state(TASK_INTERRUPTIBLE);
129538c8a9a5SSteve French 		while (!signal_pending(current)) {
129638c8a9a5SSteve French 			schedule();
129738c8a9a5SSteve French 			set_current_state(TASK_INTERRUPTIBLE);
129838c8a9a5SSteve French 		}
129938c8a9a5SSteve French 		set_current_state(TASK_RUNNING);
130038c8a9a5SSteve French 	}
130138c8a9a5SSteve French 
130238c8a9a5SSteve French 	memalloc_noreclaim_restore(noreclaim_flag);
130338c8a9a5SSteve French 	module_put_and_kthread_exit(0);
130438c8a9a5SSteve French }
130538c8a9a5SSteve French 
13062991b774SShyam Prasad N int
13072991b774SShyam Prasad N cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs)
13082991b774SShyam Prasad N {
13092991b774SShyam Prasad N 	struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
13102991b774SShyam Prasad N 	struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
13112991b774SShyam Prasad N 	struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
13122991b774SShyam Prasad N 	struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
13132991b774SShyam Prasad N 
13142991b774SShyam Prasad N 	switch (srcaddr->sa_family) {
13152991b774SShyam Prasad N 	case AF_UNSPEC:
13162991b774SShyam Prasad N 		switch (rhs->sa_family) {
13172991b774SShyam Prasad N 		case AF_UNSPEC:
13182991b774SShyam Prasad N 			return 0;
13192991b774SShyam Prasad N 		case AF_INET:
13202991b774SShyam Prasad N 		case AF_INET6:
13212991b774SShyam Prasad N 			return 1;
13222991b774SShyam Prasad N 		default:
13232991b774SShyam Prasad N 			return -1;
13242991b774SShyam Prasad N 		}
13252991b774SShyam Prasad N 	case AF_INET: {
13262991b774SShyam Prasad N 		switch (rhs->sa_family) {
13272991b774SShyam Prasad N 		case AF_UNSPEC:
13282991b774SShyam Prasad N 			return -1;
13292991b774SShyam Prasad N 		case AF_INET:
13302991b774SShyam Prasad N 			return memcmp(saddr4, vaddr4,
13312991b774SShyam Prasad N 				      sizeof(struct sockaddr_in));
13322991b774SShyam Prasad N 		case AF_INET6:
13332991b774SShyam Prasad N 			return 1;
13342991b774SShyam Prasad N 		default:
13352991b774SShyam Prasad N 			return -1;
13362991b774SShyam Prasad N 		}
13372991b774SShyam Prasad N 	}
13382991b774SShyam Prasad N 	case AF_INET6: {
13392991b774SShyam Prasad N 		switch (rhs->sa_family) {
13402991b774SShyam Prasad N 		case AF_UNSPEC:
13412991b774SShyam Prasad N 		case AF_INET:
13422991b774SShyam Prasad N 			return -1;
13432991b774SShyam Prasad N 		case AF_INET6:
13442991b774SShyam Prasad N 			return memcmp(saddr6,
13452991b774SShyam Prasad N 				      vaddr6,
13462991b774SShyam Prasad N 				      sizeof(struct sockaddr_in6));
13472991b774SShyam Prasad N 		default:
13482991b774SShyam Prasad N 			return -1;
13492991b774SShyam Prasad N 		}
13502991b774SShyam Prasad N 	}
13512991b774SShyam Prasad N 	default:
13522991b774SShyam Prasad N 		return -1; /* don't expect to be here */
13532991b774SShyam Prasad N 	}
13542991b774SShyam Prasad N }
13552991b774SShyam Prasad N 
135638c8a9a5SSteve French /*
135738c8a9a5SSteve French  * Returns true if srcaddr isn't specified and rhs isn't specified, or
135838c8a9a5SSteve French  * if srcaddr is specified and matches the IP address of the rhs argument
135938c8a9a5SSteve French  */
136038c8a9a5SSteve French bool
136138c8a9a5SSteve French cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
136238c8a9a5SSteve French {
136338c8a9a5SSteve French 	switch (srcaddr->sa_family) {
136438c8a9a5SSteve French 	case AF_UNSPEC:
136538c8a9a5SSteve French 		return (rhs->sa_family == AF_UNSPEC);
136638c8a9a5SSteve French 	case AF_INET: {
136738c8a9a5SSteve French 		struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
136838c8a9a5SSteve French 		struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
136938c8a9a5SSteve French 		return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
137038c8a9a5SSteve French 	}
137138c8a9a5SSteve French 	case AF_INET6: {
137238c8a9a5SSteve French 		struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
137338c8a9a5SSteve French 		struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
137438c8a9a5SSteve French 		return (ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr)
137538c8a9a5SSteve French 			&& saddr6->sin6_scope_id == vaddr6->sin6_scope_id);
137638c8a9a5SSteve French 	}
137738c8a9a5SSteve French 	default:
137838c8a9a5SSteve French 		WARN_ON(1);
137938c8a9a5SSteve French 		return false; /* don't expect to be here */
138038c8a9a5SSteve French 	}
138138c8a9a5SSteve French }
138238c8a9a5SSteve French 
138338c8a9a5SSteve French /*
138438c8a9a5SSteve French  * If no port is specified in addr structure, we try to match with 445 port
138538c8a9a5SSteve French  * and if it fails - with 139 ports. It should be called only if address
138638c8a9a5SSteve French  * families of server and addr are equal.
138738c8a9a5SSteve French  */
138838c8a9a5SSteve French static bool
138938c8a9a5SSteve French match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
139038c8a9a5SSteve French {
139138c8a9a5SSteve French 	__be16 port, *sport;
139238c8a9a5SSteve French 
139338c8a9a5SSteve French 	/* SMBDirect manages its own ports, don't match it here */
139438c8a9a5SSteve French 	if (server->rdma)
139538c8a9a5SSteve French 		return true;
139638c8a9a5SSteve French 
139738c8a9a5SSteve French 	switch (addr->sa_family) {
139838c8a9a5SSteve French 	case AF_INET:
139938c8a9a5SSteve French 		sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
140038c8a9a5SSteve French 		port = ((struct sockaddr_in *) addr)->sin_port;
140138c8a9a5SSteve French 		break;
140238c8a9a5SSteve French 	case AF_INET6:
140338c8a9a5SSteve French 		sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
140438c8a9a5SSteve French 		port = ((struct sockaddr_in6 *) addr)->sin6_port;
140538c8a9a5SSteve French 		break;
140638c8a9a5SSteve French 	default:
140738c8a9a5SSteve French 		WARN_ON(1);
140838c8a9a5SSteve French 		return false;
140938c8a9a5SSteve French 	}
141038c8a9a5SSteve French 
141138c8a9a5SSteve French 	if (!port) {
141238c8a9a5SSteve French 		port = htons(CIFS_PORT);
141338c8a9a5SSteve French 		if (port == *sport)
141438c8a9a5SSteve French 			return true;
141538c8a9a5SSteve French 
141638c8a9a5SSteve French 		port = htons(RFC1001_PORT);
141738c8a9a5SSteve French 	}
141838c8a9a5SSteve French 
141938c8a9a5SSteve French 	return port == *sport;
142038c8a9a5SSteve French }
142138c8a9a5SSteve French 
142238c8a9a5SSteve French static bool match_server_address(struct TCP_Server_Info *server, struct sockaddr *addr)
142338c8a9a5SSteve French {
142438c8a9a5SSteve French 	if (!cifs_match_ipaddr(addr, (struct sockaddr *)&server->dstaddr))
142538c8a9a5SSteve French 		return false;
142638c8a9a5SSteve French 
142738c8a9a5SSteve French 	return true;
142838c8a9a5SSteve French }
142938c8a9a5SSteve French 
143038c8a9a5SSteve French static bool
143138c8a9a5SSteve French match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
143238c8a9a5SSteve French {
143338c8a9a5SSteve French 	/*
143438c8a9a5SSteve French 	 * The select_sectype function should either return the ctx->sectype
143538c8a9a5SSteve French 	 * that was specified, or "Unspecified" if that sectype was not
143638c8a9a5SSteve French 	 * compatible with the given NEGOTIATE request.
143738c8a9a5SSteve French 	 */
143838c8a9a5SSteve French 	if (server->ops->select_sectype(server, ctx->sectype)
143938c8a9a5SSteve French 	     == Unspecified)
144038c8a9a5SSteve French 		return false;
144138c8a9a5SSteve French 
144238c8a9a5SSteve French 	/*
144338c8a9a5SSteve French 	 * Now check if signing mode is acceptable. No need to check
144438c8a9a5SSteve French 	 * global_secflags at this point since if MUST_SIGN is set then
144538c8a9a5SSteve French 	 * the server->sign had better be too.
144638c8a9a5SSteve French 	 */
144738c8a9a5SSteve French 	if (ctx->sign && !server->sign)
144838c8a9a5SSteve French 		return false;
144938c8a9a5SSteve French 
145038c8a9a5SSteve French 	return true;
145138c8a9a5SSteve French }
145238c8a9a5SSteve French 
145338c8a9a5SSteve French /* this function must be called with srv_lock held */
14543ae872deSPaulo Alcantara static int match_server(struct TCP_Server_Info *server,
14553ae872deSPaulo Alcantara 			struct smb3_fs_context *ctx,
14563ae872deSPaulo Alcantara 			bool match_super)
145738c8a9a5SSteve French {
145838c8a9a5SSteve French 	struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
145938c8a9a5SSteve French 
146038c8a9a5SSteve French 	lockdep_assert_held(&server->srv_lock);
146138c8a9a5SSteve French 
146238c8a9a5SSteve French 	if (ctx->nosharesock)
146338c8a9a5SSteve French 		return 0;
146438c8a9a5SSteve French 
146538c8a9a5SSteve French 	/* this server does not share socket */
146638c8a9a5SSteve French 	if (server->nosharesock)
146738c8a9a5SSteve French 		return 0;
146838c8a9a5SSteve French 
146938c8a9a5SSteve French 	/* If multidialect negotiation see if existing sessions match one */
147038c8a9a5SSteve French 	if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
147138c8a9a5SSteve French 		if (server->vals->protocol_id < SMB30_PROT_ID)
147238c8a9a5SSteve French 			return 0;
147338c8a9a5SSteve French 	} else if (strcmp(ctx->vals->version_string,
147438c8a9a5SSteve French 		   SMBDEFAULT_VERSION_STRING) == 0) {
147538c8a9a5SSteve French 		if (server->vals->protocol_id < SMB21_PROT_ID)
147638c8a9a5SSteve French 			return 0;
147738c8a9a5SSteve French 	} else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
147838c8a9a5SSteve French 		return 0;
147938c8a9a5SSteve French 
148038c8a9a5SSteve French 	if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
148138c8a9a5SSteve French 		return 0;
148238c8a9a5SSteve French 
148338c8a9a5SSteve French 	if (!cifs_match_ipaddr((struct sockaddr *)&ctx->srcaddr,
148438c8a9a5SSteve French 			       (struct sockaddr *)&server->srcaddr))
148538c8a9a5SSteve French 		return 0;
148638c8a9a5SSteve French 	/*
14873ae872deSPaulo Alcantara 	 * When matching cifs.ko superblocks (@match_super == true), we can't
14883ae872deSPaulo Alcantara 	 * really match either @server->leaf_fullpath or @server->dstaddr
14893ae872deSPaulo Alcantara 	 * directly since this @server might belong to a completely different
14903ae872deSPaulo Alcantara 	 * server -- in case of domain-based DFS referrals or DFS links -- as
14913ae872deSPaulo Alcantara 	 * provided earlier by mount(2) through 'source' and 'ip' options.
14923ae872deSPaulo Alcantara 	 *
14933ae872deSPaulo Alcantara 	 * Otherwise, match the DFS referral in @server->leaf_fullpath or the
14943ae872deSPaulo Alcantara 	 * destination address in @server->dstaddr.
14953ae872deSPaulo Alcantara 	 *
14963ae872deSPaulo Alcantara 	 * When using 'nodfs' mount option, we avoid sharing it with DFS
14973ae872deSPaulo Alcantara 	 * connections as they might failover.
149838c8a9a5SSteve French 	 */
14993ae872deSPaulo Alcantara 	if (!match_super) {
150038c8a9a5SSteve French 		if (!ctx->nodfs) {
15013ae872deSPaulo Alcantara 			if (server->leaf_fullpath) {
150238c8a9a5SSteve French 				if (!ctx->leaf_fullpath ||
150338c8a9a5SSteve French 				    strcasecmp(server->leaf_fullpath,
150438c8a9a5SSteve French 					       ctx->leaf_fullpath))
150538c8a9a5SSteve French 					return 0;
150638c8a9a5SSteve French 			} else if (ctx->leaf_fullpath) {
150738c8a9a5SSteve French 				return 0;
150838c8a9a5SSteve French 			}
15093ae872deSPaulo Alcantara 		} else if (server->leaf_fullpath) {
151038c8a9a5SSteve French 			return 0;
151138c8a9a5SSteve French 		}
15123ae872deSPaulo Alcantara 	}
151338c8a9a5SSteve French 
151438c8a9a5SSteve French 	/*
151538c8a9a5SSteve French 	 * Match for a regular connection (address/hostname/port) which has no
151638c8a9a5SSteve French 	 * DFS referrals set.
151738c8a9a5SSteve French 	 */
15183ae872deSPaulo Alcantara 	if (!server->leaf_fullpath &&
151938c8a9a5SSteve French 	    (strcasecmp(server->hostname, ctx->server_hostname) ||
152038c8a9a5SSteve French 	     !match_server_address(server, addr) ||
152138c8a9a5SSteve French 	     !match_port(server, addr)))
152238c8a9a5SSteve French 		return 0;
152338c8a9a5SSteve French 
152438c8a9a5SSteve French 	if (!match_security(server, ctx))
152538c8a9a5SSteve French 		return 0;
152638c8a9a5SSteve French 
152738c8a9a5SSteve French 	if (server->echo_interval != ctx->echo_interval * HZ)
152838c8a9a5SSteve French 		return 0;
152938c8a9a5SSteve French 
153038c8a9a5SSteve French 	if (server->rdma != ctx->rdma)
153138c8a9a5SSteve French 		return 0;
153238c8a9a5SSteve French 
153338c8a9a5SSteve French 	if (server->ignore_signature != ctx->ignore_signature)
153438c8a9a5SSteve French 		return 0;
153538c8a9a5SSteve French 
153638c8a9a5SSteve French 	if (server->min_offload != ctx->min_offload)
153738c8a9a5SSteve French 		return 0;
153838c8a9a5SSteve French 
153938c8a9a5SSteve French 	return 1;
154038c8a9a5SSteve French }
154138c8a9a5SSteve French 
154238c8a9a5SSteve French struct TCP_Server_Info *
154338c8a9a5SSteve French cifs_find_tcp_session(struct smb3_fs_context *ctx)
154438c8a9a5SSteve French {
154538c8a9a5SSteve French 	struct TCP_Server_Info *server;
154638c8a9a5SSteve French 
154738c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
154838c8a9a5SSteve French 	list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
154938c8a9a5SSteve French 		spin_lock(&server->srv_lock);
155038c8a9a5SSteve French 		/*
155138c8a9a5SSteve French 		 * Skip ses channels since they're only handled in lower layers
155238c8a9a5SSteve French 		 * (e.g. cifs_send_recv).
155338c8a9a5SSteve French 		 */
1554b3773b19SSteve French 		if (SERVER_IS_CHAN(server) ||
15553ae872deSPaulo Alcantara 		    !match_server(server, ctx, false)) {
155638c8a9a5SSteve French 			spin_unlock(&server->srv_lock);
155738c8a9a5SSteve French 			continue;
155838c8a9a5SSteve French 		}
155938c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
156038c8a9a5SSteve French 
156138c8a9a5SSteve French 		++server->srv_count;
156238c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
156338c8a9a5SSteve French 		cifs_dbg(FYI, "Existing tcp session with server found\n");
156438c8a9a5SSteve French 		return server;
156538c8a9a5SSteve French 	}
156638c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
156738c8a9a5SSteve French 	return NULL;
156838c8a9a5SSteve French }
156938c8a9a5SSteve French 
157038c8a9a5SSteve French void
157138c8a9a5SSteve French cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
157238c8a9a5SSteve French {
157338c8a9a5SSteve French 	struct task_struct *task;
157438c8a9a5SSteve French 
157538c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
157638c8a9a5SSteve French 	if (--server->srv_count > 0) {
157738c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
157838c8a9a5SSteve French 		return;
157938c8a9a5SSteve French 	}
158038c8a9a5SSteve French 
158138c8a9a5SSteve French 	/* srv_count can never go negative */
158238c8a9a5SSteve French 	WARN_ON(server->srv_count < 0);
158338c8a9a5SSteve French 
158438c8a9a5SSteve French 	put_net(cifs_net_ns(server));
158538c8a9a5SSteve French 
158638c8a9a5SSteve French 	list_del_init(&server->tcp_ses_list);
158738c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
158838c8a9a5SSteve French 
158938c8a9a5SSteve French 	/* For secondary channels, we pick up ref-count on the primary server */
1590b3773b19SSteve French 	if (SERVER_IS_CHAN(server))
159138c8a9a5SSteve French 		cifs_put_tcp_session(server->primary_server, from_reconnect);
159238c8a9a5SSteve French 
159338c8a9a5SSteve French 	cancel_delayed_work_sync(&server->echo);
159438c8a9a5SSteve French 
159538c8a9a5SSteve French 	if (from_reconnect)
159638c8a9a5SSteve French 		/*
159738c8a9a5SSteve French 		 * Avoid deadlock here: reconnect work calls
159838c8a9a5SSteve French 		 * cifs_put_tcp_session() at its end. Need to be sure
159938c8a9a5SSteve French 		 * that reconnect work does nothing with server pointer after
160038c8a9a5SSteve French 		 * that step.
160138c8a9a5SSteve French 		 */
160238c8a9a5SSteve French 		cancel_delayed_work(&server->reconnect);
160338c8a9a5SSteve French 	else
160438c8a9a5SSteve French 		cancel_delayed_work_sync(&server->reconnect);
160538c8a9a5SSteve French 
160638c8a9a5SSteve French 	spin_lock(&server->srv_lock);
160738c8a9a5SSteve French 	server->tcpStatus = CifsExiting;
160838c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
160938c8a9a5SSteve French 
161038c8a9a5SSteve French 	cifs_crypto_secmech_release(server);
161138c8a9a5SSteve French 
161238c8a9a5SSteve French 	kfree_sensitive(server->session_key.response);
161338c8a9a5SSteve French 	server->session_key.response = NULL;
161438c8a9a5SSteve French 	server->session_key.len = 0;
161538c8a9a5SSteve French 	kfree(server->hostname);
161638c8a9a5SSteve French 	server->hostname = NULL;
161738c8a9a5SSteve French 
161838c8a9a5SSteve French 	task = xchg(&server->tsk, NULL);
161938c8a9a5SSteve French 	if (task)
162038c8a9a5SSteve French 		send_sig(SIGKILL, task, 1);
162138c8a9a5SSteve French }
162238c8a9a5SSteve French 
162338c8a9a5SSteve French struct TCP_Server_Info *
162438c8a9a5SSteve French cifs_get_tcp_session(struct smb3_fs_context *ctx,
162538c8a9a5SSteve French 		     struct TCP_Server_Info *primary_server)
162638c8a9a5SSteve French {
162738c8a9a5SSteve French 	struct TCP_Server_Info *tcp_ses = NULL;
162838c8a9a5SSteve French 	int rc;
162938c8a9a5SSteve French 
163038c8a9a5SSteve French 	cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
163138c8a9a5SSteve French 
163238c8a9a5SSteve French 	/* see if we already have a matching tcp_ses */
163338c8a9a5SSteve French 	tcp_ses = cifs_find_tcp_session(ctx);
163438c8a9a5SSteve French 	if (tcp_ses)
163538c8a9a5SSteve French 		return tcp_ses;
163638c8a9a5SSteve French 
163738c8a9a5SSteve French 	tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
163838c8a9a5SSteve French 	if (!tcp_ses) {
163938c8a9a5SSteve French 		rc = -ENOMEM;
164038c8a9a5SSteve French 		goto out_err;
164138c8a9a5SSteve French 	}
164238c8a9a5SSteve French 
164338c8a9a5SSteve French 	tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
164438c8a9a5SSteve French 	if (!tcp_ses->hostname) {
164538c8a9a5SSteve French 		rc = -ENOMEM;
164638c8a9a5SSteve French 		goto out_err;
164738c8a9a5SSteve French 	}
164838c8a9a5SSteve French 
164938c8a9a5SSteve French 	if (ctx->leaf_fullpath) {
165038c8a9a5SSteve French 		tcp_ses->leaf_fullpath = kstrdup(ctx->leaf_fullpath, GFP_KERNEL);
165138c8a9a5SSteve French 		if (!tcp_ses->leaf_fullpath) {
165238c8a9a5SSteve French 			rc = -ENOMEM;
165338c8a9a5SSteve French 			goto out_err;
165438c8a9a5SSteve French 		}
165538c8a9a5SSteve French 	}
165638c8a9a5SSteve French 
165738c8a9a5SSteve French 	if (ctx->nosharesock)
165838c8a9a5SSteve French 		tcp_ses->nosharesock = true;
165938c8a9a5SSteve French 
166038c8a9a5SSteve French 	tcp_ses->ops = ctx->ops;
166138c8a9a5SSteve French 	tcp_ses->vals = ctx->vals;
166238c8a9a5SSteve French 	cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
166338c8a9a5SSteve French 
166438c8a9a5SSteve French 	tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
166538c8a9a5SSteve French 	tcp_ses->noblockcnt = ctx->rootfs;
166638c8a9a5SSteve French 	tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
166738c8a9a5SSteve French 	tcp_ses->noautotune = ctx->noautotune;
166838c8a9a5SSteve French 	tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
166938c8a9a5SSteve French 	tcp_ses->rdma = ctx->rdma;
167038c8a9a5SSteve French 	tcp_ses->in_flight = 0;
167138c8a9a5SSteve French 	tcp_ses->max_in_flight = 0;
167238c8a9a5SSteve French 	tcp_ses->credits = 1;
167338c8a9a5SSteve French 	if (primary_server) {
167438c8a9a5SSteve French 		spin_lock(&cifs_tcp_ses_lock);
167538c8a9a5SSteve French 		++primary_server->srv_count;
167638c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
167738c8a9a5SSteve French 		tcp_ses->primary_server = primary_server;
167838c8a9a5SSteve French 	}
167938c8a9a5SSteve French 	init_waitqueue_head(&tcp_ses->response_q);
168038c8a9a5SSteve French 	init_waitqueue_head(&tcp_ses->request_q);
168138c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
168238c8a9a5SSteve French 	mutex_init(&tcp_ses->_srv_mutex);
168338c8a9a5SSteve French 	memcpy(tcp_ses->workstation_RFC1001_name,
168438c8a9a5SSteve French 		ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
168538c8a9a5SSteve French 	memcpy(tcp_ses->server_RFC1001_name,
168638c8a9a5SSteve French 		ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
168738c8a9a5SSteve French 	tcp_ses->session_estab = false;
168838c8a9a5SSteve French 	tcp_ses->sequence_number = 0;
168909ee7a3bSSteve French 	tcp_ses->channel_sequence_num = 0; /* only tracked for primary channel */
169038c8a9a5SSteve French 	tcp_ses->reconnect_instance = 1;
169138c8a9a5SSteve French 	tcp_ses->lstrp = jiffies;
169238c8a9a5SSteve French 	tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
169338c8a9a5SSteve French 	spin_lock_init(&tcp_ses->req_lock);
169438c8a9a5SSteve French 	spin_lock_init(&tcp_ses->srv_lock);
169538c8a9a5SSteve French 	spin_lock_init(&tcp_ses->mid_lock);
169638c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
169738c8a9a5SSteve French 	INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
169838c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
169938c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
170038c8a9a5SSteve French 	mutex_init(&tcp_ses->reconnect_mutex);
170138c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
170238c8a9a5SSteve French 	mutex_init(&tcp_ses->refpath_lock);
170338c8a9a5SSteve French #endif
170438c8a9a5SSteve French 	memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
170538c8a9a5SSteve French 	       sizeof(tcp_ses->srcaddr));
170638c8a9a5SSteve French 	memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
170738c8a9a5SSteve French 		sizeof(tcp_ses->dstaddr));
170838c8a9a5SSteve French 	if (ctx->use_client_guid)
170938c8a9a5SSteve French 		memcpy(tcp_ses->client_guid, ctx->client_guid,
171038c8a9a5SSteve French 		       SMB2_CLIENT_GUID_SIZE);
171138c8a9a5SSteve French 	else
171238c8a9a5SSteve French 		generate_random_uuid(tcp_ses->client_guid);
171338c8a9a5SSteve French 	/*
171438c8a9a5SSteve French 	 * at this point we are the only ones with the pointer
171538c8a9a5SSteve French 	 * to the struct since the kernel thread not created yet
171638c8a9a5SSteve French 	 * no need to spinlock this init of tcpStatus or srv_count
171738c8a9a5SSteve French 	 */
171838c8a9a5SSteve French 	tcp_ses->tcpStatus = CifsNew;
171938c8a9a5SSteve French 	++tcp_ses->srv_count;
172038c8a9a5SSteve French 
172138c8a9a5SSteve French 	if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
172238c8a9a5SSteve French 		ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
172338c8a9a5SSteve French 		tcp_ses->echo_interval = ctx->echo_interval * HZ;
172438c8a9a5SSteve French 	else
172538c8a9a5SSteve French 		tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
172638c8a9a5SSteve French 	if (tcp_ses->rdma) {
172738c8a9a5SSteve French #ifndef CONFIG_CIFS_SMB_DIRECT
172838c8a9a5SSteve French 		cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
172938c8a9a5SSteve French 		rc = -ENOENT;
173038c8a9a5SSteve French 		goto out_err_crypto_release;
173138c8a9a5SSteve French #endif
173238c8a9a5SSteve French 		tcp_ses->smbd_conn = smbd_get_connection(
173338c8a9a5SSteve French 			tcp_ses, (struct sockaddr *)&ctx->dstaddr);
173438c8a9a5SSteve French 		if (tcp_ses->smbd_conn) {
173538c8a9a5SSteve French 			cifs_dbg(VFS, "RDMA transport established\n");
173638c8a9a5SSteve French 			rc = 0;
173738c8a9a5SSteve French 			goto smbd_connected;
173838c8a9a5SSteve French 		} else {
173938c8a9a5SSteve French 			rc = -ENOENT;
174038c8a9a5SSteve French 			goto out_err_crypto_release;
174138c8a9a5SSteve French 		}
174238c8a9a5SSteve French 	}
174338c8a9a5SSteve French 	rc = ip_connect(tcp_ses);
174438c8a9a5SSteve French 	if (rc < 0) {
174538c8a9a5SSteve French 		cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
174638c8a9a5SSteve French 		goto out_err_crypto_release;
174738c8a9a5SSteve French 	}
174838c8a9a5SSteve French smbd_connected:
174938c8a9a5SSteve French 	/*
175038c8a9a5SSteve French 	 * since we're in a cifs function already, we know that
175138c8a9a5SSteve French 	 * this will succeed. No need for try_module_get().
175238c8a9a5SSteve French 	 */
175338c8a9a5SSteve French 	__module_get(THIS_MODULE);
175438c8a9a5SSteve French 	tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
175538c8a9a5SSteve French 				  tcp_ses, "cifsd");
175638c8a9a5SSteve French 	if (IS_ERR(tcp_ses->tsk)) {
175738c8a9a5SSteve French 		rc = PTR_ERR(tcp_ses->tsk);
175838c8a9a5SSteve French 		cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
175938c8a9a5SSteve French 		module_put(THIS_MODULE);
176038c8a9a5SSteve French 		goto out_err_crypto_release;
176138c8a9a5SSteve French 	}
176238c8a9a5SSteve French 	tcp_ses->min_offload = ctx->min_offload;
176338c8a9a5SSteve French 	/*
176438c8a9a5SSteve French 	 * at this point we are the only ones with the pointer
176538c8a9a5SSteve French 	 * to the struct since the kernel thread not created yet
176638c8a9a5SSteve French 	 * no need to spinlock this update of tcpStatus
176738c8a9a5SSteve French 	 */
176838c8a9a5SSteve French 	spin_lock(&tcp_ses->srv_lock);
176938c8a9a5SSteve French 	tcp_ses->tcpStatus = CifsNeedNegotiate;
177038c8a9a5SSteve French 	spin_unlock(&tcp_ses->srv_lock);
177138c8a9a5SSteve French 
177238c8a9a5SSteve French 	if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
177338c8a9a5SSteve French 		tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
177438c8a9a5SSteve French 	else
177538c8a9a5SSteve French 		tcp_ses->max_credits = ctx->max_credits;
177638c8a9a5SSteve French 
177738c8a9a5SSteve French 	tcp_ses->nr_targets = 1;
177838c8a9a5SSteve French 	tcp_ses->ignore_signature = ctx->ignore_signature;
177938c8a9a5SSteve French 	/* thread spawned, put it on the list */
178038c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
178138c8a9a5SSteve French 	list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
178238c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
178338c8a9a5SSteve French 
178438c8a9a5SSteve French 	/* queue echo request delayed work */
178538c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
178638c8a9a5SSteve French 
178738c8a9a5SSteve French 	return tcp_ses;
178838c8a9a5SSteve French 
178938c8a9a5SSteve French out_err_crypto_release:
179038c8a9a5SSteve French 	cifs_crypto_secmech_release(tcp_ses);
179138c8a9a5SSteve French 
179238c8a9a5SSteve French 	put_net(cifs_net_ns(tcp_ses));
179338c8a9a5SSteve French 
179438c8a9a5SSteve French out_err:
179538c8a9a5SSteve French 	if (tcp_ses) {
1796b3773b19SSteve French 		if (SERVER_IS_CHAN(tcp_ses))
179738c8a9a5SSteve French 			cifs_put_tcp_session(tcp_ses->primary_server, false);
179838c8a9a5SSteve French 		kfree(tcp_ses->hostname);
179938c8a9a5SSteve French 		kfree(tcp_ses->leaf_fullpath);
180038c8a9a5SSteve French 		if (tcp_ses->ssocket)
180138c8a9a5SSteve French 			sock_release(tcp_ses->ssocket);
180238c8a9a5SSteve French 		kfree(tcp_ses);
180338c8a9a5SSteve French 	}
180438c8a9a5SSteve French 	return ERR_PTR(rc);
180538c8a9a5SSteve French }
180638c8a9a5SSteve French 
180738c8a9a5SSteve French /* this function must be called with ses_lock and chan_lock held */
180838c8a9a5SSteve French static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
180938c8a9a5SSteve French {
181038c8a9a5SSteve French 	if (ctx->sectype != Unspecified &&
181138c8a9a5SSteve French 	    ctx->sectype != ses->sectype)
181238c8a9a5SSteve French 		return 0;
181338c8a9a5SSteve French 
181438c8a9a5SSteve French 	/*
181538c8a9a5SSteve French 	 * If an existing session is limited to less channels than
181638c8a9a5SSteve French 	 * requested, it should not be reused
181738c8a9a5SSteve French 	 */
181838c8a9a5SSteve French 	if (ses->chan_max < ctx->max_channels)
181938c8a9a5SSteve French 		return 0;
182038c8a9a5SSteve French 
182138c8a9a5SSteve French 	switch (ses->sectype) {
182238c8a9a5SSteve French 	case Kerberos:
182338c8a9a5SSteve French 		if (!uid_eq(ctx->cred_uid, ses->cred_uid))
182438c8a9a5SSteve French 			return 0;
182538c8a9a5SSteve French 		break;
182638c8a9a5SSteve French 	default:
182738c8a9a5SSteve French 		/* NULL username means anonymous session */
182838c8a9a5SSteve French 		if (ses->user_name == NULL) {
182938c8a9a5SSteve French 			if (!ctx->nullauth)
183038c8a9a5SSteve French 				return 0;
183138c8a9a5SSteve French 			break;
183238c8a9a5SSteve French 		}
183338c8a9a5SSteve French 
183438c8a9a5SSteve French 		/* anything else takes username/password */
183538c8a9a5SSteve French 		if (strncmp(ses->user_name,
183638c8a9a5SSteve French 			    ctx->username ? ctx->username : "",
183738c8a9a5SSteve French 			    CIFS_MAX_USERNAME_LEN))
183838c8a9a5SSteve French 			return 0;
183938c8a9a5SSteve French 		if ((ctx->username && strlen(ctx->username) != 0) &&
184038c8a9a5SSteve French 		    ses->password != NULL &&
184138c8a9a5SSteve French 		    strncmp(ses->password,
184238c8a9a5SSteve French 			    ctx->password ? ctx->password : "",
184338c8a9a5SSteve French 			    CIFS_MAX_PASSWORD_LEN))
184438c8a9a5SSteve French 			return 0;
184538c8a9a5SSteve French 	}
1846a43f95fdSWinston Wen 
1847a43f95fdSWinston Wen 	if (strcmp(ctx->local_nls->charset, ses->local_nls->charset))
1848a43f95fdSWinston Wen 		return 0;
1849a43f95fdSWinston Wen 
185038c8a9a5SSteve French 	return 1;
185138c8a9a5SSteve French }
185238c8a9a5SSteve French 
185338c8a9a5SSteve French /**
185438c8a9a5SSteve French  * cifs_setup_ipc - helper to setup the IPC tcon for the session
185538c8a9a5SSteve French  * @ses: smb session to issue the request on
185638c8a9a5SSteve French  * @ctx: the superblock configuration context to use for building the
185738c8a9a5SSteve French  *       new tree connection for the IPC (interprocess communication RPC)
185838c8a9a5SSteve French  *
185938c8a9a5SSteve French  * A new IPC connection is made and stored in the session
186038c8a9a5SSteve French  * tcon_ipc. The IPC tcon has the same lifetime as the session.
186138c8a9a5SSteve French  */
186238c8a9a5SSteve French static int
186338c8a9a5SSteve French cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
186438c8a9a5SSteve French {
186538c8a9a5SSteve French 	int rc = 0, xid;
186638c8a9a5SSteve French 	struct cifs_tcon *tcon;
186738c8a9a5SSteve French 	char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
186838c8a9a5SSteve French 	bool seal = false;
186938c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
187038c8a9a5SSteve French 
187138c8a9a5SSteve French 	/*
187238c8a9a5SSteve French 	 * If the mount request that resulted in the creation of the
187338c8a9a5SSteve French 	 * session requires encryption, force IPC to be encrypted too.
187438c8a9a5SSteve French 	 */
187538c8a9a5SSteve French 	if (ctx->seal) {
187638c8a9a5SSteve French 		if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
187738c8a9a5SSteve French 			seal = true;
187838c8a9a5SSteve French 		else {
187938c8a9a5SSteve French 			cifs_server_dbg(VFS,
188038c8a9a5SSteve French 				 "IPC: server doesn't support encryption\n");
188138c8a9a5SSteve French 			return -EOPNOTSUPP;
188238c8a9a5SSteve French 		}
188338c8a9a5SSteve French 	}
188438c8a9a5SSteve French 
188538c8a9a5SSteve French 	tcon = tconInfoAlloc();
188638c8a9a5SSteve French 	if (tcon == NULL)
188738c8a9a5SSteve French 		return -ENOMEM;
188838c8a9a5SSteve French 
188938c8a9a5SSteve French 	spin_lock(&server->srv_lock);
189038c8a9a5SSteve French 	scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
189138c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
189238c8a9a5SSteve French 
189338c8a9a5SSteve French 	xid = get_xid();
189438c8a9a5SSteve French 	tcon->ses = ses;
189538c8a9a5SSteve French 	tcon->ipc = true;
189638c8a9a5SSteve French 	tcon->seal = seal;
189738c8a9a5SSteve French 	rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
189838c8a9a5SSteve French 	free_xid(xid);
189938c8a9a5SSteve French 
190038c8a9a5SSteve French 	if (rc) {
190138c8a9a5SSteve French 		cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
190238c8a9a5SSteve French 		tconInfoFree(tcon);
190338c8a9a5SSteve French 		goto out;
190438c8a9a5SSteve French 	}
190538c8a9a5SSteve French 
190638c8a9a5SSteve French 	cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid);
190738c8a9a5SSteve French 
190838c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
190938c8a9a5SSteve French 	tcon->status = TID_GOOD;
191038c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
191138c8a9a5SSteve French 	ses->tcon_ipc = tcon;
191238c8a9a5SSteve French out:
191338c8a9a5SSteve French 	return rc;
191438c8a9a5SSteve French }
191538c8a9a5SSteve French 
191638c8a9a5SSteve French /**
191738c8a9a5SSteve French  * cifs_free_ipc - helper to release the session IPC tcon
191838c8a9a5SSteve French  * @ses: smb session to unmount the IPC from
191938c8a9a5SSteve French  *
192038c8a9a5SSteve French  * Needs to be called everytime a session is destroyed.
192138c8a9a5SSteve French  *
192238c8a9a5SSteve French  * On session close, the IPC is closed and the server must release all tcons of the session.
192338c8a9a5SSteve French  * No need to send a tree disconnect here.
192438c8a9a5SSteve French  *
192538c8a9a5SSteve French  * Besides, it will make the server to not close durable and resilient files on session close, as
192638c8a9a5SSteve French  * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
192738c8a9a5SSteve French  */
192838c8a9a5SSteve French static int
192938c8a9a5SSteve French cifs_free_ipc(struct cifs_ses *ses)
193038c8a9a5SSteve French {
193138c8a9a5SSteve French 	struct cifs_tcon *tcon = ses->tcon_ipc;
193238c8a9a5SSteve French 
193338c8a9a5SSteve French 	if (tcon == NULL)
193438c8a9a5SSteve French 		return 0;
193538c8a9a5SSteve French 
193638c8a9a5SSteve French 	tconInfoFree(tcon);
193738c8a9a5SSteve French 	ses->tcon_ipc = NULL;
193838c8a9a5SSteve French 	return 0;
193938c8a9a5SSteve French }
194038c8a9a5SSteve French 
194138c8a9a5SSteve French static struct cifs_ses *
194238c8a9a5SSteve French cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
194338c8a9a5SSteve French {
194438c8a9a5SSteve French 	struct cifs_ses *ses, *ret = NULL;
194538c8a9a5SSteve French 
194638c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
194738c8a9a5SSteve French 	list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
194838c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
194938c8a9a5SSteve French 		if (ses->ses_status == SES_EXITING) {
195038c8a9a5SSteve French 			spin_unlock(&ses->ses_lock);
195138c8a9a5SSteve French 			continue;
195238c8a9a5SSteve French 		}
195338c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
195438c8a9a5SSteve French 		if (match_session(ses, ctx)) {
195538c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
195638c8a9a5SSteve French 			spin_unlock(&ses->ses_lock);
195738c8a9a5SSteve French 			ret = ses;
195838c8a9a5SSteve French 			break;
195938c8a9a5SSteve French 		}
196038c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
196138c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
196238c8a9a5SSteve French 	}
196338c8a9a5SSteve French 	if (ret)
196438c8a9a5SSteve French 		cifs_smb_ses_inc_refcount(ret);
196538c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
196638c8a9a5SSteve French 	return ret;
196738c8a9a5SSteve French }
196838c8a9a5SSteve French 
196938c8a9a5SSteve French void __cifs_put_smb_ses(struct cifs_ses *ses)
197038c8a9a5SSteve French {
197138c8a9a5SSteve French 	unsigned int rc, xid;
197238c8a9a5SSteve French 	unsigned int chan_count;
197338c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
197438c8a9a5SSteve French 
197538c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
197638c8a9a5SSteve French 	if (ses->ses_status == SES_EXITING) {
197738c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
197838c8a9a5SSteve French 		return;
197938c8a9a5SSteve French 	}
198038c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
198138c8a9a5SSteve French 
198238c8a9a5SSteve French 	cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
198338c8a9a5SSteve French 	cifs_dbg(FYI,
198438c8a9a5SSteve French 		 "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->tree_name : "NONE");
198538c8a9a5SSteve French 
198638c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
198738c8a9a5SSteve French 	if (--ses->ses_count > 0) {
198838c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
198938c8a9a5SSteve French 		return;
199038c8a9a5SSteve French 	}
1991ff7d80a9SWinston Wen 	spin_lock(&ses->ses_lock);
1992ff7d80a9SWinston Wen 	if (ses->ses_status == SES_GOOD)
1993ff7d80a9SWinston Wen 		ses->ses_status = SES_EXITING;
1994ff7d80a9SWinston Wen 	spin_unlock(&ses->ses_lock);
199538c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
199638c8a9a5SSteve French 
199738c8a9a5SSteve French 	/* ses_count can never go negative */
199838c8a9a5SSteve French 	WARN_ON(ses->ses_count < 0);
199938c8a9a5SSteve French 
200038c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
200138c8a9a5SSteve French 	if (ses->ses_status == SES_EXITING && server->ops->logoff) {
200238c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
200338c8a9a5SSteve French 		cifs_free_ipc(ses);
200438c8a9a5SSteve French 		xid = get_xid();
200538c8a9a5SSteve French 		rc = server->ops->logoff(xid, ses);
200638c8a9a5SSteve French 		if (rc)
200738c8a9a5SSteve French 			cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
200838c8a9a5SSteve French 				__func__, rc);
200938c8a9a5SSteve French 		_free_xid(xid);
201038c8a9a5SSteve French 	} else {
201138c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
201238c8a9a5SSteve French 		cifs_free_ipc(ses);
201338c8a9a5SSteve French 	}
201438c8a9a5SSteve French 
201538c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
201638c8a9a5SSteve French 	list_del_init(&ses->smb_ses_list);
201738c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
201838c8a9a5SSteve French 
201938c8a9a5SSteve French 	chan_count = ses->chan_count;
202038c8a9a5SSteve French 
202138c8a9a5SSteve French 	/* close any extra channels */
202238c8a9a5SSteve French 	if (chan_count > 1) {
202338c8a9a5SSteve French 		int i;
202438c8a9a5SSteve French 
202538c8a9a5SSteve French 		for (i = 1; i < chan_count; i++) {
202638c8a9a5SSteve French 			if (ses->chans[i].iface) {
202738c8a9a5SSteve French 				kref_put(&ses->chans[i].iface->refcount, release_iface);
202838c8a9a5SSteve French 				ses->chans[i].iface = NULL;
202938c8a9a5SSteve French 			}
203038c8a9a5SSteve French 			cifs_put_tcp_session(ses->chans[i].server, 0);
203138c8a9a5SSteve French 			ses->chans[i].server = NULL;
203238c8a9a5SSteve French 		}
203338c8a9a5SSteve French 	}
203438c8a9a5SSteve French 
203538c8a9a5SSteve French 	sesInfoFree(ses);
203638c8a9a5SSteve French 	cifs_put_tcp_session(server, 0);
203738c8a9a5SSteve French }
203838c8a9a5SSteve French 
203938c8a9a5SSteve French #ifdef CONFIG_KEYS
204038c8a9a5SSteve French 
204138c8a9a5SSteve French /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
204238c8a9a5SSteve French #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
204338c8a9a5SSteve French 
204438c8a9a5SSteve French /* Populate username and pw fields from keyring if possible */
204538c8a9a5SSteve French static int
204638c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
204738c8a9a5SSteve French {
204838c8a9a5SSteve French 	int rc = 0;
204938c8a9a5SSteve French 	int is_domain = 0;
205038c8a9a5SSteve French 	const char *delim, *payload;
205138c8a9a5SSteve French 	char *desc;
205238c8a9a5SSteve French 	ssize_t len;
205338c8a9a5SSteve French 	struct key *key;
205438c8a9a5SSteve French 	struct TCP_Server_Info *server = ses->server;
205538c8a9a5SSteve French 	struct sockaddr_in *sa;
205638c8a9a5SSteve French 	struct sockaddr_in6 *sa6;
205738c8a9a5SSteve French 	const struct user_key_payload *upayload;
205838c8a9a5SSteve French 
205938c8a9a5SSteve French 	desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
206038c8a9a5SSteve French 	if (!desc)
206138c8a9a5SSteve French 		return -ENOMEM;
206238c8a9a5SSteve French 
206338c8a9a5SSteve French 	/* try to find an address key first */
206438c8a9a5SSteve French 	switch (server->dstaddr.ss_family) {
206538c8a9a5SSteve French 	case AF_INET:
206638c8a9a5SSteve French 		sa = (struct sockaddr_in *)&server->dstaddr;
206738c8a9a5SSteve French 		sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
206838c8a9a5SSteve French 		break;
206938c8a9a5SSteve French 	case AF_INET6:
207038c8a9a5SSteve French 		sa6 = (struct sockaddr_in6 *)&server->dstaddr;
207138c8a9a5SSteve French 		sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
207238c8a9a5SSteve French 		break;
207338c8a9a5SSteve French 	default:
207438c8a9a5SSteve French 		cifs_dbg(FYI, "Bad ss_family (%hu)\n",
207538c8a9a5SSteve French 			 server->dstaddr.ss_family);
207638c8a9a5SSteve French 		rc = -EINVAL;
207738c8a9a5SSteve French 		goto out_err;
207838c8a9a5SSteve French 	}
207938c8a9a5SSteve French 
208038c8a9a5SSteve French 	cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
208138c8a9a5SSteve French 	key = request_key(&key_type_logon, desc, "");
208238c8a9a5SSteve French 	if (IS_ERR(key)) {
208338c8a9a5SSteve French 		if (!ses->domainName) {
208438c8a9a5SSteve French 			cifs_dbg(FYI, "domainName is NULL\n");
208538c8a9a5SSteve French 			rc = PTR_ERR(key);
208638c8a9a5SSteve French 			goto out_err;
208738c8a9a5SSteve French 		}
208838c8a9a5SSteve French 
208938c8a9a5SSteve French 		/* didn't work, try to find a domain key */
209038c8a9a5SSteve French 		sprintf(desc, "cifs:d:%s", ses->domainName);
209138c8a9a5SSteve French 		cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
209238c8a9a5SSteve French 		key = request_key(&key_type_logon, desc, "");
209338c8a9a5SSteve French 		if (IS_ERR(key)) {
209438c8a9a5SSteve French 			rc = PTR_ERR(key);
209538c8a9a5SSteve French 			goto out_err;
209638c8a9a5SSteve French 		}
209738c8a9a5SSteve French 		is_domain = 1;
209838c8a9a5SSteve French 	}
209938c8a9a5SSteve French 
210038c8a9a5SSteve French 	down_read(&key->sem);
210138c8a9a5SSteve French 	upayload = user_key_payload_locked(key);
210238c8a9a5SSteve French 	if (IS_ERR_OR_NULL(upayload)) {
210338c8a9a5SSteve French 		rc = upayload ? PTR_ERR(upayload) : -EINVAL;
210438c8a9a5SSteve French 		goto out_key_put;
210538c8a9a5SSteve French 	}
210638c8a9a5SSteve French 
210738c8a9a5SSteve French 	/* find first : in payload */
210838c8a9a5SSteve French 	payload = upayload->data;
210938c8a9a5SSteve French 	delim = strnchr(payload, upayload->datalen, ':');
211038c8a9a5SSteve French 	cifs_dbg(FYI, "payload=%s\n", payload);
211138c8a9a5SSteve French 	if (!delim) {
211238c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
211338c8a9a5SSteve French 			 upayload->datalen);
211438c8a9a5SSteve French 		rc = -EINVAL;
211538c8a9a5SSteve French 		goto out_key_put;
211638c8a9a5SSteve French 	}
211738c8a9a5SSteve French 
211838c8a9a5SSteve French 	len = delim - payload;
211938c8a9a5SSteve French 	if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
212038c8a9a5SSteve French 		cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
212138c8a9a5SSteve French 			 len);
212238c8a9a5SSteve French 		rc = -EINVAL;
212338c8a9a5SSteve French 		goto out_key_put;
212438c8a9a5SSteve French 	}
212538c8a9a5SSteve French 
212638c8a9a5SSteve French 	ctx->username = kstrndup(payload, len, GFP_KERNEL);
212738c8a9a5SSteve French 	if (!ctx->username) {
212838c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
212938c8a9a5SSteve French 			 len);
213038c8a9a5SSteve French 		rc = -ENOMEM;
213138c8a9a5SSteve French 		goto out_key_put;
213238c8a9a5SSteve French 	}
213338c8a9a5SSteve French 	cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
213438c8a9a5SSteve French 
213538c8a9a5SSteve French 	len = key->datalen - (len + 1);
213638c8a9a5SSteve French 	if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
213738c8a9a5SSteve French 		cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
213838c8a9a5SSteve French 		rc = -EINVAL;
213938c8a9a5SSteve French 		kfree(ctx->username);
214038c8a9a5SSteve French 		ctx->username = NULL;
214138c8a9a5SSteve French 		goto out_key_put;
214238c8a9a5SSteve French 	}
214338c8a9a5SSteve French 
214438c8a9a5SSteve French 	++delim;
214538c8a9a5SSteve French 	ctx->password = kstrndup(delim, len, GFP_KERNEL);
214638c8a9a5SSteve French 	if (!ctx->password) {
214738c8a9a5SSteve French 		cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
214838c8a9a5SSteve French 			 len);
214938c8a9a5SSteve French 		rc = -ENOMEM;
215038c8a9a5SSteve French 		kfree(ctx->username);
215138c8a9a5SSteve French 		ctx->username = NULL;
215238c8a9a5SSteve French 		goto out_key_put;
215338c8a9a5SSteve French 	}
215438c8a9a5SSteve French 
215538c8a9a5SSteve French 	/*
215638c8a9a5SSteve French 	 * If we have a domain key then we must set the domainName in the
215738c8a9a5SSteve French 	 * for the request.
215838c8a9a5SSteve French 	 */
215938c8a9a5SSteve French 	if (is_domain && ses->domainName) {
216038c8a9a5SSteve French 		ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
216138c8a9a5SSteve French 		if (!ctx->domainname) {
216238c8a9a5SSteve French 			cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
216338c8a9a5SSteve French 				 len);
216438c8a9a5SSteve French 			rc = -ENOMEM;
216538c8a9a5SSteve French 			kfree(ctx->username);
216638c8a9a5SSteve French 			ctx->username = NULL;
216738c8a9a5SSteve French 			kfree_sensitive(ctx->password);
216838c8a9a5SSteve French 			ctx->password = NULL;
216938c8a9a5SSteve French 			goto out_key_put;
217038c8a9a5SSteve French 		}
217138c8a9a5SSteve French 	}
217238c8a9a5SSteve French 
217338c8a9a5SSteve French 	strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name));
217438c8a9a5SSteve French 
217538c8a9a5SSteve French out_key_put:
217638c8a9a5SSteve French 	up_read(&key->sem);
217738c8a9a5SSteve French 	key_put(key);
217838c8a9a5SSteve French out_err:
217938c8a9a5SSteve French 	kfree(desc);
218038c8a9a5SSteve French 	cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
218138c8a9a5SSteve French 	return rc;
218238c8a9a5SSteve French }
218338c8a9a5SSteve French #else /* ! CONFIG_KEYS */
218438c8a9a5SSteve French static inline int
218538c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
218638c8a9a5SSteve French 		   struct cifs_ses *ses __attribute__((unused)))
218738c8a9a5SSteve French {
218838c8a9a5SSteve French 	return -ENOSYS;
218938c8a9a5SSteve French }
219038c8a9a5SSteve French #endif /* CONFIG_KEYS */
219138c8a9a5SSteve French 
219238c8a9a5SSteve French /**
219338c8a9a5SSteve French  * cifs_get_smb_ses - get a session matching @ctx data from @server
219438c8a9a5SSteve French  * @server: server to setup the session to
219538c8a9a5SSteve French  * @ctx: superblock configuration context to use to setup the session
219638c8a9a5SSteve French  *
219738c8a9a5SSteve French  * This function assumes it is being called from cifs_mount() where we
219838c8a9a5SSteve French  * already got a server reference (server refcount +1). See
219938c8a9a5SSteve French  * cifs_get_tcon() for refcount explanations.
220038c8a9a5SSteve French  */
220138c8a9a5SSteve French struct cifs_ses *
220238c8a9a5SSteve French cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
220338c8a9a5SSteve French {
220438c8a9a5SSteve French 	int rc = 0;
220538c8a9a5SSteve French 	unsigned int xid;
220638c8a9a5SSteve French 	struct cifs_ses *ses;
220738c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
220838c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
220938c8a9a5SSteve French 
221038c8a9a5SSteve French 	xid = get_xid();
221138c8a9a5SSteve French 
221238c8a9a5SSteve French 	ses = cifs_find_smb_ses(server, ctx);
221338c8a9a5SSteve French 	if (ses) {
221438c8a9a5SSteve French 		cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
221538c8a9a5SSteve French 			 ses->ses_status);
221638c8a9a5SSteve French 
221738c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
221838c8a9a5SSteve French 		if (cifs_chan_needs_reconnect(ses, server)) {
221938c8a9a5SSteve French 			spin_unlock(&ses->chan_lock);
222038c8a9a5SSteve French 			cifs_dbg(FYI, "Session needs reconnect\n");
222138c8a9a5SSteve French 
222238c8a9a5SSteve French 			mutex_lock(&ses->session_mutex);
222338c8a9a5SSteve French 			rc = cifs_negotiate_protocol(xid, ses, server);
222438c8a9a5SSteve French 			if (rc) {
222538c8a9a5SSteve French 				mutex_unlock(&ses->session_mutex);
222638c8a9a5SSteve French 				/* problem -- put our ses reference */
222738c8a9a5SSteve French 				cifs_put_smb_ses(ses);
222838c8a9a5SSteve French 				free_xid(xid);
222938c8a9a5SSteve French 				return ERR_PTR(rc);
223038c8a9a5SSteve French 			}
223138c8a9a5SSteve French 
223238c8a9a5SSteve French 			rc = cifs_setup_session(xid, ses, server,
223338c8a9a5SSteve French 						ctx->local_nls);
223438c8a9a5SSteve French 			if (rc) {
223538c8a9a5SSteve French 				mutex_unlock(&ses->session_mutex);
223638c8a9a5SSteve French 				/* problem -- put our reference */
223738c8a9a5SSteve French 				cifs_put_smb_ses(ses);
223838c8a9a5SSteve French 				free_xid(xid);
223938c8a9a5SSteve French 				return ERR_PTR(rc);
224038c8a9a5SSteve French 			}
224138c8a9a5SSteve French 			mutex_unlock(&ses->session_mutex);
224238c8a9a5SSteve French 
224338c8a9a5SSteve French 			spin_lock(&ses->chan_lock);
224438c8a9a5SSteve French 		}
224538c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
224638c8a9a5SSteve French 
224738c8a9a5SSteve French 		/* existing SMB ses has a server reference already */
224838c8a9a5SSteve French 		cifs_put_tcp_session(server, 0);
224938c8a9a5SSteve French 		free_xid(xid);
225038c8a9a5SSteve French 		return ses;
225138c8a9a5SSteve French 	}
225238c8a9a5SSteve French 
225338c8a9a5SSteve French 	rc = -ENOMEM;
225438c8a9a5SSteve French 
225538c8a9a5SSteve French 	cifs_dbg(FYI, "Existing smb sess not found\n");
225638c8a9a5SSteve French 	ses = sesInfoAlloc();
225738c8a9a5SSteve French 	if (ses == NULL)
225838c8a9a5SSteve French 		goto get_ses_fail;
225938c8a9a5SSteve French 
226038c8a9a5SSteve French 	/* new SMB session uses our server ref */
226138c8a9a5SSteve French 	ses->server = server;
226238c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6)
226338c8a9a5SSteve French 		sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
226438c8a9a5SSteve French 	else
226538c8a9a5SSteve French 		sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
226638c8a9a5SSteve French 
226738c8a9a5SSteve French 	if (ctx->username) {
226838c8a9a5SSteve French 		ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
226938c8a9a5SSteve French 		if (!ses->user_name)
227038c8a9a5SSteve French 			goto get_ses_fail;
227138c8a9a5SSteve French 	}
227238c8a9a5SSteve French 
227338c8a9a5SSteve French 	/* ctx->password freed at unmount */
227438c8a9a5SSteve French 	if (ctx->password) {
227538c8a9a5SSteve French 		ses->password = kstrdup(ctx->password, GFP_KERNEL);
227638c8a9a5SSteve French 		if (!ses->password)
227738c8a9a5SSteve French 			goto get_ses_fail;
227838c8a9a5SSteve French 	}
227938c8a9a5SSteve French 	if (ctx->domainname) {
228038c8a9a5SSteve French 		ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
228138c8a9a5SSteve French 		if (!ses->domainName)
228238c8a9a5SSteve French 			goto get_ses_fail;
228338c8a9a5SSteve French 	}
228438c8a9a5SSteve French 
228538c8a9a5SSteve French 	strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name));
228638c8a9a5SSteve French 
228738c8a9a5SSteve French 	if (ctx->domainauto)
228838c8a9a5SSteve French 		ses->domainAuto = ctx->domainauto;
228938c8a9a5SSteve French 	ses->cred_uid = ctx->cred_uid;
229038c8a9a5SSteve French 	ses->linux_uid = ctx->linux_uid;
229138c8a9a5SSteve French 
229238c8a9a5SSteve French 	ses->sectype = ctx->sectype;
229338c8a9a5SSteve French 	ses->sign = ctx->sign;
2294a43f95fdSWinston Wen 	ses->local_nls = load_nls(ctx->local_nls->charset);
229538c8a9a5SSteve French 
229638c8a9a5SSteve French 	/* add server as first channel */
229738c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
229838c8a9a5SSteve French 	ses->chans[0].server = server;
229938c8a9a5SSteve French 	ses->chan_count = 1;
230038c8a9a5SSteve French 	ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
230138c8a9a5SSteve French 	ses->chans_need_reconnect = 1;
230238c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
230338c8a9a5SSteve French 
230438c8a9a5SSteve French 	mutex_lock(&ses->session_mutex);
230538c8a9a5SSteve French 	rc = cifs_negotiate_protocol(xid, ses, server);
230638c8a9a5SSteve French 	if (!rc)
230738c8a9a5SSteve French 		rc = cifs_setup_session(xid, ses, server, ctx->local_nls);
230838c8a9a5SSteve French 	mutex_unlock(&ses->session_mutex);
230938c8a9a5SSteve French 
231038c8a9a5SSteve French 	/* each channel uses a different signing key */
231138c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
231238c8a9a5SSteve French 	memcpy(ses->chans[0].signkey, ses->smb3signingkey,
231338c8a9a5SSteve French 	       sizeof(ses->smb3signingkey));
231438c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
231538c8a9a5SSteve French 
231638c8a9a5SSteve French 	if (rc)
231738c8a9a5SSteve French 		goto get_ses_fail;
231838c8a9a5SSteve French 
231938c8a9a5SSteve French 	/*
232038c8a9a5SSteve French 	 * success, put it on the list and add it as first channel
232138c8a9a5SSteve French 	 * note: the session becomes active soon after this. So you'll
232238c8a9a5SSteve French 	 * need to lock before changing something in the session.
232338c8a9a5SSteve French 	 */
232438c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
232538c8a9a5SSteve French 	ses->dfs_root_ses = ctx->dfs_root_ses;
232638c8a9a5SSteve French 	if (ses->dfs_root_ses)
232738c8a9a5SSteve French 		ses->dfs_root_ses->ses_count++;
232838c8a9a5SSteve French 	list_add(&ses->smb_ses_list, &server->smb_ses_list);
232938c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
233038c8a9a5SSteve French 
233138c8a9a5SSteve French 	cifs_setup_ipc(ses, ctx);
233238c8a9a5SSteve French 
233338c8a9a5SSteve French 	free_xid(xid);
233438c8a9a5SSteve French 
233538c8a9a5SSteve French 	return ses;
233638c8a9a5SSteve French 
233738c8a9a5SSteve French get_ses_fail:
233838c8a9a5SSteve French 	sesInfoFree(ses);
233938c8a9a5SSteve French 	free_xid(xid);
234038c8a9a5SSteve French 	return ERR_PTR(rc);
234138c8a9a5SSteve French }
234238c8a9a5SSteve French 
234338c8a9a5SSteve French /* this function must be called with tc_lock held */
234438c8a9a5SSteve French static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
234538c8a9a5SSteve French {
234638c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
234738c8a9a5SSteve French 
234838c8a9a5SSteve French 	if (tcon->status == TID_EXITING)
234938c8a9a5SSteve French 		return 0;
23503ae872deSPaulo Alcantara 
23513ae872deSPaulo Alcantara 	if (tcon->origin_fullpath) {
23523ae872deSPaulo Alcantara 		if (!ctx->source ||
23533ae872deSPaulo Alcantara 		    !dfs_src_pathname_equal(ctx->source,
23543ae872deSPaulo Alcantara 					    tcon->origin_fullpath))
235538c8a9a5SSteve French 			return 0;
23563ae872deSPaulo Alcantara 	} else if (!server->leaf_fullpath &&
23573ae872deSPaulo Alcantara 		   strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE)) {
23583ae872deSPaulo Alcantara 		return 0;
23593ae872deSPaulo Alcantara 	}
236038c8a9a5SSteve French 	if (tcon->seal != ctx->seal)
236138c8a9a5SSteve French 		return 0;
236238c8a9a5SSteve French 	if (tcon->snapshot_time != ctx->snapshot_time)
236338c8a9a5SSteve French 		return 0;
236438c8a9a5SSteve French 	if (tcon->handle_timeout != ctx->handle_timeout)
236538c8a9a5SSteve French 		return 0;
236638c8a9a5SSteve French 	if (tcon->no_lease != ctx->no_lease)
236738c8a9a5SSteve French 		return 0;
236838c8a9a5SSteve French 	if (tcon->nodelete != ctx->nodelete)
236938c8a9a5SSteve French 		return 0;
237038c8a9a5SSteve French 	return 1;
237138c8a9a5SSteve French }
237238c8a9a5SSteve French 
237338c8a9a5SSteve French static struct cifs_tcon *
237438c8a9a5SSteve French cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
237538c8a9a5SSteve French {
237638c8a9a5SSteve French 	struct cifs_tcon *tcon;
237738c8a9a5SSteve French 
237838c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
237938c8a9a5SSteve French 	list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
238038c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
238138c8a9a5SSteve French 		if (!match_tcon(tcon, ctx)) {
238238c8a9a5SSteve French 			spin_unlock(&tcon->tc_lock);
238338c8a9a5SSteve French 			continue;
238438c8a9a5SSteve French 		}
238538c8a9a5SSteve French 		++tcon->tc_count;
238638c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
238738c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
238838c8a9a5SSteve French 		return tcon;
238938c8a9a5SSteve French 	}
239038c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
239138c8a9a5SSteve French 	return NULL;
239238c8a9a5SSteve French }
239338c8a9a5SSteve French 
239438c8a9a5SSteve French void
239538c8a9a5SSteve French cifs_put_tcon(struct cifs_tcon *tcon)
239638c8a9a5SSteve French {
239738c8a9a5SSteve French 	unsigned int xid;
239838c8a9a5SSteve French 	struct cifs_ses *ses;
239938c8a9a5SSteve French 
240038c8a9a5SSteve French 	/*
240138c8a9a5SSteve French 	 * IPC tcon share the lifetime of their session and are
240238c8a9a5SSteve French 	 * destroyed in the session put function
240338c8a9a5SSteve French 	 */
240438c8a9a5SSteve French 	if (tcon == NULL || tcon->ipc)
240538c8a9a5SSteve French 		return;
240638c8a9a5SSteve French 
240738c8a9a5SSteve French 	ses = tcon->ses;
240838c8a9a5SSteve French 	cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
240938c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
241038c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
241138c8a9a5SSteve French 	if (--tcon->tc_count > 0) {
241238c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
241338c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
241438c8a9a5SSteve French 		return;
241538c8a9a5SSteve French 	}
241638c8a9a5SSteve French 
241738c8a9a5SSteve French 	/* tc_count can never go negative */
241838c8a9a5SSteve French 	WARN_ON(tcon->tc_count < 0);
241938c8a9a5SSteve French 
242038c8a9a5SSteve French 	list_del_init(&tcon->tcon_list);
242138c8a9a5SSteve French 	tcon->status = TID_EXITING;
242238c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
242338c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
242438c8a9a5SSteve French 
242538c8a9a5SSteve French 	/* cancel polling of interfaces */
242638c8a9a5SSteve French 	cancel_delayed_work_sync(&tcon->query_interfaces);
242738c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
242838c8a9a5SSteve French 	cancel_delayed_work_sync(&tcon->dfs_cache_work);
242938c8a9a5SSteve French #endif
243038c8a9a5SSteve French 
243138c8a9a5SSteve French 	if (tcon->use_witness) {
243238c8a9a5SSteve French 		int rc;
243338c8a9a5SSteve French 
243438c8a9a5SSteve French 		rc = cifs_swn_unregister(tcon);
243538c8a9a5SSteve French 		if (rc < 0) {
243638c8a9a5SSteve French 			cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
243738c8a9a5SSteve French 					__func__, rc);
243838c8a9a5SSteve French 		}
243938c8a9a5SSteve French 	}
244038c8a9a5SSteve French 
244138c8a9a5SSteve French 	xid = get_xid();
244238c8a9a5SSteve French 	if (ses->server->ops->tree_disconnect)
244338c8a9a5SSteve French 		ses->server->ops->tree_disconnect(xid, tcon);
244438c8a9a5SSteve French 	_free_xid(xid);
244538c8a9a5SSteve French 
244638c8a9a5SSteve French 	cifs_fscache_release_super_cookie(tcon);
244738c8a9a5SSteve French 	tconInfoFree(tcon);
244838c8a9a5SSteve French 	cifs_put_smb_ses(ses);
244938c8a9a5SSteve French }
245038c8a9a5SSteve French 
245138c8a9a5SSteve French /**
245238c8a9a5SSteve French  * cifs_get_tcon - get a tcon matching @ctx data from @ses
245338c8a9a5SSteve French  * @ses: smb session to issue the request on
245438c8a9a5SSteve French  * @ctx: the superblock configuration context to use for building the
245538c8a9a5SSteve French  *
245638c8a9a5SSteve French  * - tcon refcount is the number of mount points using the tcon.
245738c8a9a5SSteve French  * - ses refcount is the number of tcon using the session.
245838c8a9a5SSteve French  *
245938c8a9a5SSteve French  * 1. This function assumes it is being called from cifs_mount() where
246038c8a9a5SSteve French  *    we already got a session reference (ses refcount +1).
246138c8a9a5SSteve French  *
246238c8a9a5SSteve French  * 2. Since we're in the context of adding a mount point, the end
246338c8a9a5SSteve French  *    result should be either:
246438c8a9a5SSteve French  *
246538c8a9a5SSteve French  * a) a new tcon already allocated with refcount=1 (1 mount point) and
246638c8a9a5SSteve French  *    its session refcount incremented (1 new tcon). This +1 was
246738c8a9a5SSteve French  *    already done in (1).
246838c8a9a5SSteve French  *
246938c8a9a5SSteve French  * b) an existing tcon with refcount+1 (add a mount point to it) and
247038c8a9a5SSteve French  *    identical ses refcount (no new tcon). Because of (1) we need to
247138c8a9a5SSteve French  *    decrement the ses refcount.
247238c8a9a5SSteve French  */
247338c8a9a5SSteve French static struct cifs_tcon *
247438c8a9a5SSteve French cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
247538c8a9a5SSteve French {
247638c8a9a5SSteve French 	int rc, xid;
247738c8a9a5SSteve French 	struct cifs_tcon *tcon;
247838c8a9a5SSteve French 
247938c8a9a5SSteve French 	tcon = cifs_find_tcon(ses, ctx);
248038c8a9a5SSteve French 	if (tcon) {
248138c8a9a5SSteve French 		/*
248238c8a9a5SSteve French 		 * tcon has refcount already incremented but we need to
248338c8a9a5SSteve French 		 * decrement extra ses reference gotten by caller (case b)
248438c8a9a5SSteve French 		 */
248538c8a9a5SSteve French 		cifs_dbg(FYI, "Found match on UNC path\n");
248638c8a9a5SSteve French 		cifs_put_smb_ses(ses);
248738c8a9a5SSteve French 		return tcon;
248838c8a9a5SSteve French 	}
248938c8a9a5SSteve French 
249038c8a9a5SSteve French 	if (!ses->server->ops->tree_connect) {
249138c8a9a5SSteve French 		rc = -ENOSYS;
249238c8a9a5SSteve French 		goto out_fail;
249338c8a9a5SSteve French 	}
249438c8a9a5SSteve French 
249538c8a9a5SSteve French 	tcon = tconInfoAlloc();
249638c8a9a5SSteve French 	if (tcon == NULL) {
249738c8a9a5SSteve French 		rc = -ENOMEM;
249838c8a9a5SSteve French 		goto out_fail;
249938c8a9a5SSteve French 	}
250038c8a9a5SSteve French 
250138c8a9a5SSteve French 	if (ctx->snapshot_time) {
250238c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
250338c8a9a5SSteve French 			cifs_dbg(VFS,
250438c8a9a5SSteve French 			     "Use SMB2 or later for snapshot mount option\n");
250538c8a9a5SSteve French 			rc = -EOPNOTSUPP;
250638c8a9a5SSteve French 			goto out_fail;
250738c8a9a5SSteve French 		} else
250838c8a9a5SSteve French 			tcon->snapshot_time = ctx->snapshot_time;
250938c8a9a5SSteve French 	}
251038c8a9a5SSteve French 
251138c8a9a5SSteve French 	if (ctx->handle_timeout) {
251238c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
251338c8a9a5SSteve French 			cifs_dbg(VFS,
251438c8a9a5SSteve French 			     "Use SMB2.1 or later for handle timeout option\n");
251538c8a9a5SSteve French 			rc = -EOPNOTSUPP;
251638c8a9a5SSteve French 			goto out_fail;
251738c8a9a5SSteve French 		} else
251838c8a9a5SSteve French 			tcon->handle_timeout = ctx->handle_timeout;
251938c8a9a5SSteve French 	}
252038c8a9a5SSteve French 
252138c8a9a5SSteve French 	tcon->ses = ses;
252238c8a9a5SSteve French 	if (ctx->password) {
252338c8a9a5SSteve French 		tcon->password = kstrdup(ctx->password, GFP_KERNEL);
252438c8a9a5SSteve French 		if (!tcon->password) {
252538c8a9a5SSteve French 			rc = -ENOMEM;
252638c8a9a5SSteve French 			goto out_fail;
252738c8a9a5SSteve French 		}
252838c8a9a5SSteve French 	}
252938c8a9a5SSteve French 
253038c8a9a5SSteve French 	if (ctx->seal) {
253138c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
253238c8a9a5SSteve French 			cifs_dbg(VFS,
253338c8a9a5SSteve French 				 "SMB3 or later required for encryption\n");
253438c8a9a5SSteve French 			rc = -EOPNOTSUPP;
253538c8a9a5SSteve French 			goto out_fail;
253638c8a9a5SSteve French 		} else if (tcon->ses->server->capabilities &
253738c8a9a5SSteve French 					SMB2_GLOBAL_CAP_ENCRYPTION)
253838c8a9a5SSteve French 			tcon->seal = true;
253938c8a9a5SSteve French 		else {
254038c8a9a5SSteve French 			cifs_dbg(VFS, "Encryption is not supported on share\n");
254138c8a9a5SSteve French 			rc = -EOPNOTSUPP;
254238c8a9a5SSteve French 			goto out_fail;
254338c8a9a5SSteve French 		}
254438c8a9a5SSteve French 	}
254538c8a9a5SSteve French 
254638c8a9a5SSteve French 	if (ctx->linux_ext) {
254738c8a9a5SSteve French 		if (ses->server->posix_ext_supported) {
254838c8a9a5SSteve French 			tcon->posix_extensions = true;
254938c8a9a5SSteve French 			pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
255038c8a9a5SSteve French 		} else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
255138c8a9a5SSteve French 		    (strcmp(ses->server->vals->version_string,
255238c8a9a5SSteve French 		     SMB3ANY_VERSION_STRING) == 0) ||
255338c8a9a5SSteve French 		    (strcmp(ses->server->vals->version_string,
255438c8a9a5SSteve French 		     SMBDEFAULT_VERSION_STRING) == 0)) {
255538c8a9a5SSteve French 			cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
255638c8a9a5SSteve French 			rc = -EOPNOTSUPP;
255738c8a9a5SSteve French 			goto out_fail;
255838c8a9a5SSteve French 		} else {
255938c8a9a5SSteve French 			cifs_dbg(VFS, "Check vers= mount option. SMB3.11 "
256038c8a9a5SSteve French 				"disabled but required for POSIX extensions\n");
256138c8a9a5SSteve French 			rc = -EOPNOTSUPP;
256238c8a9a5SSteve French 			goto out_fail;
256338c8a9a5SSteve French 		}
256438c8a9a5SSteve French 	}
256538c8a9a5SSteve French 
256638c8a9a5SSteve French 	xid = get_xid();
256738c8a9a5SSteve French 	rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
256838c8a9a5SSteve French 					    ctx->local_nls);
256938c8a9a5SSteve French 	free_xid(xid);
257038c8a9a5SSteve French 	cifs_dbg(FYI, "Tcon rc = %d\n", rc);
257138c8a9a5SSteve French 	if (rc)
257238c8a9a5SSteve French 		goto out_fail;
257338c8a9a5SSteve French 
257438c8a9a5SSteve French 	tcon->use_persistent = false;
257538c8a9a5SSteve French 	/* check if SMB2 or later, CIFS does not support persistent handles */
257638c8a9a5SSteve French 	if (ctx->persistent) {
257738c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
257838c8a9a5SSteve French 			cifs_dbg(VFS,
257938c8a9a5SSteve French 			     "SMB3 or later required for persistent handles\n");
258038c8a9a5SSteve French 			rc = -EOPNOTSUPP;
258138c8a9a5SSteve French 			goto out_fail;
258238c8a9a5SSteve French 		} else if (ses->server->capabilities &
258338c8a9a5SSteve French 			   SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
258438c8a9a5SSteve French 			tcon->use_persistent = true;
258538c8a9a5SSteve French 		else /* persistent handles requested but not supported */ {
258638c8a9a5SSteve French 			cifs_dbg(VFS,
258738c8a9a5SSteve French 				"Persistent handles not supported on share\n");
258838c8a9a5SSteve French 			rc = -EOPNOTSUPP;
258938c8a9a5SSteve French 			goto out_fail;
259038c8a9a5SSteve French 		}
259138c8a9a5SSteve French 	} else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
259238c8a9a5SSteve French 	     && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
259338c8a9a5SSteve French 	     && (ctx->nopersistent == false)) {
259438c8a9a5SSteve French 		cifs_dbg(FYI, "enabling persistent handles\n");
259538c8a9a5SSteve French 		tcon->use_persistent = true;
259638c8a9a5SSteve French 	} else if (ctx->resilient) {
259738c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
259838c8a9a5SSteve French 			cifs_dbg(VFS,
259938c8a9a5SSteve French 			     "SMB2.1 or later required for resilient handles\n");
260038c8a9a5SSteve French 			rc = -EOPNOTSUPP;
260138c8a9a5SSteve French 			goto out_fail;
260238c8a9a5SSteve French 		}
260338c8a9a5SSteve French 		tcon->use_resilient = true;
260438c8a9a5SSteve French 	}
260538c8a9a5SSteve French 
260638c8a9a5SSteve French 	tcon->use_witness = false;
260738c8a9a5SSteve French 	if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
260838c8a9a5SSteve French 		if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
260938c8a9a5SSteve French 			if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
261038c8a9a5SSteve French 				/*
261138c8a9a5SSteve French 				 * Set witness in use flag in first place
261238c8a9a5SSteve French 				 * to retry registration in the echo task
261338c8a9a5SSteve French 				 */
261438c8a9a5SSteve French 				tcon->use_witness = true;
261538c8a9a5SSteve French 				/* And try to register immediately */
261638c8a9a5SSteve French 				rc = cifs_swn_register(tcon);
261738c8a9a5SSteve French 				if (rc < 0) {
261838c8a9a5SSteve French 					cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
261938c8a9a5SSteve French 					goto out_fail;
262038c8a9a5SSteve French 				}
262138c8a9a5SSteve French 			} else {
262238c8a9a5SSteve French 				/* TODO: try to extend for non-cluster uses (eg multichannel) */
262338c8a9a5SSteve French 				cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
262438c8a9a5SSteve French 				rc = -EOPNOTSUPP;
262538c8a9a5SSteve French 				goto out_fail;
262638c8a9a5SSteve French 			}
262738c8a9a5SSteve French 		} else {
262838c8a9a5SSteve French 			cifs_dbg(VFS, "SMB3 or later required for witness option\n");
262938c8a9a5SSteve French 			rc = -EOPNOTSUPP;
263038c8a9a5SSteve French 			goto out_fail;
263138c8a9a5SSteve French 		}
263238c8a9a5SSteve French 	}
263338c8a9a5SSteve French 
263438c8a9a5SSteve French 	/* If the user really knows what they are doing they can override */
263538c8a9a5SSteve French 	if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
263638c8a9a5SSteve French 		if (ctx->cache_ro)
263738c8a9a5SSteve French 			cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
263838c8a9a5SSteve French 		else if (ctx->cache_rw)
263938c8a9a5SSteve French 			cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
264038c8a9a5SSteve French 	}
264138c8a9a5SSteve French 
264238c8a9a5SSteve French 	if (ctx->no_lease) {
264338c8a9a5SSteve French 		if (ses->server->vals->protocol_id == 0) {
264438c8a9a5SSteve French 			cifs_dbg(VFS,
264538c8a9a5SSteve French 				"SMB2 or later required for nolease option\n");
264638c8a9a5SSteve French 			rc = -EOPNOTSUPP;
264738c8a9a5SSteve French 			goto out_fail;
264838c8a9a5SSteve French 		} else
264938c8a9a5SSteve French 			tcon->no_lease = ctx->no_lease;
265038c8a9a5SSteve French 	}
265138c8a9a5SSteve French 
265238c8a9a5SSteve French 	/*
265338c8a9a5SSteve French 	 * We can have only one retry value for a connection to a share so for
265438c8a9a5SSteve French 	 * resources mounted more than once to the same server share the last
265538c8a9a5SSteve French 	 * value passed in for the retry flag is used.
265638c8a9a5SSteve French 	 */
265738c8a9a5SSteve French 	tcon->retry = ctx->retry;
265838c8a9a5SSteve French 	tcon->nocase = ctx->nocase;
265938c8a9a5SSteve French 	tcon->broken_sparse_sup = ctx->no_sparse;
2660*6a50d71dSSteve French 	tcon->max_cached_dirs = ctx->max_cached_dirs;
266138c8a9a5SSteve French 	if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
266238c8a9a5SSteve French 		tcon->nohandlecache = ctx->nohandlecache;
266338c8a9a5SSteve French 	else
266438c8a9a5SSteve French 		tcon->nohandlecache = true;
266538c8a9a5SSteve French 	tcon->nodelete = ctx->nodelete;
266638c8a9a5SSteve French 	tcon->local_lease = ctx->local_lease;
266738c8a9a5SSteve French 	INIT_LIST_HEAD(&tcon->pending_opens);
266838c8a9a5SSteve French 	tcon->status = TID_GOOD;
266938c8a9a5SSteve French 
267038c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcon->query_interfaces,
267138c8a9a5SSteve French 			  smb2_query_server_interfaces);
267238c8a9a5SSteve French 	if (ses->server->dialect >= SMB30_PROT_ID &&
267338c8a9a5SSteve French 	    (ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
267438c8a9a5SSteve French 		/* schedule query interfaces poll */
267538c8a9a5SSteve French 		queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
267638c8a9a5SSteve French 				   (SMB_INTERFACE_POLL_INTERVAL * HZ));
267738c8a9a5SSteve French 	}
267838c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
267938c8a9a5SSteve French 	INIT_DELAYED_WORK(&tcon->dfs_cache_work, dfs_cache_refresh);
268038c8a9a5SSteve French #endif
268138c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
268238c8a9a5SSteve French 	list_add(&tcon->tcon_list, &ses->tcon_list);
268338c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
268438c8a9a5SSteve French 
268538c8a9a5SSteve French 	return tcon;
268638c8a9a5SSteve French 
268738c8a9a5SSteve French out_fail:
268838c8a9a5SSteve French 	tconInfoFree(tcon);
268938c8a9a5SSteve French 	return ERR_PTR(rc);
269038c8a9a5SSteve French }
269138c8a9a5SSteve French 
269238c8a9a5SSteve French void
269338c8a9a5SSteve French cifs_put_tlink(struct tcon_link *tlink)
269438c8a9a5SSteve French {
269538c8a9a5SSteve French 	if (!tlink || IS_ERR(tlink))
269638c8a9a5SSteve French 		return;
269738c8a9a5SSteve French 
269838c8a9a5SSteve French 	if (!atomic_dec_and_test(&tlink->tl_count) ||
269938c8a9a5SSteve French 	    test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
270038c8a9a5SSteve French 		tlink->tl_time = jiffies;
270138c8a9a5SSteve French 		return;
270238c8a9a5SSteve French 	}
270338c8a9a5SSteve French 
270438c8a9a5SSteve French 	if (!IS_ERR(tlink_tcon(tlink)))
270538c8a9a5SSteve French 		cifs_put_tcon(tlink_tcon(tlink));
270638c8a9a5SSteve French 	kfree(tlink);
270738c8a9a5SSteve French 	return;
270838c8a9a5SSteve French }
270938c8a9a5SSteve French 
271038c8a9a5SSteve French static int
271138c8a9a5SSteve French compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
271238c8a9a5SSteve French {
271338c8a9a5SSteve French 	struct cifs_sb_info *old = CIFS_SB(sb);
271438c8a9a5SSteve French 	struct cifs_sb_info *new = mnt_data->cifs_sb;
271538c8a9a5SSteve French 	unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
271638c8a9a5SSteve French 	unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
271738c8a9a5SSteve French 
271838c8a9a5SSteve French 	if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
271938c8a9a5SSteve French 		return 0;
272038c8a9a5SSteve French 
272138c8a9a5SSteve French 	if (old->mnt_cifs_serverino_autodisabled)
272238c8a9a5SSteve French 		newflags &= ~CIFS_MOUNT_SERVER_INUM;
272338c8a9a5SSteve French 
272438c8a9a5SSteve French 	if (oldflags != newflags)
272538c8a9a5SSteve French 		return 0;
272638c8a9a5SSteve French 
272738c8a9a5SSteve French 	/*
272838c8a9a5SSteve French 	 * We want to share sb only if we don't specify an r/wsize or
272938c8a9a5SSteve French 	 * specified r/wsize is greater than or equal to existing one.
273038c8a9a5SSteve French 	 */
273138c8a9a5SSteve French 	if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
273238c8a9a5SSteve French 		return 0;
273338c8a9a5SSteve French 
273438c8a9a5SSteve French 	if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
273538c8a9a5SSteve French 		return 0;
273638c8a9a5SSteve French 
273738c8a9a5SSteve French 	if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
273838c8a9a5SSteve French 	    !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
273938c8a9a5SSteve French 		return 0;
274038c8a9a5SSteve French 
274138c8a9a5SSteve French 	if (old->ctx->file_mode != new->ctx->file_mode ||
274238c8a9a5SSteve French 	    old->ctx->dir_mode != new->ctx->dir_mode)
274338c8a9a5SSteve French 		return 0;
274438c8a9a5SSteve French 
274538c8a9a5SSteve French 	if (strcmp(old->local_nls->charset, new->local_nls->charset))
274638c8a9a5SSteve French 		return 0;
274738c8a9a5SSteve French 
274838c8a9a5SSteve French 	if (old->ctx->acregmax != new->ctx->acregmax)
274938c8a9a5SSteve French 		return 0;
275038c8a9a5SSteve French 	if (old->ctx->acdirmax != new->ctx->acdirmax)
275138c8a9a5SSteve French 		return 0;
275238c8a9a5SSteve French 	if (old->ctx->closetimeo != new->ctx->closetimeo)
275338c8a9a5SSteve French 		return 0;
275438c8a9a5SSteve French 
275538c8a9a5SSteve French 	return 1;
275638c8a9a5SSteve French }
275738c8a9a5SSteve French 
275838c8a9a5SSteve French static int match_prepath(struct super_block *sb,
27593ae872deSPaulo Alcantara 			 struct cifs_tcon *tcon,
276038c8a9a5SSteve French 			 struct cifs_mnt_data *mnt_data)
276138c8a9a5SSteve French {
276238c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_data->ctx;
276338c8a9a5SSteve French 	struct cifs_sb_info *old = CIFS_SB(sb);
276438c8a9a5SSteve French 	struct cifs_sb_info *new = mnt_data->cifs_sb;
276538c8a9a5SSteve French 	bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
276638c8a9a5SSteve French 		old->prepath;
276738c8a9a5SSteve French 	bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
276838c8a9a5SSteve French 		new->prepath;
276938c8a9a5SSteve French 
27703ae872deSPaulo Alcantara 	if (tcon->origin_fullpath &&
27713ae872deSPaulo Alcantara 	    dfs_src_pathname_equal(tcon->origin_fullpath, ctx->source))
277238c8a9a5SSteve French 		return 1;
277338c8a9a5SSteve French 
277438c8a9a5SSteve French 	if (old_set && new_set && !strcmp(new->prepath, old->prepath))
277538c8a9a5SSteve French 		return 1;
277638c8a9a5SSteve French 	else if (!old_set && !new_set)
277738c8a9a5SSteve French 		return 1;
277838c8a9a5SSteve French 
277938c8a9a5SSteve French 	return 0;
278038c8a9a5SSteve French }
278138c8a9a5SSteve French 
278238c8a9a5SSteve French int
278338c8a9a5SSteve French cifs_match_super(struct super_block *sb, void *data)
278438c8a9a5SSteve French {
278538c8a9a5SSteve French 	struct cifs_mnt_data *mnt_data = data;
278638c8a9a5SSteve French 	struct smb3_fs_context *ctx;
278738c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb;
278838c8a9a5SSteve French 	struct TCP_Server_Info *tcp_srv;
278938c8a9a5SSteve French 	struct cifs_ses *ses;
279038c8a9a5SSteve French 	struct cifs_tcon *tcon;
279138c8a9a5SSteve French 	struct tcon_link *tlink;
279238c8a9a5SSteve French 	int rc = 0;
279338c8a9a5SSteve French 
279438c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
279538c8a9a5SSteve French 	cifs_sb = CIFS_SB(sb);
279638c8a9a5SSteve French 
279738c8a9a5SSteve French 	/* We do not want to use a superblock that has been shutdown */
279838c8a9a5SSteve French 	if (CIFS_MOUNT_SHUTDOWN & cifs_sb->mnt_cifs_flags) {
279938c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
280038c8a9a5SSteve French 		return 0;
280138c8a9a5SSteve French 	}
280238c8a9a5SSteve French 
280338c8a9a5SSteve French 	tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2804fc1bd51dSPaulo Alcantara 	if (IS_ERR_OR_NULL(tlink)) {
2805fc1bd51dSPaulo Alcantara 		pr_warn_once("%s: skip super matching due to bad tlink(%p)\n",
2806fc1bd51dSPaulo Alcantara 			     __func__, tlink);
280738c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
280838c8a9a5SSteve French 		return 0;
280938c8a9a5SSteve French 	}
281038c8a9a5SSteve French 	tcon = tlink_tcon(tlink);
281138c8a9a5SSteve French 	ses = tcon->ses;
281238c8a9a5SSteve French 	tcp_srv = ses->server;
281338c8a9a5SSteve French 
281438c8a9a5SSteve French 	ctx = mnt_data->ctx;
281538c8a9a5SSteve French 
281638c8a9a5SSteve French 	spin_lock(&tcp_srv->srv_lock);
281738c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
281838c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
281938c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
28203ae872deSPaulo Alcantara 	if (!match_server(tcp_srv, ctx, true) ||
282138c8a9a5SSteve French 	    !match_session(ses, ctx) ||
282238c8a9a5SSteve French 	    !match_tcon(tcon, ctx) ||
28233ae872deSPaulo Alcantara 	    !match_prepath(sb, tcon, mnt_data)) {
282438c8a9a5SSteve French 		rc = 0;
282538c8a9a5SSteve French 		goto out;
282638c8a9a5SSteve French 	}
282738c8a9a5SSteve French 
282838c8a9a5SSteve French 	rc = compare_mount_options(sb, mnt_data);
282938c8a9a5SSteve French out:
283038c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
283138c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
283238c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
283338c8a9a5SSteve French 	spin_unlock(&tcp_srv->srv_lock);
283438c8a9a5SSteve French 
283538c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
283638c8a9a5SSteve French 	cifs_put_tlink(tlink);
283738c8a9a5SSteve French 	return rc;
283838c8a9a5SSteve French }
283938c8a9a5SSteve French 
284038c8a9a5SSteve French #ifdef CONFIG_DEBUG_LOCK_ALLOC
284138c8a9a5SSteve French static struct lock_class_key cifs_key[2];
284238c8a9a5SSteve French static struct lock_class_key cifs_slock_key[2];
284338c8a9a5SSteve French 
284438c8a9a5SSteve French static inline void
284538c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock)
284638c8a9a5SSteve French {
284738c8a9a5SSteve French 	struct sock *sk = sock->sk;
284838c8a9a5SSteve French 	BUG_ON(!sock_allow_reclassification(sk));
284938c8a9a5SSteve French 	sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
285038c8a9a5SSteve French 		&cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
285138c8a9a5SSteve French }
285238c8a9a5SSteve French 
285338c8a9a5SSteve French static inline void
285438c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock)
285538c8a9a5SSteve French {
285638c8a9a5SSteve French 	struct sock *sk = sock->sk;
285738c8a9a5SSteve French 	BUG_ON(!sock_allow_reclassification(sk));
285838c8a9a5SSteve French 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
285938c8a9a5SSteve French 		&cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
286038c8a9a5SSteve French }
286138c8a9a5SSteve French #else
286238c8a9a5SSteve French static inline void
286338c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock)
286438c8a9a5SSteve French {
286538c8a9a5SSteve French }
286638c8a9a5SSteve French 
286738c8a9a5SSteve French static inline void
286838c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock)
286938c8a9a5SSteve French {
287038c8a9a5SSteve French }
287138c8a9a5SSteve French #endif
287238c8a9a5SSteve French 
287338c8a9a5SSteve French /* See RFC1001 section 14 on representation of Netbios names */
287438c8a9a5SSteve French static void rfc1002mangle(char *target, char *source, unsigned int length)
287538c8a9a5SSteve French {
287638c8a9a5SSteve French 	unsigned int i, j;
287738c8a9a5SSteve French 
287838c8a9a5SSteve French 	for (i = 0, j = 0; i < (length); i++) {
287938c8a9a5SSteve French 		/* mask a nibble at a time and encode */
288038c8a9a5SSteve French 		target[j] = 'A' + (0x0F & (source[i] >> 4));
288138c8a9a5SSteve French 		target[j+1] = 'A' + (0x0F & source[i]);
288238c8a9a5SSteve French 		j += 2;
288338c8a9a5SSteve French 	}
288438c8a9a5SSteve French 
288538c8a9a5SSteve French }
288638c8a9a5SSteve French 
288738c8a9a5SSteve French static int
288838c8a9a5SSteve French bind_socket(struct TCP_Server_Info *server)
288938c8a9a5SSteve French {
289038c8a9a5SSteve French 	int rc = 0;
289138c8a9a5SSteve French 	if (server->srcaddr.ss_family != AF_UNSPEC) {
289238c8a9a5SSteve French 		/* Bind to the specified local IP address */
289338c8a9a5SSteve French 		struct socket *socket = server->ssocket;
289438c8a9a5SSteve French 		rc = socket->ops->bind(socket,
289538c8a9a5SSteve French 				       (struct sockaddr *) &server->srcaddr,
289638c8a9a5SSteve French 				       sizeof(server->srcaddr));
289738c8a9a5SSteve French 		if (rc < 0) {
289838c8a9a5SSteve French 			struct sockaddr_in *saddr4;
289938c8a9a5SSteve French 			struct sockaddr_in6 *saddr6;
290038c8a9a5SSteve French 			saddr4 = (struct sockaddr_in *)&server->srcaddr;
290138c8a9a5SSteve French 			saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
290238c8a9a5SSteve French 			if (saddr6->sin6_family == AF_INET6)
290338c8a9a5SSteve French 				cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
290438c8a9a5SSteve French 					 &saddr6->sin6_addr, rc);
290538c8a9a5SSteve French 			else
290638c8a9a5SSteve French 				cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
290738c8a9a5SSteve French 					 &saddr4->sin_addr.s_addr, rc);
290838c8a9a5SSteve French 		}
290938c8a9a5SSteve French 	}
291038c8a9a5SSteve French 	return rc;
291138c8a9a5SSteve French }
291238c8a9a5SSteve French 
291338c8a9a5SSteve French static int
291438c8a9a5SSteve French ip_rfc1001_connect(struct TCP_Server_Info *server)
291538c8a9a5SSteve French {
291638c8a9a5SSteve French 	int rc = 0;
291738c8a9a5SSteve French 	/*
291838c8a9a5SSteve French 	 * some servers require RFC1001 sessinit before sending
291938c8a9a5SSteve French 	 * negprot - BB check reconnection in case where second
292038c8a9a5SSteve French 	 * sessinit is sent but no second negprot
292138c8a9a5SSteve French 	 */
292238c8a9a5SSteve French 	struct rfc1002_session_packet req = {};
292338c8a9a5SSteve French 	struct smb_hdr *smb_buf = (struct smb_hdr *)&req;
292438c8a9a5SSteve French 	unsigned int len;
292538c8a9a5SSteve French 
292638c8a9a5SSteve French 	req.trailer.session_req.called_len = sizeof(req.trailer.session_req.called_name);
292738c8a9a5SSteve French 
292838c8a9a5SSteve French 	if (server->server_RFC1001_name[0] != 0)
292938c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.called_name,
293038c8a9a5SSteve French 			      server->server_RFC1001_name,
293138c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
293238c8a9a5SSteve French 	else
293338c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.called_name,
293438c8a9a5SSteve French 			      DEFAULT_CIFS_CALLED_NAME,
293538c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
293638c8a9a5SSteve French 
293738c8a9a5SSteve French 	req.trailer.session_req.calling_len = sizeof(req.trailer.session_req.calling_name);
293838c8a9a5SSteve French 
293938c8a9a5SSteve French 	/* calling name ends in null (byte 16) from old smb convention */
294038c8a9a5SSteve French 	if (server->workstation_RFC1001_name[0] != 0)
294138c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.calling_name,
294238c8a9a5SSteve French 			      server->workstation_RFC1001_name,
294338c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
294438c8a9a5SSteve French 	else
294538c8a9a5SSteve French 		rfc1002mangle(req.trailer.session_req.calling_name,
294638c8a9a5SSteve French 			      "LINUX_CIFS_CLNT",
294738c8a9a5SSteve French 			      RFC1001_NAME_LEN_WITH_NULL);
294838c8a9a5SSteve French 
294938c8a9a5SSteve French 	/*
295038c8a9a5SSteve French 	 * As per rfc1002, @len must be the number of bytes that follows the
295138c8a9a5SSteve French 	 * length field of a rfc1002 session request payload.
295238c8a9a5SSteve French 	 */
295338c8a9a5SSteve French 	len = sizeof(req) - offsetof(struct rfc1002_session_packet, trailer.session_req);
295438c8a9a5SSteve French 
295538c8a9a5SSteve French 	smb_buf->smb_buf_length = cpu_to_be32((RFC1002_SESSION_REQUEST << 24) | len);
295638c8a9a5SSteve French 	rc = smb_send(server, smb_buf, len);
295738c8a9a5SSteve French 	/*
295838c8a9a5SSteve French 	 * RFC1001 layer in at least one server requires very short break before
295938c8a9a5SSteve French 	 * negprot presumably because not expecting negprot to follow so fast.
296038c8a9a5SSteve French 	 * This is a simple solution that works without complicating the code
296138c8a9a5SSteve French 	 * and causes no significant slowing down on mount for everyone else
296238c8a9a5SSteve French 	 */
296338c8a9a5SSteve French 	usleep_range(1000, 2000);
296438c8a9a5SSteve French 
296538c8a9a5SSteve French 	return rc;
296638c8a9a5SSteve French }
296738c8a9a5SSteve French 
296838c8a9a5SSteve French static int
296938c8a9a5SSteve French generic_ip_connect(struct TCP_Server_Info *server)
297038c8a9a5SSteve French {
297138c8a9a5SSteve French 	struct sockaddr *saddr;
2972f0b6a834SPaulo Alcantara 	struct socket *socket;
2973f0b6a834SPaulo Alcantara 	int slen, sfamily;
2974f0b6a834SPaulo Alcantara 	__be16 sport;
2975f0b6a834SPaulo Alcantara 	int rc = 0;
297638c8a9a5SSteve French 
297738c8a9a5SSteve French 	saddr = (struct sockaddr *) &server->dstaddr;
297838c8a9a5SSteve French 
297938c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6) {
298038c8a9a5SSteve French 		struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
298138c8a9a5SSteve French 
298238c8a9a5SSteve French 		sport = ipv6->sin6_port;
298338c8a9a5SSteve French 		slen = sizeof(struct sockaddr_in6);
298438c8a9a5SSteve French 		sfamily = AF_INET6;
298538c8a9a5SSteve French 		cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
298638c8a9a5SSteve French 				ntohs(sport));
298738c8a9a5SSteve French 	} else {
298838c8a9a5SSteve French 		struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
298938c8a9a5SSteve French 
299038c8a9a5SSteve French 		sport = ipv4->sin_port;
299138c8a9a5SSteve French 		slen = sizeof(struct sockaddr_in);
299238c8a9a5SSteve French 		sfamily = AF_INET;
299338c8a9a5SSteve French 		cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
299438c8a9a5SSteve French 				ntohs(sport));
299538c8a9a5SSteve French 	}
299638c8a9a5SSteve French 
2997f0b6a834SPaulo Alcantara 	if (server->ssocket) {
2998f0b6a834SPaulo Alcantara 		socket = server->ssocket;
2999f0b6a834SPaulo Alcantara 	} else {
300038c8a9a5SSteve French 		rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3001f0b6a834SPaulo Alcantara 				   IPPROTO_TCP, &server->ssocket, 1);
300238c8a9a5SSteve French 		if (rc < 0) {
300338c8a9a5SSteve French 			cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
300438c8a9a5SSteve French 			return rc;
300538c8a9a5SSteve French 		}
300638c8a9a5SSteve French 
300738c8a9a5SSteve French 		/* BB other socket options to set KEEPALIVE, NODELAY? */
300838c8a9a5SSteve French 		cifs_dbg(FYI, "Socket created\n");
3009f0b6a834SPaulo Alcantara 		socket = server->ssocket;
301038c8a9a5SSteve French 		socket->sk->sk_allocation = GFP_NOFS;
301138c8a9a5SSteve French 		socket->sk->sk_use_task_frag = false;
301238c8a9a5SSteve French 		if (sfamily == AF_INET6)
301338c8a9a5SSteve French 			cifs_reclassify_socket6(socket);
301438c8a9a5SSteve French 		else
301538c8a9a5SSteve French 			cifs_reclassify_socket4(socket);
301638c8a9a5SSteve French 	}
301738c8a9a5SSteve French 
301838c8a9a5SSteve French 	rc = bind_socket(server);
301938c8a9a5SSteve French 	if (rc < 0)
302038c8a9a5SSteve French 		return rc;
302138c8a9a5SSteve French 
302238c8a9a5SSteve French 	/*
302338c8a9a5SSteve French 	 * Eventually check for other socket options to change from
302438c8a9a5SSteve French 	 * the default. sock_setsockopt not used because it expects
302538c8a9a5SSteve French 	 * user space buffer
302638c8a9a5SSteve French 	 */
302738c8a9a5SSteve French 	socket->sk->sk_rcvtimeo = 7 * HZ;
302838c8a9a5SSteve French 	socket->sk->sk_sndtimeo = 5 * HZ;
302938c8a9a5SSteve French 
303038c8a9a5SSteve French 	/* make the bufsizes depend on wsize/rsize and max requests */
303138c8a9a5SSteve French 	if (server->noautotune) {
303238c8a9a5SSteve French 		if (socket->sk->sk_sndbuf < (200 * 1024))
303338c8a9a5SSteve French 			socket->sk->sk_sndbuf = 200 * 1024;
303438c8a9a5SSteve French 		if (socket->sk->sk_rcvbuf < (140 * 1024))
303538c8a9a5SSteve French 			socket->sk->sk_rcvbuf = 140 * 1024;
303638c8a9a5SSteve French 	}
303738c8a9a5SSteve French 
303838c8a9a5SSteve French 	if (server->tcp_nodelay)
303938c8a9a5SSteve French 		tcp_sock_set_nodelay(socket->sk);
304038c8a9a5SSteve French 
304138c8a9a5SSteve French 	cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
304238c8a9a5SSteve French 		 socket->sk->sk_sndbuf,
304338c8a9a5SSteve French 		 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
304438c8a9a5SSteve French 
304538c8a9a5SSteve French 	rc = socket->ops->connect(socket, saddr, slen,
304638c8a9a5SSteve French 				  server->noblockcnt ? O_NONBLOCK : 0);
304738c8a9a5SSteve French 	/*
304838c8a9a5SSteve French 	 * When mounting SMB root file systems, we do not want to block in
304938c8a9a5SSteve French 	 * connect. Otherwise bail out and then let cifs_reconnect() perform
305038c8a9a5SSteve French 	 * reconnect failover - if possible.
305138c8a9a5SSteve French 	 */
305238c8a9a5SSteve French 	if (server->noblockcnt && rc == -EINPROGRESS)
305338c8a9a5SSteve French 		rc = 0;
305438c8a9a5SSteve French 	if (rc < 0) {
305538c8a9a5SSteve French 		cifs_dbg(FYI, "Error %d connecting to server\n", rc);
305638c8a9a5SSteve French 		trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
305738c8a9a5SSteve French 		sock_release(socket);
305838c8a9a5SSteve French 		server->ssocket = NULL;
305938c8a9a5SSteve French 		return rc;
306038c8a9a5SSteve French 	}
306138c8a9a5SSteve French 	trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
306238c8a9a5SSteve French 	if (sport == htons(RFC1001_PORT))
306338c8a9a5SSteve French 		rc = ip_rfc1001_connect(server);
306438c8a9a5SSteve French 
306538c8a9a5SSteve French 	return rc;
306638c8a9a5SSteve French }
306738c8a9a5SSteve French 
306838c8a9a5SSteve French static int
306938c8a9a5SSteve French ip_connect(struct TCP_Server_Info *server)
307038c8a9a5SSteve French {
307138c8a9a5SSteve French 	__be16 *sport;
307238c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
307338c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
307438c8a9a5SSteve French 
307538c8a9a5SSteve French 	if (server->dstaddr.ss_family == AF_INET6)
307638c8a9a5SSteve French 		sport = &addr6->sin6_port;
307738c8a9a5SSteve French 	else
307838c8a9a5SSteve French 		sport = &addr->sin_port;
307938c8a9a5SSteve French 
308038c8a9a5SSteve French 	if (*sport == 0) {
308138c8a9a5SSteve French 		int rc;
308238c8a9a5SSteve French 
308338c8a9a5SSteve French 		/* try with 445 port at first */
308438c8a9a5SSteve French 		*sport = htons(CIFS_PORT);
308538c8a9a5SSteve French 
308638c8a9a5SSteve French 		rc = generic_ip_connect(server);
308738c8a9a5SSteve French 		if (rc >= 0)
308838c8a9a5SSteve French 			return rc;
308938c8a9a5SSteve French 
309038c8a9a5SSteve French 		/* if it failed, try with 139 port */
309138c8a9a5SSteve French 		*sport = htons(RFC1001_PORT);
309238c8a9a5SSteve French 	}
309338c8a9a5SSteve French 
309438c8a9a5SSteve French 	return generic_ip_connect(server);
309538c8a9a5SSteve French }
309638c8a9a5SSteve French 
309738c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
309838c8a9a5SSteve French void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
309938c8a9a5SSteve French 			  struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
310038c8a9a5SSteve French {
310138c8a9a5SSteve French 	/*
310238c8a9a5SSteve French 	 * If we are reconnecting then should we check to see if
310338c8a9a5SSteve French 	 * any requested capabilities changed locally e.g. via
310438c8a9a5SSteve French 	 * remount but we can not do much about it here
310538c8a9a5SSteve French 	 * if they have (even if we could detect it by the following)
310638c8a9a5SSteve French 	 * Perhaps we could add a backpointer to array of sb from tcon
310738c8a9a5SSteve French 	 * or if we change to make all sb to same share the same
310838c8a9a5SSteve French 	 * sb as NFS - then we only have one backpointer to sb.
310938c8a9a5SSteve French 	 * What if we wanted to mount the server share twice once with
311038c8a9a5SSteve French 	 * and once without posixacls or posix paths?
311138c8a9a5SSteve French 	 */
311238c8a9a5SSteve French 	__u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
311338c8a9a5SSteve French 
311438c8a9a5SSteve French 	if (ctx && ctx->no_linux_ext) {
311538c8a9a5SSteve French 		tcon->fsUnixInfo.Capability = 0;
311638c8a9a5SSteve French 		tcon->unix_ext = 0; /* Unix Extensions disabled */
311738c8a9a5SSteve French 		cifs_dbg(FYI, "Linux protocol extensions disabled\n");
311838c8a9a5SSteve French 		return;
311938c8a9a5SSteve French 	} else if (ctx)
312038c8a9a5SSteve French 		tcon->unix_ext = 1; /* Unix Extensions supported */
312138c8a9a5SSteve French 
312238c8a9a5SSteve French 	if (!tcon->unix_ext) {
312338c8a9a5SSteve French 		cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
312438c8a9a5SSteve French 		return;
312538c8a9a5SSteve French 	}
312638c8a9a5SSteve French 
312738c8a9a5SSteve French 	if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
312838c8a9a5SSteve French 		__u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
312938c8a9a5SSteve French 		cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
313038c8a9a5SSteve French 		/*
313138c8a9a5SSteve French 		 * check for reconnect case in which we do not
313238c8a9a5SSteve French 		 * want to change the mount behavior if we can avoid it
313338c8a9a5SSteve French 		 */
313438c8a9a5SSteve French 		if (ctx == NULL) {
313538c8a9a5SSteve French 			/*
313638c8a9a5SSteve French 			 * turn off POSIX ACL and PATHNAMES if not set
313738c8a9a5SSteve French 			 * originally at mount time
313838c8a9a5SSteve French 			 */
313938c8a9a5SSteve French 			if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
314038c8a9a5SSteve French 				cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
314138c8a9a5SSteve French 			if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
314238c8a9a5SSteve French 				if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
314338c8a9a5SSteve French 					cifs_dbg(VFS, "POSIXPATH support change\n");
314438c8a9a5SSteve French 				cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
314538c8a9a5SSteve French 			} else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
314638c8a9a5SSteve French 				cifs_dbg(VFS, "possible reconnect error\n");
314738c8a9a5SSteve French 				cifs_dbg(VFS, "server disabled POSIX path support\n");
314838c8a9a5SSteve French 			}
314938c8a9a5SSteve French 		}
315038c8a9a5SSteve French 
315138c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
315238c8a9a5SSteve French 			cifs_dbg(VFS, "per-share encryption not supported yet\n");
315338c8a9a5SSteve French 
315438c8a9a5SSteve French 		cap &= CIFS_UNIX_CAP_MASK;
315538c8a9a5SSteve French 		if (ctx && ctx->no_psx_acl)
315638c8a9a5SSteve French 			cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
315738c8a9a5SSteve French 		else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
315838c8a9a5SSteve French 			cifs_dbg(FYI, "negotiated posix acl support\n");
315938c8a9a5SSteve French 			if (cifs_sb)
316038c8a9a5SSteve French 				cifs_sb->mnt_cifs_flags |=
316138c8a9a5SSteve French 					CIFS_MOUNT_POSIXACL;
316238c8a9a5SSteve French 		}
316338c8a9a5SSteve French 
316438c8a9a5SSteve French 		if (ctx && ctx->posix_paths == 0)
316538c8a9a5SSteve French 			cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
316638c8a9a5SSteve French 		else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
316738c8a9a5SSteve French 			cifs_dbg(FYI, "negotiate posix pathnames\n");
316838c8a9a5SSteve French 			if (cifs_sb)
316938c8a9a5SSteve French 				cifs_sb->mnt_cifs_flags |=
317038c8a9a5SSteve French 					CIFS_MOUNT_POSIX_PATHS;
317138c8a9a5SSteve French 		}
317238c8a9a5SSteve French 
317338c8a9a5SSteve French 		cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
317438c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2
317538c8a9a5SSteve French 		if (cap & CIFS_UNIX_FCNTL_CAP)
317638c8a9a5SSteve French 			cifs_dbg(FYI, "FCNTL cap\n");
317738c8a9a5SSteve French 		if (cap & CIFS_UNIX_EXTATTR_CAP)
317838c8a9a5SSteve French 			cifs_dbg(FYI, "EXTATTR cap\n");
317938c8a9a5SSteve French 		if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
318038c8a9a5SSteve French 			cifs_dbg(FYI, "POSIX path cap\n");
318138c8a9a5SSteve French 		if (cap & CIFS_UNIX_XATTR_CAP)
318238c8a9a5SSteve French 			cifs_dbg(FYI, "XATTR cap\n");
318338c8a9a5SSteve French 		if (cap & CIFS_UNIX_POSIX_ACL_CAP)
318438c8a9a5SSteve French 			cifs_dbg(FYI, "POSIX ACL cap\n");
318538c8a9a5SSteve French 		if (cap & CIFS_UNIX_LARGE_READ_CAP)
318638c8a9a5SSteve French 			cifs_dbg(FYI, "very large read cap\n");
318738c8a9a5SSteve French 		if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
318838c8a9a5SSteve French 			cifs_dbg(FYI, "very large write cap\n");
318938c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
319038c8a9a5SSteve French 			cifs_dbg(FYI, "transport encryption cap\n");
319138c8a9a5SSteve French 		if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
319238c8a9a5SSteve French 			cifs_dbg(FYI, "mandatory transport encryption cap\n");
319338c8a9a5SSteve French #endif /* CIFS_DEBUG2 */
319438c8a9a5SSteve French 		if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
319538c8a9a5SSteve French 			if (ctx == NULL)
319638c8a9a5SSteve French 				cifs_dbg(FYI, "resetting capabilities failed\n");
319738c8a9a5SSteve French 			else
319838c8a9a5SSteve 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");
319938c8a9a5SSteve French 
320038c8a9a5SSteve French 		}
320138c8a9a5SSteve French 	}
320238c8a9a5SSteve French }
320338c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
320438c8a9a5SSteve French 
320538c8a9a5SSteve French int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
320638c8a9a5SSteve French {
320738c8a9a5SSteve French 	struct smb3_fs_context *ctx = cifs_sb->ctx;
320838c8a9a5SSteve French 
320938c8a9a5SSteve French 	INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
321038c8a9a5SSteve French 
321138c8a9a5SSteve French 	spin_lock_init(&cifs_sb->tlink_tree_lock);
321238c8a9a5SSteve French 	cifs_sb->tlink_tree = RB_ROOT;
321338c8a9a5SSteve French 
321438c8a9a5SSteve French 	cifs_dbg(FYI, "file mode: %04ho  dir mode: %04ho\n",
321538c8a9a5SSteve French 		 ctx->file_mode, ctx->dir_mode);
321638c8a9a5SSteve French 
321738c8a9a5SSteve French 	/* this is needed for ASCII cp to Unicode converts */
321838c8a9a5SSteve French 	if (ctx->iocharset == NULL) {
321938c8a9a5SSteve French 		/* load_nls_default cannot return null */
322038c8a9a5SSteve French 		cifs_sb->local_nls = load_nls_default();
322138c8a9a5SSteve French 	} else {
322238c8a9a5SSteve French 		cifs_sb->local_nls = load_nls(ctx->iocharset);
322338c8a9a5SSteve French 		if (cifs_sb->local_nls == NULL) {
322438c8a9a5SSteve French 			cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
322538c8a9a5SSteve French 				 ctx->iocharset);
322638c8a9a5SSteve French 			return -ELIBACC;
322738c8a9a5SSteve French 		}
322838c8a9a5SSteve French 	}
322938c8a9a5SSteve French 	ctx->local_nls = cifs_sb->local_nls;
323038c8a9a5SSteve French 
323138c8a9a5SSteve French 	smb3_update_mnt_flags(cifs_sb);
323238c8a9a5SSteve French 
323338c8a9a5SSteve French 	if (ctx->direct_io)
323438c8a9a5SSteve French 		cifs_dbg(FYI, "mounting share using direct i/o\n");
323538c8a9a5SSteve French 	if (ctx->cache_ro) {
323638c8a9a5SSteve French 		cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
323738c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
323838c8a9a5SSteve French 	} else if (ctx->cache_rw) {
323938c8a9a5SSteve French 		cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
324038c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
324138c8a9a5SSteve French 					    CIFS_MOUNT_RW_CACHE);
324238c8a9a5SSteve French 	}
324338c8a9a5SSteve French 
324438c8a9a5SSteve French 	if ((ctx->cifs_acl) && (ctx->dynperm))
324538c8a9a5SSteve French 		cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
324638c8a9a5SSteve French 
324738c8a9a5SSteve French 	if (ctx->prepath) {
324838c8a9a5SSteve French 		cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
324938c8a9a5SSteve French 		if (cifs_sb->prepath == NULL)
325038c8a9a5SSteve French 			return -ENOMEM;
325138c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
325238c8a9a5SSteve French 	}
325338c8a9a5SSteve French 
325438c8a9a5SSteve French 	return 0;
325538c8a9a5SSteve French }
325638c8a9a5SSteve French 
325738c8a9a5SSteve French /* Release all succeed connections */
325838c8a9a5SSteve French void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx)
325938c8a9a5SSteve French {
326038c8a9a5SSteve French 	int rc = 0;
326138c8a9a5SSteve French 
326238c8a9a5SSteve French 	if (mnt_ctx->tcon)
326338c8a9a5SSteve French 		cifs_put_tcon(mnt_ctx->tcon);
326438c8a9a5SSteve French 	else if (mnt_ctx->ses)
326538c8a9a5SSteve French 		cifs_put_smb_ses(mnt_ctx->ses);
326638c8a9a5SSteve French 	else if (mnt_ctx->server)
326738c8a9a5SSteve French 		cifs_put_tcp_session(mnt_ctx->server, 0);
326838c8a9a5SSteve French 	mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
326938c8a9a5SSteve French 	free_xid(mnt_ctx->xid);
327038c8a9a5SSteve French }
327138c8a9a5SSteve French 
327238c8a9a5SSteve French int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx)
327338c8a9a5SSteve French {
327438c8a9a5SSteve French 	struct TCP_Server_Info *server = NULL;
327538c8a9a5SSteve French 	struct smb3_fs_context *ctx;
327638c8a9a5SSteve French 	struct cifs_ses *ses = NULL;
327738c8a9a5SSteve French 	unsigned int xid;
327838c8a9a5SSteve French 	int rc = 0;
327938c8a9a5SSteve French 
328038c8a9a5SSteve French 	xid = get_xid();
328138c8a9a5SSteve French 
328238c8a9a5SSteve French 	if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->fs_ctx)) {
328338c8a9a5SSteve French 		rc = -EINVAL;
328438c8a9a5SSteve French 		goto out;
328538c8a9a5SSteve French 	}
328638c8a9a5SSteve French 	ctx = mnt_ctx->fs_ctx;
328738c8a9a5SSteve French 
328838c8a9a5SSteve French 	/* get a reference to a tcp session */
328938c8a9a5SSteve French 	server = cifs_get_tcp_session(ctx, NULL);
329038c8a9a5SSteve French 	if (IS_ERR(server)) {
329138c8a9a5SSteve French 		rc = PTR_ERR(server);
329238c8a9a5SSteve French 		server = NULL;
329338c8a9a5SSteve French 		goto out;
329438c8a9a5SSteve French 	}
329538c8a9a5SSteve French 
329638c8a9a5SSteve French 	/* get a reference to a SMB session */
329738c8a9a5SSteve French 	ses = cifs_get_smb_ses(server, ctx);
329838c8a9a5SSteve French 	if (IS_ERR(ses)) {
329938c8a9a5SSteve French 		rc = PTR_ERR(ses);
330038c8a9a5SSteve French 		ses = NULL;
330138c8a9a5SSteve French 		goto out;
330238c8a9a5SSteve French 	}
330338c8a9a5SSteve French 
330438c8a9a5SSteve French 	if ((ctx->persistent == true) && (!(ses->server->capabilities &
330538c8a9a5SSteve French 					    SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
330638c8a9a5SSteve French 		cifs_server_dbg(VFS, "persistent handles not supported by server\n");
330738c8a9a5SSteve French 		rc = -EOPNOTSUPP;
330838c8a9a5SSteve French 	}
330938c8a9a5SSteve French 
331038c8a9a5SSteve French out:
331138c8a9a5SSteve French 	mnt_ctx->xid = xid;
331238c8a9a5SSteve French 	mnt_ctx->server = server;
331338c8a9a5SSteve French 	mnt_ctx->ses = ses;
331438c8a9a5SSteve French 	mnt_ctx->tcon = NULL;
331538c8a9a5SSteve French 
331638c8a9a5SSteve French 	return rc;
331738c8a9a5SSteve French }
331838c8a9a5SSteve French 
331938c8a9a5SSteve French int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx)
332038c8a9a5SSteve French {
332138c8a9a5SSteve French 	struct TCP_Server_Info *server;
332238c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb;
332338c8a9a5SSteve French 	struct smb3_fs_context *ctx;
332438c8a9a5SSteve French 	struct cifs_tcon *tcon = NULL;
332538c8a9a5SSteve French 	int rc = 0;
332638c8a9a5SSteve French 
332738c8a9a5SSteve French 	if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->server || !mnt_ctx->ses || !mnt_ctx->fs_ctx ||
332838c8a9a5SSteve French 			 !mnt_ctx->cifs_sb)) {
332938c8a9a5SSteve French 		rc = -EINVAL;
333038c8a9a5SSteve French 		goto out;
333138c8a9a5SSteve French 	}
333238c8a9a5SSteve French 	server = mnt_ctx->server;
333338c8a9a5SSteve French 	ctx = mnt_ctx->fs_ctx;
333438c8a9a5SSteve French 	cifs_sb = mnt_ctx->cifs_sb;
333538c8a9a5SSteve French 
333638c8a9a5SSteve French 	/* search for existing tcon to this server share */
333738c8a9a5SSteve French 	tcon = cifs_get_tcon(mnt_ctx->ses, ctx);
333838c8a9a5SSteve French 	if (IS_ERR(tcon)) {
333938c8a9a5SSteve French 		rc = PTR_ERR(tcon);
334038c8a9a5SSteve French 		tcon = NULL;
334138c8a9a5SSteve French 		goto out;
334238c8a9a5SSteve French 	}
334338c8a9a5SSteve French 
334438c8a9a5SSteve French 	/* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
334538c8a9a5SSteve French 	if (tcon->posix_extensions)
334638c8a9a5SSteve French 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
334738c8a9a5SSteve French 
334838c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
334938c8a9a5SSteve French 	/* tell server which Unix caps we support */
335038c8a9a5SSteve French 	if (cap_unix(tcon->ses)) {
335138c8a9a5SSteve French 		/*
335238c8a9a5SSteve French 		 * reset of caps checks mount to see if unix extensions disabled
335338c8a9a5SSteve French 		 * for just this mount.
335438c8a9a5SSteve French 		 */
335538c8a9a5SSteve French 		reset_cifs_unix_caps(mnt_ctx->xid, tcon, cifs_sb, ctx);
335638c8a9a5SSteve French 		spin_lock(&tcon->ses->server->srv_lock);
335738c8a9a5SSteve French 		if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
335838c8a9a5SSteve French 		    (le64_to_cpu(tcon->fsUnixInfo.Capability) &
335938c8a9a5SSteve French 		     CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
336038c8a9a5SSteve French 			spin_unlock(&tcon->ses->server->srv_lock);
336138c8a9a5SSteve French 			rc = -EACCES;
336238c8a9a5SSteve French 			goto out;
336338c8a9a5SSteve French 		}
336438c8a9a5SSteve French 		spin_unlock(&tcon->ses->server->srv_lock);
336538c8a9a5SSteve French 	} else
336638c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
336738c8a9a5SSteve French 		tcon->unix_ext = 0; /* server does not support them */
336838c8a9a5SSteve French 
336938c8a9a5SSteve French 	/* do not care if a following call succeed - informational */
337038c8a9a5SSteve French 	if (!tcon->pipe && server->ops->qfs_tcon) {
337138c8a9a5SSteve French 		server->ops->qfs_tcon(mnt_ctx->xid, tcon, cifs_sb);
337238c8a9a5SSteve French 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
337338c8a9a5SSteve French 			if (tcon->fsDevInfo.DeviceCharacteristics &
337438c8a9a5SSteve French 			    cpu_to_le32(FILE_READ_ONLY_DEVICE))
337538c8a9a5SSteve French 				cifs_dbg(VFS, "mounted to read only share\n");
337638c8a9a5SSteve French 			else if ((cifs_sb->mnt_cifs_flags &
337738c8a9a5SSteve French 				  CIFS_MOUNT_RW_CACHE) == 0)
337838c8a9a5SSteve French 				cifs_dbg(VFS, "read only mount of RW share\n");
337938c8a9a5SSteve French 			/* no need to log a RW mount of a typical RW share */
338038c8a9a5SSteve French 		}
338138c8a9a5SSteve French 	}
338238c8a9a5SSteve French 
338338c8a9a5SSteve French 	/*
338438c8a9a5SSteve French 	 * Clamp the rsize/wsize mount arguments if they are too big for the server
338538c8a9a5SSteve French 	 * and set the rsize/wsize to the negotiated values if not passed in by
338638c8a9a5SSteve French 	 * the user on mount
338738c8a9a5SSteve French 	 */
338838c8a9a5SSteve French 	if ((cifs_sb->ctx->wsize == 0) ||
338938c8a9a5SSteve French 	    (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
339038c8a9a5SSteve French 		cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
339138c8a9a5SSteve French 	if ((cifs_sb->ctx->rsize == 0) ||
339238c8a9a5SSteve French 	    (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
339338c8a9a5SSteve French 		cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
339438c8a9a5SSteve French 
339538c8a9a5SSteve French 	/*
339638c8a9a5SSteve French 	 * The cookie is initialized from volume info returned above.
339738c8a9a5SSteve French 	 * Inside cifs_fscache_get_super_cookie it checks
339838c8a9a5SSteve French 	 * that we do not get super cookie twice.
339938c8a9a5SSteve French 	 */
340038c8a9a5SSteve French 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
340138c8a9a5SSteve French 		cifs_fscache_get_super_cookie(tcon);
340238c8a9a5SSteve French 
340338c8a9a5SSteve French out:
340438c8a9a5SSteve French 	mnt_ctx->tcon = tcon;
340538c8a9a5SSteve French 	return rc;
340638c8a9a5SSteve French }
340738c8a9a5SSteve French 
340838c8a9a5SSteve French static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
340938c8a9a5SSteve French 			     struct cifs_tcon *tcon)
341038c8a9a5SSteve French {
341138c8a9a5SSteve French 	struct tcon_link *tlink;
341238c8a9a5SSteve French 
341338c8a9a5SSteve French 	/* hang the tcon off of the superblock */
341438c8a9a5SSteve French 	tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
341538c8a9a5SSteve French 	if (tlink == NULL)
341638c8a9a5SSteve French 		return -ENOMEM;
341738c8a9a5SSteve French 
341838c8a9a5SSteve French 	tlink->tl_uid = ses->linux_uid;
341938c8a9a5SSteve French 	tlink->tl_tcon = tcon;
342038c8a9a5SSteve French 	tlink->tl_time = jiffies;
342138c8a9a5SSteve French 	set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
342238c8a9a5SSteve French 	set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
342338c8a9a5SSteve French 
342438c8a9a5SSteve French 	cifs_sb->master_tlink = tlink;
342538c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
342638c8a9a5SSteve French 	tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
342738c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
342838c8a9a5SSteve French 
342938c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
343038c8a9a5SSteve French 				TLINK_IDLE_EXPIRE);
343138c8a9a5SSteve French 	return 0;
343238c8a9a5SSteve French }
343338c8a9a5SSteve French 
343438c8a9a5SSteve French static int
343538c8a9a5SSteve French cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
343638c8a9a5SSteve French 					unsigned int xid,
343738c8a9a5SSteve French 					struct cifs_tcon *tcon,
343838c8a9a5SSteve French 					struct cifs_sb_info *cifs_sb,
343938c8a9a5SSteve French 					char *full_path,
344038c8a9a5SSteve French 					int added_treename)
344138c8a9a5SSteve French {
344238c8a9a5SSteve French 	int rc;
344338c8a9a5SSteve French 	char *s;
344438c8a9a5SSteve French 	char sep, tmp;
344538c8a9a5SSteve French 	int skip = added_treename ? 1 : 0;
344638c8a9a5SSteve French 
344738c8a9a5SSteve French 	sep = CIFS_DIR_SEP(cifs_sb);
344838c8a9a5SSteve French 	s = full_path;
344938c8a9a5SSteve French 
345038c8a9a5SSteve French 	rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
345138c8a9a5SSteve French 	while (rc == 0) {
345238c8a9a5SSteve French 		/* skip separators */
345338c8a9a5SSteve French 		while (*s == sep)
345438c8a9a5SSteve French 			s++;
345538c8a9a5SSteve French 		if (!*s)
345638c8a9a5SSteve French 			break;
345738c8a9a5SSteve French 		/* next separator */
345838c8a9a5SSteve French 		while (*s && *s != sep)
345938c8a9a5SSteve French 			s++;
346038c8a9a5SSteve French 		/*
346138c8a9a5SSteve French 		 * if the treename is added, we then have to skip the first
346238c8a9a5SSteve French 		 * part within the separators
346338c8a9a5SSteve French 		 */
346438c8a9a5SSteve French 		if (skip) {
346538c8a9a5SSteve French 			skip = 0;
346638c8a9a5SSteve French 			continue;
346738c8a9a5SSteve French 		}
346838c8a9a5SSteve French 		/*
346938c8a9a5SSteve French 		 * temporarily null-terminate the path at the end of
347038c8a9a5SSteve French 		 * the current component
347138c8a9a5SSteve French 		 */
347238c8a9a5SSteve French 		tmp = *s;
347338c8a9a5SSteve French 		*s = 0;
347438c8a9a5SSteve French 		rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
347538c8a9a5SSteve French 						     full_path);
347638c8a9a5SSteve French 		*s = tmp;
347738c8a9a5SSteve French 	}
347838c8a9a5SSteve French 	return rc;
347938c8a9a5SSteve French }
348038c8a9a5SSteve French 
348138c8a9a5SSteve French /*
348238c8a9a5SSteve French  * Check if path is remote (i.e. a DFS share).
348338c8a9a5SSteve French  *
348438c8a9a5SSteve French  * Return -EREMOTE if it is, otherwise 0 or -errno.
348538c8a9a5SSteve French  */
348638c8a9a5SSteve French int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx)
348738c8a9a5SSteve French {
348838c8a9a5SSteve French 	int rc;
348938c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
349038c8a9a5SSteve French 	struct TCP_Server_Info *server = mnt_ctx->server;
349138c8a9a5SSteve French 	unsigned int xid = mnt_ctx->xid;
349238c8a9a5SSteve French 	struct cifs_tcon *tcon = mnt_ctx->tcon;
349338c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
349438c8a9a5SSteve French 	char *full_path;
349538c8a9a5SSteve French 
349638c8a9a5SSteve French 	if (!server->ops->is_path_accessible)
349738c8a9a5SSteve French 		return -EOPNOTSUPP;
349838c8a9a5SSteve French 
349938c8a9a5SSteve French 	/*
350038c8a9a5SSteve French 	 * cifs_build_path_to_root works only when we have a valid tcon
350138c8a9a5SSteve French 	 */
350238c8a9a5SSteve French 	full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
350338c8a9a5SSteve French 					    tcon->Flags & SMB_SHARE_IS_IN_DFS);
350438c8a9a5SSteve French 	if (full_path == NULL)
350538c8a9a5SSteve French 		return -ENOMEM;
350638c8a9a5SSteve French 
350738c8a9a5SSteve French 	cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
350838c8a9a5SSteve French 
350938c8a9a5SSteve French 	rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
351038c8a9a5SSteve French 					     full_path);
351138c8a9a5SSteve French 	if (rc != 0 && rc != -EREMOTE)
351238c8a9a5SSteve French 		goto out;
351338c8a9a5SSteve French 
351438c8a9a5SSteve French 	if (rc != -EREMOTE) {
351538c8a9a5SSteve French 		rc = cifs_are_all_path_components_accessible(server, xid, tcon,
351638c8a9a5SSteve French 			cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
351738c8a9a5SSteve French 		if (rc != 0) {
351838c8a9a5SSteve French 			cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
351938c8a9a5SSteve French 			cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
352038c8a9a5SSteve French 			rc = 0;
352138c8a9a5SSteve French 		}
352238c8a9a5SSteve French 	}
352338c8a9a5SSteve French 
352438c8a9a5SSteve French out:
352538c8a9a5SSteve French 	kfree(full_path);
352638c8a9a5SSteve French 	return rc;
352738c8a9a5SSteve French }
352838c8a9a5SSteve French 
352938c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL
353038c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
353138c8a9a5SSteve French {
353238c8a9a5SSteve French 	struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
353338c8a9a5SSteve French 	bool isdfs;
353438c8a9a5SSteve French 	int rc;
353538c8a9a5SSteve French 
353638c8a9a5SSteve French 	INIT_LIST_HEAD(&mnt_ctx.dfs_ses_list);
353738c8a9a5SSteve French 
353838c8a9a5SSteve French 	rc = dfs_mount_share(&mnt_ctx, &isdfs);
353938c8a9a5SSteve French 	if (rc)
354038c8a9a5SSteve French 		goto error;
354138c8a9a5SSteve French 	if (!isdfs)
354238c8a9a5SSteve French 		goto out;
354338c8a9a5SSteve French 
354438c8a9a5SSteve French 	/*
354538c8a9a5SSteve French 	 * After reconnecting to a different server, unique ids won't match anymore, so we disable
354638c8a9a5SSteve French 	 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
354738c8a9a5SSteve French 	 */
354838c8a9a5SSteve French 	cifs_autodisable_serverino(cifs_sb);
354938c8a9a5SSteve French 	/*
355038c8a9a5SSteve French 	 * Force the use of prefix path to support failover on DFS paths that resolve to targets
355138c8a9a5SSteve French 	 * that have different prefix paths.
355238c8a9a5SSteve French 	 */
355338c8a9a5SSteve French 	cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
355438c8a9a5SSteve French 	kfree(cifs_sb->prepath);
355538c8a9a5SSteve French 	cifs_sb->prepath = ctx->prepath;
355638c8a9a5SSteve French 	ctx->prepath = NULL;
355738c8a9a5SSteve French 
355838c8a9a5SSteve French out:
355938c8a9a5SSteve French 	cifs_try_adding_channels(cifs_sb, mnt_ctx.ses);
356038c8a9a5SSteve French 	rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
356138c8a9a5SSteve French 	if (rc)
356238c8a9a5SSteve French 		goto error;
356338c8a9a5SSteve French 
356438c8a9a5SSteve French 	free_xid(mnt_ctx.xid);
356538c8a9a5SSteve French 	return rc;
356638c8a9a5SSteve French 
356738c8a9a5SSteve French error:
356838c8a9a5SSteve French 	dfs_put_root_smb_sessions(&mnt_ctx.dfs_ses_list);
356938c8a9a5SSteve French 	cifs_mount_put_conns(&mnt_ctx);
357038c8a9a5SSteve French 	return rc;
357138c8a9a5SSteve French }
357238c8a9a5SSteve French #else
357338c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
357438c8a9a5SSteve French {
357538c8a9a5SSteve French 	int rc = 0;
357638c8a9a5SSteve French 	struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
357738c8a9a5SSteve French 
357838c8a9a5SSteve French 	rc = cifs_mount_get_session(&mnt_ctx);
357938c8a9a5SSteve French 	if (rc)
358038c8a9a5SSteve French 		goto error;
358138c8a9a5SSteve French 
358238c8a9a5SSteve French 	rc = cifs_mount_get_tcon(&mnt_ctx);
358338c8a9a5SSteve French 	if (rc)
358438c8a9a5SSteve French 		goto error;
358538c8a9a5SSteve French 
358638c8a9a5SSteve French 	rc = cifs_is_path_remote(&mnt_ctx);
358738c8a9a5SSteve French 	if (rc == -EREMOTE)
358838c8a9a5SSteve French 		rc = -EOPNOTSUPP;
358938c8a9a5SSteve French 	if (rc)
359038c8a9a5SSteve French 		goto error;
359138c8a9a5SSteve French 
359238c8a9a5SSteve French 	rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
359338c8a9a5SSteve French 	if (rc)
359438c8a9a5SSteve French 		goto error;
359538c8a9a5SSteve French 
359638c8a9a5SSteve French 	free_xid(mnt_ctx.xid);
359738c8a9a5SSteve French 	return rc;
359838c8a9a5SSteve French 
359938c8a9a5SSteve French error:
360038c8a9a5SSteve French 	cifs_mount_put_conns(&mnt_ctx);
360138c8a9a5SSteve French 	return rc;
360238c8a9a5SSteve French }
360338c8a9a5SSteve French #endif
360438c8a9a5SSteve French 
360538c8a9a5SSteve French /*
360638c8a9a5SSteve French  * Issue a TREE_CONNECT request.
360738c8a9a5SSteve French  */
360838c8a9a5SSteve French int
360938c8a9a5SSteve French CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
361038c8a9a5SSteve French 	 const char *tree, struct cifs_tcon *tcon,
361138c8a9a5SSteve French 	 const struct nls_table *nls_codepage)
361238c8a9a5SSteve French {
361338c8a9a5SSteve French 	struct smb_hdr *smb_buffer;
361438c8a9a5SSteve French 	struct smb_hdr *smb_buffer_response;
361538c8a9a5SSteve French 	TCONX_REQ *pSMB;
361638c8a9a5SSteve French 	TCONX_RSP *pSMBr;
361738c8a9a5SSteve French 	unsigned char *bcc_ptr;
361838c8a9a5SSteve French 	int rc = 0;
361938c8a9a5SSteve French 	int length;
362038c8a9a5SSteve French 	__u16 bytes_left, count;
362138c8a9a5SSteve French 
362238c8a9a5SSteve French 	if (ses == NULL)
362338c8a9a5SSteve French 		return -EIO;
362438c8a9a5SSteve French 
362538c8a9a5SSteve French 	smb_buffer = cifs_buf_get();
362638c8a9a5SSteve French 	if (smb_buffer == NULL)
362738c8a9a5SSteve French 		return -ENOMEM;
362838c8a9a5SSteve French 
362938c8a9a5SSteve French 	smb_buffer_response = smb_buffer;
363038c8a9a5SSteve French 
363138c8a9a5SSteve French 	header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
363238c8a9a5SSteve French 			NULL /*no tid */ , 4 /*wct */ );
363338c8a9a5SSteve French 
363438c8a9a5SSteve French 	smb_buffer->Mid = get_next_mid(ses->server);
363538c8a9a5SSteve French 	smb_buffer->Uid = ses->Suid;
363638c8a9a5SSteve French 	pSMB = (TCONX_REQ *) smb_buffer;
363738c8a9a5SSteve French 	pSMBr = (TCONX_RSP *) smb_buffer_response;
363838c8a9a5SSteve French 
363938c8a9a5SSteve French 	pSMB->AndXCommand = 0xFF;
364038c8a9a5SSteve French 	pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
364138c8a9a5SSteve French 	bcc_ptr = &pSMB->Password[0];
364238c8a9a5SSteve French 
364338c8a9a5SSteve French 	pSMB->PasswordLength = cpu_to_le16(1);	/* minimum */
364438c8a9a5SSteve French 	*bcc_ptr = 0; /* password is null byte */
364538c8a9a5SSteve French 	bcc_ptr++;              /* skip password */
364638c8a9a5SSteve French 	/* already aligned so no need to do it below */
364738c8a9a5SSteve French 
364838c8a9a5SSteve French 	if (ses->server->sign)
364938c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
365038c8a9a5SSteve French 
365138c8a9a5SSteve French 	if (ses->capabilities & CAP_STATUS32) {
365238c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
365338c8a9a5SSteve French 	}
365438c8a9a5SSteve French 	if (ses->capabilities & CAP_DFS) {
365538c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_DFS;
365638c8a9a5SSteve French 	}
365738c8a9a5SSteve French 	if (ses->capabilities & CAP_UNICODE) {
365838c8a9a5SSteve French 		smb_buffer->Flags2 |= SMBFLG2_UNICODE;
365938c8a9a5SSteve French 		length =
366038c8a9a5SSteve French 		    cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
366138c8a9a5SSteve French 			6 /* max utf8 char length in bytes */ *
366238c8a9a5SSteve French 			(/* server len*/ + 256 /* share len */), nls_codepage);
366338c8a9a5SSteve French 		bcc_ptr += 2 * length;	/* convert num 16 bit words to bytes */
366438c8a9a5SSteve French 		bcc_ptr += 2;	/* skip trailing null */
366538c8a9a5SSteve French 	} else {		/* ASCII */
366638c8a9a5SSteve French 		strcpy(bcc_ptr, tree);
366738c8a9a5SSteve French 		bcc_ptr += strlen(tree) + 1;
366838c8a9a5SSteve French 	}
366938c8a9a5SSteve French 	strcpy(bcc_ptr, "?????");
367038c8a9a5SSteve French 	bcc_ptr += strlen("?????");
367138c8a9a5SSteve French 	bcc_ptr += 1;
367238c8a9a5SSteve French 	count = bcc_ptr - &pSMB->Password[0];
367338c8a9a5SSteve French 	be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
367438c8a9a5SSteve French 	pSMB->ByteCount = cpu_to_le16(count);
367538c8a9a5SSteve French 
367638c8a9a5SSteve French 	rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
367738c8a9a5SSteve French 			 0);
367838c8a9a5SSteve French 
367938c8a9a5SSteve French 	/* above now done in SendReceive */
368038c8a9a5SSteve French 	if (rc == 0) {
368138c8a9a5SSteve French 		bool is_unicode;
368238c8a9a5SSteve French 
368338c8a9a5SSteve French 		tcon->tid = smb_buffer_response->Tid;
368438c8a9a5SSteve French 		bcc_ptr = pByteArea(smb_buffer_response);
368538c8a9a5SSteve French 		bytes_left = get_bcc(smb_buffer_response);
368638c8a9a5SSteve French 		length = strnlen(bcc_ptr, bytes_left - 2);
368738c8a9a5SSteve French 		if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
368838c8a9a5SSteve French 			is_unicode = true;
368938c8a9a5SSteve French 		else
369038c8a9a5SSteve French 			is_unicode = false;
369138c8a9a5SSteve French 
369238c8a9a5SSteve French 
369338c8a9a5SSteve French 		/* skip service field (NB: this field is always ASCII) */
369438c8a9a5SSteve French 		if (length == 3) {
369538c8a9a5SSteve French 			if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
369638c8a9a5SSteve French 			    (bcc_ptr[2] == 'C')) {
369738c8a9a5SSteve French 				cifs_dbg(FYI, "IPC connection\n");
369838c8a9a5SSteve French 				tcon->ipc = true;
369938c8a9a5SSteve French 				tcon->pipe = true;
370038c8a9a5SSteve French 			}
370138c8a9a5SSteve French 		} else if (length == 2) {
370238c8a9a5SSteve French 			if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
370338c8a9a5SSteve French 				/* the most common case */
370438c8a9a5SSteve French 				cifs_dbg(FYI, "disk share connection\n");
370538c8a9a5SSteve French 			}
370638c8a9a5SSteve French 		}
370738c8a9a5SSteve French 		bcc_ptr += length + 1;
370838c8a9a5SSteve French 		bytes_left -= (length + 1);
370938c8a9a5SSteve French 		strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
371038c8a9a5SSteve French 
371138c8a9a5SSteve French 		/* mostly informational -- no need to fail on error here */
371238c8a9a5SSteve French 		kfree(tcon->nativeFileSystem);
371338c8a9a5SSteve French 		tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
371438c8a9a5SSteve French 						      bytes_left, is_unicode,
371538c8a9a5SSteve French 						      nls_codepage);
371638c8a9a5SSteve French 
371738c8a9a5SSteve French 		cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
371838c8a9a5SSteve French 
371938c8a9a5SSteve French 		if ((smb_buffer_response->WordCount == 3) ||
372038c8a9a5SSteve French 			 (smb_buffer_response->WordCount == 7))
372138c8a9a5SSteve French 			/* field is in same location */
372238c8a9a5SSteve French 			tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
372338c8a9a5SSteve French 		else
372438c8a9a5SSteve French 			tcon->Flags = 0;
372538c8a9a5SSteve French 		cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
372638c8a9a5SSteve French 	}
372738c8a9a5SSteve French 
372838c8a9a5SSteve French 	cifs_buf_release(smb_buffer);
372938c8a9a5SSteve French 	return rc;
373038c8a9a5SSteve French }
373138c8a9a5SSteve French 
373238c8a9a5SSteve French static void delayed_free(struct rcu_head *p)
373338c8a9a5SSteve French {
373438c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
373538c8a9a5SSteve French 
373638c8a9a5SSteve French 	unload_nls(cifs_sb->local_nls);
373738c8a9a5SSteve French 	smb3_cleanup_fs_context(cifs_sb->ctx);
373838c8a9a5SSteve French 	kfree(cifs_sb);
373938c8a9a5SSteve French }
374038c8a9a5SSteve French 
374138c8a9a5SSteve French void
374238c8a9a5SSteve French cifs_umount(struct cifs_sb_info *cifs_sb)
374338c8a9a5SSteve French {
374438c8a9a5SSteve French 	struct rb_root *root = &cifs_sb->tlink_tree;
374538c8a9a5SSteve French 	struct rb_node *node;
374638c8a9a5SSteve French 	struct tcon_link *tlink;
374738c8a9a5SSteve French 
374838c8a9a5SSteve French 	cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
374938c8a9a5SSteve French 
375038c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
375138c8a9a5SSteve French 	while ((node = rb_first(root))) {
375238c8a9a5SSteve French 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
375338c8a9a5SSteve French 		cifs_get_tlink(tlink);
375438c8a9a5SSteve French 		clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
375538c8a9a5SSteve French 		rb_erase(node, root);
375638c8a9a5SSteve French 
375738c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
375838c8a9a5SSteve French 		cifs_put_tlink(tlink);
375938c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
376038c8a9a5SSteve French 	}
376138c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
376238c8a9a5SSteve French 
376338c8a9a5SSteve French 	kfree(cifs_sb->prepath);
376438c8a9a5SSteve French 	call_rcu(&cifs_sb->rcu, delayed_free);
376538c8a9a5SSteve French }
376638c8a9a5SSteve French 
376738c8a9a5SSteve French int
376838c8a9a5SSteve French cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
376938c8a9a5SSteve French 			struct TCP_Server_Info *server)
377038c8a9a5SSteve French {
377138c8a9a5SSteve French 	int rc = 0;
377238c8a9a5SSteve French 
377338c8a9a5SSteve French 	if (!server->ops->need_neg || !server->ops->negotiate)
377438c8a9a5SSteve French 		return -ENOSYS;
377538c8a9a5SSteve French 
377638c8a9a5SSteve French 	/* only send once per connect */
377738c8a9a5SSteve French 	spin_lock(&server->srv_lock);
377838c8a9a5SSteve French 	if (server->tcpStatus != CifsGood &&
377938c8a9a5SSteve French 	    server->tcpStatus != CifsNew &&
378038c8a9a5SSteve French 	    server->tcpStatus != CifsNeedNegotiate) {
378138c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
378238c8a9a5SSteve French 		return -EHOSTDOWN;
378338c8a9a5SSteve French 	}
378438c8a9a5SSteve French 
378538c8a9a5SSteve French 	if (!server->ops->need_neg(server) &&
378638c8a9a5SSteve French 	    server->tcpStatus == CifsGood) {
378738c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
378838c8a9a5SSteve French 		return 0;
378938c8a9a5SSteve French 	}
379038c8a9a5SSteve French 
379138c8a9a5SSteve French 	server->tcpStatus = CifsInNegotiate;
379238c8a9a5SSteve French 	spin_unlock(&server->srv_lock);
379338c8a9a5SSteve French 
379438c8a9a5SSteve French 	rc = server->ops->negotiate(xid, ses, server);
379538c8a9a5SSteve French 	if (rc == 0) {
379638c8a9a5SSteve French 		spin_lock(&server->srv_lock);
379738c8a9a5SSteve French 		if (server->tcpStatus == CifsInNegotiate)
379838c8a9a5SSteve French 			server->tcpStatus = CifsGood;
379938c8a9a5SSteve French 		else
380038c8a9a5SSteve French 			rc = -EHOSTDOWN;
380138c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
380238c8a9a5SSteve French 	} else {
380338c8a9a5SSteve French 		spin_lock(&server->srv_lock);
380438c8a9a5SSteve French 		if (server->tcpStatus == CifsInNegotiate)
380538c8a9a5SSteve French 			server->tcpStatus = CifsNeedNegotiate;
380638c8a9a5SSteve French 		spin_unlock(&server->srv_lock);
380738c8a9a5SSteve French 	}
380838c8a9a5SSteve French 
380938c8a9a5SSteve French 	return rc;
381038c8a9a5SSteve French }
381138c8a9a5SSteve French 
381238c8a9a5SSteve French int
381338c8a9a5SSteve French cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
381438c8a9a5SSteve French 		   struct TCP_Server_Info *server,
381538c8a9a5SSteve French 		   struct nls_table *nls_info)
381638c8a9a5SSteve French {
381738c8a9a5SSteve French 	int rc = -ENOSYS;
3818b3773b19SSteve French 	struct TCP_Server_Info *pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
381938c8a9a5SSteve French 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr;
382038c8a9a5SSteve French 	struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr;
382138c8a9a5SSteve French 	bool is_binding = false;
382238c8a9a5SSteve French 
382338c8a9a5SSteve French 	spin_lock(&ses->ses_lock);
382438c8a9a5SSteve French 	cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
382538c8a9a5SSteve French 		 __func__, ses->chans_need_reconnect);
382638c8a9a5SSteve French 
382738c8a9a5SSteve French 	if (ses->ses_status != SES_GOOD &&
382838c8a9a5SSteve French 	    ses->ses_status != SES_NEW &&
382938c8a9a5SSteve French 	    ses->ses_status != SES_NEED_RECON) {
383038c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
383138c8a9a5SSteve French 		return -EHOSTDOWN;
383238c8a9a5SSteve French 	}
383338c8a9a5SSteve French 
383438c8a9a5SSteve French 	/* only send once per connect */
383538c8a9a5SSteve French 	spin_lock(&ses->chan_lock);
383638c8a9a5SSteve French 	if (CIFS_ALL_CHANS_GOOD(ses)) {
383738c8a9a5SSteve French 		if (ses->ses_status == SES_NEED_RECON)
383838c8a9a5SSteve French 			ses->ses_status = SES_GOOD;
383938c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
384038c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
384138c8a9a5SSteve French 		return 0;
384238c8a9a5SSteve French 	}
384338c8a9a5SSteve French 
384438c8a9a5SSteve French 	cifs_chan_set_in_reconnect(ses, server);
384538c8a9a5SSteve French 	is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
384638c8a9a5SSteve French 	spin_unlock(&ses->chan_lock);
384738c8a9a5SSteve French 
384838c8a9a5SSteve French 	if (!is_binding)
384938c8a9a5SSteve French 		ses->ses_status = SES_IN_SETUP;
385038c8a9a5SSteve French 	spin_unlock(&ses->ses_lock);
385138c8a9a5SSteve French 
385238c8a9a5SSteve French 	/* update ses ip_addr only for primary chan */
385338c8a9a5SSteve French 	if (server == pserver) {
385438c8a9a5SSteve French 		if (server->dstaddr.ss_family == AF_INET6)
385538c8a9a5SSteve French 			scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr);
385638c8a9a5SSteve French 		else
385738c8a9a5SSteve French 			scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr);
385838c8a9a5SSteve French 	}
385938c8a9a5SSteve French 
386038c8a9a5SSteve French 	if (!is_binding) {
386138c8a9a5SSteve French 		ses->capabilities = server->capabilities;
386238c8a9a5SSteve French 		if (!linuxExtEnabled)
386338c8a9a5SSteve French 			ses->capabilities &= (~server->vals->cap_unix);
386438c8a9a5SSteve French 
386538c8a9a5SSteve French 		if (ses->auth_key.response) {
386638c8a9a5SSteve French 			cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
386738c8a9a5SSteve French 				 ses->auth_key.response);
386838c8a9a5SSteve French 			kfree_sensitive(ses->auth_key.response);
386938c8a9a5SSteve French 			ses->auth_key.response = NULL;
387038c8a9a5SSteve French 			ses->auth_key.len = 0;
387138c8a9a5SSteve French 		}
387238c8a9a5SSteve French 	}
387338c8a9a5SSteve French 
387438c8a9a5SSteve French 	cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
387538c8a9a5SSteve French 		 server->sec_mode, server->capabilities, server->timeAdj);
387638c8a9a5SSteve French 
387738c8a9a5SSteve French 	if (server->ops->sess_setup)
387838c8a9a5SSteve French 		rc = server->ops->sess_setup(xid, ses, server, nls_info);
387938c8a9a5SSteve French 
388038c8a9a5SSteve French 	if (rc) {
388138c8a9a5SSteve French 		cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
388238c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
388338c8a9a5SSteve French 		if (ses->ses_status == SES_IN_SETUP)
388438c8a9a5SSteve French 			ses->ses_status = SES_NEED_RECON;
388538c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
388638c8a9a5SSteve French 		cifs_chan_clear_in_reconnect(ses, server);
388738c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
388838c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
388938c8a9a5SSteve French 	} else {
389038c8a9a5SSteve French 		spin_lock(&ses->ses_lock);
389138c8a9a5SSteve French 		if (ses->ses_status == SES_IN_SETUP)
389238c8a9a5SSteve French 			ses->ses_status = SES_GOOD;
389338c8a9a5SSteve French 		spin_lock(&ses->chan_lock);
389438c8a9a5SSteve French 		cifs_chan_clear_in_reconnect(ses, server);
389538c8a9a5SSteve French 		cifs_chan_clear_need_reconnect(ses, server);
389638c8a9a5SSteve French 		spin_unlock(&ses->chan_lock);
389738c8a9a5SSteve French 		spin_unlock(&ses->ses_lock);
389838c8a9a5SSteve French 	}
389938c8a9a5SSteve French 
390038c8a9a5SSteve French 	return rc;
390138c8a9a5SSteve French }
390238c8a9a5SSteve French 
390338c8a9a5SSteve French static int
390438c8a9a5SSteve French cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
390538c8a9a5SSteve French {
390638c8a9a5SSteve French 	ctx->sectype = ses->sectype;
390738c8a9a5SSteve French 
390838c8a9a5SSteve French 	/* krb5 is special, since we don't need username or pw */
390938c8a9a5SSteve French 	if (ctx->sectype == Kerberos)
391038c8a9a5SSteve French 		return 0;
391138c8a9a5SSteve French 
391238c8a9a5SSteve French 	return cifs_set_cifscreds(ctx, ses);
391338c8a9a5SSteve French }
391438c8a9a5SSteve French 
391538c8a9a5SSteve French static struct cifs_tcon *
391638c8a9a5SSteve French cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
391738c8a9a5SSteve French {
391838c8a9a5SSteve French 	int rc;
391938c8a9a5SSteve French 	struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
392038c8a9a5SSteve French 	struct cifs_ses *ses;
392138c8a9a5SSteve French 	struct cifs_tcon *tcon = NULL;
392238c8a9a5SSteve French 	struct smb3_fs_context *ctx;
392338c8a9a5SSteve French 
392438c8a9a5SSteve French 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
392538c8a9a5SSteve French 	if (ctx == NULL)
392638c8a9a5SSteve French 		return ERR_PTR(-ENOMEM);
392738c8a9a5SSteve French 
392838c8a9a5SSteve French 	ctx->local_nls = cifs_sb->local_nls;
392938c8a9a5SSteve French 	ctx->linux_uid = fsuid;
393038c8a9a5SSteve French 	ctx->cred_uid = fsuid;
393138c8a9a5SSteve French 	ctx->UNC = master_tcon->tree_name;
393238c8a9a5SSteve French 	ctx->retry = master_tcon->retry;
393338c8a9a5SSteve French 	ctx->nocase = master_tcon->nocase;
393438c8a9a5SSteve French 	ctx->nohandlecache = master_tcon->nohandlecache;
393538c8a9a5SSteve French 	ctx->local_lease = master_tcon->local_lease;
393638c8a9a5SSteve French 	ctx->no_lease = master_tcon->no_lease;
393738c8a9a5SSteve French 	ctx->resilient = master_tcon->use_resilient;
393838c8a9a5SSteve French 	ctx->persistent = master_tcon->use_persistent;
393938c8a9a5SSteve French 	ctx->handle_timeout = master_tcon->handle_timeout;
394038c8a9a5SSteve French 	ctx->no_linux_ext = !master_tcon->unix_ext;
394138c8a9a5SSteve French 	ctx->linux_ext = master_tcon->posix_extensions;
394238c8a9a5SSteve French 	ctx->sectype = master_tcon->ses->sectype;
394338c8a9a5SSteve French 	ctx->sign = master_tcon->ses->sign;
394438c8a9a5SSteve French 	ctx->seal = master_tcon->seal;
394538c8a9a5SSteve French 	ctx->witness = master_tcon->use_witness;
394638c8a9a5SSteve French 
394738c8a9a5SSteve French 	rc = cifs_set_vol_auth(ctx, master_tcon->ses);
394838c8a9a5SSteve French 	if (rc) {
394938c8a9a5SSteve French 		tcon = ERR_PTR(rc);
395038c8a9a5SSteve French 		goto out;
395138c8a9a5SSteve French 	}
395238c8a9a5SSteve French 
395338c8a9a5SSteve French 	/* get a reference for the same TCP session */
395438c8a9a5SSteve French 	spin_lock(&cifs_tcp_ses_lock);
395538c8a9a5SSteve French 	++master_tcon->ses->server->srv_count;
395638c8a9a5SSteve French 	spin_unlock(&cifs_tcp_ses_lock);
395738c8a9a5SSteve French 
395838c8a9a5SSteve French 	ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
395938c8a9a5SSteve French 	if (IS_ERR(ses)) {
396038c8a9a5SSteve French 		tcon = (struct cifs_tcon *)ses;
396138c8a9a5SSteve French 		cifs_put_tcp_session(master_tcon->ses->server, 0);
396238c8a9a5SSteve French 		goto out;
396338c8a9a5SSteve French 	}
396438c8a9a5SSteve French 
396538c8a9a5SSteve French 	tcon = cifs_get_tcon(ses, ctx);
396638c8a9a5SSteve French 	if (IS_ERR(tcon)) {
396738c8a9a5SSteve French 		cifs_put_smb_ses(ses);
396838c8a9a5SSteve French 		goto out;
396938c8a9a5SSteve French 	}
397038c8a9a5SSteve French 
397138c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
397238c8a9a5SSteve French 	if (cap_unix(ses))
397338c8a9a5SSteve French 		reset_cifs_unix_caps(0, tcon, NULL, ctx);
397438c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
397538c8a9a5SSteve French 
397638c8a9a5SSteve French out:
397738c8a9a5SSteve French 	kfree(ctx->username);
397838c8a9a5SSteve French 	kfree_sensitive(ctx->password);
397938c8a9a5SSteve French 	kfree(ctx);
398038c8a9a5SSteve French 
398138c8a9a5SSteve French 	return tcon;
398238c8a9a5SSteve French }
398338c8a9a5SSteve French 
398438c8a9a5SSteve French struct cifs_tcon *
398538c8a9a5SSteve French cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
398638c8a9a5SSteve French {
398738c8a9a5SSteve French 	return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
398838c8a9a5SSteve French }
398938c8a9a5SSteve French 
399038c8a9a5SSteve French /* find and return a tlink with given uid */
399138c8a9a5SSteve French static struct tcon_link *
399238c8a9a5SSteve French tlink_rb_search(struct rb_root *root, kuid_t uid)
399338c8a9a5SSteve French {
399438c8a9a5SSteve French 	struct rb_node *node = root->rb_node;
399538c8a9a5SSteve French 	struct tcon_link *tlink;
399638c8a9a5SSteve French 
399738c8a9a5SSteve French 	while (node) {
399838c8a9a5SSteve French 		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
399938c8a9a5SSteve French 
400038c8a9a5SSteve French 		if (uid_gt(tlink->tl_uid, uid))
400138c8a9a5SSteve French 			node = node->rb_left;
400238c8a9a5SSteve French 		else if (uid_lt(tlink->tl_uid, uid))
400338c8a9a5SSteve French 			node = node->rb_right;
400438c8a9a5SSteve French 		else
400538c8a9a5SSteve French 			return tlink;
400638c8a9a5SSteve French 	}
400738c8a9a5SSteve French 	return NULL;
400838c8a9a5SSteve French }
400938c8a9a5SSteve French 
401038c8a9a5SSteve French /* insert a tcon_link into the tree */
401138c8a9a5SSteve French static void
401238c8a9a5SSteve French tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
401338c8a9a5SSteve French {
401438c8a9a5SSteve French 	struct rb_node **new = &(root->rb_node), *parent = NULL;
401538c8a9a5SSteve French 	struct tcon_link *tlink;
401638c8a9a5SSteve French 
401738c8a9a5SSteve French 	while (*new) {
401838c8a9a5SSteve French 		tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
401938c8a9a5SSteve French 		parent = *new;
402038c8a9a5SSteve French 
402138c8a9a5SSteve French 		if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
402238c8a9a5SSteve French 			new = &((*new)->rb_left);
402338c8a9a5SSteve French 		else
402438c8a9a5SSteve French 			new = &((*new)->rb_right);
402538c8a9a5SSteve French 	}
402638c8a9a5SSteve French 
402738c8a9a5SSteve French 	rb_link_node(&new_tlink->tl_rbnode, parent, new);
402838c8a9a5SSteve French 	rb_insert_color(&new_tlink->tl_rbnode, root);
402938c8a9a5SSteve French }
403038c8a9a5SSteve French 
403138c8a9a5SSteve French /*
403238c8a9a5SSteve French  * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
403338c8a9a5SSteve French  * current task.
403438c8a9a5SSteve French  *
403538c8a9a5SSteve French  * If the superblock doesn't refer to a multiuser mount, then just return
403638c8a9a5SSteve French  * the master tcon for the mount.
403738c8a9a5SSteve French  *
403838c8a9a5SSteve French  * First, search the rbtree for an existing tcon for this fsuid. If one
403938c8a9a5SSteve French  * exists, then check to see if it's pending construction. If it is then wait
404038c8a9a5SSteve French  * for construction to complete. Once it's no longer pending, check to see if
404138c8a9a5SSteve French  * it failed and either return an error or retry construction, depending on
404238c8a9a5SSteve French  * the timeout.
404338c8a9a5SSteve French  *
404438c8a9a5SSteve French  * If one doesn't exist then insert a new tcon_link struct into the tree and
404538c8a9a5SSteve French  * try to construct a new one.
404638c8a9a5SSteve French  */
404738c8a9a5SSteve French struct tcon_link *
404838c8a9a5SSteve French cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
404938c8a9a5SSteve French {
405038c8a9a5SSteve French 	int ret;
405138c8a9a5SSteve French 	kuid_t fsuid = current_fsuid();
405238c8a9a5SSteve French 	struct tcon_link *tlink, *newtlink;
405338c8a9a5SSteve French 
405438c8a9a5SSteve French 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
405538c8a9a5SSteve French 		return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
405638c8a9a5SSteve French 
405738c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
405838c8a9a5SSteve French 	tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
405938c8a9a5SSteve French 	if (tlink)
406038c8a9a5SSteve French 		cifs_get_tlink(tlink);
406138c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
406238c8a9a5SSteve French 
406338c8a9a5SSteve French 	if (tlink == NULL) {
406438c8a9a5SSteve French 		newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
406538c8a9a5SSteve French 		if (newtlink == NULL)
406638c8a9a5SSteve French 			return ERR_PTR(-ENOMEM);
406738c8a9a5SSteve French 		newtlink->tl_uid = fsuid;
406838c8a9a5SSteve French 		newtlink->tl_tcon = ERR_PTR(-EACCES);
406938c8a9a5SSteve French 		set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
407038c8a9a5SSteve French 		set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
407138c8a9a5SSteve French 		cifs_get_tlink(newtlink);
407238c8a9a5SSteve French 
407338c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
407438c8a9a5SSteve French 		/* was one inserted after previous search? */
407538c8a9a5SSteve French 		tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
407638c8a9a5SSteve French 		if (tlink) {
407738c8a9a5SSteve French 			cifs_get_tlink(tlink);
407838c8a9a5SSteve French 			spin_unlock(&cifs_sb->tlink_tree_lock);
407938c8a9a5SSteve French 			kfree(newtlink);
408038c8a9a5SSteve French 			goto wait_for_construction;
408138c8a9a5SSteve French 		}
408238c8a9a5SSteve French 		tlink = newtlink;
408338c8a9a5SSteve French 		tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
408438c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
408538c8a9a5SSteve French 	} else {
408638c8a9a5SSteve French wait_for_construction:
408738c8a9a5SSteve French 		ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
408838c8a9a5SSteve French 				  TASK_INTERRUPTIBLE);
408938c8a9a5SSteve French 		if (ret) {
409038c8a9a5SSteve French 			cifs_put_tlink(tlink);
409138c8a9a5SSteve French 			return ERR_PTR(-ERESTARTSYS);
409238c8a9a5SSteve French 		}
409338c8a9a5SSteve French 
409438c8a9a5SSteve French 		/* if it's good, return it */
409538c8a9a5SSteve French 		if (!IS_ERR(tlink->tl_tcon))
409638c8a9a5SSteve French 			return tlink;
409738c8a9a5SSteve French 
409838c8a9a5SSteve French 		/* return error if we tried this already recently */
409938c8a9a5SSteve French 		if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
410038c8a9a5SSteve French 			cifs_put_tlink(tlink);
410138c8a9a5SSteve French 			return ERR_PTR(-EACCES);
410238c8a9a5SSteve French 		}
410338c8a9a5SSteve French 
410438c8a9a5SSteve French 		if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
410538c8a9a5SSteve French 			goto wait_for_construction;
410638c8a9a5SSteve French 	}
410738c8a9a5SSteve French 
410838c8a9a5SSteve French 	tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
410938c8a9a5SSteve French 	clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
411038c8a9a5SSteve French 	wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
411138c8a9a5SSteve French 
411238c8a9a5SSteve French 	if (IS_ERR(tlink->tl_tcon)) {
411338c8a9a5SSteve French 		cifs_put_tlink(tlink);
411438c8a9a5SSteve French 		return ERR_PTR(-EACCES);
411538c8a9a5SSteve French 	}
411638c8a9a5SSteve French 
411738c8a9a5SSteve French 	return tlink;
411838c8a9a5SSteve French }
411938c8a9a5SSteve French 
412038c8a9a5SSteve French /*
412138c8a9a5SSteve French  * periodic workqueue job that scans tcon_tree for a superblock and closes
412238c8a9a5SSteve French  * out tcons.
412338c8a9a5SSteve French  */
412438c8a9a5SSteve French static void
412538c8a9a5SSteve French cifs_prune_tlinks(struct work_struct *work)
412638c8a9a5SSteve French {
412738c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
412838c8a9a5SSteve French 						    prune_tlinks.work);
412938c8a9a5SSteve French 	struct rb_root *root = &cifs_sb->tlink_tree;
413038c8a9a5SSteve French 	struct rb_node *node;
413138c8a9a5SSteve French 	struct rb_node *tmp;
413238c8a9a5SSteve French 	struct tcon_link *tlink;
413338c8a9a5SSteve French 
413438c8a9a5SSteve French 	/*
413538c8a9a5SSteve French 	 * Because we drop the spinlock in the loop in order to put the tlink
413638c8a9a5SSteve French 	 * it's not guarded against removal of links from the tree. The only
413738c8a9a5SSteve French 	 * places that remove entries from the tree are this function and
413838c8a9a5SSteve French 	 * umounts. Because this function is non-reentrant and is canceled
413938c8a9a5SSteve French 	 * before umount can proceed, this is safe.
414038c8a9a5SSteve French 	 */
414138c8a9a5SSteve French 	spin_lock(&cifs_sb->tlink_tree_lock);
414238c8a9a5SSteve French 	node = rb_first(root);
414338c8a9a5SSteve French 	while (node != NULL) {
414438c8a9a5SSteve French 		tmp = node;
414538c8a9a5SSteve French 		node = rb_next(tmp);
414638c8a9a5SSteve French 		tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
414738c8a9a5SSteve French 
414838c8a9a5SSteve French 		if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
414938c8a9a5SSteve French 		    atomic_read(&tlink->tl_count) != 0 ||
415038c8a9a5SSteve French 		    time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
415138c8a9a5SSteve French 			continue;
415238c8a9a5SSteve French 
415338c8a9a5SSteve French 		cifs_get_tlink(tlink);
415438c8a9a5SSteve French 		clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
415538c8a9a5SSteve French 		rb_erase(tmp, root);
415638c8a9a5SSteve French 
415738c8a9a5SSteve French 		spin_unlock(&cifs_sb->tlink_tree_lock);
415838c8a9a5SSteve French 		cifs_put_tlink(tlink);
415938c8a9a5SSteve French 		spin_lock(&cifs_sb->tlink_tree_lock);
416038c8a9a5SSteve French 	}
416138c8a9a5SSteve French 	spin_unlock(&cifs_sb->tlink_tree_lock);
416238c8a9a5SSteve French 
416338c8a9a5SSteve French 	queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
416438c8a9a5SSteve French 				TLINK_IDLE_EXPIRE);
416538c8a9a5SSteve French }
416638c8a9a5SSteve French 
416738c8a9a5SSteve French #ifndef CONFIG_CIFS_DFS_UPCALL
416838c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
416938c8a9a5SSteve French {
417038c8a9a5SSteve French 	int rc;
417138c8a9a5SSteve French 	const struct smb_version_operations *ops = tcon->ses->server->ops;
417238c8a9a5SSteve French 
417338c8a9a5SSteve French 	/* only send once per connect */
417438c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
417591f4480cSShyam Prasad N 	if (tcon->status == TID_GOOD) {
417691f4480cSShyam Prasad N 		spin_unlock(&tcon->tc_lock);
417791f4480cSShyam Prasad N 		return 0;
417891f4480cSShyam Prasad N 	}
417991f4480cSShyam Prasad N 
418038c8a9a5SSteve French 	if (tcon->status != TID_NEW &&
418138c8a9a5SSteve French 	    tcon->status != TID_NEED_TCON) {
418238c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
418338c8a9a5SSteve French 		return -EHOSTDOWN;
418438c8a9a5SSteve French 	}
418538c8a9a5SSteve French 
418638c8a9a5SSteve French 	tcon->status = TID_IN_TCON;
418738c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
418838c8a9a5SSteve French 
418938c8a9a5SSteve French 	rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, nlsc);
419038c8a9a5SSteve French 	if (rc) {
419138c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
419238c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
419338c8a9a5SSteve French 			tcon->status = TID_NEED_TCON;
419438c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
419538c8a9a5SSteve French 	} else {
419638c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
419738c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
419838c8a9a5SSteve French 			tcon->status = TID_GOOD;
419938c8a9a5SSteve French 		tcon->need_reconnect = false;
420038c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
420138c8a9a5SSteve French 	}
420238c8a9a5SSteve French 
420338c8a9a5SSteve French 	return rc;
420438c8a9a5SSteve French }
420538c8a9a5SSteve French #endif
4206