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