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