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