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