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