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