xref: /illumos-gate/usr/src/uts/common/smbsrv/smb_ktypes.h (revision 8d7e41661dc4633488e93b13363137523ce59977)
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 
59 struct smb_request;
60 struct smb_server;
61 struct smb_sd;
62 
63 int smb_noop(void *, size_t, int);
64 
65 #define	SMB_AUDIT_STACK_DEPTH	16
66 #define	SMB_AUDIT_BUF_MAX_REC	16
67 #define	SMB_AUDIT_NODE		0x00000001
68 
69 /*
70  * Maximum number of records returned in SMBsearch, SMBfind
71  * and SMBfindunique response. Value set to 10 for compatibility
72  * with Windows.
73  */
74 #define	SMB_MAX_SEARCH		10
75 
76 #define	SMB_SEARCH_HIDDEN(sattr) ((sattr) & FILE_ATTRIBUTE_HIDDEN)
77 #define	SMB_SEARCH_SYSTEM(sattr) ((sattr) & FILE_ATTRIBUTE_SYSTEM)
78 #define	SMB_SEARCH_DIRECTORY(sattr) ((sattr) & FILE_ATTRIBUTE_DIRECTORY)
79 
80 
81 extern uint32_t smb_audit_flags;
82 
83 typedef struct {
84 	uint32_t		anr_refcnt;
85 	int			anr_depth;
86 	pc_t			anr_stack[SMB_AUDIT_STACK_DEPTH];
87 } smb_audit_record_node_t;
88 
89 typedef struct {
90 	int			anb_index;
91 	int			anb_max_index;
92 	smb_audit_record_node_t	anb_records[SMB_AUDIT_BUF_MAX_REC];
93 } smb_audit_buf_node_t;
94 
95 #define	SMB_WORKER_PRIORITY	99
96 /*
97  * Thread State Machine
98  * --------------------
99  *
100  *			    T5			   T0
101  * smb_thread_destroy()	<-------+		+------- smb_thread_init()
102  *                              |		|
103  *				|		v
104  *			+-----------------------------+
105  *			|   SMB_THREAD_STATE_EXITED   |<---+
106  *			+-----------------------------+	   |
107  *				      | T1		   |
108  *				      v			   |
109  *			+-----------------------------+	   |
110  *			|  SMB_THREAD_STATE_STARTING  |	   |
111  *			+-----------------------------+	   |
112  *				     | T2		   | T4
113  *				     v			   |
114  *			+-----------------------------+	   |
115  *			|  SMB_THREAD_STATE_RUNNING   |	   |
116  *			+-----------------------------+	   |
117  *				     | T3		   |
118  *				     v			   |
119  *			+-----------------------------+	   |
120  *			|  SMB_THREAD_STATE_EXITING   |----+
121  *			+-----------------------------+
122  *
123  * Transition T0
124  *
125  *    This transition is executed in smb_thread_init().
126  *
127  * Transition T1
128  *
129  *    This transition is executed in smb_thread_start().
130  *
131  * Transition T2
132  *
133  *    This transition is executed by the thread itself when it starts running.
134  *
135  * Transition T3
136  *
137  *    This transition is executed by the thread itself in
138  *    smb_thread_entry_point() just before calling thread_exit().
139  *
140  *
141  * Transition T4
142  *
143  *    This transition is executed in smb_thread_stop().
144  *
145  * Transition T5
146  *
147  *    This transition is executed in smb_thread_destroy().
148  *
149  * Comments
150  * --------
151  *
152  *    The field smb_thread_aw_t contains a function pointer that knows how to
153  *    awake the thread. It is a temporary solution to work around the fact that
154  *    kernel threads (not part of a userspace process) cannot be signaled.
155  */
156 typedef enum smb_thread_state {
157 	SMB_THREAD_STATE_STARTING = 0,
158 	SMB_THREAD_STATE_RUNNING,
159 	SMB_THREAD_STATE_EXITING,
160 	SMB_THREAD_STATE_EXITED
161 } smb_thread_state_t;
162 
163 struct _smb_thread;
164 
165 typedef void (*smb_thread_ep_t)(struct _smb_thread *, void *ep_arg);
166 typedef void (*smb_thread_aw_t)(struct _smb_thread *, void *aw_arg);
167 
168 #define	SMB_THREAD_MAGIC	0x534D4254	/* SMBT */
169 
170 typedef struct _smb_thread {
171 	uint32_t		sth_magic;
172 	char			sth_name[16];
173 	smb_thread_state_t	sth_state;
174 	kthread_t		*sth_th;
175 	kt_did_t		sth_did;
176 	smb_thread_ep_t		sth_ep;
177 	void			*sth_ep_arg;
178 	smb_thread_aw_t		sth_aw;
179 	void			*sth_aw_arg;
180 	boolean_t		sth_kill;
181 	kmutex_t		sth_mtx;
182 	kcondvar_t		sth_cv;
183 } smb_thread_t;
184 
185 /*
186  * Pool of IDs
187  * -----------
188  *
189  *    A pool of IDs is a pool of 16 bit numbers. It is implemented as a bitmap.
190  *    A bit set to '1' indicates that that particular value has been allocated.
191  *    The allocation process is done shifting a bit through the whole bitmap.
192  *    The current position of that index bit is kept in the smb_idpool_t
193  *    structure and represented by a byte index (0 to buffer size minus 1) and
194  *    a bit index (0 to 7).
195  *
196  *    The pools start with a size of 8 bytes or 64 IDs. Each time the pool runs
197  *    out of IDs its current size is doubled until it reaches its maximum size
198  *    (8192 bytes or 65536 IDs). The IDs 0 and 65535 are never given out which
199  *    means that a pool can have a maximum number of 65534 IDs available.
200  */
201 #define	SMB_IDPOOL_MAGIC	0x4944504C	/* IDPL */
202 #define	SMB_IDPOOL_MIN_SIZE	64	/* Number of IDs to begin with */
203 #define	SMB_IDPOOL_MAX_SIZE	64 * 1024
204 
205 typedef struct smb_idpool {
206 	uint32_t	id_magic;
207 	kmutex_t	id_mutex;
208 	uint8_t		*id_pool;
209 	uint32_t	id_size;
210 	uint8_t		id_bit;
211 	uint8_t		id_bit_idx;
212 	uint32_t	id_idx;
213 	uint32_t	id_idx_msk;
214 	uint32_t	id_free_counter;
215 	uint32_t	id_max_free_counter;
216 } smb_idpool_t;
217 
218 /*
219  * Maximum size of a Transport Data Unit
220  *     4 --> NBT/TCP Transport Header.
221  *    32 --> SMB Header
222  *     1 --> Word Count byte
223  *   510 --> Maximum Number of bytes of the Word Table (2 * 255)
224  *     2 --> Byte count of the data
225  * 65535 --> Maximum size of the data
226  * -----
227  * 66084
228  */
229 #define	SMB_REQ_MAX_SIZE	66080
230 #define	SMB_XPRT_MAX_SIZE	(SMB_REQ_MAX_SIZE + NETBIOS_HDR_SZ)
231 
232 #define	SMB_TXREQ_MAGIC		0X54524251	/* 'TREQ' */
233 typedef struct {
234 	uint32_t	tr_magic;
235 	list_node_t	tr_lnd;
236 	int		tr_len;
237 	uint8_t		tr_buf[SMB_XPRT_MAX_SIZE];
238 } smb_txreq_t;
239 
240 #define	SMB_TXLST_MAGIC		0X544C5354	/* 'TLST' */
241 typedef struct {
242 	uint32_t	tl_magic;
243 	kmutex_t	tl_mutex;
244 	boolean_t	tl_active;
245 	list_t		tl_list;
246 } smb_txlst_t;
247 
248 /*
249  * Maximum buffer size for NT is 37KB.  If all clients are Windows 2000, this
250  * can be changed to 64KB.  37KB must be used with a mix of NT/Windows 2000
251  * clients because NT loses directory entries when values greater than 37KB are
252  * used.
253  *
254  * Note: NBT_MAXBUF will be subtracted from the specified max buffer size to
255  * account for the NBT header.
256  */
257 #define	NBT_MAXBUF		8
258 #define	SMB_NT_MAXBUF		(37 * 1024)
259 
260 #define	OUTBUFSIZE		(65 * 1024)
261 #define	SMBHEADERSIZE		32
262 #define	SMBND_HASH_MASK		(0xFF)
263 #define	MAX_IOVEC		512
264 #define	MAX_READREF		(8 * 1024)
265 
266 #define	SMB_WORKER_MIN		4
267 #define	SMB_WORKER_DEFAULT	64
268 #define	SMB_WORKER_MAX		1024
269 
270 /*
271  * Fix align a pointer or offset appropriately so that fields will not
272  * cross word boundaries.
273  */
274 #define	PTRALIGN(x) \
275 	(((uintptr_t)(x) + (uintptr_t)(_POINTER_ALIGNMENT) - 1l) & \
276 	    ~((uintptr_t)(_POINTER_ALIGNMENT) - 1l))
277 
278 /*
279  * native os types are defined in win32/smbinfo.h
280  */
281 
282 /*
283  * All 4 different time / date formats that will bee seen in SMB
284  */
285 typedef struct {
286 	uint16_t	Day	: 5;
287 	uint16_t	Month	: 4;
288 	uint16_t	Year	: 7;
289 } SMB_DATE;
290 
291 typedef struct {
292 	uint16_t	TwoSeconds : 5;
293 	uint16_t	Minutes	   : 6;
294 	uint16_t	Hours	   : 5;
295 } SMB_TIME;
296 
297 
298 typedef uint32_t 	UTIME;		/* seconds since Jan 1 1970 */
299 
300 typedef struct smb_malloc_list {
301 	struct smb_malloc_list	*forw;
302 	struct smb_malloc_list	*back;
303 } smb_malloc_list;
304 
305 typedef struct smb_llist {
306 	krwlock_t	ll_lock;
307 	list_t		ll_list;
308 	uint32_t	ll_count;
309 	uint64_t	ll_wrop;
310 } smb_llist_t;
311 
312 typedef struct smb_slist {
313 	kmutex_t	sl_mutex;
314 	kcondvar_t	sl_cv;
315 	list_t		sl_list;
316 	uint32_t	sl_count;
317 	boolean_t	sl_waiting;
318 } smb_slist_t;
319 
320 typedef struct smb_session_list {
321 	krwlock_t	se_lock;
322 	uint64_t	se_wrop;
323 	struct {
324 		list_t		lst;
325 		uint32_t	count;
326 	} se_rdy;
327 	struct {
328 		list_t		lst;
329 		uint32_t	count;
330 	} se_act;
331 } smb_session_list_t;
332 
333 typedef struct {
334 	kcondvar_t	rwx_cv;
335 	kmutex_t	rwx_mutex;
336 	krwlock_t	rwx_lock;
337 	boolean_t	rwx_waiting;
338 } smb_rwx_t;
339 
340 /* NOTIFY CHANGE */
341 
342 typedef struct smb_notify_change_req {
343 	list_node_t		nc_lnd;
344 	struct smb_node		*nc_node;
345 	uint32_t		nc_reply_type;
346 	uint32_t		nc_flags;
347 } smb_notify_change_req_t;
348 
349 /*
350  * SMB operates over a NetBIOS-over-TCP transport (NBT) or directly
351  * over TCP, which is also known as direct hosted NetBIOS-less SMB
352  * or SMB-over-TCP.
353  *
354  * NBT messages have a 4-byte header that defines the message type
355  * (8-bits), a 7-bit flags field and a 17-bit length.
356  *
357  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
358  * |      TYPE     |     FLAGS   |E|            LENGTH             |
359  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
360  *
361  * 8-bit type      Defined in RFC 1002
362  * 7-bit flags     Bits 0-6 reserved (must be 0)
363  *                 Bit 7: Length extension bit (E)
364  * 17-bit length   Includes bit 7 of the flags byte
365  *
366  *
367  * SMB-over-TCP is defined to use a modified version of the NBT header
368  * containing an 8-bit message type and 24-bit message length.
369  *
370  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
371  * |      TYPE     |                  LENGTH                       |
372  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
373  *
374  * 8-bit type      Must be 0
375  * 24-bit length
376  *
377  * The following structure is used to represent a generic, in-memory
378  * SMB transport header; it is not intended to map directly to either
379  * of the over-the-wire formats.
380  */
381 typedef struct {
382 	uint8_t		xh_type;
383 	uint32_t	xh_length;
384 } smb_xprt_t;
385 
386 int MBC_LENGTH(struct mbuf_chain *);
387 int MBC_MAXBYTES(struct mbuf_chain *);
388 void MBC_SETUP(struct mbuf_chain *, uint32_t);
389 void MBC_INIT(struct mbuf_chain *, uint32_t);
390 void MBC_FLUSH(struct mbuf_chain *);
391 void MBC_ATTACH_MBUF(struct mbuf_chain *, struct mbuf *);
392 void MBC_APPEND_MBUF(struct mbuf_chain *, struct mbuf *);
393 void MBC_ATTACH_BUF(struct mbuf_chain *MBC, unsigned char *BUF, int LEN);
394 int MBC_SHADOW_CHAIN(struct mbuf_chain *SUBMBC, struct mbuf_chain *MBC,
395     int OFF, int LEN);
396 
397 #define	MBC_ROOM_FOR(b, n) (((b)->chain_offset + (n)) <= (b)->max_bytes)
398 
399 typedef struct smb_oplock {
400 	struct smb_ofile	*op_ofile;
401 	uint32_t		op_flags;
402 	uint32_t		op_ipaddr;
403 	uint64_t		op_kid;
404 } smb_oplock_t;
405 
406 #define	OPLOCK_FLAG_BREAKING	1
407 
408 #define	OPLOCK_RELEASE_LOCK_RELEASED	0
409 #define	OPLOCK_RELEASE_FILE_CLOSED	1
410 
411 #define	DOS_ATTR_VALID	0x80000000
412 
413 #define	SMB_VFS_MAGIC	0x534D4256	/* 'SMBV' */
414 
415 typedef struct smb_vfs {
416 	uint32_t		sv_magic;
417 	list_node_t		sv_lnd;
418 	uint32_t		sv_refcnt;
419 	vfs_t			*sv_vfsp;
420 	vnode_t			*sv_rootvp;
421 } smb_vfs_t;
422 
423 typedef struct smb_unexport {
424 	list_node_t	ux_lnd;
425 	char		ux_sharename[MAXNAMELEN];
426 } smb_unexport_t;
427 
428 #define	SMB_NODE_MAGIC 0x4E4F4445	/* 'NODE' */
429 
430 typedef enum {
431 	SMB_NODE_STATE_AVAILABLE = 0,
432 	SMB_NODE_STATE_DESTROYING
433 } smb_node_state_t;
434 
435 typedef struct smb_node {
436 	uint32_t		n_magic;
437 	smb_rwx_t		n_lock;
438 	krwlock_t		n_share_lock;
439 	list_node_t		n_lnd;
440 	smb_node_state_t	n_state;
441 	uint32_t		n_refcnt;
442 	uint32_t		n_hashkey;
443 	struct smb_request	*n_sr;
444 	kmem_cache_t		*n_cache;
445 	smb_llist_t		*n_hash_bucket;
446 	uint64_t		n_orig_session_id;
447 	uint32_t		n_orig_uid;
448 	smb_llist_t		n_ofile_list;
449 	smb_llist_t		n_lock_list;
450 	struct smb_ofile	*readonly_creator;
451 	volatile int		flags;	/* FILE_NOTIFY_CHANGE_* */
452 	volatile int		waiting_event; /* # of clients requesting FCN */
453 	smb_attr_t		attr;
454 	unsigned int		what;
455 	u_offset_t		n_size;
456 	smb_oplock_t		n_oplock;
457 	struct smb_node		*dir_snode; /* Directory of node */
458 	struct smb_node		*unnamed_stream_node; /* set in stream nodes */
459 	/* Credentials for delayed delete */
460 	cred_t			*delete_on_close_cred;
461 	char			od_name[MAXNAMELEN];
462 	vnode_t			*vp;
463 	smb_audit_buf_node_t	*n_audit_buf;
464 } smb_node_t;
465 
466 #define	NODE_FLAGS_NOTIFY_CHANGE	0x10000fff
467 #define	NODE_OPLOCKS_IN_FORCE		0x0000f000
468 #define	NODE_OPLOCK_NONE		0x00000000
469 #define	NODE_EXCLUSIVE_OPLOCK		0x00001000
470 #define	NODE_BATCH_OPLOCK		0x00002000
471 #define	NODE_LEVEL_II_OPLOCK		0x00003000
472 #define	NODE_CAP_LEVEL_II		0x00010000
473 #define	NODE_PROTOCOL_LOCK		0x00020000
474 #define	NODE_FLAGS_WRITE_THROUGH	0x00100000
475 #define	NODE_FLAGS_SYNCATIME		0x00200000
476 #define	NODE_FLAGS_LOCKED		0x00400000
477 #define	NODE_FLAGS_ATTR_VALID		0x00800000
478 #define	NODE_XATTR_DIR			0x01000000
479 #define	NODE_FLAGS_CREATED		0x04000000
480 #define	NODE_FLAGS_CHANGED		0x08000000
481 #define	NODE_FLAGS_WATCH_TREE		0x10000000
482 #define	NODE_FLAGS_SET_SIZE		0x20000000
483 #define	NODE_FLAGS_DELETE_ON_CLOSE	0x40000000
484 #define	NODE_FLAGS_EXECUTABLE		0x80000000
485 
486 #define	OPLOCK_TYPE(n)			((n)->flags & NODE_OPLOCKS_IN_FORCE)
487 #define	OPLOCKS_IN_FORCE(n)		(OPLOCK_TYPE(n) != NODE_OPLOCK_NONE)
488 #define	EXCLUSIVE_OPLOCK_IN_FORCE(n)	\
489 	(OPLOCK_TYPE(n) == NODE_EXCLUSIVE_OPLOCK)
490 #define	BATCH_OPLOCK_IN_FORCE(n)	(OPLOCK_TYPE(n) == NODE_BATCH_OPLOCK)
491 #define	LEVEL_II_OPLOCK_IN_FORCE(n)	(OPLOCK_TYPE(n) == NODE_LEVEL_II_OPLOCK)
492 
493 #define	SMB_NODE_VFS(node)	((node)->vp->v_vfsp)
494 #define	SMB_NODE_FSID(node)	((node)->vp->v_vfsp->vfs_fsid)
495 
496 /*
497  * Based on section 2.6.1.2 (Connection Management) of the June 13,
498  * 1996 CIFS spec, a server may terminate the transport connection
499  * due to inactivity. The client software is expected to be able to
500  * automatically reconnect to the server if this happens. Like much
501  * of the useful background information, this section appears to
502  * have been dropped from later revisions of the document.
503  *
504  * Each session has an activity timestamp that's updated whenever a
505  * request is dispatched. If the session is idle, i.e. receives no
506  * requests, for SMB_SESSION_INACTIVITY_TIMEOUT minutes it will be
507  * closed.
508  *
509  * Each session has an I/O semaphore to serialize communication with
510  * the client. For example, after receiving a raw-read request, the
511  * server is not allowed to send an oplock break to the client until
512  * after it has sent the raw-read data.
513  */
514 #define	SMB_SESSION_INACTIVITY_TIMEOUT		(15 * 60)
515 
516 #define	SMB_SESSION_OFILE_MAX				(16 * 1024)
517 
518 /*
519  * When a connection is set up we need to remember both the client
520  * (peer) IP address and the local IP address used to establish the
521  * connection. When a client connects with a vc number of zero, we
522  * are supposed to abort any existing connections with that client
523  * (see notes in smb_negotiate.c and smb_session_setup_andx.c). For
524  * servers with multiple network interfaces or IP aliases, however,
525  * each interface has to be managed independently since the client
526  * is not aware of the server configuration. We have to allow the
527  * client to establish a connection on each interface with a vc
528  * number of zero without aborting the other connections.
529  *
530  * ipaddr:       the client (peer) IP address for the session.
531  * local_ipaddr: the local IP address used to connect to the server.
532  */
533 
534 #define	SMB_MAC_KEYSZ	512
535 
536 struct smb_sign {
537 	unsigned int seqnum;
538 	unsigned int mackey_len;
539 	unsigned int flags;
540 	unsigned char mackey[SMB_MAC_KEYSZ];
541 };
542 
543 #define	SMB_SIGNING_ENABLED	1
544 #define	SMB_SIGNING_CHECK	2
545 
546 /*
547  * Session State Machine
548  * ---------------------
549  *
550  * +-----------------------------+	     +------------------------------+
551  * | SMB_SESSION_STATE_CONNECTED |           | SMB_SESSION_STATE_TERMINATED |
552  * +-----------------------------+           +------------------------------+
553  *		T0|					     ^
554  *		  +--------------------+		     |T13
555  *		  v		       |T14                  |
556  * +-------------------------------+   |    +--------------------------------+
557  * | SMB_SESSION_STATE_ESTABLISHED |---+--->| SMB_SESSION_STATE_DISCONNECTED |
558  * +-------------------------------+        +--------------------------------+
559  *		T1|				^	   ^ ^ ^
560  *		  +----------+			|T9        | | |
561  *                           v			|          | | |
562  *                  +------------------------------+       | | |
563  *                  | SMB_SESSION_STATE_NEGOTIATED |       | | |
564  *                  +------------------------------+       | | |
565  *	                 ^|   ^|   | ^                     | | |
566  *      +----------------+|   ||   | |                     | | |
567  *      |+----------------+   || T7| |T8                   | | |
568  *      ||                    ||   | |                     | | |
569  *      ||   +----------------+|   | |                     | | |
570  *      ||   |+----------------+   | |                     | | |
571  *	||   ||			   v |                     | | |
572  *      ||   ||   +-----------------------------------+ T10| | |
573  *      ||   ||   | SMB_SESSION_STATE_OPLOCK_BREAKING |----+ | |
574  *      ||   ||   +-----------------------------------+      | |
575  *	||   ||T5                                            | |
576  *      ||   |+-->+-----------------------------------+	  T11| |
577  *      ||   |T6  | SMB_SESSION_STATE_READ_RAW_ACTIVE |------+ |
578  *      ||   +----+-----------------------------------+        |
579  *	||T3                                                   |
580  *      |+------->+------------------------------------+    T12|
581  *      |T4       | SMB_SESSION_STATE_WRITE_RAW_ACTIVE |-------+
582  *      +---------+------------------------------------+
583  *
584  * Transition T0
585  *
586  *
587  *
588  * Transition T1
589  *
590  *
591  *
592  * Transition T2
593  *
594  *
595  *
596  * Transition T3
597  *
598  *
599  *
600  * Transition T4
601  *
602  *
603  *
604  * Transition T5
605  *
606  *
607  *
608  * Transition T6
609  *
610  *
611  *
612  * Transition T7
613  *
614  *
615  *
616  * Transition T8
617  *
618  *
619  *
620  * Transition T9
621  *
622  *
623  *
624  * Transition T10
625  *
626  *
627  *
628  * Transition T11
629  *
630  *
631  *
632  * Transition T12
633  *
634  *
635  *
636  * Transition T13
637  *
638  *
639  *
640  * Transition T14
641  *
642  *
643  *
644  */
645 #define	SMB_SESSION_MAGIC 0x53455353	/* 'SESS' */
646 
647 typedef enum {
648 	SMB_SESSION_STATE_INITIALIZED = 0,
649 	SMB_SESSION_STATE_DISCONNECTED,
650 	SMB_SESSION_STATE_CONNECTED,
651 	SMB_SESSION_STATE_ESTABLISHED,
652 	SMB_SESSION_STATE_NEGOTIATED,
653 	SMB_SESSION_STATE_OPLOCK_BREAKING,
654 	SMB_SESSION_STATE_WRITE_RAW_ACTIVE,
655 	SMB_SESSION_STATE_TERMINATED,
656 	SMB_SESSION_STATE_SENTINEL
657 } smb_session_state_t;
658 
659 typedef struct smb_session {
660 	uint32_t		s_magic;
661 	smb_rwx_t		s_lock;
662 	list_node_t		s_lnd;
663 	uint64_t		s_kid;
664 	smb_session_state_t	s_state;
665 	uint32_t		s_flags;
666 	int			s_write_raw_status;
667 	kthread_t		*s_thread;
668 	kt_did_t		s_ktdid;
669 	smb_kmod_cfg_t		s_cfg;
670 	kmem_cache_t		*s_cache;
671 	kmem_cache_t		*s_cache_request;
672 	struct smb_server	*s_server;
673 	int32_t			s_gmtoff;
674 	uint32_t		keep_alive;
675 	uint64_t		opentime;
676 	uint16_t		vcnumber;
677 	uint16_t		s_local_port;
678 	uint32_t		ipaddr;
679 	uint32_t		local_ipaddr;
680 	char 			workstation[SMB_PI_MAX_HOST];
681 	int			dialect;
682 	int			native_os;
683 	uint32_t		capabilities;
684 	struct smb_sign		signing;
685 
686 	struct sonode		*sock;
687 
688 	smb_slist_t		s_req_list;
689 	smb_llist_t		s_xa_list;
690 	smb_llist_t		s_user_list;
691 	smb_idpool_t		s_uid_pool;
692 	smb_txlst_t		s_txlst;
693 
694 	volatile uint32_t	s_tree_cnt;
695 	volatile uint32_t	s_file_cnt;
696 	volatile uint32_t	s_dir_cnt;
697 
698 	uint16_t		secmode;
699 	uint32_t		sesskey;
700 	uint32_t		challenge_len;
701 	unsigned char		challenge_key[8];
702 	unsigned char		MAC_key[44];
703 	int64_t			activity_timestamp;
704 	/*
705 	 * Maximum negotiated buffer size between SMB client and server
706 	 * in SMB_SESSION_SETUP_ANDX
707 	 */
708 	uint16_t		smb_msg_size;
709 	uchar_t			*outpipe_data;
710 	int			outpipe_datalen;
711 	int			outpipe_cookie;
712 } smb_session_t;
713 
714 #define	SMB_USER_MAGIC 0x55534552	/* 'USER' */
715 
716 #define	SMB_USER_FLAG_GUEST			SMB_ATF_GUEST
717 #define	SMB_USER_FLAG_IPC			SMB_ATF_ANON
718 #define	SMB_USER_FLAG_ADMIN			SMB_ATF_ADMIN
719 #define	SMB_USER_FLAG_POWER_USER		SMB_ATF_POWERUSER
720 #define	SMB_USER_FLAG_BACKUP_OPERATOR		SMB_ATF_BACKUPOP
721 
722 #define	SMB_USER_PRIV_TAKE_OWNERSHIP	0x00000001
723 #define	SMB_USER_PRIV_BACKUP		0x00000002
724 #define	SMB_USER_PRIV_RESTORE		0x00000004
725 #define	SMB_USER_PRIV_SECURITY		0x00000008
726 
727 
728 typedef enum {
729 	SMB_USER_STATE_LOGGED_IN = 0,
730 	SMB_USER_STATE_LOGGING_OFF,
731 	SMB_USER_STATE_LOGGED_OFF,
732 	SMB_USER_STATE_SENTINEL
733 } smb_user_state_t;
734 
735 typedef struct smb_user {
736 	uint32_t		u_magic;
737 	list_node_t		u_lnd;
738 	kmutex_t		u_mutex;
739 	smb_user_state_t	u_state;
740 
741 	struct smb_server	*u_server;
742 	smb_session_t		*u_session;
743 	uint16_t		u_name_len;
744 	char			*u_name;
745 	uint16_t		u_domain_len;
746 	char			*u_domain;
747 	time_t			u_logon_time;
748 	cred_t			*u_cred;
749 	cred_t			*u_privcred;
750 
751 	smb_llist_t		u_tree_list;
752 	smb_idpool_t		u_tid_pool;
753 
754 	uint32_t		u_refcnt;
755 	uint32_t		u_flags;
756 	uint32_t		u_privileges;
757 	uint16_t		u_uid;
758 	uint32_t		u_audit_sid;
759 } smb_user_t;
760 
761 #define	SMB_TREE_MAGIC			0x54524545	/* 'TREE' */
762 
763 #define	SMB_TYPENAMELEN			_ST_FSTYPSZ
764 #define	SMB_VOLNAMELEN			32
765 
766 #define	SMB_TREE_READONLY		0x00000001
767 #define	SMB_TREE_SUPPORTS_ACLS		0x00000002
768 #define	SMB_TREE_STREAMS		0x00000004
769 #define	SMB_TREE_CASEINSENSITIVE	0x00000008
770 #define	SMB_TREE_NO_CASESENSITIVE	0x00000010
771 #define	SMB_TREE_NO_EXPORT		0x00000020
772 #define	SMB_TREE_NO_OPLOCKS		0x00000040
773 #define	SMB_TREE_NO_ATIME		0x00000080
774 #define	SMB_TREE_XVATTR			0x00000100
775 #define	SMB_TREE_DIRENTFLAGS		0x00000200
776 #define	SMB_TREE_ACLONCREATE		0x00000400
777 #define	SMB_TREE_ACEMASKONACCESS	0x00000800
778 #define	SMB_TREE_NFS_MOUNTED		0x00001000
779 
780 typedef enum {
781 	SMB_TREE_STATE_CONNECTED = 0,
782 	SMB_TREE_STATE_DISCONNECTING,
783 	SMB_TREE_STATE_DISCONNECTED,
784 	SMB_TREE_STATE_SENTINEL
785 } smb_tree_state_t;
786 
787 typedef struct smb_tree {
788 	uint32_t		t_magic;
789 	kmutex_t		t_mutex;
790 	list_node_t		t_lnd;
791 	smb_tree_state_t	t_state;
792 
793 	struct smb_server	*t_server;
794 	smb_session_t		*t_session;
795 	smb_user_t		*t_user;
796 	smb_node_t		*t_snode;
797 
798 	smb_llist_t		t_ofile_list;
799 	smb_idpool_t		t_fid_pool;
800 
801 	smb_llist_t		t_odir_list;
802 	smb_idpool_t		t_sid_pool;
803 
804 	uint32_t		t_refcnt;
805 	uint32_t		t_flags;
806 	int32_t			t_res_type;
807 	uint16_t		t_tid;
808 	uint16_t		t_umask;
809 	char			t_sharename[MAXNAMELEN];
810 	char			t_resource[MAXPATHLEN];
811 	char			t_typename[SMB_TYPENAMELEN];
812 	char			t_volume[SMB_VOLNAMELEN];
813 	acl_type_t		t_acltype;
814 } smb_tree_t;
815 
816 #define	SMB_TREE_VFS(tree)	((tree)->t_snode->vp->v_vfsp)
817 #define	SMB_TREE_FSID(tree)	((tree)->t_snode->vp->v_vfsp->vfs_fsid)
818 
819 #define	SMB_TREE_IS_READONLY(sr)                                        \
820 	(((sr) && (sr)->tid_tree) ?                                     \
821 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_READONLY) : 0)
822 
823 #define	SMB_TREE_IS_CASEINSENSITIVE(sr)                                 \
824 	(((sr) && (sr)->tid_tree) ?                                     \
825 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CASEINSENSITIVE) : 0)
826 
827 /*
828  * SMB_TREE_CONTAINS_NODE is used to check that a node is in the same
829  * file system as the tree.
830  */
831 #define	SMB_TREE_CONTAINS_NODE(sr, node)                                \
832 	(((sr) && (sr)->tid_tree) ?                                     \
833 	(SMB_TREE_VFS((sr)->tid_tree) == SMB_NODE_VFS(node)) : 1)
834 
835 /*
836  * SMB_NODE_IS_READONLY(node)
837  *
838  * This macro indicates whether the DOS readonly bit is set in the node's
839  * attribute cache.  The cache reflects what is on-disk.
840  */
841 
842 #define	SMB_NODE_IS_READONLY(node) \
843 	((node) && (node)->attr.sa_dosattr & FILE_ATTRIBUTE_READONLY)
844 
845 /*
846  * SMB_OFILE_IS_READONLY reflects whether an ofile is readonly or not.
847  * The macro takes into account
848  *      - the tree readonly state
849  *      - the node readonly state
850  *      - whether the specified ofile is the readonly creator
851  * The readonly creator has write permission until the ofile is closed.
852  */
853 
854 #define	SMB_OFILE_IS_READONLY(of)                               \
855 	(((of)->f_flags & SMB_OFLAGS_READONLY) ||               \
856 	SMB_NODE_IS_READONLY((of)->f_node) ||                   \
857 	(((of)->f_node->readonly_creator) &&                    \
858 	((of)->f_node->readonly_creator != (of))))
859 
860 /*
861  * SMB_PATHFILE_IS_READONLY indicates whether or not a file is
862  * readonly when the caller has a path rather than an ofile.  Unlike
863  * SMB_OFILE_IS_READONLY, the caller cannot be the readonly creator,
864  * since that requires an ofile.
865  */
866 
867 #define	SMB_PATHFILE_IS_READONLY(sr, node)                       \
868 	(SMB_TREE_IS_READONLY((sr)) ||                           \
869 	SMB_NODE_IS_READONLY((node)) ||                          \
870 	((node)->readonly_creator))
871 
872 #define	PIPE_STATE_AUTH_VERIFY	0x00000001
873 
874 /*
875  * Data structure for SMB_FTYPE_MESG_PIPE ofiles, which is used
876  * at the interface between SMB and NDR RPC.
877  */
878 typedef struct smb_opipe {
879 	kmutex_t p_mutex;
880 	kcondvar_t p_cv;
881 	char *p_name;
882 	uint32_t p_busy;
883 	smb_opipe_hdr_t p_hdr;
884 	smb_opipe_context_t p_context;
885 	uint8_t *p_doorbuf;
886 	uint8_t *p_data;
887 } smb_opipe_t;
888 
889 /*
890  * The of_ftype	of an open file should contain the SMB_FTYPE value
891  * (cifs.h) returned when the file/pipe was opened. The following
892  * assumptions are currently made:
893  *
894  * File Type	    Node       PipeInfo
895  * ---------	    --------   --------
896  * SMB_FTYPE_DISK       Valid      Null
897  * SMB_FTYPE_BYTE_PIPE  Undefined  Undefined
898  * SMB_FTYPE_MESG_PIPE  Null       Valid
899  * SMB_FTYPE_PRINTER    Undefined  Undefined
900  * SMB_FTYPE_UNKNOWN    Undefined  Undefined
901  */
902 
903 /*
904  * Some flags for ofile structure
905  *
906  *	SMB_OFLAGS_SET_DELETE_ON_CLOSE
907  *   Set this flag when the corresponding open operation whose
908  *   DELETE_ON_CLOSE bit of the CreateOptions is set. If any
909  *   open file instance has this bit set, the NODE_FLAGS_DELETE_ON_CLOSE
910  *   will be set for the file node upon close.
911  */
912 
913 #define	SMB_OFLAGS_READONLY		0x0001
914 #define	SMB_OFLAGS_SET_DELETE_ON_CLOSE	0x0004
915 #define	SMB_OFLAGS_LLF_POS_VALID	0x0008
916 
917 #define	SMB_OFILE_MAGIC 	0x4F464C45	/* 'OFLE' */
918 
919 typedef enum {
920 	SMB_OFILE_STATE_OPEN = 0,
921 	SMB_OFILE_STATE_CLOSING,
922 	SMB_OFILE_STATE_CLOSED,
923 	SMB_OFILE_STATE_SENTINEL
924 } smb_ofile_state_t;
925 
926 typedef struct smb_ofile {
927 	uint32_t		f_magic;
928 	kmutex_t		f_mutex;
929 	list_node_t		f_lnd;
930 	list_node_t		f_nnd;
931 	smb_ofile_state_t	f_state;
932 
933 	struct smb_server	*f_server;
934 	smb_session_t		*f_session;
935 	smb_user_t		*f_user;
936 	smb_tree_t		*f_tree;
937 	smb_node_t		*f_node;
938 	smb_opipe_t		*f_pipe;
939 
940 	uint32_t		f_uniqid;
941 	uint32_t		f_refcnt;
942 	uint64_t		f_seek_pos;
943 	uint32_t		f_flags;
944 	uint32_t		f_granted_access;
945 	uint32_t		f_share_access;
946 	uint32_t		f_create_options;
947 	uint16_t		f_fid;
948 	uint16_t		f_opened_by_pid;
949 	uint16_t		f_ftype;
950 	uint64_t		f_llf_pos;
951 	int			f_mode;
952 	cred_t			*f_cr;
953 	pid_t			f_pid;
954 } smb_ofile_t;
955 
956 /* odir flags bits */
957 #define	SMB_DIR_FLAG_OPEN	0x0001
958 #define	SMB_DIR_FLAG_CLOSE	0x0002
959 #define	SMB_DIR_CLOSED(dir) ((dir)->d_flags & SMB_DIR_FLAG_CLOSE)
960 
961 #define	SMB_ODIR_MAGIC 	0x4F444952	/* 'ODIR' */
962 
963 typedef enum {
964 	SMB_ODIR_STATE_OPEN = 0,
965 	SMB_ODIR_STATE_CLOSING,
966 	SMB_ODIR_STATE_CLOSED,
967 	SMB_ODIR_STATE_SENTINEL
968 } smb_odir_state_t;
969 
970 typedef struct smb_odir {
971 	uint32_t		d_magic;
972 	kmutex_t		d_mutex;
973 	list_node_t		d_lnd;
974 	smb_odir_state_t	d_state;
975 
976 	smb_session_t		*d_session;
977 	smb_user_t		*d_user;
978 	smb_tree_t		*d_tree;
979 
980 	uint32_t		d_refcnt;
981 	uint32_t		d_cookie;
982 	uint32_t		d_cookies[SMB_MAX_SEARCH];
983 	uint16_t		d_sid;
984 	uint16_t		d_opened_by_pid;
985 	uint16_t		d_sattr;
986 	char			d_pattern[MAXNAMELEN];
987 	struct smb_node		*d_dir_snode;
988 	unsigned int 		d_wildcards;
989 } smb_odir_t;
990 
991 typedef struct smb_odir_context {
992 	uint32_t	dc_cookie;
993 	uint16_t	dc_dattr;
994 	char		dc_name[MAXNAMELEN]; /* Real 'Xxxx.yyy.xx' */
995 	char		dc_name83[SMB_SHORTNAMELEN]; /* w/ dot 'XXXX    .XX ' */
996 	char		dc_shortname[SMB_SHORTNAMELEN]; /* w/ dot 'XXXX.XX' */
997 	smb_attr_t	dc_attr;
998 } smb_odir_context_t;
999 
1000 #define	SMB_LOCK_MAGIC 	0x4C4F434B	/* 'LOCK' */
1001 
1002 typedef struct smb_lock {
1003 	uint32_t		l_magic;
1004 	kmutex_t		l_mutex;
1005 	list_node_t		l_lnd;
1006 	kcondvar_t		l_cv;
1007 
1008 	list_node_t		l_conflict_lnd;
1009 	smb_slist_t		l_conflict_list;
1010 
1011 	smb_session_t		*l_session;
1012 	smb_ofile_t		*l_file;
1013 	struct smb_request	*l_sr;
1014 
1015 	uint32_t		l_flags;
1016 	uint64_t		l_session_kid;
1017 	struct smb_lock		*l_blocked_by; /* Debug info only */
1018 
1019 	uint16_t		l_pid;
1020 	uint16_t		l_uid;
1021 	uint32_t		l_type;
1022 	uint64_t		l_start;
1023 	uint64_t		l_length;
1024 	clock_t			l_end_time;
1025 } smb_lock_t;
1026 
1027 #define	SMB_LOCK_FLAG_INDEFINITE	0x0004
1028 #define	SMB_LOCK_INDEFINITE_WAIT(lock) \
1029 	((lock)->l_flags & SMB_LOCK_FLAG_INDEFINITE)
1030 
1031 #define	SMB_LOCK_TYPE_READWRITE		101
1032 #define	SMB_LOCK_TYPE_READONLY		102
1033 
1034 typedef struct vardata_block {
1035 	uint8_t			tag;
1036 	uint16_t		len;
1037 	struct uio 		uio;
1038 	struct iovec		iovec[MAX_IOVEC];
1039 } smb_vdb_t;
1040 
1041 #define	SMB_RW_MAGIC		0x52445257	/* 'RDRW' */
1042 
1043 typedef struct smb_rw_param {
1044 	uint32_t rw_magic;
1045 	smb_vdb_t rw_vdb;
1046 	uint64_t rw_offset;
1047 	uint32_t rw_last_write;
1048 	uint16_t rw_mode;
1049 	uint16_t rw_count;
1050 	uint16_t rw_mincnt;
1051 	uint16_t rw_dsoff;		/* SMB data offset */
1052 	uint8_t rw_andx;		/* SMB secondary andx command */
1053 } smb_rw_param_t;
1054 
1055 /*
1056  * fs_query_info
1057  */
1058 typedef struct smb_fqi {
1059 	char		*path;
1060 	uint16_t	srch_attr;
1061 	smb_node_t	*dir_snode;
1062 	smb_attr_t	dir_attr;
1063 	char		last_comp[MAXNAMELEN];
1064 	int		last_comp_was_found;
1065 	char		last_comp_od[MAXNAMELEN];
1066 	smb_node_t	*last_snode;
1067 	smb_attr_t	last_attr;
1068 } smb_fqi_t;
1069 
1070 #define	SMB_NULL_FQI_NODES(fqi) \
1071 	(fqi).last_snode = NULL;	\
1072 	(fqi).dir_snode = NULL;
1073 
1074 #define	FQM_DIR_MUST_EXIST	1
1075 #define	FQM_PATH_MUST_EXIST	2
1076 #define	FQM_PATH_MUST_NOT_EXIST 3
1077 
1078 #define	MYF_OPLOCK_MASK		0x000000F0
1079 #define	MYF_OPLOCK_NONE		0x00000000
1080 #define	MYF_EXCLUSIVE_OPLOCK	0x00000010
1081 #define	MYF_BATCH_OPLOCK	0x00000020
1082 #define	MYF_LEVEL_II_OPLOCK	0x00000030
1083 #define	MYF_MUST_BE_DIRECTORY	0x00000100
1084 
1085 #define	MYF_OPLOCK_TYPE(o)	    ((o) & MYF_OPLOCK_MASK)
1086 #define	MYF_OPLOCKS_REQUEST(o)	    (MYF_OPLOCK_TYPE(o) != MYF_OPLOCK_NONE)
1087 #define	MYF_IS_EXCLUSIVE_OPLOCK(o)  (MYF_OPLOCK_TYPE(o) == MYF_EXCLUSIVE_OPLOCK)
1088 #define	MYF_IS_BATCH_OPLOCK(o)	    (MYF_OPLOCK_TYPE(o) == MYF_BATCH_OPLOCK)
1089 #define	MYF_IS_LEVEL_II_OPLOCK(o)   (MYF_OPLOCK_TYPE(o) == MYF_LEVEL_II_OPLOCK)
1090 
1091 #define	OPLOCK_MIN_TIMEOUT	(5 * 1000)
1092 #define	OPLOCK_STD_TIMEOUT	(15 * 1000)
1093 #define	OPLOCK_RETRIES		2
1094 
1095 typedef struct {
1096 	uint32_t severity;
1097 	uint32_t status;
1098 	uint16_t errcls;
1099 	uint16_t errcode;
1100 } smb_error_t;
1101 
1102 /*
1103  * SMB Request State Machine
1104  * -------------------------
1105  *
1106  *                  T4               +------+		T0
1107  *      +--------------------------->| FREE |---------------------------+
1108  *      |                            +------+                           |
1109  * +-----------+                                                        |
1110  * | COMPLETED |                                                        |
1111  * +-----------+
1112  *      ^                                                               |
1113  *      | T15                      +----------+                         v
1114  * +------------+        T6        |          |                 +--------------+
1115  * | CLEANED_UP |<-----------------| CANCELED |                 | INITIALIZING |
1116  * +------------+                  |          |                 +--------------+
1117  *      |    ^                     +----------+                         |
1118  *      |    |                        ^  ^ ^ ^                          |
1119  *      |    |          +-------------+  | | |                          |
1120  *      |    |    T3    |                | | |               T13        | T1
1121  *      |    +-------------------------+ | | +----------------------+   |
1122  *      +----------------------------+ | | |                        |   |
1123  *         T16          |            | | | +-----------+            |   |
1124  *                      |           \/ | | T5          |            |   v
1125  * +-----------------+  |   T12     +--------+         |     T2    +-----------+
1126  * | EVENT_OCCURRED  |------------->| ACTIVE |<--------------------| SUBMITTED |
1127  * +-----------------+  |           +--------+         |           +-----------+
1128  *        ^             |              | ^ |           |
1129  *        |             |           T8 | | |  T7       |
1130  *        | T10      T9 |   +----------+ | +-------+   |  T11
1131  *        |             |   |            +-------+ |   |
1132  *        |             |   |               T14  | |   |
1133  *        |             |   v                    | v   |
1134  *      +----------------------+                +--------------+
1135  *	|     WAITING_EVENT    |                | WAITING_LOCK |
1136  *      +----------------------+                +--------------+
1137  *
1138  *
1139  *
1140  *
1141  *
1142  * Transition T0
1143  *
1144  * This transition occurs when the request is allocated and is still under the
1145  * control of the session thread.
1146  *
1147  * Transition T1
1148  *
1149  * This transition occurs when the session thread dispatches a task to treat the
1150  * request.
1151  *
1152  * Transition T2
1153  *
1154  *
1155  *
1156  * Transition T3
1157  *
1158  * A request completes and smbsr_cleanup is called to release resources
1159  * associated with the request (but not the smb_request_t itself).  This
1160  * includes references on smb_ofile_t, smb_node_t, and other structures.
1161  * CLEANED_UP state exists to detect if we attempt to cleanup a request
1162  * multiple times and to allow us to detect that we are accessing a
1163  * request that has already been cleaned up.
1164  *
1165  * Transition T4
1166  *
1167  *
1168  *
1169  * Transition T5
1170  *
1171  *
1172  *
1173  * Transition T6
1174  *
1175  *
1176  *
1177  * Transition T7
1178  *
1179  *
1180  *
1181  * Transition T8
1182  *
1183  *
1184  *
1185  * Transition T9
1186  *
1187  *
1188  *
1189  * Transition T10
1190  *
1191  *
1192  *
1193  * Transition T11
1194  *
1195  *
1196  *
1197  * Transition T12
1198  *
1199  *
1200  *
1201  * Transition T13
1202  *
1203  *
1204  *
1205  * Transition T14
1206  *
1207  *
1208  *
1209  * Transition T15
1210  *
1211  * Request processing is completed (control returns from smb_dispatch)
1212  *
1213  * Transition T16
1214  *
1215  * Multipart (andx) request was cleaned up with smbsr_cleanup but more "andx"
1216  * sections remain to be processed.
1217  *
1218  */
1219 
1220 #define	SMB_REQ_MAGIC 		0x534D4252	/* 'SMBR' */
1221 
1222 typedef enum smb_req_state {
1223 	SMB_REQ_STATE_FREE = 0,
1224 	SMB_REQ_STATE_INITIALIZING,
1225 	SMB_REQ_STATE_SUBMITTED,
1226 	SMB_REQ_STATE_ACTIVE,
1227 	SMB_REQ_STATE_WAITING_EVENT,
1228 	SMB_REQ_STATE_EVENT_OCCURRED,
1229 	SMB_REQ_STATE_WAITING_LOCK,
1230 	SMB_REQ_STATE_COMPLETED,
1231 	SMB_REQ_STATE_CANCELED,
1232 	SMB_REQ_STATE_CLEANED_UP,
1233 	SMB_REQ_STATE_SENTINEL
1234 } smb_req_state_t;
1235 
1236 typedef struct smb_request {
1237 	uint32_t		sr_magic;
1238 	kmutex_t		sr_mutex;
1239 	list_node_t		sr_session_lnd;
1240 	smb_req_state_t		sr_state;
1241 	boolean_t		sr_keep;
1242 	kmem_cache_t		*sr_cache;
1243 	struct smb_server	*sr_server;
1244 	pid_t			*sr_pid;
1245 	int32_t			sr_gmtoff;
1246 	smb_session_t		*session;
1247 	smb_kmod_cfg_t		*sr_cfg;
1248 	smb_notify_change_req_t	sr_ncr;
1249 
1250 	/* Info from session service header */
1251 	uint32_t		sr_req_length; /* Excluding NBT header */
1252 
1253 	/* Request buffer excluding NBT header */
1254 	void			*sr_request_buf;
1255 
1256 	/* Fields for raw writes */
1257 	uint32_t		sr_raw_data_length;
1258 	void			*sr_raw_data_buf;
1259 
1260 	smb_lock_t		*sr_awaiting;
1261 	struct mbuf_chain	command;
1262 	struct mbuf_chain	reply;
1263 	struct mbuf_chain	raw_data;
1264 	smb_malloc_list		request_storage;
1265 	struct smb_xa		*r_xa;
1266 	int			andx_prev_wct;
1267 	int 			cur_reply_offset;
1268 	int			orig_request_hdr;
1269 	unsigned int		reply_seqnum;	/* reply sequence number */
1270 	unsigned char		first_smb_com;	/* command code */
1271 	unsigned char		smb_com;	/* command code */
1272 
1273 	uint8_t			smb_rcls;	/* error code class */
1274 	uint8_t			smb_reh;	/* rsvd (AH DOS INT-24 ERR) */
1275 	uint16_t		smb_err;	/* error code */
1276 	smb_error_t		smb_error;
1277 
1278 	uint8_t			smb_flg;	/* flags */
1279 	uint16_t		smb_flg2;	/* flags */
1280 	uint16_t		smb_pid_high;	/* high part of pid */
1281 	unsigned char		smb_sig[8];	/* signiture */
1282 	uint16_t		smb_tid;	/* tree id #  */
1283 	uint16_t		smb_pid;	/* caller's process id # */
1284 	uint16_t		smb_uid;	/* user id # */
1285 	uint16_t		smb_mid;	/* mutiplex id #  */
1286 	unsigned char		smb_wct;	/* count of parameter words */
1287 	uint16_t		smb_bcc;	/* data byte count */
1288 
1289 	/* Parameters */
1290 	struct mbuf_chain	smb_vwv;	/* variable width value */
1291 
1292 	/* Data */
1293 	struct mbuf_chain	smb_data;
1294 
1295 	uint16_t		smb_fid;	/* not in hdr, but common */
1296 	uint16_t		smb_sid;	/* not in hdr, but common */
1297 
1298 	unsigned char		andx_com;
1299 	uint16_t		andx_off;
1300 
1301 	struct smb_tree		*tid_tree;
1302 	struct smb_ofile	*fid_ofile;
1303 	struct smb_odir		*sid_odir;
1304 	smb_user_t		*uid_user;
1305 
1306 	union {
1307 	    struct tcon {
1308 		char		*path;
1309 		char		*service;
1310 		int		pwdlen;
1311 		char		*password;
1312 		uint16_t	flags;
1313 		uint16_t	optional_support;
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