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