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/socket.h> 46 #include <sys/acl.h> 47 #include <sys/sdt.h> 48 #include <sys/vnode.h> 49 #include <sys/cred.h> 50 #include <sys/fem.h> 51 #include <sys/door.h> 52 #include <smbsrv/smb.h> 53 #include <smbsrv/smbinfo.h> 54 #include <smbsrv/mbuf.h> 55 #include <smbsrv/smb_sid.h> 56 #include <smbsrv/smb_xdr.h> 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 * Data structure for SMB_FTYPE_MESG_PIPE ofiles, which is used 840 * at the interface between SMB and NDR RPC. 841 */ 842 typedef struct smb_opipe { 843 kmutex_t p_mutex; 844 kcondvar_t p_cv; 845 char *p_name; 846 uint32_t p_busy; 847 smb_opipe_hdr_t p_hdr; 848 smb_opipe_context_t p_context; 849 uint8_t *p_doorbuf; 850 uint8_t *p_data; 851 } smb_opipe_t; 852 853 /* 854 * The of_ftype of an open file should contain the SMB_FTYPE value 855 * (cifs.h) returned when the file/pipe was opened. The following 856 * assumptions are currently made: 857 * 858 * File Type Node PipeInfo 859 * --------- -------- -------- 860 * SMB_FTYPE_DISK Valid Null 861 * SMB_FTYPE_BYTE_PIPE Undefined Undefined 862 * SMB_FTYPE_MESG_PIPE Null Valid 863 * SMB_FTYPE_PRINTER Undefined Undefined 864 * SMB_FTYPE_UNKNOWN Undefined Undefined 865 */ 866 867 /* 868 * Some flags for ofile structure 869 * 870 * SMB_OFLAGS_SET_DELETE_ON_CLOSE 871 * Set this flag when the corresponding open operation whose 872 * DELETE_ON_CLOSE bit of the CreateOptions is set. If any 873 * open file instance has this bit set, the NODE_FLAGS_DELETE_ON_CLOSE 874 * will be set for the file node upon close. 875 */ 876 877 #define SMB_OFLAGS_SET_DELETE_ON_CLOSE 0x0004 878 #define SMB_OFLAGS_LLF_POS_VALID 0x0008 879 880 #define SMB_OFILE_MAGIC 0x4F464C45 /* 'OFLE' */ 881 882 typedef enum { 883 SMB_OFILE_STATE_OPEN = 0, 884 SMB_OFILE_STATE_CLOSING, 885 SMB_OFILE_STATE_CLOSED, 886 SMB_OFILE_STATE_SENTINEL 887 } smb_ofile_state_t; 888 889 typedef struct smb_ofile { 890 uint32_t f_magic; 891 kmutex_t f_mutex; 892 list_node_t f_lnd; 893 list_node_t f_nnd; 894 smb_ofile_state_t f_state; 895 896 struct smb_server *f_server; 897 smb_session_t *f_session; 898 smb_user_t *f_user; 899 smb_tree_t *f_tree; 900 smb_node_t *f_node; 901 smb_opipe_t *f_pipe; 902 903 uint32_t f_uniqid; 904 uint32_t f_refcnt; 905 uint64_t f_seek_pos; 906 uint32_t f_flags; 907 uint32_t f_granted_access; 908 uint32_t f_share_access; 909 uint32_t f_create_options; 910 uint16_t f_fid; 911 uint16_t f_opened_by_pid; 912 uint16_t f_ftype; 913 uint64_t f_llf_pos; 914 int f_mode; 915 cred_t *f_cr; 916 pid_t f_pid; 917 } smb_ofile_t; 918 919 /* odir flags bits */ 920 #define SMB_DIR_FLAG_OPEN 0x0001 921 #define SMB_DIR_FLAG_CLOSE 0x0002 922 #define SMB_DIR_CLOSED(dir) ((dir)->d_flags & SMB_DIR_FLAG_CLOSE) 923 924 #define SMB_ODIR_MAGIC 0x4F444952 /* 'ODIR' */ 925 926 typedef enum { 927 SMB_ODIR_STATE_OPEN = 0, 928 SMB_ODIR_STATE_CLOSING, 929 SMB_ODIR_STATE_CLOSED, 930 SMB_ODIR_STATE_SENTINEL 931 } smb_odir_state_t; 932 933 typedef struct smb_odir { 934 uint32_t d_magic; 935 kmutex_t d_mutex; 936 list_node_t d_lnd; 937 smb_odir_state_t d_state; 938 939 smb_session_t *d_session; 940 smb_user_t *d_user; 941 smb_tree_t *d_tree; 942 943 uint32_t d_refcnt; 944 uint32_t d_cookie; 945 uint16_t d_sid; 946 uint16_t d_opened_by_pid; 947 uint16_t d_sattr; 948 char d_pattern[MAXNAMELEN]; 949 struct smb_node *d_dir_snode; 950 unsigned int d_wildcards; 951 } smb_odir_t; 952 953 typedef struct smb_odir_context { 954 uint32_t dc_cookie; 955 uint16_t dc_dattr; 956 char dc_name[MAXNAMELEN]; /* Real 'Xxxx.yyy.xx' */ 957 char dc_name83[14]; /* w/ dot 'XXXX .XX ' */ 958 char dc_shortname[14]; /* w/ dot 'XXXX.XX' */ 959 smb_attr_t dc_attr; 960 } smb_odir_context_t; 961 962 #define SMB_LOCK_MAGIC 0x4C4F434B /* 'LOCK' */ 963 964 typedef struct smb_lock { 965 uint32_t l_magic; 966 kmutex_t l_mutex; 967 list_node_t l_lnd; 968 kcondvar_t l_cv; 969 970 list_node_t l_conflict_lnd; 971 smb_slist_t l_conflict_list; 972 973 smb_session_t *l_session; 974 smb_ofile_t *l_file; 975 struct smb_request *l_sr; 976 977 uint32_t l_flags; 978 uint64_t l_session_kid; 979 struct smb_lock *l_blocked_by; /* Debug info only */ 980 981 uint16_t l_pid; 982 uint16_t l_uid; 983 uint32_t l_type; 984 uint64_t l_start; 985 uint64_t l_length; 986 clock_t l_end_time; 987 } smb_lock_t; 988 989 #define SMB_LOCK_FLAG_INDEFINITE 0x0004 990 #define SMB_LOCK_INDEFINITE_WAIT(lock) \ 991 ((lock)->l_flags & SMB_LOCK_FLAG_INDEFINITE) 992 993 #define SMB_LOCK_TYPE_READWRITE 101 994 #define SMB_LOCK_TYPE_READONLY 102 995 996 typedef struct vardata_block { 997 uint8_t tag; 998 uint16_t len; 999 struct uio uio; 1000 struct iovec iovec[MAX_IOVEC]; 1001 } smb_vdb_t; 1002 1003 #define SMB_RW_MAGIC 0x52445257 /* 'RDRW' */ 1004 1005 typedef struct smb_rw_param { 1006 uint32_t rw_magic; 1007 smb_vdb_t rw_vdb; 1008 uint64_t rw_offset; 1009 uint32_t rw_last_write; 1010 uint16_t rw_mode; 1011 uint16_t rw_count; 1012 uint16_t rw_mincnt; 1013 uint16_t rw_dsoff; /* SMB data offset */ 1014 uint8_t rw_andx; /* SMB secondary andx command */ 1015 } smb_rw_param_t; 1016 1017 struct smb_fqi { /* fs_query_info */ 1018 char *path; 1019 uint16_t srch_attr; 1020 struct smb_node *dir_snode; 1021 smb_attr_t dir_attr; 1022 char last_comp[MAXNAMELEN]; 1023 int last_comp_was_found; 1024 char last_comp_od[MAXNAMELEN]; 1025 struct smb_node *last_snode; 1026 smb_attr_t last_attr; 1027 }; 1028 1029 #define SMB_NULL_FQI_NODES(fqi) \ 1030 (fqi).last_snode = NULL; \ 1031 (fqi).dir_snode = NULL; 1032 1033 #define FQM_DIR_MUST_EXIST 1 1034 #define FQM_PATH_MUST_EXIST 2 1035 #define FQM_PATH_MUST_NOT_EXIST 3 1036 1037 #define MYF_OPLOCK_MASK 0x000000F0 1038 #define MYF_OPLOCK_NONE 0x00000000 1039 #define MYF_EXCLUSIVE_OPLOCK 0x00000010 1040 #define MYF_BATCH_OPLOCK 0x00000020 1041 #define MYF_LEVEL_II_OPLOCK 0x00000030 1042 #define MYF_MUST_BE_DIRECTORY 0x00000100 1043 1044 #define MYF_OPLOCK_TYPE(o) ((o) & MYF_OPLOCK_MASK) 1045 #define MYF_OPLOCKS_REQUEST(o) (MYF_OPLOCK_TYPE(o) != MYF_OPLOCK_NONE) 1046 #define MYF_IS_EXCLUSIVE_OPLOCK(o) (MYF_OPLOCK_TYPE(o) == MYF_EXCLUSIVE_OPLOCK) 1047 #define MYF_IS_BATCH_OPLOCK(o) (MYF_OPLOCK_TYPE(o) == MYF_BATCH_OPLOCK) 1048 #define MYF_IS_LEVEL_II_OPLOCK(o) (MYF_OPLOCK_TYPE(o) == MYF_LEVEL_II_OPLOCK) 1049 1050 #define OPLOCK_MIN_TIMEOUT (5 * 1000) 1051 #define OPLOCK_STD_TIMEOUT (15 * 1000) 1052 #define OPLOCK_RETRIES 2 1053 1054 typedef struct { 1055 uint32_t severity; 1056 uint32_t status; 1057 uint16_t errcls; 1058 uint16_t errcode; 1059 } smb_error_t; 1060 1061 /* 1062 * SMB Request State Machine 1063 * ------------------------- 1064 * 1065 * T4 +------+ T0 1066 * +--------------------------->| FREE |---------------------------+ 1067 * | +------+ | 1068 * +-----------+ | 1069 * | COMPLETED | | 1070 * +-----------+ 1071 * ^ | 1072 * | T15 +----------+ v 1073 * +------------+ T6 | | +--------------+ 1074 * | CLEANED_UP |<-----------------| CANCELED | | INITIALIZING | 1075 * +------------+ | | +--------------+ 1076 * | ^ +----------+ | 1077 * | | ^ ^ ^ ^ | 1078 * | | +-------------+ | | | | 1079 * | | T3 | | | | T13 | T1 1080 * | +-------------------------+ | | +----------------------+ | 1081 * +----------------------------+ | | | | | 1082 * T16 | | | | +-----------+ | | 1083 * | \/ | | T5 | | v 1084 * +-----------------+ | T12 +--------+ | T2 +-----------+ 1085 * | EVENT_OCCURRED |------------->| ACTIVE |<--------------------| SUBMITTED | 1086 * +-----------------+ | +--------+ | +-----------+ 1087 * ^ | | ^ | | 1088 * | | T8 | | | T7 | 1089 * | T10 T9 | +----------+ | +-------+ | T11 1090 * | | | +-------+ | | 1091 * | | | T14 | | | 1092 * | | v | v | 1093 * +----------------------+ +--------------+ 1094 * | WAITING_EVENT | | WAITING_LOCK | 1095 * +----------------------+ +--------------+ 1096 * 1097 * 1098 * 1099 * 1100 * 1101 * Transition T0 1102 * 1103 * This transition occurs when the request is allocated and is still under the 1104 * control of the session thread. 1105 * 1106 * Transition T1 1107 * 1108 * This transition occurs when the session thread dispatches a task to treat the 1109 * request. 1110 * 1111 * Transition T2 1112 * 1113 * 1114 * 1115 * Transition T3 1116 * 1117 * A request completes and smbsr_cleanup is called to release resources 1118 * associated with the request (but not the smb_request_t itself). This 1119 * includes references on smb_ofile_t, smb_node_t, and other structures. 1120 * CLEANED_UP state exists to detect if we attempt to cleanup a request 1121 * multiple times and to allow us to detect that we are accessing a 1122 * request that has already been cleaned up. 1123 * 1124 * Transition T4 1125 * 1126 * 1127 * 1128 * Transition T5 1129 * 1130 * 1131 * 1132 * Transition T6 1133 * 1134 * 1135 * 1136 * Transition T7 1137 * 1138 * 1139 * 1140 * Transition T8 1141 * 1142 * 1143 * 1144 * Transition T9 1145 * 1146 * 1147 * 1148 * Transition T10 1149 * 1150 * 1151 * 1152 * Transition T11 1153 * 1154 * 1155 * 1156 * Transition T12 1157 * 1158 * 1159 * 1160 * Transition T13 1161 * 1162 * 1163 * 1164 * Transition T14 1165 * 1166 * 1167 * 1168 * Transition T15 1169 * 1170 * Request processing is completed (control returns from smb_dispatch) 1171 * 1172 * Transition T16 1173 * 1174 * Multipart (andx) request was cleaned up with smbsr_cleanup but more "andx" 1175 * sections remain to be processed. 1176 * 1177 */ 1178 1179 #define SMB_REQ_MAGIC 0x534D4252 /* 'SMBR' */ 1180 1181 typedef enum smb_req_state { 1182 SMB_REQ_STATE_FREE = 0, 1183 SMB_REQ_STATE_INITIALIZING, 1184 SMB_REQ_STATE_SUBMITTED, 1185 SMB_REQ_STATE_ACTIVE, 1186 SMB_REQ_STATE_WAITING_EVENT, 1187 SMB_REQ_STATE_EVENT_OCCURRED, 1188 SMB_REQ_STATE_WAITING_LOCK, 1189 SMB_REQ_STATE_COMPLETED, 1190 SMB_REQ_STATE_CANCELED, 1191 SMB_REQ_STATE_CLEANED_UP, 1192 SMB_REQ_STATE_SENTINEL 1193 } smb_req_state_t; 1194 1195 typedef struct smb_request { 1196 uint32_t sr_magic; 1197 kmutex_t sr_mutex; 1198 list_node_t sr_session_lnd; 1199 smb_req_state_t sr_state; 1200 boolean_t sr_keep; 1201 kmem_cache_t *sr_cache; 1202 struct smb_server *sr_server; 1203 pid_t *sr_pid; 1204 uint32_t sr_gmtoff; 1205 smb_session_t *session; 1206 smb_kmod_cfg_t *sr_cfg; 1207 smb_notify_change_req_t sr_ncr; 1208 1209 /* Info from session service header */ 1210 uint32_t sr_req_length; /* Excluding NBT header */ 1211 1212 /* Request buffer excluding NBT header */ 1213 void *sr_request_buf; 1214 1215 /* Fields for raw writes */ 1216 uint32_t sr_raw_data_length; 1217 void *sr_raw_data_buf; 1218 1219 smb_lock_t *sr_awaiting; 1220 struct mbuf_chain command; 1221 struct mbuf_chain reply; 1222 struct mbuf_chain raw_data; 1223 smb_malloc_list request_storage; 1224 struct smb_xa *r_xa; 1225 int andx_prev_wct; 1226 int cur_reply_offset; 1227 int orig_request_hdr; 1228 unsigned int reply_seqnum; /* reply sequence number */ 1229 unsigned char first_smb_com; /* command code */ 1230 unsigned char smb_com; /* command code */ 1231 1232 uint8_t smb_rcls; /* error code class */ 1233 uint8_t smb_reh; /* rsvd (AH DOS INT-24 ERR) */ 1234 uint16_t smb_err; /* error code */ 1235 smb_error_t smb_error; 1236 1237 uint8_t smb_flg; /* flags */ 1238 uint16_t smb_flg2; /* flags */ 1239 uint16_t smb_pid_high; /* high part of pid */ 1240 unsigned char smb_sig[8]; /* signiture */ 1241 uint16_t smb_tid; /* tree id # */ 1242 uint16_t smb_pid; /* caller's process id # */ 1243 uint16_t smb_uid; /* user id # */ 1244 uint16_t smb_mid; /* mutiplex id # */ 1245 unsigned char smb_wct; /* count of parameter words */ 1246 uint16_t smb_bcc; /* data byte count */ 1247 1248 /* Parameters */ 1249 struct mbuf_chain smb_vwv; /* variable width value */ 1250 1251 /* Data */ 1252 struct mbuf_chain smb_data; 1253 1254 uint16_t smb_fid; /* not in hdr, but common */ 1255 uint16_t smb_sid; /* not in hdr, but common */ 1256 1257 unsigned char andx_com; 1258 uint16_t andx_off; 1259 1260 struct smb_tree *tid_tree; 1261 struct smb_ofile *fid_ofile; 1262 struct smb_odir *sid_odir; 1263 smb_user_t *uid_user; 1264 1265 union { 1266 struct tcon { 1267 char *path; 1268 char *service; 1269 int pwdlen; 1270 char *password; 1271 uint16_t flags; 1272 } tcon; 1273 1274 struct open_param { 1275 struct smb_fqi fqi; 1276 uint16_t omode; 1277 uint16_t oflags; 1278 uint16_t ofun; 1279 uint32_t my_flags; 1280 uint32_t timeo; 1281 uint32_t dattr; 1282 timestruc_t crtime; 1283 uint64_t dsize; 1284 uint32_t desired_access; 1285 uint32_t share_access; 1286 uint32_t create_options; 1287 uint32_t create_disposition; 1288 uint32_t ftype, devstate; 1289 uint32_t action_taken; 1290 uint64_t fileid; 1291 uint32_t rootdirfid; 1292 /* This is only set by NTTransactCreate */ 1293 struct smb_sd *sd; 1294 } open; 1295 1296 struct dirop { 1297 struct smb_fqi fqi; 1298 struct smb_fqi dst_fqi; 1299 } dirop; 1300 1301 smb_rw_param_t *rw; 1302 uint32_t timestamp; 1303 } arg; 1304 1305 cred_t *user_cr; 1306 } smb_request_t; 1307 1308 #define SMB_READ_PROTOCOL(smb_nh_ptr) \ 1309 LE_IN32(((smb_nethdr_t *)(smb_nh_ptr))->sh_protocol) 1310 1311 #define SMB_PROTOCOL_MAGIC_INVALID(rd_sr) \ 1312 (SMB_READ_PROTOCOL((rd_sr)->sr_request_buf) != SMB_PROTOCOL_MAGIC) 1313 1314 #define SMB_READ_COMMAND(smb_nh_ptr) \ 1315 (((smb_nethdr_t *)(smb_nh_ptr))->sh_command) 1316 1317 #define SMB_IS_WRITERAW(rd_sr) \ 1318 (SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_WRITE_RAW) 1319 1320 1321 #define SR_FLG_OFFSET 9 1322 1323 #define MAX_TRANS_NAME 64 1324 1325 #define SMB_XA_FLAG_OPEN 0x0001 1326 #define SMB_XA_FLAG_CLOSE 0x0002 1327 #define SMB_XA_FLAG_COMPLETE 0x0004 1328 #define SMB_XA_CLOSED(xa) (!((xa)->xa_flags & SMB_XA_FLAG_OPEN)) 1329 1330 #define SMB_XA_MAGIC 0x534D4258 /* 'SMBX' */ 1331 1332 typedef struct smb_xa { 1333 uint32_t xa_magic; 1334 kmutex_t xa_mutex; 1335 list_node_t xa_lnd; 1336 1337 uint32_t xa_refcnt; 1338 uint32_t xa_flags; 1339 1340 struct smb_session *xa_session; 1341 1342 unsigned char smb_com; /* which TRANS type */ 1343 unsigned char smb_flg; /* flags */ 1344 uint16_t smb_flg2; /* flags */ 1345 uint16_t smb_tid; /* tree id number */ 1346 uint16_t smb_pid; /* caller's process id number */ 1347 uint16_t smb_uid; /* user id number */ 1348 uint32_t smb_func; /* NT_TRANS function */ 1349 1350 uint16_t xa_smb_mid; /* mutiplex id number */ 1351 uint16_t xa_smb_fid; /* TRANS2 secondary */ 1352 1353 unsigned int reply_seqnum; /* reply sequence number */ 1354 1355 uint32_t smb_tpscnt; /* total parameter bytes being sent */ 1356 uint32_t smb_tdscnt; /* total data bytes being sent */ 1357 uint32_t smb_mprcnt; /* max parameter bytes to return */ 1358 uint32_t smb_mdrcnt; /* max data bytes to return */ 1359 uint32_t smb_msrcnt; /* max setup words to return */ 1360 uint32_t smb_flags; /* additional information: */ 1361 /* bit 0 - if set, disconnect TID in smb_tid */ 1362 /* bit 1 - if set, transaction is one way */ 1363 /* (no final response) */ 1364 int32_t smb_timeout; /* number of milliseconds to await completion */ 1365 uint32_t smb_suwcnt; /* set up word count */ 1366 1367 1368 char *xa_smb_trans_name; 1369 1370 int req_disp_param; 1371 int req_disp_data; 1372 1373 struct mbuf_chain req_setup_mb; 1374 struct mbuf_chain req_param_mb; 1375 struct mbuf_chain req_data_mb; 1376 1377 struct mbuf_chain rep_setup_mb; 1378 struct mbuf_chain rep_param_mb; 1379 struct mbuf_chain rep_data_mb; 1380 } smb_xa_t; 1381 1382 1383 #define SDDF_NO_FLAGS 0 1384 #define SDDF_SUPPRESS_TID 0x0001 1385 #define SDDF_SUPPRESS_UID 0x0002 1386 1387 /* 1388 * SMB dispatch return codes. 1389 */ 1390 typedef enum { 1391 SDRC_SUCCESS = 0, 1392 SDRC_ERROR, 1393 SDRC_DROP_VC, 1394 SDRC_NO_REPLY, 1395 SDRC_NOT_IMPLEMENTED 1396 } smb_sdrc_t; 1397 1398 #define VAR_BCC ((short)-1) 1399 1400 #define SMB_SERVER_MAGIC 0x53534552 /* 'SSER' */ 1401 1402 typedef struct { 1403 kstat_named_t state; 1404 kstat_named_t open_files; 1405 kstat_named_t open_trees; 1406 kstat_named_t open_users; 1407 } smb_server_stats_t; 1408 1409 typedef struct { 1410 kthread_t *ld_kth; 1411 kt_did_t ld_ktdid; 1412 struct sonode *ld_so; 1413 struct sockaddr_in ld_sin; 1414 smb_session_list_t ld_session_list; 1415 } smb_listener_daemon_t; 1416 1417 typedef enum smb_server_state { 1418 SMB_SERVER_STATE_CREATED = 0, 1419 SMB_SERVER_STATE_CONFIGURED, 1420 SMB_SERVER_STATE_RUNNING, 1421 SMB_SERVER_STATE_DELETING, 1422 SMB_SERVER_STATE_SENTINEL 1423 } smb_server_state_t; 1424 1425 typedef struct smb_server { 1426 uint32_t sv_magic; 1427 kcondvar_t sv_cv; 1428 kmutex_t sv_mutex; 1429 list_node_t sv_lnd; 1430 smb_server_state_t sv_state; 1431 uint32_t sv_refcnt; 1432 pid_t sv_pid; 1433 zoneid_t sv_zid; 1434 smb_listener_daemon_t sv_nbt_daemon; 1435 smb_listener_daemon_t sv_tcp_daemon; 1436 krwlock_t sv_cfg_lock; 1437 smb_kmod_cfg_t sv_cfg; 1438 smb_session_t *sv_session; 1439 1440 kstat_t *sv_ksp; 1441 kmutex_t sv_ksp_mutex; 1442 char sv_ksp_name[KSTAT_STRLEN]; 1443 smb_server_stats_t sv_ks_data; 1444 1445 door_handle_t sv_lmshrd; 1446 1447 uint32_t si_gmtoff; 1448 1449 smb_thread_t si_thread_timers; 1450 1451 taskq_t *sv_thread_pool; 1452 1453 kmem_cache_t *si_cache_vfs; 1454 kmem_cache_t *si_cache_request; 1455 kmem_cache_t *si_cache_session; 1456 kmem_cache_t *si_cache_user; 1457 kmem_cache_t *si_cache_tree; 1458 kmem_cache_t *si_cache_ofile; 1459 kmem_cache_t *si_cache_odir; 1460 kmem_cache_t *si_cache_node; 1461 1462 volatile uint32_t sv_open_trees; 1463 volatile uint32_t sv_open_files; 1464 volatile uint32_t sv_open_users; 1465 1466 smb_node_t *si_root_smb_node; 1467 smb_llist_t sv_vfs_list; 1468 } smb_server_t; 1469 1470 #define SMB_INFO_NETBIOS_SESSION_SVC_RUNNING 0x0001 1471 #define SMB_INFO_NETBIOS_SESSION_SVC_FAILED 0x0002 1472 #define SMB_INFO_USER_LEVEL_SECURITY 0x40000000 1473 #define SMB_INFO_ENCRYPT_PASSWORDS 0x80000000 1474 1475 #define SMB_NEW_KID() atomic_inc_64_nv(&smb_kids) 1476 #define SMB_UNIQ_FID() atomic_inc_32_nv(&smb_fids) 1477 1478 /* 1479 * This is to be used by Trans2SetFileInfo 1480 * and Trans2SetPathInfo 1481 */ 1482 typedef struct smb_trans2_setinfo { 1483 uint16_t level; 1484 struct smb_xa *ts_xa; 1485 struct smb_node *node; 1486 char *path; 1487 char name[MAXNAMELEN]; 1488 } smb_trans2_setinfo_t; 1489 1490 #define SMB_IS_STREAM(node) ((node)->unnamed_stream_node) 1491 1492 #ifdef DEBUG 1493 extern uint_t smb_tsd_key; 1494 #endif 1495 1496 typedef struct smb_tsd { 1497 void (*proc)(); 1498 void *arg; 1499 char name[100]; 1500 } smb_tsd_t; 1501 1502 #define SMB_INVALID_AMASK -1 1503 #define SMB_INVALID_SHAREMODE -1 1504 #define SMB_INVALID_CRDISPOSITION -1 1505 1506 typedef struct smb_dispatch_table { 1507 smb_sdrc_t (*sdt_pre_op)(smb_request_t *); 1508 smb_sdrc_t (*sdt_function)(smb_request_t *); 1509 void (*sdt_post_op)(smb_request_t *); 1510 char sdt_dialect; 1511 unsigned char sdt_flags; 1512 krw_t sdt_slock_mode; 1513 kstat_named_t sdt_dispatch_stats; /* invocations */ 1514 } smb_dispatch_table_t; 1515 1516 /* 1517 * Discretionary Access Control List (DACL) 1518 * 1519 * A Discretionary Access Control List (DACL), often abbreviated to 1520 * ACL, is a list of access controls which either allow or deny access 1521 * for users or groups to a resource. There is a list header followed 1522 * by a list of access control entries (ACE). Each ACE specifies the 1523 * access allowed or denied to a single user or group (identified by 1524 * a SID). 1525 * 1526 * There is another access control list object called a System Access 1527 * Control List (SACL), which is used to control auditing, but no 1528 * support is provideed for SACLs at this time. 1529 * 1530 * ACL 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 * | AclSize | Sbz1 | AclRevision | 1536 * +-------------------------------+---------------+---------------+ 1537 * | Sbz2 | AceCount | 1538 * +-------------------------------+-------------------------------+ 1539 * 1540 * AclRevision specifies the revision level of the ACL. This value should 1541 * be ACL_REVISION, unless the ACL contains an object-specific ACE, in which 1542 * case this value must be ACL_REVISION_DS. All ACEs in an ACL must be at the 1543 * same revision level. 1544 * 1545 * ACE header format: 1546 * 1547 * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1548 * 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 1549 * +---------------+-------+-------+---------------+---------------+ 1550 * | AceSize | AceFlags | AceType | 1551 * +---------------+-------+-------+---------------+---------------+ 1552 * 1553 * Access mask format: 1554 * 1555 * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1556 * 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 1557 * +---------------+---------------+-------------------------------+ 1558 * |G|G|G|G|Res'd|A| StandardRights| SpecificRights | 1559 * |R|W|E|A| |S| | | 1560 * +-+-------------+---------------+-------------------------------+ 1561 * 1562 * typedef struct ACCESS_MASK { 1563 * WORD SpecificRights; 1564 * BYTE StandardRights; 1565 * BYTE AccessSystemAcl : 1; 1566 * BYTE Reserved : 3; 1567 * BYTE GenericAll : 1; 1568 * BYTE GenericExecute : 1; 1569 * BYTE GenericWrite : 1; 1570 * BYTE GenericRead : 1; 1571 * } ACCESS_MASK; 1572 * 1573 */ 1574 1575 #define ACL_REVISION1 1 1576 #define ACL_REVISION2 2 1577 #define MIN_ACL_REVISION2 ACL_REVISION2 1578 #define ACL_REVISION3 3 1579 #define ACL_REVISION4 4 1580 #define MAX_ACL_REVISION ACL_REVISION4 1581 1582 /* 1583 * Current ACE and ACL revision Levels 1584 */ 1585 #define ACE_REVISION 1 1586 #define ACL_REVISION ACL_REVISION2 1587 #define ACL_REVISION_DS ACL_REVISION4 1588 1589 1590 #define ACCESS_ALLOWED_ACE_TYPE 0 1591 #define ACCESS_DENIED_ACE_TYPE 1 1592 #define SYSTEM_AUDIT_ACE_TYPE 2 1593 #define SYSTEM_ALARM_ACE_TYPE 3 1594 1595 /* 1596 * se_flags 1597 * ---------- 1598 * Specifies a set of ACE type-specific control flags. This member can be a 1599 * combination of the following values. 1600 * 1601 * CONTAINER_INHERIT_ACE: Child objects that are containers, such as 1602 * directories, inherit the ACE as an effective ACE. The inherited 1603 * ACE is inheritable unless the NO_PROPAGATE_INHERIT_ACE bit flag 1604 * is also set. 1605 * 1606 * INHERIT_ONLY_ACE: Indicates an inherit-only ACE which does not control 1607 * access to the object to which it is attached. 1608 * If this flag is not set, 1609 * the ACE is an effective ACE which controls access to the object 1610 * to which it is attached. 1611 * Both effective and inherit-only ACEs can be inherited 1612 * depending on the state of the other inheritance flags. 1613 * 1614 * INHERITED_ACE: Windows 2000/XP: Indicates that the ACE was inherited. 1615 * The system sets this bit when it propagates an 1616 * inherited ACE to a child object. 1617 * 1618 * NO_PROPAGATE_INHERIT_ACE: If the ACE is inherited by a child object, the 1619 * system clears the OBJECT_INHERIT_ACE and CONTAINER_INHERIT_ACE 1620 * flags in the inherited ACE. 1621 * This prevents the ACE from being inherited by 1622 * subsequent generations of objects. 1623 * 1624 * OBJECT_INHERIT_ACE: Noncontainer child objects inherit the ACE as an 1625 * effective ACE. For child objects that are containers, 1626 * the ACE is inherited as an inherit-only ACE unless the 1627 * NO_PROPAGATE_INHERIT_ACE bit flag is also set. 1628 */ 1629 #define OBJECT_INHERIT_ACE 0x01 1630 #define CONTAINER_INHERIT_ACE 0x02 1631 #define NO_PROPOGATE_INHERIT_ACE 0x04 1632 #define INHERIT_ONLY_ACE 0x08 1633 #define INHERITED_ACE 0x10 1634 #define INHERIT_MASK_ACE 0x1F 1635 1636 1637 /* 1638 * These flags are only used in system audit or alarm ACEs to 1639 * indicate when an audit message should be generated, i.e. 1640 * on successful access or on unsuccessful access. 1641 */ 1642 #define SUCCESSFUL_ACCESS_ACE_FLAG 0x40 1643 #define FAILED_ACCESS_ACE_FLAG 0x80 1644 1645 /* 1646 * se_bsize is the size, in bytes, of ACE as it appears on the wire. 1647 * se_sln is used to sort the ACL when it's required. 1648 */ 1649 typedef struct smb_acehdr { 1650 uint8_t se_type; 1651 uint8_t se_flags; 1652 uint16_t se_bsize; 1653 } smb_acehdr_t; 1654 1655 typedef struct smb_ace { 1656 smb_acehdr_t se_hdr; 1657 uint32_t se_mask; 1658 list_node_t se_sln; 1659 smb_sid_t *se_sid; 1660 } smb_ace_t; 1661 1662 /* 1663 * sl_bsize is the size of ACL in bytes as it appears on the wire. 1664 */ 1665 typedef struct smb_acl { 1666 uint8_t sl_revision; 1667 uint16_t sl_bsize; 1668 uint16_t sl_acecnt; 1669 smb_ace_t *sl_aces; 1670 list_t sl_sorted; 1671 } smb_acl_t; 1672 1673 /* 1674 * ACE/ACL header size, in byte, as it appears on the wire 1675 */ 1676 #define SMB_ACE_HDRSIZE 4 1677 #define SMB_ACL_HDRSIZE 8 1678 1679 /* 1680 * Security Descriptor (SD) 1681 * 1682 * Security descriptors provide protection for objects, for example 1683 * files and directories. It identifies the owner and primary group 1684 * (SIDs) and contains an access control list. When a user tries to 1685 * access an object his SID is compared to the permissions in the 1686 * DACL to determine if access should be allowed or denied. Note that 1687 * this is a simplification because there are other factors, such as 1688 * default behavior and privileges to be taken into account (see also 1689 * access tokens). 1690 * 1691 * The boolean flags have the following meanings when set: 1692 * 1693 * SE_OWNER_DEFAULTED indicates that the SID pointed to by the Owner 1694 * field was provided by a defaulting mechanism rather than explicitly 1695 * provided by the original provider of the security descriptor. This 1696 * may affect the treatment of the SID with respect to inheritance of 1697 * an owner. 1698 * 1699 * SE_GROUP_DEFAULTED indicates that the SID in the Group field was 1700 * provided by a defaulting mechanism rather than explicitly provided 1701 * by the original provider of the security descriptor. This may 1702 * affect the treatment of the SID with respect to inheritance of a 1703 * primary group. 1704 * 1705 * SE_DACL_PRESENT indicates that the security descriptor contains a 1706 * discretionary ACL. If this flag is set and the Dacl field of the 1707 * SECURITY_DESCRIPTOR is null, then a null ACL is explicitly being 1708 * specified. 1709 * 1710 * SE_DACL_DEFAULTED indicates that the ACL pointed to by the Dacl 1711 * field was provided by a defaulting mechanism rather than explicitly 1712 * provided by the original provider of the security descriptor. This 1713 * may affect the treatment of the ACL with respect to inheritance of 1714 * an ACL. This flag is ignored if the DaclPresent flag is not set. 1715 * 1716 * SE_SACL_PRESENT indicates that the security descriptor contains a 1717 * system ACL pointed to by the Sacl field. If this flag is set and 1718 * the Sacl field of the SECURITY_DESCRIPTOR is null, then an empty 1719 * (but present) ACL is being specified. 1720 * 1721 * SE_SACL_DEFAULTED indicates that the ACL pointed to by the Sacl 1722 * field was provided by a defaulting mechanism rather than explicitly 1723 * provided by the original provider of the security descriptor. This 1724 * may affect the treatment of the ACL with respect to inheritance of 1725 * an ACL. This flag is ignored if the SaclPresent flag is not set. 1726 * 1727 * SE_DACL_PROTECTED Prevents ACEs set on the DACL of the parent container 1728 * (and any objects above the parent container in the directory hierarchy) 1729 * from being applied to the object's DACL. 1730 * 1731 * SE_SACL_PROTECTED Prevents ACEs set on the SACL of the parent container 1732 * (and any objects above the parent container in the directory hierarchy) 1733 * from being applied to the object's SACL. 1734 * 1735 * Note that the SE_DACL_PRESENT flag needs to be present to set 1736 * SE_DACL_PROTECTED and SE_SACL_PRESENT needs to be present to set 1737 * SE_SACL_PROTECTED. 1738 * 1739 * SE_SELF_RELATIVE indicates that the security descriptor is in self- 1740 * relative form. In this form, all fields of the security descriptor 1741 * are contiguous in memory and all pointer fields are expressed as 1742 * offsets from the beginning of the security descriptor. 1743 * 1744 * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1745 * 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 1746 * +---------------------------------------------------------------+ 1747 * | Control |Reserved1 (SBZ)| Revision | 1748 * +---------------------------------------------------------------+ 1749 * | Owner | 1750 * +---------------------------------------------------------------+ 1751 * | Group | 1752 * +---------------------------------------------------------------+ 1753 * | Sacl | 1754 * +---------------------------------------------------------------+ 1755 * | Dacl | 1756 * +---------------------------------------------------------------+ 1757 * 1758 */ 1759 1760 #define SMB_OWNER_SECINFO 0x0001 1761 #define SMB_GROUP_SECINFO 0x0002 1762 #define SMB_DACL_SECINFO 0x0004 1763 #define SMB_SACL_SECINFO 0x0008 1764 #define SMB_ALL_SECINFO 0x000F 1765 #define SMB_ACL_SECINFO (SMB_DACL_SECINFO | SMB_SACL_SECINFO) 1766 1767 #define SECURITY_DESCRIPTOR_REVISION 1 1768 1769 1770 #define SE_OWNER_DEFAULTED 0x0001 1771 #define SE_GROUP_DEFAULTED 0x0002 1772 #define SE_DACL_PRESENT 0x0004 1773 #define SE_DACL_DEFAULTED 0x0008 1774 #define SE_SACL_PRESENT 0x0010 1775 #define SE_SACL_DEFAULTED 0x0020 1776 #define SE_DACL_AUTO_INHERIT_REQ 0x0100 1777 #define SE_SACL_AUTO_INHERIT_REQ 0x0200 1778 #define SE_DACL_AUTO_INHERITED 0x0400 1779 #define SE_SACL_AUTO_INHERITED 0x0800 1780 #define SE_DACL_PROTECTED 0x1000 1781 #define SE_SACL_PROTECTED 0x2000 1782 #define SE_SELF_RELATIVE 0x8000 1783 1784 #define SE_DACL_INHERITANCE_MASK 0x1500 1785 #define SE_SACL_INHERITANCE_MASK 0x2A00 1786 1787 /* 1788 * Security descriptor structures: 1789 * 1790 * smb_sd_t SD in SMB pointer form 1791 * smb_fssd_t SD in filesystem form 1792 * 1793 * Filesystems (e.g. ZFS/UFS) don't have something equivalent 1794 * to SD. The items comprising a SMB SD are kept separately in 1795 * filesystem. smb_fssd_t is introduced as a helper to provide 1796 * the required abstraction for CIFS code. 1797 */ 1798 1799 typedef struct smb_sd { 1800 uint8_t sd_revision; 1801 uint16_t sd_control; 1802 smb_sid_t *sd_owner; /* SID file owner */ 1803 smb_sid_t *sd_group; /* SID group (for POSIX) */ 1804 smb_acl_t *sd_sacl; /* ACL System (audits) */ 1805 smb_acl_t *sd_dacl; /* ACL Discretionary (perm) */ 1806 } smb_sd_t; 1807 1808 /* 1809 * SD header size as it appears on the wire 1810 */ 1811 #define SMB_SD_HDRSIZE 20 1812 1813 /* 1814 * values for smb_fssd.sd_flags 1815 */ 1816 #define SMB_FSSD_FLAGS_DIR 0x01 1817 1818 typedef struct smb_fssd { 1819 uint32_t sd_secinfo; 1820 uint32_t sd_flags; 1821 uid_t sd_uid; 1822 gid_t sd_gid; 1823 acl_t *sd_zdacl; 1824 acl_t *sd_zsacl; 1825 } smb_fssd_t; 1826 1827 #ifdef __cplusplus 1828 } 1829 #endif 1830 1831 #endif /* _SMBSRV_SMB_KTYPES_H */ 1832