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