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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Structures and type definitions for the SMB module. 28 */ 29 30 #ifndef _SMBSRV_SMB_KTYPES_H 31 #define _SMBSRV_SMB_KTYPES_H 32 33 #pragma ident "%Z%%M% %I% %E% SMI" 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 #include <sys/note.h> 40 #include <sys/systm.h> 41 #include <sys/param.h> 42 #include <sys/types.h> 43 #include <sys/synch.h> 44 #include <sys/taskq.h> 45 #include <sys/acl.h> 46 #include <sys/sdt.h> 47 #include <sys/vnode.h> 48 #include <sys/cred.h> 49 #include <sys/fem.h> 50 #include <sys/door.h> 51 #include <smbsrv/smb.h> 52 #include <smbsrv/lmshare.h> 53 #include <smbsrv/smbinfo.h> 54 #include <smbsrv/mbuf.h> 55 #include <smbsrv/smb_sid.h> 56 57 #include <smbsrv/netbios.h> 58 #include <smbsrv/smb_vops.h> 59 #include <smbsrv/smb_fsd.h> 60 #include <smbsrv/mlsvc.h> 61 62 struct smb_request; 63 struct smb_server; 64 struct smb_sd; 65 66 int smb_noop(void *, size_t, int); 67 68 #define SMB_AUDIT_STACK_DEPTH 16 69 #define SMB_AUDIT_BUF_MAX_REC 16 70 #define SMB_AUDIT_NODE 0x00000001 71 72 extern uint32_t smb_audit_flags; 73 74 typedef struct { 75 uint32_t anr_refcnt; 76 int anr_depth; 77 pc_t anr_stack[SMB_AUDIT_STACK_DEPTH]; 78 } smb_audit_record_node_t; 79 80 typedef struct { 81 int anb_index; 82 int anb_max_index; 83 smb_audit_record_node_t anb_records[SMB_AUDIT_BUF_MAX_REC]; 84 } smb_audit_buf_node_t; 85 86 #define SMB_WORKER_PRIORITY 99 87 /* 88 * Thread State Machine 89 * -------------------- 90 * 91 * T5 T0 92 * smb_thread_destroy() <-------+ +------- smb_thread_init() 93 * | | 94 * | v 95 * +-----------------------------+ 96 * | SMB_THREAD_STATE_EXITED |<---+ 97 * +-----------------------------+ | 98 * | T1 | 99 * v | 100 * +-----------------------------+ | 101 * | SMB_THREAD_STATE_STARTING | | 102 * +-----------------------------+ | 103 * | T2 | T4 104 * v | 105 * +-----------------------------+ | 106 * | SMB_THREAD_STATE_RUNNING | | 107 * +-----------------------------+ | 108 * | T3 | 109 * v | 110 * +-----------------------------+ | 111 * | SMB_THREAD_STATE_EXITING |----+ 112 * +-----------------------------+ 113 * 114 * Transition T0 115 * 116 * This transition is executed in smb_thread_init(). 117 * 118 * Transition T1 119 * 120 * This transition is executed in smb_thread_start(). 121 * 122 * Transition T2 123 * 124 * This transition is executed by the thread itself when it starts running. 125 * 126 * Transition T3 127 * 128 * This transition is executed by the thread itself in 129 * smb_thread_entry_point() just before calling thread_exit(). 130 * 131 * 132 * Transition T4 133 * 134 * This transition is executed in smb_thread_stop(). 135 * 136 * Transition T5 137 * 138 * This transition is executed in smb_thread_destroy(). 139 * 140 * Comments 141 * -------- 142 * 143 * The field smb_thread_aw_t contains a function pointer that knows how to 144 * awake the thread. It is a temporary solution to work around the fact that 145 * kernel threads (not part of a userspace process) cannot be signaled. 146 */ 147 typedef enum smb_thread_state { 148 SMB_THREAD_STATE_STARTING = 0, 149 SMB_THREAD_STATE_RUNNING, 150 SMB_THREAD_STATE_EXITING, 151 SMB_THREAD_STATE_EXITED 152 } smb_thread_state_t; 153 154 struct _smb_thread; 155 156 typedef void (*smb_thread_ep_t)(struct _smb_thread *, void *ep_arg); 157 typedef void (*smb_thread_aw_t)(struct _smb_thread *, void *aw_arg); 158 159 #define SMB_THREAD_MAGIC 0x534D4254 /* SMBT */ 160 161 typedef struct _smb_thread { 162 uint32_t sth_magic; 163 char sth_name[16]; 164 smb_thread_state_t sth_state; 165 kthread_t *sth_th; 166 kt_did_t sth_did; 167 smb_thread_ep_t sth_ep; 168 void *sth_ep_arg; 169 smb_thread_aw_t sth_aw; 170 void *sth_aw_arg; 171 boolean_t sth_kill; 172 kmutex_t sth_mtx; 173 kcondvar_t sth_cv; 174 } smb_thread_t; 175 176 /* 177 * Pool of IDs 178 * ----------- 179 * 180 * A pool of IDs is a pool of 16 bit numbers. It is implemented as a bitmap. 181 * A bit set to '1' indicates that that particular value has been allocated. 182 * The allocation process is done shifting a bit through the whole bitmap. 183 * The current position of that index bit is kept in the smb_idpool_t 184 * structure and represented by a byte index (0 to buffer size minus 1) and 185 * a bit index (0 to 7). 186 * 187 * The pools start with a size of 8 bytes or 64 IDs. Each time the pool runs 188 * out of IDs its current size is doubled until it reaches its maximum size 189 * (8192 bytes or 65536 IDs). The IDs 0 and 65535 are never given out which 190 * means that a pool can have a maximum number of 65534 IDs available. 191 */ 192 #define SMB_IDPOOL_MAGIC 0x4944504C /* IDPL */ 193 #define SMB_IDPOOL_MIN_SIZE 64 /* Number of IDs to begin with */ 194 #define SMB_IDPOOL_MAX_SIZE 64 * 1024 195 196 typedef struct smb_idpool { 197 uint32_t id_magic; 198 kmutex_t id_mutex; 199 uint8_t *id_pool; 200 uint32_t id_size; 201 uint8_t id_bit; 202 uint8_t id_bit_idx; 203 uint32_t id_idx; 204 uint32_t id_idx_msk; 205 uint32_t id_free_counter; 206 uint32_t id_max_free_counter; 207 } smb_idpool_t; 208 209 /* 210 * Maximum size of a Transport Data Unit 211 * 4 --> NBT/TCP Transport Header. 212 * 32 --> SMB Header 213 * 1 --> Word Count byte 214 * 510 --> Maximum Number of bytes of the Word Table (2 * 255) 215 * 2 --> Byte count of the data 216 * 65535 --> Maximum size of the data 217 * ----- 218 * 66084 219 */ 220 #define SMB_REQ_MAX_SIZE 66080 221 #define SMB_XPRT_MAX_SIZE (SMB_REQ_MAX_SIZE + NETBIOS_HDR_SZ) 222 223 #define SMB_TXREQ_MAGIC 0X54524251 /* 'TREQ' */ 224 typedef struct { 225 uint32_t tr_magic; 226 list_node_t tr_lnd; 227 int tr_len; 228 uint8_t tr_buf[SMB_XPRT_MAX_SIZE]; 229 } smb_txreq_t; 230 231 #define SMB_TXLST_MAGIC 0X544C5354 /* 'TLST' */ 232 typedef struct { 233 uint32_t tl_magic; 234 kmutex_t tl_mutex; 235 boolean_t tl_active; 236 list_t tl_list; 237 } smb_txlst_t; 238 239 /* 240 * Maximum buffer size for NT is 37KB. If all clients are Windows 2000, this 241 * can be changed to 64KB. 37KB must be used with a mix of NT/Windows 2000 242 * clients because NT loses directory entries when values greater than 37KB are 243 * used. 244 * 245 * Note: NBT_MAXBUF will be subtracted from the specified max buffer size to 246 * account for the NBT header. 247 */ 248 #define NBT_MAXBUF 8 249 #define SMB_NT_MAXBUF (37 * 1024) 250 251 #define OUTBUFSIZE (65 * 1024) 252 #define SMBHEADERSIZE 32 253 #define SMBND_HASH_MASK (0xFF) 254 #define MAX_IOVEC 512 255 #define MAX_READREF (8 * 1024) 256 257 #define SMB_WORKER_MIN 4 258 #define SMB_WORKER_DEFAULT 64 259 #define SMB_WORKER_MAX 1024 260 261 /* 262 * Fix align a pointer or offset appropriately so that fields will not 263 * cross word boundaries. 264 */ 265 #define PTRALIGN(x) \ 266 (((uintptr_t)(x) + (uintptr_t)(_POINTER_ALIGNMENT) - 1l) & \ 267 ~((uintptr_t)(_POINTER_ALIGNMENT) - 1l)) 268 269 /* 270 * native os types are defined in win32/smbinfo.h 271 */ 272 273 /* 274 * All 4 different time / date formats that will bee seen in SMB 275 */ 276 typedef struct { 277 uint16_t Day : 5; 278 uint16_t Month : 4; 279 uint16_t Year : 7; 280 } SMB_DATE; 281 282 typedef struct { 283 uint16_t TwoSeconds : 5; 284 uint16_t Minutes : 6; 285 uint16_t Hours : 5; 286 } SMB_TIME; 287 288 289 typedef uint32_t UTIME; /* seconds since Jan 1 1970 */ 290 291 typedef struct smb_malloc_list { 292 struct smb_malloc_list *forw; 293 struct smb_malloc_list *back; 294 } smb_malloc_list; 295 296 typedef struct smb_llist { 297 krwlock_t ll_lock; 298 list_t ll_list; 299 uint32_t ll_count; 300 uint64_t ll_wrop; 301 } smb_llist_t; 302 303 typedef struct smb_slist { 304 kmutex_t sl_mutex; 305 kcondvar_t sl_cv; 306 list_t sl_list; 307 uint32_t sl_count; 308 boolean_t sl_waiting; 309 } smb_slist_t; 310 311 typedef struct smb_session_list { 312 krwlock_t se_lock; 313 uint64_t se_wrop; 314 struct { 315 list_t lst; 316 uint32_t count; 317 } se_rdy; 318 struct { 319 list_t lst; 320 uint32_t count; 321 } se_act; 322 } smb_session_list_t; 323 324 typedef struct { 325 kcondvar_t rwx_cv; 326 kmutex_t rwx_mutex; 327 krwlock_t rwx_lock; 328 boolean_t rwx_waiting; 329 } smb_rwx_t; 330 331 /* NOTIFY CHANGE */ 332 333 typedef struct smb_notify_change_req { 334 list_node_t nc_lnd; 335 struct smb_node *nc_node; 336 uint32_t nc_reply_type; 337 uint32_t nc_flags; 338 } smb_notify_change_req_t; 339 340 /* 341 * SMB operates over a NetBIOS-over-TCP transport (NBT) or directly 342 * over TCP, which is also known as direct hosted NetBIOS-less SMB 343 * or SMB-over-TCP. 344 * 345 * NBT messages have a 4-byte header that defines the message type 346 * (8-bits), a 7-bit flags field and a 17-bit length. 347 * 348 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 349 * | TYPE | FLAGS |E| LENGTH | 350 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 351 * 352 * 8-bit type Defined in RFC 1002 353 * 7-bit flags Bits 0-6 reserved (must be 0) 354 * Bit 7: Length extension bit (E) 355 * 17-bit length Includes bit 7 of the flags byte 356 * 357 * 358 * SMB-over-TCP is defined to use a modified version of the NBT header 359 * containing an 8-bit message type and 24-bit message length. 360 * 361 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 362 * | TYPE | LENGTH | 363 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 364 * 365 * 8-bit type Must be 0 366 * 24-bit length 367 * 368 * The following structure is used to represent a generic, in-memory 369 * SMB transport header; it is not intended to map directly to either 370 * of the over-the-wire formats. 371 */ 372 typedef struct { 373 uint8_t xh_type; 374 uint32_t xh_length; 375 } smb_xprt_t; 376 377 int MBC_LENGTH(struct mbuf_chain *); 378 void MBC_SETUP(struct mbuf_chain *, uint32_t); 379 void MBC_INIT(struct mbuf_chain *, uint32_t); 380 void MBC_FLUSH(struct mbuf_chain *); 381 void MBC_ATTACH_MBUF(struct mbuf_chain *, struct mbuf *); 382 void MBC_APPEND_MBUF(struct mbuf_chain *, struct mbuf *); 383 void MBC_ATTACH_BUF(struct mbuf_chain *MBC, unsigned char *BUF, int LEN); 384 int MBC_SHADOW_CHAIN(struct mbuf_chain *SUBMBC, struct mbuf_chain *MBC, 385 int OFF, int LEN); 386 387 #define MBC_ROOM_FOR(b, n) (((b)->chain_offset + (n)) <= (b)->max_bytes) 388 389 typedef struct smb_oplock { 390 struct smb_ofile *op_ofile; 391 uint32_t op_flags; 392 uint32_t op_ipaddr; 393 uint64_t op_kid; 394 } smb_oplock_t; 395 396 #define OPLOCK_FLAG_BREAKING 1 397 398 #define OPLOCK_RELEASE_LOCK_RELEASED 0 399 #define OPLOCK_RELEASE_FILE_CLOSED 1 400 401 #define DOS_ATTR_VALID 0x80000000 402 403 #define SMB_VFS_MAGIC 0x534D4256 /* 'SMBV' */ 404 405 typedef struct smb_vfs { 406 uint32_t sv_magic; 407 list_node_t sv_lnd; 408 uint32_t sv_refcnt; 409 vfs_t *sv_vfsp; 410 vnode_t *sv_rootvp; 411 } smb_vfs_t; 412 413 #define SMB_NODE_MAGIC 0x4E4F4445 /* 'NODE' */ 414 415 typedef enum { 416 SMB_NODE_STATE_AVAILABLE = 0, 417 SMB_NODE_STATE_DESTROYING 418 } smb_node_state_t; 419 420 typedef struct smb_node { 421 uint32_t n_magic; 422 smb_rwx_t n_lock; 423 krwlock_t n_share_lock; 424 list_node_t n_lnd; 425 smb_node_state_t n_state; 426 uint32_t n_refcnt; 427 uint32_t n_hashkey; 428 struct smb_request *n_sr; 429 kmem_cache_t *n_cache; 430 smb_llist_t *n_hash_bucket; 431 uint64_t n_orig_session_id; 432 uint32_t n_orig_uid; 433 smb_llist_t n_ofile_list; 434 smb_llist_t n_lock_list; 435 volatile int flags; /* FILE_NOTIFY_CHANGE_* */ 436 volatile int waiting_event; /* # of clients requesting FCN */ 437 smb_attr_t attr; 438 unsigned int what; 439 u_offset_t n_size; 440 smb_oplock_t n_oplock; 441 struct smb_node *dir_snode; /* Directory of node */ 442 struct smb_node *unnamed_stream_node; /* set in stream nodes */ 443 /* Credentials for delayed delete */ 444 cred_t *delete_on_close_cred; 445 char od_name[MAXNAMELEN]; 446 timestruc_t set_mtime; 447 fs_desc_t tree_fsd; 448 vnode_t *vp; 449 smb_audit_buf_node_t *n_audit_buf; 450 451 } smb_node_t; 452 453 #define NODE_FLAGS_NOTIFY_CHANGE 0x10000fff 454 #define NODE_OPLOCKS_IN_FORCE 0x0000f000 455 #define NODE_OPLOCK_NONE 0x00000000 456 #define NODE_EXCLUSIVE_OPLOCK 0x00001000 457 #define NODE_BATCH_OPLOCK 0x00002000 458 #define NODE_LEVEL_II_OPLOCK 0x00003000 459 #define NODE_CAP_LEVEL_II 0x00010000 460 #define NODE_PROTOCOL_LOCK 0x00020000 461 #define NODE_READ_ONLY 0x00040000 462 #define NODE_CREATED_READONLY 0x00080000 463 #define NODE_FLAGS_WRITE_THROUGH 0x00100000 464 #define NODE_FLAGS_SYNCATIME 0x00200000 465 #define NODE_FLAGS_LOCKED 0x00400000 466 #define NODE_FLAGS_ATTR_VALID 0x00800000 467 #define NODE_XATTR_DIR 0x01000000 468 #define NODE_FLAGS_CREATED 0x04000000 469 #define NODE_FLAGS_CHANGED 0x08000000 470 #define NODE_FLAGS_WATCH_TREE 0x10000000 471 #define NODE_FLAGS_SET_SIZE 0x20000000 472 #define NODE_FLAGS_DELETE_ON_CLOSE 0x40000000 473 #define NODE_FLAGS_EXECUTABLE 0x80000000 474 475 #define NODE_IS_READONLY(node) \ 476 ((node->attr.sa_dosattr & FILE_ATTRIBUTE_READONLY) || \ 477 (node->flags & NODE_READ_ONLY) || \ 478 (node->flags & NODE_CREATED_READONLY)) 479 480 #define OPLOCK_TYPE(n) ((n)->flags & NODE_OPLOCKS_IN_FORCE) 481 #define OPLOCKS_IN_FORCE(n) (OPLOCK_TYPE(n) != NODE_OPLOCK_NONE) 482 #define EXCLUSIVE_OPLOCK_IN_FORCE(n) \ 483 (OPLOCK_TYPE(n) == NODE_EXCLUSIVE_OPLOCK) 484 #define BATCH_OPLOCK_IN_FORCE(n) (OPLOCK_TYPE(n) == NODE_BATCH_OPLOCK) 485 #define LEVEL_II_OPLOCK_IN_FORCE(n) (OPLOCK_TYPE(n) == NODE_LEVEL_II_OPLOCK) 486 487 /* 488 * Based on section 2.6.1.2 (Connection Management) of the June 13, 489 * 1996 CIFS spec, a server may terminate the transport connection 490 * due to inactivity. The client software is expected to be able to 491 * automatically reconnect to the server if this happens. Like much 492 * of the useful background information, this section appears to 493 * have been dropped from later revisions of the document. 494 * 495 * Each session has an activity timestamp that's updated whenever a 496 * request is dispatched. If the session is idle, i.e. receives no 497 * requests, for SMB_SESSION_INACTIVITY_TIMEOUT minutes it will be 498 * closed. 499 * 500 * Each session has an I/O semaphore to serialize communication with 501 * the client. For example, after receiving a raw-read request, the 502 * server is not allowed to send an oplock break to the client until 503 * after it has sent the raw-read data. 504 */ 505 #define SMB_SESSION_INACTIVITY_TIMEOUT (15 * 60) 506 507 #define SMB_SESSION_OFILE_MAX (16 * 1024) 508 509 /* 510 * When a connection is set up we need to remember both the client 511 * (peer) IP address and the local IP address used to establish the 512 * connection. When a client connects with a vc number of zero, we 513 * are supposed to abort any existing connections with that client 514 * (see notes in smb_negotiate.c and smb_session_setup_andx.c). For 515 * servers with multiple network interfaces or IP aliases, however, 516 * each interface has to be managed independently since the client 517 * is not aware of the server configuration. We have to allow the 518 * client to establish a connection on each interface with a vc 519 * number of zero without aborting the other connections. 520 * 521 * ipaddr: the client (peer) IP address for the session. 522 * local_ipaddr: the local IP address used to connect to the server. 523 */ 524 525 #define SMB_MAC_KEYSZ 512 526 527 struct smb_sign { 528 unsigned int seqnum; 529 unsigned int mackey_len; 530 unsigned int flags; 531 unsigned char mackey[SMB_MAC_KEYSZ]; 532 }; 533 534 #define SMB_SIGNING_ENABLED 1 535 #define SMB_SIGNING_CHECK 2 536 537 /* 538 * Session State Machine 539 * --------------------- 540 * 541 * +-----------------------------+ +------------------------------+ 542 * | SMB_SESSION_STATE_CONNECTED | | SMB_SESSION_STATE_TERMINATED | 543 * +-----------------------------+ +------------------------------+ 544 * T0| ^ 545 * +--------------------+ |T13 546 * v |T14 | 547 * +-------------------------------+ | +--------------------------------+ 548 * | SMB_SESSION_STATE_ESTABLISHED |---+--->| SMB_SESSION_STATE_DISCONNECTED | 549 * +-------------------------------+ +--------------------------------+ 550 * T1| ^ ^ ^ ^ 551 * +----------+ |T9 | | | 552 * v | | | | 553 * +------------------------------+ | | | 554 * | SMB_SESSION_STATE_NEGOTIATED | | | | 555 * +------------------------------+ | | | 556 * ^| ^| | ^ | | | 557 * +----------------+| || | | | | | 558 * |+----------------+ || T7| |T8 | | | 559 * || || | | | | | 560 * || +----------------+| | | | | | 561 * || |+----------------+ | | | | | 562 * || || v | | | | 563 * || || +-----------------------------------+ T10| | | 564 * || || | SMB_SESSION_STATE_OPLOCK_BREAKING |----+ | | 565 * || || +-----------------------------------+ | | 566 * || ||T5 | | 567 * || |+-->+-----------------------------------+ T11| | 568 * || |T6 | SMB_SESSION_STATE_READ_RAW_ACTIVE |------+ | 569 * || +----+-----------------------------------+ | 570 * ||T3 | 571 * |+------->+------------------------------------+ T12| 572 * |T4 | SMB_SESSION_STATE_WRITE_RAW_ACTIVE |-------+ 573 * +---------+------------------------------------+ 574 * 575 * Transition T0 576 * 577 * 578 * 579 * Transition T1 580 * 581 * 582 * 583 * Transition T2 584 * 585 * 586 * 587 * Transition T3 588 * 589 * 590 * 591 * Transition T4 592 * 593 * 594 * 595 * Transition T5 596 * 597 * 598 * 599 * Transition T6 600 * 601 * 602 * 603 * Transition T7 604 * 605 * 606 * 607 * Transition T8 608 * 609 * 610 * 611 * Transition T9 612 * 613 * 614 * 615 * Transition T10 616 * 617 * 618 * 619 * Transition T11 620 * 621 * 622 * 623 * Transition T12 624 * 625 * 626 * 627 * Transition T13 628 * 629 * 630 * 631 * Transition T14 632 * 633 * 634 * 635 */ 636 #define SMB_SESSION_MAGIC 0x53455353 /* 'SESS' */ 637 638 typedef enum { 639 SMB_SESSION_STATE_INITIALIZED = 0, 640 SMB_SESSION_STATE_DISCONNECTED, 641 SMB_SESSION_STATE_CONNECTED, 642 SMB_SESSION_STATE_ESTABLISHED, 643 SMB_SESSION_STATE_NEGOTIATED, 644 SMB_SESSION_STATE_OPLOCK_BREAKING, 645 SMB_SESSION_STATE_WRITE_RAW_ACTIVE, 646 SMB_SESSION_STATE_TERMINATED, 647 SMB_SESSION_STATE_SENTINEL 648 } smb_session_state_t; 649 650 typedef struct smb_session { 651 uint32_t s_magic; 652 smb_rwx_t s_lock; 653 list_node_t s_lnd; 654 uint64_t s_kid; 655 smb_session_state_t s_state; 656 uint32_t s_flags; 657 int s_write_raw_status; 658 kthread_t *s_thread; 659 kt_did_t s_ktdid; 660 smb_kmod_cfg_t s_cfg; 661 kmem_cache_t *s_cache; 662 kmem_cache_t *s_cache_request; 663 struct smb_server *s_server; 664 uint32_t s_gmtoff; 665 uint32_t keep_alive; 666 uint64_t opentime; 667 uint16_t vcnumber; 668 uint16_t s_local_port; 669 uint32_t ipaddr; 670 uint32_t local_ipaddr; 671 char workstation[SMB_PI_MAX_HOST]; 672 int dialect; 673 int native_os; 674 uint32_t capabilities; 675 struct smb_sign signing; 676 677 struct sonode *sock; 678 679 smb_slist_t s_req_list; 680 smb_llist_t s_xa_list; 681 smb_llist_t s_user_list; 682 smb_idpool_t s_uid_pool; 683 smb_txlst_t s_txlst; 684 685 volatile uint32_t s_tree_cnt; 686 volatile uint32_t s_file_cnt; 687 volatile uint32_t s_dir_cnt; 688 689 uint16_t secmode; 690 uint32_t sesskey; 691 uint32_t challenge_len; 692 unsigned char challenge_key[8]; 693 unsigned char MAC_key[44]; 694 int64_t activity_timestamp; 695 /* 696 * Maximum negotiated buffer size between SMB client and server 697 * in SMB_SESSION_SETUP_ANDX 698 */ 699 uint16_t smb_msg_size; 700 uchar_t *outpipe_data; 701 int outpipe_datalen; 702 int outpipe_cookie; 703 } smb_session_t; 704 705 #define SMB_USER_MAGIC 0x55534552 /* 'USER' */ 706 707 #define SMB_USER_FLAG_GUEST SMB_ATF_GUEST 708 #define SMB_USER_FLAG_IPC SMB_ATF_ANON 709 #define SMB_USER_FLAG_ADMIN SMB_ATF_ADMIN 710 #define SMB_USER_FLAG_POWER_USER SMB_ATF_POWERUSER 711 #define SMB_USER_FLAG_BACKUP_OPERATOR SMB_ATF_BACKUPOP 712 713 #define SMB_USER_PRIV_TAKE_OWNERSHIP 0x00000001 714 #define SMB_USER_PRIV_BACKUP 0x00000002 715 #define SMB_USER_PRIV_RESTORE 0x00000004 716 #define SMB_USER_PRIV_SECURITY 0x00000008 717 718 719 typedef enum { 720 SMB_USER_STATE_LOGGED_IN = 0, 721 SMB_USER_STATE_LOGGING_OFF, 722 SMB_USER_STATE_LOGGED_OFF, 723 SMB_USER_STATE_SENTINEL 724 } smb_user_state_t; 725 726 typedef struct smb_user { 727 uint32_t u_magic; 728 list_node_t u_lnd; 729 kmutex_t u_mutex; 730 smb_user_state_t u_state; 731 732 struct smb_server *u_server; 733 smb_session_t *u_session; 734 uint16_t u_name_len; 735 char *u_name; 736 uint16_t u_domain_len; 737 char *u_domain; 738 time_t u_logon_time; 739 cred_t *u_cred; 740 741 smb_llist_t u_tree_list; 742 smb_idpool_t u_tid_pool; 743 744 uint32_t u_refcnt; 745 uint32_t u_flags; 746 uint32_t u_privileges; 747 uint16_t u_uid; 748 uint32_t u_audit_sid; 749 } smb_user_t; 750 751 #define SMB_TREE_MAGIC 0x54524545 /* 'TREE' */ 752 #define SMB_TREE_TYPENAME_SZ 8 753 754 typedef enum { 755 SMB_TREE_STATE_CONNECTED = 0, 756 SMB_TREE_STATE_DISCONNECTING, 757 SMB_TREE_STATE_DISCONNECTED, 758 SMB_TREE_STATE_SENTINEL 759 } smb_tree_state_t; 760 761 typedef struct smb_tree { 762 uint32_t t_magic; 763 kmutex_t t_mutex; 764 list_node_t t_lnd; 765 smb_tree_state_t t_state; 766 767 struct smb_server *t_server; 768 smb_session_t *t_session; 769 smb_user_t *t_user; 770 smb_node_t *t_snode; 771 772 smb_llist_t t_ofile_list; 773 smb_idpool_t t_fid_pool; 774 775 smb_llist_t t_odir_list; 776 smb_idpool_t t_sid_pool; 777 778 uint32_t t_refcnt; 779 uint32_t t_flags; 780 int32_t t_res_type; 781 uint16_t t_tid; 782 uint16_t t_access; 783 uint16_t t_umask; 784 char t_sharename[MAXNAMELEN]; 785 char t_resource[MAXPATHLEN]; 786 char t_typename[SMB_TREE_TYPENAME_SZ]; 787 fs_desc_t t_fsd; 788 acl_type_t t_acltype; 789 } smb_tree_t; 790 791 /* Tree access bits */ 792 #define SMB_TREE_NO_ACCESS 0x0000 793 #define SMB_TREE_READ_ONLY 0x0001 794 #define SMB_TREE_READ_WRITE 0x0002 795 796 /* 797 * Tree flags 798 * 799 * SMB_TREE_FLAG_ACLONCREATE Underlying FS supports ACL on create. 800 * 801 * SMB_TREE_FLAG_ACEMASKONACCESS Underlying FS understands 32-bit access mask 802 */ 803 #define SMB_TREE_FLAG_OPEN 0x0001 804 #define SMB_TREE_FLAG_CLOSE 0x0002 805 #define SMB_TREE_FLAG_ACLONCREATE 0x0004 806 #define SMB_TREE_FLAG_ACEMASKONACCESS 0x0008 807 #define SMB_TREE_FLAG_IGNORE_CASE 0x0010 808 #define SMB_TREE_FLAG_NFS_MOUNTED 0x0020 809 #define SMB_TREE_FLAG_UFS 0x0040 810 #define SMB_TREE_CLOSED(tree) ((tree)->t_flags & SMB_TREE_FLAG_CLOSE) 811 812 /* 813 * SMB_TREE_CASE_INSENSITIVE returns whether operations on a given tree 814 * will be case-insensitive or not. SMB_TREE_FLAG_IGNORE_CASE is set at 815 * share set up time based on file system capability and client preference. 816 */ 817 818 #define SMB_TREE_CASE_INSENSITIVE(sr) \ 819 (((sr) && (sr)->tid_tree) ? \ 820 ((sr)->tid_tree->t_flags & SMB_TREE_FLAG_IGNORE_CASE) : 0) 821 822 /* 823 * SMB_TREE_ROOT_FS is called by certain smb_fsop_* functions to make sure 824 * that a given vnode is in the same file system as the share root. 825 */ 826 827 #define SMB_TREE_ROOT_FS(sr, node) \ 828 (((sr) && (sr)->tid_tree) ? \ 829 ((sr)->tid_tree->t_snode->vp->v_vfsp == (node)->vp->v_vfsp) : 1) 830 831 #define SMB_TREE_IS_READ_ONLY(sr) \ 832 ((sr) && ((sr)->tid_tree->t_access == SMB_TREE_READ_ONLY)) 833 834 835 #define PIPE_STATE_AUTH_VERIFY 0x00000001 836 837 /* 838 * The of_ftype of an open file should contain the SMB_FTYPE value 839 * (cifs.h) returned when the file/pipe was opened. The following 840 * assumptions are currently made: 841 * 842 * File Type Node PipeInfo 843 * --------- -------- -------- 844 * SMB_FTYPE_DISK Valid Null 845 * SMB_FTYPE_BYTE_PIPE Undefined Undefined 846 * SMB_FTYPE_MESG_PIPE Null Valid 847 * SMB_FTYPE_PRINTER Undefined Undefined 848 * SMB_FTYPE_UNKNOWN Undefined Undefined 849 */ 850 851 /* 852 * Some flags for ofile structure 853 * 854 * SMB_OFLAGS_SET_DELETE_ON_CLOSE 855 * Set this flag when the corresponding open operation whose 856 * DELETE_ON_CLOSE bit of the CreateOptions is set. If any 857 * open file instance has this bit set, the NODE_FLAGS_DELETE_ON_CLOSE 858 * will be set for the file node upon close. 859 */ 860 861 #define SMB_OFLAGS_SET_DELETE_ON_CLOSE 0x0004 862 #define SMB_OFLAGS_LLF_POS_VALID 0x0008 863 864 #define SMB_OFILE_MAGIC 0x4F464C45 /* 'OFLE' */ 865 866 typedef enum { 867 SMB_OFILE_STATE_OPEN = 0, 868 SMB_OFILE_STATE_CLOSING, 869 SMB_OFILE_STATE_CLOSED, 870 SMB_OFILE_STATE_SENTINEL 871 } smb_ofile_state_t; 872 873 typedef struct smb_ofile { 874 uint32_t f_magic; 875 kmutex_t f_mutex; 876 list_node_t f_lnd; 877 list_node_t f_nnd; 878 smb_ofile_state_t f_state; 879 880 struct smb_server *f_server; 881 smb_session_t *f_session; 882 smb_user_t *f_user; 883 smb_tree_t *f_tree; 884 smb_node_t *f_node; 885 886 mlsvc_pipe_t *f_pipe_info; 887 888 uint32_t f_uniqid; 889 uint32_t f_refcnt; 890 uint64_t f_seek_pos; 891 uint32_t f_flags; 892 uint32_t f_granted_access; 893 uint32_t f_share_access; 894 uint32_t f_create_options; 895 uint16_t f_fid; 896 uint16_t f_opened_by_pid; 897 uint16_t f_ftype; 898 uint64_t f_llf_pos; 899 int f_mode; 900 cred_t *f_cr; 901 pid_t f_pid; 902 } smb_ofile_t; 903 904 /* odir flags bits */ 905 #define SMB_DIR_FLAG_OPEN 0x0001 906 #define SMB_DIR_FLAG_CLOSE 0x0002 907 #define SMB_DIR_CLOSED(dir) ((dir)->d_flags & SMB_DIR_FLAG_CLOSE) 908 909 #define SMB_ODIR_MAGIC 0x4F444952 /* 'ODIR' */ 910 911 typedef enum { 912 SMB_ODIR_STATE_OPEN = 0, 913 SMB_ODIR_STATE_CLOSING, 914 SMB_ODIR_STATE_CLOSED, 915 SMB_ODIR_STATE_SENTINEL 916 } smb_odir_state_t; 917 918 typedef struct smb_odir { 919 uint32_t d_magic; 920 kmutex_t d_mutex; 921 list_node_t d_lnd; 922 smb_odir_state_t d_state; 923 924 smb_session_t *d_session; 925 smb_user_t *d_user; 926 smb_tree_t *d_tree; 927 928 uint32_t d_refcnt; 929 uint32_t d_cookie; 930 uint16_t d_sid; 931 uint16_t d_opened_by_pid; 932 uint16_t d_sattr; 933 char d_pattern[MAXNAMELEN]; 934 struct smb_node *d_dir_snode; 935 unsigned int d_wildcards; 936 } smb_odir_t; 937 938 typedef struct smb_odir_context { 939 uint32_t dc_cookie; 940 uint16_t dc_dattr; 941 char dc_name[MAXNAMELEN]; /* Real 'Xxxx.yyy.xx' */ 942 char dc_name83[14]; /* w/ dot 'XXXX .XX ' */ 943 char dc_shortname[14]; /* w/ dot 'XXXX.XX' */ 944 smb_attr_t dc_attr; 945 } smb_odir_context_t; 946 947 #define SMB_LOCK_MAGIC 0x4C4F434B /* 'LOCK' */ 948 949 typedef struct smb_lock { 950 uint32_t l_magic; 951 kmutex_t l_mutex; 952 list_node_t l_lnd; 953 kcondvar_t l_cv; 954 955 list_node_t l_conflict_lnd; 956 smb_slist_t l_conflict_list; 957 958 smb_session_t *l_session; 959 smb_ofile_t *l_file; 960 struct smb_request *l_sr; 961 962 uint32_t l_flags; 963 uint64_t l_session_kid; 964 struct smb_lock *l_blocked_by; /* Debug info only */ 965 966 uint16_t l_pid; 967 uint16_t l_uid; 968 uint32_t l_type; 969 uint64_t l_start; 970 uint64_t l_length; 971 clock_t l_end_time; 972 } smb_lock_t; 973 974 #define SMB_LOCK_FLAG_INDEFINITE 0x0004 975 #define SMB_LOCK_INDEFINITE_WAIT(lock) \ 976 ((lock)->l_flags & SMB_LOCK_FLAG_INDEFINITE) 977 978 #define SMB_LOCK_TYPE_READWRITE 101 979 #define SMB_LOCK_TYPE_READONLY 102 980 981 typedef struct vardata_block { 982 uint8_t tag; 983 uint16_t len; 984 struct uio uio; 985 struct iovec iovec[MAX_IOVEC]; 986 } smb_vdb_t; 987 988 #define SMB_RW_MAGIC 0x52445257 /* 'RDRW' */ 989 990 typedef struct smb_rw_param { 991 uint32_t rw_magic; 992 smb_vdb_t rw_vdb; 993 uint64_t rw_offset; 994 uint32_t rw_last_write; 995 uint16_t rw_mode; 996 uint16_t rw_count; 997 uint16_t rw_mincnt; 998 uint16_t rw_dsoff; /* SMB data offset */ 999 uint8_t rw_andx; /* SMB secondary andx command */ 1000 } smb_rw_param_t; 1001 1002 struct smb_fqi { /* fs_query_info */ 1003 char *path; 1004 uint16_t srch_attr; 1005 struct smb_node *dir_snode; 1006 smb_attr_t dir_attr; 1007 char last_comp[MAXNAMELEN]; 1008 int last_comp_was_found; 1009 char last_comp_od[MAXNAMELEN]; 1010 struct smb_node *last_snode; 1011 smb_attr_t last_attr; 1012 }; 1013 1014 #define SMB_NULL_FQI_NODES(fqi) \ 1015 (fqi).last_snode = NULL; \ 1016 (fqi).dir_snode = NULL; 1017 1018 #define FQM_DIR_MUST_EXIST 1 1019 #define FQM_PATH_MUST_EXIST 2 1020 #define FQM_PATH_MUST_NOT_EXIST 3 1021 1022 #define MYF_OPLOCK_MASK 0x000000F0 1023 #define MYF_OPLOCK_NONE 0x00000000 1024 #define MYF_EXCLUSIVE_OPLOCK 0x00000010 1025 #define MYF_BATCH_OPLOCK 0x00000020 1026 #define MYF_LEVEL_II_OPLOCK 0x00000030 1027 #define MYF_MUST_BE_DIRECTORY 0x00000100 1028 1029 #define MYF_OPLOCK_TYPE(o) ((o) & MYF_OPLOCK_MASK) 1030 #define MYF_OPLOCKS_REQUEST(o) (MYF_OPLOCK_TYPE(o) != MYF_OPLOCK_NONE) 1031 #define MYF_IS_EXCLUSIVE_OPLOCK(o) (MYF_OPLOCK_TYPE(o) == MYF_EXCLUSIVE_OPLOCK) 1032 #define MYF_IS_BATCH_OPLOCK(o) (MYF_OPLOCK_TYPE(o) == MYF_BATCH_OPLOCK) 1033 #define MYF_IS_LEVEL_II_OPLOCK(o) (MYF_OPLOCK_TYPE(o) == MYF_LEVEL_II_OPLOCK) 1034 1035 #define OPLOCK_MIN_TIMEOUT (5 * 1000) 1036 #define OPLOCK_STD_TIMEOUT (15 * 1000) 1037 #define OPLOCK_RETRIES 2 1038 1039 typedef struct { 1040 uint32_t severity; 1041 uint32_t status; 1042 uint16_t errcls; 1043 uint16_t errcode; 1044 } smb_error_t; 1045 1046 /* 1047 * SMB Request State Machine 1048 * ------------------------- 1049 * 1050 * T4 +------+ T0 1051 * +--------------------------->| FREE |---------------------------+ 1052 * | +------+ | 1053 * +-----------+ | 1054 * | COMPLETED | | 1055 * +-----------+ 1056 * ^ | 1057 * | T15 +----------+ v 1058 * +------------+ T6 | | +--------------+ 1059 * | CLEANED_UP |<-----------------| CANCELED | | INITIALIZING | 1060 * +------------+ | | +--------------+ 1061 * | ^ +----------+ | 1062 * | | ^ ^ ^ ^ | 1063 * | | +-------------+ | | | | 1064 * | | T3 | | | | T13 | T1 1065 * | +-------------------------+ | | +----------------------+ | 1066 * +----------------------------+ | | | | | 1067 * T16 | | | | +-----------+ | | 1068 * | \/ | | T5 | | v 1069 * +-----------------+ | T12 +--------+ | T2 +-----------+ 1070 * | EVENT_OCCURRED |------------->| ACTIVE |<--------------------| SUBMITTED | 1071 * +-----------------+ | +--------+ | +-----------+ 1072 * ^ | | ^ | | 1073 * | | T8 | | | T7 | 1074 * | T10 T9 | +----------+ | +-------+ | T11 1075 * | | | +-------+ | | 1076 * | | | T14 | | | 1077 * | | v | v | 1078 * +----------------------+ +--------------+ 1079 * | WAITING_EVENT | | WAITING_LOCK | 1080 * +----------------------+ +--------------+ 1081 * 1082 * 1083 * 1084 * 1085 * 1086 * Transition T0 1087 * 1088 * This transition occurs when the request is allocated and is still under the 1089 * control of the session thread. 1090 * 1091 * Transition T1 1092 * 1093 * This transition occurs when the session thread dispatches a task to treat the 1094 * request. 1095 * 1096 * Transition T2 1097 * 1098 * 1099 * 1100 * Transition T3 1101 * 1102 * A request completes and smbsr_cleanup is called to release resources 1103 * associated with the request (but not the smb_request_t itself). This 1104 * includes references on smb_ofile_t, smb_node_t, and other structures. 1105 * CLEANED_UP state exists to detect if we attempt to cleanup a request 1106 * multiple times and to allow us to detect that we are accessing a 1107 * request that has already been cleaned up. 1108 * 1109 * Transition T4 1110 * 1111 * 1112 * 1113 * Transition T5 1114 * 1115 * 1116 * 1117 * Transition T6 1118 * 1119 * 1120 * 1121 * Transition T7 1122 * 1123 * 1124 * 1125 * Transition T8 1126 * 1127 * 1128 * 1129 * Transition T9 1130 * 1131 * 1132 * 1133 * Transition T10 1134 * 1135 * 1136 * 1137 * Transition T11 1138 * 1139 * 1140 * 1141 * Transition T12 1142 * 1143 * 1144 * 1145 * Transition T13 1146 * 1147 * 1148 * 1149 * Transition T14 1150 * 1151 * 1152 * 1153 * Transition T15 1154 * 1155 * Request processing is completed (control returns from smb_dispatch) 1156 * 1157 * Transition T16 1158 * 1159 * Multipart (andx) request was cleaned up with smbsr_cleanup but more "andx" 1160 * sections remain to be processed. 1161 * 1162 */ 1163 1164 #define SMB_REQ_MAGIC 0x534D4252 /* 'SMBR' */ 1165 1166 typedef enum smb_req_state { 1167 SMB_REQ_STATE_FREE = 0, 1168 SMB_REQ_STATE_INITIALIZING, 1169 SMB_REQ_STATE_SUBMITTED, 1170 SMB_REQ_STATE_ACTIVE, 1171 SMB_REQ_STATE_WAITING_EVENT, 1172 SMB_REQ_STATE_EVENT_OCCURRED, 1173 SMB_REQ_STATE_WAITING_LOCK, 1174 SMB_REQ_STATE_COMPLETED, 1175 SMB_REQ_STATE_CANCELED, 1176 SMB_REQ_STATE_CLEANED_UP, 1177 SMB_REQ_STATE_SENTINEL 1178 } smb_req_state_t; 1179 1180 typedef struct smb_request { 1181 uint32_t sr_magic; 1182 kmutex_t sr_mutex; 1183 list_node_t sr_session_lnd; 1184 smb_req_state_t sr_state; 1185 boolean_t sr_keep; 1186 kmem_cache_t *sr_cache; 1187 struct smb_server *sr_server; 1188 pid_t *sr_pid; 1189 uint32_t sr_gmtoff; 1190 smb_session_t *session; 1191 smb_kmod_cfg_t *sr_cfg; 1192 smb_notify_change_req_t sr_ncr; 1193 1194 /* Info from session service header */ 1195 uint32_t sr_req_length; /* Excluding NBT header */ 1196 1197 /* Request buffer excluding NBT header */ 1198 void *sr_request_buf; 1199 1200 /* Fields for raw writes */ 1201 uint32_t sr_raw_data_length; 1202 void *sr_raw_data_buf; 1203 1204 smb_lock_t *sr_awaiting; 1205 struct mbuf_chain command; 1206 struct mbuf_chain reply; 1207 struct mbuf_chain raw_data; 1208 smb_malloc_list request_storage; 1209 struct smb_xa *r_xa; 1210 int andx_prev_wct; 1211 int cur_reply_offset; 1212 int orig_request_hdr; 1213 unsigned int reply_seqnum; /* reply sequence number */ 1214 unsigned char first_smb_com; /* command code */ 1215 unsigned char smb_com; /* command code */ 1216 1217 uint8_t smb_rcls; /* error code class */ 1218 uint8_t smb_reh; /* rsvd (AH DOS INT-24 ERR) */ 1219 uint16_t smb_err; /* error code */ 1220 smb_error_t smb_error; 1221 1222 uint8_t smb_flg; /* flags */ 1223 uint16_t smb_flg2; /* flags */ 1224 uint16_t smb_pid_high; /* high part of pid */ 1225 unsigned char smb_sig[8]; /* signiture */ 1226 uint16_t smb_tid; /* tree id # */ 1227 uint16_t smb_pid; /* caller's process id # */ 1228 uint16_t smb_uid; /* user id # */ 1229 uint16_t smb_mid; /* mutiplex id # */ 1230 unsigned char smb_wct; /* count of parameter words */ 1231 uint16_t smb_bcc; /* data byte count */ 1232 1233 /* Parameters */ 1234 struct mbuf_chain smb_vwv; /* variable width value */ 1235 1236 /* Data */ 1237 struct mbuf_chain smb_data; 1238 1239 uint16_t smb_fid; /* not in hdr, but common */ 1240 uint16_t smb_sid; /* not in hdr, but common */ 1241 1242 unsigned char andx_com; 1243 uint16_t andx_off; 1244 1245 struct smb_tree *tid_tree; 1246 struct smb_ofile *fid_ofile; 1247 struct smb_odir *sid_odir; 1248 smb_user_t *uid_user; 1249 1250 union { 1251 struct tcon { 1252 char *path; 1253 char *service; 1254 int pwdlen; 1255 char *password; 1256 uint16_t flags; 1257 } tcon; 1258 1259 struct open_param { 1260 struct smb_fqi fqi; 1261 uint16_t omode; 1262 uint16_t oflags; 1263 uint16_t ofun; 1264 uint32_t my_flags; 1265 uint32_t timeo; 1266 uint32_t dattr; 1267 timestruc_t crtime; 1268 uint64_t dsize; 1269 uint32_t desired_access; 1270 uint32_t share_access; 1271 uint32_t create_options; 1272 uint32_t create_disposition; 1273 uint32_t ftype, devstate; 1274 uint32_t action_taken; 1275 uint64_t fileid; 1276 uint32_t rootdirfid; 1277 /* This is only set by NTTransactCreate */ 1278 struct smb_sd *sd; 1279 } open; 1280 1281 struct dirop { 1282 struct smb_fqi fqi; 1283 struct smb_fqi dst_fqi; 1284 } dirop; 1285 1286 smb_rw_param_t *rw; 1287 uint32_t timestamp; 1288 } arg; 1289 1290 cred_t *user_cr; 1291 } smb_request_t; 1292 1293 #define SMB_READ_PROTOCOL(smb_nh_ptr) \ 1294 LE_IN32(((smb_nethdr_t *)(smb_nh_ptr))->sh_protocol) 1295 1296 #define SMB_PROTOCOL_MAGIC_INVALID(rd_sr) \ 1297 (SMB_READ_PROTOCOL((rd_sr)->sr_request_buf) != SMB_PROTOCOL_MAGIC) 1298 1299 #define SMB_READ_COMMAND(smb_nh_ptr) \ 1300 (((smb_nethdr_t *)(smb_nh_ptr))->sh_command) 1301 1302 #define SMB_IS_WRITERAW(rd_sr) \ 1303 (SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_WRITE_RAW) 1304 1305 1306 #define SR_FLG_OFFSET 9 1307 1308 #define MAX_TRANS_NAME 64 1309 1310 #define SMB_XA_FLAG_OPEN 0x0001 1311 #define SMB_XA_FLAG_CLOSE 0x0002 1312 #define SMB_XA_FLAG_COMPLETE 0x0004 1313 #define SMB_XA_CLOSED(xa) (!((xa)->xa_flags & SMB_XA_FLAG_OPEN)) 1314 1315 #define SMB_XA_MAGIC 0x534D4258 /* 'SMBX' */ 1316 1317 typedef struct smb_xa { 1318 uint32_t xa_magic; 1319 kmutex_t xa_mutex; 1320 list_node_t xa_lnd; 1321 1322 uint32_t xa_refcnt; 1323 uint32_t xa_flags; 1324 1325 struct smb_session *xa_session; 1326 1327 unsigned char smb_com; /* which TRANS type */ 1328 unsigned char smb_flg; /* flags */ 1329 uint16_t smb_flg2; /* flags */ 1330 uint16_t smb_tid; /* tree id number */ 1331 uint16_t smb_pid; /* caller's process id number */ 1332 uint16_t smb_uid; /* user id number */ 1333 uint32_t smb_func; /* NT_TRANS function */ 1334 1335 uint16_t xa_smb_mid; /* mutiplex id number */ 1336 uint16_t xa_smb_fid; /* TRANS2 secondary */ 1337 1338 unsigned int reply_seqnum; /* reply sequence number */ 1339 1340 uint32_t smb_tpscnt; /* total parameter bytes being sent */ 1341 uint32_t smb_tdscnt; /* total data bytes being sent */ 1342 uint32_t smb_mprcnt; /* max parameter bytes to return */ 1343 uint32_t smb_mdrcnt; /* max data bytes to return */ 1344 uint32_t smb_msrcnt; /* max setup words to return */ 1345 uint32_t smb_flags; /* additional information: */ 1346 /* bit 0 - if set, disconnect TID in smb_tid */ 1347 /* bit 1 - if set, transaction is one way */ 1348 /* (no final response) */ 1349 int32_t smb_timeout; /* number of milliseconds to await completion */ 1350 uint32_t smb_suwcnt; /* set up word count */ 1351 1352 1353 char *xa_smb_trans_name; 1354 1355 int req_disp_param; 1356 int req_disp_data; 1357 1358 struct mbuf_chain req_setup_mb; 1359 struct mbuf_chain req_param_mb; 1360 struct mbuf_chain req_data_mb; 1361 1362 struct mbuf_chain rep_setup_mb; 1363 struct mbuf_chain rep_param_mb; 1364 struct mbuf_chain rep_data_mb; 1365 } smb_xa_t; 1366 1367 1368 #define SDDF_NO_FLAGS 0 1369 #define SDDF_SUPPRESS_TID 0x0001 1370 #define SDDF_SUPPRESS_UID 0x0002 1371 1372 /* 1373 * SMB dispatch return codes. 1374 */ 1375 typedef enum { 1376 SDRC_SUCCESS = 0, 1377 SDRC_ERROR, 1378 SDRC_DROP_VC, 1379 SDRC_NO_REPLY, 1380 SDRC_NOT_IMPLEMENTED 1381 } smb_sdrc_t; 1382 1383 #define VAR_BCC ((short)-1) 1384 1385 #define SMB_SERVER_MAGIC 0x53534552 /* 'SSER' */ 1386 1387 typedef struct { 1388 kstat_named_t state; 1389 kstat_named_t open_files; 1390 kstat_named_t open_trees; 1391 kstat_named_t open_users; 1392 } smb_server_stats_t; 1393 1394 typedef struct { 1395 kthread_t *ld_kth; 1396 kt_did_t ld_ktdid; 1397 struct sonode *ld_so; 1398 struct sockaddr_in ld_sin; 1399 smb_session_list_t ld_session_list; 1400 } smb_listener_daemon_t; 1401 1402 typedef enum smb_server_state { 1403 SMB_SERVER_STATE_CREATED = 0, 1404 SMB_SERVER_STATE_CONFIGURED, 1405 SMB_SERVER_STATE_RUNNING, 1406 SMB_SERVER_STATE_DELETING, 1407 SMB_SERVER_STATE_SENTINEL 1408 } smb_server_state_t; 1409 1410 typedef struct smb_server { 1411 uint32_t sv_magic; 1412 kcondvar_t sv_cv; 1413 kmutex_t sv_mutex; 1414 list_node_t sv_lnd; 1415 smb_server_state_t sv_state; 1416 uint32_t sv_refcnt; 1417 pid_t sv_pid; 1418 zoneid_t sv_zid; 1419 smb_listener_daemon_t sv_nbt_daemon; 1420 smb_listener_daemon_t sv_tcp_daemon; 1421 krwlock_t sv_cfg_lock; 1422 smb_kmod_cfg_t sv_cfg; 1423 smb_session_t *sv_session; 1424 1425 kstat_t *sv_ksp; 1426 kmutex_t sv_ksp_mutex; 1427 char sv_ksp_name[KSTAT_STRLEN]; 1428 smb_server_stats_t sv_ks_data; 1429 1430 door_handle_t sv_lmshrd; 1431 1432 uint32_t si_gmtoff; 1433 1434 smb_thread_t si_thread_timers; 1435 1436 taskq_t *sv_thread_pool; 1437 1438 kmem_cache_t *si_cache_vfs; 1439 kmem_cache_t *si_cache_request; 1440 kmem_cache_t *si_cache_session; 1441 kmem_cache_t *si_cache_user; 1442 kmem_cache_t *si_cache_tree; 1443 kmem_cache_t *si_cache_ofile; 1444 kmem_cache_t *si_cache_odir; 1445 kmem_cache_t *si_cache_node; 1446 1447 volatile uint32_t sv_open_trees; 1448 volatile uint32_t sv_open_files; 1449 volatile uint32_t sv_open_users; 1450 1451 smb_node_t *si_root_smb_node; 1452 smb_llist_t sv_vfs_list; 1453 } smb_server_t; 1454 1455 #define SMB_INFO_NETBIOS_SESSION_SVC_RUNNING 0x0001 1456 #define SMB_INFO_NETBIOS_SESSION_SVC_FAILED 0x0002 1457 #define SMB_INFO_USER_LEVEL_SECURITY 0x40000000 1458 #define SMB_INFO_ENCRYPT_PASSWORDS 0x80000000 1459 1460 #define SMB_NEW_KID() atomic_inc_64_nv(&smb_kids) 1461 #define SMB_UNIQ_FID() atomic_inc_32_nv(&smb_fids) 1462 1463 /* 1464 * This is to be used by Trans2SetFileInfo 1465 * and Trans2SetPathInfo 1466 */ 1467 typedef struct smb_trans2_setinfo { 1468 uint16_t level; 1469 struct smb_xa *ts_xa; 1470 struct smb_node *node; 1471 char *path; 1472 char name[MAXNAMELEN]; 1473 } smb_trans2_setinfo_t; 1474 1475 #define SMB_IS_STREAM(node) ((node)->unnamed_stream_node) 1476 1477 #ifdef DEBUG 1478 extern uint_t smb_tsd_key; 1479 #endif 1480 1481 typedef struct smb_tsd { 1482 void (*proc)(); 1483 void *arg; 1484 char name[100]; 1485 } smb_tsd_t; 1486 1487 #define SMB_INVALID_AMASK -1 1488 #define SMB_INVALID_SHAREMODE -1 1489 #define SMB_INVALID_CRDISPOSITION -1 1490 1491 typedef struct smb_dispatch_table { 1492 smb_sdrc_t (*sdt_pre_op)(smb_request_t *); 1493 smb_sdrc_t (*sdt_function)(smb_request_t *); 1494 void (*sdt_post_op)(smb_request_t *); 1495 char sdt_dialect; 1496 unsigned char sdt_flags; 1497 krw_t sdt_slock_mode; 1498 kstat_named_t sdt_dispatch_stats; /* invocations */ 1499 } smb_dispatch_table_t; 1500 1501 /* 1502 * Discretionary Access Control List (DACL) 1503 * 1504 * A Discretionary Access Control List (DACL), often abbreviated to 1505 * ACL, is a list of access controls which either allow or deny access 1506 * for users or groups to a resource. There is a list header followed 1507 * by a list of access control entries (ACE). Each ACE specifies the 1508 * access allowed or denied to a single user or group (identified by 1509 * a SID). 1510 * 1511 * There is another access control list object called a System Access 1512 * Control List (SACL), which is used to control auditing, but no 1513 * support is provideed for SACLs at this time. 1514 * 1515 * ACL header format: 1516 * 1517 * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1518 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 1519 * +-------------------------------+---------------+---------------+ 1520 * | AclSize | Sbz1 | AclRevision | 1521 * +-------------------------------+---------------+---------------+ 1522 * | Sbz2 | AceCount | 1523 * +-------------------------------+-------------------------------+ 1524 * 1525 * AclRevision specifies the revision level of the ACL. This value should 1526 * be ACL_REVISION, unless the ACL contains an object-specific ACE, in which 1527 * case this value must be ACL_REVISION_DS. All ACEs in an ACL must be at the 1528 * same revision level. 1529 * 1530 * ACE header format: 1531 * 1532 * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1533 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 1534 * +---------------+-------+-------+---------------+---------------+ 1535 * | AceSize | AceFlags | AceType | 1536 * +---------------+-------+-------+---------------+---------------+ 1537 * 1538 * Access mask format: 1539 * 1540 * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1541 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 1542 * +---------------+---------------+-------------------------------+ 1543 * |G|G|G|G|Res'd|A| StandardRights| SpecificRights | 1544 * |R|W|E|A| |S| | | 1545 * +-+-------------+---------------+-------------------------------+ 1546 * 1547 * typedef struct ACCESS_MASK { 1548 * WORD SpecificRights; 1549 * BYTE StandardRights; 1550 * BYTE AccessSystemAcl : 1; 1551 * BYTE Reserved : 3; 1552 * BYTE GenericAll : 1; 1553 * BYTE GenericExecute : 1; 1554 * BYTE GenericWrite : 1; 1555 * BYTE GenericRead : 1; 1556 * } ACCESS_MASK; 1557 * 1558 */ 1559 1560 #define ACL_REVISION1 1 1561 #define ACL_REVISION2 2 1562 #define MIN_ACL_REVISION2 ACL_REVISION2 1563 #define ACL_REVISION3 3 1564 #define ACL_REVISION4 4 1565 #define MAX_ACL_REVISION ACL_REVISION4 1566 1567 /* 1568 * Current ACE and ACL revision Levels 1569 */ 1570 #define ACE_REVISION 1 1571 #define ACL_REVISION ACL_REVISION2 1572 #define ACL_REVISION_DS ACL_REVISION4 1573 1574 1575 #define ACCESS_ALLOWED_ACE_TYPE 0 1576 #define ACCESS_DENIED_ACE_TYPE 1 1577 #define SYSTEM_AUDIT_ACE_TYPE 2 1578 #define SYSTEM_ALARM_ACE_TYPE 3 1579 1580 /* 1581 * se_flags 1582 * ---------- 1583 * Specifies a set of ACE type-specific control flags. This member can be a 1584 * combination of the following values. 1585 * 1586 * CONTAINER_INHERIT_ACE: Child objects that are containers, such as 1587 * directories, inherit the ACE as an effective ACE. The inherited 1588 * ACE is inheritable unless the NO_PROPAGATE_INHERIT_ACE bit flag 1589 * is also set. 1590 * 1591 * INHERIT_ONLY_ACE: Indicates an inherit-only ACE which does not control 1592 * access to the object to which it is attached. 1593 * If this flag is not set, 1594 * the ACE is an effective ACE which controls access to the object 1595 * to which it is attached. 1596 * Both effective and inherit-only ACEs can be inherited 1597 * depending on the state of the other inheritance flags. 1598 * 1599 * INHERITED_ACE: Windows 2000/XP: Indicates that the ACE was inherited. 1600 * The system sets this bit when it propagates an 1601 * inherited ACE to a child object. 1602 * 1603 * NO_PROPAGATE_INHERIT_ACE: If the ACE is inherited by a child object, the 1604 * system clears the OBJECT_INHERIT_ACE and CONTAINER_INHERIT_ACE 1605 * flags in the inherited ACE. 1606 * This prevents the ACE from being inherited by 1607 * subsequent generations of objects. 1608 * 1609 * OBJECT_INHERIT_ACE: Noncontainer child objects inherit the ACE as an 1610 * effective ACE. For child objects that are containers, 1611 * the ACE is inherited as an inherit-only ACE unless the 1612 * NO_PROPAGATE_INHERIT_ACE bit flag is also set. 1613 */ 1614 #define OBJECT_INHERIT_ACE 0x01 1615 #define CONTAINER_INHERIT_ACE 0x02 1616 #define NO_PROPOGATE_INHERIT_ACE 0x04 1617 #define INHERIT_ONLY_ACE 0x08 1618 #define INHERITED_ACE 0x10 1619 #define INHERIT_MASK_ACE 0x1F 1620 1621 1622 /* 1623 * These flags are only used in system audit or alarm ACEs to 1624 * indicate when an audit message should be generated, i.e. 1625 * on successful access or on unsuccessful access. 1626 */ 1627 #define SUCCESSFUL_ACCESS_ACE_FLAG 0x40 1628 #define FAILED_ACCESS_ACE_FLAG 0x80 1629 1630 /* 1631 * se_bsize is the size, in bytes, of ACE as it appears on the wire. 1632 * se_sln is used to sort the ACL when it's required. 1633 */ 1634 typedef struct smb_acehdr { 1635 uint8_t se_type; 1636 uint8_t se_flags; 1637 uint16_t se_bsize; 1638 } smb_acehdr_t; 1639 1640 typedef struct smb_ace { 1641 smb_acehdr_t se_hdr; 1642 uint32_t se_mask; 1643 list_node_t se_sln; 1644 smb_sid_t *se_sid; 1645 } smb_ace_t; 1646 1647 /* 1648 * sl_bsize is the size of ACL in bytes as it appears on the wire. 1649 */ 1650 typedef struct smb_acl { 1651 uint8_t sl_revision; 1652 uint16_t sl_bsize; 1653 uint16_t sl_acecnt; 1654 smb_ace_t *sl_aces; 1655 list_t sl_sorted; 1656 } smb_acl_t; 1657 1658 /* 1659 * ACE/ACL header size, in byte, as it appears on the wire 1660 */ 1661 #define SMB_ACE_HDRSIZE 4 1662 #define SMB_ACL_HDRSIZE 8 1663 1664 /* 1665 * Security Descriptor (SD) 1666 * 1667 * Security descriptors provide protection for objects, for example 1668 * files and directories. It identifies the owner and primary group 1669 * (SIDs) and contains an access control list. When a user tries to 1670 * access an object his SID is compared to the permissions in the 1671 * DACL to determine if access should be allowed or denied. Note that 1672 * this is a simplification because there are other factors, such as 1673 * default behavior and privileges to be taken into account (see also 1674 * access tokens). 1675 * 1676 * The boolean flags have the following meanings when set: 1677 * 1678 * SE_OWNER_DEFAULTED indicates that the SID pointed to by the Owner 1679 * field was provided by a defaulting mechanism rather than explicitly 1680 * provided by the original provider of the security descriptor. This 1681 * may affect the treatment of the SID with respect to inheritance of 1682 * an owner. 1683 * 1684 * SE_GROUP_DEFAULTED indicates that the SID in the Group field was 1685 * provided by a defaulting mechanism rather than explicitly provided 1686 * by the original provider of the security descriptor. This may 1687 * affect the treatment of the SID with respect to inheritance of a 1688 * primary group. 1689 * 1690 * SE_DACL_PRESENT indicates that the security descriptor contains a 1691 * discretionary ACL. If this flag is set and the Dacl field of the 1692 * SECURITY_DESCRIPTOR is null, then a null ACL is explicitly being 1693 * specified. 1694 * 1695 * SE_DACL_DEFAULTED indicates that the ACL pointed to by the Dacl 1696 * field was provided by a defaulting mechanism rather than explicitly 1697 * provided by the original provider of the security descriptor. This 1698 * may affect the treatment of the ACL with respect to inheritance of 1699 * an ACL. This flag is ignored if the DaclPresent flag is not set. 1700 * 1701 * SE_SACL_PRESENT indicates that the security descriptor contains a 1702 * system ACL pointed to by the Sacl field. If this flag is set and 1703 * the Sacl field of the SECURITY_DESCRIPTOR is null, then an empty 1704 * (but present) ACL is being specified. 1705 * 1706 * SE_SACL_DEFAULTED indicates that the ACL pointed to by the Sacl 1707 * field was provided by a defaulting mechanism rather than explicitly 1708 * provided by the original provider of the security descriptor. This 1709 * may affect the treatment of the ACL with respect to inheritance of 1710 * an ACL. This flag is ignored if the SaclPresent flag is not set. 1711 * 1712 * SE_DACL_PROTECTED Prevents ACEs set on the DACL of the parent container 1713 * (and any objects above the parent container in the directory hierarchy) 1714 * from being applied to the object's DACL. 1715 * 1716 * SE_SACL_PROTECTED Prevents ACEs set on the SACL of the parent container 1717 * (and any objects above the parent container in the directory hierarchy) 1718 * from being applied to the object's SACL. 1719 * 1720 * Note that the SE_DACL_PRESENT flag needs to be present to set 1721 * SE_DACL_PROTECTED and SE_SACL_PRESENT needs to be present to set 1722 * SE_SACL_PROTECTED. 1723 * 1724 * SE_SELF_RELATIVE indicates that the security descriptor is in self- 1725 * relative form. In this form, all fields of the security descriptor 1726 * are contiguous in memory and all pointer fields are expressed as 1727 * offsets from the beginning of the security descriptor. 1728 * 1729 * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1730 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 1731 * +---------------------------------------------------------------+ 1732 * | Control |Reserved1 (SBZ)| Revision | 1733 * +---------------------------------------------------------------+ 1734 * | Owner | 1735 * +---------------------------------------------------------------+ 1736 * | Group | 1737 * +---------------------------------------------------------------+ 1738 * | Sacl | 1739 * +---------------------------------------------------------------+ 1740 * | Dacl | 1741 * +---------------------------------------------------------------+ 1742 * 1743 */ 1744 1745 #define SMB_OWNER_SECINFO 0x0001 1746 #define SMB_GROUP_SECINFO 0x0002 1747 #define SMB_DACL_SECINFO 0x0004 1748 #define SMB_SACL_SECINFO 0x0008 1749 #define SMB_ALL_SECINFO 0x000F 1750 #define SMB_ACL_SECINFO (SMB_DACL_SECINFO | SMB_SACL_SECINFO) 1751 1752 #define SECURITY_DESCRIPTOR_REVISION 1 1753 1754 1755 #define SE_OWNER_DEFAULTED 0x0001 1756 #define SE_GROUP_DEFAULTED 0x0002 1757 #define SE_DACL_PRESENT 0x0004 1758 #define SE_DACL_DEFAULTED 0x0008 1759 #define SE_SACL_PRESENT 0x0010 1760 #define SE_SACL_DEFAULTED 0x0020 1761 #define SE_DACL_AUTO_INHERIT_REQ 0x0100 1762 #define SE_SACL_AUTO_INHERIT_REQ 0x0200 1763 #define SE_DACL_AUTO_INHERITED 0x0400 1764 #define SE_SACL_AUTO_INHERITED 0x0800 1765 #define SE_DACL_PROTECTED 0x1000 1766 #define SE_SACL_PROTECTED 0x2000 1767 #define SE_SELF_RELATIVE 0x8000 1768 1769 #define SE_DACL_INHERITANCE_MASK 0x1500 1770 #define SE_SACL_INHERITANCE_MASK 0x2A00 1771 1772 /* 1773 * Security descriptor structures: 1774 * 1775 * smb_sd_t SD in SMB pointer form 1776 * smb_fssd_t SD in filesystem form 1777 * 1778 * Filesystems (e.g. ZFS/UFS) don't have something equivalent 1779 * to SD. The items comprising a SMB SD are kept separately in 1780 * filesystem. smb_fssd_t is introduced as a helper to provide 1781 * the required abstraction for CIFS code. 1782 */ 1783 1784 typedef struct smb_sd { 1785 uint8_t sd_revision; 1786 uint16_t sd_control; 1787 smb_sid_t *sd_owner; /* SID file owner */ 1788 smb_sid_t *sd_group; /* SID group (for POSIX) */ 1789 smb_acl_t *sd_sacl; /* ACL System (audits) */ 1790 smb_acl_t *sd_dacl; /* ACL Discretionary (perm) */ 1791 } smb_sd_t; 1792 1793 /* 1794 * SD header size as it appears on the wire 1795 */ 1796 #define SMB_SD_HDRSIZE 20 1797 1798 /* 1799 * values for smb_fssd.sd_flags 1800 */ 1801 #define SMB_FSSD_FLAGS_DIR 0x01 1802 1803 typedef struct smb_fssd { 1804 uint32_t sd_secinfo; 1805 uint32_t sd_flags; 1806 uid_t sd_uid; 1807 gid_t sd_gid; 1808 acl_t *sd_zdacl; 1809 acl_t *sd_zsacl; 1810 } smb_fssd_t; 1811 1812 #ifdef __cplusplus 1813 } 1814 #endif 1815 1816 #endif /* _SMBSRV_SMB_KTYPES_H */ 1817