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