xref: /illumos-gate/usr/src/uts/common/smbsrv/smb_ktypes.h (revision 55f0a249fd3511728b02627190771a4ce4ddf20e)
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_bucket {
395 	smb_llist_t	b_list;
396 	uint32_t	b_max_seen;
397 } smb_bucket_t;
398 
399 typedef struct smb_hash {
400 	uint32_t	rshift;
401 	uint32_t	num_buckets;
402 	smb_bucket_t	*buckets;
403 } smb_hash_t;
404 
405 typedef struct smb_slist {
406 	kmutex_t	sl_mutex;
407 	kcondvar_t	sl_cv;
408 	list_t		sl_list;
409 	uint32_t	sl_count;
410 	boolean_t	sl_waiting;
411 } smb_slist_t;
412 
413 /*
414  * smb_avl_t State Machine
415  * --------------------
416  *
417  *                      +-----------------------------+
418  *                      |     SMB_AVL_STATE_START     |
419  *                      +-----------------------------+
420  *                                    | T0
421  *                                    v
422  *                      +-----------------------------+
423  *                      |     SMB_AVL_STATE_READY     |
424  *                      +-----------------------------+
425  *                                    | T1
426  *                                    v
427  *                      +-----------------------------+
428  *                      |  SMB_AVL_STATE_DESTROYING   |
429  *                      +-----------------------------+
430  *
431  * Transition T0
432  *
433  *    This transition is executed in smb_avl_create().
434  *
435  * Transition T1
436  *
437  *    This transition is executed in smb_avl_destroy().
438  *
439  */
440 typedef enum {
441 	SMB_AVL_STATE_START = 0,
442 	SMB_AVL_STATE_READY,
443 	SMB_AVL_STATE_DESTROYING
444 } smb_avl_state_t;
445 
446 typedef struct smb_avl_nops {
447 	int		(*avln_cmp) (const void *, const void *);
448 	void		(*avln_hold)(const void *);
449 	boolean_t	(*avln_rele)(const void *);
450 	void		(*avln_destroy)(void *);
451 } smb_avl_nops_t;
452 
453 typedef struct smb_avl_cursor {
454 	void		*avlc_next;
455 	uint32_t	avlc_sequence;
456 } smb_avl_cursor_t;
457 
458 typedef struct smb_avl {
459 	krwlock_t	avl_lock;
460 	avl_tree_t	avl_tree;
461 	kmutex_t	avl_mutex;
462 	kcondvar_t	avl_cv;
463 	smb_avl_state_t	avl_state;
464 	uint32_t	avl_refcnt;
465 	uint32_t	avl_sequence;
466 	const smb_avl_nops_t	*avl_nops;
467 } smb_avl_t;
468 
469 typedef struct {
470 	kcondvar_t	rwx_cv;
471 	kmutex_t	rwx_mutex;
472 	krwlock_t	rwx_lock;
473 	boolean_t	rwx_waiting;
474 } smb_rwx_t;
475 
476 typedef struct smb_export {
477 	kmutex_t	e_mutex;
478 	boolean_t	e_ready;
479 	smb_llist_t	e_vfs_list;
480 	smb_avl_t	e_share_avl;
481 	smb_slist_t	e_unexport_list;
482 	smb_thread_t	e_unexport_thread;
483 } smb_export_t;
484 
485 /*
486  * NOTIFY CHANGE, a.k.a. File Change Notification (FCN)
487  */
488 
489 /*
490  * These FCN filter mask values are not from MS-FSCC, but
491  * must not overlap with any FILE_NOTIFY_VALID_MASK values.
492  */
493 #define	FILE_NOTIFY_CHANGE_EV_SUBDIR	0x00010000
494 #define	FILE_NOTIFY_CHANGE_EV_DELETE	0x00020000
495 #define	FILE_NOTIFY_CHANGE_EV_CLOSED	0x00040000
496 #define	FILE_NOTIFY_CHANGE_EV_OVERFLOW	0x00080000
497 
498 /*
499  * Note: These FCN action values are not from MS-FSCC, but must
500  * follow in sequence from FILE_ACTION_MODIFIED_STREAM.
501  *
502  * FILE_ACTION_SUBDIR_CHANGED is used internally for
503  * "watch tree" support, posted to all parents of a
504  * directory that had one of the changes above.
505  *
506  * FILE_ACTION_DELETE_PENDING is used internally to tell
507  * notify change requests when the "delete-on-close" flag
508  * has been set on the directory being watched.
509  *
510  * FILE_ACTION_HANDLE_CLOSED is used to wakeup notify change
511  * requests when the watched directory handle is closed.
512  */
513 #define	FILE_ACTION_SUBDIR_CHANGED	0x00000009
514 #define	FILE_ACTION_DELETE_PENDING	0x0000000a
515 #define	FILE_ACTION_HANDLE_CLOSED	0x0000000b
516 
517 /*
518  * Sub-struct within smb_ofile_t
519  */
520 typedef struct smb_notify {
521 	list_t			nc_waiters; /* Waiting SRs */
522 	mbuf_chain_t		nc_buffer;
523 	uint32_t		nc_filter;
524 	uint32_t		nc_events;
525 	int			nc_last_off;
526 	boolean_t		nc_subscribed;
527 } smb_notify_t;
528 
529 /*
530  * SMB operates over a NetBIOS-over-TCP transport (NBT) or directly
531  * over TCP, which is also known as direct hosted NetBIOS-less SMB
532  * or SMB-over-TCP.
533  *
534  * NBT messages have a 4-byte header that defines the message type
535  * (8-bits), a 7-bit flags field and a 17-bit length.
536  *
537  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
538  * |      TYPE     |     FLAGS   |E|            LENGTH             |
539  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
540  *
541  * 8-bit type      Defined in RFC 1002
542  * 7-bit flags     Bits 0-6 reserved (must be 0)
543  *                 Bit 7: Length extension bit (E)
544  * 17-bit length   Includes bit 7 of the flags byte
545  *
546  *
547  * SMB-over-TCP is defined to use a modified version of the NBT header
548  * containing an 8-bit message type and 24-bit message length.
549  *
550  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
551  * |      TYPE     |                  LENGTH                       |
552  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
553  *
554  * 8-bit type      Must be 0
555  * 24-bit length
556  *
557  * The following structure is used to represent a generic, in-memory
558  * SMB transport header; it is not intended to map directly to either
559  * of the over-the-wire formats.
560  */
561 typedef struct {
562 	uint8_t		xh_type;
563 	uint32_t	xh_length;
564 } smb_xprt_t;
565 
566 int MBC_LENGTH(struct mbuf_chain *);
567 int MBC_MAXBYTES(struct mbuf_chain *);
568 void MBC_SETUP(struct mbuf_chain *, uint32_t);
569 void MBC_INIT(struct mbuf_chain *, uint32_t);
570 void MBC_FLUSH(struct mbuf_chain *);
571 void MBC_ATTACH_MBUF(struct mbuf_chain *, struct mbuf *);
572 void MBC_APPEND_MBUF(struct mbuf_chain *, struct mbuf *);
573 void MBC_ATTACH_BUF(struct mbuf_chain *MBC, unsigned char *BUF, int LEN);
574 int MBC_SHADOW_CHAIN(struct mbuf_chain *SUBMBC, struct mbuf_chain *MBC,
575     int OFF, int LEN);
576 
577 #define	MBC_ROOM_FOR(b, n) (((b)->chain_offset + (n)) <= (b)->max_bytes)
578 
579 /*
580  * Per smb_node oplock state
581  */
582 typedef struct smb_oplock {
583 	kmutex_t		ol_mutex;
584 	boolean_t		ol_fem;		/* fem monitor installed? */
585 	struct smb_ofile	*excl_open;
586 	uint32_t		ol_state;
587 	int32_t			cnt_II;
588 	int32_t			cnt_R;
589 	int32_t			cnt_RH;
590 	int32_t			cnt_RHBQ;
591 	int32_t			waiters;
592 	kcondvar_t		WaitingOpenCV;
593 } smb_oplock_t;
594 
595 /*
596  * Per smb_ofile oplock state
597  */
598 typedef struct smb_oplock_grant {
599 	/* smb protocol-level state */
600 	uint32_t		og_state;	/* latest sent to client */
601 	uint32_t		og_breaking;	/* BREAK_TO... flags */
602 	uint16_t		og_dialect;	/* how to send breaks */
603 	/* File-system level state */
604 	uint8_t			onlist_II;
605 	uint8_t			onlist_R;
606 	uint8_t			onlist_RH;
607 	uint8_t			onlist_RHBQ;
608 	uint8_t			BreakingToRead;
609 } smb_oplock_grant_t;
610 
611 #define	SMB_LEASE_KEY_SZ	16
612 
613 typedef struct smb_lease {
614 	list_node_t		ls_lnd;		/* sv_lease_ht */
615 	kmutex_t		ls_mutex;
616 	smb_llist_t		*ls_bucket;
617 	struct smb_node		*ls_node;
618 	/*
619 	 * With a lease, just one ofile has the oplock.
620 	 * This (used only for comparison) identifies which.
621 	 */
622 	void			*ls_oplock_ofile;
623 	uint32_t		ls_refcnt;
624 	uint32_t		ls_state;
625 	uint32_t		ls_breaking;	/* BREAK_TO... flags */
626 	uint16_t		ls_epoch;
627 	uint16_t		ls_version;
628 	uint8_t			ls_key[SMB_LEASE_KEY_SZ];
629 	uint8_t			ls_clnt[SMB_LEASE_KEY_SZ];
630 } smb_lease_t;
631 
632 #define	SMB_VFS_MAGIC	0x534D4256	/* 'SMBV' */
633 
634 typedef struct smb_vfs {
635 	list_node_t		sv_lnd;
636 	uint32_t		sv_magic;
637 	uint32_t		sv_refcnt;
638 	vfs_t			*sv_vfsp;
639 	vnode_t			*sv_rootvp;
640 } smb_vfs_t;
641 
642 #define	SMB_NODE_MAGIC		0x4E4F4445	/* 'NODE' */
643 #define	SMB_NODE_VALID(p)	ASSERT((p)->n_magic == SMB_NODE_MAGIC)
644 
645 typedef enum {
646 	SMB_NODE_STATE_AVAILABLE = 0,
647 	SMB_NODE_STATE_DESTROYING
648 } smb_node_state_t;
649 
650 /*
651  * waiting_event        # of clients requesting FCN
652  * n_timestamps         cached timestamps
653  * n_allocsz            cached file allocation size
654  * n_dnode              directory node
655  * n_unode              unnamed stream node
656  * delete_on_close_cred credentials for delayed delete
657  */
658 typedef struct smb_node {
659 	list_node_t		n_lnd;
660 	uint32_t		n_magic;
661 	krwlock_t		n_lock;
662 	kmutex_t		n_mutex;
663 	smb_node_state_t	n_state;
664 	uint32_t		n_refcnt;
665 	uint32_t		n_hashkey;
666 	smb_llist_t		*n_hash_bucket;
667 	uint32_t		n_open_count;
668 	uint32_t		n_opening_count;
669 	smb_llist_t		n_ofile_list;
670 	/* If entering both, go in order n_lock_list, n_wlock_list */
671 	smb_llist_t		n_lock_list;	/* active locks */
672 	smb_llist_t		n_wlock_list;	/* waiting locks */
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_COMMITTED	0x20000000
693 #define	NODE_FLAGS_DELETE_ON_CLOSE	0x40000000
694 #define	NODE_FLAGS_EXECUTABLE		0x80000000
695 
696 #define	SMB_NODE_VFS(node)	((node)->vp->v_vfsp)
697 #define	SMB_NODE_FSID(node)	((node)->vp->v_vfsp->vfs_fsid)
698 
699 /* Maximum buffer size for encryption key */
700 #define	SMB_ENCRYPT_KEY_MAXLEN		32
701 
702 #define	SMB_SHARE_MAGIC		0x4B534852	/* KSHR */
703 
704 typedef struct smb_kshare {
705 	uint32_t	shr_magic;
706 	char		*shr_name;
707 	char		*shr_path;
708 	char		*shr_cmnt;
709 	char		*shr_container;
710 	char		*shr_oemname;
711 	uint32_t	shr_flags;
712 	uint32_t	shr_type;
713 	uint32_t	shr_refcnt;
714 	uint32_t	shr_autocnt;
715 	uid_t		shr_uid;
716 	gid_t		shr_gid;
717 	char		*shr_access_none;
718 	char		*shr_access_ro;
719 	char		*shr_access_rw;
720 	avl_node_t	shr_link;
721 	kmutex_t	shr_mutex;
722 	smb_cfg_val_t	shr_encrypt; /* Share.EncryptData */
723 } smb_kshare_t;
724 
725 
726 typedef struct smb_arg_negotiate {
727 	char		*ni_name;
728 	int		ni_dialect;
729 	int		ni_index;
730 	uint32_t	ni_capabilities;
731 	uint16_t	ni_maxmpxcount;
732 	int16_t		ni_tzcorrection;
733 	uint8_t		ni_keylen;
734 	uint8_t		ni_key[SMB_ENCRYPT_KEY_MAXLEN];
735 	timestruc_t	ni_servertime;
736 } smb_arg_negotiate_t;
737 
738 typedef enum {
739 	SMB_SSNSETUP_PRE_NTLM012 = 1,
740 	SMB_SSNSETUP_NTLM012_NOEXT,
741 	SMB_SSNSETUP_NTLM012_EXTSEC
742 } smb_ssnsetup_type_t;
743 
744 typedef struct smb_arg_sessionsetup {
745 	smb_ssnsetup_type_t ssi_type;
746 	char		*ssi_user;
747 	char		*ssi_domain;
748 	/* LM password hash, f.k.a. case-insensitive p/w */
749 	uint16_t	ssi_lmpwlen;
750 	uint8_t		*ssi_lmpwd;
751 	/* NT password hash, f.k.a. case-sensitive p/w */
752 	uint16_t	ssi_ntpwlen;
753 	uint8_t		*ssi_ntpwd;
754 	/* Incoming security blob */
755 	uint16_t	ssi_iseclen;
756 	uint8_t		*ssi_isecblob;
757 	/* Incoming security blob */
758 	uint16_t	ssi_oseclen;
759 	uint8_t		*ssi_osecblob;
760 	/* parameters */
761 	uint16_t	ssi_maxbufsize;
762 	uint16_t	ssi_maxmpxcount;
763 	uint32_t	ssi_capabilities;
764 	int		ssi_native_os;
765 	int		ssi_native_lm;
766 } smb_arg_sessionsetup_t;
767 
768 typedef struct tcon {
769 	char		*name;
770 	char		*path;
771 	char		*service;
772 	int		pwdlen;
773 	char		*password;
774 	uint16_t	flags;
775 	uint16_t	optional_support;
776 	smb_kshare_t	*si;
777 } smb_arg_tcon_t;
778 
779 /*
780  * Based on section 2.6.1.2 (Connection Management) of the June 13,
781  * 1996 CIFS spec, a server may terminate the transport connection
782  * due to inactivity. The client software is expected to be able to
783  * automatically reconnect to the server if this happens. Like much
784  * of the useful background information, this section appears to
785  * have been dropped from later revisions of the document.
786  *
787  * Each session has an activity timestamp that's updated whenever a
788  * request is dispatched. If the session is idle, i.e. receives no
789  * requests, for SMB_SESSION_INACTIVITY_TIMEOUT minutes it will be
790  * closed.
791  *
792  * Each session has an I/O semaphore to serialize communication with
793  * the client. For example, after receiving a raw-read request, the
794  * server is not allowed to send an oplock break to the client until
795  * after it has sent the raw-read data.
796  */
797 #define	SMB_SESSION_INACTIVITY_TIMEOUT		(15 * 60)
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  * Locking notes:
820  * If you hold the mutex/lock on an object, don't flush the deleteq
821  * of the objects directly below it in the logical hierarchy
822  * (i.e. via smb_llist_exit()). I.e. don't drop s_tree_list when
823  * you hold u_mutex, because deleted trees need u_mutex to
824  * lower the refcnt.
825  *
826  * Note that this also applies to u_mutex and t_ofile_list.
827  */
828 
829 /*
830  * The "session" object.
831  *
832  * Note that the smb_session_t object here corresponds to what MS-SMB2
833  * calls a "connection".  Adding to the confusion, what MS calls a
834  * "session" corresponds to our smb_user_t (below).
835  */
836 
837 /*
838  * Session State Machine
839  * ---------------------
840  *
841  *
842  * +-----------------------------+	    +----------------------------+
843  * | SMB_SESSION_STATE_CONNECTED |	    | SMB_SESSION_STATE_SHUTDOWN |
844  * +-----------------------------+	    +----------------------------+
845  *		  |					     ^
846  *		  |					     |T6
847  *		  |			    +------------------------------+
848  *		  |			    | SMB_SESSION_STATE_TERMINATED |
849  *		T0|			    +------------------------------+
850  *		  +--------------------+		     ^
851  *		  v		       |T4                   |T5
852  * +-------------------------------+   |    +--------------------------------+
853  * | SMB_SESSION_STATE_ESTABLISHED |---+--->| SMB_SESSION_STATE_DISCONNECTED |
854  * +-------------------------------+        +--------------------------------+
855  *		T1|				^
856  *		  +----------+			|T3
857  *                           v			|
858  *                  +------------------------------+
859  *                  | SMB_SESSION_STATE_NEGOTIATED |
860  *                  +------------------------------+
861  *
862  *
863  * Transition T0
864  *
865  *
866  *
867  * Transition T1
868  *
869  *
870  *
871  * Transition T2
872  *
873  *
874  *
875  * Transition T3
876  *
877  *
878  *
879  * Transition T4
880  *
881  *
882  *
883  * Transition T5
884  *
885  *
886  *
887  * Transition T6
888  *
889  *
890  *
891  */
892 #define	SMB_SESSION_MAGIC	0x53455353	/* 'SESS' */
893 #define	SMB_SESSION_VALID(p)	\
894     ASSERT(((p) != NULL) && ((p)->s_magic == SMB_SESSION_MAGIC))
895 
896 #define	SMB_CHALLENGE_SZ	8
897 
898 typedef enum {
899 	SMB_SESSION_STATE_INITIALIZED = 0,
900 	SMB_SESSION_STATE_DISCONNECTED,
901 	SMB_SESSION_STATE_CONNECTED,
902 	SMB_SESSION_STATE_ESTABLISHED,
903 	SMB_SESSION_STATE_NEGOTIATED,
904 	SMB_SESSION_STATE_TERMINATED,
905 	SMB_SESSION_STATE_SHUTDOWN,
906 	SMB_SESSION_STATE_SENTINEL
907 } smb_session_state_t;
908 
909 /* Bits in s_flags below */
910 #define	SMB_SSN_AAPL_CCEXT	1	/* Saw "AAPL" create ctx. ext. */
911 #define	SMB_SSN_AAPL_READDIR	2	/* Wants MacOS ext. readdir */
912 
913 typedef struct smb_session {
914 	list_node_t		s_lnd;
915 	uint32_t		s_magic;
916 	smb_rwx_t		s_lock;
917 	uint64_t		s_kid;
918 	smb_session_state_t	s_state;
919 	uint32_t		s_flags;
920 	taskqid_t		s_receiver_tqid;
921 	kthread_t		*s_thread;
922 	kt_did_t		s_ktdid;
923 	int	(*newrq_func)(struct smb_request *);
924 	struct smb_server	*s_server;
925 	smb_kmod_cfg_t		s_cfg;
926 	int32_t			s_gmtoff;
927 	uint32_t		keep_alive;
928 	uint64_t		opentime;
929 	uint16_t		s_local_port;
930 	uint16_t		s_remote_port;
931 	smb_inaddr_t		ipaddr;
932 	smb_inaddr_t		local_ipaddr;
933 	int			dialect;
934 	int			native_os;
935 	int			native_lm;
936 
937 	kmutex_t		s_credits_mutex;
938 	uint16_t		s_cur_credits;
939 	uint16_t		s_max_credits;
940 
941 	uint32_t		capabilities;
942 	uint32_t		srv_cap;
943 
944 	struct smb_sign		signing;	/* SMB1 */
945 	void			*sign_mech;	/* mechanism info */
946 	void			*enc_mech;
947 
948 	/* SMB2/SMB3 signing support */
949 	int			(*sign_calc)(struct smb_request *,
950 					struct mbuf_chain *, uint8_t *);
951 	void			(*sign_fini)(struct smb_session *);
952 
953 	ksocket_t		sock;
954 
955 	smb_slist_t		s_req_list;
956 	smb_llist_t		s_xa_list;
957 	smb_llist_t		s_user_list;
958 	smb_llist_t		s_tree_list;
959 	smb_idpool_t		s_uid_pool;
960 	smb_idpool_t		s_tid_pool;
961 	smb_txlst_t		s_txlst;
962 
963 	volatile uint32_t	s_tree_cnt;
964 	volatile uint32_t	s_file_cnt;
965 	volatile uint32_t	s_dir_cnt;
966 
967 	uint16_t		cli_secmode;
968 	uint16_t		srv_secmode;
969 	uint32_t		sesskey;
970 	uint32_t		challenge_len;
971 	unsigned char		challenge_key[SMB_CHALLENGE_SZ];
972 	int64_t			activity_timestamp;
973 
974 	/*
975 	 * Maximum negotiated buffer sizes between SMB client and server
976 	 * in SMB_SESSION_SETUP_ANDX
977 	 */
978 	int			cmd_max_bytes;
979 	int			reply_max_bytes;
980 	uint16_t		smb_msg_size;
981 	uint16_t		smb_max_mpx;
982 	smb_srqueue_t		*s_srqueue;
983 	uint64_t		start_time;
984 	unsigned char		MAC_key[44];
985 	char			ip_addr_str[INET6_ADDRSTRLEN];
986 	uint8_t			clnt_uuid[16];
987 	char 			workstation[SMB_PI_MAX_HOST];
988 } smb_session_t;
989 
990 /*
991  * The "user" object.
992  *
993  * Note that smb_user_t object here corresponds to what MS-SMB2 calls
994  * a "session".  (Our smb_session_t is something else -- see above).
995  */
996 
997 #define	SMB_USER_MAGIC 0x55534552	/* 'USER' */
998 #define	SMB_USER_VALID(u)	\
999     ASSERT(((u) != NULL) && ((u)->u_magic == SMB_USER_MAGIC))
1000 
1001 /* These flags are all <= 0x00000010 */
1002 #define	SMB_USER_FLAG_GUEST			SMB_ATF_GUEST
1003 #define	SMB_USER_FLAG_ANON			SMB_ATF_ANON
1004 #define	SMB_USER_FLAG_ADMIN			SMB_ATF_ADMIN
1005 #define	SMB_USER_FLAG_POWER_USER		SMB_ATF_POWERUSER
1006 #define	SMB_USER_FLAG_BACKUP_OPERATOR		SMB_ATF_BACKUPOP
1007 
1008 #define	SMB_USER_IS_ADMIN(U)	(((U)->u_flags & SMB_USER_FLAG_ADMIN) != 0)
1009 #define	SMB_USER_IS_GUEST(U)	(((U)->u_flags & SMB_USER_FLAG_GUEST) != 0)
1010 
1011 #define	SMB_USER_PRIV_TAKE_OWNERSHIP	0x00000001
1012 #define	SMB_USER_PRIV_BACKUP		0x00000002
1013 #define	SMB_USER_PRIV_RESTORE		0x00000004
1014 #define	SMB_USER_PRIV_SECURITY		0x00000008
1015 
1016 /*
1017  * See the long "User State Machine" comment in smb_user.c
1018  */
1019 typedef enum {
1020 	SMB_USER_STATE_LOGGING_ON = 0,
1021 	SMB_USER_STATE_LOGGED_ON,
1022 	SMB_USER_STATE_LOGGING_OFF,
1023 	SMB_USER_STATE_LOGGED_OFF,
1024 	SMB_USER_STATE_SENTINEL
1025 } smb_user_state_t;
1026 
1027 typedef enum {
1028 	SMB2_DH_PRESERVE_NONE = 0,
1029 	SMB2_DH_PRESERVE_SOME,
1030 	SMB2_DH_PRESERVE_ALL
1031 } smb_preserve_type_t;
1032 
1033 typedef struct smb_user {
1034 	list_node_t		u_lnd;
1035 	uint32_t		u_magic;
1036 	kmutex_t		u_mutex;
1037 	smb_user_state_t	u_state;
1038 
1039 	struct smb_server	*u_server;
1040 	smb_session_t		*u_session;
1041 	ksocket_t		u_authsock;
1042 	timeout_id_t		u_auth_tmo;
1043 	uint16_t		u_name_len;
1044 	char			*u_name;
1045 	uint16_t		u_domain_len;
1046 	char			*u_domain;
1047 	time_t			u_logon_time;
1048 	cred_t			*u_cred;
1049 	cred_t			*u_privcred;
1050 
1051 	uint64_t		u_ssnid;	/* unique server-wide */
1052 	uint32_t		u_refcnt;
1053 	uint32_t		u_flags;
1054 	smb_preserve_type_t	preserve_opens;
1055 	uint32_t		u_privileges;
1056 	uint16_t		u_uid;		/* unique per-session */
1057 	uint32_t		u_audit_sid;
1058 
1059 	uint32_t		u_sign_flags;
1060 	struct smb_key		u_sign_key;	/* SMB2 signing */
1061 
1062 	struct smb_key		u_enc_key;
1063 	struct smb_key		u_dec_key;
1064 	volatile uint64_t	u_nonce_cnt;
1065 	uint8_t			u_nonce_fixed[4];
1066 	uint64_t		u_salt;
1067 	smb_cfg_val_t		u_encrypt;
1068 } smb_user_t;
1069 
1070 #define	SMB_TREE_MAGIC			0x54524545	/* 'TREE' */
1071 #define	SMB_TREE_VALID(p)	\
1072     ASSERT((p != NULL) && ((p)->t_magic == SMB_TREE_MAGIC))
1073 
1074 #define	SMB_TYPENAMELEN			_ST_FSTYPSZ
1075 #define	SMB_VOLNAMELEN			32
1076 
1077 #define	SMB_TREE_READONLY		0x00000001
1078 #define	SMB_TREE_SUPPORTS_ACLS		0x00000002
1079 #define	SMB_TREE_STREAMS		0x00000004
1080 #define	SMB_TREE_CASEINSENSITIVE	0x00000008
1081 #define	SMB_TREE_NO_CASESENSITIVE	0x00000010
1082 #define	SMB_TREE_NO_EXPORT		0x00000020
1083 #define	SMB_TREE_OPLOCKS		0x00000040
1084 #define	SMB_TREE_SHORTNAMES		0x00000080
1085 #define	SMB_TREE_XVATTR			0x00000100
1086 #define	SMB_TREE_DIRENTFLAGS		0x00000200
1087 #define	SMB_TREE_ACLONCREATE		0x00000400
1088 #define	SMB_TREE_ACEMASKONACCESS	0x00000800
1089 #define	SMB_TREE_NFS_MOUNTED		0x00001000
1090 #define	SMB_TREE_UNICODE_ON_DISK	0x00002000
1091 #define	SMB_TREE_CATIA			0x00004000
1092 #define	SMB_TREE_ABE			0x00008000
1093 #define	SMB_TREE_QUOTA			0x00010000
1094 #define	SMB_TREE_DFSROOT		0x00020000
1095 #define	SMB_TREE_SPARSE			0x00040000
1096 #define	SMB_TREE_TRAVERSE_MOUNTS	0x00080000
1097 #define	SMB_TREE_FORCE_L2_OPLOCK	0x00100000
1098 /* Note: SMB_TREE_... in the mdb module too. */
1099 
1100 /*
1101  * See the long "Tree State Machine" comment in smb_tree.c
1102  */
1103 typedef enum {
1104 	SMB_TREE_STATE_CONNECTED = 0,
1105 	SMB_TREE_STATE_DISCONNECTING,
1106 	SMB_TREE_STATE_DISCONNECTED,
1107 	SMB_TREE_STATE_SENTINEL
1108 } smb_tree_state_t;
1109 
1110 typedef struct smb_tree {
1111 	list_node_t		t_lnd;
1112 	uint32_t		t_magic;
1113 	kmutex_t		t_mutex;
1114 	smb_tree_state_t	t_state;
1115 
1116 	struct smb_server	*t_server;
1117 	smb_session_t		*t_session;
1118 	/*
1119 	 * user whose uid was in the tree connect message
1120 	 * ("owner" in MS-CIFS parlance, see section 2.2.1.6 definition of FID)
1121 	 */
1122 	smb_user_t		*t_owner;
1123 	smb_node_t		*t_snode;
1124 
1125 	smb_llist_t		t_ofile_list;
1126 	smb_idpool_t		t_fid_pool;
1127 
1128 	smb_llist_t		t_odir_list;
1129 	smb_idpool_t		t_odid_pool;
1130 
1131 	uint32_t		t_refcnt;
1132 	uint32_t		t_flags;
1133 	int32_t			t_res_type;
1134 	uint16_t		t_tid;
1135 	uint16_t		t_umask;
1136 	char			t_sharename[MAXNAMELEN];
1137 	char			t_resource[MAXPATHLEN];
1138 	char			t_typename[SMB_TYPENAMELEN];
1139 	char			t_volume[SMB_VOLNAMELEN];
1140 	acl_type_t		t_acltype;
1141 	uint32_t		t_access;
1142 	uint32_t		t_execflags;
1143 	time_t			t_connect_time;
1144 	volatile uint32_t	t_open_files;
1145 	smb_cfg_val_t		t_encrypt; /* Share.EncryptData */
1146 } smb_tree_t;
1147 
1148 #define	SMB_TREE_VFS(tree)	((tree)->t_snode->vp->v_vfsp)
1149 #define	SMB_TREE_FSID(tree)	((tree)->t_snode->vp->v_vfsp->vfs_fsid)
1150 
1151 #define	SMB_TREE_IS_READONLY(sr)					\
1152 	((sr) != NULL && (sr)->tid_tree != NULL &&			\
1153 	!((sr)->tid_tree->t_access & ACE_ALL_WRITE_PERMS))
1154 
1155 #define	SMB_TREE_IS_CASEINSENSITIVE(sr)                                 \
1156 	(((sr) && (sr)->tid_tree) ?                                     \
1157 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CASEINSENSITIVE) : 0)
1158 
1159 #define	SMB_TREE_HAS_ACCESS(sr, acemask)				\
1160 	((sr) == NULL ? ACE_ALL_PERMS : (				\
1161 	(((sr) && (sr)->tid_tree) ?					\
1162 	(((sr)->tid_tree->t_access) & (acemask)) : 0)))
1163 
1164 #define	SMB_TREE_SUPPORTS_CATIA(sr)            				\
1165 	(((sr) && (sr)->tid_tree) ?                                     \
1166 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CATIA) : 0)
1167 
1168 #define	SMB_TREE_SUPPORTS_ABE(sr)            				\
1169 	(((sr) && (sr)->tid_tree) ?                                     \
1170 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_ABE) : 0)
1171 
1172 #define	SMB_TREE_IS_DFSROOT(sr)            				\
1173 	(((sr) && (sr)->tid_tree) ?                                     \
1174 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_DFSROOT) : 0)
1175 
1176 #define	SMB_TREE_SUPPORTS_SHORTNAMES(sr)				\
1177 	(((sr) && (sr)->tid_tree) ?					\
1178 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_SHORTNAMES) : 0)
1179 
1180 /*
1181  * SMB_TREE_CONTAINS_NODE is used to check if a node is on the same
1182  * file system as the tree's root filesystem, or if mount point traversal
1183  * should be allowed.  Note that this is also called in some cases with
1184  * sr=NULL, where it is expected to evaluate to TRUE.
1185  */
1186 
1187 #define	SMB_TREE_CONTAINS_NODE(sr, node)                                \
1188 	((sr) == NULL || (sr)->tid_tree == NULL ||                      \
1189 	SMB_TREE_VFS((sr)->tid_tree) == SMB_NODE_VFS(node) ||           \
1190 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_TRAVERSE_MOUNTS))
1191 
1192 /*
1193  * SMB_PATHFILE_IS_READONLY indicates whether or not a file is
1194  * readonly when the caller has a path rather than an ofile.
1195  */
1196 #define	SMB_PATHFILE_IS_READONLY(sr, node)			\
1197 	(SMB_TREE_IS_READONLY((sr)) ||				\
1198 	smb_node_file_is_readonly((node)))
1199 
1200 #define	SMB_ODIR_MAGIC 		0x4F444952	/* 'ODIR' */
1201 #define	SMB_ODIR_VALID(p)	\
1202     ASSERT((p != NULL) && ((p)->d_magic == SMB_ODIR_MAGIC))
1203 
1204 #define	SMB_ODIR_BUFSIZE	(8 * 1024)
1205 
1206 #define	SMB_ODIR_FLAG_WILDCARDS		0x0001
1207 #define	SMB_ODIR_FLAG_IGNORE_CASE	0x0002
1208 #define	SMB_ODIR_FLAG_XATTR		0x0004
1209 #define	SMB_ODIR_FLAG_EDIRENT		0x0008
1210 #define	SMB_ODIR_FLAG_CATIA		0x0010
1211 #define	SMB_ODIR_FLAG_ABE		0x0020
1212 #define	SMB_ODIR_FLAG_SHORTNAMES	0x0040
1213 
1214 typedef enum {
1215 	SMB_ODIR_STATE_OPEN = 0,
1216 	SMB_ODIR_STATE_IN_USE,
1217 	SMB_ODIR_STATE_CLOSING,
1218 	SMB_ODIR_STATE_CLOSED,
1219 	SMB_ODIR_STATE_SENTINEL
1220 } smb_odir_state_t;
1221 
1222 typedef enum {
1223 	SMB_ODIR_RESUME_CONT,
1224 	SMB_ODIR_RESUME_IDX,
1225 	SMB_ODIR_RESUME_COOKIE,
1226 	SMB_ODIR_RESUME_FNAME
1227 } smb_odir_resume_type_t;
1228 
1229 typedef struct smb_odir_resume {
1230 	smb_odir_resume_type_t	or_type;
1231 	int			or_idx;
1232 	uint32_t		or_cookie;
1233 	char			*or_fname;
1234 } smb_odir_resume_t;
1235 
1236 /*
1237  * Flags used when opening an odir
1238  */
1239 #define	SMB_ODIR_OPENF_BACKUP_INTENT	0x01
1240 
1241 typedef struct smb_odir {
1242 	list_node_t		d_lnd;
1243 	uint32_t		d_magic;
1244 	kmutex_t		d_mutex;
1245 	smb_odir_state_t	d_state;
1246 	smb_session_t		*d_session;
1247 	smb_user_t		*d_user;
1248 	smb_tree_t		*d_tree;
1249 	smb_node_t		*d_dnode;
1250 	cred_t			*d_cred;
1251 	uint32_t		d_opened_by_pid;
1252 	uint16_t		d_odid;
1253 	uint16_t		d_sattr;
1254 	uint32_t		d_refcnt;
1255 	uint32_t		d_flags;
1256 	boolean_t		d_eof;
1257 	int			d_bufsize;
1258 	uint64_t		d_offset;
1259 	union {
1260 		char		*u_bufptr;
1261 		struct edirent	*u_edp;
1262 		struct dirent64	*u_dp;
1263 	} d_u;
1264 	uint32_t		d_last_cookie;
1265 	uint32_t		d_cookies[SMB_MAX_SEARCH];
1266 	char			d_pattern[MAXNAMELEN];
1267 	char			d_buf[SMB_ODIR_BUFSIZE];
1268 	char			d_last_name[MAXNAMELEN];
1269 } smb_odir_t;
1270 #define	d_bufptr	d_u.u_bufptr
1271 #define	d_edp		d_u.u_edp
1272 #define	d_dp		d_u.u_dp
1273 
1274 typedef struct smb_odirent {
1275 	char		od_name[MAXNAMELEN];	/* on disk name */
1276 	ino64_t		od_ino;
1277 	uint32_t	od_eflags;
1278 } smb_odirent_t;
1279 
1280 #define	SMB_OPIPE_MAGIC		0x50495045	/* 'PIPE' */
1281 #define	SMB_OPIPE_VALID(p)	\
1282     ASSERT(((p) != NULL) && (p)->p_magic == SMB_OPIPE_MAGIC)
1283 #define	SMB_OPIPE_MAXNAME	32
1284 
1285 /*
1286  * Data structure for SMB_FTYPE_MESG_PIPE ofiles, which is used
1287  * at the interface between SMB and NDR RPC.
1288  */
1289 typedef struct smb_opipe {
1290 	uint32_t		p_magic;
1291 	kmutex_t		p_mutex;
1292 	kcondvar_t		p_cv;
1293 	struct smb_ofile	*p_ofile;
1294 	struct smb_server	*p_server;
1295 	uint32_t		p_refcnt;
1296 	ksocket_t		p_socket;
1297 	/* This is the "flat" name, without path prefix */
1298 	char			p_name[SMB_OPIPE_MAXNAME];
1299 } smb_opipe_t;
1300 
1301 /*
1302  * The of_ftype	of an open file should contain the SMB_FTYPE value
1303  * returned when the file/pipe was opened. The following
1304  * assumptions are currently made:
1305  *
1306  * File Type	    Node       PipeInfo
1307  * ---------	    --------   --------
1308  * SMB_FTYPE_DISK       Valid      Null
1309  * SMB_FTYPE_BYTE_PIPE  Undefined  Undefined
1310  * SMB_FTYPE_MESG_PIPE  Null       Valid
1311  * SMB_FTYPE_PRINTER    Undefined  Undefined
1312  * SMB_FTYPE_UNKNOWN    Undefined  Undefined
1313  */
1314 
1315 /*
1316  * Some flags for ofile structure
1317  *
1318  *	SMB_OFLAGS_SET_DELETE_ON_CLOSE
1319  *   Set this flag when the corresponding open operation whose
1320  *   DELETE_ON_CLOSE bit of the CreateOptions is set. If any
1321  *   open file instance has this bit set, the NODE_FLAGS_DELETE_ON_CLOSE
1322  *   will be set for the file node upon close.
1323  */
1324 
1325 /*	SMB_OFLAGS_READONLY		0x0001 (obsolete) */
1326 #define	SMB_OFLAGS_EXECONLY		0x0002
1327 #define	SMB_OFLAGS_SET_DELETE_ON_CLOSE	0x0004
1328 #define	SMB_OFLAGS_LLF_POS_VALID	0x0008
1329 
1330 #define	SMB_OFILE_MAGIC 	0x4F464C45	/* 'OFLE' */
1331 #define	SMB_OFILE_VALID(p)	\
1332     ASSERT((p != NULL) && ((p)->f_magic == SMB_OFILE_MAGIC))
1333 
1334 /*
1335  * This is the size of the per-handle "Lock Sequence" array.
1336  * See LockSequenceIndex in [MS-SMB2] 2.2.26, and smb2_lock.c
1337  */
1338 #define	SMB_OFILE_LSEQ_MAX		64
1339 
1340 /* {arg_open,ofile}->dh_vers values */
1341 typedef enum {
1342 	SMB2_NOT_DURABLE = 0,
1343 	SMB2_DURABLE_V1,
1344 	SMB2_DURABLE_V2,
1345 	SMB2_RESILIENT,
1346 } smb_dh_vers_t;
1347 
1348 /*
1349  * See the long "Ofile State Machine" comment in smb_ofile.c
1350  */
1351 typedef enum {
1352 	SMB_OFILE_STATE_ALLOC = 0,
1353 	SMB_OFILE_STATE_OPEN,
1354 	SMB_OFILE_STATE_SAVE_DH,
1355 	SMB_OFILE_STATE_SAVING,
1356 	SMB_OFILE_STATE_CLOSING,
1357 	SMB_OFILE_STATE_CLOSED,
1358 	SMB_OFILE_STATE_ORPHANED,
1359 	SMB_OFILE_STATE_RECONNECT,
1360 	SMB_OFILE_STATE_EXPIRED,
1361 	SMB_OFILE_STATE_SENTINEL
1362 } smb_ofile_state_t;
1363 
1364 typedef struct smb_ofile {
1365 	list_node_t		f_tree_lnd;	/* t_ofile_list */
1366 	list_node_t		f_node_lnd;	/* n_ofile_list */
1367 	list_node_t		f_dh_lnd;	/* sv_persistid_ht */
1368 	uint32_t		f_magic;
1369 	kmutex_t		f_mutex;
1370 	smb_ofile_state_t	f_state;
1371 
1372 	struct smb_server	*f_server;
1373 	smb_session_t		*f_session;
1374 	smb_user_t		*f_user;
1375 	smb_tree_t		*f_tree;
1376 	smb_node_t		*f_node;
1377 	smb_odir_t		*f_odir;
1378 	smb_opipe_t		*f_pipe;
1379 
1380 	kcondvar_t		f_cv;
1381 	/*
1382 	 * Note: f_persistid == 0 means this ofile has no persistid
1383 	 * (same interpretation at the protocol level).  IFF non-zero,
1384 	 * this ofile is linked in the sv_persistid_ht hash table.
1385 	 */
1386 	uint64_t		f_persistid;
1387 	uint32_t		f_uniqid;
1388 	uint32_t		f_refcnt;
1389 	uint64_t		f_seek_pos;
1390 	uint32_t		f_flags;
1391 	uint32_t		f_granted_access;
1392 	uint32_t		f_share_access;
1393 	uint32_t		f_create_options;
1394 	uint32_t		f_opened_by_pid;
1395 	uint16_t		f_fid;
1396 	uint16_t		f_ftype;
1397 	uint64_t		f_llf_pos;
1398 	int			f_mode;
1399 	cred_t			*f_cr;
1400 	pid_t			f_pid;
1401 	smb_attr_t		f_pending_attr;
1402 	boolean_t		f_written;
1403 	smb_oplock_grant_t	f_oplock;
1404 	uint8_t			TargetOplockKey[SMB_LEASE_KEY_SZ];
1405 	uint8_t			ParentOplockKey[SMB_LEASE_KEY_SZ];
1406 	struct smb_lease	*f_lease;
1407 
1408 	smb_notify_t		f_notify;
1409 
1410 	smb_dh_vers_t		dh_vers;
1411 	hrtime_t		dh_timeout_offset; /* time offset for timeout */
1412 	hrtime_t		dh_expire_time; /* time the handle expires */
1413 	boolean_t		dh_persist;
1414 	uint8_t			dh_create_guid[16];
1415 	char			f_quota_resume[SMB_SID_STRSZ];
1416 	uint8_t			f_lock_seq[SMB_OFILE_LSEQ_MAX];
1417 } smb_ofile_t;
1418 
1419 typedef struct smb_fileinfo {
1420 	char		fi_name[MAXNAMELEN];
1421 	char		fi_shortname[SMB_SHORTNAMELEN];
1422 	uint32_t	fi_cookie;	/* Dir offset (of next entry) */
1423 	uint32_t	fi_dosattr;	/* DOS attributes */
1424 	uint64_t	fi_nodeid;	/* file system node id */
1425 	uint64_t	fi_size;	/* file size in bytes */
1426 	uint64_t	fi_alloc_size;	/* allocation size in bytes */
1427 	timestruc_t	fi_atime;	/* last access */
1428 	timestruc_t	fi_mtime;	/* last modification */
1429 	timestruc_t	fi_ctime;	/* last status change */
1430 	timestruc_t	fi_crtime;	/* file creation */
1431 } smb_fileinfo_t;
1432 
1433 typedef struct smb_streaminfo {
1434 	uint64_t	si_size;
1435 	uint64_t	si_alloc_size;
1436 	char		si_name[MAXPATHLEN];
1437 } smb_streaminfo_t;
1438 
1439 #define	SMB_LOCK_MAGIC 	0x4C4F434B	/* 'LOCK' */
1440 
1441 typedef struct smb_lock {
1442 	list_node_t		l_lnd;
1443 	uint32_t		l_magic;
1444 	kmutex_t		l_mutex;
1445 	kcondvar_t		l_cv;
1446 
1447 	smb_ofile_t		*l_file;
1448 
1449 	struct smb_lock		*l_blocked_by; /* Debug info only */
1450 
1451 	uint32_t		l_conflicts;
1452 	uint32_t		l_flags;
1453 	uint32_t		l_pid;
1454 	uint32_t		l_type;
1455 	uint64_t		l_start;
1456 	uint64_t		l_length;
1457 	clock_t			l_end_time;
1458 } smb_lock_t;
1459 
1460 #define	SMB_LOCK_FLAG_INDEFINITE	0x0004
1461 #define	SMB_LOCK_FLAG_CLOSED		0x0008
1462 #define	SMB_LOCK_FLAG_CANCELLED		0x0010
1463 
1464 #define	SMB_LOCK_TYPE_READWRITE		101
1465 #define	SMB_LOCK_TYPE_READONLY		102
1466 
1467 typedef struct vardata_block {
1468 	uint8_t			vdb_tag;
1469 	uint32_t		vdb_len;
1470 	struct uio 		vdb_uio;
1471 	struct iovec		vdb_iovec[MAX_IOVEC];
1472 } smb_vdb_t;
1473 
1474 #define	SMB_WRMODE_WRITE_THRU	0x0001
1475 #define	SMB_WRMODE_IS_STABLE(M)	((M) & SMB_WRMODE_WRITE_THRU)
1476 
1477 #define	SMB_RW_MAGIC		0x52445257	/* 'RDRW' */
1478 
1479 typedef struct smb_rw_param {
1480 	uint32_t rw_magic;
1481 	smb_vdb_t rw_vdb;
1482 	uint64_t rw_offset;
1483 	uint32_t rw_last_write;
1484 	uint16_t rw_mode;
1485 	uint32_t rw_count;		/* bytes in this request */
1486 	uint16_t rw_mincnt;
1487 	uint32_t rw_total;		/* total bytes (write-raw) */
1488 	uint16_t rw_dsoff;		/* SMB data offset */
1489 	uint8_t rw_andx;		/* SMB secondary andx command */
1490 } smb_rw_param_t;
1491 
1492 typedef struct smb_pathname {
1493 	char	*pn_path;
1494 	char	*pn_pname;
1495 	char	*pn_fname;
1496 	char	*pn_sname;
1497 	char	*pn_stype;
1498 } smb_pathname_t;
1499 
1500 /*
1501  * fs_query_info
1502  */
1503 typedef struct smb_fqi {
1504 	smb_pathname_t	fq_path;
1505 	uint16_t	fq_sattr;
1506 	smb_node_t	*fq_dnode;
1507 	smb_node_t	*fq_fnode;
1508 	smb_attr_t	fq_fattr;
1509 	char		fq_last_comp[MAXNAMELEN];
1510 } smb_fqi_t;
1511 
1512 typedef struct dirop {
1513 	smb_fqi_t	fqi;
1514 	smb_fqi_t	dst_fqi;
1515 	uint16_t	info_level;
1516 	uint16_t	flags;
1517 } smb_arg_dirop_t;
1518 
1519 typedef struct smb_queryinfo {
1520 	smb_node_t	*qi_node;	/* NULL for pipes */
1521 	uint8_t qi_InfoType;
1522 	uint8_t qi_InfoClass;
1523 	uint8_t	qi_delete_on_close;
1524 	uint8_t qi_isdir;
1525 	uint32_t qi_AddlInfo;
1526 	uint32_t qi_Flags;
1527 	mbuf_chain_t in_data;
1528 	smb_attr_t	qi_attr;
1529 	uint32_t	qi_namelen;
1530 	char		qi_shortname[SMB_SHORTNAMELEN];
1531 	char		qi_name[MAXPATHLEN];
1532 } smb_queryinfo_t;
1533 
1534 typedef struct smb_setinfo {
1535 	smb_node_t *si_node;
1536 	mbuf_chain_t si_data;
1537 	smb_attr_t si_attr;
1538 } smb_setinfo_t;
1539 
1540 /*
1541  * smb_fssize_t
1542  * volume_units and volume avail are the total allocated and
1543  * available units on the volume.
1544  * caller_units and caller_avail are the allocated and available
1545  * units on the volume for the user associated with the calling
1546  * thread.
1547  */
1548 typedef struct smb_fssize {
1549 	uint64_t	fs_volume_units;
1550 	uint64_t	fs_volume_avail;
1551 	uint64_t	fs_caller_units;
1552 	uint64_t	fs_caller_avail;
1553 	uint32_t	fs_sectors_per_unit;
1554 	uint32_t	fs_bytes_per_sector;
1555 } smb_fssize_t;
1556 
1557 /*
1558  * SMB FsCtl operations (SMB2 Ioctl, and some SMB1 trans calls)
1559  */
1560 typedef struct {
1561 	uint32_t CtlCode;
1562 	uint32_t InputCount;
1563 	uint32_t OutputCount;
1564 	uint32_t MaxOutputResp;
1565 	mbuf_chain_t *in_mbc;
1566 	mbuf_chain_t *out_mbc;
1567 } smb_fsctl_t;
1568 
1569 typedef struct {
1570 	uint64_t	persistent;
1571 	uint64_t	temporal;
1572 } smb2fid_t;
1573 
1574 typedef struct {
1575 	uint32_t status;
1576 	uint16_t errcls;
1577 	uint16_t errcode;
1578 } smb_error_t;
1579 
1580 typedef struct open_param {
1581 	smb_fqi_t	fqi;
1582 	uint16_t	omode;
1583 	uint16_t	ofun;
1584 	uint32_t	nt_flags;
1585 	uint32_t	timeo;
1586 	uint32_t	dattr;
1587 	timestruc_t	crtime;
1588 	timestruc_t	mtime;
1589 	timestruc_t	timewarp;
1590 	/*
1591 	 * Careful: dsize is the desired (allocation) size before the
1592 	 * common open function, and the actual size afterwards.
1593 	 */
1594 	uint64_t	dsize;	/* alloc size, actual size */
1595 	uint32_t	desired_access;
1596 	uint32_t	maximum_access;
1597 	uint32_t	share_access;
1598 	uint32_t	create_options;
1599 	uint32_t	create_disposition;
1600 	boolean_t	create_timewarp;
1601 	boolean_t	created_readonly;
1602 	uint32_t	ftype;
1603 	uint32_t	devstate;
1604 	uint32_t	action_taken;
1605 	uint64_t	fileid;
1606 	uint32_t	rootdirfid;
1607 	fsid_t		op_fsid;
1608 	smb_ofile_t	*dir;
1609 	smb_opipe_t	*pipe;	/* for smb_opipe_open */
1610 	struct smb_sd	*sd;	/* for NTTransactCreate */
1611 	void		*create_ctx;
1612 
1613 	uint8_t		op_oplock_level;	/* requested/granted level */
1614 	uint32_t	op_oplock_state;	/* internal type+level */
1615 	uint32_t	lease_state;		/* SMB2_LEASE_... */
1616 	uint32_t	lease_flags;
1617 	uint16_t	lease_epoch;
1618 	uint16_t	lease_version;		/* 1 or 2 */
1619 	uint8_t		lease_key[SMB_LEASE_KEY_SZ];	/* from client */
1620 	uint8_t		parent_lease_key[SMB_LEASE_KEY_SZ]; /* for V2 */
1621 
1622 	smb_dh_vers_t	dh_vers;
1623 	smb2fid_t	dh_fileid;		/* for durable reconnect */
1624 	uint8_t		create_guid[16];
1625 	uint32_t	dh_v2_flags;
1626 	uint32_t	dh_timeout;
1627 } smb_arg_open_t;
1628 
1629 typedef struct smb_arg_lock {
1630 	void		*lvec;
1631 	uint32_t	lcnt;
1632 	uint32_t	lseq;
1633 } smb_arg_lock_t;
1634 
1635 typedef struct smb_arg_olbrk {
1636 	uint32_t	NewLevel;
1637 	boolean_t	AckRequired;
1638 } smb_arg_olbrk_t;
1639 
1640 /*
1641  * SMB Request State Machine
1642  * -------------------------
1643  *
1644  *                  T4               +------+		T0
1645  *      +--------------------------->| FREE |---------------------------+
1646  *      |                            +------+                           |
1647  * +-----------+                                                        |
1648  * | COMPLETED |                                                        |
1649  * +-----------+
1650  *      ^                                                               |
1651  *      | T15                      +-----------+                        v
1652  * +------------+        T6        |           |                +--------------+
1653  * | CLEANED_UP |<-----------------| CANCELLED |                | INITIALIZING |
1654  * +------------+                  |           |                +--------------+
1655  *      |    ^                     +-----------+                        |
1656  *      |    |                        ^  ^ ^ ^                          |
1657  *      |    |          +-------------+  | | |                          |
1658  *      |    |    T3    |                | | |               T13        | T1
1659  *      |    +-------------------------+ | | +----------------------+   |
1660  *      +----------------------------+ | | |                        |   |
1661  *         T16          |            | | | +-----------+            |   |
1662  *                      |           \/ | | T5          |            |   v
1663  * +-----------------+  |   T12     +--------+         |     T2    +-----------+
1664  * | EVENT_OCCURRED  |------------->| ACTIVE |<--------------------| SUBMITTED |
1665  * +-----------------+  |           +--------+         |           +-----------+
1666  *        ^             |              | ^ |           |
1667  *        |             |           T8 | | |  T7       |
1668  *        | T10      T9 |   +----------+ | +-------+   |  T11
1669  *        |             |   |            +-------+ |   |
1670  *        |             |   |               T14  | |   |
1671  *        |             |   v                    | v   |
1672  *      +----------------------+                +--------------+
1673  *	|     WAITING_EVENT    |                | WAITING_LOCK |
1674  *      +----------------------+                +--------------+
1675  *
1676  *
1677  *
1678  *
1679  *
1680  * Transition T0
1681  *
1682  * This transition occurs when the request is allocated and is still under the
1683  * control of the session thread.
1684  *
1685  * Transition T1
1686  *
1687  * This transition occurs when the session thread dispatches a task to treat the
1688  * request.
1689  *
1690  * Transition T2
1691  *
1692  *
1693  *
1694  * Transition T3
1695  *
1696  * A request completes and smbsr_cleanup is called to release resources
1697  * associated with the request (but not the smb_request_t itself).  This
1698  * includes references on smb_ofile_t, smb_node_t, and other structures.
1699  * CLEANED_UP state exists to detect if we attempt to cleanup a request
1700  * multiple times and to allow us to detect that we are accessing a
1701  * request that has already been cleaned up.
1702  *
1703  * Transition T4
1704  *
1705  *
1706  *
1707  * Transition T5
1708  *
1709  *
1710  *
1711  * Transition T6
1712  *
1713  *
1714  *
1715  * Transition T7
1716  *
1717  *
1718  *
1719  * Transition T8
1720  *
1721  *
1722  *
1723  * Transition T9
1724  *
1725  *
1726  *
1727  * Transition T10
1728  *
1729  *
1730  *
1731  * Transition T11
1732  *
1733  *
1734  *
1735  * Transition T12
1736  *
1737  *
1738  *
1739  * Transition T13
1740  *
1741  *
1742  *
1743  * Transition T14
1744  *
1745  *
1746  *
1747  * Transition T15
1748  *
1749  * Request processing is completed (control returns from smb_dispatch)
1750  *
1751  * Transition T16
1752  *
1753  * Multipart (andx) request was cleaned up with smbsr_cleanup but more "andx"
1754  * sections remain to be processed.
1755  *
1756  */
1757 
1758 #define	SMB_REQ_MAGIC 		0x534D4252	/* 'SMBR' */
1759 #define	SMB_REQ_VALID(p)	ASSERT((p)->sr_magic == SMB_REQ_MAGIC)
1760 
1761 typedef enum smb_req_state {
1762 	SMB_REQ_STATE_FREE = 0,
1763 	SMB_REQ_STATE_INITIALIZING,
1764 	SMB_REQ_STATE_SUBMITTED,
1765 	SMB_REQ_STATE_ACTIVE,
1766 	SMB_REQ_STATE_WAITING_AUTH,
1767 	SMB_REQ_STATE_WAITING_FCN1,
1768 	SMB_REQ_STATE_WAITING_FCN2,
1769 	SMB_REQ_STATE_WAITING_LOCK,
1770 	SMB_REQ_STATE_WAITING_PIPE,
1771 	SMB_REQ_STATE_COMPLETED,
1772 	SMB_REQ_STATE_CANCEL_PENDING,
1773 	SMB_REQ_STATE_CANCELLED,
1774 	SMB_REQ_STATE_CLEANED_UP,
1775 	SMB_REQ_STATE_SENTINEL
1776 } smb_req_state_t;
1777 
1778 typedef struct smb_request {
1779 	list_node_t		sr_session_lnd;
1780 	uint32_t		sr_magic;
1781 	kmutex_t		sr_mutex;
1782 	smb_req_state_t		sr_state;
1783 	struct smb_server	*sr_server;
1784 	pid_t			*sr_pid;
1785 	int32_t			sr_gmtoff;
1786 	smb_session_t		*session;
1787 	smb_kmod_cfg_t		*sr_cfg;
1788 	void			(*cancel_method)(struct smb_request *);
1789 	void			*cancel_arg2;
1790 
1791 	/* Queue used by smb_request_append_postwork. */
1792 	struct smb_request	*sr_postwork;
1793 
1794 	list_node_t		sr_waiters;	/* smb_notify.c */
1795 
1796 	/* Info from session service header */
1797 	uint32_t		sr_req_length; /* Excluding NBT header */
1798 
1799 	/* Request buffer excluding NBT header */
1800 	void			*sr_request_buf;
1801 
1802 	struct mbuf_chain	command;
1803 	struct mbuf_chain	reply;
1804 	struct mbuf_chain	raw_data;
1805 	list_t			sr_storage;
1806 	struct smb_xa		*r_xa;
1807 	int			andx_prev_wct;
1808 	int 			cur_reply_offset;
1809 	int			orig_request_hdr;
1810 	unsigned int		reply_seqnum;	/* reply sequence number */
1811 	unsigned char		first_smb_com;	/* command code */
1812 	unsigned char		smb_com;	/* command code */
1813 
1814 	uint8_t			smb_rcls;	/* error code class */
1815 	uint8_t			smb_reh;	/* rsvd (AH DOS INT-24 ERR) */
1816 	uint16_t		smb_err;	/* error code */
1817 	smb_error_t		smb_error;
1818 
1819 	uint8_t			smb_flg;	/* flags */
1820 	uint16_t		smb_flg2;	/* flags */
1821 	unsigned char		smb_sig[8];	/* signiture */
1822 	uint16_t		smb_tid;	/* tree id #  */
1823 	uint32_t		smb_pid;	/* caller's process id # */
1824 	uint16_t		smb_uid;	/* local (smb1) user id # */
1825 	uint16_t		smb_mid;	/* mutiplex id #  */
1826 	unsigned char		smb_wct;	/* count of parameter words */
1827 	uint16_t		smb_bcc;	/* data byte count */
1828 
1829 	/*
1830 	 * Beginning offsets (in the mbuf chain) for the
1831 	 * command and reply headers, and the next reply.
1832 	 */
1833 	uint32_t		smb2_cmd_hdr;
1834 	uint32_t		smb2_reply_hdr;
1835 	uint32_t		smb2_next_reply;
1836 
1837 	/*
1838 	 * SMB2 header fields.  [MS-SMB2 2.2.1.2]
1839 	 * XXX: Later do a union w smb1 members
1840 	 */
1841 	uint16_t		smb2_credit_charge;
1842 	uint16_t		smb2_chan_seq;	/* cmd only */
1843 	uint32_t		smb2_status;
1844 	uint16_t		smb2_cmd_code;
1845 	uint16_t		smb2_credit_request;
1846 	uint16_t		smb2_credit_response;
1847 	uint16_t		smb2_total_credits; /* in compound */
1848 	uint32_t		smb2_hdr_flags;
1849 	uint32_t		smb2_next_command;
1850 	uint64_t		smb2_messageid;
1851 	uint64_t		smb2_first_msgid;
1852 	/* uint32_t		smb2_pid; use smb_pid */
1853 	/* uint32_t		smb2_tid; use smb_tid */
1854 	uint64_t		smb2_ssnid;	/* See u_ssnid */
1855 	uint8_t			smb2_sig[16];	/* signature */
1856 
1857 	/*
1858 	 * SMB3 transform header fields. [MS-SMB2 2.2.41]
1859 	 */
1860 	uint64_t		smb3_tform_ssnid;
1861 	smb_user_t		*tform_ssn;
1862 	uint32_t		msgsize;
1863 	uint8_t			nonce[16];
1864 
1865 	boolean_t		encrypted;
1866 
1867 	boolean_t		smb2_async;
1868 	uint64_t		smb2_async_id;
1869 	/* Parameters */
1870 	struct mbuf_chain	smb_vwv;	/* variable width value */
1871 
1872 	/* Data */
1873 	struct mbuf_chain	smb_data;
1874 
1875 	uint16_t		smb_fid;	/* not in hdr, but common */
1876 
1877 	unsigned char		andx_com;
1878 	uint16_t		andx_off;
1879 
1880 	struct smb_tree		*tid_tree;
1881 	struct smb_ofile	*fid_ofile;
1882 	smb_user_t		*uid_user;
1883 
1884 	cred_t			*user_cr;
1885 	kthread_t		*sr_worker;
1886 	hrtime_t		sr_time_submitted;
1887 	hrtime_t		sr_time_active;
1888 	hrtime_t		sr_time_start;
1889 	int32_t			sr_txb;
1890 	uint32_t		sr_seqnum;
1891 
1892 	union {
1893 		smb_arg_negotiate_t	*negprot;
1894 		smb_arg_sessionsetup_t	*ssetup;
1895 		smb_arg_tcon_t		tcon;
1896 		smb_arg_dirop_t		dirop;
1897 		smb_arg_open_t		open;
1898 		smb_arg_lock_t		lock;
1899 		smb_arg_olbrk_t		olbrk;	/* for async oplock break */
1900 		smb_rw_param_t		*rw;
1901 		int32_t			timestamp;
1902 		void			*other;
1903 	} arg;
1904 } smb_request_t;
1905 
1906 #define	sr_ssetup	arg.ssetup
1907 #define	sr_negprot	arg.negprot
1908 #define	sr_tcon		arg.tcon
1909 #define	sr_dirop	arg.dirop
1910 #define	sr_open		arg.open
1911 #define	sr_rw		arg.rw
1912 #define	sr_timestamp	arg.timestamp
1913 
1914 #define	SMB_READ_PROTOCOL(hdr) \
1915 	LE_IN32(((smb_hdr_t *)(hdr))->protocol)
1916 
1917 #define	SMB_PROTOCOL_MAGIC_INVALID(rd_sr) \
1918 	(SMB_READ_PROTOCOL((rd_sr)->sr_request_buf) != SMB_PROTOCOL_MAGIC)
1919 
1920 #define	SMB_READ_COMMAND(hdr) \
1921 	(((smb_hdr_t *)(hdr))->command)
1922 
1923 #define	SMB_IS_NT_CANCEL(rd_sr) \
1924 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_NT_CANCEL)
1925 
1926 #define	SMB_IS_SESSION_SETUP_ANDX(rd_sr) \
1927 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == \
1928 	    SMB_COM_SESSION_SETUP_ANDX)
1929 
1930 #define	SMB_IS_NT_NEGOTIATE(rd_sr) \
1931 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_NEGOTIATE)
1932 
1933 #define	SMB_IS_TREE_CONNECT_ANDX(rd_sr) \
1934 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_TREE_CONNECT_ANDX)
1935 
1936 #define	SMB_XA_FLAG_OPEN	0x0001
1937 #define	SMB_XA_FLAG_CLOSE	0x0002
1938 #define	SMB_XA_FLAG_COMPLETE	0x0004
1939 #define	SMB_XA_CLOSED(xa) (!((xa)->xa_flags & SMB_XA_FLAG_OPEN))
1940 
1941 #define	SMB_XA_MAGIC		0x534D4258	/* 'SMBX' */
1942 
1943 typedef struct smb_xa {
1944 	list_node_t		xa_lnd;
1945 	uint32_t		xa_magic;
1946 	kmutex_t		xa_mutex;
1947 
1948 	uint32_t		xa_refcnt;
1949 	uint32_t		xa_flags;
1950 
1951 	struct smb_session	*xa_session;
1952 
1953 	unsigned char		smb_com;	/* which TRANS type */
1954 	unsigned char		smb_flg;	/* flags */
1955 	uint16_t		smb_flg2;	/* flags */
1956 	uint16_t		smb_tid;	/* tree id number */
1957 	uint32_t		smb_pid;	/* caller's process id */
1958 	uint16_t		smb_uid;	/* user id number */
1959 	uint32_t		smb_func;	/* NT_TRANS function */
1960 
1961 	uint16_t		xa_smb_mid;	/* mutiplex id number */
1962 	uint16_t		xa_smb_fid;	/* TRANS2 secondary */
1963 
1964 	unsigned int		reply_seqnum;	/* reply sequence number */
1965 
1966 	uint32_t	smb_tpscnt;	/* total parameter bytes being sent */
1967 	uint32_t	smb_tdscnt;	/* total data bytes being sent */
1968 	uint32_t	smb_mprcnt;	/* max parameter bytes to return */
1969 	uint32_t	smb_mdrcnt;	/* max data bytes to return */
1970 	uint32_t	smb_msrcnt;	/* max setup words to return */
1971 	uint32_t	smb_flags;	/* additional information: */
1972 				/*  bit 0 - if set, disconnect TID in smb_tid */
1973 				/*  bit 1 - if set, transaction is one way */
1974 				/*  (no final response) */
1975 	int32_t	smb_timeout;	/* number of milliseconds to await completion */
1976 	uint32_t	smb_suwcnt;	/* set up word count */
1977 
1978 	char			*xa_pipe_name;
1979 
1980 	/*
1981 	 * These are the param and data count received so far,
1982 	 * used to decide if the whole trans is here yet.
1983 	 */
1984 	int			req_disp_param;
1985 	int			req_disp_data;
1986 
1987 	struct mbuf_chain	req_setup_mb;
1988 	struct mbuf_chain	req_param_mb;
1989 	struct mbuf_chain	req_data_mb;
1990 
1991 	struct mbuf_chain	rep_setup_mb;
1992 	struct mbuf_chain	rep_param_mb;
1993 	struct mbuf_chain	rep_data_mb;
1994 } smb_xa_t;
1995 
1996 
1997 #define	SDDF_NO_FLAGS			0
1998 #define	SDDF_SUPPRESS_TID		0x0001
1999 #define	SDDF_SUPPRESS_UID		0x0002
2000 
2001 /*
2002  * SMB dispatch return codes.
2003  */
2004 typedef enum {
2005 	SDRC_SUCCESS = 0,
2006 	SDRC_ERROR,
2007 	SDRC_DROP_VC,
2008 	SDRC_NO_REPLY,
2009 	SDRC_SR_KEPT,
2010 	SDRC_NOT_IMPLEMENTED
2011 } smb_sdrc_t;
2012 
2013 #define	VAR_BCC		((short)-1)
2014 
2015 #define	SMB_SERVER_MAGIC	0x53534552	/* 'SSER' */
2016 #define	SMB_SERVER_VALID(s)	\
2017     ASSERT(((s) != NULL) && ((s)->sv_magic == SMB_SERVER_MAGIC))
2018 
2019 #define	SMB_LISTENER_MAGIC	0x4C53544E	/* 'LSTN' */
2020 #define	SMB_LISTENER_VALID(ld)	\
2021     ASSERT(((ld) != NULL) && ((ld)->ld_magic == SMB_LISTENER_MAGIC))
2022 
2023 typedef struct {
2024 	uint32_t		ld_magic;
2025 	struct smb_server	*ld_sv;
2026 	smb_thread_t		ld_thread;
2027 	ksocket_t		ld_so;
2028 	in_port_t		ld_port;
2029 	int			ld_family;
2030 	struct sockaddr_in	ld_sin;
2031 	struct sockaddr_in6	ld_sin6;
2032 } smb_listener_daemon_t;
2033 
2034 #define	SMB_SSETUP_CMD			"authentication"
2035 #define	SMB_TCON_CMD			"share mapping"
2036 #define	SMB_OPIPE_CMD			"pipe open"
2037 #define	SMB_THRESHOLD_REPORT_THROTTLE	50
2038 typedef struct smb_cmd_threshold {
2039 	char			*ct_cmd;
2040 	kmutex_t		ct_mutex;
2041 	volatile uint32_t	ct_active_cnt;
2042 	volatile uint32_t	ct_blocked_cnt;
2043 	uint32_t		ct_threshold;
2044 	uint32_t		ct_timeout; /* milliseconds */
2045 	kcondvar_t		ct_cond;
2046 } smb_cmd_threshold_t;
2047 
2048 typedef struct {
2049 	kstat_named_t		ls_files;
2050 	kstat_named_t		ls_trees;
2051 	kstat_named_t		ls_users;
2052 } smb_server_legacy_kstat_t;
2053 
2054 typedef enum smb_server_state {
2055 	SMB_SERVER_STATE_CREATED = 0,
2056 	SMB_SERVER_STATE_CONFIGURED,
2057 	SMB_SERVER_STATE_RUNNING,
2058 	SMB_SERVER_STATE_STOPPING,
2059 	SMB_SERVER_STATE_DELETING,
2060 	SMB_SERVER_STATE_SENTINEL
2061 } smb_server_state_t;
2062 
2063 typedef struct {
2064 	/* protected by sv_mutex */
2065 	kcondvar_t		sp_cv;
2066 	uint32_t 		sp_cnt;
2067 	smb_llist_t		sp_list;
2068 	smb_llist_t		sp_fidlist;
2069 } smb_spool_t;
2070 
2071 #define	SMB_SERVER_STATE_VALID(S)               \
2072     ASSERT(((S) == SMB_SERVER_STATE_CREATED) || \
2073 	    ((S) == SMB_SERVER_STATE_CONFIGURED) || \
2074 	    ((S) == SMB_SERVER_STATE_RUNNING) ||    \
2075 	    ((S) == SMB_SERVER_STATE_STOPPING) ||   \
2076 	    ((S) == SMB_SERVER_STATE_DELETING))
2077 
2078 typedef struct smb_server {
2079 	list_node_t		sv_lnd;
2080 	uint32_t		sv_magic;
2081 	kcondvar_t		sv_cv;
2082 	kmutex_t		sv_mutex;
2083 	smb_server_state_t	sv_state;
2084 	uint32_t		sv_refcnt;
2085 	pid_t			sv_pid;
2086 	zoneid_t		sv_zid;
2087 	smb_listener_daemon_t	sv_nbt_daemon;
2088 	smb_listener_daemon_t	sv_tcp_daemon;
2089 	krwlock_t		sv_cfg_lock;
2090 	smb_kmod_cfg_t		sv_cfg;
2091 	smb_session_t		*sv_session;
2092 	smb_llist_t		sv_session_list;
2093 	smb_hash_t		*sv_persistid_ht;
2094 	smb_hash_t		*sv_lease_ht;
2095 
2096 	struct smb_export	sv_export;
2097 	struct __door_handle	*sv_lmshrd;
2098 
2099 	/* Internal door for up-calls to smbd */
2100 	struct __door_handle	*sv_kdoor_hd;
2101 	int			sv_kdoor_id; /* init -1 */
2102 	uint64_t		sv_kdoor_ncall;
2103 	kmutex_t		sv_kdoor_mutex;
2104 	kcondvar_t		sv_kdoor_cv;
2105 
2106 	int32_t			si_gmtoff;
2107 
2108 	smb_thread_t		si_thread_timers;
2109 
2110 	taskq_t			*sv_worker_pool;
2111 	taskq_t			*sv_receiver_pool;
2112 
2113 	smb_node_t		*si_root_smb_node;
2114 	smb_llist_t		sv_opipe_list;
2115 	smb_llist_t		sv_event_list;
2116 
2117 	/* Statistics */
2118 	hrtime_t		sv_start_time;
2119 	kstat_t			*sv_ksp;
2120 	volatile uint32_t	sv_nbt_sess;
2121 	volatile uint32_t	sv_tcp_sess;
2122 	volatile uint32_t	sv_users;
2123 	volatile uint32_t	sv_trees;
2124 	volatile uint32_t	sv_files;
2125 	volatile uint32_t	sv_pipes;
2126 	volatile uint64_t	sv_txb;
2127 	volatile uint64_t	sv_rxb;
2128 	volatile uint64_t	sv_nreq;
2129 	smb_srqueue_t		sv_srqueue;
2130 	smb_spool_t		sp_info;
2131 	smb_cmd_threshold_t	sv_ssetup_ct;
2132 	smb_cmd_threshold_t	sv_tcon_ct;
2133 	smb_cmd_threshold_t	sv_opipe_ct;
2134 	kstat_t			*sv_legacy_ksp;
2135 	kmutex_t		sv_legacy_ksmtx;
2136 	smb_disp_stats_t	*sv_disp_stats1;
2137 	smb_disp_stats_t	*sv_disp_stats2;
2138 } smb_server_t;
2139 
2140 #define	SMB_EVENT_MAGIC		0x45564E54	/* EVNT */
2141 #define	SMB_EVENT_TIMEOUT	45		/* seconds */
2142 #define	SMB_EVENT_VALID(e)	\
2143     ASSERT(((e) != NULL) && ((e)->se_magic == SMB_EVENT_MAGIC))
2144 typedef struct smb_event {
2145 	list_node_t		se_lnd;
2146 	uint32_t		se_magic;
2147 	kmutex_t		se_mutex;
2148 	kcondvar_t		se_cv;
2149 	smb_server_t		*se_server;
2150 	uint32_t		se_txid;
2151 	boolean_t		se_notified;
2152 	int			se_waittime;
2153 	int			se_timeout;
2154 	int			se_errno;
2155 } smb_event_t;
2156 
2157 typedef struct smb_kspooldoc {
2158 	list_node_t	sd_lnd;
2159 	uint32_t	sd_magic;
2160 	smb_inaddr_t	sd_ipaddr;
2161 	uint32_t	sd_spool_num;
2162 	uint16_t	sd_fid;
2163 	char		sd_username[MAXNAMELEN];
2164 	char		sd_path[MAXPATHLEN];
2165 } smb_kspooldoc_t;
2166 
2167 typedef struct smb_spoolfid {
2168 	list_node_t	sf_lnd;
2169 	uint32_t	sf_magic;
2170 	uint16_t	sf_fid;
2171 } smb_spoolfid_t;
2172 
2173 #define	SMB_INFO_NETBIOS_SESSION_SVC_RUNNING	0x0001
2174 #define	SMB_INFO_NETBIOS_SESSION_SVC_FAILED	0x0002
2175 #define	SMB_INFO_USER_LEVEL_SECURITY		0x40000000
2176 #define	SMB_INFO_ENCRYPT_PASSWORDS		0x80000000
2177 
2178 #define	SMB_IS_STREAM(node) ((node)->n_unode)
2179 
2180 typedef struct smb_tsd {
2181 	void (*proc)();
2182 	void *arg;
2183 	char name[100];
2184 } smb_tsd_t;
2185 
2186 typedef struct smb_disp_entry {
2187 	char		sdt_name[KSTAT_STRLEN];
2188 	smb_sdrc_t	(*sdt_pre_op)(smb_request_t *);
2189 	smb_sdrc_t	(*sdt_function)(smb_request_t *);
2190 	void		(*sdt_post_op)(smb_request_t *);
2191 	uint8_t		sdt_com;
2192 	char		sdt_dialect;
2193 	uint8_t		sdt_flags;
2194 } smb_disp_entry_t;
2195 
2196 typedef struct smb_xlate {
2197 	int	code;
2198 	char	*str;
2199 } smb_xlate_t;
2200 
2201 /*
2202  * This structure is a helper for building RAP NetShareEnum response
2203  *
2204  * es_posix_uid UID of the user requesting the shares list which
2205  *              is used to detect if the user has any autohome
2206  * es_bufsize   size of the response buffer
2207  * es_buf       pointer to the response buffer
2208  * es_ntotal    total number of shares exported by server which
2209  *              their OEM names is less then 13 chars
2210  * es_nsent     number of shares that can fit in the specified buffer
2211  * es_datasize  actual data size (share's data) which was encoded
2212  *              in the response buffer
2213  */
2214 typedef struct smb_enumshare_info {
2215 	uid_t		es_posix_uid;
2216 	uint16_t	es_bufsize;
2217 	char		*es_buf;
2218 	uint16_t	es_ntotal;
2219 	uint16_t	es_nsent;
2220 	uint16_t	es_datasize;
2221 } smb_enumshare_info_t;
2222 
2223 #ifdef	__cplusplus
2224 }
2225 #endif
2226 
2227 #endif /* _SMBSRV_SMB_KTYPES_H */
2228