Steve French | 929be90 | 2021-06-18 00:31:49 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Steve French | 1080ef7 | 2011-02-24 18:07:19 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2002,2011 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/net.h> |
| 10 | #include <linux/string.h> |
Eric Biggers | dc92027 | 2020-03-08 22:58:20 -0700 | [diff] [blame] | 11 | #include <linux/sched/mm.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 12 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/list.h> |
| 14 | #include <linux/wait.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/ctype.h> |
| 18 | #include <linux/utsname.h> |
| 19 | #include <linux/mempool.h> |
Steve French | b8643e1 | 2005-04-28 22:41:07 -0700 | [diff] [blame] | 20 | #include <linux/delay.h> |
Steve French | f191401 | 2005-08-18 09:37:34 -0700 | [diff] [blame] | 21 | #include <linux/completion.h> |
Igor Mammedov | aaf737a | 2007-04-03 19:16:43 +0000 | [diff] [blame] | 22 | #include <linux/kthread.h> |
Steve French | 0ae0efa | 2005-10-10 10:57:19 -0700 | [diff] [blame] | 23 | #include <linux/pagevec.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 24 | #include <linux/freezer.h> |
Igor Mammedov | 5c2503a | 2009-04-21 19:31:05 +0400 | [diff] [blame] | 25 | #include <linux/namei.h> |
Andrew Lunn | c6e970a | 2017-03-28 23:45:06 +0200 | [diff] [blame] | 26 | #include <linux/uuid.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/processor.h> |
Jeff Layton | 50b64e3 | 2009-06-02 06:55:20 -0400 | [diff] [blame] | 29 | #include <linux/inet.h> |
Paul Gortmaker | 143cb49 | 2011-07-01 14:23:34 -0400 | [diff] [blame] | 30 | #include <linux/module.h> |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 31 | #include <keys/user-type.h> |
Steve French | 0e2beda | 2009-01-30 21:24:41 +0000 | [diff] [blame] | 32 | #include <net/ipv6.h> |
Sachin Prabhu | 8830d7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 33 | #include <linux/parser.h> |
Christoph Hellwig | 2f8b544 | 2016-11-01 07:40:13 -0600 | [diff] [blame] | 34 | #include <linux/bvec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #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 Jayaraman | 488f1d2d | 2010-07-05 18:12:15 +0530 | [diff] [blame] | 44 | #include "fscache.h" |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 45 | #include "smb2proto.h" |
Long Li | 2f89464 | 2017-11-22 17:38:34 -0700 | [diff] [blame] | 46 | #include "smbdirect.h" |
Paulo Alcantara | 1c78022 | 2018-11-14 16:24:03 -0200 | [diff] [blame] | 47 | #include "dns_resolve.h" |
| 48 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 49 | #include "dfs_cache.h" |
| 50 | #endif |
Ronnie Sahlberg | 5c6e5aa | 2020-10-21 10:37:11 +1000 | [diff] [blame] | 51 | #include "fs_context.h" |
Samuel Cabrero | bf80e5d | 2020-11-30 19:02:51 +0100 | [diff] [blame] | 52 | #include "cifs_swn.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | extern mempool_t *cifs_req_poolp; |
Steve French | f92a720 | 2018-05-24 04:11:07 -0500 | [diff] [blame] | 55 | extern bool disable_legacy_dialects; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 57 | /* FIXME: should these be tunable? */ |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 58 | #define TLINK_ERROR_EXPIRE (1 * HZ) |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 59 | #define TLINK_IDLE_EXPIRE (600 * HZ) |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 60 | |
Rohith Surabattula | 8e670f7 | 2020-09-18 05:37:28 +0000 | [diff] [blame] | 61 | /* Drop the connection to not overload the server */ |
| 62 | #define NUM_STATUS_IO_TIMEOUT 5 |
| 63 | |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 64 | static int ip_connect(struct TCP_Server_Info *server); |
| 65 | static int generic_ip_connect(struct TCP_Server_Info *server); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 66 | static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink); |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 67 | static void cifs_prune_tlinks(struct work_struct *work); |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 68 | |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 69 | /* |
| 70 | * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may |
| 71 | * get their ip addresses changed at some point. |
| 72 | * |
| 73 | * This should be called with server->srv_mutex held. |
| 74 | */ |
Samuel Cabrero | 7d6535b | 2020-11-30 19:02:55 +0100 | [diff] [blame] | 75 | static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server) |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 76 | { |
| 77 | int rc; |
| 78 | int len; |
| 79 | char *unc, *ipaddr = NULL; |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 80 | time64_t expiry, now; |
| 81 | unsigned long ttl = SMB_DNS_RESOLVE_INTERVAL_DEFAULT; |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 82 | |
| 83 | if (!server->hostname) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | len = strlen(server->hostname) + 3; |
| 87 | |
| 88 | unc = kmalloc(len, GFP_KERNEL); |
| 89 | if (!unc) { |
| 90 | cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__); |
| 91 | return -ENOMEM; |
| 92 | } |
Ronnie Sahlberg | 74ea5f9 | 2019-02-09 09:51:11 +1000 | [diff] [blame] | 93 | scnprintf(unc, len, "\\\\%s", server->hostname); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 94 | |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 95 | rc = dns_resolve_server_name_to_ip(unc, &ipaddr, &expiry); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 96 | kfree(unc); |
| 97 | |
| 98 | if (rc < 0) { |
| 99 | cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n", |
| 100 | __func__, server->hostname, rc); |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 101 | goto requeue_resolve; |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 102 | } |
| 103 | |
Ronnie Sahlberg | fada37f | 2020-04-21 12:37:39 +1000 | [diff] [blame] | 104 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 105 | rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr, |
| 106 | strlen(ipaddr)); |
Ronnie Sahlberg | fada37f | 2020-04-21 12:37:39 +1000 | [diff] [blame] | 107 | spin_unlock(&cifs_tcp_ses_lock); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 108 | kfree(ipaddr); |
| 109 | |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 110 | /* rc == 1 means success here */ |
| 111 | if (rc) { |
| 112 | now = ktime_get_real_seconds(); |
| 113 | if (expiry && expiry > now) |
| 114 | /* |
| 115 | * To make sure we don't use the cached entry, retry 1s |
| 116 | * after expiry. |
| 117 | */ |
| 118 | ttl = (expiry - now + 1); |
| 119 | } |
| 120 | rc = !rc ? -1 : 0; |
| 121 | |
| 122 | requeue_resolve: |
| 123 | cifs_dbg(FYI, "%s: next dns resolution scheduled for %lu seconds in the future\n", |
| 124 | __func__, ttl); |
| 125 | mod_delayed_work(cifsiod_wq, &server->resolve, (ttl * HZ)); |
| 126 | |
| 127 | return rc; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static void cifs_resolve_server(struct work_struct *work) |
| 132 | { |
| 133 | int rc; |
| 134 | struct TCP_Server_Info *server = container_of(work, |
| 135 | struct TCP_Server_Info, resolve.work); |
| 136 | |
| 137 | mutex_lock(&server->srv_mutex); |
| 138 | |
| 139 | /* |
| 140 | * Resolve the hostname again to make sure that IP address is up-to-date. |
| 141 | */ |
| 142 | rc = reconn_set_ipaddr_from_hostname(server); |
| 143 | if (rc) { |
| 144 | cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n", |
| 145 | __func__, rc); |
| 146 | } |
| 147 | |
| 148 | mutex_unlock(&server->srv_mutex); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 149 | } |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 150 | |
Shyam Prasad N | 4e456b3 | 2021-03-31 14:35:24 +0000 | [diff] [blame] | 151 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 152 | /* These functions must be called with server->srv_mutex held */ |
Paulo Alcantara | 7d397a0 | 2020-07-21 09:36:40 -0300 | [diff] [blame] | 153 | static void reconn_set_next_dfs_target(struct TCP_Server_Info *server, |
| 154 | struct cifs_sb_info *cifs_sb, |
| 155 | struct dfs_cache_tgt_list *tgt_list, |
| 156 | struct dfs_cache_tgt_iterator **tgt_it) |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 157 | { |
| 158 | const char *name; |
Samuel Cabrero | 7d6535b | 2020-11-30 19:02:55 +0100 | [diff] [blame] | 159 | int rc; |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 160 | |
Paulo Alcantara | 7d397a0 | 2020-07-21 09:36:40 -0300 | [diff] [blame] | 161 | if (!cifs_sb || !cifs_sb->origin_fullpath) |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 162 | return; |
| 163 | |
| 164 | if (!*tgt_it) { |
| 165 | *tgt_it = dfs_cache_get_tgt_iterator(tgt_list); |
| 166 | } else { |
| 167 | *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it); |
| 168 | if (!*tgt_it) |
| 169 | *tgt_it = dfs_cache_get_tgt_iterator(tgt_list); |
| 170 | } |
| 171 | |
| 172 | cifs_dbg(FYI, "%s: UNC: %s\n", __func__, cifs_sb->origin_fullpath); |
| 173 | |
| 174 | name = dfs_cache_get_tgt_name(*tgt_it); |
| 175 | |
| 176 | kfree(server->hostname); |
| 177 | |
| 178 | server->hostname = extract_hostname(name); |
Dan Carpenter | 8428817 | 2019-01-05 15:25:29 +0300 | [diff] [blame] | 179 | if (IS_ERR(server->hostname)) { |
| 180 | cifs_dbg(FYI, |
| 181 | "%s: failed to extract hostname from target: %ld\n", |
| 182 | __func__, PTR_ERR(server->hostname)); |
Samuel Cabrero | 0bf1baf | 2020-12-18 10:29:49 +0100 | [diff] [blame] | 183 | return; |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 184 | } |
Samuel Cabrero | 7d6535b | 2020-11-30 19:02:55 +0100 | [diff] [blame] | 185 | |
| 186 | rc = reconn_set_ipaddr_from_hostname(server); |
| 187 | if (rc) { |
| 188 | cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n", |
| 189 | __func__, rc); |
| 190 | } |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static inline int reconn_setup_dfs_targets(struct cifs_sb_info *cifs_sb, |
Paulo Alcantara | baf3f08 | 2020-05-19 15:38:29 -0300 | [diff] [blame] | 194 | struct dfs_cache_tgt_list *tl) |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 195 | { |
| 196 | if (!cifs_sb->origin_fullpath) |
| 197 | return -EOPNOTSUPP; |
| 198 | return dfs_cache_noreq_find(cifs_sb->origin_fullpath + 1, NULL, tl); |
| 199 | } |
| 200 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame^] | 202 | /** |
| 203 | * Mark all sessions and tcons for reconnect. |
Jeff Layton | d5c5605 | 2008-12-01 18:42:33 -0500 | [diff] [blame] | 204 | * |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame^] | 205 | * @server needs to be previously set to CifsNeedReconnect. |
Jeff Layton | d5c5605 | 2008-12-01 18:42:33 -0500 | [diff] [blame] | 206 | */ |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame^] | 207 | static void cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 209 | struct list_head *tmp, *tmp2; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 210 | struct cifs_ses *ses; |
| 211 | struct cifs_tcon *tcon; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 212 | struct mid_q_entry *mid_entry; |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 213 | struct list_head retry_list; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | server->maxBuf = 0; |
Pavel Shilovsky | aa24d1e | 2011-12-27 16:23:34 +0400 | [diff] [blame] | 216 | server->max_read = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Steve French | 6e4d3bb | 2018-09-22 11:25:04 -0500 | [diff] [blame] | 218 | cifs_dbg(FYI, "Mark tcp session as need reconnect\n"); |
Shyam Prasad N | 6d82c27 | 2021-02-03 23:20:46 -0800 | [diff] [blame] | 219 | trace_smb3_reconnect(server->CurrentMid, server->conn_id, server->hostname); |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame^] | 220 | /* |
| 221 | * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they |
| 222 | * are not used until reconnected. |
| 223 | */ |
| 224 | cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n", __func__); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 225 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 226 | list_for_each(tmp, &server->smb_ses_list) { |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 227 | ses = list_entry(tmp, struct cifs_ses, smb_ses_list); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 228 | ses->need_reconnect = true; |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 229 | list_for_each(tmp2, &ses->tcon_list) { |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 230 | tcon = list_entry(tmp2, struct cifs_tcon, tcon_list); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 231 | tcon->need_reconnect = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 233 | if (ses->tcon_ipc) |
| 234 | ses->tcon_ipc->need_reconnect = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 236 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 2b84a36c | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* do not want to be sending data on a socket we are freeing */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 239 | cifs_dbg(FYI, "%s: tearing down socket\n", __func__); |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 240 | mutex_lock(&server->srv_mutex); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 241 | if (server->ssocket) { |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame^] | 242 | cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state, |
| 243 | server->ssocket->flags); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 244 | kernel_sock_shutdown(server->ssocket, SHUT_WR); |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame^] | 245 | cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state, |
| 246 | server->ssocket->flags); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 247 | sock_release(server->ssocket); |
| 248 | server->ssocket = NULL; |
| 249 | } |
| 250 | server->sequence_number = 0; |
| 251 | server->session_estab = false; |
| 252 | kfree(server->session_key.response); |
| 253 | server->session_key.response = NULL; |
| 254 | server->session_key.len = 0; |
| 255 | server->lstrp = jiffies; |
Long Li | 214bab4 | 2019-04-05 21:36:35 +0000 | [diff] [blame] | 256 | |
| 257 | /* mark submitted MIDs for retry and issue callback */ |
| 258 | INIT_LIST_HEAD(&retry_list); |
| 259 | cifs_dbg(FYI, "%s: moving mids to private list\n", __func__); |
| 260 | spin_lock(&GlobalMid_Lock); |
| 261 | list_for_each_safe(tmp, tmp2, &server->pending_mid_q) { |
| 262 | mid_entry = list_entry(tmp, struct mid_q_entry, qhead); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 263 | kref_get(&mid_entry->refcount); |
Long Li | 214bab4 | 2019-04-05 21:36:35 +0000 | [diff] [blame] | 264 | if (mid_entry->mid_state == MID_REQUEST_SUBMITTED) |
| 265 | mid_entry->mid_state = MID_RETRY_NEEDED; |
| 266 | list_move(&mid_entry->qhead, &retry_list); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 267 | mid_entry->mid_flags |= MID_DELETED; |
Long Li | 214bab4 | 2019-04-05 21:36:35 +0000 | [diff] [blame] | 268 | } |
| 269 | spin_unlock(&GlobalMid_Lock); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 270 | mutex_unlock(&server->srv_mutex); |
Long Li | 214bab4 | 2019-04-05 21:36:35 +0000 | [diff] [blame] | 271 | |
| 272 | cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__); |
| 273 | list_for_each_safe(tmp, tmp2, &retry_list) { |
| 274 | mid_entry = list_entry(tmp, struct mid_q_entry, qhead); |
| 275 | list_del_init(&mid_entry->qhead); |
| 276 | mid_entry->callback(mid_entry); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 277 | cifs_mid_q_entry_release(mid_entry); |
Long Li | 214bab4 | 2019-04-05 21:36:35 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 280 | if (cifs_rdma_enabled(server)) { |
| 281 | mutex_lock(&server->srv_mutex); |
Long Li | 050b8c3 | 2019-04-04 11:35:42 -0500 | [diff] [blame] | 282 | smbd_destroy(server); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 283 | mutex_unlock(&server->srv_mutex); |
| 284 | } |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame^] | 285 | } |
| 286 | |
| 287 | /* |
| 288 | * cifs tcp session reconnection |
| 289 | * |
| 290 | * mark tcp session as reconnecting so temporarily locked |
| 291 | * mark all smb sessions as reconnecting for tcp session |
| 292 | * reconnect tcp session |
| 293 | * wake up waiters on reconnection? - (not needed currently) |
| 294 | */ |
| 295 | int |
| 296 | cifs_reconnect(struct TCP_Server_Info *server) |
| 297 | { |
| 298 | int rc = 0; |
| 299 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 300 | struct super_block *sb = NULL; |
| 301 | struct cifs_sb_info *cifs_sb = NULL; |
| 302 | struct dfs_cache_tgt_list tgt_list = DFS_CACHE_TGT_LIST_INIT(tgt_list); |
| 303 | struct dfs_cache_tgt_iterator *tgt_it = NULL; |
| 304 | #endif |
| 305 | |
| 306 | spin_lock(&GlobalMid_Lock); |
| 307 | server->nr_targets = 1; |
| 308 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 309 | spin_unlock(&GlobalMid_Lock); |
| 310 | sb = cifs_get_tcp_super(server); |
| 311 | if (IS_ERR(sb)) { |
| 312 | rc = PTR_ERR(sb); |
| 313 | cifs_dbg(FYI, "%s: will not do DFS failover: rc = %d\n", |
| 314 | __func__, rc); |
| 315 | sb = NULL; |
| 316 | } else { |
| 317 | cifs_sb = CIFS_SB(sb); |
| 318 | rc = reconn_setup_dfs_targets(cifs_sb, &tgt_list); |
| 319 | if (rc) { |
| 320 | cifs_sb = NULL; |
| 321 | if (rc != -EOPNOTSUPP) { |
| 322 | cifs_server_dbg(VFS, "%s: no target servers for DFS failover\n", |
| 323 | __func__); |
| 324 | } |
| 325 | } else { |
| 326 | server->nr_targets = dfs_cache_get_nr_tgts(&tgt_list); |
| 327 | } |
| 328 | } |
| 329 | cifs_dbg(FYI, "%s: will retry %d target(s)\n", __func__, |
| 330 | server->nr_targets); |
| 331 | spin_lock(&GlobalMid_Lock); |
| 332 | #endif |
| 333 | if (server->tcpStatus == CifsExiting) { |
| 334 | /* the demux thread will exit normally next time through the loop */ |
| 335 | spin_unlock(&GlobalMid_Lock); |
| 336 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 337 | dfs_cache_free_tgts(&tgt_list); |
| 338 | cifs_put_tcp_super(sb); |
| 339 | #endif |
| 340 | wake_up(&server->response_q); |
| 341 | return rc; |
| 342 | } else |
| 343 | server->tcpStatus = CifsNeedReconnect; |
| 344 | spin_unlock(&GlobalMid_Lock); |
| 345 | |
| 346 | cifs_mark_tcp_ses_conns_for_reconnect(server); |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 347 | |
Jeff Layton | 7fdbaa1 | 2011-06-10 16:14:57 -0400 | [diff] [blame] | 348 | do { |
Steve French | 6c3d890 | 2006-07-31 22:46:20 +0000 | [diff] [blame] | 349 | try_to_freeze(); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 350 | |
Jeff Layton | 73e216a | 2013-09-05 08:38:10 -0400 | [diff] [blame] | 351 | mutex_lock(&server->srv_mutex); |
Samuel Cabrero | 121d947 | 2020-11-30 19:02:56 +0100 | [diff] [blame] | 352 | |
Samuel Cabrero | 121d947 | 2020-11-30 19:02:56 +0100 | [diff] [blame] | 353 | |
Aurelien Aptel | b7fd0fa | 2021-04-09 16:31:37 +0200 | [diff] [blame] | 354 | if (!cifs_swn_set_server_dstaddr(server)) { |
Paulo Alcantara | aaa3aef | 2020-05-19 15:38:27 -0300 | [diff] [blame] | 355 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Shyam Prasad N | 4e456b3 | 2021-03-31 14:35:24 +0000 | [diff] [blame] | 356 | if (cifs_sb && cifs_sb->origin_fullpath) |
Samuel Cabrero | 121d947 | 2020-11-30 19:02:56 +0100 | [diff] [blame] | 357 | /* |
| 358 | * Set up next DFS target server (if any) for reconnect. If DFS |
| 359 | * feature is disabled, then we will retry last server we |
| 360 | * connected to before. |
| 361 | */ |
| 362 | reconn_set_next_dfs_target(server, cifs_sb, &tgt_list, &tgt_it); |
Shyam Prasad N | 4e456b3 | 2021-03-31 14:35:24 +0000 | [diff] [blame] | 363 | else { |
Samuel Cabrero | 121d947 | 2020-11-30 19:02:56 +0100 | [diff] [blame] | 364 | #endif |
Shyam Prasad N | 4e456b3 | 2021-03-31 14:35:24 +0000 | [diff] [blame] | 365 | /* |
| 366 | * Resolve the hostname again to make sure that IP address is up-to-date. |
| 367 | */ |
| 368 | rc = reconn_set_ipaddr_from_hostname(server); |
| 369 | if (rc) { |
| 370 | cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n", |
| 371 | __func__, rc); |
| 372 | } |
| 373 | |
| 374 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 375 | } |
| 376 | #endif |
| 377 | |
Samuel Cabrero | 121d947 | 2020-11-30 19:02:56 +0100 | [diff] [blame] | 378 | |
Samuel Cabrero | 121d947 | 2020-11-30 19:02:56 +0100 | [diff] [blame] | 379 | } |
Paulo Alcantara | aaa3aef | 2020-05-19 15:38:27 -0300 | [diff] [blame] | 380 | |
Long Li | 781a805 | 2017-11-22 17:38:36 -0700 | [diff] [blame] | 381 | if (cifs_rdma_enabled(server)) |
| 382 | rc = smbd_reconnect(server); |
| 383 | else |
| 384 | rc = generic_ip_connect(server); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 385 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 386 | cifs_dbg(FYI, "reconnect error %d\n", rc); |
Federico Sauter | 4afe260 | 2015-03-17 17:45:28 +0100 | [diff] [blame] | 387 | mutex_unlock(&server->srv_mutex); |
Steve French | 0cb766a | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 388 | msleep(3000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } else { |
| 390 | atomic_inc(&tcpSesReconnectCount); |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 391 | set_credits(server, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | spin_lock(&GlobalMid_Lock); |
Jeff Layton | 469ee61 | 2008-10-16 18:46:39 +0000 | [diff] [blame] | 393 | if (server->tcpStatus != CifsExiting) |
Steve French | fd88ce9 | 2011-04-12 01:01:14 +0000 | [diff] [blame] | 394 | server->tcpStatus = CifsNeedNegotiate; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 395 | spin_unlock(&GlobalMid_Lock); |
Aurelien Aptel | b7fd0fa | 2021-04-09 16:31:37 +0200 | [diff] [blame] | 396 | cifs_swn_reset_server_dstaddr(server); |
Federico Sauter | 4afe260 | 2015-03-17 17:45:28 +0100 | [diff] [blame] | 397 | mutex_unlock(&server->srv_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
Jeff Layton | 7fdbaa1 | 2011-06-10 16:14:57 -0400 | [diff] [blame] | 399 | } while (server->tcpStatus == CifsNeedReconnect); |
Jeff Layton | 2b84a36c | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 400 | |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 401 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 402 | if (tgt_it) { |
| 403 | rc = dfs_cache_noreq_update_tgthint(cifs_sb->origin_fullpath + 1, |
| 404 | tgt_it); |
| 405 | if (rc) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 406 | cifs_server_dbg(VFS, "%s: failed to update DFS target hint: rc = %d\n", |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 407 | __func__, rc); |
| 408 | } |
Paulo Alcantara | 2332440 | 2018-11-20 14:37:18 -0200 | [diff] [blame] | 409 | dfs_cache_free_tgts(&tgt_list); |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 410 | } |
Paulo Alcantara (SUSE) | 8354d88 | 2019-11-22 12:30:51 -0300 | [diff] [blame] | 411 | |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 412 | cifs_put_tcp_super(sb); |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 413 | #endif |
Sachin Prabhu | b8c6001 | 2016-10-20 19:52:24 -0400 | [diff] [blame] | 414 | if (server->tcpStatus == CifsNeedNegotiate) |
| 415 | mod_delayed_work(cifsiod_wq, &server->echo, 0); |
| 416 | |
Stefan Metzmacher | e2e8751 | 2020-02-24 14:31:02 -0600 | [diff] [blame] | 417 | wake_up(&server->response_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return rc; |
| 419 | } |
| 420 | |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 421 | static void |
| 422 | cifs_echo_request(struct work_struct *work) |
| 423 | { |
| 424 | int rc; |
| 425 | struct TCP_Server_Info *server = container_of(work, |
| 426 | struct TCP_Server_Info, echo.work); |
Sachin Prabhu | b8c6001 | 2016-10-20 19:52:24 -0400 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | * We cannot send an echo if it is disabled. |
| 430 | * Also, no need to ping if we got a response recently. |
Jeff Layton | 247ec9b | 2011-02-04 17:09:50 -0500 | [diff] [blame] | 431 | */ |
Steve French | 4fcd181 | 2016-06-22 20:12:05 -0500 | [diff] [blame] | 432 | |
| 433 | if (server->tcpStatus == CifsNeedReconnect || |
Sachin Prabhu | b8c6001 | 2016-10-20 19:52:24 -0400 | [diff] [blame] | 434 | server->tcpStatus == CifsExiting || |
| 435 | server->tcpStatus == CifsNew || |
Pavel Shilovsky | f6d7617 | 2012-05-25 14:47:16 +0400 | [diff] [blame] | 436 | (server->ops->can_echo && !server->ops->can_echo(server)) || |
Shyam Prasad N | 5b2abda | 2021-05-01 16:17:07 +0000 | [diff] [blame] | 437 | time_before(jiffies, server->lstrp + server->echo_interval - HZ)) |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 438 | goto requeue_echo; |
| 439 | |
Pavel Shilovsky | f6d7617 | 2012-05-25 14:47:16 +0400 | [diff] [blame] | 440 | rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS; |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 441 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 442 | cifs_dbg(FYI, "Unable to send echo request to server: %s\n", |
| 443 | server->hostname); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 444 | |
Samuel Cabrero | 21077c6 | 2020-11-30 19:02:54 +0100 | [diff] [blame] | 445 | /* Check witness registrations */ |
| 446 | cifs_swn_check(); |
Samuel Cabrero | 21077c6 | 2020-11-30 19:02:54 +0100 | [diff] [blame] | 447 | |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 448 | requeue_echo: |
Sachin Prabhu | b8c6001 | 2016-10-20 19:52:24 -0400 | [diff] [blame] | 449 | queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 450 | } |
| 451 | |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 452 | static bool |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 453 | allocate_buffers(struct TCP_Server_Info *server) |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 454 | { |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 455 | if (!server->bigbuf) { |
| 456 | server->bigbuf = (char *)cifs_buf_get(); |
| 457 | if (!server->bigbuf) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 458 | cifs_server_dbg(VFS, "No memory for large SMB response\n"); |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 459 | msleep(3000); |
| 460 | /* retry will check if exiting */ |
| 461 | return false; |
| 462 | } |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 463 | } else if (server->large_buf) { |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 464 | /* we are reusing a dirty large buf, clear its start */ |
Pavel Shilovsky | 1887f60 | 2012-05-17 12:45:31 +0400 | [diff] [blame] | 465 | memset(server->bigbuf, 0, HEADER_SIZE(server)); |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 466 | } |
| 467 | |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 468 | if (!server->smallbuf) { |
| 469 | server->smallbuf = (char *)cifs_small_buf_get(); |
| 470 | if (!server->smallbuf) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 471 | cifs_server_dbg(VFS, "No memory for SMB response\n"); |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 472 | msleep(1000); |
| 473 | /* retry will check if exiting */ |
| 474 | return false; |
| 475 | } |
| 476 | /* beginning of smb buffer is cleared in our buf_get */ |
| 477 | } else { |
| 478 | /* if existing small buf clear beginning */ |
Pavel Shilovsky | 1887f60 | 2012-05-17 12:45:31 +0400 | [diff] [blame] | 479 | memset(server->smallbuf, 0, HEADER_SIZE(server)); |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 480 | } |
| 481 | |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 482 | return true; |
| 483 | } |
| 484 | |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 485 | static bool |
| 486 | server_unresponsive(struct TCP_Server_Info *server) |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 487 | { |
Pavel Shilovsky | 6dae51a | 2012-02-21 16:50:23 +0300 | [diff] [blame] | 488 | /* |
Ronnie Sahlberg | f2caf90 | 2019-07-06 06:52:46 +1000 | [diff] [blame] | 489 | * We need to wait 3 echo intervals to make sure we handle such |
Pavel Shilovsky | 6dae51a | 2012-02-21 16:50:23 +0300 | [diff] [blame] | 490 | * situations right: |
| 491 | * 1s client sends a normal SMB request |
Ronnie Sahlberg | becc2ba | 2019-07-24 11:43:49 +1000 | [diff] [blame] | 492 | * 2s client gets a response |
Pavel Shilovsky | 6dae51a | 2012-02-21 16:50:23 +0300 | [diff] [blame] | 493 | * 30s echo workqueue job pops, and decides we got a response recently |
| 494 | * and don't need to send another |
| 495 | * ... |
| 496 | * 65s kernel_recvmsg times out, and we see that we haven't gotten |
| 497 | * a response in >60s. |
| 498 | */ |
Samuel Cabrero | 76e7527 | 2017-07-11 12:44:39 +0200 | [diff] [blame] | 499 | if ((server->tcpStatus == CifsGood || |
| 500 | server->tcpStatus == CifsNeedNegotiate) && |
Shyam Prasad N | f491664 | 2021-04-29 07:53:18 +0000 | [diff] [blame] | 501 | (!server->ops->can_echo || server->ops->can_echo(server)) && |
Ronnie Sahlberg | f2caf90 | 2019-07-06 06:52:46 +1000 | [diff] [blame] | 502 | time_after(jiffies, server->lstrp + 3 * server->echo_interval)) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 503 | cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n", |
| 504 | (3 * server->echo_interval) / HZ); |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 505 | cifs_reconnect(server); |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 506 | return true; |
| 507 | } |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 508 | |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 509 | return false; |
| 510 | } |
| 511 | |
Pavel Shilovsky | ef68e83 | 2019-01-18 17:25:36 -0800 | [diff] [blame] | 512 | static inline bool |
| 513 | zero_credits(struct TCP_Server_Info *server) |
| 514 | { |
| 515 | int val; |
| 516 | |
| 517 | spin_lock(&server->req_lock); |
| 518 | val = server->credits + server->echo_credits + server->oplock_credits; |
| 519 | if (server->in_flight == 0 && val == 0) { |
| 520 | spin_unlock(&server->req_lock); |
| 521 | return true; |
| 522 | } |
| 523 | spin_unlock(&server->req_lock); |
| 524 | return false; |
| 525 | } |
| 526 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 527 | static int |
| 528 | cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg) |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 529 | { |
Jeff Layton | a52c1eb | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 530 | int length = 0; |
| 531 | int total_read; |
Jeff Layton | 42c4dfc | 2011-10-19 15:28:17 -0400 | [diff] [blame] | 532 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 533 | smb_msg->msg_control = NULL; |
| 534 | smb_msg->msg_controllen = 0; |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 535 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 536 | for (total_read = 0; msg_data_left(smb_msg); total_read += length) { |
Jeff Layton | 95edcff | 2011-12-01 20:22:41 -0500 | [diff] [blame] | 537 | try_to_freeze(); |
| 538 | |
Pavel Shilovsky | ef68e83 | 2019-01-18 17:25:36 -0800 | [diff] [blame] | 539 | /* reconnect if no credits and no requests in flight */ |
| 540 | if (zero_credits(server)) { |
| 541 | cifs_reconnect(server); |
| 542 | return -ECONNABORTED; |
| 543 | } |
| 544 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 545 | if (server_unresponsive(server)) |
| 546 | return -ECONNABORTED; |
Long Li | 2fef137 | 2017-11-22 17:38:41 -0700 | [diff] [blame] | 547 | if (cifs_rdma_enabled(server) && server->smbd_conn) |
| 548 | length = smbd_recv(server->smbd_conn, smb_msg); |
| 549 | else |
| 550 | length = sock_recvmsg(server->ssocket, smb_msg, 0); |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 551 | |
| 552 | if (server->tcpStatus == CifsExiting) |
| 553 | return -ESHUTDOWN; |
| 554 | |
| 555 | if (server->tcpStatus == CifsNeedReconnect) { |
| 556 | cifs_reconnect(server); |
| 557 | return -ECONNABORTED; |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 558 | } |
| 559 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 560 | if (length == -ERESTARTSYS || |
| 561 | length == -EAGAIN || |
| 562 | length == -EINTR) { |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 563 | /* |
| 564 | * Minimum sleep to prevent looping, allowing socket |
| 565 | * to clear and app threads to set tcpStatus |
| 566 | * CifsNeedReconnect if server hung. |
| 567 | */ |
| 568 | usleep_range(1000, 2000); |
| 569 | length = 0; |
Jeff Layton | a52c1eb | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 570 | continue; |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | if (length <= 0) { |
Al Viro | 09aab88 | 2015-11-13 03:00:17 -0500 | [diff] [blame] | 574 | cifs_dbg(FYI, "Received no data or error: %d\n", length); |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 575 | cifs_reconnect(server); |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 576 | return -ECONNABORTED; |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 577 | } |
| 578 | } |
Jeff Layton | a52c1eb | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 579 | return total_read; |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 580 | } |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 581 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 582 | int |
| 583 | cifs_read_from_socket(struct TCP_Server_Info *server, char *buf, |
| 584 | unsigned int to_read) |
Jeff Layton | 42c4dfc | 2011-10-19 15:28:17 -0400 | [diff] [blame] | 585 | { |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 586 | struct msghdr smb_msg; |
| 587 | struct kvec iov = {.iov_base = buf, .iov_len = to_read}; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 588 | iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read); |
Jeff Layton | 42c4dfc | 2011-10-19 15:28:17 -0400 | [diff] [blame] | 589 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 590 | return cifs_readv_from_socket(server, &smb_msg); |
| 591 | } |
Jeff Layton | 42c4dfc | 2011-10-19 15:28:17 -0400 | [diff] [blame] | 592 | |
David Howells | cf0604a | 2021-02-04 00:15:21 -0600 | [diff] [blame] | 593 | ssize_t |
| 594 | cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read) |
| 595 | { |
| 596 | struct msghdr smb_msg; |
| 597 | |
| 598 | /* |
| 599 | * iov_iter_discard already sets smb_msg.type and count and iov_offset |
| 600 | * and cifs_readv_from_socket sets msg_control and msg_controllen |
| 601 | * so little to initialize in struct msghdr |
| 602 | */ |
| 603 | smb_msg.msg_name = NULL; |
| 604 | smb_msg.msg_namelen = 0; |
| 605 | iov_iter_discard(&smb_msg.msg_iter, READ, to_read); |
| 606 | |
| 607 | return cifs_readv_from_socket(server, &smb_msg); |
| 608 | } |
| 609 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 610 | int |
| 611 | cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page, |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 612 | unsigned int page_offset, unsigned int to_read) |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 613 | { |
| 614 | struct msghdr smb_msg; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 615 | struct bio_vec bv = { |
| 616 | .bv_page = page, .bv_len = to_read, .bv_offset = page_offset}; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 617 | iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read); |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 618 | return cifs_readv_from_socket(server, &smb_msg); |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 619 | } |
| 620 | |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 621 | static bool |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 622 | is_smb_response(struct TCP_Server_Info *server, unsigned char type) |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 623 | { |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 624 | /* |
| 625 | * The first byte big endian of the length field, |
| 626 | * is actually not part of the length but the type |
| 627 | * with the most common, zero, as regular data. |
| 628 | */ |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 629 | switch (type) { |
| 630 | case RFC1002_SESSION_MESSAGE: |
| 631 | /* Regular SMB response */ |
| 632 | return true; |
| 633 | case RFC1002_SESSION_KEEP_ALIVE: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 634 | cifs_dbg(FYI, "RFC 1002 session keep alive\n"); |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 635 | break; |
| 636 | case RFC1002_POSITIVE_SESSION_RESPONSE: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 637 | cifs_dbg(FYI, "RFC 1002 positive session response\n"); |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 638 | break; |
| 639 | case RFC1002_NEGATIVE_SESSION_RESPONSE: |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 640 | /* |
| 641 | * We get this from Windows 98 instead of an error on |
| 642 | * SMB negprot response. |
| 643 | */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 644 | cifs_dbg(FYI, "RFC 1002 negative session response\n"); |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 645 | /* give server a second to clean up */ |
| 646 | msleep(1000); |
| 647 | /* |
| 648 | * Always try 445 first on reconnect since we get NACK |
| 649 | * on some if we ever connected to port 139 (the NACK |
| 650 | * is since we do not begin with RFC1001 session |
| 651 | * initialize frame). |
| 652 | */ |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 653 | cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT); |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 654 | cifs_reconnect(server); |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 655 | break; |
| 656 | default: |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 657 | cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type); |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 658 | cifs_reconnect(server); |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 659 | } |
| 660 | |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 661 | return false; |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 662 | } |
| 663 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 664 | void |
| 665 | dequeue_mid(struct mid_q_entry *mid, bool malformed) |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 666 | { |
| 667 | #ifdef CONFIG_CIFS_STATS2 |
| 668 | mid->when_received = jiffies; |
| 669 | #endif |
| 670 | spin_lock(&GlobalMid_Lock); |
| 671 | if (!malformed) |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 672 | mid->mid_state = MID_RESPONSE_RECEIVED; |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 673 | else |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 674 | mid->mid_state = MID_RESPONSE_MALFORMED; |
Ronnie Sahlberg | ddf83af | 2018-08-30 10:12:59 +1000 | [diff] [blame] | 675 | /* |
| 676 | * Trying to handle/dequeue a mid after the send_recv() |
| 677 | * function has finished processing it is a bug. |
| 678 | */ |
| 679 | if (mid->mid_flags & MID_DELETED) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 680 | pr_warn_once("trying to dequeue a deleted mid\n"); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 681 | else { |
Ronnie Sahlberg | ddf83af | 2018-08-30 10:12:59 +1000 | [diff] [blame] | 682 | list_del_init(&mid->qhead); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 683 | mid->mid_flags |= MID_DELETED; |
| 684 | } |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 685 | spin_unlock(&GlobalMid_Lock); |
| 686 | } |
| 687 | |
Pavel Shilovsky | 86a7964 | 2019-11-21 11:35:13 -0800 | [diff] [blame] | 688 | static unsigned int |
| 689 | smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) |
| 690 | { |
Ronnie Sahlberg | 0d35e38 | 2021-11-05 08:39:01 +0900 | [diff] [blame] | 691 | struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; |
Pavel Shilovsky | 86a7964 | 2019-11-21 11:35:13 -0800 | [diff] [blame] | 692 | |
| 693 | /* |
| 694 | * SMB1 does not use credits. |
| 695 | */ |
| 696 | if (server->vals->header_preamble_size) |
| 697 | return 0; |
| 698 | |
| 699 | return le16_to_cpu(shdr->CreditRequest); |
| 700 | } |
| 701 | |
Jeff Layton | c8054eb | 2011-10-19 15:29:31 -0400 | [diff] [blame] | 702 | static void |
| 703 | handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server, |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 704 | char *buf, int malformed) |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 705 | { |
Pavel Shilovsky | 316cf94 | 2012-05-23 14:31:03 +0400 | [diff] [blame] | 706 | if (server->ops->check_trans2 && |
| 707 | server->ops->check_trans2(mid, server, buf, malformed)) |
Jeff Layton | c8054eb | 2011-10-19 15:29:31 -0400 | [diff] [blame] | 708 | return; |
Pavel Shilovsky | 86a7964 | 2019-11-21 11:35:13 -0800 | [diff] [blame] | 709 | mid->credits_received = smb2_get_credits_from_hdr(buf, server); |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 710 | mid->resp_buf = buf; |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 711 | mid->large_buf = server->large_buf; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 712 | /* Was previous buf put in mpx struct for multi-rsp? */ |
| 713 | if (!mid->multiRsp) { |
| 714 | /* smb buffer will be freed by user thread */ |
| 715 | if (server->large_buf) |
| 716 | server->bigbuf = NULL; |
| 717 | else |
| 718 | server->smallbuf = NULL; |
| 719 | } |
Jeff Layton | ffc00e2 | 2011-10-19 15:29:13 -0400 | [diff] [blame] | 720 | dequeue_mid(mid, malformed); |
Pavel Shilovsky | ad69bae | 2011-08-01 13:19:43 +0400 | [diff] [blame] | 721 | } |
| 722 | |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 723 | static void clean_demultiplex_info(struct TCP_Server_Info *server) |
| 724 | { |
| 725 | int length; |
| 726 | |
| 727 | /* take it off the list, if it's not already */ |
| 728 | spin_lock(&cifs_tcp_ses_lock); |
| 729 | list_del_init(&server->tcp_ses_list); |
| 730 | spin_unlock(&cifs_tcp_ses_lock); |
| 731 | |
Paulo Alcantara | 2122533 | 2020-11-28 16:54:02 -0300 | [diff] [blame] | 732 | cancel_delayed_work_sync(&server->echo); |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 733 | cancel_delayed_work_sync(&server->resolve); |
Paulo Alcantara | 2122533 | 2020-11-28 16:54:02 -0300 | [diff] [blame] | 734 | |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 735 | spin_lock(&GlobalMid_Lock); |
| 736 | server->tcpStatus = CifsExiting; |
| 737 | spin_unlock(&GlobalMid_Lock); |
| 738 | wake_up_all(&server->response_q); |
| 739 | |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 740 | /* check if we have blocked requests that need to free */ |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 741 | spin_lock(&server->req_lock); |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 742 | if (server->credits <= 0) |
| 743 | server->credits = 1; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 744 | spin_unlock(&server->req_lock); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 745 | /* |
| 746 | * Although there should not be any requests blocked on this queue it |
| 747 | * can not hurt to be paranoid and try to wake up requests that may |
| 748 | * haven been blocked when more than 50 at time were on the wire to the |
| 749 | * same server - they now will see the session is in exit state and get |
| 750 | * out of SendReceive. |
| 751 | */ |
| 752 | wake_up_all(&server->request_q); |
| 753 | /* give those requests time to exit */ |
| 754 | msleep(125); |
Long Li | 050b8c3 | 2019-04-04 11:35:42 -0500 | [diff] [blame] | 755 | if (cifs_rdma_enabled(server)) |
| 756 | smbd_destroy(server); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 757 | if (server->ssocket) { |
| 758 | sock_release(server->ssocket); |
| 759 | server->ssocket = NULL; |
| 760 | } |
| 761 | |
| 762 | if (!list_empty(&server->pending_mid_q)) { |
| 763 | struct list_head dispose_list; |
| 764 | struct mid_q_entry *mid_entry; |
| 765 | struct list_head *tmp, *tmp2; |
| 766 | |
| 767 | INIT_LIST_HEAD(&dispose_list); |
| 768 | spin_lock(&GlobalMid_Lock); |
| 769 | list_for_each_safe(tmp, tmp2, &server->pending_mid_q) { |
| 770 | mid_entry = list_entry(tmp, struct mid_q_entry, qhead); |
Paulo Alcantara | bf1bc694 | 2021-03-08 12:00:47 -0300 | [diff] [blame] | 771 | cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 772 | kref_get(&mid_entry->refcount); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 773 | mid_entry->mid_state = MID_SHUTDOWN; |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 774 | list_move(&mid_entry->qhead, &dispose_list); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 775 | mid_entry->mid_flags |= MID_DELETED; |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 776 | } |
| 777 | spin_unlock(&GlobalMid_Lock); |
| 778 | |
| 779 | /* now walk dispose list and issue callbacks */ |
| 780 | list_for_each_safe(tmp, tmp2, &dispose_list) { |
| 781 | mid_entry = list_entry(tmp, struct mid_q_entry, qhead); |
Paulo Alcantara | bf1bc694 | 2021-03-08 12:00:47 -0300 | [diff] [blame] | 782 | cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 783 | list_del_init(&mid_entry->qhead); |
| 784 | mid_entry->callback(mid_entry); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 785 | cifs_mid_q_entry_release(mid_entry); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 786 | } |
| 787 | /* 1/8th of sec is more than enough time for them to exit */ |
| 788 | msleep(125); |
| 789 | } |
| 790 | |
| 791 | if (!list_empty(&server->pending_mid_q)) { |
| 792 | /* |
| 793 | * mpx threads have not exited yet give them at least the smb |
| 794 | * send timeout time for long ops. |
| 795 | * |
| 796 | * Due to delays on oplock break requests, we need to wait at |
| 797 | * least 45 seconds before giving up on a request getting a |
| 798 | * response and going ahead and killing cifsd. |
| 799 | */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 800 | cifs_dbg(FYI, "Wait for exit from demultiplex thread\n"); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 801 | msleep(46000); |
| 802 | /* |
| 803 | * If threads still have not exited they are probably never |
| 804 | * coming home not much else we can do but free the memory. |
| 805 | */ |
| 806 | } |
| 807 | |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 808 | kfree(server); |
| 809 | |
| 810 | length = atomic_dec_return(&tcpSesAllocCount); |
| 811 | if (length > 0) |
David Rientjes | 11d8336 | 2015-04-14 15:48:21 -0700 | [diff] [blame] | 812 | mempool_resize(cifs_req_poolp, length + cifs_min_rcv); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 813 | } |
| 814 | |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 815 | static int |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 816 | standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 817 | { |
| 818 | int length; |
| 819 | char *buf = server->smallbuf; |
Ronnie Sahlberg | 2e96467 | 2018-04-09 18:06:26 +1000 | [diff] [blame] | 820 | unsigned int pdu_length = server->pdu_size; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 821 | |
| 822 | /* make sure this will fit in a large buffer */ |
Ronnie Sahlberg | 93012bf | 2018-03-31 11:45:31 +1100 | [diff] [blame] | 823 | if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - |
| 824 | server->vals->header_preamble_size) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 825 | cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length); |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 826 | cifs_reconnect(server); |
Pavel Shilovsky | 3fabaa2 | 2014-07-10 09:55:52 +0400 | [diff] [blame] | 827 | return -ECONNABORTED; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* switch to large buffer if too big for a small one */ |
| 831 | if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) { |
| 832 | server->large_buf = true; |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 833 | memcpy(server->bigbuf, buf, server->total_read); |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 834 | buf = server->bigbuf; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* now read the rest */ |
Pavel Shilovsky | 1887f60 | 2012-05-17 12:45:31 +0400 | [diff] [blame] | 838 | length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, |
Ronnie Sahlberg | 93012bf | 2018-03-31 11:45:31 +1100 | [diff] [blame] | 839 | pdu_length - HEADER_SIZE(server) + 1 |
| 840 | + server->vals->header_preamble_size); |
| 841 | |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 842 | if (length < 0) |
| 843 | return length; |
| 844 | server->total_read += length; |
| 845 | |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 846 | dump_smb(buf, server->total_read); |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 847 | |
Pavel Shilovsky | 4326ed2 | 2016-11-17 15:24:46 -0800 | [diff] [blame] | 848 | return cifs_handle_standard(server, mid); |
| 849 | } |
| 850 | |
| 851 | int |
| 852 | cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 853 | { |
| 854 | char *buf = server->large_buf ? server->bigbuf : server->smallbuf; |
| 855 | int length; |
| 856 | |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 857 | /* |
| 858 | * We know that we received enough to get to the MID as we |
| 859 | * checked the pdu_length earlier. Now check to see |
| 860 | * if the rest of the header is OK. We borrow the length |
| 861 | * var for the rest of the loop to avoid a new stack var. |
| 862 | * |
| 863 | * 48 bytes is enough to display the header and a little bit |
| 864 | * into the payload for debugging purposes. |
| 865 | */ |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 866 | length = server->ops->check_message(buf, server->total_read, server); |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 867 | if (length != 0) |
| 868 | cifs_dump_mem("Bad SMB: ", buf, |
| 869 | min_t(unsigned int, server->total_read, 48)); |
| 870 | |
Pavel Shilovsky | 511c54a | 2017-07-08 14:32:00 -0700 | [diff] [blame] | 871 | if (server->ops->is_session_expired && |
| 872 | server->ops->is_session_expired(buf)) { |
| 873 | cifs_reconnect(server); |
Pavel Shilovsky | 511c54a | 2017-07-08 14:32:00 -0700 | [diff] [blame] | 874 | return -1; |
| 875 | } |
| 876 | |
Pavel Shilovsky | 2e44b28 | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 877 | if (server->ops->is_status_pending && |
Pavel Shilovsky | 66265f1 | 2019-01-23 17:11:16 -0800 | [diff] [blame] | 878 | server->ops->is_status_pending(buf, server)) |
Pavel Shilovsky | 2e44b28 | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 879 | return -1; |
| 880 | |
Jeff Layton | ff4fa4a | 2012-02-07 06:31:05 -0500 | [diff] [blame] | 881 | if (!mid) |
| 882 | return length; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 883 | |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 884 | handle_mid(mid, server, buf, length); |
Jeff Layton | ff4fa4a | 2012-02-07 06:31:05 -0500 | [diff] [blame] | 885 | return 0; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 886 | } |
| 887 | |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 888 | static void |
| 889 | smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) |
| 890 | { |
Ronnie Sahlberg | 0d35e38 | 2021-11-05 08:39:01 +0900 | [diff] [blame] | 891 | struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; |
Shyam Prasad N | 6d82c27 | 2021-02-03 23:20:46 -0800 | [diff] [blame] | 892 | int scredits, in_flight; |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 893 | |
| 894 | /* |
| 895 | * SMB1 does not use credits. |
| 896 | */ |
| 897 | if (server->vals->header_preamble_size) |
| 898 | return; |
| 899 | |
| 900 | if (shdr->CreditRequest) { |
| 901 | spin_lock(&server->req_lock); |
| 902 | server->credits += le16_to_cpu(shdr->CreditRequest); |
Shyam Prasad N | 6d82c27 | 2021-02-03 23:20:46 -0800 | [diff] [blame] | 903 | scredits = server->credits; |
| 904 | in_flight = server->in_flight; |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 905 | spin_unlock(&server->req_lock); |
| 906 | wake_up(&server->request_q); |
Shyam Prasad N | cd7b699 | 2020-11-12 08:56:49 -0800 | [diff] [blame] | 907 | |
| 908 | trace_smb3_add_credits(server->CurrentMid, |
Shyam Prasad N | 6d82c27 | 2021-02-03 23:20:46 -0800 | [diff] [blame] | 909 | server->conn_id, server->hostname, scredits, |
| 910 | le16_to_cpu(shdr->CreditRequest), in_flight); |
Shyam Prasad N | cd7b699 | 2020-11-12 08:56:49 -0800 | [diff] [blame] | 911 | cifs_server_dbg(FYI, "%s: added %u credits total=%d\n", |
| 912 | __func__, le16_to_cpu(shdr->CreditRequest), |
| 913 | scredits); |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | |
| 917 | |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 918 | static int |
Al Viro | 7c97c20 | 2011-06-21 08:51:28 -0400 | [diff] [blame] | 919 | cifs_demultiplex_thread(void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 921 | int i, num_mids, length; |
Al Viro | 7c97c20 | 2011-06-21 08:51:28 -0400 | [diff] [blame] | 922 | struct TCP_Server_Info *server = p; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 923 | unsigned int pdu_length; |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 924 | unsigned int next_offset; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 925 | char *buf = NULL; |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 926 | struct task_struct *task_to_wake = NULL; |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 927 | struct mid_q_entry *mids[MAX_COMPOUND]; |
| 928 | char *bufs[MAX_COMPOUND]; |
Rohith Surabattula | 8e670f7 | 2020-09-18 05:37:28 +0000 | [diff] [blame] | 929 | unsigned int noreclaim_flag, num_io_timeout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
Eric Biggers | dc92027 | 2020-03-08 22:58:20 -0700 | [diff] [blame] | 931 | noreclaim_flag = memalloc_noreclaim_save(); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 932 | cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current)); |
Jeff Layton | 93d0ec8 | 2008-08-02 08:00:48 -0400 | [diff] [blame] | 933 | |
| 934 | length = atomic_inc_return(&tcpSesAllocCount); |
| 935 | if (length > 1) |
David Rientjes | 11d8336 | 2015-04-14 15:48:21 -0700 | [diff] [blame] | 936 | mempool_resize(cifs_req_poolp, length + cifs_min_rcv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 938 | set_freezable(); |
Eric W. Biederman | 33da8e7 | 2019-08-16 12:33:54 -0500 | [diff] [blame] | 939 | allow_kernel_signal(SIGKILL); |
Jeff Layton | 469ee61 | 2008-10-16 18:46:39 +0000 | [diff] [blame] | 940 | while (server->tcpStatus != CifsExiting) { |
Steve French | ede1327 | 2005-08-30 20:10:14 -0700 | [diff] [blame] | 941 | if (try_to_freeze()) |
| 942 | continue; |
Steve French | b8643e1 | 2005-04-28 22:41:07 -0700 | [diff] [blame] | 943 | |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 944 | if (!allocate_buffers(server)) |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 945 | continue; |
Steve French | b8643e1 | 2005-04-28 22:41:07 -0700 | [diff] [blame] | 946 | |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 947 | server->large_buf = false; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 948 | buf = server->smallbuf; |
Steve French | f01d5e1 | 2007-08-30 21:13:31 +0000 | [diff] [blame] | 949 | pdu_length = 4; /* enough to get RFC1001 header */ |
Steve French | fda3594 | 2011-01-20 18:06:34 +0000 | [diff] [blame] | 950 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 951 | length = cifs_read_from_socket(server, buf, pdu_length); |
Jeff Layton | a52c1eb | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 952 | if (length < 0) |
Steve French | fda3594 | 2011-01-20 18:06:34 +0000 | [diff] [blame] | 953 | continue; |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 954 | |
| 955 | if (server->vals->header_preamble_size == 0) |
| 956 | server->total_read = 0; |
| 957 | else |
| 958 | server->total_read = length; |
Steve French | 67010fb | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 959 | |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 960 | /* |
| 961 | * The right amount was read from socket - 4 bytes, |
| 962 | * so we can now interpret the length field. |
| 963 | */ |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 964 | pdu_length = get_rfc1002_length(buf); |
Steve French | 46810cb | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 965 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 966 | cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length); |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 967 | if (!is_smb_response(server, buf[0])) |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 968 | continue; |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 969 | next_pdu: |
| 970 | server->pdu_size = pdu_length; |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 971 | |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 972 | /* make sure we have enough to get to the MID */ |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 973 | if (server->pdu_size < HEADER_SIZE(server) - 1 - |
Ronnie Sahlberg | 93012bf | 2018-03-31 11:45:31 +1100 | [diff] [blame] | 974 | server->vals->header_preamble_size) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 975 | cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n", |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 976 | server->pdu_size); |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 977 | cifs_reconnect(server); |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 978 | continue; |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 979 | } |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 980 | |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 981 | /* read down to the MID */ |
Ronnie Sahlberg | 93012bf | 2018-03-31 11:45:31 +1100 | [diff] [blame] | 982 | length = cifs_read_from_socket(server, |
| 983 | buf + server->vals->header_preamble_size, |
| 984 | HEADER_SIZE(server) - 1 |
| 985 | - server->vals->header_preamble_size); |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 986 | if (length < 0) |
| 987 | continue; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 988 | server->total_read += length; |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 989 | |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 990 | if (server->ops->next_header) { |
| 991 | next_offset = server->ops->next_header(buf); |
| 992 | if (next_offset) |
| 993 | server->pdu_size = next_offset; |
| 994 | } |
| 995 | |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 996 | memset(mids, 0, sizeof(mids)); |
| 997 | memset(bufs, 0, sizeof(bufs)); |
| 998 | num_mids = 0; |
| 999 | |
Pavel Shilovsky | 9bb17e0 | 2016-11-17 15:24:34 -0800 | [diff] [blame] | 1000 | if (server->ops->is_transform_hdr && |
| 1001 | server->ops->receive_transform && |
| 1002 | server->ops->is_transform_hdr(buf)) { |
| 1003 | length = server->ops->receive_transform(server, |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1004 | mids, |
| 1005 | bufs, |
| 1006 | &num_mids); |
Pavel Shilovsky | 9bb17e0 | 2016-11-17 15:24:34 -0800 | [diff] [blame] | 1007 | } else { |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1008 | mids[0] = server->ops->find_mid(server, buf); |
| 1009 | bufs[0] = buf; |
Steve French | 7af929d | 2018-10-02 18:54:09 -0500 | [diff] [blame] | 1010 | num_mids = 1; |
Jeff Layton | c8054eb | 2011-10-19 15:29:31 -0400 | [diff] [blame] | 1011 | |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1012 | if (!mids[0] || !mids[0]->receive) |
| 1013 | length = standard_receive3(server, mids[0]); |
Pavel Shilovsky | 9bb17e0 | 2016-11-17 15:24:34 -0800 | [diff] [blame] | 1014 | else |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1015 | length = mids[0]->receive(server, mids[0]); |
Pavel Shilovsky | 9bb17e0 | 2016-11-17 15:24:34 -0800 | [diff] [blame] | 1016 | } |
Jeff Layton | 44d22d8 | 2011-10-19 15:29:49 -0400 | [diff] [blame] | 1017 | |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 1018 | if (length < 0) { |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1019 | for (i = 0; i < num_mids; i++) |
| 1020 | if (mids[i]) |
| 1021 | cifs_mid_q_entry_release(mids[i]); |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1022 | continue; |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 1023 | } |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1024 | |
Rohith Surabattula | 8e670f7 | 2020-09-18 05:37:28 +0000 | [diff] [blame] | 1025 | if (server->ops->is_status_io_timeout && |
| 1026 | server->ops->is_status_io_timeout(buf)) { |
| 1027 | num_io_timeout++; |
| 1028 | if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) { |
| 1029 | cifs_reconnect(server); |
| 1030 | num_io_timeout = 0; |
| 1031 | continue; |
| 1032 | } |
| 1033 | } |
| 1034 | |
Steve French | fda3594 | 2011-01-20 18:06:34 +0000 | [diff] [blame] | 1035 | server->lstrp = jiffies; |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1036 | |
| 1037 | for (i = 0; i < num_mids; i++) { |
| 1038 | if (mids[i] != NULL) { |
| 1039 | mids[i]->resp_buf_size = server->pdu_size; |
Sachin Prabhu | 38bd490 | 2017-03-03 15:41:38 -0800 | [diff] [blame] | 1040 | |
Rohith Surabattula | 9e550b0 | 2021-02-16 10:40:45 +0000 | [diff] [blame] | 1041 | if (bufs[i] && server->ops->is_network_name_deleted) |
| 1042 | server->ops->is_network_name_deleted(bufs[i], |
| 1043 | server); |
| 1044 | |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1045 | if (!mids[i]->multiRsp || mids[i]->multiEnd) |
| 1046 | mids[i]->callback(mids[i]); |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 1047 | |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1048 | cifs_mid_q_entry_release(mids[i]); |
| 1049 | } else if (server->ops->is_oplock_break && |
| 1050 | server->ops->is_oplock_break(bufs[i], |
| 1051 | server)) { |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 1052 | smb2_add_credits_from_hdr(bufs[i], server); |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1053 | cifs_dbg(FYI, "Received oplock break\n"); |
| 1054 | } else { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1055 | cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n", |
| 1056 | atomic_read(&midCount)); |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1057 | cifs_dump_mem("Received Data is: ", bufs[i], |
| 1058 | HEADER_SIZE(server)); |
Ronnie Sahlberg | 3e27257 | 2019-07-06 06:43:08 +1000 | [diff] [blame] | 1059 | smb2_add_credits_from_hdr(bufs[i], server); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1060 | #ifdef CONFIG_CIFS_DEBUG2 |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1061 | if (server->ops->dump_detail) |
| 1062 | server->ops->dump_detail(bufs[i], |
| 1063 | server); |
| 1064 | cifs_dump_mids(server); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1065 | #endif /* CIFS_DEBUG2 */ |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1066 | } |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 1067 | } |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1068 | |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 1069 | if (pdu_length > server->pdu_size) { |
| 1070 | if (!allocate_buffers(server)) |
| 1071 | continue; |
| 1072 | pdu_length -= server->pdu_size; |
| 1073 | server->total_read = 0; |
| 1074 | server->large_buf = false; |
| 1075 | buf = server->smallbuf; |
| 1076 | goto next_pdu; |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1077 | } |
| 1078 | } /* end while !EXITING */ |
| 1079 | |
Justin P. Mattock | fd62cb7 | 2011-02-24 22:15:02 -0800 | [diff] [blame] | 1080 | /* buffer usually freed in free_mid - need to free it here on exit */ |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 1081 | cifs_buf_release(server->bigbuf); |
| 1082 | if (server->smallbuf) /* no sense logging a debug message if NULL */ |
| 1083 | cifs_small_buf_release(server->smallbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 1085 | task_to_wake = xchg(&server->tsk, NULL); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 1086 | clean_demultiplex_info(server); |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 1087 | |
| 1088 | /* if server->tsk was NULL then wait for a signal before exiting */ |
| 1089 | if (!task_to_wake) { |
| 1090 | set_current_state(TASK_INTERRUPTIBLE); |
| 1091 | while (!signal_pending(current)) { |
| 1092 | schedule(); |
| 1093 | set_current_state(TASK_INTERRUPTIBLE); |
| 1094 | } |
| 1095 | set_current_state(TASK_RUNNING); |
| 1096 | } |
| 1097 | |
Eric Biggers | dc92027 | 2020-03-08 22:58:20 -0700 | [diff] [blame] | 1098 | memalloc_noreclaim_restore(noreclaim_flag); |
Jeff Layton | 0468a2c | 2008-12-01 07:09:35 -0500 | [diff] [blame] | 1099 | module_put_and_exit(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 1102 | /* |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 1103 | * Returns true if srcaddr isn't specified and rhs isn't specified, or |
| 1104 | * if srcaddr is specified and matches the IP address of the rhs argument |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1105 | */ |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 1106 | bool |
| 1107 | cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs) |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1108 | { |
| 1109 | switch (srcaddr->sa_family) { |
| 1110 | case AF_UNSPEC: |
| 1111 | return (rhs->sa_family == AF_UNSPEC); |
| 1112 | case AF_INET: { |
| 1113 | struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr; |
| 1114 | struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs; |
| 1115 | return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr); |
| 1116 | } |
| 1117 | case AF_INET6: { |
| 1118 | struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr; |
Nickolai Zeldovich | e3e2775 | 2013-01-16 21:36:17 -0500 | [diff] [blame] | 1119 | struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs; |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1120 | return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr); |
| 1121 | } |
| 1122 | default: |
| 1123 | WARN_ON(1); |
| 1124 | return false; /* don't expect to be here */ |
| 1125 | } |
| 1126 | } |
| 1127 | |
Pavel Shilovsky | 4b88613 | 2010-12-13 22:18:07 +0300 | [diff] [blame] | 1128 | /* |
| 1129 | * If no port is specified in addr structure, we try to match with 445 port |
| 1130 | * and if it fails - with 139 ports. It should be called only if address |
| 1131 | * families of server and addr are equal. |
| 1132 | */ |
| 1133 | static bool |
| 1134 | match_port(struct TCP_Server_Info *server, struct sockaddr *addr) |
| 1135 | { |
Steve French | 6da9791 | 2011-03-13 18:55:55 +0000 | [diff] [blame] | 1136 | __be16 port, *sport; |
Pavel Shilovsky | 4b88613 | 2010-12-13 22:18:07 +0300 | [diff] [blame] | 1137 | |
Long Li | 3b24911 | 2019-05-15 14:09:04 -0700 | [diff] [blame] | 1138 | /* SMBDirect manages its own ports, don't match it here */ |
| 1139 | if (server->rdma) |
| 1140 | return true; |
| 1141 | |
Pavel Shilovsky | 4b88613 | 2010-12-13 22:18:07 +0300 | [diff] [blame] | 1142 | switch (addr->sa_family) { |
| 1143 | case AF_INET: |
| 1144 | sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port; |
| 1145 | port = ((struct sockaddr_in *) addr)->sin_port; |
| 1146 | break; |
| 1147 | case AF_INET6: |
| 1148 | sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port; |
| 1149 | port = ((struct sockaddr_in6 *) addr)->sin6_port; |
| 1150 | break; |
| 1151 | default: |
| 1152 | WARN_ON(1); |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
| 1156 | if (!port) { |
| 1157 | port = htons(CIFS_PORT); |
| 1158 | if (port == *sport) |
| 1159 | return true; |
| 1160 | |
| 1161 | port = htons(RFC1001_PORT); |
| 1162 | } |
| 1163 | |
| 1164 | return port == *sport; |
| 1165 | } |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1166 | |
| 1167 | static bool |
| 1168 | match_address(struct TCP_Server_Info *server, struct sockaddr *addr, |
| 1169 | struct sockaddr *srcaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | { |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1171 | switch (addr->sa_family) { |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1172 | case AF_INET: { |
| 1173 | struct sockaddr_in *addr4 = (struct sockaddr_in *)addr; |
| 1174 | struct sockaddr_in *srv_addr4 = |
| 1175 | (struct sockaddr_in *)&server->dstaddr; |
| 1176 | |
| 1177 | if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr) |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1178 | return false; |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1179 | break; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1180 | } |
| 1181 | case AF_INET6: { |
| 1182 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr; |
| 1183 | struct sockaddr_in6 *srv_addr6 = |
| 1184 | (struct sockaddr_in6 *)&server->dstaddr; |
| 1185 | |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1186 | if (!ipv6_addr_equal(&addr6->sin6_addr, |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1187 | &srv_addr6->sin6_addr)) |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1188 | return false; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1189 | if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id) |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1190 | return false; |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1191 | break; |
| 1192 | } |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1193 | default: |
| 1194 | WARN_ON(1); |
| 1195 | return false; /* don't expect to be here */ |
| 1196 | } |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1197 | |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 1198 | if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr)) |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1199 | return false; |
| 1200 | |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1201 | return true; |
| 1202 | } |
| 1203 | |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1204 | static bool |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1205 | match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1206 | { |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1207 | /* |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1208 | * The select_sectype function should either return the ctx->sectype |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1209 | * that was specified, or "Unspecified" if that sectype was not |
| 1210 | * compatible with the given NEGOTIATE request. |
| 1211 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1212 | if (server->ops->select_sectype(server, ctx->sectype) |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1213 | == Unspecified) |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1214 | return false; |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1215 | |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1216 | /* |
| 1217 | * Now check if signing mode is acceptable. No need to check |
| 1218 | * global_secflags at this point since if MUST_SIGN is set then |
| 1219 | * the server->sign had better be too. |
| 1220 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1221 | if (ctx->sign && !server->sign) |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 1222 | return false; |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1223 | |
| 1224 | return true; |
| 1225 | } |
| 1226 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1227 | static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1228 | { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1229 | struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr; |
Jeff Layton | 9fa114f | 2012-11-26 11:09:57 -0500 | [diff] [blame] | 1230 | |
Shyam Prasad N | c9f1c19 | 2021-11-06 11:31:53 +0000 | [diff] [blame] | 1231 | if (ctx->nosharesock) { |
| 1232 | server->nosharesock = true; |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | /* this server does not share socket */ |
| 1237 | if (server->nosharesock) |
Jeff Layton | a0b3df5 | 2013-05-24 07:40:59 -0400 | [diff] [blame] | 1238 | return 0; |
| 1239 | |
Steve French | 43cdae8 | 2019-06-13 14:26:49 -0500 | [diff] [blame] | 1240 | /* If multidialect negotiation see if existing sessions match one */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1241 | if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) { |
Steve French | 43cdae8 | 2019-06-13 14:26:49 -0500 | [diff] [blame] | 1242 | if (server->vals->protocol_id < SMB30_PROT_ID) |
| 1243 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1244 | } else if (strcmp(ctx->vals->version_string, |
Steve French | 43cdae8 | 2019-06-13 14:26:49 -0500 | [diff] [blame] | 1245 | SMBDEFAULT_VERSION_STRING) == 0) { |
| 1246 | if (server->vals->protocol_id < SMB21_PROT_ID) |
| 1247 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1248 | } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops)) |
Jeff Layton | 23db65f | 2012-05-15 12:20:51 -0400 | [diff] [blame] | 1249 | return 0; |
| 1250 | |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1251 | if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns)) |
| 1252 | return 0; |
| 1253 | |
Shyam Prasad N | 7be3248 | 2021-10-14 11:52:39 +0000 | [diff] [blame] | 1254 | if (strcasecmp(server->hostname, ctx->server_hostname)) |
| 1255 | return 0; |
| 1256 | |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1257 | if (!match_address(server, addr, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1258 | (struct sockaddr *)&ctx->srcaddr)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1259 | return 0; |
| 1260 | |
| 1261 | if (!match_port(server, addr)) |
| 1262 | return 0; |
| 1263 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1264 | if (!match_security(server, ctx)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1265 | return 0; |
| 1266 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1267 | if (server->echo_interval != ctx->echo_interval * HZ) |
Steve French | adfeb3e | 2015-12-18 12:31:36 -0600 | [diff] [blame] | 1268 | return 0; |
| 1269 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1270 | if (server->rdma != ctx->rdma) |
Long Li | 8339dd3 | 2017-11-07 01:54:55 -0700 | [diff] [blame] | 1271 | return 0; |
| 1272 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1273 | if (server->ignore_signature != ctx->ignore_signature) |
Steve French | 4f5c10f | 2019-09-03 21:18:49 -0500 | [diff] [blame] | 1274 | return 0; |
| 1275 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1276 | if (server->min_offload != ctx->min_offload) |
Steve French | 563317e | 2019-09-08 23:22:02 -0500 | [diff] [blame] | 1277 | return 0; |
| 1278 | |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1279 | return 1; |
| 1280 | } |
| 1281 | |
Paulo Alcantara | 54be1f6 | 2018-11-14 16:01:21 -0200 | [diff] [blame] | 1282 | struct TCP_Server_Info * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1283 | cifs_find_tcp_session(struct smb3_fs_context *ctx) |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1284 | { |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1285 | struct TCP_Server_Info *server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1287 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1288 | list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) { |
Paulo Alcantara | cdc3363 | 2021-07-15 21:53:53 -0300 | [diff] [blame] | 1289 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 1290 | /* |
| 1291 | * DFS failover implementation in cifs_reconnect() requires unique tcp sessions for |
| 1292 | * DFS connections to do failover properly, so avoid sharing them with regular |
| 1293 | * shares or even links that may connect to same server but having completely |
| 1294 | * different failover targets. |
| 1295 | */ |
| 1296 | if (server->is_dfs_conn) |
| 1297 | continue; |
| 1298 | #endif |
Paulo Alcantara (SUSE) | 3345bb4 | 2019-12-04 11:25:06 -0300 | [diff] [blame] | 1299 | /* |
| 1300 | * Skip ses channels since they're only handled in lower layers |
| 1301 | * (e.g. cifs_send_recv). |
| 1302 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1303 | if (server->is_channel || !match_server(server, ctx)) |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1304 | continue; |
| 1305 | |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1306 | ++server->srv_count; |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1307 | spin_unlock(&cifs_tcp_ses_lock); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1308 | cifs_dbg(FYI, "Existing tcp session with server found\n"); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1309 | return server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | } |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1311 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | return NULL; |
| 1313 | } |
| 1314 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1315 | void |
| 1316 | cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | { |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 1318 | struct task_struct *task; |
| 1319 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1320 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1321 | if (--server->srv_count > 0) { |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1322 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1323 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | } |
Steve French | dea570e0 | 2008-05-06 22:05:51 +0000 | [diff] [blame] | 1325 | |
Shyam Prasad N | 16dd9b8 | 2021-07-14 23:00:00 -0500 | [diff] [blame] | 1326 | /* srv_count can never go negative */ |
| 1327 | WARN_ON(server->srv_count < 0); |
| 1328 | |
Rob Landley | f1d0c99 | 2011-01-22 15:44:05 -0600 | [diff] [blame] | 1329 | put_net(cifs_net_ns(server)); |
| 1330 | |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1331 | list_del_init(&server->tcp_ses_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1332 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1333 | |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1334 | cancel_delayed_work_sync(&server->echo); |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 1335 | cancel_delayed_work_sync(&server->resolve); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1336 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1337 | if (from_reconnect) |
| 1338 | /* |
| 1339 | * Avoid deadlock here: reconnect work calls |
| 1340 | * cifs_put_tcp_session() at its end. Need to be sure |
| 1341 | * that reconnect work does nothing with server pointer after |
| 1342 | * that step. |
| 1343 | */ |
| 1344 | cancel_delayed_work(&server->reconnect); |
| 1345 | else |
| 1346 | cancel_delayed_work_sync(&server->reconnect); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1347 | |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1348 | spin_lock(&GlobalMid_Lock); |
| 1349 | server->tcpStatus = CifsExiting; |
| 1350 | spin_unlock(&GlobalMid_Lock); |
| 1351 | |
Pavel Shilovsky | 026e93d | 2016-11-03 16:47:37 -0700 | [diff] [blame] | 1352 | cifs_crypto_secmech_release(server); |
Suresh Jayaraman | 488f1d2d | 2010-07-05 18:12:15 +0530 | [diff] [blame] | 1353 | cifs_fscache_release_client_cookie(server); |
| 1354 | |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 1355 | kfree(server->session_key.response); |
| 1356 | server->session_key.response = NULL; |
| 1357 | server->session_key.len = 0; |
Shyam Prasad N | 7be3248 | 2021-10-14 11:52:39 +0000 | [diff] [blame] | 1358 | kfree(server->hostname); |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 1359 | |
| 1360 | task = xchg(&server->tsk, NULL); |
| 1361 | if (task) |
Eric W. Biederman | 72abe3b | 2019-05-15 12:33:50 -0500 | [diff] [blame] | 1362 | send_sig(SIGKILL, task, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1365 | struct TCP_Server_Info * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1366 | cifs_get_tcp_session(struct smb3_fs_context *ctx) |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1367 | { |
| 1368 | struct TCP_Server_Info *tcp_ses = NULL; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1369 | int rc; |
| 1370 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1371 | cifs_dbg(FYI, "UNC: %s\n", ctx->UNC); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1372 | |
| 1373 | /* see if we already have a matching tcp_ses */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1374 | tcp_ses = cifs_find_tcp_session(ctx); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1375 | if (tcp_ses) |
| 1376 | return tcp_ses; |
| 1377 | |
| 1378 | tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL); |
| 1379 | if (!tcp_ses) { |
| 1380 | rc = -ENOMEM; |
| 1381 | goto out_err; |
| 1382 | } |
| 1383 | |
Shyam Prasad N | 7be3248 | 2021-10-14 11:52:39 +0000 | [diff] [blame] | 1384 | tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL); |
| 1385 | if (!tcp_ses->hostname) { |
| 1386 | rc = -ENOMEM; |
| 1387 | goto out_err; |
| 1388 | } |
| 1389 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1390 | tcp_ses->ops = ctx->ops; |
| 1391 | tcp_ses->vals = ctx->vals; |
Rob Landley | f1d0c99 | 2011-01-22 15:44:05 -0600 | [diff] [blame] | 1392 | cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns)); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1393 | |
Shyam Prasad N | 6d82c27 | 2021-02-03 23:20:46 -0800 | [diff] [blame] | 1394 | tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1395 | tcp_ses->noblockcnt = ctx->rootfs; |
| 1396 | tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs; |
| 1397 | tcp_ses->noautotune = ctx->noautotune; |
| 1398 | tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay; |
| 1399 | tcp_ses->rdma = ctx->rdma; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 1400 | tcp_ses->in_flight = 0; |
Steve French | 1b63f18 | 2019-09-09 22:57:11 -0500 | [diff] [blame] | 1401 | tcp_ses->max_in_flight = 0; |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 1402 | tcp_ses->credits = 1; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1403 | init_waitqueue_head(&tcp_ses->response_q); |
| 1404 | init_waitqueue_head(&tcp_ses->request_q); |
| 1405 | INIT_LIST_HEAD(&tcp_ses->pending_mid_q); |
| 1406 | mutex_init(&tcp_ses->srv_mutex); |
| 1407 | memcpy(tcp_ses->workstation_RFC1001_name, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1408 | ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1409 | memcpy(tcp_ses->server_RFC1001_name, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1410 | ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); |
Shirish Pargaonkar | 5d0d288 | 2010-10-13 18:15:00 -0500 | [diff] [blame] | 1411 | tcp_ses->session_estab = false; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1412 | tcp_ses->sequence_number = 0; |
Pavel Shilovsky | 5b96485 | 2019-01-18 11:30:26 -0800 | [diff] [blame] | 1413 | tcp_ses->reconnect_instance = 1; |
Steve French | fda3594 | 2011-01-20 18:06:34 +0000 | [diff] [blame] | 1414 | tcp_ses->lstrp = jiffies; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1415 | tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression); |
Jeff Layton | 58fa015 | 2012-05-01 17:41:16 -0400 | [diff] [blame] | 1416 | spin_lock_init(&tcp_ses->req_lock); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1417 | INIT_LIST_HEAD(&tcp_ses->tcp_ses_list); |
| 1418 | INIT_LIST_HEAD(&tcp_ses->smb_ses_list); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1419 | INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request); |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 1420 | INIT_DELAYED_WORK(&tcp_ses->resolve, cifs_resolve_server); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1421 | INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server); |
| 1422 | mutex_init(&tcp_ses->reconnect_mutex); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1423 | memcpy(&tcp_ses->srcaddr, &ctx->srcaddr, |
Jeff Layton | 9fa114f | 2012-11-26 11:09:57 -0500 | [diff] [blame] | 1424 | sizeof(tcp_ses->srcaddr)); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1425 | memcpy(&tcp_ses->dstaddr, &ctx->dstaddr, |
Jeff Layton | 9fa114f | 2012-11-26 11:09:57 -0500 | [diff] [blame] | 1426 | sizeof(tcp_ses->dstaddr)); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1427 | if (ctx->use_client_guid) |
| 1428 | memcpy(tcp_ses->client_guid, ctx->client_guid, |
Aurelien Aptel | bcc8880 | 2019-09-20 04:32:20 +0200 | [diff] [blame] | 1429 | SMB2_CLIENT_GUID_SIZE); |
| 1430 | else |
| 1431 | generate_random_uuid(tcp_ses->client_guid); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1432 | /* |
| 1433 | * at this point we are the only ones with the pointer |
| 1434 | * to the struct since the kernel thread not created yet |
| 1435 | * no need to spinlock this init of tcpStatus or srv_count |
| 1436 | */ |
| 1437 | tcp_ses->tcpStatus = CifsNew; |
| 1438 | ++tcp_ses->srv_count; |
| 1439 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1440 | if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN && |
| 1441 | ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX) |
| 1442 | tcp_ses->echo_interval = ctx->echo_interval * HZ; |
Steve French | adfeb3e | 2015-12-18 12:31:36 -0600 | [diff] [blame] | 1443 | else |
| 1444 | tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ; |
Long Li | 2f89464 | 2017-11-22 17:38:34 -0700 | [diff] [blame] | 1445 | if (tcp_ses->rdma) { |
| 1446 | #ifndef CONFIG_CIFS_SMB_DIRECT |
| 1447 | cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n"); |
| 1448 | rc = -ENOENT; |
| 1449 | goto out_err_crypto_release; |
| 1450 | #endif |
| 1451 | tcp_ses->smbd_conn = smbd_get_connection( |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1452 | tcp_ses, (struct sockaddr *)&ctx->dstaddr); |
Long Li | 2f89464 | 2017-11-22 17:38:34 -0700 | [diff] [blame] | 1453 | if (tcp_ses->smbd_conn) { |
| 1454 | cifs_dbg(VFS, "RDMA transport established\n"); |
| 1455 | rc = 0; |
| 1456 | goto smbd_connected; |
| 1457 | } else { |
| 1458 | rc = -ENOENT; |
| 1459 | goto out_err_crypto_release; |
| 1460 | } |
| 1461 | } |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1462 | rc = ip_connect(tcp_ses); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1463 | if (rc < 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1464 | cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n"); |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 1465 | goto out_err_crypto_release; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1466 | } |
Long Li | 2f89464 | 2017-11-22 17:38:34 -0700 | [diff] [blame] | 1467 | smbd_connected: |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1468 | /* |
| 1469 | * since we're in a cifs function already, we know that |
| 1470 | * this will succeed. No need for try_module_get(). |
| 1471 | */ |
| 1472 | __module_get(THIS_MODULE); |
Al Viro | 7c97c20 | 2011-06-21 08:51:28 -0400 | [diff] [blame] | 1473 | tcp_ses->tsk = kthread_run(cifs_demultiplex_thread, |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1474 | tcp_ses, "cifsd"); |
| 1475 | if (IS_ERR(tcp_ses->tsk)) { |
| 1476 | rc = PTR_ERR(tcp_ses->tsk); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1477 | cifs_dbg(VFS, "error %d create cifsd thread\n", rc); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1478 | module_put(THIS_MODULE); |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 1479 | goto out_err_crypto_release; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1480 | } |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1481 | tcp_ses->min_offload = ctx->min_offload; |
Steve French | 01cf308 | 2021-07-01 12:22:47 -0500 | [diff] [blame] | 1482 | /* |
| 1483 | * at this point we are the only ones with the pointer |
| 1484 | * to the struct since the kernel thread not created yet |
| 1485 | * no need to spinlock this update of tcpStatus |
| 1486 | */ |
Steve French | fd88ce9 | 2011-04-12 01:01:14 +0000 | [diff] [blame] | 1487 | tcp_ses->tcpStatus = CifsNeedNegotiate; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1488 | |
Aurelien Aptel | a249cc8 | 2021-03-04 17:42:21 +0000 | [diff] [blame] | 1489 | if ((ctx->max_credits < 20) || (ctx->max_credits > 60000)) |
| 1490 | tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE; |
| 1491 | else |
| 1492 | tcp_ses->max_credits = ctx->max_credits; |
| 1493 | |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 1494 | tcp_ses->nr_targets = 1; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1495 | tcp_ses->ignore_signature = ctx->ignore_signature; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1496 | /* thread spawned, put it on the list */ |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1497 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1498 | list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1499 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1500 | |
Suresh Jayaraman | 488f1d2d | 2010-07-05 18:12:15 +0530 | [diff] [blame] | 1501 | cifs_fscache_get_client_cookie(tcp_ses); |
| 1502 | |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1503 | /* queue echo request delayed work */ |
Steve French | adfeb3e | 2015-12-18 12:31:36 -0600 | [diff] [blame] | 1504 | queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1505 | |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 1506 | /* queue dns resolution delayed work */ |
| 1507 | cifs_dbg(FYI, "%s: next dns resolution scheduled for %d seconds in the future\n", |
| 1508 | __func__, SMB_DNS_RESOLVE_INTERVAL_DEFAULT); |
| 1509 | |
| 1510 | queue_delayed_work(cifsiod_wq, &tcp_ses->resolve, (SMB_DNS_RESOLVE_INTERVAL_DEFAULT * HZ)); |
| 1511 | |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1512 | return tcp_ses; |
| 1513 | |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 1514 | out_err_crypto_release: |
Pavel Shilovsky | 026e93d | 2016-11-03 16:47:37 -0700 | [diff] [blame] | 1515 | cifs_crypto_secmech_release(tcp_ses); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 1516 | |
Rob Landley | f1d0c99 | 2011-01-22 15:44:05 -0600 | [diff] [blame] | 1517 | put_net(cifs_net_ns(tcp_ses)); |
| 1518 | |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1519 | out_err: |
| 1520 | if (tcp_ses) { |
Shyam Prasad N | 7be3248 | 2021-10-14 11:52:39 +0000 | [diff] [blame] | 1521 | kfree(tcp_ses->hostname); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1522 | if (tcp_ses->ssocket) |
| 1523 | sock_release(tcp_ses->ssocket); |
| 1524 | kfree(tcp_ses); |
| 1525 | } |
| 1526 | return ERR_PTR(rc); |
| 1527 | } |
| 1528 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1529 | static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1530 | { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1531 | if (ctx->sectype != Unspecified && |
| 1532 | ctx->sectype != ses->sectype) |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1533 | return 0; |
| 1534 | |
Aurelien Aptel | bcc8880 | 2019-09-20 04:32:20 +0200 | [diff] [blame] | 1535 | /* |
| 1536 | * If an existing session is limited to less channels than |
| 1537 | * requested, it should not be reused |
| 1538 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1539 | if (ses->chan_max < ctx->max_channels) |
Aurelien Aptel | bcc8880 | 2019-09-20 04:32:20 +0200 | [diff] [blame] | 1540 | return 0; |
| 1541 | |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1542 | switch (ses->sectype) { |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1543 | case Kerberos: |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1544 | if (!uid_eq(ctx->cred_uid, ses->cred_uid)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1545 | return 0; |
| 1546 | break; |
| 1547 | default: |
Jeff Layton | 04febab | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1548 | /* NULL username means anonymous session */ |
| 1549 | if (ses->user_name == NULL) { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1550 | if (!ctx->nullauth) |
Jeff Layton | 04febab | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1551 | return 0; |
| 1552 | break; |
| 1553 | } |
| 1554 | |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1555 | /* anything else takes username/password */ |
Jeff Layton | 04febab | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1556 | if (strncmp(ses->user_name, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1557 | ctx->username ? ctx->username : "", |
Scott Lovenberg | 8c3a2b4 | 2013-08-09 08:47:17 -0400 | [diff] [blame] | 1558 | CIFS_MAX_USERNAME_LEN)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1559 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1560 | if ((ctx->username && strlen(ctx->username) != 0) && |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1561 | ses->password != NULL && |
| 1562 | strncmp(ses->password, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1563 | ctx->password ? ctx->password : "", |
Scott Lovenberg | 8c3a2b4 | 2013-08-09 08:47:17 -0400 | [diff] [blame] | 1564 | CIFS_MAX_PASSWORD_LEN)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1565 | return 0; |
| 1566 | } |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1570 | /** |
| 1571 | * cifs_setup_ipc - helper to setup the IPC tcon for the session |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 1572 | * @ses: smb session to issue the request on |
| 1573 | * @ctx: the superblock configuration context to use for building the |
| 1574 | * new tree connection for the IPC (interprocess communication RPC) |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1575 | * |
| 1576 | * A new IPC connection is made and stored in the session |
| 1577 | * tcon_ipc. The IPC tcon has the same lifetime as the session. |
| 1578 | */ |
| 1579 | static int |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1580 | cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1581 | { |
| 1582 | int rc = 0, xid; |
| 1583 | struct cifs_tcon *tcon; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1584 | char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0}; |
| 1585 | bool seal = false; |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1586 | struct TCP_Server_Info *server = ses->server; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1587 | |
| 1588 | /* |
| 1589 | * If the mount request that resulted in the creation of the |
| 1590 | * session requires encryption, force IPC to be encrypted too. |
| 1591 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1592 | if (ctx->seal) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1593 | if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1594 | seal = true; |
| 1595 | else { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1596 | cifs_server_dbg(VFS, |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1597 | "IPC: server doesn't support encryption\n"); |
| 1598 | return -EOPNOTSUPP; |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | tcon = tconInfoAlloc(); |
| 1603 | if (tcon == NULL) |
| 1604 | return -ENOMEM; |
| 1605 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1606 | scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1607 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1608 | xid = get_xid(); |
| 1609 | tcon->ses = ses; |
| 1610 | tcon->ipc = true; |
| 1611 | tcon->seal = seal; |
Ronnie Sahlberg | 6fd4ea8 | 2020-12-14 16:40:21 +1000 | [diff] [blame] | 1612 | rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1613 | free_xid(xid); |
| 1614 | |
| 1615 | if (rc) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1616 | cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1617 | tconInfoFree(tcon); |
| 1618 | goto out; |
| 1619 | } |
| 1620 | |
| 1621 | cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid); |
| 1622 | |
| 1623 | ses->tcon_ipc = tcon; |
| 1624 | out: |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1625 | return rc; |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * cifs_free_ipc - helper to release the session IPC tcon |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 1630 | * @ses: smb session to unmount the IPC from |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1631 | * |
Paulo Alcantara | f3191fc | 2021-06-04 19:25:27 -0300 | [diff] [blame] | 1632 | * Needs to be called everytime a session is destroyed. |
| 1633 | * |
| 1634 | * On session close, the IPC is closed and the server must release all tcons of the session. |
| 1635 | * No need to send a tree disconnect here. |
| 1636 | * |
| 1637 | * Besides, it will make the server to not close durable and resilient files on session close, as |
| 1638 | * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request. |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1639 | */ |
| 1640 | static int |
| 1641 | cifs_free_ipc(struct cifs_ses *ses) |
| 1642 | { |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1643 | struct cifs_tcon *tcon = ses->tcon_ipc; |
| 1644 | |
| 1645 | if (tcon == NULL) |
| 1646 | return 0; |
| 1647 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1648 | tconInfoFree(tcon); |
| 1649 | ses->tcon_ipc = NULL; |
Paulo Alcantara | f3191fc | 2021-06-04 19:25:27 -0300 | [diff] [blame] | 1650 | return 0; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1651 | } |
| 1652 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1653 | static struct cifs_ses * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1654 | cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | { |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1656 | struct cifs_ses *ses; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1658 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 4ff67b7 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1659 | list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1660 | if (ses->status == CifsExiting) |
| 1661 | continue; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1662 | if (!match_session(ses, ctx)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1663 | continue; |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1664 | ++ses->ses_count; |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1665 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1666 | return ses; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | } |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1668 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | return NULL; |
| 1670 | } |
| 1671 | |
Paulo Alcantara (SUSE) | 5072010 | 2019-03-19 16:54:29 -0300 | [diff] [blame] | 1672 | void cifs_put_smb_ses(struct cifs_ses *ses) |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1673 | { |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1674 | unsigned int rc, xid; |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1675 | struct TCP_Server_Info *server = ses->server; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1676 | cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count); |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1677 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1678 | spin_lock(&cifs_tcp_ses_lock); |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1679 | if (ses->status == CifsExiting) { |
| 1680 | spin_unlock(&cifs_tcp_ses_lock); |
| 1681 | return; |
| 1682 | } |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 1683 | |
| 1684 | cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count); |
| 1685 | cifs_dbg(FYI, "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->treeName : "NONE"); |
| 1686 | |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1687 | if (--ses->ses_count > 0) { |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1688 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1689 | return; |
| 1690 | } |
Steve French | 0060a4f | 2021-06-24 15:28:04 -0500 | [diff] [blame] | 1691 | spin_unlock(&cifs_tcp_ses_lock); |
| 1692 | |
Shyam Prasad N | 16dd9b8 | 2021-07-14 23:00:00 -0500 | [diff] [blame] | 1693 | /* ses_count can never go negative */ |
| 1694 | WARN_ON(ses->ses_count < 0); |
| 1695 | |
Steve French | 0060a4f | 2021-06-24 15:28:04 -0500 | [diff] [blame] | 1696 | spin_lock(&GlobalMid_Lock); |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1697 | if (ses->status == CifsGood) |
| 1698 | ses->status = CifsExiting; |
Steve French | 0060a4f | 2021-06-24 15:28:04 -0500 | [diff] [blame] | 1699 | spin_unlock(&GlobalMid_Lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1700 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1701 | cifs_free_ipc(ses); |
| 1702 | |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1703 | if (ses->status == CifsExiting && server->ops->logoff) { |
| 1704 | xid = get_xid(); |
| 1705 | rc = server->ops->logoff(xid, ses); |
| 1706 | if (rc) |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1707 | cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n", |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1708 | __func__, rc); |
| 1709 | _free_xid(xid); |
| 1710 | } |
| 1711 | |
| 1712 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1713 | list_del_init(&ses->smb_ses_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1714 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1715 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1716 | /* close any extra channels */ |
| 1717 | if (ses->chan_count > 1) { |
| 1718 | int i; |
| 1719 | |
| 1720 | for (i = 1; i < ses->chan_count; i++) |
| 1721 | cifs_put_tcp_session(ses->chans[i].server, 0); |
| 1722 | } |
| 1723 | |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1724 | sesInfoFree(ses); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1725 | cifs_put_tcp_session(server, 0); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1726 | } |
| 1727 | |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1728 | #ifdef CONFIG_KEYS |
| 1729 | |
Chen Gang | 057d633 | 2013-07-19 09:01:36 +0800 | [diff] [blame] | 1730 | /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */ |
| 1731 | #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1) |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1732 | |
| 1733 | /* Populate username and pw fields from keyring if possible */ |
| 1734 | static int |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1735 | cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses) |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1736 | { |
| 1737 | int rc = 0; |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1738 | int is_domain = 0; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 1739 | const char *delim, *payload; |
| 1740 | char *desc; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1741 | ssize_t len; |
| 1742 | struct key *key; |
| 1743 | struct TCP_Server_Info *server = ses->server; |
| 1744 | struct sockaddr_in *sa; |
| 1745 | struct sockaddr_in6 *sa6; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 1746 | const struct user_key_payload *upayload; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1747 | |
| 1748 | desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL); |
| 1749 | if (!desc) |
| 1750 | return -ENOMEM; |
| 1751 | |
| 1752 | /* try to find an address key first */ |
| 1753 | switch (server->dstaddr.ss_family) { |
| 1754 | case AF_INET: |
| 1755 | sa = (struct sockaddr_in *)&server->dstaddr; |
| 1756 | sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr); |
| 1757 | break; |
| 1758 | case AF_INET6: |
| 1759 | sa6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 1760 | sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr); |
| 1761 | break; |
| 1762 | default: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1763 | cifs_dbg(FYI, "Bad ss_family (%hu)\n", |
| 1764 | server->dstaddr.ss_family); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1765 | rc = -EINVAL; |
| 1766 | goto out_err; |
| 1767 | } |
| 1768 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1769 | cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 1770 | key = request_key(&key_type_logon, desc, ""); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1771 | if (IS_ERR(key)) { |
| 1772 | if (!ses->domainName) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1773 | cifs_dbg(FYI, "domainName is NULL\n"); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1774 | rc = PTR_ERR(key); |
| 1775 | goto out_err; |
| 1776 | } |
| 1777 | |
| 1778 | /* didn't work, try to find a domain key */ |
| 1779 | sprintf(desc, "cifs:d:%s", ses->domainName); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1780 | cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 1781 | key = request_key(&key_type_logon, desc, ""); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1782 | if (IS_ERR(key)) { |
| 1783 | rc = PTR_ERR(key); |
| 1784 | goto out_err; |
| 1785 | } |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1786 | is_domain = 1; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | down_read(&key->sem); |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 1790 | upayload = user_key_payload_locked(key); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1791 | if (IS_ERR_OR_NULL(upayload)) { |
Jeff Layton | 4edc53c | 2012-02-07 06:30:51 -0500 | [diff] [blame] | 1792 | rc = upayload ? PTR_ERR(upayload) : -EINVAL; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1793 | goto out_key_put; |
| 1794 | } |
| 1795 | |
| 1796 | /* find first : in payload */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 1797 | payload = upayload->data; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1798 | delim = strnchr(payload, upayload->datalen, ':'); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1799 | cifs_dbg(FYI, "payload=%s\n", payload); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1800 | if (!delim) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1801 | cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n", |
| 1802 | upayload->datalen); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1803 | rc = -EINVAL; |
| 1804 | goto out_key_put; |
| 1805 | } |
| 1806 | |
| 1807 | len = delim - payload; |
Scott Lovenberg | 8c3a2b4 | 2013-08-09 08:47:17 -0400 | [diff] [blame] | 1808 | if (len > CIFS_MAX_USERNAME_LEN || len <= 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1809 | cifs_dbg(FYI, "Bad value from username search (len=%zd)\n", |
| 1810 | len); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1811 | rc = -EINVAL; |
| 1812 | goto out_key_put; |
| 1813 | } |
| 1814 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1815 | ctx->username = kstrndup(payload, len, GFP_KERNEL); |
| 1816 | if (!ctx->username) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1817 | cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n", |
| 1818 | len); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1819 | rc = -ENOMEM; |
| 1820 | goto out_key_put; |
| 1821 | } |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1822 | cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1823 | |
| 1824 | len = key->datalen - (len + 1); |
Scott Lovenberg | 8c3a2b4 | 2013-08-09 08:47:17 -0400 | [diff] [blame] | 1825 | if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1826 | cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1827 | rc = -EINVAL; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1828 | kfree(ctx->username); |
| 1829 | ctx->username = NULL; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1830 | goto out_key_put; |
| 1831 | } |
| 1832 | |
| 1833 | ++delim; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1834 | ctx->password = kstrndup(delim, len, GFP_KERNEL); |
| 1835 | if (!ctx->password) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1836 | cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n", |
| 1837 | len); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1838 | rc = -ENOMEM; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1839 | kfree(ctx->username); |
| 1840 | ctx->username = NULL; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1841 | goto out_key_put; |
| 1842 | } |
| 1843 | |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1844 | /* |
| 1845 | * If we have a domain key then we must set the domainName in the |
| 1846 | * for the request. |
| 1847 | */ |
| 1848 | if (is_domain && ses->domainName) { |
Al Viro | 8d76722 | 2021-03-05 15:02:34 -0500 | [diff] [blame] | 1849 | ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1850 | if (!ctx->domainname) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1851 | cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n", |
| 1852 | len); |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1853 | rc = -ENOMEM; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1854 | kfree(ctx->username); |
| 1855 | ctx->username = NULL; |
| 1856 | kfree_sensitive(ctx->password); |
| 1857 | ctx->password = NULL; |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1858 | goto out_key_put; |
| 1859 | } |
| 1860 | } |
| 1861 | |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1862 | out_key_put: |
| 1863 | up_read(&key->sem); |
| 1864 | key_put(key); |
| 1865 | out_err: |
| 1866 | kfree(desc); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1867 | cifs_dbg(FYI, "%s: returning %d\n", __func__, rc); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1868 | return rc; |
| 1869 | } |
| 1870 | #else /* ! CONFIG_KEYS */ |
| 1871 | static inline int |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1872 | cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)), |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1873 | struct cifs_ses *ses __attribute__((unused))) |
| 1874 | { |
| 1875 | return -ENOSYS; |
| 1876 | } |
| 1877 | #endif /* CONFIG_KEYS */ |
| 1878 | |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 1879 | /** |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1880 | * cifs_get_smb_ses - get a session matching @ctx data from @server |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 1881 | * @server: server to setup the session to |
| 1882 | * @ctx: superblock configuration context to use to setup the session |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 1883 | * |
| 1884 | * This function assumes it is being called from cifs_mount() where we |
| 1885 | * already got a server reference (server refcount +1). See |
| 1886 | * cifs_get_tcon() for refcount explanations. |
| 1887 | */ |
Paulo Alcantara (SUSE) | 5072010 | 2019-03-19 16:54:29 -0300 | [diff] [blame] | 1888 | struct cifs_ses * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1889 | cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1890 | { |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 1891 | int rc = -ENOMEM; |
| 1892 | unsigned int xid; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1893 | struct cifs_ses *ses; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1894 | struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; |
| 1895 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1896 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1897 | xid = get_xid(); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1898 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1899 | ses = cifs_find_smb_ses(server, ctx); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1900 | if (ses) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1901 | cifs_dbg(FYI, "Existing smb sess found (status=%d)\n", |
| 1902 | ses->status); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1903 | |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1904 | mutex_lock(&ses->session_mutex); |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 1905 | rc = cifs_negotiate_protocol(xid, ses); |
| 1906 | if (rc) { |
| 1907 | mutex_unlock(&ses->session_mutex); |
| 1908 | /* problem -- put our ses reference */ |
| 1909 | cifs_put_smb_ses(ses); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1910 | free_xid(xid); |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 1911 | return ERR_PTR(rc); |
| 1912 | } |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1913 | if (ses->need_reconnect) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1914 | cifs_dbg(FYI, "Session needs reconnect\n"); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1915 | rc = cifs_setup_session(xid, ses, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1916 | ctx->local_nls); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1917 | if (rc) { |
| 1918 | mutex_unlock(&ses->session_mutex); |
| 1919 | /* problem -- put our reference */ |
| 1920 | cifs_put_smb_ses(ses); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1921 | free_xid(xid); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1922 | return ERR_PTR(rc); |
| 1923 | } |
| 1924 | } |
| 1925 | mutex_unlock(&ses->session_mutex); |
Jeff Layton | 460cf34 | 2010-09-14 11:38:24 -0400 | [diff] [blame] | 1926 | |
| 1927 | /* existing SMB ses has a server reference already */ |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1928 | cifs_put_tcp_session(server, 0); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1929 | free_xid(xid); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1930 | return ses; |
| 1931 | } |
| 1932 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1933 | cifs_dbg(FYI, "Existing smb sess not found\n"); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1934 | ses = sesInfoAlloc(); |
| 1935 | if (ses == NULL) |
| 1936 | goto get_ses_fail; |
| 1937 | |
| 1938 | /* new SMB session uses our server ref */ |
| 1939 | ses->server = server; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1940 | if (server->dstaddr.ss_family == AF_INET6) |
Steve French | b438fcf | 2021-02-20 19:24:11 -0600 | [diff] [blame] | 1941 | sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1942 | else |
Steve French | b438fcf | 2021-02-20 19:24:11 -0600 | [diff] [blame] | 1943 | sprintf(ses->ip_addr, "%pI4", &addr->sin_addr); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1944 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1945 | if (ctx->username) { |
| 1946 | ses->user_name = kstrdup(ctx->username, GFP_KERNEL); |
Steve French | 8727c8a | 2011-02-25 01:11:56 -0600 | [diff] [blame] | 1947 | if (!ses->user_name) |
| 1948 | goto get_ses_fail; |
| 1949 | } |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1950 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1951 | /* ctx->password freed at unmount */ |
| 1952 | if (ctx->password) { |
| 1953 | ses->password = kstrdup(ctx->password, GFP_KERNEL); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1954 | if (!ses->password) |
| 1955 | goto get_ses_fail; |
| 1956 | } |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1957 | if (ctx->domainname) { |
| 1958 | ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL); |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 1959 | if (!ses->domainName) |
| 1960 | goto get_ses_fail; |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1961 | } |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 1962 | if (ctx->workstation_name) { |
| 1963 | ses->workstation_name = kstrdup(ctx->workstation_name, |
| 1964 | GFP_KERNEL); |
| 1965 | if (!ses->workstation_name) |
| 1966 | goto get_ses_fail; |
| 1967 | } |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1968 | if (ctx->domainauto) |
| 1969 | ses->domainAuto = ctx->domainauto; |
| 1970 | ses->cred_uid = ctx->cred_uid; |
| 1971 | ses->linux_uid = ctx->linux_uid; |
Steve French | d9b9420 | 2011-04-12 01:24:57 +0000 | [diff] [blame] | 1972 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1973 | ses->sectype = ctx->sectype; |
| 1974 | ses->sign = ctx->sign; |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1975 | mutex_lock(&ses->session_mutex); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1976 | |
| 1977 | /* add server as first channel */ |
| 1978 | ses->chans[0].server = server; |
| 1979 | ses->chan_count = 1; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1980 | ses->chan_max = ctx->multichannel ? ctx->max_channels:1; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1981 | |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 1982 | rc = cifs_negotiate_protocol(xid, ses); |
| 1983 | if (!rc) |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1984 | rc = cifs_setup_session(xid, ses, ctx->local_nls); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1985 | |
| 1986 | /* each channel uses a different signing key */ |
| 1987 | memcpy(ses->chans[0].signkey, ses->smb3signingkey, |
| 1988 | sizeof(ses->smb3signingkey)); |
| 1989 | |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1990 | mutex_unlock(&ses->session_mutex); |
Steve French | c8e56f1 | 2010-09-08 21:10:58 +0000 | [diff] [blame] | 1991 | if (rc) |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1992 | goto get_ses_fail; |
| 1993 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1994 | /* success, put it on the list and add it as first channel */ |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1995 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1996 | list_add(&ses->smb_ses_list, &server->smb_ses_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1997 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 1998 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1999 | free_xid(xid); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 2000 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2001 | cifs_setup_ipc(ses, ctx); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 2002 | |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2003 | return ses; |
| 2004 | |
| 2005 | get_ses_fail: |
| 2006 | sesInfoFree(ses); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2007 | free_xid(xid); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2008 | return ERR_PTR(rc); |
| 2009 | } |
| 2010 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2011 | static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 2012 | { |
| 2013 | if (tcon->tidStatus == CifsExiting) |
| 2014 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2015 | if (strncmp(tcon->treeName, ctx->UNC, MAX_TREE_SIZE)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 2016 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2017 | if (tcon->seal != ctx->seal) |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 2018 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2019 | if (tcon->snapshot_time != ctx->snapshot_time) |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 2020 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2021 | if (tcon->handle_timeout != ctx->handle_timeout) |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2022 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2023 | if (tcon->no_lease != ctx->no_lease) |
Steve French | 3e7a02d | 2019-09-11 21:46:20 -0500 | [diff] [blame] | 2024 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2025 | if (tcon->nodelete != ctx->nodelete) |
Steve French | 82e9367 | 2020-05-19 03:06:57 -0500 | [diff] [blame] | 2026 | return 0; |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 2027 | return 1; |
| 2028 | } |
| 2029 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2030 | static struct cifs_tcon * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2031 | cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | { |
| 2033 | struct list_head *tmp; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2034 | struct cifs_tcon *tcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2036 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2037 | list_for_each(tmp, &ses->tcon_list) { |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2038 | tcon = list_entry(tmp, struct cifs_tcon, tcon_list); |
Paulo Alcantara | f3c852b | 2021-06-04 19:25:33 -0300 | [diff] [blame] | 2039 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2040 | if (!match_tcon(tcon, ctx)) |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2041 | continue; |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2042 | ++tcon->tc_count; |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2043 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | return tcon; |
| 2045 | } |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2046 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | return NULL; |
| 2048 | } |
| 2049 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 2050 | void |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2051 | cifs_put_tcon(struct cifs_tcon *tcon) |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2052 | { |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 2053 | unsigned int xid; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 2054 | struct cifs_ses *ses; |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2055 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 2056 | /* |
| 2057 | * IPC tcon share the lifetime of their session and are |
| 2058 | * destroyed in the session put function |
| 2059 | */ |
| 2060 | if (tcon == NULL || tcon->ipc) |
| 2061 | return; |
| 2062 | |
| 2063 | ses = tcon->ses; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2064 | cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2065 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2066 | if (--tcon->tc_count > 0) { |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2067 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2068 | return; |
| 2069 | } |
| 2070 | |
Shyam Prasad N | 16dd9b8 | 2021-07-14 23:00:00 -0500 | [diff] [blame] | 2071 | /* tc_count can never go negative */ |
| 2072 | WARN_ON(tcon->tc_count < 0); |
| 2073 | |
Samuel Cabrero | bf80e5d | 2020-11-30 19:02:51 +0100 | [diff] [blame] | 2074 | if (tcon->use_witness) { |
| 2075 | int rc; |
| 2076 | |
| 2077 | rc = cifs_swn_unregister(tcon); |
| 2078 | if (rc < 0) { |
| 2079 | cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n", |
| 2080 | __func__, rc); |
| 2081 | } |
| 2082 | } |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2083 | |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2084 | list_del_init(&tcon->tcon_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2085 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2086 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2087 | xid = get_xid(); |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 2088 | if (ses->server->ops->tree_disconnect) |
| 2089 | ses->server->ops->tree_disconnect(xid, tcon); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2090 | _free_xid(xid); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2091 | |
Suresh Jayaraman | d03382c | 2010-07-05 18:12:27 +0530 | [diff] [blame] | 2092 | cifs_fscache_release_super_cookie(tcon); |
Steve French | 9f84159 | 2010-07-23 20:37:53 +0000 | [diff] [blame] | 2093 | tconInfoFree(tcon); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2094 | cifs_put_smb_ses(ses); |
| 2095 | } |
| 2096 | |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 2097 | /** |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2098 | * cifs_get_tcon - get a tcon matching @ctx data from @ses |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 2099 | * @ses: smb session to issue the request on |
| 2100 | * @ctx: the superblock configuration context to use for building the |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 2101 | * |
| 2102 | * - tcon refcount is the number of mount points using the tcon. |
| 2103 | * - ses refcount is the number of tcon using the session. |
| 2104 | * |
| 2105 | * 1. This function assumes it is being called from cifs_mount() where |
| 2106 | * we already got a session reference (ses refcount +1). |
| 2107 | * |
| 2108 | * 2. Since we're in the context of adding a mount point, the end |
| 2109 | * result should be either: |
| 2110 | * |
| 2111 | * a) a new tcon already allocated with refcount=1 (1 mount point) and |
| 2112 | * its session refcount incremented (1 new tcon). This +1 was |
| 2113 | * already done in (1). |
| 2114 | * |
| 2115 | * b) an existing tcon with refcount+1 (add a mount point to it) and |
| 2116 | * identical ses refcount (no new tcon). Because of (1) we need to |
| 2117 | * decrement the ses refcount. |
| 2118 | */ |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2119 | static struct cifs_tcon * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2120 | cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2121 | { |
| 2122 | int rc, xid; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2123 | struct cifs_tcon *tcon; |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2124 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2125 | tcon = cifs_find_tcon(ses, ctx); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2126 | if (tcon) { |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 2127 | /* |
| 2128 | * tcon has refcount already incremented but we need to |
| 2129 | * decrement extra ses reference gotten by caller (case b) |
| 2130 | */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2131 | cifs_dbg(FYI, "Found match on UNC path\n"); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2132 | cifs_put_smb_ses(ses); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2133 | return tcon; |
| 2134 | } |
| 2135 | |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 2136 | if (!ses->server->ops->tree_connect) { |
| 2137 | rc = -ENOSYS; |
| 2138 | goto out_fail; |
| 2139 | } |
| 2140 | |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2141 | tcon = tconInfoAlloc(); |
| 2142 | if (tcon == NULL) { |
| 2143 | rc = -ENOMEM; |
| 2144 | goto out_fail; |
| 2145 | } |
| 2146 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2147 | if (ctx->snapshot_time) { |
Steve French | 8b217fe | 2016-11-11 22:36:20 -0600 | [diff] [blame] | 2148 | if (ses->server->vals->protocol_id == 0) { |
| 2149 | cifs_dbg(VFS, |
| 2150 | "Use SMB2 or later for snapshot mount option\n"); |
| 2151 | rc = -EOPNOTSUPP; |
| 2152 | goto out_fail; |
| 2153 | } else |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2154 | tcon->snapshot_time = ctx->snapshot_time; |
Steve French | 8b217fe | 2016-11-11 22:36:20 -0600 | [diff] [blame] | 2155 | } |
| 2156 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2157 | if (ctx->handle_timeout) { |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2158 | if (ses->server->vals->protocol_id == 0) { |
| 2159 | cifs_dbg(VFS, |
| 2160 | "Use SMB2.1 or later for handle timeout option\n"); |
| 2161 | rc = -EOPNOTSUPP; |
| 2162 | goto out_fail; |
| 2163 | } else |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2164 | tcon->handle_timeout = ctx->handle_timeout; |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2165 | } |
| 2166 | |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2167 | tcon->ses = ses; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2168 | if (ctx->password) { |
| 2169 | tcon->password = kstrdup(ctx->password, GFP_KERNEL); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2170 | if (!tcon->password) { |
| 2171 | rc = -ENOMEM; |
| 2172 | goto out_fail; |
| 2173 | } |
| 2174 | } |
| 2175 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2176 | if (ctx->seal) { |
Steve French | 23657ad | 2018-04-22 15:14:58 -0500 | [diff] [blame] | 2177 | if (ses->server->vals->protocol_id == 0) { |
| 2178 | cifs_dbg(VFS, |
| 2179 | "SMB3 or later required for encryption\n"); |
| 2180 | rc = -EOPNOTSUPP; |
| 2181 | goto out_fail; |
| 2182 | } else if (tcon->ses->server->capabilities & |
| 2183 | SMB2_GLOBAL_CAP_ENCRYPTION) |
| 2184 | tcon->seal = true; |
| 2185 | else { |
| 2186 | cifs_dbg(VFS, "Encryption is not supported on share\n"); |
| 2187 | rc = -EOPNOTSUPP; |
| 2188 | goto out_fail; |
| 2189 | } |
| 2190 | } |
| 2191 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2192 | if (ctx->linux_ext) { |
Steve French | 8505c8b | 2018-06-18 14:01:59 -0500 | [diff] [blame] | 2193 | if (ses->server->posix_ext_supported) { |
Steve French | b326614 | 2018-05-20 23:41:10 -0500 | [diff] [blame] | 2194 | tcon->posix_extensions = true; |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 2195 | pr_warn_once("SMB3.11 POSIX Extensions are experimental\n"); |
Steve French | 8505c8b | 2018-06-18 14:01:59 -0500 | [diff] [blame] | 2196 | } else { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 2197 | cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n"); |
Steve French | 8505c8b | 2018-06-18 14:01:59 -0500 | [diff] [blame] | 2198 | rc = -EOPNOTSUPP; |
| 2199 | goto out_fail; |
Steve French | 2fbb564 | 2018-06-12 12:11:31 -0500 | [diff] [blame] | 2200 | } |
Steve French | b326614 | 2018-05-20 23:41:10 -0500 | [diff] [blame] | 2201 | } |
Steve French | b326614 | 2018-05-20 23:41:10 -0500 | [diff] [blame] | 2202 | |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 2203 | /* |
| 2204 | * BB Do we need to wrap session_mutex around this TCon call and Unix |
| 2205 | * SetFS as we do on SessSetup and reconnect? |
| 2206 | */ |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2207 | xid = get_xid(); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2208 | rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon, |
| 2209 | ctx->local_nls); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2210 | free_xid(xid); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2211 | cifs_dbg(FYI, "Tcon rc = %d\n", rc); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2212 | if (rc) |
| 2213 | goto out_fail; |
| 2214 | |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2215 | tcon->use_persistent = false; |
| 2216 | /* check if SMB2 or later, CIFS does not support persistent handles */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2217 | if (ctx->persistent) { |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2218 | if (ses->server->vals->protocol_id == 0) { |
| 2219 | cifs_dbg(VFS, |
| 2220 | "SMB3 or later required for persistent handles\n"); |
| 2221 | rc = -EOPNOTSUPP; |
| 2222 | goto out_fail; |
| 2223 | } else if (ses->server->capabilities & |
| 2224 | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) |
| 2225 | tcon->use_persistent = true; |
| 2226 | else /* persistent handles requested but not supported */ { |
| 2227 | cifs_dbg(VFS, |
| 2228 | "Persistent handles not supported on share\n"); |
| 2229 | rc = -EOPNOTSUPP; |
| 2230 | goto out_fail; |
| 2231 | } |
| 2232 | } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY) |
| 2233 | && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2234 | && (ctx->nopersistent == false)) { |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2235 | cifs_dbg(FYI, "enabling persistent handles\n"); |
| 2236 | tcon->use_persistent = true; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2237 | } else if (ctx->resilient) { |
Steve French | 592fafe | 2015-11-03 10:08:53 -0600 | [diff] [blame] | 2238 | if (ses->server->vals->protocol_id == 0) { |
| 2239 | cifs_dbg(VFS, |
| 2240 | "SMB2.1 or later required for resilient handles\n"); |
| 2241 | rc = -EOPNOTSUPP; |
| 2242 | goto out_fail; |
| 2243 | } |
| 2244 | tcon->use_resilient = true; |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2245 | } |
Aurelien Aptel | b7fd0fa | 2021-04-09 16:31:37 +0200 | [diff] [blame] | 2246 | |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2247 | tcon->use_witness = false; |
Aurelien Aptel | b7fd0fa | 2021-04-09 16:31:37 +0200 | [diff] [blame] | 2248 | if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) { |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2249 | if (ses->server->vals->protocol_id >= SMB30_PROT_ID) { |
| 2250 | if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) { |
Samuel Cabrero | bf80e5d | 2020-11-30 19:02:51 +0100 | [diff] [blame] | 2251 | /* |
| 2252 | * Set witness in use flag in first place |
| 2253 | * to retry registration in the echo task |
| 2254 | */ |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2255 | tcon->use_witness = true; |
Samuel Cabrero | bf80e5d | 2020-11-30 19:02:51 +0100 | [diff] [blame] | 2256 | /* And try to register immediately */ |
| 2257 | rc = cifs_swn_register(tcon); |
| 2258 | if (rc < 0) { |
| 2259 | cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc); |
| 2260 | goto out_fail; |
| 2261 | } |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2262 | } else { |
| 2263 | /* TODO: try to extend for non-cluster uses (eg multichannel) */ |
| 2264 | cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n"); |
| 2265 | rc = -EOPNOTSUPP; |
| 2266 | goto out_fail; |
| 2267 | } |
| 2268 | } else { |
| 2269 | cifs_dbg(VFS, "SMB3 or later required for witness option\n"); |
| 2270 | rc = -EOPNOTSUPP; |
| 2271 | goto out_fail; |
| 2272 | } |
| 2273 | } |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2274 | |
Steve French | cae53f7 | 2019-09-03 17:49:46 -0500 | [diff] [blame] | 2275 | /* If the user really knows what they are doing they can override */ |
| 2276 | if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2277 | if (ctx->cache_ro) |
Steve French | cae53f7 | 2019-09-03 17:49:46 -0500 | [diff] [blame] | 2278 | cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n"); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2279 | else if (ctx->cache_rw) |
Steve French | cae53f7 | 2019-09-03 17:49:46 -0500 | [diff] [blame] | 2280 | cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n"); |
| 2281 | } |
| 2282 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2283 | if (ctx->no_lease) { |
Kenneth D'souza | 8fd6e1d | 2020-05-18 13:01:34 +0530 | [diff] [blame] | 2284 | if (ses->server->vals->protocol_id == 0) { |
| 2285 | cifs_dbg(VFS, |
| 2286 | "SMB2 or later required for nolease option\n"); |
| 2287 | rc = -EOPNOTSUPP; |
| 2288 | goto out_fail; |
| 2289 | } else |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2290 | tcon->no_lease = ctx->no_lease; |
Kenneth D'souza | 8fd6e1d | 2020-05-18 13:01:34 +0530 | [diff] [blame] | 2291 | } |
| 2292 | |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 2293 | /* |
| 2294 | * We can have only one retry value for a connection to a share so for |
| 2295 | * resources mounted more than once to the same server share the last |
| 2296 | * value passed in for the retry flag is used. |
| 2297 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2298 | tcon->retry = ctx->retry; |
| 2299 | tcon->nocase = ctx->nocase; |
Steve French | 3c6e65e | 2020-10-21 00:15:42 -0500 | [diff] [blame] | 2300 | if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2301 | tcon->nohandlecache = ctx->nohandlecache; |
Steve French | 3c6e65e | 2020-10-21 00:15:42 -0500 | [diff] [blame] | 2302 | else |
Jiapeng Zhong | 2be449f | 2021-01-14 17:09:20 +0800 | [diff] [blame] | 2303 | tcon->nohandlecache = true; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2304 | tcon->nodelete = ctx->nodelete; |
| 2305 | tcon->local_lease = ctx->local_lease; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 2306 | INIT_LIST_HEAD(&tcon->pending_opens); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2307 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2308 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2309 | list_add(&tcon->tcon_list, &ses->tcon_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2310 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2311 | |
Suresh Jayaraman | d03382c | 2010-07-05 18:12:27 +0530 | [diff] [blame] | 2312 | cifs_fscache_get_super_cookie(tcon); |
| 2313 | |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2314 | return tcon; |
| 2315 | |
| 2316 | out_fail: |
| 2317 | tconInfoFree(tcon); |
| 2318 | return ERR_PTR(rc); |
| 2319 | } |
| 2320 | |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 2321 | void |
| 2322 | cifs_put_tlink(struct tcon_link *tlink) |
| 2323 | { |
| 2324 | if (!tlink || IS_ERR(tlink)) |
| 2325 | return; |
| 2326 | |
| 2327 | if (!atomic_dec_and_test(&tlink->tl_count) || |
| 2328 | test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) { |
| 2329 | tlink->tl_time = jiffies; |
| 2330 | return; |
| 2331 | } |
| 2332 | |
| 2333 | if (!IS_ERR(tlink_tcon(tlink))) |
| 2334 | cifs_put_tcon(tlink_tcon(tlink)); |
| 2335 | kfree(tlink); |
| 2336 | return; |
| 2337 | } |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2338 | |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2339 | static int |
| 2340 | compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data) |
| 2341 | { |
| 2342 | struct cifs_sb_info *old = CIFS_SB(sb); |
| 2343 | struct cifs_sb_info *new = mnt_data->cifs_sb; |
Paulo Alcantara (SUSE) | 29fbeb7 | 2019-06-18 16:16:02 -0300 | [diff] [blame] | 2344 | unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK; |
| 2345 | unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2346 | |
| 2347 | if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK)) |
| 2348 | return 0; |
| 2349 | |
Paulo Alcantara (SUSE) | 29fbeb7 | 2019-06-18 16:16:02 -0300 | [diff] [blame] | 2350 | if (old->mnt_cifs_serverino_autodisabled) |
| 2351 | newflags &= ~CIFS_MOUNT_SERVER_INUM; |
| 2352 | |
| 2353 | if (oldflags != newflags) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2354 | return 0; |
| 2355 | |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2356 | /* |
Jeff Layton | 5eba8ab | 2011-10-19 15:30:26 -0400 | [diff] [blame] | 2357 | * We want to share sb only if we don't specify an r/wsize or |
| 2358 | * specified r/wsize is greater than or equal to existing one. |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2359 | */ |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 2360 | if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2361 | return 0; |
| 2362 | |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 2363 | if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize) |
Jeff Layton | 5eba8ab | 2011-10-19 15:30:26 -0400 | [diff] [blame] | 2364 | return 0; |
| 2365 | |
Ronnie Sahlberg | 8401e93 | 2020-12-12 13:40:50 -0600 | [diff] [blame] | 2366 | if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) || |
| 2367 | !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid)) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2368 | return 0; |
| 2369 | |
Ronnie Sahlberg | 8401e93 | 2020-12-12 13:40:50 -0600 | [diff] [blame] | 2370 | if (old->ctx->file_mode != new->ctx->file_mode || |
| 2371 | old->ctx->dir_mode != new->ctx->dir_mode) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2372 | return 0; |
| 2373 | |
| 2374 | if (strcmp(old->local_nls->charset, new->local_nls->charset)) |
| 2375 | return 0; |
| 2376 | |
Steve French | 5780464 | 2021-02-24 12:12:53 -0600 | [diff] [blame] | 2377 | if (old->ctx->acregmax != new->ctx->acregmax) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2378 | return 0; |
Steve French | 4c9f948 | 2021-02-23 15:50:57 -0600 | [diff] [blame] | 2379 | if (old->ctx->acdirmax != new->ctx->acdirmax) |
| 2380 | return 0; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2381 | |
| 2382 | return 1; |
| 2383 | } |
| 2384 | |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2385 | static int |
| 2386 | match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data) |
| 2387 | { |
| 2388 | struct cifs_sb_info *old = CIFS_SB(sb); |
| 2389 | struct cifs_sb_info *new = mnt_data->cifs_sb; |
Ronnie Sahlberg | fe12926 | 2020-01-22 11:07:56 +1000 | [diff] [blame] | 2390 | bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && |
| 2391 | old->prepath; |
| 2392 | bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && |
| 2393 | new->prepath; |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2394 | |
Sachin Prabhu | cd8c429 | 2017-04-26 14:05:46 +0100 | [diff] [blame] | 2395 | if (old_set && new_set && !strcmp(new->prepath, old->prepath)) |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2396 | return 1; |
Sachin Prabhu | cd8c429 | 2017-04-26 14:05:46 +0100 | [diff] [blame] | 2397 | else if (!old_set && !new_set) |
| 2398 | return 1; |
| 2399 | |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2400 | return 0; |
| 2401 | } |
| 2402 | |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2403 | int |
| 2404 | cifs_match_super(struct super_block *sb, void *data) |
| 2405 | { |
| 2406 | struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2407 | struct smb3_fs_context *ctx; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2408 | struct cifs_sb_info *cifs_sb; |
| 2409 | struct TCP_Server_Info *tcp_srv; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2410 | struct cifs_ses *ses; |
| 2411 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2412 | struct tcon_link *tlink; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2413 | int rc = 0; |
| 2414 | |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2415 | spin_lock(&cifs_tcp_ses_lock); |
| 2416 | cifs_sb = CIFS_SB(sb); |
| 2417 | tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); |
Steve French | 9ed38fd | 2021-09-23 19:18:37 -0500 | [diff] [blame] | 2418 | if (tlink == NULL) { |
| 2419 | /* can not match superblock if tlink were ever null */ |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2420 | spin_unlock(&cifs_tcp_ses_lock); |
Steve French | 9ed38fd | 2021-09-23 19:18:37 -0500 | [diff] [blame] | 2421 | return 0; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2422 | } |
| 2423 | tcon = tlink_tcon(tlink); |
| 2424 | ses = tcon->ses; |
| 2425 | tcp_srv = ses->server; |
| 2426 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2427 | ctx = mnt_data->ctx; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2428 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2429 | if (!match_server(tcp_srv, ctx) || |
| 2430 | !match_session(ses, ctx) || |
| 2431 | !match_tcon(tcon, ctx) || |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2432 | !match_prepath(sb, mnt_data)) { |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2433 | rc = 0; |
| 2434 | goto out; |
| 2435 | } |
| 2436 | |
| 2437 | rc = compare_mount_options(sb, mnt_data); |
| 2438 | out: |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2439 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | f484b5d0 | 2011-07-11 10:16:34 -0400 | [diff] [blame] | 2440 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2441 | return rc; |
| 2442 | } |
| 2443 | |
Jeff Layton | 09e50d5 | 2008-07-23 10:11:19 -0400 | [diff] [blame] | 2444 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 2445 | static struct lock_class_key cifs_key[2]; |
| 2446 | static struct lock_class_key cifs_slock_key[2]; |
| 2447 | |
| 2448 | static inline void |
| 2449 | cifs_reclassify_socket4(struct socket *sock) |
| 2450 | { |
| 2451 | struct sock *sk = sock->sk; |
Hannes Frederic Sowa | fafc4e1 | 2016-04-08 15:11:27 +0200 | [diff] [blame] | 2452 | BUG_ON(!sock_allow_reclassification(sk)); |
Jeff Layton | 09e50d5 | 2008-07-23 10:11:19 -0400 | [diff] [blame] | 2453 | sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS", |
| 2454 | &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]); |
| 2455 | } |
| 2456 | |
| 2457 | static inline void |
| 2458 | cifs_reclassify_socket6(struct socket *sock) |
| 2459 | { |
| 2460 | struct sock *sk = sock->sk; |
Hannes Frederic Sowa | fafc4e1 | 2016-04-08 15:11:27 +0200 | [diff] [blame] | 2461 | BUG_ON(!sock_allow_reclassification(sk)); |
Jeff Layton | 09e50d5 | 2008-07-23 10:11:19 -0400 | [diff] [blame] | 2462 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS", |
| 2463 | &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]); |
| 2464 | } |
| 2465 | #else |
| 2466 | static inline void |
| 2467 | cifs_reclassify_socket4(struct socket *sock) |
| 2468 | { |
| 2469 | } |
| 2470 | |
| 2471 | static inline void |
| 2472 | cifs_reclassify_socket6(struct socket *sock) |
| 2473 | { |
| 2474 | } |
| 2475 | #endif |
| 2476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | /* See RFC1001 section 14 on representation of Netbios names */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2478 | static void rfc1002mangle(char *target, char *source, unsigned int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2480 | unsigned int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2482 | for (i = 0, j = 0; i < (length); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | /* mask a nibble at a time and encode */ |
| 2484 | target[j] = 'A' + (0x0F & (source[i] >> 4)); |
| 2485 | target[j+1] = 'A' + (0x0F & source[i]); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2486 | j += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | } |
| 2490 | |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 2491 | static int |
| 2492 | bind_socket(struct TCP_Server_Info *server) |
| 2493 | { |
| 2494 | int rc = 0; |
| 2495 | if (server->srcaddr.ss_family != AF_UNSPEC) { |
| 2496 | /* Bind to the specified local IP address */ |
| 2497 | struct socket *socket = server->ssocket; |
| 2498 | rc = socket->ops->bind(socket, |
| 2499 | (struct sockaddr *) &server->srcaddr, |
| 2500 | sizeof(server->srcaddr)); |
| 2501 | if (rc < 0) { |
| 2502 | struct sockaddr_in *saddr4; |
| 2503 | struct sockaddr_in6 *saddr6; |
| 2504 | saddr4 = (struct sockaddr_in *)&server->srcaddr; |
| 2505 | saddr6 = (struct sockaddr_in6 *)&server->srcaddr; |
| 2506 | if (saddr6->sin6_family == AF_INET6) |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 2507 | cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n", |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2508 | &saddr6->sin6_addr, rc); |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 2509 | else |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 2510 | cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n", |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2511 | &saddr4->sin_addr.s_addr, rc); |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 2512 | } |
| 2513 | } |
| 2514 | return rc; |
| 2515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | |
| 2517 | static int |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2518 | ip_rfc1001_connect(struct TCP_Server_Info *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | { |
| 2520 | int rc = 0; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2521 | /* |
| 2522 | * some servers require RFC1001 sessinit before sending |
| 2523 | * negprot - BB check reconnection in case where second |
| 2524 | * sessinit is sent but no second negprot |
| 2525 | */ |
| 2526 | struct rfc1002_session_packet *ses_init_buf; |
| 2527 | struct smb_hdr *smb_buf; |
| 2528 | ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet), |
| 2529 | GFP_KERNEL); |
| 2530 | if (ses_init_buf) { |
| 2531 | ses_init_buf->trailer.session_req.called_len = 32; |
| 2532 | |
Colin Ian King | 997152f | 2016-01-25 16:25:54 +0000 | [diff] [blame] | 2533 | if (server->server_RFC1001_name[0] != 0) |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2534 | rfc1002mangle(ses_init_buf->trailer. |
| 2535 | session_req.called_name, |
| 2536 | server->server_RFC1001_name, |
| 2537 | RFC1001_NAME_LEN_WITH_NULL); |
| 2538 | else |
| 2539 | rfc1002mangle(ses_init_buf->trailer. |
| 2540 | session_req.called_name, |
| 2541 | DEFAULT_CIFS_CALLED_NAME, |
| 2542 | RFC1001_NAME_LEN_WITH_NULL); |
| 2543 | |
| 2544 | ses_init_buf->trailer.session_req.calling_len = 32; |
| 2545 | |
| 2546 | /* |
| 2547 | * calling name ends in null (byte 16) from old smb |
| 2548 | * convention. |
| 2549 | */ |
Steve French | c85c35f | 2015-03-27 01:15:02 -0500 | [diff] [blame] | 2550 | if (server->workstation_RFC1001_name[0] != 0) |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2551 | rfc1002mangle(ses_init_buf->trailer. |
| 2552 | session_req.calling_name, |
| 2553 | server->workstation_RFC1001_name, |
| 2554 | RFC1001_NAME_LEN_WITH_NULL); |
| 2555 | else |
| 2556 | rfc1002mangle(ses_init_buf->trailer. |
| 2557 | session_req.calling_name, |
| 2558 | "LINUX_CIFS_CLNT", |
| 2559 | RFC1001_NAME_LEN_WITH_NULL); |
| 2560 | |
| 2561 | ses_init_buf->trailer.session_req.scope1 = 0; |
| 2562 | ses_init_buf->trailer.session_req.scope2 = 0; |
| 2563 | smb_buf = (struct smb_hdr *)ses_init_buf; |
| 2564 | |
| 2565 | /* sizeof RFC1002_SESSION_REQUEST with no scope */ |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2566 | smb_buf->smb_buf_length = cpu_to_be32(0x81000044); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2567 | rc = smb_send(server, smb_buf, 0x44); |
| 2568 | kfree(ses_init_buf); |
| 2569 | /* |
| 2570 | * RFC1001 layer in at least one server |
| 2571 | * requires very short break before negprot |
| 2572 | * presumably because not expecting negprot |
| 2573 | * to follow so fast. This is a simple |
| 2574 | * solution that works without |
| 2575 | * complicating the code and causes no |
| 2576 | * significant slowing down on mount |
| 2577 | * for everyone else |
| 2578 | */ |
| 2579 | usleep_range(1000, 2000); |
| 2580 | } |
| 2581 | /* |
| 2582 | * else the negprot may still work without this |
| 2583 | * even though malloc failed |
| 2584 | */ |
| 2585 | |
| 2586 | return rc; |
| 2587 | } |
| 2588 | |
| 2589 | static int |
| 2590 | generic_ip_connect(struct TCP_Server_Info *server) |
| 2591 | { |
| 2592 | int rc = 0; |
Steve French | 6da9791 | 2011-03-13 18:55:55 +0000 | [diff] [blame] | 2593 | __be16 sport; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2594 | int slen, sfamily; |
Jeff Layton | bcf4b10 | 2008-12-01 18:42:15 -0500 | [diff] [blame] | 2595 | struct socket *socket = server->ssocket; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2596 | struct sockaddr *saddr; |
| 2597 | |
| 2598 | saddr = (struct sockaddr *) &server->dstaddr; |
| 2599 | |
| 2600 | if (server->dstaddr.ss_family == AF_INET6) { |
Samuel Cabrero | def6e1d | 2020-10-16 11:54:55 +0200 | [diff] [blame] | 2601 | struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 2602 | |
| 2603 | sport = ipv6->sin6_port; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2604 | slen = sizeof(struct sockaddr_in6); |
| 2605 | sfamily = AF_INET6; |
Samuel Cabrero | def6e1d | 2020-10-16 11:54:55 +0200 | [diff] [blame] | 2606 | cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr, |
| 2607 | ntohs(sport)); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2608 | } else { |
Samuel Cabrero | def6e1d | 2020-10-16 11:54:55 +0200 | [diff] [blame] | 2609 | struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr; |
| 2610 | |
| 2611 | sport = ipv4->sin_port; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2612 | slen = sizeof(struct sockaddr_in); |
| 2613 | sfamily = AF_INET; |
Samuel Cabrero | def6e1d | 2020-10-16 11:54:55 +0200 | [diff] [blame] | 2614 | cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr, |
| 2615 | ntohs(sport)); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2616 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | |
Jeff Layton | bcf4b10 | 2008-12-01 18:42:15 -0500 | [diff] [blame] | 2618 | if (socket == NULL) { |
Rob Landley | f1d0c99 | 2011-01-22 15:44:05 -0600 | [diff] [blame] | 2619 | rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM, |
| 2620 | IPPROTO_TCP, &socket, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | if (rc < 0) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 2622 | cifs_server_dbg(VFS, "Error %d creating socket\n", rc); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2623 | server->ssocket = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | } |
Jeff Layton | bcf4b10 | 2008-12-01 18:42:15 -0500 | [diff] [blame] | 2626 | |
| 2627 | /* BB other socket options to set KEEPALIVE, NODELAY? */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2628 | cifs_dbg(FYI, "Socket created\n"); |
Jeff Layton | bcf4b10 | 2008-12-01 18:42:15 -0500 | [diff] [blame] | 2629 | server->ssocket = socket; |
| 2630 | socket->sk->sk_allocation = GFP_NOFS; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2631 | if (sfamily == AF_INET6) |
| 2632 | cifs_reclassify_socket6(socket); |
| 2633 | else |
| 2634 | cifs_reclassify_socket4(socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 2637 | rc = bind_socket(server); |
| 2638 | if (rc < 0) |
| 2639 | return rc; |
| 2640 | |
Jeff Layton | d5c5605 | 2008-12-01 18:42:33 -0500 | [diff] [blame] | 2641 | /* |
| 2642 | * Eventually check for other socket options to change from |
| 2643 | * the default. sock_setsockopt not used because it expects |
| 2644 | * user space buffer |
| 2645 | */ |
| 2646 | socket->sk->sk_rcvtimeo = 7 * HZ; |
Steve French | da505c3 | 2009-01-19 03:49:35 +0000 | [diff] [blame] | 2647 | socket->sk->sk_sndtimeo = 5 * HZ; |
Steve French | 6a5fa236 | 2010-01-01 01:28:43 +0000 | [diff] [blame] | 2648 | |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2649 | /* make the bufsizes depend on wsize/rsize and max requests */ |
| 2650 | if (server->noautotune) { |
| 2651 | if (socket->sk->sk_sndbuf < (200 * 1024)) |
| 2652 | socket->sk->sk_sndbuf = 200 * 1024; |
| 2653 | if (socket->sk->sk_rcvbuf < (140 * 1024)) |
| 2654 | socket->sk->sk_rcvbuf = 140 * 1024; |
| 2655 | } |
| 2656 | |
Christoph Hellwig | 12abc5e | 2020-05-28 07:12:19 +0200 | [diff] [blame] | 2657 | if (server->tcp_nodelay) |
| 2658 | tcp_sock_set_nodelay(socket->sk); |
Steve French | 6a5fa236 | 2010-01-01 01:28:43 +0000 | [diff] [blame] | 2659 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2660 | cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n", |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2661 | socket->sk->sk_sndbuf, |
| 2662 | socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo); |
| 2663 | |
Paulo Alcantara (SUSE) | 8eecd1c | 2019-07-16 19:04:50 -0300 | [diff] [blame] | 2664 | rc = socket->ops->connect(socket, saddr, slen, |
| 2665 | server->noblockcnt ? O_NONBLOCK : 0); |
Paulo Alcantara (SUSE) | d532cc7 | 2019-10-10 12:31:58 -0300 | [diff] [blame] | 2666 | /* |
| 2667 | * When mounting SMB root file systems, we do not want to block in |
| 2668 | * connect. Otherwise bail out and then let cifs_reconnect() perform |
| 2669 | * reconnect failover - if possible. |
| 2670 | */ |
| 2671 | if (server->noblockcnt && rc == -EINPROGRESS) |
Paulo Alcantara (SUSE) | 8eecd1c | 2019-07-16 19:04:50 -0300 | [diff] [blame] | 2672 | rc = 0; |
Jeff Layton | ee1b3ea | 2011-06-21 07:18:26 -0400 | [diff] [blame] | 2673 | if (rc < 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2674 | cifs_dbg(FYI, "Error %d connecting to server\n", rc); |
Steve French | d7171cd | 2021-11-04 15:56:37 -0500 | [diff] [blame] | 2675 | trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc); |
Jeff Layton | ee1b3ea | 2011-06-21 07:18:26 -0400 | [diff] [blame] | 2676 | sock_release(socket); |
| 2677 | server->ssocket = NULL; |
| 2678 | return rc; |
| 2679 | } |
Steve French | d7171cd | 2021-11-04 15:56:37 -0500 | [diff] [blame] | 2680 | trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2681 | if (sport == htons(RFC1001_PORT)) |
| 2682 | rc = ip_rfc1001_connect(server); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | return rc; |
| 2685 | } |
| 2686 | |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2687 | static int |
| 2688 | ip_connect(struct TCP_Server_Info *server) |
| 2689 | { |
Steve French | 6da9791 | 2011-03-13 18:55:55 +0000 | [diff] [blame] | 2690 | __be16 *sport; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2691 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 2692 | struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; |
| 2693 | |
| 2694 | if (server->dstaddr.ss_family == AF_INET6) |
| 2695 | sport = &addr6->sin6_port; |
| 2696 | else |
| 2697 | sport = &addr->sin_port; |
| 2698 | |
| 2699 | if (*sport == 0) { |
| 2700 | int rc; |
| 2701 | |
| 2702 | /* try with 445 port at first */ |
| 2703 | *sport = htons(CIFS_PORT); |
| 2704 | |
| 2705 | rc = generic_ip_connect(server); |
| 2706 | if (rc >= 0) |
| 2707 | return rc; |
| 2708 | |
| 2709 | /* if it failed, try with 139 port */ |
| 2710 | *sport = htons(RFC1001_PORT); |
| 2711 | } |
| 2712 | |
| 2713 | return generic_ip_connect(server); |
| 2714 | } |
| 2715 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2716 | void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2717 | struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2718 | { |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 2719 | /* |
| 2720 | * If we are reconnecting then should we check to see if |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2721 | * any requested capabilities changed locally e.g. via |
| 2722 | * remount but we can not do much about it here |
| 2723 | * if they have (even if we could detect it by the following) |
| 2724 | * Perhaps we could add a backpointer to array of sb from tcon |
| 2725 | * or if we change to make all sb to same share the same |
| 2726 | * sb as NFS - then we only have one backpointer to sb. |
| 2727 | * What if we wanted to mount the server share twice once with |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 2728 | * and once without posixacls or posix paths? |
| 2729 | */ |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2730 | __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2731 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2732 | if (ctx && ctx->no_linux_ext) { |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 2733 | tcon->fsUnixInfo.Capability = 0; |
| 2734 | tcon->unix_ext = 0; /* Unix Extensions disabled */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2735 | cifs_dbg(FYI, "Linux protocol extensions disabled\n"); |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 2736 | return; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2737 | } else if (ctx) |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 2738 | tcon->unix_ext = 1; /* Unix Extensions supported */ |
| 2739 | |
Jiapeng Zhong | 16a7885 | 2021-01-14 18:02:23 +0800 | [diff] [blame] | 2740 | if (!tcon->unix_ext) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2741 | cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n"); |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 2742 | return; |
| 2743 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2744 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 2745 | if (!CIFSSMBQFSUnixInfo(xid, tcon)) { |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2746 | __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2747 | cifs_dbg(FYI, "unix caps which server supports %lld\n", cap); |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 2748 | /* |
| 2749 | * check for reconnect case in which we do not |
| 2750 | * want to change the mount behavior if we can avoid it |
| 2751 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2752 | if (ctx == NULL) { |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 2753 | /* |
| 2754 | * turn off POSIX ACL and PATHNAMES if not set |
| 2755 | * originally at mount time |
| 2756 | */ |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2757 | if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0) |
| 2758 | cap &= ~CIFS_UNIX_POSIX_ACL_CAP; |
Igor Mammedov | 11b6d64 | 2008-02-15 19:06:04 +0000 | [diff] [blame] | 2759 | if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) { |
| 2760 | if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2761 | cifs_dbg(VFS, "POSIXPATH support change\n"); |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2762 | cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP; |
Igor Mammedov | 11b6d64 | 2008-02-15 19:06:04 +0000 | [diff] [blame] | 2763 | } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2764 | cifs_dbg(VFS, "possible reconnect error\n"); |
| 2765 | cifs_dbg(VFS, "server disabled POSIX path support\n"); |
Igor Mammedov | 11b6d64 | 2008-02-15 19:06:04 +0000 | [diff] [blame] | 2766 | } |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2767 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2768 | |
Steve French | 6848b73 | 2011-05-26 18:38:54 +0000 | [diff] [blame] | 2769 | if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2770 | cifs_dbg(VFS, "per-share encryption not supported yet\n"); |
Steve French | 6848b73 | 2011-05-26 18:38:54 +0000 | [diff] [blame] | 2771 | |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2772 | cap &= CIFS_UNIX_CAP_MASK; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2773 | if (ctx && ctx->no_psx_acl) |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2774 | cap &= ~CIFS_UNIX_POSIX_ACL_CAP; |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2775 | else if (CIFS_UNIX_POSIX_ACL_CAP & cap) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2776 | cifs_dbg(FYI, "negotiated posix acl support\n"); |
Al Viro | 2c6292a | 2011-06-17 09:05:48 -0400 | [diff] [blame] | 2777 | if (cifs_sb) |
| 2778 | cifs_sb->mnt_cifs_flags |= |
| 2779 | CIFS_MOUNT_POSIXACL; |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2782 | if (ctx && ctx->posix_paths == 0) |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2783 | cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP; |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2784 | else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2785 | cifs_dbg(FYI, "negotiate posix pathnames\n"); |
Al Viro | 2c6292a | 2011-06-17 09:05:48 -0400 | [diff] [blame] | 2786 | if (cifs_sb) |
| 2787 | cifs_sb->mnt_cifs_flags |= |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2788 | CIFS_MOUNT_POSIX_PATHS; |
| 2789 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2790 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2791 | cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap); |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2792 | #ifdef CONFIG_CIFS_DEBUG2 |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2793 | if (cap & CIFS_UNIX_FCNTL_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2794 | cifs_dbg(FYI, "FCNTL cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2795 | if (cap & CIFS_UNIX_EXTATTR_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2796 | cifs_dbg(FYI, "EXTATTR cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2797 | if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2798 | cifs_dbg(FYI, "POSIX path cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2799 | if (cap & CIFS_UNIX_XATTR_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2800 | cifs_dbg(FYI, "XATTR cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2801 | if (cap & CIFS_UNIX_POSIX_ACL_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2802 | cifs_dbg(FYI, "POSIX ACL cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2803 | if (cap & CIFS_UNIX_LARGE_READ_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2804 | cifs_dbg(FYI, "very large read cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2805 | if (cap & CIFS_UNIX_LARGE_WRITE_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2806 | cifs_dbg(FYI, "very large write cap\n"); |
Steve French | 6848b73 | 2011-05-26 18:38:54 +0000 | [diff] [blame] | 2807 | if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2808 | cifs_dbg(FYI, "transport encryption cap\n"); |
Steve French | 6848b73 | 2011-05-26 18:38:54 +0000 | [diff] [blame] | 2809 | if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2810 | cifs_dbg(FYI, "mandatory transport encryption cap\n"); |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2811 | #endif /* CIFS_DEBUG2 */ |
| 2812 | if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2813 | if (ctx == NULL) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2814 | cifs_dbg(FYI, "resetting capabilities failed\n"); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2815 | else |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2816 | 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 French | 5a44b31 | 2007-09-20 15:16:24 +0000 | [diff] [blame] | 2817 | |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | } |
| 2821 | |
Ronnie Sahlberg | 51acd20 | 2020-12-14 16:40:24 +1000 | [diff] [blame] | 2822 | int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb) |
Jeff Layton | b1c8d2b | 2008-10-22 13:57:07 -0400 | [diff] [blame] | 2823 | { |
Ronnie Sahlberg | 51acd20 | 2020-12-14 16:40:24 +1000 | [diff] [blame] | 2824 | struct smb3_fs_context *ctx = cifs_sb->ctx; |
| 2825 | |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 2826 | INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks); |
| 2827 | |
Al Viro | 2ced6f6 | 2011-06-17 09:20:04 -0400 | [diff] [blame] | 2828 | spin_lock_init(&cifs_sb->tlink_tree_lock); |
| 2829 | cifs_sb->tlink_tree = RB_ROOT; |
| 2830 | |
Frank Sorenson | f52aa79 | 2020-02-12 15:31:48 -0600 | [diff] [blame] | 2831 | cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n", |
Ronnie Sahlberg | 51acd20 | 2020-12-14 16:40:24 +1000 | [diff] [blame] | 2832 | ctx->file_mode, ctx->dir_mode); |
Steve French | 3b79521 | 2008-11-13 19:45:32 +0000 | [diff] [blame] | 2833 | |
Ronnie Sahlberg | 387ec58 | 2020-12-14 16:40:20 +1000 | [diff] [blame] | 2834 | /* this is needed for ASCII cp to Unicode converts */ |
| 2835 | if (ctx->iocharset == NULL) { |
| 2836 | /* load_nls_default cannot return null */ |
| 2837 | cifs_sb->local_nls = load_nls_default(); |
| 2838 | } else { |
| 2839 | cifs_sb->local_nls = load_nls(ctx->iocharset); |
| 2840 | if (cifs_sb->local_nls == NULL) { |
| 2841 | cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n", |
| 2842 | ctx->iocharset); |
| 2843 | return -ELIBACC; |
| 2844 | } |
| 2845 | } |
| 2846 | ctx->local_nls = cifs_sb->local_nls; |
Suresh Jayaraman | 6d20e84 | 2010-12-01 14:42:28 +0530 | [diff] [blame] | 2847 | |
Ronnie Sahlberg | 2d39f50 | 2020-12-14 16:40:25 +1000 | [diff] [blame] | 2848 | smb3_update_mnt_flags(cifs_sb); |
| 2849 | |
| 2850 | if (ctx->direct_io) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2851 | cifs_dbg(FYI, "mounting share using direct i/o\n"); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2852 | if (ctx->cache_ro) { |
Steve French | 83bbfa7 | 2019-08-27 23:58:54 -0500 | [diff] [blame] | 2853 | cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n"); |
| 2854 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2855 | } else if (ctx->cache_rw) { |
Steve French | 41e033f | 2019-08-30 02:12:41 -0500 | [diff] [blame] | 2856 | cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n"); |
| 2857 | cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE | |
| 2858 | CIFS_MOUNT_RW_CACHE); |
Steve French | 83bbfa7 | 2019-08-27 23:58:54 -0500 | [diff] [blame] | 2859 | } |
Steve French | 3b79521 | 2008-11-13 19:45:32 +0000 | [diff] [blame] | 2860 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2861 | if ((ctx->cifs_acl) && (ctx->dynperm)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2862 | cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n"); |
Sachin Prabhu | 4214ebf | 2016-07-29 22:38:19 +0100 | [diff] [blame] | 2863 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2864 | if (ctx->prepath) { |
| 2865 | cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL); |
Sachin Prabhu | 4214ebf | 2016-07-29 22:38:19 +0100 | [diff] [blame] | 2866 | if (cifs_sb->prepath == NULL) |
| 2867 | return -ENOMEM; |
Shyam Prasad N | a738c93 | 2021-02-11 03:26:54 -0800 | [diff] [blame] | 2868 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; |
Sachin Prabhu | 4214ebf | 2016-07-29 22:38:19 +0100 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | return 0; |
Jeff Layton | b1c8d2b | 2008-10-22 13:57:07 -0400 | [diff] [blame] | 2872 | } |
| 2873 | |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2874 | /* Release all succeed connections */ |
| 2875 | static inline void mount_put_conns(struct cifs_sb_info *cifs_sb, |
| 2876 | unsigned int xid, |
| 2877 | struct TCP_Server_Info *server, |
| 2878 | struct cifs_ses *ses, struct cifs_tcon *tcon) |
| 2879 | { |
| 2880 | int rc = 0; |
| 2881 | |
| 2882 | if (tcon) |
| 2883 | cifs_put_tcon(tcon); |
| 2884 | else if (ses) |
| 2885 | cifs_put_smb_ses(ses); |
| 2886 | else if (server) |
| 2887 | cifs_put_tcp_session(server, 0); |
| 2888 | cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS; |
| 2889 | free_xid(xid); |
| 2890 | } |
| 2891 | |
| 2892 | /* Get connections for tcp, ses and tcon */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2893 | static int mount_get_conns(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb, |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2894 | unsigned int *xid, |
| 2895 | struct TCP_Server_Info **nserver, |
| 2896 | struct cifs_ses **nses, struct cifs_tcon **ntcon) |
| 2897 | { |
| 2898 | int rc = 0; |
| 2899 | struct TCP_Server_Info *server; |
| 2900 | struct cifs_ses *ses; |
| 2901 | struct cifs_tcon *tcon; |
| 2902 | |
| 2903 | *nserver = NULL; |
| 2904 | *nses = NULL; |
| 2905 | *ntcon = NULL; |
| 2906 | |
| 2907 | *xid = get_xid(); |
| 2908 | |
| 2909 | /* get a reference to a tcp session */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2910 | server = cifs_get_tcp_session(ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2911 | if (IS_ERR(server)) { |
| 2912 | rc = PTR_ERR(server); |
| 2913 | return rc; |
| 2914 | } |
| 2915 | |
| 2916 | *nserver = server; |
| 2917 | |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2918 | /* get a reference to a SMB session */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2919 | ses = cifs_get_smb_ses(server, ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2920 | if (IS_ERR(ses)) { |
| 2921 | rc = PTR_ERR(ses); |
| 2922 | return rc; |
| 2923 | } |
| 2924 | |
| 2925 | *nses = ses; |
| 2926 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2927 | if ((ctx->persistent == true) && (!(ses->server->capabilities & |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2928 | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 2929 | cifs_server_dbg(VFS, "persistent handles not supported by server\n"); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2930 | return -EOPNOTSUPP; |
| 2931 | } |
| 2932 | |
| 2933 | /* search for existing tcon to this server share */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2934 | tcon = cifs_get_tcon(ses, ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2935 | if (IS_ERR(tcon)) { |
| 2936 | rc = PTR_ERR(tcon); |
| 2937 | return rc; |
| 2938 | } |
| 2939 | |
| 2940 | *ntcon = tcon; |
| 2941 | |
| 2942 | /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */ |
| 2943 | if (tcon->posix_extensions) |
| 2944 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS; |
| 2945 | |
| 2946 | /* tell server which Unix caps we support */ |
| 2947 | if (cap_unix(tcon->ses)) { |
| 2948 | /* |
| 2949 | * reset of caps checks mount to see if unix extensions disabled |
| 2950 | * for just this mount. |
| 2951 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2952 | reset_cifs_unix_caps(*xid, tcon, cifs_sb, ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2953 | if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) && |
| 2954 | (le64_to_cpu(tcon->fsUnixInfo.Capability) & |
| 2955 | CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) |
| 2956 | return -EACCES; |
| 2957 | } else |
| 2958 | tcon->unix_ext = 0; /* server does not support them */ |
| 2959 | |
| 2960 | /* do not care if a following call succeed - informational */ |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 2961 | if (!tcon->pipe && server->ops->qfs_tcon) { |
Amir Goldstein | 0f06093 | 2020-02-03 21:46:43 +0200 | [diff] [blame] | 2962 | server->ops->qfs_tcon(*xid, tcon, cifs_sb); |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 2963 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) { |
| 2964 | if (tcon->fsDevInfo.DeviceCharacteristics & |
Steve French | 52870d5 | 2019-10-01 21:25:46 -0500 | [diff] [blame] | 2965 | cpu_to_le32(FILE_READ_ONLY_DEVICE)) |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 2966 | cifs_dbg(VFS, "mounted to read only share\n"); |
Steve French | 41e033f | 2019-08-30 02:12:41 -0500 | [diff] [blame] | 2967 | else if ((cifs_sb->mnt_cifs_flags & |
| 2968 | CIFS_MOUNT_RW_CACHE) == 0) |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 2969 | cifs_dbg(VFS, "read only mount of RW share\n"); |
Steve French | 41e033f | 2019-08-30 02:12:41 -0500 | [diff] [blame] | 2970 | /* no need to log a RW mount of a typical RW share */ |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 2971 | } |
| 2972 | } |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2973 | |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 2974 | /* |
| 2975 | * Clamp the rsize/wsize mount arguments if they are too big for the server |
Steve French | 0c2b5f7 | 2020-12-15 13:28:50 -0600 | [diff] [blame] | 2976 | * and set the rsize/wsize to the negotiated values if not passed in by |
| 2977 | * the user on mount |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 2978 | */ |
Steve French | 0c2b5f7 | 2020-12-15 13:28:50 -0600 | [diff] [blame] | 2979 | if ((cifs_sb->ctx->wsize == 0) || |
| 2980 | (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx))) |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 2981 | cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx); |
Steve French | 0c2b5f7 | 2020-12-15 13:28:50 -0600 | [diff] [blame] | 2982 | if ((cifs_sb->ctx->rsize == 0) || |
| 2983 | (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx))) |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 2984 | cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 2985 | |
| 2986 | return 0; |
| 2987 | } |
| 2988 | |
| 2989 | static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses, |
| 2990 | struct cifs_tcon *tcon) |
| 2991 | { |
| 2992 | struct tcon_link *tlink; |
| 2993 | |
| 2994 | /* hang the tcon off of the superblock */ |
| 2995 | tlink = kzalloc(sizeof(*tlink), GFP_KERNEL); |
| 2996 | if (tlink == NULL) |
| 2997 | return -ENOMEM; |
| 2998 | |
| 2999 | tlink->tl_uid = ses->linux_uid; |
| 3000 | tlink->tl_tcon = tcon; |
| 3001 | tlink->tl_time = jiffies; |
| 3002 | set_bit(TCON_LINK_MASTER, &tlink->tl_flags); |
| 3003 | set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); |
| 3004 | |
| 3005 | cifs_sb->master_tlink = tlink; |
| 3006 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 3007 | tlink_rb_insert(&cifs_sb->tlink_tree, tlink); |
| 3008 | spin_unlock(&cifs_sb->tlink_tree_lock); |
| 3009 | |
| 3010 | queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, |
| 3011 | TLINK_IDLE_EXPIRE); |
| 3012 | return 0; |
| 3013 | } |
Jeff Layton | b9bce2e | 2011-07-06 08:10:39 -0400 | [diff] [blame] | 3014 | |
Steve French | 2d6d589 | 2009-04-09 00:36:44 +0000 | [diff] [blame] | 3015 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Paulo Alcantara | cdc3363 | 2021-07-15 21:53:53 -0300 | [diff] [blame] | 3016 | static int mount_get_dfs_conns(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb, |
| 3017 | unsigned int *xid, struct TCP_Server_Info **nserver, |
| 3018 | struct cifs_ses **nses, struct cifs_tcon **ntcon) |
| 3019 | { |
| 3020 | int rc; |
| 3021 | |
| 3022 | ctx->nosharesock = true; |
| 3023 | rc = mount_get_conns(ctx, cifs_sb, xid, nserver, nses, ntcon); |
| 3024 | if (*nserver) { |
| 3025 | cifs_dbg(FYI, "%s: marking tcp session as a dfs connection\n", __func__); |
| 3026 | spin_lock(&cifs_tcp_ses_lock); |
| 3027 | (*nserver)->is_dfs_conn = true; |
| 3028 | spin_unlock(&cifs_tcp_ses_lock); |
| 3029 | } |
| 3030 | return rc; |
| 3031 | } |
| 3032 | |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 3033 | /* |
| 3034 | * cifs_build_path_to_root returns full path to root when we do not have an |
Steve French | 201023c | 2021-02-15 11:03:45 -0600 | [diff] [blame] | 3035 | * existing connection (tcon) |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 3036 | */ |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3037 | static char * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3038 | build_unc_path_to_root(const struct smb3_fs_context *ctx, |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3039 | const struct cifs_sb_info *cifs_sb, bool useppath) |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3040 | { |
Jeff Layton | b2a0fa1 | 2011-07-06 08:10:36 -0400 | [diff] [blame] | 3041 | char *full_path, *pos; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3042 | unsigned int pplen = useppath && ctx->prepath ? |
| 3043 | strlen(ctx->prepath) + 1 : 0; |
| 3044 | unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1); |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3045 | |
Ronnie Sahlberg | 340625e | 2019-08-27 09:30:14 +1000 | [diff] [blame] | 3046 | if (unc_len > MAX_TREE_SIZE) |
| 3047 | return ERR_PTR(-EINVAL); |
| 3048 | |
Jeff Layton | b2a0fa1 | 2011-07-06 08:10:36 -0400 | [diff] [blame] | 3049 | full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL); |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3050 | if (full_path == NULL) |
| 3051 | return ERR_PTR(-ENOMEM); |
| 3052 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3053 | memcpy(full_path, ctx->UNC, unc_len); |
Jeff Layton | b2a0fa1 | 2011-07-06 08:10:36 -0400 | [diff] [blame] | 3054 | pos = full_path + unc_len; |
| 3055 | |
| 3056 | if (pplen) { |
Jeff Layton | 1fc29ba | 2013-05-31 10:00:18 -0400 | [diff] [blame] | 3057 | *pos = CIFS_DIR_SEP(cifs_sb); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3058 | memcpy(pos + 1, ctx->prepath, pplen); |
Jeff Layton | b2a0fa1 | 2011-07-06 08:10:36 -0400 | [diff] [blame] | 3059 | pos += pplen; |
| 3060 | } |
| 3061 | |
| 3062 | *pos = '\0'; /* add trailing null */ |
Steve French | f87d39d | 2011-05-27 03:50:55 +0000 | [diff] [blame] | 3063 | convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb)); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3064 | cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path); |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3065 | return full_path; |
| 3066 | } |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3067 | |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 3068 | /* |
Paulo Alcantara | 1c78022 | 2018-11-14 16:24:03 -0200 | [diff] [blame] | 3069 | * expand_dfs_referral - Perform a dfs referral query and update the cifs_sb |
| 3070 | * |
Ronnie Sahlberg | a2a52a8 | 2020-11-10 09:12:31 +1000 | [diff] [blame] | 3071 | * If a referral is found, cifs_sb->ctx->mount_options will be (re-)allocated |
Sean Finney | 046462a | 2011-04-11 13:19:33 +0000 | [diff] [blame] | 3072 | * to a string containing updated options for the submount. Otherwise it |
| 3073 | * will be left untouched. |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3074 | * |
| 3075 | * Returns the rc from get_dfs_path to the caller, which can be used to |
| 3076 | * determine whether there were referrals. |
| 3077 | */ |
| 3078 | static int |
Pavel Shilovsky | b669f33 | 2012-05-27 20:21:53 +0400 | [diff] [blame] | 3079 | expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3080 | struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb, |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3081 | char *ref_path) |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3082 | { |
| 3083 | int rc; |
Paulo Alcantara | 1c78022 | 2018-11-14 16:24:03 -0200 | [diff] [blame] | 3084 | struct dfs_info3_param referral = {0}; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3085 | char *full_path = NULL, *mdata = NULL; |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3086 | |
Aurelien Aptel | 8393072 | 2018-09-20 18:10:25 -0700 | [diff] [blame] | 3087 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) |
| 3088 | return -EREMOTE; |
| 3089 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3090 | full_path = build_unc_path_to_root(ctx, cifs_sb, true); |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3091 | if (IS_ERR(full_path)) |
| 3092 | return PTR_ERR(full_path); |
| 3093 | |
Paulo Alcantara | 1c78022 | 2018-11-14 16:24:03 -0200 | [diff] [blame] | 3094 | rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb), |
| 3095 | ref_path, &referral, NULL); |
| 3096 | if (!rc) { |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3097 | char *fake_devname = NULL; |
| 3098 | |
Ronnie Sahlberg | a2a52a8 | 2020-11-10 09:12:31 +1000 | [diff] [blame] | 3099 | mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3100 | full_path + 1, &referral, |
| 3101 | &fake_devname); |
Paulo Alcantara | 1c78022 | 2018-11-14 16:24:03 -0200 | [diff] [blame] | 3102 | free_dfs_info_param(&referral); |
Sean Finney | 046462a | 2011-04-11 13:19:33 +0000 | [diff] [blame] | 3103 | |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3104 | if (IS_ERR(mdata)) { |
| 3105 | rc = PTR_ERR(mdata); |
| 3106 | mdata = NULL; |
Jeff Layton | b9bce2e | 2011-07-06 08:10:39 -0400 | [diff] [blame] | 3107 | } else { |
Ronnie Sahlberg | abd4af4 | 2021-02-08 16:48:31 +1000 | [diff] [blame] | 3108 | /* |
| 3109 | * We can not clear out the whole structure since we |
| 3110 | * no longer have an explicit function to parse |
| 3111 | * a mount-string. Instead we need to clear out the |
| 3112 | * individual fields that are no longer valid. |
| 3113 | */ |
| 3114 | kfree(ctx->prepath); |
| 3115 | ctx->prepath = NULL; |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3116 | rc = cifs_setup_volume_info(ctx, mdata, fake_devname); |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3117 | } |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3118 | kfree(fake_devname); |
Ronnie Sahlberg | a2a52a8 | 2020-11-10 09:12:31 +1000 | [diff] [blame] | 3119 | kfree(cifs_sb->ctx->mount_options); |
| 3120 | cifs_sb->ctx->mount_options = mdata; |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3121 | } |
| 3122 | kfree(full_path); |
| 3123 | return rc; |
| 3124 | } |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3125 | |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3126 | static int get_next_dfs_tgt(struct dfs_cache_tgt_list *tgt_list, |
| 3127 | struct dfs_cache_tgt_iterator **tgt_it) |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3128 | { |
| 3129 | if (!*tgt_it) |
| 3130 | *tgt_it = dfs_cache_get_tgt_iterator(tgt_list); |
| 3131 | else |
| 3132 | *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it); |
| 3133 | return !*tgt_it ? -EHOSTDOWN : 0; |
| 3134 | } |
| 3135 | |
| 3136 | static int update_vol_info(const struct dfs_cache_tgt_iterator *tgt_it, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3137 | struct smb3_fs_context *fake_ctx, struct smb3_fs_context *ctx) |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3138 | { |
| 3139 | const char *tgt = dfs_cache_get_tgt_name(tgt_it); |
| 3140 | int len = strlen(tgt) + 2; |
| 3141 | char *new_unc; |
| 3142 | |
| 3143 | new_unc = kmalloc(len, GFP_KERNEL); |
| 3144 | if (!new_unc) |
| 3145 | return -ENOMEM; |
Ronnie Sahlberg | 74ea5f9 | 2019-02-09 09:51:11 +1000 | [diff] [blame] | 3146 | scnprintf(new_unc, len, "\\%s", tgt); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3147 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3148 | kfree(ctx->UNC); |
| 3149 | ctx->UNC = new_unc; |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3150 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3151 | if (fake_ctx->prepath) { |
| 3152 | kfree(ctx->prepath); |
| 3153 | ctx->prepath = fake_ctx->prepath; |
| 3154 | fake_ctx->prepath = NULL; |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3155 | } |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3156 | memcpy(&ctx->dstaddr, &fake_ctx->dstaddr, sizeof(ctx->dstaddr)); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3157 | |
| 3158 | return 0; |
| 3159 | } |
| 3160 | |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3161 | static int do_dfs_failover(const char *path, const char *full_path, struct cifs_sb_info *cifs_sb, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3162 | struct smb3_fs_context *ctx, struct cifs_ses *root_ses, |
| 3163 | unsigned int *xid, struct TCP_Server_Info **server, |
| 3164 | struct cifs_ses **ses, struct cifs_tcon **tcon) |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3165 | { |
| 3166 | int rc; |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3167 | char *npath = NULL; |
Paulo Alcantara | b623661 | 2021-07-16 03:26:41 -0300 | [diff] [blame] | 3168 | struct dfs_cache_tgt_list tgt_list = DFS_CACHE_TGT_LIST_INIT(tgt_list); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3169 | struct dfs_cache_tgt_iterator *tgt_it = NULL; |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3170 | struct smb3_fs_context tmp_ctx = {NULL}; |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3171 | |
| 3172 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) |
| 3173 | return -EOPNOTSUPP; |
| 3174 | |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3175 | npath = dfs_cache_canonical_path(path, cifs_sb->local_nls, cifs_remap(cifs_sb)); |
| 3176 | if (IS_ERR(npath)) |
| 3177 | return PTR_ERR(npath); |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3178 | |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3179 | cifs_dbg(FYI, "%s: path=%s full_path=%s\n", __func__, npath, full_path); |
| 3180 | |
| 3181 | rc = dfs_cache_noreq_find(npath, NULL, &tgt_list); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3182 | if (rc) |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3183 | goto out; |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3184 | /* |
| 3185 | * We use a 'tmp_ctx' here because we need pass it down to the mount_{get,put} functions to |
| 3186 | * test connection against new DFS targets. |
| 3187 | */ |
| 3188 | rc = smb3_fs_context_dup(&tmp_ctx, ctx); |
| 3189 | if (rc) |
| 3190 | goto out; |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3191 | |
| 3192 | for (;;) { |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3193 | struct dfs_info3_param ref = {0}; |
| 3194 | char *fake_devname = NULL, *mdata = NULL; |
| 3195 | |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3196 | /* Get next DFS target server - if any */ |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3197 | rc = get_next_dfs_tgt(&tgt_list, &tgt_it); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3198 | if (rc) |
| 3199 | break; |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3200 | |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3201 | rc = dfs_cache_get_tgt_referral(npath, tgt_it, &ref); |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3202 | if (rc) |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3203 | break; |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3204 | |
| 3205 | cifs_dbg(FYI, "%s: old ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC, |
| 3206 | tmp_ctx.prepath); |
| 3207 | |
| 3208 | mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, &ref, |
| 3209 | &fake_devname); |
| 3210 | free_dfs_info_param(&ref); |
| 3211 | |
| 3212 | if (IS_ERR(mdata)) { |
| 3213 | rc = PTR_ERR(mdata); |
| 3214 | mdata = NULL; |
| 3215 | } else |
| 3216 | rc = cifs_setup_volume_info(&tmp_ctx, mdata, fake_devname); |
| 3217 | |
| 3218 | kfree(mdata); |
| 3219 | kfree(fake_devname); |
| 3220 | |
| 3221 | if (rc) |
| 3222 | break; |
| 3223 | |
| 3224 | cifs_dbg(FYI, "%s: new ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC, |
| 3225 | tmp_ctx.prepath); |
| 3226 | |
| 3227 | mount_put_conns(cifs_sb, *xid, *server, *ses, *tcon); |
Paulo Alcantara | cdc3363 | 2021-07-15 21:53:53 -0300 | [diff] [blame] | 3228 | rc = mount_get_dfs_conns(&tmp_ctx, cifs_sb, xid, server, ses, tcon); |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3229 | if (!rc || (*server && *ses)) { |
| 3230 | /* |
| 3231 | * We were able to connect to new target server. Update current context with |
| 3232 | * new target server. |
| 3233 | */ |
| 3234 | rc = update_vol_info(tgt_it, &tmp_ctx, ctx); |
| 3235 | break; |
| 3236 | } |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3237 | } |
| 3238 | if (!rc) { |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3239 | cifs_dbg(FYI, "%s: final ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC, |
| 3240 | tmp_ctx.prepath); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3241 | /* |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3242 | * Update DFS target hint in DFS referral cache with the target server we |
| 3243 | * successfully reconnected to. |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3244 | */ |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3245 | rc = dfs_cache_update_tgthint(*xid, root_ses ? root_ses : *ses, cifs_sb->local_nls, |
| 3246 | cifs_remap(cifs_sb), path, tgt_it); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3247 | } |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3248 | |
| 3249 | out: |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3250 | kfree(npath); |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3251 | smb3_cleanup_fs_context_contents(&tmp_ctx); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3252 | dfs_cache_free_tgts(&tgt_list); |
| 3253 | return rc; |
| 3254 | } |
Steve French | 2d6d589 | 2009-04-09 00:36:44 +0000 | [diff] [blame] | 3255 | #endif |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3256 | |
Ronnie Sahlberg | 24e0a1e | 2020-12-10 00:06:02 -0600 | [diff] [blame] | 3257 | /* TODO: all callers to this are broken. We are not parsing mount_options here |
| 3258 | * we should pass a clone of the original context? |
| 3259 | */ |
Paulo Alcantara (SUSE) | 5072010 | 2019-03-19 16:54:29 -0300 | [diff] [blame] | 3260 | int |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3261 | cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | { |
Paulo Alcantara | 5c1acf3 | 2021-05-03 11:55:26 -0300 | [diff] [blame] | 3263 | int rc; |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3264 | |
Paulo Alcantara | 5c1acf3 | 2021-05-03 11:55:26 -0300 | [diff] [blame] | 3265 | if (devname) { |
| 3266 | cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname); |
| 3267 | rc = smb3_parse_devname(devname, ctx); |
| 3268 | if (rc) { |
| 3269 | cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc); |
| 3270 | return rc; |
| 3271 | } |
| 3272 | } |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3273 | |
| 3274 | if (mntopts) { |
| 3275 | char *ip; |
| 3276 | |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3277 | rc = smb3_parse_opt(mntopts, "ip", &ip); |
Paulo Alcantara | 5c1acf3 | 2021-05-03 11:55:26 -0300 | [diff] [blame] | 3278 | if (rc) { |
| 3279 | cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc); |
| 3280 | return rc; |
| 3281 | } |
| 3282 | |
| 3283 | rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip)); |
| 3284 | kfree(ip); |
| 3285 | if (!rc) { |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3286 | cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__); |
| 3287 | return -EINVAL; |
| 3288 | } |
| 3289 | } |
| 3290 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3291 | if (ctx->nullauth) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3292 | cifs_dbg(FYI, "Anonymous login\n"); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3293 | kfree(ctx->username); |
| 3294 | ctx->username = NULL; |
| 3295 | } else if (ctx->username) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | /* BB fixme parse for domain name here */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3297 | cifs_dbg(FYI, "Username: %s\n", ctx->username); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | } else { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3299 | cifs_dbg(VFS, "No username specified\n"); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3300 | /* In userspace mount helper we can get user name from alternate |
| 3301 | locations such as env variables and files on disk */ |
Jeff Layton | 04db79b | 2011-07-06 08:10:38 -0400 | [diff] [blame] | 3302 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | } |
| 3304 | |
Paulo Alcantara | 5c1acf3 | 2021-05-03 11:55:26 -0300 | [diff] [blame] | 3305 | return 0; |
Jeff Layton | 04db79b | 2011-07-06 08:10:38 -0400 | [diff] [blame] | 3306 | } |
| 3307 | |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3308 | static int |
| 3309 | cifs_are_all_path_components_accessible(struct TCP_Server_Info *server, |
| 3310 | unsigned int xid, |
| 3311 | struct cifs_tcon *tcon, |
| 3312 | struct cifs_sb_info *cifs_sb, |
Ronnie Sahlberg | ce465bf | 2019-07-11 13:46:58 +1000 | [diff] [blame] | 3313 | char *full_path, |
| 3314 | int added_treename) |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3315 | { |
| 3316 | int rc; |
| 3317 | char *s; |
| 3318 | char sep, tmp; |
Ronnie Sahlberg | ce465bf | 2019-07-11 13:46:58 +1000 | [diff] [blame] | 3319 | int skip = added_treename ? 1 : 0; |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3320 | |
| 3321 | sep = CIFS_DIR_SEP(cifs_sb); |
| 3322 | s = full_path; |
| 3323 | |
| 3324 | rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, ""); |
| 3325 | while (rc == 0) { |
| 3326 | /* skip separators */ |
| 3327 | while (*s == sep) |
| 3328 | s++; |
| 3329 | if (!*s) |
| 3330 | break; |
| 3331 | /* next separator */ |
| 3332 | while (*s && *s != sep) |
| 3333 | s++; |
Ronnie Sahlberg | ce465bf | 2019-07-11 13:46:58 +1000 | [diff] [blame] | 3334 | /* |
| 3335 | * if the treename is added, we then have to skip the first |
| 3336 | * part within the separators |
| 3337 | */ |
| 3338 | if (skip) { |
| 3339 | skip = 0; |
| 3340 | continue; |
| 3341 | } |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3342 | /* |
| 3343 | * temporarily null-terminate the path at the end of |
| 3344 | * the current component |
| 3345 | */ |
| 3346 | tmp = *s; |
| 3347 | *s = 0; |
| 3348 | rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, |
| 3349 | full_path); |
| 3350 | *s = tmp; |
| 3351 | } |
| 3352 | return rc; |
| 3353 | } |
| 3354 | |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3355 | /* |
| 3356 | * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is, |
| 3357 | * otherwise 0. |
| 3358 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3359 | static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx, |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3360 | const unsigned int xid, |
| 3361 | struct TCP_Server_Info *server, |
| 3362 | struct cifs_tcon *tcon) |
Pavel Shilovsky | 724d9f1 | 2011-05-05 09:55:12 +0000 | [diff] [blame] | 3363 | { |
Jeff Layton | 1daaae8 | 2012-03-21 06:30:40 -0400 | [diff] [blame] | 3364 | int rc; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3365 | char *full_path; |
| 3366 | |
| 3367 | if (!server->ops->is_path_accessible) |
| 3368 | return -EOPNOTSUPP; |
| 3369 | |
| 3370 | /* |
| 3371 | * cifs_build_path_to_root works only when we have a valid tcon |
| 3372 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3373 | full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon, |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3374 | tcon->Flags & SMB_SHARE_IS_IN_DFS); |
| 3375 | if (full_path == NULL) |
| 3376 | return -ENOMEM; |
| 3377 | |
| 3378 | cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path); |
| 3379 | |
| 3380 | rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, |
| 3381 | full_path); |
| 3382 | if (rc != 0 && rc != -EREMOTE) { |
| 3383 | kfree(full_path); |
| 3384 | return rc; |
| 3385 | } |
| 3386 | |
| 3387 | if (rc != -EREMOTE) { |
| 3388 | rc = cifs_are_all_path_components_accessible(server, xid, tcon, |
Ronnie Sahlberg | ce465bf | 2019-07-11 13:46:58 +1000 | [diff] [blame] | 3389 | cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3390 | if (rc != 0) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 3391 | cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n"); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3392 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; |
| 3393 | rc = 0; |
| 3394 | } |
| 3395 | } |
| 3396 | |
| 3397 | kfree(full_path); |
| 3398 | return rc; |
| 3399 | } |
| 3400 | |
| 3401 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3402 | static void set_root_ses(struct cifs_sb_info *cifs_sb, const uuid_t *mount_id, struct cifs_ses *ses, |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3403 | struct cifs_ses **root_ses) |
Paulo Alcantara (SUSE) | 5bb30a4 | 2019-11-22 12:30:56 -0300 | [diff] [blame] | 3404 | { |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3405 | if (ses) { |
| 3406 | spin_lock(&cifs_tcp_ses_lock); |
| 3407 | ses->ses_count++; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3408 | spin_unlock(&cifs_tcp_ses_lock); |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3409 | dfs_cache_add_refsrv_session(mount_id, ses); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3410 | } |
| 3411 | *root_ses = ses; |
| 3412 | } |
| 3413 | |
Paulo Alcantara | ff2c54a | 2021-02-24 20:59:22 -0300 | [diff] [blame] | 3414 | /* Set up next dfs prefix path in @dfs_path */ |
| 3415 | static int next_dfs_prepath(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx, |
| 3416 | const unsigned int xid, struct TCP_Server_Info *server, |
| 3417 | struct cifs_tcon *tcon, char **dfs_path) |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3418 | { |
Paulo Alcantara | ff2c54a | 2021-02-24 20:59:22 -0300 | [diff] [blame] | 3419 | char *path, *npath; |
| 3420 | int added_treename = is_tcon_dfs(tcon); |
| 3421 | int rc; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3422 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3423 | path = cifs_build_path_to_root(ctx, cifs_sb, tcon, added_treename); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3424 | if (!path) |
| 3425 | return -ENOMEM; |
| 3426 | |
Paulo Alcantara | ff2c54a | 2021-02-24 20:59:22 -0300 | [diff] [blame] | 3427 | rc = is_path_remote(cifs_sb, ctx, xid, server, tcon); |
| 3428 | if (rc == -EREMOTE) { |
| 3429 | struct smb3_fs_context v = {NULL}; |
| 3430 | /* if @path contains a tree name, skip it in the prefix path */ |
| 3431 | if (added_treename) { |
| 3432 | rc = smb3_parse_devname(path, &v); |
| 3433 | if (rc) |
| 3434 | goto out; |
| 3435 | npath = build_unc_path_to_root(&v, cifs_sb, true); |
| 3436 | smb3_cleanup_fs_context_contents(&v); |
| 3437 | } else { |
| 3438 | v.UNC = ctx->UNC; |
| 3439 | v.prepath = path + 1; |
| 3440 | npath = build_unc_path_to_root(&v, cifs_sb, true); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3441 | } |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3442 | |
Paulo Alcantara | ff2c54a | 2021-02-24 20:59:22 -0300 | [diff] [blame] | 3443 | if (IS_ERR(npath)) { |
| 3444 | rc = PTR_ERR(npath); |
| 3445 | goto out; |
| 3446 | } |
| 3447 | |
| 3448 | kfree(*dfs_path); |
| 3449 | *dfs_path = npath; |
| 3450 | rc = -EREMOTE; |
| 3451 | } |
| 3452 | |
| 3453 | out: |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3454 | kfree(path); |
| 3455 | return rc; |
Paulo Alcantara (SUSE) | 5bb30a4 | 2019-11-22 12:30:56 -0300 | [diff] [blame] | 3456 | } |
| 3457 | |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3458 | /* Check if resolved targets can handle any DFS referrals */ |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3459 | static int is_referral_server(const char *ref_path, struct cifs_sb_info *cifs_sb, |
| 3460 | struct cifs_tcon *tcon, bool *ref_server) |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3461 | { |
| 3462 | int rc; |
| 3463 | struct dfs_info3_param ref = {0}; |
| 3464 | |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3465 | cifs_dbg(FYI, "%s: ref_path=%s\n", __func__, ref_path); |
| 3466 | |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3467 | if (is_tcon_dfs(tcon)) { |
| 3468 | *ref_server = true; |
| 3469 | } else { |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3470 | char *npath; |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3471 | |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3472 | npath = dfs_cache_canonical_path(ref_path, cifs_sb->local_nls, cifs_remap(cifs_sb)); |
| 3473 | if (IS_ERR(npath)) |
| 3474 | return PTR_ERR(npath); |
| 3475 | |
| 3476 | rc = dfs_cache_noreq_find(npath, &ref, NULL); |
| 3477 | kfree(npath); |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3478 | if (rc) { |
| 3479 | cifs_dbg(VFS, "%s: dfs_cache_noreq_find: failed (rc=%d)\n", __func__, rc); |
| 3480 | return rc; |
| 3481 | } |
| 3482 | cifs_dbg(FYI, "%s: ref.flags=0x%x\n", __func__, ref.flags); |
| 3483 | /* |
| 3484 | * Check if all targets are capable of handling DFS referrals as per |
| 3485 | * MS-DFSC 2.2.4 RESP_GET_DFS_REFERRAL. |
| 3486 | */ |
| 3487 | *ref_server = !!(ref.flags & DFSREF_REFERRAL_SERVER); |
| 3488 | free_dfs_info_param(&ref); |
| 3489 | } |
| 3490 | return 0; |
| 3491 | } |
| 3492 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3493 | int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3494 | { |
| 3495 | int rc = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3496 | unsigned int xid; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3497 | struct TCP_Server_Info *server = NULL; |
| 3498 | struct cifs_ses *ses = NULL, *root_ses = NULL; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3499 | struct cifs_tcon *tcon = NULL; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3500 | int count = 0; |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3501 | uuid_t mount_id = {0}; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3502 | char *ref_path = NULL, *full_path = NULL; |
| 3503 | char *oldmnt = NULL; |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3504 | bool ref_server = false; |
Al Viro | dd85446 | 2011-06-17 08:24:42 -0400 | [diff] [blame] | 3505 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3506 | rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3507 | /* |
Paulo Alcantara | d01132a | 2021-02-24 20:59:24 -0300 | [diff] [blame] | 3508 | * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally |
| 3509 | * try to get an DFS referral (even cached) to determine whether it is an DFS mount. |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3510 | * |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3511 | * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem |
| 3512 | * to respond with PATH_NOT_COVERED to requests that include the prefix. |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3513 | */ |
Paulo Alcantara | d01132a | 2021-02-24 20:59:24 -0300 | [diff] [blame] | 3514 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) || |
| 3515 | dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb), ctx->UNC + 1, NULL, |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3516 | NULL)) { |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3517 | if (rc) |
| 3518 | goto error; |
| 3519 | /* Check if it is fully accessible and then mount it */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3520 | rc = is_path_remote(cifs_sb, ctx, xid, server, tcon); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3521 | if (!rc) |
| 3522 | goto out; |
| 3523 | if (rc != -EREMOTE) |
| 3524 | goto error; |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3525 | } |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3526 | |
Paulo Alcantara | cdc3363 | 2021-07-15 21:53:53 -0300 | [diff] [blame] | 3527 | mount_put_conns(cifs_sb, xid, server, ses, tcon); |
| 3528 | /* |
| 3529 | * Ignore error check here because we may failover to other targets from cached a |
| 3530 | * referral. |
| 3531 | */ |
| 3532 | (void)mount_get_dfs_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon); |
Paulo Alcantara | f3c852b | 2021-06-04 19:25:33 -0300 | [diff] [blame] | 3533 | |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3534 | /* Get path of DFS root */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3535 | ref_path = build_unc_path_to_root(ctx, cifs_sb, false); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3536 | if (IS_ERR(ref_path)) { |
| 3537 | rc = PTR_ERR(ref_path); |
| 3538 | ref_path = NULL; |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3539 | goto error; |
| 3540 | } |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3541 | |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3542 | uuid_gen(&mount_id); |
| 3543 | set_root_ses(cifs_sb, &mount_id, ses, &root_ses); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3544 | do { |
| 3545 | /* Save full path of last DFS path we used to resolve final target server */ |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3546 | kfree(full_path); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3547 | full_path = build_unc_path_to_root(ctx, cifs_sb, !!count); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3548 | if (IS_ERR(full_path)) { |
| 3549 | rc = PTR_ERR(full_path); |
Dan Carpenter | 20b135e | 2020-08-05 12:52:07 +0300 | [diff] [blame] | 3550 | full_path = NULL; |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3551 | break; |
| 3552 | } |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3553 | /* Chase referral */ |
Ronnie Sahlberg | a2a52a8 | 2020-11-10 09:12:31 +1000 | [diff] [blame] | 3554 | oldmnt = cifs_sb->ctx->mount_options; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3555 | rc = expand_dfs_referral(xid, root_ses, ctx, cifs_sb, ref_path + 1); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3556 | if (rc) |
| 3557 | break; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3558 | /* Connect to new DFS target only if we were redirected */ |
Ronnie Sahlberg | a2a52a8 | 2020-11-10 09:12:31 +1000 | [diff] [blame] | 3559 | if (oldmnt != cifs_sb->ctx->mount_options) { |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3560 | mount_put_conns(cifs_sb, xid, server, ses, tcon); |
Paulo Alcantara | cdc3363 | 2021-07-15 21:53:53 -0300 | [diff] [blame] | 3561 | rc = mount_get_dfs_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon); |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3562 | } |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3563 | if (rc && !server && !ses) { |
| 3564 | /* Failed to connect. Try to connect to other targets in the referral. */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3565 | rc = do_dfs_failover(ref_path + 1, full_path, cifs_sb, ctx, root_ses, &xid, |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3566 | &server, &ses, &tcon); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3567 | } |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3568 | if (rc == -EACCES || rc == -EOPNOTSUPP || !server || !ses) |
| 3569 | break; |
| 3570 | if (!tcon) |
| 3571 | continue; |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3572 | |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3573 | /* Make sure that requests go through new root servers */ |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3574 | rc = is_referral_server(ref_path + 1, cifs_sb, tcon, &ref_server); |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3575 | if (rc) |
| 3576 | break; |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3577 | if (ref_server) |
| 3578 | set_root_ses(cifs_sb, &mount_id, ses, &root_ses); |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3579 | |
Paulo Alcantara | ff2c54a | 2021-02-24 20:59:22 -0300 | [diff] [blame] | 3580 | /* Get next dfs path and then continue chasing them if -EREMOTE */ |
| 3581 | rc = next_dfs_prepath(cifs_sb, ctx, xid, server, tcon, &ref_path); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3582 | /* Prevent recursion on broken link referrals */ |
| 3583 | if (rc == -EREMOTE && ++count > MAX_NESTED_LINKS) |
| 3584 | rc = -ELOOP; |
| 3585 | } while (rc == -EREMOTE); |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3586 | |
Steve French | 63f94e9 | 2021-07-13 19:40:33 -0500 | [diff] [blame] | 3587 | if (rc || !tcon || !ses) |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3588 | goto error; |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3589 | |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3590 | kfree(ref_path); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3591 | /* |
| 3592 | * Store DFS full path in both superblock and tree connect structures. |
| 3593 | * |
| 3594 | * For DFS root mounts, the prefix path (cifs_sb->prepath) is preserved during reconnect so |
| 3595 | * only the root path is set in cifs_sb->origin_fullpath and tcon->dfs_path. And for DFS |
| 3596 | * links, the prefix path is included in both and may be changed during reconnect. See |
| 3597 | * cifs_tree_connect(). |
| 3598 | */ |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3599 | ref_path = dfs_cache_canonical_path(full_path, cifs_sb->local_nls, cifs_remap(cifs_sb)); |
| 3600 | kfree(full_path); |
| 3601 | full_path = NULL; |
| 3602 | |
| 3603 | if (IS_ERR(ref_path)) { |
| 3604 | rc = PTR_ERR(ref_path); |
| 3605 | ref_path = NULL; |
| 3606 | goto error; |
| 3607 | } |
| 3608 | cifs_sb->origin_fullpath = ref_path; |
| 3609 | |
| 3610 | ref_path = kstrdup(cifs_sb->origin_fullpath, GFP_KERNEL); |
| 3611 | if (!ref_path) { |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 3612 | rc = -ENOMEM; |
| 3613 | goto error; |
| 3614 | } |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3615 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3616 | tcon->dfs_path = ref_path; |
| 3617 | ref_path = NULL; |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 3618 | spin_unlock(&cifs_tcp_ses_lock); |
| 3619 | |
Aurelien Aptel | 5fc7fcd | 2018-11-16 16:13:25 +0100 | [diff] [blame] | 3620 | /* |
| 3621 | * After reconnecting to a different server, unique ids won't |
| 3622 | * match anymore, so we disable serverino. This prevents |
| 3623 | * dentry revalidation to think the dentry are stale (ESTALE). |
| 3624 | */ |
| 3625 | cifs_autodisable_serverino(cifs_sb); |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 3626 | /* |
| 3627 | * Force the use of prefix path to support failover on DFS paths that |
| 3628 | * resolve to targets that have different prefix paths. |
| 3629 | */ |
| 3630 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; |
| 3631 | kfree(cifs_sb->prepath); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3632 | cifs_sb->prepath = ctx->prepath; |
| 3633 | ctx->prepath = NULL; |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3634 | uuid_copy(&cifs_sb->dfs_mount_id, &mount_id); |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 3635 | |
Jeff Layton | 70fe7dc | 2007-11-16 22:21:07 +0000 | [diff] [blame] | 3636 | out: |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3637 | free_xid(xid); |
Ronnie Sahlberg | 387ec58 | 2020-12-14 16:40:20 +1000 | [diff] [blame] | 3638 | cifs_try_adding_channels(cifs_sb, ses); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3639 | return mount_setup_tlink(cifs_sb, ses, tcon); |
| 3640 | |
| 3641 | error: |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3642 | kfree(ref_path); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3643 | kfree(full_path); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3644 | kfree(cifs_sb->origin_fullpath); |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3645 | dfs_cache_put_refsrv_sessions(&mount_id); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3646 | mount_put_conns(cifs_sb, xid, server, ses, tcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | return rc; |
| 3648 | } |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3649 | #else |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3650 | int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3651 | { |
| 3652 | int rc = 0; |
| 3653 | unsigned int xid; |
| 3654 | struct cifs_ses *ses; |
| 3655 | struct cifs_tcon *tcon; |
| 3656 | struct TCP_Server_Info *server; |
| 3657 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3658 | rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3659 | if (rc) |
| 3660 | goto error; |
| 3661 | |
| 3662 | if (tcon) { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3663 | rc = is_path_remote(cifs_sb, ctx, xid, server, tcon); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3664 | if (rc == -EREMOTE) |
| 3665 | rc = -EOPNOTSUPP; |
| 3666 | if (rc) |
| 3667 | goto error; |
| 3668 | } |
| 3669 | |
| 3670 | free_xid(xid); |
| 3671 | |
| 3672 | return mount_setup_tlink(cifs_sb, ses, tcon); |
| 3673 | |
| 3674 | error: |
| 3675 | mount_put_conns(cifs_sb, xid, server, ses, tcon); |
| 3676 | return rc; |
| 3677 | } |
| 3678 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | |
Jeff Layton | 8d1bca3 | 2011-06-11 21:17:10 -0400 | [diff] [blame] | 3680 | /* |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3681 | * Issue a TREE_CONNECT request. |
Jeff Layton | 8d1bca3 | 2011-06-11 21:17:10 -0400 | [diff] [blame] | 3682 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | int |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 3684 | CIFSTCon(const unsigned int xid, struct cifs_ses *ses, |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3685 | const char *tree, struct cifs_tcon *tcon, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | const struct nls_table *nls_codepage) |
| 3687 | { |
| 3688 | struct smb_hdr *smb_buffer; |
| 3689 | struct smb_hdr *smb_buffer_response; |
| 3690 | TCONX_REQ *pSMB; |
| 3691 | TCONX_RSP *pSMBr; |
| 3692 | unsigned char *bcc_ptr; |
| 3693 | int rc = 0; |
Jeff Layton | 690c522 | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 3694 | int length; |
| 3695 | __u16 bytes_left, count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | |
| 3697 | if (ses == NULL) |
| 3698 | return -EIO; |
| 3699 | |
| 3700 | smb_buffer = cifs_buf_get(); |
Steve French | ca43e3b | 2009-09-01 17:20:50 +0000 | [diff] [blame] | 3701 | if (smb_buffer == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | return -ENOMEM; |
Steve French | ca43e3b | 2009-09-01 17:20:50 +0000 | [diff] [blame] | 3703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | smb_buffer_response = smb_buffer; |
| 3705 | |
| 3706 | header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX, |
| 3707 | NULL /*no tid */ , 4 /*wct */ ); |
Steve French | 1982c34 | 2005-08-17 12:38:22 -0700 | [diff] [blame] | 3708 | |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 3709 | smb_buffer->Mid = get_next_mid(ses->server); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | smb_buffer->Uid = ses->Suid; |
| 3711 | pSMB = (TCONX_REQ *) smb_buffer; |
| 3712 | pSMBr = (TCONX_RSP *) smb_buffer_response; |
| 3713 | |
| 3714 | pSMB->AndXCommand = 0xFF; |
| 3715 | pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | bcc_ptr = &pSMB->Password[0]; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3717 | if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) { |
Steve French | eeac804 | 2006-01-13 21:34:58 -0800 | [diff] [blame] | 3718 | pSMB->PasswordLength = cpu_to_le16(1); /* minimum */ |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 3719 | *bcc_ptr = 0; /* password is null byte */ |
Steve French | eeac804 | 2006-01-13 21:34:58 -0800 | [diff] [blame] | 3720 | bcc_ptr++; /* skip password */ |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 3721 | /* already aligned so no need to do it below */ |
Steve French | eeac804 | 2006-01-13 21:34:58 -0800 | [diff] [blame] | 3722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 3724 | if (ses->server->sign) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
| 3726 | |
| 3727 | if (ses->capabilities & CAP_STATUS32) { |
| 3728 | smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS; |
| 3729 | } |
| 3730 | if (ses->capabilities & CAP_DFS) { |
| 3731 | smb_buffer->Flags2 |= SMBFLG2_DFS; |
| 3732 | } |
| 3733 | if (ses->capabilities & CAP_UNICODE) { |
| 3734 | smb_buffer->Flags2 |= SMBFLG2_UNICODE; |
| 3735 | length = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3736 | cifs_strtoUTF16((__le16 *) bcc_ptr, tree, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3737 | 6 /* max utf8 char length in bytes */ * |
Steve French | a878fb2 | 2006-05-30 18:04:19 +0000 | [diff] [blame] | 3738 | (/* server len*/ + 256 /* share len */), nls_codepage); |
| 3739 | bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | bcc_ptr += 2; /* skip trailing null */ |
| 3741 | } else { /* ASCII */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | strcpy(bcc_ptr, tree); |
| 3743 | bcc_ptr += strlen(tree) + 1; |
| 3744 | } |
| 3745 | strcpy(bcc_ptr, "?????"); |
| 3746 | bcc_ptr += strlen("?????"); |
| 3747 | bcc_ptr += 1; |
| 3748 | count = bcc_ptr - &pSMB->Password[0]; |
Qinglang Miao | 1a0e7f7 | 2020-07-25 16:56:01 +0800 | [diff] [blame] | 3749 | be32_add_cpu(&pSMB->hdr.smb_buf_length, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | pSMB->ByteCount = cpu_to_le16(count); |
| 3751 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 3752 | rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length, |
Jeff Layton | 7749981 | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 3753 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | /* above now done in SendReceive */ |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3756 | if (rc == 0) { |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3757 | bool is_unicode; |
| 3758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | tcon->tidStatus = CifsGood; |
Steve French | 3b79521 | 2008-11-13 19:45:32 +0000 | [diff] [blame] | 3760 | tcon->need_reconnect = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | tcon->tid = smb_buffer_response->Tid; |
| 3762 | bcc_ptr = pByteArea(smb_buffer_response); |
Jeff Layton | 690c522 | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 3763 | bytes_left = get_bcc(smb_buffer_response); |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3764 | length = strnlen(bcc_ptr, bytes_left - 2); |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3765 | if (smb_buffer->Flags2 & SMBFLG2_UNICODE) |
| 3766 | is_unicode = true; |
| 3767 | else |
| 3768 | is_unicode = false; |
| 3769 | |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3770 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3771 | /* skip service field (NB: this field is always ASCII) */ |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 3772 | if (length == 3) { |
| 3773 | if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') && |
| 3774 | (bcc_ptr[2] == 'C')) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3775 | cifs_dbg(FYI, "IPC connection\n"); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3776 | tcon->ipc = true; |
| 3777 | tcon->pipe = true; |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 3778 | } |
| 3779 | } else if (length == 2) { |
| 3780 | if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) { |
| 3781 | /* the most common case */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3782 | cifs_dbg(FYI, "disk share connection\n"); |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 3783 | } |
| 3784 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3785 | bcc_ptr += length + 1; |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3786 | bytes_left -= (length + 1); |
Zhao Hongjiang | 46b51d0 | 2013-06-24 01:57:47 -0500 | [diff] [blame] | 3787 | strlcpy(tcon->treeName, tree, sizeof(tcon->treeName)); |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3788 | |
| 3789 | /* mostly informational -- no need to fail on error here */ |
Jeff Layton | 90a98b2 | 2009-07-20 13:40:52 -0400 | [diff] [blame] | 3790 | kfree(tcon->nativeFileSystem); |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3791 | tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr, |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3792 | bytes_left, is_unicode, |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3793 | nls_codepage); |
| 3794 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3795 | cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem); |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3796 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3797 | if ((smb_buffer_response->WordCount == 3) || |
Steve French | 1a4e15a | 2006-10-12 21:33:51 +0000 | [diff] [blame] | 3798 | (smb_buffer_response->WordCount == 7)) |
| 3799 | /* field is in same location */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 3800 | tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport); |
| 3801 | else |
| 3802 | tcon->Flags = 0; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3803 | cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3804 | } |
| 3805 | |
Mariusz Kozlowski | a8a11d3 | 2007-10-03 16:41:24 +0000 | [diff] [blame] | 3806 | cifs_buf_release(smb_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3807 | return rc; |
| 3808 | } |
| 3809 | |
Al Viro | 2e32cf5 | 2013-10-03 12:53:37 -0400 | [diff] [blame] | 3810 | static void delayed_free(struct rcu_head *p) |
| 3811 | { |
Ronnie Sahlberg | d17abdf7 | 2020-11-10 08:59:26 +1000 | [diff] [blame] | 3812 | struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu); |
| 3813 | |
| 3814 | unload_nls(cifs_sb->local_nls); |
Ronnie Sahlberg | c741cba | 2020-12-14 16:40:16 +1000 | [diff] [blame] | 3815 | smb3_cleanup_fs_context(cifs_sb->ctx); |
Ronnie Sahlberg | d17abdf7 | 2020-11-10 08:59:26 +1000 | [diff] [blame] | 3816 | kfree(cifs_sb); |
Al Viro | 2e32cf5 | 2013-10-03 12:53:37 -0400 | [diff] [blame] | 3817 | } |
| 3818 | |
Al Viro | 2a9b995 | 2011-06-17 09:27:16 -0400 | [diff] [blame] | 3819 | void |
| 3820 | cifs_umount(struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | { |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 3822 | struct rb_root *root = &cifs_sb->tlink_tree; |
| 3823 | struct rb_node *node; |
| 3824 | struct tcon_link *tlink; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 3826 | cancel_delayed_work_sync(&cifs_sb->prune_tlinks); |
| 3827 | |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 3828 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 3829 | while ((node = rb_first(root))) { |
| 3830 | tlink = rb_entry(node, struct tcon_link, tl_rbnode); |
| 3831 | cifs_get_tlink(tlink); |
| 3832 | clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); |
| 3833 | rb_erase(node, root); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3834 | |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 3835 | spin_unlock(&cifs_sb->tlink_tree_lock); |
| 3836 | cifs_put_tlink(tlink); |
| 3837 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 3838 | } |
| 3839 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3840 | |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3841 | kfree(cifs_sb->prepath); |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 3842 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3843 | dfs_cache_put_refsrv_sessions(&cifs_sb->dfs_mount_id); |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 3844 | kfree(cifs_sb->origin_fullpath); |
| 3845 | #endif |
Al Viro | 2e32cf5 | 2013-10-03 12:53:37 -0400 | [diff] [blame] | 3846 | call_rcu(&cifs_sb->rcu, delayed_free); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 3849 | int |
| 3850 | cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3851 | { |
| 3852 | int rc = 0; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 3853 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 3855 | if (!server->ops->need_neg || !server->ops->negotiate) |
| 3856 | return -ENOSYS; |
| 3857 | |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3858 | /* only send once per connect */ |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 3859 | if (!server->ops->need_neg(server)) |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3860 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 3862 | rc = server->ops->negotiate(xid, ses); |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3863 | if (rc == 0) { |
| 3864 | spin_lock(&GlobalMid_Lock); |
Jeff Layton | 7fdbaa1 | 2011-06-10 16:14:57 -0400 | [diff] [blame] | 3865 | if (server->tcpStatus == CifsNeedNegotiate) |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3866 | server->tcpStatus = CifsGood; |
| 3867 | else |
| 3868 | rc = -EHOSTDOWN; |
| 3869 | spin_unlock(&GlobalMid_Lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | } |
Steve French | 26b994f | 2008-08-06 05:11:33 +0000 | [diff] [blame] | 3871 | |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3872 | return rc; |
| 3873 | } |
Steve French | 26b994f | 2008-08-06 05:11:33 +0000 | [diff] [blame] | 3874 | |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 3875 | int |
| 3876 | cifs_setup_session(const unsigned int xid, struct cifs_ses *ses, |
| 3877 | struct nls_table *nls_info) |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3878 | { |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 3879 | int rc = -ENOSYS; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 3880 | struct TCP_Server_Info *server = cifs_ses_server(ses); |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3881 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 3882 | if (!ses->binding) { |
| 3883 | ses->capabilities = server->capabilities; |
YANG LI | ed6b192 | 2021-01-11 17:15:28 +0800 | [diff] [blame] | 3884 | if (!linuxExtEnabled) |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 3885 | ses->capabilities &= (~server->vals->cap_unix); |
| 3886 | |
| 3887 | if (ses->auth_key.response) { |
| 3888 | cifs_dbg(FYI, "Free previous auth_key.response = %p\n", |
| 3889 | ses->auth_key.response); |
| 3890 | kfree(ses->auth_key.response); |
| 3891 | ses->auth_key.response = NULL; |
| 3892 | ses->auth_key.len = 0; |
| 3893 | } |
| 3894 | } |
Steve French | 20418ac | 2009-04-30 16:13:32 +0000 | [diff] [blame] | 3895 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3896 | cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n", |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3897 | server->sec_mode, server->capabilities, server->timeAdj); |
Jeff Layton | cb7691b | 2008-08-18 15:41:05 -0400 | [diff] [blame] | 3898 | |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 3899 | if (server->ops->sess_setup) |
| 3900 | rc = server->ops->sess_setup(xid, ses, nls_info); |
| 3901 | |
Shirish Pargaonkar | d4e63bd | 2013-08-29 08:35:09 -0500 | [diff] [blame] | 3902 | if (rc) |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 3903 | cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc); |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 3904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3905 | return rc; |
| 3906 | } |
| 3907 | |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3908 | static int |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3909 | cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses) |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3910 | { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3911 | ctx->sectype = ses->sectype; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 3912 | |
| 3913 | /* krb5 is special, since we don't need username or pw */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3914 | if (ctx->sectype == Kerberos) |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3915 | return 0; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3916 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3917 | return cifs_set_cifscreds(ctx, ses); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3918 | } |
| 3919 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3920 | static struct cifs_tcon * |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 3921 | cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3922 | { |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3923 | int rc; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3924 | struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb); |
| 3925 | struct cifs_ses *ses; |
| 3926 | struct cifs_tcon *tcon = NULL; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3927 | struct smb3_fs_context *ctx; |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3928 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3929 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 3930 | if (ctx == NULL) |
Dan Carpenter | 803ab97 | 2012-01-24 11:39:22 +0300 | [diff] [blame] | 3931 | return ERR_PTR(-ENOMEM); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3932 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3933 | ctx->local_nls = cifs_sb->local_nls; |
| 3934 | ctx->linux_uid = fsuid; |
| 3935 | ctx->cred_uid = fsuid; |
| 3936 | ctx->UNC = master_tcon->treeName; |
| 3937 | ctx->retry = master_tcon->retry; |
| 3938 | ctx->nocase = master_tcon->nocase; |
| 3939 | ctx->nohandlecache = master_tcon->nohandlecache; |
| 3940 | ctx->local_lease = master_tcon->local_lease; |
| 3941 | ctx->no_lease = master_tcon->no_lease; |
| 3942 | ctx->resilient = master_tcon->use_resilient; |
| 3943 | ctx->persistent = master_tcon->use_persistent; |
| 3944 | ctx->handle_timeout = master_tcon->handle_timeout; |
| 3945 | ctx->no_linux_ext = !master_tcon->unix_ext; |
| 3946 | ctx->linux_ext = master_tcon->posix_extensions; |
| 3947 | ctx->sectype = master_tcon->ses->sectype; |
| 3948 | ctx->sign = master_tcon->ses->sign; |
| 3949 | ctx->seal = master_tcon->seal; |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 3950 | ctx->witness = master_tcon->use_witness; |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3951 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3952 | rc = cifs_set_vol_auth(ctx, master_tcon->ses); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3953 | if (rc) { |
| 3954 | tcon = ERR_PTR(rc); |
| 3955 | goto out; |
| 3956 | } |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3957 | |
| 3958 | /* get a reference for the same TCP session */ |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 3959 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3960 | ++master_tcon->ses->server->srv_count; |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 3961 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3962 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3963 | ses = cifs_get_smb_ses(master_tcon->ses->server, ctx); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3964 | if (IS_ERR(ses)) { |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3965 | tcon = (struct cifs_tcon *)ses; |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 3966 | cifs_put_tcp_session(master_tcon->ses->server, 0); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3967 | goto out; |
| 3968 | } |
| 3969 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3970 | tcon = cifs_get_tcon(ses, ctx); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3971 | if (IS_ERR(tcon)) { |
| 3972 | cifs_put_smb_ses(ses); |
| 3973 | goto out; |
| 3974 | } |
| 3975 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 3976 | if (cap_unix(ses)) |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3977 | reset_cifs_unix_caps(0, tcon, NULL, ctx); |
Steve French | b326614 | 2018-05-20 23:41:10 -0500 | [diff] [blame] | 3978 | |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3979 | out: |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3980 | kfree(ctx->username); |
| 3981 | kfree_sensitive(ctx->password); |
| 3982 | kfree(ctx); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3983 | |
| 3984 | return tcon; |
| 3985 | } |
| 3986 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3987 | struct cifs_tcon * |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3988 | cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb) |
| 3989 | { |
| 3990 | return tlink_tcon(cifs_sb_master_tlink(cifs_sb)); |
| 3991 | } |
| 3992 | |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 3993 | /* find and return a tlink with given uid */ |
| 3994 | static struct tcon_link * |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 3995 | tlink_rb_search(struct rb_root *root, kuid_t uid) |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 3996 | { |
| 3997 | struct rb_node *node = root->rb_node; |
| 3998 | struct tcon_link *tlink; |
| 3999 | |
| 4000 | while (node) { |
| 4001 | tlink = rb_entry(node, struct tcon_link, tl_rbnode); |
| 4002 | |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 4003 | if (uid_gt(tlink->tl_uid, uid)) |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4004 | node = node->rb_left; |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 4005 | else if (uid_lt(tlink->tl_uid, uid)) |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4006 | node = node->rb_right; |
| 4007 | else |
| 4008 | return tlink; |
| 4009 | } |
| 4010 | return NULL; |
| 4011 | } |
| 4012 | |
| 4013 | /* insert a tcon_link into the tree */ |
| 4014 | static void |
| 4015 | tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink) |
| 4016 | { |
| 4017 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 4018 | struct tcon_link *tlink; |
| 4019 | |
| 4020 | while (*new) { |
| 4021 | tlink = rb_entry(*new, struct tcon_link, tl_rbnode); |
| 4022 | parent = *new; |
| 4023 | |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 4024 | if (uid_gt(tlink->tl_uid, new_tlink->tl_uid)) |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4025 | new = &((*new)->rb_left); |
| 4026 | else |
| 4027 | new = &((*new)->rb_right); |
| 4028 | } |
| 4029 | |
| 4030 | rb_link_node(&new_tlink->tl_rbnode, parent, new); |
| 4031 | rb_insert_color(&new_tlink->tl_rbnode, root); |
| 4032 | } |
| 4033 | |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4034 | /* |
| 4035 | * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the |
| 4036 | * current task. |
| 4037 | * |
| 4038 | * If the superblock doesn't refer to a multiuser mount, then just return |
| 4039 | * the master tcon for the mount. |
| 4040 | * |
Suresh Jayaraman | 6ef933a | 2010-11-03 10:53:49 +0530 | [diff] [blame] | 4041 | * First, search the rbtree for an existing tcon for this fsuid. If one |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4042 | * exists, then check to see if it's pending construction. If it is then wait |
| 4043 | * for construction to complete. Once it's no longer pending, check to see if |
| 4044 | * it failed and either return an error or retry construction, depending on |
| 4045 | * the timeout. |
| 4046 | * |
| 4047 | * If one doesn't exist then insert a new tcon_link struct into the tree and |
| 4048 | * try to construct a new one. |
| 4049 | */ |
| 4050 | struct tcon_link * |
| 4051 | cifs_sb_tlink(struct cifs_sb_info *cifs_sb) |
| 4052 | { |
| 4053 | int ret; |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 4054 | kuid_t fsuid = current_fsuid(); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4055 | struct tcon_link *tlink, *newtlink; |
| 4056 | |
| 4057 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) |
| 4058 | return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); |
| 4059 | |
| 4060 | spin_lock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4061 | tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4062 | if (tlink) |
| 4063 | cifs_get_tlink(tlink); |
| 4064 | spin_unlock(&cifs_sb->tlink_tree_lock); |
| 4065 | |
| 4066 | if (tlink == NULL) { |
| 4067 | newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL); |
| 4068 | if (newtlink == NULL) |
| 4069 | return ERR_PTR(-ENOMEM); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4070 | newtlink->tl_uid = fsuid; |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4071 | newtlink->tl_tcon = ERR_PTR(-EACCES); |
| 4072 | set_bit(TCON_LINK_PENDING, &newtlink->tl_flags); |
| 4073 | set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags); |
| 4074 | cifs_get_tlink(newtlink); |
| 4075 | |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4076 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 4077 | /* was one inserted after previous search? */ |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4078 | tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4079 | if (tlink) { |
| 4080 | cifs_get_tlink(tlink); |
| 4081 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4082 | kfree(newtlink); |
| 4083 | goto wait_for_construction; |
| 4084 | } |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4085 | tlink = newtlink; |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4086 | tlink_rb_insert(&cifs_sb->tlink_tree, tlink); |
| 4087 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4088 | } else { |
| 4089 | wait_for_construction: |
| 4090 | ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING, |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4091 | TASK_INTERRUPTIBLE); |
| 4092 | if (ret) { |
| 4093 | cifs_put_tlink(tlink); |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 4094 | return ERR_PTR(-ERESTARTSYS); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | /* if it's good, return it */ |
| 4098 | if (!IS_ERR(tlink->tl_tcon)) |
| 4099 | return tlink; |
| 4100 | |
| 4101 | /* return error if we tried this already recently */ |
| 4102 | if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) { |
| 4103 | cifs_put_tlink(tlink); |
| 4104 | return ERR_PTR(-EACCES); |
| 4105 | } |
| 4106 | |
| 4107 | if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags)) |
| 4108 | goto wait_for_construction; |
| 4109 | } |
| 4110 | |
| 4111 | tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid); |
| 4112 | clear_bit(TCON_LINK_PENDING, &tlink->tl_flags); |
| 4113 | wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING); |
| 4114 | |
| 4115 | if (IS_ERR(tlink->tl_tcon)) { |
| 4116 | cifs_put_tlink(tlink); |
| 4117 | return ERR_PTR(-EACCES); |
| 4118 | } |
| 4119 | |
| 4120 | return tlink; |
| 4121 | } |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4122 | |
| 4123 | /* |
| 4124 | * periodic workqueue job that scans tcon_tree for a superblock and closes |
| 4125 | * out tcons. |
| 4126 | */ |
| 4127 | static void |
| 4128 | cifs_prune_tlinks(struct work_struct *work) |
| 4129 | { |
| 4130 | struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info, |
| 4131 | prune_tlinks.work); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4132 | struct rb_root *root = &cifs_sb->tlink_tree; |
Colin Ian King | 37e12f5 | 2018-01-17 09:52:39 +0000 | [diff] [blame] | 4133 | struct rb_node *node; |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4134 | struct rb_node *tmp; |
| 4135 | struct tcon_link *tlink; |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4136 | |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4137 | /* |
| 4138 | * Because we drop the spinlock in the loop in order to put the tlink |
| 4139 | * it's not guarded against removal of links from the tree. The only |
| 4140 | * places that remove entries from the tree are this function and |
| 4141 | * umounts. Because this function is non-reentrant and is canceled |
| 4142 | * before umount can proceed, this is safe. |
| 4143 | */ |
| 4144 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 4145 | node = rb_first(root); |
| 4146 | while (node != NULL) { |
| 4147 | tmp = node; |
| 4148 | node = rb_next(tmp); |
| 4149 | tlink = rb_entry(tmp, struct tcon_link, tl_rbnode); |
| 4150 | |
| 4151 | if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) || |
| 4152 | atomic_read(&tlink->tl_count) != 0 || |
| 4153 | time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies)) |
| 4154 | continue; |
| 4155 | |
| 4156 | cifs_get_tlink(tlink); |
| 4157 | clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); |
| 4158 | rb_erase(tmp, root); |
| 4159 | |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4160 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4161 | cifs_put_tlink(tlink); |
| 4162 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 4163 | } |
| 4164 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4165 | |
Jeff Layton | da472fc | 2012-03-23 14:40:53 -0400 | [diff] [blame] | 4166 | queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4167 | TLINK_IDLE_EXPIRE); |
| 4168 | } |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4169 | |
| 4170 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 4171 | int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc) |
| 4172 | { |
| 4173 | int rc; |
| 4174 | struct TCP_Server_Info *server = tcon->ses->server; |
| 4175 | const struct smb_version_operations *ops = server->ops; |
| 4176 | struct dfs_cache_tgt_list tl; |
| 4177 | struct dfs_cache_tgt_iterator *it = NULL; |
| 4178 | char *tree; |
| 4179 | const char *tcp_host; |
| 4180 | size_t tcp_host_len; |
| 4181 | const char *dfs_host; |
| 4182 | size_t dfs_host_len; |
Paulo Alcantara | 7548e1d | 2020-07-21 09:36:42 -0300 | [diff] [blame] | 4183 | char *share = NULL, *prefix = NULL; |
Paulo Alcantara | 11375a5 | 2020-07-21 09:36:43 -0300 | [diff] [blame] | 4184 | struct dfs_info3_param ref = {0}; |
| 4185 | bool isroot; |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4186 | |
| 4187 | tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL); |
| 4188 | if (!tree) |
| 4189 | return -ENOMEM; |
| 4190 | |
Paulo Alcantara | 507345b | 2021-07-12 12:38:24 -0300 | [diff] [blame] | 4191 | /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */ |
| 4192 | if (!tcon->dfs_path || dfs_cache_noreq_find(tcon->dfs_path + 1, &ref, &tl)) { |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4193 | if (tcon->ipc) { |
| 4194 | scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname); |
| 4195 | rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc); |
| 4196 | } else { |
| 4197 | rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc); |
| 4198 | } |
| 4199 | goto out; |
| 4200 | } |
| 4201 | |
Paulo Alcantara | 11375a5 | 2020-07-21 09:36:43 -0300 | [diff] [blame] | 4202 | isroot = ref.server_type == DFS_TYPE_ROOT; |
| 4203 | free_dfs_info_param(&ref); |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4204 | |
| 4205 | extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len); |
| 4206 | |
| 4207 | for (it = dfs_cache_get_tgt_iterator(&tl); it; it = dfs_cache_get_next_tgt(&tl, it)) { |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4208 | bool target_match; |
| 4209 | |
Paulo Alcantara | 7548e1d | 2020-07-21 09:36:42 -0300 | [diff] [blame] | 4210 | kfree(share); |
| 4211 | kfree(prefix); |
Colin Ian King | c6a80e1 | 2020-07-31 18:13:42 +0100 | [diff] [blame] | 4212 | share = NULL; |
| 4213 | prefix = NULL; |
Paulo Alcantara | 7548e1d | 2020-07-21 09:36:42 -0300 | [diff] [blame] | 4214 | |
| 4215 | rc = dfs_cache_get_tgt_share(tcon->dfs_path + 1, it, &share, &prefix); |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4216 | if (rc) { |
| 4217 | cifs_dbg(VFS, "%s: failed to parse target share %d\n", |
| 4218 | __func__, rc); |
| 4219 | continue; |
| 4220 | } |
| 4221 | |
| 4222 | extract_unc_hostname(share, &dfs_host, &dfs_host_len); |
| 4223 | |
| 4224 | if (dfs_host_len != tcp_host_len |
| 4225 | || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) { |
| 4226 | cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len, |
| 4227 | dfs_host, (int)tcp_host_len, tcp_host); |
| 4228 | |
| 4229 | rc = match_target_ip(server, dfs_host, dfs_host_len, &target_match); |
| 4230 | if (rc) { |
| 4231 | cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc); |
| 4232 | break; |
| 4233 | } |
| 4234 | |
| 4235 | if (!target_match) { |
| 4236 | cifs_dbg(FYI, "%s: skipping target\n", __func__); |
| 4237 | continue; |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | if (tcon->ipc) { |
Paulo Alcantara | 7548e1d | 2020-07-21 09:36:42 -0300 | [diff] [blame] | 4242 | scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", share); |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4243 | rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc); |
| 4244 | } else { |
Paulo Alcantara | 7548e1d | 2020-07-21 09:36:42 -0300 | [diff] [blame] | 4245 | scnprintf(tree, MAX_TREE_SIZE, "\\%s", share); |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4246 | rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc); |
Paulo Alcantara | 11375a5 | 2020-07-21 09:36:43 -0300 | [diff] [blame] | 4247 | /* Only handle prefix paths of DFS link targets */ |
| 4248 | if (!rc && !isroot) { |
Paulo Alcantara | 7548e1d | 2020-07-21 09:36:42 -0300 | [diff] [blame] | 4249 | rc = update_super_prepath(tcon, prefix); |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4250 | break; |
| 4251 | } |
| 4252 | } |
| 4253 | if (rc == -EREMOTE) |
| 4254 | break; |
| 4255 | } |
| 4256 | |
Paulo Alcantara | 7548e1d | 2020-07-21 09:36:42 -0300 | [diff] [blame] | 4257 | kfree(share); |
| 4258 | kfree(prefix); |
| 4259 | |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4260 | if (!rc) { |
| 4261 | if (it) |
| 4262 | rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1, it); |
| 4263 | else |
| 4264 | rc = -ENOENT; |
| 4265 | } |
| 4266 | dfs_cache_free_tgts(&tl); |
| 4267 | out: |
| 4268 | kfree(tree); |
| 4269 | return rc; |
| 4270 | } |
| 4271 | #else |
| 4272 | int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc) |
| 4273 | { |
| 4274 | const struct smb_version_operations *ops = tcon->ses->server->ops; |
| 4275 | |
| 4276 | return ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc); |
| 4277 | } |
| 4278 | #endif |