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