blob: 68642e3d4270525c2d231903ef8f9bd8a9d1c823 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/connect.c
3 *
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 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
Steve Frenchfb8c4b12007-07-10 01:16:18 +000019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
Eric Biggersdc920272020-03-08 22:58:20 -070024#include <linux/sched/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010025#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/list.h>
27#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pagemap.h>
30#include <linux/ctype.h>
31#include <linux/utsname.h>
32#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070033#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070034#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000035#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070036#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040038#include <linux/namei.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020039#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040042#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040043#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050044#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000045#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040046#include <linux/parser.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060047#include <linux/bvec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "cifspdu.h"
49#include "cifsglob.h"
50#include "cifsproto.h"
51#include "cifs_unicode.h"
52#include "cifs_debug.h"
53#include "cifs_fs_sb.h"
54#include "ntlmssp.h"
55#include "nterr.h"
56#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053057#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070058#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070059#include "smbdirect.h"
Paulo Alcantara1c780222018-11-14 16:24:03 -020060#include "dns_resolve.h"
61#ifdef CONFIG_CIFS_DFS_UPCALL
62#include "dfs_cache.h"
63#endif
Ronnie Sahlberg5c6e5aa2020-10-21 10:37:11 +100064#include "fs_context.h"
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +010065#ifdef CONFIG_CIFS_SWN_UPCALL
66#include "cifs_swn.h"
67#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050070extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jeff Layton2de970f2010-10-06 19:51:12 -040072/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040073#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040074#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040075
Rohith Surabattula8e670f72020-09-18 05:37:28 +000076/* Drop the connection to not overload the server */
77#define NUM_STATUS_IO_TIMEOUT 5
78
Pavel Shilovskya9f1b852010-12-13 19:08:35 +030079static int ip_connect(struct TCP_Server_Info *server);
80static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -040081static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -040082static void cifs_prune_tlinks(struct work_struct *work);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -020083
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020084/*
85 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
86 * get their ip addresses changed at some point.
87 *
88 * This should be called with server->srv_mutex held.
89 */
90#ifdef CONFIG_CIFS_DFS_UPCALL
Samuel Cabrero7d6535b2020-11-30 19:02:55 +010091static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020092{
93 int rc;
94 int len;
95 char *unc, *ipaddr = NULL;
96
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
109 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
110 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);
115 return rc;
116 }
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
124 return !rc ? -1 : 0;
125}
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200126
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200127/* These functions must be called with server->srv_mutex held */
Paulo Alcantara7d397a02020-07-21 09:36:40 -0300128static void reconn_set_next_dfs_target(struct TCP_Server_Info *server,
129 struct cifs_sb_info *cifs_sb,
130 struct dfs_cache_tgt_list *tgt_list,
131 struct dfs_cache_tgt_iterator **tgt_it)
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200132{
133 const char *name;
Samuel Cabrero7d6535b2020-11-30 19:02:55 +0100134 int rc;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200135
Paulo Alcantara7d397a02020-07-21 09:36:40 -0300136 if (!cifs_sb || !cifs_sb->origin_fullpath)
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200137 return;
138
139 if (!*tgt_it) {
140 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
141 } else {
142 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
143 if (!*tgt_it)
144 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
145 }
146
147 cifs_dbg(FYI, "%s: UNC: %s\n", __func__, cifs_sb->origin_fullpath);
148
149 name = dfs_cache_get_tgt_name(*tgt_it);
150
151 kfree(server->hostname);
152
153 server->hostname = extract_hostname(name);
Dan Carpenter84288172019-01-05 15:25:29 +0300154 if (IS_ERR(server->hostname)) {
155 cifs_dbg(FYI,
156 "%s: failed to extract hostname from target: %ld\n",
157 __func__, PTR_ERR(server->hostname));
Samuel Cabrero0bf1baf2020-12-18 10:29:49 +0100158 return;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200159 }
Samuel Cabrero7d6535b2020-11-30 19:02:55 +0100160
161 rc = reconn_set_ipaddr_from_hostname(server);
162 if (rc) {
163 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
164 __func__, rc);
165 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200166}
167
168static inline int reconn_setup_dfs_targets(struct cifs_sb_info *cifs_sb,
Paulo Alcantarabaf3f082020-05-19 15:38:29 -0300169 struct dfs_cache_tgt_list *tl)
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200170{
171 if (!cifs_sb->origin_fullpath)
172 return -EOPNOTSUPP;
173 return dfs_cache_noreq_find(cifs_sb->origin_fullpath + 1, NULL, tl);
174}
175#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Jeff Laytond5c56052008-12-01 18:42:33 -0500177/*
178 * cifs tcp session reconnection
179 *
180 * mark tcp session as reconnecting so temporarily locked
181 * mark all smb sessions as reconnecting for tcp session
182 * reconnect tcp session
183 * wake up waiters on reconnection? - (not needed currently)
184 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400185int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186cifs_reconnect(struct TCP_Server_Info *server)
187{
188 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500189 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000190 struct cifs_ses *ses;
191 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000192 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400193 struct list_head retry_list;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200194#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300195 struct super_block *sb = NULL;
Paulo Alcantara23324402018-11-20 14:37:18 -0200196 struct cifs_sb_info *cifs_sb = NULL;
197 struct dfs_cache_tgt_list tgt_list = {0};
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200198 struct dfs_cache_tgt_iterator *tgt_it = NULL;
199#endif
Steve French50c2f752007-07-13 00:33:32 +0000200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 spin_lock(&GlobalMid_Lock);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200202 server->nr_targets = 1;
203#ifdef CONFIG_CIFS_DFS_UPCALL
Ronnie Sahlberg61cabc72019-06-14 13:02:29 +1000204 spin_unlock(&GlobalMid_Lock);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300205 sb = cifs_get_tcp_super(server);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300206 if (IS_ERR(sb)) {
207 rc = PTR_ERR(sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200208 cifs_dbg(FYI, "%s: will not do DFS failover: rc = %d\n",
209 __func__, rc);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300210 sb = NULL;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200211 } else {
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300212 cifs_sb = CIFS_SB(sb);
Paulo Alcantarabaf3f082020-05-19 15:38:29 -0300213 rc = reconn_setup_dfs_targets(cifs_sb, &tgt_list);
Paulo Alcantaraa5293032020-07-21 09:36:41 -0300214 if (rc) {
215 cifs_sb = NULL;
216 if (rc != -EOPNOTSUPP) {
217 cifs_server_dbg(VFS, "%s: no target servers for DFS failover\n",
218 __func__);
219 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200220 } else {
221 server->nr_targets = dfs_cache_get_nr_tgts(&tgt_list);
222 }
223 }
224 cifs_dbg(FYI, "%s: will retry %d target(s)\n", __func__,
225 server->nr_targets);
Ronnie Sahlberg61cabc72019-06-14 13:02:29 +1000226 spin_lock(&GlobalMid_Lock);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200227#endif
Jeff Layton469ee612008-10-16 18:46:39 +0000228 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000229 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 next time through the loop */
231 spin_unlock(&GlobalMid_Lock);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300232#ifdef CONFIG_CIFS_DFS_UPCALL
233 dfs_cache_free_tgts(&tgt_list);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300234 cifs_put_tcp_super(sb);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300235#endif
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600236 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return rc;
238 } else
239 server->tcpStatus = CifsNeedReconnect;
240 spin_unlock(&GlobalMid_Lock);
241 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400242 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Steve French6e4d3bb2018-09-22 11:25:04 -0500244 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800245 trace_smb3_reconnect(server->CurrentMid, server->conn_id, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* before reconnecting the tcp session, mark the smb session (uid)
248 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500249 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
250 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530251 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500252 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000253 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500254 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500255 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000256 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500257 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100259 if (ses->tcon_ipc)
260 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530262 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500265 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500266 mutex_lock(&server->srv_mutex);
Long Li1d2a4f52019-05-13 21:01:28 -0700267 if (server->ssocket) {
268 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
269 server->ssocket->state, server->ssocket->flags);
270 kernel_sock_shutdown(server->ssocket, SHUT_WR);
271 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
272 server->ssocket->state, server->ssocket->flags);
273 sock_release(server->ssocket);
274 server->ssocket = NULL;
275 }
276 server->sequence_number = 0;
277 server->session_estab = false;
278 kfree(server->session_key.response);
279 server->session_key.response = NULL;
280 server->session_key.len = 0;
281 server->lstrp = jiffies;
Long Li214bab42019-04-05 21:36:35 +0000282
283 /* mark submitted MIDs for retry and issue callback */
284 INIT_LIST_HEAD(&retry_list);
285 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
286 spin_lock(&GlobalMid_Lock);
287 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
288 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700289 kref_get(&mid_entry->refcount);
Long Li214bab42019-04-05 21:36:35 +0000290 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
291 mid_entry->mid_state = MID_RETRY_NEEDED;
292 list_move(&mid_entry->qhead, &retry_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700293 mid_entry->mid_flags |= MID_DELETED;
Long Li214bab42019-04-05 21:36:35 +0000294 }
295 spin_unlock(&GlobalMid_Lock);
Long Li1d2a4f52019-05-13 21:01:28 -0700296 mutex_unlock(&server->srv_mutex);
Long Li214bab42019-04-05 21:36:35 +0000297
298 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
299 list_for_each_safe(tmp, tmp2, &retry_list) {
300 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
301 list_del_init(&mid_entry->qhead);
302 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700303 cifs_mid_q_entry_release(mid_entry);
Long Li214bab42019-04-05 21:36:35 +0000304 }
305
Long Li1d2a4f52019-05-13 21:01:28 -0700306 if (cifs_rdma_enabled(server)) {
307 mutex_lock(&server->srv_mutex);
Long Li050b8c32019-04-04 11:35:42 -0500308 smbd_destroy(server);
Long Li1d2a4f52019-05-13 21:01:28 -0700309 mutex_unlock(&server->srv_mutex);
310 }
Jeff Layton3c1105d2011-05-22 07:09:13 -0400311
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400312 do {
Steve French6c3d8902006-07-31 22:46:20 +0000313 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300314
Jeff Layton73e216a2013-09-05 08:38:10 -0400315 mutex_lock(&server->srv_mutex);
Samuel Cabrero121d9472020-11-30 19:02:56 +0100316
317#ifdef CONFIG_CIFS_SWN_UPCALL
318 if (server->use_swn_dstaddr) {
319 server->dstaddr = server->swn_dstaddr;
320 } else {
321#endif
322
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300323#ifdef CONFIG_CIFS_DFS_UPCALL
Samuel Cabrero121d9472020-11-30 19:02:56 +0100324 /*
325 * Set up next DFS target server (if any) for reconnect. If DFS
326 * feature is disabled, then we will retry last server we
327 * connected to before.
328 */
329 reconn_set_next_dfs_target(server, cifs_sb, &tgt_list, &tgt_it);
330#endif
331
332#ifdef CONFIG_CIFS_SWN_UPCALL
333 }
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300334#endif
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300335
Long Li781a8052017-11-22 17:38:36 -0700336 if (cifs_rdma_enabled(server))
337 rc = smbd_reconnect(server);
338 else
339 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000340 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500341 cifs_dbg(FYI, "reconnect error %d\n", rc);
Federico Sauter4afe2602015-03-17 17:45:28 +0100342 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700343 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else {
345 atomic_inc(&tcpSesReconnectCount);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800346 set_credits(server, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000348 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000349 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000350 spin_unlock(&GlobalMid_Lock);
Samuel Cabrero121d9472020-11-30 19:02:56 +0100351#ifdef CONFIG_CIFS_SWN_UPCALL
352 server->use_swn_dstaddr = false;
353#endif
Federico Sauter4afe2602015-03-17 17:45:28 +0100354 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400356 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500357
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200358#ifdef CONFIG_CIFS_DFS_UPCALL
359 if (tgt_it) {
360 rc = dfs_cache_noreq_update_tgthint(cifs_sb->origin_fullpath + 1,
361 tgt_it);
362 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000363 cifs_server_dbg(VFS, "%s: failed to update DFS target hint: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200364 __func__, rc);
365 }
366 rc = dfs_cache_update_vol(cifs_sb->origin_fullpath, server);
367 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000368 cifs_server_dbg(VFS, "%s: failed to update vol info in DFS cache: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200369 __func__, rc);
370 }
Paulo Alcantara23324402018-11-20 14:37:18 -0200371 dfs_cache_free_tgts(&tgt_list);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300372
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200373 }
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300374
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300375 cifs_put_tcp_super(sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200376#endif
Sachin Prabhub8c60012016-10-20 19:52:24 -0400377 if (server->tcpStatus == CifsNeedNegotiate)
378 mod_delayed_work(cifsiod_wq, &server->echo, 0);
379
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600380 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return rc;
382}
383
Jeff Laytonc74093b2011-01-11 07:24:23 -0500384static void
385cifs_echo_request(struct work_struct *work)
386{
387 int rc;
388 struct TCP_Server_Info *server = container_of(work,
389 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400390 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500391
Jeff Layton247ec9b2011-02-04 17:09:50 -0500392 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400393 * If we need to renegotiate, set echo interval to zero to
394 * immediately call echo service where we can renegotiate.
395 */
396 if (server->tcpStatus == CifsNeedNegotiate)
397 echo_interval = 0;
398 else
399 echo_interval = server->echo_interval;
400
401 /*
402 * We cannot send an echo if it is disabled.
403 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500404 */
Steve French4fcd1812016-06-22 20:12:05 -0500405
406 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400407 server->tcpStatus == CifsExiting ||
408 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400409 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600410 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500411 goto requeue_echo;
412
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400413 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500414 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500415 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
416 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500417
Samuel Cabrero21077c62020-11-30 19:02:54 +0100418#ifdef CONFIG_CIFS_SWN_UPCALL
419 /* Check witness registrations */
420 cifs_swn_check();
421#endif
422
Jeff Laytonc74093b2011-01-11 07:24:23 -0500423requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400424 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500425}
426
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400427static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400428allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400429{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400430 if (!server->bigbuf) {
431 server->bigbuf = (char *)cifs_buf_get();
432 if (!server->bigbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000433 cifs_server_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400434 msleep(3000);
435 /* retry will check if exiting */
436 return false;
437 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400438 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400439 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400440 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400441 }
442
Jeff Layton2a37ef92011-10-19 15:29:23 -0400443 if (!server->smallbuf) {
444 server->smallbuf = (char *)cifs_small_buf_get();
445 if (!server->smallbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000446 cifs_server_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400447 msleep(1000);
448 /* retry will check if exiting */
449 return false;
450 }
451 /* beginning of smb buffer is cleared in our buf_get */
452 } else {
453 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400454 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400455 }
456
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400457 return true;
458}
459
Jeff Laytonba749e62011-10-11 06:41:32 -0400460static bool
461server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400462{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300463 /*
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000464 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300465 * situations right:
466 * 1s client sends a normal SMB request
Ronnie Sahlbergbecc2ba2019-07-24 11:43:49 +1000467 * 2s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300468 * 30s echo workqueue job pops, and decides we got a response recently
469 * and don't need to send another
470 * ...
471 * 65s kernel_recvmsg times out, and we see that we haven't gotten
472 * a response in >60s.
473 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200474 if ((server->tcpStatus == CifsGood ||
475 server->tcpStatus == CifsNeedNegotiate) &&
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000476 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000477 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
478 (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400479 cifs_reconnect(server);
Jeff Laytonba749e62011-10-11 06:41:32 -0400480 return true;
481 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400482
Jeff Laytonba749e62011-10-11 06:41:32 -0400483 return false;
484}
485
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800486static inline bool
487zero_credits(struct TCP_Server_Info *server)
488{
489 int val;
490
491 spin_lock(&server->req_lock);
492 val = server->credits + server->echo_credits + server->oplock_credits;
493 if (server->in_flight == 0 && val == 0) {
494 spin_unlock(&server->req_lock);
495 return true;
496 }
497 spin_unlock(&server->req_lock);
498 return false;
499}
500
Al Viro71335662016-01-09 19:54:50 -0500501static int
502cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400503{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400504 int length = 0;
505 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400506
Al Viro71335662016-01-09 19:54:50 -0500507 smb_msg->msg_control = NULL;
508 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400509
Al Viro71335662016-01-09 19:54:50 -0500510 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500511 try_to_freeze();
512
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800513 /* reconnect if no credits and no requests in flight */
514 if (zero_credits(server)) {
515 cifs_reconnect(server);
516 return -ECONNABORTED;
517 }
518
Al Viro71335662016-01-09 19:54:50 -0500519 if (server_unresponsive(server))
520 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700521 if (cifs_rdma_enabled(server) && server->smbd_conn)
522 length = smbd_recv(server->smbd_conn, smb_msg);
523 else
524 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500525
526 if (server->tcpStatus == CifsExiting)
527 return -ESHUTDOWN;
528
529 if (server->tcpStatus == CifsNeedReconnect) {
530 cifs_reconnect(server);
531 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400532 }
533
Al Viro71335662016-01-09 19:54:50 -0500534 if (length == -ERESTARTSYS ||
535 length == -EAGAIN ||
536 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400537 /*
538 * Minimum sleep to prevent looping, allowing socket
539 * to clear and app threads to set tcpStatus
540 * CifsNeedReconnect if server hung.
541 */
542 usleep_range(1000, 2000);
543 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400544 continue;
Al Viro71335662016-01-09 19:54:50 -0500545 }
546
547 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500548 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400549 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500550 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400551 }
552 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400553 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400554}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400555
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400556int
557cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
558 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400559{
Al Viro71335662016-01-09 19:54:50 -0500560 struct msghdr smb_msg;
561 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100562 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400563
Al Viro71335662016-01-09 19:54:50 -0500564 return cifs_readv_from_socket(server, &smb_msg);
565}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400566
David Howellscf0604a2021-02-04 00:15:21 -0600567ssize_t
568cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
569{
570 struct msghdr smb_msg;
571
572 /*
573 * iov_iter_discard already sets smb_msg.type and count and iov_offset
574 * and cifs_readv_from_socket sets msg_control and msg_controllen
575 * so little to initialize in struct msghdr
576 */
577 smb_msg.msg_name = NULL;
578 smb_msg.msg_namelen = 0;
579 iov_iter_discard(&smb_msg.msg_iter, READ, to_read);
580
581 return cifs_readv_from_socket(server, &smb_msg);
582}
583
Al Viro71335662016-01-09 19:54:50 -0500584int
585cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700586 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500587{
588 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700589 struct bio_vec bv = {
590 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100591 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500592 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400593}
594
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400595static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400596is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400597{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400598 /*
599 * The first byte big endian of the length field,
600 * is actually not part of the length but the type
601 * with the most common, zero, as regular data.
602 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400603 switch (type) {
604 case RFC1002_SESSION_MESSAGE:
605 /* Regular SMB response */
606 return true;
607 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500608 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400609 break;
610 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500611 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400612 break;
613 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400614 /*
615 * We get this from Windows 98 instead of an error on
616 * SMB negprot response.
617 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500618 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400619 /* give server a second to clean up */
620 msleep(1000);
621 /*
622 * Always try 445 first on reconnect since we get NACK
623 * on some if we ever connected to port 139 (the NACK
624 * is since we do not begin with RFC1001 session
625 * initialize frame).
626 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400627 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400628 cifs_reconnect(server);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400629 break;
630 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000631 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400632 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400633 }
634
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400635 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400636}
637
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400638void
639dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400640{
641#ifdef CONFIG_CIFS_STATS2
642 mid->when_received = jiffies;
643#endif
644 spin_lock(&GlobalMid_Lock);
645 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400646 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400647 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400648 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000649 /*
650 * Trying to handle/dequeue a mid after the send_recv()
651 * function has finished processing it is a bug.
652 */
653 if (mid->mid_flags & MID_DELETED)
Joe Perchesa0a30362020-04-14 22:42:53 -0700654 pr_warn_once("trying to dequeue a deleted mid\n");
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700655 else {
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000656 list_del_init(&mid->qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700657 mid->mid_flags |= MID_DELETED;
658 }
Jeff Laytonea1f4502011-10-19 15:29:05 -0400659 spin_unlock(&GlobalMid_Lock);
660}
661
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800662static unsigned int
663smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
664{
665 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
666
667 /*
668 * SMB1 does not use credits.
669 */
670 if (server->vals->header_preamble_size)
671 return 0;
672
673 return le16_to_cpu(shdr->CreditRequest);
674}
675
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400676static void
677handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400678 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400679{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400680 if (server->ops->check_trans2 &&
681 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400682 return;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800683 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400684 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400685 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400686 /* Was previous buf put in mpx struct for multi-rsp? */
687 if (!mid->multiRsp) {
688 /* smb buffer will be freed by user thread */
689 if (server->large_buf)
690 server->bigbuf = NULL;
691 else
692 server->smallbuf = NULL;
693 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400694 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400695}
696
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400697static void clean_demultiplex_info(struct TCP_Server_Info *server)
698{
699 int length;
700
701 /* take it off the list, if it's not already */
702 spin_lock(&cifs_tcp_ses_lock);
703 list_del_init(&server->tcp_ses_list);
704 spin_unlock(&cifs_tcp_ses_lock);
705
Paulo Alcantara21225332020-11-28 16:54:02 -0300706 cancel_delayed_work_sync(&server->echo);
707
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400708 spin_lock(&GlobalMid_Lock);
709 server->tcpStatus = CifsExiting;
710 spin_unlock(&GlobalMid_Lock);
711 wake_up_all(&server->response_q);
712
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400713 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300714 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400715 if (server->credits <= 0)
716 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300717 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400718 /*
719 * Although there should not be any requests blocked on this queue it
720 * can not hurt to be paranoid and try to wake up requests that may
721 * haven been blocked when more than 50 at time were on the wire to the
722 * same server - they now will see the session is in exit state and get
723 * out of SendReceive.
724 */
725 wake_up_all(&server->request_q);
726 /* give those requests time to exit */
727 msleep(125);
Long Li050b8c32019-04-04 11:35:42 -0500728 if (cifs_rdma_enabled(server))
729 smbd_destroy(server);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400730 if (server->ssocket) {
731 sock_release(server->ssocket);
732 server->ssocket = NULL;
733 }
734
735 if (!list_empty(&server->pending_mid_q)) {
736 struct list_head dispose_list;
737 struct mid_q_entry *mid_entry;
738 struct list_head *tmp, *tmp2;
739
740 INIT_LIST_HEAD(&dispose_list);
741 spin_lock(&GlobalMid_Lock);
742 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
743 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500744 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700745 kref_get(&mid_entry->refcount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400746 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400747 list_move(&mid_entry->qhead, &dispose_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700748 mid_entry->mid_flags |= MID_DELETED;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400749 }
750 spin_unlock(&GlobalMid_Lock);
751
752 /* now walk dispose list and issue callbacks */
753 list_for_each_safe(tmp, tmp2, &dispose_list) {
754 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500755 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400756 list_del_init(&mid_entry->qhead);
757 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700758 cifs_mid_q_entry_release(mid_entry);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400759 }
760 /* 1/8th of sec is more than enough time for them to exit */
761 msleep(125);
762 }
763
764 if (!list_empty(&server->pending_mid_q)) {
765 /*
766 * mpx threads have not exited yet give them at least the smb
767 * send timeout time for long ops.
768 *
769 * Due to delays on oplock break requests, we need to wait at
770 * least 45 seconds before giving up on a request getting a
771 * response and going ahead and killing cifsd.
772 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500773 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400774 msleep(46000);
775 /*
776 * If threads still have not exited they are probably never
777 * coming home not much else we can do but free the memory.
778 */
779 }
780
781 kfree(server->hostname);
782 kfree(server);
783
784 length = atomic_dec_return(&tcpSesAllocCount);
785 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700786 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400787}
788
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400789static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400790standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
791{
792 int length;
793 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000794 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400795
796 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100797 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
798 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000799 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400800 cifs_reconnect(server);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400801 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400802 }
803
804 /* switch to large buffer if too big for a small one */
805 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
806 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400807 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400808 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400809 }
810
811 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400812 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100813 pdu_length - HEADER_SIZE(server) + 1
814 + server->vals->header_preamble_size);
815
Jeff Laytone9097ab2011-10-19 15:29:40 -0400816 if (length < 0)
817 return length;
818 server->total_read += length;
819
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400820 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400821
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800822 return cifs_handle_standard(server, mid);
823}
824
825int
826cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
827{
828 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
829 int length;
830
Jeff Laytone9097ab2011-10-19 15:29:40 -0400831 /*
832 * We know that we received enough to get to the MID as we
833 * checked the pdu_length earlier. Now check to see
834 * if the rest of the header is OK. We borrow the length
835 * var for the rest of the loop to avoid a new stack var.
836 *
837 * 48 bytes is enough to display the header and a little bit
838 * into the payload for debugging purposes.
839 */
Steve French373512e2015-12-18 13:05:30 -0600840 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400841 if (length != 0)
842 cifs_dump_mem("Bad SMB: ", buf,
843 min_t(unsigned int, server->total_read, 48));
844
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700845 if (server->ops->is_session_expired &&
846 server->ops->is_session_expired(buf)) {
847 cifs_reconnect(server);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700848 return -1;
849 }
850
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700851 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -0800852 server->ops->is_status_pending(buf, server))
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700853 return -1;
854
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500855 if (!mid)
856 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400857
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400858 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500859 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400860}
861
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000862static void
863smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
864{
865 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800866 int scredits, in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000867
868 /*
869 * SMB1 does not use credits.
870 */
871 if (server->vals->header_preamble_size)
872 return;
873
874 if (shdr->CreditRequest) {
875 spin_lock(&server->req_lock);
876 server->credits += le16_to_cpu(shdr->CreditRequest);
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800877 scredits = server->credits;
878 in_flight = server->in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000879 spin_unlock(&server->req_lock);
880 wake_up(&server->request_q);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800881
882 trace_smb3_add_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800883 server->conn_id, server->hostname, scredits,
884 le16_to_cpu(shdr->CreditRequest), in_flight);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800885 cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
886 __func__, le16_to_cpu(shdr->CreditRequest),
887 scredits);
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000888 }
889}
890
891
Jeff Laytone9097ab2011-10-19 15:29:40 -0400892static int
Al Viro7c97c202011-06-21 08:51:28 -0400893cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000895 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400896 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400897 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000898 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400899 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500900 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000901 struct mid_q_entry *mids[MAX_COMPOUND];
902 char *bufs[MAX_COMPOUND];
Rohith Surabattula8e670f72020-09-18 05:37:28 +0000903 unsigned int noreclaim_flag, num_io_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Eric Biggersdc920272020-03-08 22:58:20 -0700905 noreclaim_flag = memalloc_noreclaim_save();
Joe Perchesf96637b2013-05-04 22:12:25 -0500906 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400907
908 length = atomic_inc_return(&tcpSesAllocCount);
909 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700910 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700912 set_freezable();
Eric W. Biederman33da8e72019-08-16 12:33:54 -0500913 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +0000914 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700915 if (try_to_freeze())
916 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700917
Jeff Layton2a37ef92011-10-19 15:29:23 -0400918 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400919 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700920
Jeff Layton2a37ef92011-10-19 15:29:23 -0400921 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400922 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000923 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000924
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400925 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400926 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000927 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000928
929 if (server->vals->header_preamble_size == 0)
930 server->total_read = 0;
931 else
932 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700933
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400934 /*
935 * The right amount was read from socket - 4 bytes,
936 * so we can now interpret the length field.
937 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400938 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700939
Joe Perchesf96637b2013-05-04 22:12:25 -0500940 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400941 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000942 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000943next_pdu:
944 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700945
Jeff Layton89482a52011-10-19 15:28:57 -0400946 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000947 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100948 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000949 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000950 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400951 cifs_reconnect(server);
Jeff Layton89482a52011-10-19 15:28:57 -0400952 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700953 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400954
Jeff Layton89482a52011-10-19 15:28:57 -0400955 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100956 length = cifs_read_from_socket(server,
957 buf + server->vals->header_preamble_size,
958 HEADER_SIZE(server) - 1
959 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400960 if (length < 0)
961 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400962 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400963
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000964 if (server->ops->next_header) {
965 next_offset = server->ops->next_header(buf);
966 if (next_offset)
967 server->pdu_size = next_offset;
968 }
969
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000970 memset(mids, 0, sizeof(mids));
971 memset(bufs, 0, sizeof(bufs));
972 num_mids = 0;
973
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800974 if (server->ops->is_transform_hdr &&
975 server->ops->receive_transform &&
976 server->ops->is_transform_hdr(buf)) {
977 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000978 mids,
979 bufs,
980 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800981 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000982 mids[0] = server->ops->find_mid(server, buf);
983 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -0500984 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400985
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000986 if (!mids[0] || !mids[0]->receive)
987 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800988 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000989 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800990 }
Jeff Layton44d22d82011-10-19 15:29:49 -0400991
Lars Persson696e4202018-06-25 14:05:25 +0200992 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000993 for (i = 0; i < num_mids; i++)
994 if (mids[i])
995 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -0700996 continue;
Lars Persson696e4202018-06-25 14:05:25 +0200997 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700998
Rohith Surabattula8e670f72020-09-18 05:37:28 +0000999 if (server->ops->is_status_io_timeout &&
1000 server->ops->is_status_io_timeout(buf)) {
1001 num_io_timeout++;
1002 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
1003 cifs_reconnect(server);
1004 num_io_timeout = 0;
1005 continue;
1006 }
1007 }
1008
Steve Frenchfda35942011-01-20 18:06:34 +00001009 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001010
1011 for (i = 0; i < num_mids; i++) {
1012 if (mids[i] != NULL) {
1013 mids[i]->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001014
Rohith Surabattula9e550b02021-02-16 10:40:45 +00001015 if (bufs[i] && server->ops->is_network_name_deleted)
1016 server->ops->is_network_name_deleted(bufs[i],
1017 server);
1018
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001019 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1020 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001021
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001022 cifs_mid_q_entry_release(mids[i]);
1023 } else if (server->ops->is_oplock_break &&
1024 server->ops->is_oplock_break(bufs[i],
1025 server)) {
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001026 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001027 cifs_dbg(FYI, "Received oplock break\n");
1028 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07001029 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1030 atomic_read(&midCount));
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001031 cifs_dump_mem("Received Data is: ", bufs[i],
1032 HEADER_SIZE(server));
Ronnie Sahlberg3e272572019-07-06 06:43:08 +10001033 smb2_add_credits_from_hdr(bufs[i], server);
Steve French39798772006-05-31 22:40:51 +00001034#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001035 if (server->ops->dump_detail)
1036 server->ops->dump_detail(bufs[i],
1037 server);
1038 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001039#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001040 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001041 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001042
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001043 if (pdu_length > server->pdu_size) {
1044 if (!allocate_buffers(server))
1045 continue;
1046 pdu_length -= server->pdu_size;
1047 server->total_read = 0;
1048 server->large_buf = false;
1049 buf = server->smallbuf;
1050 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001051 }
1052 } /* end while !EXITING */
1053
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001054 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001055 cifs_buf_release(server->bigbuf);
1056 if (server->smallbuf) /* no sense logging a debug message if NULL */
1057 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001059 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001060 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001061
1062 /* if server->tsk was NULL then wait for a signal before exiting */
1063 if (!task_to_wake) {
1064 set_current_state(TASK_INTERRUPTIBLE);
1065 while (!signal_pending(current)) {
1066 schedule();
1067 set_current_state(TASK_INTERRUPTIBLE);
1068 }
1069 set_current_state(TASK_RUNNING);
1070 }
1071
Eric Biggersdc920272020-03-08 22:58:20 -07001072 memalloc_noreclaim_restore(noreclaim_flag);
Jeff Layton0468a2c2008-12-01 07:09:35 -05001073 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Steve Frenchbc044992020-12-11 19:48:26 -06001076/**
1077 * Returns true if srcaddr isn't specified and rhs isn't specified, or
1078 * if srcaddr is specified and matches the IP address of the rhs argument
Ben Greear3eb9a882010-09-01 17:06:02 -07001079 */
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001080bool
1081cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
Ben Greear3eb9a882010-09-01 17:06:02 -07001082{
1083 switch (srcaddr->sa_family) {
1084 case AF_UNSPEC:
1085 return (rhs->sa_family == AF_UNSPEC);
1086 case AF_INET: {
1087 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1088 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1089 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1090 }
1091 case AF_INET6: {
1092 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05001093 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07001094 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1095 }
1096 default:
1097 WARN_ON(1);
1098 return false; /* don't expect to be here */
1099 }
1100}
1101
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001102/*
1103 * If no port is specified in addr structure, we try to match with 445 port
1104 * and if it fails - with 139 ports. It should be called only if address
1105 * families of server and addr are equal.
1106 */
1107static bool
1108match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1109{
Steve French6da97912011-03-13 18:55:55 +00001110 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001111
Long Li3b249112019-05-15 14:09:04 -07001112 /* SMBDirect manages its own ports, don't match it here */
1113 if (server->rdma)
1114 return true;
1115
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001116 switch (addr->sa_family) {
1117 case AF_INET:
1118 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1119 port = ((struct sockaddr_in *) addr)->sin_port;
1120 break;
1121 case AF_INET6:
1122 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1123 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1124 break;
1125 default:
1126 WARN_ON(1);
1127 return false;
1128 }
1129
1130 if (!port) {
1131 port = htons(CIFS_PORT);
1132 if (port == *sport)
1133 return true;
1134
1135 port = htons(RFC1001_PORT);
1136 }
1137
1138 return port == *sport;
1139}
Ben Greear3eb9a882010-09-01 17:06:02 -07001140
1141static bool
1142match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1143 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Jeff Layton45151482010-07-06 20:43:02 -04001145 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001146 case AF_INET: {
1147 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1148 struct sockaddr_in *srv_addr4 =
1149 (struct sockaddr_in *)&server->dstaddr;
1150
1151 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04001152 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001153 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001154 }
1155 case AF_INET6: {
1156 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1157 struct sockaddr_in6 *srv_addr6 =
1158 (struct sockaddr_in6 *)&server->dstaddr;
1159
Jeff Layton45151482010-07-06 20:43:02 -04001160 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001161 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04001162 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001163 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04001164 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001165 break;
1166 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001167 default:
1168 WARN_ON(1);
1169 return false; /* don't expect to be here */
1170 }
Jeff Layton45151482010-07-06 20:43:02 -04001171
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001172 if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
Ben Greear3eb9a882010-09-01 17:06:02 -07001173 return false;
1174
Jeff Layton45151482010-07-06 20:43:02 -04001175 return true;
1176}
1177
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001178static bool
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001179match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001180{
Jeff Layton3f618222013-06-12 19:52:14 -05001181 /*
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001182 * The select_sectype function should either return the ctx->sectype
Jeff Layton3f618222013-06-12 19:52:14 -05001183 * that was specified, or "Unspecified" if that sectype was not
1184 * compatible with the given NEGOTIATE request.
1185 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001186 if (server->ops->select_sectype(server, ctx->sectype)
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301187 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001188 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001189
Jeff Layton3f618222013-06-12 19:52:14 -05001190 /*
1191 * Now check if signing mode is acceptable. No need to check
1192 * global_secflags at this point since if MUST_SIGN is set then
1193 * the server->sign had better be too.
1194 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001195 if (ctx->sign && !server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04001196 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001197
1198 return true;
1199}
1200
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001201static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001202{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001203 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
Jeff Layton9fa114f2012-11-26 11:09:57 -05001204
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001205 if (ctx->nosharesock)
Jeff Laytona0b3df52013-05-24 07:40:59 -04001206 return 0;
1207
Steve French43cdae82019-06-13 14:26:49 -05001208 /* If multidialect negotiation see if existing sessions match one */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001209 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
Steve French43cdae82019-06-13 14:26:49 -05001210 if (server->vals->protocol_id < SMB30_PROT_ID)
1211 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001212 } else if (strcmp(ctx->vals->version_string,
Steve French43cdae82019-06-13 14:26:49 -05001213 SMBDEFAULT_VERSION_STRING) == 0) {
1214 if (server->vals->protocol_id < SMB21_PROT_ID)
1215 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001216 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
Jeff Layton23db65f2012-05-15 12:20:51 -04001217 return 0;
1218
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001219 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1220 return 0;
1221
1222 if (!match_address(server, addr,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001223 (struct sockaddr *)&ctx->srcaddr))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001224 return 0;
1225
1226 if (!match_port(server, addr))
1227 return 0;
1228
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001229 if (!match_security(server, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001230 return 0;
1231
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001232 if (server->echo_interval != ctx->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001233 return 0;
1234
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001235 if (server->rdma != ctx->rdma)
Long Li8339dd32017-11-07 01:54:55 -07001236 return 0;
1237
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001238 if (server->ignore_signature != ctx->ignore_signature)
Steve French4f5c10f2019-09-03 21:18:49 -05001239 return 0;
1240
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001241 if (server->min_offload != ctx->min_offload)
Steve French563317e2019-09-08 23:22:02 -05001242 return 0;
1243
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001244 return 1;
1245}
1246
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001247struct TCP_Server_Info *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001248cifs_find_tcp_session(struct smb3_fs_context *ctx)
Jeff Layton45151482010-07-06 20:43:02 -04001249{
Jeff Laytone7ddee92008-11-14 13:44:38 -05001250 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301252 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04001253 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -03001254 /*
1255 * Skip ses channels since they're only handled in lower layers
1256 * (e.g. cifs_send_recv).
1257 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001258 if (server->is_channel || !match_server(server, ctx))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001259 continue;
1260
Jeff Laytone7ddee92008-11-14 13:44:38 -05001261 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301262 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05001263 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05001264 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301266 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return NULL;
1268}
1269
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001270void
1271cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001273 struct task_struct *task;
1274
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301275 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001276 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301277 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001278 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Steve Frenchdea570e02008-05-06 22:05:51 +00001280
Rob Landleyf1d0c992011-01-22 15:44:05 -06001281 put_net(cifs_net_ns(server));
1282
Jeff Laytone7ddee92008-11-14 13:44:38 -05001283 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301284 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001285
Jeff Laytonc74093b2011-01-11 07:24:23 -05001286 cancel_delayed_work_sync(&server->echo);
1287
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001288 if (from_reconnect)
1289 /*
1290 * Avoid deadlock here: reconnect work calls
1291 * cifs_put_tcp_session() at its end. Need to be sure
1292 * that reconnect work does nothing with server pointer after
1293 * that step.
1294 */
1295 cancel_delayed_work(&server->reconnect);
1296 else
1297 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001298
Jeff Laytone7ddee92008-11-14 13:44:38 -05001299 spin_lock(&GlobalMid_Lock);
1300 server->tcpStatus = CifsExiting;
1301 spin_unlock(&GlobalMid_Lock);
1302
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001303 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05301304 cifs_fscache_release_client_cookie(server);
1305
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05001306 kfree(server->session_key.response);
1307 server->session_key.response = NULL;
1308 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001309
1310 task = xchg(&server->tsk, NULL);
1311 if (task)
Eric W. Biederman72abe3b2019-05-15 12:33:50 -05001312 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001315struct TCP_Server_Info *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001316cifs_get_tcp_session(struct smb3_fs_context *ctx)
Jeff Layton63c038c2008-12-01 18:41:46 -05001317{
1318 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05001319 int rc;
1320
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001321 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05001322
1323 /* see if we already have a matching tcp_ses */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001324 tcp_ses = cifs_find_tcp_session(ctx);
Jeff Layton63c038c2008-12-01 18:41:46 -05001325 if (tcp_ses)
1326 return tcp_ses;
1327
1328 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1329 if (!tcp_ses) {
1330 rc = -ENOMEM;
1331 goto out_err;
1332 }
1333
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001334 tcp_ses->ops = ctx->ops;
1335 tcp_ses->vals = ctx->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06001336 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001337 tcp_ses->hostname = extract_hostname(ctx->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05001338 if (IS_ERR(tcp_ses->hostname)) {
1339 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001340 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001341 }
1342
Shyam Prasad N6d82c272021-02-03 23:20:46 -08001343 tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001344 tcp_ses->noblockcnt = ctx->rootfs;
1345 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1346 tcp_ses->noautotune = ctx->noautotune;
1347 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1348 tcp_ses->rdma = ctx->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03001349 tcp_ses->in_flight = 0;
Steve French1b63f182019-09-09 22:57:11 -05001350 tcp_ses->max_in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04001351 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05001352 init_waitqueue_head(&tcp_ses->response_q);
1353 init_waitqueue_head(&tcp_ses->request_q);
1354 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1355 mutex_init(&tcp_ses->srv_mutex);
1356 memcpy(tcp_ses->workstation_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001357 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Jeff Layton63c038c2008-12-01 18:41:46 -05001358 memcpy(tcp_ses->server_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001359 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05001360 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05001361 tcp_ses->sequence_number = 0;
Pavel Shilovsky5b964852019-01-18 11:30:26 -08001362 tcp_ses->reconnect_instance = 1;
Steve Frenchfda35942011-01-20 18:06:34 +00001363 tcp_ses->lstrp = jiffies;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001364 tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
Jeff Layton58fa0152012-05-01 17:41:16 -04001365 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001366 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1367 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001368 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001369 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1370 mutex_init(&tcp_ses->reconnect_mutex);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001371 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001372 sizeof(tcp_ses->srcaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001373 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001374 sizeof(tcp_ses->dstaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001375 if (ctx->use_client_guid)
1376 memcpy(tcp_ses->client_guid, ctx->client_guid,
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001377 SMB2_CLIENT_GUID_SIZE);
1378 else
1379 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05001380 /*
1381 * at this point we are the only ones with the pointer
1382 * to the struct since the kernel thread not created yet
1383 * no need to spinlock this init of tcpStatus or srv_count
1384 */
1385 tcp_ses->tcpStatus = CifsNew;
1386 ++tcp_ses->srv_count;
1387
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001388 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1389 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1390 tcp_ses->echo_interval = ctx->echo_interval * HZ;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001391 else
1392 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07001393 if (tcp_ses->rdma) {
1394#ifndef CONFIG_CIFS_SMB_DIRECT
1395 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1396 rc = -ENOENT;
1397 goto out_err_crypto_release;
1398#endif
1399 tcp_ses->smbd_conn = smbd_get_connection(
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001400 tcp_ses, (struct sockaddr *)&ctx->dstaddr);
Long Li2f894642017-11-22 17:38:34 -07001401 if (tcp_ses->smbd_conn) {
1402 cifs_dbg(VFS, "RDMA transport established\n");
1403 rc = 0;
1404 goto smbd_connected;
1405 } else {
1406 rc = -ENOENT;
1407 goto out_err_crypto_release;
1408 }
1409 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001410 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05001411 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001412 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001413 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001414 }
Long Li2f894642017-11-22 17:38:34 -07001415smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05001416 /*
1417 * since we're in a cifs function already, we know that
1418 * this will succeed. No need for try_module_get().
1419 */
1420 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04001421 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05001422 tcp_ses, "cifsd");
1423 if (IS_ERR(tcp_ses->tsk)) {
1424 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05001425 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05001426 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001427 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001428 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001429 tcp_ses->min_offload = ctx->min_offload;
Steve Frenchfd88ce92011-04-12 01:01:14 +00001430 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05001431
Aurelien Aptela249cc82021-03-04 17:42:21 +00001432 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
1433 tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
1434 else
1435 tcp_ses->max_credits = ctx->max_credits;
1436
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02001437 tcp_ses->nr_targets = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001438 tcp_ses->ignore_signature = ctx->ignore_signature;
Jeff Layton63c038c2008-12-01 18:41:46 -05001439 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301440 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001441 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301442 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001443
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05301444 cifs_fscache_get_client_cookie(tcp_ses);
1445
Jeff Laytonc74093b2011-01-11 07:24:23 -05001446 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001447 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001448
Jeff Layton63c038c2008-12-01 18:41:46 -05001449 return tcp_ses;
1450
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001451out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001452 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05001453
Rob Landleyf1d0c992011-01-22 15:44:05 -06001454 put_net(cifs_net_ns(tcp_ses));
1455
Jeff Layton63c038c2008-12-01 18:41:46 -05001456out_err:
1457 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00001458 if (!IS_ERR(tcp_ses->hostname))
1459 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05001460 if (tcp_ses->ssocket)
1461 sock_release(tcp_ses->ssocket);
1462 kfree(tcp_ses);
1463 }
1464 return ERR_PTR(rc);
1465}
1466
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001467static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001468{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001469 if (ctx->sectype != Unspecified &&
1470 ctx->sectype != ses->sectype)
Jeff Layton3f618222013-06-12 19:52:14 -05001471 return 0;
1472
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001473 /*
1474 * If an existing session is limited to less channels than
1475 * requested, it should not be reused
1476 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001477 if (ses->chan_max < ctx->max_channels)
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001478 return 0;
1479
Jeff Layton3f618222013-06-12 19:52:14 -05001480 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001481 case Kerberos:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001482 if (!uid_eq(ctx->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001483 return 0;
1484 break;
1485 default:
Jeff Layton04febab2012-01-17 16:09:15 -05001486 /* NULL username means anonymous session */
1487 if (ses->user_name == NULL) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001488 if (!ctx->nullauth)
Jeff Layton04febab2012-01-17 16:09:15 -05001489 return 0;
1490 break;
1491 }
1492
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001493 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05001494 if (strncmp(ses->user_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001495 ctx->username ? ctx->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001496 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001497 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001498 if ((ctx->username && strlen(ctx->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001499 ses->password != NULL &&
1500 strncmp(ses->password,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001501 ctx->password ? ctx->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001502 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001503 return 0;
1504 }
1505 return 1;
1506}
1507
Aurelien Aptelb327a712018-01-24 13:46:10 +01001508/**
1509 * cifs_setup_ipc - helper to setup the IPC tcon for the session
1510 *
1511 * A new IPC connection is made and stored in the session
1512 * tcon_ipc. The IPC tcon has the same lifetime as the session.
1513 */
1514static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001515cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001516{
1517 int rc = 0, xid;
1518 struct cifs_tcon *tcon;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001519 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1520 bool seal = false;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001521 struct TCP_Server_Info *server = ses->server;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001522
1523 /*
1524 * If the mount request that resulted in the creation of the
1525 * session requires encryption, force IPC to be encrypted too.
1526 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001527 if (ctx->seal) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001528 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001529 seal = true;
1530 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001531 cifs_server_dbg(VFS,
Aurelien Aptelb327a712018-01-24 13:46:10 +01001532 "IPC: server doesn't support encryption\n");
1533 return -EOPNOTSUPP;
1534 }
1535 }
1536
1537 tcon = tconInfoAlloc();
1538 if (tcon == NULL)
1539 return -ENOMEM;
1540
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001541 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001542
Aurelien Aptelb327a712018-01-24 13:46:10 +01001543 xid = get_xid();
1544 tcon->ses = ses;
1545 tcon->ipc = true;
1546 tcon->seal = seal;
Ronnie Sahlberg6fd4ea82020-12-14 16:40:21 +10001547 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001548 free_xid(xid);
1549
1550 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001551 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001552 tconInfoFree(tcon);
1553 goto out;
1554 }
1555
1556 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
1557
1558 ses->tcon_ipc = tcon;
1559out:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001560 return rc;
1561}
1562
1563/**
1564 * cifs_free_ipc - helper to release the session IPC tcon
1565 *
1566 * Needs to be called everytime a session is destroyed
1567 */
1568static int
1569cifs_free_ipc(struct cifs_ses *ses)
1570{
1571 int rc = 0, xid;
1572 struct cifs_tcon *tcon = ses->tcon_ipc;
1573
1574 if (tcon == NULL)
1575 return 0;
1576
1577 if (ses->server->ops->tree_disconnect) {
1578 xid = get_xid();
1579 rc = ses->server->ops->tree_disconnect(xid, tcon);
1580 free_xid(xid);
1581 }
1582
1583 if (rc)
1584 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
1585
1586 tconInfoFree(tcon);
1587 ses->tcon_ipc = NULL;
1588 return rc;
1589}
1590
Steve French96daf2b2011-05-27 04:34:02 +00001591static struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001592cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Steve French96daf2b2011-05-27 04:34:02 +00001594 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301596 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04001597 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001598 if (ses->status == CifsExiting)
1599 continue;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001600 if (!match_session(ses, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001601 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05001602 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301603 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001604 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301606 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return NULL;
1608}
1609
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001610void cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05001611{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001612 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05001613 struct TCP_Server_Info *server = ses->server;
1614
Joe Perchesf96637b2013-05-04 22:12:25 -05001615 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001616
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301617 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001618 if (ses->status == CifsExiting) {
1619 spin_unlock(&cifs_tcp_ses_lock);
1620 return;
1621 }
Jeff Layton14fbf502008-11-14 13:53:46 -05001622 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301623 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001624 return;
1625 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001626 if (ses->status == CifsGood)
1627 ses->status = CifsExiting;
1628 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001629
Aurelien Aptelb327a712018-01-24 13:46:10 +01001630 cifs_free_ipc(ses);
1631
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001632 if (ses->status == CifsExiting && server->ops->logoff) {
1633 xid = get_xid();
1634 rc = server->ops->logoff(xid, ses);
1635 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001636 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001637 __func__, rc);
1638 _free_xid(xid);
1639 }
1640
1641 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001642 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301643 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001644
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001645 /* close any extra channels */
1646 if (ses->chan_count > 1) {
1647 int i;
1648
1649 for (i = 1; i < ses->chan_count; i++)
1650 cifs_put_tcp_session(ses->chans[i].server, 0);
1651 }
1652
Jeff Layton14fbf502008-11-14 13:53:46 -05001653 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001654 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05001655}
1656
Jeff Layton8a8798a2012-01-17 16:09:15 -05001657#ifdef CONFIG_KEYS
1658
Chen Gang057d6332013-07-19 09:01:36 +08001659/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
1660#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001661
1662/* Populate username and pw fields from keyring if possible */
1663static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001664cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001665{
1666 int rc = 0;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001667 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01001668 const char *delim, *payload;
1669 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001670 ssize_t len;
1671 struct key *key;
1672 struct TCP_Server_Info *server = ses->server;
1673 struct sockaddr_in *sa;
1674 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01001675 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001676
1677 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
1678 if (!desc)
1679 return -ENOMEM;
1680
1681 /* try to find an address key first */
1682 switch (server->dstaddr.ss_family) {
1683 case AF_INET:
1684 sa = (struct sockaddr_in *)&server->dstaddr;
1685 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
1686 break;
1687 case AF_INET6:
1688 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
1689 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
1690 break;
1691 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001692 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
1693 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001694 rc = -EINVAL;
1695 goto out_err;
1696 }
1697
Joe Perchesf96637b2013-05-04 22:12:25 -05001698 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001699 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001700 if (IS_ERR(key)) {
1701 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001702 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001703 rc = PTR_ERR(key);
1704 goto out_err;
1705 }
1706
1707 /* didn't work, try to find a domain key */
1708 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05001709 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001710 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001711 if (IS_ERR(key)) {
1712 rc = PTR_ERR(key);
1713 goto out_err;
1714 }
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001715 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001716 }
1717
1718 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00001719 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001720 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05001721 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001722 goto out_key_put;
1723 }
1724
1725 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01001726 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001727 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05001728 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001729 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001730 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
1731 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001732 rc = -EINVAL;
1733 goto out_key_put;
1734 }
1735
1736 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001737 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001738 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
1739 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001740 rc = -EINVAL;
1741 goto out_key_put;
1742 }
1743
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001744 ctx->username = kstrndup(payload, len, GFP_KERNEL);
1745 if (!ctx->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001746 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
1747 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001748 rc = -ENOMEM;
1749 goto out_key_put;
1750 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001751 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001752
1753 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001754 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001755 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001756 rc = -EINVAL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001757 kfree(ctx->username);
1758 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001759 goto out_key_put;
1760 }
1761
1762 ++delim;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001763 ctx->password = kstrndup(delim, len, GFP_KERNEL);
1764 if (!ctx->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001765 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
1766 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001767 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001768 kfree(ctx->username);
1769 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001770 goto out_key_put;
1771 }
1772
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001773 /*
1774 * If we have a domain key then we must set the domainName in the
1775 * for the request.
1776 */
1777 if (is_domain && ses->domainName) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001778 ctx->domainname = kstrndup(ses->domainName,
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001779 strlen(ses->domainName),
1780 GFP_KERNEL);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001781 if (!ctx->domainname) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001782 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
1783 len);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001784 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001785 kfree(ctx->username);
1786 ctx->username = NULL;
1787 kfree_sensitive(ctx->password);
1788 ctx->password = NULL;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001789 goto out_key_put;
1790 }
1791 }
1792
Jeff Layton8a8798a2012-01-17 16:09:15 -05001793out_key_put:
1794 up_read(&key->sem);
1795 key_put(key);
1796out_err:
1797 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05001798 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001799 return rc;
1800}
1801#else /* ! CONFIG_KEYS */
1802static inline int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001803cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
Jeff Layton8a8798a2012-01-17 16:09:15 -05001804 struct cifs_ses *ses __attribute__((unused)))
1805{
1806 return -ENOSYS;
1807}
1808#endif /* CONFIG_KEYS */
1809
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01001810/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001811 * cifs_get_smb_ses - get a session matching @ctx data from @server
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01001812 *
1813 * This function assumes it is being called from cifs_mount() where we
1814 * already got a server reference (server refcount +1). See
1815 * cifs_get_tcon() for refcount explanations.
1816 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001817struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001818cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Layton36988c72010-04-24 07:57:43 -04001819{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04001820 int rc = -ENOMEM;
1821 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00001822 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001823 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
1824 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04001825
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001826 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04001827
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001828 ses = cifs_find_smb_ses(server, ctx);
Jeff Layton36988c72010-04-24 07:57:43 -04001829 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001830 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
1831 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04001832
Jeff Layton36988c72010-04-24 07:57:43 -04001833 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04001834 rc = cifs_negotiate_protocol(xid, ses);
1835 if (rc) {
1836 mutex_unlock(&ses->session_mutex);
1837 /* problem -- put our ses reference */
1838 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001839 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04001840 return ERR_PTR(rc);
1841 }
Jeff Layton36988c72010-04-24 07:57:43 -04001842 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001843 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04001844 rc = cifs_setup_session(xid, ses,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001845 ctx->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04001846 if (rc) {
1847 mutex_unlock(&ses->session_mutex);
1848 /* problem -- put our reference */
1849 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001850 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04001851 return ERR_PTR(rc);
1852 }
1853 }
1854 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04001855
1856 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001857 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001858 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04001859 return ses;
1860 }
1861
Joe Perchesf96637b2013-05-04 22:12:25 -05001862 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04001863 ses = sesInfoAlloc();
1864 if (ses == NULL)
1865 goto get_ses_fail;
1866
1867 /* new SMB session uses our server ref */
1868 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001869 if (server->dstaddr.ss_family == AF_INET6)
Steve Frenchb438fcf2021-02-20 19:24:11 -06001870 sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04001871 else
Steve Frenchb438fcf2021-02-20 19:24:11 -06001872 sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04001873
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001874 if (ctx->username) {
1875 ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
Steve French8727c8a2011-02-25 01:11:56 -06001876 if (!ses->user_name)
1877 goto get_ses_fail;
1878 }
Jeff Layton36988c72010-04-24 07:57:43 -04001879
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001880 /* ctx->password freed at unmount */
1881 if (ctx->password) {
1882 ses->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Layton36988c72010-04-24 07:57:43 -04001883 if (!ses->password)
1884 goto get_ses_fail;
1885 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001886 if (ctx->domainname) {
1887 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05001888 if (!ses->domainName)
1889 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04001890 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001891 if (ctx->domainauto)
1892 ses->domainAuto = ctx->domainauto;
1893 ses->cred_uid = ctx->cred_uid;
1894 ses->linux_uid = ctx->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00001895
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001896 ses->sectype = ctx->sectype;
1897 ses->sign = ctx->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04001898 mutex_lock(&ses->session_mutex);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001899
1900 /* add server as first channel */
1901 ses->chans[0].server = server;
1902 ses->chan_count = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001903 ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001904
Jeff Layton198b5682010-04-24 07:57:48 -04001905 rc = cifs_negotiate_protocol(xid, ses);
1906 if (!rc)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001907 rc = cifs_setup_session(xid, ses, ctx->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001908
1909 /* each channel uses a different signing key */
1910 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
1911 sizeof(ses->smb3signingkey));
1912
Jeff Layton36988c72010-04-24 07:57:43 -04001913 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00001914 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04001915 goto get_ses_fail;
1916
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001917 /* success, put it on the list and add it as first channel */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301918 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04001919 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301920 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04001921
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001922 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001923
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001924 cifs_setup_ipc(ses, ctx);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001925
Jeff Layton36988c72010-04-24 07:57:43 -04001926 return ses;
1927
1928get_ses_fail:
1929 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001930 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04001931 return ERR_PTR(rc);
1932}
1933
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001934static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001935{
1936 if (tcon->tidStatus == CifsExiting)
1937 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001938 if (strncmp(tcon->treeName, ctx->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001939 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001940 if (tcon->seal != ctx->seal)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001941 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001942 if (tcon->snapshot_time != ctx->snapshot_time)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08001943 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001944 if (tcon->handle_timeout != ctx->handle_timeout)
Steve Frenchca567eb2019-03-29 16:31:07 -05001945 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001946 if (tcon->no_lease != ctx->no_lease)
Steve French3e7a02d2019-09-11 21:46:20 -05001947 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001948 if (tcon->nodelete != ctx->nodelete)
Steve French82e93672020-05-19 03:06:57 -05001949 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001950 return 1;
1951}
1952
Steve French96daf2b2011-05-27 04:34:02 +00001953static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001954cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00001957 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301959 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05001960 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00001961 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Paulo Alcantara65303de2020-04-20 19:42:57 -03001962#ifdef CONFIG_CIFS_DFS_UPCALL
1963 if (tcon->dfs_path)
1964 continue;
1965#endif
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001966 if (!match_tcon(tcon, ctx))
Jeff Laytonf1987b42008-11-15 11:12:47 -05001967 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05001968 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301969 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return tcon;
1971 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301972 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return NULL;
1974}
1975
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001976void
Steve French96daf2b2011-05-27 04:34:02 +00001977cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05001978{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04001979 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001980 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05001981
Aurelien Aptelb327a712018-01-24 13:46:10 +01001982 /*
1983 * IPC tcon share the lifetime of their session and are
1984 * destroyed in the session put function
1985 */
1986 if (tcon == NULL || tcon->ipc)
1987 return;
1988
1989 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05001990 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301991 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05001992 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301993 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05001994 return;
1995 }
1996
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01001997#ifdef CONFIG_CIFS_SWN_UPCALL
1998 if (tcon->use_witness) {
1999 int rc;
2000
2001 rc = cifs_swn_unregister(tcon);
2002 if (rc < 0) {
2003 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
2004 __func__, rc);
2005 }
2006 }
2007#endif
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002008
Jeff Laytonf1987b42008-11-15 11:12:47 -05002009 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302010 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002011
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002012 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002013 if (ses->server->ops->tree_disconnect)
2014 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002015 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002016
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302017 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002018 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002019 cifs_put_smb_ses(ses);
2020}
2021
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002022/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002023 * cifs_get_tcon - get a tcon matching @ctx data from @ses
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002024 *
2025 * - tcon refcount is the number of mount points using the tcon.
2026 * - ses refcount is the number of tcon using the session.
2027 *
2028 * 1. This function assumes it is being called from cifs_mount() where
2029 * we already got a session reference (ses refcount +1).
2030 *
2031 * 2. Since we're in the context of adding a mount point, the end
2032 * result should be either:
2033 *
2034 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2035 * its session refcount incremented (1 new tcon). This +1 was
2036 * already done in (1).
2037 *
2038 * b) an existing tcon with refcount+1 (add a mount point to it) and
2039 * identical ses refcount (no new tcon). Because of (1) we need to
2040 * decrement the ses refcount.
2041 */
Steve French96daf2b2011-05-27 04:34:02 +00002042static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002043cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002044{
2045 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002046 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002047
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002048 tcon = cifs_find_tcon(ses, ctx);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002049 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002050 /*
2051 * tcon has refcount already incremented but we need to
2052 * decrement extra ses reference gotten by caller (case b)
2053 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002054 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002055 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002056 return tcon;
2057 }
2058
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002059 if (!ses->server->ops->tree_connect) {
2060 rc = -ENOSYS;
2061 goto out_fail;
2062 }
2063
Jeff Laytond00c28d2010-04-24 07:57:44 -04002064 tcon = tconInfoAlloc();
2065 if (tcon == NULL) {
2066 rc = -ENOMEM;
2067 goto out_fail;
2068 }
2069
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002070 if (ctx->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06002071 if (ses->server->vals->protocol_id == 0) {
2072 cifs_dbg(VFS,
2073 "Use SMB2 or later for snapshot mount option\n");
2074 rc = -EOPNOTSUPP;
2075 goto out_fail;
2076 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002077 tcon->snapshot_time = ctx->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06002078 }
2079
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002080 if (ctx->handle_timeout) {
Steve Frenchca567eb2019-03-29 16:31:07 -05002081 if (ses->server->vals->protocol_id == 0) {
2082 cifs_dbg(VFS,
2083 "Use SMB2.1 or later for handle timeout option\n");
2084 rc = -EOPNOTSUPP;
2085 goto out_fail;
2086 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002087 tcon->handle_timeout = ctx->handle_timeout;
Steve Frenchca567eb2019-03-29 16:31:07 -05002088 }
2089
Jeff Laytond00c28d2010-04-24 07:57:44 -04002090 tcon->ses = ses;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002091 if (ctx->password) {
2092 tcon->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002093 if (!tcon->password) {
2094 rc = -ENOMEM;
2095 goto out_fail;
2096 }
2097 }
2098
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002099 if (ctx->seal) {
Steve French23657ad2018-04-22 15:14:58 -05002100 if (ses->server->vals->protocol_id == 0) {
2101 cifs_dbg(VFS,
2102 "SMB3 or later required for encryption\n");
2103 rc = -EOPNOTSUPP;
2104 goto out_fail;
2105 } else if (tcon->ses->server->capabilities &
2106 SMB2_GLOBAL_CAP_ENCRYPTION)
2107 tcon->seal = true;
2108 else {
2109 cifs_dbg(VFS, "Encryption is not supported on share\n");
2110 rc = -EOPNOTSUPP;
2111 goto out_fail;
2112 }
2113 }
2114
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002115 if (ctx->linux_ext) {
Steve French8505c8b2018-06-18 14:01:59 -05002116 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05002117 tcon->posix_extensions = true;
Joe Perchesa0a30362020-04-14 22:42:53 -07002118 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05002119 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07002120 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
Steve French8505c8b2018-06-18 14:01:59 -05002121 rc = -EOPNOTSUPP;
2122 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05002123 }
Steve Frenchb3266142018-05-20 23:41:10 -05002124 }
Steve Frenchb3266142018-05-20 23:41:10 -05002125
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002126 /*
2127 * BB Do we need to wrap session_mutex around this TCon call and Unix
2128 * SetFS as we do on SessSetup and reconnect?
2129 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002130 xid = get_xid();
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002131 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2132 ctx->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002133 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05002134 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002135 if (rc)
2136 goto out_fail;
2137
Steve Frenchb618f002015-11-03 09:15:03 -06002138 tcon->use_persistent = false;
2139 /* check if SMB2 or later, CIFS does not support persistent handles */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002140 if (ctx->persistent) {
Steve Frenchb618f002015-11-03 09:15:03 -06002141 if (ses->server->vals->protocol_id == 0) {
2142 cifs_dbg(VFS,
2143 "SMB3 or later required for persistent handles\n");
2144 rc = -EOPNOTSUPP;
2145 goto out_fail;
2146 } else if (ses->server->capabilities &
2147 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2148 tcon->use_persistent = true;
2149 else /* persistent handles requested but not supported */ {
2150 cifs_dbg(VFS,
2151 "Persistent handles not supported on share\n");
2152 rc = -EOPNOTSUPP;
2153 goto out_fail;
2154 }
2155 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2156 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002157 && (ctx->nopersistent == false)) {
Steve Frenchb618f002015-11-03 09:15:03 -06002158 cifs_dbg(FYI, "enabling persistent handles\n");
2159 tcon->use_persistent = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002160 } else if (ctx->resilient) {
Steve French592fafe2015-11-03 10:08:53 -06002161 if (ses->server->vals->protocol_id == 0) {
2162 cifs_dbg(VFS,
2163 "SMB2.1 or later required for resilient handles\n");
2164 rc = -EOPNOTSUPP;
2165 goto out_fail;
2166 }
2167 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06002168 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002169#ifdef CONFIG_CIFS_SWN_UPCALL
2170 tcon->use_witness = false;
2171 if (ctx->witness) {
2172 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2173 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002174 /*
2175 * Set witness in use flag in first place
2176 * to retry registration in the echo task
2177 */
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002178 tcon->use_witness = true;
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002179 /* And try to register immediately */
2180 rc = cifs_swn_register(tcon);
2181 if (rc < 0) {
2182 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2183 goto out_fail;
2184 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002185 } else {
2186 /* TODO: try to extend for non-cluster uses (eg multichannel) */
2187 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2188 rc = -EOPNOTSUPP;
2189 goto out_fail;
2190 }
2191 } else {
2192 cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2193 rc = -EOPNOTSUPP;
2194 goto out_fail;
2195 }
2196 }
2197#endif
Steve Frenchb618f002015-11-03 09:15:03 -06002198
Steve Frenchcae53f72019-09-03 17:49:46 -05002199 /* If the user really knows what they are doing they can override */
2200 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002201 if (ctx->cache_ro)
Steve Frenchcae53f72019-09-03 17:49:46 -05002202 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002203 else if (ctx->cache_rw)
Steve Frenchcae53f72019-09-03 17:49:46 -05002204 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2205 }
2206
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002207 if (ctx->no_lease) {
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302208 if (ses->server->vals->protocol_id == 0) {
2209 cifs_dbg(VFS,
2210 "SMB2 or later required for nolease option\n");
2211 rc = -EOPNOTSUPP;
2212 goto out_fail;
2213 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002214 tcon->no_lease = ctx->no_lease;
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302215 }
2216
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002217 /*
2218 * We can have only one retry value for a connection to a share so for
2219 * resources mounted more than once to the same server share the last
2220 * value passed in for the retry flag is used.
2221 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002222 tcon->retry = ctx->retry;
2223 tcon->nocase = ctx->nocase;
Steve French3c6e65e2020-10-21 00:15:42 -05002224 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002225 tcon->nohandlecache = ctx->nohandlecache;
Steve French3c6e65e2020-10-21 00:15:42 -05002226 else
Jiapeng Zhong2be449f2021-01-14 17:09:20 +08002227 tcon->nohandlecache = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002228 tcon->nodelete = ctx->nodelete;
2229 tcon->local_lease = ctx->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07002230 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002231
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302232 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002233 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302234 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002235
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302236 cifs_fscache_get_super_cookie(tcon);
2237
Jeff Laytond00c28d2010-04-24 07:57:44 -04002238 return tcon;
2239
2240out_fail:
2241 tconInfoFree(tcon);
2242 return ERR_PTR(rc);
2243}
2244
Jeff Layton9d002df2010-10-06 19:51:11 -04002245void
2246cifs_put_tlink(struct tcon_link *tlink)
2247{
2248 if (!tlink || IS_ERR(tlink))
2249 return;
2250
2251 if (!atomic_dec_and_test(&tlink->tl_count) ||
2252 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2253 tlink->tl_time = jiffies;
2254 return;
2255 }
2256
2257 if (!IS_ERR(tlink_tcon(tlink)))
2258 cifs_put_tcon(tlink_tcon(tlink));
2259 kfree(tlink);
2260 return;
2261}
Jeff Laytond00c28d2010-04-24 07:57:44 -04002262
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002263static int
2264compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2265{
2266 struct cifs_sb_info *old = CIFS_SB(sb);
2267 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002268 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2269 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002270
2271 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2272 return 0;
2273
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002274 if (old->mnt_cifs_serverino_autodisabled)
2275 newflags &= ~CIFS_MOUNT_SERVER_INUM;
2276
2277 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002278 return 0;
2279
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002280 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002281 * We want to share sb only if we don't specify an r/wsize or
2282 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002283 */
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002284 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002285 return 0;
2286
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002287 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002288 return 0;
2289
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002290 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2291 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002292 return 0;
2293
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002294 if (old->ctx->file_mode != new->ctx->file_mode ||
2295 old->ctx->dir_mode != new->ctx->dir_mode)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002296 return 0;
2297
2298 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2299 return 0;
2300
Steve French57804642021-02-24 12:12:53 -06002301 if (old->ctx->acregmax != new->ctx->acregmax)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002302 return 0;
Steve French4c9f9482021-02-23 15:50:57 -06002303 if (old->ctx->acdirmax != new->ctx->acdirmax)
2304 return 0;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002305
2306 return 1;
2307}
2308
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002309static int
2310match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2311{
2312 struct cifs_sb_info *old = CIFS_SB(sb);
2313 struct cifs_sb_info *new = mnt_data->cifs_sb;
Ronnie Sahlbergfe129262020-01-22 11:07:56 +10002314 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2315 old->prepath;
2316 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2317 new->prepath;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002318
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002319 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002320 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002321 else if (!old_set && !new_set)
2322 return 1;
2323
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002324 return 0;
2325}
2326
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002327int
2328cifs_match_super(struct super_block *sb, void *data)
2329{
2330 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002331 struct smb3_fs_context *ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002332 struct cifs_sb_info *cifs_sb;
2333 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00002334 struct cifs_ses *ses;
2335 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002336 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002337 int rc = 0;
2338
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002339 spin_lock(&cifs_tcp_ses_lock);
2340 cifs_sb = CIFS_SB(sb);
2341 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2342 if (IS_ERR(tlink)) {
2343 spin_unlock(&cifs_tcp_ses_lock);
2344 return rc;
2345 }
2346 tcon = tlink_tcon(tlink);
2347 ses = tcon->ses;
2348 tcp_srv = ses->server;
2349
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002350 ctx = mnt_data->ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002351
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002352 if (!match_server(tcp_srv, ctx) ||
2353 !match_session(ses, ctx) ||
2354 !match_tcon(tcon, ctx) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002355 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002356 rc = 0;
2357 goto out;
2358 }
2359
2360 rc = compare_mount_options(sb, mnt_data);
2361out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002362 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04002363 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002364 return rc;
2365}
2366
Jeff Layton09e50d52008-07-23 10:11:19 -04002367#ifdef CONFIG_DEBUG_LOCK_ALLOC
2368static struct lock_class_key cifs_key[2];
2369static struct lock_class_key cifs_slock_key[2];
2370
2371static inline void
2372cifs_reclassify_socket4(struct socket *sock)
2373{
2374 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002375 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002376 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2377 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2378}
2379
2380static inline void
2381cifs_reclassify_socket6(struct socket *sock)
2382{
2383 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002384 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002385 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2386 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2387}
2388#else
2389static inline void
2390cifs_reclassify_socket4(struct socket *sock)
2391{
2392}
2393
2394static inline void
2395cifs_reclassify_socket6(struct socket *sock)
2396{
2397}
2398#endif
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00002401static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Steve French50c2f752007-07-13 00:33:32 +00002403 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Steve French50c2f752007-07-13 00:33:32 +00002405 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* mask a nibble at a time and encode */
2407 target[j] = 'A' + (0x0F & (source[i] >> 4));
2408 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00002409 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411
2412}
2413
Ben Greear3eb9a882010-09-01 17:06:02 -07002414static int
2415bind_socket(struct TCP_Server_Info *server)
2416{
2417 int rc = 0;
2418 if (server->srcaddr.ss_family != AF_UNSPEC) {
2419 /* Bind to the specified local IP address */
2420 struct socket *socket = server->ssocket;
2421 rc = socket->ops->bind(socket,
2422 (struct sockaddr *) &server->srcaddr,
2423 sizeof(server->srcaddr));
2424 if (rc < 0) {
2425 struct sockaddr_in *saddr4;
2426 struct sockaddr_in6 *saddr6;
2427 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2428 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2429 if (saddr6->sin6_family == AF_INET6)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002430 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002431 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002432 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002433 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002434 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002435 }
2436 }
2437 return rc;
2438}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002441ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
2443 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002444 /*
2445 * some servers require RFC1001 sessinit before sending
2446 * negprot - BB check reconnection in case where second
2447 * sessinit is sent but no second negprot
2448 */
2449 struct rfc1002_session_packet *ses_init_buf;
2450 struct smb_hdr *smb_buf;
2451 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2452 GFP_KERNEL);
2453 if (ses_init_buf) {
2454 ses_init_buf->trailer.session_req.called_len = 32;
2455
Colin Ian King997152f2016-01-25 16:25:54 +00002456 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002457 rfc1002mangle(ses_init_buf->trailer.
2458 session_req.called_name,
2459 server->server_RFC1001_name,
2460 RFC1001_NAME_LEN_WITH_NULL);
2461 else
2462 rfc1002mangle(ses_init_buf->trailer.
2463 session_req.called_name,
2464 DEFAULT_CIFS_CALLED_NAME,
2465 RFC1001_NAME_LEN_WITH_NULL);
2466
2467 ses_init_buf->trailer.session_req.calling_len = 32;
2468
2469 /*
2470 * calling name ends in null (byte 16) from old smb
2471 * convention.
2472 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05002473 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002474 rfc1002mangle(ses_init_buf->trailer.
2475 session_req.calling_name,
2476 server->workstation_RFC1001_name,
2477 RFC1001_NAME_LEN_WITH_NULL);
2478 else
2479 rfc1002mangle(ses_init_buf->trailer.
2480 session_req.calling_name,
2481 "LINUX_CIFS_CLNT",
2482 RFC1001_NAME_LEN_WITH_NULL);
2483
2484 ses_init_buf->trailer.session_req.scope1 = 0;
2485 ses_init_buf->trailer.session_req.scope2 = 0;
2486 smb_buf = (struct smb_hdr *)ses_init_buf;
2487
2488 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002489 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002490 rc = smb_send(server, smb_buf, 0x44);
2491 kfree(ses_init_buf);
2492 /*
2493 * RFC1001 layer in at least one server
2494 * requires very short break before negprot
2495 * presumably because not expecting negprot
2496 * to follow so fast. This is a simple
2497 * solution that works without
2498 * complicating the code and causes no
2499 * significant slowing down on mount
2500 * for everyone else
2501 */
2502 usleep_range(1000, 2000);
2503 }
2504 /*
2505 * else the negprot may still work without this
2506 * even though malloc failed
2507 */
2508
2509 return rc;
2510}
2511
2512static int
2513generic_ip_connect(struct TCP_Server_Info *server)
2514{
2515 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00002516 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002517 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002518 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002519 struct sockaddr *saddr;
2520
2521 saddr = (struct sockaddr *) &server->dstaddr;
2522
2523 if (server->dstaddr.ss_family == AF_INET6) {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002524 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2525
2526 sport = ipv6->sin6_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002527 slen = sizeof(struct sockaddr_in6);
2528 sfamily = AF_INET6;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002529 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2530 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002531 } else {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002532 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2533
2534 sport = ipv4->sin_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002535 slen = sizeof(struct sockaddr_in);
2536 sfamily = AF_INET;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002537 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2538 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002541 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06002542 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2543 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (rc < 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002545 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002546 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002549
2550 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05002551 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002552 server->ssocket = socket;
2553 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002554 if (sfamily == AF_INET6)
2555 cifs_reclassify_socket6(socket);
2556 else
2557 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 }
2559
Ben Greear3eb9a882010-09-01 17:06:02 -07002560 rc = bind_socket(server);
2561 if (rc < 0)
2562 return rc;
2563
Jeff Laytond5c56052008-12-01 18:42:33 -05002564 /*
2565 * Eventually check for other socket options to change from
2566 * the default. sock_setsockopt not used because it expects
2567 * user space buffer
2568 */
2569 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00002570 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00002571
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002572 /* make the bufsizes depend on wsize/rsize and max requests */
2573 if (server->noautotune) {
2574 if (socket->sk->sk_sndbuf < (200 * 1024))
2575 socket->sk->sk_sndbuf = 200 * 1024;
2576 if (socket->sk->sk_rcvbuf < (140 * 1024))
2577 socket->sk->sk_rcvbuf = 140 * 1024;
2578 }
2579
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02002580 if (server->tcp_nodelay)
2581 tcp_sock_set_nodelay(socket->sk);
Steve French6a5fa2362010-01-01 01:28:43 +00002582
Joe Perchesf96637b2013-05-04 22:12:25 -05002583 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002584 socket->sk->sk_sndbuf,
2585 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2586
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002587 rc = socket->ops->connect(socket, saddr, slen,
2588 server->noblockcnt ? O_NONBLOCK : 0);
Paulo Alcantara (SUSE)d532cc72019-10-10 12:31:58 -03002589 /*
2590 * When mounting SMB root file systems, we do not want to block in
2591 * connect. Otherwise bail out and then let cifs_reconnect() perform
2592 * reconnect failover - if possible.
2593 */
2594 if (server->noblockcnt && rc == -EINPROGRESS)
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002595 rc = 0;
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002596 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002597 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002598 sock_release(socket);
2599 server->ssocket = NULL;
2600 return rc;
2601 }
2602
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002603 if (sport == htons(RFC1001_PORT))
2604 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00002605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return rc;
2607}
2608
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002609static int
2610ip_connect(struct TCP_Server_Info *server)
2611{
Steve French6da97912011-03-13 18:55:55 +00002612 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002613 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2614 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2615
2616 if (server->dstaddr.ss_family == AF_INET6)
2617 sport = &addr6->sin6_port;
2618 else
2619 sport = &addr->sin_port;
2620
2621 if (*sport == 0) {
2622 int rc;
2623
2624 /* try with 445 port at first */
2625 *sport = htons(CIFS_PORT);
2626
2627 rc = generic_ip_connect(server);
2628 if (rc >= 0)
2629 return rc;
2630
2631 /* if it failed, try with 139 port */
2632 *sport = htons(RFC1001_PORT);
2633 }
2634
2635 return generic_ip_connect(server);
2636}
2637
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002638void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002639 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Steve French8af18972007-02-14 04:42:51 +00002640{
Steve Frenchbc044992020-12-11 19:48:26 -06002641 /*
2642 * If we are reconnecting then should we check to see if
Steve French8af18972007-02-14 04:42:51 +00002643 * any requested capabilities changed locally e.g. via
2644 * remount but we can not do much about it here
2645 * if they have (even if we could detect it by the following)
2646 * Perhaps we could add a backpointer to array of sb from tcon
2647 * or if we change to make all sb to same share the same
2648 * sb as NFS - then we only have one backpointer to sb.
2649 * What if we wanted to mount the server share twice once with
Steve Frenchbc044992020-12-11 19:48:26 -06002650 * and once without posixacls or posix paths?
2651 */
Steve French8af18972007-02-14 04:42:51 +00002652 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00002653
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002654 if (ctx && ctx->no_linux_ext) {
Steve Frenchc18c8422007-07-18 23:21:09 +00002655 tcon->fsUnixInfo.Capability = 0;
2656 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05002657 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002658 return;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002659 } else if (ctx)
Steve Frenchc18c8422007-07-18 23:21:09 +00002660 tcon->unix_ext = 1; /* Unix Extensions supported */
2661
Jiapeng Zhong16a78852021-01-14 18:02:23 +08002662 if (!tcon->unix_ext) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002663 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002664 return;
2665 }
Steve French50c2f752007-07-13 00:33:32 +00002666
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002667 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00002668 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05002669 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve Frenchbc044992020-12-11 19:48:26 -06002670 /*
2671 * check for reconnect case in which we do not
2672 * want to change the mount behavior if we can avoid it
2673 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002674 if (ctx == NULL) {
Steve Frenchbc044992020-12-11 19:48:26 -06002675 /*
2676 * turn off POSIX ACL and PATHNAMES if not set
2677 * originally at mount time
2678 */
Steve French8af18972007-02-14 04:42:51 +00002679 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
2680 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002681 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
2682 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002683 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00002684 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002685 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002686 cifs_dbg(VFS, "possible reconnect error\n");
2687 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00002688 }
Steve French8af18972007-02-14 04:42:51 +00002689 }
Steve French50c2f752007-07-13 00:33:32 +00002690
Steve French6848b732011-05-26 18:38:54 +00002691 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002692 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00002693
Steve French8af18972007-02-14 04:42:51 +00002694 cap &= CIFS_UNIX_CAP_MASK;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002695 if (ctx && ctx->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00002696 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002697 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002698 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002699 if (cifs_sb)
2700 cifs_sb->mnt_cifs_flags |=
2701 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00002702 }
2703
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002704 if (ctx && ctx->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00002705 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002706 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002707 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002708 if (cifs_sb)
2709 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00002710 CIFS_MOUNT_POSIX_PATHS;
2711 }
Steve French50c2f752007-07-13 00:33:32 +00002712
Joe Perchesf96637b2013-05-04 22:12:25 -05002713 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00002714#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00002715 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002716 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002717 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002718 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002719 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002720 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002721 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002722 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002723 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002724 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002725 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002726 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002727 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002728 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00002729 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002730 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00002731 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002732 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00002733#endif /* CIFS_DEBUG2 */
2734 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002735 if (ctx == NULL)
Joe Perchesf96637b2013-05-04 22:12:25 -05002736 cifs_dbg(FYI, "resetting capabilities failed\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002737 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002738 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 +00002739
Steve French8af18972007-02-14 04:42:51 +00002740 }
2741 }
2742}
2743
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002744int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002745{
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002746 struct smb3_fs_context *ctx = cifs_sb->ctx;
2747
Jeff Layton2de970f2010-10-06 19:51:12 -04002748 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
2749
Al Viro2ced6f62011-06-17 09:20:04 -04002750 spin_lock_init(&cifs_sb->tlink_tree_lock);
2751 cifs_sb->tlink_tree = RB_ROOT;
2752
Frank Sorensonf52aa792020-02-12 15:31:48 -06002753 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002754 ctx->file_mode, ctx->dir_mode);
Steve French3b795212008-11-13 19:45:32 +00002755
Ronnie Sahlberg387ec582020-12-14 16:40:20 +10002756 /* this is needed for ASCII cp to Unicode converts */
2757 if (ctx->iocharset == NULL) {
2758 /* load_nls_default cannot return null */
2759 cifs_sb->local_nls = load_nls_default();
2760 } else {
2761 cifs_sb->local_nls = load_nls(ctx->iocharset);
2762 if (cifs_sb->local_nls == NULL) {
2763 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
2764 ctx->iocharset);
2765 return -ELIBACC;
2766 }
2767 }
2768 ctx->local_nls = cifs_sb->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05302769
Ronnie Sahlberg2d39f502020-12-14 16:40:25 +10002770 smb3_update_mnt_flags(cifs_sb);
2771
2772 if (ctx->direct_io)
Joe Perchesf96637b2013-05-04 22:12:25 -05002773 cifs_dbg(FYI, "mounting share using direct i/o\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002774 if (ctx->cache_ro) {
Steve French83bbfa72019-08-27 23:58:54 -05002775 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
2776 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002777 } else if (ctx->cache_rw) {
Steve French41e033f2019-08-30 02:12:41 -05002778 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
2779 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
2780 CIFS_MOUNT_RW_CACHE);
Steve French83bbfa72019-08-27 23:58:54 -05002781 }
Steve French3b795212008-11-13 19:45:32 +00002782
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002783 if ((ctx->cifs_acl) && (ctx->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05002784 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002785
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002786 if (ctx->prepath) {
2787 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002788 if (cifs_sb->prepath == NULL)
2789 return -ENOMEM;
Shyam Prasad Na738c932021-02-11 03:26:54 -08002790 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002791 }
2792
2793 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002794}
2795
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002796/* Release all succeed connections */
2797static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
2798 unsigned int xid,
2799 struct TCP_Server_Info *server,
2800 struct cifs_ses *ses, struct cifs_tcon *tcon)
2801{
2802 int rc = 0;
2803
2804 if (tcon)
2805 cifs_put_tcon(tcon);
2806 else if (ses)
2807 cifs_put_smb_ses(ses);
2808 else if (server)
2809 cifs_put_tcp_session(server, 0);
2810 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
2811 free_xid(xid);
2812}
2813
2814/* Get connections for tcp, ses and tcon */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002815static int mount_get_conns(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002816 unsigned int *xid,
2817 struct TCP_Server_Info **nserver,
2818 struct cifs_ses **nses, struct cifs_tcon **ntcon)
2819{
2820 int rc = 0;
2821 struct TCP_Server_Info *server;
2822 struct cifs_ses *ses;
2823 struct cifs_tcon *tcon;
2824
2825 *nserver = NULL;
2826 *nses = NULL;
2827 *ntcon = NULL;
2828
2829 *xid = get_xid();
2830
2831 /* get a reference to a tcp session */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002832 server = cifs_get_tcp_session(ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002833 if (IS_ERR(server)) {
2834 rc = PTR_ERR(server);
2835 return rc;
2836 }
2837
2838 *nserver = server;
2839
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002840 /* get a reference to a SMB session */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002841 ses = cifs_get_smb_ses(server, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002842 if (IS_ERR(ses)) {
2843 rc = PTR_ERR(ses);
2844 return rc;
2845 }
2846
2847 *nses = ses;
2848
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002849 if ((ctx->persistent == true) && (!(ses->server->capabilities &
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002850 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002851 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002852 return -EOPNOTSUPP;
2853 }
2854
2855 /* search for existing tcon to this server share */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002856 tcon = cifs_get_tcon(ses, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002857 if (IS_ERR(tcon)) {
2858 rc = PTR_ERR(tcon);
2859 return rc;
2860 }
2861
2862 *ntcon = tcon;
2863
2864 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
2865 if (tcon->posix_extensions)
2866 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
2867
2868 /* tell server which Unix caps we support */
2869 if (cap_unix(tcon->ses)) {
2870 /*
2871 * reset of caps checks mount to see if unix extensions disabled
2872 * for just this mount.
2873 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002874 reset_cifs_unix_caps(*xid, tcon, cifs_sb, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002875 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
2876 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
2877 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
2878 return -EACCES;
2879 } else
2880 tcon->unix_ext = 0; /* server does not support them */
2881
2882 /* do not care if a following call succeed - informational */
Steve French1981eba2019-08-29 22:33:38 -05002883 if (!tcon->pipe && server->ops->qfs_tcon) {
Amir Goldstein0f060932020-02-03 21:46:43 +02002884 server->ops->qfs_tcon(*xid, tcon, cifs_sb);
Steve French1981eba2019-08-29 22:33:38 -05002885 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
2886 if (tcon->fsDevInfo.DeviceCharacteristics &
Steve French52870d52019-10-01 21:25:46 -05002887 cpu_to_le32(FILE_READ_ONLY_DEVICE))
Steve French1981eba2019-08-29 22:33:38 -05002888 cifs_dbg(VFS, "mounted to read only share\n");
Steve French41e033f2019-08-30 02:12:41 -05002889 else if ((cifs_sb->mnt_cifs_flags &
2890 CIFS_MOUNT_RW_CACHE) == 0)
Steve French1981eba2019-08-29 22:33:38 -05002891 cifs_dbg(VFS, "read only mount of RW share\n");
Steve French41e033f2019-08-30 02:12:41 -05002892 /* no need to log a RW mount of a typical RW share */
Steve French1981eba2019-08-29 22:33:38 -05002893 }
2894 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002895
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002896 /*
2897 * Clamp the rsize/wsize mount arguments if they are too big for the server
Steve French0c2b5f72020-12-15 13:28:50 -06002898 * and set the rsize/wsize to the negotiated values if not passed in by
2899 * the user on mount
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002900 */
Steve French0c2b5f72020-12-15 13:28:50 -06002901 if ((cifs_sb->ctx->wsize == 0) ||
2902 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002903 cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
Steve French0c2b5f72020-12-15 13:28:50 -06002904 if ((cifs_sb->ctx->rsize == 0) ||
2905 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002906 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002907
2908 return 0;
2909}
2910
2911static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
2912 struct cifs_tcon *tcon)
2913{
2914 struct tcon_link *tlink;
2915
2916 /* hang the tcon off of the superblock */
2917 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
2918 if (tlink == NULL)
2919 return -ENOMEM;
2920
2921 tlink->tl_uid = ses->linux_uid;
2922 tlink->tl_tcon = tcon;
2923 tlink->tl_time = jiffies;
2924 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
2925 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
2926
2927 cifs_sb->master_tlink = tlink;
2928 spin_lock(&cifs_sb->tlink_tree_lock);
2929 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
2930 spin_unlock(&cifs_sb->tlink_tree_lock);
2931
2932 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
2933 TLINK_IDLE_EXPIRE);
2934 return 0;
2935}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04002936
Steve French2d6d5892009-04-09 00:36:44 +00002937#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06002938/*
2939 * cifs_build_path_to_root returns full path to root when we do not have an
Steve French201023c2021-02-15 11:03:45 -06002940 * existing connection (tcon)
Steve French6d3ea7e2012-11-28 22:34:41 -06002941 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04002942static char *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002943build_unc_path_to_root(const struct smb3_fs_context *ctx,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02002944 const struct cifs_sb_info *cifs_sb, bool useppath)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04002945{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04002946 char *full_path, *pos;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002947 unsigned int pplen = useppath && ctx->prepath ?
2948 strlen(ctx->prepath) + 1 : 0;
2949 unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04002950
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10002951 if (unc_len > MAX_TREE_SIZE)
2952 return ERR_PTR(-EINVAL);
2953
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04002954 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04002955 if (full_path == NULL)
2956 return ERR_PTR(-ENOMEM);
2957
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002958 memcpy(full_path, ctx->UNC, unc_len);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04002959 pos = full_path + unc_len;
2960
2961 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04002962 *pos = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002963 memcpy(pos + 1, ctx->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04002964 pos += pplen;
2965 }
2966
2967 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00002968 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05002969 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04002970 return full_path;
2971}
Sean Finneydd613942011-04-11 13:19:30 +00002972
Paulo Alcantara1c780222018-11-14 16:24:03 -02002973/**
2974 * expand_dfs_referral - Perform a dfs referral query and update the cifs_sb
2975 *
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10002976 * If a referral is found, cifs_sb->ctx->mount_options will be (re-)allocated
Sean Finney046462a2011-04-11 13:19:33 +00002977 * to a string containing updated options for the submount. Otherwise it
2978 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00002979 *
2980 * Returns the rc from get_dfs_path to the caller, which can be used to
2981 * determine whether there were referrals.
2982 */
2983static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04002984expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002985 struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
Paulo Alcantara7efd0812020-07-21 09:36:44 -03002986 char *ref_path)
Sean Finneydd613942011-04-11 13:19:30 +00002987{
2988 int rc;
Paulo Alcantara1c780222018-11-14 16:24:03 -02002989 struct dfs_info3_param referral = {0};
Paulo Alcantara7efd0812020-07-21 09:36:44 -03002990 char *full_path = NULL, *mdata = NULL;
Sean Finneydd613942011-04-11 13:19:30 +00002991
Aurelien Aptel83930722018-09-20 18:10:25 -07002992 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
2993 return -EREMOTE;
2994
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002995 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
Sean Finneydd613942011-04-11 13:19:30 +00002996 if (IS_ERR(full_path))
2997 return PTR_ERR(full_path);
2998
Paulo Alcantara1c780222018-11-14 16:24:03 -02002999 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
3000 ref_path, &referral, NULL);
3001 if (!rc) {
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003002 char *fake_devname = NULL;
3003
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003004 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options,
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003005 full_path + 1, &referral,
3006 &fake_devname);
Paulo Alcantara1c780222018-11-14 16:24:03 -02003007 free_dfs_info_param(&referral);
Sean Finney046462a2011-04-11 13:19:33 +00003008
Sean Finneydd613942011-04-11 13:19:30 +00003009 if (IS_ERR(mdata)) {
3010 rc = PTR_ERR(mdata);
3011 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003012 } else {
Ronnie Sahlbergabd4af42021-02-08 16:48:31 +10003013 /*
3014 * We can not clear out the whole structure since we
3015 * no longer have an explicit function to parse
3016 * a mount-string. Instead we need to clear out the
3017 * individual fields that are no longer valid.
3018 */
3019 kfree(ctx->prepath);
3020 ctx->prepath = NULL;
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003021 rc = cifs_setup_volume_info(ctx, mdata, fake_devname);
Sean Finneydd613942011-04-11 13:19:30 +00003022 }
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003023 kfree(fake_devname);
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003024 kfree(cifs_sb->ctx->mount_options);
3025 cifs_sb->ctx->mount_options = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003026 }
3027 kfree(full_path);
3028 return rc;
3029}
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003030
3031static inline int get_next_dfs_tgt(const char *path,
3032 struct dfs_cache_tgt_list *tgt_list,
3033 struct dfs_cache_tgt_iterator **tgt_it)
3034{
3035 if (!*tgt_it)
3036 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
3037 else
3038 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
3039 return !*tgt_it ? -EHOSTDOWN : 0;
3040}
3041
3042static int update_vol_info(const struct dfs_cache_tgt_iterator *tgt_it,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003043 struct smb3_fs_context *fake_ctx, struct smb3_fs_context *ctx)
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003044{
3045 const char *tgt = dfs_cache_get_tgt_name(tgt_it);
3046 int len = strlen(tgt) + 2;
3047 char *new_unc;
3048
3049 new_unc = kmalloc(len, GFP_KERNEL);
3050 if (!new_unc)
3051 return -ENOMEM;
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10003052 scnprintf(new_unc, len, "\\%s", tgt);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003053
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003054 kfree(ctx->UNC);
3055 ctx->UNC = new_unc;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003056
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003057 if (fake_ctx->prepath) {
3058 kfree(ctx->prepath);
3059 ctx->prepath = fake_ctx->prepath;
3060 fake_ctx->prepath = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003061 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003062 memcpy(&ctx->dstaddr, &fake_ctx->dstaddr, sizeof(ctx->dstaddr));
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003063
3064 return 0;
3065}
3066
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003067static int do_dfs_failover(const char *path, const char *full_path, struct cifs_sb_info *cifs_sb,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003068 struct smb3_fs_context *ctx, struct cifs_ses *root_ses,
3069 unsigned int *xid, struct TCP_Server_Info **server,
3070 struct cifs_ses **ses, struct cifs_tcon **tcon)
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003071{
3072 int rc;
Paulo Alcantara85132222021-02-24 20:59:21 -03003073 struct dfs_cache_tgt_list tgt_list = {0};
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003074 struct dfs_cache_tgt_iterator *tgt_it = NULL;
Paulo Alcantara85132222021-02-24 20:59:21 -03003075 struct smb3_fs_context tmp_ctx = {NULL};
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003076
3077 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
3078 return -EOPNOTSUPP;
3079
Paulo Alcantara85132222021-02-24 20:59:21 -03003080 cifs_dbg(FYI, "%s: path=%s full_path=%s\n", __func__, path, full_path);
3081
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003082 rc = dfs_cache_noreq_find(path, NULL, &tgt_list);
3083 if (rc)
3084 return rc;
Paulo Alcantara85132222021-02-24 20:59:21 -03003085 /*
3086 * We use a 'tmp_ctx' here because we need pass it down to the mount_{get,put} functions to
3087 * test connection against new DFS targets.
3088 */
3089 rc = smb3_fs_context_dup(&tmp_ctx, ctx);
3090 if (rc)
3091 goto out;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003092
3093 for (;;) {
Paulo Alcantara85132222021-02-24 20:59:21 -03003094 struct dfs_info3_param ref = {0};
3095 char *fake_devname = NULL, *mdata = NULL;
3096
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003097 /* Get next DFS target server - if any */
3098 rc = get_next_dfs_tgt(path, &tgt_list, &tgt_it);
3099 if (rc)
3100 break;
Paulo Alcantara85132222021-02-24 20:59:21 -03003101
3102 rc = dfs_cache_get_tgt_referral(path, tgt_it, &ref);
3103 if (rc)
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003104 break;
Paulo Alcantara85132222021-02-24 20:59:21 -03003105
3106 cifs_dbg(FYI, "%s: old ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC,
3107 tmp_ctx.prepath);
3108
3109 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, &ref,
3110 &fake_devname);
3111 free_dfs_info_param(&ref);
3112
3113 if (IS_ERR(mdata)) {
3114 rc = PTR_ERR(mdata);
3115 mdata = NULL;
3116 } else
3117 rc = cifs_setup_volume_info(&tmp_ctx, mdata, fake_devname);
3118
3119 kfree(mdata);
3120 kfree(fake_devname);
3121
3122 if (rc)
3123 break;
3124
3125 cifs_dbg(FYI, "%s: new ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC,
3126 tmp_ctx.prepath);
3127
3128 mount_put_conns(cifs_sb, *xid, *server, *ses, *tcon);
3129 rc = mount_get_conns(&tmp_ctx, cifs_sb, xid, server, ses, tcon);
3130 if (!rc || (*server && *ses)) {
3131 /*
3132 * We were able to connect to new target server. Update current context with
3133 * new target server.
3134 */
3135 rc = update_vol_info(tgt_it, &tmp_ctx, ctx);
3136 break;
3137 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003138 }
3139 if (!rc) {
Paulo Alcantara85132222021-02-24 20:59:21 -03003140 cifs_dbg(FYI, "%s: final ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC,
3141 tmp_ctx.prepath);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003142 /*
Paulo Alcantara85132222021-02-24 20:59:21 -03003143 * Update DFS target hint in DFS referral cache with the target server we
3144 * successfully reconnected to.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003145 */
Paulo Alcantara85132222021-02-24 20:59:21 -03003146 rc = dfs_cache_update_tgthint(*xid, root_ses ? root_ses : *ses, cifs_sb->local_nls,
3147 cifs_remap(cifs_sb), path, tgt_it);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003148 }
Paulo Alcantara85132222021-02-24 20:59:21 -03003149
3150out:
3151 smb3_cleanup_fs_context_contents(&tmp_ctx);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003152 dfs_cache_free_tgts(&tgt_list);
3153 return rc;
3154}
Steve French2d6d5892009-04-09 00:36:44 +00003155#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003156
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -06003157/* TODO: all callers to this are broken. We are not parsing mount_options here
3158 * we should pass a clone of the original context?
3159 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003160int
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003161cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003163 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003164
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003165 smb3_parse_devname(devname, ctx);
3166
3167 if (mntopts) {
3168 char *ip;
3169
3170 cifs_dbg(FYI, "%s: mntopts=%s\n", __func__, mntopts);
3171 rc = smb3_parse_opt(mntopts, "ip", &ip);
3172 if (!rc && !cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip,
3173 strlen(ip))) {
3174 cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
3175 return -EINVAL;
3176 }
3177 }
3178
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003179 if (ctx->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003180 cifs_dbg(FYI, "Anonymous login\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003181 kfree(ctx->username);
3182 ctx->username = NULL;
3183 } else if (ctx->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 /* BB fixme parse for domain name here */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003185 cifs_dbg(FYI, "Username: %s\n", ctx->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003187 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003188 /* In userspace mount helper we can get user name from alternate
3189 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003190 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003193 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04003194}
3195
Aurelien Aptela6b50582016-05-25 19:59:09 +02003196static int
3197cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3198 unsigned int xid,
3199 struct cifs_tcon *tcon,
3200 struct cifs_sb_info *cifs_sb,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003201 char *full_path,
3202 int added_treename)
Aurelien Aptela6b50582016-05-25 19:59:09 +02003203{
3204 int rc;
3205 char *s;
3206 char sep, tmp;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003207 int skip = added_treename ? 1 : 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +02003208
3209 sep = CIFS_DIR_SEP(cifs_sb);
3210 s = full_path;
3211
3212 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3213 while (rc == 0) {
3214 /* skip separators */
3215 while (*s == sep)
3216 s++;
3217 if (!*s)
3218 break;
3219 /* next separator */
3220 while (*s && *s != sep)
3221 s++;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003222 /*
3223 * if the treename is added, we then have to skip the first
3224 * part within the separators
3225 */
3226 if (skip) {
3227 skip = 0;
3228 continue;
3229 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02003230 /*
3231 * temporarily null-terminate the path at the end of
3232 * the current component
3233 */
3234 tmp = *s;
3235 *s = 0;
3236 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3237 full_path);
3238 *s = tmp;
3239 }
3240 return rc;
3241}
3242
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003243/*
3244 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
3245 * otherwise 0.
3246 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003247static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003248 const unsigned int xid,
3249 struct TCP_Server_Info *server,
3250 struct cifs_tcon *tcon)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003251{
Jeff Layton1daaae82012-03-21 06:30:40 -04003252 int rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003253 char *full_path;
3254
3255 if (!server->ops->is_path_accessible)
3256 return -EOPNOTSUPP;
3257
3258 /*
3259 * cifs_build_path_to_root works only when we have a valid tcon
3260 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003261 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003262 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3263 if (full_path == NULL)
3264 return -ENOMEM;
3265
3266 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3267
3268 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3269 full_path);
3270 if (rc != 0 && rc != -EREMOTE) {
3271 kfree(full_path);
3272 return rc;
3273 }
3274
3275 if (rc != -EREMOTE) {
3276 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003277 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003278 if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07003279 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 -02003280 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3281 rc = 0;
3282 }
3283 }
3284
3285 kfree(full_path);
3286 return rc;
3287}
3288
3289#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003290static void set_root_ses(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3291 struct cifs_ses **root_ses)
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03003292{
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003293 if (ses) {
3294 spin_lock(&cifs_tcp_ses_lock);
3295 ses->ses_count++;
Aurelien Aptel59463eb2020-12-03 19:46:08 +01003296 if (ses->tcon_ipc)
3297 ses->tcon_ipc->remap = cifs_remap(cifs_sb);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003298 spin_unlock(&cifs_tcp_ses_lock);
3299 }
3300 *root_ses = ses;
3301}
3302
3303static void put_root_ses(struct cifs_ses *ses)
3304{
3305 if (ses)
3306 cifs_put_smb_ses(ses);
3307}
3308
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003309/* Set up next dfs prefix path in @dfs_path */
3310static int next_dfs_prepath(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx,
3311 const unsigned int xid, struct TCP_Server_Info *server,
3312 struct cifs_tcon *tcon, char **dfs_path)
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003313{
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003314 char *path, *npath;
3315 int added_treename = is_tcon_dfs(tcon);
3316 int rc;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003317
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003318 path = cifs_build_path_to_root(ctx, cifs_sb, tcon, added_treename);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003319 if (!path)
3320 return -ENOMEM;
3321
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003322 rc = is_path_remote(cifs_sb, ctx, xid, server, tcon);
3323 if (rc == -EREMOTE) {
3324 struct smb3_fs_context v = {NULL};
3325 /* if @path contains a tree name, skip it in the prefix path */
3326 if (added_treename) {
3327 rc = smb3_parse_devname(path, &v);
3328 if (rc)
3329 goto out;
3330 npath = build_unc_path_to_root(&v, cifs_sb, true);
3331 smb3_cleanup_fs_context_contents(&v);
3332 } else {
3333 v.UNC = ctx->UNC;
3334 v.prepath = path + 1;
3335 npath = build_unc_path_to_root(&v, cifs_sb, true);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003336 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003337
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003338 if (IS_ERR(npath)) {
3339 rc = PTR_ERR(npath);
3340 goto out;
3341 }
3342
3343 kfree(*dfs_path);
3344 *dfs_path = npath;
3345 rc = -EREMOTE;
3346 }
3347
3348out:
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003349 kfree(path);
3350 return rc;
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03003351}
3352
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003353/* Check if resolved targets can handle any DFS referrals */
3354static int is_referral_server(const char *ref_path, struct cifs_tcon *tcon, bool *ref_server)
3355{
3356 int rc;
3357 struct dfs_info3_param ref = {0};
3358
3359 if (is_tcon_dfs(tcon)) {
3360 *ref_server = true;
3361 } else {
3362 cifs_dbg(FYI, "%s: ref_path=%s\n", __func__, ref_path);
3363
3364 rc = dfs_cache_noreq_find(ref_path, &ref, NULL);
3365 if (rc) {
3366 cifs_dbg(VFS, "%s: dfs_cache_noreq_find: failed (rc=%d)\n", __func__, rc);
3367 return rc;
3368 }
3369 cifs_dbg(FYI, "%s: ref.flags=0x%x\n", __func__, ref.flags);
3370 /*
3371 * Check if all targets are capable of handling DFS referrals as per
3372 * MS-DFSC 2.2.4 RESP_GET_DFS_REFERRAL.
3373 */
3374 *ref_server = !!(ref.flags & DFSREF_REFERRAL_SERVER);
3375 free_dfs_info_param(&ref);
3376 }
3377 return 0;
3378}
3379
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003380int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003381{
3382 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003383 unsigned int xid;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003384 struct TCP_Server_Info *server = NULL;
3385 struct cifs_ses *ses = NULL, *root_ses = NULL;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003386 struct cifs_tcon *tcon = NULL;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003387 int count = 0;
3388 char *ref_path = NULL, *full_path = NULL;
3389 char *oldmnt = NULL;
3390 char *mntdata = NULL;
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003391 bool ref_server = false;
Al Virodd854462011-06-17 08:24:42 -04003392
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003393 rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003394 /*
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003395 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
3396 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003397 *
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003398 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
3399 * to respond with PATH_NOT_COVERED to requests that include the prefix.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003400 */
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003401 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
3402 dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb), ctx->UNC + 1, NULL,
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003403 NULL)) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003404 if (rc)
3405 goto error;
3406 /* Check if it is fully accessible and then mount it */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003407 rc = is_path_remote(cifs_sb, ctx, xid, server, tcon);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003408 if (!rc)
3409 goto out;
3410 if (rc != -EREMOTE)
3411 goto error;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003412 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003413 /* Save mount options */
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003414 mntdata = kstrndup(cifs_sb->ctx->mount_options,
3415 strlen(cifs_sb->ctx->mount_options), GFP_KERNEL);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003416 if (!mntdata) {
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003417 rc = -ENOMEM;
3418 goto error;
3419 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003420 /* Get path of DFS root */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003421 ref_path = build_unc_path_to_root(ctx, cifs_sb, false);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003422 if (IS_ERR(ref_path)) {
3423 rc = PTR_ERR(ref_path);
3424 ref_path = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003425 goto error;
3426 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003427
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003428 set_root_ses(cifs_sb, ses, &root_ses);
3429 do {
3430 /* Save full path of last DFS path we used to resolve final target server */
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003431 kfree(full_path);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003432 full_path = build_unc_path_to_root(ctx, cifs_sb, !!count);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003433 if (IS_ERR(full_path)) {
3434 rc = PTR_ERR(full_path);
Dan Carpenter20b135e2020-08-05 12:52:07 +03003435 full_path = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003436 break;
3437 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003438 /* Chase referral */
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003439 oldmnt = cifs_sb->ctx->mount_options;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003440 rc = expand_dfs_referral(xid, root_ses, ctx, cifs_sb, ref_path + 1);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003441 if (rc)
3442 break;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003443 /* Connect to new DFS target only if we were redirected */
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003444 if (oldmnt != cifs_sb->ctx->mount_options) {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003445 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003446 rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003447 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003448 if (rc && !server && !ses) {
3449 /* Failed to connect. Try to connect to other targets in the referral. */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003450 rc = do_dfs_failover(ref_path + 1, full_path, cifs_sb, ctx, root_ses, &xid,
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003451 &server, &ses, &tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003452 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003453 if (rc == -EACCES || rc == -EOPNOTSUPP || !server || !ses)
3454 break;
3455 if (!tcon)
3456 continue;
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003457
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003458 /* Make sure that requests go through new root servers */
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003459 rc = is_referral_server(ref_path + 1, tcon, &ref_server);
3460 if (rc)
3461 break;
3462 if (ref_server) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003463 put_root_ses(root_ses);
3464 set_root_ses(cifs_sb, ses, &root_ses);
3465 }
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003466
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003467 /* Get next dfs path and then continue chasing them if -EREMOTE */
3468 rc = next_dfs_prepath(cifs_sb, ctx, xid, server, tcon, &ref_path);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003469 /* Prevent recursion on broken link referrals */
3470 if (rc == -EREMOTE && ++count > MAX_NESTED_LINKS)
3471 rc = -ELOOP;
3472 } while (rc == -EREMOTE);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003473
Jeff Layton9d002df2010-10-06 19:51:11 -04003474 if (rc)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003475 goto error;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003476 put_root_ses(root_ses);
3477 root_ses = NULL;
3478 kfree(ref_path);
3479 ref_path = NULL;
3480 /*
3481 * Store DFS full path in both superblock and tree connect structures.
3482 *
3483 * For DFS root mounts, the prefix path (cifs_sb->prepath) is preserved during reconnect so
3484 * only the root path is set in cifs_sb->origin_fullpath and tcon->dfs_path. And for DFS
3485 * links, the prefix path is included in both and may be changed during reconnect. See
3486 * cifs_tree_connect().
3487 */
3488 cifs_sb->origin_fullpath = kstrndup(full_path, strlen(full_path), GFP_KERNEL);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003489 if (!cifs_sb->origin_fullpath) {
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003490 rc = -ENOMEM;
3491 goto error;
3492 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003493 spin_lock(&cifs_tcp_ses_lock);
3494 tcon->dfs_path = full_path;
3495 full_path = NULL;
3496 tcon->remap = cifs_remap(cifs_sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003497 spin_unlock(&cifs_tcp_ses_lock);
3498
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003499 /* Add original context for DFS cache to be used when refreshing referrals */
3500 rc = dfs_cache_add_vol(mntdata, ctx, cifs_sb->origin_fullpath);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003501 if (rc)
Paulo Alcantarae511d312018-11-14 17:16:44 -02003502 goto error;
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01003503 /*
3504 * After reconnecting to a different server, unique ids won't
3505 * match anymore, so we disable serverino. This prevents
3506 * dentry revalidation to think the dentry are stale (ESTALE).
3507 */
3508 cifs_autodisable_serverino(cifs_sb);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003509 /*
3510 * Force the use of prefix path to support failover on DFS paths that
3511 * resolve to targets that have different prefix paths.
3512 */
3513 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3514 kfree(cifs_sb->prepath);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003515 cifs_sb->prepath = ctx->prepath;
3516 ctx->prepath = NULL;
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003517
Jeff Layton70fe7dc2007-11-16 22:21:07 +00003518out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003519 free_xid(xid);
Ronnie Sahlberg387ec582020-12-14 16:40:20 +10003520 cifs_try_adding_channels(cifs_sb, ses);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003521 return mount_setup_tlink(cifs_sb, ses, tcon);
3522
3523error:
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003524 kfree(ref_path);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003525 kfree(full_path);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003526 kfree(mntdata);
3527 kfree(cifs_sb->origin_fullpath);
3528 put_root_ses(root_ses);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003529 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return rc;
3531}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003532#else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003533int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003534{
3535 int rc = 0;
3536 unsigned int xid;
3537 struct cifs_ses *ses;
3538 struct cifs_tcon *tcon;
3539 struct TCP_Server_Info *server;
3540
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003541 rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003542 if (rc)
3543 goto error;
3544
3545 if (tcon) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003546 rc = is_path_remote(cifs_sb, ctx, xid, server, tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003547 if (rc == -EREMOTE)
3548 rc = -EOPNOTSUPP;
3549 if (rc)
3550 goto error;
3551 }
3552
3553 free_xid(xid);
3554
3555 return mount_setup_tlink(cifs_sb, ses, tcon);
3556
3557error:
3558 mount_put_conns(cifs_sb, xid, server, ses, tcon);
3559 return rc;
3560}
3561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Jeff Layton8d1bca32011-06-11 21:17:10 -04003563/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01003564 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04003565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003567CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00003568 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 const struct nls_table *nls_codepage)
3570{
3571 struct smb_hdr *smb_buffer;
3572 struct smb_hdr *smb_buffer_response;
3573 TCONX_REQ *pSMB;
3574 TCONX_RSP *pSMBr;
3575 unsigned char *bcc_ptr;
3576 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05003577 int length;
3578 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
3580 if (ses == NULL)
3581 return -EIO;
3582
3583 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00003584 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00003586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 smb_buffer_response = smb_buffer;
3588
3589 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3590 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003591
Pavel Shilovsky88257362012-05-23 14:01:59 +04003592 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 smb_buffer->Uid = ses->Suid;
3594 pSMB = (TCONX_REQ *) smb_buffer;
3595 pSMBr = (TCONX_RSP *) smb_buffer_response;
3596
3597 pSMB->AndXCommand = 0xFF;
3598 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01003600 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08003601 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003602 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003603 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003604 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003605 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06003606 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08003607 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
3608 specified as required (when that support is added to
3609 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00003610 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08003611 by Samba (not sure whether other servers allow
3612 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00003613#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04003614 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05003615 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05003616 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00003617 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05003618 SECMODE_PW_ENCRYPT ? true : false,
3619 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00003620 else
3621#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06003622 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05003623 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05003624 if (rc) {
3625 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
3626 __func__, rc);
3627 cifs_buf_release(smb_buffer);
3628 return rc;
3629 }
Steve Frencheeac8042006-01-13 21:34:58 -08003630
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06003631 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003632 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00003633 /* must align unicode strings */
3634 *bcc_ptr = 0; /* null byte password */
3635 bcc_ptr++;
3636 }
Steve Frencheeac8042006-01-13 21:34:58 -08003637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Jeff Layton38d77c52013-05-26 07:01:00 -04003639 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3641
3642 if (ses->capabilities & CAP_STATUS32) {
3643 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3644 }
3645 if (ses->capabilities & CAP_DFS) {
3646 smb_buffer->Flags2 |= SMBFLG2_DFS;
3647 }
3648 if (ses->capabilities & CAP_UNICODE) {
3649 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3650 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06003651 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00003652 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00003653 (/* server len*/ + 256 /* share len */), nls_codepage);
3654 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 bcc_ptr += 2; /* skip trailing null */
3656 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 strcpy(bcc_ptr, tree);
3658 bcc_ptr += strlen(tree) + 1;
3659 }
3660 strcpy(bcc_ptr, "?????");
3661 bcc_ptr += strlen("?????");
3662 bcc_ptr += 1;
3663 count = bcc_ptr - &pSMB->Password[0];
Qinglang Miao1a0e7f72020-07-25 16:56:01 +08003664 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 pSMB->ByteCount = cpu_to_le16(count);
3666
Steve French133672e2007-11-13 22:41:37 +00003667 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05003668 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003671 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00003672 bool is_unicode;
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00003675 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 tcon->tid = smb_buffer_response->Tid;
3677 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05003678 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003679 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00003680 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3681 is_unicode = true;
3682 else
3683 is_unicode = false;
3684
Jeff Laytoncc20c032009-04-30 07:16:21 -04003685
Steve French50c2f752007-07-13 00:33:32 +00003686 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00003687 if (length == 3) {
3688 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3689 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003690 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01003691 tcon->ipc = true;
3692 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00003693 }
3694 } else if (length == 2) {
3695 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3696 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05003697 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00003698 }
3699 }
Steve French50c2f752007-07-13 00:33:32 +00003700 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04003701 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05003702 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04003703
3704 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04003705 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06003706 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00003707 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04003708 nls_codepage);
3709
Joe Perchesf96637b2013-05-04 22:12:25 -05003710 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003711
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003712 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00003713 (smb_buffer_response->WordCount == 7))
3714 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00003715 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3716 else
3717 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05003718 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 }
3720
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003721 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 return rc;
3723}
3724
Al Viro2e32cf52013-10-03 12:53:37 -04003725static void delayed_free(struct rcu_head *p)
3726{
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003727 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
3728
3729 unload_nls(cifs_sb->local_nls);
Ronnie Sahlbergc741cba2020-12-14 16:40:16 +10003730 smb3_cleanup_fs_context(cifs_sb->ctx);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003731 kfree(cifs_sb);
Al Viro2e32cf52013-10-03 12:53:37 -04003732}
3733
Al Viro2a9b9952011-06-17 09:27:16 -04003734void
3735cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
Jeff Laytonb647c352010-10-28 11:16:44 -04003737 struct rb_root *root = &cifs_sb->tlink_tree;
3738 struct rb_node *node;
3739 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Jeff Layton2de970f2010-10-06 19:51:12 -04003741 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3742
Jeff Laytonb647c352010-10-28 11:16:44 -04003743 spin_lock(&cifs_sb->tlink_tree_lock);
3744 while ((node = rb_first(root))) {
3745 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3746 cifs_get_tlink(tlink);
3747 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3748 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00003749
Jeff Laytonb647c352010-10-28 11:16:44 -04003750 spin_unlock(&cifs_sb->tlink_tree_lock);
3751 cifs_put_tlink(tlink);
3752 spin_lock(&cifs_sb->tlink_tree_lock);
3753 }
3754 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003755
Aurelien Aptela6b50582016-05-25 19:59:09 +02003756 kfree(cifs_sb->prepath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003757#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarae511d312018-11-14 17:16:44 -02003758 dfs_cache_del_vol(cifs_sb->origin_fullpath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003759 kfree(cifs_sb->origin_fullpath);
3760#endif
Al Viro2e32cf52013-10-03 12:53:37 -04003761 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00003762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003764int
3765cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
3767 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02003768 struct TCP_Server_Info *server = cifs_ses_server(ses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003770 if (!server->ops->need_neg || !server->ops->negotiate)
3771 return -ENOSYS;
3772
Jeff Layton198b5682010-04-24 07:57:48 -04003773 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003774 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04003775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003777 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04003778 if (rc == 0) {
3779 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04003780 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04003781 server->tcpStatus = CifsGood;
3782 else
3783 rc = -EHOSTDOWN;
3784 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
Steve French26b994f2008-08-06 05:11:33 +00003786
Jeff Layton198b5682010-04-24 07:57:48 -04003787 return rc;
3788}
Steve French26b994f2008-08-06 05:11:33 +00003789
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003790int
3791cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3792 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04003793{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003794 int rc = -ENOSYS;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02003795 struct TCP_Server_Info *server = cifs_ses_server(ses);
Jeff Layton198b5682010-04-24 07:57:48 -04003796
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003797 if (!ses->binding) {
3798 ses->capabilities = server->capabilities;
YANG LIed6b1922021-01-11 17:15:28 +08003799 if (!linuxExtEnabled)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003800 ses->capabilities &= (~server->vals->cap_unix);
3801
3802 if (ses->auth_key.response) {
3803 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
3804 ses->auth_key.response);
3805 kfree(ses->auth_key.response);
3806 ses->auth_key.response = NULL;
3807 ses->auth_key.len = 0;
3808 }
3809 }
Steve French20418ac2009-04-30 16:13:32 +00003810
Joe Perchesf96637b2013-05-04 22:12:25 -05003811 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00003812 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003813
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003814 if (server->ops->sess_setup)
3815 rc = server->ops->sess_setup(xid, ses, nls_info);
3816
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05003817 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003818 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05003819
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return rc;
3821}
3822
Jeff Layton8a8798a2012-01-17 16:09:15 -05003823static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003824cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003825{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003826 ctx->sectype = ses->sectype;
Jeff Layton3f618222013-06-12 19:52:14 -05003827
3828 /* krb5 is special, since we don't need username or pw */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003829 if (ctx->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003830 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003831
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003832 return cifs_set_cifscreds(ctx, ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003833}
3834
Steve French96daf2b2011-05-27 04:34:02 +00003835static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003836cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04003837{
Jeff Layton8a8798a2012-01-17 16:09:15 -05003838 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00003839 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3840 struct cifs_ses *ses;
3841 struct cifs_tcon *tcon = NULL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003842 struct smb3_fs_context *ctx;
Jeff Layton9d002df2010-10-06 19:51:11 -04003843
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003844 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3845 if (ctx == NULL)
Dan Carpenter803ab972012-01-24 11:39:22 +03003846 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04003847
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003848 ctx->local_nls = cifs_sb->local_nls;
3849 ctx->linux_uid = fsuid;
3850 ctx->cred_uid = fsuid;
3851 ctx->UNC = master_tcon->treeName;
3852 ctx->retry = master_tcon->retry;
3853 ctx->nocase = master_tcon->nocase;
3854 ctx->nohandlecache = master_tcon->nohandlecache;
3855 ctx->local_lease = master_tcon->local_lease;
3856 ctx->no_lease = master_tcon->no_lease;
3857 ctx->resilient = master_tcon->use_resilient;
3858 ctx->persistent = master_tcon->use_persistent;
3859 ctx->handle_timeout = master_tcon->handle_timeout;
3860 ctx->no_linux_ext = !master_tcon->unix_ext;
3861 ctx->linux_ext = master_tcon->posix_extensions;
3862 ctx->sectype = master_tcon->ses->sectype;
3863 ctx->sign = master_tcon->ses->sign;
3864 ctx->seal = master_tcon->seal;
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06003865#ifdef CONFIG_CIFS_SWN_UPCALL
3866 ctx->witness = master_tcon->use_witness;
3867#endif
Jeff Layton9d002df2010-10-06 19:51:11 -04003868
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003869 rc = cifs_set_vol_auth(ctx, master_tcon->ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003870 if (rc) {
3871 tcon = ERR_PTR(rc);
3872 goto out;
3873 }
Jeff Layton9d002df2010-10-06 19:51:11 -04003874
3875 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303876 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003877 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303878 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003879
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003880 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003881 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00003882 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003883 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04003884 goto out;
3885 }
3886
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003887 tcon = cifs_get_tcon(ses, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003888 if (IS_ERR(tcon)) {
3889 cifs_put_smb_ses(ses);
3890 goto out;
3891 }
3892
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003893 if (cap_unix(ses))
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003894 reset_cifs_unix_caps(0, tcon, NULL, ctx);
Steve Frenchb3266142018-05-20 23:41:10 -05003895
Jeff Layton9d002df2010-10-06 19:51:11 -04003896out:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003897 kfree(ctx->username);
3898 kfree_sensitive(ctx->password);
3899 kfree(ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003900
3901 return tcon;
3902}
3903
Steve French96daf2b2011-05-27 04:34:02 +00003904struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04003905cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
3906{
3907 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
3908}
3909
Jeff Laytonb647c352010-10-28 11:16:44 -04003910/* find and return a tlink with given uid */
3911static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003912tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04003913{
3914 struct rb_node *node = root->rb_node;
3915 struct tcon_link *tlink;
3916
3917 while (node) {
3918 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3919
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003920 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04003921 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003922 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04003923 node = node->rb_right;
3924 else
3925 return tlink;
3926 }
3927 return NULL;
3928}
3929
3930/* insert a tcon_link into the tree */
3931static void
3932tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
3933{
3934 struct rb_node **new = &(root->rb_node), *parent = NULL;
3935 struct tcon_link *tlink;
3936
3937 while (*new) {
3938 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
3939 parent = *new;
3940
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003941 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04003942 new = &((*new)->rb_left);
3943 else
3944 new = &((*new)->rb_right);
3945 }
3946
3947 rb_link_node(&new_tlink->tl_rbnode, parent, new);
3948 rb_insert_color(&new_tlink->tl_rbnode, root);
3949}
3950
Jeff Layton9d002df2010-10-06 19:51:11 -04003951/*
3952 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
3953 * current task.
3954 *
3955 * If the superblock doesn't refer to a multiuser mount, then just return
3956 * the master tcon for the mount.
3957 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05303958 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04003959 * exists, then check to see if it's pending construction. If it is then wait
3960 * for construction to complete. Once it's no longer pending, check to see if
3961 * it failed and either return an error or retry construction, depending on
3962 * the timeout.
3963 *
3964 * If one doesn't exist then insert a new tcon_link struct into the tree and
3965 * try to construct a new one.
3966 */
3967struct tcon_link *
3968cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
3969{
3970 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003971 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04003972 struct tcon_link *tlink, *newtlink;
3973
3974 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
3975 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3976
3977 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04003978 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04003979 if (tlink)
3980 cifs_get_tlink(tlink);
3981 spin_unlock(&cifs_sb->tlink_tree_lock);
3982
3983 if (tlink == NULL) {
3984 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3985 if (newtlink == NULL)
3986 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04003987 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04003988 newtlink->tl_tcon = ERR_PTR(-EACCES);
3989 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
3990 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
3991 cifs_get_tlink(newtlink);
3992
Jeff Layton9d002df2010-10-06 19:51:11 -04003993 spin_lock(&cifs_sb->tlink_tree_lock);
3994 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04003995 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04003996 if (tlink) {
3997 cifs_get_tlink(tlink);
3998 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003999 kfree(newtlink);
4000 goto wait_for_construction;
4001 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004002 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004003 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4004 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004005 } else {
4006wait_for_construction:
4007 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004008 TASK_INTERRUPTIBLE);
4009 if (ret) {
4010 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004011 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004012 }
4013
4014 /* if it's good, return it */
4015 if (!IS_ERR(tlink->tl_tcon))
4016 return tlink;
4017
4018 /* return error if we tried this already recently */
4019 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4020 cifs_put_tlink(tlink);
4021 return ERR_PTR(-EACCES);
4022 }
4023
4024 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4025 goto wait_for_construction;
4026 }
4027
4028 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4029 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4030 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4031
4032 if (IS_ERR(tlink->tl_tcon)) {
4033 cifs_put_tlink(tlink);
4034 return ERR_PTR(-EACCES);
4035 }
4036
4037 return tlink;
4038}
Jeff Layton2de970f2010-10-06 19:51:12 -04004039
4040/*
4041 * periodic workqueue job that scans tcon_tree for a superblock and closes
4042 * out tcons.
4043 */
4044static void
4045cifs_prune_tlinks(struct work_struct *work)
4046{
4047 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4048 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004049 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004050 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004051 struct rb_node *tmp;
4052 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004053
Jeff Laytonb647c352010-10-28 11:16:44 -04004054 /*
4055 * Because we drop the spinlock in the loop in order to put the tlink
4056 * it's not guarded against removal of links from the tree. The only
4057 * places that remove entries from the tree are this function and
4058 * umounts. Because this function is non-reentrant and is canceled
4059 * before umount can proceed, this is safe.
4060 */
4061 spin_lock(&cifs_sb->tlink_tree_lock);
4062 node = rb_first(root);
4063 while (node != NULL) {
4064 tmp = node;
4065 node = rb_next(tmp);
4066 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4067
4068 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4069 atomic_read(&tlink->tl_count) != 0 ||
4070 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4071 continue;
4072
4073 cifs_get_tlink(tlink);
4074 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4075 rb_erase(tmp, root);
4076
Jeff Layton2de970f2010-10-06 19:51:12 -04004077 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004078 cifs_put_tlink(tlink);
4079 spin_lock(&cifs_sb->tlink_tree_lock);
4080 }
4081 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004082
Jeff Laytonda472fc2012-03-23 14:40:53 -04004083 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004084 TLINK_IDLE_EXPIRE);
4085}
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004086
4087#ifdef CONFIG_CIFS_DFS_UPCALL
4088int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4089{
4090 int rc;
4091 struct TCP_Server_Info *server = tcon->ses->server;
4092 const struct smb_version_operations *ops = server->ops;
4093 struct dfs_cache_tgt_list tl;
4094 struct dfs_cache_tgt_iterator *it = NULL;
4095 char *tree;
4096 const char *tcp_host;
4097 size_t tcp_host_len;
4098 const char *dfs_host;
4099 size_t dfs_host_len;
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004100 char *share = NULL, *prefix = NULL;
Paulo Alcantara11375a52020-07-21 09:36:43 -03004101 struct dfs_info3_param ref = {0};
4102 bool isroot;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004103
4104 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
4105 if (!tree)
4106 return -ENOMEM;
4107
4108 if (!tcon->dfs_path) {
4109 if (tcon->ipc) {
4110 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4111 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
4112 } else {
4113 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4114 }
4115 goto out;
4116 }
4117
Paulo Alcantara11375a52020-07-21 09:36:43 -03004118 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, &ref, &tl);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004119 if (rc)
4120 goto out;
Paulo Alcantara11375a52020-07-21 09:36:43 -03004121 isroot = ref.server_type == DFS_TYPE_ROOT;
4122 free_dfs_info_param(&ref);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004123
4124 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
4125
4126 for (it = dfs_cache_get_tgt_iterator(&tl); it; it = dfs_cache_get_next_tgt(&tl, it)) {
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004127 bool target_match;
4128
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004129 kfree(share);
4130 kfree(prefix);
Colin Ian Kingc6a80e12020-07-31 18:13:42 +01004131 share = NULL;
4132 prefix = NULL;
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004133
4134 rc = dfs_cache_get_tgt_share(tcon->dfs_path + 1, it, &share, &prefix);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004135 if (rc) {
4136 cifs_dbg(VFS, "%s: failed to parse target share %d\n",
4137 __func__, rc);
4138 continue;
4139 }
4140
4141 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
4142
4143 if (dfs_host_len != tcp_host_len
4144 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
4145 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
4146 dfs_host, (int)tcp_host_len, tcp_host);
4147
4148 rc = match_target_ip(server, dfs_host, dfs_host_len, &target_match);
4149 if (rc) {
4150 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
4151 break;
4152 }
4153
4154 if (!target_match) {
4155 cifs_dbg(FYI, "%s: skipping target\n", __func__);
4156 continue;
4157 }
4158 }
4159
4160 if (tcon->ipc) {
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004161 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", share);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004162 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
4163 } else {
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004164 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004165 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
Paulo Alcantara11375a52020-07-21 09:36:43 -03004166 /* Only handle prefix paths of DFS link targets */
4167 if (!rc && !isroot) {
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004168 rc = update_super_prepath(tcon, prefix);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004169 break;
4170 }
4171 }
4172 if (rc == -EREMOTE)
4173 break;
4174 }
4175
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004176 kfree(share);
4177 kfree(prefix);
4178
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004179 if (!rc) {
4180 if (it)
4181 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1, it);
4182 else
4183 rc = -ENOENT;
4184 }
4185 dfs_cache_free_tgts(&tl);
4186out:
4187 kfree(tree);
4188 return rc;
4189}
4190#else
4191int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4192{
4193 const struct smb_version_operations *ops = tcon->ses->server->ops;
4194
4195 return ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4196}
4197#endif