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