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