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