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) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2017 by Delphix. All rights reserved. 24 * Copyright 2019 Nexenta by DDN, Inc. All rights reserved. 25 * Copyright 2022 RackTop Systems, Inc. 26 */ 27 28 /* 29 * General Structures Layout 30 * ------------------------- 31 * 32 * This is a simplified diagram showing the relationship between most of the 33 * main structures. 34 * 35 * +-------------------+ 36 * | SMB_SERVER | 37 * +-------------------+ 38 * | 39 * | 40 * v 41 * +-------------------+ +-------------------+ +-------------------+ 42 * | SESSION |<----->| SESSION |......| SESSION | 43 * +-------------------+ +-------------------+ +-------------------+ 44 * | 45 * | 46 * v 47 * +-------------------+ +-------------------+ +-------------------+ 48 * | USER |<----->| USER |......| USER | 49 * +-------------------+ +-------------------+ +-------------------+ 50 * | 51 * | 52 * v 53 * +-------------------+ +-------------------+ +-------------------+ 54 * | TREE |<----->| TREE |......| TREE | 55 * +-------------------+ +-------------------+ +-------------------+ 56 * | | 57 * | | 58 * | v 59 * | +-------+ +-------+ +-------+ 60 * | | OFILE |<----->| OFILE |......| OFILE | 61 * | +-------+ +-------+ +-------+ 62 * | 63 * | 64 * v 65 * +-------+ +------+ +------+ 66 * | ODIR |<----->| ODIR |......| ODIR | 67 * +-------+ +------+ +------+ 68 * 69 * 70 * Module Interface Overview 71 * ------------------------- 72 * 73 * 74 * +===================================+ 75 * | smbd daemon | 76 * +===================================+ 77 * | | ^ 78 * | | | 79 * User | | | 80 * -----------|--------------|----------------|-------------------------------- 81 * Kernel | | | 82 * | | | 83 * | | | 84 * +=========|==============|================|=================+ 85 * | v v | | 86 * | +-----------+ +--------------------+ +------------------+ | 87 * | | IO | | Kernel Door Server | | User Door Servers| | 88 * | | Interface | | Interface | | Interface | | 89 * | +-----------+ +--------------------+ +------------------+ | 90 * | | | ^ ^ | 91 * | v v | | | +=========+ 92 * | +-----------------------------------+ | | | | 93 * | + SMB Server Management (this file) |<------------------| ZFS | 94 * | +-----------------------------------+ | | | | 95 * | | | | Module | 96 * | +-----------------------------------+ | | | | 97 * | + SMB Server Internal Layers |------+ | +=========+ 98 * | +-----------------------------------+ | 99 * | | 100 * | | 101 * +===========================================================+ 102 * 103 * 104 * Server State Machine 105 * -------------------- 106 * | 107 * | T0 108 * | 109 * v 110 * +-----------------------------+ 111 * | SMB_SERVER_STATE_CREATED | 112 * +-----------------------------+ 113 * | 114 * | T1 115 * | 116 * v 117 * +-----------------------------+ 118 * | SMB_SERVER_STATE_CONFIGURED | 119 * +-----------------------------+ 120 * | 121 * | T2 122 * | 123 * v 124 * +-----------------------------+ 125 * | SMB_SERVER_STATE_RUNNING / | 126 * | SMB_SERVER_STATE_STOPPING | 127 * +-----------------------------+ 128 * | 129 * | T3 130 * | 131 * v 132 * +-----------------------------+ 133 * | SMB_SERVER_STATE_DELETING | 134 * +-----------------------------+ 135 * | 136 * | 137 * | 138 * v 139 * 140 * States 141 * ------ 142 * 143 * SMB_SERVER_STATE_CREATED 144 * 145 * This is the state of the server just after creation. 146 * 147 * SMB_SERVER_STATE_CONFIGURED 148 * 149 * The server has been configured. 150 * 151 * SMB_SERVER_STATE_RUNNING 152 * 153 * The server has been started. While in this state the threads listening on 154 * the sockets are started. 155 * 156 * When a client establishes a connection the thread listening dispatches 157 * a task with the new session as an argument. If the dispatch fails the new 158 * session context is destroyed. 159 * 160 * SMB_SERVER_STATE_STOPPING 161 * 162 * The threads listening on the NBT and TCP sockets are being terminated. 163 * 164 * 165 * Transitions 166 * ----------- 167 * 168 * Transition T0 169 * 170 * The daemon smbd triggers its creation by opening the smbsrv device. If 171 * the zone where the daemon lives doesn't have an smb server yet it is 172 * created. 173 * 174 * smb_drv_open() --> smb_server_create() 175 * 176 * Transition T1 177 * 178 * This transition occurs in smb_server_configure(). It is triggered by the 179 * daemon through an Ioctl. 180 * 181 * smb_drv_ioctl(SMB_IOC_CONFIG) --> smb_server_configure() 182 * 183 * Transition T2 184 * 185 * This transition occurs in smb_server_start(). It is triggered by the 186 * daemon through an Ioctl. 187 * 188 * smb_drv_ioctl(SMB_IOC_START) --> smb_server_start() 189 * 190 * Transition T3 191 * 192 * This transition occurs in smb_server_delete(). It is triggered by the 193 * daemon when closing the smbsrv device 194 * 195 * smb_drv_close() --> smb_server_delete() 196 * 197 * Comments 198 * -------- 199 * 200 * This files assumes that there will one SMB server per zone. For now the 201 * smb server works only in global zone. There's nothing in this file preventing 202 * an smb server from being created in a non global zone. That limitation is 203 * enforced in user space. 204 */ 205 206 #include <sys/cmn_err.h> 207 #include <sys/priv.h> 208 #include <sys/zone.h> 209 #include <netinet/in.h> 210 #include <netinet/in_systm.h> 211 #include <netinet/ip.h> 212 #include <netinet/ip_icmp.h> 213 #include <netinet/ip_var.h> 214 #include <netinet/tcp.h> 215 #include <smbsrv/smb2_kproto.h> 216 #include <smbsrv/string.h> 217 #include <smbsrv/netbios.h> 218 #include <smbsrv/smb_fsops.h> 219 #include <smbsrv/smb_share.h> 220 #include <smbsrv/smb_door.h> 221 #include <smbsrv/smb_kstat.h> 222 223 static void smb_server_kstat_init(smb_server_t *); 224 static void smb_server_kstat_fini(smb_server_t *); 225 static void smb_server_timers(smb_thread_t *, void *); 226 static void smb_server_store_cfg(smb_server_t *, smb_ioc_cfg_t *); 227 static void smb_server_shutdown(smb_server_t *); 228 static int smb_server_fsop_start(smb_server_t *); 229 static void smb_server_fsop_stop(smb_server_t *); 230 static void smb_event_cancel(smb_server_t *, uint32_t); 231 static uint32_t smb_event_alloc_txid(void); 232 233 static void smb_server_disconnect_share(smb_server_t *, const char *); 234 static void smb_server_enum_users(smb_server_t *, smb_svcenum_t *); 235 static void smb_server_enum_trees(smb_server_t *, smb_svcenum_t *); 236 static int smb_server_session_disconnect(smb_server_t *, const char *, 237 const char *); 238 static int smb_server_fclose(smb_server_t *, uint32_t); 239 static int smb_server_kstat_update(kstat_t *, int); 240 static int smb_server_legacy_kstat_update(kstat_t *, int); 241 static void smb_server_listener_init(smb_server_t *, smb_listener_daemon_t *, 242 char *, in_port_t, int); 243 static void smb_server_listener_destroy(smb_listener_daemon_t *); 244 static int smb_server_listener_start(smb_listener_daemon_t *); 245 static void smb_server_listener_stop(smb_listener_daemon_t *); 246 static void smb_server_listener(smb_thread_t *, void *); 247 static void smb_server_receiver(void *); 248 static void smb_server_create_session(smb_listener_daemon_t *, ksocket_t); 249 static void smb_server_destroy_session(smb_session_t *); 250 static uint16_t smb_spool_get_fid(smb_server_t *); 251 static boolean_t smb_spool_lookup_doc_byfid(smb_server_t *, uint16_t, 252 smb_kspooldoc_t *); 253 254 /* 255 * How many "buckets" should our hash tables use? On a "real" server, 256 * make them much larger than the number of CPUs we're likely to have. 257 * On "fksmbd" make it smaller so dtrace logs are shorter. 258 * These must be powers of two. 259 */ 260 #ifdef _KERNEL 261 #define DEFAULT_HASH_NBUCKETS 256 /* real server */ 262 #else 263 #define DEFAULT_HASH_NBUCKETS 16 /* for "fksmbd" */ 264 #endif 265 uint32_t SMB_OFILE_HASH_NBUCKETS = DEFAULT_HASH_NBUCKETS; 266 uint32_t SMB_LEASE_HASH_NBUCKETS = DEFAULT_HASH_NBUCKETS; 267 268 int smb_event_debug = 0; 269 270 static smb_llist_t smb_servers; 271 272 /* for smb_server_destroy_session() */ 273 static smb_llist_t smb_server_session_zombies; 274 275 kmem_cache_t *smb_cache_request; 276 kmem_cache_t *smb_cache_session; 277 kmem_cache_t *smb_cache_user; 278 kmem_cache_t *smb_cache_tree; 279 kmem_cache_t *smb_cache_ofile; 280 kmem_cache_t *smb_cache_odir; 281 kmem_cache_t *smb_cache_opipe; 282 kmem_cache_t *smb_cache_event; 283 kmem_cache_t *smb_cache_lock; 284 285 /* 286 * ***************************************************************************** 287 * **************** Functions called from the device interface ***************** 288 * ***************************************************************************** 289 * 290 * These functions typically have to determine the relevant smb server 291 * to which the call applies. 292 */ 293 294 /* 295 * How many zones have an SMB server active? 296 */ 297 int 298 smb_server_get_count(void) 299 { 300 return (smb_llist_get_count(&smb_servers)); 301 } 302 303 /* 304 * smb_server_g_init 305 * 306 * This function must be called from smb_drv_attach(). 307 */ 308 int 309 smb_server_g_init(void) 310 { 311 int rc; 312 313 if ((rc = smb_vop_init()) != 0) 314 goto errout; 315 if ((rc = smb_fem_init()) != 0) 316 goto errout; 317 318 smb_kshare_g_init(); 319 smb_codepage_init(); 320 smb_mbc_init(); /* smb_mbc_cache */ 321 smb_node_init(); /* smb_node_cache, lists */ 322 smb2_lease_init(); 323 324 smb_cache_request = kmem_cache_create("smb_request_cache", 325 sizeof (smb_request_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 326 smb_cache_session = kmem_cache_create("smb_session_cache", 327 sizeof (smb_session_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 328 smb_cache_user = kmem_cache_create("smb_user_cache", 329 sizeof (smb_user_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 330 smb_cache_tree = kmem_cache_create("smb_tree_cache", 331 sizeof (smb_tree_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 332 smb_cache_ofile = kmem_cache_create("smb_ofile_cache", 333 sizeof (smb_ofile_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 334 smb_cache_odir = kmem_cache_create("smb_odir_cache", 335 sizeof (smb_odir_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 336 smb_cache_opipe = kmem_cache_create("smb_opipe_cache", 337 sizeof (smb_opipe_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 338 smb_cache_event = kmem_cache_create("smb_event_cache", 339 sizeof (smb_event_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 340 smb_cache_lock = kmem_cache_create("smb_lock_cache", 341 sizeof (smb_lock_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 342 343 smb_llist_init(); 344 smb_llist_constructor(&smb_servers, sizeof (smb_server_t), 345 offsetof(smb_server_t, sv_lnd)); 346 347 smb_llist_constructor(&smb_server_session_zombies, 348 sizeof (smb_session_t), offsetof(smb_session_t, s_lnd)); 349 350 return (0); 351 352 errout: 353 smb_fem_fini(); 354 smb_vop_fini(); 355 return (rc); 356 } 357 358 /* 359 * smb_server_g_fini 360 * 361 * This function must called from smb_drv_detach(). It will fail if servers 362 * still exist. 363 */ 364 void 365 smb_server_g_fini(void) 366 { 367 368 ASSERT(smb_llist_get_count(&smb_servers) == 0); 369 370 smb_llist_fini(); 371 372 kmem_cache_destroy(smb_cache_request); 373 kmem_cache_destroy(smb_cache_session); 374 kmem_cache_destroy(smb_cache_user); 375 kmem_cache_destroy(smb_cache_tree); 376 kmem_cache_destroy(smb_cache_ofile); 377 kmem_cache_destroy(smb_cache_odir); 378 kmem_cache_destroy(smb_cache_opipe); 379 kmem_cache_destroy(smb_cache_event); 380 kmem_cache_destroy(smb_cache_lock); 381 382 smb2_lease_fini(); 383 smb_node_fini(); 384 smb_mbc_fini(); 385 smb_codepage_fini(); 386 smb_kshare_g_fini(); 387 388 smb_fem_fini(); 389 smb_vop_fini(); 390 391 smb_llist_destructor(&smb_servers); 392 } 393 394 /* 395 * smb_server_create 396 * 397 * This function will fail if there's already a server associated with the 398 * caller's zone. 399 */ 400 int 401 smb_server_create(void) 402 { 403 zoneid_t zid; 404 smb_server_t *sv; 405 406 zid = getzoneid(); 407 408 smb_llist_enter(&smb_servers, RW_WRITER); 409 sv = smb_llist_head(&smb_servers); 410 while (sv) { 411 SMB_SERVER_VALID(sv); 412 if (sv->sv_zid == zid) { 413 smb_llist_exit(&smb_servers); 414 return (EPERM); 415 } 416 sv = smb_llist_next(&smb_servers, sv); 417 } 418 419 sv = kmem_zalloc(sizeof (smb_server_t), KM_SLEEP); 420 421 sv->sv_magic = SMB_SERVER_MAGIC; 422 sv->sv_state = SMB_SERVER_STATE_CREATED; 423 sv->sv_zid = zid; 424 sv->sv_pid = ddi_get_pid(); 425 426 mutex_init(&sv->sv_mutex, NULL, MUTEX_DEFAULT, NULL); 427 cv_init(&sv->sv_cv, NULL, CV_DEFAULT, NULL); 428 cv_init(&sv->sp_info.sp_cv, NULL, CV_DEFAULT, NULL); 429 430 sv->sv_persistid_ht = smb_hash_create(sizeof (smb_ofile_t), 431 offsetof(smb_ofile_t, f_dh_lnd), SMB_OFILE_HASH_NBUCKETS); 432 433 sv->sv_lease_ht = smb_hash_create(sizeof (smb_lease_t), 434 offsetof(smb_lease_t, ls_lnd), SMB_LEASE_HASH_NBUCKETS); 435 436 smb_llist_constructor(&sv->sv_session_list, sizeof (smb_session_t), 437 offsetof(smb_session_t, s_lnd)); 438 439 smb_llist_constructor(&sv->sv_event_list, sizeof (smb_event_t), 440 offsetof(smb_event_t, se_lnd)); 441 442 smb_llist_constructor(&sv->sp_info.sp_list, sizeof (smb_kspooldoc_t), 443 offsetof(smb_kspooldoc_t, sd_lnd)); 444 445 smb_llist_constructor(&sv->sp_info.sp_fidlist, 446 sizeof (smb_spoolfid_t), offsetof(smb_spoolfid_t, sf_lnd)); 447 448 sv->sv_disp_stats1 = kmem_zalloc(SMB_COM_NUM * 449 sizeof (smb_disp_stats_t), KM_SLEEP); 450 451 sv->sv_disp_stats2 = kmem_zalloc(SMB2__NCMDS * 452 sizeof (smb_disp_stats_t), KM_SLEEP); 453 454 smb_thread_init(&sv->si_thread_timers, "smb_timers", 455 smb_server_timers, sv, smbsrv_timer_pri); 456 457 smb_srqueue_init(&sv->sv_srqueue); 458 459 smb_kdoor_init(sv); 460 smb_kshare_init(sv); 461 smb_server_kstat_init(sv); 462 463 smb_threshold_init(&sv->sv_ssetup_ct, SMB_SSETUP_CMD, 464 smb_ssetup_threshold, smb_ssetup_timeout); 465 smb_threshold_init(&sv->sv_tcon_ct, SMB_TCON_CMD, 466 smb_tcon_threshold, smb_tcon_timeout); 467 smb_threshold_init(&sv->sv_opipe_ct, SMB_OPIPE_CMD, 468 smb_opipe_threshold, smb_opipe_timeout); 469 470 smb_llist_insert_tail(&smb_servers, sv); 471 smb_llist_exit(&smb_servers); 472 473 return (0); 474 } 475 476 /* 477 * smb_server_delete 478 * 479 * This function will delete the server passed in. It will make sure that all 480 * activity associated that server has ceased before destroying it. 481 */ 482 int 483 smb_server_delete(smb_server_t *sv) 484 { 485 486 mutex_enter(&sv->sv_mutex); 487 switch (sv->sv_state) { 488 case SMB_SERVER_STATE_RUNNING: 489 sv->sv_state = SMB_SERVER_STATE_STOPPING; 490 mutex_exit(&sv->sv_mutex); 491 smb_server_shutdown(sv); 492 mutex_enter(&sv->sv_mutex); 493 cv_broadcast(&sv->sp_info.sp_cv); 494 sv->sv_state = SMB_SERVER_STATE_DELETING; 495 break; 496 case SMB_SERVER_STATE_STOPPING: 497 sv->sv_state = SMB_SERVER_STATE_DELETING; 498 break; 499 case SMB_SERVER_STATE_CONFIGURED: 500 case SMB_SERVER_STATE_CREATED: 501 sv->sv_state = SMB_SERVER_STATE_DELETING; 502 break; 503 default: 504 SMB_SERVER_STATE_VALID(sv->sv_state); 505 mutex_exit(&sv->sv_mutex); 506 smb_server_release(sv); 507 return (ENOTTY); 508 } 509 510 ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING); 511 512 sv->sv_refcnt--; 513 while (sv->sv_refcnt) 514 cv_wait(&sv->sv_cv, &sv->sv_mutex); 515 516 mutex_exit(&sv->sv_mutex); 517 518 smb_llist_enter(&smb_servers, RW_WRITER); 519 smb_llist_remove(&smb_servers, sv); 520 smb_llist_exit(&smb_servers); 521 522 smb_threshold_fini(&sv->sv_ssetup_ct); 523 smb_threshold_fini(&sv->sv_tcon_ct); 524 smb_threshold_fini(&sv->sv_opipe_ct); 525 526 smb_server_listener_destroy(&sv->sv_nbt_daemon); 527 smb_server_listener_destroy(&sv->sv_tcp_daemon); 528 rw_destroy(&sv->sv_cfg_lock); 529 smb_server_kstat_fini(sv); 530 smb_kshare_fini(sv); 531 smb_kdoor_fini(sv); 532 smb_llist_destructor(&sv->sv_event_list); 533 smb_llist_destructor(&sv->sv_session_list); 534 535 kmem_free(sv->sv_disp_stats1, 536 SMB_COM_NUM * sizeof (smb_disp_stats_t)); 537 538 kmem_free(sv->sv_disp_stats2, 539 SMB2__NCMDS * sizeof (smb_disp_stats_t)); 540 541 smb_srqueue_destroy(&sv->sv_srqueue); 542 smb_thread_destroy(&sv->si_thread_timers); 543 544 mutex_destroy(&sv->sv_mutex); 545 smb_hash_destroy(sv->sv_lease_ht); 546 smb_hash_destroy(sv->sv_persistid_ht); 547 cv_destroy(&sv->sv_cv); 548 sv->sv_magic = 0; 549 kmem_free(sv, sizeof (smb_server_t)); 550 551 return (0); 552 } 553 554 /* 555 * smb_server_configure 556 */ 557 int 558 smb_server_configure(smb_ioc_cfg_t *ioc) 559 { 560 int rc = 0; 561 smb_server_t *sv; 562 563 /* 564 * Reality check negotiation token length vs. #define'd maximum. 565 */ 566 if (ioc->negtok_len > SMB_PI_MAX_NEGTOK) 567 return (EINVAL); 568 569 rc = smb_server_lookup(&sv); 570 if (rc) 571 return (rc); 572 573 mutex_enter(&sv->sv_mutex); 574 switch (sv->sv_state) { 575 case SMB_SERVER_STATE_CREATED: 576 smb_server_store_cfg(sv, ioc); 577 sv->sv_state = SMB_SERVER_STATE_CONFIGURED; 578 break; 579 580 case SMB_SERVER_STATE_CONFIGURED: 581 smb_server_store_cfg(sv, ioc); 582 break; 583 584 case SMB_SERVER_STATE_RUNNING: 585 case SMB_SERVER_STATE_STOPPING: 586 rw_enter(&sv->sv_cfg_lock, RW_WRITER); 587 smb_server_store_cfg(sv, ioc); 588 rw_exit(&sv->sv_cfg_lock); 589 break; 590 591 default: 592 SMB_SERVER_STATE_VALID(sv->sv_state); 593 rc = EFAULT; 594 break; 595 } 596 mutex_exit(&sv->sv_mutex); 597 598 smb_server_release(sv); 599 600 return (rc); 601 } 602 603 /* 604 * smb_server_start 605 */ 606 int 607 smb_server_start(smb_ioc_start_t *ioc) 608 { 609 int rc = 0; 610 int family; 611 smb_server_t *sv; 612 cred_t *ucr; 613 614 rc = smb_server_lookup(&sv); 615 if (rc) 616 return (rc); 617 618 mutex_enter(&sv->sv_mutex); 619 switch (sv->sv_state) { 620 case SMB_SERVER_STATE_CONFIGURED: 621 622 if ((rc = smb_server_fsop_start(sv)) != 0) 623 break; 624 625 /* 626 * Note: smb_kshare_start needs sv_session. 627 */ 628 sv->sv_session = smb_session_create(NULL, 0, sv, 0); 629 if (sv->sv_session == NULL) { 630 rc = ENOMEM; 631 break; 632 } 633 634 /* 635 * Create a logon on the server session, 636 * used when importing CA shares. 637 */ 638 sv->sv_rootuser = smb_user_new(sv->sv_session); 639 ucr = smb_kcred_create(); 640 rc = smb_user_logon(sv->sv_rootuser, ucr, "", "root", 641 SMB_USER_FLAG_ADMIN, 0, 0); 642 crfree(ucr); 643 ucr = NULL; 644 if (rc != 0) { 645 cmn_err(CE_NOTE, "smb_server_start: " 646 "failed to create root user"); 647 break; 648 } 649 650 if ((rc = smb_kshare_start(sv)) != 0) 651 break; 652 653 /* 654 * NB: the proc passed here has to be a "system" one. 655 * Normally that's p0, or the NGZ eqivalent. 656 */ 657 sv->sv_worker_pool = taskq_create_proc("smb_workers", 658 sv->sv_cfg.skc_maxworkers, smbsrv_worker_pri, 659 sv->sv_cfg.skc_maxworkers, INT_MAX, 660 curzone->zone_zsched, TASKQ_DYNAMIC); 661 662 sv->sv_receiver_pool = taskq_create_proc("smb_receivers", 663 sv->sv_cfg.skc_maxconnections, smbsrv_receive_pri, 664 sv->sv_cfg.skc_maxconnections, INT_MAX, 665 curzone->zone_zsched, TASKQ_DYNAMIC); 666 667 if (sv->sv_worker_pool == NULL || 668 sv->sv_receiver_pool == NULL) { 669 rc = ENOMEM; 670 break; 671 } 672 673 #ifdef _KERNEL 674 ASSERT(sv->sv_lmshrd == NULL); 675 sv->sv_lmshrd = smb_kshare_door_init(ioc->lmshrd); 676 if (sv->sv_lmshrd == NULL) 677 break; 678 if ((rc = smb_kdoor_open(sv, ioc->udoor)) != 0) { 679 cmn_err(CE_WARN, "Cannot open smbd door"); 680 break; 681 } 682 #else /* _KERNEL */ 683 /* Fake kernel does not use the kshare_door */ 684 fksmb_kdoor_open(sv, ioc->udoor_func); 685 #endif /* _KERNEL */ 686 687 if ((rc = smb_thread_start(&sv->si_thread_timers)) != 0) 688 break; 689 690 family = AF_INET; 691 smb_server_listener_init(sv, &sv->sv_nbt_daemon, 692 "smb_nbt_listener", IPPORT_NETBIOS_SSN, family); 693 if (sv->sv_cfg.skc_ipv6_enable) 694 family = AF_INET6; 695 smb_server_listener_init(sv, &sv->sv_tcp_daemon, 696 "smb_tcp_listener", IPPORT_SMB, family); 697 rc = smb_server_listener_start(&sv->sv_tcp_daemon); 698 if (rc != 0) 699 break; 700 if (sv->sv_cfg.skc_netbios_enable) 701 (void) smb_server_listener_start(&sv->sv_nbt_daemon); 702 703 sv->sv_state = SMB_SERVER_STATE_RUNNING; 704 sv->sv_start_time = gethrtime(); 705 mutex_exit(&sv->sv_mutex); 706 smb_server_release(sv); 707 smb_export_start(sv); 708 return (0); 709 default: 710 SMB_SERVER_STATE_VALID(sv->sv_state); 711 mutex_exit(&sv->sv_mutex); 712 smb_server_release(sv); 713 return (ENOTTY); 714 } 715 716 mutex_exit(&sv->sv_mutex); 717 smb_server_shutdown(sv); 718 smb_server_release(sv); 719 return (rc); 720 } 721 722 /* 723 * An smbd is shutting down. 724 */ 725 int 726 smb_server_stop(void) 727 { 728 smb_server_t *sv; 729 int rc; 730 731 if ((rc = smb_server_lookup(&sv)) != 0) 732 return (rc); 733 734 mutex_enter(&sv->sv_mutex); 735 switch (sv->sv_state) { 736 case SMB_SERVER_STATE_RUNNING: 737 sv->sv_state = SMB_SERVER_STATE_STOPPING; 738 mutex_exit(&sv->sv_mutex); 739 smb_server_shutdown(sv); 740 mutex_enter(&sv->sv_mutex); 741 cv_broadcast(&sv->sp_info.sp_cv); 742 break; 743 default: 744 SMB_SERVER_STATE_VALID(sv->sv_state); 745 break; 746 } 747 mutex_exit(&sv->sv_mutex); 748 749 smb_server_release(sv); 750 return (0); 751 } 752 753 boolean_t 754 smb_server_is_stopping(smb_server_t *sv) 755 { 756 boolean_t status; 757 758 SMB_SERVER_VALID(sv); 759 760 mutex_enter(&sv->sv_mutex); 761 762 switch (sv->sv_state) { 763 case SMB_SERVER_STATE_STOPPING: 764 case SMB_SERVER_STATE_DELETING: 765 status = B_TRUE; 766 break; 767 default: 768 status = B_FALSE; 769 break; 770 } 771 772 mutex_exit(&sv->sv_mutex); 773 return (status); 774 } 775 776 void 777 smb_server_cancel_event(smb_server_t *sv, uint32_t txid) 778 { 779 smb_event_cancel(sv, txid); 780 } 781 782 int 783 smb_server_notify_event(smb_ioc_event_t *ioc) 784 { 785 smb_server_t *sv; 786 int rc; 787 788 if ((rc = smb_server_lookup(&sv)) == 0) { 789 smb_event_notify(sv, ioc->txid); 790 smb_server_release(sv); 791 } 792 793 return (rc); 794 } 795 796 /* 797 * smb_server_spooldoc 798 * 799 * Waits for print file close broadcast. 800 * Gets the head of the fid list, 801 * then searches the spooldoc list and returns 802 * this info via the ioctl to user land. 803 * 804 * rc - 0 success 805 */ 806 807 int 808 smb_server_spooldoc(smb_ioc_spooldoc_t *ioc) 809 { 810 smb_server_t *sv; 811 int rc; 812 smb_kspooldoc_t *spdoc; 813 uint16_t fid; 814 815 if ((rc = smb_server_lookup(&sv)) != 0) 816 return (rc); 817 818 if (sv->sv_cfg.skc_print_enable == 0) { 819 rc = ENOTTY; 820 goto out; 821 } 822 823 mutex_enter(&sv->sv_mutex); 824 for (;;) { 825 if (sv->sv_state != SMB_SERVER_STATE_RUNNING) { 826 rc = ECANCELED; 827 break; 828 } 829 if ((fid = smb_spool_get_fid(sv)) != 0) { 830 rc = 0; 831 break; 832 } 833 if (cv_wait_sig(&sv->sp_info.sp_cv, &sv->sv_mutex) == 0) { 834 rc = EINTR; 835 break; 836 } 837 } 838 mutex_exit(&sv->sv_mutex); 839 if (rc != 0) 840 goto out; 841 842 spdoc = kmem_zalloc(sizeof (*spdoc), KM_SLEEP); 843 if (smb_spool_lookup_doc_byfid(sv, fid, spdoc)) { 844 ioc->spool_num = spdoc->sd_spool_num; 845 ioc->ipaddr = spdoc->sd_ipaddr; 846 (void) strlcpy(ioc->path, spdoc->sd_path, 847 MAXPATHLEN); 848 (void) strlcpy(ioc->username, 849 spdoc->sd_username, MAXNAMELEN); 850 } else { 851 /* Did not find that print job. */ 852 rc = EAGAIN; 853 } 854 kmem_free(spdoc, sizeof (*spdoc)); 855 856 out: 857 smb_server_release(sv); 858 return (rc); 859 } 860 861 int 862 smb_server_set_gmtoff(smb_ioc_gmt_t *ioc) 863 { 864 int rc; 865 smb_server_t *sv; 866 867 if ((rc = smb_server_lookup(&sv)) == 0) { 868 sv->si_gmtoff = ioc->offset; 869 smb_server_release(sv); 870 } 871 872 return (rc); 873 } 874 875 int 876 smb_server_numopen(smb_ioc_opennum_t *ioc) 877 { 878 smb_server_t *sv; 879 int rc; 880 881 if ((rc = smb_server_lookup(&sv)) == 0) { 882 ioc->open_users = sv->sv_users; 883 ioc->open_trees = sv->sv_trees; 884 ioc->open_files = sv->sv_files + sv->sv_pipes; 885 smb_server_release(sv); 886 } 887 return (rc); 888 } 889 890 /* 891 * Enumerate objects within the server. The svcenum provides the 892 * enumeration context, i.e. what the caller want to get back. 893 */ 894 int 895 smb_server_enum(smb_ioc_svcenum_t *ioc) 896 { 897 smb_svcenum_t *svcenum = &ioc->svcenum; 898 smb_server_t *sv; 899 int rc; 900 901 /* 902 * Reality check that the buffer-length insize the enum doesn't 903 * overrun the ioctl's total length. 904 */ 905 if (svcenum->se_buflen + sizeof (*ioc) > ioc->hdr.len) 906 return (EINVAL); 907 908 if ((rc = smb_server_lookup(&sv)) != 0) 909 return (rc); 910 911 svcenum->se_bavail = svcenum->se_buflen; 912 svcenum->se_bused = 0; 913 svcenum->se_nitems = 0; 914 915 switch (svcenum->se_type) { 916 case SMB_SVCENUM_TYPE_USER: 917 smb_server_enum_users(sv, svcenum); 918 break; 919 case SMB_SVCENUM_TYPE_TREE: 920 case SMB_SVCENUM_TYPE_FILE: 921 smb_server_enum_trees(sv, svcenum); 922 break; 923 default: 924 rc = EINVAL; 925 } 926 927 smb_server_release(sv); 928 return (rc); 929 } 930 931 /* 932 * Look for sessions to disconnect by client and user name. 933 */ 934 int 935 smb_server_session_close(smb_ioc_session_t *ioc) 936 { 937 smb_server_t *sv; 938 int cnt; 939 int rc; 940 941 if ((rc = smb_server_lookup(&sv)) != 0) 942 return (rc); 943 944 cnt = smb_server_session_disconnect(sv, ioc->client, ioc->username); 945 946 smb_server_release(sv); 947 948 if (cnt == 0) 949 return (ENOENT); 950 return (0); 951 } 952 953 /* 954 * Close a file by uniqid. 955 */ 956 int 957 smb_server_file_close(smb_ioc_fileid_t *ioc) 958 { 959 uint32_t uniqid = ioc->uniqid; 960 smb_server_t *sv; 961 int rc; 962 963 if ((rc = smb_server_lookup(&sv)) != 0) 964 return (rc); 965 966 rc = smb_server_fclose(sv, uniqid); 967 968 smb_server_release(sv); 969 return (rc); 970 } 971 972 /* 973 * These functions determine the relevant smb server to which the call apply. 974 */ 975 976 uint32_t 977 smb_server_get_session_count(smb_server_t *sv) 978 { 979 uint32_t counter = 0; 980 981 counter = smb_llist_get_count(&sv->sv_session_list); 982 983 return (counter); 984 } 985 986 /* 987 * Gets the smb_node of the specified share path. 988 * Node is returned held (caller must rele.) 989 */ 990 int 991 smb_server_share_lookup(smb_server_t *sv, const char *shr_path, 992 smb_node_t **nodepp) 993 { 994 smb_request_t *sr; 995 smb_node_t *fnode = NULL; 996 smb_node_t *dnode = NULL; 997 char last_comp[MAXNAMELEN]; 998 int rc = 0; 999 1000 ASSERT(shr_path); 1001 1002 mutex_enter(&sv->sv_mutex); 1003 switch (sv->sv_state) { 1004 case SMB_SERVER_STATE_RUNNING: 1005 break; 1006 default: 1007 mutex_exit(&sv->sv_mutex); 1008 return (ENOTACTIVE); 1009 } 1010 mutex_exit(&sv->sv_mutex); 1011 1012 if ((sr = smb_request_alloc(sv->sv_session, 0)) == NULL) { 1013 return (ENOTCONN); 1014 } 1015 sr->user_cr = zone_kcred(); 1016 1017 rc = smb_pathname_reduce(sr, sr->user_cr, shr_path, 1018 NULL, NULL, &dnode, last_comp); 1019 1020 if (rc == 0) { 1021 rc = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS, 1022 sv->si_root_smb_node, dnode, last_comp, &fnode); 1023 smb_node_release(dnode); 1024 } 1025 1026 smb_request_free(sr); 1027 1028 if (rc != 0) 1029 return (rc); 1030 1031 ASSERT(fnode->vp && fnode->vp->v_vfsp); 1032 1033 *nodepp = fnode; 1034 1035 return (0); 1036 } 1037 1038 #ifdef _KERNEL 1039 /* 1040 * This is a special interface that will be utilized by ZFS to cause a share to 1041 * be added/removed. 1042 * 1043 * arg is either a lmshare_info_t or share_name from userspace. 1044 * It will need to be copied into the kernel. It is lmshare_info_t 1045 * for add operations and share_name for delete operations. 1046 */ 1047 int 1048 smb_server_share(void *arg, boolean_t add_share) 1049 { 1050 smb_server_t *sv; 1051 int rc; 1052 1053 if ((rc = smb_server_lookup(&sv)) == 0) { 1054 mutex_enter(&sv->sv_mutex); 1055 switch (sv->sv_state) { 1056 case SMB_SERVER_STATE_RUNNING: 1057 mutex_exit(&sv->sv_mutex); 1058 (void) smb_kshare_upcall(sv->sv_lmshrd, arg, add_share); 1059 break; 1060 default: 1061 mutex_exit(&sv->sv_mutex); 1062 break; 1063 } 1064 smb_server_release(sv); 1065 } 1066 1067 return (rc); 1068 } 1069 #endif /* _KERNEL */ 1070 1071 int 1072 smb_server_unshare(const char *sharename) 1073 { 1074 smb_server_t *sv; 1075 int rc; 1076 1077 if ((rc = smb_server_lookup(&sv))) 1078 return (rc); 1079 1080 mutex_enter(&sv->sv_mutex); 1081 switch (sv->sv_state) { 1082 case SMB_SERVER_STATE_RUNNING: 1083 case SMB_SERVER_STATE_STOPPING: 1084 break; 1085 default: 1086 mutex_exit(&sv->sv_mutex); 1087 smb_server_release(sv); 1088 return (ENOTACTIVE); 1089 } 1090 mutex_exit(&sv->sv_mutex); 1091 1092 smb_server_disconnect_share(sv, sharename); 1093 1094 smb_server_release(sv); 1095 return (0); 1096 } 1097 1098 /* 1099 * Disconnect the specified share. 1100 * Typically called when a share has been removed. 1101 */ 1102 static void 1103 smb_server_disconnect_share(smb_server_t *sv, const char *sharename) 1104 { 1105 smb_llist_t *ll; 1106 smb_session_t *session; 1107 1108 ll = &sv->sv_session_list; 1109 smb_llist_enter(ll, RW_READER); 1110 1111 session = smb_llist_head(ll); 1112 while (session) { 1113 SMB_SESSION_VALID(session); 1114 smb_rwx_rwenter(&session->s_lock, RW_READER); 1115 switch (session->s_state) { 1116 case SMB_SESSION_STATE_NEGOTIATED: 1117 smb_rwx_rwexit(&session->s_lock); 1118 smb_session_disconnect_share(session, sharename); 1119 break; 1120 default: 1121 smb_rwx_rwexit(&session->s_lock); 1122 break; 1123 } 1124 session = smb_llist_next(ll, session); 1125 } 1126 1127 smb_llist_exit(ll); 1128 } 1129 1130 /* 1131 * ***************************************************************************** 1132 * **************** Functions called from the internal layers ****************** 1133 * ***************************************************************************** 1134 * 1135 * These functions are provided the relevant smb server by the caller. 1136 */ 1137 1138 void 1139 smb_server_get_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg) 1140 { 1141 rw_enter(&sv->sv_cfg_lock, RW_READER); 1142 bcopy(&sv->sv_cfg, cfg, sizeof (*cfg)); 1143 rw_exit(&sv->sv_cfg_lock); 1144 } 1145 1146 /* 1147 * 1148 */ 1149 void 1150 smb_server_inc_nbt_sess(smb_server_t *sv) 1151 { 1152 SMB_SERVER_VALID(sv); 1153 atomic_inc_32(&sv->sv_nbt_sess); 1154 } 1155 1156 void 1157 smb_server_dec_nbt_sess(smb_server_t *sv) 1158 { 1159 SMB_SERVER_VALID(sv); 1160 atomic_dec_32(&sv->sv_nbt_sess); 1161 } 1162 1163 void 1164 smb_server_inc_tcp_sess(smb_server_t *sv) 1165 { 1166 SMB_SERVER_VALID(sv); 1167 atomic_inc_32(&sv->sv_tcp_sess); 1168 } 1169 1170 void 1171 smb_server_dec_tcp_sess(smb_server_t *sv) 1172 { 1173 SMB_SERVER_VALID(sv); 1174 atomic_dec_32(&sv->sv_tcp_sess); 1175 } 1176 1177 void 1178 smb_server_inc_users(smb_server_t *sv) 1179 { 1180 SMB_SERVER_VALID(sv); 1181 atomic_inc_32(&sv->sv_users); 1182 } 1183 1184 void 1185 smb_server_dec_users(smb_server_t *sv) 1186 { 1187 SMB_SERVER_VALID(sv); 1188 atomic_dec_32(&sv->sv_users); 1189 } 1190 1191 void 1192 smb_server_inc_trees(smb_server_t *sv) 1193 { 1194 SMB_SERVER_VALID(sv); 1195 atomic_inc_32(&sv->sv_trees); 1196 } 1197 1198 void 1199 smb_server_dec_trees(smb_server_t *sv) 1200 { 1201 SMB_SERVER_VALID(sv); 1202 atomic_dec_32(&sv->sv_trees); 1203 } 1204 1205 void 1206 smb_server_inc_files(smb_server_t *sv) 1207 { 1208 SMB_SERVER_VALID(sv); 1209 atomic_inc_32(&sv->sv_files); 1210 } 1211 1212 void 1213 smb_server_dec_files(smb_server_t *sv) 1214 { 1215 SMB_SERVER_VALID(sv); 1216 atomic_dec_32(&sv->sv_files); 1217 } 1218 1219 void 1220 smb_server_inc_pipes(smb_server_t *sv) 1221 { 1222 SMB_SERVER_VALID(sv); 1223 atomic_inc_32(&sv->sv_pipes); 1224 } 1225 1226 void 1227 smb_server_dec_pipes(smb_server_t *sv) 1228 { 1229 SMB_SERVER_VALID(sv); 1230 atomic_dec_32(&sv->sv_pipes); 1231 } 1232 1233 void 1234 smb_server_add_rxb(smb_server_t *sv, int64_t value) 1235 { 1236 SMB_SERVER_VALID(sv); 1237 atomic_add_64(&sv->sv_rxb, value); 1238 } 1239 1240 void 1241 smb_server_add_txb(smb_server_t *sv, int64_t value) 1242 { 1243 SMB_SERVER_VALID(sv); 1244 atomic_add_64(&sv->sv_txb, value); 1245 } 1246 1247 void 1248 smb_server_inc_req(smb_server_t *sv) 1249 { 1250 SMB_SERVER_VALID(sv); 1251 atomic_inc_64(&sv->sv_nreq); 1252 } 1253 1254 /* 1255 * ***************************************************************************** 1256 * *************************** Static Functions ******************************** 1257 * ***************************************************************************** 1258 */ 1259 1260 static void 1261 smb_server_timers(smb_thread_t *thread, void *arg) 1262 { 1263 smb_server_t *sv = (smb_server_t *)arg; 1264 1265 ASSERT(sv != NULL); 1266 1267 /* 1268 * This kills old inactive sessions and expired durable 1269 * handles. The session code expects one call per minute. 1270 */ 1271 while (smb_thread_continue_timedwait(thread, 60 /* Seconds */)) { 1272 if (sv->sv_cfg.skc_keepalive != 0) 1273 smb_session_timers(sv); 1274 smb2_durable_timers(sv); 1275 } 1276 } 1277 1278 /* 1279 * smb_server_kstat_init 1280 */ 1281 static void 1282 smb_server_kstat_init(smb_server_t *sv) 1283 { 1284 1285 sv->sv_ksp = kstat_create_zone(SMBSRV_KSTAT_MODULE, 0, 1286 SMBSRV_KSTAT_STATISTICS, SMBSRV_KSTAT_CLASS, KSTAT_TYPE_RAW, 1287 sizeof (smbsrv_kstats_t), 0, sv->sv_zid); 1288 1289 if (sv->sv_ksp != NULL) { 1290 sv->sv_ksp->ks_update = smb_server_kstat_update; 1291 sv->sv_ksp->ks_private = sv; 1292 ((smbsrv_kstats_t *)sv->sv_ksp->ks_data)->ks_start_time = 1293 sv->sv_start_time; 1294 smb_dispatch_stats_init(sv); 1295 smb2_dispatch_stats_init(sv); 1296 kstat_install(sv->sv_ksp); 1297 } else { 1298 cmn_err(CE_WARN, "SMB Server: Statistics unavailable"); 1299 } 1300 1301 sv->sv_legacy_ksp = kstat_create_zone(SMBSRV_KSTAT_MODULE, 0, 1302 SMBSRV_KSTAT_NAME, SMBSRV_KSTAT_CLASS, KSTAT_TYPE_NAMED, 1303 sizeof (smb_server_legacy_kstat_t) / sizeof (kstat_named_t), 1304 0, sv->sv_zid); 1305 1306 if (sv->sv_legacy_ksp != NULL) { 1307 smb_server_legacy_kstat_t *ksd; 1308 1309 ksd = sv->sv_legacy_ksp->ks_data; 1310 1311 (void) strlcpy(ksd->ls_files.name, "open_files", 1312 sizeof (ksd->ls_files.name)); 1313 ksd->ls_files.data_type = KSTAT_DATA_UINT32; 1314 1315 (void) strlcpy(ksd->ls_trees.name, "connections", 1316 sizeof (ksd->ls_trees.name)); 1317 ksd->ls_trees.data_type = KSTAT_DATA_UINT32; 1318 1319 (void) strlcpy(ksd->ls_users.name, "connections", 1320 sizeof (ksd->ls_users.name)); 1321 ksd->ls_users.data_type = KSTAT_DATA_UINT32; 1322 1323 mutex_init(&sv->sv_legacy_ksmtx, NULL, MUTEX_DEFAULT, NULL); 1324 sv->sv_legacy_ksp->ks_lock = &sv->sv_legacy_ksmtx; 1325 sv->sv_legacy_ksp->ks_update = smb_server_legacy_kstat_update; 1326 kstat_install(sv->sv_legacy_ksp); 1327 } 1328 } 1329 1330 /* 1331 * smb_server_kstat_fini 1332 */ 1333 static void 1334 smb_server_kstat_fini(smb_server_t *sv) 1335 { 1336 if (sv->sv_legacy_ksp != NULL) { 1337 kstat_delete(sv->sv_legacy_ksp); 1338 mutex_destroy(&sv->sv_legacy_ksmtx); 1339 sv->sv_legacy_ksp = NULL; 1340 } 1341 1342 if (sv->sv_ksp != NULL) { 1343 kstat_delete(sv->sv_ksp); 1344 sv->sv_ksp = NULL; 1345 smb_dispatch_stats_fini(sv); 1346 smb2_dispatch_stats_fini(sv); 1347 } 1348 } 1349 1350 /* 1351 * smb_server_kstat_update 1352 */ 1353 static int 1354 smb_server_kstat_update(kstat_t *ksp, int rw) 1355 { 1356 smb_server_t *sv; 1357 smbsrv_kstats_t *ksd; 1358 1359 if (rw == KSTAT_READ) { 1360 sv = ksp->ks_private; 1361 SMB_SERVER_VALID(sv); 1362 ksd = (smbsrv_kstats_t *)ksp->ks_data; 1363 /* 1364 * Counters 1365 */ 1366 ksd->ks_nbt_sess = sv->sv_nbt_sess; 1367 ksd->ks_tcp_sess = sv->sv_tcp_sess; 1368 ksd->ks_users = sv->sv_users; 1369 ksd->ks_trees = sv->sv_trees; 1370 ksd->ks_files = sv->sv_files; 1371 ksd->ks_pipes = sv->sv_pipes; 1372 /* 1373 * Throughput 1374 */ 1375 ksd->ks_txb = sv->sv_txb; 1376 ksd->ks_rxb = sv->sv_rxb; 1377 ksd->ks_nreq = sv->sv_nreq; 1378 /* 1379 * Busyness 1380 */ 1381 ksd->ks_maxreqs = sv->sv_cfg.skc_maxworkers; 1382 smb_srqueue_update(&sv->sv_srqueue, 1383 &ksd->ks_utilization); 1384 /* 1385 * Latency & Throughput of the requests 1386 */ 1387 smb_dispatch_stats_update(sv, ksd->ks_reqs1, 0, SMB_COM_NUM); 1388 smb2_dispatch_stats_update(sv, ksd->ks_reqs2, 0, SMB2__NCMDS); 1389 return (0); 1390 } 1391 if (rw == KSTAT_WRITE) 1392 return (EACCES); 1393 1394 return (EIO); 1395 } 1396 1397 static int 1398 smb_server_legacy_kstat_update(kstat_t *ksp, int rw) 1399 { 1400 smb_server_t *sv; 1401 smb_server_legacy_kstat_t *ksd; 1402 int rc; 1403 1404 switch (rw) { 1405 case KSTAT_WRITE: 1406 rc = EACCES; 1407 break; 1408 case KSTAT_READ: 1409 if (!smb_server_lookup(&sv)) { 1410 ASSERT(MUTEX_HELD(ksp->ks_lock)); 1411 ASSERT(sv->sv_legacy_ksp == ksp); 1412 ksd = (smb_server_legacy_kstat_t *)ksp->ks_data; 1413 ksd->ls_files.value.ui32 = sv->sv_files + sv->sv_pipes; 1414 ksd->ls_trees.value.ui32 = sv->sv_trees; 1415 ksd->ls_users.value.ui32 = sv->sv_users; 1416 smb_server_release(sv); 1417 rc = 0; 1418 break; 1419 } 1420 /* FALLTHROUGH */ 1421 default: 1422 rc = EIO; 1423 break; 1424 } 1425 return (rc); 1426 1427 } 1428 1429 /* 1430 * smb_server_shutdown 1431 */ 1432 static void 1433 smb_server_shutdown(smb_server_t *sv) 1434 { 1435 smb_llist_t *sl = &sv->sv_session_list; 1436 smb_session_t *session; 1437 clock_t time; 1438 1439 SMB_SERVER_VALID(sv); 1440 1441 /* 1442 * Stop the listeners first, so we don't get any more 1443 * new work while we're trying to shut down. 1444 */ 1445 smb_server_listener_stop(&sv->sv_nbt_daemon); 1446 smb_server_listener_stop(&sv->sv_tcp_daemon); 1447 smb_thread_stop(&sv->si_thread_timers); 1448 1449 /* Disconnect all of the sessions */ 1450 smb_llist_enter(sl, RW_READER); 1451 session = smb_llist_head(sl); 1452 while (session != NULL) { 1453 smb_session_disconnect(session); 1454 session = smb_llist_next(sl, session); 1455 } 1456 smb_llist_exit(sl); 1457 1458 /* 1459 * Wake up any threads we might have blocked. 1460 * Must precede kdoor_close etc. because those will 1461 * wait for such threads to get out. 1462 */ 1463 smb_event_cancel(sv, 0); 1464 smb_threshold_wake_all(&sv->sv_ssetup_ct); 1465 smb_threshold_wake_all(&sv->sv_tcon_ct); 1466 smb_threshold_wake_all(&sv->sv_opipe_ct); 1467 1468 /* 1469 * Wait for the session list to empty. 1470 * (cv_signal in smb_server_destroy_session) 1471 * 1472 * This should not take long, but if there are any leaked 1473 * references to ofiles, trees, or users, there could be a 1474 * session hanging around. If that happens, the ll_count 1475 * never gets to zero and we'll never get the sv_signal. 1476 * Defend against that problem using timed wait, then 1477 * complain if we find sessions left over and continue 1478 * with shutdown in spite of any leaked sessions. 1479 * That's better than a server that won't reboot. 1480 */ 1481 time = SEC_TO_TICK(5) + ddi_get_lbolt(); 1482 mutex_enter(&sv->sv_mutex); 1483 while (sv->sv_session_list.ll_count != 0) { 1484 if (cv_timedwait(&sv->sv_cv, &sv->sv_mutex, time) < 0) 1485 break; 1486 } 1487 mutex_exit(&sv->sv_mutex); 1488 #ifdef DEBUG 1489 if (sv->sv_session_list.ll_count != 0) { 1490 cmn_err(CE_NOTE, "shutdown leaked sessions"); 1491 debug_enter("shutdown leaked sessions"); 1492 } 1493 #endif 1494 1495 /* 1496 * Clean out any durable handles. After this we should 1497 * have no ofiles remaining (and no more oplock breaks). 1498 */ 1499 smb2_dh_shutdown(sv); 1500 1501 smb_kdoor_close(sv); 1502 #ifdef _KERNEL 1503 smb_kshare_door_fini(sv->sv_lmshrd); 1504 #endif /* _KERNEL */ 1505 sv->sv_lmshrd = NULL; 1506 1507 smb_export_stop(sv); 1508 smb_kshare_stop(sv); 1509 1510 /* 1511 * Both kshare and the oplock break sub-systems may have 1512 * taskq jobs on the spcial "server" session, until we've 1513 * closed all ofiles and stopped the kshare exporter. 1514 * Now it's safe to destroy the server session, but first 1515 * wait for any requests on it to finish. Note that for 1516 * normal sessions, this happens in smb_session_cancel, 1517 * but that's not called for the server session. 1518 */ 1519 if (sv->sv_rootuser != NULL) { 1520 smb_user_logoff(sv->sv_rootuser); 1521 smb_user_release(sv->sv_rootuser); 1522 sv->sv_rootuser = NULL; 1523 } 1524 if (sv->sv_session != NULL) { 1525 smb_session_cancel_requests(sv->sv_session, NULL, NULL); 1526 smb_slist_wait_for_empty(&sv->sv_session->s_req_list); 1527 1528 /* Just in case import left users and trees */ 1529 smb_session_logoff(sv->sv_session); 1530 1531 smb_session_delete(sv->sv_session); 1532 sv->sv_session = NULL; 1533 } 1534 1535 if (sv->sv_receiver_pool != NULL) { 1536 taskq_destroy(sv->sv_receiver_pool); 1537 sv->sv_receiver_pool = NULL; 1538 } 1539 1540 if (sv->sv_worker_pool != NULL) { 1541 taskq_destroy(sv->sv_worker_pool); 1542 sv->sv_worker_pool = NULL; 1543 } 1544 1545 smb_server_fsop_stop(sv); 1546 } 1547 1548 /* 1549 * smb_server_listener_init 1550 * 1551 * Initializes listener contexts. 1552 */ 1553 static void 1554 smb_server_listener_init( 1555 smb_server_t *sv, 1556 smb_listener_daemon_t *ld, 1557 char *name, 1558 in_port_t port, 1559 int family) 1560 { 1561 ASSERT(ld->ld_magic != SMB_LISTENER_MAGIC); 1562 1563 bzero(ld, sizeof (*ld)); 1564 1565 ld->ld_sv = sv; 1566 ld->ld_family = family; 1567 ld->ld_port = port; 1568 1569 if (family == AF_INET) { 1570 ld->ld_sin.sin_family = (uint32_t)family; 1571 ld->ld_sin.sin_port = htons(port); 1572 ld->ld_sin.sin_addr.s_addr = htonl(INADDR_ANY); 1573 } else { 1574 ld->ld_sin6.sin6_family = (uint32_t)family; 1575 ld->ld_sin6.sin6_port = htons(port); 1576 (void) memset(&ld->ld_sin6.sin6_addr.s6_addr, 0, 1577 sizeof (ld->ld_sin6.sin6_addr.s6_addr)); 1578 } 1579 1580 smb_thread_init(&ld->ld_thread, name, smb_server_listener, ld, 1581 smbsrv_listen_pri); 1582 ld->ld_magic = SMB_LISTENER_MAGIC; 1583 } 1584 1585 /* 1586 * smb_server_listener_destroy 1587 * 1588 * Destroyes listener contexts. 1589 */ 1590 static void 1591 smb_server_listener_destroy(smb_listener_daemon_t *ld) 1592 { 1593 /* 1594 * Note that if startup fails early, we can legitimately 1595 * get here with an all-zeros object. 1596 */ 1597 if (ld->ld_magic == 0) 1598 return; 1599 1600 SMB_LISTENER_VALID(ld); 1601 ASSERT(ld->ld_so == NULL); 1602 smb_thread_destroy(&ld->ld_thread); 1603 ld->ld_magic = 0; 1604 } 1605 1606 /* 1607 * smb_server_listener_start 1608 * 1609 * Starts the listener associated with the context passed in. 1610 * 1611 * Return: 0 Success 1612 * not 0 Failure 1613 */ 1614 static int 1615 smb_server_listener_start(smb_listener_daemon_t *ld) 1616 { 1617 int rc; 1618 uint32_t on; 1619 uint32_t off; 1620 1621 SMB_LISTENER_VALID(ld); 1622 1623 if (ld->ld_so != NULL) 1624 return (EINVAL); 1625 1626 ld->ld_so = smb_socreate(ld->ld_family, SOCK_STREAM, 0); 1627 if (ld->ld_so == NULL) { 1628 cmn_err(CE_WARN, "port %d: socket create failed", ld->ld_port); 1629 return (ENOMEM); 1630 } 1631 1632 off = 0; 1633 (void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET, 1634 SO_MAC_EXEMPT, &off, sizeof (off), CRED()); 1635 1636 on = 1; 1637 (void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET, 1638 SO_REUSEADDR, &on, sizeof (on), CRED()); 1639 1640 if (ld->ld_family == AF_INET) { 1641 rc = ksocket_bind(ld->ld_so, 1642 (struct sockaddr *)&ld->ld_sin, 1643 sizeof (ld->ld_sin), CRED()); 1644 } else { 1645 rc = ksocket_bind(ld->ld_so, 1646 (struct sockaddr *)&ld->ld_sin6, 1647 sizeof (ld->ld_sin6), CRED()); 1648 } 1649 1650 if (rc != 0) { 1651 cmn_err(CE_WARN, "port %d: bind failed", ld->ld_port); 1652 return (rc); 1653 } 1654 1655 rc = ksocket_listen(ld->ld_so, 20, CRED()); 1656 if (rc < 0) { 1657 cmn_err(CE_WARN, "port %d: listen failed", ld->ld_port); 1658 return (rc); 1659 } 1660 1661 ksocket_hold(ld->ld_so); 1662 rc = smb_thread_start(&ld->ld_thread); 1663 if (rc != 0) { 1664 ksocket_rele(ld->ld_so); 1665 cmn_err(CE_WARN, "port %d: listener failed to start", 1666 ld->ld_port); 1667 return (rc); 1668 } 1669 return (0); 1670 } 1671 1672 /* 1673 * smb_server_listener_stop 1674 * 1675 * Stops the listener associated with the context passed in. 1676 */ 1677 static void 1678 smb_server_listener_stop(smb_listener_daemon_t *ld) 1679 { 1680 SMB_LISTENER_VALID(ld); 1681 1682 if (ld->ld_so != NULL) { 1683 smb_soshutdown(ld->ld_so); 1684 smb_sodestroy(ld->ld_so); 1685 smb_thread_stop(&ld->ld_thread); 1686 ld->ld_so = NULL; 1687 } 1688 } 1689 1690 /* 1691 * smb_server_listener 1692 * 1693 * Entry point of the listeners. 1694 */ 1695 static void 1696 smb_server_listener(smb_thread_t *thread, void *arg) 1697 { 1698 _NOTE(ARGUNUSED(thread)) 1699 smb_listener_daemon_t *ld; 1700 ksocket_t s_so; 1701 int on; 1702 int txbuf_size; 1703 1704 ld = (smb_listener_daemon_t *)arg; 1705 1706 SMB_LISTENER_VALID(ld); 1707 1708 DTRACE_PROBE1(so__wait__accept, struct sonode *, ld->ld_so); 1709 1710 for (;;) { 1711 int ret = ksocket_accept(ld->ld_so, NULL, NULL, &s_so, CRED()); 1712 1713 switch (ret) { 1714 case 0: 1715 break; 1716 case ECONNABORTED: 1717 continue; 1718 case EINTR: 1719 case EBADF: /* libfakekernel */ 1720 goto out; 1721 default: 1722 cmn_err(CE_WARN, 1723 "smb_server_listener: ksocket_accept(%d)", 1724 ret); 1725 goto out; 1726 } 1727 1728 DTRACE_PROBE1(so__accept, struct sonode *, s_so); 1729 1730 on = 1; 1731 (void) ksocket_setsockopt(s_so, IPPROTO_TCP, TCP_NODELAY, 1732 &on, sizeof (on), CRED()); 1733 1734 on = 1; 1735 (void) ksocket_setsockopt(s_so, SOL_SOCKET, SO_KEEPALIVE, 1736 &on, sizeof (on), CRED()); 1737 1738 txbuf_size = 128*1024; 1739 (void) ksocket_setsockopt(s_so, SOL_SOCKET, SO_SNDBUF, 1740 (const void *)&txbuf_size, sizeof (txbuf_size), CRED()); 1741 1742 /* 1743 * Create a session for this connection. 1744 */ 1745 smb_server_create_session(ld, s_so); 1746 } 1747 out: 1748 ksocket_rele(ld->ld_so); 1749 } 1750 1751 /* 1752 * smb_server_receiver 1753 * 1754 * Entry point of the receiver threads. 1755 * Also does cleanup when socket disconnected. 1756 */ 1757 static void 1758 smb_server_receiver(void *arg) 1759 { 1760 smb_session_t *session; 1761 1762 session = (smb_session_t *)arg; 1763 1764 /* We stay in here until socket disconnect. */ 1765 smb_session_receiver(session); 1766 1767 smb_server_destroy_session(session); 1768 } 1769 1770 /* 1771 * smb_server_lookup 1772 * 1773 * This function finds the server associated with the zone of the 1774 * caller. Note: requires a fix in the dynamic taskq code: 1775 * 1501 taskq_create_proc ... TQ_DYNAMIC puts tasks in p0 1776 */ 1777 int 1778 smb_server_lookup(smb_server_t **psv) 1779 { 1780 zoneid_t zid; 1781 smb_server_t *sv; 1782 1783 zid = getzoneid(); 1784 1785 smb_llist_enter(&smb_servers, RW_READER); 1786 sv = smb_llist_head(&smb_servers); 1787 while (sv) { 1788 SMB_SERVER_VALID(sv); 1789 if (sv->sv_zid == zid) { 1790 mutex_enter(&sv->sv_mutex); 1791 if (sv->sv_state != SMB_SERVER_STATE_DELETING) { 1792 sv->sv_refcnt++; 1793 mutex_exit(&sv->sv_mutex); 1794 smb_llist_exit(&smb_servers); 1795 *psv = sv; 1796 return (0); 1797 } 1798 mutex_exit(&sv->sv_mutex); 1799 break; 1800 } 1801 sv = smb_llist_next(&smb_servers, sv); 1802 } 1803 smb_llist_exit(&smb_servers); 1804 return (EPERM); 1805 } 1806 1807 /* 1808 * smb_server_release 1809 * 1810 * This function decrements the reference count of the server and signals its 1811 * condition variable if the state of the server is SMB_SERVER_STATE_DELETING. 1812 */ 1813 void 1814 smb_server_release(smb_server_t *sv) 1815 { 1816 SMB_SERVER_VALID(sv); 1817 1818 mutex_enter(&sv->sv_mutex); 1819 ASSERT(sv->sv_refcnt); 1820 sv->sv_refcnt--; 1821 if ((sv->sv_refcnt == 0) && (sv->sv_state == SMB_SERVER_STATE_DELETING)) 1822 cv_signal(&sv->sv_cv); 1823 mutex_exit(&sv->sv_mutex); 1824 } 1825 1826 /* 1827 * Enumerate the users associated with a session list. 1828 */ 1829 static void 1830 smb_server_enum_users(smb_server_t *sv, smb_svcenum_t *svcenum) 1831 { 1832 smb_llist_t *ll = &sv->sv_session_list; 1833 smb_session_t *sn; 1834 smb_llist_t *ulist; 1835 smb_user_t *user; 1836 int rc = 0; 1837 1838 smb_llist_enter(ll, RW_READER); 1839 sn = smb_llist_head(ll); 1840 1841 while (sn != NULL) { 1842 SMB_SESSION_VALID(sn); 1843 ulist = &sn->s_user_list; 1844 smb_llist_enter(ulist, RW_READER); 1845 user = smb_llist_head(ulist); 1846 1847 while (user != NULL) { 1848 if (smb_user_hold(user)) { 1849 rc = smb_user_enum(user, svcenum); 1850 smb_user_release(user); 1851 if (rc != 0) 1852 break; 1853 } 1854 1855 user = smb_llist_next(ulist, user); 1856 } 1857 1858 smb_llist_exit(ulist); 1859 1860 if (rc != 0) 1861 break; 1862 1863 sn = smb_llist_next(ll, sn); 1864 } 1865 1866 smb_llist_exit(ll); 1867 } 1868 1869 /* 1870 * Enumerate the trees/files associated with a session list. 1871 */ 1872 static void 1873 smb_server_enum_trees(smb_server_t *sv, smb_svcenum_t *svcenum) 1874 { 1875 smb_llist_t *ll = &sv->sv_session_list; 1876 smb_session_t *sn; 1877 smb_llist_t *tlist; 1878 smb_tree_t *tree; 1879 int rc = 0; 1880 1881 smb_llist_enter(ll, RW_READER); 1882 sn = smb_llist_head(ll); 1883 1884 while (sn != NULL) { 1885 SMB_SESSION_VALID(sn); 1886 tlist = &sn->s_tree_list; 1887 smb_llist_enter(tlist, RW_READER); 1888 tree = smb_llist_head(tlist); 1889 1890 while (tree != NULL) { 1891 if (smb_tree_hold(tree)) { 1892 rc = smb_tree_enum(tree, svcenum); 1893 smb_tree_release(tree); 1894 if (rc != 0) 1895 break; 1896 } 1897 1898 tree = smb_llist_next(tlist, tree); 1899 } 1900 1901 smb_llist_exit(tlist); 1902 1903 if (rc != 0) 1904 break; 1905 1906 sn = smb_llist_next(ll, sn); 1907 } 1908 1909 smb_llist_exit(ll); 1910 } 1911 1912 /* 1913 * Disconnect sessions associated with the specified client and username. 1914 * Empty strings are treated as wildcards. 1915 */ 1916 static int 1917 smb_server_session_disconnect(smb_server_t *sv, 1918 const char *client, const char *name) 1919 { 1920 smb_llist_t *ll = &sv->sv_session_list; 1921 smb_session_t *sn; 1922 smb_llist_t *ulist; 1923 smb_user_t *user; 1924 int count = 0; 1925 1926 smb_llist_enter(ll, RW_READER); 1927 1928 for (sn = smb_llist_head(ll); 1929 sn != NULL; 1930 sn = smb_llist_next(ll, sn)) { 1931 SMB_SESSION_VALID(sn); 1932 1933 if (*client != '\0' && !smb_session_isclient(sn, client)) 1934 continue; 1935 1936 ulist = &sn->s_user_list; 1937 smb_llist_enter(ulist, RW_READER); 1938 1939 for (user = smb_llist_head(ulist); 1940 user != NULL; 1941 user = smb_llist_next(ulist, user)) { 1942 1943 if (smb_user_hold(user)) { 1944 1945 if (*name == '\0' || 1946 smb_user_namecmp(user, name)) { 1947 smb_user_logoff(user); 1948 count++; 1949 } 1950 1951 smb_user_release(user); 1952 } 1953 } 1954 1955 smb_llist_exit(ulist); 1956 } 1957 1958 smb_llist_exit(ll); 1959 return (count); 1960 } 1961 1962 /* 1963 * Close a file by its unique id. 1964 */ 1965 static int 1966 smb_server_fclose(smb_server_t *sv, uint32_t uniqid) 1967 { 1968 smb_llist_t *ll; 1969 smb_session_t *sn; 1970 smb_llist_t *tlist; 1971 smb_tree_t *tree; 1972 int rc = ENOENT; 1973 1974 ll = &sv->sv_session_list; 1975 smb_llist_enter(ll, RW_READER); 1976 sn = smb_llist_head(ll); 1977 1978 while ((sn != NULL) && (rc == ENOENT)) { 1979 SMB_SESSION_VALID(sn); 1980 tlist = &sn->s_tree_list; 1981 smb_llist_enter(tlist, RW_READER); 1982 tree = smb_llist_head(tlist); 1983 1984 while ((tree != NULL) && (rc == ENOENT)) { 1985 if (smb_tree_hold(tree)) { 1986 rc = smb_tree_fclose(tree, uniqid); 1987 smb_tree_release(tree); 1988 } 1989 1990 tree = smb_llist_next(tlist, tree); 1991 } 1992 1993 smb_llist_exit(tlist); 1994 sn = smb_llist_next(ll, sn); 1995 } 1996 1997 smb_llist_exit(ll); 1998 return (rc); 1999 } 2000 2001 /* 2002 * This is used by SMB2 session setup to logoff a previous session, 2003 * so it can force a logoff that we haven't noticed yet. 2004 * This is not called frequently, so we just walk the list of 2005 * connections searching for the user. 2006 * 2007 * Note that this must wait for any durable handles (ofiles) 2008 * owned by this user to become "orphaned", so that a reconnect 2009 * that may immediately follow can find and use such ofiles. 2010 */ 2011 void 2012 smb_server_logoff_ssnid(smb_request_t *sr, uint64_t ssnid) 2013 { 2014 smb_server_t *sv = sr->sr_server; 2015 smb_llist_t *sess_list; 2016 smb_session_t *sess; 2017 smb_user_t *user = NULL; 2018 2019 SMB_SERVER_VALID(sv); 2020 2021 if (sv->sv_state != SMB_SERVER_STATE_RUNNING) 2022 return; 2023 2024 sess_list = &sv->sv_session_list; 2025 smb_llist_enter(sess_list, RW_READER); 2026 2027 for (sess = smb_llist_head(sess_list); 2028 sess != NULL; 2029 sess = smb_llist_next(sess_list, sess)) { 2030 2031 SMB_SESSION_VALID(sess); 2032 2033 if (sess->dialect < SMB_VERS_2_BASE) 2034 continue; 2035 2036 switch (sess->s_state) { 2037 case SMB_SESSION_STATE_NEGOTIATED: 2038 case SMB_SESSION_STATE_TERMINATED: 2039 case SMB_SESSION_STATE_DISCONNECTED: 2040 break; 2041 default: 2042 continue; 2043 } 2044 2045 /* 2046 * Normal situation is to find a LOGGED_ON user. 2047 */ 2048 user = smb_session_lookup_uid_st(sess, ssnid, 0, 2049 SMB_USER_STATE_LOGGED_ON); 2050 if (user != NULL) { 2051 2052 if (smb_is_same_user(user->u_cred, sr->user_cr)) { 2053 /* Treat this as if we lost the connection */ 2054 user->preserve_opens = SMB2_DH_PRESERVE_SOME; 2055 smb_user_logoff(user); 2056 break; 2057 } 2058 smb_user_release(user); 2059 user = NULL; 2060 } 2061 2062 /* 2063 * If we raced with disconnect, may find LOGGING_OFF, 2064 * in which case we want to just wait for it. 2065 */ 2066 user = smb_session_lookup_uid_st(sess, ssnid, 0, 2067 SMB_USER_STATE_LOGGING_OFF); 2068 if (user != NULL) { 2069 if (smb_is_same_user(user->u_cred, sr->user_cr)) 2070 break; 2071 smb_user_release(user); 2072 user = NULL; 2073 } 2074 } 2075 2076 smb_llist_exit(sess_list); 2077 2078 if (user != NULL) { 2079 /* 2080 * Wait for durable handles to be orphaned. 2081 * Note: not holding the sess list rwlock. 2082 */ 2083 smb_user_wait_trees(user); 2084 2085 /* 2086 * Could be doing the last release on a user below, 2087 * which can leave work on the delete queues for 2088 * s_user_list or s_tree_list so flush those. 2089 * Must hold the session list after the user release 2090 * so that the session can't go away while we flush. 2091 */ 2092 smb_llist_enter(sess_list, RW_READER); 2093 2094 sess = user->u_session; 2095 smb_user_release(user); 2096 2097 smb_llist_flush(&sess->s_tree_list); 2098 smb_llist_flush(&sess->s_user_list); 2099 2100 smb_llist_exit(sess_list); 2101 } 2102 } 2103 2104 /* See also: libsmb smb_kmod_setcfg */ 2105 static void 2106 smb_server_store_cfg(smb_server_t *sv, smb_ioc_cfg_t *ioc) 2107 { 2108 if (ioc->maxconnections == 0) 2109 ioc->maxconnections = 0xFFFFFFFF; 2110 2111 if (ioc->encrypt == SMB_CONFIG_REQUIRED && 2112 ioc->max_protocol < SMB_VERS_3_0) { 2113 cmn_err(CE_WARN, "Server set to require encryption; " 2114 "forcing max_protocol to 3.0"); 2115 ioc->max_protocol = SMB_VERS_3_0; 2116 } 2117 sv->sv_cfg.skc_maxworkers = ioc->maxworkers; 2118 sv->sv_cfg.skc_maxconnections = ioc->maxconnections; 2119 sv->sv_cfg.skc_keepalive = ioc->keepalive; 2120 sv->sv_cfg.skc_restrict_anon = ioc->restrict_anon; 2121 sv->sv_cfg.skc_signing_enable = ioc->signing_enable; 2122 sv->sv_cfg.skc_signing_required = ioc->signing_required; 2123 sv->sv_cfg.skc_oplock_enable = ioc->oplock_enable; 2124 sv->sv_cfg.skc_sync_enable = ioc->sync_enable; 2125 sv->sv_cfg.skc_secmode = ioc->secmode; 2126 sv->sv_cfg.skc_netbios_enable = ioc->netbios_enable; 2127 sv->sv_cfg.skc_ipv6_enable = ioc->ipv6_enable; 2128 sv->sv_cfg.skc_print_enable = ioc->print_enable; 2129 sv->sv_cfg.skc_traverse_mounts = ioc->traverse_mounts; 2130 sv->sv_cfg.skc_short_names = ioc->short_names; 2131 sv->sv_cfg.skc_max_protocol = ioc->max_protocol; 2132 sv->sv_cfg.skc_min_protocol = ioc->min_protocol; 2133 sv->sv_cfg.skc_encrypt = ioc->encrypt; 2134 sv->sv_cfg.skc_encrypt_ciphers = ioc->encrypt_ciphers; 2135 sv->sv_cfg.skc_execflags = ioc->exec_flags; 2136 sv->sv_cfg.skc_negtok_len = ioc->negtok_len; 2137 sv->sv_cfg.skc_version = ioc->version; 2138 sv->sv_cfg.skc_initial_credits = ioc->initial_credits; 2139 sv->sv_cfg.skc_maximum_credits = ioc->maximum_credits; 2140 2141 (void) memcpy(sv->sv_cfg.skc_machine_uuid, ioc->machine_uuid, 2142 sizeof (uuid_t)); 2143 (void) memcpy(sv->sv_cfg.skc_negtok, ioc->negtok, 2144 sizeof (sv->sv_cfg.skc_negtok)); 2145 (void) memcpy(sv->sv_cfg.skc_native_os, ioc->native_os, 2146 sizeof (sv->sv_cfg.skc_native_os)); 2147 (void) memcpy(sv->sv_cfg.skc_native_lm, ioc->native_lm, 2148 sizeof (sv->sv_cfg.skc_native_lm)); 2149 2150 (void) strlcpy(sv->sv_cfg.skc_nbdomain, ioc->nbdomain, 2151 sizeof (sv->sv_cfg.skc_nbdomain)); 2152 (void) strlcpy(sv->sv_cfg.skc_fqdn, ioc->fqdn, 2153 sizeof (sv->sv_cfg.skc_fqdn)); 2154 (void) strlcpy(sv->sv_cfg.skc_hostname, ioc->hostname, 2155 sizeof (sv->sv_cfg.skc_hostname)); 2156 (void) strlcpy(sv->sv_cfg.skc_system_comment, ioc->system_comment, 2157 sizeof (sv->sv_cfg.skc_system_comment)); 2158 } 2159 2160 static int 2161 smb_server_fsop_start(smb_server_t *sv) 2162 { 2163 int error; 2164 2165 error = smb_node_root_init(sv, &sv->si_root_smb_node); 2166 if (error != 0) 2167 sv->si_root_smb_node = NULL; 2168 2169 return (error); 2170 } 2171 2172 static void 2173 smb_server_fsop_stop(smb_server_t *sv) 2174 { 2175 if (sv->si_root_smb_node != NULL) { 2176 smb_node_release(sv->si_root_smb_node); 2177 sv->si_root_smb_node = NULL; 2178 } 2179 } 2180 2181 smb_event_t * 2182 smb_event_create(smb_server_t *sv, int timeout) 2183 { 2184 smb_event_t *event; 2185 2186 if (smb_server_is_stopping(sv)) 2187 return (NULL); 2188 2189 event = kmem_cache_alloc(smb_cache_event, KM_SLEEP); 2190 2191 bzero(event, sizeof (smb_event_t)); 2192 mutex_init(&event->se_mutex, NULL, MUTEX_DEFAULT, NULL); 2193 cv_init(&event->se_cv, NULL, CV_DEFAULT, NULL); 2194 event->se_magic = SMB_EVENT_MAGIC; 2195 event->se_txid = smb_event_alloc_txid(); 2196 event->se_server = sv; 2197 event->se_timeout = timeout; 2198 2199 smb_llist_enter(&sv->sv_event_list, RW_WRITER); 2200 smb_llist_insert_tail(&sv->sv_event_list, event); 2201 smb_llist_exit(&sv->sv_event_list); 2202 2203 return (event); 2204 } 2205 2206 void 2207 smb_event_destroy(smb_event_t *event) 2208 { 2209 smb_server_t *sv; 2210 2211 if (event == NULL) 2212 return; 2213 2214 SMB_EVENT_VALID(event); 2215 ASSERT(event->se_waittime == 0); 2216 sv = event->se_server; 2217 SMB_SERVER_VALID(sv); 2218 2219 smb_llist_enter(&sv->sv_event_list, RW_WRITER); 2220 smb_llist_remove(&sv->sv_event_list, event); 2221 smb_llist_exit(&sv->sv_event_list); 2222 2223 event->se_magic = (uint32_t)~SMB_EVENT_MAGIC; 2224 cv_destroy(&event->se_cv); 2225 mutex_destroy(&event->se_mutex); 2226 2227 kmem_cache_free(smb_cache_event, event); 2228 } 2229 2230 /* 2231 * Get the txid for the specified event. 2232 */ 2233 uint32_t 2234 smb_event_txid(smb_event_t *event) 2235 { 2236 if (event != NULL) { 2237 SMB_EVENT_VALID(event); 2238 return (event->se_txid); 2239 } 2240 2241 cmn_err(CE_NOTE, "smb_event_txid failed"); 2242 return ((uint32_t)-1); 2243 } 2244 2245 /* 2246 * Wait for event notification. 2247 */ 2248 int 2249 smb_event_wait(smb_event_t *event) 2250 { 2251 int seconds = 1; 2252 int ticks; 2253 int err; 2254 2255 if (event == NULL) 2256 return (EINVAL); 2257 2258 SMB_EVENT_VALID(event); 2259 2260 mutex_enter(&event->se_mutex); 2261 event->se_waittime = 1; 2262 event->se_errno = 0; 2263 2264 while (!(event->se_notified)) { 2265 if (smb_event_debug && ((event->se_waittime % 30) == 0)) 2266 cmn_err(CE_NOTE, "smb_event_wait[%d] (%d sec)", 2267 event->se_txid, event->se_waittime); 2268 2269 if (event->se_errno != 0) 2270 break; 2271 2272 if (event->se_waittime > event->se_timeout) { 2273 event->se_errno = ETIME; 2274 break; 2275 } 2276 2277 ticks = SEC_TO_TICK(seconds); 2278 (void) cv_reltimedwait(&event->se_cv, 2279 &event->se_mutex, (clock_t)ticks, TR_CLOCK_TICK); 2280 ++event->se_waittime; 2281 } 2282 2283 err = event->se_errno; 2284 event->se_waittime = 0; 2285 event->se_notified = B_FALSE; 2286 cv_signal(&event->se_cv); 2287 mutex_exit(&event->se_mutex); 2288 return (err); 2289 } 2290 2291 /* 2292 * If txid is non-zero, cancel the specified event. 2293 * Otherwise, cancel all events. 2294 */ 2295 static void 2296 smb_event_cancel(smb_server_t *sv, uint32_t txid) 2297 { 2298 smb_event_t *event; 2299 smb_llist_t *event_list; 2300 2301 SMB_SERVER_VALID(sv); 2302 2303 event_list = &sv->sv_event_list; 2304 smb_llist_enter(event_list, RW_WRITER); 2305 2306 event = smb_llist_head(event_list); 2307 while (event) { 2308 SMB_EVENT_VALID(event); 2309 2310 if (txid == 0 || event->se_txid == txid) { 2311 mutex_enter(&event->se_mutex); 2312 event->se_errno = ECANCELED; 2313 event->se_notified = B_TRUE; 2314 cv_signal(&event->se_cv); 2315 mutex_exit(&event->se_mutex); 2316 2317 if (txid != 0) 2318 break; 2319 } 2320 2321 event = smb_llist_next(event_list, event); 2322 } 2323 2324 smb_llist_exit(event_list); 2325 } 2326 2327 /* 2328 * If txid is non-zero, notify the specified event. 2329 * Otherwise, notify all events. 2330 */ 2331 void 2332 smb_event_notify(smb_server_t *sv, uint32_t txid) 2333 { 2334 smb_event_t *event; 2335 smb_llist_t *event_list; 2336 2337 SMB_SERVER_VALID(sv); 2338 2339 event_list = &sv->sv_event_list; 2340 smb_llist_enter(event_list, RW_READER); 2341 2342 event = smb_llist_head(event_list); 2343 while (event) { 2344 SMB_EVENT_VALID(event); 2345 2346 if (txid == 0 || event->se_txid == txid) { 2347 mutex_enter(&event->se_mutex); 2348 event->se_notified = B_TRUE; 2349 cv_signal(&event->se_cv); 2350 mutex_exit(&event->se_mutex); 2351 2352 if (txid != 0) 2353 break; 2354 } 2355 2356 event = smb_llist_next(event_list, event); 2357 } 2358 2359 smb_llist_exit(event_list); 2360 } 2361 2362 /* 2363 * Allocate a new transaction id (txid). 2364 * 2365 * 0 or -1 are not assigned because they are used to detect invalid 2366 * conditions or to indicate all open id's. 2367 */ 2368 static uint32_t 2369 smb_event_alloc_txid(void) 2370 { 2371 static kmutex_t txmutex; 2372 static uint32_t txid; 2373 uint32_t txid_ret; 2374 2375 mutex_enter(&txmutex); 2376 2377 if (txid == 0) 2378 txid = ddi_get_lbolt() << 11; 2379 2380 do { 2381 ++txid; 2382 } while (txid == 0 || txid == (uint32_t)-1); 2383 2384 txid_ret = txid; 2385 mutex_exit(&txmutex); 2386 2387 return (txid_ret); 2388 } 2389 2390 /* 2391 * Called by the ioctl to find the corresponding 2392 * spooldoc node. removes node on success 2393 * 2394 * Return values 2395 * rc 2396 * B_FALSE - not found 2397 * B_TRUE - found 2398 * 2399 */ 2400 2401 static boolean_t 2402 smb_spool_lookup_doc_byfid(smb_server_t *sv, uint16_t fid, 2403 smb_kspooldoc_t *spdoc) 2404 { 2405 smb_kspooldoc_t *sp; 2406 smb_llist_t *splist; 2407 2408 splist = &sv->sp_info.sp_list; 2409 smb_llist_enter(splist, RW_WRITER); 2410 sp = smb_llist_head(splist); 2411 while (sp != NULL) { 2412 /* 2413 * check for a matching fid 2414 */ 2415 if (sp->sd_fid == fid) { 2416 *spdoc = *sp; 2417 smb_llist_remove(splist, sp); 2418 smb_llist_exit(splist); 2419 kmem_free(sp, sizeof (smb_kspooldoc_t)); 2420 return (B_TRUE); 2421 } 2422 sp = smb_llist_next(splist, sp); 2423 } 2424 cmn_err(CE_WARN, "smb_spool_lookup_user_byfid: no fid:%d", fid); 2425 smb_llist_exit(splist); 2426 return (B_FALSE); 2427 } 2428 2429 /* 2430 * Adds the spool fid to a linked list to be used 2431 * as a search key in the spooldoc queue 2432 * 2433 * Return values 2434 * rc non-zero error 2435 * rc zero success 2436 * 2437 */ 2438 2439 void 2440 smb_spool_add_fid(smb_server_t *sv, uint16_t fid) 2441 { 2442 smb_llist_t *fidlist; 2443 smb_spoolfid_t *sf; 2444 2445 if (sv->sv_cfg.skc_print_enable == 0) 2446 return; 2447 2448 sf = kmem_zalloc(sizeof (smb_spoolfid_t), KM_SLEEP); 2449 fidlist = &sv->sp_info.sp_fidlist; 2450 smb_llist_enter(fidlist, RW_WRITER); 2451 sf->sf_fid = fid; 2452 smb_llist_insert_tail(fidlist, sf); 2453 smb_llist_exit(fidlist); 2454 cv_broadcast(&sv->sp_info.sp_cv); 2455 } 2456 2457 /* 2458 * Called by the ioctl to get and remove the head of the fid list 2459 * 2460 * Return values 2461 * int fd 2462 * greater than 0 success 2463 * 0 - error 2464 * 2465 */ 2466 2467 static uint16_t 2468 smb_spool_get_fid(smb_server_t *sv) 2469 { 2470 smb_spoolfid_t *spfid; 2471 smb_llist_t *splist; 2472 uint16_t fid; 2473 2474 splist = &sv->sp_info.sp_fidlist; 2475 smb_llist_enter(splist, RW_WRITER); 2476 spfid = smb_llist_head(splist); 2477 if (spfid != NULL) { 2478 fid = spfid->sf_fid; 2479 smb_llist_remove(&sv->sp_info.sp_fidlist, spfid); 2480 kmem_free(spfid, sizeof (smb_spoolfid_t)); 2481 } else { 2482 fid = 0; 2483 } 2484 smb_llist_exit(splist); 2485 return (fid); 2486 } 2487 2488 /* 2489 * Adds the spooldoc to the tail of the spooldoc list 2490 * 2491 * Return values 2492 * rc non-zero error 2493 * rc zero success 2494 */ 2495 int 2496 smb_spool_add_doc(smb_tree_t *tree, smb_kspooldoc_t *sp) 2497 { 2498 smb_llist_t *splist; 2499 smb_server_t *sv = tree->t_server; 2500 int rc = 0; 2501 2502 splist = &sv->sp_info.sp_list; 2503 smb_llist_enter(splist, RW_WRITER); 2504 sp->sd_spool_num = atomic_inc_32_nv(&sv->sp_info.sp_cnt); 2505 smb_llist_insert_tail(splist, sp); 2506 smb_llist_exit(splist); 2507 2508 return (rc); 2509 } 2510 2511 /* 2512 * smb_server_create_session 2513 */ 2514 static void 2515 smb_server_create_session(smb_listener_daemon_t *ld, ksocket_t s_so) 2516 { 2517 smb_server_t *sv = ld->ld_sv; 2518 smb_session_t *session; 2519 smb_llist_t *sl; 2520 taskqid_t tqid; 2521 clock_t now; 2522 2523 session = smb_session_create(s_so, ld->ld_port, sv, 2524 ld->ld_family); 2525 2526 if (session == NULL) { 2527 /* This should be rare (create sleeps) */ 2528 smb_soshutdown(s_so); 2529 smb_sodestroy(s_so); 2530 cmn_err(CE_WARN, "SMB Session: alloc failed"); 2531 return; 2532 } 2533 2534 sl = &sv->sv_session_list; 2535 smb_llist_enter(sl, RW_WRITER); 2536 if (smb_llist_get_count(sl) >= sv->sv_cfg.skc_maxconnections) { 2537 /* 2538 * New session not in sv_session_list, so we can just 2539 * delete it directly. 2540 */ 2541 smb_llist_exit(sl); 2542 DTRACE_PROBE1(maxconn, smb_session_t *, session); 2543 smb_soshutdown(session->sock); 2544 smb_session_delete(session); 2545 goto logmaxconn; 2546 } 2547 smb_llist_insert_tail(sl, session); 2548 smb_llist_exit(sl); 2549 2550 /* 2551 * These taskq entries must run independently of one another, 2552 * so TQ_NOQUEUE. TQ_SLEEP (==0) just for clarity. 2553 */ 2554 tqid = taskq_dispatch(sv->sv_receiver_pool, 2555 smb_server_receiver, session, TQ_NOQUEUE | TQ_SLEEP); 2556 if (tqid == TASKQID_INVALID) { 2557 /* 2558 * We never entered smb_server_receiver() 2559 * so need to do it's return cleanup 2560 */ 2561 DTRACE_PROBE1(maxconn, smb_session_t *, session); 2562 smb_session_disconnect(session); 2563 smb_session_logoff(session); 2564 smb_server_destroy_session(session); 2565 goto logmaxconn; 2566 } 2567 2568 /* Success */ 2569 session->s_receiver_tqid = tqid; 2570 return; 2571 2572 logmaxconn: 2573 /* 2574 * If we hit max_connections, log something so an admin 2575 * can find out why new connections are failing, but 2576 * log this no more than once a minute. 2577 */ 2578 now = ddi_get_lbolt(); 2579 if (now > ld->ld_quiet) { 2580 ld->ld_quiet = now + SEC_TO_TICK(60); 2581 cmn_err(CE_WARN, "SMB can't create session: " 2582 "Would exceed max_connections."); 2583 } 2584 } 2585 2586 static void 2587 smb_server_destroy_session(smb_session_t *session) 2588 { 2589 smb_server_t *sv; 2590 smb_llist_t *ll; 2591 uint32_t count; 2592 2593 ASSERT(session->s_server != NULL); 2594 sv = session->s_server; 2595 ll = &sv->sv_session_list; 2596 2597 smb_llist_flush(&session->s_tree_list); 2598 smb_llist_flush(&session->s_user_list); 2599 2600 /* 2601 * The user and tree lists should be empty now. 2602 */ 2603 #ifdef DEBUG 2604 if (session->s_user_list.ll_count != 0) { 2605 cmn_err(CE_WARN, "user list not empty?"); 2606 debug_enter("s_user_list"); 2607 } 2608 if (session->s_tree_list.ll_count != 0) { 2609 cmn_err(CE_WARN, "tree list not empty?"); 2610 debug_enter("s_tree_list"); 2611 } 2612 #endif 2613 2614 smb_llist_enter(ll, RW_WRITER); 2615 smb_llist_remove(ll, session); 2616 count = ll->ll_count; 2617 smb_llist_exit(ll); 2618 2619 /* 2620 * Normally, the session should have state SHUTDOWN here. 2621 * If the session has any ofiles remaining, eg. due to 2622 * forgotten ofile references or something, the state 2623 * will be _DISCONNECTED or _TERMINATED. Keep such 2624 * sessions in the list of zombies (for debugging). 2625 */ 2626 if (session->s_state == SMB_SESSION_STATE_SHUTDOWN) { 2627 smb_session_delete(session); 2628 } else { 2629 #ifdef DEBUG 2630 cmn_err(CE_NOTE, "Leaked session: 0x%p", (void *)session); 2631 debug_enter("leaked session"); 2632 #endif 2633 smb_llist_enter(&smb_server_session_zombies, RW_WRITER); 2634 smb_llist_insert_head(&smb_server_session_zombies, session); 2635 smb_llist_exit(&smb_server_session_zombies); 2636 } 2637 2638 if (count == 0) { 2639 /* See smb_server_shutdown */ 2640 cv_signal(&sv->sv_cv); 2641 } 2642 } 2643