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