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