xref: /illumos-gate/usr/src/uts/common/smbsrv/smb_ktypes.h (revision 9fb67ea305c66b6a297583b9b0db6796b0dfe497)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Structures and type definitions for the SMB module.
28  */
29 
30 #ifndef _SMBSRV_SMB_KTYPES_H
31 #define	_SMBSRV_SMB_KTYPES_H
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #include <sys/note.h>
38 #include <sys/systm.h>
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/synch.h>
42 #include <sys/taskq.h>
43 #include <sys/socket.h>
44 #include <sys/acl.h>
45 #include <sys/sdt.h>
46 #include <sys/stat.h>
47 #include <sys/vnode.h>
48 #include <sys/cred.h>
49 #include <netinet/in.h>
50 #include <sys/ksocket.h>
51 #include <sys/fem.h>
52 #include <sys/door.h>
53 #include <sys/extdirent.h>
54 #include <smbsrv/smb.h>
55 #include <smbsrv/smbinfo.h>
56 #include <smbsrv/mbuf.h>
57 #include <smbsrv/smb_sid.h>
58 #include <smbsrv/smb_xdr.h>
59 #include <smbsrv/netbios.h>
60 #include <smbsrv/smb_vops.h>
61 
62 struct smb_disp_entry;
63 struct smb_request;
64 struct smb_server;
65 struct smb_event;
66 
67 int smb_noop(void *, size_t, int);
68 
69 #define	SMB_AUDIT_STACK_DEPTH	16
70 #define	SMB_AUDIT_BUF_MAX_REC	16
71 #define	SMB_AUDIT_NODE		0x00000001
72 
73 /*
74  * Maximum number of records returned in SMBsearch, SMBfind
75  * and SMBfindunique response. Value set to 10 for compatibility
76  * with Windows.
77  */
78 #define	SMB_MAX_SEARCH		10
79 
80 #define	SMB_SEARCH_ATTRIBUTES    \
81 	(FILE_ATTRIBUTE_HIDDEN | \
82 	FILE_ATTRIBUTE_SYSTEM |  \
83 	FILE_ATTRIBUTE_DIRECTORY)
84 
85 #define	SMB_SEARCH_HIDDEN(sattr) ((sattr) & FILE_ATTRIBUTE_HIDDEN)
86 #define	SMB_SEARCH_SYSTEM(sattr) ((sattr) & FILE_ATTRIBUTE_SYSTEM)
87 #define	SMB_SEARCH_DIRECTORY(sattr) ((sattr) & FILE_ATTRIBUTE_DIRECTORY)
88 #define	SMB_SEARCH_ALL(sattr) ((sattr) & SMB_SEARCH_ATTRIBUTES)
89 
90 typedef struct {
91 	uint32_t		anr_refcnt;
92 	int			anr_depth;
93 	pc_t			anr_stack[SMB_AUDIT_STACK_DEPTH];
94 } smb_audit_record_node_t;
95 
96 typedef struct {
97 	int			anb_index;
98 	int			anb_max_index;
99 	smb_audit_record_node_t	anb_records[SMB_AUDIT_BUF_MAX_REC];
100 } smb_audit_buf_node_t;
101 
102 #define	SMB_WORKER_PRIORITY	99
103 /*
104  * Thread State Machine
105  * --------------------
106  *
107  *			    T5			   T0
108  * smb_thread_destroy()	<-------+		+------- smb_thread_init()
109  *                              |		|
110  *				|		v
111  *			+-----------------------------+
112  *			|   SMB_THREAD_STATE_EXITED   |<---+
113  *			+-----------------------------+	   |
114  *				      | T1		   |
115  *				      v			   |
116  *			+-----------------------------+	   |
117  *			|  SMB_THREAD_STATE_STARTING  |	   |
118  *			+-----------------------------+	   |
119  *				     | T2		   | T4
120  *				     v			   |
121  *			+-----------------------------+	   |
122  *			|  SMB_THREAD_STATE_RUNNING   |	   |
123  *			+-----------------------------+	   |
124  *				     | T3		   |
125  *				     v			   |
126  *			+-----------------------------+	   |
127  *			|  SMB_THREAD_STATE_EXITING   |----+
128  *			+-----------------------------+
129  *
130  * Transition T0
131  *
132  *    This transition is executed in smb_thread_init().
133  *
134  * Transition T1
135  *
136  *    This transition is executed in smb_thread_start().
137  *
138  * Transition T2
139  *
140  *    This transition is executed by the thread itself when it starts running.
141  *
142  * Transition T3
143  *
144  *    This transition is executed by the thread itself in
145  *    smb_thread_entry_point() just before calling thread_exit().
146  *
147  *
148  * Transition T4
149  *
150  *    This transition is executed in smb_thread_stop().
151  *
152  * Transition T5
153  *
154  *    This transition is executed in smb_thread_destroy().
155  *
156  * Comments
157  * --------
158  *
159  *    The field smb_thread_aw_t contains a function pointer that knows how to
160  *    awake the thread. It is a temporary solution to work around the fact that
161  *    kernel threads (not part of a userspace process) cannot be signaled.
162  */
163 typedef enum smb_thread_state {
164 	SMB_THREAD_STATE_STARTING = 0,
165 	SMB_THREAD_STATE_RUNNING,
166 	SMB_THREAD_STATE_EXITING,
167 	SMB_THREAD_STATE_EXITED
168 } smb_thread_state_t;
169 
170 struct _smb_thread;
171 
172 typedef void (*smb_thread_ep_t)(struct _smb_thread *, void *ep_arg);
173 typedef void (*smb_thread_aw_t)(struct _smb_thread *, void *aw_arg);
174 
175 #define	SMB_THREAD_MAGIC	0x534D4254	/* SMBT */
176 
177 typedef struct _smb_thread {
178 	uint32_t		sth_magic;
179 	char			sth_name[16];
180 	smb_thread_state_t	sth_state;
181 	kthread_t		*sth_th;
182 	kt_did_t		sth_did;
183 	smb_thread_ep_t		sth_ep;
184 	void			*sth_ep_arg;
185 	smb_thread_aw_t		sth_aw;
186 	void			*sth_aw_arg;
187 	boolean_t		sth_kill;
188 	kmutex_t		sth_mtx;
189 	kcondvar_t		sth_cv;
190 } smb_thread_t;
191 
192 /*
193  * Pool of IDs
194  * -----------
195  *
196  *    A pool of IDs is a pool of 16 bit numbers. It is implemented as a bitmap.
197  *    A bit set to '1' indicates that that particular value has been allocated.
198  *    The allocation process is done shifting a bit through the whole bitmap.
199  *    The current position of that index bit is kept in the smb_idpool_t
200  *    structure and represented by a byte index (0 to buffer size minus 1) and
201  *    a bit index (0 to 7).
202  *
203  *    The pools start with a size of 8 bytes or 64 IDs. Each time the pool runs
204  *    out of IDs its current size is doubled until it reaches its maximum size
205  *    (8192 bytes or 65536 IDs). The IDs 0 and 65535 are never given out which
206  *    means that a pool can have a maximum number of 65534 IDs available.
207  */
208 #define	SMB_IDPOOL_MAGIC	0x4944504C	/* IDPL */
209 #define	SMB_IDPOOL_MIN_SIZE	64	/* Number of IDs to begin with */
210 #define	SMB_IDPOOL_MAX_SIZE	64 * 1024
211 
212 typedef struct smb_idpool {
213 	uint32_t	id_magic;
214 	kmutex_t	id_mutex;
215 	uint8_t		*id_pool;
216 	uint32_t	id_size;
217 	uint8_t		id_bit;
218 	uint8_t		id_bit_idx;
219 	uint32_t	id_idx;
220 	uint32_t	id_idx_msk;
221 	uint32_t	id_free_counter;
222 	uint32_t	id_max_free_counter;
223 } smb_idpool_t;
224 
225 /*
226  * Maximum size of a Transport Data Unit when CAP_LARGE_READX and
227  * CAP_LARGE_WRITEX are not set.  CAP_LARGE_READX/CAP_LARGE_WRITEX
228  * allow the payload to exceed the negotiated buffer size.
229  *     4 --> NBT/TCP Transport Header.
230  *    32 --> SMB Header
231  *     1 --> Word Count byte
232  *   510 --> Maximum Number of bytes of the Word Table (2 * 255)
233  *     2 --> Byte count of the data
234  * 65535 --> Maximum size of the data
235  * -----
236  * 66084
237  */
238 #define	SMB_REQ_MAX_SIZE	66560		/* 65KB */
239 #define	SMB_XPRT_MAX_SIZE	(SMB_REQ_MAX_SIZE + NETBIOS_HDR_SZ)
240 
241 #define	SMB_TXREQ_MAGIC		0X54524251	/* 'TREQ' */
242 typedef struct {
243 	uint32_t	tr_magic;
244 	list_node_t	tr_lnd;
245 	int		tr_len;
246 	uint8_t		tr_buf[SMB_XPRT_MAX_SIZE];
247 } smb_txreq_t;
248 
249 #define	SMB_TXLST_MAGIC		0X544C5354	/* 'TLST' */
250 typedef struct {
251 	uint32_t	tl_magic;
252 	kmutex_t	tl_mutex;
253 	boolean_t	tl_active;
254 	list_t		tl_list;
255 } smb_txlst_t;
256 
257 /*
258  * Maximum buffer size for NT is 37KB.  If all clients are Windows 2000, this
259  * can be changed to 64KB.  37KB must be used with a mix of NT/Windows 2000
260  * clients because NT loses directory entries when values greater than 37KB are
261  * used.
262  *
263  * Note: NBT_MAXBUF will be subtracted from the specified max buffer size to
264  * account for the NBT header.
265  */
266 #define	NBT_MAXBUF		8
267 #define	SMB_NT_MAXBUF		(37 * 1024)
268 
269 #define	OUTBUFSIZE		(65 * 1024)
270 #define	SMBHEADERSIZE		32
271 #define	SMBND_HASH_MASK		(0xFF)
272 #define	MAX_IOVEC		512
273 #define	MAX_READREF		(8 * 1024)
274 
275 #define	SMB_WORKER_MIN		4
276 #define	SMB_WORKER_DEFAULT	64
277 #define	SMB_WORKER_MAX		1024
278 
279 /*
280  * Destructor object used in the locked-list delete queue.
281  */
282 #define	SMB_DTOR_MAGIC		0x44544F52	/* DTOR */
283 #define	SMB_DTOR_VALID(d)	\
284     ASSERT(((d) != NULL) && ((d)->dt_magic == SMB_DTOR_MAGIC))
285 
286 typedef void (*smb_dtorproc_t)(void *);
287 
288 typedef struct smb_dtor {
289 	uint32_t	dt_magic;
290 	list_node_t	dt_lnd;
291 	void		*dt_object;
292 	smb_dtorproc_t	dt_proc;
293 } smb_dtor_t;
294 
295 typedef struct smb_llist {
296 	krwlock_t	ll_lock;
297 	list_t		ll_list;
298 	uint32_t	ll_count;
299 	uint64_t	ll_wrop;
300 	kmutex_t	ll_mutex;
301 	list_t		ll_deleteq;
302 	uint32_t	ll_deleteq_count;
303 } smb_llist_t;
304 
305 typedef struct smb_slist {
306 	kmutex_t	sl_mutex;
307 	kcondvar_t	sl_cv;
308 	list_t		sl_list;
309 	uint32_t	sl_count;
310 	boolean_t	sl_waiting;
311 } smb_slist_t;
312 
313 typedef struct smb_session_list {
314 	krwlock_t	se_lock;
315 	uint64_t	se_wrop;
316 	struct {
317 		list_t		lst;
318 		uint32_t	count;
319 	} se_rdy;
320 	struct {
321 		list_t		lst;
322 		uint32_t	count;
323 	} se_act;
324 } smb_session_list_t;
325 
326 typedef struct {
327 	kcondvar_t	rwx_cv;
328 	kmutex_t	rwx_mutex;
329 	krwlock_t	rwx_lock;
330 	boolean_t	rwx_waiting;
331 } smb_rwx_t;
332 
333 /* NOTIFY CHANGE */
334 
335 typedef struct smb_notify_change_req {
336 	list_node_t		nc_lnd;
337 	struct smb_node		*nc_node;
338 	uint32_t		nc_reply_type;
339 	uint32_t		nc_flags;
340 } smb_notify_change_req_t;
341 
342 /*
343  * SMB operates over a NetBIOS-over-TCP transport (NBT) or directly
344  * over TCP, which is also known as direct hosted NetBIOS-less SMB
345  * or SMB-over-TCP.
346  *
347  * NBT messages have a 4-byte header that defines the message type
348  * (8-bits), a 7-bit flags field and a 17-bit length.
349  *
350  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
351  * |      TYPE     |     FLAGS   |E|            LENGTH             |
352  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
353  *
354  * 8-bit type      Defined in RFC 1002
355  * 7-bit flags     Bits 0-6 reserved (must be 0)
356  *                 Bit 7: Length extension bit (E)
357  * 17-bit length   Includes bit 7 of the flags byte
358  *
359  *
360  * SMB-over-TCP is defined to use a modified version of the NBT header
361  * containing an 8-bit message type and 24-bit message length.
362  *
363  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
364  * |      TYPE     |                  LENGTH                       |
365  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
366  *
367  * 8-bit type      Must be 0
368  * 24-bit length
369  *
370  * The following structure is used to represent a generic, in-memory
371  * SMB transport header; it is not intended to map directly to either
372  * of the over-the-wire formats.
373  */
374 typedef struct {
375 	uint8_t		xh_type;
376 	uint32_t	xh_length;
377 } smb_xprt_t;
378 
379 int MBC_LENGTH(struct mbuf_chain *);
380 int MBC_MAXBYTES(struct mbuf_chain *);
381 void MBC_SETUP(struct mbuf_chain *, uint32_t);
382 void MBC_INIT(struct mbuf_chain *, uint32_t);
383 void MBC_FLUSH(struct mbuf_chain *);
384 void MBC_ATTACH_MBUF(struct mbuf_chain *, struct mbuf *);
385 void MBC_APPEND_MBUF(struct mbuf_chain *, struct mbuf *);
386 void MBC_ATTACH_BUF(struct mbuf_chain *MBC, unsigned char *BUF, int LEN);
387 int MBC_SHADOW_CHAIN(struct mbuf_chain *SUBMBC, struct mbuf_chain *MBC,
388     int OFF, int LEN);
389 
390 #define	MBC_ROOM_FOR(b, n) (((b)->chain_offset + (n)) <= (b)->max_bytes)
391 
392 /*
393  * ol_sess_id:
394  *
395  *	ID of the session holding the oplock (if an oplock was granted).
396  *
397  * ol_xthread:
398  *
399  *	Worker thread treating the command that was granted the oplock. Until
400  *	that thread is done with that command and has submitted the response
401  *	to the network stack, all the other threads will be suspended in
402  *	smb_oplock_enter(). They will be awaken when the worker thread
403  *	referenced in 'ol_xthread' calls smb_oplock_broadcast().
404  *
405  *	The purpose of this mechanism is to prevent another thread from
406  *	triggering a oplock break before the response conveying the grant
407  *	has been sent.
408  *
409  * ol_ofile
410  *
411  *	Open file that was granted the oplock.
412  *
413  * ol_waiters_count
414  *
415  *	Number of threads waiting for a call to smb_oplock_broadcast().
416  *
417  * ol_level
418  *
419  *	Level of the oplock granted.
420  */
421 typedef struct smb_oplock {
422 	uint64_t		ol_sess_id;
423 	kcondvar_t		ol_cv;
424 	kthread_t		*ol_xthread;
425 	struct smb_ofile	*ol_ofile;
426 	uint8_t			ol_level;
427 } smb_oplock_t;
428 
429 #define	SMB_VFS_MAGIC	0x534D4256	/* 'SMBV' */
430 
431 typedef struct smb_vfs {
432 	uint32_t		sv_magic;
433 	list_node_t		sv_lnd;
434 	uint32_t		sv_refcnt;
435 	vfs_t			*sv_vfsp;
436 	vnode_t			*sv_rootvp;
437 } smb_vfs_t;
438 
439 typedef struct smb_unexport {
440 	list_node_t	ux_lnd;
441 	char		ux_sharename[MAXNAMELEN];
442 } smb_unexport_t;
443 
444 /*
445  * Solaris file systems handle timestamps differently from NTFS.
446  * In order to provide a more similar view of an open file's
447  * timestamps, we cache the timestamps in the node and manipulate
448  * them in a manner more consistent with windows.
449  * t_cached is B_TRUE when timestamps are cached.
450  * Timestamps remain cached while there are open ofiles for the node.
451  * This includes open ofiles for named streams.  t_open_ofiles is a
452  * count of open ofiles on the node, including named streams' ofiles,
453  * n_open_ofiles cannot be used as it doesn't include ofiles opened
454  * for the node's named streams.
455  */
456 typedef struct smb_times {
457 	uint32_t		t_open_ofiles;
458 	boolean_t		t_cached;
459 	timestruc_t		t_atime;
460 	timestruc_t		t_mtime;
461 	timestruc_t		t_ctime;
462 	timestruc_t		t_crtime;
463 } smb_times_t;
464 
465 #define	SMB_NODE_MAGIC		0x4E4F4445	/* 'NODE' */
466 #define	SMB_NODE_VALID(p)	ASSERT((p)->n_magic == SMB_NODE_MAGIC)
467 
468 typedef enum {
469 	SMB_NODE_STATE_AVAILABLE = 0,
470 	SMB_NODE_STATE_OPLOCK_GRANTED,
471 	SMB_NODE_STATE_OPLOCK_BREAKING,
472 	SMB_NODE_STATE_DESTROYING
473 } smb_node_state_t;
474 
475 /*
476  * waiting_event        # of clients requesting FCN
477  * n_timestamps         cached timestamps
478  * n_allocsz            cached file allocation size
479  * n_dnode              directory node
480  * n_unode              unnamed stream node
481  * delete_on_close_cred credentials for delayed delete
482  */
483 typedef struct smb_node {
484 	uint32_t		n_magic;
485 	krwlock_t		n_lock;
486 	kmutex_t		n_mutex;
487 	list_node_t		n_lnd;
488 	smb_node_state_t	n_state;
489 	uint32_t		n_refcnt;
490 	uint32_t		n_hashkey;
491 	smb_llist_t		*n_hash_bucket;
492 	uint32_t		n_open_count;
493 	smb_llist_t		n_ofile_list;
494 	smb_llist_t		n_lock_list;
495 	struct smb_ofile	*readonly_creator;
496 	volatile int		flags;
497 	volatile int		waiting_event;
498 	smb_times_t		n_timestamps;
499 	u_offset_t		n_allocsz;
500 	smb_oplock_t		n_oplock;
501 	struct smb_node		*n_dnode;
502 	struct smb_node		*n_unode;
503 	cred_t			*delete_on_close_cred;
504 	uint32_t		n_delete_on_close_flags;
505 	char			od_name[MAXNAMELEN];
506 	vnode_t			*vp;
507 	smb_audit_buf_node_t	*n_audit_buf;
508 } smb_node_t;
509 
510 #define	NODE_FLAGS_REPARSE		0x00001000
511 #define	NODE_FLAGS_DFSLINK		0x00002000
512 #define	NODE_FLAGS_VFSROOT		0x00004000
513 #define	NODE_FLAGS_SYSTEM		0x00008000
514 #define	NODE_FLAGS_WATCH_TREE		0x10000000
515 #define	NODE_FLAGS_NOTIFY_CHANGE	\
516 	(NODE_FLAGS_WATCH_TREE | FILE_NOTIFY_VALID_MASK)
517 #define	NODE_FLAGS_CHANGED		0x08000000
518 #define	NODE_FLAGS_WRITE_THROUGH	0x00100000
519 #define	NODE_XATTR_DIR			0x01000000
520 #define	NODE_FLAGS_DELETE_ON_CLOSE	0x40000000
521 #define	NODE_FLAGS_EXECUTABLE		0x80000000
522 
523 #define	SMB_NODE_VFS(node)	((node)->vp->v_vfsp)
524 #define	SMB_NODE_FSID(node)	((node)->vp->v_vfsp->vfs_fsid)
525 
526 /*
527  * Based on section 2.6.1.2 (Connection Management) of the June 13,
528  * 1996 CIFS spec, a server may terminate the transport connection
529  * due to inactivity. The client software is expected to be able to
530  * automatically reconnect to the server if this happens. Like much
531  * of the useful background information, this section appears to
532  * have been dropped from later revisions of the document.
533  *
534  * Each session has an activity timestamp that's updated whenever a
535  * request is dispatched. If the session is idle, i.e. receives no
536  * requests, for SMB_SESSION_INACTIVITY_TIMEOUT minutes it will be
537  * closed.
538  *
539  * Each session has an I/O semaphore to serialize communication with
540  * the client. For example, after receiving a raw-read request, the
541  * server is not allowed to send an oplock break to the client until
542  * after it has sent the raw-read data.
543  */
544 #define	SMB_SESSION_INACTIVITY_TIMEOUT		(15 * 60)
545 
546 #define	SMB_SESSION_OFILE_MAX			(16 * 1024)
547 
548 /*
549  * When a connection is set up we need to remember both the client
550  * (peer) IP address and the local IP address used to establish the
551  * connection. When a client connects with a vc number of zero, we
552  * are supposed to abort any existing connections with that client
553  * (see notes in smb_negotiate.c and smb_session_setup_andx.c). For
554  * servers with multiple network interfaces or IP aliases, however,
555  * each interface has to be managed independently since the client
556  * is not aware of the server configuration. We have to allow the
557  * client to establish a connection on each interface with a vc
558  * number of zero without aborting the other connections.
559  *
560  * ipaddr:       the client (peer) IP address for the session.
561  * local_ipaddr: the local IP address used to connect to the server.
562  */
563 
564 #define	SMB_MAC_KEYSZ	512
565 
566 struct smb_sign {
567 	unsigned int seqnum;
568 	unsigned int mackey_len;
569 	unsigned int flags;
570 	unsigned char mackey[SMB_MAC_KEYSZ];
571 };
572 
573 #define	SMB_SIGNING_ENABLED	1
574 #define	SMB_SIGNING_CHECK	2
575 
576 /*
577  * Session State Machine
578  * ---------------------
579  *
580  * +-----------------------------+	     +------------------------------+
581  * | SMB_SESSION_STATE_CONNECTED |           | SMB_SESSION_STATE_TERMINATED |
582  * +-----------------------------+           +------------------------------+
583  *		T0|					     ^
584  *		  +--------------------+		     |T13
585  *		  v		       |T14                  |
586  * +-------------------------------+   |    +--------------------------------+
587  * | SMB_SESSION_STATE_ESTABLISHED |---+--->| SMB_SESSION_STATE_DISCONNECTED |
588  * +-------------------------------+        +--------------------------------+
589  *		T1|				^	   ^ ^ ^
590  *		  +----------+			|T9        | | |
591  *                           v			|          | | |
592  *                  +------------------------------+       | | |
593  *                  | SMB_SESSION_STATE_NEGOTIATED |       | | |
594  *                  +------------------------------+       | | |
595  *	                 ^|   ^|   | ^                     | | |
596  *      +----------------+|   ||   | |                     | | |
597  *      |+----------------+   || T7| |T8                   | | |
598  *      ||                    ||   | |                     | | |
599  *      ||   +----------------+|   | |                     | | |
600  *      ||   |+----------------+   | |                     | | |
601  *	||   ||			   v |                     | | |
602  *      ||   ||   +-----------------------------------+ T10| | |
603  *      ||   ||   | SMB_SESSION_STATE_OPLOCK_BREAKING |----+ | |
604  *      ||   ||   +-----------------------------------+      | |
605  *	||   ||T5                                            | |
606  *      ||   |+-->+-----------------------------------+	  T11| |
607  *      ||   |T6  | SMB_SESSION_STATE_READ_RAW_ACTIVE |------+ |
608  *      ||   +----+-----------------------------------+        |
609  *	||T3                                                   |
610  *      |+------->+------------------------------------+    T12|
611  *      |T4       | SMB_SESSION_STATE_WRITE_RAW_ACTIVE |-------+
612  *      +---------+------------------------------------+
613  *
614  * Transition T0
615  *
616  *
617  *
618  * Transition T1
619  *
620  *
621  *
622  * Transition T2
623  *
624  *
625  *
626  * Transition T3
627  *
628  *
629  *
630  * Transition T4
631  *
632  *
633  *
634  * Transition T5
635  *
636  *
637  *
638  * Transition T6
639  *
640  *
641  *
642  * Transition T7
643  *
644  *
645  *
646  * Transition T8
647  *
648  *
649  *
650  * Transition T9
651  *
652  *
653  *
654  * Transition T10
655  *
656  *
657  *
658  * Transition T11
659  *
660  *
661  *
662  * Transition T12
663  *
664  *
665  *
666  * Transition T13
667  *
668  *
669  *
670  * Transition T14
671  *
672  *
673  *
674  */
675 #define	SMB_SESSION_MAGIC	0x53455353	/* 'SESS' */
676 #define	SMB_SESSION_VALID(p)	\
677     ASSERT(((p) != NULL) && ((p)->s_magic == SMB_SESSION_MAGIC))
678 
679 typedef enum {
680 	SMB_SESSION_STATE_INITIALIZED = 0,
681 	SMB_SESSION_STATE_DISCONNECTED,
682 	SMB_SESSION_STATE_CONNECTED,
683 	SMB_SESSION_STATE_ESTABLISHED,
684 	SMB_SESSION_STATE_NEGOTIATED,
685 	SMB_SESSION_STATE_OPLOCK_BREAKING,
686 	SMB_SESSION_STATE_WRITE_RAW_ACTIVE,
687 	SMB_SESSION_STATE_READ_RAW_ACTIVE,
688 	SMB_SESSION_STATE_TERMINATED,
689 	SMB_SESSION_STATE_SENTINEL
690 } smb_session_state_t;
691 
692 typedef struct smb_session {
693 	uint32_t		s_magic;
694 	smb_rwx_t		s_lock;
695 	list_node_t		s_lnd;
696 	uint64_t		s_kid;
697 	smb_session_state_t	s_state;
698 	uint32_t		s_flags;
699 	int			s_write_raw_status;
700 	kthread_t		*s_thread;
701 	kt_did_t		s_ktdid;
702 	smb_kmod_cfg_t		s_cfg;
703 	kmem_cache_t		*s_cache;
704 	kmem_cache_t		*s_cache_request;
705 	struct smb_server	*s_server;
706 	int32_t			s_gmtoff;
707 	uint32_t		keep_alive;
708 	uint64_t		opentime;
709 	uint16_t		vcnumber;
710 	uint16_t		s_local_port;
711 	smb_inaddr_t		ipaddr;
712 	smb_inaddr_t		local_ipaddr;
713 	char 			workstation[SMB_PI_MAX_HOST];
714 	int			dialect;
715 	int			native_os;
716 	uint32_t		capabilities;
717 	struct smb_sign		signing;
718 
719 	ksocket_t		sock;
720 
721 	smb_slist_t		s_req_list;
722 	smb_llist_t		s_xa_list;
723 	smb_llist_t		s_user_list;
724 	smb_idpool_t		s_uid_pool;
725 	smb_txlst_t		s_txlst;
726 
727 	volatile uint32_t	s_tree_cnt;
728 	volatile uint32_t	s_file_cnt;
729 	volatile uint32_t	s_dir_cnt;
730 
731 	uint16_t		secmode;
732 	uint32_t		sesskey;
733 	uint32_t		challenge_len;
734 	unsigned char		challenge_key[8];
735 	unsigned char		MAC_key[44];
736 	int64_t			activity_timestamp;
737 	/*
738 	 * Maximum negotiated buffer size between SMB client and server
739 	 * in SMB_SESSION_SETUP_ANDX
740 	 */
741 	uint16_t		smb_msg_size;
742 	uchar_t			*outpipe_data;
743 	int			outpipe_datalen;
744 	int			outpipe_cookie;
745 	list_t			s_oplock_brkreqs;
746 } smb_session_t;
747 
748 #define	SMB_USER_MAGIC 0x55534552	/* 'USER' */
749 #define	SMB_USER_VALID(u)	\
750     ASSERT(((u) != NULL) && ((u)->u_magic == SMB_USER_MAGIC))
751 
752 #define	SMB_USER_FLAG_GUEST			SMB_ATF_GUEST
753 #define	SMB_USER_FLAG_IPC			SMB_ATF_ANON
754 #define	SMB_USER_FLAG_ADMIN			SMB_ATF_ADMIN
755 #define	SMB_USER_FLAG_POWER_USER		SMB_ATF_POWERUSER
756 #define	SMB_USER_FLAG_BACKUP_OPERATOR		SMB_ATF_BACKUPOP
757 
758 #define	SMB_USER_PRIV_TAKE_OWNERSHIP	0x00000001
759 #define	SMB_USER_PRIV_BACKUP		0x00000002
760 #define	SMB_USER_PRIV_RESTORE		0x00000004
761 #define	SMB_USER_PRIV_SECURITY		0x00000008
762 
763 
764 typedef enum {
765 	SMB_USER_STATE_LOGGED_IN = 0,
766 	SMB_USER_STATE_LOGGING_OFF,
767 	SMB_USER_STATE_LOGGED_OFF,
768 	SMB_USER_STATE_SENTINEL
769 } smb_user_state_t;
770 
771 typedef struct smb_user {
772 	uint32_t		u_magic;
773 	list_node_t		u_lnd;
774 	kmutex_t		u_mutex;
775 	smb_user_state_t	u_state;
776 
777 	struct smb_server	*u_server;
778 	smb_session_t		*u_session;
779 	uint16_t		u_name_len;
780 	char			*u_name;
781 	uint16_t		u_domain_len;
782 	char			*u_domain;
783 	time_t			u_logon_time;
784 	cred_t			*u_cred;
785 	cred_t			*u_privcred;
786 
787 	smb_llist_t		u_tree_list;
788 	smb_idpool_t		u_tid_pool;
789 
790 	uint32_t		u_refcnt;
791 	uint32_t		u_flags;
792 	uint32_t		u_privileges;
793 	uint16_t		u_uid;
794 	uint32_t		u_audit_sid;
795 } smb_user_t;
796 
797 #define	SMB_TREE_MAGIC			0x54524545	/* 'TREE' */
798 #define	SMB_TREE_VALID(p)	\
799     ASSERT((p != NULL) && ((p)->t_magic == SMB_TREE_MAGIC))
800 
801 #define	SMB_TYPENAMELEN			_ST_FSTYPSZ
802 #define	SMB_VOLNAMELEN			32
803 
804 #define	SMB_TREE_READONLY		0x00000001
805 #define	SMB_TREE_SUPPORTS_ACLS		0x00000002
806 #define	SMB_TREE_STREAMS		0x00000004
807 #define	SMB_TREE_CASEINSENSITIVE	0x00000008
808 #define	SMB_TREE_NO_CASESENSITIVE	0x00000010
809 #define	SMB_TREE_NO_EXPORT		0x00000020
810 #define	SMB_TREE_NO_OPLOCKS		0x00000040
811 #define	SMB_TREE_NO_ATIME		0x00000080
812 #define	SMB_TREE_XVATTR			0x00000100
813 #define	SMB_TREE_DIRENTFLAGS		0x00000200
814 #define	SMB_TREE_ACLONCREATE		0x00000400
815 #define	SMB_TREE_ACEMASKONACCESS	0x00000800
816 #define	SMB_TREE_NFS_MOUNTED		0x00001000
817 #define	SMB_TREE_UNICODE_ON_DISK	0x00002000
818 #define	SMB_TREE_CATIA			0x00004000
819 #define	SMB_TREE_ABE			0x00008000
820 #define	SMB_TREE_QUOTA			0x00010000
821 #define	SMB_TREE_DFSROOT		0x00020000
822 
823 typedef enum {
824 	SMB_TREE_STATE_CONNECTED = 0,
825 	SMB_TREE_STATE_DISCONNECTING,
826 	SMB_TREE_STATE_DISCONNECTED,
827 	SMB_TREE_STATE_SENTINEL
828 } smb_tree_state_t;
829 
830 typedef struct smb_tree {
831 	uint32_t		t_magic;
832 	kmutex_t		t_mutex;
833 	list_node_t		t_lnd;
834 	smb_tree_state_t	t_state;
835 
836 	struct smb_server	*t_server;
837 	smb_session_t		*t_session;
838 	smb_user_t		*t_user;
839 	smb_node_t		*t_snode;
840 
841 	smb_llist_t		t_ofile_list;
842 	smb_idpool_t		t_fid_pool;
843 
844 	smb_llist_t		t_odir_list;
845 	smb_idpool_t		t_odid_pool;
846 
847 	uint32_t		t_refcnt;
848 	uint32_t		t_flags;
849 	int32_t			t_res_type;
850 	uint16_t		t_tid;
851 	uint16_t		t_umask;
852 	char			t_sharename[MAXNAMELEN];
853 	char			t_resource[MAXPATHLEN];
854 	char			t_typename[SMB_TYPENAMELEN];
855 	char			t_volume[SMB_VOLNAMELEN];
856 	acl_type_t		t_acltype;
857 	uint32_t		t_access;
858 	uint32_t		t_shr_flags;
859 	time_t			t_connect_time;
860 	volatile uint32_t	t_open_files;
861 } smb_tree_t;
862 
863 #define	SMB_TREE_VFS(tree)	((tree)->t_snode->vp->v_vfsp)
864 #define	SMB_TREE_FSID(tree)	((tree)->t_snode->vp->v_vfsp->vfs_fsid)
865 
866 #define	SMB_TREE_IS_READONLY(sr)					\
867 	((sr) != NULL && (sr)->tid_tree != NULL &&			\
868 	!((sr)->tid_tree->t_access & ACE_ALL_WRITE_PERMS))
869 
870 #define	SMB_TREE_IS_CASEINSENSITIVE(sr)                                 \
871 	(((sr) && (sr)->tid_tree) ?                                     \
872 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CASEINSENSITIVE) : 0)
873 
874 #define	SMB_TREE_HAS_ACCESS(sr, acemask)				\
875 	((sr) == NULL ? ACE_ALL_PERMS : (				\
876 	(((sr) && (sr)->tid_tree) ?					\
877 	(((sr)->tid_tree->t_access) & (acemask)) : 0)))
878 
879 #define	SMB_TREE_SUPPORTS_CATIA(sr)            				\
880 	(((sr) && (sr)->tid_tree) ?                                     \
881 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CATIA) : 0)
882 
883 #define	SMB_TREE_SUPPORTS_ABE(sr)            				\
884 	(((sr) && (sr)->tid_tree) ?                                     \
885 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_ABE) : 0)
886 
887 #define	SMB_TREE_IS_DFSROOT(sr)            				\
888 	(((sr) && (sr)->tid_tree) ?                                     \
889 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_DFSROOT) : 0)
890 
891 /*
892  * SMB_TREE_CONTAINS_NODE is used to check that a node is in the same
893  * file system as the tree.
894  */
895 #define	SMB_TREE_CONTAINS_NODE(sr, node)                                \
896 	(((sr) && (sr)->tid_tree) ?                                     \
897 	(SMB_TREE_VFS((sr)->tid_tree) == SMB_NODE_VFS(node)) : 1)
898 
899 /*
900  * SMB_OFILE_IS_READONLY reflects whether an ofile is readonly or not.
901  * The macro takes into account
902  *      - the tree readonly state
903  *      - the node readonly state
904  *      - whether the specified ofile is the readonly creator
905  * The readonly creator has write permission until the ofile is closed.
906  */
907 
908 #define	SMB_OFILE_IS_READONLY(of)                               \
909 	(((of)->f_flags & SMB_OFLAGS_READONLY) ||               \
910 	smb_node_file_is_readonly((of)->f_node) ||                   \
911 	(((of)->f_node->readonly_creator) &&                    \
912 	((of)->f_node->readonly_creator != (of))))
913 
914 /*
915  * SMB_PATHFILE_IS_READONLY indicates whether or not a file is
916  * readonly when the caller has a path rather than an ofile.  Unlike
917  * SMB_OFILE_IS_READONLY, the caller cannot be the readonly creator,
918  * since that requires an ofile.
919  */
920 
921 #define	SMB_PATHFILE_IS_READONLY(sr, node)                       \
922 	(SMB_TREE_IS_READONLY((sr)) ||                           \
923 	smb_node_file_is_readonly((node)) ||                          \
924 	((node)->readonly_creator))
925 
926 #define	SMB_OPIPE_MAGIC		0x50495045	/* 'PIPE' */
927 #define	SMB_OPIPE_VALID(p)	\
928     ASSERT(((p) != NULL) && (p)->p_magic == SMB_OPIPE_MAGIC)
929 
930 /*
931  * Data structure for SMB_FTYPE_MESG_PIPE ofiles, which is used
932  * at the interface between SMB and NDR RPC.
933  */
934 typedef struct smb_opipe {
935 	uint32_t		p_magic;
936 	list_node_t		p_lnd;
937 	kmutex_t		p_mutex;
938 	kcondvar_t		p_cv;
939 	struct smb_server	*p_server;
940 	struct smb_event	*p_event;
941 	char			*p_name;
942 	uint32_t		p_busy;
943 	smb_doorhdr_t		p_hdr;
944 	smb_netuserinfo_t	p_user;
945 	uint8_t			*p_doorbuf;
946 	uint8_t			*p_data;
947 } smb_opipe_t;
948 
949 /*
950  * The of_ftype	of an open file should contain the SMB_FTYPE value
951  * returned when the file/pipe was opened. The following
952  * assumptions are currently made:
953  *
954  * File Type	    Node       PipeInfo
955  * ---------	    --------   --------
956  * SMB_FTYPE_DISK       Valid      Null
957  * SMB_FTYPE_BYTE_PIPE  Undefined  Undefined
958  * SMB_FTYPE_MESG_PIPE  Null       Valid
959  * SMB_FTYPE_PRINTER    Undefined  Undefined
960  * SMB_FTYPE_UNKNOWN    Undefined  Undefined
961  */
962 
963 /*
964  * Some flags for ofile structure
965  *
966  *	SMB_OFLAGS_SET_DELETE_ON_CLOSE
967  *   Set this flag when the corresponding open operation whose
968  *   DELETE_ON_CLOSE bit of the CreateOptions is set. If any
969  *   open file instance has this bit set, the NODE_FLAGS_DELETE_ON_CLOSE
970  *   will be set for the file node upon close.
971  *
972  *	SMB_OFLAGS_TIMESTAMPS_PENDING
973  *   This flag gets set when a write operation is performed on the
974  *   ofile. The timestamps will be updated, and the flags cleared,
975  *   when the ofile gets closed or a setattr is performed on the ofile.
976  */
977 
978 #define	SMB_OFLAGS_READONLY		0x0001
979 #define	SMB_OFLAGS_EXECONLY		0x0002
980 #define	SMB_OFLAGS_SET_DELETE_ON_CLOSE	0x0004
981 #define	SMB_OFLAGS_LLF_POS_VALID	0x0008
982 #define	SMB_OFLAGS_TIMESTAMPS_PENDING	0x0010
983 
984 #define	SMB_OFILE_MAGIC 	0x4F464C45	/* 'OFLE' */
985 #define	SMB_OFILE_VALID(p)	\
986     ASSERT((p != NULL) && ((p)->f_magic == SMB_OFILE_MAGIC))
987 
988 typedef enum {
989 	SMB_OFILE_STATE_OPEN = 0,
990 	SMB_OFILE_STATE_CLOSING,
991 	SMB_OFILE_STATE_CLOSED,
992 	SMB_OFILE_STATE_SENTINEL
993 } smb_ofile_state_t;
994 
995 typedef struct smb_ofile {
996 	uint32_t		f_magic;
997 	kmutex_t		f_mutex;
998 	list_node_t		f_lnd;
999 	list_node_t		f_nnd;
1000 	smb_ofile_state_t	f_state;
1001 
1002 	struct smb_server	*f_server;
1003 	smb_session_t		*f_session;
1004 	smb_user_t		*f_user;
1005 	smb_tree_t		*f_tree;
1006 	smb_node_t		*f_node;
1007 	smb_opipe_t		*f_pipe;
1008 
1009 	uint32_t		f_uniqid;
1010 	uint32_t		f_refcnt;
1011 	uint64_t		f_seek_pos;
1012 	uint32_t		f_flags;
1013 	uint32_t		f_granted_access;
1014 	uint32_t		f_share_access;
1015 	uint32_t		f_create_options;
1016 	uint16_t		f_fid;
1017 	uint16_t		f_opened_by_pid;
1018 	uint16_t		f_ftype;
1019 	uint64_t		f_llf_pos;
1020 	int			f_mode;
1021 	cred_t			*f_cr;
1022 	pid_t			f_pid;
1023 	boolean_t		f_oplock_granted;
1024 	boolean_t		f_oplock_exit;
1025 	uint32_t		f_explicit_times;
1026 	char			f_quota_resume[SMB_SID_STRSZ];
1027 } smb_ofile_t;
1028 
1029 #define	SMB_ODIR_MAGIC 		0x4F444952	/* 'ODIR' */
1030 #define	SMB_ODIR_VALID(p)	\
1031     ASSERT((p != NULL) && ((p)->d_magic == SMB_ODIR_MAGIC))
1032 
1033 #define	SMB_ODIR_BUFSIZE	(8 * 1024)
1034 
1035 #define	SMB_ODIR_FLAG_WILDCARDS		0x0001
1036 #define	SMB_ODIR_FLAG_IGNORE_CASE	0x0002
1037 #define	SMB_ODIR_FLAG_XATTR		0x0004
1038 #define	SMB_ODIR_FLAG_EDIRENT		0x0008
1039 #define	SMB_ODIR_FLAG_CATIA		0x0010
1040 #define	SMB_ODIR_FLAG_ABE		0x0020
1041 
1042 typedef enum {
1043 	SMB_ODIR_STATE_OPEN = 0,
1044 	SMB_ODIR_STATE_IN_USE,
1045 	SMB_ODIR_STATE_CLOSING,
1046 	SMB_ODIR_STATE_CLOSED,
1047 	SMB_ODIR_STATE_SENTINEL
1048 } smb_odir_state_t;
1049 
1050 typedef enum {
1051 	SMB_ODIR_RESUME_IDX,
1052 	SMB_ODIR_RESUME_COOKIE,
1053 	SMB_ODIR_RESUME_FNAME
1054 } smb_odir_resume_type_t;
1055 
1056 typedef struct smb_odir_resume {
1057 	smb_odir_resume_type_t	or_type;
1058 	int			or_idx;
1059 	uint32_t		or_cookie;
1060 	char			*or_fname;
1061 } smb_odir_resume_t;
1062 
1063 /*
1064  * Flags used when opening an odir
1065  */
1066 #define	SMB_ODIR_OPENF_BACKUP_INTENT	0x01
1067 
1068 typedef struct smb_odir {
1069 	uint32_t		d_magic;
1070 	kmutex_t		d_mutex;
1071 	list_node_t		d_lnd;
1072 	smb_odir_state_t	d_state;
1073 	smb_session_t		*d_session;
1074 	smb_tree_t		*d_tree;
1075 	smb_node_t		*d_dnode;
1076 	cred_t			*d_cred;
1077 	uint16_t		d_odid;
1078 	uint16_t		d_opened_by_pid;
1079 	uint16_t		d_sattr;
1080 	uint32_t		d_refcnt;
1081 	uint32_t		d_flags;
1082 	boolean_t		d_eof;
1083 	int			d_bufsize;
1084 	uint64_t		d_offset;
1085 	union {
1086 		char		*u_bufptr;
1087 		edirent_t	*u_edp;
1088 		dirent64_t	*u_dp;
1089 	} d_u;
1090 	uint32_t		d_cookies[SMB_MAX_SEARCH];
1091 	char			d_pattern[MAXNAMELEN];
1092 	char			d_buf[SMB_ODIR_BUFSIZE];
1093 } smb_odir_t;
1094 #define	d_bufptr	d_u.u_bufptr
1095 #define	d_edp		d_u.u_edp
1096 #define	d_dp		d_u.u_dp
1097 
1098 typedef struct smb_odirent {
1099 	char		od_name[MAXNAMELEN];	/* on disk name */
1100 	ino64_t		od_ino;
1101 	uint32_t	od_eflags;
1102 } smb_odirent_t;
1103 
1104 typedef struct smb_fileinfo {
1105 	char		fi_name[MAXNAMELEN];
1106 	char		fi_name83[SMB_SHORTNAMELEN];
1107 	char		fi_shortname[SMB_SHORTNAMELEN];
1108 	uint32_t	fi_cookie;
1109 	uint32_t	fi_dosattr;	/* DOS attributes */
1110 	uint64_t	fi_nodeid;	/* file system node id */
1111 	uint64_t	fi_size;	/* file size in bytes */
1112 	uint64_t	fi_alloc_size;	/* allocation size in bytes */
1113 	timestruc_t	fi_atime;	/* last access */
1114 	timestruc_t	fi_mtime;	/* last modification */
1115 	timestruc_t	fi_ctime;	/* last status change */
1116 	timestruc_t	fi_crtime;	/* file creation */
1117 } smb_fileinfo_t;
1118 
1119 typedef struct smb_streaminfo {
1120 	uint64_t	si_size;
1121 	uint64_t	si_alloc_size;
1122 	char		si_name[MAXPATHLEN];
1123 } smb_streaminfo_t;
1124 
1125 #define	SMB_LOCK_MAGIC 	0x4C4F434B	/* 'LOCK' */
1126 
1127 typedef struct smb_lock {
1128 	uint32_t		l_magic;
1129 	kmutex_t		l_mutex;
1130 	list_node_t		l_lnd;
1131 	kcondvar_t		l_cv;
1132 
1133 	list_node_t		l_conflict_lnd;
1134 	smb_slist_t		l_conflict_list;
1135 
1136 	smb_session_t		*l_session;
1137 	smb_ofile_t		*l_file;
1138 	struct smb_request	*l_sr;
1139 
1140 	uint32_t		l_flags;
1141 	uint64_t		l_session_kid;
1142 	struct smb_lock		*l_blocked_by; /* Debug info only */
1143 
1144 	uint16_t		l_pid;
1145 	uint16_t		l_uid;
1146 	uint32_t		l_type;
1147 	uint64_t		l_start;
1148 	uint64_t		l_length;
1149 	clock_t			l_end_time;
1150 } smb_lock_t;
1151 
1152 #define	SMB_LOCK_FLAG_INDEFINITE	0x0004
1153 #define	SMB_LOCK_INDEFINITE_WAIT(lock) \
1154 	((lock)->l_flags & SMB_LOCK_FLAG_INDEFINITE)
1155 
1156 #define	SMB_LOCK_TYPE_READWRITE		101
1157 #define	SMB_LOCK_TYPE_READONLY		102
1158 
1159 typedef struct vardata_block {
1160 	uint8_t			vdb_tag;
1161 	uint32_t		vdb_len;
1162 	struct uio 		vdb_uio;
1163 	struct iovec		vdb_iovec[MAX_IOVEC];
1164 } smb_vdb_t;
1165 
1166 #define	SMB_WRMODE_WRITE_THRU	0x0001
1167 #define	SMB_WRMODE_IS_STABLE(M)	((M) & SMB_WRMODE_WRITE_THRU)
1168 
1169 #define	SMB_RW_MAGIC		0x52445257	/* 'RDRW' */
1170 
1171 typedef struct smb_rw_param {
1172 	uint32_t rw_magic;
1173 	smb_vdb_t rw_vdb;
1174 	uint64_t rw_offset;
1175 	uint32_t rw_last_write;
1176 	uint16_t rw_mode;
1177 	uint32_t rw_count;		/* bytes in this request */
1178 	uint16_t rw_mincnt;
1179 	uint32_t rw_total;		/* total bytes (write-raw) */
1180 	uint16_t rw_dsoff;		/* SMB data offset */
1181 	uint8_t rw_andx;		/* SMB secondary andx command */
1182 } smb_rw_param_t;
1183 
1184 typedef struct smb_pathname {
1185 	char	*pn_path;
1186 	char	*pn_pname;
1187 	char	*pn_fname;
1188 	char	*pn_sname;
1189 	char	*pn_stype;
1190 } smb_pathname_t;
1191 
1192 /*
1193  * fs_query_info
1194  */
1195 typedef struct smb_fqi {
1196 	smb_pathname_t	fq_path;
1197 	uint16_t	fq_sattr;
1198 	smb_node_t	*fq_dnode;
1199 	smb_node_t	*fq_fnode;
1200 	smb_attr_t	fq_fattr;
1201 	char		fq_last_comp[MAXNAMELEN];
1202 } smb_fqi_t;
1203 
1204 #define	OPLOCK_MIN_TIMEOUT	(5 * 1000)
1205 #define	OPLOCK_STD_TIMEOUT	(15 * 1000)
1206 #define	OPLOCK_RETRIES		2
1207 
1208 typedef struct {
1209 	uint32_t severity;
1210 	uint32_t status;
1211 	uint16_t errcls;
1212 	uint16_t errcode;
1213 } smb_error_t;
1214 
1215 typedef struct open_param {
1216 	smb_fqi_t	fqi;
1217 	uint16_t	omode;
1218 	uint16_t	ofun;
1219 	uint32_t	nt_flags;
1220 	uint32_t	timeo;
1221 	uint32_t	dattr;
1222 	timestruc_t	crtime;
1223 	timestruc_t	mtime;
1224 	uint64_t	dsize;
1225 	uint32_t	desired_access;
1226 	uint32_t	share_access;
1227 	uint32_t	create_options;
1228 	uint32_t	create_disposition;
1229 	boolean_t	created_readonly;
1230 	uint32_t	ftype;
1231 	uint32_t	devstate;
1232 	uint32_t	action_taken;
1233 	uint64_t	fileid;
1234 	uint32_t	rootdirfid;
1235 	smb_ofile_t	*dir;
1236 	/* This is only set by NTTransactCreate */
1237 	struct smb_sd	*sd;
1238 	uint8_t		op_oplock_level;
1239 } open_param_t;
1240 
1241 #define	SMB_OPLOCK_NONE		0
1242 #define	SMB_OPLOCK_EXCLUSIVE	1
1243 #define	SMB_OPLOCK_BATCH	2
1244 #define	SMB_OPLOCK_LEVEL_II	3
1245 
1246 /*
1247  * SMB Request State Machine
1248  * -------------------------
1249  *
1250  *                  T4               +------+		T0
1251  *      +--------------------------->| FREE |---------------------------+
1252  *      |                            +------+                           |
1253  * +-----------+                                                        |
1254  * | COMPLETED |                                                        |
1255  * +-----------+
1256  *      ^                                                               |
1257  *      | T15                      +----------+                         v
1258  * +------------+        T6        |          |                 +--------------+
1259  * | CLEANED_UP |<-----------------| CANCELED |                 | INITIALIZING |
1260  * +------------+                  |          |                 +--------------+
1261  *      |    ^                     +----------+                         |
1262  *      |    |                        ^  ^ ^ ^                          |
1263  *      |    |          +-------------+  | | |                          |
1264  *      |    |    T3    |                | | |               T13        | T1
1265  *      |    +-------------------------+ | | +----------------------+   |
1266  *      +----------------------------+ | | |                        |   |
1267  *         T16          |            | | | +-----------+            |   |
1268  *                      |           \/ | | T5          |            |   v
1269  * +-----------------+  |   T12     +--------+         |     T2    +-----------+
1270  * | EVENT_OCCURRED  |------------->| ACTIVE |<--------------------| SUBMITTED |
1271  * +-----------------+  |           +--------+         |           +-----------+
1272  *        ^             |              | ^ |           |
1273  *        |             |           T8 | | |  T7       |
1274  *        | T10      T9 |   +----------+ | +-------+   |  T11
1275  *        |             |   |            +-------+ |   |
1276  *        |             |   |               T14  | |   |
1277  *        |             |   v                    | v   |
1278  *      +----------------------+                +--------------+
1279  *	|     WAITING_EVENT    |                | WAITING_LOCK |
1280  *      +----------------------+                +--------------+
1281  *
1282  *
1283  *
1284  *
1285  *
1286  * Transition T0
1287  *
1288  * This transition occurs when the request is allocated and is still under the
1289  * control of the session thread.
1290  *
1291  * Transition T1
1292  *
1293  * This transition occurs when the session thread dispatches a task to treat the
1294  * request.
1295  *
1296  * Transition T2
1297  *
1298  *
1299  *
1300  * Transition T3
1301  *
1302  * A request completes and smbsr_cleanup is called to release resources
1303  * associated with the request (but not the smb_request_t itself).  This
1304  * includes references on smb_ofile_t, smb_node_t, and other structures.
1305  * CLEANED_UP state exists to detect if we attempt to cleanup a request
1306  * multiple times and to allow us to detect that we are accessing a
1307  * request that has already been cleaned up.
1308  *
1309  * Transition T4
1310  *
1311  *
1312  *
1313  * Transition T5
1314  *
1315  *
1316  *
1317  * Transition T6
1318  *
1319  *
1320  *
1321  * Transition T7
1322  *
1323  *
1324  *
1325  * Transition T8
1326  *
1327  *
1328  *
1329  * Transition T9
1330  *
1331  *
1332  *
1333  * Transition T10
1334  *
1335  *
1336  *
1337  * Transition T11
1338  *
1339  *
1340  *
1341  * Transition T12
1342  *
1343  *
1344  *
1345  * Transition T13
1346  *
1347  *
1348  *
1349  * Transition T14
1350  *
1351  *
1352  *
1353  * Transition T15
1354  *
1355  * Request processing is completed (control returns from smb_dispatch)
1356  *
1357  * Transition T16
1358  *
1359  * Multipart (andx) request was cleaned up with smbsr_cleanup but more "andx"
1360  * sections remain to be processed.
1361  *
1362  */
1363 
1364 #define	SMB_REQ_MAGIC 		0x534D4252	/* 'SMBR' */
1365 #define	SMB_REQ_VALID(p)	ASSERT((p)->sr_magic == SMB_REQ_MAGIC)
1366 
1367 typedef enum smb_req_state {
1368 	SMB_REQ_STATE_FREE = 0,
1369 	SMB_REQ_STATE_INITIALIZING,
1370 	SMB_REQ_STATE_SUBMITTED,
1371 	SMB_REQ_STATE_ACTIVE,
1372 	SMB_REQ_STATE_WAITING_EVENT,
1373 	SMB_REQ_STATE_EVENT_OCCURRED,
1374 	SMB_REQ_STATE_WAITING_LOCK,
1375 	SMB_REQ_STATE_COMPLETED,
1376 	SMB_REQ_STATE_CANCELED,
1377 	SMB_REQ_STATE_CLEANED_UP,
1378 	SMB_REQ_STATE_SENTINEL
1379 } smb_req_state_t;
1380 
1381 typedef struct smb_request {
1382 	uint32_t		sr_magic;
1383 	kmutex_t		sr_mutex;
1384 	list_node_t		sr_session_lnd;
1385 	smb_req_state_t		sr_state;
1386 	boolean_t		sr_keep;
1387 	kmem_cache_t		*sr_cache;
1388 	struct smb_server	*sr_server;
1389 	pid_t			*sr_pid;
1390 	int32_t			sr_gmtoff;
1391 	smb_session_t		*session;
1392 	smb_kmod_cfg_t		*sr_cfg;
1393 	smb_notify_change_req_t	sr_ncr;
1394 
1395 	/* Info from session service header */
1396 	uint32_t		sr_req_length; /* Excluding NBT header */
1397 
1398 	/* Request buffer excluding NBT header */
1399 	void			*sr_request_buf;
1400 
1401 	smb_lock_t		*sr_awaiting;
1402 	struct mbuf_chain	command;
1403 	struct mbuf_chain	reply;
1404 	struct mbuf_chain	raw_data;
1405 	list_t			sr_storage;
1406 	struct smb_xa		*r_xa;
1407 	int			andx_prev_wct;
1408 	int 			cur_reply_offset;
1409 	int			orig_request_hdr;
1410 	unsigned int		reply_seqnum;	/* reply sequence number */
1411 	unsigned char		first_smb_com;	/* command code */
1412 	unsigned char		smb_com;	/* command code */
1413 
1414 	uint8_t			smb_rcls;	/* error code class */
1415 	uint8_t			smb_reh;	/* rsvd (AH DOS INT-24 ERR) */
1416 	uint16_t		smb_err;	/* error code */
1417 	smb_error_t		smb_error;
1418 
1419 	uint8_t			smb_flg;	/* flags */
1420 	uint16_t		smb_flg2;	/* flags */
1421 	uint16_t		smb_pid_high;	/* high part of pid */
1422 	unsigned char		smb_sig[8];	/* signiture */
1423 	uint16_t		smb_tid;	/* tree id #  */
1424 	uint16_t		smb_pid;	/* caller's process id # */
1425 	uint16_t		smb_uid;	/* user id # */
1426 	uint16_t		smb_mid;	/* mutiplex id #  */
1427 	unsigned char		smb_wct;	/* count of parameter words */
1428 	uint16_t		smb_bcc;	/* data byte count */
1429 
1430 	/* Parameters */
1431 	struct mbuf_chain	smb_vwv;	/* variable width value */
1432 
1433 	/* Data */
1434 	struct mbuf_chain	smb_data;
1435 
1436 	uint16_t		smb_fid;	/* not in hdr, but common */
1437 
1438 	unsigned char		andx_com;
1439 	uint16_t		andx_off;
1440 
1441 	struct smb_tree		*tid_tree;
1442 	struct smb_ofile	*fid_ofile;
1443 	smb_user_t		*uid_user;
1444 
1445 	union {
1446 	    struct tcon {
1447 		char		*path;
1448 		char		*service;
1449 		int		pwdlen;
1450 		char		*password;
1451 		uint16_t	flags;
1452 		uint16_t	optional_support;
1453 	    } tcon;
1454 
1455 	    struct dirop {
1456 		smb_fqi_t	fqi;
1457 		smb_fqi_t	dst_fqi;
1458 		uint16_t	info_level;
1459 		uint16_t	flags;
1460 	    } dirop;
1461 
1462 	    open_param_t	open;
1463 	    smb_rw_param_t	*rw;
1464 	    uint32_t		timestamp;
1465 	} arg;
1466 
1467 	cred_t			*user_cr;
1468 	kthread_t		*sr_worker;
1469 } smb_request_t;
1470 
1471 #define	SMB_READ_PROTOCOL(hdr) \
1472 	LE_IN32(((smb_hdr_t *)(hdr))->protocol)
1473 
1474 #define	SMB_PROTOCOL_MAGIC_INVALID(rd_sr) \
1475 	(SMB_READ_PROTOCOL((rd_sr)->sr_request_buf) != SMB_PROTOCOL_MAGIC)
1476 
1477 #define	SMB_READ_COMMAND(hdr) \
1478 	(((smb_hdr_t *)(hdr))->command)
1479 
1480 #define	SMB_IS_WRITERAW(rd_sr) \
1481 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_WRITE_RAW)
1482 
1483 
1484 #define	SMB_XA_FLAG_OPEN	0x0001
1485 #define	SMB_XA_FLAG_CLOSE	0x0002
1486 #define	SMB_XA_FLAG_COMPLETE	0x0004
1487 #define	SMB_XA_CLOSED(xa) (!((xa)->xa_flags & SMB_XA_FLAG_OPEN))
1488 
1489 #define	SMB_XA_MAGIC		0x534D4258	/* 'SMBX' */
1490 
1491 typedef struct smb_xa {
1492 	uint32_t		xa_magic;
1493 	kmutex_t		xa_mutex;
1494 	list_node_t		xa_lnd;
1495 
1496 	uint32_t		xa_refcnt;
1497 	uint32_t		xa_flags;
1498 
1499 	struct smb_session	*xa_session;
1500 
1501 	unsigned char		smb_com;	/* which TRANS type */
1502 	unsigned char		smb_flg;	/* flags */
1503 	uint16_t		smb_flg2;	/* flags */
1504 	uint16_t		smb_tid;	/* tree id number */
1505 	uint16_t		smb_pid;	/* caller's process id number */
1506 	uint16_t		smb_uid;	/* user id number */
1507 	uint32_t		smb_func;	/* NT_TRANS function */
1508 
1509 	uint16_t		xa_smb_mid;	/* mutiplex id number */
1510 	uint16_t		xa_smb_fid;	/* TRANS2 secondary */
1511 
1512 	unsigned int		reply_seqnum;	/* reply sequence number */
1513 
1514 	uint32_t	smb_tpscnt;	/* total parameter bytes being sent */
1515 	uint32_t	smb_tdscnt;	/* total data bytes being sent */
1516 	uint32_t	smb_mprcnt;	/* max parameter bytes to return */
1517 	uint32_t	smb_mdrcnt;	/* max data bytes to return */
1518 	uint32_t	smb_msrcnt;	/* max setup words to return */
1519 	uint32_t	smb_flags;	/* additional information: */
1520 				/*  bit 0 - if set, disconnect TID in smb_tid */
1521 				/*  bit 1 - if set, transaction is one way */
1522 				/*  (no final response) */
1523 	int32_t	smb_timeout;	/* number of milliseconds to await completion */
1524 	uint32_t	smb_suwcnt;	/* set up word count */
1525 
1526 	char			*xa_pipe_name;
1527 
1528 	int			req_disp_param;
1529 	int			req_disp_data;
1530 
1531 	struct mbuf_chain	req_setup_mb;
1532 	struct mbuf_chain	req_param_mb;
1533 	struct mbuf_chain	req_data_mb;
1534 
1535 	struct mbuf_chain	rep_setup_mb;
1536 	struct mbuf_chain	rep_param_mb;
1537 	struct mbuf_chain	rep_data_mb;
1538 } smb_xa_t;
1539 
1540 
1541 #define	SDDF_NO_FLAGS			0
1542 #define	SDDF_SUPPRESS_TID		0x0001
1543 #define	SDDF_SUPPRESS_UID		0x0002
1544 
1545 /*
1546  * SMB dispatch return codes.
1547  */
1548 typedef enum {
1549 	SDRC_SUCCESS = 0,
1550 	SDRC_ERROR,
1551 	SDRC_DROP_VC,
1552 	SDRC_NO_REPLY,
1553 	SDRC_SR_KEPT,
1554 	SDRC_NOT_IMPLEMENTED
1555 } smb_sdrc_t;
1556 
1557 #define	VAR_BCC		((short)-1)
1558 
1559 #define	SMB_SERVER_MAGIC	0x53534552	/* 'SSER' */
1560 #define	SMB_SERVER_VALID(s)	\
1561     ASSERT(((s) != NULL) && ((s)->sv_magic == SMB_SERVER_MAGIC))
1562 
1563 typedef struct {
1564 	kstat_named_t	open_files;
1565 	kstat_named_t	open_trees;
1566 	kstat_named_t	open_users;
1567 } smb_server_stats_t;
1568 
1569 typedef struct {
1570 	kthread_t		*ld_kth;
1571 	kt_did_t		ld_ktdid;
1572 	ksocket_t		ld_so;
1573 	struct sockaddr_in	ld_sin;
1574 	struct sockaddr_in6	ld_sin6;
1575 	smb_session_list_t	ld_session_list;
1576 } smb_listener_daemon_t;
1577 
1578 typedef enum smb_server_state {
1579 	SMB_SERVER_STATE_CREATED = 0,
1580 	SMB_SERVER_STATE_CONFIGURED,
1581 	SMB_SERVER_STATE_RUNNING,
1582 	SMB_SERVER_STATE_STOPPING,
1583 	SMB_SERVER_STATE_DELETING,
1584 	SMB_SERVER_STATE_SENTINEL
1585 } smb_server_state_t;
1586 
1587 #define	SMB_SERVER_STATE_VALID(S)		\
1588     ASSERT(((S) == SMB_SERVER_STATE_CREATED) ||	\
1589 	((S) == SMB_SERVER_STATE_CONFIGURED) ||	\
1590 	((S) == SMB_SERVER_STATE_RUNNING) ||	\
1591 	((S) == SMB_SERVER_STATE_STOPPING) ||	\
1592 	((S) == SMB_SERVER_STATE_DELETING))
1593 
1594 typedef struct smb_server {
1595 	uint32_t		sv_magic;
1596 	kcondvar_t		sv_cv;
1597 	kmutex_t		sv_mutex;
1598 	list_node_t		sv_lnd;
1599 	smb_server_state_t	sv_state;
1600 	uint32_t		sv_refcnt;
1601 	pid_t			sv_pid;
1602 	zoneid_t		sv_zid;
1603 	smb_listener_daemon_t	sv_nbt_daemon;
1604 	smb_listener_daemon_t	sv_tcp_daemon;
1605 	krwlock_t		sv_cfg_lock;
1606 	smb_kmod_cfg_t		sv_cfg;
1607 	smb_session_t		*sv_session;
1608 
1609 	kstat_t			*sv_ksp;
1610 	kmutex_t		sv_ksp_mutex;
1611 	char			sv_ksp_name[KSTAT_STRLEN];
1612 	smb_server_stats_t	sv_ks_data;
1613 
1614 	door_handle_t		sv_lmshrd;
1615 
1616 	int32_t			si_gmtoff;
1617 
1618 	smb_thread_t		si_thread_timers;
1619 	smb_thread_t		si_thread_unexport;
1620 
1621 	taskq_t			*sv_thread_pool;
1622 
1623 	kmem_cache_t		*si_cache_unexport;
1624 	kmem_cache_t		*si_cache_vfs;
1625 	kmem_cache_t		*si_cache_request;
1626 	kmem_cache_t		*si_cache_session;
1627 	kmem_cache_t		*si_cache_user;
1628 	kmem_cache_t		*si_cache_tree;
1629 	kmem_cache_t		*si_cache_ofile;
1630 	kmem_cache_t		*si_cache_odir;
1631 	kmem_cache_t		*si_cache_opipe;
1632 	kmem_cache_t		*si_cache_event;
1633 
1634 	volatile uint32_t	sv_open_trees;
1635 	volatile uint32_t	sv_open_files;
1636 	volatile uint32_t	sv_open_users;
1637 
1638 	smb_node_t		*si_root_smb_node;
1639 	smb_llist_t		sv_vfs_list;
1640 	smb_llist_t		sv_opipe_list;
1641 	smb_llist_t		sv_event_list;
1642 	smb_slist_t		sv_unexport_list;
1643 } smb_server_t;
1644 
1645 #define	SMB_EVENT_MAGIC		0x45564E54	/* EVNT */
1646 #define	SMB_EVENT_VALID(e)	\
1647     ASSERT(((e) != NULL) && ((e)->se_magic == SMB_EVENT_MAGIC))
1648 
1649 typedef struct smb_event {
1650 	uint32_t		se_magic;
1651 	list_node_t		se_lnd;
1652 	kmutex_t		se_mutex;
1653 	kcondvar_t		se_cv;
1654 	struct smb_server	*se_server;
1655 	uint32_t		se_txid;
1656 	boolean_t		se_notified;
1657 	int			se_waittime;
1658 	int			se_errno;
1659 } smb_event_t;
1660 
1661 #define	SMB_INFO_NETBIOS_SESSION_SVC_RUNNING	0x0001
1662 #define	SMB_INFO_NETBIOS_SESSION_SVC_FAILED	0x0002
1663 #define	SMB_INFO_USER_LEVEL_SECURITY		0x40000000
1664 #define	SMB_INFO_ENCRYPT_PASSWORDS		0x80000000
1665 
1666 #define	SMB_NEW_KID()	atomic_inc_64_nv(&smb_kids)
1667 #define	SMB_UNIQ_FID()	atomic_inc_32_nv(&smb_fids)
1668 
1669 #define	SMB_IS_STREAM(node) ((node)->n_unode)
1670 
1671 typedef struct smb_tsd {
1672 	void (*proc)();
1673 	void *arg;
1674 	char name[100];
1675 } smb_tsd_t;
1676 
1677 typedef struct smb_disp_entry {
1678 	smb_sdrc_t		(*sdt_pre_op)(smb_request_t *);
1679 	smb_sdrc_t		(*sdt_function)(smb_request_t *);
1680 	void			(*sdt_post_op)(smb_request_t *);
1681 	char			sdt_dialect;
1682 	unsigned char		sdt_flags;
1683 	kstat_named_t		sdt_dispatch_stats; /* invocations */
1684 } smb_disp_entry_t;
1685 
1686 #ifdef	__cplusplus
1687 }
1688 #endif
1689 
1690 #endif /* _SMBSRV_SMB_KTYPES_H */
1691