| 1 |
// SPDX-License-Identifier: LGPL-2.1 |
| 2 |
/* |
| 3 |
* |
| 4 |
* Copyright (C) International Business Machines Corp., 2002,2011 |
| 5 |
* Author(s): Steve French ([email protected]) |
| 6 |
* |
| 7 |
*/ |
| 8 |
#include <linux/fs.h> |
| 9 |
#include <linux/net.h> |
| 10 |
#include <linux/string.h> |
| 11 |
#include <linux/sched/mm.h> |
| 12 |
#include <linux/sched/signal.h> |
| 13 |
#include <linux/list.h> |
| 14 |
#include <linux/wait.h> |
| 15 |
#include <linux/slab.h> |
| 16 |
#include <linux/pagemap.h> |
| 17 |
#include <linux/ctype.h> |
| 18 |
#include <linux/utsname.h> |
| 19 |
#include <linux/mempool.h> |
| 20 |
#include <linux/delay.h> |
| 21 |
#include <linux/completion.h> |
| 22 |
#include <linux/kthread.h> |
| 23 |
#include <linux/pagevec.h> |
| 24 |
#include <linux/freezer.h> |
| 25 |
#include <linux/namei.h> |
| 26 |
#include <linux/uuid.h> |
| 27 |
#include <linux/uaccess.h> |
| 28 |
#include <asm/processor.h> |
| 29 |
#include <linux/inet.h> |
| 30 |
#include <linux/module.h> |
| 31 |
#include <keys/user-type.h> |
| 32 |
#include <net/ipv6.h> |
| 33 |
#include <linux/parser.h> |
| 34 |
#include <linux/bvec.h> |
| 35 |
#include "cifsglob.h" |
| 36 |
#include "cifsproto.h" |
| 37 |
#include "cifs_unicode.h" |
| 38 |
#include "cifs_debug.h" |
| 39 |
#include "cifs_fs_sb.h" |
| 40 |
#include "ntlmssp.h" |
| 41 |
#include "nterr.h" |
| 42 |
#include "rfc1002pdu.h" |
| 43 |
#include "fscache.h" |
| 44 |
#include "smb2proto.h" |
| 45 |
#include "smbdirect.h" |
| 46 |
#include "dns_resolve.h" |
| 47 |
#ifdef CONFIG_CIFS_DFS_UPCALL |
| 48 |
#include "dfs.h" |
| 49 |
#include "dfs_cache.h" |
| 50 |
#endif |
| 51 |
#include "fs_context.h" |
| 52 |
#include "cifs_swn.h" |
| 53 |
|
| 54 |
/* FIXME: should these be tunable? */ |
| 55 |
#define TLINK_ERROR_EXPIRE (1 * HZ) |
| 56 |
#define TLINK_IDLE_EXPIRE (600 * HZ) |
| 57 |
|
| 58 |
/* Drop the connection to not overload the server */ |
| 59 |
#define MAX_STATUS_IO_TIMEOUT 5 |
| 60 |
|
| 61 |
static int ip_connect(struct TCP_Server_Info *server); |
| 62 |
static int generic_ip_connect(struct TCP_Server_Info *server); |
| 63 |
static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink); |
| 64 |
static void cifs_prune_tlinks(struct work_struct *work); |
| 65 |
|
| 66 |
static struct mchan_mount *mchan_mount_alloc(struct cifs_ses *ses); |
| 67 |
static void mchan_mount_free(struct mchan_mount *mchan_mount); |
| 68 |
static void mchan_mount_work_fn(struct work_struct *work); |
| 69 |
|
| 70 |
/* |
| 71 |
* Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may |
| 72 |
* get their ip addresses changed at some point. |
| 73 |
* |
| 74 |
* This should be called with server->srv_mutex held. |
| 75 |
*/ |
| 76 |
static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server) |
| 77 |
{ |
| 78 |
struct sockaddr_storage ss; |
| 79 |
int rc; |
| 80 |
|
| 81 |
if (!server->hostname) |
| 82 |
return -EINVAL; |
| 83 |
|
| 84 |
/* if server hostname isn't populated, there's nothing to do here */ |
| 85 |
if (server->hostname[0] == '\0') |
| 86 |
return 0; |
| 87 |
|
| 88 |
spin_lock(&server->srv_lock); |
| 89 |
ss = server->dstaddr; |
| 90 |
spin_unlock(&server->srv_lock); |
| 91 |
|
| 92 |
rc = dns_resolve_name(server->dns_dom, server->hostname, |
| 93 |
strlen(server->hostname), |
| 94 |
(struct sockaddr *)&ss); |
| 95 |
if (!rc) { |
| 96 |
spin_lock(&server->srv_lock); |
| 97 |
memcpy(&server->dstaddr, &ss, sizeof(server->dstaddr)); |
| 98 |
spin_unlock(&server->srv_lock); |
| 99 |
} |
| 100 |
return rc; |
| 101 |
} |
| 102 |
|
| 103 |
void smb2_query_server_interfaces(struct work_struct *work) |
| 104 |
{ |
| 105 |
int rc; |
| 106 |
int xid; |
| 107 |
struct cifs_tcon *tcon = container_of(work, |
| 108 |
struct cifs_tcon, |
| 109 |
query_interfaces.work); |
| 110 |
struct TCP_Server_Info *server = tcon->ses->server; |
| 111 |
|
| 112 |
/* |
| 113 |
* query server network interfaces, in case they change |
| 114 |
*/ |
| 115 |
if (!server->ops->query_server_interfaces) |
| 116 |
return; |
| 117 |
|
| 118 |
xid = get_xid(); |
| 119 |
rc = server->ops->query_server_interfaces(xid, tcon, false); |
| 120 |
free_xid(xid); |
| 121 |
|
| 122 |
if (rc) |
| 123 |
cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n", |
| 124 |
__func__, rc); |
| 125 |
|
| 126 |
queue_delayed_work(cifsiod_wq, &tcon->query_interfaces, |
| 127 |
(SMB_INTERFACE_POLL_INTERVAL * HZ)); |
| 128 |
} |
| 129 |
|
| 130 |
#define set_need_reco(server) \ |
| 131 |
do { \ |
| 132 |
spin_lock(&server->srv_lock); \ |
| 133 |
if (server->tcpStatus != CifsExiting) \ |
| 134 |
server->tcpStatus = CifsNeedReconnect; \ |
| 135 |
spin_unlock(&server->srv_lock); \ |
| 136 |
} while (0) |
| 137 |
|
| 138 |
/* |
| 139 |
* Update the tcpStatus for the server. |
| 140 |
* This is used to signal the cifsd thread to call cifs_reconnect |
| 141 |
* ONLY cifsd thread should call cifs_reconnect. For any other |
| 142 |
* thread, use this function |
| 143 |
* |
| 144 |
* @server: the tcp ses for which reconnect is needed |
| 145 |
* @all_channels: if this needs to be done for all channels |
| 146 |
*/ |
| 147 |
void |
| 148 |
cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server, |
| 149 |
bool all_channels) |
| 150 |
{ |
| 151 |
struct TCP_Server_Info *nserver; |
| 152 |
struct cifs_ses *ses; |
| 153 |
LIST_HEAD(reco); |
| 154 |
int i; |
| 155 |
|
| 156 |
/* if we need to signal just this channel */ |
| 157 |
if (!all_channels) { |
| 158 |
set_need_reco(server); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
if (SERVER_IS_CHAN(server)) |
| 163 |
server = server->primary_server; |
| 164 |
scoped_guard(spinlock, &cifs_tcp_ses_lock) { |
| 165 |
set_need_reco(server); |
| 166 |
list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { |
| 167 |
spin_lock(&ses->ses_lock); |
| 168 |
if (ses->ses_status == SES_EXITING) { |
| 169 |
spin_unlock(&ses->ses_lock); |
| 170 |
continue; |
| 171 |
} |
| 172 |
spin_lock(&ses->chan_lock); |
| 173 |
for (i = 1; i < ses->chan_count; i++) { |
| 174 |
nserver = ses->chans[i].server; |
| 175 |
if (!nserver) |
| 176 |
continue; |
| 177 |
nserver->srv_count++; |
| 178 |
list_add(&nserver->rlist, &reco); |
| 179 |
} |
| 180 |
spin_unlock(&ses->chan_lock); |
| 181 |
spin_unlock(&ses->ses_lock); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
list_for_each_entry_safe(server, nserver, &reco, rlist) { |
| 186 |
list_del_init(&server->rlist); |
| 187 |
set_need_reco(server); |
| 188 |
cifs_put_tcp_session(server, 0); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/* |
| 193 |
* Mark all sessions and tcons for reconnect. |
| 194 |
* IMPORTANT: make sure that this gets called only from |
| 195 |
* cifsd thread. For any other thread, use |
| 196 |
* cifs_signal_cifsd_for_reconnect |
| 197 |
* |
| 198 |
* @server: the tcp ses for which reconnect is needed |
| 199 |
* @server needs to be previously set to CifsNeedReconnect. |
| 200 |
* @mark_smb_session: whether even sessions need to be marked |
| 201 |
*/ |
| 202 |
void |
| 203 |
cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server, |
| 204 |
bool mark_smb_session) |
| 205 |
{ |
| 206 |
struct TCP_Server_Info *pserver; |
| 207 |
struct cifs_ses *ses, *nses; |
| 208 |
struct cifs_tcon *tcon; |
| 209 |
|
| 210 |
/* |
| 211 |
* before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they |
| 212 |
* are not used until reconnected. |
| 213 |
*/ |
| 214 |
cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__); |
| 215 |
|
| 216 |
/* If server is a channel, select the primary channel */ |
| 217 |
pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; |
| 218 |
|
| 219 |
/* |
| 220 |
* if the server has been marked for termination, there is a |
| 221 |
* chance that the remaining channels all need reconnect. To be |
| 222 |
* on the safer side, mark the session and trees for reconnect |
| 223 |
* for this scenario. This might cause a few redundant session |
| 224 |
* setup and tree connect requests, but it is better than not doing |
| 225 |
* a tree connect when needed, and all following requests failing |
| 226 |
*/ |
| 227 |
if (server->terminate) { |
| 228 |
mark_smb_session = true; |
| 229 |
server = pserver; |
| 230 |
} |
| 231 |
|
| 232 |
spin_lock(&cifs_tcp_ses_lock); |
| 233 |
list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) { |
| 234 |
spin_lock(&ses->ses_lock); |
| 235 |
if (ses->ses_status == SES_EXITING) { |
| 236 |
spin_unlock(&ses->ses_lock); |
| 237 |
continue; |
| 238 |
} |
| 239 |
spin_unlock(&ses->ses_lock); |
| 240 |
|
| 241 |
spin_lock(&ses->chan_lock); |
| 242 |
if (cifs_ses_get_chan_index(ses, server) == |
| 243 |
CIFS_INVAL_CHAN_INDEX) { |
| 244 |
spin_unlock(&ses->chan_lock); |
| 245 |
continue; |
| 246 |
} |
| 247 |
|
| 248 |
if (!cifs_chan_is_iface_active(ses, server)) { |
| 249 |
spin_unlock(&ses->chan_lock); |
| 250 |
cifs_chan_update_iface(ses, server); |
| 251 |
spin_lock(&ses->chan_lock); |
| 252 |
} |
| 253 |
|
| 254 |
if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) { |
| 255 |
spin_unlock(&ses->chan_lock); |
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
if (mark_smb_session) |
| 260 |
CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses); |
| 261 |
else |
| 262 |
cifs_chan_set_need_reconnect(ses, server); |
| 263 |
|
| 264 |
cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n", |
| 265 |
__func__, ses->chans_need_reconnect); |
| 266 |
|
| 267 |
/* If all channels need reconnect, then tcon needs reconnect */ |
| 268 |
if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) { |
| 269 |
spin_unlock(&ses->chan_lock); |
| 270 |
continue; |
| 271 |
} |
| 272 |
spin_unlock(&ses->chan_lock); |
| 273 |
|
| 274 |
spin_lock(&ses->ses_lock); |
| 275 |
ses->ses_status = SES_NEED_RECON; |
| 276 |
spin_unlock(&ses->ses_lock); |
| 277 |
|
| 278 |
list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { |
| 279 |
tcon->need_reconnect = true; |
| 280 |
spin_lock(&tcon->tc_lock); |
| 281 |
tcon->status = TID_NEED_RECON; |
| 282 |
spin_unlock(&tcon->tc_lock); |
| 283 |
|
| 284 |
cancel_delayed_work(&tcon->query_interfaces); |
| 285 |
} |
| 286 |
if (ses->tcon_ipc) { |
| 287 |
ses->tcon_ipc->need_reconnect = true; |
| 288 |
spin_lock(&ses->tcon_ipc->tc_lock); |
| 289 |
ses->tcon_ipc->status = TID_NEED_RECON; |
| 290 |
spin_unlock(&ses->tcon_ipc->tc_lock); |
| 291 |
} |
| 292 |
} |
| 293 |
spin_unlock(&cifs_tcp_ses_lock); |
| 294 |
} |
| 295 |
|
| 296 |
static void |
| 297 |
cifs_abort_connection(struct TCP_Server_Info *server) |
| 298 |
{ |
| 299 |
struct mid_q_entry *mid, *nmid; |
| 300 |
struct list_head retry_list; |
| 301 |
|
| 302 |
server->maxBuf = 0; |
| 303 |
server->max_read = 0; |
| 304 |
|
| 305 |
/* do not want to be sending data on a socket we are freeing */ |
| 306 |
cifs_dbg(FYI, "%s: tearing down socket\n", __func__); |
| 307 |
cifs_server_lock(server); |
| 308 |
if (server->ssocket) { |
| 309 |
cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state, |
| 310 |
server->ssocket->flags); |
| 311 |
kernel_sock_shutdown(server->ssocket, SHUT_WR); |
| 312 |
cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state, |
| 313 |
server->ssocket->flags); |
| 314 |
sock_release(server->ssocket); |
| 315 |
server->ssocket = NULL; |
| 316 |
} else if (cifs_rdma_enabled(server)) { |
| 317 |
smbd_destroy(server); |
| 318 |
} |
| 319 |
server->sequence_number = 0; |
| 320 |
server->session_estab = false; |
| 321 |
kfree_sensitive(server->session_key.response); |
| 322 |
server->session_key.response = NULL; |
| 323 |
server->session_key.len = 0; |
| 324 |
server->lstrp = jiffies; |
| 325 |
|
| 326 |
/* mark submitted MIDs for retry and issue callback */ |
| 327 |
INIT_LIST_HEAD(&retry_list); |
| 328 |
cifs_dbg(FYI, "%s: moving mids to private list\n", __func__); |
| 329 |
spin_lock(&server->mid_queue_lock); |
| 330 |
list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) { |
| 331 |
smb_get_mid(mid); |
| 332 |
if (mid->mid_state == MID_REQUEST_SUBMITTED) |
| 333 |
mid->mid_state = MID_RETRY_NEEDED; |
| 334 |
list_move(&mid->qhead, &retry_list); |
| 335 |
mid->deleted_from_q = true; |
| 336 |
} |
| 337 |
spin_unlock(&server->mid_queue_lock); |
| 338 |
cifs_server_unlock(server); |
| 339 |
|
| 340 |
cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__); |
| 341 |
list_for_each_entry_safe(mid, nmid, &retry_list, qhead) { |
| 342 |
list_del_init(&mid->qhead); |
| 343 |
mid_execute_callback(server, mid); |
| 344 |
release_mid(server, mid); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets) |
| 349 |
{ |
| 350 |
spin_lock(&server->srv_lock); |
| 351 |
server->nr_targets = num_targets; |
| 352 |
if (server->tcpStatus == CifsExiting) { |
| 353 |
/* the demux thread will exit normally next time through the loop */ |
| 354 |
spin_unlock(&server->srv_lock); |
| 355 |
wake_up(&server->response_q); |
| 356 |
return false; |
| 357 |
} |
| 358 |
|
| 359 |
cifs_dbg(FYI, "Mark tcp session as need reconnect\n"); |
| 360 |
trace_smb3_reconnect(server->current_mid, server->conn_id, |
| 361 |
server->hostname); |
| 362 |
server->tcpStatus = CifsNeedReconnect; |
| 363 |
|
| 364 |
spin_unlock(&server->srv_lock); |
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
/* |
| 369 |
* cifs tcp session reconnection |
| 370 |
* |
| 371 |
* mark tcp session as reconnecting so temporarily locked |
| 372 |
* mark all smb sessions as reconnecting for tcp session |
| 373 |
* reconnect tcp session |
| 374 |
* wake up waiters on reconnection? - (not needed currently) |
| 375 |
* |
| 376 |
* if mark_smb_session is passed as true, unconditionally mark |
| 377 |
* the smb session (and tcon) for reconnect as well. This value |
| 378 |
* doesn't really matter for non-multichannel scenario. |
| 379 |
* |
| 380 |
*/ |
| 381 |
static int __cifs_reconnect(struct TCP_Server_Info *server, |
| 382 |
bool mark_smb_session, bool once) |
| 383 |
{ |
| 384 |
int rc = 0; |
| 385 |
|
| 386 |
if (!cifs_tcp_ses_needs_reconnect(server, 1)) |
| 387 |
return 0; |
| 388 |
|
| 389 |
/* |
| 390 |
* if smb session has been marked for reconnect, also reconnect all |
| 391 |
* connections. This way, the other connections do not end up bad. |
| 392 |
*/ |
| 393 |
if (mark_smb_session) |
| 394 |
cifs_signal_cifsd_for_reconnect(server, mark_smb_session); |
| 395 |
|
| 396 |
cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session); |
| 397 |
|
| 398 |
cifs_abort_connection(server); |
| 399 |
|
| 400 |
do { |
| 401 |
try_to_freeze(); |
| 402 |
cifs_server_lock(server); |
| 403 |
|
| 404 |
if (!cifs_swn_set_server_dstaddr(server) && |
| 405 |
!SERVER_IS_CHAN(server)) { |
| 406 |
/* resolve the hostname again to make sure that IP address is up-to-date */ |
| 407 |
rc = reconn_set_ipaddr_from_hostname(server); |
| 408 |
cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc); |
| 409 |
} |
| 410 |
|
| 411 |
if (cifs_rdma_enabled(server)) |
| 412 |
rc = smbd_reconnect(server); |
| 413 |
else |
| 414 |
rc = generic_ip_connect(server); |
| 415 |
if (rc) { |
| 416 |
cifs_server_unlock(server); |
| 417 |
cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); |
| 418 |
/* If was asked to reconnect only once, do not try it more times */ |
| 419 |
if (once) |
| 420 |
break; |
| 421 |
msleep(3000); |
| 422 |
} else { |
| 423 |
atomic_inc(&tcpSesReconnectCount); |
| 424 |
set_credits(server, 1); |
| 425 |
spin_lock(&server->srv_lock); |
| 426 |
if (server->tcpStatus != CifsExiting) |
| 427 |
server->tcpStatus = CifsNeedNegotiate; |
| 428 |
spin_unlock(&server->srv_lock); |
| 429 |
cifs_swn_reset_server_dstaddr(server); |
| 430 |
cifs_server_unlock(server); |
| 431 |
cifs_queue_server_reconn(server); |
| 432 |
} |
| 433 |
} while (server->tcpStatus == CifsNeedReconnect); |
| 434 |
|
| 435 |
spin_lock(&server->srv_lock); |
| 436 |
if (server->tcpStatus == CifsNeedNegotiate) |
| 437 |
mod_delayed_work(cifsiod_wq, &server->echo, 0); |
| 438 |
spin_unlock(&server->srv_lock); |
| 439 |
|
| 440 |
wake_up(&server->response_q); |
| 441 |
return rc; |
| 442 |
} |
| 443 |
|
| 444 |
#ifdef CONFIG_CIFS_DFS_UPCALL |
| 445 |
static int __reconnect_target_locked(struct TCP_Server_Info *server, |
| 446 |
const char *target) |
| 447 |
{ |
| 448 |
int rc; |
| 449 |
char *hostname; |
| 450 |
|
| 451 |
if (!cifs_swn_set_server_dstaddr(server)) { |
| 452 |
if (server->hostname != target) { |
| 453 |
hostname = extract_hostname(target); |
| 454 |
if (!IS_ERR(hostname)) { |
| 455 |
spin_lock(&server->srv_lock); |
| 456 |
kfree(server->hostname); |
| 457 |
server->hostname = hostname; |
| 458 |
spin_unlock(&server->srv_lock); |
| 459 |
} else { |
| 460 |
cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n", |
| 461 |
__func__, PTR_ERR(hostname)); |
| 462 |
cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__, |
| 463 |
server->hostname); |
| 464 |
} |
| 465 |
} |
| 466 |
/* resolve the hostname again to make sure that IP address is up-to-date. */ |
| 467 |
rc = reconn_set_ipaddr_from_hostname(server); |
| 468 |
cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc); |
| 469 |
} |
| 470 |
/* Reconnect the socket */ |
| 471 |
if (cifs_rdma_enabled(server)) |
| 472 |
rc = smbd_reconnect(server); |
| 473 |
else |
| 474 |
rc = generic_ip_connect(server); |
| 475 |
|
| 476 |
return rc; |
| 477 |
} |
| 478 |
|
| 479 |
static int reconnect_target_locked(struct TCP_Server_Info *server, |
| 480 |
struct dfs_cache_tgt_list *tl, |
| 481 |
struct dfs_cache_tgt_iterator **target_hint) |
| 482 |
{ |
| 483 |
struct dfs_cache_tgt_iterator *tit; |
| 484 |
int rc; |
| 485 |
|
| 486 |
*target_hint = NULL; |
| 487 |
|
| 488 |
/* If dfs target list is empty, then reconnect to last server */ |
| 489 |
tit = dfs_cache_get_tgt_iterator(tl); |
| 490 |
if (!tit) |
| 491 |
return __reconnect_target_locked(server, server->hostname); |
| 492 |
|
| 493 |
/* Otherwise, try every dfs target in @tl */ |
| 494 |
do { |
| 495 |
const char *target = dfs_cache_get_tgt_name(tit); |
| 496 |
|
| 497 |
spin_lock(&server->srv_lock); |
| 498 |
if (server->tcpStatus != CifsNeedReconnect) { |
| 499 |
spin_unlock(&server->srv_lock); |
| 500 |
return -ECONNRESET; |
| 501 |
} |
| 502 |
spin_unlock(&server->srv_lock); |
| 503 |
rc = __reconnect_target_locked(server, target); |
| 504 |
if (!rc) { |
| 505 |
*target_hint = tit; |
| 506 |
break; |
| 507 |
} |
| 508 |
} while ((tit = dfs_cache_get_next_tgt(tl, tit))); |
| 509 |
return rc; |
| 510 |
} |
| 511 |
|
| 512 |
static int reconnect_dfs_server(struct TCP_Server_Info *server) |
| 513 |
{ |
| 514 |
struct dfs_cache_tgt_iterator *target_hint = NULL; |
| 515 |
const char *ref_path = server->leaf_fullpath + 1; |
| 516 |
DFS_CACHE_TGT_LIST(tl); |
| 517 |
int num_targets = 0; |
| 518 |
int rc = 0; |
| 519 |
|
| 520 |
/* |
| 521 |
* Determine the number of dfs targets the referral path in @cifs_sb resolves to. |
| 522 |
* |
| 523 |
* smb2_reconnect() needs to know how long it should wait based upon the number of dfs |
| 524 |
* targets (server->nr_targets). It's also possible that the cached referral was cleared |
| 525 |
* through /proc/fs/cifs/dfscache or the target list is empty due to server settings after |
| 526 |
* refreshing the referral, so, in this case, default it to 1. |
| 527 |
*/ |
| 528 |
if (!dfs_cache_noreq_find(ref_path, NULL, &tl)) |
| 529 |
num_targets = dfs_cache_get_nr_tgts(&tl); |
| 530 |
if (!num_targets) |
| 531 |
num_targets = 1; |
| 532 |
|
| 533 |
if (!cifs_tcp_ses_needs_reconnect(server, num_targets)) |
| 534 |
return 0; |
| 535 |
|
| 536 |
/* |
| 537 |
* Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a |
| 538 |
* different server or share during failover. It could be improved by adding some logic to |
| 539 |
* only do that in case it connects to a different server or share, though. |
| 540 |
*/ |
| 541 |
cifs_mark_tcp_ses_conns_for_reconnect(server, true); |
| 542 |
|
| 543 |
cifs_abort_connection(server); |
| 544 |
|
| 545 |
do { |
| 546 |
try_to_freeze(); |
| 547 |
cifs_server_lock(server); |
| 548 |
|
| 549 |
rc = reconnect_target_locked(server, &tl, &target_hint); |
| 550 |
if (rc) { |
| 551 |
/* Failed to reconnect socket */ |
| 552 |
cifs_server_unlock(server); |
| 553 |
cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); |
| 554 |
msleep(3000); |
| 555 |
continue; |
| 556 |
} |
| 557 |
/* |
| 558 |
* Socket was created. Update tcp session status to CifsNeedNegotiate so that a |
| 559 |
* process waiting for reconnect will know it needs to re-establish session and tcon |
| 560 |
* through the reconnected target server. |
| 561 |
*/ |
| 562 |
atomic_inc(&tcpSesReconnectCount); |
| 563 |
set_credits(server, 1); |
| 564 |
spin_lock(&server->srv_lock); |
| 565 |
if (server->tcpStatus != CifsExiting) |
| 566 |
server->tcpStatus = CifsNeedNegotiate; |
| 567 |
spin_unlock(&server->srv_lock); |
| 568 |
cifs_swn_reset_server_dstaddr(server); |
| 569 |
cifs_server_unlock(server); |
| 570 |
cifs_queue_server_reconn(server); |
| 571 |
} while (server->tcpStatus == CifsNeedReconnect); |
| 572 |
|
| 573 |
dfs_cache_noreq_update_tgthint(ref_path, target_hint); |
| 574 |
dfs_cache_free_tgts(&tl); |
| 575 |
|
| 576 |
/* Need to set up echo worker again once connection has been established */ |
| 577 |
spin_lock(&server->srv_lock); |
| 578 |
if (server->tcpStatus == CifsNeedNegotiate) |
| 579 |
mod_delayed_work(cifsiod_wq, &server->echo, 0); |
| 580 |
spin_unlock(&server->srv_lock); |
| 581 |
|
| 582 |
wake_up(&server->response_q); |
| 583 |
return rc; |
| 584 |
} |
| 585 |
|
| 586 |
static int |
| 587 |
_cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session, bool once) |
| 588 |
{ |
| 589 |
if (!server->leaf_fullpath) |
| 590 |
return __cifs_reconnect(server, mark_smb_session, once); |
| 591 |
return reconnect_dfs_server(server); |
| 592 |
} |
| 593 |
#else |
| 594 |
static int |
| 595 |
_cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session, bool once) |
| 596 |
{ |
| 597 |
return __cifs_reconnect(server, mark_smb_session, once); |
| 598 |
} |
| 599 |
#endif |
| 600 |
|
| 601 |
int |
| 602 |
cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session) |
| 603 |
{ |
| 604 |
return _cifs_reconnect(server, mark_smb_session, false); |
| 605 |
} |
| 606 |
|
| 607 |
static int |
| 608 |
cifs_reconnect_once(struct TCP_Server_Info *server) |
| 609 |
{ |
| 610 |
return _cifs_reconnect(server, true, true); |
| 611 |
} |
| 612 |
|
| 613 |
static void |
| 614 |
cifs_echo_request(struct work_struct *work) |
| 615 |
{ |
| 616 |
int rc; |
| 617 |
struct TCP_Server_Info *server = container_of(work, |
| 618 |
struct TCP_Server_Info, echo.work); |
| 619 |
|
| 620 |
/* |
| 621 |
* We cannot send an echo if it is disabled. |
| 622 |
* Also, no need to ping if we got a response recently. |
| 623 |
*/ |
| 624 |
|
| 625 |
if (server->tcpStatus == CifsNeedReconnect || |
| 626 |
server->tcpStatus == CifsExiting || |
| 627 |
server->tcpStatus == CifsNew || |
| 628 |
(server->ops->can_echo && !server->ops->can_echo(server)) || |
| 629 |
time_before(jiffies, server->lstrp + server->echo_interval - HZ)) |
| 630 |
goto requeue_echo; |
| 631 |
|
| 632 |
rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS; |
| 633 |
cifs_server_dbg(FYI, "send echo request: rc = %d\n", rc); |
| 634 |
|
| 635 |
/* Check witness registrations */ |
| 636 |
cifs_swn_check(); |
| 637 |
|
| 638 |
requeue_echo: |
| 639 |
queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval); |
| 640 |
} |
| 641 |
|
| 642 |
static bool |
| 643 |
allocate_buffers(struct TCP_Server_Info *server) |
| 644 |
{ |
| 645 |
if (!server->bigbuf) { |
| 646 |
server->bigbuf = (char *)cifs_buf_get(); |
| 647 |
if (!server->bigbuf) { |
| 648 |
cifs_server_dbg(VFS, "No memory for large SMB response\n"); |
| 649 |
msleep(3000); |
| 650 |
/* retry will check if exiting */ |
| 651 |
return false; |
| 652 |
} |
| 653 |
} else if (server->large_buf) { |
| 654 |
/* we are reusing a dirty large buf, clear its start */ |
| 655 |
memset(server->bigbuf, 0, HEADER_SIZE(server)); |
| 656 |
} |
| 657 |
|
| 658 |
if (!server->smallbuf) { |
| 659 |
server->smallbuf = (char *)cifs_small_buf_get(); |
| 660 |
if (!server->smallbuf) { |
| 661 |
cifs_server_dbg(VFS, "No memory for SMB response\n"); |
| 662 |
msleep(1000); |
| 663 |
/* retry will check if exiting */ |
| 664 |
return false; |
| 665 |
} |
| 666 |
/* beginning of smb buffer is cleared in our buf_get */ |
| 667 |
} else { |
| 668 |
/* if existing small buf clear beginning */ |
| 669 |
memset(server->smallbuf, 0, HEADER_SIZE(server)); |
| 670 |
} |
| 671 |
|
| 672 |
return true; |
| 673 |
} |
| 674 |
|
| 675 |
static bool |
| 676 |
server_unresponsive(struct TCP_Server_Info *server) |
| 677 |
{ |
| 678 |
/* |
| 679 |
* If we're in the process of mounting a share or reconnecting a session |
| 680 |
* and the server abruptly shut down (e.g. socket wasn't closed, packet |
| 681 |
* had been ACK'ed but no SMB response), don't wait longer than 20s from |
| 682 |
* when negotiate actually started. |
| 683 |
*/ |
| 684 |
spin_lock(&server->srv_lock); |
| 685 |
if (server->tcpStatus == CifsInNegotiate && |
| 686 |
time_after(jiffies, server->neg_start + 20 * HZ)) { |
| 687 |
spin_unlock(&server->srv_lock); |
| 688 |
cifs_reconnect(server, false); |
| 689 |
return true; |
| 690 |
} |
| 691 |
/* |
| 692 |
* We need to wait 3 echo intervals to make sure we handle such |
| 693 |
* situations right: |
| 694 |
* 1s client sends a normal SMB request |
| 695 |
* 2s client gets a response |
| 696 |
* 30s echo workqueue job pops, and decides we got a response recently |
| 697 |
* and don't need to send another |
| 698 |
* ... |
| 699 |
* 65s kernel_recvmsg times out, and we see that we haven't gotten |
| 700 |
* a response in >60s. |
| 701 |
*/ |
| 702 |
if ((server->tcpStatus == CifsGood || |
| 703 |
server->tcpStatus == CifsNeedNegotiate) && |
| 704 |
(!server->ops->can_echo || server->ops->can_echo(server)) && |
| 705 |
time_after(jiffies, server->lstrp + 3 * server->echo_interval)) { |
| 706 |
spin_unlock(&server->srv_lock); |
| 707 |
cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n", |
| 708 |
(3 * server->echo_interval) / HZ); |
| 709 |
cifs_reconnect(server, false); |
| 710 |
return true; |
| 711 |
} |
| 712 |
spin_unlock(&server->srv_lock); |
| 713 |
|
| 714 |
return false; |
| 715 |
} |
| 716 |
|
| 717 |
static inline bool |
| 718 |
zero_credits(struct TCP_Server_Info *server) |
| 719 |
{ |
| 720 |
int val; |
| 721 |
|
| 722 |
spin_lock(&server->req_lock); |
| 723 |
val = server->credits + server->echo_credits + server->oplock_credits; |
| 724 |
if (server->in_flight == 0 && val == 0) { |
| 725 |
spin_unlock(&server->req_lock); |
| 726 |
return true; |
| 727 |
} |
| 728 |
spin_unlock(&server->req_lock); |
| 729 |
return false; |
| 730 |
} |
| 731 |
|
| 732 |
static int |
| 733 |
cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg) |
| 734 |
{ |
| 735 |
int length = 0; |
| 736 |
int total_read; |
| 737 |
|
| 738 |
for (total_read = 0; msg_data_left(smb_msg); total_read += length) { |
| 739 |
try_to_freeze(); |
| 740 |
|
| 741 |
/* reconnect if no credits and no requests in flight */ |
| 742 |
if (zero_credits(server)) { |
| 743 |
cifs_reconnect(server, false); |
| 744 |
return -ECONNABORTED; |
| 745 |
} |
| 746 |
|
| 747 |
if (server_unresponsive(server)) |
| 748 |
return -ECONNABORTED; |
| 749 |
if (cifs_rdma_enabled(server) && server->smbd_conn) |
| 750 |
length = smbd_recv(server->smbd_conn, smb_msg); |
| 751 |
else |
| 752 |
length = sock_recvmsg(server->ssocket, smb_msg, 0); |
| 753 |
|
| 754 |
spin_lock(&server->srv_lock); |
| 755 |
if (server->tcpStatus == CifsExiting) { |
| 756 |
spin_unlock(&server->srv_lock); |
| 757 |
return -ESHUTDOWN; |
| 758 |
} |
| 759 |
|
| 760 |
if (server->tcpStatus == CifsNeedReconnect) { |
| 761 |
spin_unlock(&server->srv_lock); |
| 762 |
cifs_reconnect(server, false); |
| 763 |
return -ECONNABORTED; |
| 764 |
} |
| 765 |
spin_unlock(&server->srv_lock); |
| 766 |
|
| 767 |
if (length == -ERESTARTSYS || |
| 768 |
length == -EAGAIN || |
| 769 |
length == -EINTR) { |
| 770 |
/* |
| 771 |
* Minimum sleep to prevent looping, allowing socket |
| 772 |
* to clear and app threads to set tcpStatus |
| 773 |
* CifsNeedReconnect if server hung. |
| 774 |
*/ |
| 775 |
usleep_range(1000, 2000); |
| 776 |
length = 0; |
| 777 |
continue; |
| 778 |
} |
| 779 |
|
| 780 |
if (length <= 0) { |
| 781 |
cifs_dbg(FYI, "Received no data or error: %d\n", length); |
| 782 |
cifs_reconnect(server, false); |
| 783 |
return -ECONNABORTED; |
| 784 |
} |
| 785 |
} |
| 786 |
return total_read; |
| 787 |
} |
| 788 |
|
| 789 |
int |
| 790 |
cifs_read_from_socket(struct TCP_Server_Info *server, char *buf, |
| 791 |
unsigned int to_read) |
| 792 |
{ |
| 793 |
struct msghdr smb_msg = {}; |
| 794 |
struct kvec iov = {.iov_base = buf, .iov_len = to_read}; |
| 795 |
|
| 796 |
iov_iter_kvec(&smb_msg.msg_iter, ITER_DEST, &iov, 1, to_read); |
| 797 |
|
| 798 |
return cifs_readv_from_socket(server, &smb_msg); |
| 799 |
} |
| 800 |
|
| 801 |
ssize_t |
| 802 |
cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read) |
| 803 |
{ |
| 804 |
struct msghdr smb_msg = {}; |
| 805 |
|
| 806 |
/* |
| 807 |
* iov_iter_discard already sets smb_msg.type and count and iov_offset |
| 808 |
* and cifs_readv_from_socket sets msg_control and msg_controllen |
| 809 |
* so little to initialize in struct msghdr |
| 810 |
*/ |
| 811 |
iov_iter_discard(&smb_msg.msg_iter, ITER_DEST, to_read); |
| 812 |
|
| 813 |
return cifs_readv_from_socket(server, &smb_msg); |
| 814 |
} |
| 815 |
|
| 816 |
int |
| 817 |
cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter *iter, |
| 818 |
unsigned int to_read) |
| 819 |
{ |
| 820 |
struct msghdr smb_msg = { .msg_iter = *iter }; |
| 821 |
|
| 822 |
iov_iter_truncate(&smb_msg.msg_iter, to_read); |
| 823 |
return cifs_readv_from_socket(server, &smb_msg); |
| 824 |
} |
| 825 |
|
| 826 |
static bool |
| 827 |
is_smb_response(struct TCP_Server_Info *server, unsigned char type) |
| 828 |
{ |
| 829 |
/* |
| 830 |
* The first byte big endian of the length field, |
| 831 |
* is actually not part of the length but the type |
| 832 |
* with the most common, zero, as regular data. |
| 833 |
*/ |
| 834 |
switch (type) { |
| 835 |
case RFC1002_SESSION_MESSAGE: |
| 836 |
/* Regular SMB response */ |
| 837 |
return true; |
| 838 |
case RFC1002_SESSION_KEEP_ALIVE: |
| 839 |
/* |
| 840 |
* RFC 1002 session keep alive can sent by the server only when |
| 841 |
* we established a RFC 1002 session. But Samba servers send |
| 842 |
* RFC 1002 session keep alive also over port 445 on which |
| 843 |
* RFC 1002 session is not established. |
| 844 |
*/ |
| 845 |
cifs_dbg(FYI, "RFC 1002 session keep alive\n"); |
| 846 |
break; |
| 847 |
case RFC1002_POSITIVE_SESSION_RESPONSE: |
| 848 |
/* |
| 849 |
* RFC 1002 positive session response cannot be returned |
| 850 |
* for SMB request. RFC 1002 session response is handled |
| 851 |
* exclusively in ip_rfc1001_connect() function. |
| 852 |
*/ |
| 853 |
cifs_server_dbg(VFS, "RFC 1002 positive session response (unexpected)\n"); |
| 854 |
cifs_reconnect(server, true); |
| 855 |
break; |
| 856 |
case RFC1002_NEGATIVE_SESSION_RESPONSE: |
| 857 |
/* |
| 858 |
* We get this from Windows 98 instead of an error on |
| 859 |
* SMB negprot response, when we have not established |
| 860 |
* RFC 1002 session (which means ip_rfc1001_connect() |
| 861 |
* was skipped). Note that same still happens with |
| 862 |
* Windows Server 2022 when connecting via port 139. |
| 863 |
* So for this case when mount option -o nonbsessinit |
| 864 |
* was not specified, try to reconnect with establishing |
| 865 |
* RFC 1002 session. If new socket establishment with |
| 866 |
* RFC 1002 session was successful then return to the |
| 867 |
* mid's caller -EAGAIN, so it can retry the request. |
| 868 |
*/ |
| 869 |
if (!cifs_rdma_enabled(server) && |
| 870 |
server->tcpStatus == CifsInNegotiate && |
| 871 |
!server->with_rfc1001 && |
| 872 |
server->rfc1001_sessinit != 0) { |
| 873 |
int rc, mid_rc; |
| 874 |
struct mid_q_entry *mid, *nmid; |
| 875 |
LIST_HEAD(dispose_list); |
| 876 |
|
| 877 |
cifs_dbg(FYI, "RFC 1002 negative session response during SMB Negotiate, retrying with NetBIOS session\n"); |
| 878 |
|
| 879 |
/* |
| 880 |
* Before reconnect, delete all pending mids for this |
| 881 |
* server, so reconnect would not signal connection |
| 882 |
* aborted error to mid's callbacks. Note that for this |
| 883 |
* server there should be exactly one pending mid |
| 884 |
* corresponding to SMB1/SMB2 Negotiate packet. |
| 885 |
*/ |
| 886 |
spin_lock(&server->mid_queue_lock); |
| 887 |
list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) { |
| 888 |
smb_get_mid(mid); |
| 889 |
list_move(&mid->qhead, &dispose_list); |
| 890 |
mid->deleted_from_q = true; |
| 891 |
} |
| 892 |
spin_unlock(&server->mid_queue_lock); |
| 893 |
|
| 894 |
/* Now try to reconnect once with NetBIOS session. */ |
| 895 |
server->with_rfc1001 = true; |
| 896 |
rc = cifs_reconnect_once(server); |
| 897 |
|
| 898 |
/* |
| 899 |
* If reconnect was successful then indicate -EAGAIN |
| 900 |
* to mid's caller. If reconnect failed with -EAGAIN |
| 901 |
* then mask it as -EHOSTDOWN, so mid's caller would |
| 902 |
* know that it failed. |
| 903 |
*/ |
| 904 |
if (rc == 0) |
| 905 |
mid_rc = -EAGAIN; |
| 906 |
else if (rc == -EAGAIN) |
| 907 |
mid_rc = -EHOSTDOWN; |
| 908 |
else |
| 909 |
mid_rc = rc; |
| 910 |
|
| 911 |
/* |
| 912 |
* After reconnect (either successful or unsuccessful) |
| 913 |
* deliver reconnect status to mid's caller via mid's |
| 914 |
* callback. Use MID_RC state which indicates that the |
| 915 |
* return code should be read from mid_rc member. |
| 916 |
*/ |
| 917 |
list_for_each_entry_safe(mid, nmid, &dispose_list, qhead) { |
| 918 |
list_del_init(&mid->qhead); |
| 919 |
mid->mid_rc = mid_rc; |
| 920 |
mid->mid_state = MID_RC; |
| 921 |
mid_execute_callback(server, mid); |
| 922 |
release_mid(server, mid); |
| 923 |
} |
| 924 |
|
| 925 |
/* |
| 926 |
* If reconnect failed then wait two seconds. In most |
| 927 |
* cases we were been called from the mount context and |
| 928 |
* delivered failure to mid's callback will stop this |
| 929 |
* receiver task thread and fails the mount process. |
| 930 |
* So wait two seconds to prevent another reconnect |
| 931 |
* in this task thread, which would be useless as the |
| 932 |
* mount context will fail at all. |
| 933 |
*/ |
| 934 |
if (rc != 0) |
| 935 |
msleep(2000); |
| 936 |
} else { |
| 937 |
cifs_server_dbg(VFS, "RFC 1002 negative session response (unexpected)\n"); |
| 938 |
cifs_reconnect(server, true); |
| 939 |
} |
| 940 |
break; |
| 941 |
case RFC1002_RETARGET_SESSION_RESPONSE: |
| 942 |
cifs_server_dbg(VFS, "RFC 1002 retarget session response (unexpected)\n"); |
| 943 |
cifs_reconnect(server, true); |
| 944 |
break; |
| 945 |
default: |
| 946 |
cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type); |
| 947 |
cifs_reconnect(server, true); |
| 948 |
} |
| 949 |
|
| 950 |
return false; |
| 951 |
} |
| 952 |
|
| 953 |
void |
| 954 |
dequeue_mid(struct TCP_Server_Info *server, struct mid_q_entry *mid, bool malformed) |
| 955 |
{ |
| 956 |
#ifdef CONFIG_CIFS_STATS2 |
| 957 |
mid->when_received = jiffies; |
| 958 |
#endif |
| 959 |
spin_lock(&server->mid_queue_lock); |
| 960 |
if (!malformed) |
| 961 |
mid->mid_state = MID_RESPONSE_RECEIVED; |
| 962 |
else |
| 963 |
mid->mid_state = MID_RESPONSE_MALFORMED; |
| 964 |
/* |
| 965 |
* Trying to handle/dequeue a mid after the send_recv() |
| 966 |
* function has finished processing it is a bug. |
| 967 |
*/ |
| 968 |
if (mid->deleted_from_q == true) { |
| 969 |
spin_unlock(&server->mid_queue_lock); |
| 970 |
pr_warn_once("trying to dequeue a deleted mid\n"); |
| 971 |
} else { |
| 972 |
list_del_init(&mid->qhead); |
| 973 |
mid->deleted_from_q = true; |
| 974 |
spin_unlock(&server->mid_queue_lock); |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
static unsigned int |
| 979 |
smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) |
| 980 |
{ |
| 981 |
struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; |
| 982 |
|
| 983 |
/* |
| 984 |
* SMB1 does not use credits. |
| 985 |
*/ |
| 986 |
if (is_smb1(server)) |
| 987 |
return 0; |
| 988 |
|
| 989 |
return le16_to_cpu(shdr->CreditRequest); |
| 990 |
} |
| 991 |
|
| 992 |
static void |
| 993 |
handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server, |
| 994 |
char *buf, int malformed) |
| 995 |
{ |
| 996 |
if (server->ops->check_trans2 && |
| 997 |
server->ops->check_trans2(mid, server, buf, malformed)) |
| 998 |
return; |
| 999 |
mid->credits_received = smb2_get_credits_from_hdr(buf, server); |
| 1000 |
mid->resp_buf = buf; |
| 1001 |
mid->large_buf = server->large_buf; |
| 1002 |
/* Was previous buf put in mpx struct for multi-rsp? */ |
| 1003 |
if (!mid->multiRsp) { |
| 1004 |
/* smb buffer will be freed by user thread */ |
| 1005 |
if (server->large_buf) |
| 1006 |
server->bigbuf = NULL; |
| 1007 |
else |
| 1008 |
server->smallbuf = NULL; |
| 1009 |
} |
| 1010 |
dequeue_mid(server, mid, malformed); |
| 1011 |
} |
| 1012 |
|
| 1013 |
int |
| 1014 |
cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required) |
| 1015 |
{ |
| 1016 |
bool srv_sign_required = server->sec_mode & server->vals->signing_required; |
| 1017 |
bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled; |
| 1018 |
bool mnt_sign_enabled; |
| 1019 |
|
| 1020 |
/* |
| 1021 |
* Is signing required by mnt options? If not then check |
| 1022 |
* global_secflags to see if it is there. |
| 1023 |
*/ |
| 1024 |
if (!mnt_sign_required) |
| 1025 |
mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) == |
| 1026 |
CIFSSEC_MUST_SIGN); |
| 1027 |
|
| 1028 |
/* |
| 1029 |
* If signing is required then it's automatically enabled too, |
| 1030 |
* otherwise, check to see if the secflags allow it. |
| 1031 |
*/ |
| 1032 |
mnt_sign_enabled = mnt_sign_required ? mnt_sign_required : |
| 1033 |
(global_secflags & CIFSSEC_MAY_SIGN); |
| 1034 |
|
| 1035 |
/* If server requires signing, does client allow it? */ |
| 1036 |
if (srv_sign_required) { |
| 1037 |
if (!mnt_sign_enabled) { |
| 1038 |
cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n"); |
| 1039 |
return -EOPNOTSUPP; |
| 1040 |
} |
| 1041 |
server->sign = true; |
| 1042 |
} |
| 1043 |
|
| 1044 |
/* If client requires signing, does server allow it? */ |
| 1045 |
if (mnt_sign_required) { |
| 1046 |
if (!srv_sign_enabled) { |
| 1047 |
cifs_dbg(VFS, "Server does not support signing!\n"); |
| 1048 |
return -EOPNOTSUPP; |
| 1049 |
} |
| 1050 |
server->sign = true; |
| 1051 |
} |
| 1052 |
|
| 1053 |
if (cifs_rdma_enabled(server) && server->sign) |
| 1054 |
cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n"); |
| 1055 |
|
| 1056 |
return 0; |
| 1057 |
} |
| 1058 |
|
| 1059 |
static noinline_for_stack void |
| 1060 |
clean_demultiplex_info(struct TCP_Server_Info *server) |
| 1061 |
{ |
| 1062 |
int length; |
| 1063 |
|
| 1064 |
/* take it off the list, if it's not already */ |
| 1065 |
spin_lock(&server->srv_lock); |
| 1066 |
list_del_init(&server->tcp_ses_list); |
| 1067 |
spin_unlock(&server->srv_lock); |
| 1068 |
|
| 1069 |
cancel_delayed_work_sync(&server->echo); |
| 1070 |
|
| 1071 |
spin_lock(&server->srv_lock); |
| 1072 |
server->tcpStatus = CifsExiting; |
| 1073 |
spin_unlock(&server->srv_lock); |
| 1074 |
wake_up_all(&server->response_q); |
| 1075 |
|
| 1076 |
/* check if we have blocked requests that need to free */ |
| 1077 |
spin_lock(&server->req_lock); |
| 1078 |
if (server->credits <= 0) |
| 1079 |
server->credits = 1; |
| 1080 |
spin_unlock(&server->req_lock); |
| 1081 |
/* |
| 1082 |
* Although there should not be any requests blocked on this queue it |
| 1083 |
* can not hurt to be paranoid and try to wake up requests that may |
| 1084 |
* haven been blocked when more than 50 at time were on the wire to the |
| 1085 |
* same server - they now will see the session is in exit state and get |
| 1086 |
* out of SendReceive. |
| 1087 |
*/ |
| 1088 |
wake_up_all(&server->request_q); |
| 1089 |
/* give those requests time to exit */ |
| 1090 |
msleep(125); |
| 1091 |
if (cifs_rdma_enabled(server)) |
| 1092 |
smbd_destroy(server); |
| 1093 |
if (server->ssocket) { |
| 1094 |
sock_release(server->ssocket); |
| 1095 |
server->ssocket = NULL; |
| 1096 |
} |
| 1097 |
|
| 1098 |
if (!list_empty(&server->pending_mid_q)) { |
| 1099 |
struct mid_q_entry *mid_entry; |
| 1100 |
struct list_head *tmp, *tmp2; |
| 1101 |
LIST_HEAD(dispose_list); |
| 1102 |
|
| 1103 |
spin_lock(&server->mid_queue_lock); |
| 1104 |
list_for_each_safe(tmp, tmp2, &server->pending_mid_q) { |
| 1105 |
mid_entry = list_entry(tmp, struct mid_q_entry, qhead); |
| 1106 |
cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid); |
| 1107 |
smb_get_mid(mid_entry); |
| 1108 |
mid_entry->mid_state = MID_SHUTDOWN; |
| 1109 |
list_move(&mid_entry->qhead, &dispose_list); |
| 1110 |
mid_entry->deleted_from_q = true; |
| 1111 |
} |
| 1112 |
spin_unlock(&server->mid_queue_lock); |
| 1113 |
|
| 1114 |
/* now walk dispose list and issue callbacks */ |
| 1115 |
list_for_each_safe(tmp, tmp2, &dispose_list) { |
| 1116 |
mid_entry = list_entry(tmp, struct mid_q_entry, qhead); |
| 1117 |
cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid); |
| 1118 |
list_del_init(&mid_entry->qhead); |
| 1119 |
mid_execute_callback(server, mid_entry); |
| 1120 |
release_mid(server, mid_entry); |
| 1121 |
} |
| 1122 |
/* 1/8th of sec is more than enough time for them to exit */ |
| 1123 |
msleep(125); |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (!list_empty(&server->pending_mid_q)) { |
| 1127 |
/* |
| 1128 |
* mpx threads have not exited yet give them at least the smb |
| 1129 |
* send timeout time for long ops. |
| 1130 |
* |
| 1131 |
* Due to delays on oplock break requests, we need to wait at |
| 1132 |
* least 45 seconds before giving up on a request getting a |
| 1133 |
* response and going ahead and killing cifsd. |
| 1134 |
*/ |
| 1135 |
cifs_dbg(FYI, "Wait for exit from demultiplex thread\n"); |
| 1136 |
msleep(46000); |
| 1137 |
/* |
| 1138 |
* If threads still have not exited they are probably never |
| 1139 |
* coming home not much else we can do but free the memory. |
| 1140 |
*/ |
| 1141 |
} |
| 1142 |
|
| 1143 |
put_net(cifs_net_ns(server)); |
| 1144 |
kfree(server->leaf_fullpath); |
| 1145 |
kfree(server->hostname); |
| 1146 |
kfree(server); |
| 1147 |
|
| 1148 |
length = atomic_dec_return(&tcpSesAllocCount); |
| 1149 |
if (length > 0) |
| 1150 |
mempool_resize(cifs_req_poolp, length + cifs_min_rcv); |
| 1151 |
} |
| 1152 |
|
| 1153 |
static int |
| 1154 |
standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 1155 |
{ |
| 1156 |
int length; |
| 1157 |
char *buf = server->smallbuf; |
| 1158 |
unsigned int pdu_length = server->pdu_size; |
| 1159 |
|
| 1160 |
/* make sure this will fit in a large buffer */ |
| 1161 |
if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) { |
| 1162 |
cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length); |
| 1163 |
cifs_reconnect(server, true); |
| 1164 |
return -ECONNABORTED; |
| 1165 |
} |
| 1166 |
|
| 1167 |
/* switch to large buffer if too big for a small one */ |
| 1168 |
if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) { |
| 1169 |
server->large_buf = true; |
| 1170 |
memcpy(server->bigbuf, buf, server->total_read); |
| 1171 |
buf = server->bigbuf; |
| 1172 |
} |
| 1173 |
|
| 1174 |
/* now read the rest */ |
| 1175 |
length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, |
| 1176 |
pdu_length - MID_HEADER_SIZE(server)); |
| 1177 |
|
| 1178 |
if (length < 0) |
| 1179 |
return length; |
| 1180 |
server->total_read += length; |
| 1181 |
|
| 1182 |
dump_smb(buf, server->total_read); |
| 1183 |
|
| 1184 |
return cifs_handle_standard(server, mid); |
| 1185 |
} |
| 1186 |
|
| 1187 |
int |
| 1188 |
cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 1189 |
{ |
| 1190 |
char *buf = server->large_buf ? server->bigbuf : server->smallbuf; |
| 1191 |
int rc; |
| 1192 |
|
| 1193 |
/* |
| 1194 |
* We know that we received enough to get to the MID as we |
| 1195 |
* checked the pdu_length earlier. Now check to see |
| 1196 |
* if the rest of the header is OK. |
| 1197 |
* |
| 1198 |
* 48 bytes is enough to display the header and a little bit |
| 1199 |
* into the payload for debugging purposes. |
| 1200 |
*/ |
| 1201 |
rc = server->ops->check_message(buf, server->pdu_size, |
| 1202 |
server->total_read, server); |
| 1203 |
if (rc) |
| 1204 |
cifs_dump_mem("Bad SMB: ", buf, |
| 1205 |
min_t(unsigned int, server->total_read, 48)); |
| 1206 |
|
| 1207 |
if (server->ops->is_session_expired && |
| 1208 |
server->ops->is_session_expired(buf)) { |
| 1209 |
cifs_reconnect(server, true); |
| 1210 |
return -1; |
| 1211 |
} |
| 1212 |
|
| 1213 |
if (server->ops->is_status_pending && |
| 1214 |
server->ops->is_status_pending(buf, server)) |
| 1215 |
return -1; |
| 1216 |
|
| 1217 |
if (!mid) |
| 1218 |
return rc; |
| 1219 |
|
| 1220 |
handle_mid(mid, server, buf, rc); |
| 1221 |
return 0; |
| 1222 |
} |
| 1223 |
|
| 1224 |
static void |
| 1225 |
smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) |
| 1226 |
{ |
| 1227 |
struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; |
| 1228 |
int scredits, in_flight; |
| 1229 |
|
| 1230 |
/* |
| 1231 |
* SMB1 does not use credits. |
| 1232 |
*/ |
| 1233 |
if (is_smb1(server)) |
| 1234 |
return; |
| 1235 |
|
| 1236 |
if (shdr->CreditRequest) { |
| 1237 |
spin_lock(&server->req_lock); |
| 1238 |
server->credits += le16_to_cpu(shdr->CreditRequest); |
| 1239 |
scredits = server->credits; |
| 1240 |
in_flight = server->in_flight; |
| 1241 |
spin_unlock(&server->req_lock); |
| 1242 |
wake_up(&server->request_q); |
| 1243 |
|
| 1244 |
trace_smb3_hdr_credits(server->current_mid, |
| 1245 |
server->conn_id, server->hostname, scredits, |
| 1246 |
le16_to_cpu(shdr->CreditRequest), in_flight); |
| 1247 |
cifs_server_dbg(FYI, "%s: added %u credits total=%d\n", |
| 1248 |
__func__, le16_to_cpu(shdr->CreditRequest), |
| 1249 |
scredits); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
static int |
| 1255 |
cifs_demultiplex_thread(void *p) |
| 1256 |
{ |
| 1257 |
int i, num_mids, length; |
| 1258 |
struct TCP_Server_Info *server = p; |
| 1259 |
unsigned int pdu_length; |
| 1260 |
unsigned int next_offset; |
| 1261 |
char *buf = NULL; |
| 1262 |
struct task_struct *task_to_wake = NULL; |
| 1263 |
struct mid_q_entry *mids[MAX_COMPOUND]; |
| 1264 |
char *bufs[MAX_COMPOUND]; |
| 1265 |
unsigned int noreclaim_flag, num_io_timeout = 0; |
| 1266 |
bool pending_reconnect = false; |
| 1267 |
|
| 1268 |
noreclaim_flag = memalloc_noreclaim_save(); |
| 1269 |
cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current)); |
| 1270 |
|
| 1271 |
length = atomic_inc_return(&tcpSesAllocCount); |
| 1272 |
if (length > 1) |
| 1273 |
mempool_resize(cifs_req_poolp, length + cifs_min_rcv); |
| 1274 |
|
| 1275 |
set_freezable(); |
| 1276 |
allow_kernel_signal(SIGKILL); |
| 1277 |
while (server->tcpStatus != CifsExiting) { |
| 1278 |
if (try_to_freeze()) |
| 1279 |
continue; |
| 1280 |
|
| 1281 |
if (!allocate_buffers(server)) |
| 1282 |
continue; |
| 1283 |
|
| 1284 |
server->large_buf = false; |
| 1285 |
buf = server->smallbuf; |
| 1286 |
pdu_length = 4; /* enough to get RFC1001 header */ |
| 1287 |
|
| 1288 |
length = cifs_read_from_socket(server, buf, pdu_length); |
| 1289 |
if (length < 0) |
| 1290 |
continue; |
| 1291 |
|
| 1292 |
server->total_read = 0; |
| 1293 |
|
| 1294 |
/* |
| 1295 |
* The right amount was read from socket - 4 bytes, |
| 1296 |
* so we can now interpret the length field. |
| 1297 |
*/ |
| 1298 |
pdu_length = be32_to_cpup(((__be32 *)buf)) & 0xffffff; |
| 1299 |
|
| 1300 |
cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length); |
| 1301 |
if (!is_smb_response(server, buf[0])) |
| 1302 |
continue; |
| 1303 |
|
| 1304 |
pending_reconnect = false; |
| 1305 |
next_pdu: |
| 1306 |
server->pdu_size = pdu_length; |
| 1307 |
|
| 1308 |
/* make sure we have enough to get to the MID */ |
| 1309 |
if (server->pdu_size < MID_HEADER_SIZE(server)) { |
| 1310 |
cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n", |
| 1311 |
server->pdu_size); |
| 1312 |
cifs_reconnect(server, true); |
| 1313 |
continue; |
| 1314 |
} |
| 1315 |
|
| 1316 |
/* read down to the MID */ |
| 1317 |
length = cifs_read_from_socket(server, buf, |
| 1318 |
MID_HEADER_SIZE(server)); |
| 1319 |
if (length < 0) |
| 1320 |
continue; |
| 1321 |
server->total_read += length; |
| 1322 |
|
| 1323 |
if (server->ops->next_header) { |
| 1324 |
if (server->ops->next_header(server, buf, &next_offset)) { |
| 1325 |
cifs_dbg(VFS, "%s: malformed response (next_offset=%u)\n", |
| 1326 |
__func__, next_offset); |
| 1327 |
cifs_reconnect(server, true); |
| 1328 |
continue; |
| 1329 |
} |
| 1330 |
if (next_offset) |
| 1331 |
server->pdu_size = next_offset; |
| 1332 |
} |
| 1333 |
|
| 1334 |
memset(mids, 0, sizeof(mids)); |
| 1335 |
memset(bufs, 0, sizeof(bufs)); |
| 1336 |
num_mids = 0; |
| 1337 |
|
| 1338 |
if (server->ops->is_transform_hdr && |
| 1339 |
server->ops->receive_transform && |
| 1340 |
server->ops->is_transform_hdr(buf)) { |
| 1341 |
length = server->ops->receive_transform(server, |
| 1342 |
mids, |
| 1343 |
bufs, |
| 1344 |
&num_mids); |
| 1345 |
} else { |
| 1346 |
mids[0] = server->ops->find_mid(server, buf); |
| 1347 |
bufs[0] = buf; |
| 1348 |
num_mids = 1; |
| 1349 |
|
| 1350 |
if (mids[0]) |
| 1351 |
mids[0]->response_pdu_len = pdu_length; |
| 1352 |
if (!mids[0] || !mids[0]->receive) |
| 1353 |
length = standard_receive3(server, mids[0]); |
| 1354 |
else |
| 1355 |
length = mids[0]->receive(server, mids[0]); |
| 1356 |
} |
| 1357 |
|
| 1358 |
if (length < 0) { |
| 1359 |
for (i = 0; i < num_mids; i++) |
| 1360 |
if (mids[i]) |
| 1361 |
release_mid(server, mids[i]); |
| 1362 |
continue; |
| 1363 |
} |
| 1364 |
|
| 1365 |
if (server->ops->is_status_io_timeout && |
| 1366 |
server->ops->is_status_io_timeout(buf)) { |
| 1367 |
num_io_timeout++; |
| 1368 |
if (num_io_timeout > MAX_STATUS_IO_TIMEOUT) { |
| 1369 |
cifs_server_dbg(VFS, |
| 1370 |
"Number of request timeouts exceeded %d. Reconnecting", |
| 1371 |
MAX_STATUS_IO_TIMEOUT); |
| 1372 |
|
| 1373 |
pending_reconnect = true; |
| 1374 |
num_io_timeout = 0; |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
server->lstrp = jiffies; |
| 1379 |
|
| 1380 |
for (i = 0; i < num_mids; i++) { |
| 1381 |
if (mids[i] != NULL) { |
| 1382 |
mids[i]->resp_buf_size = server->pdu_size; |
| 1383 |
|
| 1384 |
if (bufs[i] != NULL) { |
| 1385 |
if (server->ops->is_network_name_deleted && |
| 1386 |
server->ops->is_network_name_deleted(bufs[i], |
| 1387 |
server)) { |
| 1388 |
cifs_server_dbg(FYI, |
| 1389 |
"Share deleted. Reconnect needed"); |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
if (!mids[i]->multiRsp || mids[i]->multiEnd) |
| 1394 |
mid_execute_callback(server, mids[i]); |
| 1395 |
|
| 1396 |
release_mid(server, mids[i]); |
| 1397 |
} else if (server->ops->is_oplock_break && |
| 1398 |
server->ops->is_oplock_break(bufs[i], |
| 1399 |
server)) { |
| 1400 |
smb2_add_credits_from_hdr(bufs[i], server); |
| 1401 |
cifs_dbg(FYI, "Received oplock break\n"); |
| 1402 |
} else { |
| 1403 |
cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n", |
| 1404 |
atomic_read(&mid_count)); |
| 1405 |
cifs_dump_mem("Received Data is: ", bufs[i], |
| 1406 |
HEADER_SIZE(server)); |
| 1407 |
smb2_add_credits_from_hdr(bufs[i], server); |
| 1408 |
#ifdef CONFIG_CIFS_DEBUG2 |
| 1409 |
if (server->ops->dump_detail) |
| 1410 |
server->ops->dump_detail(bufs[i], pdu_length, |
| 1411 |
server); |
| 1412 |
cifs_dump_mids(server); |
| 1413 |
#endif /* CIFS_DEBUG2 */ |
| 1414 |
} |
| 1415 |
} |
| 1416 |
|
| 1417 |
if (pdu_length > server->pdu_size) { |
| 1418 |
if (!allocate_buffers(server)) |
| 1419 |
continue; |
| 1420 |
pdu_length -= server->pdu_size; |
| 1421 |
server->total_read = 0; |
| 1422 |
server->large_buf = false; |
| 1423 |
buf = server->smallbuf; |
| 1424 |
goto next_pdu; |
| 1425 |
} |
| 1426 |
|
| 1427 |
/* do this reconnect at the very end after processing all MIDs */ |
| 1428 |
if (pending_reconnect) |
| 1429 |
cifs_reconnect(server, true); |
| 1430 |
|
| 1431 |
} /* end while !EXITING */ |
| 1432 |
|
| 1433 |
/* buffer usually freed in free_mid - need to free it here on exit */ |
| 1434 |
cifs_buf_release(server->bigbuf); |
| 1435 |
if (server->smallbuf) /* no sense logging a debug message if NULL */ |
| 1436 |
cifs_small_buf_release(server->smallbuf); |
| 1437 |
|
| 1438 |
task_to_wake = xchg(&server->tsk, NULL); |
| 1439 |
clean_demultiplex_info(server); |
| 1440 |
|
| 1441 |
/* if server->tsk was NULL then wait for a signal before exiting */ |
| 1442 |
if (!task_to_wake) { |
| 1443 |
set_current_state(TASK_INTERRUPTIBLE); |
| 1444 |
while (!signal_pending(current)) { |
| 1445 |
schedule(); |
| 1446 |
set_current_state(TASK_INTERRUPTIBLE); |
| 1447 |
} |
| 1448 |
set_current_state(TASK_RUNNING); |
| 1449 |
} |
| 1450 |
|
| 1451 |
memalloc_noreclaim_restore(noreclaim_flag); |
| 1452 |
module_put_and_kthread_exit(0); |
| 1453 |
} |
| 1454 |
|
| 1455 |
int |
| 1456 |
cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs) |
| 1457 |
{ |
| 1458 |
struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr; |
| 1459 |
struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs; |
| 1460 |
struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr; |
| 1461 |
struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs; |
| 1462 |
|
| 1463 |
switch (srcaddr->sa_family) { |
| 1464 |
case AF_UNSPEC: |
| 1465 |
switch (rhs->sa_family) { |
| 1466 |
case AF_UNSPEC: |
| 1467 |
return 0; |
| 1468 |
case AF_INET: |
| 1469 |
case AF_INET6: |
| 1470 |
return 1; |
| 1471 |
default: |
| 1472 |
return -1; |
| 1473 |
} |
| 1474 |
case AF_INET: { |
| 1475 |
switch (rhs->sa_family) { |
| 1476 |
case AF_UNSPEC: |
| 1477 |
return -1; |
| 1478 |
case AF_INET: |
| 1479 |
return memcmp(saddr4, vaddr4, |
| 1480 |
sizeof(struct sockaddr_in)); |
| 1481 |
case AF_INET6: |
| 1482 |
return 1; |
| 1483 |
default: |
| 1484 |
return -1; |
| 1485 |
} |
| 1486 |
} |
| 1487 |
case AF_INET6: { |
| 1488 |
switch (rhs->sa_family) { |
| 1489 |
case AF_UNSPEC: |
| 1490 |
case AF_INET: |
| 1491 |
return -1; |
| 1492 |
case AF_INET6: |
| 1493 |
return memcmp(saddr6, |
| 1494 |
vaddr6, |
| 1495 |
sizeof(struct sockaddr_in6)); |
| 1496 |
default: |
| 1497 |
return -1; |
| 1498 |
} |
| 1499 |
} |
| 1500 |
default: |
| 1501 |
return -1; /* don't expect to be here */ |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
/* |
| 1506 |
* Returns true if srcaddr isn't specified and rhs isn't specified, or |
| 1507 |
* if srcaddr is specified and matches the IP address of the rhs argument |
| 1508 |
*/ |
| 1509 |
bool |
| 1510 |
cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs) |
| 1511 |
{ |
| 1512 |
switch (srcaddr->sa_family) { |
| 1513 |
case AF_UNSPEC: |
| 1514 |
return (rhs->sa_family == AF_UNSPEC); |
| 1515 |
case AF_INET: { |
| 1516 |
struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr; |
| 1517 |
struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs; |
| 1518 |
|
| 1519 |
return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr); |
| 1520 |
} |
| 1521 |
case AF_INET6: { |
| 1522 |
struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr; |
| 1523 |
struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs; |
| 1524 |
|
| 1525 |
return (ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr) |
| 1526 |
&& saddr6->sin6_scope_id == vaddr6->sin6_scope_id); |
| 1527 |
} |
| 1528 |
default: |
| 1529 |
WARN_ON(1); |
| 1530 |
return false; /* don't expect to be here */ |
| 1531 |
} |
| 1532 |
} |
| 1533 |
|
| 1534 |
/* |
| 1535 |
* If no port is specified in addr structure, we try to match with 445 port |
| 1536 |
* and if it fails - with 139 ports. It should be called only if address |
| 1537 |
* families of server and addr are equal. |
| 1538 |
*/ |
| 1539 |
static bool |
| 1540 |
match_port(struct TCP_Server_Info *server, struct sockaddr *addr) |
| 1541 |
{ |
| 1542 |
__be16 port, *sport; |
| 1543 |
|
| 1544 |
/* SMBDirect manages its own ports, don't match it here */ |
| 1545 |
if (server->rdma) |
| 1546 |
return true; |
| 1547 |
|
| 1548 |
switch (addr->sa_family) { |
| 1549 |
case AF_INET: |
| 1550 |
sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port; |
| 1551 |
port = ((struct sockaddr_in *) addr)->sin_port; |
| 1552 |
break; |
| 1553 |
case AF_INET6: |
| 1554 |
sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port; |
| 1555 |
port = ((struct sockaddr_in6 *) addr)->sin6_port; |
| 1556 |
break; |
| 1557 |
default: |
| 1558 |
WARN_ON(1); |
| 1559 |
return false; |
| 1560 |
} |
| 1561 |
|
| 1562 |
if (!port) { |
| 1563 |
port = htons(CIFS_PORT); |
| 1564 |
if (port == *sport) |
| 1565 |
return true; |
| 1566 |
|
| 1567 |
port = htons(RFC1001_PORT); |
| 1568 |
} |
| 1569 |
|
| 1570 |
return port == *sport; |
| 1571 |
} |
| 1572 |
|
| 1573 |
static bool match_server_address(struct TCP_Server_Info *server, struct sockaddr *addr) |
| 1574 |
{ |
| 1575 |
if (!cifs_match_ipaddr(addr, (struct sockaddr *)&server->dstaddr)) |
| 1576 |
return false; |
| 1577 |
|
| 1578 |
return true; |
| 1579 |
} |
| 1580 |
|
| 1581 |
static bool |
| 1582 |
match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
| 1583 |
{ |
| 1584 |
/* |
| 1585 |
* The select_sectype function should either return the ctx->sectype |
| 1586 |
* that was specified, or "Unspecified" if that sectype was not |
| 1587 |
* compatible with the given NEGOTIATE request. |
| 1588 |
*/ |
| 1589 |
if (server->ops->select_sectype(server, ctx->sectype) |
| 1590 |
== Unspecified) |
| 1591 |
return false; |
| 1592 |
|
| 1593 |
/* |
| 1594 |
* Now check if signing mode is acceptable. No need to check |
| 1595 |
* global_secflags at this point since if MUST_SIGN is set then |
| 1596 |
* the server->sign had better be too. |
| 1597 |
*/ |
| 1598 |
if (ctx->sign && !server->sign) |
| 1599 |
return false; |
| 1600 |
|
| 1601 |
return true; |
| 1602 |
} |
| 1603 |
|
| 1604 |
/* this function must be called with srv_lock held */ |
| 1605 |
static int match_server(struct TCP_Server_Info *server, |
| 1606 |
struct smb3_fs_context *ctx, |
| 1607 |
bool match_super) |
| 1608 |
{ |
| 1609 |
struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr; |
| 1610 |
|
| 1611 |
lockdep_assert_held(&server->srv_lock); |
| 1612 |
|
| 1613 |
if (ctx->nosharesock) |
| 1614 |
return 0; |
| 1615 |
|
| 1616 |
/* this server does not share socket */ |
| 1617 |
if (server->nosharesock) |
| 1618 |
return 0; |
| 1619 |
|
| 1620 |
if (!match_super && (ctx->dfs_conn || server->dfs_conn)) |
| 1621 |
return 0; |
| 1622 |
|
| 1623 |
/* If multidialect negotiation see if existing sessions match one */ |
| 1624 |
if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) { |
| 1625 |
if (server->vals->protocol_id < SMB30_PROT_ID) |
| 1626 |
return 0; |
| 1627 |
} else if (strcmp(ctx->vals->version_string, |
| 1628 |
SMBDEFAULT_VERSION_STRING) == 0) { |
| 1629 |
if (server->vals->protocol_id < SMB21_PROT_ID) |
| 1630 |
return 0; |
| 1631 |
} else if ((server->vals != ctx->vals) || (server->ops != ctx->ops)) |
| 1632 |
return 0; |
| 1633 |
|
| 1634 |
if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns)) |
| 1635 |
return 0; |
| 1636 |
|
| 1637 |
if (!cifs_match_ipaddr((struct sockaddr *)&ctx->srcaddr, |
| 1638 |
(struct sockaddr *)&server->srcaddr)) |
| 1639 |
return 0; |
| 1640 |
|
| 1641 |
if (strcasecmp(server->hostname, ctx->server_hostname) || |
| 1642 |
!match_server_address(server, addr) || |
| 1643 |
!match_port(server, addr)) |
| 1644 |
return 0; |
| 1645 |
|
| 1646 |
if (!match_security(server, ctx)) |
| 1647 |
return 0; |
| 1648 |
|
| 1649 |
if (server->echo_interval != ctx->echo_interval * HZ) |
| 1650 |
return 0; |
| 1651 |
|
| 1652 |
if (server->rdma != ctx->rdma) |
| 1653 |
return 0; |
| 1654 |
|
| 1655 |
if (server->ignore_signature != ctx->ignore_signature) |
| 1656 |
return 0; |
| 1657 |
|
| 1658 |
if (server->min_offload != ctx->min_offload) |
| 1659 |
return 0; |
| 1660 |
|
| 1661 |
if (server->retrans != ctx->retrans) |
| 1662 |
return 0; |
| 1663 |
|
| 1664 |
return 1; |
| 1665 |
} |
| 1666 |
|
| 1667 |
struct TCP_Server_Info * |
| 1668 |
cifs_find_tcp_session(struct smb3_fs_context *ctx) |
| 1669 |
{ |
| 1670 |
struct TCP_Server_Info *server; |
| 1671 |
|
| 1672 |
spin_lock(&cifs_tcp_ses_lock); |
| 1673 |
list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) { |
| 1674 |
spin_lock(&server->srv_lock); |
| 1675 |
/* |
| 1676 |
* Skip ses channels since they're only handled in lower layers |
| 1677 |
* (e.g. cifs_send_recv). |
| 1678 |
*/ |
| 1679 |
if (SERVER_IS_CHAN(server) || |
| 1680 |
!match_server(server, ctx, false)) { |
| 1681 |
spin_unlock(&server->srv_lock); |
| 1682 |
continue; |
| 1683 |
} |
| 1684 |
spin_unlock(&server->srv_lock); |
| 1685 |
|
| 1686 |
++server->srv_count; |
| 1687 |
spin_unlock(&cifs_tcp_ses_lock); |
| 1688 |
cifs_dbg(FYI, "Existing tcp session with server found\n"); |
| 1689 |
return server; |
| 1690 |
} |
| 1691 |
spin_unlock(&cifs_tcp_ses_lock); |
| 1692 |
return NULL; |
| 1693 |
} |
| 1694 |
|
| 1695 |
void |
| 1696 |
cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect) |
| 1697 |
{ |
| 1698 |
struct task_struct *task; |
| 1699 |
|
| 1700 |
spin_lock(&cifs_tcp_ses_lock); |
| 1701 |
if (--server->srv_count > 0) { |
| 1702 |
spin_unlock(&cifs_tcp_ses_lock); |
| 1703 |
return; |
| 1704 |
} |
| 1705 |
|
| 1706 |
/* srv_count can never go negative */ |
| 1707 |
WARN_ON(server->srv_count < 0); |
| 1708 |
|
| 1709 |
list_del_init(&server->tcp_ses_list); |
| 1710 |
spin_unlock(&cifs_tcp_ses_lock); |
| 1711 |
|
| 1712 |
cancel_delayed_work_sync(&server->echo); |
| 1713 |
|
| 1714 |
if (from_reconnect) |
| 1715 |
/* |
| 1716 |
* Avoid deadlock here: reconnect work calls |
| 1717 |
* cifs_put_tcp_session() at its end. Need to be sure |
| 1718 |
* that reconnect work does nothing with server pointer after |
| 1719 |
* that step. |
| 1720 |
*/ |
| 1721 |
cancel_delayed_work(&server->reconnect); |
| 1722 |
else |
| 1723 |
cancel_delayed_work_sync(&server->reconnect); |
| 1724 |
|
| 1725 |
/* For secondary channels, we pick up ref-count on the primary server */ |
| 1726 |
if (SERVER_IS_CHAN(server)) |
| 1727 |
cifs_put_tcp_session(server->primary_server, from_reconnect); |
| 1728 |
|
| 1729 |
spin_lock(&server->srv_lock); |
| 1730 |
server->tcpStatus = CifsExiting; |
| 1731 |
spin_unlock(&server->srv_lock); |
| 1732 |
|
| 1733 |
cifs_crypto_secmech_release(server); |
| 1734 |
|
| 1735 |
kfree_sensitive(server->session_key.response); |
| 1736 |
server->session_key.response = NULL; |
| 1737 |
server->session_key.len = 0; |
| 1738 |
|
| 1739 |
task = xchg(&server->tsk, NULL); |
| 1740 |
if (task) |
| 1741 |
send_sig(SIGKILL, task, 1); |
| 1742 |
} |
| 1743 |
|
| 1744 |
struct TCP_Server_Info * |
| 1745 |
cifs_get_tcp_session(struct smb3_fs_context *ctx, |
| 1746 |
struct TCP_Server_Info *primary_server) |
| 1747 |
{ |
| 1748 |
struct TCP_Server_Info *tcp_ses = NULL; |
| 1749 |
int rc; |
| 1750 |
|
| 1751 |
cifs_dbg(FYI, "UNC: %s\n", ctx->UNC); |
| 1752 |
|
| 1753 |
/* see if we already have a matching tcp_ses */ |
| 1754 |
tcp_ses = cifs_find_tcp_session(ctx); |
| 1755 |
if (tcp_ses) |
| 1756 |
return tcp_ses; |
| 1757 |
|
| 1758 |
tcp_ses = kzalloc_obj(struct TCP_Server_Info); |
| 1759 |
if (!tcp_ses) { |
| 1760 |
rc = -ENOMEM; |
| 1761 |
goto out_err; |
| 1762 |
} |
| 1763 |
|
| 1764 |
tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL); |
| 1765 |
if (!tcp_ses->hostname) { |
| 1766 |
rc = -ENOMEM; |
| 1767 |
goto out_err; |
| 1768 |
} |
| 1769 |
|
| 1770 |
if (ctx->leaf_fullpath) { |
| 1771 |
tcp_ses->leaf_fullpath = kstrdup(ctx->leaf_fullpath, GFP_KERNEL); |
| 1772 |
if (!tcp_ses->leaf_fullpath) { |
| 1773 |
rc = -ENOMEM; |
| 1774 |
goto out_err; |
| 1775 |
} |
| 1776 |
} |
| 1777 |
if (ctx->dns_dom) |
| 1778 |
strscpy(tcp_ses->dns_dom, ctx->dns_dom); |
| 1779 |
|
| 1780 |
if (ctx->nosharesock) |
| 1781 |
tcp_ses->nosharesock = true; |
| 1782 |
tcp_ses->dfs_conn = ctx->dfs_conn; |
| 1783 |
|
| 1784 |
tcp_ses->ops = ctx->ops; |
| 1785 |
tcp_ses->vals = ctx->vals; |
| 1786 |
cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns)); |
| 1787 |
|
| 1788 |
tcp_ses->sign = ctx->sign; |
| 1789 |
tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId); |
| 1790 |
tcp_ses->noblockcnt = ctx->rootfs; |
| 1791 |
tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs; |
| 1792 |
tcp_ses->noautotune = ctx->noautotune; |
| 1793 |
tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay; |
| 1794 |
tcp_ses->rdma = ctx->rdma; |
| 1795 |
tcp_ses->in_flight = 0; |
| 1796 |
tcp_ses->max_in_flight = 0; |
| 1797 |
tcp_ses->credits = 1; |
| 1798 |
if (primary_server) { |
| 1799 |
spin_lock(&cifs_tcp_ses_lock); |
| 1800 |
++primary_server->srv_count; |
| 1801 |
spin_unlock(&cifs_tcp_ses_lock); |
| 1802 |
tcp_ses->primary_server = primary_server; |
| 1803 |
} |
| 1804 |
init_waitqueue_head(&tcp_ses->response_q); |
| 1805 |
init_waitqueue_head(&tcp_ses->request_q); |
| 1806 |
INIT_LIST_HEAD(&tcp_ses->pending_mid_q); |
| 1807 |
mutex_init(&tcp_ses->_srv_mutex); |
| 1808 |
memcpy(tcp_ses->workstation_RFC1001_name, |
| 1809 |
ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); |
| 1810 |
memcpy(tcp_ses->server_RFC1001_name, |
| 1811 |
ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); |
| 1812 |
tcp_ses->rfc1001_sessinit = ctx->rfc1001_sessinit; |
| 1813 |
tcp_ses->with_rfc1001 = false; |
| 1814 |
tcp_ses->session_estab = false; |
| 1815 |
tcp_ses->sequence_number = 0; |
| 1816 |
tcp_ses->channel_sequence_num = 0; /* only tracked for primary channel */ |
| 1817 |
tcp_ses->reconnect_instance = 1; |
| 1818 |
tcp_ses->lstrp = jiffies; |
| 1819 |
tcp_ses->compression.requested = ctx->compress; |
| 1820 |
spin_lock_init(&tcp_ses->req_lock); |
| 1821 |
spin_lock_init(&tcp_ses->srv_lock); |
| 1822 |
spin_lock_init(&tcp_ses->mid_queue_lock); |
| 1823 |
spin_lock_init(&tcp_ses->mid_counter_lock); |
| 1824 |
INIT_LIST_HEAD(&tcp_ses->tcp_ses_list); |
| 1825 |
INIT_LIST_HEAD(&tcp_ses->smb_ses_list); |
| 1826 |
INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request); |
| 1827 |
INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server); |
| 1828 |
mutex_init(&tcp_ses->reconnect_mutex); |
| 1829 |
memcpy(&tcp_ses->srcaddr, &ctx->srcaddr, |
| 1830 |
sizeof(tcp_ses->srcaddr)); |
| 1831 |
memcpy(&tcp_ses->dstaddr, &ctx->dstaddr, |
| 1832 |
sizeof(tcp_ses->dstaddr)); |
| 1833 |
if (ctx->use_client_guid) |
| 1834 |
memcpy(tcp_ses->client_guid, ctx->client_guid, |
| 1835 |
SMB2_CLIENT_GUID_SIZE); |
| 1836 |
else |
| 1837 |
generate_random_uuid(tcp_ses->client_guid); |
| 1838 |
/* |
| 1839 |
* at this point we are the only ones with the pointer |
| 1840 |
* to the struct since the kernel thread not created yet |
| 1841 |
* no need to spinlock this init of tcpStatus or srv_count |
| 1842 |
*/ |
| 1843 |
tcp_ses->tcpStatus = CifsNew; |
| 1844 |
++tcp_ses->srv_count; |
| 1845 |
tcp_ses->echo_interval = ctx->echo_interval * HZ; |
| 1846 |
|
| 1847 |
if (tcp_ses->rdma) { |
| 1848 |
#ifndef CONFIG_CIFS_SMB_DIRECT |
| 1849 |
cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n"); |
| 1850 |
rc = -ENOENT; |
| 1851 |
goto out_err_crypto_release; |
| 1852 |
#endif |
| 1853 |
tcp_ses->smbd_conn = smbd_get_connection( |
| 1854 |
tcp_ses, (struct sockaddr *)&ctx->dstaddr); |
| 1855 |
if (tcp_ses->smbd_conn) { |
| 1856 |
cifs_dbg(VFS, "RDMA transport established\n"); |
| 1857 |
rc = 0; |
| 1858 |
goto smbd_connected; |
| 1859 |
} else { |
| 1860 |
rc = -ENOENT; |
| 1861 |
goto out_err_crypto_release; |
| 1862 |
} |
| 1863 |
} |
| 1864 |
rc = ip_connect(tcp_ses); |
| 1865 |
if (rc < 0) { |
| 1866 |
cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n"); |
| 1867 |
goto out_err_crypto_release; |
| 1868 |
} |
| 1869 |
smbd_connected: |
| 1870 |
/* |
| 1871 |
* since we're in a cifs function already, we know that |
| 1872 |
* this will succeed. No need for try_module_get(). |
| 1873 |
*/ |
| 1874 |
__module_get(THIS_MODULE); |
| 1875 |
tcp_ses->tsk = kthread_run(cifs_demultiplex_thread, |
| 1876 |
tcp_ses, "cifsd"); |
| 1877 |
if (IS_ERR(tcp_ses->tsk)) { |
| 1878 |
rc = PTR_ERR(tcp_ses->tsk); |
| 1879 |
cifs_dbg(VFS, "error %d create cifsd thread\n", rc); |
| 1880 |
module_put(THIS_MODULE); |
| 1881 |
goto out_err_crypto_release; |
| 1882 |
} |
| 1883 |
tcp_ses->min_offload = ctx->min_offload; |
| 1884 |
tcp_ses->retrans = ctx->retrans; |
| 1885 |
/* |
| 1886 |
* at this point we are the only ones with the pointer |
| 1887 |
* to the struct since the kernel thread not created yet |
| 1888 |
* no need to spinlock this update of tcpStatus |
| 1889 |
*/ |
| 1890 |
spin_lock(&tcp_ses->srv_lock); |
| 1891 |
tcp_ses->tcpStatus = CifsNeedNegotiate; |
| 1892 |
spin_unlock(&tcp_ses->srv_lock); |
| 1893 |
|
| 1894 |
if ((ctx->max_credits < 20) || (ctx->max_credits > 60000)) |
| 1895 |
tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE; |
| 1896 |
else |
| 1897 |
tcp_ses->max_credits = ctx->max_credits; |
| 1898 |
|
| 1899 |
tcp_ses->nr_targets = 1; |
| 1900 |
tcp_ses->ignore_signature = ctx->ignore_signature; |
| 1901 |
/* thread spawned, put it on the list */ |
| 1902 |
spin_lock(&cifs_tcp_ses_lock); |
| 1903 |
list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list); |
| 1904 |
spin_unlock(&cifs_tcp_ses_lock); |
| 1905 |
|
| 1906 |
/* queue echo request delayed work */ |
| 1907 |
queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval); |
| 1908 |
|
| 1909 |
return tcp_ses; |
| 1910 |
|
| 1911 |
out_err_crypto_release: |
| 1912 |
cifs_crypto_secmech_release(tcp_ses); |
| 1913 |
|
| 1914 |
put_net(cifs_net_ns(tcp_ses)); |
| 1915 |
|
| 1916 |
out_err: |
| 1917 |
if (tcp_ses) { |
| 1918 |
if (SERVER_IS_CHAN(tcp_ses)) |
| 1919 |
cifs_put_tcp_session(tcp_ses->primary_server, false); |
| 1920 |
kfree(tcp_ses->hostname); |
| 1921 |
kfree(tcp_ses->leaf_fullpath); |
| 1922 |
if (tcp_ses->ssocket) |
| 1923 |
sock_release(tcp_ses->ssocket); |
| 1924 |
kfree(tcp_ses); |
| 1925 |
} |
| 1926 |
return ERR_PTR(rc); |
| 1927 |
} |
| 1928 |
|
| 1929 |
/* this function must be called with ses_lock and chan_lock held */ |
| 1930 |
static int match_session(struct cifs_ses *ses, |
| 1931 |
struct smb3_fs_context *ctx, |
| 1932 |
bool match_super) |
| 1933 |
{ |
| 1934 |
struct TCP_Server_Info *server = ses->server; |
| 1935 |
enum securityEnum ctx_sec, ses_sec; |
| 1936 |
|
| 1937 |
if (!match_super && ctx->dfs_root_ses != ses->dfs_root_ses) |
| 1938 |
return 0; |
| 1939 |
|
| 1940 |
/* |
| 1941 |
* If an existing session is limited to less channels than |
| 1942 |
* requested, it should not be reused |
| 1943 |
*/ |
| 1944 |
if (ses->chan_max < ctx->max_channels) |
| 1945 |
return 0; |
| 1946 |
|
| 1947 |
ctx_sec = server->ops->select_sectype(server, ctx->sectype); |
| 1948 |
ses_sec = server->ops->select_sectype(server, ses->sectype); |
| 1949 |
|
| 1950 |
if (ctx_sec != ses_sec) |
| 1951 |
return 0; |
| 1952 |
|
| 1953 |
switch (ctx_sec) { |
| 1954 |
case IAKerb: |
| 1955 |
case Kerberos: |
| 1956 |
if (!uid_eq(ctx->cred_uid, ses->cred_uid)) |
| 1957 |
return 0; |
| 1958 |
if (strncmp(ses->user_name ?: "", |
| 1959 |
ctx->username ?: "", |
| 1960 |
CIFS_MAX_USERNAME_LEN)) |
| 1961 |
return 0; |
| 1962 |
break; |
| 1963 |
case NTLMv2: |
| 1964 |
case RawNTLMSSP: |
| 1965 |
default: |
| 1966 |
/* NULL username means anonymous session */ |
| 1967 |
if (ses->user_name == NULL) { |
| 1968 |
if (!ctx->nullauth) |
| 1969 |
return 0; |
| 1970 |
break; |
| 1971 |
} |
| 1972 |
|
| 1973 |
/* anything else takes username/password */ |
| 1974 |
if (strncmp(ses->user_name, |
| 1975 |
ctx->username ? ctx->username : "", |
| 1976 |
CIFS_MAX_USERNAME_LEN)) |
| 1977 |
return 0; |
| 1978 |
if ((ctx->username && strlen(ctx->username) != 0) && |
| 1979 |
ses->password != NULL) { |
| 1980 |
|
| 1981 |
/* New mount can only share sessions with an existing mount if: |
| 1982 |
* 1. Both password and password2 match, or |
| 1983 |
* 2. password2 of the old mount matches password of the new mount |
| 1984 |
* and password of the old mount matches password2 of the new |
| 1985 |
* mount |
| 1986 |
*/ |
| 1987 |
if (ses->password2 != NULL && ctx->password2 != NULL) { |
| 1988 |
if (!((strncmp(ses->password, ctx->password ? |
| 1989 |
ctx->password : "", CIFS_MAX_PASSWORD_LEN) == 0 && |
| 1990 |
strncmp(ses->password2, ctx->password2, |
| 1991 |
CIFS_MAX_PASSWORD_LEN) == 0) || |
| 1992 |
(strncmp(ses->password, ctx->password2, |
| 1993 |
CIFS_MAX_PASSWORD_LEN) == 0 && |
| 1994 |
strncmp(ses->password2, ctx->password ? |
| 1995 |
ctx->password : "", CIFS_MAX_PASSWORD_LEN) == 0))) |
| 1996 |
return 0; |
| 1997 |
|
| 1998 |
} else if ((ses->password2 == NULL && ctx->password2 != NULL) || |
| 1999 |
(ses->password2 != NULL && ctx->password2 == NULL)) { |
| 2000 |
return 0; |
| 2001 |
|
| 2002 |
} else { |
| 2003 |
if (strncmp(ses->password, ctx->password ? |
| 2004 |
ctx->password : "", CIFS_MAX_PASSWORD_LEN)) |
| 2005 |
return 0; |
| 2006 |
} |
| 2007 |
} |
| 2008 |
} |
| 2009 |
|
| 2010 |
if (strcmp(ctx->local_nls->charset, ses->local_nls->charset)) |
| 2011 |
return 0; |
| 2012 |
|
| 2013 |
return 1; |
| 2014 |
} |
| 2015 |
|
| 2016 |
/** |
| 2017 |
* cifs_setup_ipc - helper to setup the IPC tcon for the session |
| 2018 |
* @ses: smb session to issue the request on |
| 2019 |
* @seal: if encryption is requested |
| 2020 |
* |
| 2021 |
* A new IPC connection is made and stored in the session |
| 2022 |
* tcon_ipc. The IPC tcon has the same lifetime as the session. |
| 2023 |
*/ |
| 2024 |
struct cifs_tcon *cifs_setup_ipc(struct cifs_ses *ses, bool seal) |
| 2025 |
{ |
| 2026 |
int rc = 0, xid; |
| 2027 |
struct cifs_tcon *tcon; |
| 2028 |
char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0}; |
| 2029 |
struct TCP_Server_Info *server = ses->server; |
| 2030 |
|
| 2031 |
/* |
| 2032 |
* If the mount request that resulted in the creation of the |
| 2033 |
* session requires encryption, force IPC to be encrypted too. |
| 2034 |
*/ |
| 2035 |
if (seal && !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) { |
| 2036 |
cifs_server_dbg(VFS, "IPC: server doesn't support encryption\n"); |
| 2037 |
return ERR_PTR(-EOPNOTSUPP); |
| 2038 |
} |
| 2039 |
|
| 2040 |
/* no need to setup directory caching on IPC share, so pass in false */ |
| 2041 |
tcon = tcon_info_alloc(false, netfs_trace_tcon_ref_new_ipc); |
| 2042 |
if (tcon == NULL) |
| 2043 |
return ERR_PTR(-ENOMEM); |
| 2044 |
|
| 2045 |
spin_lock(&server->srv_lock); |
| 2046 |
scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname); |
| 2047 |
spin_unlock(&server->srv_lock); |
| 2048 |
|
| 2049 |
xid = get_xid(); |
| 2050 |
tcon->ses = ses; |
| 2051 |
tcon->ipc = true; |
| 2052 |
tcon->seal = seal; |
| 2053 |
rc = server->ops->tree_connect(xid, ses, unc, tcon, ses->local_nls); |
| 2054 |
free_xid(xid); |
| 2055 |
|
| 2056 |
if (rc) { |
| 2057 |
cifs_server_dbg(VFS | ONCE, "failed to connect to IPC (rc=%d)\n", rc); |
| 2058 |
tconInfoFree(tcon, netfs_trace_tcon_ref_free_ipc_fail); |
| 2059 |
return ERR_PTR(rc); |
| 2060 |
} |
| 2061 |
|
| 2062 |
cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid); |
| 2063 |
|
| 2064 |
spin_lock(&tcon->tc_lock); |
| 2065 |
tcon->status = TID_GOOD; |
| 2066 |
spin_unlock(&tcon->tc_lock); |
| 2067 |
return tcon; |
| 2068 |
} |
| 2069 |
|
| 2070 |
static struct cifs_ses * |
| 2071 |
cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
| 2072 |
{ |
| 2073 |
struct cifs_ses *ses, *ret = NULL; |
| 2074 |
|
| 2075 |
spin_lock(&cifs_tcp_ses_lock); |
| 2076 |
list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { |
| 2077 |
spin_lock(&ses->ses_lock); |
| 2078 |
if (ses->ses_status == SES_EXITING) { |
| 2079 |
spin_unlock(&ses->ses_lock); |
| 2080 |
continue; |
| 2081 |
} |
| 2082 |
spin_lock(&ses->chan_lock); |
| 2083 |
if (match_session(ses, ctx, false)) { |
| 2084 |
spin_unlock(&ses->chan_lock); |
| 2085 |
spin_unlock(&ses->ses_lock); |
| 2086 |
ret = ses; |
| 2087 |
break; |
| 2088 |
} |
| 2089 |
spin_unlock(&ses->chan_lock); |
| 2090 |
spin_unlock(&ses->ses_lock); |
| 2091 |
} |
| 2092 |
if (ret) |
| 2093 |
cifs_smb_ses_inc_refcount(ret); |
| 2094 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2095 |
return ret; |
| 2096 |
} |
| 2097 |
|
| 2098 |
void __cifs_put_smb_ses(struct cifs_ses *ses) |
| 2099 |
{ |
| 2100 |
struct TCP_Server_Info *server = ses->server; |
| 2101 |
struct cifs_tcon *tcon; |
| 2102 |
unsigned int xid; |
| 2103 |
size_t i; |
| 2104 |
bool do_logoff; |
| 2105 |
int rc; |
| 2106 |
|
| 2107 |
spin_lock(&cifs_tcp_ses_lock); |
| 2108 |
spin_lock(&ses->ses_lock); |
| 2109 |
cifs_dbg(FYI, "%s: id=0x%llx ses_count=%d ses_status=%u ipc=%s\n", |
| 2110 |
__func__, ses->Suid, ses->ses_count, ses->ses_status, |
| 2111 |
ses->tcon_ipc ? ses->tcon_ipc->tree_name : "none"); |
| 2112 |
if (ses->ses_status == SES_EXITING || --ses->ses_count > 0) { |
| 2113 |
spin_unlock(&ses->ses_lock); |
| 2114 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2115 |
return; |
| 2116 |
} |
| 2117 |
/* ses_count can never go negative */ |
| 2118 |
WARN_ON(ses->ses_count < 0); |
| 2119 |
|
| 2120 |
spin_lock(&ses->chan_lock); |
| 2121 |
cifs_chan_clear_need_reconnect(ses, server); |
| 2122 |
spin_unlock(&ses->chan_lock); |
| 2123 |
|
| 2124 |
do_logoff = ses->ses_status == SES_GOOD && server->ops->logoff; |
| 2125 |
ses->ses_status = SES_EXITING; |
| 2126 |
tcon = ses->tcon_ipc; |
| 2127 |
ses->tcon_ipc = NULL; |
| 2128 |
spin_unlock(&ses->ses_lock); |
| 2129 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2130 |
|
| 2131 |
/* |
| 2132 |
* On session close, the IPC is closed and the server must release all |
| 2133 |
* tcons of the session. No need to send a tree disconnect here. |
| 2134 |
* |
| 2135 |
* Besides, it will make the server to not close durable and resilient |
| 2136 |
* files on session close, as specified in MS-SMB2 3.3.5.6 Receiving an |
| 2137 |
* SMB2 LOGOFF Request. |
| 2138 |
*/ |
| 2139 |
tconInfoFree(tcon, netfs_trace_tcon_ref_free_ipc); |
| 2140 |
if (do_logoff) { |
| 2141 |
xid = get_xid(); |
| 2142 |
rc = server->ops->logoff(xid, ses); |
| 2143 |
cifs_server_dbg(FYI, "%s: Session Logoff: rc=%d\n", |
| 2144 |
__func__, rc); |
| 2145 |
_free_xid(xid); |
| 2146 |
} |
| 2147 |
|
| 2148 |
spin_lock(&cifs_tcp_ses_lock); |
| 2149 |
list_del_init(&ses->smb_ses_list); |
| 2150 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2151 |
|
| 2152 |
/* close any extra channels */ |
| 2153 |
for (i = 1; i < ses->chan_count; i++) { |
| 2154 |
if (ses->chans[i].iface) { |
| 2155 |
kref_put(&ses->chans[i].iface->refcount, release_iface); |
| 2156 |
ses->chans[i].iface = NULL; |
| 2157 |
} |
| 2158 |
cifs_put_tcp_session(ses->chans[i].server, 0); |
| 2159 |
ses->chans[i].server = NULL; |
| 2160 |
} |
| 2161 |
|
| 2162 |
/* we now account for primary channel in iface->refcount */ |
| 2163 |
if (ses->chans[0].iface) { |
| 2164 |
kref_put(&ses->chans[0].iface->refcount, release_iface); |
| 2165 |
ses->chans[0].server = NULL; |
| 2166 |
} |
| 2167 |
|
| 2168 |
sesInfoFree(ses); |
| 2169 |
cifs_put_tcp_session(server, 0); |
| 2170 |
} |
| 2171 |
|
| 2172 |
#ifdef CONFIG_KEYS |
| 2173 |
|
| 2174 |
/* Populate username and pw fields from keyring if possible */ |
| 2175 |
static int |
| 2176 |
cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses) |
| 2177 |
{ |
| 2178 |
int rc = 0; |
| 2179 |
int is_domain = 0; |
| 2180 |
const char *delim, *payload; |
| 2181 |
size_t desc_sz; |
| 2182 |
char *desc; |
| 2183 |
ssize_t len; |
| 2184 |
struct key *key; |
| 2185 |
struct TCP_Server_Info *server = ses->server; |
| 2186 |
struct sockaddr_in *sa; |
| 2187 |
struct sockaddr_in6 *sa6; |
| 2188 |
const struct user_key_payload *upayload; |
| 2189 |
|
| 2190 |
/* "cifs:a:" and "cifs:d:" are the same length; +1 for NUL terminator */ |
| 2191 |
desc_sz = strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1; |
| 2192 |
desc = kmalloc(desc_sz, GFP_KERNEL); |
| 2193 |
if (!desc) |
| 2194 |
return -ENOMEM; |
| 2195 |
|
| 2196 |
/* try to find an address key first */ |
| 2197 |
switch (server->dstaddr.ss_family) { |
| 2198 |
case AF_INET: |
| 2199 |
sa = (struct sockaddr_in *)&server->dstaddr; |
| 2200 |
snprintf(desc, desc_sz, "cifs:a:%pI4", &sa->sin_addr.s_addr); |
| 2201 |
break; |
| 2202 |
case AF_INET6: |
| 2203 |
sa6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 2204 |
snprintf(desc, desc_sz, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr); |
| 2205 |
break; |
| 2206 |
default: |
| 2207 |
cifs_dbg(FYI, "Bad ss_family (%hu)\n", |
| 2208 |
server->dstaddr.ss_family); |
| 2209 |
rc = -EINVAL; |
| 2210 |
goto out_err; |
| 2211 |
} |
| 2212 |
|
| 2213 |
cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); |
| 2214 |
key = request_key(&key_type_logon, desc, ""); |
| 2215 |
if (IS_ERR(key)) { |
| 2216 |
if (!ses->domainName) { |
| 2217 |
cifs_dbg(FYI, "domainName is NULL\n"); |
| 2218 |
rc = PTR_ERR(key); |
| 2219 |
goto out_err; |
| 2220 |
} |
| 2221 |
|
| 2222 |
/* didn't work, try to find a domain key */ |
| 2223 |
snprintf(desc, desc_sz, "cifs:d:%s", ses->domainName); |
| 2224 |
cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); |
| 2225 |
key = request_key(&key_type_logon, desc, ""); |
| 2226 |
if (IS_ERR(key)) { |
| 2227 |
rc = PTR_ERR(key); |
| 2228 |
goto out_err; |
| 2229 |
} |
| 2230 |
is_domain = 1; |
| 2231 |
} |
| 2232 |
|
| 2233 |
down_read(&key->sem); |
| 2234 |
upayload = user_key_payload_locked(key); |
| 2235 |
if (IS_ERR_OR_NULL(upayload)) { |
| 2236 |
rc = upayload ? PTR_ERR(upayload) : -EINVAL; |
| 2237 |
goto out_key_put; |
| 2238 |
} |
| 2239 |
|
| 2240 |
/* find first : in payload */ |
| 2241 |
payload = upayload->data; |
| 2242 |
delim = strnchr(payload, upayload->datalen, ':'); |
| 2243 |
if (!delim) { |
| 2244 |
cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n", |
| 2245 |
upayload->datalen); |
| 2246 |
rc = -EINVAL; |
| 2247 |
goto out_key_put; |
| 2248 |
} |
| 2249 |
|
| 2250 |
len = delim - payload; |
| 2251 |
if (len > CIFS_MAX_USERNAME_LEN || len <= 0) { |
| 2252 |
cifs_dbg(FYI, "Bad value from username search (len=%zd)\n", |
| 2253 |
len); |
| 2254 |
rc = -EINVAL; |
| 2255 |
goto out_key_put; |
| 2256 |
} |
| 2257 |
|
| 2258 |
ctx->username = kstrndup(payload, len, GFP_KERNEL); |
| 2259 |
if (!ctx->username) { |
| 2260 |
cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n", |
| 2261 |
len); |
| 2262 |
rc = -ENOMEM; |
| 2263 |
goto out_key_put; |
| 2264 |
} |
| 2265 |
cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username); |
| 2266 |
|
| 2267 |
len = key->datalen - (len + 1); |
| 2268 |
if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) { |
| 2269 |
cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len); |
| 2270 |
rc = -EINVAL; |
| 2271 |
kfree(ctx->username); |
| 2272 |
ctx->username = NULL; |
| 2273 |
goto out_key_put; |
| 2274 |
} |
| 2275 |
|
| 2276 |
++delim; |
| 2277 |
/* BB consider adding support for password2 (Key Rotation) for multiuser in future */ |
| 2278 |
ctx->password = kstrndup(delim, len, GFP_KERNEL); |
| 2279 |
if (!ctx->password) { |
| 2280 |
cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n", |
| 2281 |
len); |
| 2282 |
rc = -ENOMEM; |
| 2283 |
kfree(ctx->username); |
| 2284 |
ctx->username = NULL; |
| 2285 |
goto out_key_put; |
| 2286 |
} |
| 2287 |
|
| 2288 |
/* |
| 2289 |
* If we have a domain key then we must set the domainName in the |
| 2290 |
* for the request. |
| 2291 |
*/ |
| 2292 |
if (is_domain && ses->domainName) { |
| 2293 |
ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL); |
| 2294 |
if (!ctx->domainname) { |
| 2295 |
cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n", |
| 2296 |
len); |
| 2297 |
rc = -ENOMEM; |
| 2298 |
kfree(ctx->username); |
| 2299 |
ctx->username = NULL; |
| 2300 |
kfree_sensitive(ctx->password); |
| 2301 |
/* no need to free ctx->password2 since not allocated in this path */ |
| 2302 |
ctx->password = NULL; |
| 2303 |
goto out_key_put; |
| 2304 |
} |
| 2305 |
} |
| 2306 |
|
| 2307 |
strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name)); |
| 2308 |
|
| 2309 |
out_key_put: |
| 2310 |
up_read(&key->sem); |
| 2311 |
key_put(key); |
| 2312 |
out_err: |
| 2313 |
kfree(desc); |
| 2314 |
cifs_dbg(FYI, "%s: returning %d\n", __func__, rc); |
| 2315 |
return rc; |
| 2316 |
} |
| 2317 |
#else /* ! CONFIG_KEYS */ |
| 2318 |
static inline int |
| 2319 |
cifs_set_cifscreds(struct smb3_fs_context *ctx __maybe_unused, |
| 2320 |
struct cifs_ses *ses __maybe_unused) |
| 2321 |
{ |
| 2322 |
return -ENOSYS; |
| 2323 |
} |
| 2324 |
#endif /* CONFIG_KEYS */ |
| 2325 |
|
| 2326 |
/** |
| 2327 |
* cifs_get_smb_ses - get a session matching @ctx data from @server |
| 2328 |
* @server: server to setup the session to |
| 2329 |
* @ctx: superblock configuration context to use to setup the session |
| 2330 |
* |
| 2331 |
* This function assumes it is being called from cifs_mount() where we |
| 2332 |
* already got a server reference (server refcount +1). See |
| 2333 |
* cifs_get_tcon() for refcount explanations. |
| 2334 |
*/ |
| 2335 |
struct cifs_ses * |
| 2336 |
cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
| 2337 |
{ |
| 2338 |
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 2339 |
struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; |
| 2340 |
struct cifs_tcon *ipc; |
| 2341 |
struct cifs_ses *ses; |
| 2342 |
unsigned int xid; |
| 2343 |
int retries = 0; |
| 2344 |
size_t len; |
| 2345 |
int rc = 0; |
| 2346 |
|
| 2347 |
xid = get_xid(); |
| 2348 |
|
| 2349 |
ses = cifs_find_smb_ses(server, ctx); |
| 2350 |
if (ses) { |
| 2351 |
cifs_dbg(FYI, "Existing smb sess found (status=%d)\n", |
| 2352 |
ses->ses_status); |
| 2353 |
|
| 2354 |
spin_lock(&ses->chan_lock); |
| 2355 |
if (cifs_chan_needs_reconnect(ses, server)) { |
| 2356 |
spin_unlock(&ses->chan_lock); |
| 2357 |
cifs_dbg(FYI, "Session needs reconnect\n"); |
| 2358 |
|
| 2359 |
mutex_lock(&ses->session_mutex); |
| 2360 |
|
| 2361 |
retry_old_session: |
| 2362 |
rc = cifs_negotiate_protocol(xid, ses, server); |
| 2363 |
if (rc) { |
| 2364 |
mutex_unlock(&ses->session_mutex); |
| 2365 |
/* problem -- put our ses reference */ |
| 2366 |
cifs_put_smb_ses(ses); |
| 2367 |
free_xid(xid); |
| 2368 |
return ERR_PTR(rc); |
| 2369 |
} |
| 2370 |
|
| 2371 |
rc = cifs_setup_session(xid, ses, server, |
| 2372 |
ctx->local_nls); |
| 2373 |
if (rc) { |
| 2374 |
if (((rc == -EACCES) || (rc == -EKEYEXPIRED) || |
| 2375 |
(rc == -EKEYREVOKED)) && !retries && ses->password2) { |
| 2376 |
retries++; |
| 2377 |
cifs_dbg(FYI, "Session reconnect failed, retrying with alternate password\n"); |
| 2378 |
swap(ses->password, ses->password2); |
| 2379 |
goto retry_old_session; |
| 2380 |
} |
| 2381 |
mutex_unlock(&ses->session_mutex); |
| 2382 |
/* problem -- put our reference */ |
| 2383 |
cifs_put_smb_ses(ses); |
| 2384 |
free_xid(xid); |
| 2385 |
return ERR_PTR(rc); |
| 2386 |
} |
| 2387 |
mutex_unlock(&ses->session_mutex); |
| 2388 |
|
| 2389 |
spin_lock(&ses->chan_lock); |
| 2390 |
} |
| 2391 |
spin_unlock(&ses->chan_lock); |
| 2392 |
|
| 2393 |
/* existing SMB ses has a server reference already */ |
| 2394 |
cifs_put_tcp_session(server, 0); |
| 2395 |
free_xid(xid); |
| 2396 |
return ses; |
| 2397 |
} |
| 2398 |
|
| 2399 |
rc = -ENOMEM; |
| 2400 |
|
| 2401 |
cifs_dbg(FYI, "Existing smb sess not found\n"); |
| 2402 |
ses = sesInfoAlloc(); |
| 2403 |
if (ses == NULL) |
| 2404 |
goto get_ses_fail; |
| 2405 |
|
| 2406 |
/* new SMB session uses our server ref */ |
| 2407 |
ses->server = server; |
| 2408 |
if (server->dstaddr.ss_family == AF_INET6) |
| 2409 |
sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr); |
| 2410 |
else |
| 2411 |
sprintf(ses->ip_addr, "%pI4", &addr->sin_addr); |
| 2412 |
|
| 2413 |
if (ctx->username) { |
| 2414 |
ses->user_name = kstrdup(ctx->username, GFP_KERNEL); |
| 2415 |
if (!ses->user_name) |
| 2416 |
goto get_ses_fail; |
| 2417 |
} |
| 2418 |
|
| 2419 |
/* ctx->password freed at unmount */ |
| 2420 |
if (ctx->password) { |
| 2421 |
ses->password = kstrdup(ctx->password, GFP_KERNEL); |
| 2422 |
if (!ses->password) |
| 2423 |
goto get_ses_fail; |
| 2424 |
} |
| 2425 |
/* ctx->password freed at unmount */ |
| 2426 |
if (ctx->password2) { |
| 2427 |
ses->password2 = kstrdup(ctx->password2, GFP_KERNEL); |
| 2428 |
if (!ses->password2) |
| 2429 |
goto get_ses_fail; |
| 2430 |
} |
| 2431 |
if (ctx->domainname) { |
| 2432 |
ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL); |
| 2433 |
if (!ses->domainName) |
| 2434 |
goto get_ses_fail; |
| 2435 |
|
| 2436 |
len = strnlen(ctx->domainname, CIFS_MAX_DOMAINNAME_LEN); |
| 2437 |
if (!cifs_netbios_name(ctx->domainname, len)) { |
| 2438 |
ses->dns_dom = kstrndup(ctx->domainname, |
| 2439 |
len, GFP_KERNEL); |
| 2440 |
if (!ses->dns_dom) |
| 2441 |
goto get_ses_fail; |
| 2442 |
} |
| 2443 |
} |
| 2444 |
|
| 2445 |
strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name)); |
| 2446 |
|
| 2447 |
if (ctx->domainauto) |
| 2448 |
ses->domainAuto = ctx->domainauto; |
| 2449 |
ses->cred_uid = ctx->cred_uid; |
| 2450 |
ses->linux_uid = ctx->linux_uid; |
| 2451 |
|
| 2452 |
ses->unicode = ctx->unicode; |
| 2453 |
ses->sectype = ctx->sectype; |
| 2454 |
ses->sign = ctx->sign; |
| 2455 |
|
| 2456 |
/* |
| 2457 |
*Explicitly marking upcall_target mount option for easier handling |
| 2458 |
* by cifs_spnego.c and eventually cifs.upcall.c |
| 2459 |
*/ |
| 2460 |
|
| 2461 |
switch (ctx->upcall_target) { |
| 2462 |
case UPTARGET_UNSPECIFIED: /* default to app */ |
| 2463 |
case UPTARGET_APP: |
| 2464 |
ses->upcall_target = UPTARGET_APP; |
| 2465 |
break; |
| 2466 |
case UPTARGET_MOUNT: |
| 2467 |
ses->upcall_target = UPTARGET_MOUNT; |
| 2468 |
break; |
| 2469 |
default: |
| 2470 |
// should never happen |
| 2471 |
ses->upcall_target = UPTARGET_APP; |
| 2472 |
break; |
| 2473 |
} |
| 2474 |
|
| 2475 |
ses->local_nls = load_nls(ctx->local_nls->charset); |
| 2476 |
|
| 2477 |
/* add server as first channel */ |
| 2478 |
spin_lock(&ses->chan_lock); |
| 2479 |
ses->chans[0].server = server; |
| 2480 |
ses->chan_count = 1; |
| 2481 |
ses->chan_max = ctx->multichannel ? ctx->max_channels:1; |
| 2482 |
ses->chans_need_reconnect = 1; |
| 2483 |
spin_unlock(&ses->chan_lock); |
| 2484 |
|
| 2485 |
retry_new_session: |
| 2486 |
mutex_lock(&ses->session_mutex); |
| 2487 |
rc = cifs_negotiate_protocol(xid, ses, server); |
| 2488 |
if (!rc) |
| 2489 |
rc = cifs_setup_session(xid, ses, server, ctx->local_nls); |
| 2490 |
mutex_unlock(&ses->session_mutex); |
| 2491 |
|
| 2492 |
/* each channel uses a different signing key */ |
| 2493 |
spin_lock(&ses->chan_lock); |
| 2494 |
memcpy(ses->chans[0].signkey, ses->smb3signingkey, |
| 2495 |
sizeof(ses->smb3signingkey)); |
| 2496 |
spin_unlock(&ses->chan_lock); |
| 2497 |
|
| 2498 |
if (rc) { |
| 2499 |
if (((rc == -EACCES) || (rc == -EKEYEXPIRED) || |
| 2500 |
(rc == -EKEYREVOKED)) && !retries && ses->password2) { |
| 2501 |
retries++; |
| 2502 |
cifs_dbg(FYI, "Session setup failed, retrying with alternate password\n"); |
| 2503 |
swap(ses->password, ses->password2); |
| 2504 |
goto retry_new_session; |
| 2505 |
} else |
| 2506 |
goto get_ses_fail; |
| 2507 |
} |
| 2508 |
|
| 2509 |
/* |
| 2510 |
* success, put it on the list and add it as first channel |
| 2511 |
* note: the session becomes active soon after this. So you'll |
| 2512 |
* need to lock before changing something in the session. |
| 2513 |
*/ |
| 2514 |
spin_lock(&cifs_tcp_ses_lock); |
| 2515 |
ses->dfs_root_ses = ctx->dfs_root_ses; |
| 2516 |
list_add(&ses->smb_ses_list, &server->smb_ses_list); |
| 2517 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2518 |
|
| 2519 |
ipc = cifs_setup_ipc(ses, ctx->seal); |
| 2520 |
spin_lock(&cifs_tcp_ses_lock); |
| 2521 |
spin_lock(&ses->ses_lock); |
| 2522 |
ses->tcon_ipc = !IS_ERR(ipc) ? ipc : NULL; |
| 2523 |
spin_unlock(&ses->ses_lock); |
| 2524 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2525 |
|
| 2526 |
free_xid(xid); |
| 2527 |
|
| 2528 |
return ses; |
| 2529 |
|
| 2530 |
get_ses_fail: |
| 2531 |
sesInfoFree(ses); |
| 2532 |
free_xid(xid); |
| 2533 |
return ERR_PTR(rc); |
| 2534 |
} |
| 2535 |
|
| 2536 |
/* this function must be called with tc_lock held */ |
| 2537 |
static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx) |
| 2538 |
{ |
| 2539 |
struct TCP_Server_Info *server = tcon->ses->server; |
| 2540 |
|
| 2541 |
if (tcon->status == TID_EXITING) |
| 2542 |
return 0; |
| 2543 |
|
| 2544 |
if (tcon->origin_fullpath) { |
| 2545 |
if (!ctx->source || |
| 2546 |
!dfs_src_pathname_equal(ctx->source, |
| 2547 |
tcon->origin_fullpath)) |
| 2548 |
return 0; |
| 2549 |
} else if (!server->leaf_fullpath && |
| 2550 |
strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE)) { |
| 2551 |
return 0; |
| 2552 |
} |
| 2553 |
if (tcon->seal != ctx->seal) |
| 2554 |
return 0; |
| 2555 |
if (tcon->snapshot_time != ctx->snapshot_time) |
| 2556 |
return 0; |
| 2557 |
if (tcon->handle_timeout != ctx->handle_timeout) |
| 2558 |
return 0; |
| 2559 |
if (tcon->no_lease != ctx->no_lease) |
| 2560 |
return 0; |
| 2561 |
if (tcon->nodelete != ctx->nodelete) |
| 2562 |
return 0; |
| 2563 |
if (tcon->posix_extensions != ctx->linux_ext) |
| 2564 |
return 0; |
| 2565 |
return 1; |
| 2566 |
} |
| 2567 |
|
| 2568 |
static struct cifs_tcon * |
| 2569 |
cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
| 2570 |
{ |
| 2571 |
struct cifs_tcon *tcon; |
| 2572 |
|
| 2573 |
spin_lock(&cifs_tcp_ses_lock); |
| 2574 |
list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { |
| 2575 |
spin_lock(&tcon->tc_lock); |
| 2576 |
if (!match_tcon(tcon, ctx)) { |
| 2577 |
spin_unlock(&tcon->tc_lock); |
| 2578 |
continue; |
| 2579 |
} |
| 2580 |
++tcon->tc_count; |
| 2581 |
trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count, |
| 2582 |
netfs_trace_tcon_ref_get_find); |
| 2583 |
spin_unlock(&tcon->tc_lock); |
| 2584 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2585 |
return tcon; |
| 2586 |
} |
| 2587 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2588 |
return NULL; |
| 2589 |
} |
| 2590 |
|
| 2591 |
void |
| 2592 |
cifs_put_tcon(struct cifs_tcon *tcon, enum smb3_tcon_ref_trace trace) |
| 2593 |
{ |
| 2594 |
unsigned int xid; |
| 2595 |
struct cifs_ses *ses; |
| 2596 |
LIST_HEAD(ses_list); |
| 2597 |
|
| 2598 |
/* |
| 2599 |
* IPC tcon share the lifetime of their session and are |
| 2600 |
* destroyed in the session put function |
| 2601 |
*/ |
| 2602 |
if (tcon == NULL || tcon->ipc) |
| 2603 |
return; |
| 2604 |
|
| 2605 |
ses = tcon->ses; |
| 2606 |
cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count); |
| 2607 |
spin_lock(&cifs_tcp_ses_lock); |
| 2608 |
spin_lock(&tcon->tc_lock); |
| 2609 |
trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count - 1, trace); |
| 2610 |
if (--tcon->tc_count > 0) { |
| 2611 |
spin_unlock(&tcon->tc_lock); |
| 2612 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2613 |
return; |
| 2614 |
} |
| 2615 |
|
| 2616 |
/* tc_count can never go negative */ |
| 2617 |
WARN_ON(tcon->tc_count < 0); |
| 2618 |
|
| 2619 |
list_del_init(&tcon->tcon_list); |
| 2620 |
tcon->status = TID_EXITING; |
| 2621 |
spin_unlock(&tcon->tc_lock); |
| 2622 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2623 |
|
| 2624 |
/* cancel polling of interfaces */ |
| 2625 |
cancel_delayed_work_sync(&tcon->query_interfaces); |
| 2626 |
#ifdef CONFIG_CIFS_DFS_UPCALL |
| 2627 |
cancel_delayed_work_sync(&tcon->dfs_cache_work); |
| 2628 |
list_replace_init(&tcon->dfs_ses_list, &ses_list); |
| 2629 |
#endif |
| 2630 |
|
| 2631 |
if (tcon->use_witness) { |
| 2632 |
int rc; |
| 2633 |
|
| 2634 |
rc = cifs_swn_unregister(tcon); |
| 2635 |
if (rc < 0) { |
| 2636 |
cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n", |
| 2637 |
__func__, rc); |
| 2638 |
} |
| 2639 |
} |
| 2640 |
|
| 2641 |
xid = get_xid(); |
| 2642 |
if (ses->server->ops->tree_disconnect) |
| 2643 |
ses->server->ops->tree_disconnect(xid, tcon); |
| 2644 |
_free_xid(xid); |
| 2645 |
|
| 2646 |
cifs_fscache_release_super_cookie(tcon); |
| 2647 |
tconInfoFree(tcon, netfs_trace_tcon_ref_free); |
| 2648 |
cifs_put_smb_ses(ses); |
| 2649 |
#ifdef CONFIG_CIFS_DFS_UPCALL |
| 2650 |
dfs_put_root_smb_sessions(&ses_list); |
| 2651 |
#endif |
| 2652 |
} |
| 2653 |
|
| 2654 |
/** |
| 2655 |
* cifs_get_tcon - get a tcon matching @ctx data from @ses |
| 2656 |
* @ses: smb session to issue the request on |
| 2657 |
* @ctx: the superblock configuration context to use for building the |
| 2658 |
* |
| 2659 |
* - tcon refcount is the number of mount points using the tcon. |
| 2660 |
* - ses refcount is the number of tcon using the session. |
| 2661 |
* |
| 2662 |
* 1. This function assumes it is being called from cifs_mount() where |
| 2663 |
* we already got a session reference (ses refcount +1). |
| 2664 |
* |
| 2665 |
* 2. Since we're in the context of adding a mount point, the end |
| 2666 |
* result should be either: |
| 2667 |
* |
| 2668 |
* a) a new tcon already allocated with refcount=1 (1 mount point) and |
| 2669 |
* its session refcount incremented (1 new tcon). This +1 was |
| 2670 |
* already done in (1). |
| 2671 |
* |
| 2672 |
* b) an existing tcon with refcount+1 (add a mount point to it) and |
| 2673 |
* identical ses refcount (no new tcon). Because of (1) we need to |
| 2674 |
* decrement the ses refcount. |
| 2675 |
*/ |
| 2676 |
static struct cifs_tcon * |
| 2677 |
cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
| 2678 |
{ |
| 2679 |
struct cifs_tcon *tcon; |
| 2680 |
bool nohandlecache; |
| 2681 |
int rc, xid; |
| 2682 |
|
| 2683 |
tcon = cifs_find_tcon(ses, ctx); |
| 2684 |
if (tcon) { |
| 2685 |
/* |
| 2686 |
* tcon has refcount already incremented but we need to |
| 2687 |
* decrement extra ses reference gotten by caller (case b) |
| 2688 |
*/ |
| 2689 |
cifs_dbg(FYI, "Found match on UNC path\n"); |
| 2690 |
cifs_put_smb_ses(ses); |
| 2691 |
return tcon; |
| 2692 |
} |
| 2693 |
|
| 2694 |
if (!ses->server->ops->tree_connect) { |
| 2695 |
rc = -ENOSYS; |
| 2696 |
goto out_fail; |
| 2697 |
} |
| 2698 |
|
| 2699 |
if (ses->server->dialect >= SMB20_PROT_ID && |
| 2700 |
(ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)) |
| 2701 |
nohandlecache = ctx->nohandlecache || !dir_cache_timeout; |
| 2702 |
else |
| 2703 |
nohandlecache = true; |
| 2704 |
tcon = tcon_info_alloc(!nohandlecache, netfs_trace_tcon_ref_new); |
| 2705 |
if (tcon == NULL) { |
| 2706 |
rc = -ENOMEM; |
| 2707 |
goto out_fail; |
| 2708 |
} |
| 2709 |
tcon->nohandlecache = nohandlecache; |
| 2710 |
|
| 2711 |
if (ctx->snapshot_time) { |
| 2712 |
if (ses->server->vals->protocol_id == 0) { |
| 2713 |
cifs_dbg(VFS, |
| 2714 |
"Use SMB2 or later for snapshot mount option\n"); |
| 2715 |
rc = -EOPNOTSUPP; |
| 2716 |
goto out_fail; |
| 2717 |
} else |
| 2718 |
tcon->snapshot_time = ctx->snapshot_time; |
| 2719 |
} |
| 2720 |
|
| 2721 |
if (ctx->handle_timeout) { |
| 2722 |
if (ses->server->vals->protocol_id == 0) { |
| 2723 |
cifs_dbg(VFS, |
| 2724 |
"Use SMB2.1 or later for handle timeout option\n"); |
| 2725 |
rc = -EOPNOTSUPP; |
| 2726 |
goto out_fail; |
| 2727 |
} else |
| 2728 |
tcon->handle_timeout = ctx->handle_timeout; |
| 2729 |
} |
| 2730 |
|
| 2731 |
tcon->ses = ses; |
| 2732 |
if (ctx->password) { |
| 2733 |
tcon->password = kstrdup(ctx->password, GFP_KERNEL); |
| 2734 |
if (!tcon->password) { |
| 2735 |
rc = -ENOMEM; |
| 2736 |
goto out_fail; |
| 2737 |
} |
| 2738 |
} |
| 2739 |
|
| 2740 |
if (ctx->seal) { |
| 2741 |
if (ses->server->vals->protocol_id == 0) { |
| 2742 |
cifs_dbg(VFS, |
| 2743 |
"SMB3 or later required for encryption\n"); |
| 2744 |
rc = -EOPNOTSUPP; |
| 2745 |
goto out_fail; |
| 2746 |
} else if (tcon->ses->server->capabilities & |
| 2747 |
SMB2_GLOBAL_CAP_ENCRYPTION) |
| 2748 |
tcon->seal = true; |
| 2749 |
else { |
| 2750 |
cifs_dbg(VFS, "Encryption is not supported on share\n"); |
| 2751 |
rc = -EOPNOTSUPP; |
| 2752 |
goto out_fail; |
| 2753 |
} |
| 2754 |
} |
| 2755 |
|
| 2756 |
if (ctx->linux_ext) { |
| 2757 |
if (ses->server->posix_ext_supported) { |
| 2758 |
tcon->posix_extensions = true; |
| 2759 |
pr_warn_once("SMB3.11 POSIX Extensions are experimental\n"); |
| 2760 |
} else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) || |
| 2761 |
(strcmp(ses->server->vals->version_string, |
| 2762 |
SMB3ANY_VERSION_STRING) == 0) || |
| 2763 |
(strcmp(ses->server->vals->version_string, |
| 2764 |
SMBDEFAULT_VERSION_STRING) == 0)) { |
| 2765 |
cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n"); |
| 2766 |
rc = -EOPNOTSUPP; |
| 2767 |
goto out_fail; |
| 2768 |
} else if (ses->server->vals->protocol_id == SMB10_PROT_ID) |
| 2769 |
if (cap_unix(ses)) |
| 2770 |
cifs_dbg(FYI, "Unix Extensions requested on SMB1 mount\n"); |
| 2771 |
else { |
| 2772 |
cifs_dbg(VFS, "SMB1 Unix Extensions not supported by server\n"); |
| 2773 |
rc = -EOPNOTSUPP; |
| 2774 |
goto out_fail; |
| 2775 |
} else { |
| 2776 |
cifs_dbg(VFS, |
| 2777 |
"Check vers= mount option. SMB3.11 disabled but required for POSIX extensions\n"); |
| 2778 |
rc = -EOPNOTSUPP; |
| 2779 |
goto out_fail; |
| 2780 |
} |
| 2781 |
} |
| 2782 |
|
| 2783 |
xid = get_xid(); |
| 2784 |
rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon, |
| 2785 |
ctx->local_nls); |
| 2786 |
free_xid(xid); |
| 2787 |
cifs_dbg(FYI, "Tcon rc = %d\n", rc); |
| 2788 |
if (rc) |
| 2789 |
goto out_fail; |
| 2790 |
|
| 2791 |
tcon->use_persistent = false; |
| 2792 |
/* check if SMB2 or later, CIFS does not support persistent handles */ |
| 2793 |
if (ctx->persistent) { |
| 2794 |
if (ses->server->vals->protocol_id == 0) { |
| 2795 |
cifs_dbg(VFS, |
| 2796 |
"SMB3 or later required for persistent handles\n"); |
| 2797 |
rc = -EOPNOTSUPP; |
| 2798 |
goto out_fail; |
| 2799 |
} else if (ses->server->capabilities & |
| 2800 |
SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) |
| 2801 |
tcon->use_persistent = true; |
| 2802 |
else /* persistent handles requested but not supported */ { |
| 2803 |
cifs_dbg(VFS, |
| 2804 |
"Persistent handles not supported on share\n"); |
| 2805 |
rc = -EOPNOTSUPP; |
| 2806 |
goto out_fail; |
| 2807 |
} |
| 2808 |
} else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY) |
| 2809 |
&& (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) |
| 2810 |
&& (ctx->nopersistent == false)) { |
| 2811 |
cifs_dbg(FYI, "enabling persistent handles\n"); |
| 2812 |
tcon->use_persistent = true; |
| 2813 |
} else if (ctx->resilient) { |
| 2814 |
if (ses->server->vals->protocol_id == 0) { |
| 2815 |
cifs_dbg(VFS, |
| 2816 |
"SMB2.1 or later required for resilient handles\n"); |
| 2817 |
rc = -EOPNOTSUPP; |
| 2818 |
goto out_fail; |
| 2819 |
} |
| 2820 |
tcon->use_resilient = true; |
| 2821 |
} |
| 2822 |
|
| 2823 |
tcon->use_witness = false; |
| 2824 |
if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) { |
| 2825 |
if (ses->server->vals->protocol_id >= SMB30_PROT_ID) { |
| 2826 |
if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) { |
| 2827 |
/* |
| 2828 |
* Set witness in use flag in first place |
| 2829 |
* to retry registration in the echo task |
| 2830 |
*/ |
| 2831 |
tcon->use_witness = true; |
| 2832 |
/* And try to register immediately */ |
| 2833 |
rc = cifs_swn_register(tcon); |
| 2834 |
if (rc < 0) { |
| 2835 |
cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc); |
| 2836 |
goto out_fail; |
| 2837 |
} |
| 2838 |
} else { |
| 2839 |
/* TODO: try to extend for non-cluster uses (eg multichannel) */ |
| 2840 |
cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n"); |
| 2841 |
rc = -EOPNOTSUPP; |
| 2842 |
goto out_fail; |
| 2843 |
} |
| 2844 |
} else { |
| 2845 |
cifs_dbg(VFS, "SMB3 or later required for witness option\n"); |
| 2846 |
rc = -EOPNOTSUPP; |
| 2847 |
goto out_fail; |
| 2848 |
} |
| 2849 |
} |
| 2850 |
|
| 2851 |
/* If the user really knows what they are doing they can override */ |
| 2852 |
if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) { |
| 2853 |
if (ctx->cache_ro) |
| 2854 |
cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n"); |
| 2855 |
else if (ctx->cache_rw) |
| 2856 |
cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n"); |
| 2857 |
} |
| 2858 |
|
| 2859 |
if (ctx->no_lease) { |
| 2860 |
if (ses->server->vals->protocol_id == 0) { |
| 2861 |
cifs_dbg(VFS, |
| 2862 |
"SMB2 or later required for nolease option\n"); |
| 2863 |
rc = -EOPNOTSUPP; |
| 2864 |
goto out_fail; |
| 2865 |
} else |
| 2866 |
tcon->no_lease = ctx->no_lease; |
| 2867 |
} |
| 2868 |
|
| 2869 |
/* |
| 2870 |
* We can have only one retry value for a connection to a share so for |
| 2871 |
* resources mounted more than once to the same server share the last |
| 2872 |
* value passed in for the retry flag is used. |
| 2873 |
*/ |
| 2874 |
tcon->retry = ctx->retry; |
| 2875 |
tcon->nocase = ctx->nocase; |
| 2876 |
tcon->broken_sparse_sup = ctx->no_sparse; |
| 2877 |
tcon->max_cached_dirs = ctx->max_cached_dirs; |
| 2878 |
tcon->nodelete = ctx->nodelete; |
| 2879 |
tcon->local_lease = ctx->local_lease; |
| 2880 |
tcon->status = TID_GOOD; |
| 2881 |
|
| 2882 |
if (ses->server->dialect >= SMB30_PROT_ID && |
| 2883 |
(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) { |
| 2884 |
/* schedule query interfaces poll */ |
| 2885 |
queue_delayed_work(cifsiod_wq, &tcon->query_interfaces, |
| 2886 |
(SMB_INTERFACE_POLL_INTERVAL * HZ)); |
| 2887 |
} |
| 2888 |
spin_lock(&cifs_tcp_ses_lock); |
| 2889 |
list_add(&tcon->tcon_list, &ses->tcon_list); |
| 2890 |
spin_unlock(&cifs_tcp_ses_lock); |
| 2891 |
|
| 2892 |
return tcon; |
| 2893 |
|
| 2894 |
out_fail: |
| 2895 |
tconInfoFree(tcon, netfs_trace_tcon_ref_free_fail); |
| 2896 |
return ERR_PTR(rc); |
| 2897 |
} |
| 2898 |
|
| 2899 |
void |
| 2900 |
cifs_put_tlink(struct tcon_link *tlink) |
| 2901 |
{ |
| 2902 |
if (!tlink || IS_ERR(tlink)) |
| 2903 |
return; |
| 2904 |
|
| 2905 |
if (!atomic_dec_and_test(&tlink->tl_count) || |
| 2906 |
test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) { |
| 2907 |
tlink->tl_time = jiffies; |
| 2908 |
return; |
| 2909 |
} |
| 2910 |
|
| 2911 |
if (!IS_ERR(tlink_tcon(tlink))) |
| 2912 |
cifs_put_tcon(tlink_tcon(tlink), netfs_trace_tcon_ref_put_tlink); |
| 2913 |
kfree(tlink); |
| 2914 |
} |
| 2915 |
|
| 2916 |
static int |
| 2917 |
compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data) |
| 2918 |
{ |
| 2919 |
struct cifs_sb_info *old = CIFS_SB(sb); |
| 2920 |
struct cifs_sb_info *new = mnt_data->cifs_sb; |
| 2921 |
unsigned int oldflags = cifs_sb_flags(old) & CIFS_MOUNT_MASK; |
| 2922 |
unsigned int newflags = cifs_sb_flags(new) & CIFS_MOUNT_MASK; |
| 2923 |
|
| 2924 |
if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK)) |
| 2925 |
return 0; |
| 2926 |
|
| 2927 |
if (old->mnt_cifs_serverino_autodisabled) |
| 2928 |
newflags &= ~CIFS_MOUNT_SERVER_INUM; |
| 2929 |
|
| 2930 |
if (oldflags != newflags) |
| 2931 |
return 0; |
| 2932 |
|
| 2933 |
/* |
| 2934 |
* We want to share sb only if we don't specify an r/wsize or |
| 2935 |
* specified r/wsize is greater than or equal to existing one. |
| 2936 |
*/ |
| 2937 |
if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize) |
| 2938 |
return 0; |
| 2939 |
|
| 2940 |
if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize) |
| 2941 |
return 0; |
| 2942 |
|
| 2943 |
if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) || |
| 2944 |
!gid_eq(old->ctx->linux_gid, new->ctx->linux_gid)) |
| 2945 |
return 0; |
| 2946 |
|
| 2947 |
if (old->ctx->file_mode != new->ctx->file_mode || |
| 2948 |
old->ctx->dir_mode != new->ctx->dir_mode) |
| 2949 |
return 0; |
| 2950 |
|
| 2951 |
if (strcmp(old->local_nls->charset, new->local_nls->charset)) |
| 2952 |
return 0; |
| 2953 |
|
| 2954 |
if (old->ctx->acregmax != new->ctx->acregmax) |
| 2955 |
return 0; |
| 2956 |
if (old->ctx->acdirmax != new->ctx->acdirmax) |
| 2957 |
return 0; |
| 2958 |
if (old->ctx->closetimeo != new->ctx->closetimeo) |
| 2959 |
return 0; |
| 2960 |
if (old->ctx->reparse_type != new->ctx->reparse_type) |
| 2961 |
return 0; |
| 2962 |
if (old->ctx->nonativesocket != new->ctx->nonativesocket) |
| 2963 |
return 0; |
| 2964 |
if (old->ctx->symlink_type != new->ctx->symlink_type) |
| 2965 |
return 0; |
| 2966 |
|
| 2967 |
return 1; |
| 2968 |
} |
| 2969 |
|
| 2970 |
static int match_prepath(struct super_block *sb, |
| 2971 |
struct cifs_tcon *tcon, |
| 2972 |
struct cifs_mnt_data *mnt_data) |
| 2973 |
{ |
| 2974 |
struct smb3_fs_context *ctx = mnt_data->ctx; |
| 2975 |
struct cifs_sb_info *old = CIFS_SB(sb); |
| 2976 |
struct cifs_sb_info *new = mnt_data->cifs_sb; |
| 2977 |
bool old_set = (cifs_sb_flags(old) & CIFS_MOUNT_USE_PREFIX_PATH) && |
| 2978 |
old->prepath; |
| 2979 |
bool new_set = (cifs_sb_flags(new) & CIFS_MOUNT_USE_PREFIX_PATH) && |
| 2980 |
new->prepath; |
| 2981 |
|
| 2982 |
if (tcon->origin_fullpath && |
| 2983 |
dfs_src_pathname_equal(tcon->origin_fullpath, ctx->source)) |
| 2984 |
return 1; |
| 2985 |
|
| 2986 |
if (old_set && new_set && !strcmp(new->prepath, old->prepath)) |
| 2987 |
return 1; |
| 2988 |
else if (!old_set && !new_set) |
| 2989 |
return 1; |
| 2990 |
|
| 2991 |
return 0; |
| 2992 |
} |
| 2993 |
|
| 2994 |
int |
| 2995 |
cifs_match_super(struct super_block *sb, void *data) |
| 2996 |
{ |
| 2997 |
struct cifs_mnt_data *mnt_data = data; |
| 2998 |
struct smb3_fs_context *ctx; |
| 2999 |
struct cifs_sb_info *cifs_sb; |
| 3000 |
struct TCP_Server_Info *tcp_srv; |
| 3001 |
struct cifs_ses *ses; |
| 3002 |
struct cifs_tcon *tcon; |
| 3003 |
struct tcon_link *tlink; |
| 3004 |
int rc = 0; |
| 3005 |
|
| 3006 |
spin_lock(&cifs_tcp_ses_lock); |
| 3007 |
cifs_sb = CIFS_SB(sb); |
| 3008 |
|
| 3009 |
/* We do not want to use a superblock that has been shutdown */ |
| 3010 |
if (cifs_forced_shutdown(cifs_sb)) { |
| 3011 |
spin_unlock(&cifs_tcp_ses_lock); |
| 3012 |
return 0; |
| 3013 |
} |
| 3014 |
|
| 3015 |
tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); |
| 3016 |
if (IS_ERR_OR_NULL(tlink)) { |
| 3017 |
pr_warn_once("%s: skip super matching due to bad tlink(%p)\n", |
| 3018 |
__func__, tlink); |
| 3019 |
spin_unlock(&cifs_tcp_ses_lock); |
| 3020 |
return 0; |
| 3021 |
} |
| 3022 |
tcon = tlink_tcon(tlink); |
| 3023 |
ses = tcon->ses; |
| 3024 |
tcp_srv = ses->server; |
| 3025 |
|
| 3026 |
ctx = mnt_data->ctx; |
| 3027 |
|
| 3028 |
spin_lock(&tcp_srv->srv_lock); |
| 3029 |
spin_lock(&ses->ses_lock); |
| 3030 |
spin_lock(&ses->chan_lock); |
| 3031 |
spin_lock(&tcon->tc_lock); |
| 3032 |
if (!match_server(tcp_srv, ctx, true) || |
| 3033 |
!match_session(ses, ctx, true) || |
| 3034 |
!match_tcon(tcon, ctx) || |
| 3035 |
!match_prepath(sb, tcon, mnt_data)) { |
| 3036 |
rc = 0; |
| 3037 |
goto out; |
| 3038 |
} |
| 3039 |
|
| 3040 |
rc = compare_mount_options(sb, mnt_data); |
| 3041 |
out: |
| 3042 |
spin_unlock(&tcon->tc_lock); |
| 3043 |
spin_unlock(&ses->chan_lock); |
| 3044 |
spin_unlock(&ses->ses_lock); |
| 3045 |
spin_unlock(&tcp_srv->srv_lock); |
| 3046 |
|
| 3047 |
spin_unlock(&cifs_tcp_ses_lock); |
| 3048 |
cifs_put_tlink(tlink); |
| 3049 |
return rc; |
| 3050 |
} |
| 3051 |
|
| 3052 |
#ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 3053 |
static struct lock_class_key cifs_key[2]; |
| 3054 |
static struct lock_class_key cifs_slock_key[2]; |
| 3055 |
|
| 3056 |
static inline void |
| 3057 |
cifs_reclassify_socket4(struct socket *sock) |
| 3058 |
{ |
| 3059 |
struct sock *sk = sock->sk; |
| 3060 |
|
| 3061 |
BUG_ON(!sock_allow_reclassification(sk)); |
| 3062 |
sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS", |
| 3063 |
&cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]); |
| 3064 |
} |
| 3065 |
|
| 3066 |
static inline void |
| 3067 |
cifs_reclassify_socket6(struct socket *sock) |
| 3068 |
{ |
| 3069 |
struct sock *sk = sock->sk; |
| 3070 |
|
| 3071 |
BUG_ON(!sock_allow_reclassification(sk)); |
| 3072 |
sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS", |
| 3073 |
&cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]); |
| 3074 |
} |
| 3075 |
#else |
| 3076 |
static inline void |
| 3077 |
cifs_reclassify_socket4(struct socket *sock) |
| 3078 |
{ |
| 3079 |
} |
| 3080 |
|
| 3081 |
static inline void |
| 3082 |
cifs_reclassify_socket6(struct socket *sock) |
| 3083 |
{ |
| 3084 |
} |
| 3085 |
#endif |
| 3086 |
|
| 3087 |
/* See RFC1001 section 14 on representation of Netbios names */ |
| 3088 |
static void rfc1002mangle(char *target, char *source, unsigned int length) |
| 3089 |
{ |
| 3090 |
unsigned int i, j; |
| 3091 |
|
| 3092 |
for (i = 0, j = 0; i < (length); i++) { |
| 3093 |
/* mask a nibble at a time and encode */ |
| 3094 |
target[j] = 'A' + (0x0F & (source[i] >> 4)); |
| 3095 |
target[j+1] = 'A' + (0x0F & source[i]); |
| 3096 |
j += 2; |
| 3097 |
} |
| 3098 |
|
| 3099 |
} |
| 3100 |
|
| 3101 |
static int |
| 3102 |
bind_socket(struct TCP_Server_Info *server) |
| 3103 |
{ |
| 3104 |
int rc = 0; |
| 3105 |
|
| 3106 |
if (server->srcaddr.ss_family != AF_UNSPEC) { |
| 3107 |
/* Bind to the specified local IP address */ |
| 3108 |
struct socket *socket = server->ssocket; |
| 3109 |
|
| 3110 |
rc = kernel_bind(socket, |
| 3111 |
(struct sockaddr_unsized *) &server->srcaddr, |
| 3112 |
sizeof(server->srcaddr)); |
| 3113 |
if (rc < 0) { |
| 3114 |
struct sockaddr_in *saddr4; |
| 3115 |
struct sockaddr_in6 *saddr6; |
| 3116 |
|
| 3117 |
saddr4 = (struct sockaddr_in *)&server->srcaddr; |
| 3118 |
saddr6 = (struct sockaddr_in6 *)&server->srcaddr; |
| 3119 |
if (saddr6->sin6_family == AF_INET6) |
| 3120 |
cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n", |
| 3121 |
&saddr6->sin6_addr, rc); |
| 3122 |
else |
| 3123 |
cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n", |
| 3124 |
&saddr4->sin_addr.s_addr, rc); |
| 3125 |
} |
| 3126 |
} |
| 3127 |
return rc; |
| 3128 |
} |
| 3129 |
|
| 3130 |
static int |
| 3131 |
smb_recv_kvec(struct TCP_Server_Info *server, struct msghdr *msg, size_t *recv) |
| 3132 |
{ |
| 3133 |
int rc = 0; |
| 3134 |
int retries = 0; |
| 3135 |
int msg_flags = server->noblocksnd ? MSG_DONTWAIT : 0; |
| 3136 |
|
| 3137 |
*recv = 0; |
| 3138 |
|
| 3139 |
while (msg_data_left(msg)) { |
| 3140 |
rc = sock_recvmsg(server->ssocket, msg, msg_flags); |
| 3141 |
if (rc == -EAGAIN) { |
| 3142 |
retries++; |
| 3143 |
if (retries >= 14 || |
| 3144 |
(!server->noblocksnd && (retries > 2))) { |
| 3145 |
cifs_server_dbg(VFS, "sends on sock %p stuck for 15 seconds\n", |
| 3146 |
server->ssocket); |
| 3147 |
return -EAGAIN; |
| 3148 |
} |
| 3149 |
msleep(1 << retries); |
| 3150 |
continue; |
| 3151 |
} |
| 3152 |
|
| 3153 |
if (rc < 0) |
| 3154 |
return rc; |
| 3155 |
|
| 3156 |
if (rc == 0) { |
| 3157 |
cifs_dbg(FYI, "Received no data (TCP RST)\n"); |
| 3158 |
return -ECONNABORTED; |
| 3159 |
} |
| 3160 |
|
| 3161 |
/* recv was at least partially successful */ |
| 3162 |
*recv += rc; |
| 3163 |
retries = 0; /* in case we get ENOSPC on the next send */ |
| 3164 |
} |
| 3165 |
return 0; |
| 3166 |
} |
| 3167 |
|
| 3168 |
static int |
| 3169 |
ip_rfc1001_connect(struct TCP_Server_Info *server) |
| 3170 |
{ |
| 3171 |
int rc = 0; |
| 3172 |
/* |
| 3173 |
* some servers require RFC1001 sessinit before sending |
| 3174 |
* negprot - BB check reconnection in case where second |
| 3175 |
* sessinit is sent but no second negprot |
| 3176 |
*/ |
| 3177 |
struct rfc1002_session_packet req = {}; |
| 3178 |
struct rfc1002_session_packet resp = {}; |
| 3179 |
struct msghdr msg = {}; |
| 3180 |
struct kvec iov = {}; |
| 3181 |
unsigned int len; |
| 3182 |
size_t sent; |
| 3183 |
size_t recv; |
| 3184 |
|
| 3185 |
req.trailer.session_req.called_len = sizeof(req.trailer.session_req.called_name); |
| 3186 |
|
| 3187 |
if (server->server_RFC1001_name[0] != 0) |
| 3188 |
rfc1002mangle(req.trailer.session_req.called_name, |
| 3189 |
server->server_RFC1001_name, |
| 3190 |
RFC1001_NAME_LEN_WITH_NULL); |
| 3191 |
else |
| 3192 |
rfc1002mangle(req.trailer.session_req.called_name, |
| 3193 |
DEFAULT_CIFS_CALLED_NAME, |
| 3194 |
RFC1001_NAME_LEN_WITH_NULL); |
| 3195 |
|
| 3196 |
req.trailer.session_req.calling_len = sizeof(req.trailer.session_req.calling_name); |
| 3197 |
|
| 3198 |
/* calling name ends in null (byte 16) from old smb convention */ |
| 3199 |
if (server->workstation_RFC1001_name[0] != 0) |
| 3200 |
rfc1002mangle(req.trailer.session_req.calling_name, |
| 3201 |
server->workstation_RFC1001_name, |
| 3202 |
RFC1001_NAME_LEN_WITH_NULL); |
| 3203 |
else |
| 3204 |
rfc1002mangle(req.trailer.session_req.calling_name, |
| 3205 |
"LINUX_CIFS_CLNT", |
| 3206 |
RFC1001_NAME_LEN_WITH_NULL); |
| 3207 |
|
| 3208 |
/* |
| 3209 |
* As per rfc1002, @len must be the number of bytes that follows the |
| 3210 |
* length field of a rfc1002 session request payload. |
| 3211 |
*/ |
| 3212 |
len = sizeof(req.trailer.session_req); |
| 3213 |
req.type = RFC1002_SESSION_REQUEST; |
| 3214 |
req.flags = 0; |
| 3215 |
req.length = cpu_to_be16(len); |
| 3216 |
len += offsetof(typeof(req), trailer.session_req); |
| 3217 |
iov.iov_base = &req; |
| 3218 |
iov.iov_len = len; |
| 3219 |
iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, len); |
| 3220 |
rc = smb_send_kvec(server, &msg, &sent); |
| 3221 |
if (rc < 0 || len != sent) |
| 3222 |
return (rc == -EINTR || rc == -EAGAIN) ? rc : -ECONNABORTED; |
| 3223 |
|
| 3224 |
/* |
| 3225 |
* RFC1001 layer in at least one server requires very short break before |
| 3226 |
* negprot presumably because not expecting negprot to follow so fast. |
| 3227 |
* For example DOS SMB servers cannot process negprot if it was received |
| 3228 |
* before the server sent response for SESSION_REQUEST packet. So, wait |
| 3229 |
* for the response, read it and parse it as it can contain useful error |
| 3230 |
* information (e.g. specified server name was incorrect). For example |
| 3231 |
* even the latest Windows Server 2022 SMB1 server over port 139 send |
| 3232 |
* error if its server name was in SESSION_REQUEST packet incorrect. |
| 3233 |
* Nowadays usage of port 139 is not common, so waiting for reply here |
| 3234 |
* does not slowing down mounting of common case (over port 445). |
| 3235 |
*/ |
| 3236 |
len = offsetof(typeof(resp), trailer); |
| 3237 |
iov.iov_base = &resp; |
| 3238 |
iov.iov_len = len; |
| 3239 |
iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, len); |
| 3240 |
rc = smb_recv_kvec(server, &msg, &recv); |
| 3241 |
if (rc < 0 || recv != len) |
| 3242 |
return (rc == -EINTR || rc == -EAGAIN) ? rc : -ECONNABORTED; |
| 3243 |
|
| 3244 |
switch (resp.type) { |
| 3245 |
case RFC1002_POSITIVE_SESSION_RESPONSE: |
| 3246 |
if (be16_to_cpu(resp.length) != 0) { |
| 3247 |
cifs_dbg(VFS, "RFC 1002 positive session response but with invalid non-zero length %u\n", |
| 3248 |
be16_to_cpu(resp.length)); |
| 3249 |
return smb_EIO(smb_eio_trace_rx_pos_sess_resp); |
| 3250 |
} |
| 3251 |
cifs_dbg(FYI, "RFC 1002 positive session response"); |
| 3252 |
break; |
| 3253 |
case RFC1002_NEGATIVE_SESSION_RESPONSE: |
| 3254 |
/* Read RFC1002 response error code and convert it to errno in rc */ |
| 3255 |
len = sizeof(resp.trailer.neg_ses_resp_error_code); |
| 3256 |
iov.iov_base = &resp.trailer.neg_ses_resp_error_code; |
| 3257 |
iov.iov_len = len; |
| 3258 |
iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, len); |
| 3259 |
if (be16_to_cpu(resp.length) == len && |
| 3260 |
smb_recv_kvec(server, &msg, &recv) == 0 && |
| 3261 |
recv == len) { |
| 3262 |
cifs_dbg(VFS, "RFC 1002 negative session response with error 0x%x\n", |
| 3263 |
resp.trailer.neg_ses_resp_error_code); |
| 3264 |
switch (resp.trailer.neg_ses_resp_error_code) { |
| 3265 |
case RFC1002_NOT_LISTENING_CALLED: |
| 3266 |
/* server does not listen for specified server name */ |
| 3267 |
fallthrough; |
| 3268 |
case RFC1002_NOT_PRESENT: |
| 3269 |
/* server name is incorrect */ |
| 3270 |
rc = -ENOENT; |
| 3271 |
cifs_dbg(VFS, "Server rejected NetBIOS servername %.15s\n", |
| 3272 |
server->server_RFC1001_name[0] ? |
| 3273 |
server->server_RFC1001_name : |
| 3274 |
DEFAULT_CIFS_CALLED_NAME); |
| 3275 |
cifs_dbg(VFS, "Specify correct NetBIOS servername in source path or with -o servern= option\n"); |
| 3276 |
break; |
| 3277 |
case RFC1002_NOT_LISTENING_CALLING: |
| 3278 |
/* client name was not accepted by server */ |
| 3279 |
rc = -EACCES; |
| 3280 |
cifs_dbg(VFS, "Server rejected NetBIOS clientname %.15s\n", |
| 3281 |
server->workstation_RFC1001_name[0] ? |
| 3282 |
server->workstation_RFC1001_name : |
| 3283 |
"LINUX_CIFS_CLNT"); |
| 3284 |
cifs_dbg(VFS, "Specify correct NetBIOS clientname with -o netbiosname= option\n"); |
| 3285 |
break; |
| 3286 |
case RFC1002_INSUFFICIENT_RESOURCE: |
| 3287 |
/* remote server resource error */ |
| 3288 |
smb_EIO(smb_eio_trace_rx_insuff_res); |
| 3289 |
rc = -EREMOTEIO; |
| 3290 |
break; |
| 3291 |
case RFC1002_UNSPECIFIED_ERROR: |
| 3292 |
default: |
| 3293 |
/* other/unknown error */ |
| 3294 |
rc = smb_EIO(smb_eio_trace_rx_unspec_error); |
| 3295 |
break; |
| 3296 |
} |
| 3297 |
} else { |
| 3298 |
cifs_dbg(VFS, "RFC 1002 negative session response\n"); |
| 3299 |
rc = smb_EIO(smb_eio_trace_rx_neg_sess_resp); |
| 3300 |
} |
| 3301 |
return rc; |
| 3302 |
case RFC1002_RETARGET_SESSION_RESPONSE: |
| 3303 |
cifs_dbg(VFS, "RFC 1002 retarget session response\n"); |
| 3304 |
if (be16_to_cpu(resp.length) == sizeof(resp.trailer.retarget_resp)) { |
| 3305 |
len = sizeof(resp.trailer.retarget_resp); |
| 3306 |
iov.iov_base = &resp.trailer.retarget_resp; |
| 3307 |
iov.iov_len = len; |
| 3308 |
iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, len); |
| 3309 |
if (smb_recv_kvec(server, &msg, &recv) == 0 && recv == len) { |
| 3310 |
cifs_dbg(VFS, "Server wants to redirect connection\n"); |
| 3311 |
cifs_dbg(VFS, "Remount with options -o ip=%pI4,port=%u\n", |
| 3312 |
&resp.trailer.retarget_resp.retarget_ip_addr, |
| 3313 |
be16_to_cpu(resp.trailer.retarget_resp.port)); |
| 3314 |
} |
| 3315 |
} |
| 3316 |
cifs_dbg(VFS, "Closing connection\n"); |
| 3317 |
/* FIXME: Should we automatically redirect to new retarget_resp server? */ |
| 3318 |
return -EMULTIHOP; |
| 3319 |
default: |
| 3320 |
cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", resp.type); |
| 3321 |
return smb_EIO1(smb_eio_trace_rx_unknown_resp, resp.type); |
| 3322 |
} |
| 3323 |
|
| 3324 |
server->with_rfc1001 = true; |
| 3325 |
return 0; |
| 3326 |
} |
| 3327 |
|
| 3328 |
static int |
| 3329 |
generic_ip_connect(struct TCP_Server_Info *server) |
| 3330 |
{ |
| 3331 |
struct sockaddr *saddr; |
| 3332 |
struct socket *socket; |
| 3333 |
int slen, sfamily; |
| 3334 |
__be16 sport; |
| 3335 |
int rc = 0; |
| 3336 |
|
| 3337 |
saddr = (struct sockaddr *) &server->dstaddr; |
| 3338 |
|
| 3339 |
if (server->dstaddr.ss_family == AF_INET6) { |
| 3340 |
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 3341 |
|
| 3342 |
sport = ipv6->sin6_port; |
| 3343 |
slen = sizeof(struct sockaddr_in6); |
| 3344 |
sfamily = AF_INET6; |
| 3345 |
cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr, |
| 3346 |
ntohs(sport)); |
| 3347 |
} else { |
| 3348 |
struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr; |
| 3349 |
|
| 3350 |
sport = ipv4->sin_port; |
| 3351 |
slen = sizeof(struct sockaddr_in); |
| 3352 |
sfamily = AF_INET; |
| 3353 |
cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr, |
| 3354 |
ntohs(sport)); |
| 3355 |
} |
| 3356 |
|
| 3357 |
if (server->ssocket) { |
| 3358 |
socket = server->ssocket; |
| 3359 |
} else { |
| 3360 |
struct net *net = cifs_net_ns(server); |
| 3361 |
struct sock *sk; |
| 3362 |
|
| 3363 |
rc = sock_create_kern(net, sfamily, SOCK_STREAM, |
| 3364 |
IPPROTO_TCP, &server->ssocket); |
| 3365 |
if (rc < 0) { |
| 3366 |
cifs_server_dbg(VFS, "Error %d creating socket\n", rc); |
| 3367 |
return rc; |
| 3368 |
} |
| 3369 |
|
| 3370 |
sk = server->ssocket->sk; |
| 3371 |
sk_net_refcnt_upgrade(sk); |
| 3372 |
|
| 3373 |
/* BB other socket options to set KEEPALIVE, NODELAY? */ |
| 3374 |
cifs_dbg(FYI, "Socket created\n"); |
| 3375 |
socket = server->ssocket; |
| 3376 |
socket->sk->sk_allocation = GFP_NOFS; |
| 3377 |
socket->sk->sk_use_task_frag = false; |
| 3378 |
if (sfamily == AF_INET6) |
| 3379 |
cifs_reclassify_socket6(socket); |
| 3380 |
else |
| 3381 |
cifs_reclassify_socket4(socket); |
| 3382 |
} |
| 3383 |
|
| 3384 |
rc = bind_socket(server); |
| 3385 |
if (rc < 0) |
| 3386 |
return rc; |
| 3387 |
|
| 3388 |
/* |
| 3389 |
* Eventually check for other socket options to change from |
| 3390 |
* the default. sock_setsockopt not used because it expects |
| 3391 |
* user space buffer |
| 3392 |
*/ |
| 3393 |
socket->sk->sk_rcvtimeo = 7 * HZ; |
| 3394 |
socket->sk->sk_sndtimeo = 5 * HZ; |
| 3395 |
|
| 3396 |
/* make the bufsizes depend on wsize/rsize and max requests */ |
| 3397 |
if (server->noautotune) { |
| 3398 |
if (socket->sk->sk_sndbuf < (200 * 1024)) |
| 3399 |
socket->sk->sk_sndbuf = 200 * 1024; |
| 3400 |
if (socket->sk->sk_rcvbuf < (140 * 1024)) |
| 3401 |
socket->sk->sk_rcvbuf = 140 * 1024; |
| 3402 |
} |
| 3403 |
|
| 3404 |
if (server->tcp_nodelay) |
| 3405 |
tcp_sock_set_nodelay(socket->sk); |
| 3406 |
|
| 3407 |
cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n", |
| 3408 |
socket->sk->sk_sndbuf, |
| 3409 |
socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo); |
| 3410 |
|
| 3411 |
rc = kernel_connect(socket, (struct sockaddr_unsized *)saddr, slen, |
| 3412 |
server->noblockcnt ? O_NONBLOCK : 0); |
| 3413 |
/* |
| 3414 |
* When mounting SMB root file systems, we do not want to block in |
| 3415 |
* connect. Otherwise bail out and then let cifs_reconnect() perform |
| 3416 |
* reconnect failover - if possible. |
| 3417 |
*/ |
| 3418 |
if (server->noblockcnt && rc == -EINPROGRESS) |
| 3419 |
rc = 0; |
| 3420 |
if (rc < 0) { |
| 3421 |
cifs_dbg(FYI, "Error %d connecting to server\n", rc); |
| 3422 |
trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc); |
| 3423 |
sock_release(socket); |
| 3424 |
server->ssocket = NULL; |
| 3425 |
return rc; |
| 3426 |
} |
| 3427 |
trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr); |
| 3428 |
|
| 3429 |
/* |
| 3430 |
* Establish RFC1001 NetBIOS session when it was explicitly requested |
| 3431 |
* by mount option -o nbsessinit, or when connecting to default RFC1001 |
| 3432 |
* server port (139) and it was not explicitly disabled by mount option |
| 3433 |
* -o nonbsessinit. |
| 3434 |
*/ |
| 3435 |
if (server->with_rfc1001 || |
| 3436 |
server->rfc1001_sessinit == 1 || |
| 3437 |
(server->rfc1001_sessinit == -1 && sport == htons(RFC1001_PORT))) |
| 3438 |
rc = ip_rfc1001_connect(server); |
| 3439 |
|
| 3440 |
return rc; |
| 3441 |
} |
| 3442 |
|
| 3443 |
static int |
| 3444 |
ip_connect(struct TCP_Server_Info *server) |
| 3445 |
{ |
| 3446 |
__be16 *sport; |
| 3447 |
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 3448 |
struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; |
| 3449 |
|
| 3450 |
if (server->dstaddr.ss_family == AF_INET6) |
| 3451 |
sport = &addr6->sin6_port; |
| 3452 |
else |
| 3453 |
sport = &addr->sin_port; |
| 3454 |
|
| 3455 |
if (*sport == 0) { |
| 3456 |
int rc; |
| 3457 |
|
| 3458 |
/* try with 445 port at first */ |
| 3459 |
*sport = htons(CIFS_PORT); |
| 3460 |
|
| 3461 |
rc = generic_ip_connect(server); |
| 3462 |
if (rc >= 0) |
| 3463 |
return rc; |
| 3464 |
|
| 3465 |
/* if it failed, try with 139 port */ |
| 3466 |
*sport = htons(RFC1001_PORT); |
| 3467 |
} |
| 3468 |
|
| 3469 |
return generic_ip_connect(server); |
| 3470 |
} |
| 3471 |
|
| 3472 |
int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb) |
| 3473 |
{ |
| 3474 |
struct smb3_fs_context *ctx = cifs_sb->ctx; |
| 3475 |
unsigned int sbflags; |
| 3476 |
int rc = 0; |
| 3477 |
|
| 3478 |
INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks); |
| 3479 |
INIT_LIST_HEAD(&cifs_sb->tcon_sb_link); |
| 3480 |
|
| 3481 |
spin_lock_init(&cifs_sb->tlink_tree_lock); |
| 3482 |
cifs_sb->tlink_tree = RB_ROOT; |
| 3483 |
|
| 3484 |
cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n", |
| 3485 |
ctx->file_mode, ctx->dir_mode); |
| 3486 |
|
| 3487 |
/* this is needed for ASCII cp to Unicode converts */ |
| 3488 |
if (ctx->iocharset == NULL) { |
| 3489 |
/* load_nls_default cannot return null */ |
| 3490 |
cifs_sb->local_nls = load_nls_default(); |
| 3491 |
} else { |
| 3492 |
cifs_sb->local_nls = load_nls(ctx->iocharset); |
| 3493 |
if (cifs_sb->local_nls == NULL) { |
| 3494 |
cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n", |
| 3495 |
ctx->iocharset); |
| 3496 |
return -ELIBACC; |
| 3497 |
} |
| 3498 |
} |
| 3499 |
ctx->local_nls = cifs_sb->local_nls; |
| 3500 |
|
| 3501 |
sbflags = smb3_update_mnt_flags(cifs_sb); |
| 3502 |
|
| 3503 |
if (ctx->direct_io) |
| 3504 |
cifs_dbg(FYI, "mounting share using direct i/o\n"); |
| 3505 |
if (ctx->cache_ro) { |
| 3506 |
cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n"); |
| 3507 |
sbflags |= CIFS_MOUNT_RO_CACHE; |
| 3508 |
} else if (ctx->cache_rw) { |
| 3509 |
cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n"); |
| 3510 |
sbflags |= CIFS_MOUNT_RO_CACHE | CIFS_MOUNT_RW_CACHE; |
| 3511 |
} |
| 3512 |
|
| 3513 |
if ((ctx->cifs_acl) && (ctx->dynperm)) |
| 3514 |
cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n"); |
| 3515 |
|
| 3516 |
if (ctx->prepath) { |
| 3517 |
cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL); |
| 3518 |
if (cifs_sb->prepath == NULL) |
| 3519 |
rc = -ENOMEM; |
| 3520 |
else |
| 3521 |
sbflags |= CIFS_MOUNT_USE_PREFIX_PATH; |
| 3522 |
} |
| 3523 |
|
| 3524 |
atomic_set(&cifs_sb->mnt_cifs_flags, sbflags); |
| 3525 |
return rc; |
| 3526 |
} |
| 3527 |
|
| 3528 |
/* Release all succeed connections */ |
| 3529 |
void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx) |
| 3530 |
{ |
| 3531 |
struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3532 |
int rc = 0; |
| 3533 |
|
| 3534 |
if (mnt_ctx->tcon) |
| 3535 |
cifs_put_tcon(mnt_ctx->tcon, netfs_trace_tcon_ref_put_mnt_ctx); |
| 3536 |
else if (mnt_ctx->ses) |
| 3537 |
cifs_put_smb_ses(mnt_ctx->ses); |
| 3538 |
else if (mnt_ctx->server) |
| 3539 |
cifs_put_tcp_session(mnt_ctx->server, 0); |
| 3540 |
mnt_ctx->ses = NULL; |
| 3541 |
mnt_ctx->tcon = NULL; |
| 3542 |
mnt_ctx->server = NULL; |
| 3543 |
atomic_andnot(CIFS_MOUNT_POSIX_PATHS, &cifs_sb->mnt_cifs_flags); |
| 3544 |
free_xid(mnt_ctx->xid); |
| 3545 |
} |
| 3546 |
|
| 3547 |
int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx) |
| 3548 |
{ |
| 3549 |
struct TCP_Server_Info *server = NULL; |
| 3550 |
struct smb3_fs_context *ctx; |
| 3551 |
struct cifs_ses *ses = NULL; |
| 3552 |
unsigned int xid; |
| 3553 |
int rc = 0; |
| 3554 |
|
| 3555 |
xid = get_xid(); |
| 3556 |
|
| 3557 |
if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->fs_ctx)) { |
| 3558 |
rc = -EINVAL; |
| 3559 |
goto out; |
| 3560 |
} |
| 3561 |
ctx = mnt_ctx->fs_ctx; |
| 3562 |
|
| 3563 |
/* get a reference to a tcp session */ |
| 3564 |
server = cifs_get_tcp_session(ctx, NULL); |
| 3565 |
if (IS_ERR(server)) { |
| 3566 |
rc = PTR_ERR(server); |
| 3567 |
server = NULL; |
| 3568 |
goto out; |
| 3569 |
} |
| 3570 |
|
| 3571 |
/* get a reference to a SMB session */ |
| 3572 |
ses = cifs_get_smb_ses(server, ctx); |
| 3573 |
if (IS_ERR(ses)) { |
| 3574 |
rc = PTR_ERR(ses); |
| 3575 |
ses = NULL; |
| 3576 |
goto out; |
| 3577 |
} |
| 3578 |
|
| 3579 |
if ((ctx->persistent == true) && (!(ses->server->capabilities & |
| 3580 |
SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) { |
| 3581 |
cifs_server_dbg(VFS, "persistent handles not supported by server\n"); |
| 3582 |
rc = -EOPNOTSUPP; |
| 3583 |
} |
| 3584 |
|
| 3585 |
out: |
| 3586 |
mnt_ctx->xid = xid; |
| 3587 |
mnt_ctx->server = server; |
| 3588 |
mnt_ctx->ses = ses; |
| 3589 |
mnt_ctx->tcon = NULL; |
| 3590 |
|
| 3591 |
return rc; |
| 3592 |
} |
| 3593 |
|
| 3594 |
int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx) |
| 3595 |
{ |
| 3596 |
struct TCP_Server_Info *server; |
| 3597 |
struct cifs_tcon *tcon = NULL; |
| 3598 |
struct cifs_sb_info *cifs_sb; |
| 3599 |
struct smb3_fs_context *ctx; |
| 3600 |
unsigned int sbflags; |
| 3601 |
int rc = 0; |
| 3602 |
|
| 3603 |
if (WARN_ON_ONCE(!mnt_ctx)) |
| 3604 |
return -EINVAL; |
| 3605 |
if (WARN_ON_ONCE(!mnt_ctx->server || !mnt_ctx->ses || |
| 3606 |
!mnt_ctx->fs_ctx || !mnt_ctx->cifs_sb)) { |
| 3607 |
mnt_ctx->tcon = NULL; |
| 3608 |
return -EINVAL; |
| 3609 |
} |
| 3610 |
server = mnt_ctx->server; |
| 3611 |
ctx = mnt_ctx->fs_ctx; |
| 3612 |
cifs_sb = mnt_ctx->cifs_sb; |
| 3613 |
|
| 3614 |
/* search for existing tcon to this server share */ |
| 3615 |
tcon = cifs_get_tcon(mnt_ctx->ses, ctx); |
| 3616 |
if (IS_ERR(tcon)) { |
| 3617 |
rc = PTR_ERR(tcon); |
| 3618 |
tcon = NULL; |
| 3619 |
goto out; |
| 3620 |
} |
| 3621 |
|
| 3622 |
/* |
| 3623 |
* if new SMB3.11 POSIX extensions are supported, do not change anything in the |
| 3624 |
* path (i.e., do not remap / and \ and do not map any special characters) |
| 3625 |
*/ |
| 3626 |
if (tcon->posix_extensions) { |
| 3627 |
atomic_or(CIFS_MOUNT_POSIX_PATHS, &cifs_sb->mnt_cifs_flags); |
| 3628 |
atomic_andnot(CIFS_MOUNT_MAP_SFM_CHR | |
| 3629 |
CIFS_MOUNT_MAP_SPECIAL_CHR, |
| 3630 |
&cifs_sb->mnt_cifs_flags); |
| 3631 |
} |
| 3632 |
|
| 3633 |
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 3634 |
/* tell server which Unix caps we support */ |
| 3635 |
if (cap_unix(tcon->ses)) { |
| 3636 |
/* |
| 3637 |
* reset of caps checks mount to see if unix extensions disabled |
| 3638 |
* for just this mount. |
| 3639 |
*/ |
| 3640 |
reset_cifs_unix_caps(mnt_ctx->xid, tcon, cifs_sb, ctx); |
| 3641 |
spin_lock(&tcon->ses->server->srv_lock); |
| 3642 |
if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) && |
| 3643 |
(le64_to_cpu(tcon->fsUnixInfo.Capability) & |
| 3644 |
CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) { |
| 3645 |
spin_unlock(&tcon->ses->server->srv_lock); |
| 3646 |
rc = -EACCES; |
| 3647 |
goto out; |
| 3648 |
} |
| 3649 |
spin_unlock(&tcon->ses->server->srv_lock); |
| 3650 |
} else |
| 3651 |
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 3652 |
tcon->unix_ext = 0; /* server does not support them */ |
| 3653 |
|
| 3654 |
sbflags = cifs_sb_flags(cifs_sb); |
| 3655 |
/* do not care if a following call succeed - informational */ |
| 3656 |
if (!tcon->pipe && server->ops->qfs_tcon) { |
| 3657 |
server->ops->qfs_tcon(mnt_ctx->xid, tcon, cifs_sb); |
| 3658 |
if (sbflags & CIFS_MOUNT_RO_CACHE) { |
| 3659 |
if (tcon->fsDevInfo.DeviceCharacteristics & |
| 3660 |
cpu_to_le32(FILE_READ_ONLY_DEVICE)) |
| 3661 |
cifs_dbg(VFS, "mounted to read only share\n"); |
| 3662 |
else if (!(sbflags & CIFS_MOUNT_RW_CACHE)) |
| 3663 |
cifs_dbg(VFS, "read only mount of RW share\n"); |
| 3664 |
/* no need to log a RW mount of a typical RW share */ |
| 3665 |
} |
| 3666 |
} |
| 3667 |
|
| 3668 |
cifs_negotiate_iosize(server, cifs_sb->ctx, tcon); |
| 3669 |
/* |
| 3670 |
* The cookie is initialized from volume info returned above. |
| 3671 |
* Inside cifs_fscache_get_super_cookie it checks |
| 3672 |
* that we do not get super cookie twice. |
| 3673 |
*/ |
| 3674 |
if (sbflags & CIFS_MOUNT_FSCACHE) |
| 3675 |
cifs_fscache_get_super_cookie(tcon); |
| 3676 |
|
| 3677 |
out: |
| 3678 |
mnt_ctx->tcon = tcon; |
| 3679 |
return rc; |
| 3680 |
} |
| 3681 |
|
| 3682 |
static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses, |
| 3683 |
struct cifs_tcon *tcon) |
| 3684 |
{ |
| 3685 |
struct tcon_link *tlink; |
| 3686 |
|
| 3687 |
/* hang the tcon off of the superblock */ |
| 3688 |
tlink = kzalloc_obj(*tlink); |
| 3689 |
if (tlink == NULL) |
| 3690 |
return -ENOMEM; |
| 3691 |
|
| 3692 |
tlink->tl_uid = ses->linux_uid; |
| 3693 |
tlink->tl_tcon = tcon; |
| 3694 |
tlink->tl_time = jiffies; |
| 3695 |
set_bit(TCON_LINK_MASTER, &tlink->tl_flags); |
| 3696 |
set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); |
| 3697 |
|
| 3698 |
cifs_sb->master_tlink = tlink; |
| 3699 |
spin_lock(&cifs_sb->tlink_tree_lock); |
| 3700 |
tlink_rb_insert(&cifs_sb->tlink_tree, tlink); |
| 3701 |
spin_unlock(&cifs_sb->tlink_tree_lock); |
| 3702 |
|
| 3703 |
spin_lock(&tcon->sb_list_lock); |
| 3704 |
list_add(&cifs_sb->tcon_sb_link, &tcon->cifs_sb_list); |
| 3705 |
spin_unlock(&tcon->sb_list_lock); |
| 3706 |
|
| 3707 |
queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, |
| 3708 |
TLINK_IDLE_EXPIRE); |
| 3709 |
return 0; |
| 3710 |
} |
| 3711 |
|
| 3712 |
static int |
| 3713 |
cifs_are_all_path_components_accessible(struct TCP_Server_Info *server, |
| 3714 |
unsigned int xid, |
| 3715 |
struct cifs_tcon *tcon, |
| 3716 |
struct cifs_sb_info *cifs_sb, |
| 3717 |
char *full_path, |
| 3718 |
int added_treename) |
| 3719 |
{ |
| 3720 |
int rc; |
| 3721 |
char *s; |
| 3722 |
char sep, tmp; |
| 3723 |
int skip = added_treename ? 1 : 0; |
| 3724 |
|
| 3725 |
sep = CIFS_DIR_SEP(cifs_sb); |
| 3726 |
s = full_path; |
| 3727 |
|
| 3728 |
rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, ""); |
| 3729 |
while (rc == 0) { |
| 3730 |
/* skip separators */ |
| 3731 |
while (*s == sep) |
| 3732 |
s++; |
| 3733 |
if (!*s) |
| 3734 |
break; |
| 3735 |
/* next separator */ |
| 3736 |
while (*s && *s != sep) |
| 3737 |
s++; |
| 3738 |
/* |
| 3739 |
* if the treename is added, we then have to skip the first |
| 3740 |
* part within the separators |
| 3741 |
*/ |
| 3742 |
if (skip) { |
| 3743 |
skip = 0; |
| 3744 |
continue; |
| 3745 |
} |
| 3746 |
/* |
| 3747 |
* temporarily null-terminate the path at the end of |
| 3748 |
* the current component |
| 3749 |
*/ |
| 3750 |
tmp = *s; |
| 3751 |
*s = 0; |
| 3752 |
rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, |
| 3753 |
full_path); |
| 3754 |
*s = tmp; |
| 3755 |
} |
| 3756 |
return rc; |
| 3757 |
} |
| 3758 |
|
| 3759 |
/* |
| 3760 |
* Check if path is remote (i.e. a DFS share). |
| 3761 |
* |
| 3762 |
* Return -EREMOTE if it is, otherwise 0 or -errno. |
| 3763 |
*/ |
| 3764 |
int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx) |
| 3765 |
{ |
| 3766 |
int rc; |
| 3767 |
struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3768 |
struct TCP_Server_Info *server = mnt_ctx->server; |
| 3769 |
unsigned int xid = mnt_ctx->xid; |
| 3770 |
struct cifs_tcon *tcon = mnt_ctx->tcon; |
| 3771 |
struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; |
| 3772 |
char *full_path; |
| 3773 |
|
| 3774 |
if (!server->ops->is_path_accessible) |
| 3775 |
return -EOPNOTSUPP; |
| 3776 |
|
| 3777 |
/* |
| 3778 |
* cifs_build_path_to_root works only when we have a valid tcon |
| 3779 |
*/ |
| 3780 |
full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon, |
| 3781 |
tcon->Flags & SMB_SHARE_IS_IN_DFS); |
| 3782 |
if (full_path == NULL) |
| 3783 |
return -ENOMEM; |
| 3784 |
|
| 3785 |
cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path); |
| 3786 |
|
| 3787 |
rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, |
| 3788 |
full_path); |
| 3789 |
if (rc != 0 && rc != -EREMOTE) |
| 3790 |
goto out; |
| 3791 |
|
| 3792 |
if (rc != -EREMOTE) { |
| 3793 |
rc = cifs_are_all_path_components_accessible(server, xid, tcon, |
| 3794 |
cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS); |
| 3795 |
if (rc != 0) { |
| 3796 |
cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n"); |
| 3797 |
atomic_or(CIFS_MOUNT_USE_PREFIX_PATH, |
| 3798 |
&cifs_sb->mnt_cifs_flags); |
| 3799 |
rc = 0; |
| 3800 |
} |
| 3801 |
} |
| 3802 |
|
| 3803 |
out: |
| 3804 |
kfree(full_path); |
| 3805 |
return rc; |
| 3806 |
} |
| 3807 |
|
| 3808 |
static struct mchan_mount * |
| 3809 |
mchan_mount_alloc(struct cifs_ses *ses) |
| 3810 |
{ |
| 3811 |
struct mchan_mount *mchan_mount; |
| 3812 |
|
| 3813 |
mchan_mount = kzalloc_obj(*mchan_mount); |
| 3814 |
if (!mchan_mount) |
| 3815 |
return ERR_PTR(-ENOMEM); |
| 3816 |
|
| 3817 |
INIT_WORK(&mchan_mount->work, mchan_mount_work_fn); |
| 3818 |
|
| 3819 |
spin_lock(&cifs_tcp_ses_lock); |
| 3820 |
cifs_smb_ses_inc_refcount(ses); |
| 3821 |
spin_unlock(&cifs_tcp_ses_lock); |
| 3822 |
mchan_mount->ses = ses; |
| 3823 |
|
| 3824 |
return mchan_mount; |
| 3825 |
} |
| 3826 |
|
| 3827 |
static void |
| 3828 |
mchan_mount_free(struct mchan_mount *mchan_mount) |
| 3829 |
{ |
| 3830 |
cifs_put_smb_ses(mchan_mount->ses); |
| 3831 |
kfree(mchan_mount); |
| 3832 |
} |
| 3833 |
|
| 3834 |
static void |
| 3835 |
mchan_mount_work_fn(struct work_struct *work) |
| 3836 |
{ |
| 3837 |
struct mchan_mount *mchan_mount = container_of(work, struct mchan_mount, work); |
| 3838 |
|
| 3839 |
smb3_update_ses_channels(mchan_mount->ses, |
| 3840 |
mchan_mount->ses->server, |
| 3841 |
false /* from_reconnect */, |
| 3842 |
false /* disable_mchan */); |
| 3843 |
|
| 3844 |
mchan_mount_free(mchan_mount); |
| 3845 |
} |
| 3846 |
|
| 3847 |
#ifdef CONFIG_CIFS_DFS_UPCALL |
| 3848 |
int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) |
| 3849 |
{ |
| 3850 |
struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, }; |
| 3851 |
struct mchan_mount *mchan_mount = NULL; |
| 3852 |
int rc; |
| 3853 |
|
| 3854 |
rc = dfs_mount_share(&mnt_ctx); |
| 3855 |
if (rc) |
| 3856 |
goto error; |
| 3857 |
|
| 3858 |
if (ctx->multichannel) { |
| 3859 |
mchan_mount = mchan_mount_alloc(mnt_ctx.ses); |
| 3860 |
if (IS_ERR(mchan_mount)) { |
| 3861 |
rc = PTR_ERR(mchan_mount); |
| 3862 |
goto error; |
| 3863 |
} |
| 3864 |
} |
| 3865 |
|
| 3866 |
if (!ctx->dfs_conn) |
| 3867 |
goto out; |
| 3868 |
|
| 3869 |
/* |
| 3870 |
* After reconnecting to a different server, unique ids won't match anymore, so we disable |
| 3871 |
* serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE). |
| 3872 |
*/ |
| 3873 |
cifs_autodisable_serverino(cifs_sb); |
| 3874 |
/* |
| 3875 |
* Force the use of prefix path to support failover on DFS paths that resolve to targets |
| 3876 |
* that have different prefix paths. |
| 3877 |
*/ |
| 3878 |
atomic_or(CIFS_MOUNT_USE_PREFIX_PATH, &cifs_sb->mnt_cifs_flags); |
| 3879 |
kfree(cifs_sb->prepath); |
| 3880 |
cifs_sb->prepath = ctx->prepath; |
| 3881 |
ctx->prepath = NULL; |
| 3882 |
|
| 3883 |
out: |
| 3884 |
rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon); |
| 3885 |
if (rc) |
| 3886 |
goto error; |
| 3887 |
|
| 3888 |
if (ctx->multichannel) |
| 3889 |
queue_work(cifsiod_wq, &mchan_mount->work); |
| 3890 |
|
| 3891 |
free_xid(mnt_ctx.xid); |
| 3892 |
return rc; |
| 3893 |
|
| 3894 |
error: |
| 3895 |
if (ctx->multichannel && !IS_ERR_OR_NULL(mchan_mount)) |
| 3896 |
mchan_mount_free(mchan_mount); |
| 3897 |
cifs_mount_put_conns(&mnt_ctx); |
| 3898 |
return rc; |
| 3899 |
} |
| 3900 |
#else |
| 3901 |
int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) |
| 3902 |
{ |
| 3903 |
int rc = 0; |
| 3904 |
struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, }; |
| 3905 |
struct mchan_mount *mchan_mount = NULL; |
| 3906 |
|
| 3907 |
rc = cifs_mount_get_session(&mnt_ctx); |
| 3908 |
if (rc) |
| 3909 |
goto error; |
| 3910 |
|
| 3911 |
rc = cifs_mount_get_tcon(&mnt_ctx); |
| 3912 |
if (!rc) { |
| 3913 |
/* |
| 3914 |
* Prevent superblock from being created with any missing |
| 3915 |
* connections. |
| 3916 |
*/ |
| 3917 |
if (WARN_ON(!mnt_ctx.server)) |
| 3918 |
rc = -EHOSTDOWN; |
| 3919 |
else if (WARN_ON(!mnt_ctx.ses)) |
| 3920 |
rc = -EACCES; |
| 3921 |
else if (WARN_ON(!mnt_ctx.tcon)) |
| 3922 |
rc = -ENOENT; |
| 3923 |
} |
| 3924 |
if (rc) |
| 3925 |
goto error; |
| 3926 |
|
| 3927 |
rc = cifs_is_path_remote(&mnt_ctx); |
| 3928 |
if (rc == -EREMOTE) |
| 3929 |
rc = -EOPNOTSUPP; |
| 3930 |
if (rc) |
| 3931 |
goto error; |
| 3932 |
|
| 3933 |
if (ctx->multichannel) { |
| 3934 |
mchan_mount = mchan_mount_alloc(mnt_ctx.ses); |
| 3935 |
if (IS_ERR(mchan_mount)) { |
| 3936 |
rc = PTR_ERR(mchan_mount); |
| 3937 |
goto error; |
| 3938 |
} |
| 3939 |
} |
| 3940 |
|
| 3941 |
rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon); |
| 3942 |
if (rc) |
| 3943 |
goto error; |
| 3944 |
|
| 3945 |
if (ctx->multichannel) |
| 3946 |
queue_work(cifsiod_wq, &mchan_mount->work); |
| 3947 |
|
| 3948 |
free_xid(mnt_ctx.xid); |
| 3949 |
return rc; |
| 3950 |
|
| 3951 |
error: |
| 3952 |
if (ctx->multichannel && !IS_ERR_OR_NULL(mchan_mount)) |
| 3953 |
mchan_mount_free(mchan_mount); |
| 3954 |
cifs_mount_put_conns(&mnt_ctx); |
| 3955 |
return rc; |
| 3956 |
} |
| 3957 |
#endif |
| 3958 |
|
| 3959 |
static void delayed_free(struct rcu_head *p) |
| 3960 |
{ |
| 3961 |
struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu); |
| 3962 |
|
| 3963 |
unload_nls(cifs_sb->local_nls); |
| 3964 |
smb3_cleanup_fs_context(cifs_sb->ctx); |
| 3965 |
kfree(cifs_sb); |
| 3966 |
} |
| 3967 |
|
| 3968 |
void |
| 3969 |
cifs_umount(struct cifs_sb_info *cifs_sb) |
| 3970 |
{ |
| 3971 |
struct rb_root *root = &cifs_sb->tlink_tree; |
| 3972 |
struct rb_node *node; |
| 3973 |
struct tcon_link *tlink; |
| 3974 |
struct cifs_tcon *tcon = NULL; |
| 3975 |
|
| 3976 |
cancel_delayed_work_sync(&cifs_sb->prune_tlinks); |
| 3977 |
|
| 3978 |
if (cifs_sb->master_tlink) { |
| 3979 |
tcon = cifs_sb->master_tlink->tl_tcon; |
| 3980 |
if (tcon) { |
| 3981 |
spin_lock(&tcon->sb_list_lock); |
| 3982 |
list_del_init(&cifs_sb->tcon_sb_link); |
| 3983 |
spin_unlock(&tcon->sb_list_lock); |
| 3984 |
} |
| 3985 |
} |
| 3986 |
|
| 3987 |
spin_lock(&cifs_sb->tlink_tree_lock); |
| 3988 |
while ((node = rb_first(root))) { |
| 3989 |
tlink = rb_entry(node, struct tcon_link, tl_rbnode); |
| 3990 |
cifs_get_tlink(tlink); |
| 3991 |
clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); |
| 3992 |
rb_erase(node, root); |
| 3993 |
|
| 3994 |
spin_unlock(&cifs_sb->tlink_tree_lock); |
| 3995 |
cifs_put_tlink(tlink); |
| 3996 |
spin_lock(&cifs_sb->tlink_tree_lock); |
| 3997 |
} |
| 3998 |
spin_unlock(&cifs_sb->tlink_tree_lock); |
| 3999 |
|
| 4000 |
kfree(cifs_sb->prepath); |
| 4001 |
call_rcu(&cifs_sb->rcu, delayed_free); |
| 4002 |
} |
| 4003 |
|
| 4004 |
int |
| 4005 |
cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses, |
| 4006 |
struct TCP_Server_Info *server) |
| 4007 |
{ |
| 4008 |
bool in_retry = false; |
| 4009 |
int rc = 0; |
| 4010 |
|
| 4011 |
if (!server->ops->need_neg || !server->ops->negotiate) |
| 4012 |
return -ENOSYS; |
| 4013 |
|
| 4014 |
retry: |
| 4015 |
/* only send once per connect */ |
| 4016 |
spin_lock(&server->srv_lock); |
| 4017 |
if (server->tcpStatus != CifsGood && |
| 4018 |
server->tcpStatus != CifsNew && |
| 4019 |
server->tcpStatus != CifsNeedNegotiate) { |
| 4020 |
spin_unlock(&server->srv_lock); |
| 4021 |
return -EHOSTDOWN; |
| 4022 |
} |
| 4023 |
|
| 4024 |
if (!server->ops->need_neg(server) && |
| 4025 |
server->tcpStatus == CifsGood) { |
| 4026 |
spin_unlock(&server->srv_lock); |
| 4027 |
return 0; |
| 4028 |
} |
| 4029 |
|
| 4030 |
server->tcpStatus = CifsInNegotiate; |
| 4031 |
server->neg_start = jiffies; |
| 4032 |
spin_unlock(&server->srv_lock); |
| 4033 |
|
| 4034 |
rc = server->ops->negotiate(xid, ses, server); |
| 4035 |
if (rc == -EAGAIN) { |
| 4036 |
/* Allow one retry attempt */ |
| 4037 |
if (!in_retry) { |
| 4038 |
in_retry = true; |
| 4039 |
goto retry; |
| 4040 |
} |
| 4041 |
rc = -EHOSTDOWN; |
| 4042 |
} |
| 4043 |
if (rc == 0) { |
| 4044 |
spin_lock(&server->srv_lock); |
| 4045 |
if (server->tcpStatus == CifsInNegotiate) |
| 4046 |
server->tcpStatus = CifsGood; |
| 4047 |
else |
| 4048 |
rc = -EHOSTDOWN; |
| 4049 |
spin_unlock(&server->srv_lock); |
| 4050 |
} else { |
| 4051 |
spin_lock(&server->srv_lock); |
| 4052 |
if (server->tcpStatus == CifsInNegotiate) |
| 4053 |
server->tcpStatus = CifsNeedNegotiate; |
| 4054 |
spin_unlock(&server->srv_lock); |
| 4055 |
} |
| 4056 |
|
| 4057 |
return rc; |
| 4058 |
} |
| 4059 |
|
| 4060 |
int |
| 4061 |
cifs_setup_session(const unsigned int xid, struct cifs_ses *ses, |
| 4062 |
struct TCP_Server_Info *server, |
| 4063 |
struct nls_table *nls_info) |
| 4064 |
{ |
| 4065 |
int rc = 0; |
| 4066 |
struct TCP_Server_Info *pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; |
| 4067 |
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr; |
| 4068 |
struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr; |
| 4069 |
bool is_binding = false; |
| 4070 |
bool new_ses; |
| 4071 |
|
| 4072 |
spin_lock(&ses->ses_lock); |
| 4073 |
new_ses = ses->ses_status == SES_NEW; |
| 4074 |
cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n", |
| 4075 |
__func__, ses->chans_need_reconnect); |
| 4076 |
|
| 4077 |
if (ses->ses_status != SES_GOOD && |
| 4078 |
ses->ses_status != SES_NEW && |
| 4079 |
ses->ses_status != SES_NEED_RECON) { |
| 4080 |
spin_unlock(&ses->ses_lock); |
| 4081 |
return -EHOSTDOWN; |
| 4082 |
} |
| 4083 |
|
| 4084 |
/* only send once per connect */ |
| 4085 |
spin_lock(&ses->chan_lock); |
| 4086 |
if (CIFS_ALL_CHANS_GOOD(ses)) { |
| 4087 |
if (ses->ses_status == SES_NEED_RECON) |
| 4088 |
ses->ses_status = SES_GOOD; |
| 4089 |
spin_unlock(&ses->chan_lock); |
| 4090 |
spin_unlock(&ses->ses_lock); |
| 4091 |
return 0; |
| 4092 |
} |
| 4093 |
|
| 4094 |
cifs_chan_set_in_reconnect(ses, server); |
| 4095 |
is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses); |
| 4096 |
spin_unlock(&ses->chan_lock); |
| 4097 |
|
| 4098 |
if (!is_binding) { |
| 4099 |
ses->ses_status = SES_IN_SETUP; |
| 4100 |
|
| 4101 |
/* force iface_list refresh */ |
| 4102 |
spin_lock(&ses->iface_lock); |
| 4103 |
ses->iface_last_update = 0; |
| 4104 |
spin_unlock(&ses->iface_lock); |
| 4105 |
} |
| 4106 |
spin_unlock(&ses->ses_lock); |
| 4107 |
|
| 4108 |
/* update ses ip_addr only for primary chan */ |
| 4109 |
if (server == pserver) { |
| 4110 |
if (server->dstaddr.ss_family == AF_INET6) |
| 4111 |
scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr); |
| 4112 |
else |
| 4113 |
scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr); |
| 4114 |
} |
| 4115 |
|
| 4116 |
if (!is_binding) { |
| 4117 |
ses->capabilities = server->capabilities; |
| 4118 |
if (!linuxExtEnabled) |
| 4119 |
ses->capabilities &= (~server->vals->cap_unix); |
| 4120 |
|
| 4121 |
/* |
| 4122 |
* Check if the server supports specified encoding mode. |
| 4123 |
* Zero value in vals->cap_unicode indidcates that chosen |
| 4124 |
* protocol dialect does not support non-UNICODE mode. |
| 4125 |
*/ |
| 4126 |
if (ses->unicode == 1 && server->vals->cap_unicode != 0 && |
| 4127 |
!(server->capabilities & server->vals->cap_unicode)) { |
| 4128 |
cifs_dbg(VFS, "Server does not support mounting in UNICODE mode\n"); |
| 4129 |
rc = -EOPNOTSUPP; |
| 4130 |
} else if (ses->unicode == 0 && server->vals->cap_unicode == 0) { |
| 4131 |
cifs_dbg(VFS, "Server does not support mounting in non-UNICODE mode\n"); |
| 4132 |
rc = -EOPNOTSUPP; |
| 4133 |
} else if (ses->unicode == 0) { |
| 4134 |
/* |
| 4135 |
* When UNICODE mode was explicitly disabled then |
| 4136 |
* do not announce client UNICODE capability. |
| 4137 |
*/ |
| 4138 |
ses->capabilities &= (~server->vals->cap_unicode); |
| 4139 |
} |
| 4140 |
|
| 4141 |
if (ses->auth_key.response) { |
| 4142 |
cifs_dbg(FYI, "Free previous auth_key.response = %p\n", |
| 4143 |
ses->auth_key.response); |
| 4144 |
kfree_sensitive(ses->auth_key.response); |
| 4145 |
ses->auth_key.response = NULL; |
| 4146 |
ses->auth_key.len = 0; |
| 4147 |
} |
| 4148 |
} |
| 4149 |
|
| 4150 |
cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n", |
| 4151 |
server->sec_mode, server->capabilities, server->timeAdj); |
| 4152 |
|
| 4153 |
if (!rc) { |
| 4154 |
if (server->ops->sess_setup) |
| 4155 |
rc = server->ops->sess_setup(xid, ses, server, nls_info); |
| 4156 |
else |
| 4157 |
rc = -ENOSYS; |
| 4158 |
} |
| 4159 |
|
| 4160 |
if (rc) { |
| 4161 |
if (new_ses) { |
| 4162 |
cifs_server_dbg(VFS, "failed to create a new SMB session with %s: %d\n", |
| 4163 |
get_security_type_str(ses->sectype), rc); |
| 4164 |
} |
| 4165 |
spin_lock(&ses->ses_lock); |
| 4166 |
if (ses->ses_status == SES_IN_SETUP) |
| 4167 |
ses->ses_status = SES_NEED_RECON; |
| 4168 |
spin_lock(&ses->chan_lock); |
| 4169 |
cifs_chan_clear_in_reconnect(ses, server); |
| 4170 |
spin_unlock(&ses->chan_lock); |
| 4171 |
spin_unlock(&ses->ses_lock); |
| 4172 |
} else { |
| 4173 |
spin_lock(&ses->ses_lock); |
| 4174 |
if (ses->ses_status == SES_IN_SETUP) |
| 4175 |
ses->ses_status = SES_GOOD; |
| 4176 |
spin_lock(&ses->chan_lock); |
| 4177 |
cifs_chan_clear_in_reconnect(ses, server); |
| 4178 |
cifs_chan_clear_need_reconnect(ses, server); |
| 4179 |
spin_unlock(&ses->chan_lock); |
| 4180 |
spin_unlock(&ses->ses_lock); |
| 4181 |
} |
| 4182 |
|
| 4183 |
return rc; |
| 4184 |
} |
| 4185 |
|
| 4186 |
static int |
| 4187 |
cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses) |
| 4188 |
{ |
| 4189 |
ctx->sectype = ses->sectype; |
| 4190 |
|
| 4191 |
/* krb5 is special, since we don't need username or pw */ |
| 4192 |
if (ctx->sectype == Kerberos) |
| 4193 |
return 0; |
| 4194 |
|
| 4195 |
return cifs_set_cifscreds(ctx, ses); |
| 4196 |
} |
| 4197 |
|
| 4198 |
static struct cifs_tcon * |
| 4199 |
cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) |
| 4200 |
{ |
| 4201 |
int rc; |
| 4202 |
struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb); |
| 4203 |
struct cifs_ses *ses; |
| 4204 |
struct cifs_tcon *tcon = NULL; |
| 4205 |
struct smb3_fs_context *ctx; |
| 4206 |
char *origin_fullpath = NULL; |
| 4207 |
|
| 4208 |
ctx = kzalloc_obj(*ctx); |
| 4209 |
if (ctx == NULL) |
| 4210 |
return ERR_PTR(-ENOMEM); |
| 4211 |
|
| 4212 |
ctx->local_nls = cifs_sb->local_nls; |
| 4213 |
ctx->linux_uid = fsuid; |
| 4214 |
ctx->cred_uid = fsuid; |
| 4215 |
ctx->UNC = master_tcon->tree_name; |
| 4216 |
ctx->retry = master_tcon->retry; |
| 4217 |
ctx->nocase = master_tcon->nocase; |
| 4218 |
ctx->nohandlecache = master_tcon->nohandlecache; |
| 4219 |
ctx->local_lease = master_tcon->local_lease; |
| 4220 |
ctx->no_lease = master_tcon->no_lease; |
| 4221 |
ctx->resilient = master_tcon->use_resilient; |
| 4222 |
ctx->persistent = master_tcon->use_persistent; |
| 4223 |
ctx->handle_timeout = master_tcon->handle_timeout; |
| 4224 |
ctx->no_linux_ext = !master_tcon->unix_ext; |
| 4225 |
ctx->linux_ext = master_tcon->posix_extensions; |
| 4226 |
ctx->sectype = master_tcon->ses->sectype; |
| 4227 |
ctx->sign = master_tcon->ses->sign; |
| 4228 |
ctx->seal = master_tcon->seal; |
| 4229 |
ctx->witness = master_tcon->use_witness; |
| 4230 |
ctx->dfs_root_ses = master_tcon->ses->dfs_root_ses; |
| 4231 |
ctx->unicode = master_tcon->ses->unicode; |
| 4232 |
|
| 4233 |
rc = cifs_set_vol_auth(ctx, master_tcon->ses); |
| 4234 |
if (rc) { |
| 4235 |
tcon = ERR_PTR(rc); |
| 4236 |
goto out; |
| 4237 |
} |
| 4238 |
|
| 4239 |
/* get a reference for the same TCP session */ |
| 4240 |
spin_lock(&cifs_tcp_ses_lock); |
| 4241 |
++master_tcon->ses->server->srv_count; |
| 4242 |
spin_unlock(&cifs_tcp_ses_lock); |
| 4243 |
|
| 4244 |
ses = cifs_get_smb_ses(master_tcon->ses->server, ctx); |
| 4245 |
if (IS_ERR(ses)) { |
| 4246 |
tcon = ERR_CAST(ses); |
| 4247 |
cifs_put_tcp_session(master_tcon->ses->server, 0); |
| 4248 |
goto out; |
| 4249 |
} |
| 4250 |
|
| 4251 |
#ifdef CONFIG_CIFS_DFS_UPCALL |
| 4252 |
spin_lock(&master_tcon->tc_lock); |
| 4253 |
if (master_tcon->origin_fullpath) { |
| 4254 |
spin_unlock(&master_tcon->tc_lock); |
| 4255 |
origin_fullpath = dfs_get_path(cifs_sb, cifs_sb->ctx->source); |
| 4256 |
if (IS_ERR(origin_fullpath)) { |
| 4257 |
tcon = ERR_CAST(origin_fullpath); |
| 4258 |
origin_fullpath = NULL; |
| 4259 |
cifs_put_smb_ses(ses); |
| 4260 |
goto out; |
| 4261 |
} |
| 4262 |
} else { |
| 4263 |
spin_unlock(&master_tcon->tc_lock); |
| 4264 |
} |
| 4265 |
#endif |
| 4266 |
|
| 4267 |
tcon = cifs_get_tcon(ses, ctx); |
| 4268 |
if (IS_ERR(tcon)) { |
| 4269 |
cifs_put_smb_ses(ses); |
| 4270 |
goto out; |
| 4271 |
} |
| 4272 |
|
| 4273 |
#ifdef CONFIG_CIFS_DFS_UPCALL |
| 4274 |
if (origin_fullpath) { |
| 4275 |
spin_lock(&tcon->tc_lock); |
| 4276 |
tcon->origin_fullpath = origin_fullpath; |
| 4277 |
spin_unlock(&tcon->tc_lock); |
| 4278 |
origin_fullpath = NULL; |
| 4279 |
queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work, |
| 4280 |
dfs_cache_get_ttl() * HZ); |
| 4281 |
} |
| 4282 |
#endif |
| 4283 |
|
| 4284 |
#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 4285 |
if (cap_unix(ses)) |
| 4286 |
reset_cifs_unix_caps(0, tcon, NULL, ctx); |
| 4287 |
#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 4288 |
|
| 4289 |
out: |
| 4290 |
kfree(ctx->username); |
| 4291 |
kfree(ctx->domainname); |
| 4292 |
kfree_sensitive(ctx->password); |
| 4293 |
kfree(origin_fullpath); |
| 4294 |
kfree(ctx); |
| 4295 |
|
| 4296 |
return tcon; |
| 4297 |
} |
| 4298 |
|
| 4299 |
struct cifs_tcon * |
| 4300 |
cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb) |
| 4301 |
{ |
| 4302 |
return tlink_tcon(cifs_sb_master_tlink(cifs_sb)); |
| 4303 |
} |
| 4304 |
|
| 4305 |
/* find and return a tlink with given uid */ |
| 4306 |
static struct tcon_link * |
| 4307 |
tlink_rb_search(struct rb_root *root, kuid_t uid) |
| 4308 |
{ |
| 4309 |
struct rb_node *node = root->rb_node; |
| 4310 |
struct tcon_link *tlink; |
| 4311 |
|
| 4312 |
while (node) { |
| 4313 |
tlink = rb_entry(node, struct tcon_link, tl_rbnode); |
| 4314 |
|
| 4315 |
if (uid_gt(tlink->tl_uid, uid)) |
| 4316 |
node = node->rb_left; |
| 4317 |
else if (uid_lt(tlink->tl_uid, uid)) |
| 4318 |
node = node->rb_right; |
| 4319 |
else |
| 4320 |
return tlink; |
| 4321 |
} |
| 4322 |
return NULL; |
| 4323 |
} |
| 4324 |
|
| 4325 |
/* insert a tcon_link into the tree */ |
| 4326 |
static void |
| 4327 |
tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink) |
| 4328 |
{ |
| 4329 |
struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 4330 |
struct tcon_link *tlink; |
| 4331 |
|
| 4332 |
while (*new) { |
| 4333 |
tlink = rb_entry(*new, struct tcon_link, tl_rbnode); |
| 4334 |
parent = *new; |
| 4335 |
|
| 4336 |
if (uid_gt(tlink->tl_uid, new_tlink->tl_uid)) |
| 4337 |
new = &((*new)->rb_left); |
| 4338 |
else |
| 4339 |
new = &((*new)->rb_right); |
| 4340 |
} |
| 4341 |
|
| 4342 |
rb_link_node(&new_tlink->tl_rbnode, parent, new); |
| 4343 |
rb_insert_color(&new_tlink->tl_rbnode, root); |
| 4344 |
} |
| 4345 |
|
| 4346 |
/* |
| 4347 |
* Find or construct an appropriate tcon given a cifs_sb and the fsuid of the |
| 4348 |
* current task. |
| 4349 |
* |
| 4350 |
* If the superblock doesn't refer to a multiuser mount, then just return |
| 4351 |
* the master tcon for the mount. |
| 4352 |
* |
| 4353 |
* First, search the rbtree for an existing tcon for this fsuid. If one |
| 4354 |
* exists, then check to see if it's pending construction. If it is then wait |
| 4355 |
* for construction to complete. Once it's no longer pending, check to see if |
| 4356 |
* it failed and either return an error or retry construction, depending on |
| 4357 |
* the timeout. |
| 4358 |
* |
| 4359 |
* If one doesn't exist then insert a new tcon_link struct into the tree and |
| 4360 |
* try to construct a new one. |
| 4361 |
* |
| 4362 |
* REMEMBER to call cifs_put_tlink() after successful calls to cifs_sb_tlink, |
| 4363 |
* to avoid refcount issues |
| 4364 |
*/ |
| 4365 |
struct tcon_link * |
| 4366 |
cifs_sb_tlink(struct cifs_sb_info *cifs_sb) |
| 4367 |
{ |
| 4368 |
struct tcon_link *tlink, *newtlink; |
| 4369 |
kuid_t fsuid = current_fsuid(); |
| 4370 |
int err; |
| 4371 |
|
| 4372 |
if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_MULTIUSER)) |
| 4373 |
return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); |
| 4374 |
|
| 4375 |
spin_lock(&cifs_sb->tlink_tree_lock); |
| 4376 |
tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); |
| 4377 |
if (tlink) |
| 4378 |
cifs_get_tlink(tlink); |
| 4379 |
spin_unlock(&cifs_sb->tlink_tree_lock); |
| 4380 |
|
| 4381 |
if (tlink == NULL) { |
| 4382 |
newtlink = kzalloc_obj(*tlink); |
| 4383 |
if (newtlink == NULL) |
| 4384 |
return ERR_PTR(-ENOMEM); |
| 4385 |
newtlink->tl_uid = fsuid; |
| 4386 |
newtlink->tl_tcon = ERR_PTR(-EACCES); |
| 4387 |
set_bit(TCON_LINK_PENDING, &newtlink->tl_flags); |
| 4388 |
set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags); |
| 4389 |
cifs_get_tlink(newtlink); |
| 4390 |
|
| 4391 |
spin_lock(&cifs_sb->tlink_tree_lock); |
| 4392 |
/* was one inserted after previous search? */ |
| 4393 |
tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); |
| 4394 |
if (tlink) { |
| 4395 |
cifs_get_tlink(tlink); |
| 4396 |
spin_unlock(&cifs_sb->tlink_tree_lock); |
| 4397 |
kfree(newtlink); |
| 4398 |
goto wait_for_construction; |
| 4399 |
} |
| 4400 |
tlink = newtlink; |
| 4401 |
tlink_rb_insert(&cifs_sb->tlink_tree, tlink); |
| 4402 |
spin_unlock(&cifs_sb->tlink_tree_lock); |
| 4403 |
} else { |
| 4404 |
wait_for_construction: |
| 4405 |
err = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING, |
| 4406 |
TASK_INTERRUPTIBLE); |
| 4407 |
if (err) { |
| 4408 |
cifs_put_tlink(tlink); |
| 4409 |
return ERR_PTR(-ERESTARTSYS); |
| 4410 |
} |
| 4411 |
|
| 4412 |
/* if it's good, return it */ |
| 4413 |
if (!IS_ERR(tlink->tl_tcon)) |
| 4414 |
return tlink; |
| 4415 |
|
| 4416 |
/* return error if we tried this already recently */ |
| 4417 |
if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) { |
| 4418 |
err = PTR_ERR(tlink->tl_tcon); |
| 4419 |
cifs_put_tlink(tlink); |
| 4420 |
return ERR_PTR(err); |
| 4421 |
} |
| 4422 |
|
| 4423 |
if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags)) |
| 4424 |
goto wait_for_construction; |
| 4425 |
} |
| 4426 |
|
| 4427 |
tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid); |
| 4428 |
clear_bit(TCON_LINK_PENDING, &tlink->tl_flags); |
| 4429 |
wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING); |
| 4430 |
|
| 4431 |
if (IS_ERR(tlink->tl_tcon)) { |
| 4432 |
err = PTR_ERR(tlink->tl_tcon); |
| 4433 |
if (err == -ENOKEY) |
| 4434 |
err = -EACCES; |
| 4435 |
cifs_put_tlink(tlink); |
| 4436 |
return ERR_PTR(err); |
| 4437 |
} |
| 4438 |
|
| 4439 |
return tlink; |
| 4440 |
} |
| 4441 |
|
| 4442 |
/* |
| 4443 |
* periodic workqueue job that scans tcon_tree for a superblock and closes |
| 4444 |
* out tcons. |
| 4445 |
*/ |
| 4446 |
static void |
| 4447 |
cifs_prune_tlinks(struct work_struct *work) |
| 4448 |
{ |
| 4449 |
struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info, |
| 4450 |
prune_tlinks.work); |
| 4451 |
struct rb_root *root = &cifs_sb->tlink_tree; |
| 4452 |
struct rb_node *node; |
| 4453 |
struct rb_node *tmp; |
| 4454 |
struct tcon_link *tlink; |
| 4455 |
|
| 4456 |
/* |
| 4457 |
* Because we drop the spinlock in the loop in order to put the tlink |
| 4458 |
* it's not guarded against removal of links from the tree. The only |
| 4459 |
* places that remove entries from the tree are this function and |
| 4460 |
* umounts. Because this function is non-reentrant and is canceled |
| 4461 |
* before umount can proceed, this is safe. |
| 4462 |
*/ |
| 4463 |
spin_lock(&cifs_sb->tlink_tree_lock); |
| 4464 |
node = rb_first(root); |
| 4465 |
while (node != NULL) { |
| 4466 |
tmp = node; |
| 4467 |
node = rb_next(tmp); |
| 4468 |
tlink = rb_entry(tmp, struct tcon_link, tl_rbnode); |
| 4469 |
|
| 4470 |
if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) || |
| 4471 |
atomic_read(&tlink->tl_count) != 0 || |
| 4472 |
time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies)) |
| 4473 |
continue; |
| 4474 |
|
| 4475 |
cifs_get_tlink(tlink); |
| 4476 |
clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); |
| 4477 |
rb_erase(tmp, root); |
| 4478 |
|
| 4479 |
spin_unlock(&cifs_sb->tlink_tree_lock); |
| 4480 |
cifs_put_tlink(tlink); |
| 4481 |
spin_lock(&cifs_sb->tlink_tree_lock); |
| 4482 |
} |
| 4483 |
spin_unlock(&cifs_sb->tlink_tree_lock); |
| 4484 |
|
| 4485 |
queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, |
| 4486 |
TLINK_IDLE_EXPIRE); |
| 4487 |
} |
| 4488 |
|
| 4489 |
#ifndef CONFIG_CIFS_DFS_UPCALL |
| 4490 |
int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon) |
| 4491 |
{ |
| 4492 |
const struct smb_version_operations *ops = tcon->ses->server->ops; |
| 4493 |
int rc; |
| 4494 |
|
| 4495 |
/* only send once per connect */ |
| 4496 |
spin_lock(&tcon->tc_lock); |
| 4497 |
|
| 4498 |
/* if tcon is marked for needing reconnect, update state */ |
| 4499 |
if (tcon->need_reconnect) |
| 4500 |
tcon->status = TID_NEED_TCON; |
| 4501 |
|
| 4502 |
if (tcon->status == TID_GOOD) { |
| 4503 |
spin_unlock(&tcon->tc_lock); |
| 4504 |
return 0; |
| 4505 |
} |
| 4506 |
|
| 4507 |
if (tcon->status != TID_NEW && |
| 4508 |
tcon->status != TID_NEED_TCON) { |
| 4509 |
spin_unlock(&tcon->tc_lock); |
| 4510 |
return -EHOSTDOWN; |
| 4511 |
} |
| 4512 |
|
| 4513 |
tcon->status = TID_IN_TCON; |
| 4514 |
spin_unlock(&tcon->tc_lock); |
| 4515 |
|
| 4516 |
rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, |
| 4517 |
tcon, tcon->ses->local_nls); |
| 4518 |
if (rc) { |
| 4519 |
spin_lock(&tcon->tc_lock); |
| 4520 |
if (tcon->status == TID_IN_TCON) |
| 4521 |
tcon->status = TID_NEED_TCON; |
| 4522 |
spin_unlock(&tcon->tc_lock); |
| 4523 |
} else { |
| 4524 |
spin_lock(&tcon->tc_lock); |
| 4525 |
if (tcon->status == TID_IN_TCON) |
| 4526 |
tcon->status = TID_GOOD; |
| 4527 |
tcon->need_reconnect = false; |
| 4528 |
spin_unlock(&tcon->tc_lock); |
| 4529 |
} |
| 4530 |
|
| 4531 |
return rc; |
| 4532 |
} |
| 4533 |
#endif |