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