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