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