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