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