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