blob: 03126e7993bee406e73aeef835b4577f8b6cd3a9 [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Steve French1080ef72011-02-24 18:07:19 +00004 * Copyright (C) International Business Machines Corp., 2002,2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8#include <linux/fs.h>
9#include <linux/net.h>
10#include <linux/string.h>
Eric Biggersdc920272020-03-08 22:58:20 -070011#include <linux/sched/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010012#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/list.h>
14#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/pagemap.h>
17#include <linux/ctype.h>
18#include <linux/utsname.h>
19#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070020#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070021#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000022#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070023#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040025#include <linux/namei.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020026#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040029#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040030#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050031#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000032#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040033#include <linux/parser.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060034#include <linux/bvec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "cifspdu.h"
36#include "cifsglob.h"
37#include "cifsproto.h"
38#include "cifs_unicode.h"
39#include "cifs_debug.h"
40#include "cifs_fs_sb.h"
41#include "ntlmssp.h"
42#include "nterr.h"
43#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053044#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070045#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070046#include "smbdirect.h"
Paulo Alcantara1c780222018-11-14 16:24:03 -020047#include "dns_resolve.h"
48#ifdef CONFIG_CIFS_DFS_UPCALL
49#include "dfs_cache.h"
50#endif
Ronnie Sahlberg5c6e5aa2020-10-21 10:37:11 +100051#include "fs_context.h"
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +010052#include "cifs_swn.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050055extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jeff Layton2de970f2010-10-06 19:51:12 -040057/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040058#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040059#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040060
Rohith Surabattula8e670f72020-09-18 05:37:28 +000061/* Drop the connection to not overload the server */
62#define NUM_STATUS_IO_TIMEOUT 5
63
Pavel Shilovskya9f1b852010-12-13 19:08:35 +030064static int ip_connect(struct TCP_Server_Info *server);
65static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -040066static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -040067static void cifs_prune_tlinks(struct work_struct *work);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -020068
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020069/*
70 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
71 * get their ip addresses changed at some point.
72 *
73 * This should be called with server->srv_mutex held.
74 */
Samuel Cabrero7d6535b2020-11-30 19:02:55 +010075static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020076{
77 int rc;
78 int len;
79 char *unc, *ipaddr = NULL;
Shyam Prasad N506c1da2021-05-18 15:05:50 +000080 time64_t expiry, now;
81 unsigned long ttl = SMB_DNS_RESOLVE_INTERVAL_DEFAULT;
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020082
83 if (!server->hostname)
84 return -EINVAL;
85
86 len = strlen(server->hostname) + 3;
87
88 unc = kmalloc(len, GFP_KERNEL);
89 if (!unc) {
90 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
91 return -ENOMEM;
92 }
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +100093 scnprintf(unc, len, "\\\\%s", server->hostname);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020094
Shyam Prasad N506c1da2021-05-18 15:05:50 +000095 rc = dns_resolve_server_name_to_ip(unc, &ipaddr, &expiry);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020096 kfree(unc);
97
98 if (rc < 0) {
99 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
100 __func__, server->hostname, rc);
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000101 goto requeue_resolve;
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200102 }
103
Ronnie Sahlbergfada37f2020-04-21 12:37:39 +1000104 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200105 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
106 strlen(ipaddr));
Ronnie Sahlbergfada37f2020-04-21 12:37:39 +1000107 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200108 kfree(ipaddr);
109
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000110 /* rc == 1 means success here */
111 if (rc) {
112 now = ktime_get_real_seconds();
113 if (expiry && expiry > now)
114 /*
115 * To make sure we don't use the cached entry, retry 1s
116 * after expiry.
117 */
118 ttl = (expiry - now + 1);
119 }
120 rc = !rc ? -1 : 0;
121
122requeue_resolve:
123 cifs_dbg(FYI, "%s: next dns resolution scheduled for %lu seconds in the future\n",
124 __func__, ttl);
125 mod_delayed_work(cifsiod_wq, &server->resolve, (ttl * HZ));
126
127 return rc;
128}
129
130
131static void cifs_resolve_server(struct work_struct *work)
132{
133 int rc;
134 struct TCP_Server_Info *server = container_of(work,
135 struct TCP_Server_Info, resolve.work);
136
137 mutex_lock(&server->srv_mutex);
138
139 /*
140 * Resolve the hostname again to make sure that IP address is up-to-date.
141 */
142 rc = reconn_set_ipaddr_from_hostname(server);
143 if (rc) {
144 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
145 __func__, rc);
146 }
147
148 mutex_unlock(&server->srv_mutex);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200149}
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200150
Shyam Prasad N4e456b32021-03-31 14:35:24 +0000151#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200152/* These functions must be called with server->srv_mutex held */
Paulo Alcantara7d397a02020-07-21 09:36:40 -0300153static void reconn_set_next_dfs_target(struct TCP_Server_Info *server,
154 struct cifs_sb_info *cifs_sb,
155 struct dfs_cache_tgt_list *tgt_list,
156 struct dfs_cache_tgt_iterator **tgt_it)
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200157{
158 const char *name;
Samuel Cabrero7d6535b2020-11-30 19:02:55 +0100159 int rc;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200160
Paulo Alcantara7d397a02020-07-21 09:36:40 -0300161 if (!cifs_sb || !cifs_sb->origin_fullpath)
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200162 return;
163
164 if (!*tgt_it) {
165 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
166 } else {
167 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
168 if (!*tgt_it)
169 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
170 }
171
172 cifs_dbg(FYI, "%s: UNC: %s\n", __func__, cifs_sb->origin_fullpath);
173
174 name = dfs_cache_get_tgt_name(*tgt_it);
175
176 kfree(server->hostname);
177
178 server->hostname = extract_hostname(name);
Dan Carpenter84288172019-01-05 15:25:29 +0300179 if (IS_ERR(server->hostname)) {
180 cifs_dbg(FYI,
181 "%s: failed to extract hostname from target: %ld\n",
182 __func__, PTR_ERR(server->hostname));
Samuel Cabrero0bf1baf2020-12-18 10:29:49 +0100183 return;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200184 }
Samuel Cabrero7d6535b2020-11-30 19:02:55 +0100185
186 rc = reconn_set_ipaddr_from_hostname(server);
187 if (rc) {
188 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
189 __func__, rc);
190 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200191}
192
193static inline int reconn_setup_dfs_targets(struct cifs_sb_info *cifs_sb,
Paulo Alcantarabaf3f082020-05-19 15:38:29 -0300194 struct dfs_cache_tgt_list *tl)
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200195{
196 if (!cifs_sb->origin_fullpath)
197 return -EOPNOTSUPP;
198 return dfs_cache_noreq_find(cifs_sb->origin_fullpath + 1, NULL, tl);
199}
200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300202/**
203 * Mark all sessions and tcons for reconnect.
Jeff Laytond5c56052008-12-01 18:42:33 -0500204 *
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300205 * @server needs to be previously set to CifsNeedReconnect.
Jeff Laytond5c56052008-12-01 18:42:33 -0500206 */
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300207static void cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Jeff Laytonf1987b42008-11-15 11:12:47 -0500209 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000210 struct cifs_ses *ses;
211 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000212 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400213 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400216 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Steve French6e4d3bb2018-09-22 11:25:04 -0500218 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800219 trace_smb3_reconnect(server->CurrentMid, server->conn_id, server->hostname);
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300220 /*
221 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
222 * are not used until reconnected.
223 */
224 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n", __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530225 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500226 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000227 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500228 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500229 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000230 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500231 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100233 if (ses->tcon_ipc)
234 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530236 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500239 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500240 mutex_lock(&server->srv_mutex);
Long Li1d2a4f52019-05-13 21:01:28 -0700241 if (server->ssocket) {
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300242 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
243 server->ssocket->flags);
Long Li1d2a4f52019-05-13 21:01:28 -0700244 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300245 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
246 server->ssocket->flags);
Long Li1d2a4f52019-05-13 21:01:28 -0700247 sock_release(server->ssocket);
248 server->ssocket = NULL;
249 }
250 server->sequence_number = 0;
251 server->session_estab = false;
252 kfree(server->session_key.response);
253 server->session_key.response = NULL;
254 server->session_key.len = 0;
255 server->lstrp = jiffies;
Long Li214bab42019-04-05 21:36:35 +0000256
257 /* mark submitted MIDs for retry and issue callback */
258 INIT_LIST_HEAD(&retry_list);
259 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
260 spin_lock(&GlobalMid_Lock);
261 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
262 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700263 kref_get(&mid_entry->refcount);
Long Li214bab42019-04-05 21:36:35 +0000264 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
265 mid_entry->mid_state = MID_RETRY_NEEDED;
266 list_move(&mid_entry->qhead, &retry_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700267 mid_entry->mid_flags |= MID_DELETED;
Long Li214bab42019-04-05 21:36:35 +0000268 }
269 spin_unlock(&GlobalMid_Lock);
Long Li1d2a4f52019-05-13 21:01:28 -0700270 mutex_unlock(&server->srv_mutex);
Long Li214bab42019-04-05 21:36:35 +0000271
272 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
273 list_for_each_safe(tmp, tmp2, &retry_list) {
274 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
275 list_del_init(&mid_entry->qhead);
276 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700277 cifs_mid_q_entry_release(mid_entry);
Long Li214bab42019-04-05 21:36:35 +0000278 }
279
Long Li1d2a4f52019-05-13 21:01:28 -0700280 if (cifs_rdma_enabled(server)) {
281 mutex_lock(&server->srv_mutex);
Long Li050b8c32019-04-04 11:35:42 -0500282 smbd_destroy(server);
Long Li1d2a4f52019-05-13 21:01:28 -0700283 mutex_unlock(&server->srv_mutex);
284 }
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300285}
286
287/*
288 * cifs tcp session reconnection
289 *
290 * mark tcp session as reconnecting so temporarily locked
291 * mark all smb sessions as reconnecting for tcp session
292 * reconnect tcp session
293 * wake up waiters on reconnection? - (not needed currently)
294 */
295int
296cifs_reconnect(struct TCP_Server_Info *server)
297{
298 int rc = 0;
299#ifdef CONFIG_CIFS_DFS_UPCALL
300 struct super_block *sb = NULL;
301 struct cifs_sb_info *cifs_sb = NULL;
302 struct dfs_cache_tgt_list tgt_list = DFS_CACHE_TGT_LIST_INIT(tgt_list);
303 struct dfs_cache_tgt_iterator *tgt_it = NULL;
304#endif
305
306 spin_lock(&GlobalMid_Lock);
307 server->nr_targets = 1;
308#ifdef CONFIG_CIFS_DFS_UPCALL
309 spin_unlock(&GlobalMid_Lock);
310 sb = cifs_get_tcp_super(server);
311 if (IS_ERR(sb)) {
312 rc = PTR_ERR(sb);
313 cifs_dbg(FYI, "%s: will not do DFS failover: rc = %d\n",
314 __func__, rc);
315 sb = NULL;
316 } else {
317 cifs_sb = CIFS_SB(sb);
318 rc = reconn_setup_dfs_targets(cifs_sb, &tgt_list);
319 if (rc) {
320 cifs_sb = NULL;
321 if (rc != -EOPNOTSUPP) {
322 cifs_server_dbg(VFS, "%s: no target servers for DFS failover\n",
323 __func__);
324 }
325 } else {
326 server->nr_targets = dfs_cache_get_nr_tgts(&tgt_list);
327 }
328 }
329 cifs_dbg(FYI, "%s: will retry %d target(s)\n", __func__,
330 server->nr_targets);
331 spin_lock(&GlobalMid_Lock);
332#endif
333 if (server->tcpStatus == CifsExiting) {
334 /* the demux thread will exit normally next time through the loop */
335 spin_unlock(&GlobalMid_Lock);
336#ifdef CONFIG_CIFS_DFS_UPCALL
337 dfs_cache_free_tgts(&tgt_list);
338 cifs_put_tcp_super(sb);
339#endif
340 wake_up(&server->response_q);
341 return rc;
342 } else
343 server->tcpStatus = CifsNeedReconnect;
344 spin_unlock(&GlobalMid_Lock);
345
346 cifs_mark_tcp_ses_conns_for_reconnect(server);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400347
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400348 do {
Steve French6c3d8902006-07-31 22:46:20 +0000349 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300350
Jeff Layton73e216a2013-09-05 08:38:10 -0400351 mutex_lock(&server->srv_mutex);
Samuel Cabrero121d9472020-11-30 19:02:56 +0100352
Samuel Cabrero121d9472020-11-30 19:02:56 +0100353
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +0200354 if (!cifs_swn_set_server_dstaddr(server)) {
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300355#ifdef CONFIG_CIFS_DFS_UPCALL
Shyam Prasad N4e456b32021-03-31 14:35:24 +0000356 if (cifs_sb && cifs_sb->origin_fullpath)
Samuel Cabrero121d9472020-11-30 19:02:56 +0100357 /*
358 * Set up next DFS target server (if any) for reconnect. If DFS
359 * feature is disabled, then we will retry last server we
360 * connected to before.
361 */
362 reconn_set_next_dfs_target(server, cifs_sb, &tgt_list, &tgt_it);
Shyam Prasad N4e456b32021-03-31 14:35:24 +0000363 else {
Samuel Cabrero121d9472020-11-30 19:02:56 +0100364#endif
Shyam Prasad N4e456b32021-03-31 14:35:24 +0000365 /*
366 * Resolve the hostname again to make sure that IP address is up-to-date.
367 */
368 rc = reconn_set_ipaddr_from_hostname(server);
369 if (rc) {
370 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
371 __func__, rc);
372 }
373
374#ifdef CONFIG_CIFS_DFS_UPCALL
375 }
376#endif
377
Samuel Cabrero121d9472020-11-30 19:02:56 +0100378
Samuel Cabrero121d9472020-11-30 19:02:56 +0100379 }
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300380
Long Li781a8052017-11-22 17:38:36 -0700381 if (cifs_rdma_enabled(server))
382 rc = smbd_reconnect(server);
383 else
384 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000385 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500386 cifs_dbg(FYI, "reconnect error %d\n", rc);
Federico Sauter4afe2602015-03-17 17:45:28 +0100387 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700388 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 } else {
390 atomic_inc(&tcpSesReconnectCount);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800391 set_credits(server, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000393 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000394 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000395 spin_unlock(&GlobalMid_Lock);
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +0200396 cifs_swn_reset_server_dstaddr(server);
Federico Sauter4afe2602015-03-17 17:45:28 +0100397 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400399 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500400
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200401#ifdef CONFIG_CIFS_DFS_UPCALL
402 if (tgt_it) {
403 rc = dfs_cache_noreq_update_tgthint(cifs_sb->origin_fullpath + 1,
404 tgt_it);
405 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000406 cifs_server_dbg(VFS, "%s: failed to update DFS target hint: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200407 __func__, rc);
408 }
Paulo Alcantara23324402018-11-20 14:37:18 -0200409 dfs_cache_free_tgts(&tgt_list);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200410 }
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300411
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300412 cifs_put_tcp_super(sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200413#endif
Sachin Prabhub8c60012016-10-20 19:52:24 -0400414 if (server->tcpStatus == CifsNeedNegotiate)
415 mod_delayed_work(cifsiod_wq, &server->echo, 0);
416
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600417 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return rc;
419}
420
Jeff Laytonc74093b2011-01-11 07:24:23 -0500421static void
422cifs_echo_request(struct work_struct *work)
423{
424 int rc;
425 struct TCP_Server_Info *server = container_of(work,
426 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400427
428 /*
429 * We cannot send an echo if it is disabled.
430 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500431 */
Steve French4fcd1812016-06-22 20:12:05 -0500432
433 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400434 server->tcpStatus == CifsExiting ||
435 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400436 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Shyam Prasad N5b2abda2021-05-01 16:17:07 +0000437 time_before(jiffies, server->lstrp + server->echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500438 goto requeue_echo;
439
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400440 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500441 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500442 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
443 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500444
Samuel Cabrero21077c62020-11-30 19:02:54 +0100445 /* Check witness registrations */
446 cifs_swn_check();
Samuel Cabrero21077c62020-11-30 19:02:54 +0100447
Jeff Laytonc74093b2011-01-11 07:24:23 -0500448requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400449 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500450}
451
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400452static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400453allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400454{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400455 if (!server->bigbuf) {
456 server->bigbuf = (char *)cifs_buf_get();
457 if (!server->bigbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000458 cifs_server_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400459 msleep(3000);
460 /* retry will check if exiting */
461 return false;
462 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400463 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400464 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400465 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400466 }
467
Jeff Layton2a37ef92011-10-19 15:29:23 -0400468 if (!server->smallbuf) {
469 server->smallbuf = (char *)cifs_small_buf_get();
470 if (!server->smallbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000471 cifs_server_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400472 msleep(1000);
473 /* retry will check if exiting */
474 return false;
475 }
476 /* beginning of smb buffer is cleared in our buf_get */
477 } else {
478 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400479 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400480 }
481
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400482 return true;
483}
484
Jeff Laytonba749e62011-10-11 06:41:32 -0400485static bool
486server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400487{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300488 /*
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000489 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300490 * situations right:
491 * 1s client sends a normal SMB request
Ronnie Sahlbergbecc2ba2019-07-24 11:43:49 +1000492 * 2s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300493 * 30s echo workqueue job pops, and decides we got a response recently
494 * and don't need to send another
495 * ...
496 * 65s kernel_recvmsg times out, and we see that we haven't gotten
497 * a response in >60s.
498 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200499 if ((server->tcpStatus == CifsGood ||
500 server->tcpStatus == CifsNeedNegotiate) &&
Shyam Prasad Nf4916642021-04-29 07:53:18 +0000501 (!server->ops->can_echo || server->ops->can_echo(server)) &&
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000502 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000503 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
504 (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400505 cifs_reconnect(server);
Jeff Laytonba749e62011-10-11 06:41:32 -0400506 return true;
507 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400508
Jeff Laytonba749e62011-10-11 06:41:32 -0400509 return false;
510}
511
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800512static inline bool
513zero_credits(struct TCP_Server_Info *server)
514{
515 int val;
516
517 spin_lock(&server->req_lock);
518 val = server->credits + server->echo_credits + server->oplock_credits;
519 if (server->in_flight == 0 && val == 0) {
520 spin_unlock(&server->req_lock);
521 return true;
522 }
523 spin_unlock(&server->req_lock);
524 return false;
525}
526
Al Viro71335662016-01-09 19:54:50 -0500527static int
528cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400529{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400530 int length = 0;
531 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400532
Al Viro71335662016-01-09 19:54:50 -0500533 smb_msg->msg_control = NULL;
534 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400535
Al Viro71335662016-01-09 19:54:50 -0500536 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500537 try_to_freeze();
538
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800539 /* reconnect if no credits and no requests in flight */
540 if (zero_credits(server)) {
541 cifs_reconnect(server);
542 return -ECONNABORTED;
543 }
544
Al Viro71335662016-01-09 19:54:50 -0500545 if (server_unresponsive(server))
546 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700547 if (cifs_rdma_enabled(server) && server->smbd_conn)
548 length = smbd_recv(server->smbd_conn, smb_msg);
549 else
550 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500551
552 if (server->tcpStatus == CifsExiting)
553 return -ESHUTDOWN;
554
555 if (server->tcpStatus == CifsNeedReconnect) {
556 cifs_reconnect(server);
557 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400558 }
559
Al Viro71335662016-01-09 19:54:50 -0500560 if (length == -ERESTARTSYS ||
561 length == -EAGAIN ||
562 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400563 /*
564 * Minimum sleep to prevent looping, allowing socket
565 * to clear and app threads to set tcpStatus
566 * CifsNeedReconnect if server hung.
567 */
568 usleep_range(1000, 2000);
569 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400570 continue;
Al Viro71335662016-01-09 19:54:50 -0500571 }
572
573 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500574 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400575 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500576 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400577 }
578 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400579 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400580}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400581
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400582int
583cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
584 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400585{
Al Viro71335662016-01-09 19:54:50 -0500586 struct msghdr smb_msg;
587 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100588 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400589
Al Viro71335662016-01-09 19:54:50 -0500590 return cifs_readv_from_socket(server, &smb_msg);
591}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400592
David Howellscf0604a2021-02-04 00:15:21 -0600593ssize_t
594cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
595{
596 struct msghdr smb_msg;
597
598 /*
599 * iov_iter_discard already sets smb_msg.type and count and iov_offset
600 * and cifs_readv_from_socket sets msg_control and msg_controllen
601 * so little to initialize in struct msghdr
602 */
603 smb_msg.msg_name = NULL;
604 smb_msg.msg_namelen = 0;
605 iov_iter_discard(&smb_msg.msg_iter, READ, to_read);
606
607 return cifs_readv_from_socket(server, &smb_msg);
608}
609
Al Viro71335662016-01-09 19:54:50 -0500610int
611cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700612 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500613{
614 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700615 struct bio_vec bv = {
616 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100617 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500618 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400619}
620
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400621static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400622is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400623{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400624 /*
625 * The first byte big endian of the length field,
626 * is actually not part of the length but the type
627 * with the most common, zero, as regular data.
628 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400629 switch (type) {
630 case RFC1002_SESSION_MESSAGE:
631 /* Regular SMB response */
632 return true;
633 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500634 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400635 break;
636 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500637 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400638 break;
639 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400640 /*
641 * We get this from Windows 98 instead of an error on
642 * SMB negprot response.
643 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500644 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400645 /* give server a second to clean up */
646 msleep(1000);
647 /*
648 * Always try 445 first on reconnect since we get NACK
649 * on some if we ever connected to port 139 (the NACK
650 * is since we do not begin with RFC1001 session
651 * initialize frame).
652 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400653 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400654 cifs_reconnect(server);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400655 break;
656 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000657 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400658 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400659 }
660
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400661 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400662}
663
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400664void
665dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400666{
667#ifdef CONFIG_CIFS_STATS2
668 mid->when_received = jiffies;
669#endif
670 spin_lock(&GlobalMid_Lock);
671 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400672 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400673 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400674 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000675 /*
676 * Trying to handle/dequeue a mid after the send_recv()
677 * function has finished processing it is a bug.
678 */
679 if (mid->mid_flags & MID_DELETED)
Joe Perchesa0a30362020-04-14 22:42:53 -0700680 pr_warn_once("trying to dequeue a deleted mid\n");
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700681 else {
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000682 list_del_init(&mid->qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700683 mid->mid_flags |= MID_DELETED;
684 }
Jeff Laytonea1f4502011-10-19 15:29:05 -0400685 spin_unlock(&GlobalMid_Lock);
686}
687
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800688static unsigned int
689smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
690{
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900691 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800692
693 /*
694 * SMB1 does not use credits.
695 */
696 if (server->vals->header_preamble_size)
697 return 0;
698
699 return le16_to_cpu(shdr->CreditRequest);
700}
701
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400702static void
703handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400704 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400705{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400706 if (server->ops->check_trans2 &&
707 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400708 return;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800709 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400710 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400711 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400712 /* Was previous buf put in mpx struct for multi-rsp? */
713 if (!mid->multiRsp) {
714 /* smb buffer will be freed by user thread */
715 if (server->large_buf)
716 server->bigbuf = NULL;
717 else
718 server->smallbuf = NULL;
719 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400720 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400721}
722
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400723static void clean_demultiplex_info(struct TCP_Server_Info *server)
724{
725 int length;
726
727 /* take it off the list, if it's not already */
728 spin_lock(&cifs_tcp_ses_lock);
729 list_del_init(&server->tcp_ses_list);
730 spin_unlock(&cifs_tcp_ses_lock);
731
Paulo Alcantara21225332020-11-28 16:54:02 -0300732 cancel_delayed_work_sync(&server->echo);
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000733 cancel_delayed_work_sync(&server->resolve);
Paulo Alcantara21225332020-11-28 16:54:02 -0300734
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400735 spin_lock(&GlobalMid_Lock);
736 server->tcpStatus = CifsExiting;
737 spin_unlock(&GlobalMid_Lock);
738 wake_up_all(&server->response_q);
739
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400740 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300741 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400742 if (server->credits <= 0)
743 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300744 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400745 /*
746 * Although there should not be any requests blocked on this queue it
747 * can not hurt to be paranoid and try to wake up requests that may
748 * haven been blocked when more than 50 at time were on the wire to the
749 * same server - they now will see the session is in exit state and get
750 * out of SendReceive.
751 */
752 wake_up_all(&server->request_q);
753 /* give those requests time to exit */
754 msleep(125);
Long Li050b8c32019-04-04 11:35:42 -0500755 if (cifs_rdma_enabled(server))
756 smbd_destroy(server);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400757 if (server->ssocket) {
758 sock_release(server->ssocket);
759 server->ssocket = NULL;
760 }
761
762 if (!list_empty(&server->pending_mid_q)) {
763 struct list_head dispose_list;
764 struct mid_q_entry *mid_entry;
765 struct list_head *tmp, *tmp2;
766
767 INIT_LIST_HEAD(&dispose_list);
768 spin_lock(&GlobalMid_Lock);
769 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
770 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Paulo Alcantarabf1bc6942021-03-08 12:00:47 -0300771 cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700772 kref_get(&mid_entry->refcount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400773 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400774 list_move(&mid_entry->qhead, &dispose_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700775 mid_entry->mid_flags |= MID_DELETED;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400776 }
777 spin_unlock(&GlobalMid_Lock);
778
779 /* now walk dispose list and issue callbacks */
780 list_for_each_safe(tmp, tmp2, &dispose_list) {
781 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Paulo Alcantarabf1bc6942021-03-08 12:00:47 -0300782 cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400783 list_del_init(&mid_entry->qhead);
784 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700785 cifs_mid_q_entry_release(mid_entry);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400786 }
787 /* 1/8th of sec is more than enough time for them to exit */
788 msleep(125);
789 }
790
791 if (!list_empty(&server->pending_mid_q)) {
792 /*
793 * mpx threads have not exited yet give them at least the smb
794 * send timeout time for long ops.
795 *
796 * Due to delays on oplock break requests, we need to wait at
797 * least 45 seconds before giving up on a request getting a
798 * response and going ahead and killing cifsd.
799 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500800 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400801 msleep(46000);
802 /*
803 * If threads still have not exited they are probably never
804 * coming home not much else we can do but free the memory.
805 */
806 }
807
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400808 kfree(server);
809
810 length = atomic_dec_return(&tcpSesAllocCount);
811 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700812 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400813}
814
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400815static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400816standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
817{
818 int length;
819 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000820 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400821
822 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100823 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
824 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000825 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400826 cifs_reconnect(server);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400827 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400828 }
829
830 /* switch to large buffer if too big for a small one */
831 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
832 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400833 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400834 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400835 }
836
837 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400838 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100839 pdu_length - HEADER_SIZE(server) + 1
840 + server->vals->header_preamble_size);
841
Jeff Laytone9097ab2011-10-19 15:29:40 -0400842 if (length < 0)
843 return length;
844 server->total_read += length;
845
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400846 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400847
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800848 return cifs_handle_standard(server, mid);
849}
850
851int
852cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
853{
854 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
855 int length;
856
Jeff Laytone9097ab2011-10-19 15:29:40 -0400857 /*
858 * We know that we received enough to get to the MID as we
859 * checked the pdu_length earlier. Now check to see
860 * if the rest of the header is OK. We borrow the length
861 * var for the rest of the loop to avoid a new stack var.
862 *
863 * 48 bytes is enough to display the header and a little bit
864 * into the payload for debugging purposes.
865 */
Steve French373512e2015-12-18 13:05:30 -0600866 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400867 if (length != 0)
868 cifs_dump_mem("Bad SMB: ", buf,
869 min_t(unsigned int, server->total_read, 48));
870
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700871 if (server->ops->is_session_expired &&
872 server->ops->is_session_expired(buf)) {
873 cifs_reconnect(server);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700874 return -1;
875 }
876
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700877 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -0800878 server->ops->is_status_pending(buf, server))
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700879 return -1;
880
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500881 if (!mid)
882 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400883
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400884 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500885 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400886}
887
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000888static void
889smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
890{
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900891 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800892 int scredits, in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000893
894 /*
895 * SMB1 does not use credits.
896 */
897 if (server->vals->header_preamble_size)
898 return;
899
900 if (shdr->CreditRequest) {
901 spin_lock(&server->req_lock);
902 server->credits += le16_to_cpu(shdr->CreditRequest);
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800903 scredits = server->credits;
904 in_flight = server->in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000905 spin_unlock(&server->req_lock);
906 wake_up(&server->request_q);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800907
908 trace_smb3_add_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800909 server->conn_id, server->hostname, scredits,
910 le16_to_cpu(shdr->CreditRequest), in_flight);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800911 cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
912 __func__, le16_to_cpu(shdr->CreditRequest),
913 scredits);
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000914 }
915}
916
917
Jeff Laytone9097ab2011-10-19 15:29:40 -0400918static int
Al Viro7c97c202011-06-21 08:51:28 -0400919cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000921 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400922 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400923 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000924 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400925 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500926 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000927 struct mid_q_entry *mids[MAX_COMPOUND];
928 char *bufs[MAX_COMPOUND];
Rohith Surabattula8e670f72020-09-18 05:37:28 +0000929 unsigned int noreclaim_flag, num_io_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Eric Biggersdc920272020-03-08 22:58:20 -0700931 noreclaim_flag = memalloc_noreclaim_save();
Joe Perchesf96637b2013-05-04 22:12:25 -0500932 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400933
934 length = atomic_inc_return(&tcpSesAllocCount);
935 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700936 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700938 set_freezable();
Eric W. Biederman33da8e72019-08-16 12:33:54 -0500939 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +0000940 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700941 if (try_to_freeze())
942 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700943
Jeff Layton2a37ef92011-10-19 15:29:23 -0400944 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400945 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700946
Jeff Layton2a37ef92011-10-19 15:29:23 -0400947 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400948 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000949 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000950
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400951 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400952 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000953 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000954
955 if (server->vals->header_preamble_size == 0)
956 server->total_read = 0;
957 else
958 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700959
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400960 /*
961 * The right amount was read from socket - 4 bytes,
962 * so we can now interpret the length field.
963 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400964 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700965
Joe Perchesf96637b2013-05-04 22:12:25 -0500966 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400967 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000968 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000969next_pdu:
970 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700971
Jeff Layton89482a52011-10-19 15:28:57 -0400972 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000973 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100974 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000975 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000976 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400977 cifs_reconnect(server);
Jeff Layton89482a52011-10-19 15:28:57 -0400978 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700979 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400980
Jeff Layton89482a52011-10-19 15:28:57 -0400981 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100982 length = cifs_read_from_socket(server,
983 buf + server->vals->header_preamble_size,
984 HEADER_SIZE(server) - 1
985 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400986 if (length < 0)
987 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400988 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400989
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000990 if (server->ops->next_header) {
991 next_offset = server->ops->next_header(buf);
992 if (next_offset)
993 server->pdu_size = next_offset;
994 }
995
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000996 memset(mids, 0, sizeof(mids));
997 memset(bufs, 0, sizeof(bufs));
998 num_mids = 0;
999
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001000 if (server->ops->is_transform_hdr &&
1001 server->ops->receive_transform &&
1002 server->ops->is_transform_hdr(buf)) {
1003 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001004 mids,
1005 bufs,
1006 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001007 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001008 mids[0] = server->ops->find_mid(server, buf);
1009 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001010 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001011
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001012 if (!mids[0] || !mids[0]->receive)
1013 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001014 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001015 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001016 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001017
Lars Persson696e4202018-06-25 14:05:25 +02001018 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001019 for (i = 0; i < num_mids; i++)
1020 if (mids[i])
1021 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001022 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001023 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001024
Rohith Surabattula8e670f72020-09-18 05:37:28 +00001025 if (server->ops->is_status_io_timeout &&
1026 server->ops->is_status_io_timeout(buf)) {
1027 num_io_timeout++;
1028 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
1029 cifs_reconnect(server);
1030 num_io_timeout = 0;
1031 continue;
1032 }
1033 }
1034
Steve Frenchfda35942011-01-20 18:06:34 +00001035 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001036
1037 for (i = 0; i < num_mids; i++) {
1038 if (mids[i] != NULL) {
1039 mids[i]->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001040
Rohith Surabattula9e550b02021-02-16 10:40:45 +00001041 if (bufs[i] && server->ops->is_network_name_deleted)
1042 server->ops->is_network_name_deleted(bufs[i],
1043 server);
1044
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001045 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1046 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001047
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001048 cifs_mid_q_entry_release(mids[i]);
1049 } else if (server->ops->is_oplock_break &&
1050 server->ops->is_oplock_break(bufs[i],
1051 server)) {
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001052 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001053 cifs_dbg(FYI, "Received oplock break\n");
1054 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07001055 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1056 atomic_read(&midCount));
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001057 cifs_dump_mem("Received Data is: ", bufs[i],
1058 HEADER_SIZE(server));
Ronnie Sahlberg3e272572019-07-06 06:43:08 +10001059 smb2_add_credits_from_hdr(bufs[i], server);
Steve French39798772006-05-31 22:40:51 +00001060#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001061 if (server->ops->dump_detail)
1062 server->ops->dump_detail(bufs[i],
1063 server);
1064 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001065#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001066 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001067 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001068
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001069 if (pdu_length > server->pdu_size) {
1070 if (!allocate_buffers(server))
1071 continue;
1072 pdu_length -= server->pdu_size;
1073 server->total_read = 0;
1074 server->large_buf = false;
1075 buf = server->smallbuf;
1076 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001077 }
1078 } /* end while !EXITING */
1079
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001080 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001081 cifs_buf_release(server->bigbuf);
1082 if (server->smallbuf) /* no sense logging a debug message if NULL */
1083 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001085 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001086 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001087
1088 /* if server->tsk was NULL then wait for a signal before exiting */
1089 if (!task_to_wake) {
1090 set_current_state(TASK_INTERRUPTIBLE);
1091 while (!signal_pending(current)) {
1092 schedule();
1093 set_current_state(TASK_INTERRUPTIBLE);
1094 }
1095 set_current_state(TASK_RUNNING);
1096 }
1097
Eric Biggersdc920272020-03-08 22:58:20 -07001098 memalloc_noreclaim_restore(noreclaim_flag);
Jeff Layton0468a2c2008-12-01 07:09:35 -05001099 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Steve French4c51de12021-09-13 18:29:46 -05001102/*
Steve Frenchbc044992020-12-11 19:48:26 -06001103 * Returns true if srcaddr isn't specified and rhs isn't specified, or
1104 * if srcaddr is specified and matches the IP address of the rhs argument
Ben Greear3eb9a882010-09-01 17:06:02 -07001105 */
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001106bool
1107cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
Ben Greear3eb9a882010-09-01 17:06:02 -07001108{
1109 switch (srcaddr->sa_family) {
1110 case AF_UNSPEC:
1111 return (rhs->sa_family == AF_UNSPEC);
1112 case AF_INET: {
1113 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1114 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1115 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1116 }
1117 case AF_INET6: {
1118 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05001119 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07001120 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1121 }
1122 default:
1123 WARN_ON(1);
1124 return false; /* don't expect to be here */
1125 }
1126}
1127
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001128/*
1129 * If no port is specified in addr structure, we try to match with 445 port
1130 * and if it fails - with 139 ports. It should be called only if address
1131 * families of server and addr are equal.
1132 */
1133static bool
1134match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1135{
Steve French6da97912011-03-13 18:55:55 +00001136 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001137
Long Li3b249112019-05-15 14:09:04 -07001138 /* SMBDirect manages its own ports, don't match it here */
1139 if (server->rdma)
1140 return true;
1141
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001142 switch (addr->sa_family) {
1143 case AF_INET:
1144 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1145 port = ((struct sockaddr_in *) addr)->sin_port;
1146 break;
1147 case AF_INET6:
1148 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1149 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1150 break;
1151 default:
1152 WARN_ON(1);
1153 return false;
1154 }
1155
1156 if (!port) {
1157 port = htons(CIFS_PORT);
1158 if (port == *sport)
1159 return true;
1160
1161 port = htons(RFC1001_PORT);
1162 }
1163
1164 return port == *sport;
1165}
Ben Greear3eb9a882010-09-01 17:06:02 -07001166
1167static bool
1168match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1169 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Jeff Layton45151482010-07-06 20:43:02 -04001171 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001172 case AF_INET: {
1173 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1174 struct sockaddr_in *srv_addr4 =
1175 (struct sockaddr_in *)&server->dstaddr;
1176
1177 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04001178 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001179 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001180 }
1181 case AF_INET6: {
1182 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1183 struct sockaddr_in6 *srv_addr6 =
1184 (struct sockaddr_in6 *)&server->dstaddr;
1185
Jeff Layton45151482010-07-06 20:43:02 -04001186 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001187 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04001188 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001189 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04001190 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001191 break;
1192 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001193 default:
1194 WARN_ON(1);
1195 return false; /* don't expect to be here */
1196 }
Jeff Layton45151482010-07-06 20:43:02 -04001197
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001198 if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
Ben Greear3eb9a882010-09-01 17:06:02 -07001199 return false;
1200
Jeff Layton45151482010-07-06 20:43:02 -04001201 return true;
1202}
1203
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001204static bool
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001205match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001206{
Jeff Layton3f618222013-06-12 19:52:14 -05001207 /*
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001208 * The select_sectype function should either return the ctx->sectype
Jeff Layton3f618222013-06-12 19:52:14 -05001209 * that was specified, or "Unspecified" if that sectype was not
1210 * compatible with the given NEGOTIATE request.
1211 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001212 if (server->ops->select_sectype(server, ctx->sectype)
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301213 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001214 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001215
Jeff Layton3f618222013-06-12 19:52:14 -05001216 /*
1217 * Now check if signing mode is acceptable. No need to check
1218 * global_secflags at this point since if MUST_SIGN is set then
1219 * the server->sign had better be too.
1220 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001221 if (ctx->sign && !server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04001222 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001223
1224 return true;
1225}
1226
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001227static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001228{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001229 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
Jeff Layton9fa114f2012-11-26 11:09:57 -05001230
Shyam Prasad Nc9f1c192021-11-06 11:31:53 +00001231 if (ctx->nosharesock) {
1232 server->nosharesock = true;
1233 return 0;
1234 }
1235
1236 /* this server does not share socket */
1237 if (server->nosharesock)
Jeff Laytona0b3df52013-05-24 07:40:59 -04001238 return 0;
1239
Steve French43cdae82019-06-13 14:26:49 -05001240 /* If multidialect negotiation see if existing sessions match one */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001241 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
Steve French43cdae82019-06-13 14:26:49 -05001242 if (server->vals->protocol_id < SMB30_PROT_ID)
1243 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001244 } else if (strcmp(ctx->vals->version_string,
Steve French43cdae82019-06-13 14:26:49 -05001245 SMBDEFAULT_VERSION_STRING) == 0) {
1246 if (server->vals->protocol_id < SMB21_PROT_ID)
1247 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001248 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
Jeff Layton23db65f2012-05-15 12:20:51 -04001249 return 0;
1250
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001251 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1252 return 0;
1253
Shyam Prasad N7be32482021-10-14 11:52:39 +00001254 if (strcasecmp(server->hostname, ctx->server_hostname))
1255 return 0;
1256
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001257 if (!match_address(server, addr,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001258 (struct sockaddr *)&ctx->srcaddr))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001259 return 0;
1260
1261 if (!match_port(server, addr))
1262 return 0;
1263
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001264 if (!match_security(server, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001265 return 0;
1266
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001267 if (server->echo_interval != ctx->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001268 return 0;
1269
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001270 if (server->rdma != ctx->rdma)
Long Li8339dd32017-11-07 01:54:55 -07001271 return 0;
1272
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001273 if (server->ignore_signature != ctx->ignore_signature)
Steve French4f5c10f2019-09-03 21:18:49 -05001274 return 0;
1275
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001276 if (server->min_offload != ctx->min_offload)
Steve French563317e2019-09-08 23:22:02 -05001277 return 0;
1278
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001279 return 1;
1280}
1281
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001282struct TCP_Server_Info *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001283cifs_find_tcp_session(struct smb3_fs_context *ctx)
Jeff Layton45151482010-07-06 20:43:02 -04001284{
Jeff Laytone7ddee92008-11-14 13:44:38 -05001285 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301287 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04001288 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Paulo Alcantaracdc33632021-07-15 21:53:53 -03001289#ifdef CONFIG_CIFS_DFS_UPCALL
1290 /*
1291 * DFS failover implementation in cifs_reconnect() requires unique tcp sessions for
1292 * DFS connections to do failover properly, so avoid sharing them with regular
1293 * shares or even links that may connect to same server but having completely
1294 * different failover targets.
1295 */
1296 if (server->is_dfs_conn)
1297 continue;
1298#endif
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -03001299 /*
1300 * Skip ses channels since they're only handled in lower layers
1301 * (e.g. cifs_send_recv).
1302 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001303 if (server->is_channel || !match_server(server, ctx))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001304 continue;
1305
Jeff Laytone7ddee92008-11-14 13:44:38 -05001306 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301307 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05001308 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05001309 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301311 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return NULL;
1313}
1314
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001315void
1316cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001318 struct task_struct *task;
1319
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301320 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001321 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301322 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001323 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Steve Frenchdea570e02008-05-06 22:05:51 +00001325
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05001326 /* srv_count can never go negative */
1327 WARN_ON(server->srv_count < 0);
1328
Rob Landleyf1d0c992011-01-22 15:44:05 -06001329 put_net(cifs_net_ns(server));
1330
Jeff Laytone7ddee92008-11-14 13:44:38 -05001331 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301332 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001333
Jeff Laytonc74093b2011-01-11 07:24:23 -05001334 cancel_delayed_work_sync(&server->echo);
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001335 cancel_delayed_work_sync(&server->resolve);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001336
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001337 if (from_reconnect)
1338 /*
1339 * Avoid deadlock here: reconnect work calls
1340 * cifs_put_tcp_session() at its end. Need to be sure
1341 * that reconnect work does nothing with server pointer after
1342 * that step.
1343 */
1344 cancel_delayed_work(&server->reconnect);
1345 else
1346 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001347
Jeff Laytone7ddee92008-11-14 13:44:38 -05001348 spin_lock(&GlobalMid_Lock);
1349 server->tcpStatus = CifsExiting;
1350 spin_unlock(&GlobalMid_Lock);
1351
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001352 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05301353 cifs_fscache_release_client_cookie(server);
1354
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05001355 kfree(server->session_key.response);
1356 server->session_key.response = NULL;
1357 server->session_key.len = 0;
Shyam Prasad N7be32482021-10-14 11:52:39 +00001358 kfree(server->hostname);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001359
1360 task = xchg(&server->tsk, NULL);
1361 if (task)
Eric W. Biederman72abe3b2019-05-15 12:33:50 -05001362 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001365struct TCP_Server_Info *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001366cifs_get_tcp_session(struct smb3_fs_context *ctx)
Jeff Layton63c038c2008-12-01 18:41:46 -05001367{
1368 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05001369 int rc;
1370
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001371 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05001372
1373 /* see if we already have a matching tcp_ses */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001374 tcp_ses = cifs_find_tcp_session(ctx);
Jeff Layton63c038c2008-12-01 18:41:46 -05001375 if (tcp_ses)
1376 return tcp_ses;
1377
1378 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1379 if (!tcp_ses) {
1380 rc = -ENOMEM;
1381 goto out_err;
1382 }
1383
Shyam Prasad N7be32482021-10-14 11:52:39 +00001384 tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
1385 if (!tcp_ses->hostname) {
1386 rc = -ENOMEM;
1387 goto out_err;
1388 }
1389
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001390 tcp_ses->ops = ctx->ops;
1391 tcp_ses->vals = ctx->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06001392 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05001393
Shyam Prasad N6d82c272021-02-03 23:20:46 -08001394 tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001395 tcp_ses->noblockcnt = ctx->rootfs;
1396 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1397 tcp_ses->noautotune = ctx->noautotune;
1398 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1399 tcp_ses->rdma = ctx->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03001400 tcp_ses->in_flight = 0;
Steve French1b63f182019-09-09 22:57:11 -05001401 tcp_ses->max_in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04001402 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05001403 init_waitqueue_head(&tcp_ses->response_q);
1404 init_waitqueue_head(&tcp_ses->request_q);
1405 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1406 mutex_init(&tcp_ses->srv_mutex);
1407 memcpy(tcp_ses->workstation_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001408 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Jeff Layton63c038c2008-12-01 18:41:46 -05001409 memcpy(tcp_ses->server_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001410 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05001411 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05001412 tcp_ses->sequence_number = 0;
Pavel Shilovsky5b964852019-01-18 11:30:26 -08001413 tcp_ses->reconnect_instance = 1;
Steve Frenchfda35942011-01-20 18:06:34 +00001414 tcp_ses->lstrp = jiffies;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001415 tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
Jeff Layton58fa0152012-05-01 17:41:16 -04001416 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001417 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1418 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001419 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001420 INIT_DELAYED_WORK(&tcp_ses->resolve, cifs_resolve_server);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001421 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1422 mutex_init(&tcp_ses->reconnect_mutex);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001423 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001424 sizeof(tcp_ses->srcaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001425 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001426 sizeof(tcp_ses->dstaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001427 if (ctx->use_client_guid)
1428 memcpy(tcp_ses->client_guid, ctx->client_guid,
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001429 SMB2_CLIENT_GUID_SIZE);
1430 else
1431 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05001432 /*
1433 * at this point we are the only ones with the pointer
1434 * to the struct since the kernel thread not created yet
1435 * no need to spinlock this init of tcpStatus or srv_count
1436 */
1437 tcp_ses->tcpStatus = CifsNew;
1438 ++tcp_ses->srv_count;
1439
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001440 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1441 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1442 tcp_ses->echo_interval = ctx->echo_interval * HZ;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001443 else
1444 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07001445 if (tcp_ses->rdma) {
1446#ifndef CONFIG_CIFS_SMB_DIRECT
1447 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1448 rc = -ENOENT;
1449 goto out_err_crypto_release;
1450#endif
1451 tcp_ses->smbd_conn = smbd_get_connection(
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001452 tcp_ses, (struct sockaddr *)&ctx->dstaddr);
Long Li2f894642017-11-22 17:38:34 -07001453 if (tcp_ses->smbd_conn) {
1454 cifs_dbg(VFS, "RDMA transport established\n");
1455 rc = 0;
1456 goto smbd_connected;
1457 } else {
1458 rc = -ENOENT;
1459 goto out_err_crypto_release;
1460 }
1461 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001462 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05001463 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001464 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001465 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001466 }
Long Li2f894642017-11-22 17:38:34 -07001467smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05001468 /*
1469 * since we're in a cifs function already, we know that
1470 * this will succeed. No need for try_module_get().
1471 */
1472 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04001473 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05001474 tcp_ses, "cifsd");
1475 if (IS_ERR(tcp_ses->tsk)) {
1476 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05001477 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05001478 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001479 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001480 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001481 tcp_ses->min_offload = ctx->min_offload;
Steve French01cf3082021-07-01 12:22:47 -05001482 /*
1483 * at this point we are the only ones with the pointer
1484 * to the struct since the kernel thread not created yet
1485 * no need to spinlock this update of tcpStatus
1486 */
Steve Frenchfd88ce92011-04-12 01:01:14 +00001487 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05001488
Aurelien Aptela249cc82021-03-04 17:42:21 +00001489 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
1490 tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
1491 else
1492 tcp_ses->max_credits = ctx->max_credits;
1493
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02001494 tcp_ses->nr_targets = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001495 tcp_ses->ignore_signature = ctx->ignore_signature;
Jeff Layton63c038c2008-12-01 18:41:46 -05001496 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301497 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001498 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301499 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001500
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05301501 cifs_fscache_get_client_cookie(tcp_ses);
1502
Jeff Laytonc74093b2011-01-11 07:24:23 -05001503 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001504 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001505
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001506 /* queue dns resolution delayed work */
1507 cifs_dbg(FYI, "%s: next dns resolution scheduled for %d seconds in the future\n",
1508 __func__, SMB_DNS_RESOLVE_INTERVAL_DEFAULT);
1509
1510 queue_delayed_work(cifsiod_wq, &tcp_ses->resolve, (SMB_DNS_RESOLVE_INTERVAL_DEFAULT * HZ));
1511
Jeff Layton63c038c2008-12-01 18:41:46 -05001512 return tcp_ses;
1513
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001514out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001515 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05001516
Rob Landleyf1d0c992011-01-22 15:44:05 -06001517 put_net(cifs_net_ns(tcp_ses));
1518
Jeff Layton63c038c2008-12-01 18:41:46 -05001519out_err:
1520 if (tcp_ses) {
Shyam Prasad N7be32482021-10-14 11:52:39 +00001521 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05001522 if (tcp_ses->ssocket)
1523 sock_release(tcp_ses->ssocket);
1524 kfree(tcp_ses);
1525 }
1526 return ERR_PTR(rc);
1527}
1528
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001529static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001530{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001531 if (ctx->sectype != Unspecified &&
1532 ctx->sectype != ses->sectype)
Jeff Layton3f618222013-06-12 19:52:14 -05001533 return 0;
1534
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001535 /*
1536 * If an existing session is limited to less channels than
1537 * requested, it should not be reused
1538 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001539 if (ses->chan_max < ctx->max_channels)
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001540 return 0;
1541
Jeff Layton3f618222013-06-12 19:52:14 -05001542 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001543 case Kerberos:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001544 if (!uid_eq(ctx->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001545 return 0;
1546 break;
1547 default:
Jeff Layton04febab2012-01-17 16:09:15 -05001548 /* NULL username means anonymous session */
1549 if (ses->user_name == NULL) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001550 if (!ctx->nullauth)
Jeff Layton04febab2012-01-17 16:09:15 -05001551 return 0;
1552 break;
1553 }
1554
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001555 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05001556 if (strncmp(ses->user_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001557 ctx->username ? ctx->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001558 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001559 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001560 if ((ctx->username && strlen(ctx->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001561 ses->password != NULL &&
1562 strncmp(ses->password,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001563 ctx->password ? ctx->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001564 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001565 return 0;
1566 }
1567 return 1;
1568}
1569
Aurelien Aptelb327a712018-01-24 13:46:10 +01001570/**
1571 * cifs_setup_ipc - helper to setup the IPC tcon for the session
Steve French4c51de12021-09-13 18:29:46 -05001572 * @ses: smb session to issue the request on
1573 * @ctx: the superblock configuration context to use for building the
1574 * new tree connection for the IPC (interprocess communication RPC)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001575 *
1576 * A new IPC connection is made and stored in the session
1577 * tcon_ipc. The IPC tcon has the same lifetime as the session.
1578 */
1579static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001580cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001581{
1582 int rc = 0, xid;
1583 struct cifs_tcon *tcon;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001584 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1585 bool seal = false;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001586 struct TCP_Server_Info *server = ses->server;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001587
1588 /*
1589 * If the mount request that resulted in the creation of the
1590 * session requires encryption, force IPC to be encrypted too.
1591 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001592 if (ctx->seal) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001593 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001594 seal = true;
1595 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001596 cifs_server_dbg(VFS,
Aurelien Aptelb327a712018-01-24 13:46:10 +01001597 "IPC: server doesn't support encryption\n");
1598 return -EOPNOTSUPP;
1599 }
1600 }
1601
1602 tcon = tconInfoAlloc();
1603 if (tcon == NULL)
1604 return -ENOMEM;
1605
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001606 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001607
Aurelien Aptelb327a712018-01-24 13:46:10 +01001608 xid = get_xid();
1609 tcon->ses = ses;
1610 tcon->ipc = true;
1611 tcon->seal = seal;
Ronnie Sahlberg6fd4ea82020-12-14 16:40:21 +10001612 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001613 free_xid(xid);
1614
1615 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001616 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001617 tconInfoFree(tcon);
1618 goto out;
1619 }
1620
1621 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
1622
1623 ses->tcon_ipc = tcon;
1624out:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001625 return rc;
1626}
1627
1628/**
1629 * cifs_free_ipc - helper to release the session IPC tcon
Steve French4c51de12021-09-13 18:29:46 -05001630 * @ses: smb session to unmount the IPC from
Aurelien Aptelb327a712018-01-24 13:46:10 +01001631 *
Paulo Alcantaraf3191fc2021-06-04 19:25:27 -03001632 * Needs to be called everytime a session is destroyed.
1633 *
1634 * On session close, the IPC is closed and the server must release all tcons of the session.
1635 * No need to send a tree disconnect here.
1636 *
1637 * Besides, it will make the server to not close durable and resilient files on session close, as
1638 * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
Aurelien Aptelb327a712018-01-24 13:46:10 +01001639 */
1640static int
1641cifs_free_ipc(struct cifs_ses *ses)
1642{
Aurelien Aptelb327a712018-01-24 13:46:10 +01001643 struct cifs_tcon *tcon = ses->tcon_ipc;
1644
1645 if (tcon == NULL)
1646 return 0;
1647
Aurelien Aptelb327a712018-01-24 13:46:10 +01001648 tconInfoFree(tcon);
1649 ses->tcon_ipc = NULL;
Paulo Alcantaraf3191fc2021-06-04 19:25:27 -03001650 return 0;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001651}
1652
Steve French96daf2b2011-05-27 04:34:02 +00001653static struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001654cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Steve French96daf2b2011-05-27 04:34:02 +00001656 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301658 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04001659 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001660 if (ses->status == CifsExiting)
1661 continue;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001662 if (!match_session(ses, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001663 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05001664 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301665 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001666 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301668 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return NULL;
1670}
1671
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001672void cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05001673{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001674 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05001675 struct TCP_Server_Info *server = ses->server;
Joe Perchesf96637b2013-05-04 22:12:25 -05001676 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001677
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301678 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001679 if (ses->status == CifsExiting) {
1680 spin_unlock(&cifs_tcp_ses_lock);
1681 return;
1682 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001683
1684 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1685 cifs_dbg(FYI, "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->treeName : "NONE");
1686
Jeff Layton14fbf502008-11-14 13:53:46 -05001687 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301688 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001689 return;
1690 }
Steve French0060a4f2021-06-24 15:28:04 -05001691 spin_unlock(&cifs_tcp_ses_lock);
1692
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05001693 /* ses_count can never go negative */
1694 WARN_ON(ses->ses_count < 0);
1695
Steve French0060a4f2021-06-24 15:28:04 -05001696 spin_lock(&GlobalMid_Lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001697 if (ses->status == CifsGood)
1698 ses->status = CifsExiting;
Steve French0060a4f2021-06-24 15:28:04 -05001699 spin_unlock(&GlobalMid_Lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001700
Aurelien Aptelb327a712018-01-24 13:46:10 +01001701 cifs_free_ipc(ses);
1702
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001703 if (ses->status == CifsExiting && server->ops->logoff) {
1704 xid = get_xid();
1705 rc = server->ops->logoff(xid, ses);
1706 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001707 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001708 __func__, rc);
1709 _free_xid(xid);
1710 }
1711
1712 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001713 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301714 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001715
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001716 /* close any extra channels */
1717 if (ses->chan_count > 1) {
1718 int i;
1719
1720 for (i = 1; i < ses->chan_count; i++)
1721 cifs_put_tcp_session(ses->chans[i].server, 0);
1722 }
1723
Jeff Layton14fbf502008-11-14 13:53:46 -05001724 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001725 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05001726}
1727
Jeff Layton8a8798a2012-01-17 16:09:15 -05001728#ifdef CONFIG_KEYS
1729
Chen Gang057d6332013-07-19 09:01:36 +08001730/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
1731#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001732
1733/* Populate username and pw fields from keyring if possible */
1734static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001735cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001736{
1737 int rc = 0;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001738 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01001739 const char *delim, *payload;
1740 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001741 ssize_t len;
1742 struct key *key;
1743 struct TCP_Server_Info *server = ses->server;
1744 struct sockaddr_in *sa;
1745 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01001746 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001747
1748 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
1749 if (!desc)
1750 return -ENOMEM;
1751
1752 /* try to find an address key first */
1753 switch (server->dstaddr.ss_family) {
1754 case AF_INET:
1755 sa = (struct sockaddr_in *)&server->dstaddr;
1756 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
1757 break;
1758 case AF_INET6:
1759 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
1760 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
1761 break;
1762 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001763 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
1764 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001765 rc = -EINVAL;
1766 goto out_err;
1767 }
1768
Joe Perchesf96637b2013-05-04 22:12:25 -05001769 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001770 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001771 if (IS_ERR(key)) {
1772 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001773 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001774 rc = PTR_ERR(key);
1775 goto out_err;
1776 }
1777
1778 /* didn't work, try to find a domain key */
1779 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05001780 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001781 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001782 if (IS_ERR(key)) {
1783 rc = PTR_ERR(key);
1784 goto out_err;
1785 }
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001786 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001787 }
1788
1789 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00001790 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001791 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05001792 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001793 goto out_key_put;
1794 }
1795
1796 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01001797 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001798 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05001799 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001800 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001801 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
1802 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001803 rc = -EINVAL;
1804 goto out_key_put;
1805 }
1806
1807 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001808 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001809 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
1810 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001811 rc = -EINVAL;
1812 goto out_key_put;
1813 }
1814
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001815 ctx->username = kstrndup(payload, len, GFP_KERNEL);
1816 if (!ctx->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001817 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
1818 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001819 rc = -ENOMEM;
1820 goto out_key_put;
1821 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001822 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001823
1824 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001825 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001826 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001827 rc = -EINVAL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001828 kfree(ctx->username);
1829 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001830 goto out_key_put;
1831 }
1832
1833 ++delim;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001834 ctx->password = kstrndup(delim, len, GFP_KERNEL);
1835 if (!ctx->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001836 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
1837 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001838 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001839 kfree(ctx->username);
1840 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001841 goto out_key_put;
1842 }
1843
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001844 /*
1845 * If we have a domain key then we must set the domainName in the
1846 * for the request.
1847 */
1848 if (is_domain && ses->domainName) {
Al Viro8d767222021-03-05 15:02:34 -05001849 ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001850 if (!ctx->domainname) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001851 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
1852 len);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001853 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001854 kfree(ctx->username);
1855 ctx->username = NULL;
1856 kfree_sensitive(ctx->password);
1857 ctx->password = NULL;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001858 goto out_key_put;
1859 }
1860 }
1861
Jeff Layton8a8798a2012-01-17 16:09:15 -05001862out_key_put:
1863 up_read(&key->sem);
1864 key_put(key);
1865out_err:
1866 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05001867 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001868 return rc;
1869}
1870#else /* ! CONFIG_KEYS */
1871static inline int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001872cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
Jeff Layton8a8798a2012-01-17 16:09:15 -05001873 struct cifs_ses *ses __attribute__((unused)))
1874{
1875 return -ENOSYS;
1876}
1877#endif /* CONFIG_KEYS */
1878
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01001879/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001880 * cifs_get_smb_ses - get a session matching @ctx data from @server
Steve French4c51de12021-09-13 18:29:46 -05001881 * @server: server to setup the session to
1882 * @ctx: superblock configuration context to use to setup the session
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01001883 *
1884 * This function assumes it is being called from cifs_mount() where we
1885 * already got a server reference (server refcount +1). See
1886 * cifs_get_tcon() for refcount explanations.
1887 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001888struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001889cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Layton36988c72010-04-24 07:57:43 -04001890{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04001891 int rc = -ENOMEM;
1892 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00001893 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001894 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
1895 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04001896
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001897 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04001898
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001899 ses = cifs_find_smb_ses(server, ctx);
Jeff Layton36988c72010-04-24 07:57:43 -04001900 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001901 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
1902 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04001903
Jeff Layton36988c72010-04-24 07:57:43 -04001904 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04001905 rc = cifs_negotiate_protocol(xid, ses);
1906 if (rc) {
1907 mutex_unlock(&ses->session_mutex);
1908 /* problem -- put our ses reference */
1909 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001910 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04001911 return ERR_PTR(rc);
1912 }
Jeff Layton36988c72010-04-24 07:57:43 -04001913 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001914 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04001915 rc = cifs_setup_session(xid, ses,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001916 ctx->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04001917 if (rc) {
1918 mutex_unlock(&ses->session_mutex);
1919 /* problem -- put our reference */
1920 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001921 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04001922 return ERR_PTR(rc);
1923 }
1924 }
1925 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04001926
1927 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001928 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001929 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04001930 return ses;
1931 }
1932
Joe Perchesf96637b2013-05-04 22:12:25 -05001933 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04001934 ses = sesInfoAlloc();
1935 if (ses == NULL)
1936 goto get_ses_fail;
1937
1938 /* new SMB session uses our server ref */
1939 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001940 if (server->dstaddr.ss_family == AF_INET6)
Steve Frenchb438fcf2021-02-20 19:24:11 -06001941 sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04001942 else
Steve Frenchb438fcf2021-02-20 19:24:11 -06001943 sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04001944
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001945 if (ctx->username) {
1946 ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
Steve French8727c8a2011-02-25 01:11:56 -06001947 if (!ses->user_name)
1948 goto get_ses_fail;
1949 }
Jeff Layton36988c72010-04-24 07:57:43 -04001950
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001951 /* ctx->password freed at unmount */
1952 if (ctx->password) {
1953 ses->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Layton36988c72010-04-24 07:57:43 -04001954 if (!ses->password)
1955 goto get_ses_fail;
1956 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001957 if (ctx->domainname) {
1958 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05001959 if (!ses->domainName)
1960 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04001961 }
Shyam Prasad N49bd49f2021-11-05 19:03:57 +00001962 if (ctx->workstation_name) {
1963 ses->workstation_name = kstrdup(ctx->workstation_name,
1964 GFP_KERNEL);
1965 if (!ses->workstation_name)
1966 goto get_ses_fail;
1967 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001968 if (ctx->domainauto)
1969 ses->domainAuto = ctx->domainauto;
1970 ses->cred_uid = ctx->cred_uid;
1971 ses->linux_uid = ctx->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00001972
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001973 ses->sectype = ctx->sectype;
1974 ses->sign = ctx->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04001975 mutex_lock(&ses->session_mutex);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001976
1977 /* add server as first channel */
1978 ses->chans[0].server = server;
1979 ses->chan_count = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001980 ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001981
Jeff Layton198b5682010-04-24 07:57:48 -04001982 rc = cifs_negotiate_protocol(xid, ses);
1983 if (!rc)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001984 rc = cifs_setup_session(xid, ses, ctx->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001985
1986 /* each channel uses a different signing key */
1987 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
1988 sizeof(ses->smb3signingkey));
1989
Jeff Layton36988c72010-04-24 07:57:43 -04001990 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00001991 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04001992 goto get_ses_fail;
1993
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001994 /* success, put it on the list and add it as first channel */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301995 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04001996 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301997 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04001998
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001999 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002000
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002001 cifs_setup_ipc(ses, ctx);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002002
Jeff Layton36988c72010-04-24 07:57:43 -04002003 return ses;
2004
2005get_ses_fail:
2006 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002007 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002008 return ERR_PTR(rc);
2009}
2010
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002011static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002012{
2013 if (tcon->tidStatus == CifsExiting)
2014 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002015 if (strncmp(tcon->treeName, ctx->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002016 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002017 if (tcon->seal != ctx->seal)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002018 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002019 if (tcon->snapshot_time != ctx->snapshot_time)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002020 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002021 if (tcon->handle_timeout != ctx->handle_timeout)
Steve Frenchca567eb2019-03-29 16:31:07 -05002022 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002023 if (tcon->no_lease != ctx->no_lease)
Steve French3e7a02d2019-09-11 21:46:20 -05002024 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002025 if (tcon->nodelete != ctx->nodelete)
Steve French82e93672020-05-19 03:06:57 -05002026 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002027 return 1;
2028}
2029
Steve French96daf2b2011-05-27 04:34:02 +00002030static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002031cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002034 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302036 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002037 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002038 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Paulo Alcantaraf3c852b2021-06-04 19:25:33 -03002039
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002040 if (!match_tcon(tcon, ctx))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002041 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002042 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302043 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return tcon;
2045 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302046 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return NULL;
2048}
2049
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002050void
Steve French96daf2b2011-05-27 04:34:02 +00002051cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002052{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002053 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002054 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002055
Aurelien Aptelb327a712018-01-24 13:46:10 +01002056 /*
2057 * IPC tcon share the lifetime of their session and are
2058 * destroyed in the session put function
2059 */
2060 if (tcon == NULL || tcon->ipc)
2061 return;
2062
2063 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002064 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302065 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002066 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302067 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002068 return;
2069 }
2070
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05002071 /* tc_count can never go negative */
2072 WARN_ON(tcon->tc_count < 0);
2073
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002074 if (tcon->use_witness) {
2075 int rc;
2076
2077 rc = cifs_swn_unregister(tcon);
2078 if (rc < 0) {
2079 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
2080 __func__, rc);
2081 }
2082 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002083
Jeff Laytonf1987b42008-11-15 11:12:47 -05002084 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302085 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002086
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002087 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002088 if (ses->server->ops->tree_disconnect)
2089 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002090 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002091
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302092 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002093 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002094 cifs_put_smb_ses(ses);
2095}
2096
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002097/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002098 * cifs_get_tcon - get a tcon matching @ctx data from @ses
Steve French4c51de12021-09-13 18:29:46 -05002099 * @ses: smb session to issue the request on
2100 * @ctx: the superblock configuration context to use for building the
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002101 *
2102 * - tcon refcount is the number of mount points using the tcon.
2103 * - ses refcount is the number of tcon using the session.
2104 *
2105 * 1. This function assumes it is being called from cifs_mount() where
2106 * we already got a session reference (ses refcount +1).
2107 *
2108 * 2. Since we're in the context of adding a mount point, the end
2109 * result should be either:
2110 *
2111 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2112 * its session refcount incremented (1 new tcon). This +1 was
2113 * already done in (1).
2114 *
2115 * b) an existing tcon with refcount+1 (add a mount point to it) and
2116 * identical ses refcount (no new tcon). Because of (1) we need to
2117 * decrement the ses refcount.
2118 */
Steve French96daf2b2011-05-27 04:34:02 +00002119static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002120cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002121{
2122 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002123 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002124
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002125 tcon = cifs_find_tcon(ses, ctx);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002126 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002127 /*
2128 * tcon has refcount already incremented but we need to
2129 * decrement extra ses reference gotten by caller (case b)
2130 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002131 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002132 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002133 return tcon;
2134 }
2135
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002136 if (!ses->server->ops->tree_connect) {
2137 rc = -ENOSYS;
2138 goto out_fail;
2139 }
2140
Jeff Laytond00c28d2010-04-24 07:57:44 -04002141 tcon = tconInfoAlloc();
2142 if (tcon == NULL) {
2143 rc = -ENOMEM;
2144 goto out_fail;
2145 }
2146
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002147 if (ctx->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06002148 if (ses->server->vals->protocol_id == 0) {
2149 cifs_dbg(VFS,
2150 "Use SMB2 or later for snapshot mount option\n");
2151 rc = -EOPNOTSUPP;
2152 goto out_fail;
2153 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002154 tcon->snapshot_time = ctx->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06002155 }
2156
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002157 if (ctx->handle_timeout) {
Steve Frenchca567eb2019-03-29 16:31:07 -05002158 if (ses->server->vals->protocol_id == 0) {
2159 cifs_dbg(VFS,
2160 "Use SMB2.1 or later for handle timeout option\n");
2161 rc = -EOPNOTSUPP;
2162 goto out_fail;
2163 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002164 tcon->handle_timeout = ctx->handle_timeout;
Steve Frenchca567eb2019-03-29 16:31:07 -05002165 }
2166
Jeff Laytond00c28d2010-04-24 07:57:44 -04002167 tcon->ses = ses;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002168 if (ctx->password) {
2169 tcon->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002170 if (!tcon->password) {
2171 rc = -ENOMEM;
2172 goto out_fail;
2173 }
2174 }
2175
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002176 if (ctx->seal) {
Steve French23657ad2018-04-22 15:14:58 -05002177 if (ses->server->vals->protocol_id == 0) {
2178 cifs_dbg(VFS,
2179 "SMB3 or later required for encryption\n");
2180 rc = -EOPNOTSUPP;
2181 goto out_fail;
2182 } else if (tcon->ses->server->capabilities &
2183 SMB2_GLOBAL_CAP_ENCRYPTION)
2184 tcon->seal = true;
2185 else {
2186 cifs_dbg(VFS, "Encryption is not supported on share\n");
2187 rc = -EOPNOTSUPP;
2188 goto out_fail;
2189 }
2190 }
2191
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002192 if (ctx->linux_ext) {
Steve French8505c8b2018-06-18 14:01:59 -05002193 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05002194 tcon->posix_extensions = true;
Joe Perchesa0a30362020-04-14 22:42:53 -07002195 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05002196 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07002197 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
Steve French8505c8b2018-06-18 14:01:59 -05002198 rc = -EOPNOTSUPP;
2199 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05002200 }
Steve Frenchb3266142018-05-20 23:41:10 -05002201 }
Steve Frenchb3266142018-05-20 23:41:10 -05002202
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002203 /*
2204 * BB Do we need to wrap session_mutex around this TCon call and Unix
2205 * SetFS as we do on SessSetup and reconnect?
2206 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002207 xid = get_xid();
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002208 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2209 ctx->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002210 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05002211 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002212 if (rc)
2213 goto out_fail;
2214
Steve Frenchb618f002015-11-03 09:15:03 -06002215 tcon->use_persistent = false;
2216 /* check if SMB2 or later, CIFS does not support persistent handles */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002217 if (ctx->persistent) {
Steve Frenchb618f002015-11-03 09:15:03 -06002218 if (ses->server->vals->protocol_id == 0) {
2219 cifs_dbg(VFS,
2220 "SMB3 or later required for persistent handles\n");
2221 rc = -EOPNOTSUPP;
2222 goto out_fail;
2223 } else if (ses->server->capabilities &
2224 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2225 tcon->use_persistent = true;
2226 else /* persistent handles requested but not supported */ {
2227 cifs_dbg(VFS,
2228 "Persistent handles not supported on share\n");
2229 rc = -EOPNOTSUPP;
2230 goto out_fail;
2231 }
2232 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2233 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002234 && (ctx->nopersistent == false)) {
Steve Frenchb618f002015-11-03 09:15:03 -06002235 cifs_dbg(FYI, "enabling persistent handles\n");
2236 tcon->use_persistent = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002237 } else if (ctx->resilient) {
Steve French592fafe2015-11-03 10:08:53 -06002238 if (ses->server->vals->protocol_id == 0) {
2239 cifs_dbg(VFS,
2240 "SMB2.1 or later required for resilient handles\n");
2241 rc = -EOPNOTSUPP;
2242 goto out_fail;
2243 }
2244 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06002245 }
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +02002246
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002247 tcon->use_witness = false;
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +02002248 if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002249 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2250 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002251 /*
2252 * Set witness in use flag in first place
2253 * to retry registration in the echo task
2254 */
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002255 tcon->use_witness = true;
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002256 /* And try to register immediately */
2257 rc = cifs_swn_register(tcon);
2258 if (rc < 0) {
2259 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2260 goto out_fail;
2261 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002262 } else {
2263 /* TODO: try to extend for non-cluster uses (eg multichannel) */
2264 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2265 rc = -EOPNOTSUPP;
2266 goto out_fail;
2267 }
2268 } else {
2269 cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2270 rc = -EOPNOTSUPP;
2271 goto out_fail;
2272 }
2273 }
Steve Frenchb618f002015-11-03 09:15:03 -06002274
Steve Frenchcae53f72019-09-03 17:49:46 -05002275 /* If the user really knows what they are doing they can override */
2276 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002277 if (ctx->cache_ro)
Steve Frenchcae53f72019-09-03 17:49:46 -05002278 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002279 else if (ctx->cache_rw)
Steve Frenchcae53f72019-09-03 17:49:46 -05002280 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2281 }
2282
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002283 if (ctx->no_lease) {
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302284 if (ses->server->vals->protocol_id == 0) {
2285 cifs_dbg(VFS,
2286 "SMB2 or later required for nolease option\n");
2287 rc = -EOPNOTSUPP;
2288 goto out_fail;
2289 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002290 tcon->no_lease = ctx->no_lease;
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302291 }
2292
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002293 /*
2294 * We can have only one retry value for a connection to a share so for
2295 * resources mounted more than once to the same server share the last
2296 * value passed in for the retry flag is used.
2297 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002298 tcon->retry = ctx->retry;
2299 tcon->nocase = ctx->nocase;
Steve French3c6e65e2020-10-21 00:15:42 -05002300 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002301 tcon->nohandlecache = ctx->nohandlecache;
Steve French3c6e65e2020-10-21 00:15:42 -05002302 else
Jiapeng Zhong2be449f2021-01-14 17:09:20 +08002303 tcon->nohandlecache = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002304 tcon->nodelete = ctx->nodelete;
2305 tcon->local_lease = ctx->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07002306 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002307
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302308 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002309 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302310 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002311
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302312 cifs_fscache_get_super_cookie(tcon);
2313
Jeff Laytond00c28d2010-04-24 07:57:44 -04002314 return tcon;
2315
2316out_fail:
2317 tconInfoFree(tcon);
2318 return ERR_PTR(rc);
2319}
2320
Jeff Layton9d002df2010-10-06 19:51:11 -04002321void
2322cifs_put_tlink(struct tcon_link *tlink)
2323{
2324 if (!tlink || IS_ERR(tlink))
2325 return;
2326
2327 if (!atomic_dec_and_test(&tlink->tl_count) ||
2328 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2329 tlink->tl_time = jiffies;
2330 return;
2331 }
2332
2333 if (!IS_ERR(tlink_tcon(tlink)))
2334 cifs_put_tcon(tlink_tcon(tlink));
2335 kfree(tlink);
2336 return;
2337}
Jeff Laytond00c28d2010-04-24 07:57:44 -04002338
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002339static int
2340compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2341{
2342 struct cifs_sb_info *old = CIFS_SB(sb);
2343 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002344 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2345 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002346
2347 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2348 return 0;
2349
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002350 if (old->mnt_cifs_serverino_autodisabled)
2351 newflags &= ~CIFS_MOUNT_SERVER_INUM;
2352
2353 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002354 return 0;
2355
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002356 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002357 * We want to share sb only if we don't specify an r/wsize or
2358 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002359 */
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002360 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002361 return 0;
2362
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002363 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002364 return 0;
2365
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002366 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2367 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002368 return 0;
2369
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002370 if (old->ctx->file_mode != new->ctx->file_mode ||
2371 old->ctx->dir_mode != new->ctx->dir_mode)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002372 return 0;
2373
2374 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2375 return 0;
2376
Steve French57804642021-02-24 12:12:53 -06002377 if (old->ctx->acregmax != new->ctx->acregmax)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002378 return 0;
Steve French4c9f9482021-02-23 15:50:57 -06002379 if (old->ctx->acdirmax != new->ctx->acdirmax)
2380 return 0;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002381
2382 return 1;
2383}
2384
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002385static int
2386match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2387{
2388 struct cifs_sb_info *old = CIFS_SB(sb);
2389 struct cifs_sb_info *new = mnt_data->cifs_sb;
Ronnie Sahlbergfe129262020-01-22 11:07:56 +10002390 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2391 old->prepath;
2392 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2393 new->prepath;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002394
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002395 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002396 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002397 else if (!old_set && !new_set)
2398 return 1;
2399
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002400 return 0;
2401}
2402
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002403int
2404cifs_match_super(struct super_block *sb, void *data)
2405{
2406 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002407 struct smb3_fs_context *ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002408 struct cifs_sb_info *cifs_sb;
2409 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00002410 struct cifs_ses *ses;
2411 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002412 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002413 int rc = 0;
2414
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002415 spin_lock(&cifs_tcp_ses_lock);
2416 cifs_sb = CIFS_SB(sb);
2417 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
Steve French9ed38fd2021-09-23 19:18:37 -05002418 if (tlink == NULL) {
2419 /* can not match superblock if tlink were ever null */
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002420 spin_unlock(&cifs_tcp_ses_lock);
Steve French9ed38fd2021-09-23 19:18:37 -05002421 return 0;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002422 }
2423 tcon = tlink_tcon(tlink);
2424 ses = tcon->ses;
2425 tcp_srv = ses->server;
2426
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002427 ctx = mnt_data->ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002428
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002429 if (!match_server(tcp_srv, ctx) ||
2430 !match_session(ses, ctx) ||
2431 !match_tcon(tcon, ctx) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002432 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002433 rc = 0;
2434 goto out;
2435 }
2436
2437 rc = compare_mount_options(sb, mnt_data);
2438out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002439 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04002440 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002441 return rc;
2442}
2443
Jeff Layton09e50d52008-07-23 10:11:19 -04002444#ifdef CONFIG_DEBUG_LOCK_ALLOC
2445static struct lock_class_key cifs_key[2];
2446static struct lock_class_key cifs_slock_key[2];
2447
2448static inline void
2449cifs_reclassify_socket4(struct socket *sock)
2450{
2451 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002452 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002453 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2454 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2455}
2456
2457static inline void
2458cifs_reclassify_socket6(struct socket *sock)
2459{
2460 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002461 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002462 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2463 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2464}
2465#else
2466static inline void
2467cifs_reclassify_socket4(struct socket *sock)
2468{
2469}
2470
2471static inline void
2472cifs_reclassify_socket6(struct socket *sock)
2473{
2474}
2475#endif
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00002478static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
Steve French50c2f752007-07-13 00:33:32 +00002480 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Steve French50c2f752007-07-13 00:33:32 +00002482 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 /* mask a nibble at a time and encode */
2484 target[j] = 'A' + (0x0F & (source[i] >> 4));
2485 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00002486 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488
2489}
2490
Ben Greear3eb9a882010-09-01 17:06:02 -07002491static int
2492bind_socket(struct TCP_Server_Info *server)
2493{
2494 int rc = 0;
2495 if (server->srcaddr.ss_family != AF_UNSPEC) {
2496 /* Bind to the specified local IP address */
2497 struct socket *socket = server->ssocket;
2498 rc = socket->ops->bind(socket,
2499 (struct sockaddr *) &server->srcaddr,
2500 sizeof(server->srcaddr));
2501 if (rc < 0) {
2502 struct sockaddr_in *saddr4;
2503 struct sockaddr_in6 *saddr6;
2504 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2505 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2506 if (saddr6->sin6_family == AF_INET6)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002507 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002508 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002509 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002510 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002511 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002512 }
2513 }
2514 return rc;
2515}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002518ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
2520 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002521 /*
2522 * some servers require RFC1001 sessinit before sending
2523 * negprot - BB check reconnection in case where second
2524 * sessinit is sent but no second negprot
2525 */
2526 struct rfc1002_session_packet *ses_init_buf;
2527 struct smb_hdr *smb_buf;
2528 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2529 GFP_KERNEL);
2530 if (ses_init_buf) {
2531 ses_init_buf->trailer.session_req.called_len = 32;
2532
Colin Ian King997152f2016-01-25 16:25:54 +00002533 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002534 rfc1002mangle(ses_init_buf->trailer.
2535 session_req.called_name,
2536 server->server_RFC1001_name,
2537 RFC1001_NAME_LEN_WITH_NULL);
2538 else
2539 rfc1002mangle(ses_init_buf->trailer.
2540 session_req.called_name,
2541 DEFAULT_CIFS_CALLED_NAME,
2542 RFC1001_NAME_LEN_WITH_NULL);
2543
2544 ses_init_buf->trailer.session_req.calling_len = 32;
2545
2546 /*
2547 * calling name ends in null (byte 16) from old smb
2548 * convention.
2549 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05002550 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002551 rfc1002mangle(ses_init_buf->trailer.
2552 session_req.calling_name,
2553 server->workstation_RFC1001_name,
2554 RFC1001_NAME_LEN_WITH_NULL);
2555 else
2556 rfc1002mangle(ses_init_buf->trailer.
2557 session_req.calling_name,
2558 "LINUX_CIFS_CLNT",
2559 RFC1001_NAME_LEN_WITH_NULL);
2560
2561 ses_init_buf->trailer.session_req.scope1 = 0;
2562 ses_init_buf->trailer.session_req.scope2 = 0;
2563 smb_buf = (struct smb_hdr *)ses_init_buf;
2564
2565 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002566 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002567 rc = smb_send(server, smb_buf, 0x44);
2568 kfree(ses_init_buf);
2569 /*
2570 * RFC1001 layer in at least one server
2571 * requires very short break before negprot
2572 * presumably because not expecting negprot
2573 * to follow so fast. This is a simple
2574 * solution that works without
2575 * complicating the code and causes no
2576 * significant slowing down on mount
2577 * for everyone else
2578 */
2579 usleep_range(1000, 2000);
2580 }
2581 /*
2582 * else the negprot may still work without this
2583 * even though malloc failed
2584 */
2585
2586 return rc;
2587}
2588
2589static int
2590generic_ip_connect(struct TCP_Server_Info *server)
2591{
2592 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00002593 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002594 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002595 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002596 struct sockaddr *saddr;
2597
2598 saddr = (struct sockaddr *) &server->dstaddr;
2599
2600 if (server->dstaddr.ss_family == AF_INET6) {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002601 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2602
2603 sport = ipv6->sin6_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002604 slen = sizeof(struct sockaddr_in6);
2605 sfamily = AF_INET6;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002606 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2607 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002608 } else {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002609 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2610
2611 sport = ipv4->sin_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002612 slen = sizeof(struct sockaddr_in);
2613 sfamily = AF_INET;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002614 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2615 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002618 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06002619 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2620 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (rc < 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002622 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002623 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002626
2627 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05002628 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002629 server->ssocket = socket;
2630 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002631 if (sfamily == AF_INET6)
2632 cifs_reclassify_socket6(socket);
2633 else
2634 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 }
2636
Ben Greear3eb9a882010-09-01 17:06:02 -07002637 rc = bind_socket(server);
2638 if (rc < 0)
2639 return rc;
2640
Jeff Laytond5c56052008-12-01 18:42:33 -05002641 /*
2642 * Eventually check for other socket options to change from
2643 * the default. sock_setsockopt not used because it expects
2644 * user space buffer
2645 */
2646 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00002647 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00002648
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002649 /* make the bufsizes depend on wsize/rsize and max requests */
2650 if (server->noautotune) {
2651 if (socket->sk->sk_sndbuf < (200 * 1024))
2652 socket->sk->sk_sndbuf = 200 * 1024;
2653 if (socket->sk->sk_rcvbuf < (140 * 1024))
2654 socket->sk->sk_rcvbuf = 140 * 1024;
2655 }
2656
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02002657 if (server->tcp_nodelay)
2658 tcp_sock_set_nodelay(socket->sk);
Steve French6a5fa2362010-01-01 01:28:43 +00002659
Joe Perchesf96637b2013-05-04 22:12:25 -05002660 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002661 socket->sk->sk_sndbuf,
2662 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2663
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002664 rc = socket->ops->connect(socket, saddr, slen,
2665 server->noblockcnt ? O_NONBLOCK : 0);
Paulo Alcantara (SUSE)d532cc72019-10-10 12:31:58 -03002666 /*
2667 * When mounting SMB root file systems, we do not want to block in
2668 * connect. Otherwise bail out and then let cifs_reconnect() perform
2669 * reconnect failover - if possible.
2670 */
2671 if (server->noblockcnt && rc == -EINPROGRESS)
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002672 rc = 0;
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002673 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002674 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Steve Frenchd7171cd2021-11-04 15:56:37 -05002675 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002676 sock_release(socket);
2677 server->ssocket = NULL;
2678 return rc;
2679 }
Steve Frenchd7171cd2021-11-04 15:56:37 -05002680 trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002681 if (sport == htons(RFC1001_PORT))
2682 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00002683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return rc;
2685}
2686
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002687static int
2688ip_connect(struct TCP_Server_Info *server)
2689{
Steve French6da97912011-03-13 18:55:55 +00002690 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002691 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2692 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2693
2694 if (server->dstaddr.ss_family == AF_INET6)
2695 sport = &addr6->sin6_port;
2696 else
2697 sport = &addr->sin_port;
2698
2699 if (*sport == 0) {
2700 int rc;
2701
2702 /* try with 445 port at first */
2703 *sport = htons(CIFS_PORT);
2704
2705 rc = generic_ip_connect(server);
2706 if (rc >= 0)
2707 return rc;
2708
2709 /* if it failed, try with 139 port */
2710 *sport = htons(RFC1001_PORT);
2711 }
2712
2713 return generic_ip_connect(server);
2714}
2715
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002716void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002717 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Steve French8af18972007-02-14 04:42:51 +00002718{
Steve Frenchbc044992020-12-11 19:48:26 -06002719 /*
2720 * If we are reconnecting then should we check to see if
Steve French8af18972007-02-14 04:42:51 +00002721 * any requested capabilities changed locally e.g. via
2722 * remount but we can not do much about it here
2723 * if they have (even if we could detect it by the following)
2724 * Perhaps we could add a backpointer to array of sb from tcon
2725 * or if we change to make all sb to same share the same
2726 * sb as NFS - then we only have one backpointer to sb.
2727 * What if we wanted to mount the server share twice once with
Steve Frenchbc044992020-12-11 19:48:26 -06002728 * and once without posixacls or posix paths?
2729 */
Steve French8af18972007-02-14 04:42:51 +00002730 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00002731
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002732 if (ctx && ctx->no_linux_ext) {
Steve Frenchc18c8422007-07-18 23:21:09 +00002733 tcon->fsUnixInfo.Capability = 0;
2734 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05002735 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002736 return;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002737 } else if (ctx)
Steve Frenchc18c8422007-07-18 23:21:09 +00002738 tcon->unix_ext = 1; /* Unix Extensions supported */
2739
Jiapeng Zhong16a78852021-01-14 18:02:23 +08002740 if (!tcon->unix_ext) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002741 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002742 return;
2743 }
Steve French50c2f752007-07-13 00:33:32 +00002744
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002745 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00002746 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05002747 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve Frenchbc044992020-12-11 19:48:26 -06002748 /*
2749 * check for reconnect case in which we do not
2750 * want to change the mount behavior if we can avoid it
2751 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002752 if (ctx == NULL) {
Steve Frenchbc044992020-12-11 19:48:26 -06002753 /*
2754 * turn off POSIX ACL and PATHNAMES if not set
2755 * originally at mount time
2756 */
Steve French8af18972007-02-14 04:42:51 +00002757 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
2758 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002759 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
2760 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002761 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00002762 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002763 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002764 cifs_dbg(VFS, "possible reconnect error\n");
2765 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00002766 }
Steve French8af18972007-02-14 04:42:51 +00002767 }
Steve French50c2f752007-07-13 00:33:32 +00002768
Steve French6848b732011-05-26 18:38:54 +00002769 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002770 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00002771
Steve French8af18972007-02-14 04:42:51 +00002772 cap &= CIFS_UNIX_CAP_MASK;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002773 if (ctx && ctx->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00002774 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002775 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002776 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002777 if (cifs_sb)
2778 cifs_sb->mnt_cifs_flags |=
2779 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00002780 }
2781
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002782 if (ctx && ctx->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00002783 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002784 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002785 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002786 if (cifs_sb)
2787 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00002788 CIFS_MOUNT_POSIX_PATHS;
2789 }
Steve French50c2f752007-07-13 00:33:32 +00002790
Joe Perchesf96637b2013-05-04 22:12:25 -05002791 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00002792#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00002793 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002794 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002795 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002796 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002797 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002798 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002799 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002800 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002801 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002802 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002803 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002804 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002805 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002806 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00002807 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002808 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00002809 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002810 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00002811#endif /* CIFS_DEBUG2 */
2812 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002813 if (ctx == NULL)
Joe Perchesf96637b2013-05-04 22:12:25 -05002814 cifs_dbg(FYI, "resetting capabilities failed\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002815 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002816 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 +00002817
Steve French8af18972007-02-14 04:42:51 +00002818 }
2819 }
2820}
2821
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002822int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002823{
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002824 struct smb3_fs_context *ctx = cifs_sb->ctx;
2825
Jeff Layton2de970f2010-10-06 19:51:12 -04002826 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
2827
Al Viro2ced6f62011-06-17 09:20:04 -04002828 spin_lock_init(&cifs_sb->tlink_tree_lock);
2829 cifs_sb->tlink_tree = RB_ROOT;
2830
Frank Sorensonf52aa792020-02-12 15:31:48 -06002831 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002832 ctx->file_mode, ctx->dir_mode);
Steve French3b795212008-11-13 19:45:32 +00002833
Ronnie Sahlberg387ec582020-12-14 16:40:20 +10002834 /* this is needed for ASCII cp to Unicode converts */
2835 if (ctx->iocharset == NULL) {
2836 /* load_nls_default cannot return null */
2837 cifs_sb->local_nls = load_nls_default();
2838 } else {
2839 cifs_sb->local_nls = load_nls(ctx->iocharset);
2840 if (cifs_sb->local_nls == NULL) {
2841 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
2842 ctx->iocharset);
2843 return -ELIBACC;
2844 }
2845 }
2846 ctx->local_nls = cifs_sb->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05302847
Ronnie Sahlberg2d39f502020-12-14 16:40:25 +10002848 smb3_update_mnt_flags(cifs_sb);
2849
2850 if (ctx->direct_io)
Joe Perchesf96637b2013-05-04 22:12:25 -05002851 cifs_dbg(FYI, "mounting share using direct i/o\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002852 if (ctx->cache_ro) {
Steve French83bbfa72019-08-27 23:58:54 -05002853 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
2854 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002855 } else if (ctx->cache_rw) {
Steve French41e033f2019-08-30 02:12:41 -05002856 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
2857 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
2858 CIFS_MOUNT_RW_CACHE);
Steve French83bbfa72019-08-27 23:58:54 -05002859 }
Steve French3b795212008-11-13 19:45:32 +00002860
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002861 if ((ctx->cifs_acl) && (ctx->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05002862 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002863
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002864 if (ctx->prepath) {
2865 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002866 if (cifs_sb->prepath == NULL)
2867 return -ENOMEM;
Shyam Prasad Na738c932021-02-11 03:26:54 -08002868 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002869 }
2870
2871 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002872}
2873
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002874/* Release all succeed connections */
2875static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
2876 unsigned int xid,
2877 struct TCP_Server_Info *server,
2878 struct cifs_ses *ses, struct cifs_tcon *tcon)
2879{
2880 int rc = 0;
2881
2882 if (tcon)
2883 cifs_put_tcon(tcon);
2884 else if (ses)
2885 cifs_put_smb_ses(ses);
2886 else if (server)
2887 cifs_put_tcp_session(server, 0);
2888 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
2889 free_xid(xid);
2890}
2891
2892/* Get connections for tcp, ses and tcon */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002893static int mount_get_conns(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002894 unsigned int *xid,
2895 struct TCP_Server_Info **nserver,
2896 struct cifs_ses **nses, struct cifs_tcon **ntcon)
2897{
2898 int rc = 0;
2899 struct TCP_Server_Info *server;
2900 struct cifs_ses *ses;
2901 struct cifs_tcon *tcon;
2902
2903 *nserver = NULL;
2904 *nses = NULL;
2905 *ntcon = NULL;
2906
2907 *xid = get_xid();
2908
2909 /* get a reference to a tcp session */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002910 server = cifs_get_tcp_session(ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002911 if (IS_ERR(server)) {
2912 rc = PTR_ERR(server);
2913 return rc;
2914 }
2915
2916 *nserver = server;
2917
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002918 /* get a reference to a SMB session */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002919 ses = cifs_get_smb_ses(server, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002920 if (IS_ERR(ses)) {
2921 rc = PTR_ERR(ses);
2922 return rc;
2923 }
2924
2925 *nses = ses;
2926
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002927 if ((ctx->persistent == true) && (!(ses->server->capabilities &
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002928 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002929 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002930 return -EOPNOTSUPP;
2931 }
2932
2933 /* search for existing tcon to this server share */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002934 tcon = cifs_get_tcon(ses, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002935 if (IS_ERR(tcon)) {
2936 rc = PTR_ERR(tcon);
2937 return rc;
2938 }
2939
2940 *ntcon = tcon;
2941
2942 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
2943 if (tcon->posix_extensions)
2944 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
2945
2946 /* tell server which Unix caps we support */
2947 if (cap_unix(tcon->ses)) {
2948 /*
2949 * reset of caps checks mount to see if unix extensions disabled
2950 * for just this mount.
2951 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002952 reset_cifs_unix_caps(*xid, tcon, cifs_sb, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002953 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
2954 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
2955 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
2956 return -EACCES;
2957 } else
2958 tcon->unix_ext = 0; /* server does not support them */
2959
2960 /* do not care if a following call succeed - informational */
Steve French1981eba2019-08-29 22:33:38 -05002961 if (!tcon->pipe && server->ops->qfs_tcon) {
Amir Goldstein0f060932020-02-03 21:46:43 +02002962 server->ops->qfs_tcon(*xid, tcon, cifs_sb);
Steve French1981eba2019-08-29 22:33:38 -05002963 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
2964 if (tcon->fsDevInfo.DeviceCharacteristics &
Steve French52870d52019-10-01 21:25:46 -05002965 cpu_to_le32(FILE_READ_ONLY_DEVICE))
Steve French1981eba2019-08-29 22:33:38 -05002966 cifs_dbg(VFS, "mounted to read only share\n");
Steve French41e033f2019-08-30 02:12:41 -05002967 else if ((cifs_sb->mnt_cifs_flags &
2968 CIFS_MOUNT_RW_CACHE) == 0)
Steve French1981eba2019-08-29 22:33:38 -05002969 cifs_dbg(VFS, "read only mount of RW share\n");
Steve French41e033f2019-08-30 02:12:41 -05002970 /* no need to log a RW mount of a typical RW share */
Steve French1981eba2019-08-29 22:33:38 -05002971 }
2972 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002973
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002974 /*
2975 * Clamp the rsize/wsize mount arguments if they are too big for the server
Steve French0c2b5f72020-12-15 13:28:50 -06002976 * and set the rsize/wsize to the negotiated values if not passed in by
2977 * the user on mount
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002978 */
Steve French0c2b5f72020-12-15 13:28:50 -06002979 if ((cifs_sb->ctx->wsize == 0) ||
2980 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002981 cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
Steve French0c2b5f72020-12-15 13:28:50 -06002982 if ((cifs_sb->ctx->rsize == 0) ||
2983 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002984 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002985
2986 return 0;
2987}
2988
2989static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
2990 struct cifs_tcon *tcon)
2991{
2992 struct tcon_link *tlink;
2993
2994 /* hang the tcon off of the superblock */
2995 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
2996 if (tlink == NULL)
2997 return -ENOMEM;
2998
2999 tlink->tl_uid = ses->linux_uid;
3000 tlink->tl_tcon = tcon;
3001 tlink->tl_time = jiffies;
3002 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3003 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3004
3005 cifs_sb->master_tlink = tlink;
3006 spin_lock(&cifs_sb->tlink_tree_lock);
3007 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3008 spin_unlock(&cifs_sb->tlink_tree_lock);
3009
3010 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3011 TLINK_IDLE_EXPIRE);
3012 return 0;
3013}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003014
Steve French2d6d5892009-04-09 00:36:44 +00003015#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003016static int mount_get_dfs_conns(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
3017 unsigned int *xid, struct TCP_Server_Info **nserver,
3018 struct cifs_ses **nses, struct cifs_tcon **ntcon)
3019{
3020 int rc;
3021
3022 ctx->nosharesock = true;
3023 rc = mount_get_conns(ctx, cifs_sb, xid, nserver, nses, ntcon);
3024 if (*nserver) {
3025 cifs_dbg(FYI, "%s: marking tcp session as a dfs connection\n", __func__);
3026 spin_lock(&cifs_tcp_ses_lock);
3027 (*nserver)->is_dfs_conn = true;
3028 spin_unlock(&cifs_tcp_ses_lock);
3029 }
3030 return rc;
3031}
3032
Steve French6d3ea7e2012-11-28 22:34:41 -06003033/*
3034 * cifs_build_path_to_root returns full path to root when we do not have an
Steve French201023c2021-02-15 11:03:45 -06003035 * existing connection (tcon)
Steve French6d3ea7e2012-11-28 22:34:41 -06003036 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003037static char *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003038build_unc_path_to_root(const struct smb3_fs_context *ctx,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003039 const struct cifs_sb_info *cifs_sb, bool useppath)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003040{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003041 char *full_path, *pos;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003042 unsigned int pplen = useppath && ctx->prepath ?
3043 strlen(ctx->prepath) + 1 : 0;
3044 unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003045
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003046 if (unc_len > MAX_TREE_SIZE)
3047 return ERR_PTR(-EINVAL);
3048
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003049 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003050 if (full_path == NULL)
3051 return ERR_PTR(-ENOMEM);
3052
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003053 memcpy(full_path, ctx->UNC, unc_len);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003054 pos = full_path + unc_len;
3055
3056 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003057 *pos = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003058 memcpy(pos + 1, ctx->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003059 pos += pplen;
3060 }
3061
3062 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003063 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003064 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003065 return full_path;
3066}
Sean Finneydd613942011-04-11 13:19:30 +00003067
Steve French4c51de12021-09-13 18:29:46 -05003068/*
Paulo Alcantara1c780222018-11-14 16:24:03 -02003069 * expand_dfs_referral - Perform a dfs referral query and update the cifs_sb
3070 *
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003071 * If a referral is found, cifs_sb->ctx->mount_options will be (re-)allocated
Sean Finney046462a2011-04-11 13:19:33 +00003072 * to a string containing updated options for the submount. Otherwise it
3073 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003074 *
3075 * Returns the rc from get_dfs_path to the caller, which can be used to
3076 * determine whether there were referrals.
3077 */
3078static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003079expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003080 struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003081 char *ref_path)
Sean Finneydd613942011-04-11 13:19:30 +00003082{
3083 int rc;
Paulo Alcantara1c780222018-11-14 16:24:03 -02003084 struct dfs_info3_param referral = {0};
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003085 char *full_path = NULL, *mdata = NULL;
Sean Finneydd613942011-04-11 13:19:30 +00003086
Aurelien Aptel83930722018-09-20 18:10:25 -07003087 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
3088 return -EREMOTE;
3089
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003090 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
Sean Finneydd613942011-04-11 13:19:30 +00003091 if (IS_ERR(full_path))
3092 return PTR_ERR(full_path);
3093
Paulo Alcantara1c780222018-11-14 16:24:03 -02003094 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
3095 ref_path, &referral, NULL);
3096 if (!rc) {
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003097 char *fake_devname = NULL;
3098
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003099 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options,
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003100 full_path + 1, &referral,
3101 &fake_devname);
Paulo Alcantara1c780222018-11-14 16:24:03 -02003102 free_dfs_info_param(&referral);
Sean Finney046462a2011-04-11 13:19:33 +00003103
Sean Finneydd613942011-04-11 13:19:30 +00003104 if (IS_ERR(mdata)) {
3105 rc = PTR_ERR(mdata);
3106 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003107 } else {
Ronnie Sahlbergabd4af42021-02-08 16:48:31 +10003108 /*
3109 * We can not clear out the whole structure since we
3110 * no longer have an explicit function to parse
3111 * a mount-string. Instead we need to clear out the
3112 * individual fields that are no longer valid.
3113 */
3114 kfree(ctx->prepath);
3115 ctx->prepath = NULL;
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003116 rc = cifs_setup_volume_info(ctx, mdata, fake_devname);
Sean Finneydd613942011-04-11 13:19:30 +00003117 }
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003118 kfree(fake_devname);
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003119 kfree(cifs_sb->ctx->mount_options);
3120 cifs_sb->ctx->mount_options = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003121 }
3122 kfree(full_path);
3123 return rc;
3124}
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003125
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003126static int get_next_dfs_tgt(struct dfs_cache_tgt_list *tgt_list,
3127 struct dfs_cache_tgt_iterator **tgt_it)
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003128{
3129 if (!*tgt_it)
3130 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
3131 else
3132 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
3133 return !*tgt_it ? -EHOSTDOWN : 0;
3134}
3135
3136static int update_vol_info(const struct dfs_cache_tgt_iterator *tgt_it,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003137 struct smb3_fs_context *fake_ctx, struct smb3_fs_context *ctx)
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003138{
3139 const char *tgt = dfs_cache_get_tgt_name(tgt_it);
3140 int len = strlen(tgt) + 2;
3141 char *new_unc;
3142
3143 new_unc = kmalloc(len, GFP_KERNEL);
3144 if (!new_unc)
3145 return -ENOMEM;
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10003146 scnprintf(new_unc, len, "\\%s", tgt);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003147
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003148 kfree(ctx->UNC);
3149 ctx->UNC = new_unc;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003150
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003151 if (fake_ctx->prepath) {
3152 kfree(ctx->prepath);
3153 ctx->prepath = fake_ctx->prepath;
3154 fake_ctx->prepath = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003155 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003156 memcpy(&ctx->dstaddr, &fake_ctx->dstaddr, sizeof(ctx->dstaddr));
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003157
3158 return 0;
3159}
3160
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003161static int do_dfs_failover(const char *path, const char *full_path, struct cifs_sb_info *cifs_sb,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003162 struct smb3_fs_context *ctx, struct cifs_ses *root_ses,
3163 unsigned int *xid, struct TCP_Server_Info **server,
3164 struct cifs_ses **ses, struct cifs_tcon **tcon)
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003165{
3166 int rc;
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003167 char *npath = NULL;
Paulo Alcantarab6236612021-07-16 03:26:41 -03003168 struct dfs_cache_tgt_list tgt_list = DFS_CACHE_TGT_LIST_INIT(tgt_list);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003169 struct dfs_cache_tgt_iterator *tgt_it = NULL;
Paulo Alcantara85132222021-02-24 20:59:21 -03003170 struct smb3_fs_context tmp_ctx = {NULL};
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003171
3172 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
3173 return -EOPNOTSUPP;
3174
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003175 npath = dfs_cache_canonical_path(path, cifs_sb->local_nls, cifs_remap(cifs_sb));
3176 if (IS_ERR(npath))
3177 return PTR_ERR(npath);
Paulo Alcantara85132222021-02-24 20:59:21 -03003178
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003179 cifs_dbg(FYI, "%s: path=%s full_path=%s\n", __func__, npath, full_path);
3180
3181 rc = dfs_cache_noreq_find(npath, NULL, &tgt_list);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003182 if (rc)
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003183 goto out;
Paulo Alcantara85132222021-02-24 20:59:21 -03003184 /*
3185 * We use a 'tmp_ctx' here because we need pass it down to the mount_{get,put} functions to
3186 * test connection against new DFS targets.
3187 */
3188 rc = smb3_fs_context_dup(&tmp_ctx, ctx);
3189 if (rc)
3190 goto out;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003191
3192 for (;;) {
Paulo Alcantara85132222021-02-24 20:59:21 -03003193 struct dfs_info3_param ref = {0};
3194 char *fake_devname = NULL, *mdata = NULL;
3195
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003196 /* Get next DFS target server - if any */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003197 rc = get_next_dfs_tgt(&tgt_list, &tgt_it);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003198 if (rc)
3199 break;
Paulo Alcantara85132222021-02-24 20:59:21 -03003200
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003201 rc = dfs_cache_get_tgt_referral(npath, tgt_it, &ref);
Paulo Alcantara85132222021-02-24 20:59:21 -03003202 if (rc)
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003203 break;
Paulo Alcantara85132222021-02-24 20:59:21 -03003204
3205 cifs_dbg(FYI, "%s: old ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC,
3206 tmp_ctx.prepath);
3207
3208 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, &ref,
3209 &fake_devname);
3210 free_dfs_info_param(&ref);
3211
3212 if (IS_ERR(mdata)) {
3213 rc = PTR_ERR(mdata);
3214 mdata = NULL;
3215 } else
3216 rc = cifs_setup_volume_info(&tmp_ctx, mdata, fake_devname);
3217
3218 kfree(mdata);
3219 kfree(fake_devname);
3220
3221 if (rc)
3222 break;
3223
3224 cifs_dbg(FYI, "%s: new ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC,
3225 tmp_ctx.prepath);
3226
3227 mount_put_conns(cifs_sb, *xid, *server, *ses, *tcon);
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003228 rc = mount_get_dfs_conns(&tmp_ctx, cifs_sb, xid, server, ses, tcon);
Paulo Alcantara85132222021-02-24 20:59:21 -03003229 if (!rc || (*server && *ses)) {
3230 /*
3231 * We were able to connect to new target server. Update current context with
3232 * new target server.
3233 */
3234 rc = update_vol_info(tgt_it, &tmp_ctx, ctx);
3235 break;
3236 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003237 }
3238 if (!rc) {
Paulo Alcantara85132222021-02-24 20:59:21 -03003239 cifs_dbg(FYI, "%s: final ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC,
3240 tmp_ctx.prepath);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003241 /*
Paulo Alcantara85132222021-02-24 20:59:21 -03003242 * Update DFS target hint in DFS referral cache with the target server we
3243 * successfully reconnected to.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003244 */
Paulo Alcantara85132222021-02-24 20:59:21 -03003245 rc = dfs_cache_update_tgthint(*xid, root_ses ? root_ses : *ses, cifs_sb->local_nls,
3246 cifs_remap(cifs_sb), path, tgt_it);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003247 }
Paulo Alcantara85132222021-02-24 20:59:21 -03003248
3249out:
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003250 kfree(npath);
Paulo Alcantara85132222021-02-24 20:59:21 -03003251 smb3_cleanup_fs_context_contents(&tmp_ctx);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003252 dfs_cache_free_tgts(&tgt_list);
3253 return rc;
3254}
Steve French2d6d5892009-04-09 00:36:44 +00003255#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003256
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -06003257/* TODO: all callers to this are broken. We are not parsing mount_options here
3258 * we should pass a clone of the original context?
3259 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003260int
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003261cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003263 int rc;
Sean Finneydd613942011-04-11 13:19:30 +00003264
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003265 if (devname) {
3266 cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
3267 rc = smb3_parse_devname(devname, ctx);
3268 if (rc) {
3269 cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
3270 return rc;
3271 }
3272 }
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003273
3274 if (mntopts) {
3275 char *ip;
3276
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003277 rc = smb3_parse_opt(mntopts, "ip", &ip);
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003278 if (rc) {
3279 cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3280 return rc;
3281 }
3282
3283 rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3284 kfree(ip);
3285 if (!rc) {
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003286 cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
3287 return -EINVAL;
3288 }
3289 }
3290
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003291 if (ctx->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003292 cifs_dbg(FYI, "Anonymous login\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003293 kfree(ctx->username);
3294 ctx->username = NULL;
3295 } else if (ctx->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 /* BB fixme parse for domain name here */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003297 cifs_dbg(FYI, "Username: %s\n", ctx->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003299 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003300 /* In userspace mount helper we can get user name from alternate
3301 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003302 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
3304
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003305 return 0;
Jeff Layton04db79b2011-07-06 08:10:38 -04003306}
3307
Aurelien Aptela6b50582016-05-25 19:59:09 +02003308static int
3309cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3310 unsigned int xid,
3311 struct cifs_tcon *tcon,
3312 struct cifs_sb_info *cifs_sb,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003313 char *full_path,
3314 int added_treename)
Aurelien Aptela6b50582016-05-25 19:59:09 +02003315{
3316 int rc;
3317 char *s;
3318 char sep, tmp;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003319 int skip = added_treename ? 1 : 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +02003320
3321 sep = CIFS_DIR_SEP(cifs_sb);
3322 s = full_path;
3323
3324 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3325 while (rc == 0) {
3326 /* skip separators */
3327 while (*s == sep)
3328 s++;
3329 if (!*s)
3330 break;
3331 /* next separator */
3332 while (*s && *s != sep)
3333 s++;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003334 /*
3335 * if the treename is added, we then have to skip the first
3336 * part within the separators
3337 */
3338 if (skip) {
3339 skip = 0;
3340 continue;
3341 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02003342 /*
3343 * temporarily null-terminate the path at the end of
3344 * the current component
3345 */
3346 tmp = *s;
3347 *s = 0;
3348 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3349 full_path);
3350 *s = tmp;
3351 }
3352 return rc;
3353}
3354
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003355/*
3356 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
3357 * otherwise 0.
3358 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003359static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003360 const unsigned int xid,
3361 struct TCP_Server_Info *server,
3362 struct cifs_tcon *tcon)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003363{
Jeff Layton1daaae82012-03-21 06:30:40 -04003364 int rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003365 char *full_path;
3366
3367 if (!server->ops->is_path_accessible)
3368 return -EOPNOTSUPP;
3369
3370 /*
3371 * cifs_build_path_to_root works only when we have a valid tcon
3372 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003373 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003374 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3375 if (full_path == NULL)
3376 return -ENOMEM;
3377
3378 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3379
3380 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3381 full_path);
3382 if (rc != 0 && rc != -EREMOTE) {
3383 kfree(full_path);
3384 return rc;
3385 }
3386
3387 if (rc != -EREMOTE) {
3388 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003389 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003390 if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07003391 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 -02003392 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3393 rc = 0;
3394 }
3395 }
3396
3397 kfree(full_path);
3398 return rc;
3399}
3400
3401#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003402static void set_root_ses(struct cifs_sb_info *cifs_sb, const uuid_t *mount_id, struct cifs_ses *ses,
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003403 struct cifs_ses **root_ses)
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03003404{
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003405 if (ses) {
3406 spin_lock(&cifs_tcp_ses_lock);
3407 ses->ses_count++;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003408 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003409 dfs_cache_add_refsrv_session(mount_id, ses);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003410 }
3411 *root_ses = ses;
3412}
3413
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003414/* Set up next dfs prefix path in @dfs_path */
3415static int next_dfs_prepath(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx,
3416 const unsigned int xid, struct TCP_Server_Info *server,
3417 struct cifs_tcon *tcon, char **dfs_path)
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003418{
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003419 char *path, *npath;
3420 int added_treename = is_tcon_dfs(tcon);
3421 int rc;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003422
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003423 path = cifs_build_path_to_root(ctx, cifs_sb, tcon, added_treename);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003424 if (!path)
3425 return -ENOMEM;
3426
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003427 rc = is_path_remote(cifs_sb, ctx, xid, server, tcon);
3428 if (rc == -EREMOTE) {
3429 struct smb3_fs_context v = {NULL};
3430 /* if @path contains a tree name, skip it in the prefix path */
3431 if (added_treename) {
3432 rc = smb3_parse_devname(path, &v);
3433 if (rc)
3434 goto out;
3435 npath = build_unc_path_to_root(&v, cifs_sb, true);
3436 smb3_cleanup_fs_context_contents(&v);
3437 } else {
3438 v.UNC = ctx->UNC;
3439 v.prepath = path + 1;
3440 npath = build_unc_path_to_root(&v, cifs_sb, true);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003441 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003442
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003443 if (IS_ERR(npath)) {
3444 rc = PTR_ERR(npath);
3445 goto out;
3446 }
3447
3448 kfree(*dfs_path);
3449 *dfs_path = npath;
3450 rc = -EREMOTE;
3451 }
3452
3453out:
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003454 kfree(path);
3455 return rc;
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03003456}
3457
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003458/* Check if resolved targets can handle any DFS referrals */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003459static int is_referral_server(const char *ref_path, struct cifs_sb_info *cifs_sb,
3460 struct cifs_tcon *tcon, bool *ref_server)
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003461{
3462 int rc;
3463 struct dfs_info3_param ref = {0};
3464
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003465 cifs_dbg(FYI, "%s: ref_path=%s\n", __func__, ref_path);
3466
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003467 if (is_tcon_dfs(tcon)) {
3468 *ref_server = true;
3469 } else {
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003470 char *npath;
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003471
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003472 npath = dfs_cache_canonical_path(ref_path, cifs_sb->local_nls, cifs_remap(cifs_sb));
3473 if (IS_ERR(npath))
3474 return PTR_ERR(npath);
3475
3476 rc = dfs_cache_noreq_find(npath, &ref, NULL);
3477 kfree(npath);
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003478 if (rc) {
3479 cifs_dbg(VFS, "%s: dfs_cache_noreq_find: failed (rc=%d)\n", __func__, rc);
3480 return rc;
3481 }
3482 cifs_dbg(FYI, "%s: ref.flags=0x%x\n", __func__, ref.flags);
3483 /*
3484 * Check if all targets are capable of handling DFS referrals as per
3485 * MS-DFSC 2.2.4 RESP_GET_DFS_REFERRAL.
3486 */
3487 *ref_server = !!(ref.flags & DFSREF_REFERRAL_SERVER);
3488 free_dfs_info_param(&ref);
3489 }
3490 return 0;
3491}
3492
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003493int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003494{
3495 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003496 unsigned int xid;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003497 struct TCP_Server_Info *server = NULL;
3498 struct cifs_ses *ses = NULL, *root_ses = NULL;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003499 struct cifs_tcon *tcon = NULL;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003500 int count = 0;
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003501 uuid_t mount_id = {0};
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003502 char *ref_path = NULL, *full_path = NULL;
3503 char *oldmnt = NULL;
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003504 bool ref_server = false;
Al Virodd854462011-06-17 08:24:42 -04003505
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003506 rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003507 /*
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003508 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
3509 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003510 *
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003511 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
3512 * to respond with PATH_NOT_COVERED to requests that include the prefix.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003513 */
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003514 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
3515 dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb), ctx->UNC + 1, NULL,
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003516 NULL)) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003517 if (rc)
3518 goto error;
3519 /* Check if it is fully accessible and then mount it */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003520 rc = is_path_remote(cifs_sb, ctx, xid, server, tcon);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003521 if (!rc)
3522 goto out;
3523 if (rc != -EREMOTE)
3524 goto error;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003525 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003526
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003527 mount_put_conns(cifs_sb, xid, server, ses, tcon);
3528 /*
3529 * Ignore error check here because we may failover to other targets from cached a
3530 * referral.
3531 */
3532 (void)mount_get_dfs_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
Paulo Alcantaraf3c852b2021-06-04 19:25:33 -03003533
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003534 /* Get path of DFS root */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003535 ref_path = build_unc_path_to_root(ctx, cifs_sb, false);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003536 if (IS_ERR(ref_path)) {
3537 rc = PTR_ERR(ref_path);
3538 ref_path = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003539 goto error;
3540 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003541
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003542 uuid_gen(&mount_id);
3543 set_root_ses(cifs_sb, &mount_id, ses, &root_ses);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003544 do {
3545 /* Save full path of last DFS path we used to resolve final target server */
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003546 kfree(full_path);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003547 full_path = build_unc_path_to_root(ctx, cifs_sb, !!count);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003548 if (IS_ERR(full_path)) {
3549 rc = PTR_ERR(full_path);
Dan Carpenter20b135e2020-08-05 12:52:07 +03003550 full_path = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003551 break;
3552 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003553 /* Chase referral */
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003554 oldmnt = cifs_sb->ctx->mount_options;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003555 rc = expand_dfs_referral(xid, root_ses, ctx, cifs_sb, ref_path + 1);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003556 if (rc)
3557 break;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003558 /* Connect to new DFS target only if we were redirected */
Ronnie Sahlberga2a52a82020-11-10 09:12:31 +10003559 if (oldmnt != cifs_sb->ctx->mount_options) {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003560 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003561 rc = mount_get_dfs_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003562 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003563 if (rc && !server && !ses) {
3564 /* Failed to connect. Try to connect to other targets in the referral. */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003565 rc = do_dfs_failover(ref_path + 1, full_path, cifs_sb, ctx, root_ses, &xid,
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003566 &server, &ses, &tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003567 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003568 if (rc == -EACCES || rc == -EOPNOTSUPP || !server || !ses)
3569 break;
3570 if (!tcon)
3571 continue;
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003572
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003573 /* Make sure that requests go through new root servers */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003574 rc = is_referral_server(ref_path + 1, cifs_sb, tcon, &ref_server);
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003575 if (rc)
3576 break;
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003577 if (ref_server)
3578 set_root_ses(cifs_sb, &mount_id, ses, &root_ses);
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003579
Paulo Alcantaraff2c54a2021-02-24 20:59:22 -03003580 /* Get next dfs path and then continue chasing them if -EREMOTE */
3581 rc = next_dfs_prepath(cifs_sb, ctx, xid, server, tcon, &ref_path);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003582 /* Prevent recursion on broken link referrals */
3583 if (rc == -EREMOTE && ++count > MAX_NESTED_LINKS)
3584 rc = -ELOOP;
3585 } while (rc == -EREMOTE);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003586
Steve French63f94e92021-07-13 19:40:33 -05003587 if (rc || !tcon || !ses)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003588 goto error;
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003589
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003590 kfree(ref_path);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003591 /*
3592 * Store DFS full path in both superblock and tree connect structures.
3593 *
3594 * For DFS root mounts, the prefix path (cifs_sb->prepath) is preserved during reconnect so
3595 * only the root path is set in cifs_sb->origin_fullpath and tcon->dfs_path. And for DFS
3596 * links, the prefix path is included in both and may be changed during reconnect. See
3597 * cifs_tree_connect().
3598 */
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003599 ref_path = dfs_cache_canonical_path(full_path, cifs_sb->local_nls, cifs_remap(cifs_sb));
3600 kfree(full_path);
3601 full_path = NULL;
3602
3603 if (IS_ERR(ref_path)) {
3604 rc = PTR_ERR(ref_path);
3605 ref_path = NULL;
3606 goto error;
3607 }
3608 cifs_sb->origin_fullpath = ref_path;
3609
3610 ref_path = kstrdup(cifs_sb->origin_fullpath, GFP_KERNEL);
3611 if (!ref_path) {
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003612 rc = -ENOMEM;
3613 goto error;
3614 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003615 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003616 tcon->dfs_path = ref_path;
3617 ref_path = NULL;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003618 spin_unlock(&cifs_tcp_ses_lock);
3619
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01003620 /*
3621 * After reconnecting to a different server, unique ids won't
3622 * match anymore, so we disable serverino. This prevents
3623 * dentry revalidation to think the dentry are stale (ESTALE).
3624 */
3625 cifs_autodisable_serverino(cifs_sb);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003626 /*
3627 * Force the use of prefix path to support failover on DFS paths that
3628 * resolve to targets that have different prefix paths.
3629 */
3630 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3631 kfree(cifs_sb->prepath);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003632 cifs_sb->prepath = ctx->prepath;
3633 ctx->prepath = NULL;
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003634 uuid_copy(&cifs_sb->dfs_mount_id, &mount_id);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003635
Jeff Layton70fe7dc2007-11-16 22:21:07 +00003636out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003637 free_xid(xid);
Ronnie Sahlberg387ec582020-12-14 16:40:20 +10003638 cifs_try_adding_channels(cifs_sb, ses);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003639 return mount_setup_tlink(cifs_sb, ses, tcon);
3640
3641error:
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003642 kfree(ref_path);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003643 kfree(full_path);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003644 kfree(cifs_sb->origin_fullpath);
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003645 dfs_cache_put_refsrv_sessions(&mount_id);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003646 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 return rc;
3648}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003649#else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003650int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003651{
3652 int rc = 0;
3653 unsigned int xid;
3654 struct cifs_ses *ses;
3655 struct cifs_tcon *tcon;
3656 struct TCP_Server_Info *server;
3657
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003658 rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003659 if (rc)
3660 goto error;
3661
3662 if (tcon) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003663 rc = is_path_remote(cifs_sb, ctx, xid, server, tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003664 if (rc == -EREMOTE)
3665 rc = -EOPNOTSUPP;
3666 if (rc)
3667 goto error;
3668 }
3669
3670 free_xid(xid);
3671
3672 return mount_setup_tlink(cifs_sb, ses, tcon);
3673
3674error:
3675 mount_put_conns(cifs_sb, xid, server, ses, tcon);
3676 return rc;
3677}
3678#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Jeff Layton8d1bca32011-06-11 21:17:10 -04003680/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01003681 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04003682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003684CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00003685 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 const struct nls_table *nls_codepage)
3687{
3688 struct smb_hdr *smb_buffer;
3689 struct smb_hdr *smb_buffer_response;
3690 TCONX_REQ *pSMB;
3691 TCONX_RSP *pSMBr;
3692 unsigned char *bcc_ptr;
3693 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05003694 int length;
3695 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
3697 if (ses == NULL)
3698 return -EIO;
3699
3700 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00003701 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00003703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 smb_buffer_response = smb_buffer;
3705
3706 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3707 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003708
Pavel Shilovsky88257362012-05-23 14:01:59 +04003709 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 smb_buffer->Uid = ses->Suid;
3711 pSMB = (TCONX_REQ *) smb_buffer;
3712 pSMBr = (TCONX_RSP *) smb_buffer_response;
3713
3714 pSMB->AndXCommand = 0xFF;
3715 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01003717 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08003718 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003719 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003720 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003721 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Jeff Layton38d77c52013-05-26 07:01:00 -04003724 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3726
3727 if (ses->capabilities & CAP_STATUS32) {
3728 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3729 }
3730 if (ses->capabilities & CAP_DFS) {
3731 smb_buffer->Flags2 |= SMBFLG2_DFS;
3732 }
3733 if (ses->capabilities & CAP_UNICODE) {
3734 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3735 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06003736 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00003737 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00003738 (/* server len*/ + 256 /* share len */), nls_codepage);
3739 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 bcc_ptr += 2; /* skip trailing null */
3741 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 strcpy(bcc_ptr, tree);
3743 bcc_ptr += strlen(tree) + 1;
3744 }
3745 strcpy(bcc_ptr, "?????");
3746 bcc_ptr += strlen("?????");
3747 bcc_ptr += 1;
3748 count = bcc_ptr - &pSMB->Password[0];
Qinglang Miao1a0e7f72020-07-25 16:56:01 +08003749 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 pSMB->ByteCount = cpu_to_le16(count);
3751
Steve French133672e2007-11-13 22:41:37 +00003752 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05003753 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003756 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00003757 bool is_unicode;
3758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00003760 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 tcon->tid = smb_buffer_response->Tid;
3762 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05003763 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003764 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00003765 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3766 is_unicode = true;
3767 else
3768 is_unicode = false;
3769
Jeff Laytoncc20c032009-04-30 07:16:21 -04003770
Steve French50c2f752007-07-13 00:33:32 +00003771 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00003772 if (length == 3) {
3773 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3774 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003775 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01003776 tcon->ipc = true;
3777 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00003778 }
3779 } else if (length == 2) {
3780 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3781 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05003782 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00003783 }
3784 }
Steve French50c2f752007-07-13 00:33:32 +00003785 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04003786 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05003787 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04003788
3789 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04003790 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06003791 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00003792 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04003793 nls_codepage);
3794
Joe Perchesf96637b2013-05-04 22:12:25 -05003795 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003796
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003797 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00003798 (smb_buffer_response->WordCount == 7))
3799 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00003800 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3801 else
3802 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05003803 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 }
3805
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003806 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 return rc;
3808}
3809
Al Viro2e32cf52013-10-03 12:53:37 -04003810static void delayed_free(struct rcu_head *p)
3811{
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003812 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
3813
3814 unload_nls(cifs_sb->local_nls);
Ronnie Sahlbergc741cba2020-12-14 16:40:16 +10003815 smb3_cleanup_fs_context(cifs_sb->ctx);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003816 kfree(cifs_sb);
Al Viro2e32cf52013-10-03 12:53:37 -04003817}
3818
Al Viro2a9b9952011-06-17 09:27:16 -04003819void
3820cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
Jeff Laytonb647c352010-10-28 11:16:44 -04003822 struct rb_root *root = &cifs_sb->tlink_tree;
3823 struct rb_node *node;
3824 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Jeff Layton2de970f2010-10-06 19:51:12 -04003826 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3827
Jeff Laytonb647c352010-10-28 11:16:44 -04003828 spin_lock(&cifs_sb->tlink_tree_lock);
3829 while ((node = rb_first(root))) {
3830 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3831 cifs_get_tlink(tlink);
3832 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3833 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00003834
Jeff Laytonb647c352010-10-28 11:16:44 -04003835 spin_unlock(&cifs_sb->tlink_tree_lock);
3836 cifs_put_tlink(tlink);
3837 spin_lock(&cifs_sb->tlink_tree_lock);
3838 }
3839 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003840
Aurelien Aptela6b50582016-05-25 19:59:09 +02003841 kfree(cifs_sb->prepath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003842#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003843 dfs_cache_put_refsrv_sessions(&cifs_sb->dfs_mount_id);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003844 kfree(cifs_sb->origin_fullpath);
3845#endif
Al Viro2e32cf52013-10-03 12:53:37 -04003846 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00003847}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003849int
3850cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
3852 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02003853 struct TCP_Server_Info *server = cifs_ses_server(ses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003855 if (!server->ops->need_neg || !server->ops->negotiate)
3856 return -ENOSYS;
3857
Jeff Layton198b5682010-04-24 07:57:48 -04003858 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003859 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04003860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003862 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04003863 if (rc == 0) {
3864 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04003865 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04003866 server->tcpStatus = CifsGood;
3867 else
3868 rc = -EHOSTDOWN;
3869 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
Steve French26b994f2008-08-06 05:11:33 +00003871
Jeff Layton198b5682010-04-24 07:57:48 -04003872 return rc;
3873}
Steve French26b994f2008-08-06 05:11:33 +00003874
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003875int
3876cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3877 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04003878{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003879 int rc = -ENOSYS;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02003880 struct TCP_Server_Info *server = cifs_ses_server(ses);
Jeff Layton198b5682010-04-24 07:57:48 -04003881
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003882 if (!ses->binding) {
3883 ses->capabilities = server->capabilities;
YANG LIed6b1922021-01-11 17:15:28 +08003884 if (!linuxExtEnabled)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003885 ses->capabilities &= (~server->vals->cap_unix);
3886
3887 if (ses->auth_key.response) {
3888 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
3889 ses->auth_key.response);
3890 kfree(ses->auth_key.response);
3891 ses->auth_key.response = NULL;
3892 ses->auth_key.len = 0;
3893 }
3894 }
Steve French20418ac2009-04-30 16:13:32 +00003895
Joe Perchesf96637b2013-05-04 22:12:25 -05003896 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00003897 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003898
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003899 if (server->ops->sess_setup)
3900 rc = server->ops->sess_setup(xid, ses, nls_info);
3901
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05003902 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003903 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05003904
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 return rc;
3906}
3907
Jeff Layton8a8798a2012-01-17 16:09:15 -05003908static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003909cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003910{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003911 ctx->sectype = ses->sectype;
Jeff Layton3f618222013-06-12 19:52:14 -05003912
3913 /* krb5 is special, since we don't need username or pw */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003914 if (ctx->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003915 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003916
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003917 return cifs_set_cifscreds(ctx, ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003918}
3919
Steve French96daf2b2011-05-27 04:34:02 +00003920static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003921cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04003922{
Jeff Layton8a8798a2012-01-17 16:09:15 -05003923 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00003924 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3925 struct cifs_ses *ses;
3926 struct cifs_tcon *tcon = NULL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003927 struct smb3_fs_context *ctx;
Jeff Layton9d002df2010-10-06 19:51:11 -04003928
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003929 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3930 if (ctx == NULL)
Dan Carpenter803ab972012-01-24 11:39:22 +03003931 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04003932
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003933 ctx->local_nls = cifs_sb->local_nls;
3934 ctx->linux_uid = fsuid;
3935 ctx->cred_uid = fsuid;
3936 ctx->UNC = master_tcon->treeName;
3937 ctx->retry = master_tcon->retry;
3938 ctx->nocase = master_tcon->nocase;
3939 ctx->nohandlecache = master_tcon->nohandlecache;
3940 ctx->local_lease = master_tcon->local_lease;
3941 ctx->no_lease = master_tcon->no_lease;
3942 ctx->resilient = master_tcon->use_resilient;
3943 ctx->persistent = master_tcon->use_persistent;
3944 ctx->handle_timeout = master_tcon->handle_timeout;
3945 ctx->no_linux_ext = !master_tcon->unix_ext;
3946 ctx->linux_ext = master_tcon->posix_extensions;
3947 ctx->sectype = master_tcon->ses->sectype;
3948 ctx->sign = master_tcon->ses->sign;
3949 ctx->seal = master_tcon->seal;
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06003950 ctx->witness = master_tcon->use_witness;
Jeff Layton9d002df2010-10-06 19:51:11 -04003951
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003952 rc = cifs_set_vol_auth(ctx, master_tcon->ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003953 if (rc) {
3954 tcon = ERR_PTR(rc);
3955 goto out;
3956 }
Jeff Layton9d002df2010-10-06 19:51:11 -04003957
3958 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303959 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003960 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303961 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003962
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003963 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003964 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00003965 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003966 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04003967 goto out;
3968 }
3969
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003970 tcon = cifs_get_tcon(ses, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003971 if (IS_ERR(tcon)) {
3972 cifs_put_smb_ses(ses);
3973 goto out;
3974 }
3975
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003976 if (cap_unix(ses))
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003977 reset_cifs_unix_caps(0, tcon, NULL, ctx);
Steve Frenchb3266142018-05-20 23:41:10 -05003978
Jeff Layton9d002df2010-10-06 19:51:11 -04003979out:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003980 kfree(ctx->username);
3981 kfree_sensitive(ctx->password);
3982 kfree(ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003983
3984 return tcon;
3985}
3986
Steve French96daf2b2011-05-27 04:34:02 +00003987struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04003988cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
3989{
3990 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
3991}
3992
Jeff Laytonb647c352010-10-28 11:16:44 -04003993/* find and return a tlink with given uid */
3994static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003995tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04003996{
3997 struct rb_node *node = root->rb_node;
3998 struct tcon_link *tlink;
3999
4000 while (node) {
4001 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4002
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004003 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004004 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004005 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004006 node = node->rb_right;
4007 else
4008 return tlink;
4009 }
4010 return NULL;
4011}
4012
4013/* insert a tcon_link into the tree */
4014static void
4015tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4016{
4017 struct rb_node **new = &(root->rb_node), *parent = NULL;
4018 struct tcon_link *tlink;
4019
4020 while (*new) {
4021 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4022 parent = *new;
4023
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004024 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004025 new = &((*new)->rb_left);
4026 else
4027 new = &((*new)->rb_right);
4028 }
4029
4030 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4031 rb_insert_color(&new_tlink->tl_rbnode, root);
4032}
4033
Jeff Layton9d002df2010-10-06 19:51:11 -04004034/*
4035 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4036 * current task.
4037 *
4038 * If the superblock doesn't refer to a multiuser mount, then just return
4039 * the master tcon for the mount.
4040 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304041 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004042 * exists, then check to see if it's pending construction. If it is then wait
4043 * for construction to complete. Once it's no longer pending, check to see if
4044 * it failed and either return an error or retry construction, depending on
4045 * the timeout.
4046 *
4047 * If one doesn't exist then insert a new tcon_link struct into the tree and
4048 * try to construct a new one.
4049 */
4050struct tcon_link *
4051cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4052{
4053 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004054 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004055 struct tcon_link *tlink, *newtlink;
4056
4057 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4058 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4059
4060 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004061 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004062 if (tlink)
4063 cifs_get_tlink(tlink);
4064 spin_unlock(&cifs_sb->tlink_tree_lock);
4065
4066 if (tlink == NULL) {
4067 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4068 if (newtlink == NULL)
4069 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004070 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004071 newtlink->tl_tcon = ERR_PTR(-EACCES);
4072 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4073 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4074 cifs_get_tlink(newtlink);
4075
Jeff Layton9d002df2010-10-06 19:51:11 -04004076 spin_lock(&cifs_sb->tlink_tree_lock);
4077 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004078 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004079 if (tlink) {
4080 cifs_get_tlink(tlink);
4081 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004082 kfree(newtlink);
4083 goto wait_for_construction;
4084 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004085 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004086 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4087 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004088 } else {
4089wait_for_construction:
4090 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004091 TASK_INTERRUPTIBLE);
4092 if (ret) {
4093 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004094 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004095 }
4096
4097 /* if it's good, return it */
4098 if (!IS_ERR(tlink->tl_tcon))
4099 return tlink;
4100
4101 /* return error if we tried this already recently */
4102 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4103 cifs_put_tlink(tlink);
4104 return ERR_PTR(-EACCES);
4105 }
4106
4107 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4108 goto wait_for_construction;
4109 }
4110
4111 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4112 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4113 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4114
4115 if (IS_ERR(tlink->tl_tcon)) {
4116 cifs_put_tlink(tlink);
4117 return ERR_PTR(-EACCES);
4118 }
4119
4120 return tlink;
4121}
Jeff Layton2de970f2010-10-06 19:51:12 -04004122
4123/*
4124 * periodic workqueue job that scans tcon_tree for a superblock and closes
4125 * out tcons.
4126 */
4127static void
4128cifs_prune_tlinks(struct work_struct *work)
4129{
4130 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4131 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004132 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004133 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004134 struct rb_node *tmp;
4135 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004136
Jeff Laytonb647c352010-10-28 11:16:44 -04004137 /*
4138 * Because we drop the spinlock in the loop in order to put the tlink
4139 * it's not guarded against removal of links from the tree. The only
4140 * places that remove entries from the tree are this function and
4141 * umounts. Because this function is non-reentrant and is canceled
4142 * before umount can proceed, this is safe.
4143 */
4144 spin_lock(&cifs_sb->tlink_tree_lock);
4145 node = rb_first(root);
4146 while (node != NULL) {
4147 tmp = node;
4148 node = rb_next(tmp);
4149 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4150
4151 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4152 atomic_read(&tlink->tl_count) != 0 ||
4153 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4154 continue;
4155
4156 cifs_get_tlink(tlink);
4157 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4158 rb_erase(tmp, root);
4159
Jeff Layton2de970f2010-10-06 19:51:12 -04004160 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004161 cifs_put_tlink(tlink);
4162 spin_lock(&cifs_sb->tlink_tree_lock);
4163 }
4164 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004165
Jeff Laytonda472fc2012-03-23 14:40:53 -04004166 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004167 TLINK_IDLE_EXPIRE);
4168}
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004169
4170#ifdef CONFIG_CIFS_DFS_UPCALL
4171int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4172{
4173 int rc;
4174 struct TCP_Server_Info *server = tcon->ses->server;
4175 const struct smb_version_operations *ops = server->ops;
4176 struct dfs_cache_tgt_list tl;
4177 struct dfs_cache_tgt_iterator *it = NULL;
4178 char *tree;
4179 const char *tcp_host;
4180 size_t tcp_host_len;
4181 const char *dfs_host;
4182 size_t dfs_host_len;
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004183 char *share = NULL, *prefix = NULL;
Paulo Alcantara11375a52020-07-21 09:36:43 -03004184 struct dfs_info3_param ref = {0};
4185 bool isroot;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004186
4187 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
4188 if (!tree)
4189 return -ENOMEM;
4190
Paulo Alcantara507345b2021-07-12 12:38:24 -03004191 /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */
4192 if (!tcon->dfs_path || dfs_cache_noreq_find(tcon->dfs_path + 1, &ref, &tl)) {
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004193 if (tcon->ipc) {
4194 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4195 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
4196 } else {
4197 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4198 }
4199 goto out;
4200 }
4201
Paulo Alcantara11375a52020-07-21 09:36:43 -03004202 isroot = ref.server_type == DFS_TYPE_ROOT;
4203 free_dfs_info_param(&ref);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004204
4205 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
4206
4207 for (it = dfs_cache_get_tgt_iterator(&tl); it; it = dfs_cache_get_next_tgt(&tl, it)) {
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004208 bool target_match;
4209
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004210 kfree(share);
4211 kfree(prefix);
Colin Ian Kingc6a80e12020-07-31 18:13:42 +01004212 share = NULL;
4213 prefix = NULL;
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004214
4215 rc = dfs_cache_get_tgt_share(tcon->dfs_path + 1, it, &share, &prefix);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004216 if (rc) {
4217 cifs_dbg(VFS, "%s: failed to parse target share %d\n",
4218 __func__, rc);
4219 continue;
4220 }
4221
4222 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
4223
4224 if (dfs_host_len != tcp_host_len
4225 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
4226 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
4227 dfs_host, (int)tcp_host_len, tcp_host);
4228
4229 rc = match_target_ip(server, dfs_host, dfs_host_len, &target_match);
4230 if (rc) {
4231 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
4232 break;
4233 }
4234
4235 if (!target_match) {
4236 cifs_dbg(FYI, "%s: skipping target\n", __func__);
4237 continue;
4238 }
4239 }
4240
4241 if (tcon->ipc) {
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004242 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", share);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004243 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
4244 } else {
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004245 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004246 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
Paulo Alcantara11375a52020-07-21 09:36:43 -03004247 /* Only handle prefix paths of DFS link targets */
4248 if (!rc && !isroot) {
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004249 rc = update_super_prepath(tcon, prefix);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004250 break;
4251 }
4252 }
4253 if (rc == -EREMOTE)
4254 break;
4255 }
4256
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004257 kfree(share);
4258 kfree(prefix);
4259
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004260 if (!rc) {
4261 if (it)
4262 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1, it);
4263 else
4264 rc = -ENOENT;
4265 }
4266 dfs_cache_free_tgts(&tl);
4267out:
4268 kfree(tree);
4269 return rc;
4270}
4271#else
4272int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4273{
4274 const struct smb_version_operations *ops = tcon->ses->server->ops;
4275
4276 return ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4277}
4278#endif