1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2018 Nexenta Systems, Inc. All rights reserved. 24 */ 25 26 #include <sys/atomic.h> 27 #include <sys/synch.h> 28 #include <sys/types.h> 29 #include <sys/sdt.h> 30 #include <sys/random.h> 31 #include <smbsrv/netbios.h> 32 #include <smbsrv/smb2_kproto.h> 33 #include <smbsrv/string.h> 34 #include <netinet/tcp.h> 35 36 /* How many iovec we'll handle as a local array (no allocation) */ 37 #define SMB_LOCAL_IOV_MAX 16 38 39 #define SMB_NEW_KID() atomic_inc_64_nv(&smb_kids) 40 41 static volatile uint64_t smb_kids; 42 43 /* 44 * We track the keepalive in minutes, but this constant 45 * specifies it in seconds, so convert to minutes. 46 */ 47 uint32_t smb_keep_alive = SMB_PI_KEEP_ALIVE_MIN / 60; 48 49 /* 50 * There are many smbtorture test cases that send 51 * racing requests, and where the tests fail if we 52 * don't execute them in exactly the order sent. 53 * These are test bugs. The protocol makes no 54 * guarantees about execution order of requests 55 * that are concurrently active. 56 * 57 * Nonetheless, smbtorture has many useful tests, 58 * so we have this work-around we can enable to 59 * basically force sequential execution. When 60 * enabled, insert a delay after each request is 61 * issued a taskq job. Enable this with mdb by 62 * setting smb_reader_delay to 10. Don't make it 63 * more than 500 or so or the server will appear 64 * to be so slow that tests may time out. 65 */ 66 int smb_reader_delay = 0; /* mSec. */ 67 68 static int smbsr_newrq_initial(smb_request_t *); 69 70 static void smb_session_cancel(smb_session_t *); 71 static int smb_session_reader(smb_session_t *); 72 static int smb_session_xprt_puthdr(smb_session_t *, 73 uint8_t msg_type, uint32_t msg_len, 74 uint8_t *dst, size_t dstlen); 75 static smb_tree_t *smb_session_get_tree(smb_session_t *, smb_tree_t *); 76 static void smb_session_logoff(smb_session_t *); 77 static void smb_session_disconnect_trees(smb_session_t *); 78 static void smb_request_init_command_mbuf(smb_request_t *sr); 79 static void smb_session_genkey(smb_session_t *); 80 81 /* 82 * This (legacy) code is in support of an "idle timeout" feature, 83 * which is apparently incomplete. To complete it, we should: 84 * when the keep_alive timer expires, check whether the client 85 * has any open files, and if not then kill their session. 86 * Right now the timers are there, but nothing happens when 87 * a timer expires. 88 * 89 * Todo: complete logic to kill idle sessions. 90 * 91 * Only called when sv_cfg.skc_keepalive != 0 92 */ 93 void 94 smb_session_timers(smb_server_t *sv) 95 { 96 smb_session_t *session; 97 smb_llist_t *ll; 98 99 ll = &sv->sv_session_list; 100 smb_llist_enter(ll, RW_READER); 101 session = smb_llist_head(ll); 102 while (session != NULL) { 103 /* 104 * Walk through the table and decrement each keep_alive 105 * timer that has not timed out yet. (keepalive > 0) 106 */ 107 SMB_SESSION_VALID(session); 108 if (session->keep_alive && 109 (session->keep_alive != (uint32_t)-1)) 110 session->keep_alive--; 111 112 session = smb_llist_next(ll, session); 113 } 114 smb_llist_exit(ll); 115 } 116 117 /* 118 * Send a session message - supports SMB-over-NBT and SMB-over-TCP. 119 * If an mbuf chain is provided (optional), it will be freed and 120 * set to NULL -- unconditionally! (error or not) 121 * 122 * Builds a I/O vector (uio/iov) to do the send from mbufs, plus one 123 * segment for the 4-byte NBT header. 124 */ 125 int 126 smb_session_send(smb_session_t *session, uint8_t nbt_type, mbuf_chain_t *mbc) 127 { 128 uio_t uio; 129 iovec_t local_iov[SMB_LOCAL_IOV_MAX]; 130 iovec_t *alloc_iov = NULL; 131 int alloc_sz = 0; 132 mbuf_t *m; 133 uint8_t nbt_hdr[NETBIOS_HDR_SZ]; 134 uint32_t nbt_len; 135 int i, nseg; 136 int rc; 137 138 switch (session->s_state) { 139 case SMB_SESSION_STATE_DISCONNECTED: 140 case SMB_SESSION_STATE_TERMINATED: 141 rc = ENOTCONN; 142 goto out; 143 default: 144 break; 145 } 146 147 /* 148 * Setup the IOV. First, count the number of IOV segments 149 * (plus one for the NBT header) and decide whether we 150 * need to allocate an iovec or can use local_iov; 151 */ 152 bzero(&uio, sizeof (uio)); 153 nseg = 1; 154 m = (mbc != NULL) ? mbc->chain : NULL; 155 while (m != NULL) { 156 nseg++; 157 m = m->m_next; 158 } 159 if (nseg <= SMB_LOCAL_IOV_MAX) { 160 uio.uio_iov = local_iov; 161 } else { 162 alloc_sz = nseg * sizeof (iovec_t); 163 alloc_iov = kmem_alloc(alloc_sz, KM_SLEEP); 164 uio.uio_iov = alloc_iov; 165 } 166 uio.uio_iovcnt = nseg; 167 uio.uio_segflg = UIO_SYSSPACE; 168 uio.uio_extflg = UIO_COPY_DEFAULT; 169 170 /* 171 * Build the iov list, meanwhile computing the length of 172 * the SMB payload (to put in the NBT header). 173 */ 174 uio.uio_iov[0].iov_base = (void *)nbt_hdr; 175 uio.uio_iov[0].iov_len = sizeof (nbt_hdr); 176 i = 1; 177 nbt_len = 0; 178 m = (mbc != NULL) ? mbc->chain : NULL; 179 while (m != NULL) { 180 uio.uio_iov[i].iov_base = m->m_data; 181 uio.uio_iov[i++].iov_len = m->m_len; 182 nbt_len += m->m_len; 183 m = m->m_next; 184 } 185 ASSERT3S(i, ==, nseg); 186 187 /* 188 * Set the NBT header, set uio_resid 189 */ 190 uio.uio_resid = nbt_len + NETBIOS_HDR_SZ; 191 rc = smb_session_xprt_puthdr(session, nbt_type, nbt_len, 192 nbt_hdr, NETBIOS_HDR_SZ); 193 if (rc != 0) 194 goto out; 195 196 smb_server_add_txb(session->s_server, (int64_t)uio.uio_resid); 197 rc = smb_net_send_uio(session, &uio); 198 199 out: 200 if (alloc_iov != NULL) 201 kmem_free(alloc_iov, alloc_sz); 202 if ((mbc != NULL) && (mbc->chain != NULL)) { 203 m_freem(mbc->chain); 204 mbc->chain = NULL; 205 mbc->flags = 0; 206 } 207 return (rc); 208 } 209 210 /* 211 * Read, process and respond to a NetBIOS session request. 212 * 213 * A NetBIOS session must be established for SMB-over-NetBIOS. Validate 214 * the calling and called name format and save the client NetBIOS name, 215 * which is used when a NetBIOS session is established to check for and 216 * cleanup leftover state from a previous session. 217 * 218 * Session requests are not valid for SMB-over-TCP, which is unfortunate 219 * because without the client name leftover state cannot be cleaned up 220 * if the client is behind a NAT server. 221 */ 222 static int 223 smb_netbios_session_request(struct smb_session *session) 224 { 225 int rc; 226 char *calling_name; 227 char *called_name; 228 char client_name[NETBIOS_NAME_SZ]; 229 struct mbuf_chain mbc; 230 char *names = NULL; 231 smb_wchar_t *wbuf = NULL; 232 smb_xprt_t hdr; 233 char *p; 234 int rc1, rc2; 235 236 session->keep_alive = smb_keep_alive; 237 238 if ((rc = smb_session_xprt_gethdr(session, &hdr)) != 0) 239 return (rc); 240 241 DTRACE_PROBE2(receive__session__req__xprthdr, struct session *, session, 242 smb_xprt_t *, &hdr); 243 244 if ((hdr.xh_type != SESSION_REQUEST) || 245 (hdr.xh_length != NETBIOS_SESSION_REQUEST_DATA_LENGTH)) { 246 DTRACE_PROBE1(receive__session__req__failed, 247 struct session *, session); 248 return (EINVAL); 249 } 250 251 names = kmem_alloc(hdr.xh_length, KM_SLEEP); 252 253 if ((rc = smb_sorecv(session->sock, names, hdr.xh_length)) != 0) { 254 kmem_free(names, hdr.xh_length); 255 DTRACE_PROBE1(receive__session__req__failed, 256 struct session *, session); 257 return (rc); 258 } 259 260 DTRACE_PROBE3(receive__session__req__data, struct session *, session, 261 char *, names, uint32_t, hdr.xh_length); 262 263 called_name = &names[0]; 264 calling_name = &names[NETBIOS_ENCODED_NAME_SZ + 2]; 265 266 rc1 = netbios_name_isvalid(called_name, 0); 267 rc2 = netbios_name_isvalid(calling_name, client_name); 268 269 if (rc1 == 0 || rc2 == 0) { 270 271 DTRACE_PROBE3(receive__invalid__session__req, 272 struct session *, session, char *, names, 273 uint32_t, hdr.xh_length); 274 275 kmem_free(names, hdr.xh_length); 276 MBC_INIT(&mbc, MAX_DATAGRAM_LENGTH); 277 (void) smb_mbc_encodef(&mbc, "b", 278 DATAGRAM_INVALID_SOURCE_NAME_FORMAT); 279 (void) smb_session_send(session, NEGATIVE_SESSION_RESPONSE, 280 &mbc); 281 return (EINVAL); 282 } 283 284 DTRACE_PROBE3(receive__session__req__calling__decoded, 285 struct session *, session, 286 char *, calling_name, char *, client_name); 287 288 /* 289 * The client NetBIOS name is in oem codepage format. 290 * We need to convert it to unicode and store it in 291 * multi-byte format. We also need to strip off any 292 * spaces added as part of the NetBIOS name encoding. 293 */ 294 wbuf = kmem_alloc((SMB_PI_MAX_HOST * sizeof (smb_wchar_t)), KM_SLEEP); 295 (void) oemtoucs(wbuf, client_name, SMB_PI_MAX_HOST, OEM_CPG_850); 296 (void) smb_wcstombs(session->workstation, wbuf, SMB_PI_MAX_HOST); 297 kmem_free(wbuf, (SMB_PI_MAX_HOST * sizeof (smb_wchar_t))); 298 299 if ((p = strchr(session->workstation, ' ')) != 0) 300 *p = '\0'; 301 302 kmem_free(names, hdr.xh_length); 303 return (smb_session_send(session, POSITIVE_SESSION_RESPONSE, NULL)); 304 } 305 306 /* 307 * Read 4-byte header from the session socket and build an in-memory 308 * session transport header. See smb_xprt_t definition for header 309 * format information. 310 * 311 * Direct hosted NetBIOS-less SMB (SMB-over-TCP) uses port 445. The 312 * first byte of the four-byte header must be 0 and the next three 313 * bytes contain the length of the remaining data. 314 */ 315 int 316 smb_session_xprt_gethdr(smb_session_t *session, smb_xprt_t *ret_hdr) 317 { 318 int rc; 319 unsigned char buf[NETBIOS_HDR_SZ]; 320 321 if ((rc = smb_sorecv(session->sock, buf, NETBIOS_HDR_SZ)) != 0) 322 return (rc); 323 324 switch (session->s_local_port) { 325 case IPPORT_NETBIOS_SSN: 326 ret_hdr->xh_type = buf[0]; 327 ret_hdr->xh_length = (((uint32_t)buf[1] & 1) << 16) | 328 ((uint32_t)buf[2] << 8) | 329 ((uint32_t)buf[3]); 330 break; 331 332 case IPPORT_SMB: 333 ret_hdr->xh_type = buf[0]; 334 335 if (ret_hdr->xh_type != 0) { 336 cmn_err(CE_WARN, "invalid NBT type (%u) from %s", 337 ret_hdr->xh_type, session->ip_addr_str); 338 return (EPROTO); 339 } 340 341 ret_hdr->xh_length = ((uint32_t)buf[1] << 16) | 342 ((uint32_t)buf[2] << 8) | 343 ((uint32_t)buf[3]); 344 break; 345 346 default: 347 cmn_err(CE_WARN, "invalid port %u", session->s_local_port); 348 return (EPROTO); 349 } 350 351 return (0); 352 } 353 354 /* 355 * Encode a transport session packet header into a 4-byte buffer. 356 */ 357 static int 358 smb_session_xprt_puthdr(smb_session_t *session, 359 uint8_t msg_type, uint32_t msg_length, 360 uint8_t *buf, size_t buflen) 361 { 362 if (buf == NULL || buflen < NETBIOS_HDR_SZ) { 363 return (-1); 364 } 365 366 switch (session->s_local_port) { 367 case IPPORT_NETBIOS_SSN: 368 /* Per RFC 1001, 1002: msg. len < 128KB */ 369 if (msg_length >= (1 << 17)) 370 return (-1); 371 buf[0] = msg_type; 372 buf[1] = ((msg_length >> 16) & 1); 373 buf[2] = (msg_length >> 8) & 0xff; 374 buf[3] = msg_length & 0xff; 375 break; 376 377 case IPPORT_SMB: 378 /* 379 * SMB over TCP is like NetBIOS but the one byte 380 * message type is always zero, and the length 381 * part is three bytes. It could actually use 382 * longer messages, but this is conservative. 383 */ 384 if (msg_length >= (1 << 24)) 385 return (-1); 386 buf[0] = msg_type; 387 buf[1] = (msg_length >> 16) & 0xff; 388 buf[2] = (msg_length >> 8) & 0xff; 389 buf[3] = msg_length & 0xff; 390 break; 391 392 default: 393 cmn_err(CE_WARN, "invalid port %u", session->s_local_port); 394 return (-1); 395 } 396 397 return (0); 398 } 399 400 static void 401 smb_request_init_command_mbuf(smb_request_t *sr) 402 { 403 404 /* 405 * Setup mbuf using the buffer we allocated. 406 */ 407 MBC_ATTACH_BUF(&sr->command, sr->sr_request_buf, sr->sr_req_length); 408 409 sr->command.flags = 0; 410 sr->command.shadow_of = NULL; 411 } 412 413 /* 414 * smb_request_cancel 415 * 416 * Handle a cancel for a request properly depending on the current request 417 * state. 418 */ 419 void 420 smb_request_cancel(smb_request_t *sr) 421 { 422 void (*cancel_method)(smb_request_t *) = NULL; 423 424 mutex_enter(&sr->sr_mutex); 425 switch (sr->sr_state) { 426 427 case SMB_REQ_STATE_INITIALIZING: 428 case SMB_REQ_STATE_SUBMITTED: 429 case SMB_REQ_STATE_ACTIVE: 430 case SMB_REQ_STATE_CLEANED_UP: 431 sr->sr_state = SMB_REQ_STATE_CANCELLED; 432 break; 433 434 case SMB_REQ_STATE_WAITING_AUTH: 435 case SMB_REQ_STATE_WAITING_FCN1: 436 case SMB_REQ_STATE_WAITING_LOCK: 437 case SMB_REQ_STATE_WAITING_PIPE: 438 /* 439 * These are states that have a cancel_method. 440 * Make the state change now, to ensure that 441 * we call cancel_method exactly once. Do the 442 * method call below, after we drop sr_mutex. 443 * When the cancelled request thread resumes, 444 * it should re-take sr_mutex and set sr_state 445 * to CANCELLED, then return STATUS_CANCELLED. 446 */ 447 sr->sr_state = SMB_REQ_STATE_CANCEL_PENDING; 448 cancel_method = sr->cancel_method; 449 VERIFY(cancel_method != NULL); 450 break; 451 452 case SMB_REQ_STATE_WAITING_FCN2: 453 case SMB_REQ_STATE_COMPLETED: 454 case SMB_REQ_STATE_CANCEL_PENDING: 455 case SMB_REQ_STATE_CANCELLED: 456 /* 457 * No action required for these states since the request 458 * is completing. 459 */ 460 break; 461 462 case SMB_REQ_STATE_FREE: 463 default: 464 SMB_PANIC(); 465 } 466 mutex_exit(&sr->sr_mutex); 467 468 if (cancel_method != NULL) { 469 cancel_method(sr); 470 } 471 } 472 473 /* 474 * smb_session_receiver 475 * 476 * Receives request from the network and dispatches them to a worker. 477 * 478 * When we receive a disconnect here, it _could_ be due to the server 479 * having initiated disconnect, in which case the session state will be 480 * SMB_SESSION_STATE_TERMINATED and we want to keep that state so later 481 * tear-down logic will know which side initiated. 482 */ 483 void 484 smb_session_receiver(smb_session_t *session) 485 { 486 int rc = 0; 487 488 SMB_SESSION_VALID(session); 489 490 session->s_thread = curthread; 491 492 if (session->s_local_port == IPPORT_NETBIOS_SSN) { 493 rc = smb_netbios_session_request(session); 494 if (rc != 0) { 495 smb_rwx_rwenter(&session->s_lock, RW_WRITER); 496 if (session->s_state != SMB_SESSION_STATE_TERMINATED) 497 session->s_state = 498 SMB_SESSION_STATE_DISCONNECTED; 499 smb_rwx_rwexit(&session->s_lock); 500 return; 501 } 502 } 503 504 smb_rwx_rwenter(&session->s_lock, RW_WRITER); 505 session->s_state = SMB_SESSION_STATE_ESTABLISHED; 506 smb_rwx_rwexit(&session->s_lock); 507 508 (void) smb_session_reader(session); 509 510 smb_rwx_rwenter(&session->s_lock, RW_WRITER); 511 if (session->s_state != SMB_SESSION_STATE_TERMINATED) 512 session->s_state = SMB_SESSION_STATE_DISCONNECTED; 513 smb_rwx_rwexit(&session->s_lock); 514 515 smb_soshutdown(session->sock); 516 517 DTRACE_PROBE2(session__drop, struct session *, session, int, rc); 518 519 smb_session_cancel(session); 520 /* 521 * At this point everything related to the session should have been 522 * cleaned up and we expect that nothing will attempt to use the 523 * socket. 524 */ 525 } 526 527 /* 528 * smb_session_disconnect 529 * 530 * Server-initiated disconnect (i.e. server shutdown) 531 */ 532 void 533 smb_session_disconnect(smb_session_t *session) 534 { 535 SMB_SESSION_VALID(session); 536 537 smb_rwx_rwenter(&session->s_lock, RW_WRITER); 538 switch (session->s_state) { 539 case SMB_SESSION_STATE_INITIALIZED: 540 case SMB_SESSION_STATE_CONNECTED: 541 case SMB_SESSION_STATE_ESTABLISHED: 542 case SMB_SESSION_STATE_NEGOTIATED: 543 smb_soshutdown(session->sock); 544 session->s_state = SMB_SESSION_STATE_TERMINATED; 545 break; 546 case SMB_SESSION_STATE_DISCONNECTED: 547 case SMB_SESSION_STATE_TERMINATED: 548 break; 549 } 550 smb_rwx_rwexit(&session->s_lock); 551 } 552 553 /* 554 * Read and process SMB requests. 555 * 556 * Returns: 557 * 0 Success 558 * 1 Unable to read transport header 559 * 2 Invalid transport header type 560 * 3 Invalid SMB length (too small) 561 * 4 Unable to read SMB header 562 * 5 Invalid SMB header (bad magic number) 563 * 6 Unable to read SMB data 564 */ 565 static int 566 smb_session_reader(smb_session_t *session) 567 { 568 smb_server_t *sv; 569 smb_request_t *sr = NULL; 570 smb_xprt_t hdr; 571 uint8_t *req_buf; 572 uint32_t resid; 573 int rc; 574 575 sv = session->s_server; 576 577 for (;;) { 578 579 rc = smb_session_xprt_gethdr(session, &hdr); 580 if (rc) 581 return (rc); 582 583 DTRACE_PROBE2(session__receive__xprthdr, session_t *, session, 584 smb_xprt_t *, &hdr); 585 586 if (hdr.xh_type != SESSION_MESSAGE) { 587 /* 588 * Anything other than SESSION_MESSAGE or 589 * SESSION_KEEP_ALIVE is an error. A SESSION_REQUEST 590 * may indicate a new session request but we need to 591 * close this session and we can treat it as an error 592 * here. 593 */ 594 if (hdr.xh_type == SESSION_KEEP_ALIVE) { 595 session->keep_alive = smb_keep_alive; 596 continue; 597 } 598 return (EPROTO); 599 } 600 601 if (hdr.xh_length == 0) { 602 /* zero length is another form of keep alive */ 603 session->keep_alive = smb_keep_alive; 604 continue; 605 } 606 607 if (hdr.xh_length < SMB_HEADER_LEN) 608 return (EPROTO); 609 if (hdr.xh_length > session->cmd_max_bytes) 610 return (EPROTO); 611 612 session->keep_alive = smb_keep_alive; 613 614 /* 615 * Allocate a request context, read the whole message. 616 * If the request alloc fails, we've disconnected 617 * and won't be able to send the reply anyway, so bail now. 618 */ 619 if ((sr = smb_request_alloc(session, hdr.xh_length)) == NULL) 620 break; 621 622 req_buf = (uint8_t *)sr->sr_request_buf; 623 resid = hdr.xh_length; 624 625 rc = smb_sorecv(session->sock, req_buf, resid); 626 if (rc) { 627 smb_request_free(sr); 628 break; 629 } 630 631 /* accounting: received bytes */ 632 smb_server_add_rxb(sv, 633 (int64_t)(hdr.xh_length + NETBIOS_HDR_SZ)); 634 635 /* 636 * Initialize command MBC to represent the received data. 637 */ 638 smb_request_init_command_mbuf(sr); 639 640 DTRACE_PROBE1(session__receive__smb, smb_request_t *, sr); 641 642 rc = session->newrq_func(sr); 643 sr = NULL; /* enqueued or freed */ 644 if (rc != 0) 645 break; 646 647 /* See notes where this is defined (above). */ 648 if (smb_reader_delay) { 649 delay(MSEC_TO_TICK(smb_reader_delay)); 650 } 651 } 652 return (rc); 653 } 654 655 /* 656 * This is the initial handler for new smb requests, called from 657 * from smb_session_reader when we have not yet seen any requests. 658 * The first SMB request must be "negotiate", which determines 659 * which protocol and dialect we'll be using. That's the ONLY 660 * request type handled here, because with all later requests, 661 * we know the protocol and handle those with either the SMB1 or 662 * SMB2 handlers: smb1sr_post() or smb2sr_post(). 663 * Those do NOT allow SMB negotiate, because that's only allowed 664 * as the first request on new session. 665 * 666 * This and other "post a request" handlers must either enqueue 667 * the new request for the session taskq, or smb_request_free it 668 * (in case we've decided to drop this connection). In this 669 * (special) new request handler, we always free the request. 670 * 671 * Return value is 0 for success, and anything else will 672 * terminate the reader thread (drop the connection). 673 */ 674 static int 675 smbsr_newrq_initial(smb_request_t *sr) 676 { 677 uint32_t magic; 678 int rc = EPROTO; 679 680 mutex_enter(&sr->sr_mutex); 681 sr->sr_state = SMB_REQ_STATE_ACTIVE; 682 mutex_exit(&sr->sr_mutex); 683 684 magic = SMB_READ_PROTOCOL(sr->sr_request_buf); 685 if (magic == SMB_PROTOCOL_MAGIC) 686 rc = smb1_newrq_negotiate(sr); 687 if (magic == SMB2_PROTOCOL_MAGIC) 688 rc = smb2_newrq_negotiate(sr); 689 690 mutex_enter(&sr->sr_mutex); 691 sr->sr_state = SMB_REQ_STATE_COMPLETED; 692 mutex_exit(&sr->sr_mutex); 693 694 smb_request_free(sr); 695 return (rc); 696 } 697 698 /* 699 * Port will be IPPORT_NETBIOS_SSN or IPPORT_SMB. 700 */ 701 smb_session_t * 702 smb_session_create(ksocket_t new_so, uint16_t port, smb_server_t *sv, 703 int family) 704 { 705 struct sockaddr_in sin; 706 socklen_t slen; 707 struct sockaddr_in6 sin6; 708 smb_session_t *session; 709 int64_t now; 710 uint16_t rport; 711 712 session = kmem_cache_alloc(smb_cache_session, KM_SLEEP); 713 bzero(session, sizeof (smb_session_t)); 714 715 if (smb_idpool_constructor(&session->s_uid_pool)) { 716 kmem_cache_free(smb_cache_session, session); 717 return (NULL); 718 } 719 if (smb_idpool_constructor(&session->s_tid_pool)) { 720 smb_idpool_destructor(&session->s_uid_pool); 721 kmem_cache_free(smb_cache_session, session); 722 return (NULL); 723 } 724 725 now = ddi_get_lbolt64(); 726 727 session->s_server = sv; 728 session->s_kid = SMB_NEW_KID(); 729 session->s_state = SMB_SESSION_STATE_INITIALIZED; 730 session->native_os = NATIVE_OS_UNKNOWN; 731 session->opentime = now; 732 session->keep_alive = smb_keep_alive; 733 session->activity_timestamp = now; 734 735 smb_session_genkey(session); 736 737 mutex_init(&session->s_credits_mutex, NULL, MUTEX_DEFAULT, NULL); 738 739 smb_slist_constructor(&session->s_req_list, sizeof (smb_request_t), 740 offsetof(smb_request_t, sr_session_lnd)); 741 742 smb_llist_constructor(&session->s_user_list, sizeof (smb_user_t), 743 offsetof(smb_user_t, u_lnd)); 744 745 smb_llist_constructor(&session->s_tree_list, sizeof (smb_tree_t), 746 offsetof(smb_tree_t, t_lnd)); 747 748 smb_llist_constructor(&session->s_xa_list, sizeof (smb_xa_t), 749 offsetof(smb_xa_t, xa_lnd)); 750 751 smb_net_txl_constructor(&session->s_txlst); 752 753 smb_rwx_init(&session->s_lock); 754 755 if (new_so != NULL) { 756 if (family == AF_INET) { 757 slen = sizeof (sin); 758 (void) ksocket_getsockname(new_so, 759 (struct sockaddr *)&sin, &slen, CRED()); 760 bcopy(&sin.sin_addr, 761 &session->local_ipaddr.au_addr.au_ipv4, 762 sizeof (in_addr_t)); 763 slen = sizeof (sin); 764 (void) ksocket_getpeername(new_so, 765 (struct sockaddr *)&sin, &slen, CRED()); 766 bcopy(&sin.sin_addr, 767 &session->ipaddr.au_addr.au_ipv4, 768 sizeof (in_addr_t)); 769 rport = sin.sin_port; 770 } else { 771 slen = sizeof (sin6); 772 (void) ksocket_getsockname(new_so, 773 (struct sockaddr *)&sin6, &slen, CRED()); 774 bcopy(&sin6.sin6_addr, 775 &session->local_ipaddr.au_addr.au_ipv6, 776 sizeof (in6_addr_t)); 777 slen = sizeof (sin6); 778 (void) ksocket_getpeername(new_so, 779 (struct sockaddr *)&sin6, &slen, CRED()); 780 bcopy(&sin6.sin6_addr, 781 &session->ipaddr.au_addr.au_ipv6, 782 sizeof (in6_addr_t)); 783 rport = sin6.sin6_port; 784 } 785 session->ipaddr.a_family = family; 786 session->local_ipaddr.a_family = family; 787 session->s_local_port = port; 788 session->s_remote_port = ntohs(rport); 789 session->sock = new_so; 790 (void) smb_inet_ntop(&session->ipaddr, 791 session->ip_addr_str, INET6_ADDRSTRLEN); 792 if (port == IPPORT_NETBIOS_SSN) 793 smb_server_inc_nbt_sess(sv); 794 else 795 smb_server_inc_tcp_sess(sv); 796 } 797 smb_server_get_cfg(sv, &session->s_cfg); 798 session->s_srqueue = &sv->sv_srqueue; 799 800 /* 801 * The initial new request handler is special, 802 * and only accepts negotiation requests. 803 */ 804 session->newrq_func = smbsr_newrq_initial; 805 806 /* These may increase in SMB2 negotiate. */ 807 session->cmd_max_bytes = SMB_REQ_MAX_SIZE; 808 session->reply_max_bytes = SMB_REQ_MAX_SIZE; 809 810 session->s_magic = SMB_SESSION_MAGIC; 811 return (session); 812 } 813 814 void 815 smb_session_delete(smb_session_t *session) 816 { 817 818 ASSERT(session->s_magic == SMB_SESSION_MAGIC); 819 820 if (session->enc_mech != NULL) 821 smb3_encrypt_fini(session); 822 823 if (session->sign_fini != NULL) 824 session->sign_fini(session); 825 826 if (session->signing.mackey != NULL) { 827 kmem_free(session->signing.mackey, 828 session->signing.mackey_len); 829 } 830 831 session->s_magic = 0; 832 833 smb_rwx_destroy(&session->s_lock); 834 smb_net_txl_destructor(&session->s_txlst); 835 836 mutex_destroy(&session->s_credits_mutex); 837 838 smb_slist_destructor(&session->s_req_list); 839 smb_llist_destructor(&session->s_tree_list); 840 smb_llist_destructor(&session->s_user_list); 841 smb_llist_destructor(&session->s_xa_list); 842 843 ASSERT(session->s_tree_cnt == 0); 844 ASSERT(session->s_file_cnt == 0); 845 ASSERT(session->s_dir_cnt == 0); 846 847 smb_idpool_destructor(&session->s_tid_pool); 848 smb_idpool_destructor(&session->s_uid_pool); 849 if (session->sock != NULL) { 850 if (session->s_local_port == IPPORT_NETBIOS_SSN) 851 smb_server_dec_nbt_sess(session->s_server); 852 else 853 smb_server_dec_tcp_sess(session->s_server); 854 smb_sodestroy(session->sock); 855 } 856 kmem_cache_free(smb_cache_session, session); 857 } 858 859 static void 860 smb_session_cancel(smb_session_t *session) 861 { 862 smb_xa_t *xa, *nextxa; 863 864 /* All the request currently being treated must be canceled. */ 865 smb_session_cancel_requests(session, NULL, NULL); 866 867 /* 868 * We wait for the completion of all the requests associated with 869 * this session. 870 */ 871 smb_slist_wait_for_empty(&session->s_req_list); 872 873 /* 874 * At this point the reference count of the users, trees, files, 875 * directories should be zero. It should be possible to destroy them 876 * without any problem. 877 */ 878 xa = smb_llist_head(&session->s_xa_list); 879 while (xa) { 880 nextxa = smb_llist_next(&session->s_xa_list, xa); 881 smb_xa_close(xa); 882 xa = nextxa; 883 } 884 885 smb_session_logoff(session); 886 } 887 888 /* 889 * Cancel requests. If a non-null tree is specified, only requests specific 890 * to that tree will be cancelled. If a non-null sr is specified, that sr 891 * will be not be cancelled - this would typically be the caller's sr. 892 */ 893 void 894 smb_session_cancel_requests( 895 smb_session_t *session, 896 smb_tree_t *tree, 897 smb_request_t *exclude_sr) 898 { 899 smb_request_t *sr; 900 901 smb_slist_enter(&session->s_req_list); 902 sr = smb_slist_head(&session->s_req_list); 903 904 while (sr) { 905 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 906 if ((sr != exclude_sr) && 907 (tree == NULL || sr->tid_tree == tree)) 908 smb_request_cancel(sr); 909 910 sr = smb_slist_next(&session->s_req_list, sr); 911 } 912 913 smb_slist_exit(&session->s_req_list); 914 } 915 916 /* 917 * Find a user on the specified session by SMB UID. 918 */ 919 smb_user_t * 920 smb_session_lookup_uid(smb_session_t *session, uint16_t uid) 921 { 922 return (smb_session_lookup_uid_st(session, 0, uid, 923 SMB_USER_STATE_LOGGED_ON)); 924 } 925 926 /* 927 * Find a user on the specified session by SMB2 SSNID. 928 */ 929 smb_user_t * 930 smb_session_lookup_ssnid(smb_session_t *session, uint64_t ssnid) 931 { 932 return (smb_session_lookup_uid_st(session, ssnid, 0, 933 SMB_USER_STATE_LOGGED_ON)); 934 } 935 936 smb_user_t * 937 smb_session_lookup_uid_st(smb_session_t *session, uint64_t ssnid, 938 uint16_t uid, smb_user_state_t st) 939 { 940 smb_user_t *user; 941 smb_llist_t *user_list; 942 943 SMB_SESSION_VALID(session); 944 945 user_list = &session->s_user_list; 946 smb_llist_enter(user_list, RW_READER); 947 948 for (user = smb_llist_head(user_list); 949 user != NULL; 950 user = smb_llist_next(user_list, user)) { 951 952 SMB_USER_VALID(user); 953 ASSERT(user->u_session == session); 954 955 if (user->u_ssnid != ssnid && user->u_uid != uid) 956 continue; 957 958 mutex_enter(&user->u_mutex); 959 if (user->u_state == st) { 960 // smb_user_hold_internal(user); 961 user->u_refcnt++; 962 mutex_exit(&user->u_mutex); 963 break; 964 } 965 mutex_exit(&user->u_mutex); 966 } 967 968 smb_llist_exit(user_list); 969 return (user); 970 } 971 972 /* 973 * Find a tree by tree-id. 974 */ 975 smb_tree_t * 976 smb_session_lookup_tree( 977 smb_session_t *session, 978 uint16_t tid) 979 { 980 smb_tree_t *tree; 981 982 SMB_SESSION_VALID(session); 983 984 smb_llist_enter(&session->s_tree_list, RW_READER); 985 tree = smb_llist_head(&session->s_tree_list); 986 987 while (tree) { 988 ASSERT3U(tree->t_magic, ==, SMB_TREE_MAGIC); 989 ASSERT(tree->t_session == session); 990 991 if (tree->t_tid == tid) { 992 if (smb_tree_hold(tree)) { 993 smb_llist_exit(&session->s_tree_list); 994 return (tree); 995 } else { 996 smb_llist_exit(&session->s_tree_list); 997 return (NULL); 998 } 999 } 1000 1001 tree = smb_llist_next(&session->s_tree_list, tree); 1002 } 1003 1004 smb_llist_exit(&session->s_tree_list); 1005 return (NULL); 1006 } 1007 1008 /* 1009 * Find the first connected tree that matches the specified sharename. 1010 * If the specified tree is NULL the search starts from the beginning of 1011 * the user's tree list. If a tree is provided the search starts just 1012 * after that tree. 1013 */ 1014 smb_tree_t * 1015 smb_session_lookup_share( 1016 smb_session_t *session, 1017 const char *sharename, 1018 smb_tree_t *tree) 1019 { 1020 SMB_SESSION_VALID(session); 1021 ASSERT(sharename); 1022 1023 smb_llist_enter(&session->s_tree_list, RW_READER); 1024 1025 if (tree) { 1026 ASSERT3U(tree->t_magic, ==, SMB_TREE_MAGIC); 1027 ASSERT(tree->t_session == session); 1028 tree = smb_llist_next(&session->s_tree_list, tree); 1029 } else { 1030 tree = smb_llist_head(&session->s_tree_list); 1031 } 1032 1033 while (tree) { 1034 ASSERT3U(tree->t_magic, ==, SMB_TREE_MAGIC); 1035 ASSERT(tree->t_session == session); 1036 if (smb_strcasecmp(tree->t_sharename, sharename, 0) == 0) { 1037 if (smb_tree_hold(tree)) { 1038 smb_llist_exit(&session->s_tree_list); 1039 return (tree); 1040 } 1041 } 1042 tree = smb_llist_next(&session->s_tree_list, tree); 1043 } 1044 1045 smb_llist_exit(&session->s_tree_list); 1046 return (NULL); 1047 } 1048 1049 /* 1050 * Find the first connected tree that matches the specified volume name. 1051 * If the specified tree is NULL the search starts from the beginning of 1052 * the user's tree list. If a tree is provided the search starts just 1053 * after that tree. 1054 */ 1055 smb_tree_t * 1056 smb_session_lookup_volume( 1057 smb_session_t *session, 1058 const char *name, 1059 smb_tree_t *tree) 1060 { 1061 SMB_SESSION_VALID(session); 1062 ASSERT(name); 1063 1064 smb_llist_enter(&session->s_tree_list, RW_READER); 1065 1066 if (tree) { 1067 ASSERT3U(tree->t_magic, ==, SMB_TREE_MAGIC); 1068 ASSERT(tree->t_session == session); 1069 tree = smb_llist_next(&session->s_tree_list, tree); 1070 } else { 1071 tree = smb_llist_head(&session->s_tree_list); 1072 } 1073 1074 while (tree) { 1075 ASSERT3U(tree->t_magic, ==, SMB_TREE_MAGIC); 1076 ASSERT(tree->t_session == session); 1077 1078 if (smb_strcasecmp(tree->t_volume, name, 0) == 0) { 1079 if (smb_tree_hold(tree)) { 1080 smb_llist_exit(&session->s_tree_list); 1081 return (tree); 1082 } 1083 } 1084 1085 tree = smb_llist_next(&session->s_tree_list, tree); 1086 } 1087 1088 smb_llist_exit(&session->s_tree_list); 1089 return (NULL); 1090 } 1091 1092 /* 1093 * Disconnect all trees that match the specified client process-id. 1094 */ 1095 void 1096 smb_session_close_pid( 1097 smb_session_t *session, 1098 uint32_t pid) 1099 { 1100 smb_tree_t *tree; 1101 1102 SMB_SESSION_VALID(session); 1103 1104 tree = smb_session_get_tree(session, NULL); 1105 while (tree) { 1106 smb_tree_t *next; 1107 ASSERT3U(tree->t_magic, ==, SMB_TREE_MAGIC); 1108 ASSERT(tree->t_session == session); 1109 smb_tree_close_pid(tree, pid); 1110 next = smb_session_get_tree(session, tree); 1111 smb_tree_release(tree); 1112 tree = next; 1113 } 1114 } 1115 1116 static void 1117 smb_session_tree_dtor(void *t) 1118 { 1119 smb_tree_t *tree = (smb_tree_t *)t; 1120 1121 smb_tree_disconnect(tree, B_TRUE); 1122 /* release the ref acquired during the traversal loop */ 1123 smb_tree_release(tree); 1124 } 1125 1126 1127 /* 1128 * Disconnect all trees that this user has connected. 1129 */ 1130 void 1131 smb_session_disconnect_owned_trees( 1132 smb_session_t *session, 1133 smb_user_t *owner) 1134 { 1135 smb_tree_t *tree; 1136 smb_llist_t *tree_list = &session->s_tree_list; 1137 1138 SMB_SESSION_VALID(session); 1139 SMB_USER_VALID(owner); 1140 1141 smb_llist_enter(tree_list, RW_READER); 1142 1143 tree = smb_llist_head(tree_list); 1144 while (tree) { 1145 if ((tree->t_owner == owner) && 1146 smb_tree_hold(tree)) { 1147 /* 1148 * smb_tree_hold() succeeded, hence we are in state 1149 * SMB_TREE_STATE_CONNECTED; schedule this tree 1150 * for disconnect after smb_llist_exit because 1151 * the "unmap exec" up-call can block, and we'd 1152 * rather not block with the tree list locked. 1153 */ 1154 smb_llist_post(tree_list, tree, smb_session_tree_dtor); 1155 } 1156 tree = smb_llist_next(tree_list, tree); 1157 } 1158 1159 /* drop the lock and flush the dtor queue */ 1160 smb_llist_exit(tree_list); 1161 } 1162 1163 /* 1164 * Disconnect all trees that this user has connected. 1165 */ 1166 static void 1167 smb_session_disconnect_trees( 1168 smb_session_t *session) 1169 { 1170 smb_tree_t *tree, *next_tree; 1171 1172 SMB_SESSION_VALID(session); 1173 1174 tree = smb_session_get_tree(session, NULL); 1175 while (tree) { 1176 ASSERT3U(tree->t_magic, ==, SMB_TREE_MAGIC); 1177 ASSERT(tree->t_session == session); 1178 smb_tree_disconnect(tree, B_TRUE); 1179 next_tree = smb_session_get_tree(session, tree); 1180 smb_tree_release(tree); 1181 tree = next_tree; 1182 } 1183 } 1184 1185 /* 1186 * Disconnect all trees that match the specified share name. 1187 */ 1188 void 1189 smb_session_disconnect_share( 1190 smb_session_t *session, 1191 const char *sharename) 1192 { 1193 smb_tree_t *tree; 1194 smb_tree_t *next; 1195 1196 SMB_SESSION_VALID(session); 1197 1198 tree = smb_session_lookup_share(session, sharename, NULL); 1199 while (tree) { 1200 ASSERT3U(tree->t_magic, ==, SMB_TREE_MAGIC); 1201 ASSERT(tree->t_session == session); 1202 smb_tree_disconnect(tree, B_TRUE); 1203 smb_session_cancel_requests(session, tree, NULL); 1204 next = smb_session_lookup_share(session, sharename, tree); 1205 smb_tree_release(tree); 1206 tree = next; 1207 } 1208 } 1209 1210 /* 1211 * Get the next connected tree in the list. A reference is taken on 1212 * the tree, which can be released later with smb_tree_release(). 1213 * 1214 * If the specified tree is NULL the search starts from the beginning of 1215 * the tree list. If a tree is provided the search starts just after 1216 * that tree. 1217 * 1218 * Returns NULL if there are no connected trees in the list. 1219 */ 1220 static smb_tree_t * 1221 smb_session_get_tree( 1222 smb_session_t *session, 1223 smb_tree_t *tree) 1224 { 1225 smb_llist_t *tree_list; 1226 1227 SMB_SESSION_VALID(session); 1228 tree_list = &session->s_tree_list; 1229 1230 smb_llist_enter(tree_list, RW_READER); 1231 1232 if (tree) { 1233 ASSERT3U(tree->t_magic, ==, SMB_TREE_MAGIC); 1234 tree = smb_llist_next(tree_list, tree); 1235 } else { 1236 tree = smb_llist_head(tree_list); 1237 } 1238 1239 while (tree) { 1240 if (smb_tree_hold(tree)) 1241 break; 1242 1243 tree = smb_llist_next(tree_list, tree); 1244 } 1245 1246 smb_llist_exit(tree_list); 1247 return (tree); 1248 } 1249 1250 /* 1251 * Logoff all users associated with the specified session. 1252 * 1253 * This is called for both server-initiated disconnect 1254 * (SMB_SESSION_STATE_TERMINATED) and client-initiated 1255 * disconnect (SMB_SESSION_STATE_DISCONNECTED). 1256 * If client-initiated, save durable handles. 1257 */ 1258 static void 1259 smb_session_logoff(smb_session_t *session) 1260 { 1261 smb_llist_t *ulist; 1262 smb_user_t *user; 1263 1264 SMB_SESSION_VALID(session); 1265 1266 top: 1267 ulist = &session->s_user_list; 1268 smb_llist_enter(ulist, RW_READER); 1269 1270 user = smb_llist_head(ulist); 1271 while (user) { 1272 SMB_USER_VALID(user); 1273 ASSERT(user->u_session == session); 1274 1275 mutex_enter(&user->u_mutex); 1276 switch (user->u_state) { 1277 case SMB_USER_STATE_LOGGING_ON: 1278 case SMB_USER_STATE_LOGGED_ON: 1279 // smb_user_hold_internal(user); 1280 user->u_refcnt++; 1281 mutex_exit(&user->u_mutex); 1282 if (user->u_session->s_state == 1283 SMB_SESSION_STATE_DISCONNECTED) 1284 user->preserve_opens = SMB2_DH_PRESERVE_ALL; 1285 smb_user_logoff(user); 1286 smb_user_release(user); 1287 break; 1288 1289 case SMB_USER_STATE_LOGGED_OFF: 1290 case SMB_USER_STATE_LOGGING_OFF: 1291 mutex_exit(&user->u_mutex); 1292 break; 1293 1294 default: 1295 ASSERT(0); 1296 mutex_exit(&user->u_mutex); 1297 break; 1298 } 1299 1300 user = smb_llist_next(ulist, user); 1301 } 1302 1303 /* Needed below (Was the list empty?) */ 1304 user = smb_llist_head(ulist); 1305 1306 smb_llist_exit(ulist); 1307 1308 /* 1309 * It's possible for user objects to remain due to references 1310 * obtained via smb_server_lookup_ssnid(), when an SMB2 1311 * session setup is destroying a previous session. 1312 * 1313 * Wait for user objects to clear out (last refs. go away, 1314 * then smb_user_delete takes them out of the list). When 1315 * the last user object is removed, the session state is 1316 * set to SHUTDOWN and s_lock is signaled. 1317 * 1318 * Not all places that call smb_user_release necessarily 1319 * flush the delete queue, so after we wait for the list 1320 * to empty out, go back to the top and recheck the list 1321 * delete queue to make sure smb_user_delete happens. 1322 */ 1323 if (user == NULL) { 1324 /* User list is empty. */ 1325 smb_rwx_rwenter(&session->s_lock, RW_WRITER); 1326 session->s_state = SMB_SESSION_STATE_SHUTDOWN; 1327 smb_rwx_rwexit(&session->s_lock); 1328 } else { 1329 smb_rwx_rwenter(&session->s_lock, RW_READER); 1330 if (session->s_state != SMB_SESSION_STATE_SHUTDOWN) { 1331 (void) smb_rwx_cvwait(&session->s_lock, 1332 MSEC_TO_TICK(200)); 1333 smb_rwx_rwexit(&session->s_lock); 1334 goto top; 1335 } 1336 smb_rwx_rwexit(&session->s_lock); 1337 } 1338 ASSERT(session->s_state == SMB_SESSION_STATE_SHUTDOWN); 1339 1340 /* 1341 * User list should be empty now. 1342 */ 1343 #ifdef DEBUG 1344 if (ulist->ll_count != 0) { 1345 cmn_err(CE_WARN, "user list not empty?"); 1346 debug_enter("s_user_list"); 1347 } 1348 #endif 1349 1350 /* 1351 * User logoff happens first so we'll set preserve_opens 1352 * for client-initiated disconnect. When that's done 1353 * there should be no trees left, but check anyway. 1354 */ 1355 smb_session_disconnect_trees(session); 1356 } 1357 1358 /* 1359 * Copy the session workstation/client name to buf. If the workstation 1360 * is an empty string (which it will be on TCP connections), use the 1361 * client IP address. 1362 */ 1363 void 1364 smb_session_getclient(smb_session_t *sn, char *buf, size_t buflen) 1365 { 1366 1367 *buf = '\0'; 1368 1369 if (sn->workstation[0] != '\0') { 1370 (void) strlcpy(buf, sn->workstation, buflen); 1371 return; 1372 } 1373 1374 (void) strlcpy(buf, sn->ip_addr_str, buflen); 1375 } 1376 1377 /* 1378 * Check whether or not the specified client name is the client of this 1379 * session. The name may be in UNC format (\\CLIENT). 1380 * 1381 * A workstation/client name is setup on NBT connections as part of the 1382 * NetBIOS session request but that isn't available on TCP connections. 1383 * If the session doesn't have a client name we typically return the 1384 * client IP address as the workstation name on MSRPC requests. So we 1385 * check for the IP address here in addition to the workstation name. 1386 */ 1387 boolean_t 1388 smb_session_isclient(smb_session_t *sn, const char *client) 1389 { 1390 1391 client += strspn(client, "\\"); 1392 1393 if (smb_strcasecmp(client, sn->workstation, 0) == 0) 1394 return (B_TRUE); 1395 1396 if (smb_strcasecmp(client, sn->ip_addr_str, 0) == 0) 1397 return (B_TRUE); 1398 1399 return (B_FALSE); 1400 } 1401 1402 /* 1403 * smb_request_alloc 1404 * 1405 * Allocate an smb_request_t structure from the kmem_cache. Partially 1406 * initialize the found/new request. 1407 * 1408 * Returns pointer to a request, or NULL if the session state is 1409 * one in which new requests are no longer allowed. 1410 */ 1411 smb_request_t * 1412 smb_request_alloc(smb_session_t *session, int req_length) 1413 { 1414 smb_request_t *sr; 1415 1416 ASSERT(session->s_magic == SMB_SESSION_MAGIC); 1417 ASSERT(req_length <= session->cmd_max_bytes); 1418 1419 sr = kmem_cache_alloc(smb_cache_request, KM_SLEEP); 1420 1421 /* 1422 * Future: Use constructor to pre-initialize some fields. For now 1423 * there are so many fields that it is easiest just to zero the 1424 * whole thing and start over. 1425 */ 1426 bzero(sr, sizeof (smb_request_t)); 1427 1428 mutex_init(&sr->sr_mutex, NULL, MUTEX_DEFAULT, NULL); 1429 smb_srm_init(sr); 1430 sr->session = session; 1431 sr->sr_server = session->s_server; 1432 sr->sr_gmtoff = session->s_server->si_gmtoff; 1433 sr->sr_cfg = &session->s_cfg; 1434 sr->command.max_bytes = req_length; 1435 sr->reply.max_bytes = session->reply_max_bytes; 1436 sr->sr_req_length = req_length; 1437 if (req_length) 1438 sr->sr_request_buf = kmem_alloc(req_length, KM_SLEEP); 1439 sr->sr_magic = SMB_REQ_MAGIC; 1440 sr->sr_state = SMB_REQ_STATE_INITIALIZING; 1441 1442 /* 1443 * Only allow new SMB requests in some states. 1444 */ 1445 smb_rwx_rwenter(&session->s_lock, RW_WRITER); 1446 switch (session->s_state) { 1447 case SMB_SESSION_STATE_CONNECTED: 1448 case SMB_SESSION_STATE_INITIALIZED: 1449 case SMB_SESSION_STATE_ESTABLISHED: 1450 case SMB_SESSION_STATE_NEGOTIATED: 1451 smb_slist_insert_tail(&session->s_req_list, sr); 1452 break; 1453 1454 default: 1455 ASSERT(0); 1456 /* FALLTHROUGH */ 1457 case SMB_SESSION_STATE_DISCONNECTED: 1458 case SMB_SESSION_STATE_SHUTDOWN: 1459 case SMB_SESSION_STATE_TERMINATED: 1460 /* Disallow new requests in these states. */ 1461 if (sr->sr_request_buf) 1462 kmem_free(sr->sr_request_buf, sr->sr_req_length); 1463 sr->session = NULL; 1464 sr->sr_magic = 0; 1465 mutex_destroy(&sr->sr_mutex); 1466 kmem_cache_free(smb_cache_request, sr); 1467 sr = NULL; 1468 break; 1469 } 1470 smb_rwx_rwexit(&session->s_lock); 1471 1472 return (sr); 1473 } 1474 1475 /* 1476 * smb_request_free 1477 * 1478 * release the memories which have been allocated for a smb request. 1479 */ 1480 void 1481 smb_request_free(smb_request_t *sr) 1482 { 1483 ASSERT(sr->sr_magic == SMB_REQ_MAGIC); 1484 ASSERT(sr->session); 1485 ASSERT(sr->r_xa == NULL); 1486 1487 if (sr->fid_ofile != NULL) { 1488 smb_ofile_release(sr->fid_ofile); 1489 } 1490 1491 if (sr->tid_tree != NULL) 1492 smb_tree_release(sr->tid_tree); 1493 1494 if (sr->uid_user != NULL) 1495 smb_user_release(sr->uid_user); 1496 1497 if (sr->tform_ssn != NULL) 1498 smb_user_release(sr->tform_ssn); 1499 1500 /* 1501 * The above may have left work on the delete queues 1502 */ 1503 smb_llist_flush(&sr->session->s_tree_list); 1504 smb_llist_flush(&sr->session->s_user_list); 1505 1506 smb_slist_remove(&sr->session->s_req_list, sr); 1507 1508 sr->session = NULL; 1509 1510 smb_srm_fini(sr); 1511 1512 if (sr->sr_request_buf) 1513 kmem_free(sr->sr_request_buf, sr->sr_req_length); 1514 if (sr->command.chain) 1515 m_freem(sr->command.chain); 1516 if (sr->reply.chain) 1517 m_freem(sr->reply.chain); 1518 if (sr->raw_data.chain) 1519 m_freem(sr->raw_data.chain); 1520 1521 sr->sr_magic = 0; 1522 mutex_destroy(&sr->sr_mutex); 1523 kmem_cache_free(smb_cache_request, sr); 1524 } 1525 1526 boolean_t 1527 smb_session_oplocks_enable(smb_session_t *session) 1528 { 1529 SMB_SESSION_VALID(session); 1530 if (session->s_cfg.skc_oplock_enable == 0) 1531 return (B_FALSE); 1532 else 1533 return (B_TRUE); 1534 } 1535 1536 boolean_t 1537 smb_session_levelII_oplocks(smb_session_t *session) 1538 { 1539 SMB_SESSION_VALID(session); 1540 1541 /* Older clients only do Level II oplocks if negotiated. */ 1542 if ((session->capabilities & CAP_LEVEL_II_OPLOCKS) != 0) 1543 return (B_TRUE); 1544 1545 return (B_FALSE); 1546 } 1547 1548 static void 1549 smb_session_genkey(smb_session_t *session) 1550 { 1551 uint8_t tmp_key[SMB_CHALLENGE_SZ]; 1552 1553 (void) random_get_pseudo_bytes(tmp_key, SMB_CHALLENGE_SZ); 1554 bcopy(tmp_key, &session->challenge_key, SMB_CHALLENGE_SZ); 1555 session->challenge_len = SMB_CHALLENGE_SZ; 1556 1557 (void) random_get_pseudo_bytes(tmp_key, 4); 1558 session->sesskey = tmp_key[0] | tmp_key[1] << 8 | 1559 tmp_key[2] << 16 | tmp_key[3] << 24; 1560 } 1561