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