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