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