blob: 18448dbd762a89ce92b6c70e0b55eb54c1d0f74a [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Steve French1080ef72011-02-24 18:07:19 +00004 * Copyright (C) International Business Machines Corp., 2002,2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8#include <linux/fs.h>
9#include <linux/net.h>
10#include <linux/string.h>
Eric Biggersdc920272020-03-08 22:58:20 -070011#include <linux/sched/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010012#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/list.h>
14#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/pagemap.h>
17#include <linux/ctype.h>
18#include <linux/utsname.h>
19#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070020#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070021#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000022#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070023#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040025#include <linux/namei.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020026#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040029#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040030#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050031#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000032#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040033#include <linux/parser.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060034#include <linux/bvec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "cifspdu.h"
36#include "cifsglob.h"
37#include "cifsproto.h"
38#include "cifs_unicode.h"
39#include "cifs_debug.h"
40#include "cifs_fs_sb.h"
41#include "ntlmssp.h"
42#include "nterr.h"
43#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053044#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070045#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070046#include "smbdirect.h"
Paulo Alcantara1c780222018-11-14 16:24:03 -020047#include "dns_resolve.h"
48#ifdef CONFIG_CIFS_DFS_UPCALL
49#include "dfs_cache.h"
50#endif
Ronnie Sahlberg5c6e5aa2020-10-21 10:37:11 +100051#include "fs_context.h"
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +010052#include "cifs_swn.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050055extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jeff Layton2de970f2010-10-06 19:51:12 -040057/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040058#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040059#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040060
Rohith Surabattula8e670f72020-09-18 05:37:28 +000061/* Drop the connection to not overload the server */
62#define NUM_STATUS_IO_TIMEOUT 5
63
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -030064struct mount_ctx {
65 struct cifs_sb_info *cifs_sb;
66 struct smb3_fs_context *fs_ctx;
67 unsigned int xid;
68 struct TCP_Server_Info *server;
69 struct cifs_ses *ses;
70 struct cifs_tcon *tcon;
71#ifdef CONFIG_CIFS_DFS_UPCALL
72 struct cifs_ses *root_ses;
73 uuid_t mount_id;
74 char *origin_fullpath, *leaf_fullpath;
75#endif
76};
77
Pavel Shilovskya9f1b852010-12-13 19:08:35 +030078static int ip_connect(struct TCP_Server_Info *server);
79static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -040080static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -040081static void cifs_prune_tlinks(struct work_struct *work);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -020082
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020083/*
84 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
85 * get their ip addresses changed at some point.
86 *
87 * This should be called with server->srv_mutex held.
88 */
Samuel Cabrero7d6535b2020-11-30 19:02:55 +010089static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020090{
91 int rc;
92 int len;
93 char *unc, *ipaddr = NULL;
Shyam Prasad N506c1da2021-05-18 15:05:50 +000094 time64_t expiry, now;
95 unsigned long ttl = SMB_DNS_RESOLVE_INTERVAL_DEFAULT;
Paulo Alcantara28eb24f2018-11-20 15:16:36 -020096
97 if (!server->hostname)
98 return -EINVAL;
99
100 len = strlen(server->hostname) + 3;
101
102 unc = kmalloc(len, GFP_KERNEL);
103 if (!unc) {
104 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
105 return -ENOMEM;
106 }
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000107 scnprintf(unc, len, "\\\\%s", server->hostname);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200108
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000109 rc = dns_resolve_server_name_to_ip(unc, &ipaddr, &expiry);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200110 kfree(unc);
111
112 if (rc < 0) {
113 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
114 __func__, server->hostname, rc);
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000115 goto requeue_resolve;
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200116 }
117
Ronnie Sahlbergfada37f2020-04-21 12:37:39 +1000118 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200119 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
120 strlen(ipaddr));
Ronnie Sahlbergfada37f2020-04-21 12:37:39 +1000121 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200122 kfree(ipaddr);
123
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000124 /* rc == 1 means success here */
125 if (rc) {
126 now = ktime_get_real_seconds();
127 if (expiry && expiry > now)
128 /*
129 * To make sure we don't use the cached entry, retry 1s
130 * after expiry.
131 */
Paulo Alcantara4ac05362021-11-04 13:13:28 -0300132 ttl = max_t(unsigned long, expiry - now, SMB_DNS_RESOLVE_INTERVAL_MIN) + 1;
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000133 }
134 rc = !rc ? -1 : 0;
135
136requeue_resolve:
137 cifs_dbg(FYI, "%s: next dns resolution scheduled for %lu seconds in the future\n",
138 __func__, ttl);
139 mod_delayed_work(cifsiod_wq, &server->resolve, (ttl * HZ));
140
141 return rc;
142}
143
144
145static void cifs_resolve_server(struct work_struct *work)
146{
147 int rc;
148 struct TCP_Server_Info *server = container_of(work,
149 struct TCP_Server_Info, resolve.work);
150
151 mutex_lock(&server->srv_mutex);
152
153 /*
154 * Resolve the hostname again to make sure that IP address is up-to-date.
155 */
156 rc = reconn_set_ipaddr_from_hostname(server);
157 if (rc) {
158 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
159 __func__, rc);
160 }
161
162 mutex_unlock(&server->srv_mutex);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200163}
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200164
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300165/**
166 * Mark all sessions and tcons for reconnect.
Jeff Laytond5c56052008-12-01 18:42:33 -0500167 *
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300168 * @server needs to be previously set to CifsNeedReconnect.
Jeff Laytond5c56052008-12-01 18:42:33 -0500169 */
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300170static void cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Steve French96daf2b2011-05-27 04:34:02 +0000172 struct cifs_ses *ses;
173 struct cifs_tcon *tcon;
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300174 struct mid_q_entry *mid, *nmid;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400175 struct list_head retry_list;
Shyam Prasad N0f2b3052021-07-19 11:26:24 +0000176 struct TCP_Server_Info *pserver;
Steve French50c2f752007-07-13 00:33:32 +0000177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400179 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Steve French6e4d3bb2018-09-22 11:25:04 -0500181 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800182 trace_smb3_reconnect(server->CurrentMid, server->conn_id, server->hostname);
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300183 /*
184 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
185 * are not used until reconnected.
186 */
187 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n", __func__);
Shyam Prasad N0f2b3052021-07-19 11:26:24 +0000188
189 /* If server is a channel, select the primary channel */
190 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
191
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530192 spin_lock(&cifs_tcp_ses_lock);
Shyam Prasad N0f2b3052021-07-19 11:26:24 +0000193 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
Jeff Layton14fbf502008-11-14 13:53:46 -0500194 ses->need_reconnect = true;
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300195 list_for_each_entry(tcon, &ses->tcon_list, tcon_list)
Jeff Laytonf1987b42008-11-15 11:12:47 -0500196 tcon->need_reconnect = true;
Aurelien Aptelb327a712018-01-24 13:46:10 +0100197 if (ses->tcon_ipc)
198 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530200 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500203 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500204 mutex_lock(&server->srv_mutex);
Long Li1d2a4f52019-05-13 21:01:28 -0700205 if (server->ssocket) {
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300206 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
207 server->ssocket->flags);
Long Li1d2a4f52019-05-13 21:01:28 -0700208 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300209 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
210 server->ssocket->flags);
Long Li1d2a4f52019-05-13 21:01:28 -0700211 sock_release(server->ssocket);
212 server->ssocket = NULL;
213 }
214 server->sequence_number = 0;
215 server->session_estab = false;
216 kfree(server->session_key.response);
217 server->session_key.response = NULL;
218 server->session_key.len = 0;
219 server->lstrp = jiffies;
Long Li214bab42019-04-05 21:36:35 +0000220
221 /* mark submitted MIDs for retry and issue callback */
222 INIT_LIST_HEAD(&retry_list);
223 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
224 spin_lock(&GlobalMid_Lock);
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300225 list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
226 kref_get(&mid->refcount);
227 if (mid->mid_state == MID_REQUEST_SUBMITTED)
228 mid->mid_state = MID_RETRY_NEEDED;
229 list_move(&mid->qhead, &retry_list);
230 mid->mid_flags |= MID_DELETED;
Long Li214bab42019-04-05 21:36:35 +0000231 }
232 spin_unlock(&GlobalMid_Lock);
Long Li1d2a4f52019-05-13 21:01:28 -0700233 mutex_unlock(&server->srv_mutex);
Long Li214bab42019-04-05 21:36:35 +0000234
235 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300236 list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
237 list_del_init(&mid->qhead);
238 mid->callback(mid);
239 cifs_mid_q_entry_release(mid);
Long Li214bab42019-04-05 21:36:35 +0000240 }
241
Long Li1d2a4f52019-05-13 21:01:28 -0700242 if (cifs_rdma_enabled(server)) {
243 mutex_lock(&server->srv_mutex);
Long Li050b8c32019-04-04 11:35:42 -0500244 smbd_destroy(server);
Long Li1d2a4f52019-05-13 21:01:28 -0700245 mutex_unlock(&server->srv_mutex);
246 }
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300247}
248
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300249static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
250{
251 spin_lock(&GlobalMid_Lock);
252 server->nr_targets = num_targets;
253 if (server->tcpStatus == CifsExiting) {
254 /* the demux thread will exit normally next time through the loop */
255 spin_unlock(&GlobalMid_Lock);
256 wake_up(&server->response_q);
257 return false;
258 }
259 server->tcpStatus = CifsNeedReconnect;
260 spin_unlock(&GlobalMid_Lock);
261 return true;
262}
263
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300264/*
265 * cifs tcp session reconnection
266 *
267 * mark tcp session as reconnecting so temporarily locked
268 * mark all smb sessions as reconnecting for tcp session
269 * reconnect tcp session
270 * wake up waiters on reconnection? - (not needed currently)
271 */
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300272static int __cifs_reconnect(struct TCP_Server_Info *server)
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300273{
274 int rc = 0;
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300275
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300276 if (!cifs_tcp_ses_needs_reconnect(server, 1))
277 return 0;
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300278
279 cifs_mark_tcp_ses_conns_for_reconnect(server);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400280
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400281 do {
Steve French6c3d8902006-07-31 22:46:20 +0000282 try_to_freeze();
Jeff Layton73e216a2013-09-05 08:38:10 -0400283 mutex_lock(&server->srv_mutex);
Samuel Cabrero121d9472020-11-30 19:02:56 +0100284
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +0200285 if (!cifs_swn_set_server_dstaddr(server)) {
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300286 /* resolve the hostname again to make sure that IP address is up-to-date */
Shyam Prasad N4e456b32021-03-31 14:35:24 +0000287 rc = reconn_set_ipaddr_from_hostname(server);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300288 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
Samuel Cabrero121d9472020-11-30 19:02:56 +0100289 }
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300290
Long Li781a8052017-11-22 17:38:36 -0700291 if (cifs_rdma_enabled(server))
292 rc = smbd_reconnect(server);
293 else
294 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000295 if (rc) {
Federico Sauter4afe2602015-03-17 17:45:28 +0100296 mutex_unlock(&server->srv_mutex);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300297 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
Steve French0cb766a2005-04-28 22:41:11 -0700298 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 } else {
300 atomic_inc(&tcpSesReconnectCount);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800301 set_credits(server, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000303 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000304 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000305 spin_unlock(&GlobalMid_Lock);
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +0200306 cifs_swn_reset_server_dstaddr(server);
Federico Sauter4afe2602015-03-17 17:45:28 +0100307 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400309 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500310
Sachin Prabhub8c60012016-10-20 19:52:24 -0400311 if (server->tcpStatus == CifsNeedNegotiate)
312 mod_delayed_work(cifsiod_wq, &server->echo, 0);
313
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600314 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return rc;
316}
317
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300318#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300319static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
320{
321 int rc;
322 char *hostname;
323
324 if (!cifs_swn_set_server_dstaddr(server)) {
325 if (server->hostname != target) {
326 hostname = extract_hostname(target);
327 if (!IS_ERR(hostname)) {
328 kfree(server->hostname);
329 server->hostname = hostname;
330 } else {
331 cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
332 __func__, PTR_ERR(hostname));
333 cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
334 server->hostname);
335 }
336 }
337 /* resolve the hostname again to make sure that IP address is up-to-date. */
338 rc = reconn_set_ipaddr_from_hostname(server);
339 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
340 }
341 /* Reconnect the socket */
342 if (cifs_rdma_enabled(server))
343 rc = smbd_reconnect(server);
344 else
345 rc = generic_ip_connect(server);
346
347 return rc;
348}
349
350static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
351 struct dfs_cache_tgt_iterator **target_hint)
352{
353 int rc;
354 struct dfs_cache_tgt_iterator *tit;
355
356 *target_hint = NULL;
357
358 /* If dfs target list is empty, then reconnect to last server */
359 tit = dfs_cache_get_tgt_iterator(tl);
360 if (!tit)
361 return __reconnect_target_unlocked(server, server->hostname);
362
363 /* Otherwise, try every dfs target in @tl */
364 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
365 rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
366 if (!rc) {
367 *target_hint = tit;
368 break;
369 }
370 }
371 return rc;
372}
373
374static int reconnect_dfs_server(struct TCP_Server_Info *server)
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300375{
376 int rc = 0;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300377 const char *refpath = server->current_fullpath + 1;
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300378 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300379 struct dfs_cache_tgt_iterator *target_hint = NULL;
380 int num_targets = 0;
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300381
382 /*
383 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
384 *
385 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
386 * targets (server->nr_targets). It's also possible that the cached referral was cleared
387 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
388 * refreshing the referral, so, in this case, default it to 1.
389 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300390 if (!dfs_cache_noreq_find(refpath, NULL, &tl))
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300391 num_targets = dfs_cache_get_nr_tgts(&tl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300392 if (!num_targets)
393 num_targets = 1;
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300394
395 if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
396 return 0;
397
398 cifs_mark_tcp_ses_conns_for_reconnect(server);
399
400 do {
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300401 try_to_freeze();
402 mutex_lock(&server->srv_mutex);
403
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300404 rc = reconnect_target_unlocked(server, &tl, &target_hint);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300405 if (rc) {
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300406 /* Failed to reconnect socket */
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300407 mutex_unlock(&server->srv_mutex);
408 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
409 msleep(3000);
410 continue;
411 }
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300412 /*
413 * Socket was created. Update tcp session status to CifsNeedNegotiate so that a
414 * process waiting for reconnect will know it needs to re-establish session and tcon
415 * through the reconnected target server.
416 */
417 atomic_inc(&tcpSesReconnectCount);
418 set_credits(server, 1);
419 spin_lock(&GlobalMid_Lock);
420 if (server->tcpStatus != CifsExiting)
421 server->tcpStatus = CifsNeedNegotiate;
422 spin_unlock(&GlobalMid_Lock);
423 cifs_swn_reset_server_dstaddr(server);
424 mutex_unlock(&server->srv_mutex);
425 } while (server->tcpStatus == CifsNeedReconnect);
426
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300427 if (target_hint)
428 dfs_cache_noreq_update_tgthint(refpath, target_hint);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300429
430 dfs_cache_free_tgts(&tl);
431
432 /* Need to set up echo worker again once connection has been established */
433 if (server->tcpStatus == CifsNeedNegotiate)
434 mod_delayed_work(cifsiod_wq, &server->echo, 0);
435
436 wake_up(&server->response_q);
437 return rc;
438}
439
440int cifs_reconnect(struct TCP_Server_Info *server)
441{
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300442 /* If tcp session is not an dfs connection, then reconnect to last target server */
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300443 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300444 if (!server->is_dfs_conn || !server->origin_fullpath || !server->leaf_fullpath) {
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300445 spin_unlock(&cifs_tcp_ses_lock);
446 return __cifs_reconnect(server);
447 }
448 spin_unlock(&cifs_tcp_ses_lock);
449
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300450 return reconnect_dfs_server(server);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300451}
452#else
453int cifs_reconnect(struct TCP_Server_Info *server)
454{
455 return __cifs_reconnect(server);
456}
457#endif
458
Jeff Laytonc74093b2011-01-11 07:24:23 -0500459static void
460cifs_echo_request(struct work_struct *work)
461{
462 int rc;
463 struct TCP_Server_Info *server = container_of(work,
464 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400465
466 /*
467 * We cannot send an echo if it is disabled.
468 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500469 */
Steve French4fcd1812016-06-22 20:12:05 -0500470
471 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400472 server->tcpStatus == CifsExiting ||
473 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400474 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Shyam Prasad N5b2abda2021-05-01 16:17:07 +0000475 time_before(jiffies, server->lstrp + server->echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500476 goto requeue_echo;
477
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400478 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500479 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500480 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
481 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500482
Samuel Cabrero21077c62020-11-30 19:02:54 +0100483 /* Check witness registrations */
484 cifs_swn_check();
Samuel Cabrero21077c62020-11-30 19:02:54 +0100485
Jeff Laytonc74093b2011-01-11 07:24:23 -0500486requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400487 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500488}
489
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400490static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400491allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400492{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400493 if (!server->bigbuf) {
494 server->bigbuf = (char *)cifs_buf_get();
495 if (!server->bigbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000496 cifs_server_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400497 msleep(3000);
498 /* retry will check if exiting */
499 return false;
500 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400501 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400502 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400503 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400504 }
505
Jeff Layton2a37ef92011-10-19 15:29:23 -0400506 if (!server->smallbuf) {
507 server->smallbuf = (char *)cifs_small_buf_get();
508 if (!server->smallbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000509 cifs_server_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400510 msleep(1000);
511 /* retry will check if exiting */
512 return false;
513 }
514 /* beginning of smb buffer is cleared in our buf_get */
515 } else {
516 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400517 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400518 }
519
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400520 return true;
521}
522
Jeff Laytonba749e62011-10-11 06:41:32 -0400523static bool
524server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400525{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300526 /*
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000527 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300528 * situations right:
529 * 1s client sends a normal SMB request
Ronnie Sahlbergbecc2ba2019-07-24 11:43:49 +1000530 * 2s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300531 * 30s echo workqueue job pops, and decides we got a response recently
532 * and don't need to send another
533 * ...
534 * 65s kernel_recvmsg times out, and we see that we haven't gotten
535 * a response in >60s.
536 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200537 if ((server->tcpStatus == CifsGood ||
538 server->tcpStatus == CifsNeedNegotiate) &&
Shyam Prasad Nf4916642021-04-29 07:53:18 +0000539 (!server->ops->can_echo || server->ops->can_echo(server)) &&
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000540 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000541 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
542 (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400543 cifs_reconnect(server);
Jeff Laytonba749e62011-10-11 06:41:32 -0400544 return true;
545 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400546
Jeff Laytonba749e62011-10-11 06:41:32 -0400547 return false;
548}
549
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800550static inline bool
551zero_credits(struct TCP_Server_Info *server)
552{
553 int val;
554
555 spin_lock(&server->req_lock);
556 val = server->credits + server->echo_credits + server->oplock_credits;
557 if (server->in_flight == 0 && val == 0) {
558 spin_unlock(&server->req_lock);
559 return true;
560 }
561 spin_unlock(&server->req_lock);
562 return false;
563}
564
Al Viro71335662016-01-09 19:54:50 -0500565static int
566cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400567{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400568 int length = 0;
569 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400570
Al Viro71335662016-01-09 19:54:50 -0500571 smb_msg->msg_control = NULL;
572 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400573
Al Viro71335662016-01-09 19:54:50 -0500574 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500575 try_to_freeze();
576
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800577 /* reconnect if no credits and no requests in flight */
578 if (zero_credits(server)) {
579 cifs_reconnect(server);
580 return -ECONNABORTED;
581 }
582
Al Viro71335662016-01-09 19:54:50 -0500583 if (server_unresponsive(server))
584 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700585 if (cifs_rdma_enabled(server) && server->smbd_conn)
586 length = smbd_recv(server->smbd_conn, smb_msg);
587 else
588 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500589
590 if (server->tcpStatus == CifsExiting)
591 return -ESHUTDOWN;
592
593 if (server->tcpStatus == CifsNeedReconnect) {
594 cifs_reconnect(server);
595 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400596 }
597
Al Viro71335662016-01-09 19:54:50 -0500598 if (length == -ERESTARTSYS ||
599 length == -EAGAIN ||
600 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400601 /*
602 * Minimum sleep to prevent looping, allowing socket
603 * to clear and app threads to set tcpStatus
604 * CifsNeedReconnect if server hung.
605 */
606 usleep_range(1000, 2000);
607 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400608 continue;
Al Viro71335662016-01-09 19:54:50 -0500609 }
610
611 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500612 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400613 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500614 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400615 }
616 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400617 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400618}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400619
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400620int
621cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
622 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400623{
Al Viro71335662016-01-09 19:54:50 -0500624 struct msghdr smb_msg;
625 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100626 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400627
Al Viro71335662016-01-09 19:54:50 -0500628 return cifs_readv_from_socket(server, &smb_msg);
629}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400630
David Howellscf0604a2021-02-04 00:15:21 -0600631ssize_t
632cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
633{
634 struct msghdr smb_msg;
635
636 /*
637 * iov_iter_discard already sets smb_msg.type and count and iov_offset
638 * and cifs_readv_from_socket sets msg_control and msg_controllen
639 * so little to initialize in struct msghdr
640 */
641 smb_msg.msg_name = NULL;
642 smb_msg.msg_namelen = 0;
643 iov_iter_discard(&smb_msg.msg_iter, READ, to_read);
644
645 return cifs_readv_from_socket(server, &smb_msg);
646}
647
Al Viro71335662016-01-09 19:54:50 -0500648int
649cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700650 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500651{
652 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700653 struct bio_vec bv = {
654 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100655 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500656 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400657}
658
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400659static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400660is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400661{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400662 /*
663 * The first byte big endian of the length field,
664 * is actually not part of the length but the type
665 * with the most common, zero, as regular data.
666 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400667 switch (type) {
668 case RFC1002_SESSION_MESSAGE:
669 /* Regular SMB response */
670 return true;
671 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500672 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400673 break;
674 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500675 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400676 break;
677 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400678 /*
679 * We get this from Windows 98 instead of an error on
680 * SMB negprot response.
681 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500682 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400683 /* give server a second to clean up */
684 msleep(1000);
685 /*
686 * Always try 445 first on reconnect since we get NACK
687 * on some if we ever connected to port 139 (the NACK
688 * is since we do not begin with RFC1001 session
689 * initialize frame).
690 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400691 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400692 cifs_reconnect(server);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400693 break;
694 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000695 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400696 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400697 }
698
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400699 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400700}
701
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400702void
703dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400704{
705#ifdef CONFIG_CIFS_STATS2
706 mid->when_received = jiffies;
707#endif
708 spin_lock(&GlobalMid_Lock);
709 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400710 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400711 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400712 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000713 /*
714 * Trying to handle/dequeue a mid after the send_recv()
715 * function has finished processing it is a bug.
716 */
Steve French9e7ffa72021-11-12 09:55:03 -0600717 if (mid->mid_flags & MID_DELETED) {
718 spin_unlock(&GlobalMid_Lock);
Joe Perchesa0a30362020-04-14 22:42:53 -0700719 pr_warn_once("trying to dequeue a deleted mid\n");
Steve French9e7ffa72021-11-12 09:55:03 -0600720 } else {
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000721 list_del_init(&mid->qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700722 mid->mid_flags |= MID_DELETED;
Steve French9e7ffa72021-11-12 09:55:03 -0600723 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700724 }
Jeff Laytonea1f4502011-10-19 15:29:05 -0400725}
726
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800727static unsigned int
728smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
729{
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900730 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800731
732 /*
733 * SMB1 does not use credits.
734 */
735 if (server->vals->header_preamble_size)
736 return 0;
737
738 return le16_to_cpu(shdr->CreditRequest);
739}
740
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400741static void
742handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400743 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400744{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400745 if (server->ops->check_trans2 &&
746 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400747 return;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800748 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400749 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400750 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400751 /* Was previous buf put in mpx struct for multi-rsp? */
752 if (!mid->multiRsp) {
753 /* smb buffer will be freed by user thread */
754 if (server->large_buf)
755 server->bigbuf = NULL;
756 else
757 server->smallbuf = NULL;
758 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400759 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400760}
761
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400762static void clean_demultiplex_info(struct TCP_Server_Info *server)
763{
764 int length;
765
766 /* take it off the list, if it's not already */
767 spin_lock(&cifs_tcp_ses_lock);
768 list_del_init(&server->tcp_ses_list);
769 spin_unlock(&cifs_tcp_ses_lock);
770
Paulo Alcantara21225332020-11-28 16:54:02 -0300771 cancel_delayed_work_sync(&server->echo);
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000772 cancel_delayed_work_sync(&server->resolve);
Paulo Alcantara21225332020-11-28 16:54:02 -0300773
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400774 spin_lock(&GlobalMid_Lock);
775 server->tcpStatus = CifsExiting;
776 spin_unlock(&GlobalMid_Lock);
777 wake_up_all(&server->response_q);
778
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400779 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300780 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400781 if (server->credits <= 0)
782 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300783 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400784 /*
785 * Although there should not be any requests blocked on this queue it
786 * can not hurt to be paranoid and try to wake up requests that may
787 * haven been blocked when more than 50 at time were on the wire to the
788 * same server - they now will see the session is in exit state and get
789 * out of SendReceive.
790 */
791 wake_up_all(&server->request_q);
792 /* give those requests time to exit */
793 msleep(125);
Long Li050b8c32019-04-04 11:35:42 -0500794 if (cifs_rdma_enabled(server))
795 smbd_destroy(server);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400796 if (server->ssocket) {
797 sock_release(server->ssocket);
798 server->ssocket = NULL;
799 }
800
801 if (!list_empty(&server->pending_mid_q)) {
802 struct list_head dispose_list;
803 struct mid_q_entry *mid_entry;
804 struct list_head *tmp, *tmp2;
805
806 INIT_LIST_HEAD(&dispose_list);
807 spin_lock(&GlobalMid_Lock);
808 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
809 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Paulo Alcantarabf1bc6942021-03-08 12:00:47 -0300810 cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700811 kref_get(&mid_entry->refcount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400812 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400813 list_move(&mid_entry->qhead, &dispose_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700814 mid_entry->mid_flags |= MID_DELETED;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400815 }
816 spin_unlock(&GlobalMid_Lock);
817
818 /* now walk dispose list and issue callbacks */
819 list_for_each_safe(tmp, tmp2, &dispose_list) {
820 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Paulo Alcantarabf1bc6942021-03-08 12:00:47 -0300821 cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400822 list_del_init(&mid_entry->qhead);
823 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700824 cifs_mid_q_entry_release(mid_entry);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400825 }
826 /* 1/8th of sec is more than enough time for them to exit */
827 msleep(125);
828 }
829
830 if (!list_empty(&server->pending_mid_q)) {
831 /*
832 * mpx threads have not exited yet give them at least the smb
833 * send timeout time for long ops.
834 *
835 * Due to delays on oplock break requests, we need to wait at
836 * least 45 seconds before giving up on a request getting a
837 * response and going ahead and killing cifsd.
838 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500839 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400840 msleep(46000);
841 /*
842 * If threads still have not exited they are probably never
843 * coming home not much else we can do but free the memory.
844 */
845 }
846
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300847#ifdef CONFIG_CIFS_DFS_UPCALL
848 kfree(server->origin_fullpath);
849 kfree(server->leaf_fullpath);
850#endif
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400851 kfree(server);
852
853 length = atomic_dec_return(&tcpSesAllocCount);
854 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700855 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400856}
857
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400858static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400859standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
860{
861 int length;
862 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000863 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400864
865 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100866 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
867 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000868 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400869 cifs_reconnect(server);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400870 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400871 }
872
873 /* switch to large buffer if too big for a small one */
874 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
875 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400876 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400877 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400878 }
879
880 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400881 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100882 pdu_length - HEADER_SIZE(server) + 1
883 + server->vals->header_preamble_size);
884
Jeff Laytone9097ab2011-10-19 15:29:40 -0400885 if (length < 0)
886 return length;
887 server->total_read += length;
888
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400889 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400890
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800891 return cifs_handle_standard(server, mid);
892}
893
894int
895cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
896{
897 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
898 int length;
899
Jeff Laytone9097ab2011-10-19 15:29:40 -0400900 /*
901 * We know that we received enough to get to the MID as we
902 * checked the pdu_length earlier. Now check to see
903 * if the rest of the header is OK. We borrow the length
904 * var for the rest of the loop to avoid a new stack var.
905 *
906 * 48 bytes is enough to display the header and a little bit
907 * into the payload for debugging purposes.
908 */
Steve French373512e2015-12-18 13:05:30 -0600909 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400910 if (length != 0)
911 cifs_dump_mem("Bad SMB: ", buf,
912 min_t(unsigned int, server->total_read, 48));
913
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700914 if (server->ops->is_session_expired &&
915 server->ops->is_session_expired(buf)) {
916 cifs_reconnect(server);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700917 return -1;
918 }
919
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700920 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -0800921 server->ops->is_status_pending(buf, server))
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700922 return -1;
923
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500924 if (!mid)
925 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400926
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400927 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500928 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400929}
930
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000931static void
932smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
933{
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900934 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800935 int scredits, in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000936
937 /*
938 * SMB1 does not use credits.
939 */
940 if (server->vals->header_preamble_size)
941 return;
942
943 if (shdr->CreditRequest) {
944 spin_lock(&server->req_lock);
945 server->credits += le16_to_cpu(shdr->CreditRequest);
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800946 scredits = server->credits;
947 in_flight = server->in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000948 spin_unlock(&server->req_lock);
949 wake_up(&server->request_q);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800950
951 trace_smb3_add_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800952 server->conn_id, server->hostname, scredits,
953 le16_to_cpu(shdr->CreditRequest), in_flight);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800954 cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
955 __func__, le16_to_cpu(shdr->CreditRequest),
956 scredits);
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000957 }
958}
959
960
Jeff Laytone9097ab2011-10-19 15:29:40 -0400961static int
Al Viro7c97c202011-06-21 08:51:28 -0400962cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000964 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400965 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400966 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000967 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400968 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500969 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000970 struct mid_q_entry *mids[MAX_COMPOUND];
971 char *bufs[MAX_COMPOUND];
Rohith Surabattula8e670f72020-09-18 05:37:28 +0000972 unsigned int noreclaim_flag, num_io_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Eric Biggersdc920272020-03-08 22:58:20 -0700974 noreclaim_flag = memalloc_noreclaim_save();
Joe Perchesf96637b2013-05-04 22:12:25 -0500975 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400976
977 length = atomic_inc_return(&tcpSesAllocCount);
978 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700979 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700981 set_freezable();
Eric W. Biederman33da8e72019-08-16 12:33:54 -0500982 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +0000983 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700984 if (try_to_freeze())
985 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700986
Jeff Layton2a37ef92011-10-19 15:29:23 -0400987 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400988 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700989
Jeff Layton2a37ef92011-10-19 15:29:23 -0400990 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400991 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000992 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000993
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400994 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400995 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000996 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000997
998 if (server->vals->header_preamble_size == 0)
999 server->total_read = 0;
1000 else
1001 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -07001002
Pavel Shilovsky98bac622011-08-01 13:19:42 +04001003 /*
1004 * The right amount was read from socket - 4 bytes,
1005 * so we can now interpret the length field.
1006 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001007 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -07001008
Joe Perchesf96637b2013-05-04 22:12:25 -05001009 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -04001010 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001011 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001012next_pdu:
1013 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -07001014
Jeff Layton89482a52011-10-19 15:28:57 -04001015 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001016 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001017 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001018 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001019 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001020 cifs_reconnect(server);
Jeff Layton89482a52011-10-19 15:28:57 -04001021 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -07001022 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001023
Jeff Layton89482a52011-10-19 15:28:57 -04001024 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001025 length = cifs_read_from_socket(server,
1026 buf + server->vals->header_preamble_size,
1027 HEADER_SIZE(server) - 1
1028 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001029 if (length < 0)
1030 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001031 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001032
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001033 if (server->ops->next_header) {
1034 next_offset = server->ops->next_header(buf);
1035 if (next_offset)
1036 server->pdu_size = next_offset;
1037 }
1038
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001039 memset(mids, 0, sizeof(mids));
1040 memset(bufs, 0, sizeof(bufs));
1041 num_mids = 0;
1042
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001043 if (server->ops->is_transform_hdr &&
1044 server->ops->receive_transform &&
1045 server->ops->is_transform_hdr(buf)) {
1046 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001047 mids,
1048 bufs,
1049 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001050 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001051 mids[0] = server->ops->find_mid(server, buf);
1052 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001053 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001054
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001055 if (!mids[0] || !mids[0]->receive)
1056 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001057 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001058 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001059 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001060
Lars Persson696e4202018-06-25 14:05:25 +02001061 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001062 for (i = 0; i < num_mids; i++)
1063 if (mids[i])
1064 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001065 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001066 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001067
Rohith Surabattula8e670f72020-09-18 05:37:28 +00001068 if (server->ops->is_status_io_timeout &&
1069 server->ops->is_status_io_timeout(buf)) {
1070 num_io_timeout++;
1071 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
1072 cifs_reconnect(server);
1073 num_io_timeout = 0;
1074 continue;
1075 }
1076 }
1077
Steve Frenchfda35942011-01-20 18:06:34 +00001078 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001079
1080 for (i = 0; i < num_mids; i++) {
1081 if (mids[i] != NULL) {
1082 mids[i]->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001083
Rohith Surabattula9e550b02021-02-16 10:40:45 +00001084 if (bufs[i] && server->ops->is_network_name_deleted)
1085 server->ops->is_network_name_deleted(bufs[i],
1086 server);
1087
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001088 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1089 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001090
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001091 cifs_mid_q_entry_release(mids[i]);
1092 } else if (server->ops->is_oplock_break &&
1093 server->ops->is_oplock_break(bufs[i],
1094 server)) {
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001095 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001096 cifs_dbg(FYI, "Received oplock break\n");
1097 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07001098 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1099 atomic_read(&midCount));
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001100 cifs_dump_mem("Received Data is: ", bufs[i],
1101 HEADER_SIZE(server));
Ronnie Sahlberg3e272572019-07-06 06:43:08 +10001102 smb2_add_credits_from_hdr(bufs[i], server);
Steve French39798772006-05-31 22:40:51 +00001103#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001104 if (server->ops->dump_detail)
1105 server->ops->dump_detail(bufs[i],
1106 server);
1107 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001108#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001109 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001110 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001111
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001112 if (pdu_length > server->pdu_size) {
1113 if (!allocate_buffers(server))
1114 continue;
1115 pdu_length -= server->pdu_size;
1116 server->total_read = 0;
1117 server->large_buf = false;
1118 buf = server->smallbuf;
1119 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001120 }
1121 } /* end while !EXITING */
1122
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001123 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001124 cifs_buf_release(server->bigbuf);
1125 if (server->smallbuf) /* no sense logging a debug message if NULL */
1126 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001128 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001129 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001130
1131 /* if server->tsk was NULL then wait for a signal before exiting */
1132 if (!task_to_wake) {
1133 set_current_state(TASK_INTERRUPTIBLE);
1134 while (!signal_pending(current)) {
1135 schedule();
1136 set_current_state(TASK_INTERRUPTIBLE);
1137 }
1138 set_current_state(TASK_RUNNING);
1139 }
1140
Eric Biggersdc920272020-03-08 22:58:20 -07001141 memalloc_noreclaim_restore(noreclaim_flag);
Jeff Layton0468a2c2008-12-01 07:09:35 -05001142 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Steve French4c51de12021-09-13 18:29:46 -05001145/*
Steve Frenchbc044992020-12-11 19:48:26 -06001146 * Returns true if srcaddr isn't specified and rhs isn't specified, or
1147 * if srcaddr is specified and matches the IP address of the rhs argument
Ben Greear3eb9a882010-09-01 17:06:02 -07001148 */
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001149bool
1150cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
Ben Greear3eb9a882010-09-01 17:06:02 -07001151{
1152 switch (srcaddr->sa_family) {
1153 case AF_UNSPEC:
1154 return (rhs->sa_family == AF_UNSPEC);
1155 case AF_INET: {
1156 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1157 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1158 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1159 }
1160 case AF_INET6: {
1161 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05001162 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07001163 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1164 }
1165 default:
1166 WARN_ON(1);
1167 return false; /* don't expect to be here */
1168 }
1169}
1170
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001171/*
1172 * If no port is specified in addr structure, we try to match with 445 port
1173 * and if it fails - with 139 ports. It should be called only if address
1174 * families of server and addr are equal.
1175 */
1176static bool
1177match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1178{
Steve French6da97912011-03-13 18:55:55 +00001179 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001180
Long Li3b249112019-05-15 14:09:04 -07001181 /* SMBDirect manages its own ports, don't match it here */
1182 if (server->rdma)
1183 return true;
1184
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001185 switch (addr->sa_family) {
1186 case AF_INET:
1187 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1188 port = ((struct sockaddr_in *) addr)->sin_port;
1189 break;
1190 case AF_INET6:
1191 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1192 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1193 break;
1194 default:
1195 WARN_ON(1);
1196 return false;
1197 }
1198
1199 if (!port) {
1200 port = htons(CIFS_PORT);
1201 if (port == *sport)
1202 return true;
1203
1204 port = htons(RFC1001_PORT);
1205 }
1206
1207 return port == *sport;
1208}
Ben Greear3eb9a882010-09-01 17:06:02 -07001209
1210static bool
1211match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1212 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Jeff Layton45151482010-07-06 20:43:02 -04001214 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001215 case AF_INET: {
1216 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1217 struct sockaddr_in *srv_addr4 =
1218 (struct sockaddr_in *)&server->dstaddr;
1219
1220 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04001221 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001222 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001223 }
1224 case AF_INET6: {
1225 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1226 struct sockaddr_in6 *srv_addr6 =
1227 (struct sockaddr_in6 *)&server->dstaddr;
1228
Jeff Layton45151482010-07-06 20:43:02 -04001229 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001230 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04001231 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001232 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04001233 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001234 break;
1235 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001236 default:
1237 WARN_ON(1);
1238 return false; /* don't expect to be here */
1239 }
Jeff Layton45151482010-07-06 20:43:02 -04001240
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001241 if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
Ben Greear3eb9a882010-09-01 17:06:02 -07001242 return false;
1243
Jeff Layton45151482010-07-06 20:43:02 -04001244 return true;
1245}
1246
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001247static bool
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001248match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001249{
Jeff Layton3f618222013-06-12 19:52:14 -05001250 /*
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001251 * The select_sectype function should either return the ctx->sectype
Jeff Layton3f618222013-06-12 19:52:14 -05001252 * that was specified, or "Unspecified" if that sectype was not
1253 * compatible with the given NEGOTIATE request.
1254 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001255 if (server->ops->select_sectype(server, ctx->sectype)
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301256 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001257 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001258
Jeff Layton3f618222013-06-12 19:52:14 -05001259 /*
1260 * Now check if signing mode is acceptable. No need to check
1261 * global_secflags at this point since if MUST_SIGN is set then
1262 * the server->sign had better be too.
1263 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001264 if (ctx->sign && !server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04001265 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001266
1267 return true;
1268}
1269
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001270static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001271{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001272 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
Jeff Layton9fa114f2012-11-26 11:09:57 -05001273
Shyam Prasad Nb9ad6b52021-11-21 16:45:44 +00001274 if (ctx->nosharesock)
Shyam Prasad Nc9f1c192021-11-06 11:31:53 +00001275 return 0;
Shyam Prasad Nc9f1c192021-11-06 11:31:53 +00001276
1277 /* this server does not share socket */
1278 if (server->nosharesock)
Jeff Laytona0b3df52013-05-24 07:40:59 -04001279 return 0;
1280
Steve French43cdae82019-06-13 14:26:49 -05001281 /* If multidialect negotiation see if existing sessions match one */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001282 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
Steve French43cdae82019-06-13 14:26:49 -05001283 if (server->vals->protocol_id < SMB30_PROT_ID)
1284 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001285 } else if (strcmp(ctx->vals->version_string,
Steve French43cdae82019-06-13 14:26:49 -05001286 SMBDEFAULT_VERSION_STRING) == 0) {
1287 if (server->vals->protocol_id < SMB21_PROT_ID)
1288 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001289 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
Jeff Layton23db65f2012-05-15 12:20:51 -04001290 return 0;
1291
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001292 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1293 return 0;
1294
Shyam Prasad N7be32482021-10-14 11:52:39 +00001295 if (strcasecmp(server->hostname, ctx->server_hostname))
1296 return 0;
1297
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001298 if (!match_address(server, addr,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001299 (struct sockaddr *)&ctx->srcaddr))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001300 return 0;
1301
1302 if (!match_port(server, addr))
1303 return 0;
1304
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001305 if (!match_security(server, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001306 return 0;
1307
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001308 if (server->echo_interval != ctx->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001309 return 0;
1310
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001311 if (server->rdma != ctx->rdma)
Long Li8339dd32017-11-07 01:54:55 -07001312 return 0;
1313
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001314 if (server->ignore_signature != ctx->ignore_signature)
Steve French4f5c10f2019-09-03 21:18:49 -05001315 return 0;
1316
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001317 if (server->min_offload != ctx->min_offload)
Steve French563317e2019-09-08 23:22:02 -05001318 return 0;
1319
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001320 return 1;
1321}
1322
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001323struct TCP_Server_Info *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001324cifs_find_tcp_session(struct smb3_fs_context *ctx)
Jeff Layton45151482010-07-06 20:43:02 -04001325{
Jeff Laytone7ddee92008-11-14 13:44:38 -05001326 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301328 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04001329 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Paulo Alcantaracdc33632021-07-15 21:53:53 -03001330#ifdef CONFIG_CIFS_DFS_UPCALL
1331 /*
1332 * DFS failover implementation in cifs_reconnect() requires unique tcp sessions for
1333 * DFS connections to do failover properly, so avoid sharing them with regular
1334 * shares or even links that may connect to same server but having completely
1335 * different failover targets.
1336 */
1337 if (server->is_dfs_conn)
1338 continue;
1339#endif
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -03001340 /*
1341 * Skip ses channels since they're only handled in lower layers
1342 * (e.g. cifs_send_recv).
1343 */
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001344 if (CIFS_SERVER_IS_CHAN(server) || !match_server(server, ctx))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001345 continue;
1346
Jeff Laytone7ddee92008-11-14 13:44:38 -05001347 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301348 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05001349 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05001350 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301352 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return NULL;
1354}
1355
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001356void
1357cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001359 struct task_struct *task;
1360
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301361 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001362 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301363 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001364 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Steve Frenchdea570e02008-05-06 22:05:51 +00001366
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05001367 /* srv_count can never go negative */
1368 WARN_ON(server->srv_count < 0);
1369
Rob Landleyf1d0c992011-01-22 15:44:05 -06001370 put_net(cifs_net_ns(server));
1371
Jeff Laytone7ddee92008-11-14 13:44:38 -05001372 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301373 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001374
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001375 /* For secondary channels, we pick up ref-count on the primary server */
1376 if (CIFS_SERVER_IS_CHAN(server))
1377 cifs_put_tcp_session(server->primary_server, from_reconnect);
1378
Jeff Laytonc74093b2011-01-11 07:24:23 -05001379 cancel_delayed_work_sync(&server->echo);
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001380 cancel_delayed_work_sync(&server->resolve);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001381
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001382 if (from_reconnect)
1383 /*
1384 * Avoid deadlock here: reconnect work calls
1385 * cifs_put_tcp_session() at its end. Need to be sure
1386 * that reconnect work does nothing with server pointer after
1387 * that step.
1388 */
1389 cancel_delayed_work(&server->reconnect);
1390 else
1391 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001392
Jeff Laytone7ddee92008-11-14 13:44:38 -05001393 spin_lock(&GlobalMid_Lock);
1394 server->tcpStatus = CifsExiting;
1395 spin_unlock(&GlobalMid_Lock);
1396
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001397 cifs_crypto_secmech_release(server);
Shyam Prasad N46bb1b92021-07-19 13:59:14 +00001398
1399 /* fscache server cookies are based on primary channel only */
1400 if (!CIFS_SERVER_IS_CHAN(server))
1401 cifs_fscache_release_client_cookie(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05301402
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05001403 kfree(server->session_key.response);
1404 server->session_key.response = NULL;
1405 server->session_key.len = 0;
Shyam Prasad N7be32482021-10-14 11:52:39 +00001406 kfree(server->hostname);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001407
1408 task = xchg(&server->tsk, NULL);
1409 if (task)
Eric W. Biederman72abe3b2019-05-15 12:33:50 -05001410 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001413struct TCP_Server_Info *
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001414cifs_get_tcp_session(struct smb3_fs_context *ctx,
1415 struct TCP_Server_Info *primary_server)
Jeff Layton63c038c2008-12-01 18:41:46 -05001416{
1417 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05001418 int rc;
1419
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001420 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05001421
1422 /* see if we already have a matching tcp_ses */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001423 tcp_ses = cifs_find_tcp_session(ctx);
Jeff Layton63c038c2008-12-01 18:41:46 -05001424 if (tcp_ses)
1425 return tcp_ses;
1426
1427 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1428 if (!tcp_ses) {
1429 rc = -ENOMEM;
1430 goto out_err;
1431 }
1432
Shyam Prasad N7be32482021-10-14 11:52:39 +00001433 tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
1434 if (!tcp_ses->hostname) {
1435 rc = -ENOMEM;
1436 goto out_err;
1437 }
1438
Shyam Prasad Nb9ad6b52021-11-21 16:45:44 +00001439 if (ctx->nosharesock)
1440 tcp_ses->nosharesock = true;
1441
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001442 tcp_ses->ops = ctx->ops;
1443 tcp_ses->vals = ctx->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06001444 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05001445
Shyam Prasad N6d82c272021-02-03 23:20:46 -08001446 tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001447 tcp_ses->noblockcnt = ctx->rootfs;
1448 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1449 tcp_ses->noautotune = ctx->noautotune;
1450 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1451 tcp_ses->rdma = ctx->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03001452 tcp_ses->in_flight = 0;
Steve French1b63f182019-09-09 22:57:11 -05001453 tcp_ses->max_in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04001454 tcp_ses->credits = 1;
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001455 if (primary_server) {
Steve French446e2142021-11-15 18:02:27 -06001456 spin_lock(&cifs_tcp_ses_lock);
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001457 ++primary_server->srv_count;
1458 tcp_ses->primary_server = primary_server;
Steve French446e2142021-11-15 18:02:27 -06001459 spin_unlock(&cifs_tcp_ses_lock);
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001460 }
Jeff Layton63c038c2008-12-01 18:41:46 -05001461 init_waitqueue_head(&tcp_ses->response_q);
1462 init_waitqueue_head(&tcp_ses->request_q);
1463 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1464 mutex_init(&tcp_ses->srv_mutex);
1465 memcpy(tcp_ses->workstation_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001466 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Jeff Layton63c038c2008-12-01 18:41:46 -05001467 memcpy(tcp_ses->server_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001468 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05001469 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05001470 tcp_ses->sequence_number = 0;
Pavel Shilovsky5b964852019-01-18 11:30:26 -08001471 tcp_ses->reconnect_instance = 1;
Steve Frenchfda35942011-01-20 18:06:34 +00001472 tcp_ses->lstrp = jiffies;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001473 tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
Jeff Layton58fa0152012-05-01 17:41:16 -04001474 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001475 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1476 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001477 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001478 INIT_DELAYED_WORK(&tcp_ses->resolve, cifs_resolve_server);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001479 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1480 mutex_init(&tcp_ses->reconnect_mutex);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03001481#ifdef CONFIG_CIFS_DFS_UPCALL
1482 mutex_init(&tcp_ses->refpath_lock);
1483#endif
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001484 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001485 sizeof(tcp_ses->srcaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001486 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001487 sizeof(tcp_ses->dstaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001488 if (ctx->use_client_guid)
1489 memcpy(tcp_ses->client_guid, ctx->client_guid,
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001490 SMB2_CLIENT_GUID_SIZE);
1491 else
1492 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05001493 /*
1494 * at this point we are the only ones with the pointer
1495 * to the struct since the kernel thread not created yet
1496 * no need to spinlock this init of tcpStatus or srv_count
1497 */
1498 tcp_ses->tcpStatus = CifsNew;
1499 ++tcp_ses->srv_count;
1500
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001501 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1502 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1503 tcp_ses->echo_interval = ctx->echo_interval * HZ;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001504 else
1505 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07001506 if (tcp_ses->rdma) {
1507#ifndef CONFIG_CIFS_SMB_DIRECT
1508 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1509 rc = -ENOENT;
1510 goto out_err_crypto_release;
1511#endif
1512 tcp_ses->smbd_conn = smbd_get_connection(
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001513 tcp_ses, (struct sockaddr *)&ctx->dstaddr);
Long Li2f894642017-11-22 17:38:34 -07001514 if (tcp_ses->smbd_conn) {
1515 cifs_dbg(VFS, "RDMA transport established\n");
1516 rc = 0;
1517 goto smbd_connected;
1518 } else {
1519 rc = -ENOENT;
1520 goto out_err_crypto_release;
1521 }
1522 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001523 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05001524 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001525 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001526 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001527 }
Long Li2f894642017-11-22 17:38:34 -07001528smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05001529 /*
1530 * since we're in a cifs function already, we know that
1531 * this will succeed. No need for try_module_get().
1532 */
1533 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04001534 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05001535 tcp_ses, "cifsd");
1536 if (IS_ERR(tcp_ses->tsk)) {
1537 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05001538 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05001539 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001540 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001541 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001542 tcp_ses->min_offload = ctx->min_offload;
Steve French01cf3082021-07-01 12:22:47 -05001543 /*
1544 * at this point we are the only ones with the pointer
1545 * to the struct since the kernel thread not created yet
1546 * no need to spinlock this update of tcpStatus
1547 */
Steve Frenchfd88ce92011-04-12 01:01:14 +00001548 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05001549
Aurelien Aptela249cc82021-03-04 17:42:21 +00001550 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
1551 tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
1552 else
1553 tcp_ses->max_credits = ctx->max_credits;
1554
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02001555 tcp_ses->nr_targets = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001556 tcp_ses->ignore_signature = ctx->ignore_signature;
Jeff Layton63c038c2008-12-01 18:41:46 -05001557 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301558 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001559 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301560 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001561
Shyam Prasad N46bb1b92021-07-19 13:59:14 +00001562 /* fscache server cookies are based on primary channel only */
1563 if (!CIFS_SERVER_IS_CHAN(tcp_ses))
1564 cifs_fscache_get_client_cookie(tcp_ses);
Shyam Prasad N2adc8202021-12-02 07:30:00 +00001565#ifdef CONFIG_CIFS_FSCACHE
1566 else
1567 tcp_ses->fscache = tcp_ses->primary_server->fscache;
1568#endif /* CONFIG_CIFS_FSCACHE */
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05301569
Jeff Laytonc74093b2011-01-11 07:24:23 -05001570 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001571 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001572
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001573 /* queue dns resolution delayed work */
1574 cifs_dbg(FYI, "%s: next dns resolution scheduled for %d seconds in the future\n",
1575 __func__, SMB_DNS_RESOLVE_INTERVAL_DEFAULT);
1576
1577 queue_delayed_work(cifsiod_wq, &tcp_ses->resolve, (SMB_DNS_RESOLVE_INTERVAL_DEFAULT * HZ));
1578
Jeff Layton63c038c2008-12-01 18:41:46 -05001579 return tcp_ses;
1580
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001581out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001582 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05001583
Rob Landleyf1d0c992011-01-22 15:44:05 -06001584 put_net(cifs_net_ns(tcp_ses));
1585
Jeff Layton63c038c2008-12-01 18:41:46 -05001586out_err:
1587 if (tcp_ses) {
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00001588 if (CIFS_SERVER_IS_CHAN(tcp_ses))
1589 cifs_put_tcp_session(tcp_ses->primary_server, false);
Shyam Prasad N7be32482021-10-14 11:52:39 +00001590 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05001591 if (tcp_ses->ssocket)
1592 sock_release(tcp_ses->ssocket);
1593 kfree(tcp_ses);
1594 }
1595 return ERR_PTR(rc);
1596}
1597
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001598static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001599{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001600 if (ctx->sectype != Unspecified &&
1601 ctx->sectype != ses->sectype)
Jeff Layton3f618222013-06-12 19:52:14 -05001602 return 0;
1603
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001604 /*
1605 * If an existing session is limited to less channels than
1606 * requested, it should not be reused
1607 */
Shyam Prasad N724244c2021-07-19 10:54:46 +00001608 spin_lock(&ses->chan_lock);
1609 if (ses->chan_max < ctx->max_channels) {
1610 spin_unlock(&ses->chan_lock);
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001611 return 0;
Shyam Prasad N724244c2021-07-19 10:54:46 +00001612 }
1613 spin_unlock(&ses->chan_lock);
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001614
Jeff Layton3f618222013-06-12 19:52:14 -05001615 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001616 case Kerberos:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001617 if (!uid_eq(ctx->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001618 return 0;
1619 break;
1620 default:
Jeff Layton04febab2012-01-17 16:09:15 -05001621 /* NULL username means anonymous session */
1622 if (ses->user_name == NULL) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001623 if (!ctx->nullauth)
Jeff Layton04febab2012-01-17 16:09:15 -05001624 return 0;
1625 break;
1626 }
1627
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001628 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05001629 if (strncmp(ses->user_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001630 ctx->username ? ctx->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001631 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001632 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001633 if ((ctx->username && strlen(ctx->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001634 ses->password != NULL &&
1635 strncmp(ses->password,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001636 ctx->password ? ctx->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001637 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001638 return 0;
1639 }
1640 return 1;
1641}
1642
Aurelien Aptelb327a712018-01-24 13:46:10 +01001643/**
1644 * cifs_setup_ipc - helper to setup the IPC tcon for the session
Steve French4c51de12021-09-13 18:29:46 -05001645 * @ses: smb session to issue the request on
1646 * @ctx: the superblock configuration context to use for building the
1647 * new tree connection for the IPC (interprocess communication RPC)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001648 *
1649 * A new IPC connection is made and stored in the session
1650 * tcon_ipc. The IPC tcon has the same lifetime as the session.
1651 */
1652static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001653cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001654{
1655 int rc = 0, xid;
1656 struct cifs_tcon *tcon;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001657 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1658 bool seal = false;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001659 struct TCP_Server_Info *server = ses->server;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001660
1661 /*
1662 * If the mount request that resulted in the creation of the
1663 * session requires encryption, force IPC to be encrypted too.
1664 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001665 if (ctx->seal) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001666 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001667 seal = true;
1668 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001669 cifs_server_dbg(VFS,
Aurelien Aptelb327a712018-01-24 13:46:10 +01001670 "IPC: server doesn't support encryption\n");
1671 return -EOPNOTSUPP;
1672 }
1673 }
1674
1675 tcon = tconInfoAlloc();
1676 if (tcon == NULL)
1677 return -ENOMEM;
1678
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001679 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001680
Aurelien Aptelb327a712018-01-24 13:46:10 +01001681 xid = get_xid();
1682 tcon->ses = ses;
1683 tcon->ipc = true;
1684 tcon->seal = seal;
Ronnie Sahlberg6fd4ea82020-12-14 16:40:21 +10001685 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001686 free_xid(xid);
1687
1688 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001689 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001690 tconInfoFree(tcon);
1691 goto out;
1692 }
1693
1694 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
1695
1696 ses->tcon_ipc = tcon;
1697out:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001698 return rc;
1699}
1700
1701/**
1702 * cifs_free_ipc - helper to release the session IPC tcon
Steve French4c51de12021-09-13 18:29:46 -05001703 * @ses: smb session to unmount the IPC from
Aurelien Aptelb327a712018-01-24 13:46:10 +01001704 *
Paulo Alcantaraf3191fc2021-06-04 19:25:27 -03001705 * Needs to be called everytime a session is destroyed.
1706 *
1707 * On session close, the IPC is closed and the server must release all tcons of the session.
1708 * No need to send a tree disconnect here.
1709 *
1710 * Besides, it will make the server to not close durable and resilient files on session close, as
1711 * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
Aurelien Aptelb327a712018-01-24 13:46:10 +01001712 */
1713static int
1714cifs_free_ipc(struct cifs_ses *ses)
1715{
Aurelien Aptelb327a712018-01-24 13:46:10 +01001716 struct cifs_tcon *tcon = ses->tcon_ipc;
1717
1718 if (tcon == NULL)
1719 return 0;
1720
Aurelien Aptelb327a712018-01-24 13:46:10 +01001721 tconInfoFree(tcon);
1722 ses->tcon_ipc = NULL;
Paulo Alcantaraf3191fc2021-06-04 19:25:27 -03001723 return 0;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001724}
1725
Steve French96daf2b2011-05-27 04:34:02 +00001726static struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001727cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Steve French96daf2b2011-05-27 04:34:02 +00001729 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301731 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04001732 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001733 if (ses->status == CifsExiting)
1734 continue;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001735 if (!match_session(ses, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001736 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05001737 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301738 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001739 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301741 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return NULL;
1743}
1744
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001745void cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05001746{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001747 unsigned int rc, xid;
Shyam Prasad N724244c2021-07-19 10:54:46 +00001748 unsigned int chan_count;
Jeff Layton14fbf502008-11-14 13:53:46 -05001749 struct TCP_Server_Info *server = ses->server;
Joe Perchesf96637b2013-05-04 22:12:25 -05001750 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001751
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301752 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001753 if (ses->status == CifsExiting) {
1754 spin_unlock(&cifs_tcp_ses_lock);
1755 return;
1756 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001757
1758 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1759 cifs_dbg(FYI, "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->treeName : "NONE");
1760
Jeff Layton14fbf502008-11-14 13:53:46 -05001761 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301762 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001763 return;
1764 }
Steve French0060a4f2021-06-24 15:28:04 -05001765 spin_unlock(&cifs_tcp_ses_lock);
1766
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05001767 /* ses_count can never go negative */
1768 WARN_ON(ses->ses_count < 0);
1769
Steve French0060a4f2021-06-24 15:28:04 -05001770 spin_lock(&GlobalMid_Lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001771 if (ses->status == CifsGood)
1772 ses->status = CifsExiting;
Steve French0060a4f2021-06-24 15:28:04 -05001773 spin_unlock(&GlobalMid_Lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001774
Aurelien Aptelb327a712018-01-24 13:46:10 +01001775 cifs_free_ipc(ses);
1776
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001777 if (ses->status == CifsExiting && server->ops->logoff) {
1778 xid = get_xid();
1779 rc = server->ops->logoff(xid, ses);
1780 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001781 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001782 __func__, rc);
1783 _free_xid(xid);
1784 }
1785
1786 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001787 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301788 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001789
Shyam Prasad N724244c2021-07-19 10:54:46 +00001790 spin_lock(&ses->chan_lock);
1791 chan_count = ses->chan_count;
1792 spin_unlock(&ses->chan_lock);
1793
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001794 /* close any extra channels */
Shyam Prasad N724244c2021-07-19 10:54:46 +00001795 if (chan_count > 1) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001796 int i;
1797
Shyam Prasad N724244c2021-07-19 10:54:46 +00001798 for (i = 1; i < chan_count; i++) {
1799 /*
1800 * note: for now, we're okay accessing ses->chans
1801 * without chan_lock. But when chans can go away, we'll
1802 * need to introduce ref counting to make sure that chan
1803 * is not freed from under us.
1804 */
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001805 cifs_put_tcp_session(ses->chans[i].server, 0);
Shyam Prasad N724244c2021-07-19 10:54:46 +00001806 ses->chans[i].server = NULL;
1807 }
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001808 }
1809
Jeff Layton14fbf502008-11-14 13:53:46 -05001810 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001811 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05001812}
1813
Jeff Layton8a8798a2012-01-17 16:09:15 -05001814#ifdef CONFIG_KEYS
1815
Chen Gang057d6332013-07-19 09:01:36 +08001816/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
1817#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001818
1819/* Populate username and pw fields from keyring if possible */
1820static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001821cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001822{
1823 int rc = 0;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001824 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01001825 const char *delim, *payload;
1826 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001827 ssize_t len;
1828 struct key *key;
1829 struct TCP_Server_Info *server = ses->server;
1830 struct sockaddr_in *sa;
1831 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01001832 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001833
1834 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
1835 if (!desc)
1836 return -ENOMEM;
1837
1838 /* try to find an address key first */
1839 switch (server->dstaddr.ss_family) {
1840 case AF_INET:
1841 sa = (struct sockaddr_in *)&server->dstaddr;
1842 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
1843 break;
1844 case AF_INET6:
1845 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
1846 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
1847 break;
1848 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001849 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
1850 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001851 rc = -EINVAL;
1852 goto out_err;
1853 }
1854
Joe Perchesf96637b2013-05-04 22:12:25 -05001855 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001856 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001857 if (IS_ERR(key)) {
1858 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001859 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001860 rc = PTR_ERR(key);
1861 goto out_err;
1862 }
1863
1864 /* didn't work, try to find a domain key */
1865 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05001866 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001867 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001868 if (IS_ERR(key)) {
1869 rc = PTR_ERR(key);
1870 goto out_err;
1871 }
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001872 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001873 }
1874
1875 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00001876 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001877 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05001878 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001879 goto out_key_put;
1880 }
1881
1882 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01001883 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001884 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05001885 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001886 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001887 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
1888 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001889 rc = -EINVAL;
1890 goto out_key_put;
1891 }
1892
1893 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001894 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001895 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
1896 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001897 rc = -EINVAL;
1898 goto out_key_put;
1899 }
1900
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001901 ctx->username = kstrndup(payload, len, GFP_KERNEL);
1902 if (!ctx->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001903 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
1904 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001905 rc = -ENOMEM;
1906 goto out_key_put;
1907 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001908 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001909
1910 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001911 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001912 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001913 rc = -EINVAL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001914 kfree(ctx->username);
1915 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001916 goto out_key_put;
1917 }
1918
1919 ++delim;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001920 ctx->password = kstrndup(delim, len, GFP_KERNEL);
1921 if (!ctx->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001922 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
1923 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001924 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001925 kfree(ctx->username);
1926 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001927 goto out_key_put;
1928 }
1929
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001930 /*
1931 * If we have a domain key then we must set the domainName in the
1932 * for the request.
1933 */
1934 if (is_domain && ses->domainName) {
Al Viro8d767222021-03-05 15:02:34 -05001935 ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001936 if (!ctx->domainname) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001937 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
1938 len);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001939 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001940 kfree(ctx->username);
1941 ctx->username = NULL;
1942 kfree_sensitive(ctx->password);
1943 ctx->password = NULL;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001944 goto out_key_put;
1945 }
1946 }
1947
Jeff Layton8a8798a2012-01-17 16:09:15 -05001948out_key_put:
1949 up_read(&key->sem);
1950 key_put(key);
1951out_err:
1952 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05001953 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001954 return rc;
1955}
1956#else /* ! CONFIG_KEYS */
1957static inline int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001958cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
Jeff Layton8a8798a2012-01-17 16:09:15 -05001959 struct cifs_ses *ses __attribute__((unused)))
1960{
1961 return -ENOSYS;
1962}
1963#endif /* CONFIG_KEYS */
1964
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01001965/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001966 * cifs_get_smb_ses - get a session matching @ctx data from @server
Steve French4c51de12021-09-13 18:29:46 -05001967 * @server: server to setup the session to
1968 * @ctx: superblock configuration context to use to setup the session
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01001969 *
1970 * This function assumes it is being called from cifs_mount() where we
1971 * already got a server reference (server refcount +1). See
1972 * cifs_get_tcon() for refcount explanations.
1973 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001974struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001975cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Layton36988c72010-04-24 07:57:43 -04001976{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04001977 int rc = -ENOMEM;
1978 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00001979 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001980 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
1981 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04001982
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001983 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04001984
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001985 ses = cifs_find_smb_ses(server, ctx);
Jeff Layton36988c72010-04-24 07:57:43 -04001986 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001987 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
1988 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04001989
Jeff Layton36988c72010-04-24 07:57:43 -04001990 mutex_lock(&ses->session_mutex);
1991 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001992 cifs_dbg(FYI, "Session needs reconnect\n");
Shyam Prasad N8e077572021-07-19 10:03:38 +00001993
1994 rc = cifs_negotiate_protocol(xid, ses);
1995 if (rc) {
1996 mutex_unlock(&ses->session_mutex);
1997 /* problem -- put our ses reference */
1998 cifs_put_smb_ses(ses);
1999 free_xid(xid);
2000 return ERR_PTR(rc);
2001 }
2002
Jeff Layton36988c72010-04-24 07:57:43 -04002003 rc = cifs_setup_session(xid, ses,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002004 ctx->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002005 if (rc) {
2006 mutex_unlock(&ses->session_mutex);
2007 /* problem -- put our reference */
2008 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002009 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002010 return ERR_PTR(rc);
2011 }
2012 }
2013 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002014
2015 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002016 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002017 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002018 return ses;
2019 }
2020
Joe Perchesf96637b2013-05-04 22:12:25 -05002021 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002022 ses = sesInfoAlloc();
2023 if (ses == NULL)
2024 goto get_ses_fail;
2025
2026 /* new SMB session uses our server ref */
2027 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002028 if (server->dstaddr.ss_family == AF_INET6)
Steve Frenchb438fcf2021-02-20 19:24:11 -06002029 sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002030 else
Steve Frenchb438fcf2021-02-20 19:24:11 -06002031 sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002032
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002033 if (ctx->username) {
2034 ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
Steve French8727c8a2011-02-25 01:11:56 -06002035 if (!ses->user_name)
2036 goto get_ses_fail;
2037 }
Jeff Layton36988c72010-04-24 07:57:43 -04002038
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002039 /* ctx->password freed at unmount */
2040 if (ctx->password) {
2041 ses->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Layton36988c72010-04-24 07:57:43 -04002042 if (!ses->password)
2043 goto get_ses_fail;
2044 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002045 if (ctx->domainname) {
2046 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002047 if (!ses->domainName)
2048 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002049 }
Shyam Prasad N49bd49f2021-11-05 19:03:57 +00002050 if (ctx->workstation_name) {
2051 ses->workstation_name = kstrdup(ctx->workstation_name,
2052 GFP_KERNEL);
2053 if (!ses->workstation_name)
2054 goto get_ses_fail;
2055 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002056 if (ctx->domainauto)
2057 ses->domainAuto = ctx->domainauto;
2058 ses->cred_uid = ctx->cred_uid;
2059 ses->linux_uid = ctx->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002060
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002061 ses->sectype = ctx->sectype;
2062 ses->sign = ctx->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002063 mutex_lock(&ses->session_mutex);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002064
2065 /* add server as first channel */
Shyam Prasad N724244c2021-07-19 10:54:46 +00002066 spin_lock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002067 ses->chans[0].server = server;
2068 ses->chan_count = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002069 ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
Shyam Prasad N724244c2021-07-19 10:54:46 +00002070 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002071
Jeff Layton198b5682010-04-24 07:57:48 -04002072 rc = cifs_negotiate_protocol(xid, ses);
2073 if (!rc)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002074 rc = cifs_setup_session(xid, ses, ctx->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002075
2076 /* each channel uses a different signing key */
2077 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
2078 sizeof(ses->smb3signingkey));
2079
Jeff Layton36988c72010-04-24 07:57:43 -04002080 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002081 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002082 goto get_ses_fail;
2083
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002084 /* success, put it on the list and add it as first channel */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302085 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002086 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302087 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002088
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002089 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002090
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002091 cifs_setup_ipc(ses, ctx);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002092
Jeff Layton36988c72010-04-24 07:57:43 -04002093 return ses;
2094
2095get_ses_fail:
2096 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002097 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002098 return ERR_PTR(rc);
2099}
2100
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002101static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002102{
2103 if (tcon->tidStatus == CifsExiting)
2104 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002105 if (strncmp(tcon->treeName, ctx->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002106 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002107 if (tcon->seal != ctx->seal)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002108 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002109 if (tcon->snapshot_time != ctx->snapshot_time)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002110 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002111 if (tcon->handle_timeout != ctx->handle_timeout)
Steve Frenchca567eb2019-03-29 16:31:07 -05002112 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002113 if (tcon->no_lease != ctx->no_lease)
Steve French3e7a02d2019-09-11 21:46:20 -05002114 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002115 if (tcon->nodelete != ctx->nodelete)
Steve French82e93672020-05-19 03:06:57 -05002116 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002117 return 1;
2118}
2119
Steve French96daf2b2011-05-27 04:34:02 +00002120static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002121cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002124 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302126 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002127 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002128 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Paulo Alcantaraf3c852b2021-06-04 19:25:33 -03002129
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002130 if (!match_tcon(tcon, ctx))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002131 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002132 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302133 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return tcon;
2135 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302136 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return NULL;
2138}
2139
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002140void
Steve French96daf2b2011-05-27 04:34:02 +00002141cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002142{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002143 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002144 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002145
Aurelien Aptelb327a712018-01-24 13:46:10 +01002146 /*
2147 * IPC tcon share the lifetime of their session and are
2148 * destroyed in the session put function
2149 */
2150 if (tcon == NULL || tcon->ipc)
2151 return;
2152
2153 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002154 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302155 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002156 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302157 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002158 return;
2159 }
2160
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05002161 /* tc_count can never go negative */
2162 WARN_ON(tcon->tc_count < 0);
2163
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002164 if (tcon->use_witness) {
2165 int rc;
2166
2167 rc = cifs_swn_unregister(tcon);
2168 if (rc < 0) {
2169 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
2170 __func__, rc);
2171 }
2172 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002173
Jeff Laytonf1987b42008-11-15 11:12:47 -05002174 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302175 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002176
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002177 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002178 if (ses->server->ops->tree_disconnect)
2179 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002180 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002181
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302182 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002183 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002184 cifs_put_smb_ses(ses);
2185}
2186
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002187/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002188 * cifs_get_tcon - get a tcon matching @ctx data from @ses
Steve French4c51de12021-09-13 18:29:46 -05002189 * @ses: smb session to issue the request on
2190 * @ctx: the superblock configuration context to use for building the
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002191 *
2192 * - tcon refcount is the number of mount points using the tcon.
2193 * - ses refcount is the number of tcon using the session.
2194 *
2195 * 1. This function assumes it is being called from cifs_mount() where
2196 * we already got a session reference (ses refcount +1).
2197 *
2198 * 2. Since we're in the context of adding a mount point, the end
2199 * result should be either:
2200 *
2201 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2202 * its session refcount incremented (1 new tcon). This +1 was
2203 * already done in (1).
2204 *
2205 * b) an existing tcon with refcount+1 (add a mount point to it) and
2206 * identical ses refcount (no new tcon). Because of (1) we need to
2207 * decrement the ses refcount.
2208 */
Steve French96daf2b2011-05-27 04:34:02 +00002209static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002210cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002211{
2212 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002213 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002214
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002215 tcon = cifs_find_tcon(ses, ctx);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002216 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002217 /*
2218 * tcon has refcount already incremented but we need to
2219 * decrement extra ses reference gotten by caller (case b)
2220 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002221 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002222 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002223 return tcon;
2224 }
2225
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002226 if (!ses->server->ops->tree_connect) {
2227 rc = -ENOSYS;
2228 goto out_fail;
2229 }
2230
Jeff Laytond00c28d2010-04-24 07:57:44 -04002231 tcon = tconInfoAlloc();
2232 if (tcon == NULL) {
2233 rc = -ENOMEM;
2234 goto out_fail;
2235 }
2236
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002237 if (ctx->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06002238 if (ses->server->vals->protocol_id == 0) {
2239 cifs_dbg(VFS,
2240 "Use SMB2 or later for snapshot mount option\n");
2241 rc = -EOPNOTSUPP;
2242 goto out_fail;
2243 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002244 tcon->snapshot_time = ctx->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06002245 }
2246
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002247 if (ctx->handle_timeout) {
Steve Frenchca567eb2019-03-29 16:31:07 -05002248 if (ses->server->vals->protocol_id == 0) {
2249 cifs_dbg(VFS,
2250 "Use SMB2.1 or later for handle timeout option\n");
2251 rc = -EOPNOTSUPP;
2252 goto out_fail;
2253 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002254 tcon->handle_timeout = ctx->handle_timeout;
Steve Frenchca567eb2019-03-29 16:31:07 -05002255 }
2256
Jeff Laytond00c28d2010-04-24 07:57:44 -04002257 tcon->ses = ses;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002258 if (ctx->password) {
2259 tcon->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002260 if (!tcon->password) {
2261 rc = -ENOMEM;
2262 goto out_fail;
2263 }
2264 }
2265
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002266 if (ctx->seal) {
Steve French23657ad2018-04-22 15:14:58 -05002267 if (ses->server->vals->protocol_id == 0) {
2268 cifs_dbg(VFS,
2269 "SMB3 or later required for encryption\n");
2270 rc = -EOPNOTSUPP;
2271 goto out_fail;
2272 } else if (tcon->ses->server->capabilities &
2273 SMB2_GLOBAL_CAP_ENCRYPTION)
2274 tcon->seal = true;
2275 else {
2276 cifs_dbg(VFS, "Encryption is not supported on share\n");
2277 rc = -EOPNOTSUPP;
2278 goto out_fail;
2279 }
2280 }
2281
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002282 if (ctx->linux_ext) {
Steve French8505c8b2018-06-18 14:01:59 -05002283 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05002284 tcon->posix_extensions = true;
Joe Perchesa0a30362020-04-14 22:42:53 -07002285 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05002286 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07002287 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
Steve French8505c8b2018-06-18 14:01:59 -05002288 rc = -EOPNOTSUPP;
2289 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05002290 }
Steve Frenchb3266142018-05-20 23:41:10 -05002291 }
Steve Frenchb3266142018-05-20 23:41:10 -05002292
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002293 /*
2294 * BB Do we need to wrap session_mutex around this TCon call and Unix
2295 * SetFS as we do on SessSetup and reconnect?
2296 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002297 xid = get_xid();
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002298 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2299 ctx->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002300 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05002301 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002302 if (rc)
2303 goto out_fail;
2304
Steve Frenchb618f002015-11-03 09:15:03 -06002305 tcon->use_persistent = false;
2306 /* check if SMB2 or later, CIFS does not support persistent handles */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002307 if (ctx->persistent) {
Steve Frenchb618f002015-11-03 09:15:03 -06002308 if (ses->server->vals->protocol_id == 0) {
2309 cifs_dbg(VFS,
2310 "SMB3 or later required for persistent handles\n");
2311 rc = -EOPNOTSUPP;
2312 goto out_fail;
2313 } else if (ses->server->capabilities &
2314 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2315 tcon->use_persistent = true;
2316 else /* persistent handles requested but not supported */ {
2317 cifs_dbg(VFS,
2318 "Persistent handles not supported on share\n");
2319 rc = -EOPNOTSUPP;
2320 goto out_fail;
2321 }
2322 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2323 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002324 && (ctx->nopersistent == false)) {
Steve Frenchb618f002015-11-03 09:15:03 -06002325 cifs_dbg(FYI, "enabling persistent handles\n");
2326 tcon->use_persistent = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002327 } else if (ctx->resilient) {
Steve French592fafe2015-11-03 10:08:53 -06002328 if (ses->server->vals->protocol_id == 0) {
2329 cifs_dbg(VFS,
2330 "SMB2.1 or later required for resilient handles\n");
2331 rc = -EOPNOTSUPP;
2332 goto out_fail;
2333 }
2334 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06002335 }
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +02002336
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002337 tcon->use_witness = false;
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +02002338 if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002339 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2340 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002341 /*
2342 * Set witness in use flag in first place
2343 * to retry registration in the echo task
2344 */
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002345 tcon->use_witness = true;
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002346 /* And try to register immediately */
2347 rc = cifs_swn_register(tcon);
2348 if (rc < 0) {
2349 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2350 goto out_fail;
2351 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002352 } else {
2353 /* TODO: try to extend for non-cluster uses (eg multichannel) */
2354 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2355 rc = -EOPNOTSUPP;
2356 goto out_fail;
2357 }
2358 } else {
2359 cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2360 rc = -EOPNOTSUPP;
2361 goto out_fail;
2362 }
2363 }
Steve Frenchb618f002015-11-03 09:15:03 -06002364
Steve Frenchcae53f72019-09-03 17:49:46 -05002365 /* If the user really knows what they are doing they can override */
2366 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002367 if (ctx->cache_ro)
Steve Frenchcae53f72019-09-03 17:49:46 -05002368 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002369 else if (ctx->cache_rw)
Steve Frenchcae53f72019-09-03 17:49:46 -05002370 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2371 }
2372
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002373 if (ctx->no_lease) {
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302374 if (ses->server->vals->protocol_id == 0) {
2375 cifs_dbg(VFS,
2376 "SMB2 or later required for nolease option\n");
2377 rc = -EOPNOTSUPP;
2378 goto out_fail;
2379 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002380 tcon->no_lease = ctx->no_lease;
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302381 }
2382
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002383 /*
2384 * We can have only one retry value for a connection to a share so for
2385 * resources mounted more than once to the same server share the last
2386 * value passed in for the retry flag is used.
2387 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002388 tcon->retry = ctx->retry;
2389 tcon->nocase = ctx->nocase;
Steve French3c6e65e2020-10-21 00:15:42 -05002390 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002391 tcon->nohandlecache = ctx->nohandlecache;
Steve French3c6e65e2020-10-21 00:15:42 -05002392 else
Jiapeng Zhong2be449f2021-01-14 17:09:20 +08002393 tcon->nohandlecache = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002394 tcon->nodelete = ctx->nodelete;
2395 tcon->local_lease = ctx->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07002396 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002397
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302398 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002399 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302400 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002401
2402 return tcon;
2403
2404out_fail:
2405 tconInfoFree(tcon);
2406 return ERR_PTR(rc);
2407}
2408
Jeff Layton9d002df2010-10-06 19:51:11 -04002409void
2410cifs_put_tlink(struct tcon_link *tlink)
2411{
2412 if (!tlink || IS_ERR(tlink))
2413 return;
2414
2415 if (!atomic_dec_and_test(&tlink->tl_count) ||
2416 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2417 tlink->tl_time = jiffies;
2418 return;
2419 }
2420
2421 if (!IS_ERR(tlink_tcon(tlink)))
2422 cifs_put_tcon(tlink_tcon(tlink));
2423 kfree(tlink);
2424 return;
2425}
Jeff Laytond00c28d2010-04-24 07:57:44 -04002426
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002427static int
2428compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2429{
2430 struct cifs_sb_info *old = CIFS_SB(sb);
2431 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002432 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2433 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002434
2435 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2436 return 0;
2437
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002438 if (old->mnt_cifs_serverino_autodisabled)
2439 newflags &= ~CIFS_MOUNT_SERVER_INUM;
2440
2441 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002442 return 0;
2443
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002444 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002445 * We want to share sb only if we don't specify an r/wsize or
2446 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002447 */
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002448 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002449 return 0;
2450
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002451 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002452 return 0;
2453
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002454 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2455 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002456 return 0;
2457
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002458 if (old->ctx->file_mode != new->ctx->file_mode ||
2459 old->ctx->dir_mode != new->ctx->dir_mode)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002460 return 0;
2461
2462 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2463 return 0;
2464
Steve French57804642021-02-24 12:12:53 -06002465 if (old->ctx->acregmax != new->ctx->acregmax)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002466 return 0;
Steve French4c9f9482021-02-23 15:50:57 -06002467 if (old->ctx->acdirmax != new->ctx->acdirmax)
2468 return 0;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002469
2470 return 1;
2471}
2472
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002473static int
2474match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2475{
2476 struct cifs_sb_info *old = CIFS_SB(sb);
2477 struct cifs_sb_info *new = mnt_data->cifs_sb;
Ronnie Sahlbergfe129262020-01-22 11:07:56 +10002478 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2479 old->prepath;
2480 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2481 new->prepath;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002482
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002483 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002484 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002485 else if (!old_set && !new_set)
2486 return 1;
2487
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002488 return 0;
2489}
2490
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002491int
2492cifs_match_super(struct super_block *sb, void *data)
2493{
2494 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002495 struct smb3_fs_context *ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002496 struct cifs_sb_info *cifs_sb;
2497 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00002498 struct cifs_ses *ses;
2499 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002500 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002501 int rc = 0;
2502
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002503 spin_lock(&cifs_tcp_ses_lock);
2504 cifs_sb = CIFS_SB(sb);
2505 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
Steve French9ed38fd2021-09-23 19:18:37 -05002506 if (tlink == NULL) {
2507 /* can not match superblock if tlink were ever null */
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002508 spin_unlock(&cifs_tcp_ses_lock);
Steve French9ed38fd2021-09-23 19:18:37 -05002509 return 0;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002510 }
2511 tcon = tlink_tcon(tlink);
2512 ses = tcon->ses;
2513 tcp_srv = ses->server;
2514
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002515 ctx = mnt_data->ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002516
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002517 if (!match_server(tcp_srv, ctx) ||
2518 !match_session(ses, ctx) ||
2519 !match_tcon(tcon, ctx) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002520 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002521 rc = 0;
2522 goto out;
2523 }
2524
2525 rc = compare_mount_options(sb, mnt_data);
2526out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002527 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04002528 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002529 return rc;
2530}
2531
Jeff Layton09e50d52008-07-23 10:11:19 -04002532#ifdef CONFIG_DEBUG_LOCK_ALLOC
2533static struct lock_class_key cifs_key[2];
2534static struct lock_class_key cifs_slock_key[2];
2535
2536static inline void
2537cifs_reclassify_socket4(struct socket *sock)
2538{
2539 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002540 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002541 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2542 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2543}
2544
2545static inline void
2546cifs_reclassify_socket6(struct socket *sock)
2547{
2548 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002549 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002550 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2551 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2552}
2553#else
2554static inline void
2555cifs_reclassify_socket4(struct socket *sock)
2556{
2557}
2558
2559static inline void
2560cifs_reclassify_socket6(struct socket *sock)
2561{
2562}
2563#endif
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00002566static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Steve French50c2f752007-07-13 00:33:32 +00002568 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Steve French50c2f752007-07-13 00:33:32 +00002570 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 /* mask a nibble at a time and encode */
2572 target[j] = 'A' + (0x0F & (source[i] >> 4));
2573 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00002574 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576
2577}
2578
Ben Greear3eb9a882010-09-01 17:06:02 -07002579static int
2580bind_socket(struct TCP_Server_Info *server)
2581{
2582 int rc = 0;
2583 if (server->srcaddr.ss_family != AF_UNSPEC) {
2584 /* Bind to the specified local IP address */
2585 struct socket *socket = server->ssocket;
2586 rc = socket->ops->bind(socket,
2587 (struct sockaddr *) &server->srcaddr,
2588 sizeof(server->srcaddr));
2589 if (rc < 0) {
2590 struct sockaddr_in *saddr4;
2591 struct sockaddr_in6 *saddr6;
2592 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2593 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2594 if (saddr6->sin6_family == AF_INET6)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002595 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002596 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002597 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002598 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002599 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002600 }
2601 }
2602 return rc;
2603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002606ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
2608 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002609 /*
2610 * some servers require RFC1001 sessinit before sending
2611 * negprot - BB check reconnection in case where second
2612 * sessinit is sent but no second negprot
2613 */
2614 struct rfc1002_session_packet *ses_init_buf;
2615 struct smb_hdr *smb_buf;
2616 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2617 GFP_KERNEL);
2618 if (ses_init_buf) {
2619 ses_init_buf->trailer.session_req.called_len = 32;
2620
Colin Ian King997152f2016-01-25 16:25:54 +00002621 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002622 rfc1002mangle(ses_init_buf->trailer.
2623 session_req.called_name,
2624 server->server_RFC1001_name,
2625 RFC1001_NAME_LEN_WITH_NULL);
2626 else
2627 rfc1002mangle(ses_init_buf->trailer.
2628 session_req.called_name,
2629 DEFAULT_CIFS_CALLED_NAME,
2630 RFC1001_NAME_LEN_WITH_NULL);
2631
2632 ses_init_buf->trailer.session_req.calling_len = 32;
2633
2634 /*
2635 * calling name ends in null (byte 16) from old smb
2636 * convention.
2637 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05002638 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002639 rfc1002mangle(ses_init_buf->trailer.
2640 session_req.calling_name,
2641 server->workstation_RFC1001_name,
2642 RFC1001_NAME_LEN_WITH_NULL);
2643 else
2644 rfc1002mangle(ses_init_buf->trailer.
2645 session_req.calling_name,
2646 "LINUX_CIFS_CLNT",
2647 RFC1001_NAME_LEN_WITH_NULL);
2648
2649 ses_init_buf->trailer.session_req.scope1 = 0;
2650 ses_init_buf->trailer.session_req.scope2 = 0;
2651 smb_buf = (struct smb_hdr *)ses_init_buf;
2652
2653 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002654 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002655 rc = smb_send(server, smb_buf, 0x44);
2656 kfree(ses_init_buf);
2657 /*
2658 * RFC1001 layer in at least one server
2659 * requires very short break before negprot
2660 * presumably because not expecting negprot
2661 * to follow so fast. This is a simple
2662 * solution that works without
2663 * complicating the code and causes no
2664 * significant slowing down on mount
2665 * for everyone else
2666 */
2667 usleep_range(1000, 2000);
2668 }
2669 /*
2670 * else the negprot may still work without this
2671 * even though malloc failed
2672 */
2673
2674 return rc;
2675}
2676
2677static int
2678generic_ip_connect(struct TCP_Server_Info *server)
2679{
2680 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00002681 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002682 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002683 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002684 struct sockaddr *saddr;
2685
2686 saddr = (struct sockaddr *) &server->dstaddr;
2687
2688 if (server->dstaddr.ss_family == AF_INET6) {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002689 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2690
2691 sport = ipv6->sin6_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002692 slen = sizeof(struct sockaddr_in6);
2693 sfamily = AF_INET6;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002694 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2695 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002696 } else {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002697 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2698
2699 sport = ipv4->sin_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002700 slen = sizeof(struct sockaddr_in);
2701 sfamily = AF_INET;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002702 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2703 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002706 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06002707 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2708 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (rc < 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002710 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002711 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002714
2715 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05002716 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002717 server->ssocket = socket;
2718 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002719 if (sfamily == AF_INET6)
2720 cifs_reclassify_socket6(socket);
2721 else
2722 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724
Ben Greear3eb9a882010-09-01 17:06:02 -07002725 rc = bind_socket(server);
2726 if (rc < 0)
2727 return rc;
2728
Jeff Laytond5c56052008-12-01 18:42:33 -05002729 /*
2730 * Eventually check for other socket options to change from
2731 * the default. sock_setsockopt not used because it expects
2732 * user space buffer
2733 */
2734 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00002735 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00002736
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002737 /* make the bufsizes depend on wsize/rsize and max requests */
2738 if (server->noautotune) {
2739 if (socket->sk->sk_sndbuf < (200 * 1024))
2740 socket->sk->sk_sndbuf = 200 * 1024;
2741 if (socket->sk->sk_rcvbuf < (140 * 1024))
2742 socket->sk->sk_rcvbuf = 140 * 1024;
2743 }
2744
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02002745 if (server->tcp_nodelay)
2746 tcp_sock_set_nodelay(socket->sk);
Steve French6a5fa2362010-01-01 01:28:43 +00002747
Joe Perchesf96637b2013-05-04 22:12:25 -05002748 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002749 socket->sk->sk_sndbuf,
2750 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2751
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002752 rc = socket->ops->connect(socket, saddr, slen,
2753 server->noblockcnt ? O_NONBLOCK : 0);
Paulo Alcantara (SUSE)d532cc72019-10-10 12:31:58 -03002754 /*
2755 * When mounting SMB root file systems, we do not want to block in
2756 * connect. Otherwise bail out and then let cifs_reconnect() perform
2757 * reconnect failover - if possible.
2758 */
2759 if (server->noblockcnt && rc == -EINPROGRESS)
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002760 rc = 0;
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002761 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002762 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Steve Frenchd7171cd2021-11-04 15:56:37 -05002763 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002764 sock_release(socket);
2765 server->ssocket = NULL;
2766 return rc;
2767 }
Steve Frenchd7171cd2021-11-04 15:56:37 -05002768 trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002769 if (sport == htons(RFC1001_PORT))
2770 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00002771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return rc;
2773}
2774
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002775static int
2776ip_connect(struct TCP_Server_Info *server)
2777{
Steve French6da97912011-03-13 18:55:55 +00002778 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002779 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2780 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2781
2782 if (server->dstaddr.ss_family == AF_INET6)
2783 sport = &addr6->sin6_port;
2784 else
2785 sport = &addr->sin_port;
2786
2787 if (*sport == 0) {
2788 int rc;
2789
2790 /* try with 445 port at first */
2791 *sport = htons(CIFS_PORT);
2792
2793 rc = generic_ip_connect(server);
2794 if (rc >= 0)
2795 return rc;
2796
2797 /* if it failed, try with 139 port */
2798 *sport = htons(RFC1001_PORT);
2799 }
2800
2801 return generic_ip_connect(server);
2802}
2803
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002804void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002805 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Steve French8af18972007-02-14 04:42:51 +00002806{
Steve Frenchbc044992020-12-11 19:48:26 -06002807 /*
2808 * If we are reconnecting then should we check to see if
Steve French8af18972007-02-14 04:42:51 +00002809 * any requested capabilities changed locally e.g. via
2810 * remount but we can not do much about it here
2811 * if they have (even if we could detect it by the following)
2812 * Perhaps we could add a backpointer to array of sb from tcon
2813 * or if we change to make all sb to same share the same
2814 * sb as NFS - then we only have one backpointer to sb.
2815 * What if we wanted to mount the server share twice once with
Steve Frenchbc044992020-12-11 19:48:26 -06002816 * and once without posixacls or posix paths?
2817 */
Steve French8af18972007-02-14 04:42:51 +00002818 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00002819
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002820 if (ctx && ctx->no_linux_ext) {
Steve Frenchc18c8422007-07-18 23:21:09 +00002821 tcon->fsUnixInfo.Capability = 0;
2822 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05002823 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002824 return;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002825 } else if (ctx)
Steve Frenchc18c8422007-07-18 23:21:09 +00002826 tcon->unix_ext = 1; /* Unix Extensions supported */
2827
Jiapeng Zhong16a78852021-01-14 18:02:23 +08002828 if (!tcon->unix_ext) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002829 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002830 return;
2831 }
Steve French50c2f752007-07-13 00:33:32 +00002832
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002833 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00002834 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05002835 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve Frenchbc044992020-12-11 19:48:26 -06002836 /*
2837 * check for reconnect case in which we do not
2838 * want to change the mount behavior if we can avoid it
2839 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002840 if (ctx == NULL) {
Steve Frenchbc044992020-12-11 19:48:26 -06002841 /*
2842 * turn off POSIX ACL and PATHNAMES if not set
2843 * originally at mount time
2844 */
Steve French8af18972007-02-14 04:42:51 +00002845 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
2846 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002847 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
2848 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002849 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00002850 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002851 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002852 cifs_dbg(VFS, "possible reconnect error\n");
2853 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00002854 }
Steve French8af18972007-02-14 04:42:51 +00002855 }
Steve French50c2f752007-07-13 00:33:32 +00002856
Steve French6848b732011-05-26 18:38:54 +00002857 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002858 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00002859
Steve French8af18972007-02-14 04:42:51 +00002860 cap &= CIFS_UNIX_CAP_MASK;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002861 if (ctx && ctx->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00002862 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002863 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002864 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002865 if (cifs_sb)
2866 cifs_sb->mnt_cifs_flags |=
2867 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00002868 }
2869
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002870 if (ctx && ctx->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00002871 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002872 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002873 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002874 if (cifs_sb)
2875 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00002876 CIFS_MOUNT_POSIX_PATHS;
2877 }
Steve French50c2f752007-07-13 00:33:32 +00002878
Joe Perchesf96637b2013-05-04 22:12:25 -05002879 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00002880#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00002881 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002882 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002883 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002884 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002885 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002886 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002887 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002888 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002889 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002890 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002891 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002892 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002893 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002894 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00002895 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002896 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00002897 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002898 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00002899#endif /* CIFS_DEBUG2 */
2900 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002901 if (ctx == NULL)
Joe Perchesf96637b2013-05-04 22:12:25 -05002902 cifs_dbg(FYI, "resetting capabilities failed\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002903 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002904 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 +00002905
Steve French8af18972007-02-14 04:42:51 +00002906 }
2907 }
2908}
2909
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002910int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002911{
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002912 struct smb3_fs_context *ctx = cifs_sb->ctx;
2913
Jeff Layton2de970f2010-10-06 19:51:12 -04002914 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
2915
Al Viro2ced6f62011-06-17 09:20:04 -04002916 spin_lock_init(&cifs_sb->tlink_tree_lock);
2917 cifs_sb->tlink_tree = RB_ROOT;
2918
Frank Sorensonf52aa792020-02-12 15:31:48 -06002919 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002920 ctx->file_mode, ctx->dir_mode);
Steve French3b795212008-11-13 19:45:32 +00002921
Ronnie Sahlberg387ec582020-12-14 16:40:20 +10002922 /* this is needed for ASCII cp to Unicode converts */
2923 if (ctx->iocharset == NULL) {
2924 /* load_nls_default cannot return null */
2925 cifs_sb->local_nls = load_nls_default();
2926 } else {
2927 cifs_sb->local_nls = load_nls(ctx->iocharset);
2928 if (cifs_sb->local_nls == NULL) {
2929 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
2930 ctx->iocharset);
2931 return -ELIBACC;
2932 }
2933 }
2934 ctx->local_nls = cifs_sb->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05302935
Ronnie Sahlberg2d39f502020-12-14 16:40:25 +10002936 smb3_update_mnt_flags(cifs_sb);
2937
2938 if (ctx->direct_io)
Joe Perchesf96637b2013-05-04 22:12:25 -05002939 cifs_dbg(FYI, "mounting share using direct i/o\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002940 if (ctx->cache_ro) {
Steve French83bbfa72019-08-27 23:58:54 -05002941 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
2942 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002943 } else if (ctx->cache_rw) {
Steve French41e033f2019-08-30 02:12:41 -05002944 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
2945 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
2946 CIFS_MOUNT_RW_CACHE);
Steve French83bbfa72019-08-27 23:58:54 -05002947 }
Steve French3b795212008-11-13 19:45:32 +00002948
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002949 if ((ctx->cifs_acl) && (ctx->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05002950 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002951
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002952 if (ctx->prepath) {
2953 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002954 if (cifs_sb->prepath == NULL)
2955 return -ENOMEM;
Shyam Prasad Na738c932021-02-11 03:26:54 -08002956 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002957 }
2958
2959 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002960}
2961
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002962/* Release all succeed connections */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002963static inline void mount_put_conns(struct mount_ctx *mnt_ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002964{
2965 int rc = 0;
2966
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002967 if (mnt_ctx->tcon)
2968 cifs_put_tcon(mnt_ctx->tcon);
2969 else if (mnt_ctx->ses)
2970 cifs_put_smb_ses(mnt_ctx->ses);
2971 else if (mnt_ctx->server)
2972 cifs_put_tcp_session(mnt_ctx->server, 0);
2973 mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
2974 free_xid(mnt_ctx->xid);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002975}
2976
2977/* Get connections for tcp, ses and tcon */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002978static int mount_get_conns(struct mount_ctx *mnt_ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002979{
2980 int rc = 0;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002981 struct TCP_Server_Info *server = NULL;
2982 struct cifs_ses *ses = NULL;
2983 struct cifs_tcon *tcon = NULL;
2984 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
2985 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
2986 unsigned int xid;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002987
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002988 xid = get_xid();
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002989
2990 /* get a reference to a tcp session */
Shyam Prasad N0f2b3052021-07-19 11:26:24 +00002991 server = cifs_get_tcp_session(ctx, NULL);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002992 if (IS_ERR(server)) {
2993 rc = PTR_ERR(server);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002994 server = NULL;
2995 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002996 }
2997
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002998 /* get a reference to a SMB session */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002999 ses = cifs_get_smb_ses(server, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003000 if (IS_ERR(ses)) {
3001 rc = PTR_ERR(ses);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003002 ses = NULL;
3003 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003004 }
3005
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003006 if ((ctx->persistent == true) && (!(ses->server->capabilities &
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003007 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003008 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003009 rc = -EOPNOTSUPP;
3010 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003011 }
3012
3013 /* search for existing tcon to this server share */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003014 tcon = cifs_get_tcon(ses, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003015 if (IS_ERR(tcon)) {
3016 rc = PTR_ERR(tcon);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003017 tcon = NULL;
3018 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003019 }
3020
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003021 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3022 if (tcon->posix_extensions)
3023 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3024
3025 /* tell server which Unix caps we support */
3026 if (cap_unix(tcon->ses)) {
3027 /*
3028 * reset of caps checks mount to see if unix extensions disabled
3029 * for just this mount.
3030 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003031 reset_cifs_unix_caps(xid, tcon, cifs_sb, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003032 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3033 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003034 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3035 rc = -EACCES;
3036 goto out;
3037 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003038 } else
3039 tcon->unix_ext = 0; /* server does not support them */
3040
3041 /* do not care if a following call succeed - informational */
Steve French1981eba2019-08-29 22:33:38 -05003042 if (!tcon->pipe && server->ops->qfs_tcon) {
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003043 server->ops->qfs_tcon(xid, tcon, cifs_sb);
Steve French1981eba2019-08-29 22:33:38 -05003044 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
3045 if (tcon->fsDevInfo.DeviceCharacteristics &
Steve French52870d52019-10-01 21:25:46 -05003046 cpu_to_le32(FILE_READ_ONLY_DEVICE))
Steve French1981eba2019-08-29 22:33:38 -05003047 cifs_dbg(VFS, "mounted to read only share\n");
Steve French41e033f2019-08-30 02:12:41 -05003048 else if ((cifs_sb->mnt_cifs_flags &
3049 CIFS_MOUNT_RW_CACHE) == 0)
Steve French1981eba2019-08-29 22:33:38 -05003050 cifs_dbg(VFS, "read only mount of RW share\n");
Steve French41e033f2019-08-30 02:12:41 -05003051 /* no need to log a RW mount of a typical RW share */
Steve French1981eba2019-08-29 22:33:38 -05003052 }
3053 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003054
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003055 /*
3056 * Clamp the rsize/wsize mount arguments if they are too big for the server
Steve French0c2b5f72020-12-15 13:28:50 -06003057 * and set the rsize/wsize to the negotiated values if not passed in by
3058 * the user on mount
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003059 */
Steve French0c2b5f72020-12-15 13:28:50 -06003060 if ((cifs_sb->ctx->wsize == 0) ||
3061 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003062 cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
Steve French0c2b5f72020-12-15 13:28:50 -06003063 if ((cifs_sb->ctx->rsize == 0) ||
3064 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003065 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003066
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003067out:
3068 mnt_ctx->server = server;
3069 mnt_ctx->ses = ses;
3070 mnt_ctx->tcon = tcon;
3071 mnt_ctx->xid = xid;
3072
3073 return rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003074}
3075
3076static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3077 struct cifs_tcon *tcon)
3078{
3079 struct tcon_link *tlink;
3080
3081 /* hang the tcon off of the superblock */
3082 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3083 if (tlink == NULL)
3084 return -ENOMEM;
3085
3086 tlink->tl_uid = ses->linux_uid;
3087 tlink->tl_tcon = tcon;
3088 tlink->tl_time = jiffies;
3089 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3090 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3091
3092 cifs_sb->master_tlink = tlink;
3093 spin_lock(&cifs_sb->tlink_tree_lock);
3094 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3095 spin_unlock(&cifs_sb->tlink_tree_lock);
3096
3097 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3098 TLINK_IDLE_EXPIRE);
3099 return 0;
3100}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003101
Steve French2d6d5892009-04-09 00:36:44 +00003102#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003103/* Get unique dfs connections */
3104static int mount_get_dfs_conns(struct mount_ctx *mnt_ctx)
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003105{
3106 int rc;
3107
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003108 mnt_ctx->fs_ctx->nosharesock = true;
3109 rc = mount_get_conns(mnt_ctx);
3110 if (mnt_ctx->server) {
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003111 cifs_dbg(FYI, "%s: marking tcp session as a dfs connection\n", __func__);
3112 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003113 mnt_ctx->server->is_dfs_conn = true;
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003114 spin_unlock(&cifs_tcp_ses_lock);
3115 }
3116 return rc;
3117}
3118
Steve French6d3ea7e2012-11-28 22:34:41 -06003119/*
3120 * cifs_build_path_to_root returns full path to root when we do not have an
Steve French201023c2021-02-15 11:03:45 -06003121 * existing connection (tcon)
Steve French6d3ea7e2012-11-28 22:34:41 -06003122 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003123static char *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003124build_unc_path_to_root(const struct smb3_fs_context *ctx,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003125 const struct cifs_sb_info *cifs_sb, bool useppath)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003126{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003127 char *full_path, *pos;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003128 unsigned int pplen = useppath && ctx->prepath ?
3129 strlen(ctx->prepath) + 1 : 0;
3130 unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003131
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003132 if (unc_len > MAX_TREE_SIZE)
3133 return ERR_PTR(-EINVAL);
3134
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003135 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003136 if (full_path == NULL)
3137 return ERR_PTR(-ENOMEM);
3138
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003139 memcpy(full_path, ctx->UNC, unc_len);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003140 pos = full_path + unc_len;
3141
3142 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003143 *pos = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003144 memcpy(pos + 1, ctx->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003145 pos += pplen;
3146 }
3147
3148 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003149 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003150 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003151 return full_path;
3152}
Sean Finneydd613942011-04-11 13:19:30 +00003153
Steve French4c51de12021-09-13 18:29:46 -05003154/*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003155 * expand_dfs_referral - Update cifs_sb from dfs referral path
Paulo Alcantara1c780222018-11-14 16:24:03 -02003156 *
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003157 * cifs_sb->ctx->mount_options will be (re-)allocated to a string containing updated options for the
3158 * submount. Otherwise it will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003159 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003160static int expand_dfs_referral(struct mount_ctx *mnt_ctx, const char *full_path,
3161 struct dfs_info3_param *referral)
Sean Finneydd613942011-04-11 13:19:30 +00003162{
3163 int rc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003164 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3165 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3166 char *fake_devname = NULL, *mdata = NULL;
Sean Finneydd613942011-04-11 13:19:30 +00003167
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003168 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, referral,
3169 &fake_devname);
3170 if (IS_ERR(mdata)) {
3171 rc = PTR_ERR(mdata);
3172 mdata = NULL;
3173 } else {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003174 /*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003175 * We can not clear out the whole structure since we no longer have an explicit
3176 * function to parse a mount-string. Instead we need to clear out the individual
3177 * fields that are no longer valid.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003178 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003179 kfree(ctx->prepath);
3180 ctx->prepath = NULL;
3181 rc = cifs_setup_volume_info(ctx, mdata, fake_devname);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003182 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003183 kfree(fake_devname);
3184 kfree(cifs_sb->ctx->mount_options);
3185 cifs_sb->ctx->mount_options = mdata;
Paulo Alcantara85132222021-02-24 20:59:21 -03003186
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003187 return rc;
3188}
Steve French2d6d5892009-04-09 00:36:44 +00003189#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003190
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -06003191/* TODO: all callers to this are broken. We are not parsing mount_options here
3192 * we should pass a clone of the original context?
3193 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003194int
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003195cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003197 int rc;
Sean Finneydd613942011-04-11 13:19:30 +00003198
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003199 if (devname) {
3200 cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
3201 rc = smb3_parse_devname(devname, ctx);
3202 if (rc) {
3203 cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
3204 return rc;
3205 }
3206 }
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003207
3208 if (mntopts) {
3209 char *ip;
3210
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003211 rc = smb3_parse_opt(mntopts, "ip", &ip);
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003212 if (rc) {
3213 cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3214 return rc;
3215 }
3216
3217 rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3218 kfree(ip);
3219 if (!rc) {
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003220 cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
3221 return -EINVAL;
3222 }
3223 }
3224
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003225 if (ctx->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003226 cifs_dbg(FYI, "Anonymous login\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003227 kfree(ctx->username);
3228 ctx->username = NULL;
3229 } else if (ctx->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 /* BB fixme parse for domain name here */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003231 cifs_dbg(FYI, "Username: %s\n", ctx->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003233 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003234 /* In userspace mount helper we can get user name from alternate
3235 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003236 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
3238
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003239 return 0;
Jeff Layton04db79b2011-07-06 08:10:38 -04003240}
3241
Aurelien Aptela6b50582016-05-25 19:59:09 +02003242static int
3243cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3244 unsigned int xid,
3245 struct cifs_tcon *tcon,
3246 struct cifs_sb_info *cifs_sb,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003247 char *full_path,
3248 int added_treename)
Aurelien Aptela6b50582016-05-25 19:59:09 +02003249{
3250 int rc;
3251 char *s;
3252 char sep, tmp;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003253 int skip = added_treename ? 1 : 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +02003254
3255 sep = CIFS_DIR_SEP(cifs_sb);
3256 s = full_path;
3257
3258 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3259 while (rc == 0) {
3260 /* skip separators */
3261 while (*s == sep)
3262 s++;
3263 if (!*s)
3264 break;
3265 /* next separator */
3266 while (*s && *s != sep)
3267 s++;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003268 /*
3269 * if the treename is added, we then have to skip the first
3270 * part within the separators
3271 */
3272 if (skip) {
3273 skip = 0;
3274 continue;
3275 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02003276 /*
3277 * temporarily null-terminate the path at the end of
3278 * the current component
3279 */
3280 tmp = *s;
3281 *s = 0;
3282 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3283 full_path);
3284 *s = tmp;
3285 }
3286 return rc;
3287}
3288
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003289/*
3290 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
3291 * otherwise 0.
3292 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003293static int is_path_remote(struct mount_ctx *mnt_ctx)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003294{
Jeff Layton1daaae82012-03-21 06:30:40 -04003295 int rc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003296 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3297 struct TCP_Server_Info *server = mnt_ctx->server;
3298 unsigned int xid = mnt_ctx->xid;
3299 struct cifs_tcon *tcon = mnt_ctx->tcon;
3300 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003301 char *full_path;
3302
3303 if (!server->ops->is_path_accessible)
3304 return -EOPNOTSUPP;
3305
3306 /*
3307 * cifs_build_path_to_root works only when we have a valid tcon
3308 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003309 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003310 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3311 if (full_path == NULL)
3312 return -ENOMEM;
3313
3314 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3315
3316 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3317 full_path);
3318 if (rc != 0 && rc != -EREMOTE) {
3319 kfree(full_path);
3320 return rc;
3321 }
3322
3323 if (rc != -EREMOTE) {
3324 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003325 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003326 if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07003327 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 -02003328 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3329 rc = 0;
3330 }
3331 }
3332
3333 kfree(full_path);
3334 return rc;
3335}
3336
3337#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003338static void set_root_ses(struct mount_ctx *mnt_ctx)
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03003339{
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003340 if (mnt_ctx->ses) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003341 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003342 mnt_ctx->ses->ses_count++;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003343 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003344 dfs_cache_add_refsrv_session(&mnt_ctx->mount_id, mnt_ctx->ses);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003345 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003346 mnt_ctx->root_ses = mnt_ctx->ses;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003347}
3348
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003349static int is_dfs_mount(struct mount_ctx *mnt_ctx, bool *isdfs, struct dfs_cache_tgt_list *root_tl)
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003350{
3351 int rc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003352 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3353 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003354
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003355 *isdfs = true;
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003356
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003357 rc = mount_get_conns(mnt_ctx);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003358 /*
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003359 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
3360 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003361 *
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003362 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
3363 * to respond with PATH_NOT_COVERED to requests that include the prefix.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003364 */
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003365 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003366 dfs_cache_find(mnt_ctx->xid, mnt_ctx->ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
3367 ctx->UNC + 1, NULL, root_tl)) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003368 if (rc)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003369 return rc;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003370 /* Check if it is fully accessible and then mount it */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003371 rc = is_path_remote(mnt_ctx);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003372 if (!rc)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003373 *isdfs = false;
3374 else if (rc != -EREMOTE)
3375 return rc;
3376 }
3377 return 0;
3378}
3379
3380static int connect_dfs_target(struct mount_ctx *mnt_ctx, const char *full_path,
3381 const char *ref_path, struct dfs_cache_tgt_iterator *tit)
3382{
3383 int rc;
3384 struct dfs_info3_param ref = {};
3385 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3386 char *oldmnt = cifs_sb->ctx->mount_options;
3387
3388 rc = dfs_cache_get_tgt_referral(ref_path, tit, &ref);
3389 if (rc)
3390 goto out;
3391
3392 rc = expand_dfs_referral(mnt_ctx, full_path, &ref);
3393 if (rc)
3394 goto out;
3395
3396 /* Connect to new target only if we were redirected (e.g. mount options changed) */
3397 if (oldmnt != cifs_sb->ctx->mount_options) {
3398 mount_put_conns(mnt_ctx);
3399 rc = mount_get_dfs_conns(mnt_ctx);
3400 }
3401 if (!rc) {
3402 if (cifs_is_referral_server(mnt_ctx->tcon, &ref))
3403 set_root_ses(mnt_ctx);
3404 rc = dfs_cache_update_tgthint(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3405 cifs_remap(cifs_sb), ref_path, tit);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003406 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003407
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003408out:
3409 free_dfs_info_param(&ref);
3410 return rc;
3411}
Paulo Alcantaraf3c852b2021-06-04 19:25:33 -03003412
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003413static int connect_dfs_root(struct mount_ctx *mnt_ctx, struct dfs_cache_tgt_list *root_tl)
3414{
3415 int rc;
3416 char *full_path;
3417 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3418 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3419 struct dfs_cache_tgt_iterator *tit;
3420
3421 /* Put initial connections as they might be shared with other mounts. We need unique dfs
3422 * connections per mount to properly failover, so mount_get_dfs_conns() must be used from
3423 * now on.
3424 */
3425 mount_put_conns(mnt_ctx);
3426 mount_get_dfs_conns(mnt_ctx);
Paulo Alcantara65de2622021-12-02 15:29:35 -03003427 set_root_ses(mnt_ctx);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003428
3429 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3430 if (IS_ERR(full_path))
3431 return PTR_ERR(full_path);
3432
3433 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(ctx->UNC, cifs_sb->local_nls,
3434 cifs_remap(cifs_sb));
3435 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3436 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3437 mnt_ctx->origin_fullpath = NULL;
3438 goto out;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003439 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003440
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003441 /* Try all dfs root targets */
3442 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(root_tl);
3443 tit; tit = dfs_cache_get_next_tgt(root_tl, tit)) {
3444 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->origin_fullpath + 1, tit);
3445 if (!rc) {
3446 mnt_ctx->leaf_fullpath = kstrdup(mnt_ctx->origin_fullpath, GFP_KERNEL);
3447 if (!mnt_ctx->leaf_fullpath)
3448 rc = -ENOMEM;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003449 break;
3450 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003451 }
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003452
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003453out:
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003454 kfree(full_path);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003455 return rc;
3456}
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003457
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003458static int __follow_dfs_link(struct mount_ctx *mnt_ctx)
3459{
3460 int rc;
3461 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3462 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3463 char *full_path;
3464 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3465 struct dfs_cache_tgt_iterator *tit;
3466
3467 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3468 if (IS_ERR(full_path))
3469 return PTR_ERR(full_path);
3470
3471 kfree(mnt_ctx->leaf_fullpath);
3472 mnt_ctx->leaf_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3473 cifs_remap(cifs_sb));
3474 if (IS_ERR(mnt_ctx->leaf_fullpath)) {
3475 rc = PTR_ERR(mnt_ctx->leaf_fullpath);
3476 mnt_ctx->leaf_fullpath = NULL;
3477 goto out;
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003478 }
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003479
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003480 /* Get referral from dfs link */
3481 rc = dfs_cache_find(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3482 cifs_remap(cifs_sb), mnt_ctx->leaf_fullpath + 1, NULL, &tl);
3483 if (rc)
3484 goto out;
3485
3486 /* Try all dfs link targets */
3487 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(&tl);
3488 tit; tit = dfs_cache_get_next_tgt(&tl, tit)) {
3489 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->leaf_fullpath + 1, tit);
3490 if (!rc) {
3491 rc = is_path_remote(mnt_ctx);
3492 break;
3493 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003494 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003495
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003496out:
3497 kfree(full_path);
3498 dfs_cache_free_tgts(&tl);
3499 return rc;
3500}
3501
3502static int follow_dfs_link(struct mount_ctx *mnt_ctx)
3503{
3504 int rc;
3505 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3506 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3507 char *full_path;
3508 int num_links = 0;
3509
3510 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3511 if (IS_ERR(full_path))
3512 return PTR_ERR(full_path);
3513
3514 kfree(mnt_ctx->origin_fullpath);
3515 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3516 cifs_remap(cifs_sb));
3517 kfree(full_path);
3518
3519 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3520 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3521 mnt_ctx->origin_fullpath = NULL;
3522 return rc;
3523 }
3524
3525 do {
3526 rc = __follow_dfs_link(mnt_ctx);
3527 if (!rc || rc != -EREMOTE)
3528 break;
3529 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
3530
3531 return rc;
3532}
3533
3534/* Set up DFS referral paths for failover */
3535static void setup_server_referral_paths(struct mount_ctx *mnt_ctx)
3536{
3537 struct TCP_Server_Info *server = mnt_ctx->server;
3538
3539 server->origin_fullpath = mnt_ctx->origin_fullpath;
3540 server->leaf_fullpath = mnt_ctx->leaf_fullpath;
3541 server->current_fullpath = mnt_ctx->leaf_fullpath;
3542 mnt_ctx->origin_fullpath = mnt_ctx->leaf_fullpath = NULL;
3543}
3544
3545int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3546{
3547 int rc;
3548 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3549 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3550 bool isdfs;
3551
3552 rc = is_dfs_mount(&mnt_ctx, &isdfs, &tl);
3553 if (rc)
3554 goto error;
3555 if (!isdfs)
3556 goto out;
3557
3558 uuid_gen(&mnt_ctx.mount_id);
3559 rc = connect_dfs_root(&mnt_ctx, &tl);
3560 dfs_cache_free_tgts(&tl);
3561
3562 if (rc)
3563 goto error;
3564
3565 rc = is_path_remote(&mnt_ctx);
3566 if (rc == -EREMOTE)
3567 rc = follow_dfs_link(&mnt_ctx);
3568 if (rc)
3569 goto error;
3570
3571 setup_server_referral_paths(&mnt_ctx);
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01003572 /*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003573 * After reconnecting to a different server, unique ids won't match anymore, so we disable
3574 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01003575 */
3576 cifs_autodisable_serverino(cifs_sb);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003577 /*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003578 * Force the use of prefix path to support failover on DFS paths that resolve to targets
3579 * that have different prefix paths.
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003580 */
3581 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3582 kfree(cifs_sb->prepath);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003583 cifs_sb->prepath = ctx->prepath;
3584 ctx->prepath = NULL;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003585 uuid_copy(&cifs_sb->dfs_mount_id, &mnt_ctx.mount_id);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003586
Jeff Layton70fe7dc2007-11-16 22:21:07 +00003587out:
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003588 free_xid(mnt_ctx.xid);
3589 cifs_try_adding_channels(cifs_sb, mnt_ctx.ses);
3590 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003591
3592error:
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003593 dfs_cache_put_refsrv_sessions(&mnt_ctx.mount_id);
3594 kfree(mnt_ctx.origin_fullpath);
3595 kfree(mnt_ctx.leaf_fullpath);
3596 mount_put_conns(&mnt_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 return rc;
3598}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003599#else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003600int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003601{
3602 int rc = 0;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003603 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003604
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003605 rc = mount_get_conns(&mnt_ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003606 if (rc)
3607 goto error;
3608
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003609 if (mnt_ctx.tcon) {
3610 rc = is_path_remote(&mnt_ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003611 if (rc == -EREMOTE)
3612 rc = -EOPNOTSUPP;
3613 if (rc)
3614 goto error;
3615 }
3616
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003617 free_xid(mnt_ctx.xid);
3618 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003619
3620error:
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003621 mount_put_conns(&mnt_ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003622 return rc;
3623}
3624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
Jeff Layton8d1bca32011-06-11 21:17:10 -04003626/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01003627 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04003628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003630CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00003631 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 const struct nls_table *nls_codepage)
3633{
3634 struct smb_hdr *smb_buffer;
3635 struct smb_hdr *smb_buffer_response;
3636 TCONX_REQ *pSMB;
3637 TCONX_RSP *pSMBr;
3638 unsigned char *bcc_ptr;
3639 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05003640 int length;
3641 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
3643 if (ses == NULL)
3644 return -EIO;
3645
3646 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00003647 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00003649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 smb_buffer_response = smb_buffer;
3651
3652 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3653 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003654
Pavel Shilovsky88257362012-05-23 14:01:59 +04003655 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 smb_buffer->Uid = ses->Suid;
3657 pSMB = (TCONX_REQ *) smb_buffer;
3658 pSMBr = (TCONX_RSP *) smb_buffer_response;
3659
3660 pSMB->AndXCommand = 0xFF;
3661 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01003663 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08003664 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003665 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003666 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003667 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Jeff Layton38d77c52013-05-26 07:01:00 -04003670 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3672
3673 if (ses->capabilities & CAP_STATUS32) {
3674 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3675 }
3676 if (ses->capabilities & CAP_DFS) {
3677 smb_buffer->Flags2 |= SMBFLG2_DFS;
3678 }
3679 if (ses->capabilities & CAP_UNICODE) {
3680 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3681 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06003682 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00003683 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00003684 (/* server len*/ + 256 /* share len */), nls_codepage);
3685 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 bcc_ptr += 2; /* skip trailing null */
3687 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 strcpy(bcc_ptr, tree);
3689 bcc_ptr += strlen(tree) + 1;
3690 }
3691 strcpy(bcc_ptr, "?????");
3692 bcc_ptr += strlen("?????");
3693 bcc_ptr += 1;
3694 count = bcc_ptr - &pSMB->Password[0];
Qinglang Miao1a0e7f72020-07-25 16:56:01 +08003695 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 pSMB->ByteCount = cpu_to_le16(count);
3697
Steve French133672e2007-11-13 22:41:37 +00003698 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05003699 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003702 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00003703 bool is_unicode;
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00003706 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 tcon->tid = smb_buffer_response->Tid;
3708 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05003709 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003710 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00003711 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3712 is_unicode = true;
3713 else
3714 is_unicode = false;
3715
Jeff Laytoncc20c032009-04-30 07:16:21 -04003716
Steve French50c2f752007-07-13 00:33:32 +00003717 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00003718 if (length == 3) {
3719 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3720 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003721 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01003722 tcon->ipc = true;
3723 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00003724 }
3725 } else if (length == 2) {
3726 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3727 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05003728 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00003729 }
3730 }
Steve French50c2f752007-07-13 00:33:32 +00003731 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04003732 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05003733 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04003734
3735 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04003736 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06003737 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00003738 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04003739 nls_codepage);
3740
Joe Perchesf96637b2013-05-04 22:12:25 -05003741 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003742
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003743 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00003744 (smb_buffer_response->WordCount == 7))
3745 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00003746 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3747 else
3748 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05003749 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 }
3751
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003752 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 return rc;
3754}
3755
Al Viro2e32cf52013-10-03 12:53:37 -04003756static void delayed_free(struct rcu_head *p)
3757{
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003758 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
3759
3760 unload_nls(cifs_sb->local_nls);
Ronnie Sahlbergc741cba2020-12-14 16:40:16 +10003761 smb3_cleanup_fs_context(cifs_sb->ctx);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003762 kfree(cifs_sb);
Al Viro2e32cf52013-10-03 12:53:37 -04003763}
3764
Al Viro2a9b9952011-06-17 09:27:16 -04003765void
3766cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767{
Jeff Laytonb647c352010-10-28 11:16:44 -04003768 struct rb_root *root = &cifs_sb->tlink_tree;
3769 struct rb_node *node;
3770 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Jeff Layton2de970f2010-10-06 19:51:12 -04003772 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3773
Jeff Laytonb647c352010-10-28 11:16:44 -04003774 spin_lock(&cifs_sb->tlink_tree_lock);
3775 while ((node = rb_first(root))) {
3776 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3777 cifs_get_tlink(tlink);
3778 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3779 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00003780
Jeff Laytonb647c352010-10-28 11:16:44 -04003781 spin_unlock(&cifs_sb->tlink_tree_lock);
3782 cifs_put_tlink(tlink);
3783 spin_lock(&cifs_sb->tlink_tree_lock);
3784 }
3785 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003786
Aurelien Aptela6b50582016-05-25 19:59:09 +02003787 kfree(cifs_sb->prepath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003788#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003789 dfs_cache_put_refsrv_sessions(&cifs_sb->dfs_mount_id);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003790#endif
Al Viro2e32cf52013-10-03 12:53:37 -04003791 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00003792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003794int
3795cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
3797 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02003798 struct TCP_Server_Info *server = cifs_ses_server(ses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003800 if (!server->ops->need_neg || !server->ops->negotiate)
3801 return -ENOSYS;
3802
Jeff Layton198b5682010-04-24 07:57:48 -04003803 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003804 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04003805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003807 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04003808 if (rc == 0) {
3809 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04003810 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04003811 server->tcpStatus = CifsGood;
3812 else
3813 rc = -EHOSTDOWN;
3814 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
Steve French26b994f2008-08-06 05:11:33 +00003816
Jeff Layton198b5682010-04-24 07:57:48 -04003817 return rc;
3818}
Steve French26b994f2008-08-06 05:11:33 +00003819
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003820int
3821cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3822 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04003823{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003824 int rc = -ENOSYS;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02003825 struct TCP_Server_Info *server = cifs_ses_server(ses);
Jeff Layton198b5682010-04-24 07:57:48 -04003826
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003827 if (!ses->binding) {
3828 ses->capabilities = server->capabilities;
YANG LIed6b1922021-01-11 17:15:28 +08003829 if (!linuxExtEnabled)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003830 ses->capabilities &= (~server->vals->cap_unix);
3831
3832 if (ses->auth_key.response) {
3833 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
3834 ses->auth_key.response);
3835 kfree(ses->auth_key.response);
3836 ses->auth_key.response = NULL;
3837 ses->auth_key.len = 0;
3838 }
3839 }
Steve French20418ac2009-04-30 16:13:32 +00003840
Joe Perchesf96637b2013-05-04 22:12:25 -05003841 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00003842 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003843
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003844 if (server->ops->sess_setup)
3845 rc = server->ops->sess_setup(xid, ses, nls_info);
3846
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05003847 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003848 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05003849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 return rc;
3851}
3852
Jeff Layton8a8798a2012-01-17 16:09:15 -05003853static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003854cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003855{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003856 ctx->sectype = ses->sectype;
Jeff Layton3f618222013-06-12 19:52:14 -05003857
3858 /* krb5 is special, since we don't need username or pw */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003859 if (ctx->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003860 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003861
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003862 return cifs_set_cifscreds(ctx, ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003863}
3864
Steve French96daf2b2011-05-27 04:34:02 +00003865static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003866cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04003867{
Jeff Layton8a8798a2012-01-17 16:09:15 -05003868 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00003869 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3870 struct cifs_ses *ses;
3871 struct cifs_tcon *tcon = NULL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003872 struct smb3_fs_context *ctx;
Jeff Layton9d002df2010-10-06 19:51:11 -04003873
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003874 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3875 if (ctx == NULL)
Dan Carpenter803ab972012-01-24 11:39:22 +03003876 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04003877
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003878 ctx->local_nls = cifs_sb->local_nls;
3879 ctx->linux_uid = fsuid;
3880 ctx->cred_uid = fsuid;
3881 ctx->UNC = master_tcon->treeName;
3882 ctx->retry = master_tcon->retry;
3883 ctx->nocase = master_tcon->nocase;
3884 ctx->nohandlecache = master_tcon->nohandlecache;
3885 ctx->local_lease = master_tcon->local_lease;
3886 ctx->no_lease = master_tcon->no_lease;
3887 ctx->resilient = master_tcon->use_resilient;
3888 ctx->persistent = master_tcon->use_persistent;
3889 ctx->handle_timeout = master_tcon->handle_timeout;
3890 ctx->no_linux_ext = !master_tcon->unix_ext;
3891 ctx->linux_ext = master_tcon->posix_extensions;
3892 ctx->sectype = master_tcon->ses->sectype;
3893 ctx->sign = master_tcon->ses->sign;
3894 ctx->seal = master_tcon->seal;
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06003895 ctx->witness = master_tcon->use_witness;
Jeff Layton9d002df2010-10-06 19:51:11 -04003896
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003897 rc = cifs_set_vol_auth(ctx, master_tcon->ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003898 if (rc) {
3899 tcon = ERR_PTR(rc);
3900 goto out;
3901 }
Jeff Layton9d002df2010-10-06 19:51:11 -04003902
3903 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303904 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003905 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303906 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003907
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003908 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003909 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00003910 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003911 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04003912 goto out;
3913 }
3914
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003915 tcon = cifs_get_tcon(ses, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003916 if (IS_ERR(tcon)) {
3917 cifs_put_smb_ses(ses);
3918 goto out;
3919 }
3920
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003921 if (cap_unix(ses))
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003922 reset_cifs_unix_caps(0, tcon, NULL, ctx);
Steve Frenchb3266142018-05-20 23:41:10 -05003923
Jeff Layton9d002df2010-10-06 19:51:11 -04003924out:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003925 kfree(ctx->username);
3926 kfree_sensitive(ctx->password);
3927 kfree(ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003928
3929 return tcon;
3930}
3931
Steve French96daf2b2011-05-27 04:34:02 +00003932struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04003933cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
3934{
3935 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
3936}
3937
Jeff Laytonb647c352010-10-28 11:16:44 -04003938/* find and return a tlink with given uid */
3939static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003940tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04003941{
3942 struct rb_node *node = root->rb_node;
3943 struct tcon_link *tlink;
3944
3945 while (node) {
3946 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3947
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003948 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04003949 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003950 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04003951 node = node->rb_right;
3952 else
3953 return tlink;
3954 }
3955 return NULL;
3956}
3957
3958/* insert a tcon_link into the tree */
3959static void
3960tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
3961{
3962 struct rb_node **new = &(root->rb_node), *parent = NULL;
3963 struct tcon_link *tlink;
3964
3965 while (*new) {
3966 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
3967 parent = *new;
3968
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003969 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04003970 new = &((*new)->rb_left);
3971 else
3972 new = &((*new)->rb_right);
3973 }
3974
3975 rb_link_node(&new_tlink->tl_rbnode, parent, new);
3976 rb_insert_color(&new_tlink->tl_rbnode, root);
3977}
3978
Jeff Layton9d002df2010-10-06 19:51:11 -04003979/*
3980 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
3981 * current task.
3982 *
3983 * If the superblock doesn't refer to a multiuser mount, then just return
3984 * the master tcon for the mount.
3985 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05303986 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04003987 * exists, then check to see if it's pending construction. If it is then wait
3988 * for construction to complete. Once it's no longer pending, check to see if
3989 * it failed and either return an error or retry construction, depending on
3990 * the timeout.
3991 *
3992 * If one doesn't exist then insert a new tcon_link struct into the tree and
3993 * try to construct a new one.
3994 */
3995struct tcon_link *
3996cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
3997{
3998 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003999 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004000 struct tcon_link *tlink, *newtlink;
4001
4002 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4003 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4004
4005 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004006 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004007 if (tlink)
4008 cifs_get_tlink(tlink);
4009 spin_unlock(&cifs_sb->tlink_tree_lock);
4010
4011 if (tlink == NULL) {
4012 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4013 if (newtlink == NULL)
4014 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004015 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004016 newtlink->tl_tcon = ERR_PTR(-EACCES);
4017 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4018 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4019 cifs_get_tlink(newtlink);
4020
Jeff Layton9d002df2010-10-06 19:51:11 -04004021 spin_lock(&cifs_sb->tlink_tree_lock);
4022 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004023 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004024 if (tlink) {
4025 cifs_get_tlink(tlink);
4026 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004027 kfree(newtlink);
4028 goto wait_for_construction;
4029 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004030 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004031 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4032 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004033 } else {
4034wait_for_construction:
4035 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004036 TASK_INTERRUPTIBLE);
4037 if (ret) {
4038 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004039 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004040 }
4041
4042 /* if it's good, return it */
4043 if (!IS_ERR(tlink->tl_tcon))
4044 return tlink;
4045
4046 /* return error if we tried this already recently */
4047 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4048 cifs_put_tlink(tlink);
4049 return ERR_PTR(-EACCES);
4050 }
4051
4052 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4053 goto wait_for_construction;
4054 }
4055
4056 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4057 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4058 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4059
4060 if (IS_ERR(tlink->tl_tcon)) {
4061 cifs_put_tlink(tlink);
4062 return ERR_PTR(-EACCES);
4063 }
4064
4065 return tlink;
4066}
Jeff Layton2de970f2010-10-06 19:51:12 -04004067
4068/*
4069 * periodic workqueue job that scans tcon_tree for a superblock and closes
4070 * out tcons.
4071 */
4072static void
4073cifs_prune_tlinks(struct work_struct *work)
4074{
4075 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4076 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004077 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004078 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004079 struct rb_node *tmp;
4080 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004081
Jeff Laytonb647c352010-10-28 11:16:44 -04004082 /*
4083 * Because we drop the spinlock in the loop in order to put the tlink
4084 * it's not guarded against removal of links from the tree. The only
4085 * places that remove entries from the tree are this function and
4086 * umounts. Because this function is non-reentrant and is canceled
4087 * before umount can proceed, this is safe.
4088 */
4089 spin_lock(&cifs_sb->tlink_tree_lock);
4090 node = rb_first(root);
4091 while (node != NULL) {
4092 tmp = node;
4093 node = rb_next(tmp);
4094 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4095
4096 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4097 atomic_read(&tlink->tl_count) != 0 ||
4098 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4099 continue;
4100
4101 cifs_get_tlink(tlink);
4102 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4103 rb_erase(tmp, root);
4104
Jeff Layton2de970f2010-10-06 19:51:12 -04004105 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004106 cifs_put_tlink(tlink);
4107 spin_lock(&cifs_sb->tlink_tree_lock);
4108 }
4109 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004110
Jeff Laytonda472fc2012-03-23 14:40:53 -04004111 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004112 TLINK_IDLE_EXPIRE);
4113}
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004114
4115#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004116/* Update dfs referral path of superblock */
4117static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
4118 const char *target)
4119{
4120 int rc = 0;
4121 size_t len = strlen(target);
4122 char *refpath, *npath;
4123
4124 if (unlikely(len < 2 || *target != '\\'))
4125 return -EINVAL;
4126
4127 if (target[1] == '\\') {
4128 len += 1;
4129 refpath = kmalloc(len, GFP_KERNEL);
4130 if (!refpath)
4131 return -ENOMEM;
4132
4133 scnprintf(refpath, len, "%s", target);
4134 } else {
4135 len += sizeof("\\");
4136 refpath = kmalloc(len, GFP_KERNEL);
4137 if (!refpath)
4138 return -ENOMEM;
4139
4140 scnprintf(refpath, len, "\\%s", target);
4141 }
4142
4143 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
4144 kfree(refpath);
4145
4146 if (IS_ERR(npath)) {
4147 rc = PTR_ERR(npath);
4148 } else {
4149 mutex_lock(&server->refpath_lock);
4150 kfree(server->leaf_fullpath);
4151 server->leaf_fullpath = npath;
4152 mutex_unlock(&server->refpath_lock);
4153 server->current_fullpath = server->leaf_fullpath;
4154 }
4155 return rc;
4156}
4157
4158static int target_share_matches_server(struct TCP_Server_Info *server, const char *tcp_host,
4159 size_t tcp_host_len, char *share, bool *target_match)
4160{
4161 int rc = 0;
4162 const char *dfs_host;
4163 size_t dfs_host_len;
4164
4165 *target_match = true;
4166 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
4167
4168 /* Check if hostnames or addresses match */
4169 if (dfs_host_len != tcp_host_len || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
4170 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
4171 dfs_host, (int)tcp_host_len, tcp_host);
4172 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
4173 if (rc)
4174 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
4175 }
4176 return rc;
4177}
4178
Steve French0e629042021-11-10 03:09:52 -06004179static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004180 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4181 struct dfs_cache_tgt_list *tl)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004182{
4183 int rc;
4184 struct TCP_Server_Info *server = tcon->ses->server;
4185 const struct smb_version_operations *ops = server->ops;
4186 struct cifs_tcon *ipc = tcon->ses->tcon_ipc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004187 char *share = NULL, *prefix = NULL;
4188 const char *tcp_host;
4189 size_t tcp_host_len;
4190 struct dfs_cache_tgt_iterator *tit;
4191 bool target_match;
4192
4193 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
4194
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004195 tit = dfs_cache_get_tgt_iterator(tl);
4196 if (!tit) {
4197 rc = -ENOENT;
4198 goto out;
4199 }
4200
4201 /* Try to tree connect to all dfs targets */
4202 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
4203 const char *target = dfs_cache_get_tgt_name(tit);
4204 struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl);
4205
4206 kfree(share);
4207 kfree(prefix);
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004208 share = prefix = NULL;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004209
4210 /* Check if share matches with tcp ses */
4211 rc = dfs_cache_get_tgt_share(server->current_fullpath + 1, tit, &share, &prefix);
4212 if (rc) {
4213 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
4214 break;
4215 }
4216
4217 rc = target_share_matches_server(server, tcp_host, tcp_host_len, share,
4218 &target_match);
4219 if (rc)
4220 break;
4221 if (!target_match) {
4222 rc = -EHOSTUNREACH;
4223 continue;
4224 }
4225
4226 if (ipc->need_reconnect) {
4227 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4228 rc = ops->tree_connect(xid, ipc->ses, tree, ipc, cifs_sb->local_nls);
4229 if (rc)
4230 break;
4231 }
4232
4233 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
4234 if (!islink) {
4235 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4236 break;
4237 }
4238 /*
4239 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
4240 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
4241 * reconnect so either the demultiplex thread or the echo worker will reconnect to
4242 * newly resolved target.
4243 */
4244 if (dfs_cache_find(xid, tcon->ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004245 NULL, &ntl)) {
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004246 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4247 if (rc)
4248 continue;
4249 rc = dfs_cache_noreq_update_tgthint(server->current_fullpath + 1, tit);
4250 if (!rc)
4251 rc = cifs_update_super_prepath(cifs_sb, prefix);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004252 } else {
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004253 /* Target is another dfs share */
4254 rc = update_server_fullpath(server, cifs_sb, target);
4255 dfs_cache_free_tgts(tl);
4256
4257 if (!rc) {
4258 rc = -EREMOTE;
4259 list_replace_init(&ntl.tl_list, &tl->tl_list);
4260 } else
4261 dfs_cache_free_tgts(&ntl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004262 }
4263 break;
4264 }
4265
4266out:
4267 kfree(share);
4268 kfree(prefix);
4269
4270 return rc;
4271}
4272
Steve French0e629042021-11-10 03:09:52 -06004273static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004274 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4275 struct dfs_cache_tgt_list *tl)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004276{
4277 int rc;
4278 int num_links = 0;
4279 struct TCP_Server_Info *server = tcon->ses->server;
4280
4281 do {
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004282 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004283 if (!rc || rc != -EREMOTE)
4284 break;
4285 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
4286 /*
4287 * If we couldn't tree connect to any targets from last referral path, then retry from
4288 * original referral path.
4289 */
4290 if (rc && server->current_fullpath != server->origin_fullpath) {
4291 server->current_fullpath = server->origin_fullpath;
Paulo Alcantara8ae87bb2021-11-16 13:38:58 -03004292 cifs_ses_mark_for_reconnect(tcon->ses);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004293 }
4294
4295 dfs_cache_free_tgts(tl);
4296 return rc;
4297}
4298
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004299int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4300{
4301 int rc;
4302 struct TCP_Server_Info *server = tcon->ses->server;
4303 const struct smb_version_operations *ops = server->ops;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004304 struct super_block *sb = NULL;
4305 struct cifs_sb_info *cifs_sb;
4306 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004307 char *tree;
Paulo Alcantara11375a52020-07-21 09:36:43 -03004308 struct dfs_info3_param ref = {0};
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004309
4310 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
4311 if (!tree)
4312 return -ENOMEM;
4313
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004314 if (tcon->ipc) {
4315 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4316 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004317 goto out;
4318 }
4319
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004320 sb = cifs_get_tcp_super(server);
4321 if (IS_ERR(sb)) {
4322 rc = PTR_ERR(sb);
4323 cifs_dbg(VFS, "%s: could not find superblock: %d\n", __func__, rc);
4324 goto out;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004325 }
4326
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004327 cifs_sb = CIFS_SB(sb);
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004328
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004329 /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */
4330 if (!server->current_fullpath ||
4331 dfs_cache_noreq_find(server->current_fullpath + 1, &ref, &tl)) {
4332 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, cifs_sb->local_nls);
4333 goto out;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004334 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004335
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004336 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
4337 &tl);
4338 free_dfs_info_param(&ref);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004339
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004340out:
4341 kfree(tree);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004342 cifs_put_tcp_super(sb);
4343
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004344 return rc;
4345}
4346#else
4347int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4348{
4349 const struct smb_version_operations *ops = tcon->ses->server->ops;
4350
4351 return ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4352}
4353#endif