blob: 11a22a30ee141b74c265b5edaaf3410beb6db6bf [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Steve French1080ef72011-02-24 18:07:19 +00004 * Copyright (C) International Business Machines Corp., 2002,2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8#include <linux/fs.h>
9#include <linux/net.h>
10#include <linux/string.h>
Eric Biggersdc920272020-03-08 22:58:20 -070011#include <linux/sched/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010012#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/list.h>
14#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/pagemap.h>
17#include <linux/ctype.h>
18#include <linux/utsname.h>
19#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070020#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070021#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000022#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070023#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040025#include <linux/namei.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020026#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040029#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040030#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050031#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000032#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040033#include <linux/parser.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060034#include <linux/bvec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "cifspdu.h"
36#include "cifsglob.h"
37#include "cifsproto.h"
38#include "cifs_unicode.h"
39#include "cifs_debug.h"
40#include "cifs_fs_sb.h"
41#include "ntlmssp.h"
42#include "nterr.h"
43#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053044#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070045#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070046#include "smbdirect.h"
Paulo Alcantara1c780222018-11-14 16:24:03 -020047#include "dns_resolve.h"
48#ifdef CONFIG_CIFS_DFS_UPCALL
49#include "dfs_cache.h"
50#endif
Ronnie Sahlberg5c6e5aa2020-10-21 10:37:11 +100051#include "fs_context.h"
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +010052#include "cifs_swn.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050055extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jeff Layton2de970f2010-10-06 19:51:12 -040057/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040058#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040059#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040060
Rohith Surabattula8e670f72020-09-18 05:37:28 +000061/* Drop the connection to not overload the server */
62#define NUM_STATUS_IO_TIMEOUT 5
63
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -030064struct mount_ctx {
65 struct cifs_sb_info *cifs_sb;
66 struct smb3_fs_context *fs_ctx;
67 unsigned int xid;
68 struct TCP_Server_Info *server;
69 struct cifs_ses *ses;
70 struct cifs_tcon *tcon;
71#ifdef CONFIG_CIFS_DFS_UPCALL
72 struct cifs_ses *root_ses;
73 uuid_t mount_id;
74 char *origin_fullpath, *leaf_fullpath;
75#endif
76};
77
Pavel Shilovskya9f1b852010-12-13 19:08:35 +030078static int ip_connect(struct TCP_Server_Info *server);
79static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -040080static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -040081static void cifs_prune_tlinks(struct work_struct *work);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -020082
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020083/*
84 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
85 * get their ip addresses changed at some point.
86 *
87 * This should be called with server->srv_mutex held.
88 */
Samuel Cabrero7d6535b2020-11-30 19:02:55 +010089static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020090{
91 int rc;
92 int len;
93 char *unc, *ipaddr = NULL;
Shyam Prasad N506c1da2021-05-18 15:05:50 +000094 time64_t expiry, now;
95 unsigned long ttl = SMB_DNS_RESOLVE_INTERVAL_DEFAULT;
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020096
97 if (!server->hostname)
98 return -EINVAL;
99
100 len = strlen(server->hostname) + 3;
101
102 unc = kmalloc(len, GFP_KERNEL);
103 if (!unc) {
104 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
105 return -ENOMEM;
106 }
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000107 scnprintf(unc, len, "\\\\%s", server->hostname);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200108
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000109 rc = dns_resolve_server_name_to_ip(unc, &ipaddr, &expiry);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200110 kfree(unc);
111
112 if (rc < 0) {
113 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
114 __func__, server->hostname, rc);
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000115 goto requeue_resolve;
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200116 }
117
Ronnie Sahlbergfada37f2020-04-21 12:37:39 +1000118 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200119 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
120 strlen(ipaddr));
Ronnie Sahlbergfada37f2020-04-21 12:37:39 +1000121 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200122 kfree(ipaddr);
123
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000124 /* rc == 1 means success here */
125 if (rc) {
126 now = ktime_get_real_seconds();
127 if (expiry && expiry > now)
128 /*
129 * To make sure we don't use the cached entry, retry 1s
130 * after expiry.
131 */
Paulo Alcantara4ac05362021-11-04 13:13:28 -0300132 ttl = max_t(unsigned long, expiry - now, SMB_DNS_RESOLVE_INTERVAL_MIN) + 1;
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000133 }
134 rc = !rc ? -1 : 0;
135
136requeue_resolve:
137 cifs_dbg(FYI, "%s: next dns resolution scheduled for %lu seconds in the future\n",
138 __func__, ttl);
139 mod_delayed_work(cifsiod_wq, &server->resolve, (ttl * HZ));
140
141 return rc;
142}
143
144
145static void cifs_resolve_server(struct work_struct *work)
146{
147 int rc;
148 struct TCP_Server_Info *server = container_of(work,
149 struct TCP_Server_Info, resolve.work);
150
151 mutex_lock(&server->srv_mutex);
152
153 /*
154 * Resolve the hostname again to make sure that IP address is up-to-date.
155 */
156 rc = reconn_set_ipaddr_from_hostname(server);
157 if (rc) {
158 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
159 __func__, rc);
160 }
161
162 mutex_unlock(&server->srv_mutex);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200163}
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200164
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300165/**
166 * Mark all sessions and tcons for reconnect.
Jeff Laytond5c56052008-12-01 18:42:33 -0500167 *
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300168 * @server needs to be previously set to CifsNeedReconnect.
Shyam Prasad N183eea22021-07-19 14:14:46 +0000169 *
Jeff Laytond5c56052008-12-01 18:42:33 -0500170 */
Shyam Prasad Nba978e82022-01-17 07:15:02 +0000171void
Shyam Prasad N183eea22021-07-19 14:14:46 +0000172cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
173 bool mark_smb_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Shyam Prasad Nbda487a2021-10-25 05:44:10 +0000175 struct TCP_Server_Info *pserver;
Steve French96daf2b2011-05-27 04:34:02 +0000176 struct cifs_ses *ses;
177 struct cifs_tcon *tcon;
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300178 struct mid_q_entry *mid, *nmid;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400179 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400182 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300184 /*
185 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
186 * are not used until reconnected.
187 */
Shyam Prasad Na05885c2021-11-17 15:57:22 +0000188 cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__);
Shyam Prasad N0f2b3052021-07-19 11:26:24 +0000189
190 /* If server is a channel, select the primary channel */
191 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
192
Shyam Prasad Na05885c2021-11-17 15:57:22 +0000193
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530194 spin_lock(&cifs_tcp_ses_lock);
Shyam Prasad N0f2b3052021-07-19 11:26:24 +0000195 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +0000196 spin_lock(&ses->chan_lock);
Shyam Prasad N183eea22021-07-19 14:14:46 +0000197 if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server))
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +0000198 goto next_session;
199
Shyam Prasad Nba978e82022-01-17 07:15:02 +0000200 if (mark_smb_session)
201 CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses);
202 else
203 cifs_chan_set_need_reconnect(ses, server);
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +0000204
205 /* If all channels need reconnect, then tcon needs reconnect */
Shyam Prasad N183eea22021-07-19 14:14:46 +0000206 if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses))
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +0000207 goto next_session;
208
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +0000209 ses->status = CifsNeedReconnect;
Shyam Prasad Nf486ef82021-07-19 13:54:16 +0000210
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +0000211 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
Jeff Laytonf1987b42008-11-15 11:12:47 -0500212 tcon->need_reconnect = true;
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +0000213 tcon->tidStatus = CifsNeedReconnect;
214 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100215 if (ses->tcon_ipc)
216 ses->tcon_ipc->need_reconnect = true;
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +0000217
218next_session:
219 spin_unlock(&ses->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530221 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* do not want to be sending data on a socket we are freeing */
Shyam Prasad Na05885c2021-11-17 15:57:22 +0000224 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500225 mutex_lock(&server->srv_mutex);
Long Li1d2a4f52019-05-13 21:01:28 -0700226 if (server->ssocket) {
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300227 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
228 server->ssocket->flags);
Long Li1d2a4f52019-05-13 21:01:28 -0700229 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300230 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
231 server->ssocket->flags);
Long Li1d2a4f52019-05-13 21:01:28 -0700232 sock_release(server->ssocket);
233 server->ssocket = NULL;
234 }
235 server->sequence_number = 0;
236 server->session_estab = false;
237 kfree(server->session_key.response);
238 server->session_key.response = NULL;
239 server->session_key.len = 0;
240 server->lstrp = jiffies;
Long Li214bab42019-04-05 21:36:35 +0000241
242 /* mark submitted MIDs for retry and issue callback */
243 INIT_LIST_HEAD(&retry_list);
244 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
245 spin_lock(&GlobalMid_Lock);
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300246 list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
247 kref_get(&mid->refcount);
248 if (mid->mid_state == MID_REQUEST_SUBMITTED)
249 mid->mid_state = MID_RETRY_NEEDED;
250 list_move(&mid->qhead, &retry_list);
251 mid->mid_flags |= MID_DELETED;
Long Li214bab42019-04-05 21:36:35 +0000252 }
253 spin_unlock(&GlobalMid_Lock);
Long Li1d2a4f52019-05-13 21:01:28 -0700254 mutex_unlock(&server->srv_mutex);
Long Li214bab42019-04-05 21:36:35 +0000255
256 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300257 list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
258 list_del_init(&mid->qhead);
259 mid->callback(mid);
260 cifs_mid_q_entry_release(mid);
Long Li214bab42019-04-05 21:36:35 +0000261 }
262
Long Li1d2a4f52019-05-13 21:01:28 -0700263 if (cifs_rdma_enabled(server)) {
264 mutex_lock(&server->srv_mutex);
Long Li050b8c32019-04-04 11:35:42 -0500265 smbd_destroy(server);
Long Li1d2a4f52019-05-13 21:01:28 -0700266 mutex_unlock(&server->srv_mutex);
267 }
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300268}
269
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300270static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
271{
Shyam Prasad N183eea22021-07-19 14:14:46 +0000272 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300273 server->nr_targets = num_targets;
274 if (server->tcpStatus == CifsExiting) {
275 /* the demux thread will exit normally next time through the loop */
Shyam Prasad N183eea22021-07-19 14:14:46 +0000276 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300277 wake_up(&server->response_q);
278 return false;
279 }
Shyam Prasad Na05885c2021-11-17 15:57:22 +0000280
281 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
282 trace_smb3_reconnect(server->CurrentMid, server->conn_id,
283 server->hostname);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300284 server->tcpStatus = CifsNeedReconnect;
Shyam Prasad Na05885c2021-11-17 15:57:22 +0000285
Shyam Prasad N183eea22021-07-19 14:14:46 +0000286 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300287 return true;
288}
289
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300290/*
291 * cifs tcp session reconnection
292 *
293 * mark tcp session as reconnecting so temporarily locked
294 * mark all smb sessions as reconnecting for tcp session
295 * reconnect tcp session
296 * wake up waiters on reconnection? - (not needed currently)
Shyam Prasad N183eea22021-07-19 14:14:46 +0000297 *
298 * if mark_smb_session is passed as true, unconditionally mark
299 * the smb session (and tcon) for reconnect as well. This value
300 * doesn't really matter for non-multichannel scenario.
301 *
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300302 */
Shyam Prasad N183eea22021-07-19 14:14:46 +0000303static int __cifs_reconnect(struct TCP_Server_Info *server,
304 bool mark_smb_session)
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300305{
306 int rc = 0;
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300307
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300308 if (!cifs_tcp_ses_needs_reconnect(server, 1))
309 return 0;
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300310
Shyam Prasad N183eea22021-07-19 14:14:46 +0000311 cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400312
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400313 do {
Steve French6c3d8902006-07-31 22:46:20 +0000314 try_to_freeze();
Jeff Layton73e216a2013-09-05 08:38:10 -0400315 mutex_lock(&server->srv_mutex);
Samuel Cabrero121d9472020-11-30 19:02:56 +0100316
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +0200317 if (!cifs_swn_set_server_dstaddr(server)) {
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300318 /* resolve the hostname again to make sure that IP address is up-to-date */
Shyam Prasad N4e456b32021-03-31 14:35:24 +0000319 rc = reconn_set_ipaddr_from_hostname(server);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300320 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
Samuel Cabrero121d9472020-11-30 19:02:56 +0100321 }
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300322
Long Li781a8052017-11-22 17:38:36 -0700323 if (cifs_rdma_enabled(server))
324 rc = smbd_reconnect(server);
325 else
326 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000327 if (rc) {
Federico Sauter4afe2602015-03-17 17:45:28 +0100328 mutex_unlock(&server->srv_mutex);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300329 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
Steve French0cb766a2005-04-28 22:41:11 -0700330 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 } else {
332 atomic_inc(&tcpSesReconnectCount);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800333 set_credits(server, 1);
Shyam Prasad N183eea22021-07-19 14:14:46 +0000334 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000335 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000336 server->tcpStatus = CifsNeedNegotiate;
Shyam Prasad N183eea22021-07-19 14:14:46 +0000337 spin_unlock(&cifs_tcp_ses_lock);
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +0200338 cifs_swn_reset_server_dstaddr(server);
Federico Sauter4afe2602015-03-17 17:45:28 +0100339 mutex_unlock(&server->srv_mutex);
Shyam Prasad N3663c902021-10-30 04:51:35 +0000340 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400342 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500343
Shyam Prasad Ne154cb72022-01-16 11:19:36 +0000344 spin_lock(&cifs_tcp_ses_lock);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400345 if (server->tcpStatus == CifsNeedNegotiate)
346 mod_delayed_work(cifsiod_wq, &server->echo, 0);
Shyam Prasad Ne154cb72022-01-16 11:19:36 +0000347 spin_unlock(&cifs_tcp_ses_lock);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400348
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600349 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return rc;
351}
352
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300353#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300354static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
355{
356 int rc;
357 char *hostname;
358
359 if (!cifs_swn_set_server_dstaddr(server)) {
360 if (server->hostname != target) {
361 hostname = extract_hostname(target);
362 if (!IS_ERR(hostname)) {
363 kfree(server->hostname);
364 server->hostname = hostname;
365 } else {
366 cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
367 __func__, PTR_ERR(hostname));
368 cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
369 server->hostname);
370 }
371 }
372 /* resolve the hostname again to make sure that IP address is up-to-date. */
373 rc = reconn_set_ipaddr_from_hostname(server);
374 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
375 }
376 /* Reconnect the socket */
377 if (cifs_rdma_enabled(server))
378 rc = smbd_reconnect(server);
379 else
380 rc = generic_ip_connect(server);
381
382 return rc;
383}
384
385static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
386 struct dfs_cache_tgt_iterator **target_hint)
387{
388 int rc;
389 struct dfs_cache_tgt_iterator *tit;
390
391 *target_hint = NULL;
392
393 /* If dfs target list is empty, then reconnect to last server */
394 tit = dfs_cache_get_tgt_iterator(tl);
395 if (!tit)
396 return __reconnect_target_unlocked(server, server->hostname);
397
398 /* Otherwise, try every dfs target in @tl */
399 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
400 rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
401 if (!rc) {
402 *target_hint = tit;
403 break;
404 }
405 }
406 return rc;
407}
408
Shyam Prasad N183eea22021-07-19 14:14:46 +0000409static int
410reconnect_dfs_server(struct TCP_Server_Info *server,
411 bool mark_smb_session)
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300412{
413 int rc = 0;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300414 const char *refpath = server->current_fullpath + 1;
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300415 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300416 struct dfs_cache_tgt_iterator *target_hint = NULL;
417 int num_targets = 0;
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300418
419 /*
420 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
421 *
422 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
423 * targets (server->nr_targets). It's also possible that the cached referral was cleared
424 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
425 * refreshing the referral, so, in this case, default it to 1.
426 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300427 if (!dfs_cache_noreq_find(refpath, NULL, &tl))
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300428 num_targets = dfs_cache_get_nr_tgts(&tl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300429 if (!num_targets)
430 num_targets = 1;
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300431
432 if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
433 return 0;
434
Shyam Prasad N183eea22021-07-19 14:14:46 +0000435 cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300436
437 do {
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300438 try_to_freeze();
439 mutex_lock(&server->srv_mutex);
440
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300441 rc = reconnect_target_unlocked(server, &tl, &target_hint);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300442 if (rc) {
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300443 /* Failed to reconnect socket */
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300444 mutex_unlock(&server->srv_mutex);
445 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
446 msleep(3000);
447 continue;
448 }
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300449 /*
450 * Socket was created. Update tcp session status to CifsNeedNegotiate so that a
451 * process waiting for reconnect will know it needs to re-establish session and tcon
452 * through the reconnected target server.
453 */
454 atomic_inc(&tcpSesReconnectCount);
455 set_credits(server, 1);
Shyam Prasad N183eea22021-07-19 14:14:46 +0000456 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300457 if (server->tcpStatus != CifsExiting)
458 server->tcpStatus = CifsNeedNegotiate;
Shyam Prasad N183eea22021-07-19 14:14:46 +0000459 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300460 cifs_swn_reset_server_dstaddr(server);
461 mutex_unlock(&server->srv_mutex);
Shyam Prasad Ne154cb72022-01-16 11:19:36 +0000462 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300463 } while (server->tcpStatus == CifsNeedReconnect);
464
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300465 if (target_hint)
466 dfs_cache_noreq_update_tgthint(refpath, target_hint);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300467
468 dfs_cache_free_tgts(&tl);
469
470 /* Need to set up echo worker again once connection has been established */
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000471 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300472 if (server->tcpStatus == CifsNeedNegotiate)
473 mod_delayed_work(cifsiod_wq, &server->echo, 0);
474
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000475 spin_unlock(&cifs_tcp_ses_lock);
476
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300477 wake_up(&server->response_q);
478 return rc;
479}
480
Shyam Prasad N183eea22021-07-19 14:14:46 +0000481int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300482{
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300483 /* If tcp session is not an dfs connection, then reconnect to last target server */
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300484 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300485 if (!server->is_dfs_conn || !server->origin_fullpath || !server->leaf_fullpath) {
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300486 spin_unlock(&cifs_tcp_ses_lock);
Shyam Prasad N183eea22021-07-19 14:14:46 +0000487 return __cifs_reconnect(server, mark_smb_session);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300488 }
489 spin_unlock(&cifs_tcp_ses_lock);
490
Shyam Prasad N183eea22021-07-19 14:14:46 +0000491 return reconnect_dfs_server(server, mark_smb_session);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300492}
493#else
Shyam Prasad N183eea22021-07-19 14:14:46 +0000494int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300495{
Shyam Prasad N183eea22021-07-19 14:14:46 +0000496 return __cifs_reconnect(server, mark_smb_session);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300497}
498#endif
499
Jeff Laytonc74093b2011-01-11 07:24:23 -0500500static void
501cifs_echo_request(struct work_struct *work)
502{
503 int rc;
504 struct TCP_Server_Info *server = container_of(work,
505 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400506
507 /*
508 * We cannot send an echo if it is disabled.
509 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500510 */
Steve French4fcd1812016-06-22 20:12:05 -0500511
512 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400513 server->tcpStatus == CifsExiting ||
514 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400515 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Shyam Prasad N5b2abda2021-05-01 16:17:07 +0000516 time_before(jiffies, server->lstrp + server->echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500517 goto requeue_echo;
518
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400519 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500520 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500521 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
522 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500523
Samuel Cabrero21077c62020-11-30 19:02:54 +0100524 /* Check witness registrations */
525 cifs_swn_check();
Samuel Cabrero21077c62020-11-30 19:02:54 +0100526
Jeff Laytonc74093b2011-01-11 07:24:23 -0500527requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400528 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500529}
530
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400531static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400532allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400533{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400534 if (!server->bigbuf) {
535 server->bigbuf = (char *)cifs_buf_get();
536 if (!server->bigbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000537 cifs_server_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400538 msleep(3000);
539 /* retry will check if exiting */
540 return false;
541 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400542 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400543 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400544 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400545 }
546
Jeff Layton2a37ef92011-10-19 15:29:23 -0400547 if (!server->smallbuf) {
548 server->smallbuf = (char *)cifs_small_buf_get();
549 if (!server->smallbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000550 cifs_server_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400551 msleep(1000);
552 /* retry will check if exiting */
553 return false;
554 }
555 /* beginning of smb buffer is cleared in our buf_get */
556 } else {
557 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400558 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400559 }
560
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400561 return true;
562}
563
Jeff Laytonba749e62011-10-11 06:41:32 -0400564static bool
565server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400566{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300567 /*
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000568 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300569 * situations right:
570 * 1s client sends a normal SMB request
Ronnie Sahlbergbecc2ba2019-07-24 11:43:49 +1000571 * 2s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300572 * 30s echo workqueue job pops, and decides we got a response recently
573 * and don't need to send another
574 * ...
575 * 65s kernel_recvmsg times out, and we see that we haven't gotten
576 * a response in >60s.
577 */
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000578 spin_lock(&cifs_tcp_ses_lock);
Samuel Cabrero76e75272017-07-11 12:44:39 +0200579 if ((server->tcpStatus == CifsGood ||
580 server->tcpStatus == CifsNeedNegotiate) &&
Shyam Prasad Nf4916642021-04-29 07:53:18 +0000581 (!server->ops->can_echo || server->ops->can_echo(server)) &&
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000582 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000583 spin_unlock(&cifs_tcp_ses_lock);
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000584 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
585 (3 * server->echo_interval) / HZ);
Shyam Prasad N183eea22021-07-19 14:14:46 +0000586 cifs_reconnect(server, false);
Jeff Laytonba749e62011-10-11 06:41:32 -0400587 return true;
588 }
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000589 spin_unlock(&cifs_tcp_ses_lock);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400590
Jeff Laytonba749e62011-10-11 06:41:32 -0400591 return false;
592}
593
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800594static inline bool
595zero_credits(struct TCP_Server_Info *server)
596{
597 int val;
598
599 spin_lock(&server->req_lock);
600 val = server->credits + server->echo_credits + server->oplock_credits;
601 if (server->in_flight == 0 && val == 0) {
602 spin_unlock(&server->req_lock);
603 return true;
604 }
605 spin_unlock(&server->req_lock);
606 return false;
607}
608
Al Viro71335662016-01-09 19:54:50 -0500609static int
610cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400611{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400612 int length = 0;
613 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400614
Al Viro71335662016-01-09 19:54:50 -0500615 smb_msg->msg_control = NULL;
616 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400617
Al Viro71335662016-01-09 19:54:50 -0500618 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500619 try_to_freeze();
620
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800621 /* reconnect if no credits and no requests in flight */
622 if (zero_credits(server)) {
Shyam Prasad N183eea22021-07-19 14:14:46 +0000623 cifs_reconnect(server, false);
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800624 return -ECONNABORTED;
625 }
626
Al Viro71335662016-01-09 19:54:50 -0500627 if (server_unresponsive(server))
628 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700629 if (cifs_rdma_enabled(server) && server->smbd_conn)
630 length = smbd_recv(server->smbd_conn, smb_msg);
631 else
632 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500633
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000634 spin_lock(&cifs_tcp_ses_lock);
635 if (server->tcpStatus == CifsExiting) {
636 spin_unlock(&cifs_tcp_ses_lock);
Al Viro71335662016-01-09 19:54:50 -0500637 return -ESHUTDOWN;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000638 }
Al Viro71335662016-01-09 19:54:50 -0500639
640 if (server->tcpStatus == CifsNeedReconnect) {
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000641 spin_unlock(&cifs_tcp_ses_lock);
Al Viro71335662016-01-09 19:54:50 -0500642 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400643 }
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000644 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonba749e62011-10-11 06:41:32 -0400645
Al Viro71335662016-01-09 19:54:50 -0500646 if (length == -ERESTARTSYS ||
647 length == -EAGAIN ||
648 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400649 /*
650 * Minimum sleep to prevent looping, allowing socket
651 * to clear and app threads to set tcpStatus
652 * CifsNeedReconnect if server hung.
653 */
654 usleep_range(1000, 2000);
655 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400656 continue;
Al Viro71335662016-01-09 19:54:50 -0500657 }
658
659 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500660 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Shyam Prasad N183eea22021-07-19 14:14:46 +0000661 cifs_reconnect(server, false);
Al Viro71335662016-01-09 19:54:50 -0500662 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400663 }
664 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400665 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400666}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400667
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400668int
669cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
670 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400671{
Al Viro71335662016-01-09 19:54:50 -0500672 struct msghdr smb_msg;
673 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100674 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400675
Al Viro71335662016-01-09 19:54:50 -0500676 return cifs_readv_from_socket(server, &smb_msg);
677}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400678
David Howellscf0604a2021-02-04 00:15:21 -0600679ssize_t
680cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
681{
682 struct msghdr smb_msg;
683
684 /*
685 * iov_iter_discard already sets smb_msg.type and count and iov_offset
686 * and cifs_readv_from_socket sets msg_control and msg_controllen
687 * so little to initialize in struct msghdr
688 */
689 smb_msg.msg_name = NULL;
690 smb_msg.msg_namelen = 0;
691 iov_iter_discard(&smb_msg.msg_iter, READ, to_read);
692
693 return cifs_readv_from_socket(server, &smb_msg);
694}
695
Al Viro71335662016-01-09 19:54:50 -0500696int
697cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700698 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500699{
700 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700701 struct bio_vec bv = {
702 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100703 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500704 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400705}
706
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400707static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400708is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400709{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400710 /*
711 * The first byte big endian of the length field,
712 * is actually not part of the length but the type
713 * with the most common, zero, as regular data.
714 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400715 switch (type) {
716 case RFC1002_SESSION_MESSAGE:
717 /* Regular SMB response */
718 return true;
719 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500720 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400721 break;
722 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500723 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400724 break;
725 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400726 /*
727 * We get this from Windows 98 instead of an error on
728 * SMB negprot response.
729 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500730 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400731 /* give server a second to clean up */
732 msleep(1000);
733 /*
734 * Always try 445 first on reconnect since we get NACK
735 * on some if we ever connected to port 139 (the NACK
736 * is since we do not begin with RFC1001 session
737 * initialize frame).
738 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400739 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Shyam Prasad N183eea22021-07-19 14:14:46 +0000740 cifs_reconnect(server, true);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400741 break;
742 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000743 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Shyam Prasad N183eea22021-07-19 14:14:46 +0000744 cifs_reconnect(server, true);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400745 }
746
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400747 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400748}
749
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400750void
751dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400752{
753#ifdef CONFIG_CIFS_STATS2
754 mid->when_received = jiffies;
755#endif
756 spin_lock(&GlobalMid_Lock);
757 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400758 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400759 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400760 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000761 /*
762 * Trying to handle/dequeue a mid after the send_recv()
763 * function has finished processing it is a bug.
764 */
Steve French9e7ffa72021-11-12 09:55:03 -0600765 if (mid->mid_flags & MID_DELETED) {
766 spin_unlock(&GlobalMid_Lock);
Joe Perchesa0a30362020-04-14 22:42:53 -0700767 pr_warn_once("trying to dequeue a deleted mid\n");
Steve French9e7ffa72021-11-12 09:55:03 -0600768 } else {
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000769 list_del_init(&mid->qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700770 mid->mid_flags |= MID_DELETED;
Steve French9e7ffa72021-11-12 09:55:03 -0600771 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700772 }
Jeff Laytonea1f4502011-10-19 15:29:05 -0400773}
774
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800775static unsigned int
776smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
777{
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900778 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800779
780 /*
781 * SMB1 does not use credits.
782 */
783 if (server->vals->header_preamble_size)
784 return 0;
785
786 return le16_to_cpu(shdr->CreditRequest);
787}
788
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400789static void
790handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400791 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400792{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400793 if (server->ops->check_trans2 &&
794 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400795 return;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800796 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400797 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400798 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400799 /* Was previous buf put in mpx struct for multi-rsp? */
800 if (!mid->multiRsp) {
801 /* smb buffer will be freed by user thread */
802 if (server->large_buf)
803 server->bigbuf = NULL;
804 else
805 server->smallbuf = NULL;
806 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400807 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400808}
809
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400810static void clean_demultiplex_info(struct TCP_Server_Info *server)
811{
812 int length;
813
814 /* take it off the list, if it's not already */
815 spin_lock(&cifs_tcp_ses_lock);
816 list_del_init(&server->tcp_ses_list);
817 spin_unlock(&cifs_tcp_ses_lock);
818
Paulo Alcantara21225332020-11-28 16:54:02 -0300819 cancel_delayed_work_sync(&server->echo);
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000820 cancel_delayed_work_sync(&server->resolve);
Paulo Alcantara21225332020-11-28 16:54:02 -0300821
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000822 spin_lock(&cifs_tcp_ses_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400823 server->tcpStatus = CifsExiting;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +0000824 spin_unlock(&cifs_tcp_ses_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400825 wake_up_all(&server->response_q);
826
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400827 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300828 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400829 if (server->credits <= 0)
830 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300831 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400832 /*
833 * Although there should not be any requests blocked on this queue it
834 * can not hurt to be paranoid and try to wake up requests that may
835 * haven been blocked when more than 50 at time were on the wire to the
836 * same server - they now will see the session is in exit state and get
837 * out of SendReceive.
838 */
839 wake_up_all(&server->request_q);
840 /* give those requests time to exit */
841 msleep(125);
Long Li050b8c32019-04-04 11:35:42 -0500842 if (cifs_rdma_enabled(server))
843 smbd_destroy(server);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400844 if (server->ssocket) {
845 sock_release(server->ssocket);
846 server->ssocket = NULL;
847 }
848
849 if (!list_empty(&server->pending_mid_q)) {
850 struct list_head dispose_list;
851 struct mid_q_entry *mid_entry;
852 struct list_head *tmp, *tmp2;
853
854 INIT_LIST_HEAD(&dispose_list);
855 spin_lock(&GlobalMid_Lock);
856 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
857 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Paulo Alcantarabf1bc6942021-03-08 12:00:47 -0300858 cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700859 kref_get(&mid_entry->refcount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400860 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400861 list_move(&mid_entry->qhead, &dispose_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700862 mid_entry->mid_flags |= MID_DELETED;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400863 }
864 spin_unlock(&GlobalMid_Lock);
865
866 /* now walk dispose list and issue callbacks */
867 list_for_each_safe(tmp, tmp2, &dispose_list) {
868 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Paulo Alcantarabf1bc6942021-03-08 12:00:47 -0300869 cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400870 list_del_init(&mid_entry->qhead);
871 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700872 cifs_mid_q_entry_release(mid_entry);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400873 }
874 /* 1/8th of sec is more than enough time for them to exit */
875 msleep(125);
876 }
877
878 if (!list_empty(&server->pending_mid_q)) {
879 /*
880 * mpx threads have not exited yet give them at least the smb
881 * send timeout time for long ops.
882 *
883 * Due to delays on oplock break requests, we need to wait at
884 * least 45 seconds before giving up on a request getting a
885 * response and going ahead and killing cifsd.
886 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500887 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400888 msleep(46000);
889 /*
890 * If threads still have not exited they are probably never
891 * coming home not much else we can do but free the memory.
892 */
893 }
894
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300895#ifdef CONFIG_CIFS_DFS_UPCALL
896 kfree(server->origin_fullpath);
897 kfree(server->leaf_fullpath);
898#endif
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400899 kfree(server);
900
901 length = atomic_dec_return(&tcpSesAllocCount);
902 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700903 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400904}
905
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400906static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400907standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
908{
909 int length;
910 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000911 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400912
913 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100914 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
915 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000916 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Shyam Prasad N183eea22021-07-19 14:14:46 +0000917 cifs_reconnect(server, true);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400918 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400919 }
920
921 /* switch to large buffer if too big for a small one */
922 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
923 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400924 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400925 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400926 }
927
928 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400929 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100930 pdu_length - HEADER_SIZE(server) + 1
931 + server->vals->header_preamble_size);
932
Jeff Laytone9097ab2011-10-19 15:29:40 -0400933 if (length < 0)
934 return length;
935 server->total_read += length;
936
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400937 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400938
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800939 return cifs_handle_standard(server, mid);
940}
941
942int
943cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
944{
945 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
946 int length;
947
Jeff Laytone9097ab2011-10-19 15:29:40 -0400948 /*
949 * We know that we received enough to get to the MID as we
950 * checked the pdu_length earlier. Now check to see
951 * if the rest of the header is OK. We borrow the length
952 * var for the rest of the loop to avoid a new stack var.
953 *
954 * 48 bytes is enough to display the header and a little bit
955 * into the payload for debugging purposes.
956 */
Steve French373512e2015-12-18 13:05:30 -0600957 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400958 if (length != 0)
959 cifs_dump_mem("Bad SMB: ", buf,
960 min_t(unsigned int, server->total_read, 48));
961
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700962 if (server->ops->is_session_expired &&
963 server->ops->is_session_expired(buf)) {
Shyam Prasad N183eea22021-07-19 14:14:46 +0000964 cifs_reconnect(server, true);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700965 return -1;
966 }
967
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700968 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -0800969 server->ops->is_status_pending(buf, server))
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700970 return -1;
971
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500972 if (!mid)
973 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400974
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400975 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500976 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400977}
978
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000979static void
980smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
981{
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900982 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800983 int scredits, in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000984
985 /*
986 * SMB1 does not use credits.
987 */
988 if (server->vals->header_preamble_size)
989 return;
990
991 if (shdr->CreditRequest) {
992 spin_lock(&server->req_lock);
993 server->credits += le16_to_cpu(shdr->CreditRequest);
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800994 scredits = server->credits;
995 in_flight = server->in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000996 spin_unlock(&server->req_lock);
997 wake_up(&server->request_q);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800998
999 trace_smb3_add_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -08001000 server->conn_id, server->hostname, scredits,
1001 le16_to_cpu(shdr->CreditRequest), in_flight);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -08001002 cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
1003 __func__, le16_to_cpu(shdr->CreditRequest),
1004 scredits);
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001005 }
1006}
1007
1008
Jeff Laytone9097ab2011-10-19 15:29:40 -04001009static int
Al Viro7c97c202011-06-21 08:51:28 -04001010cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001012 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -04001013 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001014 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001015 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001016 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001017 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001018 struct mid_q_entry *mids[MAX_COMPOUND];
1019 char *bufs[MAX_COMPOUND];
Rohith Surabattula8e670f72020-09-18 05:37:28 +00001020 unsigned int noreclaim_flag, num_io_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Eric Biggersdc920272020-03-08 22:58:20 -07001022 noreclaim_flag = memalloc_noreclaim_save();
Joe Perchesf96637b2013-05-04 22:12:25 -05001023 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -04001024
1025 length = atomic_inc_return(&tcpSesAllocCount);
1026 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -07001027 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001029 set_freezable();
Eric W. Biederman33da8e72019-08-16 12:33:54 -05001030 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +00001031 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -07001032 if (try_to_freeze())
1033 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001034
Jeff Layton2a37ef92011-10-19 15:29:23 -04001035 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +04001036 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001037
Jeff Layton2a37ef92011-10-19 15:29:23 -04001038 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001039 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +00001040 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +00001041
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001042 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -04001043 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +00001044 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10001045
1046 if (server->vals->header_preamble_size == 0)
1047 server->total_read = 0;
1048 else
1049 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -07001050
Pavel Shilovsky98bac622011-08-01 13:19:42 +04001051 /*
1052 * The right amount was read from socket - 4 bytes,
1053 * so we can now interpret the length field.
1054 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001055 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -07001056
Joe Perchesf96637b2013-05-04 22:12:25 -05001057 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -04001058 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001059 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001060next_pdu:
1061 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -07001062
Jeff Layton89482a52011-10-19 15:28:57 -04001063 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001064 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001065 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001066 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001067 server->pdu_size);
Shyam Prasad N183eea22021-07-19 14:14:46 +00001068 cifs_reconnect(server, true);
Jeff Layton89482a52011-10-19 15:28:57 -04001069 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -07001070 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001071
Jeff Layton89482a52011-10-19 15:28:57 -04001072 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001073 length = cifs_read_from_socket(server,
1074 buf + server->vals->header_preamble_size,
1075 HEADER_SIZE(server) - 1
1076 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001077 if (length < 0)
1078 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001079 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001080
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001081 if (server->ops->next_header) {
1082 next_offset = server->ops->next_header(buf);
1083 if (next_offset)
1084 server->pdu_size = next_offset;
1085 }
1086
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001087 memset(mids, 0, sizeof(mids));
1088 memset(bufs, 0, sizeof(bufs));
1089 num_mids = 0;
1090
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001091 if (server->ops->is_transform_hdr &&
1092 server->ops->receive_transform &&
1093 server->ops->is_transform_hdr(buf)) {
1094 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001095 mids,
1096 bufs,
1097 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001098 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001099 mids[0] = server->ops->find_mid(server, buf);
1100 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001101 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001102
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001103 if (!mids[0] || !mids[0]->receive)
1104 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001105 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001106 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001107 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001108
Lars Persson696e4202018-06-25 14:05:25 +02001109 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001110 for (i = 0; i < num_mids; i++)
1111 if (mids[i])
1112 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001113 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001114 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001115
Rohith Surabattula8e670f72020-09-18 05:37:28 +00001116 if (server->ops->is_status_io_timeout &&
1117 server->ops->is_status_io_timeout(buf)) {
1118 num_io_timeout++;
1119 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
Shyam Prasad N183eea22021-07-19 14:14:46 +00001120 cifs_reconnect(server, false);
Rohith Surabattula8e670f72020-09-18 05:37:28 +00001121 num_io_timeout = 0;
1122 continue;
1123 }
1124 }
1125
Steve Frenchfda35942011-01-20 18:06:34 +00001126 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001127
1128 for (i = 0; i < num_mids; i++) {
1129 if (mids[i] != NULL) {
1130 mids[i]->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001131
Rohith Surabattula9e550b02021-02-16 10:40:45 +00001132 if (bufs[i] && server->ops->is_network_name_deleted)
1133 server->ops->is_network_name_deleted(bufs[i],
1134 server);
1135
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001136 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1137 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001138
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001139 cifs_mid_q_entry_release(mids[i]);
1140 } else if (server->ops->is_oplock_break &&
1141 server->ops->is_oplock_break(bufs[i],
1142 server)) {
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001143 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001144 cifs_dbg(FYI, "Received oplock break\n");
1145 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07001146 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1147 atomic_read(&midCount));
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001148 cifs_dump_mem("Received Data is: ", bufs[i],
1149 HEADER_SIZE(server));
Ronnie Sahlberg3e272572019-07-06 06:43:08 +10001150 smb2_add_credits_from_hdr(bufs[i], server);
Steve French39798772006-05-31 22:40:51 +00001151#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001152 if (server->ops->dump_detail)
1153 server->ops->dump_detail(bufs[i],
1154 server);
1155 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001156#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001157 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001158 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001159
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001160 if (pdu_length > server->pdu_size) {
1161 if (!allocate_buffers(server))
1162 continue;
1163 pdu_length -= server->pdu_size;
1164 server->total_read = 0;
1165 server->large_buf = false;
1166 buf = server->smallbuf;
1167 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001168 }
1169 } /* end while !EXITING */
1170
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001171 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001172 cifs_buf_release(server->bigbuf);
1173 if (server->smallbuf) /* no sense logging a debug message if NULL */
1174 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001176 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001177 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001178
1179 /* if server->tsk was NULL then wait for a signal before exiting */
1180 if (!task_to_wake) {
1181 set_current_state(TASK_INTERRUPTIBLE);
1182 while (!signal_pending(current)) {
1183 schedule();
1184 set_current_state(TASK_INTERRUPTIBLE);
1185 }
1186 set_current_state(TASK_RUNNING);
1187 }
1188
Eric Biggersdc920272020-03-08 22:58:20 -07001189 memalloc_noreclaim_restore(noreclaim_flag);
Eric W. Biedermanca3574b2021-12-03 11:00:19 -06001190 module_put_and_kthread_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Steve French4c51de12021-09-13 18:29:46 -05001193/*
Steve Frenchbc044992020-12-11 19:48:26 -06001194 * Returns true if srcaddr isn't specified and rhs isn't specified, or
1195 * if srcaddr is specified and matches the IP address of the rhs argument
Ben Greear3eb9a882010-09-01 17:06:02 -07001196 */
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001197bool
1198cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
Ben Greear3eb9a882010-09-01 17:06:02 -07001199{
1200 switch (srcaddr->sa_family) {
1201 case AF_UNSPEC:
1202 return (rhs->sa_family == AF_UNSPEC);
1203 case AF_INET: {
1204 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1205 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1206 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1207 }
1208 case AF_INET6: {
1209 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05001210 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07001211 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1212 }
1213 default:
1214 WARN_ON(1);
1215 return false; /* don't expect to be here */
1216 }
1217}
1218
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001219/*
1220 * If no port is specified in addr structure, we try to match with 445 port
1221 * and if it fails - with 139 ports. It should be called only if address
1222 * families of server and addr are equal.
1223 */
1224static bool
1225match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1226{
Steve French6da97912011-03-13 18:55:55 +00001227 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001228
Long Li3b249112019-05-15 14:09:04 -07001229 /* SMBDirect manages its own ports, don't match it here */
1230 if (server->rdma)
1231 return true;
1232
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001233 switch (addr->sa_family) {
1234 case AF_INET:
1235 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1236 port = ((struct sockaddr_in *) addr)->sin_port;
1237 break;
1238 case AF_INET6:
1239 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1240 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1241 break;
1242 default:
1243 WARN_ON(1);
1244 return false;
1245 }
1246
1247 if (!port) {
1248 port = htons(CIFS_PORT);
1249 if (port == *sport)
1250 return true;
1251
1252 port = htons(RFC1001_PORT);
1253 }
1254
1255 return port == *sport;
1256}
Ben Greear3eb9a882010-09-01 17:06:02 -07001257
1258static bool
1259match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1260 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Jeff Layton45151482010-07-06 20:43:02 -04001262 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001263 case AF_INET: {
1264 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1265 struct sockaddr_in *srv_addr4 =
1266 (struct sockaddr_in *)&server->dstaddr;
1267
1268 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04001269 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001270 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001271 }
1272 case AF_INET6: {
1273 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1274 struct sockaddr_in6 *srv_addr6 =
1275 (struct sockaddr_in6 *)&server->dstaddr;
1276
Jeff Layton45151482010-07-06 20:43:02 -04001277 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001278 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04001279 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001280 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04001281 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001282 break;
1283 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001284 default:
1285 WARN_ON(1);
1286 return false; /* don't expect to be here */
1287 }
Jeff Layton45151482010-07-06 20:43:02 -04001288
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001289 if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
Ben Greear3eb9a882010-09-01 17:06:02 -07001290 return false;
1291
Jeff Layton45151482010-07-06 20:43:02 -04001292 return true;
1293}
1294
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001295static bool
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001296match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001297{
Jeff Layton3f618222013-06-12 19:52:14 -05001298 /*
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001299 * The select_sectype function should either return the ctx->sectype
Jeff Layton3f618222013-06-12 19:52:14 -05001300 * that was specified, or "Unspecified" if that sectype was not
1301 * compatible with the given NEGOTIATE request.
1302 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001303 if (server->ops->select_sectype(server, ctx->sectype)
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301304 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001305 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001306
Jeff Layton3f618222013-06-12 19:52:14 -05001307 /*
1308 * Now check if signing mode is acceptable. No need to check
1309 * global_secflags at this point since if MUST_SIGN is set then
1310 * the server->sign had better be too.
1311 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001312 if (ctx->sign && !server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04001313 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001314
1315 return true;
1316}
1317
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001318static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001319{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001320 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
Jeff Layton9fa114f2012-11-26 11:09:57 -05001321
Shyam Prasad Nb9ad6b52021-11-21 16:45:44 +00001322 if (ctx->nosharesock)
Shyam Prasad Nc9f1c192021-11-06 11:31:53 +00001323 return 0;
Shyam Prasad Nc9f1c192021-11-06 11:31:53 +00001324
1325 /* this server does not share socket */
1326 if (server->nosharesock)
Jeff Laytona0b3df52013-05-24 07:40:59 -04001327 return 0;
1328
Steve French43cdae82019-06-13 14:26:49 -05001329 /* If multidialect negotiation see if existing sessions match one */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001330 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
Steve French43cdae82019-06-13 14:26:49 -05001331 if (server->vals->protocol_id < SMB30_PROT_ID)
1332 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001333 } else if (strcmp(ctx->vals->version_string,
Steve French43cdae82019-06-13 14:26:49 -05001334 SMBDEFAULT_VERSION_STRING) == 0) {
1335 if (server->vals->protocol_id < SMB21_PROT_ID)
1336 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001337 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
Jeff Layton23db65f2012-05-15 12:20:51 -04001338 return 0;
1339
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001340 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1341 return 0;
1342
Shyam Prasad N7be32482021-10-14 11:52:39 +00001343 if (strcasecmp(server->hostname, ctx->server_hostname))
1344 return 0;
1345
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001346 if (!match_address(server, addr,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001347 (struct sockaddr *)&ctx->srcaddr))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001348 return 0;
1349
1350 if (!match_port(server, addr))
1351 return 0;
1352
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001353 if (!match_security(server, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001354 return 0;
1355
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001356 if (server->echo_interval != ctx->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001357 return 0;
1358
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001359 if (server->rdma != ctx->rdma)
Long Li8339dd32017-11-07 01:54:55 -07001360 return 0;
1361
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001362 if (server->ignore_signature != ctx->ignore_signature)
Steve French4f5c10f2019-09-03 21:18:49 -05001363 return 0;
1364
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001365 if (server->min_offload != ctx->min_offload)
Steve French563317e2019-09-08 23:22:02 -05001366 return 0;
1367
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001368 return 1;
1369}
1370
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001371struct TCP_Server_Info *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001372cifs_find_tcp_session(struct smb3_fs_context *ctx)
Jeff Layton45151482010-07-06 20:43:02 -04001373{
Jeff Laytone7ddee92008-11-14 13:44:38 -05001374 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301376 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04001377 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Paulo Alcantaracdc33632021-07-15 21:53:53 -03001378#ifdef CONFIG_CIFS_DFS_UPCALL
1379 /*
1380 * DFS failover implementation in cifs_reconnect() requires unique tcp sessions for
1381 * DFS connections to do failover properly, so avoid sharing them with regular
1382 * shares or even links that may connect to same server but having completely
1383 * different failover targets.
1384 */
1385 if (server->is_dfs_conn)
1386 continue;
1387#endif
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -03001388 /*
1389 * Skip ses channels since they're only handled in lower layers
1390 * (e.g. cifs_send_recv).
1391 */
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001392 if (CIFS_SERVER_IS_CHAN(server) || !match_server(server, ctx))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001393 continue;
1394
Jeff Laytone7ddee92008-11-14 13:44:38 -05001395 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301396 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05001397 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05001398 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301400 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return NULL;
1402}
1403
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001404void
1405cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001407 struct task_struct *task;
1408
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301409 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001410 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301411 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001412 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Steve Frenchdea570e02008-05-06 22:05:51 +00001414
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05001415 /* srv_count can never go negative */
1416 WARN_ON(server->srv_count < 0);
1417
Rob Landleyf1d0c992011-01-22 15:44:05 -06001418 put_net(cifs_net_ns(server));
1419
Jeff Laytone7ddee92008-11-14 13:44:38 -05001420 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301421 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001422
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001423 /* For secondary channels, we pick up ref-count on the primary server */
1424 if (CIFS_SERVER_IS_CHAN(server))
1425 cifs_put_tcp_session(server->primary_server, from_reconnect);
1426
Jeff Laytonc74093b2011-01-11 07:24:23 -05001427 cancel_delayed_work_sync(&server->echo);
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001428 cancel_delayed_work_sync(&server->resolve);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001429
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001430 if (from_reconnect)
1431 /*
1432 * Avoid deadlock here: reconnect work calls
1433 * cifs_put_tcp_session() at its end. Need to be sure
1434 * that reconnect work does nothing with server pointer after
1435 * that step.
1436 */
1437 cancel_delayed_work(&server->reconnect);
1438 else
1439 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001440
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00001441 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001442 server->tcpStatus = CifsExiting;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00001443 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001444
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001445 cifs_crypto_secmech_release(server);
Shyam Prasad N46bb1b92021-07-19 13:59:14 +00001446
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05001447 kfree(server->session_key.response);
1448 server->session_key.response = NULL;
1449 server->session_key.len = 0;
Shyam Prasad N7be32482021-10-14 11:52:39 +00001450 kfree(server->hostname);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001451
1452 task = xchg(&server->tsk, NULL);
1453 if (task)
Eric W. Biederman72abe3b2019-05-15 12:33:50 -05001454 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001457struct TCP_Server_Info *
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001458cifs_get_tcp_session(struct smb3_fs_context *ctx,
1459 struct TCP_Server_Info *primary_server)
Jeff Layton63c038c2008-12-01 18:41:46 -05001460{
1461 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05001462 int rc;
1463
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001464 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05001465
1466 /* see if we already have a matching tcp_ses */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001467 tcp_ses = cifs_find_tcp_session(ctx);
Jeff Layton63c038c2008-12-01 18:41:46 -05001468 if (tcp_ses)
1469 return tcp_ses;
1470
1471 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1472 if (!tcp_ses) {
1473 rc = -ENOMEM;
1474 goto out_err;
1475 }
1476
Shyam Prasad N7be32482021-10-14 11:52:39 +00001477 tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
1478 if (!tcp_ses->hostname) {
1479 rc = -ENOMEM;
1480 goto out_err;
1481 }
1482
Shyam Prasad Nb9ad6b52021-11-21 16:45:44 +00001483 if (ctx->nosharesock)
1484 tcp_ses->nosharesock = true;
1485
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001486 tcp_ses->ops = ctx->ops;
1487 tcp_ses->vals = ctx->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06001488 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05001489
Shyam Prasad N6d82c272021-02-03 23:20:46 -08001490 tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001491 tcp_ses->noblockcnt = ctx->rootfs;
1492 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1493 tcp_ses->noautotune = ctx->noautotune;
1494 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1495 tcp_ses->rdma = ctx->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03001496 tcp_ses->in_flight = 0;
Steve French1b63f182019-09-09 22:57:11 -05001497 tcp_ses->max_in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04001498 tcp_ses->credits = 1;
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001499 if (primary_server) {
Steve French446e2142021-11-15 18:02:27 -06001500 spin_lock(&cifs_tcp_ses_lock);
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001501 ++primary_server->srv_count;
1502 tcp_ses->primary_server = primary_server;
Steve French446e2142021-11-15 18:02:27 -06001503 spin_unlock(&cifs_tcp_ses_lock);
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001504 }
Jeff Layton63c038c2008-12-01 18:41:46 -05001505 init_waitqueue_head(&tcp_ses->response_q);
1506 init_waitqueue_head(&tcp_ses->request_q);
1507 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1508 mutex_init(&tcp_ses->srv_mutex);
1509 memcpy(tcp_ses->workstation_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001510 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Jeff Layton63c038c2008-12-01 18:41:46 -05001511 memcpy(tcp_ses->server_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001512 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05001513 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05001514 tcp_ses->sequence_number = 0;
Pavel Shilovsky5b964852019-01-18 11:30:26 -08001515 tcp_ses->reconnect_instance = 1;
Steve Frenchfda35942011-01-20 18:06:34 +00001516 tcp_ses->lstrp = jiffies;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001517 tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
Jeff Layton58fa0152012-05-01 17:41:16 -04001518 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001519 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1520 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001521 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001522 INIT_DELAYED_WORK(&tcp_ses->resolve, cifs_resolve_server);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001523 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1524 mutex_init(&tcp_ses->reconnect_mutex);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03001525#ifdef CONFIG_CIFS_DFS_UPCALL
1526 mutex_init(&tcp_ses->refpath_lock);
1527#endif
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001528 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001529 sizeof(tcp_ses->srcaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001530 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001531 sizeof(tcp_ses->dstaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001532 if (ctx->use_client_guid)
1533 memcpy(tcp_ses->client_guid, ctx->client_guid,
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001534 SMB2_CLIENT_GUID_SIZE);
1535 else
1536 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05001537 /*
1538 * at this point we are the only ones with the pointer
1539 * to the struct since the kernel thread not created yet
1540 * no need to spinlock this init of tcpStatus or srv_count
1541 */
1542 tcp_ses->tcpStatus = CifsNew;
1543 ++tcp_ses->srv_count;
1544
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001545 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1546 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1547 tcp_ses->echo_interval = ctx->echo_interval * HZ;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001548 else
1549 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07001550 if (tcp_ses->rdma) {
1551#ifndef CONFIG_CIFS_SMB_DIRECT
1552 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1553 rc = -ENOENT;
1554 goto out_err_crypto_release;
1555#endif
1556 tcp_ses->smbd_conn = smbd_get_connection(
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001557 tcp_ses, (struct sockaddr *)&ctx->dstaddr);
Long Li2f894642017-11-22 17:38:34 -07001558 if (tcp_ses->smbd_conn) {
1559 cifs_dbg(VFS, "RDMA transport established\n");
1560 rc = 0;
1561 goto smbd_connected;
1562 } else {
1563 rc = -ENOENT;
1564 goto out_err_crypto_release;
1565 }
1566 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001567 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05001568 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001569 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001570 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001571 }
Long Li2f894642017-11-22 17:38:34 -07001572smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05001573 /*
1574 * since we're in a cifs function already, we know that
1575 * this will succeed. No need for try_module_get().
1576 */
1577 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04001578 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05001579 tcp_ses, "cifsd");
1580 if (IS_ERR(tcp_ses->tsk)) {
1581 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05001582 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05001583 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001584 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001585 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001586 tcp_ses->min_offload = ctx->min_offload;
Steve French01cf3082021-07-01 12:22:47 -05001587 /*
1588 * at this point we are the only ones with the pointer
1589 * to the struct since the kernel thread not created yet
1590 * no need to spinlock this update of tcpStatus
1591 */
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00001592 spin_lock(&cifs_tcp_ses_lock);
Steve Frenchfd88ce92011-04-12 01:01:14 +00001593 tcp_ses->tcpStatus = CifsNeedNegotiate;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00001594 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001595
Aurelien Aptela249cc82021-03-04 17:42:21 +00001596 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
1597 tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
1598 else
1599 tcp_ses->max_credits = ctx->max_credits;
1600
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02001601 tcp_ses->nr_targets = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001602 tcp_ses->ignore_signature = ctx->ignore_signature;
Jeff Layton63c038c2008-12-01 18:41:46 -05001603 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301604 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001605 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301606 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001607
Jeff Laytonc74093b2011-01-11 07:24:23 -05001608 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001609 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001610
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001611 /* queue dns resolution delayed work */
1612 cifs_dbg(FYI, "%s: next dns resolution scheduled for %d seconds in the future\n",
1613 __func__, SMB_DNS_RESOLVE_INTERVAL_DEFAULT);
1614
1615 queue_delayed_work(cifsiod_wq, &tcp_ses->resolve, (SMB_DNS_RESOLVE_INTERVAL_DEFAULT * HZ));
1616
Jeff Layton63c038c2008-12-01 18:41:46 -05001617 return tcp_ses;
1618
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001619out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001620 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05001621
Rob Landleyf1d0c992011-01-22 15:44:05 -06001622 put_net(cifs_net_ns(tcp_ses));
1623
Jeff Layton63c038c2008-12-01 18:41:46 -05001624out_err:
1625 if (tcp_ses) {
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001626 if (CIFS_SERVER_IS_CHAN(tcp_ses))
1627 cifs_put_tcp_session(tcp_ses->primary_server, false);
Shyam Prasad N7be32482021-10-14 11:52:39 +00001628 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05001629 if (tcp_ses->ssocket)
1630 sock_release(tcp_ses->ssocket);
1631 kfree(tcp_ses);
1632 }
1633 return ERR_PTR(rc);
1634}
1635
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001636static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001637{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001638 if (ctx->sectype != Unspecified &&
1639 ctx->sectype != ses->sectype)
Jeff Layton3f618222013-06-12 19:52:14 -05001640 return 0;
1641
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001642 /*
1643 * If an existing session is limited to less channels than
1644 * requested, it should not be reused
1645 */
Shyam Prasad N724244c2021-07-19 10:54:46 +00001646 spin_lock(&ses->chan_lock);
1647 if (ses->chan_max < ctx->max_channels) {
1648 spin_unlock(&ses->chan_lock);
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001649 return 0;
Shyam Prasad N724244c2021-07-19 10:54:46 +00001650 }
1651 spin_unlock(&ses->chan_lock);
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001652
Jeff Layton3f618222013-06-12 19:52:14 -05001653 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001654 case Kerberos:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001655 if (!uid_eq(ctx->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001656 return 0;
1657 break;
1658 default:
Jeff Layton04febab2012-01-17 16:09:15 -05001659 /* NULL username means anonymous session */
1660 if (ses->user_name == NULL) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001661 if (!ctx->nullauth)
Jeff Layton04febab2012-01-17 16:09:15 -05001662 return 0;
1663 break;
1664 }
1665
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001666 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05001667 if (strncmp(ses->user_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001668 ctx->username ? ctx->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001669 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001670 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001671 if ((ctx->username && strlen(ctx->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001672 ses->password != NULL &&
1673 strncmp(ses->password,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001674 ctx->password ? ctx->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001675 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001676 return 0;
1677 }
1678 return 1;
1679}
1680
Aurelien Aptelb327a712018-01-24 13:46:10 +01001681/**
1682 * cifs_setup_ipc - helper to setup the IPC tcon for the session
Steve French4c51de12021-09-13 18:29:46 -05001683 * @ses: smb session to issue the request on
1684 * @ctx: the superblock configuration context to use for building the
1685 * new tree connection for the IPC (interprocess communication RPC)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001686 *
1687 * A new IPC connection is made and stored in the session
1688 * tcon_ipc. The IPC tcon has the same lifetime as the session.
1689 */
1690static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001691cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001692{
1693 int rc = 0, xid;
1694 struct cifs_tcon *tcon;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001695 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1696 bool seal = false;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001697 struct TCP_Server_Info *server = ses->server;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001698
1699 /*
1700 * If the mount request that resulted in the creation of the
1701 * session requires encryption, force IPC to be encrypted too.
1702 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001703 if (ctx->seal) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001704 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001705 seal = true;
1706 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001707 cifs_server_dbg(VFS,
Aurelien Aptelb327a712018-01-24 13:46:10 +01001708 "IPC: server doesn't support encryption\n");
1709 return -EOPNOTSUPP;
1710 }
1711 }
1712
1713 tcon = tconInfoAlloc();
1714 if (tcon == NULL)
1715 return -ENOMEM;
1716
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001717 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001718
Aurelien Aptelb327a712018-01-24 13:46:10 +01001719 xid = get_xid();
1720 tcon->ses = ses;
1721 tcon->ipc = true;
1722 tcon->seal = seal;
Ronnie Sahlberg6fd4ea82020-12-14 16:40:21 +10001723 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001724 free_xid(xid);
1725
1726 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001727 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001728 tconInfoFree(tcon);
1729 goto out;
1730 }
1731
1732 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
1733
1734 ses->tcon_ipc = tcon;
1735out:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001736 return rc;
1737}
1738
1739/**
1740 * cifs_free_ipc - helper to release the session IPC tcon
Steve French4c51de12021-09-13 18:29:46 -05001741 * @ses: smb session to unmount the IPC from
Aurelien Aptelb327a712018-01-24 13:46:10 +01001742 *
Paulo Alcantaraf3191fc2021-06-04 19:25:27 -03001743 * Needs to be called everytime a session is destroyed.
1744 *
1745 * On session close, the IPC is closed and the server must release all tcons of the session.
1746 * No need to send a tree disconnect here.
1747 *
1748 * Besides, it will make the server to not close durable and resilient files on session close, as
1749 * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
Aurelien Aptelb327a712018-01-24 13:46:10 +01001750 */
1751static int
1752cifs_free_ipc(struct cifs_ses *ses)
1753{
Aurelien Aptelb327a712018-01-24 13:46:10 +01001754 struct cifs_tcon *tcon = ses->tcon_ipc;
1755
1756 if (tcon == NULL)
1757 return 0;
1758
Aurelien Aptelb327a712018-01-24 13:46:10 +01001759 tconInfoFree(tcon);
1760 ses->tcon_ipc = NULL;
Paulo Alcantaraf3191fc2021-06-04 19:25:27 -03001761 return 0;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001762}
1763
Steve French96daf2b2011-05-27 04:34:02 +00001764static struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001765cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Steve French96daf2b2011-05-27 04:34:02 +00001767 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301769 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04001770 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001771 if (ses->status == CifsExiting)
1772 continue;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001773 if (!match_session(ses, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001774 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05001775 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301776 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001777 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301779 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return NULL;
1781}
1782
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001783void cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05001784{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001785 unsigned int rc, xid;
Shyam Prasad N724244c2021-07-19 10:54:46 +00001786 unsigned int chan_count;
Jeff Layton14fbf502008-11-14 13:53:46 -05001787 struct TCP_Server_Info *server = ses->server;
Joe Perchesf96637b2013-05-04 22:12:25 -05001788 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001789
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301790 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001791 if (ses->status == CifsExiting) {
1792 spin_unlock(&cifs_tcp_ses_lock);
1793 return;
1794 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001795
1796 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1797 cifs_dbg(FYI, "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->treeName : "NONE");
1798
Jeff Layton14fbf502008-11-14 13:53:46 -05001799 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301800 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001801 return;
1802 }
Steve French0060a4f2021-06-24 15:28:04 -05001803
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05001804 /* ses_count can never go negative */
1805 WARN_ON(ses->ses_count < 0);
1806
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001807 if (ses->status == CifsGood)
1808 ses->status = CifsExiting;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00001809 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001810
Aurelien Aptelb327a712018-01-24 13:46:10 +01001811 cifs_free_ipc(ses);
1812
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001813 if (ses->status == CifsExiting && server->ops->logoff) {
1814 xid = get_xid();
1815 rc = server->ops->logoff(xid, ses);
1816 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001817 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001818 __func__, rc);
1819 _free_xid(xid);
1820 }
1821
1822 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001823 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301824 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001825
Shyam Prasad N724244c2021-07-19 10:54:46 +00001826 spin_lock(&ses->chan_lock);
1827 chan_count = ses->chan_count;
Shyam Prasad N724244c2021-07-19 10:54:46 +00001828
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001829 /* close any extra channels */
Shyam Prasad N724244c2021-07-19 10:54:46 +00001830 if (chan_count > 1) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001831 int i;
1832
Shyam Prasad N724244c2021-07-19 10:54:46 +00001833 for (i = 1; i < chan_count; i++) {
1834 /*
1835 * note: for now, we're okay accessing ses->chans
1836 * without chan_lock. But when chans can go away, we'll
1837 * need to introduce ref counting to make sure that chan
1838 * is not freed from under us.
1839 */
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001840 cifs_put_tcp_session(ses->chans[i].server, 0);
Shyam Prasad N724244c2021-07-19 10:54:46 +00001841 ses->chans[i].server = NULL;
1842 }
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001843 }
Shyam Prasad N88b024f2021-11-19 14:16:57 +00001844 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001845
Jeff Layton14fbf502008-11-14 13:53:46 -05001846 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001847 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05001848}
1849
Jeff Layton8a8798a2012-01-17 16:09:15 -05001850#ifdef CONFIG_KEYS
1851
Chen Gang057d6332013-07-19 09:01:36 +08001852/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
1853#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001854
1855/* Populate username and pw fields from keyring if possible */
1856static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001857cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001858{
1859 int rc = 0;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001860 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01001861 const char *delim, *payload;
1862 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001863 ssize_t len;
1864 struct key *key;
1865 struct TCP_Server_Info *server = ses->server;
1866 struct sockaddr_in *sa;
1867 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01001868 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001869
1870 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
1871 if (!desc)
1872 return -ENOMEM;
1873
1874 /* try to find an address key first */
1875 switch (server->dstaddr.ss_family) {
1876 case AF_INET:
1877 sa = (struct sockaddr_in *)&server->dstaddr;
1878 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
1879 break;
1880 case AF_INET6:
1881 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
1882 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
1883 break;
1884 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001885 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
1886 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001887 rc = -EINVAL;
1888 goto out_err;
1889 }
1890
Joe Perchesf96637b2013-05-04 22:12:25 -05001891 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001892 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001893 if (IS_ERR(key)) {
1894 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001895 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001896 rc = PTR_ERR(key);
1897 goto out_err;
1898 }
1899
1900 /* didn't work, try to find a domain key */
1901 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05001902 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001903 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001904 if (IS_ERR(key)) {
1905 rc = PTR_ERR(key);
1906 goto out_err;
1907 }
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001908 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001909 }
1910
1911 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00001912 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001913 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05001914 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001915 goto out_key_put;
1916 }
1917
1918 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01001919 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001920 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05001921 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001922 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001923 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
1924 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001925 rc = -EINVAL;
1926 goto out_key_put;
1927 }
1928
1929 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001930 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001931 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
1932 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001933 rc = -EINVAL;
1934 goto out_key_put;
1935 }
1936
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001937 ctx->username = kstrndup(payload, len, GFP_KERNEL);
1938 if (!ctx->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001939 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
1940 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001941 rc = -ENOMEM;
1942 goto out_key_put;
1943 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001944 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001945
1946 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001947 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001948 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001949 rc = -EINVAL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001950 kfree(ctx->username);
1951 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001952 goto out_key_put;
1953 }
1954
1955 ++delim;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001956 ctx->password = kstrndup(delim, len, GFP_KERNEL);
1957 if (!ctx->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001958 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
1959 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001960 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001961 kfree(ctx->username);
1962 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001963 goto out_key_put;
1964 }
1965
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001966 /*
1967 * If we have a domain key then we must set the domainName in the
1968 * for the request.
1969 */
1970 if (is_domain && ses->domainName) {
Al Viro8d767222021-03-05 15:02:34 -05001971 ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001972 if (!ctx->domainname) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001973 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
1974 len);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001975 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001976 kfree(ctx->username);
1977 ctx->username = NULL;
1978 kfree_sensitive(ctx->password);
1979 ctx->password = NULL;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001980 goto out_key_put;
1981 }
1982 }
1983
Jeff Layton8a8798a2012-01-17 16:09:15 -05001984out_key_put:
1985 up_read(&key->sem);
1986 key_put(key);
1987out_err:
1988 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05001989 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001990 return rc;
1991}
1992#else /* ! CONFIG_KEYS */
1993static inline int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001994cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
Jeff Layton8a8798a2012-01-17 16:09:15 -05001995 struct cifs_ses *ses __attribute__((unused)))
1996{
1997 return -ENOSYS;
1998}
1999#endif /* CONFIG_KEYS */
2000
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002001/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002002 * cifs_get_smb_ses - get a session matching @ctx data from @server
Steve French4c51de12021-09-13 18:29:46 -05002003 * @server: server to setup the session to
2004 * @ctx: superblock configuration context to use to setup the session
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002005 *
2006 * This function assumes it is being called from cifs_mount() where we
2007 * already got a server reference (server refcount +1). See
2008 * cifs_get_tcon() for refcount explanations.
2009 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03002010struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002011cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Layton36988c72010-04-24 07:57:43 -04002012{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002013 int rc = -ENOMEM;
2014 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002015 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002016 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2017 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002018
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002019 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002020
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002021 ses = cifs_find_smb_ses(server, ctx);
Jeff Layton36988c72010-04-24 07:57:43 -04002022 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002023 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2024 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002025
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +00002026 spin_lock(&ses->chan_lock);
2027 if (cifs_chan_needs_reconnect(ses, server)) {
2028 spin_unlock(&ses->chan_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002029 cifs_dbg(FYI, "Session needs reconnect\n");
Shyam Prasad N8e077572021-07-19 10:03:38 +00002030
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00002031 mutex_lock(&ses->session_mutex);
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00002032 rc = cifs_negotiate_protocol(xid, ses, server);
Shyam Prasad N8e077572021-07-19 10:03:38 +00002033 if (rc) {
2034 mutex_unlock(&ses->session_mutex);
2035 /* problem -- put our ses reference */
2036 cifs_put_smb_ses(ses);
2037 free_xid(xid);
2038 return ERR_PTR(rc);
2039 }
2040
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00002041 rc = cifs_setup_session(xid, ses, server,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002042 ctx->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002043 if (rc) {
2044 mutex_unlock(&ses->session_mutex);
2045 /* problem -- put our reference */
2046 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002047 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002048 return ERR_PTR(rc);
2049 }
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00002050 mutex_unlock(&ses->session_mutex);
2051
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +00002052 spin_lock(&ses->chan_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002053 }
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +00002054 spin_unlock(&ses->chan_lock);
Jeff Layton460cf342010-09-14 11:38:24 -04002055
2056 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002057 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002058 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002059 return ses;
2060 }
2061
Joe Perchesf96637b2013-05-04 22:12:25 -05002062 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002063 ses = sesInfoAlloc();
2064 if (ses == NULL)
2065 goto get_ses_fail;
2066
2067 /* new SMB session uses our server ref */
2068 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002069 if (server->dstaddr.ss_family == AF_INET6)
Steve Frenchb438fcf2021-02-20 19:24:11 -06002070 sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002071 else
Steve Frenchb438fcf2021-02-20 19:24:11 -06002072 sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002073
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002074 if (ctx->username) {
2075 ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
Steve French8727c8a2011-02-25 01:11:56 -06002076 if (!ses->user_name)
2077 goto get_ses_fail;
2078 }
Jeff Layton36988c72010-04-24 07:57:43 -04002079
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002080 /* ctx->password freed at unmount */
2081 if (ctx->password) {
2082 ses->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Layton36988c72010-04-24 07:57:43 -04002083 if (!ses->password)
2084 goto get_ses_fail;
2085 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002086 if (ctx->domainname) {
2087 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002088 if (!ses->domainName)
2089 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002090 }
Shyam Prasad N49bd49f2021-11-05 19:03:57 +00002091 if (ctx->workstation_name) {
2092 ses->workstation_name = kstrdup(ctx->workstation_name,
2093 GFP_KERNEL);
2094 if (!ses->workstation_name)
2095 goto get_ses_fail;
2096 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002097 if (ctx->domainauto)
2098 ses->domainAuto = ctx->domainauto;
2099 ses->cred_uid = ctx->cred_uid;
2100 ses->linux_uid = ctx->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002101
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002102 ses->sectype = ctx->sectype;
2103 ses->sign = ctx->sign;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002104
2105 /* add server as first channel */
Shyam Prasad N724244c2021-07-19 10:54:46 +00002106 spin_lock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002107 ses->chans[0].server = server;
2108 ses->chan_count = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002109 ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +00002110 ses->chans_need_reconnect = 1;
Shyam Prasad N724244c2021-07-19 10:54:46 +00002111 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002112
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00002113 mutex_lock(&ses->session_mutex);
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00002114 rc = cifs_negotiate_protocol(xid, ses, server);
Jeff Layton198b5682010-04-24 07:57:48 -04002115 if (!rc)
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00002116 rc = cifs_setup_session(xid, ses, server, ctx->local_nls);
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00002117 mutex_unlock(&ses->session_mutex);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002118
2119 /* each channel uses a different signing key */
Shyam Prasad N88b024f2021-11-19 14:16:57 +00002120 spin_lock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002121 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
2122 sizeof(ses->smb3signingkey));
Shyam Prasad N88b024f2021-11-19 14:16:57 +00002123 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002124
Steve Frenchc8e56f12010-09-08 21:10:58 +00002125 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002126 goto get_ses_fail;
2127
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +00002128 /*
2129 * success, put it on the list and add it as first channel
2130 * note: the session becomes active soon after this. So you'll
2131 * need to lock before changing something in the session.
2132 */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302133 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002134 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302135 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002136
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002137 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002138
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002139 cifs_setup_ipc(ses, ctx);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002140
Jeff Layton36988c72010-04-24 07:57:43 -04002141 return ses;
2142
2143get_ses_fail:
2144 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002145 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002146 return ERR_PTR(rc);
2147}
2148
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002149static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002150{
2151 if (tcon->tidStatus == CifsExiting)
2152 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002153 if (strncmp(tcon->treeName, ctx->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002154 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002155 if (tcon->seal != ctx->seal)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002156 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002157 if (tcon->snapshot_time != ctx->snapshot_time)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002158 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002159 if (tcon->handle_timeout != ctx->handle_timeout)
Steve Frenchca567eb2019-03-29 16:31:07 -05002160 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002161 if (tcon->no_lease != ctx->no_lease)
Steve French3e7a02d2019-09-11 21:46:20 -05002162 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002163 if (tcon->nodelete != ctx->nodelete)
Steve French82e93672020-05-19 03:06:57 -05002164 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002165 return 1;
2166}
2167
Steve French96daf2b2011-05-27 04:34:02 +00002168static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002169cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002172 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302174 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002175 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002176 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Paulo Alcantaraf3c852b2021-06-04 19:25:33 -03002177
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002178 if (!match_tcon(tcon, ctx))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002179 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002180 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302181 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return tcon;
2183 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302184 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return NULL;
2186}
2187
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002188void
Steve French96daf2b2011-05-27 04:34:02 +00002189cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002190{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002191 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002192 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002193
Aurelien Aptelb327a712018-01-24 13:46:10 +01002194 /*
2195 * IPC tcon share the lifetime of their session and are
2196 * destroyed in the session put function
2197 */
2198 if (tcon == NULL || tcon->ipc)
2199 return;
2200
2201 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002202 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302203 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002204 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302205 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002206 return;
2207 }
2208
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05002209 /* tc_count can never go negative */
2210 WARN_ON(tcon->tc_count < 0);
2211
Shyam Prasad Nd1a931c2021-07-19 12:46:53 +00002212 list_del_init(&tcon->tcon_list);
2213 spin_unlock(&cifs_tcp_ses_lock);
2214
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002215 if (tcon->use_witness) {
2216 int rc;
2217
2218 rc = cifs_swn_unregister(tcon);
2219 if (rc < 0) {
2220 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
2221 __func__, rc);
2222 }
2223 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002224
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002225 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002226 if (ses->server->ops->tree_disconnect)
2227 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002228 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002229
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302230 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002231 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002232 cifs_put_smb_ses(ses);
2233}
2234
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002235/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002236 * cifs_get_tcon - get a tcon matching @ctx data from @ses
Steve French4c51de12021-09-13 18:29:46 -05002237 * @ses: smb session to issue the request on
2238 * @ctx: the superblock configuration context to use for building the
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002239 *
2240 * - tcon refcount is the number of mount points using the tcon.
2241 * - ses refcount is the number of tcon using the session.
2242 *
2243 * 1. This function assumes it is being called from cifs_mount() where
2244 * we already got a session reference (ses refcount +1).
2245 *
2246 * 2. Since we're in the context of adding a mount point, the end
2247 * result should be either:
2248 *
2249 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2250 * its session refcount incremented (1 new tcon). This +1 was
2251 * already done in (1).
2252 *
2253 * b) an existing tcon with refcount+1 (add a mount point to it) and
2254 * identical ses refcount (no new tcon). Because of (1) we need to
2255 * decrement the ses refcount.
2256 */
Steve French96daf2b2011-05-27 04:34:02 +00002257static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002258cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002259{
2260 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002261 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002262
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002263 tcon = cifs_find_tcon(ses, ctx);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002264 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002265 /*
2266 * tcon has refcount already incremented but we need to
2267 * decrement extra ses reference gotten by caller (case b)
2268 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002269 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002270 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002271 return tcon;
2272 }
2273
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002274 if (!ses->server->ops->tree_connect) {
2275 rc = -ENOSYS;
2276 goto out_fail;
2277 }
2278
Jeff Laytond00c28d2010-04-24 07:57:44 -04002279 tcon = tconInfoAlloc();
2280 if (tcon == NULL) {
2281 rc = -ENOMEM;
2282 goto out_fail;
2283 }
2284
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002285 if (ctx->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06002286 if (ses->server->vals->protocol_id == 0) {
2287 cifs_dbg(VFS,
2288 "Use SMB2 or later for snapshot mount option\n");
2289 rc = -EOPNOTSUPP;
2290 goto out_fail;
2291 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002292 tcon->snapshot_time = ctx->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06002293 }
2294
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002295 if (ctx->handle_timeout) {
Steve Frenchca567eb2019-03-29 16:31:07 -05002296 if (ses->server->vals->protocol_id == 0) {
2297 cifs_dbg(VFS,
2298 "Use SMB2.1 or later for handle timeout option\n");
2299 rc = -EOPNOTSUPP;
2300 goto out_fail;
2301 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002302 tcon->handle_timeout = ctx->handle_timeout;
Steve Frenchca567eb2019-03-29 16:31:07 -05002303 }
2304
Jeff Laytond00c28d2010-04-24 07:57:44 -04002305 tcon->ses = ses;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002306 if (ctx->password) {
2307 tcon->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002308 if (!tcon->password) {
2309 rc = -ENOMEM;
2310 goto out_fail;
2311 }
2312 }
2313
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002314 if (ctx->seal) {
Steve French23657ad2018-04-22 15:14:58 -05002315 if (ses->server->vals->protocol_id == 0) {
2316 cifs_dbg(VFS,
2317 "SMB3 or later required for encryption\n");
2318 rc = -EOPNOTSUPP;
2319 goto out_fail;
2320 } else if (tcon->ses->server->capabilities &
2321 SMB2_GLOBAL_CAP_ENCRYPTION)
2322 tcon->seal = true;
2323 else {
2324 cifs_dbg(VFS, "Encryption is not supported on share\n");
2325 rc = -EOPNOTSUPP;
2326 goto out_fail;
2327 }
2328 }
2329
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002330 if (ctx->linux_ext) {
Steve French8505c8b2018-06-18 14:01:59 -05002331 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05002332 tcon->posix_extensions = true;
Joe Perchesa0a30362020-04-14 22:42:53 -07002333 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05002334 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07002335 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
Steve French8505c8b2018-06-18 14:01:59 -05002336 rc = -EOPNOTSUPP;
2337 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05002338 }
Steve Frenchb3266142018-05-20 23:41:10 -05002339 }
Steve Frenchb3266142018-05-20 23:41:10 -05002340
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002341 xid = get_xid();
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002342 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2343 ctx->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002344 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05002345 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002346 if (rc)
2347 goto out_fail;
2348
Steve Frenchb618f002015-11-03 09:15:03 -06002349 tcon->use_persistent = false;
2350 /* check if SMB2 or later, CIFS does not support persistent handles */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002351 if (ctx->persistent) {
Steve Frenchb618f002015-11-03 09:15:03 -06002352 if (ses->server->vals->protocol_id == 0) {
2353 cifs_dbg(VFS,
2354 "SMB3 or later required for persistent handles\n");
2355 rc = -EOPNOTSUPP;
2356 goto out_fail;
2357 } else if (ses->server->capabilities &
2358 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2359 tcon->use_persistent = true;
2360 else /* persistent handles requested but not supported */ {
2361 cifs_dbg(VFS,
2362 "Persistent handles not supported on share\n");
2363 rc = -EOPNOTSUPP;
2364 goto out_fail;
2365 }
2366 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2367 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002368 && (ctx->nopersistent == false)) {
Steve Frenchb618f002015-11-03 09:15:03 -06002369 cifs_dbg(FYI, "enabling persistent handles\n");
2370 tcon->use_persistent = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002371 } else if (ctx->resilient) {
Steve French592fafe2015-11-03 10:08:53 -06002372 if (ses->server->vals->protocol_id == 0) {
2373 cifs_dbg(VFS,
2374 "SMB2.1 or later required for resilient handles\n");
2375 rc = -EOPNOTSUPP;
2376 goto out_fail;
2377 }
2378 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06002379 }
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +02002380
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002381 tcon->use_witness = false;
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +02002382 if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002383 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2384 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002385 /*
2386 * Set witness in use flag in first place
2387 * to retry registration in the echo task
2388 */
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002389 tcon->use_witness = true;
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002390 /* And try to register immediately */
2391 rc = cifs_swn_register(tcon);
2392 if (rc < 0) {
2393 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2394 goto out_fail;
2395 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002396 } else {
2397 /* TODO: try to extend for non-cluster uses (eg multichannel) */
2398 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2399 rc = -EOPNOTSUPP;
2400 goto out_fail;
2401 }
2402 } else {
2403 cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2404 rc = -EOPNOTSUPP;
2405 goto out_fail;
2406 }
2407 }
Steve Frenchb618f002015-11-03 09:15:03 -06002408
Steve Frenchcae53f72019-09-03 17:49:46 -05002409 /* If the user really knows what they are doing they can override */
2410 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002411 if (ctx->cache_ro)
Steve Frenchcae53f72019-09-03 17:49:46 -05002412 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002413 else if (ctx->cache_rw)
Steve Frenchcae53f72019-09-03 17:49:46 -05002414 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2415 }
2416
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002417 if (ctx->no_lease) {
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302418 if (ses->server->vals->protocol_id == 0) {
2419 cifs_dbg(VFS,
2420 "SMB2 or later required for nolease option\n");
2421 rc = -EOPNOTSUPP;
2422 goto out_fail;
2423 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002424 tcon->no_lease = ctx->no_lease;
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302425 }
2426
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002427 /*
2428 * We can have only one retry value for a connection to a share so for
2429 * resources mounted more than once to the same server share the last
2430 * value passed in for the retry flag is used.
2431 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002432 tcon->retry = ctx->retry;
2433 tcon->nocase = ctx->nocase;
Steve French3c6e65e2020-10-21 00:15:42 -05002434 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002435 tcon->nohandlecache = ctx->nohandlecache;
Steve French3c6e65e2020-10-21 00:15:42 -05002436 else
Jiapeng Zhong2be449f2021-01-14 17:09:20 +08002437 tcon->nohandlecache = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002438 tcon->nodelete = ctx->nodelete;
2439 tcon->local_lease = ctx->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07002440 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002441
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302442 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002443 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302444 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002445
2446 return tcon;
2447
2448out_fail:
2449 tconInfoFree(tcon);
2450 return ERR_PTR(rc);
2451}
2452
Jeff Layton9d002df2010-10-06 19:51:11 -04002453void
2454cifs_put_tlink(struct tcon_link *tlink)
2455{
2456 if (!tlink || IS_ERR(tlink))
2457 return;
2458
2459 if (!atomic_dec_and_test(&tlink->tl_count) ||
2460 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2461 tlink->tl_time = jiffies;
2462 return;
2463 }
2464
2465 if (!IS_ERR(tlink_tcon(tlink)))
2466 cifs_put_tcon(tlink_tcon(tlink));
2467 kfree(tlink);
2468 return;
2469}
Jeff Laytond00c28d2010-04-24 07:57:44 -04002470
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002471static int
2472compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2473{
2474 struct cifs_sb_info *old = CIFS_SB(sb);
2475 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002476 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2477 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002478
2479 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2480 return 0;
2481
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002482 if (old->mnt_cifs_serverino_autodisabled)
2483 newflags &= ~CIFS_MOUNT_SERVER_INUM;
2484
2485 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002486 return 0;
2487
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002488 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002489 * We want to share sb only if we don't specify an r/wsize or
2490 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002491 */
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002492 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002493 return 0;
2494
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002495 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002496 return 0;
2497
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002498 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2499 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002500 return 0;
2501
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002502 if (old->ctx->file_mode != new->ctx->file_mode ||
2503 old->ctx->dir_mode != new->ctx->dir_mode)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002504 return 0;
2505
2506 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2507 return 0;
2508
Steve French57804642021-02-24 12:12:53 -06002509 if (old->ctx->acregmax != new->ctx->acregmax)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002510 return 0;
Steve French4c9f9482021-02-23 15:50:57 -06002511 if (old->ctx->acdirmax != new->ctx->acdirmax)
2512 return 0;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002513
2514 return 1;
2515}
2516
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002517static int
2518match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2519{
2520 struct cifs_sb_info *old = CIFS_SB(sb);
2521 struct cifs_sb_info *new = mnt_data->cifs_sb;
Ronnie Sahlbergfe129262020-01-22 11:07:56 +10002522 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2523 old->prepath;
2524 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2525 new->prepath;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002526
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002527 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002528 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002529 else if (!old_set && !new_set)
2530 return 1;
2531
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002532 return 0;
2533}
2534
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002535int
2536cifs_match_super(struct super_block *sb, void *data)
2537{
2538 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002539 struct smb3_fs_context *ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002540 struct cifs_sb_info *cifs_sb;
2541 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00002542 struct cifs_ses *ses;
2543 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002544 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002545 int rc = 0;
2546
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002547 spin_lock(&cifs_tcp_ses_lock);
2548 cifs_sb = CIFS_SB(sb);
2549 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
Steve French9ed38fd2021-09-23 19:18:37 -05002550 if (tlink == NULL) {
2551 /* can not match superblock if tlink were ever null */
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002552 spin_unlock(&cifs_tcp_ses_lock);
Steve French9ed38fd2021-09-23 19:18:37 -05002553 return 0;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002554 }
2555 tcon = tlink_tcon(tlink);
2556 ses = tcon->ses;
2557 tcp_srv = ses->server;
2558
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002559 ctx = mnt_data->ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002560
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002561 if (!match_server(tcp_srv, ctx) ||
2562 !match_session(ses, ctx) ||
2563 !match_tcon(tcon, ctx) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002564 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002565 rc = 0;
2566 goto out;
2567 }
2568
2569 rc = compare_mount_options(sb, mnt_data);
2570out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002571 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04002572 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002573 return rc;
2574}
2575
Jeff Layton09e50d52008-07-23 10:11:19 -04002576#ifdef CONFIG_DEBUG_LOCK_ALLOC
2577static struct lock_class_key cifs_key[2];
2578static struct lock_class_key cifs_slock_key[2];
2579
2580static inline void
2581cifs_reclassify_socket4(struct socket *sock)
2582{
2583 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002584 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002585 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2586 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2587}
2588
2589static inline void
2590cifs_reclassify_socket6(struct socket *sock)
2591{
2592 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002593 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002594 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2595 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2596}
2597#else
2598static inline void
2599cifs_reclassify_socket4(struct socket *sock)
2600{
2601}
2602
2603static inline void
2604cifs_reclassify_socket6(struct socket *sock)
2605{
2606}
2607#endif
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00002610static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Steve French50c2f752007-07-13 00:33:32 +00002612 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Steve French50c2f752007-07-13 00:33:32 +00002614 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /* mask a nibble at a time and encode */
2616 target[j] = 'A' + (0x0F & (source[i] >> 4));
2617 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00002618 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620
2621}
2622
Ben Greear3eb9a882010-09-01 17:06:02 -07002623static int
2624bind_socket(struct TCP_Server_Info *server)
2625{
2626 int rc = 0;
2627 if (server->srcaddr.ss_family != AF_UNSPEC) {
2628 /* Bind to the specified local IP address */
2629 struct socket *socket = server->ssocket;
2630 rc = socket->ops->bind(socket,
2631 (struct sockaddr *) &server->srcaddr,
2632 sizeof(server->srcaddr));
2633 if (rc < 0) {
2634 struct sockaddr_in *saddr4;
2635 struct sockaddr_in6 *saddr6;
2636 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2637 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2638 if (saddr6->sin6_family == AF_INET6)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002639 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002640 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002641 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002642 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002643 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002644 }
2645 }
2646 return rc;
2647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002650ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
2652 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002653 /*
2654 * some servers require RFC1001 sessinit before sending
2655 * negprot - BB check reconnection in case where second
2656 * sessinit is sent but no second negprot
2657 */
2658 struct rfc1002_session_packet *ses_init_buf;
2659 struct smb_hdr *smb_buf;
2660 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2661 GFP_KERNEL);
2662 if (ses_init_buf) {
2663 ses_init_buf->trailer.session_req.called_len = 32;
2664
Colin Ian King997152f2016-01-25 16:25:54 +00002665 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002666 rfc1002mangle(ses_init_buf->trailer.
2667 session_req.called_name,
2668 server->server_RFC1001_name,
2669 RFC1001_NAME_LEN_WITH_NULL);
2670 else
2671 rfc1002mangle(ses_init_buf->trailer.
2672 session_req.called_name,
2673 DEFAULT_CIFS_CALLED_NAME,
2674 RFC1001_NAME_LEN_WITH_NULL);
2675
2676 ses_init_buf->trailer.session_req.calling_len = 32;
2677
2678 /*
2679 * calling name ends in null (byte 16) from old smb
2680 * convention.
2681 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05002682 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002683 rfc1002mangle(ses_init_buf->trailer.
2684 session_req.calling_name,
2685 server->workstation_RFC1001_name,
2686 RFC1001_NAME_LEN_WITH_NULL);
2687 else
2688 rfc1002mangle(ses_init_buf->trailer.
2689 session_req.calling_name,
2690 "LINUX_CIFS_CLNT",
2691 RFC1001_NAME_LEN_WITH_NULL);
2692
2693 ses_init_buf->trailer.session_req.scope1 = 0;
2694 ses_init_buf->trailer.session_req.scope2 = 0;
2695 smb_buf = (struct smb_hdr *)ses_init_buf;
2696
2697 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002698 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002699 rc = smb_send(server, smb_buf, 0x44);
2700 kfree(ses_init_buf);
2701 /*
2702 * RFC1001 layer in at least one server
2703 * requires very short break before negprot
2704 * presumably because not expecting negprot
2705 * to follow so fast. This is a simple
2706 * solution that works without
2707 * complicating the code and causes no
2708 * significant slowing down on mount
2709 * for everyone else
2710 */
2711 usleep_range(1000, 2000);
2712 }
2713 /*
2714 * else the negprot may still work without this
2715 * even though malloc failed
2716 */
2717
2718 return rc;
2719}
2720
2721static int
2722generic_ip_connect(struct TCP_Server_Info *server)
2723{
2724 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00002725 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002726 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002727 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002728 struct sockaddr *saddr;
2729
2730 saddr = (struct sockaddr *) &server->dstaddr;
2731
2732 if (server->dstaddr.ss_family == AF_INET6) {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002733 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2734
2735 sport = ipv6->sin6_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002736 slen = sizeof(struct sockaddr_in6);
2737 sfamily = AF_INET6;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002738 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2739 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002740 } else {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002741 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2742
2743 sport = ipv4->sin_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002744 slen = sizeof(struct sockaddr_in);
2745 sfamily = AF_INET;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002746 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2747 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002750 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06002751 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2752 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (rc < 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002754 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002755 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002758
2759 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05002760 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002761 server->ssocket = socket;
2762 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002763 if (sfamily == AF_INET6)
2764 cifs_reclassify_socket6(socket);
2765 else
2766 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768
Ben Greear3eb9a882010-09-01 17:06:02 -07002769 rc = bind_socket(server);
2770 if (rc < 0)
2771 return rc;
2772
Jeff Laytond5c56052008-12-01 18:42:33 -05002773 /*
2774 * Eventually check for other socket options to change from
2775 * the default. sock_setsockopt not used because it expects
2776 * user space buffer
2777 */
2778 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00002779 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00002780
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002781 /* make the bufsizes depend on wsize/rsize and max requests */
2782 if (server->noautotune) {
2783 if (socket->sk->sk_sndbuf < (200 * 1024))
2784 socket->sk->sk_sndbuf = 200 * 1024;
2785 if (socket->sk->sk_rcvbuf < (140 * 1024))
2786 socket->sk->sk_rcvbuf = 140 * 1024;
2787 }
2788
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02002789 if (server->tcp_nodelay)
2790 tcp_sock_set_nodelay(socket->sk);
Steve French6a5fa2362010-01-01 01:28:43 +00002791
Joe Perchesf96637b2013-05-04 22:12:25 -05002792 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002793 socket->sk->sk_sndbuf,
2794 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2795
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002796 rc = socket->ops->connect(socket, saddr, slen,
2797 server->noblockcnt ? O_NONBLOCK : 0);
Paulo Alcantara (SUSE)d532cc72019-10-10 12:31:58 -03002798 /*
2799 * When mounting SMB root file systems, we do not want to block in
2800 * connect. Otherwise bail out and then let cifs_reconnect() perform
2801 * reconnect failover - if possible.
2802 */
2803 if (server->noblockcnt && rc == -EINPROGRESS)
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002804 rc = 0;
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002805 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002806 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Steve Frenchd7171cd2021-11-04 15:56:37 -05002807 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002808 sock_release(socket);
2809 server->ssocket = NULL;
2810 return rc;
2811 }
Steve Frenchd7171cd2021-11-04 15:56:37 -05002812 trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002813 if (sport == htons(RFC1001_PORT))
2814 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00002815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return rc;
2817}
2818
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002819static int
2820ip_connect(struct TCP_Server_Info *server)
2821{
Steve French6da97912011-03-13 18:55:55 +00002822 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002823 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2824 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2825
2826 if (server->dstaddr.ss_family == AF_INET6)
2827 sport = &addr6->sin6_port;
2828 else
2829 sport = &addr->sin_port;
2830
2831 if (*sport == 0) {
2832 int rc;
2833
2834 /* try with 445 port at first */
2835 *sport = htons(CIFS_PORT);
2836
2837 rc = generic_ip_connect(server);
2838 if (rc >= 0)
2839 return rc;
2840
2841 /* if it failed, try with 139 port */
2842 *sport = htons(RFC1001_PORT);
2843 }
2844
2845 return generic_ip_connect(server);
2846}
2847
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002848void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002849 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Steve French8af18972007-02-14 04:42:51 +00002850{
Steve Frenchbc044992020-12-11 19:48:26 -06002851 /*
2852 * If we are reconnecting then should we check to see if
Steve French8af18972007-02-14 04:42:51 +00002853 * any requested capabilities changed locally e.g. via
2854 * remount but we can not do much about it here
2855 * if they have (even if we could detect it by the following)
2856 * Perhaps we could add a backpointer to array of sb from tcon
2857 * or if we change to make all sb to same share the same
2858 * sb as NFS - then we only have one backpointer to sb.
2859 * What if we wanted to mount the server share twice once with
Steve Frenchbc044992020-12-11 19:48:26 -06002860 * and once without posixacls or posix paths?
2861 */
Steve French8af18972007-02-14 04:42:51 +00002862 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00002863
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002864 if (ctx && ctx->no_linux_ext) {
Steve Frenchc18c8422007-07-18 23:21:09 +00002865 tcon->fsUnixInfo.Capability = 0;
2866 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05002867 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002868 return;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002869 } else if (ctx)
Steve Frenchc18c8422007-07-18 23:21:09 +00002870 tcon->unix_ext = 1; /* Unix Extensions supported */
2871
Jiapeng Zhong16a78852021-01-14 18:02:23 +08002872 if (!tcon->unix_ext) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002873 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002874 return;
2875 }
Steve French50c2f752007-07-13 00:33:32 +00002876
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002877 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00002878 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05002879 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve Frenchbc044992020-12-11 19:48:26 -06002880 /*
2881 * check for reconnect case in which we do not
2882 * want to change the mount behavior if we can avoid it
2883 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002884 if (ctx == NULL) {
Steve Frenchbc044992020-12-11 19:48:26 -06002885 /*
2886 * turn off POSIX ACL and PATHNAMES if not set
2887 * originally at mount time
2888 */
Steve French8af18972007-02-14 04:42:51 +00002889 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
2890 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002891 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
2892 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002893 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00002894 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002895 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002896 cifs_dbg(VFS, "possible reconnect error\n");
2897 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00002898 }
Steve French8af18972007-02-14 04:42:51 +00002899 }
Steve French50c2f752007-07-13 00:33:32 +00002900
Steve French6848b732011-05-26 18:38:54 +00002901 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002902 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00002903
Steve French8af18972007-02-14 04:42:51 +00002904 cap &= CIFS_UNIX_CAP_MASK;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002905 if (ctx && ctx->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00002906 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002907 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002908 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002909 if (cifs_sb)
2910 cifs_sb->mnt_cifs_flags |=
2911 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00002912 }
2913
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002914 if (ctx && ctx->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00002915 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002916 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002917 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002918 if (cifs_sb)
2919 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00002920 CIFS_MOUNT_POSIX_PATHS;
2921 }
Steve French50c2f752007-07-13 00:33:32 +00002922
Joe Perchesf96637b2013-05-04 22:12:25 -05002923 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00002924#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00002925 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002926 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002927 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002928 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002929 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002930 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002931 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002932 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002933 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002934 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002935 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002936 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002937 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002938 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00002939 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002940 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00002941 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002942 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00002943#endif /* CIFS_DEBUG2 */
2944 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002945 if (ctx == NULL)
Joe Perchesf96637b2013-05-04 22:12:25 -05002946 cifs_dbg(FYI, "resetting capabilities failed\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002947 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002948 cifs_dbg(VFS, "Negotiating Unix capabilities with the server failed. Consider mounting with the Unix Extensions disabled if problems are found by specifying the nounix mount option.\n");
Steve French5a44b312007-09-20 15:16:24 +00002949
Steve French8af18972007-02-14 04:42:51 +00002950 }
2951 }
2952}
2953
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002954int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002955{
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002956 struct smb3_fs_context *ctx = cifs_sb->ctx;
2957
Jeff Layton2de970f2010-10-06 19:51:12 -04002958 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
2959
Al Viro2ced6f62011-06-17 09:20:04 -04002960 spin_lock_init(&cifs_sb->tlink_tree_lock);
2961 cifs_sb->tlink_tree = RB_ROOT;
2962
Frank Sorensonf52aa792020-02-12 15:31:48 -06002963 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002964 ctx->file_mode, ctx->dir_mode);
Steve French3b795212008-11-13 19:45:32 +00002965
Ronnie Sahlberg387ec582020-12-14 16:40:20 +10002966 /* this is needed for ASCII cp to Unicode converts */
2967 if (ctx->iocharset == NULL) {
2968 /* load_nls_default cannot return null */
2969 cifs_sb->local_nls = load_nls_default();
2970 } else {
2971 cifs_sb->local_nls = load_nls(ctx->iocharset);
2972 if (cifs_sb->local_nls == NULL) {
2973 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
2974 ctx->iocharset);
2975 return -ELIBACC;
2976 }
2977 }
2978 ctx->local_nls = cifs_sb->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05302979
Ronnie Sahlberg2d39f502020-12-14 16:40:25 +10002980 smb3_update_mnt_flags(cifs_sb);
2981
2982 if (ctx->direct_io)
Joe Perchesf96637b2013-05-04 22:12:25 -05002983 cifs_dbg(FYI, "mounting share using direct i/o\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002984 if (ctx->cache_ro) {
Steve French83bbfa72019-08-27 23:58:54 -05002985 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
2986 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002987 } else if (ctx->cache_rw) {
Steve French41e033f2019-08-30 02:12:41 -05002988 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
2989 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
2990 CIFS_MOUNT_RW_CACHE);
Steve French83bbfa72019-08-27 23:58:54 -05002991 }
Steve French3b795212008-11-13 19:45:32 +00002992
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002993 if ((ctx->cifs_acl) && (ctx->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05002994 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002995
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002996 if (ctx->prepath) {
2997 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002998 if (cifs_sb->prepath == NULL)
2999 return -ENOMEM;
Shyam Prasad Na738c932021-02-11 03:26:54 -08003000 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003001 }
3002
3003 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003004}
3005
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003006/* Release all succeed connections */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003007static inline void mount_put_conns(struct mount_ctx *mnt_ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003008{
3009 int rc = 0;
3010
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003011 if (mnt_ctx->tcon)
3012 cifs_put_tcon(mnt_ctx->tcon);
3013 else if (mnt_ctx->ses)
3014 cifs_put_smb_ses(mnt_ctx->ses);
3015 else if (mnt_ctx->server)
3016 cifs_put_tcp_session(mnt_ctx->server, 0);
3017 mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3018 free_xid(mnt_ctx->xid);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003019}
3020
3021/* Get connections for tcp, ses and tcon */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003022static int mount_get_conns(struct mount_ctx *mnt_ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003023{
3024 int rc = 0;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003025 struct TCP_Server_Info *server = NULL;
3026 struct cifs_ses *ses = NULL;
3027 struct cifs_tcon *tcon = NULL;
3028 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3029 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3030 unsigned int xid;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003031
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003032 xid = get_xid();
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003033
3034 /* get a reference to a tcp session */
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00003035 server = cifs_get_tcp_session(ctx, NULL);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003036 if (IS_ERR(server)) {
3037 rc = PTR_ERR(server);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003038 server = NULL;
3039 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003040 }
3041
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003042 /* get a reference to a SMB session */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003043 ses = cifs_get_smb_ses(server, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003044 if (IS_ERR(ses)) {
3045 rc = PTR_ERR(ses);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003046 ses = NULL;
3047 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003048 }
3049
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003050 if ((ctx->persistent == true) && (!(ses->server->capabilities &
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003051 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003052 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003053 rc = -EOPNOTSUPP;
3054 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003055 }
3056
3057 /* search for existing tcon to this server share */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003058 tcon = cifs_get_tcon(ses, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003059 if (IS_ERR(tcon)) {
3060 rc = PTR_ERR(tcon);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003061 tcon = NULL;
3062 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003063 }
3064
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003065 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3066 if (tcon->posix_extensions)
3067 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3068
3069 /* tell server which Unix caps we support */
3070 if (cap_unix(tcon->ses)) {
3071 /*
3072 * reset of caps checks mount to see if unix extensions disabled
3073 * for just this mount.
3074 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003075 reset_cifs_unix_caps(xid, tcon, cifs_sb, ctx);
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00003076 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003077 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3078 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003079 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00003080 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003081 rc = -EACCES;
3082 goto out;
3083 }
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00003084 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003085 } else
3086 tcon->unix_ext = 0; /* server does not support them */
3087
3088 /* do not care if a following call succeed - informational */
Steve French1981eba2019-08-29 22:33:38 -05003089 if (!tcon->pipe && server->ops->qfs_tcon) {
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003090 server->ops->qfs_tcon(xid, tcon, cifs_sb);
Steve French1981eba2019-08-29 22:33:38 -05003091 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
3092 if (tcon->fsDevInfo.DeviceCharacteristics &
Steve French52870d52019-10-01 21:25:46 -05003093 cpu_to_le32(FILE_READ_ONLY_DEVICE))
Steve French1981eba2019-08-29 22:33:38 -05003094 cifs_dbg(VFS, "mounted to read only share\n");
Steve French41e033f2019-08-30 02:12:41 -05003095 else if ((cifs_sb->mnt_cifs_flags &
3096 CIFS_MOUNT_RW_CACHE) == 0)
Steve French1981eba2019-08-29 22:33:38 -05003097 cifs_dbg(VFS, "read only mount of RW share\n");
Steve French41e033f2019-08-30 02:12:41 -05003098 /* no need to log a RW mount of a typical RW share */
Steve French1981eba2019-08-29 22:33:38 -05003099 }
3100 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003101
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003102 /*
3103 * Clamp the rsize/wsize mount arguments if they are too big for the server
Steve French0c2b5f72020-12-15 13:28:50 -06003104 * and set the rsize/wsize to the negotiated values if not passed in by
3105 * the user on mount
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003106 */
Steve French0c2b5f72020-12-15 13:28:50 -06003107 if ((cifs_sb->ctx->wsize == 0) ||
3108 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003109 cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
Steve French0c2b5f72020-12-15 13:28:50 -06003110 if ((cifs_sb->ctx->rsize == 0) ||
3111 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003112 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003113
Shyam Prasad Nb7743022021-12-08 16:33:19 +00003114 /*
3115 * The cookie is initialized from volume info returned above.
3116 * Inside cifs_fscache_get_super_cookie it checks
3117 * that we do not get super cookie twice.
3118 */
David Howells70431bf2020-11-17 15:56:59 +00003119 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
3120 cifs_fscache_get_super_cookie(tcon);
Shyam Prasad Nb7743022021-12-08 16:33:19 +00003121
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003122out:
3123 mnt_ctx->server = server;
3124 mnt_ctx->ses = ses;
3125 mnt_ctx->tcon = tcon;
3126 mnt_ctx->xid = xid;
3127
3128 return rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003129}
3130
3131static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3132 struct cifs_tcon *tcon)
3133{
3134 struct tcon_link *tlink;
3135
3136 /* hang the tcon off of the superblock */
3137 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3138 if (tlink == NULL)
3139 return -ENOMEM;
3140
3141 tlink->tl_uid = ses->linux_uid;
3142 tlink->tl_tcon = tcon;
3143 tlink->tl_time = jiffies;
3144 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3145 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3146
3147 cifs_sb->master_tlink = tlink;
3148 spin_lock(&cifs_sb->tlink_tree_lock);
3149 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3150 spin_unlock(&cifs_sb->tlink_tree_lock);
3151
3152 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3153 TLINK_IDLE_EXPIRE);
3154 return 0;
3155}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003156
Steve French2d6d5892009-04-09 00:36:44 +00003157#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003158/* Get unique dfs connections */
3159static int mount_get_dfs_conns(struct mount_ctx *mnt_ctx)
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003160{
3161 int rc;
3162
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003163 mnt_ctx->fs_ctx->nosharesock = true;
3164 rc = mount_get_conns(mnt_ctx);
3165 if (mnt_ctx->server) {
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003166 cifs_dbg(FYI, "%s: marking tcp session as a dfs connection\n", __func__);
3167 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003168 mnt_ctx->server->is_dfs_conn = true;
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003169 spin_unlock(&cifs_tcp_ses_lock);
3170 }
3171 return rc;
3172}
3173
Steve French6d3ea7e2012-11-28 22:34:41 -06003174/*
3175 * cifs_build_path_to_root returns full path to root when we do not have an
Steve French201023c2021-02-15 11:03:45 -06003176 * existing connection (tcon)
Steve French6d3ea7e2012-11-28 22:34:41 -06003177 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003178static char *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003179build_unc_path_to_root(const struct smb3_fs_context *ctx,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003180 const struct cifs_sb_info *cifs_sb, bool useppath)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003181{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003182 char *full_path, *pos;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003183 unsigned int pplen = useppath && ctx->prepath ?
3184 strlen(ctx->prepath) + 1 : 0;
3185 unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003186
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003187 if (unc_len > MAX_TREE_SIZE)
3188 return ERR_PTR(-EINVAL);
3189
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003190 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003191 if (full_path == NULL)
3192 return ERR_PTR(-ENOMEM);
3193
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003194 memcpy(full_path, ctx->UNC, unc_len);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003195 pos = full_path + unc_len;
3196
3197 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003198 *pos = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003199 memcpy(pos + 1, ctx->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003200 pos += pplen;
3201 }
3202
3203 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003204 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003205 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003206 return full_path;
3207}
Sean Finneydd613942011-04-11 13:19:30 +00003208
Steve French4c51de12021-09-13 18:29:46 -05003209/*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003210 * expand_dfs_referral - Update cifs_sb from dfs referral path
Paulo Alcantara1c780222018-11-14 16:24:03 -02003211 *
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003212 * cifs_sb->ctx->mount_options will be (re-)allocated to a string containing updated options for the
3213 * submount. Otherwise it will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003214 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003215static int expand_dfs_referral(struct mount_ctx *mnt_ctx, const char *full_path,
3216 struct dfs_info3_param *referral)
Sean Finneydd613942011-04-11 13:19:30 +00003217{
3218 int rc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003219 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3220 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3221 char *fake_devname = NULL, *mdata = NULL;
Sean Finneydd613942011-04-11 13:19:30 +00003222
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003223 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, referral,
3224 &fake_devname);
3225 if (IS_ERR(mdata)) {
3226 rc = PTR_ERR(mdata);
3227 mdata = NULL;
3228 } else {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003229 /*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003230 * We can not clear out the whole structure since we no longer have an explicit
3231 * function to parse a mount-string. Instead we need to clear out the individual
3232 * fields that are no longer valid.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003233 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003234 kfree(ctx->prepath);
3235 ctx->prepath = NULL;
3236 rc = cifs_setup_volume_info(ctx, mdata, fake_devname);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003237 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003238 kfree(fake_devname);
3239 kfree(cifs_sb->ctx->mount_options);
3240 cifs_sb->ctx->mount_options = mdata;
Paulo Alcantara85132222021-02-24 20:59:21 -03003241
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003242 return rc;
3243}
Steve French2d6d5892009-04-09 00:36:44 +00003244#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003245
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -06003246/* TODO: all callers to this are broken. We are not parsing mount_options here
3247 * we should pass a clone of the original context?
3248 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003249int
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003250cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003252 int rc;
Sean Finneydd613942011-04-11 13:19:30 +00003253
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003254 if (devname) {
3255 cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
3256 rc = smb3_parse_devname(devname, ctx);
3257 if (rc) {
3258 cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
3259 return rc;
3260 }
3261 }
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003262
3263 if (mntopts) {
3264 char *ip;
3265
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003266 rc = smb3_parse_opt(mntopts, "ip", &ip);
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003267 if (rc) {
3268 cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3269 return rc;
3270 }
3271
3272 rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3273 kfree(ip);
3274 if (!rc) {
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003275 cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
3276 return -EINVAL;
3277 }
3278 }
3279
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003280 if (ctx->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003281 cifs_dbg(FYI, "Anonymous login\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003282 kfree(ctx->username);
3283 ctx->username = NULL;
3284 } else if (ctx->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 /* BB fixme parse for domain name here */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003286 cifs_dbg(FYI, "Username: %s\n", ctx->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003288 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003289 /* In userspace mount helper we can get user name from alternate
3290 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003291 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 }
3293
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003294 return 0;
Jeff Layton04db79b2011-07-06 08:10:38 -04003295}
3296
Aurelien Aptela6b50582016-05-25 19:59:09 +02003297static int
3298cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3299 unsigned int xid,
3300 struct cifs_tcon *tcon,
3301 struct cifs_sb_info *cifs_sb,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003302 char *full_path,
3303 int added_treename)
Aurelien Aptela6b50582016-05-25 19:59:09 +02003304{
3305 int rc;
3306 char *s;
3307 char sep, tmp;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003308 int skip = added_treename ? 1 : 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +02003309
3310 sep = CIFS_DIR_SEP(cifs_sb);
3311 s = full_path;
3312
3313 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3314 while (rc == 0) {
3315 /* skip separators */
3316 while (*s == sep)
3317 s++;
3318 if (!*s)
3319 break;
3320 /* next separator */
3321 while (*s && *s != sep)
3322 s++;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003323 /*
3324 * if the treename is added, we then have to skip the first
3325 * part within the separators
3326 */
3327 if (skip) {
3328 skip = 0;
3329 continue;
3330 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02003331 /*
3332 * temporarily null-terminate the path at the end of
3333 * the current component
3334 */
3335 tmp = *s;
3336 *s = 0;
3337 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3338 full_path);
3339 *s = tmp;
3340 }
3341 return rc;
3342}
3343
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003344/*
3345 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
3346 * otherwise 0.
3347 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003348static int is_path_remote(struct mount_ctx *mnt_ctx)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003349{
Jeff Layton1daaae82012-03-21 06:30:40 -04003350 int rc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003351 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3352 struct TCP_Server_Info *server = mnt_ctx->server;
3353 unsigned int xid = mnt_ctx->xid;
3354 struct cifs_tcon *tcon = mnt_ctx->tcon;
3355 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003356 char *full_path;
3357
3358 if (!server->ops->is_path_accessible)
3359 return -EOPNOTSUPP;
3360
3361 /*
3362 * cifs_build_path_to_root works only when we have a valid tcon
3363 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003364 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003365 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3366 if (full_path == NULL)
3367 return -ENOMEM;
3368
3369 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3370
3371 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3372 full_path);
Eugene Korenevskya2809d02022-01-14 22:53:40 +03003373#ifdef CONFIG_CIFS_DFS_UPCALL
3374 if (rc == -ENOENT && is_tcon_dfs(tcon))
3375 rc = cifs_dfs_query_info_nonascii_quirk(xid, tcon, cifs_sb,
3376 full_path);
3377#endif
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003378 if (rc != 0 && rc != -EREMOTE) {
3379 kfree(full_path);
3380 return rc;
3381 }
3382
3383 if (rc != -EREMOTE) {
3384 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003385 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003386 if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07003387 cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003388 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3389 rc = 0;
3390 }
3391 }
3392
3393 kfree(full_path);
3394 return rc;
3395}
3396
3397#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003398static void set_root_ses(struct mount_ctx *mnt_ctx)
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03003399{
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003400 if (mnt_ctx->ses) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003401 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003402 mnt_ctx->ses->ses_count++;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003403 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003404 dfs_cache_add_refsrv_session(&mnt_ctx->mount_id, mnt_ctx->ses);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003405 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003406 mnt_ctx->root_ses = mnt_ctx->ses;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003407}
3408
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003409static int is_dfs_mount(struct mount_ctx *mnt_ctx, bool *isdfs, struct dfs_cache_tgt_list *root_tl)
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003410{
3411 int rc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003412 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3413 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003414
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003415 *isdfs = true;
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003416
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003417 rc = mount_get_conns(mnt_ctx);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003418 /*
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003419 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
3420 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003421 *
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003422 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
3423 * to respond with PATH_NOT_COVERED to requests that include the prefix.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003424 */
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003425 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003426 dfs_cache_find(mnt_ctx->xid, mnt_ctx->ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
3427 ctx->UNC + 1, NULL, root_tl)) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003428 if (rc)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003429 return rc;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003430 /* Check if it is fully accessible and then mount it */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003431 rc = is_path_remote(mnt_ctx);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003432 if (!rc)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003433 *isdfs = false;
3434 else if (rc != -EREMOTE)
3435 return rc;
3436 }
3437 return 0;
3438}
3439
3440static int connect_dfs_target(struct mount_ctx *mnt_ctx, const char *full_path,
3441 const char *ref_path, struct dfs_cache_tgt_iterator *tit)
3442{
3443 int rc;
3444 struct dfs_info3_param ref = {};
3445 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3446 char *oldmnt = cifs_sb->ctx->mount_options;
3447
3448 rc = dfs_cache_get_tgt_referral(ref_path, tit, &ref);
3449 if (rc)
3450 goto out;
3451
3452 rc = expand_dfs_referral(mnt_ctx, full_path, &ref);
3453 if (rc)
3454 goto out;
3455
3456 /* Connect to new target only if we were redirected (e.g. mount options changed) */
3457 if (oldmnt != cifs_sb->ctx->mount_options) {
3458 mount_put_conns(mnt_ctx);
3459 rc = mount_get_dfs_conns(mnt_ctx);
3460 }
3461 if (!rc) {
3462 if (cifs_is_referral_server(mnt_ctx->tcon, &ref))
3463 set_root_ses(mnt_ctx);
3464 rc = dfs_cache_update_tgthint(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3465 cifs_remap(cifs_sb), ref_path, tit);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003466 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003467
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003468out:
3469 free_dfs_info_param(&ref);
3470 return rc;
3471}
Paulo Alcantaraf3c852b2021-06-04 19:25:33 -03003472
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003473static int connect_dfs_root(struct mount_ctx *mnt_ctx, struct dfs_cache_tgt_list *root_tl)
3474{
3475 int rc;
3476 char *full_path;
3477 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3478 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3479 struct dfs_cache_tgt_iterator *tit;
3480
3481 /* Put initial connections as they might be shared with other mounts. We need unique dfs
3482 * connections per mount to properly failover, so mount_get_dfs_conns() must be used from
3483 * now on.
3484 */
3485 mount_put_conns(mnt_ctx);
3486 mount_get_dfs_conns(mnt_ctx);
Paulo Alcantara65de2622021-12-02 15:29:35 -03003487 set_root_ses(mnt_ctx);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003488
3489 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3490 if (IS_ERR(full_path))
3491 return PTR_ERR(full_path);
3492
3493 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(ctx->UNC, cifs_sb->local_nls,
3494 cifs_remap(cifs_sb));
3495 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3496 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3497 mnt_ctx->origin_fullpath = NULL;
3498 goto out;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003499 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003500
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003501 /* Try all dfs root targets */
3502 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(root_tl);
3503 tit; tit = dfs_cache_get_next_tgt(root_tl, tit)) {
3504 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->origin_fullpath + 1, tit);
3505 if (!rc) {
3506 mnt_ctx->leaf_fullpath = kstrdup(mnt_ctx->origin_fullpath, GFP_KERNEL);
3507 if (!mnt_ctx->leaf_fullpath)
3508 rc = -ENOMEM;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003509 break;
3510 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003511 }
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003512
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003513out:
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003514 kfree(full_path);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003515 return rc;
3516}
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003517
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003518static int __follow_dfs_link(struct mount_ctx *mnt_ctx)
3519{
3520 int rc;
3521 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3522 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3523 char *full_path;
3524 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3525 struct dfs_cache_tgt_iterator *tit;
3526
3527 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3528 if (IS_ERR(full_path))
3529 return PTR_ERR(full_path);
3530
3531 kfree(mnt_ctx->leaf_fullpath);
3532 mnt_ctx->leaf_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3533 cifs_remap(cifs_sb));
3534 if (IS_ERR(mnt_ctx->leaf_fullpath)) {
3535 rc = PTR_ERR(mnt_ctx->leaf_fullpath);
3536 mnt_ctx->leaf_fullpath = NULL;
3537 goto out;
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003538 }
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003539
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003540 /* Get referral from dfs link */
3541 rc = dfs_cache_find(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3542 cifs_remap(cifs_sb), mnt_ctx->leaf_fullpath + 1, NULL, &tl);
3543 if (rc)
3544 goto out;
3545
3546 /* Try all dfs link targets */
3547 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(&tl);
3548 tit; tit = dfs_cache_get_next_tgt(&tl, tit)) {
3549 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->leaf_fullpath + 1, tit);
3550 if (!rc) {
3551 rc = is_path_remote(mnt_ctx);
3552 break;
3553 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003554 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003555
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003556out:
3557 kfree(full_path);
3558 dfs_cache_free_tgts(&tl);
3559 return rc;
3560}
3561
3562static int follow_dfs_link(struct mount_ctx *mnt_ctx)
3563{
3564 int rc;
3565 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3566 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3567 char *full_path;
3568 int num_links = 0;
3569
3570 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3571 if (IS_ERR(full_path))
3572 return PTR_ERR(full_path);
3573
3574 kfree(mnt_ctx->origin_fullpath);
3575 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3576 cifs_remap(cifs_sb));
3577 kfree(full_path);
3578
3579 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3580 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3581 mnt_ctx->origin_fullpath = NULL;
3582 return rc;
3583 }
3584
3585 do {
3586 rc = __follow_dfs_link(mnt_ctx);
3587 if (!rc || rc != -EREMOTE)
3588 break;
3589 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
3590
3591 return rc;
3592}
3593
3594/* Set up DFS referral paths for failover */
3595static void setup_server_referral_paths(struct mount_ctx *mnt_ctx)
3596{
3597 struct TCP_Server_Info *server = mnt_ctx->server;
3598
3599 server->origin_fullpath = mnt_ctx->origin_fullpath;
3600 server->leaf_fullpath = mnt_ctx->leaf_fullpath;
3601 server->current_fullpath = mnt_ctx->leaf_fullpath;
3602 mnt_ctx->origin_fullpath = mnt_ctx->leaf_fullpath = NULL;
3603}
3604
3605int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3606{
3607 int rc;
3608 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3609 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3610 bool isdfs;
3611
3612 rc = is_dfs_mount(&mnt_ctx, &isdfs, &tl);
3613 if (rc)
3614 goto error;
3615 if (!isdfs)
3616 goto out;
3617
3618 uuid_gen(&mnt_ctx.mount_id);
3619 rc = connect_dfs_root(&mnt_ctx, &tl);
3620 dfs_cache_free_tgts(&tl);
3621
3622 if (rc)
3623 goto error;
3624
3625 rc = is_path_remote(&mnt_ctx);
3626 if (rc == -EREMOTE)
3627 rc = follow_dfs_link(&mnt_ctx);
3628 if (rc)
3629 goto error;
3630
3631 setup_server_referral_paths(&mnt_ctx);
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01003632 /*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003633 * After reconnecting to a different server, unique ids won't match anymore, so we disable
3634 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01003635 */
3636 cifs_autodisable_serverino(cifs_sb);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003637 /*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003638 * Force the use of prefix path to support failover on DFS paths that resolve to targets
3639 * that have different prefix paths.
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003640 */
3641 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3642 kfree(cifs_sb->prepath);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003643 cifs_sb->prepath = ctx->prepath;
3644 ctx->prepath = NULL;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003645 uuid_copy(&cifs_sb->dfs_mount_id, &mnt_ctx.mount_id);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003646
Jeff Layton70fe7dc2007-11-16 22:21:07 +00003647out:
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003648 free_xid(mnt_ctx.xid);
3649 cifs_try_adding_channels(cifs_sb, mnt_ctx.ses);
3650 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003651
3652error:
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003653 dfs_cache_put_refsrv_sessions(&mnt_ctx.mount_id);
3654 kfree(mnt_ctx.origin_fullpath);
3655 kfree(mnt_ctx.leaf_fullpath);
3656 mount_put_conns(&mnt_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 return rc;
3658}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003659#else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003660int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003661{
3662 int rc = 0;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003663 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003664
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003665 rc = mount_get_conns(&mnt_ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003666 if (rc)
3667 goto error;
3668
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003669 if (mnt_ctx.tcon) {
3670 rc = is_path_remote(&mnt_ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003671 if (rc == -EREMOTE)
3672 rc = -EOPNOTSUPP;
3673 if (rc)
3674 goto error;
3675 }
3676
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003677 free_xid(mnt_ctx.xid);
3678 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003679
3680error:
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003681 mount_put_conns(&mnt_ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003682 return rc;
3683}
3684#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Jeff Layton8d1bca32011-06-11 21:17:10 -04003686/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01003687 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04003688 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003690CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00003691 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 const struct nls_table *nls_codepage)
3693{
3694 struct smb_hdr *smb_buffer;
3695 struct smb_hdr *smb_buffer_response;
3696 TCONX_REQ *pSMB;
3697 TCONX_RSP *pSMBr;
3698 unsigned char *bcc_ptr;
3699 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05003700 int length;
3701 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
3703 if (ses == NULL)
3704 return -EIO;
3705
3706 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00003707 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00003709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 smb_buffer_response = smb_buffer;
3711
3712 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3713 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003714
Pavel Shilovsky88257362012-05-23 14:01:59 +04003715 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 smb_buffer->Uid = ses->Suid;
3717 pSMB = (TCONX_REQ *) smb_buffer;
3718 pSMBr = (TCONX_RSP *) smb_buffer_response;
3719
3720 pSMB->AndXCommand = 0xFF;
3721 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01003723 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08003724 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003725 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003726 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003727 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Jeff Layton38d77c52013-05-26 07:01:00 -04003730 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3732
3733 if (ses->capabilities & CAP_STATUS32) {
3734 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3735 }
3736 if (ses->capabilities & CAP_DFS) {
3737 smb_buffer->Flags2 |= SMBFLG2_DFS;
3738 }
3739 if (ses->capabilities & CAP_UNICODE) {
3740 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3741 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06003742 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00003743 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00003744 (/* server len*/ + 256 /* share len */), nls_codepage);
3745 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 bcc_ptr += 2; /* skip trailing null */
3747 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 strcpy(bcc_ptr, tree);
3749 bcc_ptr += strlen(tree) + 1;
3750 }
3751 strcpy(bcc_ptr, "?????");
3752 bcc_ptr += strlen("?????");
3753 bcc_ptr += 1;
3754 count = bcc_ptr - &pSMB->Password[0];
Qinglang Miao1a0e7f72020-07-25 16:56:01 +08003755 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 pSMB->ByteCount = cpu_to_le16(count);
3757
Steve French133672e2007-11-13 22:41:37 +00003758 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05003759 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003762 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00003763 bool is_unicode;
3764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 tcon->tid = smb_buffer_response->Tid;
3766 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05003767 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003768 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00003769 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3770 is_unicode = true;
3771 else
3772 is_unicode = false;
3773
Jeff Laytoncc20c032009-04-30 07:16:21 -04003774
Steve French50c2f752007-07-13 00:33:32 +00003775 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00003776 if (length == 3) {
3777 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3778 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003779 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01003780 tcon->ipc = true;
3781 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00003782 }
3783 } else if (length == 2) {
3784 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3785 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05003786 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00003787 }
3788 }
Steve French50c2f752007-07-13 00:33:32 +00003789 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04003790 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05003791 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04003792
3793 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04003794 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06003795 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00003796 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04003797 nls_codepage);
3798
Joe Perchesf96637b2013-05-04 22:12:25 -05003799 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003800
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003801 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00003802 (smb_buffer_response->WordCount == 7))
3803 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00003804 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3805 else
3806 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05003807 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
3809
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003810 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return rc;
3812}
3813
Al Viro2e32cf52013-10-03 12:53:37 -04003814static void delayed_free(struct rcu_head *p)
3815{
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003816 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
3817
3818 unload_nls(cifs_sb->local_nls);
Ronnie Sahlbergc741cba2020-12-14 16:40:16 +10003819 smb3_cleanup_fs_context(cifs_sb->ctx);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003820 kfree(cifs_sb);
Al Viro2e32cf52013-10-03 12:53:37 -04003821}
3822
Al Viro2a9b9952011-06-17 09:27:16 -04003823void
3824cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
Jeff Laytonb647c352010-10-28 11:16:44 -04003826 struct rb_root *root = &cifs_sb->tlink_tree;
3827 struct rb_node *node;
3828 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Jeff Layton2de970f2010-10-06 19:51:12 -04003830 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3831
Jeff Laytonb647c352010-10-28 11:16:44 -04003832 spin_lock(&cifs_sb->tlink_tree_lock);
3833 while ((node = rb_first(root))) {
3834 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3835 cifs_get_tlink(tlink);
3836 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3837 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00003838
Jeff Laytonb647c352010-10-28 11:16:44 -04003839 spin_unlock(&cifs_sb->tlink_tree_lock);
3840 cifs_put_tlink(tlink);
3841 spin_lock(&cifs_sb->tlink_tree_lock);
3842 }
3843 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003844
Aurelien Aptela6b50582016-05-25 19:59:09 +02003845 kfree(cifs_sb->prepath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003846#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003847 dfs_cache_put_refsrv_sessions(&cifs_sb->dfs_mount_id);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003848#endif
Al Viro2e32cf52013-10-03 12:53:37 -04003849 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00003850}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003852int
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00003853cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
3854 struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
3856 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003858 if (!server->ops->need_neg || !server->ops->negotiate)
3859 return -ENOSYS;
3860
Jeff Layton198b5682010-04-24 07:57:48 -04003861 /* only send once per connect */
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00003862 spin_lock(&cifs_tcp_ses_lock);
3863 if (!server->ops->need_neg(server) ||
3864 server->tcpStatus != CifsNeedNegotiate) {
3865 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton198b5682010-04-24 07:57:48 -04003866 return 0;
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00003867 }
3868 server->tcpStatus = CifsInNegotiate;
3869 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00003871 rc = server->ops->negotiate(xid, ses, server);
Jeff Layton198b5682010-04-24 07:57:48 -04003872 if (rc == 0) {
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00003873 spin_lock(&cifs_tcp_ses_lock);
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00003874 if (server->tcpStatus == CifsInNegotiate)
3875 server->tcpStatus = CifsNeedSessSetup;
Jeff Layton198b5682010-04-24 07:57:48 -04003876 else
3877 rc = -EHOSTDOWN;
Shyam Prasad N080dc5e2021-07-19 17:05:53 +00003878 spin_unlock(&cifs_tcp_ses_lock);
Shyam Prasad Na05885c2021-11-17 15:57:22 +00003879 } else {
3880 spin_lock(&cifs_tcp_ses_lock);
3881 if (server->tcpStatus == CifsInNegotiate)
3882 server->tcpStatus = CifsNeedNegotiate;
3883 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 }
Steve French26b994f2008-08-06 05:11:33 +00003885
Jeff Layton198b5682010-04-24 07:57:48 -04003886 return rc;
3887}
Steve French26b994f2008-08-06 05:11:33 +00003888
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003889int
3890cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00003891 struct TCP_Server_Info *server,
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003892 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04003893{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003894 int rc = -ENOSYS;
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00003895 bool is_binding = false;
Jeff Layton198b5682010-04-24 07:57:48 -04003896
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00003897 /* only send once per connect */
3898 spin_lock(&cifs_tcp_ses_lock);
3899 if (server->tcpStatus != CifsNeedSessSetup) {
3900 spin_unlock(&cifs_tcp_ses_lock);
3901 return 0;
3902 }
Shyam Prasad N47de7602022-01-18 09:24:08 +00003903 server->tcpStatus = CifsInSessSetup;
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00003904 spin_unlock(&cifs_tcp_ses_lock);
3905
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00003906 spin_lock(&ses->chan_lock);
3907 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
3908 spin_unlock(&ses->chan_lock);
3909
3910 if (!is_binding) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003911 ses->capabilities = server->capabilities;
YANG LIed6b1922021-01-11 17:15:28 +08003912 if (!linuxExtEnabled)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003913 ses->capabilities &= (~server->vals->cap_unix);
3914
3915 if (ses->auth_key.response) {
3916 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
3917 ses->auth_key.response);
3918 kfree(ses->auth_key.response);
3919 ses->auth_key.response = NULL;
3920 ses->auth_key.len = 0;
3921 }
3922 }
Steve French20418ac2009-04-30 16:13:32 +00003923
Joe Perchesf96637b2013-05-04 22:12:25 -05003924 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00003925 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003926
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003927 if (server->ops->sess_setup)
Shyam Prasad Nf486ef82021-07-19 13:54:16 +00003928 rc = server->ops->sess_setup(xid, ses, server, nls_info);
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003929
Shyam Prasad Na05885c2021-11-17 15:57:22 +00003930 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003931 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shyam Prasad Na05885c2021-11-17 15:57:22 +00003932 spin_lock(&cifs_tcp_ses_lock);
3933 if (server->tcpStatus == CifsInSessSetup)
3934 server->tcpStatus = CifsNeedSessSetup;
3935 spin_unlock(&cifs_tcp_ses_lock);
3936 } else {
3937 spin_lock(&cifs_tcp_ses_lock);
3938 if (server->tcpStatus == CifsInSessSetup)
3939 server->tcpStatus = CifsGood;
Shyam Prasad Nc1604da2022-01-16 13:38:14 +00003940 /* Even if one channel is active, session is in good state */
Shyam Prasad N47de7602022-01-18 09:24:08 +00003941 ses->status = CifsGood;
Shyam Prasad Na05885c2021-11-17 15:57:22 +00003942 spin_unlock(&cifs_tcp_ses_lock);
Shyam Prasad Nc1604da2022-01-16 13:38:14 +00003943
3944 spin_lock(&ses->chan_lock);
3945 cifs_chan_clear_need_reconnect(ses, server);
3946 spin_unlock(&ses->chan_lock);
Shyam Prasad Na05885c2021-11-17 15:57:22 +00003947 }
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05003948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 return rc;
3950}
3951
Jeff Layton8a8798a2012-01-17 16:09:15 -05003952static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003953cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003954{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003955 ctx->sectype = ses->sectype;
Jeff Layton3f618222013-06-12 19:52:14 -05003956
3957 /* krb5 is special, since we don't need username or pw */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003958 if (ctx->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003959 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003960
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003961 return cifs_set_cifscreds(ctx, ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003962}
3963
Steve French96daf2b2011-05-27 04:34:02 +00003964static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003965cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04003966{
Jeff Layton8a8798a2012-01-17 16:09:15 -05003967 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00003968 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3969 struct cifs_ses *ses;
3970 struct cifs_tcon *tcon = NULL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003971 struct smb3_fs_context *ctx;
Jeff Layton9d002df2010-10-06 19:51:11 -04003972
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003973 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3974 if (ctx == NULL)
Dan Carpenter803ab972012-01-24 11:39:22 +03003975 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04003976
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003977 ctx->local_nls = cifs_sb->local_nls;
3978 ctx->linux_uid = fsuid;
3979 ctx->cred_uid = fsuid;
3980 ctx->UNC = master_tcon->treeName;
3981 ctx->retry = master_tcon->retry;
3982 ctx->nocase = master_tcon->nocase;
3983 ctx->nohandlecache = master_tcon->nohandlecache;
3984 ctx->local_lease = master_tcon->local_lease;
3985 ctx->no_lease = master_tcon->no_lease;
3986 ctx->resilient = master_tcon->use_resilient;
3987 ctx->persistent = master_tcon->use_persistent;
3988 ctx->handle_timeout = master_tcon->handle_timeout;
3989 ctx->no_linux_ext = !master_tcon->unix_ext;
3990 ctx->linux_ext = master_tcon->posix_extensions;
3991 ctx->sectype = master_tcon->ses->sectype;
3992 ctx->sign = master_tcon->ses->sign;
3993 ctx->seal = master_tcon->seal;
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06003994 ctx->witness = master_tcon->use_witness;
Jeff Layton9d002df2010-10-06 19:51:11 -04003995
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003996 rc = cifs_set_vol_auth(ctx, master_tcon->ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003997 if (rc) {
3998 tcon = ERR_PTR(rc);
3999 goto out;
4000 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004001
4002 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304003 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004004 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304005 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004006
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06004007 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04004008 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004009 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004010 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004011 goto out;
4012 }
4013
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06004014 tcon = cifs_get_tcon(ses, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04004015 if (IS_ERR(tcon)) {
4016 cifs_put_smb_ses(ses);
4017 goto out;
4018 }
4019
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004020 if (cap_unix(ses))
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06004021 reset_cifs_unix_caps(0, tcon, NULL, ctx);
Steve Frenchb3266142018-05-20 23:41:10 -05004022
Jeff Layton9d002df2010-10-06 19:51:11 -04004023out:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06004024 kfree(ctx->username);
4025 kfree_sensitive(ctx->password);
4026 kfree(ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04004027
4028 return tcon;
4029}
4030
Steve French96daf2b2011-05-27 04:34:02 +00004031struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004032cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4033{
4034 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4035}
4036
Jeff Laytonb647c352010-10-28 11:16:44 -04004037/* find and return a tlink with given uid */
4038static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004039tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004040{
4041 struct rb_node *node = root->rb_node;
4042 struct tcon_link *tlink;
4043
4044 while (node) {
4045 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4046
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004047 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004048 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004049 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004050 node = node->rb_right;
4051 else
4052 return tlink;
4053 }
4054 return NULL;
4055}
4056
4057/* insert a tcon_link into the tree */
4058static void
4059tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4060{
4061 struct rb_node **new = &(root->rb_node), *parent = NULL;
4062 struct tcon_link *tlink;
4063
4064 while (*new) {
4065 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4066 parent = *new;
4067
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004068 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004069 new = &((*new)->rb_left);
4070 else
4071 new = &((*new)->rb_right);
4072 }
4073
4074 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4075 rb_insert_color(&new_tlink->tl_rbnode, root);
4076}
4077
Jeff Layton9d002df2010-10-06 19:51:11 -04004078/*
4079 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4080 * current task.
4081 *
4082 * If the superblock doesn't refer to a multiuser mount, then just return
4083 * the master tcon for the mount.
4084 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304085 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004086 * exists, then check to see if it's pending construction. If it is then wait
4087 * for construction to complete. Once it's no longer pending, check to see if
4088 * it failed and either return an error or retry construction, depending on
4089 * the timeout.
4090 *
4091 * If one doesn't exist then insert a new tcon_link struct into the tree and
4092 * try to construct a new one.
4093 */
4094struct tcon_link *
4095cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4096{
4097 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004098 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004099 struct tcon_link *tlink, *newtlink;
4100
4101 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4102 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4103
4104 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004105 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004106 if (tlink)
4107 cifs_get_tlink(tlink);
4108 spin_unlock(&cifs_sb->tlink_tree_lock);
4109
4110 if (tlink == NULL) {
4111 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4112 if (newtlink == NULL)
4113 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004114 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004115 newtlink->tl_tcon = ERR_PTR(-EACCES);
4116 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4117 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4118 cifs_get_tlink(newtlink);
4119
Jeff Layton9d002df2010-10-06 19:51:11 -04004120 spin_lock(&cifs_sb->tlink_tree_lock);
4121 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004122 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004123 if (tlink) {
4124 cifs_get_tlink(tlink);
4125 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004126 kfree(newtlink);
4127 goto wait_for_construction;
4128 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004129 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004130 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4131 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004132 } else {
4133wait_for_construction:
4134 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004135 TASK_INTERRUPTIBLE);
4136 if (ret) {
4137 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004138 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004139 }
4140
4141 /* if it's good, return it */
4142 if (!IS_ERR(tlink->tl_tcon))
4143 return tlink;
4144
4145 /* return error if we tried this already recently */
4146 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4147 cifs_put_tlink(tlink);
4148 return ERR_PTR(-EACCES);
4149 }
4150
4151 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4152 goto wait_for_construction;
4153 }
4154
4155 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4156 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4157 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4158
4159 if (IS_ERR(tlink->tl_tcon)) {
4160 cifs_put_tlink(tlink);
4161 return ERR_PTR(-EACCES);
4162 }
4163
4164 return tlink;
4165}
Jeff Layton2de970f2010-10-06 19:51:12 -04004166
4167/*
4168 * periodic workqueue job that scans tcon_tree for a superblock and closes
4169 * out tcons.
4170 */
4171static void
4172cifs_prune_tlinks(struct work_struct *work)
4173{
4174 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4175 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004176 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004177 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004178 struct rb_node *tmp;
4179 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004180
Jeff Laytonb647c352010-10-28 11:16:44 -04004181 /*
4182 * Because we drop the spinlock in the loop in order to put the tlink
4183 * it's not guarded against removal of links from the tree. The only
4184 * places that remove entries from the tree are this function and
4185 * umounts. Because this function is non-reentrant and is canceled
4186 * before umount can proceed, this is safe.
4187 */
4188 spin_lock(&cifs_sb->tlink_tree_lock);
4189 node = rb_first(root);
4190 while (node != NULL) {
4191 tmp = node;
4192 node = rb_next(tmp);
4193 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4194
4195 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4196 atomic_read(&tlink->tl_count) != 0 ||
4197 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4198 continue;
4199
4200 cifs_get_tlink(tlink);
4201 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4202 rb_erase(tmp, root);
4203
Jeff Layton2de970f2010-10-06 19:51:12 -04004204 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004205 cifs_put_tlink(tlink);
4206 spin_lock(&cifs_sb->tlink_tree_lock);
4207 }
4208 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004209
Jeff Laytonda472fc2012-03-23 14:40:53 -04004210 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004211 TLINK_IDLE_EXPIRE);
4212}
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004213
4214#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004215/* Update dfs referral path of superblock */
4216static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
4217 const char *target)
4218{
4219 int rc = 0;
4220 size_t len = strlen(target);
4221 char *refpath, *npath;
4222
4223 if (unlikely(len < 2 || *target != '\\'))
4224 return -EINVAL;
4225
4226 if (target[1] == '\\') {
4227 len += 1;
4228 refpath = kmalloc(len, GFP_KERNEL);
4229 if (!refpath)
4230 return -ENOMEM;
4231
4232 scnprintf(refpath, len, "%s", target);
4233 } else {
4234 len += sizeof("\\");
4235 refpath = kmalloc(len, GFP_KERNEL);
4236 if (!refpath)
4237 return -ENOMEM;
4238
4239 scnprintf(refpath, len, "\\%s", target);
4240 }
4241
4242 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
4243 kfree(refpath);
4244
4245 if (IS_ERR(npath)) {
4246 rc = PTR_ERR(npath);
4247 } else {
4248 mutex_lock(&server->refpath_lock);
4249 kfree(server->leaf_fullpath);
4250 server->leaf_fullpath = npath;
4251 mutex_unlock(&server->refpath_lock);
4252 server->current_fullpath = server->leaf_fullpath;
4253 }
4254 return rc;
4255}
4256
4257static int target_share_matches_server(struct TCP_Server_Info *server, const char *tcp_host,
4258 size_t tcp_host_len, char *share, bool *target_match)
4259{
4260 int rc = 0;
4261 const char *dfs_host;
4262 size_t dfs_host_len;
4263
4264 *target_match = true;
4265 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
4266
4267 /* Check if hostnames or addresses match */
4268 if (dfs_host_len != tcp_host_len || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
4269 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
4270 dfs_host, (int)tcp_host_len, tcp_host);
4271 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
4272 if (rc)
4273 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
4274 }
4275 return rc;
4276}
4277
Steve French0e629042021-11-10 03:09:52 -06004278static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004279 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4280 struct dfs_cache_tgt_list *tl)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004281{
4282 int rc;
4283 struct TCP_Server_Info *server = tcon->ses->server;
4284 const struct smb_version_operations *ops = server->ops;
4285 struct cifs_tcon *ipc = tcon->ses->tcon_ipc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004286 char *share = NULL, *prefix = NULL;
4287 const char *tcp_host;
4288 size_t tcp_host_len;
4289 struct dfs_cache_tgt_iterator *tit;
4290 bool target_match;
4291
4292 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
4293
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004294 tit = dfs_cache_get_tgt_iterator(tl);
4295 if (!tit) {
4296 rc = -ENOENT;
4297 goto out;
4298 }
4299
4300 /* Try to tree connect to all dfs targets */
4301 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
4302 const char *target = dfs_cache_get_tgt_name(tit);
4303 struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl);
4304
4305 kfree(share);
4306 kfree(prefix);
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004307 share = prefix = NULL;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004308
4309 /* Check if share matches with tcp ses */
4310 rc = dfs_cache_get_tgt_share(server->current_fullpath + 1, tit, &share, &prefix);
4311 if (rc) {
4312 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
4313 break;
4314 }
4315
4316 rc = target_share_matches_server(server, tcp_host, tcp_host_len, share,
4317 &target_match);
4318 if (rc)
4319 break;
4320 if (!target_match) {
4321 rc = -EHOSTUNREACH;
4322 continue;
4323 }
4324
4325 if (ipc->need_reconnect) {
4326 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4327 rc = ops->tree_connect(xid, ipc->ses, tree, ipc, cifs_sb->local_nls);
4328 if (rc)
4329 break;
4330 }
4331
4332 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
4333 if (!islink) {
4334 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4335 break;
4336 }
4337 /*
4338 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
4339 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
4340 * reconnect so either the demultiplex thread or the echo worker will reconnect to
4341 * newly resolved target.
4342 */
4343 if (dfs_cache_find(xid, tcon->ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004344 NULL, &ntl)) {
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004345 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4346 if (rc)
4347 continue;
4348 rc = dfs_cache_noreq_update_tgthint(server->current_fullpath + 1, tit);
4349 if (!rc)
4350 rc = cifs_update_super_prepath(cifs_sb, prefix);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004351 } else {
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004352 /* Target is another dfs share */
4353 rc = update_server_fullpath(server, cifs_sb, target);
4354 dfs_cache_free_tgts(tl);
4355
4356 if (!rc) {
4357 rc = -EREMOTE;
4358 list_replace_init(&ntl.tl_list, &tl->tl_list);
4359 } else
4360 dfs_cache_free_tgts(&ntl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004361 }
4362 break;
4363 }
4364
4365out:
4366 kfree(share);
4367 kfree(prefix);
4368
4369 return rc;
4370}
4371
Steve French0e629042021-11-10 03:09:52 -06004372static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004373 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4374 struct dfs_cache_tgt_list *tl)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004375{
4376 int rc;
4377 int num_links = 0;
4378 struct TCP_Server_Info *server = tcon->ses->server;
4379
4380 do {
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004381 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004382 if (!rc || rc != -EREMOTE)
4383 break;
4384 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
4385 /*
4386 * If we couldn't tree connect to any targets from last referral path, then retry from
4387 * original referral path.
4388 */
4389 if (rc && server->current_fullpath != server->origin_fullpath) {
4390 server->current_fullpath = server->origin_fullpath;
Shyam Prasad Nba978e82022-01-17 07:15:02 +00004391 cifs_reconnect(tcon->ses->server, true);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004392 }
4393
4394 dfs_cache_free_tgts(tl);
4395 return rc;
4396}
4397
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004398int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4399{
4400 int rc;
4401 struct TCP_Server_Info *server = tcon->ses->server;
4402 const struct smb_version_operations *ops = server->ops;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004403 struct super_block *sb = NULL;
4404 struct cifs_sb_info *cifs_sb;
4405 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004406 char *tree;
Paulo Alcantara11375a52020-07-21 09:36:43 -03004407 struct dfs_info3_param ref = {0};
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004408
Shyam Prasad N3663c902021-10-30 04:51:35 +00004409 /* only send once per connect */
4410 spin_lock(&cifs_tcp_ses_lock);
4411 if (tcon->ses->status != CifsGood ||
4412 (tcon->tidStatus != CifsNew &&
4413 tcon->tidStatus != CifsNeedTcon)) {
4414 spin_unlock(&cifs_tcp_ses_lock);
4415 return 0;
4416 }
4417 tcon->tidStatus = CifsInTcon;
4418 spin_unlock(&cifs_tcp_ses_lock);
4419
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004420 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
Shyam Prasad N3663c902021-10-30 04:51:35 +00004421 if (!tree) {
4422 rc = -ENOMEM;
4423 goto out;
4424 }
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004425
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004426 if (tcon->ipc) {
4427 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4428 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004429 goto out;
4430 }
4431
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004432 sb = cifs_get_tcp_super(server);
4433 if (IS_ERR(sb)) {
4434 rc = PTR_ERR(sb);
4435 cifs_dbg(VFS, "%s: could not find superblock: %d\n", __func__, rc);
4436 goto out;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004437 }
4438
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004439 cifs_sb = CIFS_SB(sb);
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004440
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004441 /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */
4442 if (!server->current_fullpath ||
4443 dfs_cache_noreq_find(server->current_fullpath + 1, &ref, &tl)) {
4444 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, cifs_sb->local_nls);
4445 goto out;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004446 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004447
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004448 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
4449 &tl);
4450 free_dfs_info_param(&ref);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004451
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004452out:
4453 kfree(tree);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004454 cifs_put_tcp_super(sb);
4455
Shyam Prasad N3663c902021-10-30 04:51:35 +00004456 if (rc) {
4457 spin_lock(&cifs_tcp_ses_lock);
Shyam Prasad Nc1604da2022-01-16 13:38:14 +00004458 if (tcon->tidStatus == CifsInTcon)
4459 tcon->tidStatus = CifsNeedTcon;
Shyam Prasad N3663c902021-10-30 04:51:35 +00004460 spin_unlock(&cifs_tcp_ses_lock);
Shyam Prasad Nc1604da2022-01-16 13:38:14 +00004461 } else {
4462 spin_lock(&cifs_tcp_ses_lock);
4463 if (tcon->tidStatus == CifsInTcon)
4464 tcon->tidStatus = CifsGood;
4465 spin_unlock(&cifs_tcp_ses_lock);
4466 tcon->need_reconnect = false;
Shyam Prasad N3663c902021-10-30 04:51:35 +00004467 }
4468
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004469 return rc;
4470}
4471#else
4472int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4473{
Shyam Prasad N3663c902021-10-30 04:51:35 +00004474 int rc;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004475 const struct smb_version_operations *ops = tcon->ses->server->ops;
4476
Shyam Prasad N73f9bfb2021-07-19 17:37:52 +00004477 /* only send once per connect */
4478 spin_lock(&cifs_tcp_ses_lock);
4479 if (tcon->ses->status != CifsGood ||
4480 (tcon->tidStatus != CifsNew &&
4481 tcon->tidStatus != CifsNeedTcon)) {
4482 spin_unlock(&cifs_tcp_ses_lock);
4483 return 0;
4484 }
4485 tcon->tidStatus = CifsInTcon;
4486 spin_unlock(&cifs_tcp_ses_lock);
4487
Shyam Prasad N3663c902021-10-30 04:51:35 +00004488 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4489 if (rc) {
4490 spin_lock(&cifs_tcp_ses_lock);
Shyam Prasad Nc1604da2022-01-16 13:38:14 +00004491 if (tcon->tidStatus == CifsInTcon)
4492 tcon->tidStatus = CifsNeedTcon;
Shyam Prasad N3663c902021-10-30 04:51:35 +00004493 spin_unlock(&cifs_tcp_ses_lock);
Shyam Prasad Nc1604da2022-01-16 13:38:14 +00004494 } else {
4495 spin_lock(&cifs_tcp_ses_lock);
4496 if (tcon->tidStatus == CifsInTcon)
4497 tcon->tidStatus = CifsGood;
4498 spin_unlock(&cifs_tcp_ses_lock);
4499 tcon->need_reconnect = false;
Shyam Prasad N3663c902021-10-30 04:51:35 +00004500 }
4501
4502 return rc;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004503}
4504#endif