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 	boolean_t	ll_flushing;
390 } smb_llist_t;
391 
392 typedef struct smb_slist {
393 	kmutex_t	sl_mutex;
394 	kcondvar_t	sl_cv;
395 	list_t		sl_list;
396 	uint32_t	sl_count;
397 	boolean_t	sl_waiting;
398 } smb_slist_t;
399 
400 /*
401  * smb_avl_t State Machine
402  * --------------------
403  *
404  *                      +-----------------------------+
405  *                      |     SMB_AVL_STATE_START     |
406  *                      +-----------------------------+
407  *                                    | T0
408  *                                    v
409  *                      +-----------------------------+
410  *                      |     SMB_AVL_STATE_READY     |
411  *                      +-----------------------------+
412  *                                    | T1
413  *                                    v
414  *                      +-----------------------------+
415  *                      |  SMB_AVL_STATE_DESTROYING   |
416  *                      +-----------------------------+
417  *
418  * Transition T0
419  *
420  *    This transition is executed in smb_avl_create().
421  *
422  * Transition T1
423  *
424  *    This transition is executed in smb_avl_destroy().
425  *
426  */
427 typedef enum {
428 	SMB_AVL_STATE_START = 0,
429 	SMB_AVL_STATE_READY,
430 	SMB_AVL_STATE_DESTROYING
431 } smb_avl_state_t;
432 
433 typedef struct smb_avl_nops {
434 	int		(*avln_cmp) (const void *, const void *);
435 	void		(*avln_hold)(const void *);
436 	boolean_t	(*avln_rele)(const void *);
437 	void		(*avln_destroy)(void *);
438 } smb_avl_nops_t;
439 
440 typedef struct smb_avl_cursor {
441 	void		*avlc_next;
442 	uint32_t	avlc_sequence;
443 } smb_avl_cursor_t;
444 
445 typedef struct smb_avl {
446 	krwlock_t	avl_lock;
447 	avl_tree_t	avl_tree;
448 	kmutex_t	avl_mutex;
449 	kcondvar_t	avl_cv;
450 	smb_avl_state_t	avl_state;
451 	uint32_t	avl_refcnt;
452 	uint32_t	avl_sequence;
453 	smb_avl_nops_t	*avl_nops;
454 } smb_avl_t;
455 
456 typedef struct smb_session_list {
457 	krwlock_t	se_lock;
458 	uint64_t	se_wrop;
459 	struct {
460 		list_t		lst;
461 		uint32_t	count;
462 	} se_rdy;
463 	struct {
464 		list_t		lst;
465 		uint32_t	count;
466 	} se_act;
467 } smb_session_list_t;
468 
469 typedef struct {
470 	kcondvar_t	rwx_cv;
471 	kmutex_t	rwx_mutex;
472 	krwlock_t	rwx_lock;
473 	boolean_t	rwx_waiting;
474 } smb_rwx_t;
475 
476 /* NOTIFY CHANGE */
477 
478 typedef struct smb_notify_change_req {
479 	list_node_t		nc_lnd;
480 	struct smb_node		*nc_node;
481 	uint32_t		nc_reply_type;
482 	uint32_t		nc_flags;
483 } smb_notify_change_req_t;
484 
485 /*
486  * SMB operates over a NetBIOS-over-TCP transport (NBT) or directly
487  * over TCP, which is also known as direct hosted NetBIOS-less SMB
488  * or SMB-over-TCP.
489  *
490  * NBT messages have a 4-byte header that defines the message type
491  * (8-bits), a 7-bit flags field and a 17-bit length.
492  *
493  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
494  * |      TYPE     |     FLAGS   |E|            LENGTH             |
495  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
496  *
497  * 8-bit type      Defined in RFC 1002
498  * 7-bit flags     Bits 0-6 reserved (must be 0)
499  *                 Bit 7: Length extension bit (E)
500  * 17-bit length   Includes bit 7 of the flags byte
501  *
502  *
503  * SMB-over-TCP is defined to use a modified version of the NBT header
504  * containing an 8-bit message type and 24-bit message length.
505  *
506  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
507  * |      TYPE     |                  LENGTH                       |
508  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
509  *
510  * 8-bit type      Must be 0
511  * 24-bit length
512  *
513  * The following structure is used to represent a generic, in-memory
514  * SMB transport header; it is not intended to map directly to either
515  * of the over-the-wire formats.
516  */
517 typedef struct {
518 	uint8_t		xh_type;
519 	uint32_t	xh_length;
520 } smb_xprt_t;
521 
522 int MBC_LENGTH(struct mbuf_chain *);
523 int MBC_MAXBYTES(struct mbuf_chain *);
524 void MBC_SETUP(struct mbuf_chain *, uint32_t);
525 void MBC_INIT(struct mbuf_chain *, uint32_t);
526 void MBC_FLUSH(struct mbuf_chain *);
527 void MBC_ATTACH_MBUF(struct mbuf_chain *, struct mbuf *);
528 void MBC_APPEND_MBUF(struct mbuf_chain *, struct mbuf *);
529 void MBC_ATTACH_BUF(struct mbuf_chain *MBC, unsigned char *BUF, int LEN);
530 int MBC_SHADOW_CHAIN(struct mbuf_chain *SUBMBC, struct mbuf_chain *MBC,
531     int OFF, int LEN);
532 
533 #define	MBC_ROOM_FOR(b, n) (((b)->chain_offset + (n)) <= (b)->max_bytes)
534 
535 #define	OPLOCK_MIN_TIMEOUT	(5 * 1000)
536 #define	OPLOCK_STD_TIMEOUT	(30 * 1000)
537 
538 /*
539  * Oplock break flags:
540  * SMB_OPLOCK_BREAK_EXCLUSIVE - only break exclusive oplock
541  * (type SMB_OPLOCK_EXCLUSIVE or SMB_OPLOCK_BATCH)
542  * SMB_OPLOCK_BREAK_BATCH - only break exclusive BATCH oplock
543  * SMB_OPLOCK_BREAK_NOWAIT - do not wait for oplock break ack
544  */
545 #define	SMB_OPLOCK_NO_BREAK		0x00
546 #define	SMB_OPLOCK_BREAK_TO_NONE	0x01
547 #define	SMB_OPLOCK_BREAK_TO_LEVEL_II	0x02
548 #define	SMB_OPLOCK_BREAK_EXCLUSIVE	0x04
549 #define	SMB_OPLOCK_BREAK_BATCH		0x08
550 #define	SMB_OPLOCK_BREAK_NOWAIT		0x10
551 
552 /*
553  * Oplocks levels are defined to match the levels in the SMB
554  * protocol (nt_create_andx / nt_transact_create) and should
555  * not be changed
556  */
557 #define	SMB_OPLOCK_NONE		0
558 #define	SMB_OPLOCK_EXCLUSIVE	1
559 #define	SMB_OPLOCK_BATCH	2
560 #define	SMB_OPLOCK_LEVEL_II	3
561 
562 typedef struct smb_oplock {
563 	kmutex_t		ol_mutex;
564 	kcondvar_t		ol_cv;
565 	kthread_t		*ol_xthread;
566 	boolean_t		ol_fem;		/* fem monitor installed? */
567 	uint8_t			ol_break;
568 	uint32_t		ol_count;	/* number of grants */
569 	list_t			ol_grants;	/* list of smb_oplock_grant_t */
570 } smb_oplock_t;
571 
572 #define	SMB_OPLOCK_GRANT_MAGIC	0x4F4C4B47	/* OLKG */
573 #define	SMB_OPLOCK_GRANT_VALID(p) \
574 	ASSERT((p)->og_magic == SMB_OPLOCK_GRANT_MAGIC)
575 #define	SMB_OFILE_OPLOCK_GRANTED(p) \
576 	((p)->f_oplock_grant.og_magic == SMB_OPLOCK_GRANT_MAGIC)
577 typedef struct smb_oplock_grant {
578 	uint32_t		og_magic;
579 	list_node_t		og_lnd;
580 	uint8_t			og_level;
581 	uint16_t		og_fid;
582 	uint16_t		og_tid;
583 	uint16_t		og_uid;
584 	struct smb_session	*og_session;
585 	struct smb_ofile	*og_ofile;
586 } smb_oplock_grant_t;
587 
588 #define	SMB_OPLOCK_BREAK_MAGIC	0x4F4C4B42	/* OLKB */
589 #define	SMB_OPLOCK_BREAK_VALID(p) \
590 	ASSERT((p)->ob_magic == SMB_OPLOCK_BREAK_MAGIC)
591 typedef struct smb_oplock_break {
592 	uint32_t	ob_magic;
593 	list_node_t	ob_lnd;
594 	struct smb_node	*ob_node;
595 } smb_oplock_break_t;
596 
597 
598 #define	SMB_VFS_MAGIC	0x534D4256	/* 'SMBV' */
599 
600 typedef struct smb_vfs {
601 	uint32_t		sv_magic;
602 	list_node_t		sv_lnd;
603 	uint32_t		sv_refcnt;
604 	vfs_t			*sv_vfsp;
605 	vnode_t			*sv_rootvp;
606 } smb_vfs_t;
607 
608 /*
609  * Solaris file systems handle timestamps differently from NTFS.
610  * In order to provide a more similar view of an open file's
611  * timestamps, we cache the timestamps in the node and manipulate
612  * them in a manner more consistent with windows.
613  * t_cached is B_TRUE when timestamps are cached.
614  * Timestamps remain cached while there are open ofiles for the node.
615  * This includes open ofiles for named streams.  t_open_ofiles is a
616  * count of open ofiles on the node, including named streams' ofiles,
617  * n_open_count cannot be used as it doesn't include ofiles opened
618  * for the node's named streams.
619  */
620 typedef struct smb_times {
621 	uint32_t		t_open_ofiles;
622 	boolean_t		t_cached;
623 	timestruc_t		t_atime;
624 	timestruc_t		t_mtime;
625 	timestruc_t		t_ctime;
626 	timestruc_t		t_crtime;
627 } smb_times_t;
628 
629 #define	SMB_NODE_MAGIC		0x4E4F4445	/* 'NODE' */
630 #define	SMB_NODE_VALID(p)	ASSERT((p)->n_magic == SMB_NODE_MAGIC)
631 
632 typedef enum {
633 	SMB_NODE_STATE_AVAILABLE = 0,
634 	SMB_NODE_STATE_DESTROYING
635 } smb_node_state_t;
636 
637 /*
638  * waiting_event        # of clients requesting FCN
639  * n_timestamps         cached timestamps
640  * n_allocsz            cached file allocation size
641  * n_dnode              directory node
642  * n_unode              unnamed stream node
643  * delete_on_close_cred credentials for delayed delete
644  */
645 typedef struct smb_node {
646 	uint32_t		n_magic;
647 	krwlock_t		n_lock;
648 	kmutex_t		n_mutex;
649 	list_node_t		n_lnd;
650 	smb_node_state_t	n_state;
651 	uint32_t		n_refcnt;
652 	uint32_t		n_hashkey;
653 	smb_llist_t		*n_hash_bucket;
654 	uint32_t		n_open_count;
655 	uint32_t		n_opening_count;
656 	smb_llist_t		n_ofile_list;
657 	smb_llist_t		n_lock_list;
658 	struct smb_ofile	*readonly_creator;
659 	volatile int		flags;
660 	volatile int		waiting_event;
661 	smb_times_t		n_timestamps;
662 	u_offset_t		n_allocsz;
663 	smb_oplock_t		n_oplock;
664 	struct smb_node		*n_dnode;
665 	struct smb_node		*n_unode;
666 	cred_t			*delete_on_close_cred;
667 	uint32_t		n_delete_on_close_flags;
668 	char			od_name[MAXNAMELEN];
669 	vnode_t			*vp;
670 	smb_audit_buf_node_t	*n_audit_buf;
671 } smb_node_t;
672 
673 #define	NODE_FLAGS_REPARSE		0x00001000
674 #define	NODE_FLAGS_DFSLINK		0x00002000
675 #define	NODE_FLAGS_VFSROOT		0x00004000
676 #define	NODE_FLAGS_SYSTEM		0x00008000
677 #define	NODE_FLAGS_WATCH_TREE		0x10000000
678 #define	NODE_FLAGS_NOTIFY_CHANGE	\
679 	(NODE_FLAGS_WATCH_TREE | FILE_NOTIFY_VALID_MASK)
680 #define	NODE_FLAGS_CHANGED		0x08000000
681 #define	NODE_FLAGS_WRITE_THROUGH	0x00100000
682 #define	NODE_XATTR_DIR			0x01000000
683 #define	NODE_FLAGS_DELETE_ON_CLOSE	0x40000000
684 #define	NODE_FLAGS_EXECUTABLE		0x80000000
685 
686 #define	SMB_NODE_VFS(node)	((node)->vp->v_vfsp)
687 #define	SMB_NODE_FSID(node)	((node)->vp->v_vfsp->vfs_fsid)
688 
689 /* Maximum buffer size for encryption key */
690 #define	SMB_ENCRYPT_KEY_MAXLEN		32
691 
692 #define	SMB_SHARE_MAGIC		0x4B534852	/* KSHR */
693 
694 typedef struct smb_kshare {
695 	uint32_t	shr_magic;
696 	char		*shr_name;
697 	char		*shr_path;
698 	char		*shr_cmnt;
699 	char		*shr_container;
700 	char		*shr_oemname;
701 	uint32_t	shr_flags;
702 	uint32_t	shr_type;
703 	uint32_t	shr_refcnt;
704 	uint32_t	shr_autocnt;
705 	uid_t		shr_uid;
706 	gid_t		shr_gid;
707 	char		*shr_access_none;
708 	char		*shr_access_ro;
709 	char		*shr_access_rw;
710 	avl_node_t	shr_link;
711 	kmem_cache_t	*shr_cache;
712 	kmutex_t	shr_mutex;
713 } smb_kshare_t;
714 
715 
716 typedef struct smb_arg_negotiate {
717 	char		*ni_name;
718 	int		ni_dialect;
719 	int		ni_index;
720 	uint32_t	ni_capabilities;
721 	uint16_t	ni_maxmpxcount;
722 	int16_t		ni_tzcorrection;
723 	uint8_t		ni_keylen;
724 	uint8_t		ni_key[SMB_ENCRYPT_KEY_MAXLEN];
725 	timestruc_t	ni_servertime;
726 } smb_arg_negotiate_t;
727 
728 typedef struct smb_arg_sessionsetup {
729 	char		*ssi_user;
730 	char		*ssi_domain;
731 	uint16_t	ssi_cipwlen;
732 	uint8_t		*ssi_cipwd;
733 	uint16_t	ssi_cspwlen;
734 	uint8_t		*ssi_cspwd;
735 	uint16_t	ssi_maxmpxcount;
736 	uint32_t	ssi_capabilities;
737 	uint32_t	ssi_sesskey;
738 	boolean_t	ssi_guest;
739 } smb_arg_sessionsetup_t;
740 
741 typedef struct tcon {
742 	char		*path;
743 	char		*service;
744 	int		pwdlen;
745 	char		*password;
746 	uint16_t	flags;
747 	uint16_t	optional_support;
748 	smb_kshare_t	*si;
749 } smb_arg_tcon_t;
750 
751 /*
752  * Based on section 2.6.1.2 (Connection Management) of the June 13,
753  * 1996 CIFS spec, a server may terminate the transport connection
754  * due to inactivity. The client software is expected to be able to
755  * automatically reconnect to the server if this happens. Like much
756  * of the useful background information, this section appears to
757  * have been dropped from later revisions of the document.
758  *
759  * Each session has an activity timestamp that's updated whenever a
760  * request is dispatched. If the session is idle, i.e. receives no
761  * requests, for SMB_SESSION_INACTIVITY_TIMEOUT minutes it will be
762  * closed.
763  *
764  * Each session has an I/O semaphore to serialize communication with
765  * the client. For example, after receiving a raw-read request, the
766  * server is not allowed to send an oplock break to the client until
767  * after it has sent the raw-read data.
768  */
769 #define	SMB_SESSION_INACTIVITY_TIMEOUT		(15 * 60)
770 
771 #define	SMB_SESSION_OFILE_MAX			(16 * 1024)
772 
773 /*
774  * When a connection is set up we need to remember both the client
775  * (peer) IP address and the local IP address used to establish the
776  * connection. When a client connects with a vc number of zero, we
777  * are supposed to abort any existing connections with that client
778  * (see notes in smb_negotiate.c and smb_session_setup_andx.c). For
779  * servers with multiple network interfaces or IP aliases, however,
780  * each interface has to be managed independently since the client
781  * is not aware of the server configuration. We have to allow the
782  * client to establish a connection on each interface with a vc
783  * number of zero without aborting the other connections.
784  *
785  * ipaddr:       the client (peer) IP address for the session.
786  * local_ipaddr: the local IP address used to connect to the server.
787  */
788 
789 #define	SMB_MAC_KEYSZ	512
790 
791 struct smb_sign {
792 	unsigned int seqnum;
793 	unsigned int mackey_len;
794 	unsigned int flags;
795 	unsigned char mackey[SMB_MAC_KEYSZ];
796 };
797 
798 #define	SMB_SIGNING_ENABLED	1
799 #define	SMB_SIGNING_CHECK	2
800 
801 /*
802  * Session State Machine
803  * ---------------------
804  *
805  * +-----------------------------+	     +------------------------------+
806  * | SMB_SESSION_STATE_CONNECTED |           | SMB_SESSION_STATE_TERMINATED |
807  * +-----------------------------+           +------------------------------+
808  *		T0|					     ^
809  *		  +--------------------+		     |T13
810  *		  v		       |T14                  |
811  * +-------------------------------+   |    +--------------------------------+
812  * | SMB_SESSION_STATE_ESTABLISHED |---+--->| SMB_SESSION_STATE_DISCONNECTED |
813  * +-------------------------------+        +--------------------------------+
814  *		T1|				^	   ^ ^ ^
815  *		  +----------+			|T9        | | |
816  *                           v			|          | | |
817  *                  +------------------------------+       | | |
818  *                  | SMB_SESSION_STATE_NEGOTIATED |       | | |
819  *                  +------------------------------+       | | |
820  *	                 ^|   ^|   | ^                     | | |
821  *      +----------------+|   ||   | |                     | | |
822  *      |+----------------+   || T7| |T8                   | | |
823  *      ||                    ||   | |                     | | |
824  *      ||   +----------------+|   | |                     | | |
825  *      ||   |+----------------+   | |                     | | |
826  *	||   ||			   v |                     | | |
827  *      ||   ||   +-----------------------------------+ T10| | |
828  *      ||   ||   | SMB_SESSION_STATE_OPLOCK_BREAKING |----+ | |
829  *      ||   ||   +-----------------------------------+      | |
830  *	||   ||T5                                            | |
831  *      ||   |+-->+-----------------------------------+	  T11| |
832  *      ||   |T6  | SMB_SESSION_STATE_READ_RAW_ACTIVE |------+ |
833  *      ||   +----+-----------------------------------+        |
834  *	||T3                                                   |
835  *      |+------->+------------------------------------+    T12|
836  *      |T4       | SMB_SESSION_STATE_WRITE_RAW_ACTIVE |-------+
837  *      +---------+------------------------------------+
838  *
839  * Transition T0
840  *
841  *
842  *
843  * Transition T1
844  *
845  *
846  *
847  * Transition T2
848  *
849  *
850  *
851  * Transition T3
852  *
853  *
854  *
855  * Transition T4
856  *
857  *
858  *
859  * Transition T5
860  *
861  *
862  *
863  * Transition T6
864  *
865  *
866  *
867  * Transition T7
868  *
869  *
870  *
871  * Transition T8
872  *
873  *
874  *
875  * Transition T9
876  *
877  *
878  *
879  * Transition T10
880  *
881  *
882  *
883  * Transition T11
884  *
885  *
886  *
887  * Transition T12
888  *
889  *
890  *
891  * Transition T13
892  *
893  *
894  *
895  * Transition T14
896  *
897  *
898  *
899  */
900 #define	SMB_SESSION_MAGIC	0x53455353	/* 'SESS' */
901 #define	SMB_SESSION_VALID(p)	\
902     ASSERT(((p) != NULL) && ((p)->s_magic == SMB_SESSION_MAGIC))
903 
904 typedef enum {
905 	SMB_SESSION_STATE_INITIALIZED = 0,
906 	SMB_SESSION_STATE_DISCONNECTED,
907 	SMB_SESSION_STATE_CONNECTED,
908 	SMB_SESSION_STATE_ESTABLISHED,
909 	SMB_SESSION_STATE_NEGOTIATED,
910 	SMB_SESSION_STATE_OPLOCK_BREAKING,
911 	SMB_SESSION_STATE_WRITE_RAW_ACTIVE,
912 	SMB_SESSION_STATE_READ_RAW_ACTIVE,
913 	SMB_SESSION_STATE_TERMINATED,
914 	SMB_SESSION_STATE_SENTINEL
915 } smb_session_state_t;
916 
917 typedef struct smb_session {
918 	uint32_t		s_magic;
919 	smb_rwx_t		s_lock;
920 	list_node_t		s_lnd;
921 	uint64_t		s_kid;
922 	smb_session_state_t	s_state;
923 	uint32_t		s_flags;
924 	int			s_write_raw_status;
925 	kthread_t		*s_thread;
926 	kt_did_t		s_ktdid;
927 	smb_kmod_cfg_t		s_cfg;
928 	kmem_cache_t		*s_cache;
929 	kmem_cache_t		*s_cache_request;
930 	struct smb_server	*s_server;
931 	int32_t			s_gmtoff;
932 	uint32_t		keep_alive;
933 	uint64_t		opentime;
934 	uint16_t		vcnumber;
935 	uint16_t		s_local_port;
936 	smb_inaddr_t		ipaddr;
937 	smb_inaddr_t		local_ipaddr;
938 	char 			workstation[SMB_PI_MAX_HOST];
939 	int			dialect;
940 	int			native_os;
941 	int			native_lm;
942 
943 	uint32_t		capabilities;
944 	struct smb_sign		signing;
945 
946 	ksocket_t		sock;
947 
948 	smb_slist_t		s_req_list;
949 	smb_llist_t		s_xa_list;
950 	smb_llist_t		s_user_list;
951 	smb_idpool_t		s_uid_pool;
952 	smb_txlst_t		s_txlst;
953 
954 	volatile uint32_t	s_tree_cnt;
955 	volatile uint32_t	s_file_cnt;
956 	volatile uint32_t	s_dir_cnt;
957 
958 	uint16_t		secmode;
959 	uint32_t		sesskey;
960 	uint32_t		challenge_len;
961 	unsigned char		challenge_key[8];
962 	unsigned char		MAC_key[44];
963 	int64_t			activity_timestamp;
964 	/*
965 	 * Maximum negotiated buffer size between SMB client and server
966 	 * in SMB_SESSION_SETUP_ANDX
967 	 */
968 	uint16_t		smb_msg_size;
969 	uchar_t			*outpipe_data;
970 	int			outpipe_datalen;
971 	int			outpipe_cookie;
972 	list_t			s_oplock_brkreqs;
973 	smb_srqueue_t		*s_srqueue;
974 } smb_session_t;
975 
976 #define	SMB_USER_MAGIC 0x55534552	/* 'USER' */
977 #define	SMB_USER_VALID(u)	\
978     ASSERT(((u) != NULL) && ((u)->u_magic == SMB_USER_MAGIC))
979 
980 #define	SMB_USER_FLAG_GUEST			SMB_ATF_GUEST
981 #define	SMB_USER_FLAG_IPC			SMB_ATF_ANON
982 #define	SMB_USER_FLAG_ADMIN			SMB_ATF_ADMIN
983 #define	SMB_USER_FLAG_POWER_USER		SMB_ATF_POWERUSER
984 #define	SMB_USER_FLAG_BACKUP_OPERATOR		SMB_ATF_BACKUPOP
985 
986 #define	SMB_USER_IS_ADMIN(U)	(((U)->u_flags & SMB_USER_FLAG_ADMIN) != 0)
987 #define	SMB_USER_IS_GUEST(U)	(((U)->u_flags & SMB_USER_FLAG_GUEST) != 0)
988 
989 #define	SMB_USER_PRIV_TAKE_OWNERSHIP	0x00000001
990 #define	SMB_USER_PRIV_BACKUP		0x00000002
991 #define	SMB_USER_PRIV_RESTORE		0x00000004
992 #define	SMB_USER_PRIV_SECURITY		0x00000008
993 
994 
995 typedef enum {
996 	SMB_USER_STATE_LOGGED_IN = 0,
997 	SMB_USER_STATE_LOGGING_OFF,
998 	SMB_USER_STATE_LOGGED_OFF,
999 	SMB_USER_STATE_SENTINEL
1000 } smb_user_state_t;
1001 
1002 typedef struct smb_user {
1003 	uint32_t		u_magic;
1004 	list_node_t		u_lnd;
1005 	kmutex_t		u_mutex;
1006 	smb_user_state_t	u_state;
1007 
1008 	struct smb_server	*u_server;
1009 	smb_session_t		*u_session;
1010 	uint16_t		u_name_len;
1011 	char			*u_name;
1012 	uint16_t		u_domain_len;
1013 	char			*u_domain;
1014 	time_t			u_logon_time;
1015 	cred_t			*u_cred;
1016 	cred_t			*u_privcred;
1017 
1018 	smb_llist_t		u_tree_list;
1019 	smb_idpool_t		u_tid_pool;
1020 
1021 	uint32_t		u_refcnt;
1022 	uint32_t		u_flags;
1023 	uint32_t		u_privileges;
1024 	uint16_t		u_uid;
1025 	uint32_t		u_audit_sid;
1026 } smb_user_t;
1027 
1028 #define	SMB_TREE_MAGIC			0x54524545	/* 'TREE' */
1029 #define	SMB_TREE_VALID(p)	\
1030     ASSERT((p != NULL) && ((p)->t_magic == SMB_TREE_MAGIC))
1031 
1032 #define	SMB_TYPENAMELEN			_ST_FSTYPSZ
1033 #define	SMB_VOLNAMELEN			32
1034 
1035 #define	SMB_TREE_READONLY		0x00000001
1036 #define	SMB_TREE_SUPPORTS_ACLS		0x00000002
1037 #define	SMB_TREE_STREAMS		0x00000004
1038 #define	SMB_TREE_CASEINSENSITIVE	0x00000008
1039 #define	SMB_TREE_NO_CASESENSITIVE	0x00000010
1040 #define	SMB_TREE_NO_EXPORT		0x00000020
1041 #define	SMB_TREE_OPLOCKS		0x00000040
1042 #define	SMB_TREE_SHORTNAMES		0x00000080
1043 #define	SMB_TREE_XVATTR			0x00000100
1044 #define	SMB_TREE_DIRENTFLAGS		0x00000200
1045 #define	SMB_TREE_ACLONCREATE		0x00000400
1046 #define	SMB_TREE_ACEMASKONACCESS	0x00000800
1047 #define	SMB_TREE_NFS_MOUNTED		0x00001000
1048 #define	SMB_TREE_UNICODE_ON_DISK	0x00002000
1049 #define	SMB_TREE_CATIA			0x00004000
1050 #define	SMB_TREE_ABE			0x00008000
1051 #define	SMB_TREE_QUOTA			0x00010000
1052 #define	SMB_TREE_DFSROOT		0x00020000
1053 
1054 typedef enum {
1055 	SMB_TREE_STATE_CONNECTED = 0,
1056 	SMB_TREE_STATE_DISCONNECTING,
1057 	SMB_TREE_STATE_DISCONNECTED,
1058 	SMB_TREE_STATE_SENTINEL
1059 } smb_tree_state_t;
1060 
1061 typedef struct smb_tree {
1062 	uint32_t		t_magic;
1063 	kmutex_t		t_mutex;
1064 	list_node_t		t_lnd;
1065 	smb_tree_state_t	t_state;
1066 
1067 	struct smb_server	*t_server;
1068 	smb_session_t		*t_session;
1069 	smb_user_t		*t_user;
1070 	smb_node_t		*t_snode;
1071 
1072 	smb_llist_t		t_ofile_list;
1073 	smb_idpool_t		t_fid_pool;
1074 
1075 	smb_llist_t		t_odir_list;
1076 	smb_idpool_t		t_odid_pool;
1077 
1078 	uint32_t		t_refcnt;
1079 	uint32_t		t_flags;
1080 	int32_t			t_res_type;
1081 	uint16_t		t_tid;
1082 	uint16_t		t_umask;
1083 	char			t_sharename[MAXNAMELEN];
1084 	char			t_resource[MAXPATHLEN];
1085 	char			t_typename[SMB_TYPENAMELEN];
1086 	char			t_volume[SMB_VOLNAMELEN];
1087 	acl_type_t		t_acltype;
1088 	uint32_t		t_access;
1089 	uint32_t		t_execflags;
1090 	time_t			t_connect_time;
1091 	volatile uint32_t	t_open_files;
1092 } smb_tree_t;
1093 
1094 #define	SMB_TREE_VFS(tree)	((tree)->t_snode->vp->v_vfsp)
1095 #define	SMB_TREE_FSID(tree)	((tree)->t_snode->vp->v_vfsp->vfs_fsid)
1096 
1097 #define	SMB_TREE_IS_READONLY(sr)					\
1098 	((sr) != NULL && (sr)->tid_tree != NULL &&			\
1099 	!((sr)->tid_tree->t_access & ACE_ALL_WRITE_PERMS))
1100 
1101 #define	SMB_TREE_IS_CASEINSENSITIVE(sr)                                 \
1102 	(((sr) && (sr)->tid_tree) ?                                     \
1103 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CASEINSENSITIVE) : 0)
1104 
1105 #define	SMB_TREE_HAS_ACCESS(sr, acemask)				\
1106 	((sr) == NULL ? ACE_ALL_PERMS : (				\
1107 	(((sr) && (sr)->tid_tree) ?					\
1108 	(((sr)->tid_tree->t_access) & (acemask)) : 0)))
1109 
1110 #define	SMB_TREE_SUPPORTS_CATIA(sr)            				\
1111 	(((sr) && (sr)->tid_tree) ?                                     \
1112 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CATIA) : 0)
1113 
1114 #define	SMB_TREE_SUPPORTS_ABE(sr)            				\
1115 	(((sr) && (sr)->tid_tree) ?                                     \
1116 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_ABE) : 0)
1117 
1118 #define	SMB_TREE_IS_DFSROOT(sr)            				\
1119 	(((sr) && (sr)->tid_tree) ?                                     \
1120 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_DFSROOT) : 0)
1121 
1122 #define	SMB_TREE_SUPPORTS_SHORTNAMES(sr)				\
1123 	(((sr) && (sr)->tid_tree) ?					\
1124 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_SHORTNAMES) : 0)
1125 
1126 /*
1127  * SMB_TREE_CONTAINS_NODE is used to check that a node is in the same
1128  * file system as the tree.
1129  */
1130 #define	SMB_TREE_CONTAINS_NODE(sr, node)                                \
1131 	(((sr) && (sr)->tid_tree) ?                                     \
1132 	(SMB_TREE_VFS((sr)->tid_tree) == SMB_NODE_VFS(node)) : 1)
1133 
1134 /*
1135  * SMB_OFILE_IS_READONLY reflects whether an ofile is readonly or not.
1136  * The macro takes into account
1137  *      - the tree readonly state
1138  *      - the node readonly state
1139  *      - whether the specified ofile is the readonly creator
1140  * The readonly creator has write permission until the ofile is closed.
1141  */
1142 
1143 #define	SMB_OFILE_IS_READONLY(of)                               \
1144 	(((of)->f_flags & SMB_OFLAGS_READONLY) ||               \
1145 	smb_node_file_is_readonly((of)->f_node) ||                   \
1146 	(((of)->f_node->readonly_creator) &&                    \
1147 	((of)->f_node->readonly_creator != (of))))
1148 
1149 /*
1150  * SMB_PATHFILE_IS_READONLY indicates whether or not a file is
1151  * readonly when the caller has a path rather than an ofile.  Unlike
1152  * SMB_OFILE_IS_READONLY, the caller cannot be the readonly creator,
1153  * since that requires an ofile.
1154  */
1155 
1156 #define	SMB_PATHFILE_IS_READONLY(sr, node)                       \
1157 	(SMB_TREE_IS_READONLY((sr)) ||                           \
1158 	smb_node_file_is_readonly((node)) ||                          \
1159 	((node)->readonly_creator))
1160 
1161 #define	SMB_OPIPE_MAGIC		0x50495045	/* 'PIPE' */
1162 #define	SMB_OPIPE_VALID(p)	\
1163     ASSERT(((p) != NULL) && (p)->p_magic == SMB_OPIPE_MAGIC)
1164 
1165 /*
1166  * Data structure for SMB_FTYPE_MESG_PIPE ofiles, which is used
1167  * at the interface between SMB and NDR RPC.
1168  */
1169 typedef struct smb_opipe {
1170 	uint32_t		p_magic;
1171 	list_node_t		p_lnd;
1172 	kmutex_t		p_mutex;
1173 	kcondvar_t		p_cv;
1174 	struct smb_server	*p_server;
1175 	struct smb_event	*p_event;
1176 	char			*p_name;
1177 	uint32_t		p_busy;
1178 	smb_doorhdr_t		p_hdr;
1179 	smb_netuserinfo_t	p_user;
1180 	uint8_t			*p_doorbuf;
1181 	uint8_t			*p_data;
1182 } smb_opipe_t;
1183 
1184 /*
1185  * The of_ftype	of an open file should contain the SMB_FTYPE value
1186  * returned when the file/pipe was opened. The following
1187  * assumptions are currently made:
1188  *
1189  * File Type	    Node       PipeInfo
1190  * ---------	    --------   --------
1191  * SMB_FTYPE_DISK       Valid      Null
1192  * SMB_FTYPE_BYTE_PIPE  Undefined  Undefined
1193  * SMB_FTYPE_MESG_PIPE  Null       Valid
1194  * SMB_FTYPE_PRINTER    Undefined  Undefined
1195  * SMB_FTYPE_UNKNOWN    Undefined  Undefined
1196  */
1197 
1198 /*
1199  * Some flags for ofile structure
1200  *
1201  *	SMB_OFLAGS_SET_DELETE_ON_CLOSE
1202  *   Set this flag when the corresponding open operation whose
1203  *   DELETE_ON_CLOSE bit of the CreateOptions is set. If any
1204  *   open file instance has this bit set, the NODE_FLAGS_DELETE_ON_CLOSE
1205  *   will be set for the file node upon close.
1206  *
1207  *	SMB_OFLAGS_TIMESTAMPS_PENDING
1208  *   This flag gets set when a write operation is performed on the
1209  *   ofile. The timestamps will be updated, and the flags cleared,
1210  *   when the ofile gets closed or a setattr is performed on the ofile.
1211  */
1212 
1213 #define	SMB_OFLAGS_READONLY		0x0001
1214 #define	SMB_OFLAGS_EXECONLY		0x0002
1215 #define	SMB_OFLAGS_SET_DELETE_ON_CLOSE	0x0004
1216 #define	SMB_OFLAGS_LLF_POS_VALID	0x0008
1217 #define	SMB_OFLAGS_TIMESTAMPS_PENDING	0x0010
1218 
1219 #define	SMB_OFILE_MAGIC 	0x4F464C45	/* 'OFLE' */
1220 #define	SMB_OFILE_VALID(p)	\
1221     ASSERT((p != NULL) && ((p)->f_magic == SMB_OFILE_MAGIC))
1222 
1223 typedef enum {
1224 	SMB_OFILE_STATE_OPEN = 0,
1225 	SMB_OFILE_STATE_CLOSING,
1226 	SMB_OFILE_STATE_CLOSED,
1227 	SMB_OFILE_STATE_SENTINEL
1228 } smb_ofile_state_t;
1229 
1230 typedef struct smb_ofile {
1231 	uint32_t		f_magic;
1232 	kmutex_t		f_mutex;
1233 	list_node_t		f_lnd;
1234 	list_node_t		f_nnd;
1235 	smb_ofile_state_t	f_state;
1236 
1237 	struct smb_server	*f_server;
1238 	smb_session_t		*f_session;
1239 	smb_user_t		*f_user;
1240 	smb_tree_t		*f_tree;
1241 	smb_node_t		*f_node;
1242 	smb_opipe_t		*f_pipe;
1243 
1244 	uint32_t		f_uniqid;
1245 	uint32_t		f_refcnt;
1246 	uint64_t		f_seek_pos;
1247 	uint32_t		f_flags;
1248 	uint32_t		f_granted_access;
1249 	uint32_t		f_share_access;
1250 	uint32_t		f_create_options;
1251 	uint16_t		f_fid;
1252 	uint16_t		f_opened_by_pid;
1253 	uint16_t		f_ftype;
1254 	uint64_t		f_llf_pos;
1255 	int			f_mode;
1256 	cred_t			*f_cr;
1257 	pid_t			f_pid;
1258 	uint32_t		f_explicit_times;
1259 	char			f_quota_resume[SMB_SID_STRSZ];
1260 	smb_oplock_grant_t	f_oplock_grant;
1261 } smb_ofile_t;
1262 
1263 #define	SMB_ODIR_MAGIC 		0x4F444952	/* 'ODIR' */
1264 #define	SMB_ODIR_VALID(p)	\
1265     ASSERT((p != NULL) && ((p)->d_magic == SMB_ODIR_MAGIC))
1266 
1267 #define	SMB_ODIR_BUFSIZE	(8 * 1024)
1268 
1269 #define	SMB_ODIR_FLAG_WILDCARDS		0x0001
1270 #define	SMB_ODIR_FLAG_IGNORE_CASE	0x0002
1271 #define	SMB_ODIR_FLAG_XATTR		0x0004
1272 #define	SMB_ODIR_FLAG_EDIRENT		0x0008
1273 #define	SMB_ODIR_FLAG_CATIA		0x0010
1274 #define	SMB_ODIR_FLAG_ABE		0x0020
1275 #define	SMB_ODIR_FLAG_SHORTNAMES	0x0040
1276 
1277 typedef enum {
1278 	SMB_ODIR_STATE_OPEN = 0,
1279 	SMB_ODIR_STATE_IN_USE,
1280 	SMB_ODIR_STATE_CLOSING,
1281 	SMB_ODIR_STATE_CLOSED,
1282 	SMB_ODIR_STATE_SENTINEL
1283 } smb_odir_state_t;
1284 
1285 typedef enum {
1286 	SMB_ODIR_RESUME_IDX,
1287 	SMB_ODIR_RESUME_COOKIE,
1288 	SMB_ODIR_RESUME_FNAME
1289 } smb_odir_resume_type_t;
1290 
1291 typedef struct smb_odir_resume {
1292 	smb_odir_resume_type_t	or_type;
1293 	int			or_idx;
1294 	uint32_t		or_cookie;
1295 	char			*or_fname;
1296 } smb_odir_resume_t;
1297 
1298 /*
1299  * Flags used when opening an odir
1300  */
1301 #define	SMB_ODIR_OPENF_BACKUP_INTENT	0x01
1302 
1303 typedef struct smb_odir {
1304 	uint32_t		d_magic;
1305 	kmutex_t		d_mutex;
1306 	list_node_t		d_lnd;
1307 	smb_odir_state_t	d_state;
1308 	smb_session_t		*d_session;
1309 	smb_tree_t		*d_tree;
1310 	smb_node_t		*d_dnode;
1311 	cred_t			*d_cred;
1312 	uint16_t		d_odid;
1313 	uint16_t		d_opened_by_pid;
1314 	uint16_t		d_sattr;
1315 	uint32_t		d_refcnt;
1316 	uint32_t		d_flags;
1317 	boolean_t		d_eof;
1318 	int			d_bufsize;
1319 	uint64_t		d_offset;
1320 	union {
1321 		char		*u_bufptr;
1322 		edirent_t	*u_edp;
1323 		dirent64_t	*u_dp;
1324 	} d_u;
1325 	uint32_t		d_cookies[SMB_MAX_SEARCH];
1326 	char			d_pattern[MAXNAMELEN];
1327 	char			d_buf[SMB_ODIR_BUFSIZE];
1328 } smb_odir_t;
1329 #define	d_bufptr	d_u.u_bufptr
1330 #define	d_edp		d_u.u_edp
1331 #define	d_dp		d_u.u_dp
1332 
1333 typedef struct smb_odirent {
1334 	char		od_name[MAXNAMELEN];	/* on disk name */
1335 	ino64_t		od_ino;
1336 	uint32_t	od_eflags;
1337 } smb_odirent_t;
1338 
1339 typedef struct smb_fileinfo {
1340 	char		fi_name[MAXNAMELEN];
1341 	char		fi_shortname[SMB_SHORTNAMELEN];
1342 	uint32_t	fi_cookie;
1343 	uint32_t	fi_dosattr;	/* DOS attributes */
1344 	uint64_t	fi_nodeid;	/* file system node id */
1345 	uint64_t	fi_size;	/* file size in bytes */
1346 	uint64_t	fi_alloc_size;	/* allocation size in bytes */
1347 	timestruc_t	fi_atime;	/* last access */
1348 	timestruc_t	fi_mtime;	/* last modification */
1349 	timestruc_t	fi_ctime;	/* last status change */
1350 	timestruc_t	fi_crtime;	/* file creation */
1351 } smb_fileinfo_t;
1352 
1353 typedef struct smb_streaminfo {
1354 	uint64_t	si_size;
1355 	uint64_t	si_alloc_size;
1356 	char		si_name[MAXPATHLEN];
1357 } smb_streaminfo_t;
1358 
1359 #define	SMB_LOCK_MAGIC 	0x4C4F434B	/* 'LOCK' */
1360 
1361 typedef struct smb_lock {
1362 	uint32_t		l_magic;
1363 	kmutex_t		l_mutex;
1364 	list_node_t		l_lnd;
1365 	kcondvar_t		l_cv;
1366 
1367 	list_node_t		l_conflict_lnd;
1368 	smb_slist_t		l_conflict_list;
1369 
1370 	smb_session_t		*l_session;
1371 	smb_ofile_t		*l_file;
1372 	struct smb_request	*l_sr;
1373 
1374 	uint32_t		l_flags;
1375 	uint64_t		l_session_kid;
1376 	struct smb_lock		*l_blocked_by; /* Debug info only */
1377 
1378 	uint16_t		l_pid;
1379 	uint16_t		l_uid;
1380 	uint32_t		l_type;
1381 	uint64_t		l_start;
1382 	uint64_t		l_length;
1383 	clock_t			l_end_time;
1384 } smb_lock_t;
1385 
1386 #define	SMB_LOCK_FLAG_INDEFINITE	0x0004
1387 #define	SMB_LOCK_INDEFINITE_WAIT(lock) \
1388 	((lock)->l_flags & SMB_LOCK_FLAG_INDEFINITE)
1389 
1390 #define	SMB_LOCK_TYPE_READWRITE		101
1391 #define	SMB_LOCK_TYPE_READONLY		102
1392 
1393 typedef struct vardata_block {
1394 	uint8_t			vdb_tag;
1395 	uint32_t		vdb_len;
1396 	struct uio 		vdb_uio;
1397 	struct iovec		vdb_iovec[MAX_IOVEC];
1398 } smb_vdb_t;
1399 
1400 #define	SMB_WRMODE_WRITE_THRU	0x0001
1401 #define	SMB_WRMODE_IS_STABLE(M)	((M) & SMB_WRMODE_WRITE_THRU)
1402 
1403 #define	SMB_RW_MAGIC		0x52445257	/* 'RDRW' */
1404 
1405 typedef struct smb_rw_param {
1406 	uint32_t rw_magic;
1407 	smb_vdb_t rw_vdb;
1408 	uint64_t rw_offset;
1409 	uint32_t rw_last_write;
1410 	uint16_t rw_mode;
1411 	uint32_t rw_count;		/* bytes in this request */
1412 	uint16_t rw_mincnt;
1413 	uint32_t rw_total;		/* total bytes (write-raw) */
1414 	uint16_t rw_dsoff;		/* SMB data offset */
1415 	uint8_t rw_andx;		/* SMB secondary andx command */
1416 } smb_rw_param_t;
1417 
1418 typedef struct smb_pathname {
1419 	char	*pn_path;
1420 	char	*pn_pname;
1421 	char	*pn_fname;
1422 	char	*pn_sname;
1423 	char	*pn_stype;
1424 } smb_pathname_t;
1425 
1426 /*
1427  * fs_query_info
1428  */
1429 typedef struct smb_fqi {
1430 	smb_pathname_t	fq_path;
1431 	uint16_t	fq_sattr;
1432 	smb_node_t	*fq_dnode;
1433 	smb_node_t	*fq_fnode;
1434 	smb_attr_t	fq_fattr;
1435 	char		fq_last_comp[MAXNAMELEN];
1436 } smb_fqi_t;
1437 
1438 typedef struct dirop {
1439 	smb_fqi_t	fqi;
1440 	smb_fqi_t	dst_fqi;
1441 	uint16_t	info_level;
1442 	uint16_t	flags;
1443 } smb_arg_dirop_t;
1444 
1445 typedef struct {
1446 	uint32_t status;
1447 	uint16_t errcls;
1448 	uint16_t errcode;
1449 } smb_error_t;
1450 
1451 typedef struct open_param {
1452 	smb_fqi_t	fqi;
1453 	uint16_t	omode;
1454 	uint16_t	ofun;
1455 	uint32_t	nt_flags;
1456 	uint32_t	timeo;
1457 	uint32_t	dattr;
1458 	timestruc_t	crtime;
1459 	timestruc_t	mtime;
1460 	uint64_t	dsize;
1461 	uint32_t	desired_access;
1462 	uint32_t	share_access;
1463 	uint32_t	create_options;
1464 	uint32_t	create_disposition;
1465 	boolean_t	created_readonly;
1466 	uint32_t	ftype;
1467 	uint32_t	devstate;
1468 	uint32_t	action_taken;
1469 	uint64_t	fileid;
1470 	uint32_t	rootdirfid;
1471 	smb_ofile_t	*dir;
1472 	/* This is only set by NTTransactCreate */
1473 	struct smb_sd	*sd;
1474 	uint8_t		op_oplock_level;	/* requested/granted level */
1475 	boolean_t	op_oplock_levelII;	/* TRUE if levelII supported */
1476 } smb_arg_open_t;
1477 
1478 /*
1479  * SMB Request State Machine
1480  * -------------------------
1481  *
1482  *                  T4               +------+		T0
1483  *      +--------------------------->| FREE |---------------------------+
1484  *      |                            +------+                           |
1485  * +-----------+                                                        |
1486  * | COMPLETED |                                                        |
1487  * +-----------+
1488  *      ^                                                               |
1489  *      | T15                      +----------+                         v
1490  * +------------+        T6        |          |                 +--------------+
1491  * | CLEANED_UP |<-----------------| CANCELED |                 | INITIALIZING |
1492  * +------------+                  |          |                 +--------------+
1493  *      |    ^                     +----------+                         |
1494  *      |    |                        ^  ^ ^ ^                          |
1495  *      |    |          +-------------+  | | |                          |
1496  *      |    |    T3    |                | | |               T13        | T1
1497  *      |    +-------------------------+ | | +----------------------+   |
1498  *      +----------------------------+ | | |                        |   |
1499  *         T16          |            | | | +-----------+            |   |
1500  *                      |           \/ | | T5          |            |   v
1501  * +-----------------+  |   T12     +--------+         |     T2    +-----------+
1502  * | EVENT_OCCURRED  |------------->| ACTIVE |<--------------------| SUBMITTED |
1503  * +-----------------+  |           +--------+         |           +-----------+
1504  *        ^             |              | ^ |           |
1505  *        |             |           T8 | | |  T7       |
1506  *        | T10      T9 |   +----------+ | +-------+   |  T11
1507  *        |             |   |            +-------+ |   |
1508  *        |             |   |               T14  | |   |
1509  *        |             |   v                    | v   |
1510  *      +----------------------+                +--------------+
1511  *	|     WAITING_EVENT    |                | WAITING_LOCK |
1512  *      +----------------------+                +--------------+
1513  *
1514  *
1515  *
1516  *
1517  *
1518  * Transition T0
1519  *
1520  * This transition occurs when the request is allocated and is still under the
1521  * control of the session thread.
1522  *
1523  * Transition T1
1524  *
1525  * This transition occurs when the session thread dispatches a task to treat the
1526  * request.
1527  *
1528  * Transition T2
1529  *
1530  *
1531  *
1532  * Transition T3
1533  *
1534  * A request completes and smbsr_cleanup is called to release resources
1535  * associated with the request (but not the smb_request_t itself).  This
1536  * includes references on smb_ofile_t, smb_node_t, and other structures.
1537  * CLEANED_UP state exists to detect if we attempt to cleanup a request
1538  * multiple times and to allow us to detect that we are accessing a
1539  * request that has already been cleaned up.
1540  *
1541  * Transition T4
1542  *
1543  *
1544  *
1545  * Transition T5
1546  *
1547  *
1548  *
1549  * Transition T6
1550  *
1551  *
1552  *
1553  * Transition T7
1554  *
1555  *
1556  *
1557  * Transition T8
1558  *
1559  *
1560  *
1561  * Transition T9
1562  *
1563  *
1564  *
1565  * Transition T10
1566  *
1567  *
1568  *
1569  * Transition T11
1570  *
1571  *
1572  *
1573  * Transition T12
1574  *
1575  *
1576  *
1577  * Transition T13
1578  *
1579  *
1580  *
1581  * Transition T14
1582  *
1583  *
1584  *
1585  * Transition T15
1586  *
1587  * Request processing is completed (control returns from smb_dispatch)
1588  *
1589  * Transition T16
1590  *
1591  * Multipart (andx) request was cleaned up with smbsr_cleanup but more "andx"
1592  * sections remain to be processed.
1593  *
1594  */
1595 
1596 #define	SMB_REQ_MAGIC 		0x534D4252	/* 'SMBR' */
1597 #define	SMB_REQ_VALID(p)	ASSERT((p)->sr_magic == SMB_REQ_MAGIC)
1598 
1599 typedef enum smb_req_state {
1600 	SMB_REQ_STATE_FREE = 0,
1601 	SMB_REQ_STATE_INITIALIZING,
1602 	SMB_REQ_STATE_SUBMITTED,
1603 	SMB_REQ_STATE_ACTIVE,
1604 	SMB_REQ_STATE_WAITING_EVENT,
1605 	SMB_REQ_STATE_EVENT_OCCURRED,
1606 	SMB_REQ_STATE_WAITING_LOCK,
1607 	SMB_REQ_STATE_COMPLETED,
1608 	SMB_REQ_STATE_CANCELED,
1609 	SMB_REQ_STATE_CLEANED_UP,
1610 	SMB_REQ_STATE_SENTINEL
1611 } smb_req_state_t;
1612 
1613 typedef struct smb_request {
1614 	uint32_t		sr_magic;
1615 	kmutex_t		sr_mutex;
1616 	list_node_t		sr_session_lnd;
1617 	smb_req_state_t		sr_state;
1618 	boolean_t		sr_keep;
1619 	kmem_cache_t		*sr_cache;
1620 	struct smb_server	*sr_server;
1621 	pid_t			*sr_pid;
1622 	int32_t			sr_gmtoff;
1623 	smb_session_t		*session;
1624 	smb_kmod_cfg_t		*sr_cfg;
1625 	smb_notify_change_req_t	sr_ncr;
1626 
1627 	/* Info from session service header */
1628 	uint32_t		sr_req_length; /* Excluding NBT header */
1629 
1630 	/* Request buffer excluding NBT header */
1631 	void			*sr_request_buf;
1632 
1633 	smb_lock_t		*sr_awaiting;
1634 	struct mbuf_chain	command;
1635 	struct mbuf_chain	reply;
1636 	struct mbuf_chain	raw_data;
1637 	list_t			sr_storage;
1638 	struct smb_xa		*r_xa;
1639 	int			andx_prev_wct;
1640 	int 			cur_reply_offset;
1641 	int			orig_request_hdr;
1642 	unsigned int		reply_seqnum;	/* reply sequence number */
1643 	unsigned char		first_smb_com;	/* command code */
1644 	unsigned char		smb_com;	/* command code */
1645 
1646 	uint8_t			smb_rcls;	/* error code class */
1647 	uint8_t			smb_reh;	/* rsvd (AH DOS INT-24 ERR) */
1648 	uint16_t		smb_err;	/* error code */
1649 	smb_error_t		smb_error;
1650 
1651 	uint8_t			smb_flg;	/* flags */
1652 	uint16_t		smb_flg2;	/* flags */
1653 	uint16_t		smb_pid_high;	/* high part of pid */
1654 	unsigned char		smb_sig[8];	/* signiture */
1655 	uint16_t		smb_tid;	/* tree id #  */
1656 	uint16_t		smb_pid;	/* caller's process id # */
1657 	uint16_t		smb_uid;	/* user id # */
1658 	uint16_t		smb_mid;	/* mutiplex id #  */
1659 	unsigned char		smb_wct;	/* count of parameter words */
1660 	uint16_t		smb_bcc;	/* data byte count */
1661 
1662 	/* Parameters */
1663 	struct mbuf_chain	smb_vwv;	/* variable width value */
1664 
1665 	/* Data */
1666 	struct mbuf_chain	smb_data;
1667 
1668 	uint16_t		smb_fid;	/* not in hdr, but common */
1669 
1670 	unsigned char		andx_com;
1671 	uint16_t		andx_off;
1672 
1673 	struct smb_tree		*tid_tree;
1674 	struct smb_ofile	*fid_ofile;
1675 	smb_user_t		*uid_user;
1676 
1677 	union {
1678 		smb_arg_negotiate_t	*negprot;
1679 		smb_arg_sessionsetup_t	*ssetup;
1680 		smb_arg_tcon_t		tcon;
1681 		smb_arg_dirop_t		dirop;
1682 		smb_arg_open_t		open;
1683 		smb_rw_param_t		*rw;
1684 		uint32_t		timestamp;
1685 	} arg;
1686 
1687 	cred_t			*user_cr;
1688 	kthread_t		*sr_worker;
1689 	hrtime_t		sr_time_submitted;
1690 	hrtime_t		sr_time_active;
1691 	hrtime_t		sr_time_start;
1692 	int32_t			sr_txb;
1693 	uint32_t		sr_seqnum;
1694 } smb_request_t;
1695 
1696 #define	sr_ssetup	arg.ssetup
1697 #define	sr_negprot	arg.negprot
1698 #define	sr_tcon		arg.tcon
1699 #define	sr_dirop	arg.dirop
1700 #define	sr_open		arg.open
1701 #define	sr_rw		arg.rw
1702 #define	sr_timestamp	arg.timestamp
1703 
1704 #define	SMB_READ_PROTOCOL(hdr) \
1705 	LE_IN32(((smb_hdr_t *)(hdr))->protocol)
1706 
1707 #define	SMB_PROTOCOL_MAGIC_INVALID(rd_sr) \
1708 	(SMB_READ_PROTOCOL((rd_sr)->sr_request_buf) != SMB_PROTOCOL_MAGIC)
1709 
1710 #define	SMB_READ_COMMAND(hdr) \
1711 	(((smb_hdr_t *)(hdr))->command)
1712 
1713 #define	SMB_IS_WRITERAW(rd_sr) \
1714 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_WRITE_RAW)
1715 
1716 #define	SMB_IS_NT_CANCEL(rd_sr) \
1717 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_NT_CANCEL)
1718 
1719 #define	SMB_IS_SESSION_SETUP_ANDX(rd_sr) \
1720 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == \
1721 	    SMB_COM_SESSION_SETUP_ANDX)
1722 
1723 #define	SMB_IS_NT_NEGOTIATE(rd_sr) \
1724 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_NEGOTIATE)
1725 
1726 #define	SMB_IS_TREE_CONNECT_ANDX(rd_sr) \
1727 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_TREE_CONNECT_ANDX)
1728 
1729 #define	SMB_XA_FLAG_OPEN	0x0001
1730 #define	SMB_XA_FLAG_CLOSE	0x0002
1731 #define	SMB_XA_FLAG_COMPLETE	0x0004
1732 #define	SMB_XA_CLOSED(xa) (!((xa)->xa_flags & SMB_XA_FLAG_OPEN))
1733 
1734 #define	SMB_XA_MAGIC		0x534D4258	/* 'SMBX' */
1735 
1736 typedef struct smb_xa {
1737 	uint32_t		xa_magic;
1738 	kmutex_t		xa_mutex;
1739 	list_node_t		xa_lnd;
1740 
1741 	uint32_t		xa_refcnt;
1742 	uint32_t		xa_flags;
1743 
1744 	struct smb_session	*xa_session;
1745 
1746 	unsigned char		smb_com;	/* which TRANS type */
1747 	unsigned char		smb_flg;	/* flags */
1748 	uint16_t		smb_flg2;	/* flags */
1749 	uint16_t		smb_tid;	/* tree id number */
1750 	uint16_t		smb_pid;	/* caller's process id number */
1751 	uint16_t		smb_uid;	/* user id number */
1752 	uint32_t		smb_func;	/* NT_TRANS function */
1753 
1754 	uint16_t		xa_smb_mid;	/* mutiplex id number */
1755 	uint16_t		xa_smb_fid;	/* TRANS2 secondary */
1756 
1757 	unsigned int		reply_seqnum;	/* reply sequence number */
1758 
1759 	uint32_t	smb_tpscnt;	/* total parameter bytes being sent */
1760 	uint32_t	smb_tdscnt;	/* total data bytes being sent */
1761 	uint32_t	smb_mprcnt;	/* max parameter bytes to return */
1762 	uint32_t	smb_mdrcnt;	/* max data bytes to return */
1763 	uint32_t	smb_msrcnt;	/* max setup words to return */
1764 	uint32_t	smb_flags;	/* additional information: */
1765 				/*  bit 0 - if set, disconnect TID in smb_tid */
1766 				/*  bit 1 - if set, transaction is one way */
1767 				/*  (no final response) */
1768 	int32_t	smb_timeout;	/* number of milliseconds to await completion */
1769 	uint32_t	smb_suwcnt;	/* set up word count */
1770 
1771 	char			*xa_pipe_name;
1772 
1773 	int			req_disp_param;
1774 	int			req_disp_data;
1775 
1776 	struct mbuf_chain	req_setup_mb;
1777 	struct mbuf_chain	req_param_mb;
1778 	struct mbuf_chain	req_data_mb;
1779 
1780 	struct mbuf_chain	rep_setup_mb;
1781 	struct mbuf_chain	rep_param_mb;
1782 	struct mbuf_chain	rep_data_mb;
1783 } smb_xa_t;
1784 
1785 
1786 #define	SDDF_NO_FLAGS			0
1787 #define	SDDF_SUPPRESS_TID		0x0001
1788 #define	SDDF_SUPPRESS_UID		0x0002
1789 
1790 /*
1791  * SMB dispatch return codes.
1792  */
1793 typedef enum {
1794 	SDRC_SUCCESS = 0,
1795 	SDRC_ERROR,
1796 	SDRC_DROP_VC,
1797 	SDRC_NO_REPLY,
1798 	SDRC_SR_KEPT,
1799 	SDRC_NOT_IMPLEMENTED
1800 } smb_sdrc_t;
1801 
1802 #define	VAR_BCC		((short)-1)
1803 
1804 #define	SMB_SERVER_MAGIC	0x53534552	/* 'SSER' */
1805 #define	SMB_SERVER_VALID(s)	\
1806     ASSERT(((s) != NULL) && ((s)->sv_magic == SMB_SERVER_MAGIC))
1807 
1808 typedef struct {
1809 	kthread_t		*ld_kth;
1810 	kt_did_t		ld_ktdid;
1811 	ksocket_t		ld_so;
1812 	struct sockaddr_in	ld_sin;
1813 	struct sockaddr_in6	ld_sin6;
1814 	smb_session_list_t	ld_session_list;
1815 } smb_listener_daemon_t;
1816 
1817 #define	SMB_SSETUP_CMD			"authentication"
1818 #define	SMB_TCON_CMD			"share mapping"
1819 #define	SMB_OPIPE_CMD			"pipe open"
1820 #define	SMB_THRESHOLD_REPORT_THROTTLE	50
1821 typedef struct smb_cmd_threshold {
1822 	char			*ct_cmd;
1823 	kmutex_t		ct_mutex;
1824 	volatile uint32_t	ct_active_cnt;
1825 	volatile uint32_t	ct_blocked_cnt;
1826 	volatile uint32_t	ct_error_cnt;
1827 	uint32_t		ct_threshold;
1828 	struct smb_event	*ct_event;
1829 	uint32_t		ct_event_id;
1830 } smb_cmd_threshold_t;
1831 
1832 typedef struct {
1833 	kstat_named_t		ls_files;
1834 	kstat_named_t		ls_trees;
1835 	kstat_named_t		ls_users;
1836 } smb_server_legacy_kstat_t;
1837 
1838 typedef enum smb_server_state {
1839 	SMB_SERVER_STATE_CREATED = 0,
1840 	SMB_SERVER_STATE_CONFIGURED,
1841 	SMB_SERVER_STATE_RUNNING,
1842 	SMB_SERVER_STATE_STOPPING,
1843 	SMB_SERVER_STATE_DELETING,
1844 	SMB_SERVER_STATE_SENTINEL
1845 } smb_server_state_t;
1846 
1847 typedef struct {
1848 	kcondvar_t		sp_cv;
1849 	kmutex_t		sp_mutex;
1850 	uint32_t 		sp_cnt;
1851 	smb_llist_t		sp_list;
1852 	smb_llist_t		sp_fidlist;
1853 } smb_spool_t;
1854 
1855 #define	SMB_SERVER_STATE_VALID(S)               \
1856     ASSERT(((S) == SMB_SERVER_STATE_CREATED) || \
1857 	    ((S) == SMB_SERVER_STATE_CONFIGURED) || \
1858 	    ((S) == SMB_SERVER_STATE_RUNNING) ||    \
1859 	    ((S) == SMB_SERVER_STATE_STOPPING) ||   \
1860 	    ((S) == SMB_SERVER_STATE_DELETING))
1861 
1862 typedef struct smb_server {
1863 	uint32_t		sv_magic;
1864 	kcondvar_t		sv_cv;
1865 	kmutex_t		sv_mutex;
1866 	list_node_t		sv_lnd;
1867 	smb_server_state_t	sv_state;
1868 	uint32_t		sv_refcnt;
1869 	pid_t			sv_pid;
1870 	zoneid_t		sv_zid;
1871 	smb_listener_daemon_t	sv_nbt_daemon;
1872 	smb_listener_daemon_t	sv_tcp_daemon;
1873 	krwlock_t		sv_cfg_lock;
1874 	smb_kmod_cfg_t		sv_cfg;
1875 	smb_session_t		*sv_session;
1876 
1877 	door_handle_t		sv_lmshrd;
1878 
1879 	int32_t			si_gmtoff;
1880 
1881 	smb_thread_t		si_thread_timers;
1882 
1883 	taskq_t			*sv_thread_pool;
1884 
1885 	kmem_cache_t		*si_cache_request;
1886 	kmem_cache_t		*si_cache_session;
1887 	kmem_cache_t		*si_cache_user;
1888 	kmem_cache_t		*si_cache_tree;
1889 	kmem_cache_t		*si_cache_ofile;
1890 	kmem_cache_t		*si_cache_odir;
1891 	kmem_cache_t		*si_cache_opipe;
1892 	kmem_cache_t		*si_cache_event;
1893 
1894 	smb_node_t		*si_root_smb_node;
1895 	smb_llist_t		sv_opipe_list;
1896 	smb_llist_t		sv_event_list;
1897 
1898 	/* Statistics */
1899 	hrtime_t		sv_start_time;
1900 	kstat_t			*sv_ksp;
1901 	volatile uint32_t	sv_nbt_sess;
1902 	volatile uint32_t	sv_tcp_sess;
1903 	volatile uint32_t	sv_users;
1904 	volatile uint32_t	sv_trees;
1905 	volatile uint32_t	sv_files;
1906 	volatile uint32_t	sv_pipes;
1907 	volatile uint64_t	sv_txb;
1908 	volatile uint64_t	sv_rxb;
1909 	volatile uint64_t	sv_nreq;
1910 	smb_srqueue_t		sv_srqueue;
1911 	smb_spool_t		sp_info;
1912 	smb_cmd_threshold_t	sv_ssetup_ct;
1913 	smb_cmd_threshold_t	sv_tcon_ct;
1914 	smb_cmd_threshold_t	sv_opipe_ct;
1915 	kstat_t			*sv_legacy_ksp;
1916 	kmutex_t		sv_legacy_ksmtx;
1917 } smb_server_t;
1918 
1919 #define	SMB_EVENT_MAGIC		0x45564E54	/* EVNT */
1920 #define	SMB_EVENT_TIMEOUT	45		/* seconds */
1921 #define	SMB_EVENT_VALID(e)	\
1922     ASSERT(((e) != NULL) && ((e)->se_magic == SMB_EVENT_MAGIC))
1923 typedef struct smb_event {
1924 	uint32_t		se_magic;
1925 	list_node_t		se_lnd;
1926 	kmutex_t		se_mutex;
1927 	kcondvar_t		se_cv;
1928 	smb_server_t		*se_server;
1929 	uint32_t		se_txid;
1930 	boolean_t		se_notified;
1931 	int			se_waittime;
1932 	int			se_timeout;
1933 	int			se_errno;
1934 } smb_event_t;
1935 
1936 typedef struct smb_kspooldoc {
1937 	uint32_t	sd_magic;
1938 	list_node_t	sd_lnd;
1939 	smb_inaddr_t	sd_ipaddr;
1940 	uint32_t	sd_spool_num;
1941 	uint16_t	sd_fid;
1942 	char		sd_username[MAXNAMELEN];
1943 	char		sd_path[MAXPATHLEN];
1944 } smb_kspooldoc_t;
1945 
1946 typedef struct smb_spoolfid {
1947 	uint32_t	sf_magic;
1948 	list_node_t	sf_lnd;
1949 	uint16_t	sf_fid;
1950 } smb_spoolfid_t;
1951 
1952 #define	SMB_INFO_NETBIOS_SESSION_SVC_RUNNING	0x0001
1953 #define	SMB_INFO_NETBIOS_SESSION_SVC_FAILED	0x0002
1954 #define	SMB_INFO_USER_LEVEL_SECURITY		0x40000000
1955 #define	SMB_INFO_ENCRYPT_PASSWORDS		0x80000000
1956 
1957 #define	SMB_NEW_KID()	atomic_inc_64_nv(&smb_kids)
1958 #define	SMB_UNIQ_FID()	atomic_inc_32_nv(&smb_fids)
1959 
1960 #define	SMB_IS_STREAM(node) ((node)->n_unode)
1961 
1962 typedef struct smb_tsd {
1963 	void (*proc)();
1964 	void *arg;
1965 	char name[100];
1966 } smb_tsd_t;
1967 
1968 typedef struct smb_disp_entry {
1969 	char		sdt_name[KSTAT_STRLEN];
1970 	smb_sdrc_t	(*sdt_pre_op)(smb_request_t *);
1971 	smb_sdrc_t	(*sdt_function)(smb_request_t *);
1972 	void		(*sdt_post_op)(smb_request_t *);
1973 	uint8_t		sdt_com;
1974 	char		sdt_dialect;
1975 	uint8_t		sdt_flags;
1976 	volatile uint64_t sdt_txb;
1977 	volatile uint64_t sdt_rxb;
1978 	smb_latency_t	sdt_lat;
1979 } smb_disp_entry_t;
1980 
1981 typedef struct smb_xlate {
1982 	int	code;
1983 	char	*str;
1984 } smb_xlate_t;
1985 
1986 typedef struct smb_export {
1987 	kmutex_t	e_mutex;
1988 	boolean_t	e_ready;
1989 	smb_llist_t	e_vfs_list;
1990 	smb_avl_t	e_share_avl;
1991 	smb_slist_t	e_unexport_list;
1992 
1993 	kmem_cache_t	*e_cache_share;
1994 	kmem_cache_t	*e_cache_vfs;
1995 	kmem_cache_t	*e_cache_unexport;
1996 
1997 	smb_thread_t	e_unexport_thread;
1998 } smb_export_t;
1999 
2000 /*
2001  * This structure is a helper for building RAP NetShareEnum response
2002  *
2003  * es_posix_uid UID of the user requesting the shares list which
2004  *              is used to detect if the user has any autohome
2005  * es_bufsize   size of the response buffer
2006  * es_buf       pointer to the response buffer
2007  * es_ntotal    total number of shares exported by server which
2008  *              their OEM names is less then 13 chars
2009  * es_nsent     number of shares that can fit in the specified buffer
2010  * es_datasize  actual data size (share's data) which was encoded
2011  *              in the response buffer
2012  */
2013 typedef struct smb_enumshare_info {
2014 	uid_t		es_posix_uid;
2015 	uint16_t	es_bufsize;
2016 	char		*es_buf;
2017 	uint16_t	es_ntotal;
2018 	uint16_t	es_nsent;
2019 	uint16_t	es_datasize;
2020 } smb_enumshare_info_t;
2021 
2022 #ifdef	__cplusplus
2023 }
2024 #endif
2025 
2026 #endif /* _SMBSRV_SMB_KTYPES_H */
2027