blob: 09a532ed8fe42eb657fef0e758fb9d78cfc9329c [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;
Steve French50c2f752007-07-13 00:33:32 +0000176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400178 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Steve French6e4d3bb2018-09-22 11:25:04 -0500180 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800181 trace_smb3_reconnect(server->CurrentMid, server->conn_id, server->hostname);
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300182 /*
183 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
184 * are not used until reconnected.
185 */
186 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n", __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530187 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300188 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Jeff Layton14fbf502008-11-14 13:53:46 -0500189 ses->need_reconnect = true;
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300190 list_for_each_entry(tcon, &ses->tcon_list, tcon_list)
Jeff Laytonf1987b42008-11-15 11:12:47 -0500191 tcon->need_reconnect = true;
Aurelien Aptelb327a712018-01-24 13:46:10 +0100192 if (ses->tcon_ipc)
193 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530195 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500198 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500199 mutex_lock(&server->srv_mutex);
Long Li1d2a4f52019-05-13 21:01:28 -0700200 if (server->ssocket) {
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300201 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
202 server->ssocket->flags);
Long Li1d2a4f52019-05-13 21:01:28 -0700203 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300204 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
205 server->ssocket->flags);
Long Li1d2a4f52019-05-13 21:01:28 -0700206 sock_release(server->ssocket);
207 server->ssocket = NULL;
208 }
209 server->sequence_number = 0;
210 server->session_estab = false;
211 kfree(server->session_key.response);
212 server->session_key.response = NULL;
213 server->session_key.len = 0;
214 server->lstrp = jiffies;
Long Li214bab42019-04-05 21:36:35 +0000215
216 /* mark submitted MIDs for retry and issue callback */
217 INIT_LIST_HEAD(&retry_list);
218 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
219 spin_lock(&GlobalMid_Lock);
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300220 list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
221 kref_get(&mid->refcount);
222 if (mid->mid_state == MID_REQUEST_SUBMITTED)
223 mid->mid_state = MID_RETRY_NEEDED;
224 list_move(&mid->qhead, &retry_list);
225 mid->mid_flags |= MID_DELETED;
Long Li214bab42019-04-05 21:36:35 +0000226 }
227 spin_unlock(&GlobalMid_Lock);
Long Li1d2a4f52019-05-13 21:01:28 -0700228 mutex_unlock(&server->srv_mutex);
Long Li214bab42019-04-05 21:36:35 +0000229
230 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Paulo Alcantaraae0abb42021-10-14 14:19:17 -0300231 list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
232 list_del_init(&mid->qhead);
233 mid->callback(mid);
234 cifs_mid_q_entry_release(mid);
Long Li214bab42019-04-05 21:36:35 +0000235 }
236
Long Li1d2a4f52019-05-13 21:01:28 -0700237 if (cifs_rdma_enabled(server)) {
238 mutex_lock(&server->srv_mutex);
Long Li050b8c32019-04-04 11:35:42 -0500239 smbd_destroy(server);
Long Li1d2a4f52019-05-13 21:01:28 -0700240 mutex_unlock(&server->srv_mutex);
241 }
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300242}
243
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300244static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
245{
246 spin_lock(&GlobalMid_Lock);
247 server->nr_targets = num_targets;
248 if (server->tcpStatus == CifsExiting) {
249 /* the demux thread will exit normally next time through the loop */
250 spin_unlock(&GlobalMid_Lock);
251 wake_up(&server->response_q);
252 return false;
253 }
254 server->tcpStatus = CifsNeedReconnect;
255 spin_unlock(&GlobalMid_Lock);
256 return true;
257}
258
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300259/*
260 * cifs tcp session reconnection
261 *
262 * mark tcp session as reconnecting so temporarily locked
263 * mark all smb sessions as reconnecting for tcp session
264 * reconnect tcp session
265 * wake up waiters on reconnection? - (not needed currently)
266 */
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300267static int __cifs_reconnect(struct TCP_Server_Info *server)
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300268{
269 int rc = 0;
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300270
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300271 if (!cifs_tcp_ses_needs_reconnect(server, 1))
272 return 0;
Paulo Alcantara43b459a2021-10-14 13:57:21 -0300273
274 cifs_mark_tcp_ses_conns_for_reconnect(server);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400275
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400276 do {
Steve French6c3d8902006-07-31 22:46:20 +0000277 try_to_freeze();
Jeff Layton73e216a2013-09-05 08:38:10 -0400278 mutex_lock(&server->srv_mutex);
Samuel Cabrero121d9472020-11-30 19:02:56 +0100279
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +0200280 if (!cifs_swn_set_server_dstaddr(server)) {
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300281 /* resolve the hostname again to make sure that IP address is up-to-date */
Shyam Prasad N4e456b32021-03-31 14:35:24 +0000282 rc = reconn_set_ipaddr_from_hostname(server);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300283 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
Samuel Cabrero121d9472020-11-30 19:02:56 +0100284 }
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300285
Long Li781a8052017-11-22 17:38:36 -0700286 if (cifs_rdma_enabled(server))
287 rc = smbd_reconnect(server);
288 else
289 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000290 if (rc) {
Federico Sauter4afe2602015-03-17 17:45:28 +0100291 mutex_unlock(&server->srv_mutex);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300292 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
Steve French0cb766a2005-04-28 22:41:11 -0700293 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 } else {
295 atomic_inc(&tcpSesReconnectCount);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800296 set_credits(server, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000298 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000299 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000300 spin_unlock(&GlobalMid_Lock);
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +0200301 cifs_swn_reset_server_dstaddr(server);
Federico Sauter4afe2602015-03-17 17:45:28 +0100302 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400304 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500305
Sachin Prabhub8c60012016-10-20 19:52:24 -0400306 if (server->tcpStatus == CifsNeedNegotiate)
307 mod_delayed_work(cifsiod_wq, &server->echo, 0);
308
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600309 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return rc;
311}
312
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300313#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300314static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
315{
316 int rc;
317 char *hostname;
318
319 if (!cifs_swn_set_server_dstaddr(server)) {
320 if (server->hostname != target) {
321 hostname = extract_hostname(target);
322 if (!IS_ERR(hostname)) {
323 kfree(server->hostname);
324 server->hostname = hostname;
325 } else {
326 cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
327 __func__, PTR_ERR(hostname));
328 cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
329 server->hostname);
330 }
331 }
332 /* resolve the hostname again to make sure that IP address is up-to-date. */
333 rc = reconn_set_ipaddr_from_hostname(server);
334 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
335 }
336 /* Reconnect the socket */
337 if (cifs_rdma_enabled(server))
338 rc = smbd_reconnect(server);
339 else
340 rc = generic_ip_connect(server);
341
342 return rc;
343}
344
345static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
346 struct dfs_cache_tgt_iterator **target_hint)
347{
348 int rc;
349 struct dfs_cache_tgt_iterator *tit;
350
351 *target_hint = NULL;
352
353 /* If dfs target list is empty, then reconnect to last server */
354 tit = dfs_cache_get_tgt_iterator(tl);
355 if (!tit)
356 return __reconnect_target_unlocked(server, server->hostname);
357
358 /* Otherwise, try every dfs target in @tl */
359 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
360 rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
361 if (!rc) {
362 *target_hint = tit;
363 break;
364 }
365 }
366 return rc;
367}
368
369static int reconnect_dfs_server(struct TCP_Server_Info *server)
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300370{
371 int rc = 0;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300372 const char *refpath = server->current_fullpath + 1;
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300373 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300374 struct dfs_cache_tgt_iterator *target_hint = NULL;
375 int num_targets = 0;
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300376
377 /*
378 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
379 *
380 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
381 * targets (server->nr_targets). It's also possible that the cached referral was cleared
382 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
383 * refreshing the referral, so, in this case, default it to 1.
384 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300385 if (!dfs_cache_noreq_find(refpath, NULL, &tl))
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300386 num_targets = dfs_cache_get_nr_tgts(&tl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300387 if (!num_targets)
388 num_targets = 1;
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300389
390 if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
391 return 0;
392
393 cifs_mark_tcp_ses_conns_for_reconnect(server);
394
395 do {
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300396 try_to_freeze();
397 mutex_lock(&server->srv_mutex);
398
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300399 rc = reconnect_target_unlocked(server, &tl, &target_hint);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300400 if (rc) {
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300401 /* Failed to reconnect socket */
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300402 mutex_unlock(&server->srv_mutex);
403 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
404 msleep(3000);
405 continue;
406 }
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300407 /*
408 * Socket was created. Update tcp session status to CifsNeedNegotiate so that a
409 * process waiting for reconnect will know it needs to re-establish session and tcon
410 * through the reconnected target server.
411 */
412 atomic_inc(&tcpSesReconnectCount);
413 set_credits(server, 1);
414 spin_lock(&GlobalMid_Lock);
415 if (server->tcpStatus != CifsExiting)
416 server->tcpStatus = CifsNeedNegotiate;
417 spin_unlock(&GlobalMid_Lock);
418 cifs_swn_reset_server_dstaddr(server);
419 mutex_unlock(&server->srv_mutex);
420 } while (server->tcpStatus == CifsNeedReconnect);
421
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300422 if (target_hint)
423 dfs_cache_noreq_update_tgthint(refpath, target_hint);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300424
425 dfs_cache_free_tgts(&tl);
426
427 /* Need to set up echo worker again once connection has been established */
428 if (server->tcpStatus == CifsNeedNegotiate)
429 mod_delayed_work(cifsiod_wq, &server->echo, 0);
430
431 wake_up(&server->response_q);
432 return rc;
433}
434
435int cifs_reconnect(struct TCP_Server_Info *server)
436{
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300437 /* If tcp session is not an dfs connection, then reconnect to last target server */
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300438 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300439 if (!server->is_dfs_conn || !server->origin_fullpath || !server->leaf_fullpath) {
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300440 spin_unlock(&cifs_tcp_ses_lock);
441 return __cifs_reconnect(server);
442 }
443 spin_unlock(&cifs_tcp_ses_lock);
444
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300445 return reconnect_dfs_server(server);
Paulo Alcantarabbcce362021-10-14 17:49:54 -0300446}
447#else
448int cifs_reconnect(struct TCP_Server_Info *server)
449{
450 return __cifs_reconnect(server);
451}
452#endif
453
Jeff Laytonc74093b2011-01-11 07:24:23 -0500454static void
455cifs_echo_request(struct work_struct *work)
456{
457 int rc;
458 struct TCP_Server_Info *server = container_of(work,
459 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400460
461 /*
462 * We cannot send an echo if it is disabled.
463 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500464 */
Steve French4fcd1812016-06-22 20:12:05 -0500465
466 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400467 server->tcpStatus == CifsExiting ||
468 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400469 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Shyam Prasad N5b2abda2021-05-01 16:17:07 +0000470 time_before(jiffies, server->lstrp + server->echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500471 goto requeue_echo;
472
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400473 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500474 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500475 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
476 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500477
Samuel Cabrero21077c62020-11-30 19:02:54 +0100478 /* Check witness registrations */
479 cifs_swn_check();
Samuel Cabrero21077c62020-11-30 19:02:54 +0100480
Jeff Laytonc74093b2011-01-11 07:24:23 -0500481requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400482 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500483}
484
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400485static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400486allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400487{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400488 if (!server->bigbuf) {
489 server->bigbuf = (char *)cifs_buf_get();
490 if (!server->bigbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000491 cifs_server_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400492 msleep(3000);
493 /* retry will check if exiting */
494 return false;
495 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400496 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400497 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400498 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400499 }
500
Jeff Layton2a37ef92011-10-19 15:29:23 -0400501 if (!server->smallbuf) {
502 server->smallbuf = (char *)cifs_small_buf_get();
503 if (!server->smallbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000504 cifs_server_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400505 msleep(1000);
506 /* retry will check if exiting */
507 return false;
508 }
509 /* beginning of smb buffer is cleared in our buf_get */
510 } else {
511 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400512 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400513 }
514
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400515 return true;
516}
517
Jeff Laytonba749e62011-10-11 06:41:32 -0400518static bool
519server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400520{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300521 /*
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000522 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300523 * situations right:
524 * 1s client sends a normal SMB request
Ronnie Sahlbergbecc2ba2019-07-24 11:43:49 +1000525 * 2s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300526 * 30s echo workqueue job pops, and decides we got a response recently
527 * and don't need to send another
528 * ...
529 * 65s kernel_recvmsg times out, and we see that we haven't gotten
530 * a response in >60s.
531 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200532 if ((server->tcpStatus == CifsGood ||
533 server->tcpStatus == CifsNeedNegotiate) &&
Shyam Prasad Nf4916642021-04-29 07:53:18 +0000534 (!server->ops->can_echo || server->ops->can_echo(server)) &&
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000535 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000536 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
537 (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400538 cifs_reconnect(server);
Jeff Laytonba749e62011-10-11 06:41:32 -0400539 return true;
540 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400541
Jeff Laytonba749e62011-10-11 06:41:32 -0400542 return false;
543}
544
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800545static inline bool
546zero_credits(struct TCP_Server_Info *server)
547{
548 int val;
549
550 spin_lock(&server->req_lock);
551 val = server->credits + server->echo_credits + server->oplock_credits;
552 if (server->in_flight == 0 && val == 0) {
553 spin_unlock(&server->req_lock);
554 return true;
555 }
556 spin_unlock(&server->req_lock);
557 return false;
558}
559
Al Viro71335662016-01-09 19:54:50 -0500560static int
561cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400562{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400563 int length = 0;
564 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400565
Al Viro71335662016-01-09 19:54:50 -0500566 smb_msg->msg_control = NULL;
567 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400568
Al Viro71335662016-01-09 19:54:50 -0500569 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500570 try_to_freeze();
571
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800572 /* reconnect if no credits and no requests in flight */
573 if (zero_credits(server)) {
574 cifs_reconnect(server);
575 return -ECONNABORTED;
576 }
577
Al Viro71335662016-01-09 19:54:50 -0500578 if (server_unresponsive(server))
579 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700580 if (cifs_rdma_enabled(server) && server->smbd_conn)
581 length = smbd_recv(server->smbd_conn, smb_msg);
582 else
583 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500584
585 if (server->tcpStatus == CifsExiting)
586 return -ESHUTDOWN;
587
588 if (server->tcpStatus == CifsNeedReconnect) {
589 cifs_reconnect(server);
590 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400591 }
592
Al Viro71335662016-01-09 19:54:50 -0500593 if (length == -ERESTARTSYS ||
594 length == -EAGAIN ||
595 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400596 /*
597 * Minimum sleep to prevent looping, allowing socket
598 * to clear and app threads to set tcpStatus
599 * CifsNeedReconnect if server hung.
600 */
601 usleep_range(1000, 2000);
602 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400603 continue;
Al Viro71335662016-01-09 19:54:50 -0500604 }
605
606 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500607 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400608 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500609 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400610 }
611 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400612 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400613}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400614
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400615int
616cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
617 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400618{
Al Viro71335662016-01-09 19:54:50 -0500619 struct msghdr smb_msg;
620 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100621 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400622
Al Viro71335662016-01-09 19:54:50 -0500623 return cifs_readv_from_socket(server, &smb_msg);
624}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400625
David Howellscf0604a2021-02-04 00:15:21 -0600626ssize_t
627cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
628{
629 struct msghdr smb_msg;
630
631 /*
632 * iov_iter_discard already sets smb_msg.type and count and iov_offset
633 * and cifs_readv_from_socket sets msg_control and msg_controllen
634 * so little to initialize in struct msghdr
635 */
636 smb_msg.msg_name = NULL;
637 smb_msg.msg_namelen = 0;
638 iov_iter_discard(&smb_msg.msg_iter, READ, to_read);
639
640 return cifs_readv_from_socket(server, &smb_msg);
641}
642
Al Viro71335662016-01-09 19:54:50 -0500643int
644cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700645 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500646{
647 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700648 struct bio_vec bv = {
649 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100650 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500651 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400652}
653
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400654static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400655is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400656{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400657 /*
658 * The first byte big endian of the length field,
659 * is actually not part of the length but the type
660 * with the most common, zero, as regular data.
661 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400662 switch (type) {
663 case RFC1002_SESSION_MESSAGE:
664 /* Regular SMB response */
665 return true;
666 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500667 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400668 break;
669 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500670 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400671 break;
672 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400673 /*
674 * We get this from Windows 98 instead of an error on
675 * SMB negprot response.
676 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500677 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400678 /* give server a second to clean up */
679 msleep(1000);
680 /*
681 * Always try 445 first on reconnect since we get NACK
682 * on some if we ever connected to port 139 (the NACK
683 * is since we do not begin with RFC1001 session
684 * initialize frame).
685 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400686 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400687 cifs_reconnect(server);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400688 break;
689 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000690 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400691 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400692 }
693
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400694 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400695}
696
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400697void
698dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400699{
700#ifdef CONFIG_CIFS_STATS2
701 mid->when_received = jiffies;
702#endif
703 spin_lock(&GlobalMid_Lock);
704 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400705 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400706 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400707 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000708 /*
709 * Trying to handle/dequeue a mid after the send_recv()
710 * function has finished processing it is a bug.
711 */
Steve French9e7ffa72021-11-12 09:55:03 -0600712 if (mid->mid_flags & MID_DELETED) {
713 spin_unlock(&GlobalMid_Lock);
Joe Perchesa0a30362020-04-14 22:42:53 -0700714 pr_warn_once("trying to dequeue a deleted mid\n");
Steve French9e7ffa72021-11-12 09:55:03 -0600715 } else {
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000716 list_del_init(&mid->qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700717 mid->mid_flags |= MID_DELETED;
Steve French9e7ffa72021-11-12 09:55:03 -0600718 spin_unlock(&GlobalMid_Lock);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700719 }
Jeff Laytonea1f4502011-10-19 15:29:05 -0400720}
721
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800722static unsigned int
723smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
724{
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900725 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800726
727 /*
728 * SMB1 does not use credits.
729 */
730 if (server->vals->header_preamble_size)
731 return 0;
732
733 return le16_to_cpu(shdr->CreditRequest);
734}
735
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400736static void
737handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400738 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400739{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400740 if (server->ops->check_trans2 &&
741 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400742 return;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800743 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400744 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400745 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400746 /* Was previous buf put in mpx struct for multi-rsp? */
747 if (!mid->multiRsp) {
748 /* smb buffer will be freed by user thread */
749 if (server->large_buf)
750 server->bigbuf = NULL;
751 else
752 server->smallbuf = NULL;
753 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400754 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400755}
756
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400757static void clean_demultiplex_info(struct TCP_Server_Info *server)
758{
759 int length;
760
761 /* take it off the list, if it's not already */
762 spin_lock(&cifs_tcp_ses_lock);
763 list_del_init(&server->tcp_ses_list);
764 spin_unlock(&cifs_tcp_ses_lock);
765
Paulo Alcantara21225332020-11-28 16:54:02 -0300766 cancel_delayed_work_sync(&server->echo);
Shyam Prasad N506c1da2021-05-18 15:05:50 +0000767 cancel_delayed_work_sync(&server->resolve);
Paulo Alcantara21225332020-11-28 16:54:02 -0300768
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400769 spin_lock(&GlobalMid_Lock);
770 server->tcpStatus = CifsExiting;
771 spin_unlock(&GlobalMid_Lock);
772 wake_up_all(&server->response_q);
773
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400774 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300775 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400776 if (server->credits <= 0)
777 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300778 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400779 /*
780 * Although there should not be any requests blocked on this queue it
781 * can not hurt to be paranoid and try to wake up requests that may
782 * haven been blocked when more than 50 at time were on the wire to the
783 * same server - they now will see the session is in exit state and get
784 * out of SendReceive.
785 */
786 wake_up_all(&server->request_q);
787 /* give those requests time to exit */
788 msleep(125);
Long Li050b8c32019-04-04 11:35:42 -0500789 if (cifs_rdma_enabled(server))
790 smbd_destroy(server);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400791 if (server->ssocket) {
792 sock_release(server->ssocket);
793 server->ssocket = NULL;
794 }
795
796 if (!list_empty(&server->pending_mid_q)) {
797 struct list_head dispose_list;
798 struct mid_q_entry *mid_entry;
799 struct list_head *tmp, *tmp2;
800
801 INIT_LIST_HEAD(&dispose_list);
802 spin_lock(&GlobalMid_Lock);
803 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
804 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Paulo Alcantarabf1bc6942021-03-08 12:00:47 -0300805 cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700806 kref_get(&mid_entry->refcount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400807 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400808 list_move(&mid_entry->qhead, &dispose_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700809 mid_entry->mid_flags |= MID_DELETED;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400810 }
811 spin_unlock(&GlobalMid_Lock);
812
813 /* now walk dispose list and issue callbacks */
814 list_for_each_safe(tmp, tmp2, &dispose_list) {
815 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Paulo Alcantarabf1bc6942021-03-08 12:00:47 -0300816 cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400817 list_del_init(&mid_entry->qhead);
818 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700819 cifs_mid_q_entry_release(mid_entry);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400820 }
821 /* 1/8th of sec is more than enough time for them to exit */
822 msleep(125);
823 }
824
825 if (!list_empty(&server->pending_mid_q)) {
826 /*
827 * mpx threads have not exited yet give them at least the smb
828 * send timeout time for long ops.
829 *
830 * Due to delays on oplock break requests, we need to wait at
831 * least 45 seconds before giving up on a request getting a
832 * response and going ahead and killing cifsd.
833 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500834 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400835 msleep(46000);
836 /*
837 * If threads still have not exited they are probably never
838 * coming home not much else we can do but free the memory.
839 */
840 }
841
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -0300842#ifdef CONFIG_CIFS_DFS_UPCALL
843 kfree(server->origin_fullpath);
844 kfree(server->leaf_fullpath);
845#endif
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400846 kfree(server);
847
848 length = atomic_dec_return(&tcpSesAllocCount);
849 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700850 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400851}
852
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400853static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400854standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
855{
856 int length;
857 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000858 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400859
860 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100861 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
862 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000863 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400864 cifs_reconnect(server);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400865 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400866 }
867
868 /* switch to large buffer if too big for a small one */
869 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
870 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400871 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400872 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400873 }
874
875 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400876 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100877 pdu_length - HEADER_SIZE(server) + 1
878 + server->vals->header_preamble_size);
879
Jeff Laytone9097ab2011-10-19 15:29:40 -0400880 if (length < 0)
881 return length;
882 server->total_read += length;
883
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400884 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400885
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800886 return cifs_handle_standard(server, mid);
887}
888
889int
890cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
891{
892 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
893 int length;
894
Jeff Laytone9097ab2011-10-19 15:29:40 -0400895 /*
896 * We know that we received enough to get to the MID as we
897 * checked the pdu_length earlier. Now check to see
898 * if the rest of the header is OK. We borrow the length
899 * var for the rest of the loop to avoid a new stack var.
900 *
901 * 48 bytes is enough to display the header and a little bit
902 * into the payload for debugging purposes.
903 */
Steve French373512e2015-12-18 13:05:30 -0600904 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400905 if (length != 0)
906 cifs_dump_mem("Bad SMB: ", buf,
907 min_t(unsigned int, server->total_read, 48));
908
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700909 if (server->ops->is_session_expired &&
910 server->ops->is_session_expired(buf)) {
911 cifs_reconnect(server);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700912 return -1;
913 }
914
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700915 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -0800916 server->ops->is_status_pending(buf, server))
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700917 return -1;
918
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500919 if (!mid)
920 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400921
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400922 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500923 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400924}
925
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000926static void
927smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
928{
Ronnie Sahlberg0d35e382021-11-05 08:39:01 +0900929 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800930 int scredits, in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000931
932 /*
933 * SMB1 does not use credits.
934 */
935 if (server->vals->header_preamble_size)
936 return;
937
938 if (shdr->CreditRequest) {
939 spin_lock(&server->req_lock);
940 server->credits += le16_to_cpu(shdr->CreditRequest);
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800941 scredits = server->credits;
942 in_flight = server->in_flight;
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000943 spin_unlock(&server->req_lock);
944 wake_up(&server->request_q);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800945
946 trace_smb3_add_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800947 server->conn_id, server->hostname, scredits,
948 le16_to_cpu(shdr->CreditRequest), in_flight);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800949 cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
950 __func__, le16_to_cpu(shdr->CreditRequest),
951 scredits);
Ronnie Sahlbergeca00452019-02-05 12:56:44 +1000952 }
953}
954
955
Jeff Laytone9097ab2011-10-19 15:29:40 -0400956static int
Al Viro7c97c202011-06-21 08:51:28 -0400957cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000959 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400960 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400961 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000962 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400963 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500964 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000965 struct mid_q_entry *mids[MAX_COMPOUND];
966 char *bufs[MAX_COMPOUND];
Rohith Surabattula8e670f72020-09-18 05:37:28 +0000967 unsigned int noreclaim_flag, num_io_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Eric Biggersdc920272020-03-08 22:58:20 -0700969 noreclaim_flag = memalloc_noreclaim_save();
Joe Perchesf96637b2013-05-04 22:12:25 -0500970 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400971
972 length = atomic_inc_return(&tcpSesAllocCount);
973 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700974 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700976 set_freezable();
Eric W. Biederman33da8e72019-08-16 12:33:54 -0500977 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +0000978 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700979 if (try_to_freeze())
980 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700981
Jeff Layton2a37ef92011-10-19 15:29:23 -0400982 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400983 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700984
Jeff Layton2a37ef92011-10-19 15:29:23 -0400985 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400986 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000987 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000988
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400989 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400990 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000991 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000992
993 if (server->vals->header_preamble_size == 0)
994 server->total_read = 0;
995 else
996 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700997
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400998 /*
999 * The right amount was read from socket - 4 bytes,
1000 * so we can now interpret the length field.
1001 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001002 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -07001003
Joe Perchesf96637b2013-05-04 22:12:25 -05001004 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -04001005 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001006 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001007next_pdu:
1008 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -07001009
Jeff Layton89482a52011-10-19 15:28:57 -04001010 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001011 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001012 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001013 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001014 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001015 cifs_reconnect(server);
Jeff Layton89482a52011-10-19 15:28:57 -04001016 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -07001017 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001018
Jeff Layton89482a52011-10-19 15:28:57 -04001019 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001020 length = cifs_read_from_socket(server,
1021 buf + server->vals->header_preamble_size,
1022 HEADER_SIZE(server) - 1
1023 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001024 if (length < 0)
1025 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001026 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001027
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001028 if (server->ops->next_header) {
1029 next_offset = server->ops->next_header(buf);
1030 if (next_offset)
1031 server->pdu_size = next_offset;
1032 }
1033
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001034 memset(mids, 0, sizeof(mids));
1035 memset(bufs, 0, sizeof(bufs));
1036 num_mids = 0;
1037
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001038 if (server->ops->is_transform_hdr &&
1039 server->ops->receive_transform &&
1040 server->ops->is_transform_hdr(buf)) {
1041 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001042 mids,
1043 bufs,
1044 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001045 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001046 mids[0] = server->ops->find_mid(server, buf);
1047 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001048 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001049
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001050 if (!mids[0] || !mids[0]->receive)
1051 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001052 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001053 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001054 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001055
Lars Persson696e4202018-06-25 14:05:25 +02001056 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001057 for (i = 0; i < num_mids; i++)
1058 if (mids[i])
1059 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001060 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001061 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001062
Rohith Surabattula8e670f72020-09-18 05:37:28 +00001063 if (server->ops->is_status_io_timeout &&
1064 server->ops->is_status_io_timeout(buf)) {
1065 num_io_timeout++;
1066 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
1067 cifs_reconnect(server);
1068 num_io_timeout = 0;
1069 continue;
1070 }
1071 }
1072
Steve Frenchfda35942011-01-20 18:06:34 +00001073 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001074
1075 for (i = 0; i < num_mids; i++) {
1076 if (mids[i] != NULL) {
1077 mids[i]->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001078
Rohith Surabattula9e550b02021-02-16 10:40:45 +00001079 if (bufs[i] && server->ops->is_network_name_deleted)
1080 server->ops->is_network_name_deleted(bufs[i],
1081 server);
1082
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001083 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1084 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001085
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001086 cifs_mid_q_entry_release(mids[i]);
1087 } else if (server->ops->is_oplock_break &&
1088 server->ops->is_oplock_break(bufs[i],
1089 server)) {
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001090 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001091 cifs_dbg(FYI, "Received oplock break\n");
1092 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07001093 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1094 atomic_read(&midCount));
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001095 cifs_dump_mem("Received Data is: ", bufs[i],
1096 HEADER_SIZE(server));
Ronnie Sahlberg3e272572019-07-06 06:43:08 +10001097 smb2_add_credits_from_hdr(bufs[i], server);
Steve French39798772006-05-31 22:40:51 +00001098#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001099 if (server->ops->dump_detail)
1100 server->ops->dump_detail(bufs[i],
1101 server);
1102 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001103#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001104 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001105 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001106
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001107 if (pdu_length > server->pdu_size) {
1108 if (!allocate_buffers(server))
1109 continue;
1110 pdu_length -= server->pdu_size;
1111 server->total_read = 0;
1112 server->large_buf = false;
1113 buf = server->smallbuf;
1114 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001115 }
1116 } /* end while !EXITING */
1117
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001118 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001119 cifs_buf_release(server->bigbuf);
1120 if (server->smallbuf) /* no sense logging a debug message if NULL */
1121 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001123 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001124 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001125
1126 /* if server->tsk was NULL then wait for a signal before exiting */
1127 if (!task_to_wake) {
1128 set_current_state(TASK_INTERRUPTIBLE);
1129 while (!signal_pending(current)) {
1130 schedule();
1131 set_current_state(TASK_INTERRUPTIBLE);
1132 }
1133 set_current_state(TASK_RUNNING);
1134 }
1135
Eric Biggersdc920272020-03-08 22:58:20 -07001136 memalloc_noreclaim_restore(noreclaim_flag);
Jeff Layton0468a2c2008-12-01 07:09:35 -05001137 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
Steve French4c51de12021-09-13 18:29:46 -05001140/*
Steve Frenchbc044992020-12-11 19:48:26 -06001141 * Returns true if srcaddr isn't specified and rhs isn't specified, or
1142 * if srcaddr is specified and matches the IP address of the rhs argument
Ben Greear3eb9a882010-09-01 17:06:02 -07001143 */
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001144bool
1145cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
Ben Greear3eb9a882010-09-01 17:06:02 -07001146{
1147 switch (srcaddr->sa_family) {
1148 case AF_UNSPEC:
1149 return (rhs->sa_family == AF_UNSPEC);
1150 case AF_INET: {
1151 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1152 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1153 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1154 }
1155 case AF_INET6: {
1156 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05001157 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07001158 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1159 }
1160 default:
1161 WARN_ON(1);
1162 return false; /* don't expect to be here */
1163 }
1164}
1165
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001166/*
1167 * If no port is specified in addr structure, we try to match with 445 port
1168 * and if it fails - with 139 ports. It should be called only if address
1169 * families of server and addr are equal.
1170 */
1171static bool
1172match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1173{
Steve French6da97912011-03-13 18:55:55 +00001174 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001175
Long Li3b249112019-05-15 14:09:04 -07001176 /* SMBDirect manages its own ports, don't match it here */
1177 if (server->rdma)
1178 return true;
1179
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001180 switch (addr->sa_family) {
1181 case AF_INET:
1182 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1183 port = ((struct sockaddr_in *) addr)->sin_port;
1184 break;
1185 case AF_INET6:
1186 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1187 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1188 break;
1189 default:
1190 WARN_ON(1);
1191 return false;
1192 }
1193
1194 if (!port) {
1195 port = htons(CIFS_PORT);
1196 if (port == *sport)
1197 return true;
1198
1199 port = htons(RFC1001_PORT);
1200 }
1201
1202 return port == *sport;
1203}
Ben Greear3eb9a882010-09-01 17:06:02 -07001204
1205static bool
1206match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1207 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Jeff Layton45151482010-07-06 20:43:02 -04001209 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001210 case AF_INET: {
1211 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1212 struct sockaddr_in *srv_addr4 =
1213 (struct sockaddr_in *)&server->dstaddr;
1214
1215 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04001216 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001217 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001218 }
1219 case AF_INET6: {
1220 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1221 struct sockaddr_in6 *srv_addr6 =
1222 (struct sockaddr_in6 *)&server->dstaddr;
1223
Jeff Layton45151482010-07-06 20:43:02 -04001224 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001225 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04001226 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001227 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04001228 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001229 break;
1230 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001231 default:
1232 WARN_ON(1);
1233 return false; /* don't expect to be here */
1234 }
Jeff Layton45151482010-07-06 20:43:02 -04001235
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03001236 if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
Ben Greear3eb9a882010-09-01 17:06:02 -07001237 return false;
1238
Jeff Layton45151482010-07-06 20:43:02 -04001239 return true;
1240}
1241
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001242static bool
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001243match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001244{
Jeff Layton3f618222013-06-12 19:52:14 -05001245 /*
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001246 * The select_sectype function should either return the ctx->sectype
Jeff Layton3f618222013-06-12 19:52:14 -05001247 * that was specified, or "Unspecified" if that sectype was not
1248 * compatible with the given NEGOTIATE request.
1249 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001250 if (server->ops->select_sectype(server, ctx->sectype)
Sachin Prabhuef65aae2017-01-18 15:35:57 +05301251 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001252 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001253
Jeff Layton3f618222013-06-12 19:52:14 -05001254 /*
1255 * Now check if signing mode is acceptable. No need to check
1256 * global_secflags at this point since if MUST_SIGN is set then
1257 * the server->sign had better be too.
1258 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001259 if (ctx->sign && !server->sign)
Jeff Layton38d77c52013-05-26 07:01:00 -04001260 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001261
1262 return true;
1263}
1264
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001265static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001266{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001267 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
Jeff Layton9fa114f2012-11-26 11:09:57 -05001268
Shyam Prasad Nc9f1c192021-11-06 11:31:53 +00001269 if (ctx->nosharesock) {
1270 server->nosharesock = true;
1271 return 0;
1272 }
1273
1274 /* this server does not share socket */
1275 if (server->nosharesock)
Jeff Laytona0b3df52013-05-24 07:40:59 -04001276 return 0;
1277
Steve French43cdae82019-06-13 14:26:49 -05001278 /* If multidialect negotiation see if existing sessions match one */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001279 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
Steve French43cdae82019-06-13 14:26:49 -05001280 if (server->vals->protocol_id < SMB30_PROT_ID)
1281 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001282 } else if (strcmp(ctx->vals->version_string,
Steve French43cdae82019-06-13 14:26:49 -05001283 SMBDEFAULT_VERSION_STRING) == 0) {
1284 if (server->vals->protocol_id < SMB21_PROT_ID)
1285 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001286 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
Jeff Layton23db65f2012-05-15 12:20:51 -04001287 return 0;
1288
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001289 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1290 return 0;
1291
Shyam Prasad N7be32482021-10-14 11:52:39 +00001292 if (strcasecmp(server->hostname, ctx->server_hostname))
1293 return 0;
1294
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001295 if (!match_address(server, addr,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001296 (struct sockaddr *)&ctx->srcaddr))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001297 return 0;
1298
1299 if (!match_port(server, addr))
1300 return 0;
1301
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001302 if (!match_security(server, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001303 return 0;
1304
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001305 if (server->echo_interval != ctx->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001306 return 0;
1307
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001308 if (server->rdma != ctx->rdma)
Long Li8339dd32017-11-07 01:54:55 -07001309 return 0;
1310
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001311 if (server->ignore_signature != ctx->ignore_signature)
Steve French4f5c10f2019-09-03 21:18:49 -05001312 return 0;
1313
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001314 if (server->min_offload != ctx->min_offload)
Steve French563317e2019-09-08 23:22:02 -05001315 return 0;
1316
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001317 return 1;
1318}
1319
Paulo Alcantara54be1f62018-11-14 16:01:21 -02001320struct TCP_Server_Info *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001321cifs_find_tcp_session(struct smb3_fs_context *ctx)
Jeff Layton45151482010-07-06 20:43:02 -04001322{
Jeff Laytone7ddee92008-11-14 13:44:38 -05001323 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301325 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04001326 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Paulo Alcantaracdc33632021-07-15 21:53:53 -03001327#ifdef CONFIG_CIFS_DFS_UPCALL
1328 /*
1329 * DFS failover implementation in cifs_reconnect() requires unique tcp sessions for
1330 * DFS connections to do failover properly, so avoid sharing them with regular
1331 * shares or even links that may connect to same server but having completely
1332 * different failover targets.
1333 */
1334 if (server->is_dfs_conn)
1335 continue;
1336#endif
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -03001337 /*
1338 * Skip ses channels since they're only handled in lower layers
1339 * (e.g. cifs_send_recv).
1340 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001341 if (server->is_channel || !match_server(server, ctx))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001342 continue;
1343
Jeff Laytone7ddee92008-11-14 13:44:38 -05001344 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301345 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05001346 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05001347 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301349 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return NULL;
1351}
1352
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001353void
1354cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001356 struct task_struct *task;
1357
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301358 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001359 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301360 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001361 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Steve Frenchdea570e02008-05-06 22:05:51 +00001363
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05001364 /* srv_count can never go negative */
1365 WARN_ON(server->srv_count < 0);
1366
Rob Landleyf1d0c992011-01-22 15:44:05 -06001367 put_net(cifs_net_ns(server));
1368
Jeff Laytone7ddee92008-11-14 13:44:38 -05001369 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301370 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05001371
Jeff Laytonc74093b2011-01-11 07:24:23 -05001372 cancel_delayed_work_sync(&server->echo);
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001373 cancel_delayed_work_sync(&server->resolve);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001374
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001375 if (from_reconnect)
1376 /*
1377 * Avoid deadlock here: reconnect work calls
1378 * cifs_put_tcp_session() at its end. Need to be sure
1379 * that reconnect work does nothing with server pointer after
1380 * that step.
1381 */
1382 cancel_delayed_work(&server->reconnect);
1383 else
1384 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001385
Jeff Laytone7ddee92008-11-14 13:44:38 -05001386 spin_lock(&GlobalMid_Lock);
1387 server->tcpStatus = CifsExiting;
1388 spin_unlock(&GlobalMid_Lock);
1389
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001390 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05301391 cifs_fscache_release_client_cookie(server);
1392
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05001393 kfree(server->session_key.response);
1394 server->session_key.response = NULL;
1395 server->session_key.len = 0;
Shyam Prasad N7be32482021-10-14 11:52:39 +00001396 kfree(server->hostname);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001397
1398 task = xchg(&server->tsk, NULL);
1399 if (task)
Eric W. Biederman72abe3b2019-05-15 12:33:50 -05001400 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001403struct TCP_Server_Info *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001404cifs_get_tcp_session(struct smb3_fs_context *ctx)
Jeff Layton63c038c2008-12-01 18:41:46 -05001405{
1406 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05001407 int rc;
1408
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001409 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05001410
1411 /* see if we already have a matching tcp_ses */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001412 tcp_ses = cifs_find_tcp_session(ctx);
Jeff Layton63c038c2008-12-01 18:41:46 -05001413 if (tcp_ses)
1414 return tcp_ses;
1415
1416 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1417 if (!tcp_ses) {
1418 rc = -ENOMEM;
1419 goto out_err;
1420 }
1421
Shyam Prasad N7be32482021-10-14 11:52:39 +00001422 tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
1423 if (!tcp_ses->hostname) {
1424 rc = -ENOMEM;
1425 goto out_err;
1426 }
1427
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001428 tcp_ses->ops = ctx->ops;
1429 tcp_ses->vals = ctx->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06001430 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05001431
Shyam Prasad N6d82c272021-02-03 23:20:46 -08001432 tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001433 tcp_ses->noblockcnt = ctx->rootfs;
1434 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1435 tcp_ses->noautotune = ctx->noautotune;
1436 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1437 tcp_ses->rdma = ctx->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03001438 tcp_ses->in_flight = 0;
Steve French1b63f182019-09-09 22:57:11 -05001439 tcp_ses->max_in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04001440 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05001441 init_waitqueue_head(&tcp_ses->response_q);
1442 init_waitqueue_head(&tcp_ses->request_q);
1443 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1444 mutex_init(&tcp_ses->srv_mutex);
1445 memcpy(tcp_ses->workstation_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001446 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Jeff Layton63c038c2008-12-01 18:41:46 -05001447 memcpy(tcp_ses->server_RFC1001_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001448 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05001449 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05001450 tcp_ses->sequence_number = 0;
Pavel Shilovsky5b964852019-01-18 11:30:26 -08001451 tcp_ses->reconnect_instance = 1;
Steve Frenchfda35942011-01-20 18:06:34 +00001452 tcp_ses->lstrp = jiffies;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001453 tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
Jeff Layton58fa0152012-05-01 17:41:16 -04001454 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001455 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1456 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001457 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001458 INIT_DELAYED_WORK(&tcp_ses->resolve, cifs_resolve_server);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001459 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1460 mutex_init(&tcp_ses->reconnect_mutex);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03001461#ifdef CONFIG_CIFS_DFS_UPCALL
1462 mutex_init(&tcp_ses->refpath_lock);
1463#endif
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001464 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001465 sizeof(tcp_ses->srcaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001466 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
Jeff Layton9fa114f2012-11-26 11:09:57 -05001467 sizeof(tcp_ses->dstaddr));
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001468 if (ctx->use_client_guid)
1469 memcpy(tcp_ses->client_guid, ctx->client_guid,
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001470 SMB2_CLIENT_GUID_SIZE);
1471 else
1472 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05001473 /*
1474 * at this point we are the only ones with the pointer
1475 * to the struct since the kernel thread not created yet
1476 * no need to spinlock this init of tcpStatus or srv_count
1477 */
1478 tcp_ses->tcpStatus = CifsNew;
1479 ++tcp_ses->srv_count;
1480
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001481 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1482 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1483 tcp_ses->echo_interval = ctx->echo_interval * HZ;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001484 else
1485 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07001486 if (tcp_ses->rdma) {
1487#ifndef CONFIG_CIFS_SMB_DIRECT
1488 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1489 rc = -ENOENT;
1490 goto out_err_crypto_release;
1491#endif
1492 tcp_ses->smbd_conn = smbd_get_connection(
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001493 tcp_ses, (struct sockaddr *)&ctx->dstaddr);
Long Li2f894642017-11-22 17:38:34 -07001494 if (tcp_ses->smbd_conn) {
1495 cifs_dbg(VFS, "RDMA transport established\n");
1496 rc = 0;
1497 goto smbd_connected;
1498 } else {
1499 rc = -ENOENT;
1500 goto out_err_crypto_release;
1501 }
1502 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001503 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05001504 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001505 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001506 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001507 }
Long Li2f894642017-11-22 17:38:34 -07001508smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05001509 /*
1510 * since we're in a cifs function already, we know that
1511 * this will succeed. No need for try_module_get().
1512 */
1513 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04001514 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05001515 tcp_ses, "cifsd");
1516 if (IS_ERR(tcp_ses->tsk)) {
1517 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05001518 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05001519 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001520 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05001521 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001522 tcp_ses->min_offload = ctx->min_offload;
Steve French01cf3082021-07-01 12:22:47 -05001523 /*
1524 * at this point we are the only ones with the pointer
1525 * to the struct since the kernel thread not created yet
1526 * no need to spinlock this update of tcpStatus
1527 */
Steve Frenchfd88ce92011-04-12 01:01:14 +00001528 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05001529
Aurelien Aptela249cc82021-03-04 17:42:21 +00001530 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
1531 tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
1532 else
1533 tcp_ses->max_credits = ctx->max_credits;
1534
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02001535 tcp_ses->nr_targets = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001536 tcp_ses->ignore_signature = ctx->ignore_signature;
Jeff Layton63c038c2008-12-01 18:41:46 -05001537 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301538 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001539 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301540 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05001541
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05301542 cifs_fscache_get_client_cookie(tcp_ses);
1543
Jeff Laytonc74093b2011-01-11 07:24:23 -05001544 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001545 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05001546
Shyam Prasad N506c1da2021-05-18 15:05:50 +00001547 /* queue dns resolution delayed work */
1548 cifs_dbg(FYI, "%s: next dns resolution scheduled for %d seconds in the future\n",
1549 __func__, SMB_DNS_RESOLVE_INTERVAL_DEFAULT);
1550
1551 queue_delayed_work(cifsiod_wq, &tcp_ses->resolve, (SMB_DNS_RESOLVE_INTERVAL_DEFAULT * HZ));
1552
Jeff Layton63c038c2008-12-01 18:41:46 -05001553 return tcp_ses;
1554
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05001555out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001556 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05001557
Rob Landleyf1d0c992011-01-22 15:44:05 -06001558 put_net(cifs_net_ns(tcp_ses));
1559
Jeff Layton63c038c2008-12-01 18:41:46 -05001560out_err:
1561 if (tcp_ses) {
Shyam Prasad N7be32482021-10-14 11:52:39 +00001562 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05001563 if (tcp_ses->ssocket)
1564 sock_release(tcp_ses->ssocket);
1565 kfree(tcp_ses);
1566 }
1567 return ERR_PTR(rc);
1568}
1569
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001570static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001571{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001572 if (ctx->sectype != Unspecified &&
1573 ctx->sectype != ses->sectype)
Jeff Layton3f618222013-06-12 19:52:14 -05001574 return 0;
1575
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001576 /*
1577 * If an existing session is limited to less channels than
1578 * requested, it should not be reused
1579 */
Shyam Prasad N724244c2021-07-19 10:54:46 +00001580 spin_lock(&ses->chan_lock);
1581 if (ses->chan_max < ctx->max_channels) {
1582 spin_unlock(&ses->chan_lock);
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001583 return 0;
Shyam Prasad N724244c2021-07-19 10:54:46 +00001584 }
1585 spin_unlock(&ses->chan_lock);
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001586
Jeff Layton3f618222013-06-12 19:52:14 -05001587 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001588 case Kerberos:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001589 if (!uid_eq(ctx->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001590 return 0;
1591 break;
1592 default:
Jeff Layton04febab2012-01-17 16:09:15 -05001593 /* NULL username means anonymous session */
1594 if (ses->user_name == NULL) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001595 if (!ctx->nullauth)
Jeff Layton04febab2012-01-17 16:09:15 -05001596 return 0;
1597 break;
1598 }
1599
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001600 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05001601 if (strncmp(ses->user_name,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001602 ctx->username ? ctx->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001603 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001604 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001605 if ((ctx->username && strlen(ctx->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001606 ses->password != NULL &&
1607 strncmp(ses->password,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001608 ctx->password ? ctx->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001609 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001610 return 0;
1611 }
1612 return 1;
1613}
1614
Aurelien Aptelb327a712018-01-24 13:46:10 +01001615/**
1616 * cifs_setup_ipc - helper to setup the IPC tcon for the session
Steve French4c51de12021-09-13 18:29:46 -05001617 * @ses: smb session to issue the request on
1618 * @ctx: the superblock configuration context to use for building the
1619 * new tree connection for the IPC (interprocess communication RPC)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001620 *
1621 * A new IPC connection is made and stored in the session
1622 * tcon_ipc. The IPC tcon has the same lifetime as the session.
1623 */
1624static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001625cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001626{
1627 int rc = 0, xid;
1628 struct cifs_tcon *tcon;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001629 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1630 bool seal = false;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001631 struct TCP_Server_Info *server = ses->server;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001632
1633 /*
1634 * If the mount request that resulted in the creation of the
1635 * session requires encryption, force IPC to be encrypted too.
1636 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001637 if (ctx->seal) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001638 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
Aurelien Aptelb327a712018-01-24 13:46:10 +01001639 seal = true;
1640 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001641 cifs_server_dbg(VFS,
Aurelien Aptelb327a712018-01-24 13:46:10 +01001642 "IPC: server doesn't support encryption\n");
1643 return -EOPNOTSUPP;
1644 }
1645 }
1646
1647 tcon = tconInfoAlloc();
1648 if (tcon == NULL)
1649 return -ENOMEM;
1650
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001651 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001652
Aurelien Aptelb327a712018-01-24 13:46:10 +01001653 xid = get_xid();
1654 tcon->ses = ses;
1655 tcon->ipc = true;
1656 tcon->seal = seal;
Ronnie Sahlberg6fd4ea82020-12-14 16:40:21 +10001657 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001658 free_xid(xid);
1659
1660 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001661 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
Aurelien Aptelb327a712018-01-24 13:46:10 +01001662 tconInfoFree(tcon);
1663 goto out;
1664 }
1665
1666 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
1667
1668 ses->tcon_ipc = tcon;
1669out:
Aurelien Aptelb327a712018-01-24 13:46:10 +01001670 return rc;
1671}
1672
1673/**
1674 * cifs_free_ipc - helper to release the session IPC tcon
Steve French4c51de12021-09-13 18:29:46 -05001675 * @ses: smb session to unmount the IPC from
Aurelien Aptelb327a712018-01-24 13:46:10 +01001676 *
Paulo Alcantaraf3191fc2021-06-04 19:25:27 -03001677 * Needs to be called everytime a session is destroyed.
1678 *
1679 * On session close, the IPC is closed and the server must release all tcons of the session.
1680 * No need to send a tree disconnect here.
1681 *
1682 * Besides, it will make the server to not close durable and resilient files on session close, as
1683 * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
Aurelien Aptelb327a712018-01-24 13:46:10 +01001684 */
1685static int
1686cifs_free_ipc(struct cifs_ses *ses)
1687{
Aurelien Aptelb327a712018-01-24 13:46:10 +01001688 struct cifs_tcon *tcon = ses->tcon_ipc;
1689
1690 if (tcon == NULL)
1691 return 0;
1692
Aurelien Aptelb327a712018-01-24 13:46:10 +01001693 tconInfoFree(tcon);
1694 ses->tcon_ipc = NULL;
Paulo Alcantaraf3191fc2021-06-04 19:25:27 -03001695 return 0;
Aurelien Aptelb327a712018-01-24 13:46:10 +01001696}
1697
Steve French96daf2b2011-05-27 04:34:02 +00001698static struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001699cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Steve French96daf2b2011-05-27 04:34:02 +00001701 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301703 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04001704 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001705 if (ses->status == CifsExiting)
1706 continue;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001707 if (!match_session(ses, ctx))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001708 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05001709 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301710 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001711 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301713 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return NULL;
1715}
1716
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001717void cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05001718{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001719 unsigned int rc, xid;
Shyam Prasad N724244c2021-07-19 10:54:46 +00001720 unsigned int chan_count;
Jeff Layton14fbf502008-11-14 13:53:46 -05001721 struct TCP_Server_Info *server = ses->server;
Joe Perchesf96637b2013-05-04 22:12:25 -05001722 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001723
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301724 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001725 if (ses->status == CifsExiting) {
1726 spin_unlock(&cifs_tcp_ses_lock);
1727 return;
1728 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03001729
1730 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1731 cifs_dbg(FYI, "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->treeName : "NONE");
1732
Jeff Layton14fbf502008-11-14 13:53:46 -05001733 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301734 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001735 return;
1736 }
Steve French0060a4f2021-06-24 15:28:04 -05001737 spin_unlock(&cifs_tcp_ses_lock);
1738
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05001739 /* ses_count can never go negative */
1740 WARN_ON(ses->ses_count < 0);
1741
Steve French0060a4f2021-06-24 15:28:04 -05001742 spin_lock(&GlobalMid_Lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001743 if (ses->status == CifsGood)
1744 ses->status = CifsExiting;
Steve French0060a4f2021-06-24 15:28:04 -05001745 spin_unlock(&GlobalMid_Lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001746
Aurelien Aptelb327a712018-01-24 13:46:10 +01001747 cifs_free_ipc(ses);
1748
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001749 if (ses->status == CifsExiting && server->ops->logoff) {
1750 xid = get_xid();
1751 rc = server->ops->logoff(xid, ses);
1752 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001753 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05001754 __func__, rc);
1755 _free_xid(xid);
1756 }
1757
1758 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001759 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05301760 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05001761
Shyam Prasad N724244c2021-07-19 10:54:46 +00001762 spin_lock(&ses->chan_lock);
1763 chan_count = ses->chan_count;
1764 spin_unlock(&ses->chan_lock);
1765
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001766 /* close any extra channels */
Shyam Prasad N724244c2021-07-19 10:54:46 +00001767 if (chan_count > 1) {
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001768 int i;
1769
Shyam Prasad N724244c2021-07-19 10:54:46 +00001770 for (i = 1; i < chan_count; i++) {
1771 /*
1772 * note: for now, we're okay accessing ses->chans
1773 * without chan_lock. But when chans can go away, we'll
1774 * need to introduce ref counting to make sure that chan
1775 * is not freed from under us.
1776 */
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001777 cifs_put_tcp_session(ses->chans[i].server, 0);
Shyam Prasad N724244c2021-07-19 10:54:46 +00001778 ses->chans[i].server = NULL;
1779 }
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02001780 }
1781
Jeff Layton14fbf502008-11-14 13:53:46 -05001782 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001783 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05001784}
1785
Jeff Layton8a8798a2012-01-17 16:09:15 -05001786#ifdef CONFIG_KEYS
1787
Chen Gang057d6332013-07-19 09:01:36 +08001788/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
1789#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001790
1791/* Populate username and pw fields from keyring if possible */
1792static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001793cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05001794{
1795 int rc = 0;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001796 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01001797 const char *delim, *payload;
1798 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001799 ssize_t len;
1800 struct key *key;
1801 struct TCP_Server_Info *server = ses->server;
1802 struct sockaddr_in *sa;
1803 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01001804 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001805
1806 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
1807 if (!desc)
1808 return -ENOMEM;
1809
1810 /* try to find an address key first */
1811 switch (server->dstaddr.ss_family) {
1812 case AF_INET:
1813 sa = (struct sockaddr_in *)&server->dstaddr;
1814 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
1815 break;
1816 case AF_INET6:
1817 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
1818 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
1819 break;
1820 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001821 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
1822 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001823 rc = -EINVAL;
1824 goto out_err;
1825 }
1826
Joe Perchesf96637b2013-05-04 22:12:25 -05001827 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001828 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001829 if (IS_ERR(key)) {
1830 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001831 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001832 rc = PTR_ERR(key);
1833 goto out_err;
1834 }
1835
1836 /* didn't work, try to find a domain key */
1837 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05001838 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07001839 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05001840 if (IS_ERR(key)) {
1841 rc = PTR_ERR(key);
1842 goto out_err;
1843 }
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001844 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001845 }
1846
1847 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00001848 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001849 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05001850 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001851 goto out_key_put;
1852 }
1853
1854 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01001855 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001856 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05001857 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001858 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001859 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
1860 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001861 rc = -EINVAL;
1862 goto out_key_put;
1863 }
1864
1865 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001866 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001867 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
1868 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001869 rc = -EINVAL;
1870 goto out_key_put;
1871 }
1872
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001873 ctx->username = kstrndup(payload, len, GFP_KERNEL);
1874 if (!ctx->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001875 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
1876 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001877 rc = -ENOMEM;
1878 goto out_key_put;
1879 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001880 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001881
1882 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001883 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001884 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001885 rc = -EINVAL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001886 kfree(ctx->username);
1887 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001888 goto out_key_put;
1889 }
1890
1891 ++delim;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001892 ctx->password = kstrndup(delim, len, GFP_KERNEL);
1893 if (!ctx->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001894 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
1895 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001896 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001897 kfree(ctx->username);
1898 ctx->username = NULL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05001899 goto out_key_put;
1900 }
1901
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001902 /*
1903 * If we have a domain key then we must set the domainName in the
1904 * for the request.
1905 */
1906 if (is_domain && ses->domainName) {
Al Viro8d767222021-03-05 15:02:34 -05001907 ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001908 if (!ctx->domainname) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001909 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
1910 len);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001911 rc = -ENOMEM;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001912 kfree(ctx->username);
1913 ctx->username = NULL;
1914 kfree_sensitive(ctx->password);
1915 ctx->password = NULL;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10001916 goto out_key_put;
1917 }
1918 }
1919
Jeff Layton8a8798a2012-01-17 16:09:15 -05001920out_key_put:
1921 up_read(&key->sem);
1922 key_put(key);
1923out_err:
1924 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05001925 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05001926 return rc;
1927}
1928#else /* ! CONFIG_KEYS */
1929static inline int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001930cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
Jeff Layton8a8798a2012-01-17 16:09:15 -05001931 struct cifs_ses *ses __attribute__((unused)))
1932{
1933 return -ENOSYS;
1934}
1935#endif /* CONFIG_KEYS */
1936
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01001937/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001938 * cifs_get_smb_ses - get a session matching @ctx data from @server
Steve French4c51de12021-09-13 18:29:46 -05001939 * @server: server to setup the session to
1940 * @ctx: superblock configuration context to use to setup the session
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01001941 *
1942 * This function assumes it is being called from cifs_mount() where we
1943 * already got a server reference (server refcount +1). See
1944 * cifs_get_tcon() for refcount explanations.
1945 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03001946struct cifs_ses *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001947cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
Jeff Layton36988c72010-04-24 07:57:43 -04001948{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04001949 int rc = -ENOMEM;
1950 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00001951 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001952 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
1953 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04001954
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001955 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04001956
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001957 ses = cifs_find_smb_ses(server, ctx);
Jeff Layton36988c72010-04-24 07:57:43 -04001958 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001959 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
1960 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04001961
Jeff Layton36988c72010-04-24 07:57:43 -04001962 mutex_lock(&ses->session_mutex);
1963 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001964 cifs_dbg(FYI, "Session needs reconnect\n");
Shyam Prasad N8e077572021-07-19 10:03:38 +00001965
1966 rc = cifs_negotiate_protocol(xid, ses);
1967 if (rc) {
1968 mutex_unlock(&ses->session_mutex);
1969 /* problem -- put our ses reference */
1970 cifs_put_smb_ses(ses);
1971 free_xid(xid);
1972 return ERR_PTR(rc);
1973 }
1974
Jeff Layton36988c72010-04-24 07:57:43 -04001975 rc = cifs_setup_session(xid, ses,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06001976 ctx->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04001977 if (rc) {
1978 mutex_unlock(&ses->session_mutex);
1979 /* problem -- put our reference */
1980 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001981 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04001982 return ERR_PTR(rc);
1983 }
1984 }
1985 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04001986
1987 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07001988 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001989 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04001990 return ses;
1991 }
1992
Joe Perchesf96637b2013-05-04 22:12:25 -05001993 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04001994 ses = sesInfoAlloc();
1995 if (ses == NULL)
1996 goto get_ses_fail;
1997
1998 /* new SMB session uses our server ref */
1999 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002000 if (server->dstaddr.ss_family == AF_INET6)
Steve Frenchb438fcf2021-02-20 19:24:11 -06002001 sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002002 else
Steve Frenchb438fcf2021-02-20 19:24:11 -06002003 sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002004
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002005 if (ctx->username) {
2006 ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
Steve French8727c8a2011-02-25 01:11:56 -06002007 if (!ses->user_name)
2008 goto get_ses_fail;
2009 }
Jeff Layton36988c72010-04-24 07:57:43 -04002010
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002011 /* ctx->password freed at unmount */
2012 if (ctx->password) {
2013 ses->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Layton36988c72010-04-24 07:57:43 -04002014 if (!ses->password)
2015 goto get_ses_fail;
2016 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002017 if (ctx->domainname) {
2018 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002019 if (!ses->domainName)
2020 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002021 }
Shyam Prasad N49bd49f2021-11-05 19:03:57 +00002022 if (ctx->workstation_name) {
2023 ses->workstation_name = kstrdup(ctx->workstation_name,
2024 GFP_KERNEL);
2025 if (!ses->workstation_name)
2026 goto get_ses_fail;
2027 }
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002028 if (ctx->domainauto)
2029 ses->domainAuto = ctx->domainauto;
2030 ses->cred_uid = ctx->cred_uid;
2031 ses->linux_uid = ctx->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002032
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002033 ses->sectype = ctx->sectype;
2034 ses->sign = ctx->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002035 mutex_lock(&ses->session_mutex);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002036
2037 /* add server as first channel */
Shyam Prasad N724244c2021-07-19 10:54:46 +00002038 spin_lock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002039 ses->chans[0].server = server;
2040 ses->chan_count = 1;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002041 ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
Shyam Prasad N724244c2021-07-19 10:54:46 +00002042 spin_unlock(&ses->chan_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002043
Jeff Layton198b5682010-04-24 07:57:48 -04002044 rc = cifs_negotiate_protocol(xid, ses);
2045 if (!rc)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002046 rc = cifs_setup_session(xid, ses, ctx->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002047
2048 /* each channel uses a different signing key */
2049 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
2050 sizeof(ses->smb3signingkey));
2051
Jeff Layton36988c72010-04-24 07:57:43 -04002052 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002053 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002054 goto get_ses_fail;
2055
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002056 /* success, put it on the list and add it as first channel */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302057 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002058 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302059 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002060
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002061 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002062
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002063 cifs_setup_ipc(ses, ctx);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002064
Jeff Layton36988c72010-04-24 07:57:43 -04002065 return ses;
2066
2067get_ses_fail:
2068 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002069 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002070 return ERR_PTR(rc);
2071}
2072
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002073static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002074{
2075 if (tcon->tidStatus == CifsExiting)
2076 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002077 if (strncmp(tcon->treeName, ctx->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002078 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002079 if (tcon->seal != ctx->seal)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002080 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002081 if (tcon->snapshot_time != ctx->snapshot_time)
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002082 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002083 if (tcon->handle_timeout != ctx->handle_timeout)
Steve Frenchca567eb2019-03-29 16:31:07 -05002084 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002085 if (tcon->no_lease != ctx->no_lease)
Steve French3e7a02d2019-09-11 21:46:20 -05002086 return 0;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002087 if (tcon->nodelete != ctx->nodelete)
Steve French82e93672020-05-19 03:06:57 -05002088 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002089 return 1;
2090}
2091
Steve French96daf2b2011-05-27 04:34:02 +00002092static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002093cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002096 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302098 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002099 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002100 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Paulo Alcantaraf3c852b2021-06-04 19:25:33 -03002101
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002102 if (!match_tcon(tcon, ctx))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002103 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002104 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302105 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return tcon;
2107 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302108 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return NULL;
2110}
2111
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002112void
Steve French96daf2b2011-05-27 04:34:02 +00002113cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002114{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002115 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002116 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002117
Aurelien Aptelb327a712018-01-24 13:46:10 +01002118 /*
2119 * IPC tcon share the lifetime of their session and are
2120 * destroyed in the session put function
2121 */
2122 if (tcon == NULL || tcon->ipc)
2123 return;
2124
2125 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002126 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302127 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002128 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302129 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002130 return;
2131 }
2132
Shyam Prasad N16dd9b82021-07-14 23:00:00 -05002133 /* tc_count can never go negative */
2134 WARN_ON(tcon->tc_count < 0);
2135
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002136 if (tcon->use_witness) {
2137 int rc;
2138
2139 rc = cifs_swn_unregister(tcon);
2140 if (rc < 0) {
2141 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
2142 __func__, rc);
2143 }
2144 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002145
Jeff Laytonf1987b42008-11-15 11:12:47 -05002146 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302147 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002148
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002149 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002150 if (ses->server->ops->tree_disconnect)
2151 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002152 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002153
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302154 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002155 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002156 cifs_put_smb_ses(ses);
2157}
2158
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002159/**
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002160 * cifs_get_tcon - get a tcon matching @ctx data from @ses
Steve French4c51de12021-09-13 18:29:46 -05002161 * @ses: smb session to issue the request on
2162 * @ctx: the superblock configuration context to use for building the
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002163 *
2164 * - tcon refcount is the number of mount points using the tcon.
2165 * - ses refcount is the number of tcon using the session.
2166 *
2167 * 1. This function assumes it is being called from cifs_mount() where
2168 * we already got a session reference (ses refcount +1).
2169 *
2170 * 2. Since we're in the context of adding a mount point, the end
2171 * result should be either:
2172 *
2173 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2174 * its session refcount incremented (1 new tcon). This +1 was
2175 * already done in (1).
2176 *
2177 * b) an existing tcon with refcount+1 (add a mount point to it) and
2178 * identical ses refcount (no new tcon). Because of (1) we need to
2179 * decrement the ses refcount.
2180 */
Steve French96daf2b2011-05-27 04:34:02 +00002181static struct cifs_tcon *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002182cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002183{
2184 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002185 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002186
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002187 tcon = cifs_find_tcon(ses, ctx);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002188 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002189 /*
2190 * tcon has refcount already incremented but we need to
2191 * decrement extra ses reference gotten by caller (case b)
2192 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002193 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04002194 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002195 return tcon;
2196 }
2197
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002198 if (!ses->server->ops->tree_connect) {
2199 rc = -ENOSYS;
2200 goto out_fail;
2201 }
2202
Jeff Laytond00c28d2010-04-24 07:57:44 -04002203 tcon = tconInfoAlloc();
2204 if (tcon == NULL) {
2205 rc = -ENOMEM;
2206 goto out_fail;
2207 }
2208
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002209 if (ctx->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06002210 if (ses->server->vals->protocol_id == 0) {
2211 cifs_dbg(VFS,
2212 "Use SMB2 or later for snapshot mount option\n");
2213 rc = -EOPNOTSUPP;
2214 goto out_fail;
2215 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002216 tcon->snapshot_time = ctx->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06002217 }
2218
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002219 if (ctx->handle_timeout) {
Steve Frenchca567eb2019-03-29 16:31:07 -05002220 if (ses->server->vals->protocol_id == 0) {
2221 cifs_dbg(VFS,
2222 "Use SMB2.1 or later for handle timeout option\n");
2223 rc = -EOPNOTSUPP;
2224 goto out_fail;
2225 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002226 tcon->handle_timeout = ctx->handle_timeout;
Steve Frenchca567eb2019-03-29 16:31:07 -05002227 }
2228
Jeff Laytond00c28d2010-04-24 07:57:44 -04002229 tcon->ses = ses;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002230 if (ctx->password) {
2231 tcon->password = kstrdup(ctx->password, GFP_KERNEL);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002232 if (!tcon->password) {
2233 rc = -ENOMEM;
2234 goto out_fail;
2235 }
2236 }
2237
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002238 if (ctx->seal) {
Steve French23657ad2018-04-22 15:14:58 -05002239 if (ses->server->vals->protocol_id == 0) {
2240 cifs_dbg(VFS,
2241 "SMB3 or later required for encryption\n");
2242 rc = -EOPNOTSUPP;
2243 goto out_fail;
2244 } else if (tcon->ses->server->capabilities &
2245 SMB2_GLOBAL_CAP_ENCRYPTION)
2246 tcon->seal = true;
2247 else {
2248 cifs_dbg(VFS, "Encryption is not supported on share\n");
2249 rc = -EOPNOTSUPP;
2250 goto out_fail;
2251 }
2252 }
2253
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002254 if (ctx->linux_ext) {
Steve French8505c8b2018-06-18 14:01:59 -05002255 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05002256 tcon->posix_extensions = true;
Joe Perchesa0a30362020-04-14 22:42:53 -07002257 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05002258 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07002259 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
Steve French8505c8b2018-06-18 14:01:59 -05002260 rc = -EOPNOTSUPP;
2261 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05002262 }
Steve Frenchb3266142018-05-20 23:41:10 -05002263 }
Steve Frenchb3266142018-05-20 23:41:10 -05002264
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002265 /*
2266 * BB Do we need to wrap session_mutex around this TCon call and Unix
2267 * SetFS as we do on SessSetup and reconnect?
2268 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002269 xid = get_xid();
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002270 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2271 ctx->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002272 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05002273 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002274 if (rc)
2275 goto out_fail;
2276
Steve Frenchb618f002015-11-03 09:15:03 -06002277 tcon->use_persistent = false;
2278 /* check if SMB2 or later, CIFS does not support persistent handles */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002279 if (ctx->persistent) {
Steve Frenchb618f002015-11-03 09:15:03 -06002280 if (ses->server->vals->protocol_id == 0) {
2281 cifs_dbg(VFS,
2282 "SMB3 or later required for persistent handles\n");
2283 rc = -EOPNOTSUPP;
2284 goto out_fail;
2285 } else if (ses->server->capabilities &
2286 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2287 tcon->use_persistent = true;
2288 else /* persistent handles requested but not supported */ {
2289 cifs_dbg(VFS,
2290 "Persistent handles not supported on share\n");
2291 rc = -EOPNOTSUPP;
2292 goto out_fail;
2293 }
2294 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2295 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002296 && (ctx->nopersistent == false)) {
Steve Frenchb618f002015-11-03 09:15:03 -06002297 cifs_dbg(FYI, "enabling persistent handles\n");
2298 tcon->use_persistent = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002299 } else if (ctx->resilient) {
Steve French592fafe2015-11-03 10:08:53 -06002300 if (ses->server->vals->protocol_id == 0) {
2301 cifs_dbg(VFS,
2302 "SMB2.1 or later required for resilient handles\n");
2303 rc = -EOPNOTSUPP;
2304 goto out_fail;
2305 }
2306 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06002307 }
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +02002308
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002309 tcon->use_witness = false;
Aurelien Aptelb7fd0fa2021-04-09 16:31:37 +02002310 if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002311 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2312 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002313 /*
2314 * Set witness in use flag in first place
2315 * to retry registration in the echo task
2316 */
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002317 tcon->use_witness = true;
Samuel Cabrerobf80e5d2020-11-30 19:02:51 +01002318 /* And try to register immediately */
2319 rc = cifs_swn_register(tcon);
2320 if (rc < 0) {
2321 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2322 goto out_fail;
2323 }
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06002324 } else {
2325 /* TODO: try to extend for non-cluster uses (eg multichannel) */
2326 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2327 rc = -EOPNOTSUPP;
2328 goto out_fail;
2329 }
2330 } else {
2331 cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2332 rc = -EOPNOTSUPP;
2333 goto out_fail;
2334 }
2335 }
Steve Frenchb618f002015-11-03 09:15:03 -06002336
Steve Frenchcae53f72019-09-03 17:49:46 -05002337 /* If the user really knows what they are doing they can override */
2338 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002339 if (ctx->cache_ro)
Steve Frenchcae53f72019-09-03 17:49:46 -05002340 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002341 else if (ctx->cache_rw)
Steve Frenchcae53f72019-09-03 17:49:46 -05002342 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2343 }
2344
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002345 if (ctx->no_lease) {
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302346 if (ses->server->vals->protocol_id == 0) {
2347 cifs_dbg(VFS,
2348 "SMB2 or later required for nolease option\n");
2349 rc = -EOPNOTSUPP;
2350 goto out_fail;
2351 } else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002352 tcon->no_lease = ctx->no_lease;
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05302353 }
2354
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002355 /*
2356 * We can have only one retry value for a connection to a share so for
2357 * resources mounted more than once to the same server share the last
2358 * value passed in for the retry flag is used.
2359 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002360 tcon->retry = ctx->retry;
2361 tcon->nocase = ctx->nocase;
Steve French3c6e65e2020-10-21 00:15:42 -05002362 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002363 tcon->nohandlecache = ctx->nohandlecache;
Steve French3c6e65e2020-10-21 00:15:42 -05002364 else
Jiapeng Zhong2be449f2021-01-14 17:09:20 +08002365 tcon->nohandlecache = true;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002366 tcon->nodelete = ctx->nodelete;
2367 tcon->local_lease = ctx->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07002368 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002369
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302370 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002371 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302372 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002373
2374 return tcon;
2375
2376out_fail:
2377 tconInfoFree(tcon);
2378 return ERR_PTR(rc);
2379}
2380
Jeff Layton9d002df2010-10-06 19:51:11 -04002381void
2382cifs_put_tlink(struct tcon_link *tlink)
2383{
2384 if (!tlink || IS_ERR(tlink))
2385 return;
2386
2387 if (!atomic_dec_and_test(&tlink->tl_count) ||
2388 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2389 tlink->tl_time = jiffies;
2390 return;
2391 }
2392
2393 if (!IS_ERR(tlink_tcon(tlink)))
2394 cifs_put_tcon(tlink_tcon(tlink));
2395 kfree(tlink);
2396 return;
2397}
Jeff Laytond00c28d2010-04-24 07:57:44 -04002398
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002399static int
2400compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2401{
2402 struct cifs_sb_info *old = CIFS_SB(sb);
2403 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002404 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2405 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002406
2407 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2408 return 0;
2409
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03002410 if (old->mnt_cifs_serverino_autodisabled)
2411 newflags &= ~CIFS_MOUNT_SERVER_INUM;
2412
2413 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002414 return 0;
2415
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002416 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002417 * We want to share sb only if we don't specify an r/wsize or
2418 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002419 */
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002420 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002421 return 0;
2422
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10002423 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002424 return 0;
2425
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002426 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2427 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002428 return 0;
2429
Ronnie Sahlberg8401e932020-12-12 13:40:50 -06002430 if (old->ctx->file_mode != new->ctx->file_mode ||
2431 old->ctx->dir_mode != new->ctx->dir_mode)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002432 return 0;
2433
2434 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2435 return 0;
2436
Steve French57804642021-02-24 12:12:53 -06002437 if (old->ctx->acregmax != new->ctx->acregmax)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002438 return 0;
Steve French4c9f9482021-02-23 15:50:57 -06002439 if (old->ctx->acdirmax != new->ctx->acdirmax)
2440 return 0;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002441
2442 return 1;
2443}
2444
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002445static int
2446match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2447{
2448 struct cifs_sb_info *old = CIFS_SB(sb);
2449 struct cifs_sb_info *new = mnt_data->cifs_sb;
Ronnie Sahlbergfe129262020-01-22 11:07:56 +10002450 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2451 old->prepath;
2452 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2453 new->prepath;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002454
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002455 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002456 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01002457 else if (!old_set && !new_set)
2458 return 1;
2459
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002460 return 0;
2461}
2462
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002463int
2464cifs_match_super(struct super_block *sb, void *data)
2465{
2466 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002467 struct smb3_fs_context *ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002468 struct cifs_sb_info *cifs_sb;
2469 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00002470 struct cifs_ses *ses;
2471 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002472 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002473 int rc = 0;
2474
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002475 spin_lock(&cifs_tcp_ses_lock);
2476 cifs_sb = CIFS_SB(sb);
2477 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
Steve French9ed38fd2021-09-23 19:18:37 -05002478 if (tlink == NULL) {
2479 /* can not match superblock if tlink were ever null */
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002480 spin_unlock(&cifs_tcp_ses_lock);
Steve French9ed38fd2021-09-23 19:18:37 -05002481 return 0;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002482 }
2483 tcon = tlink_tcon(tlink);
2484 ses = tcon->ses;
2485 tcp_srv = ses->server;
2486
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002487 ctx = mnt_data->ctx;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002488
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002489 if (!match_server(tcp_srv, ctx) ||
2490 !match_session(ses, ctx) ||
2491 !match_tcon(tcon, ctx) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01002492 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002493 rc = 0;
2494 goto out;
2495 }
2496
2497 rc = compare_mount_options(sb, mnt_data);
2498out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002499 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04002500 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002501 return rc;
2502}
2503
Jeff Layton09e50d52008-07-23 10:11:19 -04002504#ifdef CONFIG_DEBUG_LOCK_ALLOC
2505static struct lock_class_key cifs_key[2];
2506static struct lock_class_key cifs_slock_key[2];
2507
2508static inline void
2509cifs_reclassify_socket4(struct socket *sock)
2510{
2511 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002512 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002513 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2514 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2515}
2516
2517static inline void
2518cifs_reclassify_socket6(struct socket *sock)
2519{
2520 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02002521 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04002522 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2523 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2524}
2525#else
2526static inline void
2527cifs_reclassify_socket4(struct socket *sock)
2528{
2529}
2530
2531static inline void
2532cifs_reclassify_socket6(struct socket *sock)
2533{
2534}
2535#endif
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00002538static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Steve French50c2f752007-07-13 00:33:32 +00002540 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Steve French50c2f752007-07-13 00:33:32 +00002542 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 /* mask a nibble at a time and encode */
2544 target[j] = 'A' + (0x0F & (source[i] >> 4));
2545 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00002546 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548
2549}
2550
Ben Greear3eb9a882010-09-01 17:06:02 -07002551static int
2552bind_socket(struct TCP_Server_Info *server)
2553{
2554 int rc = 0;
2555 if (server->srcaddr.ss_family != AF_UNSPEC) {
2556 /* Bind to the specified local IP address */
2557 struct socket *socket = server->ssocket;
2558 rc = socket->ops->bind(socket,
2559 (struct sockaddr *) &server->srcaddr,
2560 sizeof(server->srcaddr));
2561 if (rc < 0) {
2562 struct sockaddr_in *saddr4;
2563 struct sockaddr_in6 *saddr6;
2564 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2565 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2566 if (saddr6->sin6_family == AF_INET6)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002567 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002568 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002569 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002570 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002571 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07002572 }
2573 }
2574 return rc;
2575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002578ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002581 /*
2582 * some servers require RFC1001 sessinit before sending
2583 * negprot - BB check reconnection in case where second
2584 * sessinit is sent but no second negprot
2585 */
2586 struct rfc1002_session_packet *ses_init_buf;
2587 struct smb_hdr *smb_buf;
2588 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2589 GFP_KERNEL);
2590 if (ses_init_buf) {
2591 ses_init_buf->trailer.session_req.called_len = 32;
2592
Colin Ian King997152f2016-01-25 16:25:54 +00002593 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002594 rfc1002mangle(ses_init_buf->trailer.
2595 session_req.called_name,
2596 server->server_RFC1001_name,
2597 RFC1001_NAME_LEN_WITH_NULL);
2598 else
2599 rfc1002mangle(ses_init_buf->trailer.
2600 session_req.called_name,
2601 DEFAULT_CIFS_CALLED_NAME,
2602 RFC1001_NAME_LEN_WITH_NULL);
2603
2604 ses_init_buf->trailer.session_req.calling_len = 32;
2605
2606 /*
2607 * calling name ends in null (byte 16) from old smb
2608 * convention.
2609 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05002610 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002611 rfc1002mangle(ses_init_buf->trailer.
2612 session_req.calling_name,
2613 server->workstation_RFC1001_name,
2614 RFC1001_NAME_LEN_WITH_NULL);
2615 else
2616 rfc1002mangle(ses_init_buf->trailer.
2617 session_req.calling_name,
2618 "LINUX_CIFS_CLNT",
2619 RFC1001_NAME_LEN_WITH_NULL);
2620
2621 ses_init_buf->trailer.session_req.scope1 = 0;
2622 ses_init_buf->trailer.session_req.scope2 = 0;
2623 smb_buf = (struct smb_hdr *)ses_init_buf;
2624
2625 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002626 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002627 rc = smb_send(server, smb_buf, 0x44);
2628 kfree(ses_init_buf);
2629 /*
2630 * RFC1001 layer in at least one server
2631 * requires very short break before negprot
2632 * presumably because not expecting negprot
2633 * to follow so fast. This is a simple
2634 * solution that works without
2635 * complicating the code and causes no
2636 * significant slowing down on mount
2637 * for everyone else
2638 */
2639 usleep_range(1000, 2000);
2640 }
2641 /*
2642 * else the negprot may still work without this
2643 * even though malloc failed
2644 */
2645
2646 return rc;
2647}
2648
2649static int
2650generic_ip_connect(struct TCP_Server_Info *server)
2651{
2652 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00002653 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002654 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002655 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002656 struct sockaddr *saddr;
2657
2658 saddr = (struct sockaddr *) &server->dstaddr;
2659
2660 if (server->dstaddr.ss_family == AF_INET6) {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002661 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2662
2663 sport = ipv6->sin6_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002664 slen = sizeof(struct sockaddr_in6);
2665 sfamily = AF_INET6;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002666 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2667 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002668 } else {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002669 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2670
2671 sport = ipv4->sin_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002672 slen = sizeof(struct sockaddr_in);
2673 sfamily = AF_INET;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02002674 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2675 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002678 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06002679 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2680 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (rc < 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002682 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002683 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002686
2687 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05002688 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002689 server->ssocket = socket;
2690 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002691 if (sfamily == AF_INET6)
2692 cifs_reclassify_socket6(socket);
2693 else
2694 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
2696
Ben Greear3eb9a882010-09-01 17:06:02 -07002697 rc = bind_socket(server);
2698 if (rc < 0)
2699 return rc;
2700
Jeff Laytond5c56052008-12-01 18:42:33 -05002701 /*
2702 * Eventually check for other socket options to change from
2703 * the default. sock_setsockopt not used because it expects
2704 * user space buffer
2705 */
2706 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00002707 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00002708
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002709 /* make the bufsizes depend on wsize/rsize and max requests */
2710 if (server->noautotune) {
2711 if (socket->sk->sk_sndbuf < (200 * 1024))
2712 socket->sk->sk_sndbuf = 200 * 1024;
2713 if (socket->sk->sk_rcvbuf < (140 * 1024))
2714 socket->sk->sk_rcvbuf = 140 * 1024;
2715 }
2716
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02002717 if (server->tcp_nodelay)
2718 tcp_sock_set_nodelay(socket->sk);
Steve French6a5fa2362010-01-01 01:28:43 +00002719
Joe Perchesf96637b2013-05-04 22:12:25 -05002720 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002721 socket->sk->sk_sndbuf,
2722 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2723
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002724 rc = socket->ops->connect(socket, saddr, slen,
2725 server->noblockcnt ? O_NONBLOCK : 0);
Paulo Alcantara (SUSE)d532cc72019-10-10 12:31:58 -03002726 /*
2727 * When mounting SMB root file systems, we do not want to block in
2728 * connect. Otherwise bail out and then let cifs_reconnect() perform
2729 * reconnect failover - if possible.
2730 */
2731 if (server->noblockcnt && rc == -EINPROGRESS)
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002732 rc = 0;
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002733 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002734 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Steve Frenchd7171cd2021-11-04 15:56:37 -05002735 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04002736 sock_release(socket);
2737 server->ssocket = NULL;
2738 return rc;
2739 }
Steve Frenchd7171cd2021-11-04 15:56:37 -05002740 trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002741 if (sport == htons(RFC1001_PORT))
2742 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00002743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return rc;
2745}
2746
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002747static int
2748ip_connect(struct TCP_Server_Info *server)
2749{
Steve French6da97912011-03-13 18:55:55 +00002750 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002751 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2752 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2753
2754 if (server->dstaddr.ss_family == AF_INET6)
2755 sport = &addr6->sin6_port;
2756 else
2757 sport = &addr->sin_port;
2758
2759 if (*sport == 0) {
2760 int rc;
2761
2762 /* try with 445 port at first */
2763 *sport = htons(CIFS_PORT);
2764
2765 rc = generic_ip_connect(server);
2766 if (rc >= 0)
2767 return rc;
2768
2769 /* if it failed, try with 139 port */
2770 *sport = htons(RFC1001_PORT);
2771 }
2772
2773 return generic_ip_connect(server);
2774}
2775
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002776void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002777 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Steve French8af18972007-02-14 04:42:51 +00002778{
Steve Frenchbc044992020-12-11 19:48:26 -06002779 /*
2780 * If we are reconnecting then should we check to see if
Steve French8af18972007-02-14 04:42:51 +00002781 * any requested capabilities changed locally e.g. via
2782 * remount but we can not do much about it here
2783 * if they have (even if we could detect it by the following)
2784 * Perhaps we could add a backpointer to array of sb from tcon
2785 * or if we change to make all sb to same share the same
2786 * sb as NFS - then we only have one backpointer to sb.
2787 * What if we wanted to mount the server share twice once with
Steve Frenchbc044992020-12-11 19:48:26 -06002788 * and once without posixacls or posix paths?
2789 */
Steve French8af18972007-02-14 04:42:51 +00002790 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00002791
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002792 if (ctx && ctx->no_linux_ext) {
Steve Frenchc18c8422007-07-18 23:21:09 +00002793 tcon->fsUnixInfo.Capability = 0;
2794 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05002795 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002796 return;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002797 } else if (ctx)
Steve Frenchc18c8422007-07-18 23:21:09 +00002798 tcon->unix_ext = 1; /* Unix Extensions supported */
2799
Jiapeng Zhong16a78852021-01-14 18:02:23 +08002800 if (!tcon->unix_ext) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002801 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00002802 return;
2803 }
Steve French50c2f752007-07-13 00:33:32 +00002804
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002805 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00002806 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05002807 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve Frenchbc044992020-12-11 19:48:26 -06002808 /*
2809 * check for reconnect case in which we do not
2810 * want to change the mount behavior if we can avoid it
2811 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002812 if (ctx == NULL) {
Steve Frenchbc044992020-12-11 19:48:26 -06002813 /*
2814 * turn off POSIX ACL and PATHNAMES if not set
2815 * originally at mount time
2816 */
Steve French8af18972007-02-14 04:42:51 +00002817 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
2818 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002819 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
2820 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002821 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00002822 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00002823 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002824 cifs_dbg(VFS, "possible reconnect error\n");
2825 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00002826 }
Steve French8af18972007-02-14 04:42:51 +00002827 }
Steve French50c2f752007-07-13 00:33:32 +00002828
Steve French6848b732011-05-26 18:38:54 +00002829 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002830 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00002831
Steve French8af18972007-02-14 04:42:51 +00002832 cap &= CIFS_UNIX_CAP_MASK;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002833 if (ctx && ctx->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00002834 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002835 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002836 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002837 if (cifs_sb)
2838 cifs_sb->mnt_cifs_flags |=
2839 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00002840 }
2841
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002842 if (ctx && ctx->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00002843 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00002844 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002845 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04002846 if (cifs_sb)
2847 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00002848 CIFS_MOUNT_POSIX_PATHS;
2849 }
Steve French50c2f752007-07-13 00:33:32 +00002850
Joe Perchesf96637b2013-05-04 22:12:25 -05002851 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00002852#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00002853 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002854 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002855 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002856 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002857 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002858 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002859 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002860 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002861 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002862 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002863 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002864 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00002865 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002866 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00002867 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002868 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00002869 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05002870 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00002871#endif /* CIFS_DEBUG2 */
2872 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002873 if (ctx == NULL)
Joe Perchesf96637b2013-05-04 22:12:25 -05002874 cifs_dbg(FYI, "resetting capabilities failed\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002875 else
Joe Perchesf96637b2013-05-04 22:12:25 -05002876 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 +00002877
Steve French8af18972007-02-14 04:42:51 +00002878 }
2879 }
2880}
2881
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002882int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002883{
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002884 struct smb3_fs_context *ctx = cifs_sb->ctx;
2885
Jeff Layton2de970f2010-10-06 19:51:12 -04002886 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
2887
Al Viro2ced6f62011-06-17 09:20:04 -04002888 spin_lock_init(&cifs_sb->tlink_tree_lock);
2889 cifs_sb->tlink_tree = RB_ROOT;
2890
Frank Sorensonf52aa792020-02-12 15:31:48 -06002891 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
Ronnie Sahlberg51acd202020-12-14 16:40:24 +10002892 ctx->file_mode, ctx->dir_mode);
Steve French3b795212008-11-13 19:45:32 +00002893
Ronnie Sahlberg387ec582020-12-14 16:40:20 +10002894 /* this is needed for ASCII cp to Unicode converts */
2895 if (ctx->iocharset == NULL) {
2896 /* load_nls_default cannot return null */
2897 cifs_sb->local_nls = load_nls_default();
2898 } else {
2899 cifs_sb->local_nls = load_nls(ctx->iocharset);
2900 if (cifs_sb->local_nls == NULL) {
2901 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
2902 ctx->iocharset);
2903 return -ELIBACC;
2904 }
2905 }
2906 ctx->local_nls = cifs_sb->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05302907
Ronnie Sahlberg2d39f502020-12-14 16:40:25 +10002908 smb3_update_mnt_flags(cifs_sb);
2909
2910 if (ctx->direct_io)
Joe Perchesf96637b2013-05-04 22:12:25 -05002911 cifs_dbg(FYI, "mounting share using direct i/o\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002912 if (ctx->cache_ro) {
Steve French83bbfa72019-08-27 23:58:54 -05002913 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
2914 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002915 } else if (ctx->cache_rw) {
Steve French41e033f2019-08-30 02:12:41 -05002916 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
2917 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
2918 CIFS_MOUNT_RW_CACHE);
Steve French83bbfa72019-08-27 23:58:54 -05002919 }
Steve French3b795212008-11-13 19:45:32 +00002920
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002921 if ((ctx->cifs_acl) && (ctx->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05002922 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002923
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002924 if (ctx->prepath) {
2925 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002926 if (cifs_sb->prepath == NULL)
2927 return -ENOMEM;
Shyam Prasad Na738c932021-02-11 03:26:54 -08002928 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01002929 }
2930
2931 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002932}
2933
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002934/* Release all succeed connections */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002935static inline void mount_put_conns(struct mount_ctx *mnt_ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002936{
2937 int rc = 0;
2938
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002939 if (mnt_ctx->tcon)
2940 cifs_put_tcon(mnt_ctx->tcon);
2941 else if (mnt_ctx->ses)
2942 cifs_put_smb_ses(mnt_ctx->ses);
2943 else if (mnt_ctx->server)
2944 cifs_put_tcp_session(mnt_ctx->server, 0);
2945 mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
2946 free_xid(mnt_ctx->xid);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002947}
2948
2949/* Get connections for tcp, ses and tcon */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002950static int mount_get_conns(struct mount_ctx *mnt_ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002951{
2952 int rc = 0;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002953 struct TCP_Server_Info *server = NULL;
2954 struct cifs_ses *ses = NULL;
2955 struct cifs_tcon *tcon = NULL;
2956 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
2957 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
2958 unsigned int xid;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002959
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002960 xid = get_xid();
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002961
2962 /* get a reference to a tcp session */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002963 server = cifs_get_tcp_session(ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002964 if (IS_ERR(server)) {
2965 rc = PTR_ERR(server);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002966 server = NULL;
2967 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002968 }
2969
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002970 /* get a reference to a SMB session */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002971 ses = cifs_get_smb_ses(server, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002972 if (IS_ERR(ses)) {
2973 rc = PTR_ERR(ses);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002974 ses = NULL;
2975 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002976 }
2977
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002978 if ((ctx->persistent == true) && (!(ses->server->capabilities &
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002979 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002980 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002981 rc = -EOPNOTSUPP;
2982 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002983 }
2984
2985 /* search for existing tcon to this server share */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06002986 tcon = cifs_get_tcon(ses, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002987 if (IS_ERR(tcon)) {
2988 rc = PTR_ERR(tcon);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03002989 tcon = NULL;
2990 goto out;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002991 }
2992
Paulo Alcantara56c762e2018-11-14 13:03:14 -02002993 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
2994 if (tcon->posix_extensions)
2995 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
2996
2997 /* tell server which Unix caps we support */
2998 if (cap_unix(tcon->ses)) {
2999 /*
3000 * reset of caps checks mount to see if unix extensions disabled
3001 * for just this mount.
3002 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003003 reset_cifs_unix_caps(xid, tcon, cifs_sb, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003004 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3005 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003006 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3007 rc = -EACCES;
3008 goto out;
3009 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003010 } else
3011 tcon->unix_ext = 0; /* server does not support them */
3012
3013 /* do not care if a following call succeed - informational */
Steve French1981eba2019-08-29 22:33:38 -05003014 if (!tcon->pipe && server->ops->qfs_tcon) {
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003015 server->ops->qfs_tcon(xid, tcon, cifs_sb);
Steve French1981eba2019-08-29 22:33:38 -05003016 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
3017 if (tcon->fsDevInfo.DeviceCharacteristics &
Steve French52870d52019-10-01 21:25:46 -05003018 cpu_to_le32(FILE_READ_ONLY_DEVICE))
Steve French1981eba2019-08-29 22:33:38 -05003019 cifs_dbg(VFS, "mounted to read only share\n");
Steve French41e033f2019-08-30 02:12:41 -05003020 else if ((cifs_sb->mnt_cifs_flags &
3021 CIFS_MOUNT_RW_CACHE) == 0)
Steve French1981eba2019-08-29 22:33:38 -05003022 cifs_dbg(VFS, "read only mount of RW share\n");
Steve French41e033f2019-08-30 02:12:41 -05003023 /* no need to log a RW mount of a typical RW share */
Steve French1981eba2019-08-29 22:33:38 -05003024 }
Steve French02102742021-11-10 03:15:29 -06003025 /*
3026 * The cookie is initialized from volume info returned above.
3027 * Inside cifs_fscache_get_super_cookie it checks
3028 * that we do not get super cookie twice.
3029 */
3030 cifs_fscache_get_super_cookie(tcon);
Steve French1981eba2019-08-29 22:33:38 -05003031 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003032
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003033 /*
3034 * Clamp the rsize/wsize mount arguments if they are too big for the server
Steve French0c2b5f72020-12-15 13:28:50 -06003035 * and set the rsize/wsize to the negotiated values if not passed in by
3036 * the user on mount
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003037 */
Steve French0c2b5f72020-12-15 13:28:50 -06003038 if ((cifs_sb->ctx->wsize == 0) ||
3039 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003040 cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
Steve French0c2b5f72020-12-15 13:28:50 -06003041 if ((cifs_sb->ctx->rsize == 0) ||
3042 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10003043 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003044
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003045out:
3046 mnt_ctx->server = server;
3047 mnt_ctx->ses = ses;
3048 mnt_ctx->tcon = tcon;
3049 mnt_ctx->xid = xid;
3050
3051 return rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003052}
3053
3054static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3055 struct cifs_tcon *tcon)
3056{
3057 struct tcon_link *tlink;
3058
3059 /* hang the tcon off of the superblock */
3060 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3061 if (tlink == NULL)
3062 return -ENOMEM;
3063
3064 tlink->tl_uid = ses->linux_uid;
3065 tlink->tl_tcon = tcon;
3066 tlink->tl_time = jiffies;
3067 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3068 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3069
3070 cifs_sb->master_tlink = tlink;
3071 spin_lock(&cifs_sb->tlink_tree_lock);
3072 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3073 spin_unlock(&cifs_sb->tlink_tree_lock);
3074
3075 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3076 TLINK_IDLE_EXPIRE);
3077 return 0;
3078}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003079
Steve French2d6d5892009-04-09 00:36:44 +00003080#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003081/* Get unique dfs connections */
3082static int mount_get_dfs_conns(struct mount_ctx *mnt_ctx)
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003083{
3084 int rc;
3085
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003086 mnt_ctx->fs_ctx->nosharesock = true;
3087 rc = mount_get_conns(mnt_ctx);
3088 if (mnt_ctx->server) {
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003089 cifs_dbg(FYI, "%s: marking tcp session as a dfs connection\n", __func__);
3090 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003091 mnt_ctx->server->is_dfs_conn = true;
Paulo Alcantaracdc33632021-07-15 21:53:53 -03003092 spin_unlock(&cifs_tcp_ses_lock);
3093 }
3094 return rc;
3095}
3096
Steve French6d3ea7e2012-11-28 22:34:41 -06003097/*
3098 * cifs_build_path_to_root returns full path to root when we do not have an
Steve French201023c2021-02-15 11:03:45 -06003099 * existing connection (tcon)
Steve French6d3ea7e2012-11-28 22:34:41 -06003100 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003101static char *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003102build_unc_path_to_root(const struct smb3_fs_context *ctx,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003103 const struct cifs_sb_info *cifs_sb, bool useppath)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003104{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003105 char *full_path, *pos;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003106 unsigned int pplen = useppath && ctx->prepath ?
3107 strlen(ctx->prepath) + 1 : 0;
3108 unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003109
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10003110 if (unc_len > MAX_TREE_SIZE)
3111 return ERR_PTR(-EINVAL);
3112
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003113 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003114 if (full_path == NULL)
3115 return ERR_PTR(-ENOMEM);
3116
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003117 memcpy(full_path, ctx->UNC, unc_len);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003118 pos = full_path + unc_len;
3119
3120 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003121 *pos = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003122 memcpy(pos + 1, ctx->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003123 pos += pplen;
3124 }
3125
3126 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003127 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003128 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003129 return full_path;
3130}
Sean Finneydd613942011-04-11 13:19:30 +00003131
Steve French4c51de12021-09-13 18:29:46 -05003132/*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003133 * expand_dfs_referral - Update cifs_sb from dfs referral path
Paulo Alcantara1c780222018-11-14 16:24:03 -02003134 *
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003135 * cifs_sb->ctx->mount_options will be (re-)allocated to a string containing updated options for the
3136 * submount. Otherwise it will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003137 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003138static int expand_dfs_referral(struct mount_ctx *mnt_ctx, const char *full_path,
3139 struct dfs_info3_param *referral)
Sean Finneydd613942011-04-11 13:19:30 +00003140{
3141 int rc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003142 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3143 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3144 char *fake_devname = NULL, *mdata = NULL;
Sean Finneydd613942011-04-11 13:19:30 +00003145
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003146 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, referral,
3147 &fake_devname);
3148 if (IS_ERR(mdata)) {
3149 rc = PTR_ERR(mdata);
3150 mdata = NULL;
3151 } else {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003152 /*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003153 * We can not clear out the whole structure since we no longer have an explicit
3154 * function to parse a mount-string. Instead we need to clear out the individual
3155 * fields that are no longer valid.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003156 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003157 kfree(ctx->prepath);
3158 ctx->prepath = NULL;
3159 rc = cifs_setup_volume_info(ctx, mdata, fake_devname);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003160 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003161 kfree(fake_devname);
3162 kfree(cifs_sb->ctx->mount_options);
3163 cifs_sb->ctx->mount_options = mdata;
Paulo Alcantara85132222021-02-24 20:59:21 -03003164
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003165 return rc;
3166}
Steve French2d6d5892009-04-09 00:36:44 +00003167#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003168
Ronnie Sahlberg24e0a1e2020-12-10 00:06:02 -06003169/* TODO: all callers to this are broken. We are not parsing mount_options here
3170 * we should pass a clone of the original context?
3171 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003172int
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003173cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003175 int rc;
Sean Finneydd613942011-04-11 13:19:30 +00003176
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003177 if (devname) {
3178 cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
3179 rc = smb3_parse_devname(devname, ctx);
3180 if (rc) {
3181 cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
3182 return rc;
3183 }
3184 }
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003185
3186 if (mntopts) {
3187 char *ip;
3188
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003189 rc = smb3_parse_opt(mntopts, "ip", &ip);
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003190 if (rc) {
3191 cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3192 return rc;
3193 }
3194
3195 rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3196 kfree(ip);
3197 if (!rc) {
Ronnie Sahlberg0d4873f2021-01-28 21:35:10 -06003198 cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
3199 return -EINVAL;
3200 }
3201 }
3202
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003203 if (ctx->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003204 cifs_dbg(FYI, "Anonymous login\n");
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003205 kfree(ctx->username);
3206 ctx->username = NULL;
3207 } else if (ctx->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 /* BB fixme parse for domain name here */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003209 cifs_dbg(FYI, "Username: %s\n", ctx->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003211 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003212 /* In userspace mount helper we can get user name from alternate
3213 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003214 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
3216
Paulo Alcantara5c1acf32021-05-03 11:55:26 -03003217 return 0;
Jeff Layton04db79b2011-07-06 08:10:38 -04003218}
3219
Aurelien Aptela6b50582016-05-25 19:59:09 +02003220static int
3221cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3222 unsigned int xid,
3223 struct cifs_tcon *tcon,
3224 struct cifs_sb_info *cifs_sb,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003225 char *full_path,
3226 int added_treename)
Aurelien Aptela6b50582016-05-25 19:59:09 +02003227{
3228 int rc;
3229 char *s;
3230 char sep, tmp;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003231 int skip = added_treename ? 1 : 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +02003232
3233 sep = CIFS_DIR_SEP(cifs_sb);
3234 s = full_path;
3235
3236 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3237 while (rc == 0) {
3238 /* skip separators */
3239 while (*s == sep)
3240 s++;
3241 if (!*s)
3242 break;
3243 /* next separator */
3244 while (*s && *s != sep)
3245 s++;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003246 /*
3247 * if the treename is added, we then have to skip the first
3248 * part within the separators
3249 */
3250 if (skip) {
3251 skip = 0;
3252 continue;
3253 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02003254 /*
3255 * temporarily null-terminate the path at the end of
3256 * the current component
3257 */
3258 tmp = *s;
3259 *s = 0;
3260 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3261 full_path);
3262 *s = tmp;
3263 }
3264 return rc;
3265}
3266
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003267/*
3268 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
3269 * otherwise 0.
3270 */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003271static int is_path_remote(struct mount_ctx *mnt_ctx)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003272{
Jeff Layton1daaae82012-03-21 06:30:40 -04003273 int rc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003274 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3275 struct TCP_Server_Info *server = mnt_ctx->server;
3276 unsigned int xid = mnt_ctx->xid;
3277 struct cifs_tcon *tcon = mnt_ctx->tcon;
3278 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003279 char *full_path;
3280
3281 if (!server->ops->is_path_accessible)
3282 return -EOPNOTSUPP;
3283
3284 /*
3285 * cifs_build_path_to_root works only when we have a valid tcon
3286 */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003287 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003288 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3289 if (full_path == NULL)
3290 return -ENOMEM;
3291
3292 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3293
3294 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3295 full_path);
3296 if (rc != 0 && rc != -EREMOTE) {
3297 kfree(full_path);
3298 return rc;
3299 }
3300
3301 if (rc != -EREMOTE) {
3302 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10003303 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003304 if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07003305 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 -02003306 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3307 rc = 0;
3308 }
3309 }
3310
3311 kfree(full_path);
3312 return rc;
3313}
3314
3315#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003316static void set_root_ses(struct mount_ctx *mnt_ctx)
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03003317{
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003318 if (mnt_ctx->ses) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003319 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003320 mnt_ctx->ses->ses_count++;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003321 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003322 dfs_cache_add_refsrv_session(&mnt_ctx->mount_id, mnt_ctx->ses);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003323 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003324 mnt_ctx->root_ses = mnt_ctx->ses;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003325}
3326
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003327static 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 -03003328{
3329 int rc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003330 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3331 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003332
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003333 *isdfs = true;
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003334
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003335 rc = mount_get_conns(mnt_ctx);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003336 /*
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003337 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
3338 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003339 *
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003340 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
3341 * to respond with PATH_NOT_COVERED to requests that include the prefix.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003342 */
Paulo Alcantarad01132a2021-02-24 20:59:24 -03003343 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003344 dfs_cache_find(mnt_ctx->xid, mnt_ctx->ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
3345 ctx->UNC + 1, NULL, root_tl)) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003346 if (rc)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003347 return rc;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003348 /* Check if it is fully accessible and then mount it */
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003349 rc = is_path_remote(mnt_ctx);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03003350 if (!rc)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003351 *isdfs = false;
3352 else if (rc != -EREMOTE)
3353 return rc;
3354 }
3355 return 0;
3356}
3357
3358static int connect_dfs_target(struct mount_ctx *mnt_ctx, const char *full_path,
3359 const char *ref_path, struct dfs_cache_tgt_iterator *tit)
3360{
3361 int rc;
3362 struct dfs_info3_param ref = {};
3363 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3364 char *oldmnt = cifs_sb->ctx->mount_options;
3365
3366 rc = dfs_cache_get_tgt_referral(ref_path, tit, &ref);
3367 if (rc)
3368 goto out;
3369
3370 rc = expand_dfs_referral(mnt_ctx, full_path, &ref);
3371 if (rc)
3372 goto out;
3373
3374 /* Connect to new target only if we were redirected (e.g. mount options changed) */
3375 if (oldmnt != cifs_sb->ctx->mount_options) {
3376 mount_put_conns(mnt_ctx);
3377 rc = mount_get_dfs_conns(mnt_ctx);
3378 }
3379 if (!rc) {
3380 if (cifs_is_referral_server(mnt_ctx->tcon, &ref))
3381 set_root_ses(mnt_ctx);
3382 rc = dfs_cache_update_tgthint(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3383 cifs_remap(cifs_sb), ref_path, tit);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003384 }
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003385
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003386out:
3387 free_dfs_info_param(&ref);
3388 return rc;
3389}
Paulo Alcantaraf3c852b2021-06-04 19:25:33 -03003390
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003391static int connect_dfs_root(struct mount_ctx *mnt_ctx, struct dfs_cache_tgt_list *root_tl)
3392{
3393 int rc;
3394 char *full_path;
3395 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3396 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3397 struct dfs_cache_tgt_iterator *tit;
3398
3399 /* Put initial connections as they might be shared with other mounts. We need unique dfs
3400 * connections per mount to properly failover, so mount_get_dfs_conns() must be used from
3401 * now on.
3402 */
3403 mount_put_conns(mnt_ctx);
3404 mount_get_dfs_conns(mnt_ctx);
3405
3406 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3407 if (IS_ERR(full_path))
3408 return PTR_ERR(full_path);
3409
3410 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(ctx->UNC, cifs_sb->local_nls,
3411 cifs_remap(cifs_sb));
3412 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3413 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3414 mnt_ctx->origin_fullpath = NULL;
3415 goto out;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003416 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003417
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003418 /* Try all dfs root targets */
3419 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(root_tl);
3420 tit; tit = dfs_cache_get_next_tgt(root_tl, tit)) {
3421 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->origin_fullpath + 1, tit);
3422 if (!rc) {
3423 mnt_ctx->leaf_fullpath = kstrdup(mnt_ctx->origin_fullpath, GFP_KERNEL);
3424 if (!mnt_ctx->leaf_fullpath)
3425 rc = -ENOMEM;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02003426 break;
3427 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003428 }
Paulo Alcantara5ff28362021-02-24 20:59:23 -03003429
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003430out:
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003431 kfree(full_path);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003432 return rc;
3433}
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003434
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003435static int __follow_dfs_link(struct mount_ctx *mnt_ctx)
3436{
3437 int rc;
3438 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3439 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3440 char *full_path;
3441 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3442 struct dfs_cache_tgt_iterator *tit;
3443
3444 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3445 if (IS_ERR(full_path))
3446 return PTR_ERR(full_path);
3447
3448 kfree(mnt_ctx->leaf_fullpath);
3449 mnt_ctx->leaf_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3450 cifs_remap(cifs_sb));
3451 if (IS_ERR(mnt_ctx->leaf_fullpath)) {
3452 rc = PTR_ERR(mnt_ctx->leaf_fullpath);
3453 mnt_ctx->leaf_fullpath = NULL;
3454 goto out;
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003455 }
Paulo Alcantarac870a8e2021-06-04 19:25:30 -03003456
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003457 /* Get referral from dfs link */
3458 rc = dfs_cache_find(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3459 cifs_remap(cifs_sb), mnt_ctx->leaf_fullpath + 1, NULL, &tl);
3460 if (rc)
3461 goto out;
3462
3463 /* Try all dfs link targets */
3464 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(&tl);
3465 tit; tit = dfs_cache_get_next_tgt(&tl, tit)) {
3466 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->leaf_fullpath + 1, tit);
3467 if (!rc) {
3468 rc = is_path_remote(mnt_ctx);
3469 break;
3470 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003471 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003472
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003473out:
3474 kfree(full_path);
3475 dfs_cache_free_tgts(&tl);
3476 return rc;
3477}
3478
3479static int follow_dfs_link(struct mount_ctx *mnt_ctx)
3480{
3481 int rc;
3482 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3483 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3484 char *full_path;
3485 int num_links = 0;
3486
3487 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3488 if (IS_ERR(full_path))
3489 return PTR_ERR(full_path);
3490
3491 kfree(mnt_ctx->origin_fullpath);
3492 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3493 cifs_remap(cifs_sb));
3494 kfree(full_path);
3495
3496 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3497 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3498 mnt_ctx->origin_fullpath = NULL;
3499 return rc;
3500 }
3501
3502 do {
3503 rc = __follow_dfs_link(mnt_ctx);
3504 if (!rc || rc != -EREMOTE)
3505 break;
3506 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
3507
3508 return rc;
3509}
3510
3511/* Set up DFS referral paths for failover */
3512static void setup_server_referral_paths(struct mount_ctx *mnt_ctx)
3513{
3514 struct TCP_Server_Info *server = mnt_ctx->server;
3515
3516 server->origin_fullpath = mnt_ctx->origin_fullpath;
3517 server->leaf_fullpath = mnt_ctx->leaf_fullpath;
3518 server->current_fullpath = mnt_ctx->leaf_fullpath;
3519 mnt_ctx->origin_fullpath = mnt_ctx->leaf_fullpath = NULL;
3520}
3521
3522int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3523{
3524 int rc;
3525 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3526 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3527 bool isdfs;
3528
3529 rc = is_dfs_mount(&mnt_ctx, &isdfs, &tl);
3530 if (rc)
3531 goto error;
3532 if (!isdfs)
3533 goto out;
3534
3535 uuid_gen(&mnt_ctx.mount_id);
3536 rc = connect_dfs_root(&mnt_ctx, &tl);
3537 dfs_cache_free_tgts(&tl);
3538
3539 if (rc)
3540 goto error;
3541
3542 rc = is_path_remote(&mnt_ctx);
3543 if (rc == -EREMOTE)
3544 rc = follow_dfs_link(&mnt_ctx);
3545 if (rc)
3546 goto error;
3547
3548 setup_server_referral_paths(&mnt_ctx);
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01003549 /*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003550 * After reconnecting to a different server, unique ids won't match anymore, so we disable
3551 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01003552 */
3553 cifs_autodisable_serverino(cifs_sb);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003554 /*
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003555 * Force the use of prefix path to support failover on DFS paths that resolve to targets
3556 * that have different prefix paths.
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003557 */
3558 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3559 kfree(cifs_sb->prepath);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003560 cifs_sb->prepath = ctx->prepath;
3561 ctx->prepath = NULL;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003562 uuid_copy(&cifs_sb->dfs_mount_id, &mnt_ctx.mount_id);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03003563
Jeff Layton70fe7dc2007-11-16 22:21:07 +00003564out:
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003565 free_xid(mnt_ctx.xid);
3566 cifs_try_adding_channels(cifs_sb, mnt_ctx.ses);
3567 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003568
3569error:
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003570 dfs_cache_put_refsrv_sessions(&mnt_ctx.mount_id);
3571 kfree(mnt_ctx.origin_fullpath);
3572 kfree(mnt_ctx.leaf_fullpath);
3573 mount_put_conns(&mnt_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return rc;
3575}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003576#else
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003577int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003578{
3579 int rc = 0;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003580 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003581
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003582 rc = mount_get_conns(&mnt_ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003583 if (rc)
3584 goto error;
3585
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003586 if (mnt_ctx.tcon) {
3587 rc = is_path_remote(&mnt_ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003588 if (rc == -EREMOTE)
3589 rc = -EOPNOTSUPP;
3590 if (rc)
3591 goto error;
3592 }
3593
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003594 free_xid(mnt_ctx.xid);
3595 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003596
3597error:
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03003598 mount_put_conns(&mnt_ctx);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003599 return rc;
3600}
3601#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Jeff Layton8d1bca32011-06-11 21:17:10 -04003603/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01003604 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04003605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003607CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00003608 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 const struct nls_table *nls_codepage)
3610{
3611 struct smb_hdr *smb_buffer;
3612 struct smb_hdr *smb_buffer_response;
3613 TCONX_REQ *pSMB;
3614 TCONX_RSP *pSMBr;
3615 unsigned char *bcc_ptr;
3616 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05003617 int length;
3618 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 if (ses == NULL)
3621 return -EIO;
3622
3623 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00003624 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00003626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 smb_buffer_response = smb_buffer;
3628
3629 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3630 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003631
Pavel Shilovsky88257362012-05-23 14:01:59 +04003632 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 smb_buffer->Uid = ses->Suid;
3634 pSMB = (TCONX_REQ *) smb_buffer;
3635 pSMBr = (TCONX_RSP *) smb_buffer_response;
3636
3637 pSMB->AndXCommand = 0xFF;
3638 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01003640 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08003641 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003642 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003643 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003644 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Jeff Layton38d77c52013-05-26 07:01:00 -04003647 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3649
3650 if (ses->capabilities & CAP_STATUS32) {
3651 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3652 }
3653 if (ses->capabilities & CAP_DFS) {
3654 smb_buffer->Flags2 |= SMBFLG2_DFS;
3655 }
3656 if (ses->capabilities & CAP_UNICODE) {
3657 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3658 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06003659 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00003660 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00003661 (/* server len*/ + 256 /* share len */), nls_codepage);
3662 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 bcc_ptr += 2; /* skip trailing null */
3664 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 strcpy(bcc_ptr, tree);
3666 bcc_ptr += strlen(tree) + 1;
3667 }
3668 strcpy(bcc_ptr, "?????");
3669 bcc_ptr += strlen("?????");
3670 bcc_ptr += 1;
3671 count = bcc_ptr - &pSMB->Password[0];
Qinglang Miao1a0e7f72020-07-25 16:56:01 +08003672 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 pSMB->ByteCount = cpu_to_le16(count);
3674
Steve French133672e2007-11-13 22:41:37 +00003675 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05003676 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01003679 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00003680 bool is_unicode;
3681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00003683 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 tcon->tid = smb_buffer_response->Tid;
3685 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05003686 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003687 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00003688 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3689 is_unicode = true;
3690 else
3691 is_unicode = false;
3692
Jeff Laytoncc20c032009-04-30 07:16:21 -04003693
Steve French50c2f752007-07-13 00:33:32 +00003694 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00003695 if (length == 3) {
3696 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3697 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003698 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01003699 tcon->ipc = true;
3700 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00003701 }
3702 } else if (length == 2) {
3703 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3704 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05003705 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00003706 }
3707 }
Steve French50c2f752007-07-13 00:33:32 +00003708 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04003709 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05003710 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04003711
3712 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04003713 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06003714 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00003715 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04003716 nls_codepage);
3717
Joe Perchesf96637b2013-05-04 22:12:25 -05003718 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003719
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003720 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00003721 (smb_buffer_response->WordCount == 7))
3722 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00003723 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3724 else
3725 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05003726 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 }
3728
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003729 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 return rc;
3731}
3732
Al Viro2e32cf52013-10-03 12:53:37 -04003733static void delayed_free(struct rcu_head *p)
3734{
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003735 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
3736
3737 unload_nls(cifs_sb->local_nls);
Ronnie Sahlbergc741cba2020-12-14 16:40:16 +10003738 smb3_cleanup_fs_context(cifs_sb->ctx);
Ronnie Sahlbergd17abdf72020-11-10 08:59:26 +10003739 kfree(cifs_sb);
Al Viro2e32cf52013-10-03 12:53:37 -04003740}
3741
Al Viro2a9b9952011-06-17 09:27:16 -04003742void
3743cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
Jeff Laytonb647c352010-10-28 11:16:44 -04003745 struct rb_root *root = &cifs_sb->tlink_tree;
3746 struct rb_node *node;
3747 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Jeff Layton2de970f2010-10-06 19:51:12 -04003749 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3750
Jeff Laytonb647c352010-10-28 11:16:44 -04003751 spin_lock(&cifs_sb->tlink_tree_lock);
3752 while ((node = rb_first(root))) {
3753 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3754 cifs_get_tlink(tlink);
3755 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3756 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00003757
Jeff Laytonb647c352010-10-28 11:16:44 -04003758 spin_unlock(&cifs_sb->tlink_tree_lock);
3759 cifs_put_tlink(tlink);
3760 spin_lock(&cifs_sb->tlink_tree_lock);
3761 }
3762 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003763
Aurelien Aptela6b50582016-05-25 19:59:09 +02003764 kfree(cifs_sb->prepath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003765#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac9f71102021-06-04 19:25:29 -03003766 dfs_cache_put_refsrv_sessions(&cifs_sb->dfs_mount_id);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02003767#endif
Al Viro2e32cf52013-10-03 12:53:37 -04003768 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00003769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003771int
3772cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
3774 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02003775 struct TCP_Server_Info *server = cifs_ses_server(ses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003777 if (!server->ops->need_neg || !server->ops->negotiate)
3778 return -ENOSYS;
3779
Jeff Layton198b5682010-04-24 07:57:48 -04003780 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003781 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04003782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003784 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04003785 if (rc == 0) {
3786 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04003787 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04003788 server->tcpStatus = CifsGood;
3789 else
3790 rc = -EHOSTDOWN;
3791 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
Steve French26b994f2008-08-06 05:11:33 +00003793
Jeff Layton198b5682010-04-24 07:57:48 -04003794 return rc;
3795}
Steve French26b994f2008-08-06 05:11:33 +00003796
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003797int
3798cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3799 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04003800{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003801 int rc = -ENOSYS;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02003802 struct TCP_Server_Info *server = cifs_ses_server(ses);
Jeff Layton198b5682010-04-24 07:57:48 -04003803
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003804 if (!ses->binding) {
3805 ses->capabilities = server->capabilities;
YANG LIed6b1922021-01-11 17:15:28 +08003806 if (!linuxExtEnabled)
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003807 ses->capabilities &= (~server->vals->cap_unix);
3808
3809 if (ses->auth_key.response) {
3810 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
3811 ses->auth_key.response);
3812 kfree(ses->auth_key.response);
3813 ses->auth_key.response = NULL;
3814 ses->auth_key.len = 0;
3815 }
3816 }
Steve French20418ac2009-04-30 16:13:32 +00003817
Joe Perchesf96637b2013-05-04 22:12:25 -05003818 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00003819 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003820
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003821 if (server->ops->sess_setup)
3822 rc = server->ops->sess_setup(xid, ses, nls_info);
3823
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05003824 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003825 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05003826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 return rc;
3828}
3829
Jeff Layton8a8798a2012-01-17 16:09:15 -05003830static int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003831cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003832{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003833 ctx->sectype = ses->sectype;
Jeff Layton3f618222013-06-12 19:52:14 -05003834
3835 /* krb5 is special, since we don't need username or pw */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003836 if (ctx->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003837 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003838
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003839 return cifs_set_cifscreds(ctx, ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003840}
3841
Steve French96daf2b2011-05-27 04:34:02 +00003842static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003843cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04003844{
Jeff Layton8a8798a2012-01-17 16:09:15 -05003845 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00003846 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3847 struct cifs_ses *ses;
3848 struct cifs_tcon *tcon = NULL;
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003849 struct smb3_fs_context *ctx;
Jeff Layton9d002df2010-10-06 19:51:11 -04003850
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003851 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3852 if (ctx == NULL)
Dan Carpenter803ab972012-01-24 11:39:22 +03003853 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04003854
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003855 ctx->local_nls = cifs_sb->local_nls;
3856 ctx->linux_uid = fsuid;
3857 ctx->cred_uid = fsuid;
3858 ctx->UNC = master_tcon->treeName;
3859 ctx->retry = master_tcon->retry;
3860 ctx->nocase = master_tcon->nocase;
3861 ctx->nohandlecache = master_tcon->nohandlecache;
3862 ctx->local_lease = master_tcon->local_lease;
3863 ctx->no_lease = master_tcon->no_lease;
3864 ctx->resilient = master_tcon->use_resilient;
3865 ctx->persistent = master_tcon->use_persistent;
3866 ctx->handle_timeout = master_tcon->handle_timeout;
3867 ctx->no_linux_ext = !master_tcon->unix_ext;
3868 ctx->linux_ext = master_tcon->posix_extensions;
3869 ctx->sectype = master_tcon->ses->sectype;
3870 ctx->sign = master_tcon->ses->sign;
3871 ctx->seal = master_tcon->seal;
Samuel Cabrero0ac4e292020-12-11 22:59:29 -06003872 ctx->witness = master_tcon->use_witness;
Jeff Layton9d002df2010-10-06 19:51:11 -04003873
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003874 rc = cifs_set_vol_auth(ctx, master_tcon->ses);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003875 if (rc) {
3876 tcon = ERR_PTR(rc);
3877 goto out;
3878 }
Jeff Layton9d002df2010-10-06 19:51:11 -04003879
3880 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303881 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003882 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303883 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003884
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003885 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003886 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00003887 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003888 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04003889 goto out;
3890 }
3891
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003892 tcon = cifs_get_tcon(ses, ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003893 if (IS_ERR(tcon)) {
3894 cifs_put_smb_ses(ses);
3895 goto out;
3896 }
3897
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003898 if (cap_unix(ses))
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003899 reset_cifs_unix_caps(0, tcon, NULL, ctx);
Steve Frenchb3266142018-05-20 23:41:10 -05003900
Jeff Layton9d002df2010-10-06 19:51:11 -04003901out:
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -06003902 kfree(ctx->username);
3903 kfree_sensitive(ctx->password);
3904 kfree(ctx);
Jeff Layton9d002df2010-10-06 19:51:11 -04003905
3906 return tcon;
3907}
3908
Steve French96daf2b2011-05-27 04:34:02 +00003909struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04003910cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
3911{
3912 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
3913}
3914
Jeff Laytonb647c352010-10-28 11:16:44 -04003915/* find and return a tlink with given uid */
3916static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003917tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04003918{
3919 struct rb_node *node = root->rb_node;
3920 struct tcon_link *tlink;
3921
3922 while (node) {
3923 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3924
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003925 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04003926 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003927 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04003928 node = node->rb_right;
3929 else
3930 return tlink;
3931 }
3932 return NULL;
3933}
3934
3935/* insert a tcon_link into the tree */
3936static void
3937tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
3938{
3939 struct rb_node **new = &(root->rb_node), *parent = NULL;
3940 struct tcon_link *tlink;
3941
3942 while (*new) {
3943 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
3944 parent = *new;
3945
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003946 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04003947 new = &((*new)->rb_left);
3948 else
3949 new = &((*new)->rb_right);
3950 }
3951
3952 rb_link_node(&new_tlink->tl_rbnode, parent, new);
3953 rb_insert_color(&new_tlink->tl_rbnode, root);
3954}
3955
Jeff Layton9d002df2010-10-06 19:51:11 -04003956/*
3957 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
3958 * current task.
3959 *
3960 * If the superblock doesn't refer to a multiuser mount, then just return
3961 * the master tcon for the mount.
3962 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05303963 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04003964 * exists, then check to see if it's pending construction. If it is then wait
3965 * for construction to complete. Once it's no longer pending, check to see if
3966 * it failed and either return an error or retry construction, depending on
3967 * the timeout.
3968 *
3969 * If one doesn't exist then insert a new tcon_link struct into the tree and
3970 * try to construct a new one.
3971 */
3972struct tcon_link *
3973cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
3974{
3975 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08003976 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04003977 struct tcon_link *tlink, *newtlink;
3978
3979 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
3980 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3981
3982 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04003983 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04003984 if (tlink)
3985 cifs_get_tlink(tlink);
3986 spin_unlock(&cifs_sb->tlink_tree_lock);
3987
3988 if (tlink == NULL) {
3989 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3990 if (newtlink == NULL)
3991 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04003992 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04003993 newtlink->tl_tcon = ERR_PTR(-EACCES);
3994 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
3995 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
3996 cifs_get_tlink(newtlink);
3997
Jeff Layton9d002df2010-10-06 19:51:11 -04003998 spin_lock(&cifs_sb->tlink_tree_lock);
3999 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004000 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004001 if (tlink) {
4002 cifs_get_tlink(tlink);
4003 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004004 kfree(newtlink);
4005 goto wait_for_construction;
4006 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004007 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004008 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4009 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004010 } else {
4011wait_for_construction:
4012 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004013 TASK_INTERRUPTIBLE);
4014 if (ret) {
4015 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004016 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004017 }
4018
4019 /* if it's good, return it */
4020 if (!IS_ERR(tlink->tl_tcon))
4021 return tlink;
4022
4023 /* return error if we tried this already recently */
4024 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4025 cifs_put_tlink(tlink);
4026 return ERR_PTR(-EACCES);
4027 }
4028
4029 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4030 goto wait_for_construction;
4031 }
4032
4033 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4034 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4035 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4036
4037 if (IS_ERR(tlink->tl_tcon)) {
4038 cifs_put_tlink(tlink);
4039 return ERR_PTR(-EACCES);
4040 }
4041
4042 return tlink;
4043}
Jeff Layton2de970f2010-10-06 19:51:12 -04004044
4045/*
4046 * periodic workqueue job that scans tcon_tree for a superblock and closes
4047 * out tcons.
4048 */
4049static void
4050cifs_prune_tlinks(struct work_struct *work)
4051{
4052 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4053 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004054 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004055 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004056 struct rb_node *tmp;
4057 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004058
Jeff Laytonb647c352010-10-28 11:16:44 -04004059 /*
4060 * Because we drop the spinlock in the loop in order to put the tlink
4061 * it's not guarded against removal of links from the tree. The only
4062 * places that remove entries from the tree are this function and
4063 * umounts. Because this function is non-reentrant and is canceled
4064 * before umount can proceed, this is safe.
4065 */
4066 spin_lock(&cifs_sb->tlink_tree_lock);
4067 node = rb_first(root);
4068 while (node != NULL) {
4069 tmp = node;
4070 node = rb_next(tmp);
4071 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4072
4073 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4074 atomic_read(&tlink->tl_count) != 0 ||
4075 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4076 continue;
4077
4078 cifs_get_tlink(tlink);
4079 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4080 rb_erase(tmp, root);
4081
Jeff Layton2de970f2010-10-06 19:51:12 -04004082 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004083 cifs_put_tlink(tlink);
4084 spin_lock(&cifs_sb->tlink_tree_lock);
4085 }
4086 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004087
Jeff Laytonda472fc2012-03-23 14:40:53 -04004088 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004089 TLINK_IDLE_EXPIRE);
4090}
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004091
4092#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004093static void mark_tcon_tcp_ses_for_reconnect(struct cifs_tcon *tcon)
4094{
4095 int i;
4096
4097 for (i = 0; i < tcon->ses->chan_count; i++) {
4098 spin_lock(&GlobalMid_Lock);
4099 if (tcon->ses->chans[i].server->tcpStatus != CifsExiting)
4100 tcon->ses->chans[i].server->tcpStatus = CifsNeedReconnect;
4101 spin_unlock(&GlobalMid_Lock);
4102 }
4103}
4104
4105/* Update dfs referral path of superblock */
4106static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
4107 const char *target)
4108{
4109 int rc = 0;
4110 size_t len = strlen(target);
4111 char *refpath, *npath;
4112
4113 if (unlikely(len < 2 || *target != '\\'))
4114 return -EINVAL;
4115
4116 if (target[1] == '\\') {
4117 len += 1;
4118 refpath = kmalloc(len, GFP_KERNEL);
4119 if (!refpath)
4120 return -ENOMEM;
4121
4122 scnprintf(refpath, len, "%s", target);
4123 } else {
4124 len += sizeof("\\");
4125 refpath = kmalloc(len, GFP_KERNEL);
4126 if (!refpath)
4127 return -ENOMEM;
4128
4129 scnprintf(refpath, len, "\\%s", target);
4130 }
4131
4132 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
4133 kfree(refpath);
4134
4135 if (IS_ERR(npath)) {
4136 rc = PTR_ERR(npath);
4137 } else {
4138 mutex_lock(&server->refpath_lock);
4139 kfree(server->leaf_fullpath);
4140 server->leaf_fullpath = npath;
4141 mutex_unlock(&server->refpath_lock);
4142 server->current_fullpath = server->leaf_fullpath;
4143 }
4144 return rc;
4145}
4146
4147static int target_share_matches_server(struct TCP_Server_Info *server, const char *tcp_host,
4148 size_t tcp_host_len, char *share, bool *target_match)
4149{
4150 int rc = 0;
4151 const char *dfs_host;
4152 size_t dfs_host_len;
4153
4154 *target_match = true;
4155 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
4156
4157 /* Check if hostnames or addresses match */
4158 if (dfs_host_len != tcp_host_len || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
4159 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
4160 dfs_host, (int)tcp_host_len, tcp_host);
4161 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
4162 if (rc)
4163 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
4164 }
4165 return rc;
4166}
4167
Steve French0e629042021-11-10 03:09:52 -06004168static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004169 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4170 struct dfs_cache_tgt_list *tl)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004171{
4172 int rc;
4173 struct TCP_Server_Info *server = tcon->ses->server;
4174 const struct smb_version_operations *ops = server->ops;
4175 struct cifs_tcon *ipc = tcon->ses->tcon_ipc;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004176 char *share = NULL, *prefix = NULL;
4177 const char *tcp_host;
4178 size_t tcp_host_len;
4179 struct dfs_cache_tgt_iterator *tit;
4180 bool target_match;
4181
4182 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
4183
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004184 tit = dfs_cache_get_tgt_iterator(tl);
4185 if (!tit) {
4186 rc = -ENOENT;
4187 goto out;
4188 }
4189
4190 /* Try to tree connect to all dfs targets */
4191 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
4192 const char *target = dfs_cache_get_tgt_name(tit);
4193 struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl);
4194
4195 kfree(share);
4196 kfree(prefix);
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004197 share = prefix = NULL;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004198
4199 /* Check if share matches with tcp ses */
4200 rc = dfs_cache_get_tgt_share(server->current_fullpath + 1, tit, &share, &prefix);
4201 if (rc) {
4202 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
4203 break;
4204 }
4205
4206 rc = target_share_matches_server(server, tcp_host, tcp_host_len, share,
4207 &target_match);
4208 if (rc)
4209 break;
4210 if (!target_match) {
4211 rc = -EHOSTUNREACH;
4212 continue;
4213 }
4214
4215 if (ipc->need_reconnect) {
4216 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4217 rc = ops->tree_connect(xid, ipc->ses, tree, ipc, cifs_sb->local_nls);
4218 if (rc)
4219 break;
4220 }
4221
4222 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
4223 if (!islink) {
4224 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4225 break;
4226 }
4227 /*
4228 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
4229 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
4230 * reconnect so either the demultiplex thread or the echo worker will reconnect to
4231 * newly resolved target.
4232 */
4233 if (dfs_cache_find(xid, tcon->ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004234 NULL, &ntl)) {
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004235 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4236 if (rc)
4237 continue;
4238 rc = dfs_cache_noreq_update_tgthint(server->current_fullpath + 1, tit);
4239 if (!rc)
4240 rc = cifs_update_super_prepath(cifs_sb, prefix);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004241 } else {
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004242 /* Target is another dfs share */
4243 rc = update_server_fullpath(server, cifs_sb, target);
4244 dfs_cache_free_tgts(tl);
4245
4246 if (!rc) {
4247 rc = -EREMOTE;
4248 list_replace_init(&ntl.tl_list, &tl->tl_list);
4249 } else
4250 dfs_cache_free_tgts(&ntl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004251 }
4252 break;
4253 }
4254
4255out:
4256 kfree(share);
4257 kfree(prefix);
4258
4259 return rc;
4260}
4261
Steve French0e629042021-11-10 03:09:52 -06004262static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004263 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4264 struct dfs_cache_tgt_list *tl)
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004265{
4266 int rc;
4267 int num_links = 0;
4268 struct TCP_Server_Info *server = tcon->ses->server;
4269
4270 do {
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004271 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004272 if (!rc || rc != -EREMOTE)
4273 break;
4274 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
4275 /*
4276 * If we couldn't tree connect to any targets from last referral path, then retry from
4277 * original referral path.
4278 */
4279 if (rc && server->current_fullpath != server->origin_fullpath) {
4280 server->current_fullpath = server->origin_fullpath;
4281 mark_tcon_tcp_ses_for_reconnect(tcon);
4282 }
4283
4284 dfs_cache_free_tgts(tl);
4285 return rc;
4286}
4287
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004288int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4289{
4290 int rc;
4291 struct TCP_Server_Info *server = tcon->ses->server;
4292 const struct smb_version_operations *ops = server->ops;
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004293 struct super_block *sb = NULL;
4294 struct cifs_sb_info *cifs_sb;
4295 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004296 char *tree;
Paulo Alcantara11375a52020-07-21 09:36:43 -03004297 struct dfs_info3_param ref = {0};
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004298
4299 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
4300 if (!tree)
4301 return -ENOMEM;
4302
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004303 if (tcon->ipc) {
4304 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4305 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004306 goto out;
4307 }
4308
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004309 sb = cifs_get_tcp_super(server);
4310 if (IS_ERR(sb)) {
4311 rc = PTR_ERR(sb);
4312 cifs_dbg(VFS, "%s: could not find superblock: %d\n", __func__, rc);
4313 goto out;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004314 }
4315
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004316 cifs_sb = CIFS_SB(sb);
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03004317
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004318 /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */
4319 if (!server->current_fullpath ||
4320 dfs_cache_noreq_find(server->current_fullpath + 1, &ref, &tl)) {
4321 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, cifs_sb->local_nls);
4322 goto out;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004323 }
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004324
Paulo Alcantara7f28af92021-11-12 15:16:08 -03004325 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
4326 &tl);
4327 free_dfs_info_param(&ref);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004328
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004329out:
4330 kfree(tree);
Paulo Alcantarac88f7dc2021-11-03 13:53:29 -03004331 cifs_put_tcp_super(sb);
4332
Stefan Metzmacher565674d2020-07-21 09:36:38 -03004333 return rc;
4334}
4335#else
4336int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4337{
4338 const struct smb_version_operations *ops = tcon->ses->server->ops;
4339
4340 return ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4341}
4342#endif