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