xref: /illumos-gate/usr/src/uts/common/smbsrv/smb_ktypes.h (revision 7f3ef643e446c82e27a9386991b140b128baf22c)
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 <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 typedef struct smb_node_fcn {
456 	kmutex_t	fcn_mutex;
457 	uint32_t	fcn_count;
458 	list_t		fcn_watchers;	/* smb_request_t, sr_ncr.nc_lnd */
459 } smb_node_fcn_t;
460 
461 typedef struct smb_notify_change_req {
462 	list_node_t		nc_lnd;	/* n_fcn.fcn_watchers */
463 	kcondvar_t		nc_cv;	/* prot: sr_mutex */
464 	uint32_t		nc_flags;
465 	uint32_t		nc_action;
466 	char			*nc_fname;
467 } smb_notify_change_req_t;
468 
469 /*
470  * SMB operates over a NetBIOS-over-TCP transport (NBT) or directly
471  * over TCP, which is also known as direct hosted NetBIOS-less SMB
472  * or SMB-over-TCP.
473  *
474  * NBT messages have a 4-byte header that defines the message type
475  * (8-bits), a 7-bit flags field and a 17-bit length.
476  *
477  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
478  * |      TYPE     |     FLAGS   |E|            LENGTH             |
479  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
480  *
481  * 8-bit type      Defined in RFC 1002
482  * 7-bit flags     Bits 0-6 reserved (must be 0)
483  *                 Bit 7: Length extension bit (E)
484  * 17-bit length   Includes bit 7 of the flags byte
485  *
486  *
487  * SMB-over-TCP is defined to use a modified version of the NBT header
488  * containing an 8-bit message type and 24-bit message length.
489  *
490  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491  * |      TYPE     |                  LENGTH                       |
492  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
493  *
494  * 8-bit type      Must be 0
495  * 24-bit length
496  *
497  * The following structure is used to represent a generic, in-memory
498  * SMB transport header; it is not intended to map directly to either
499  * of the over-the-wire formats.
500  */
501 typedef struct {
502 	uint8_t		xh_type;
503 	uint32_t	xh_length;
504 } smb_xprt_t;
505 
506 int MBC_LENGTH(struct mbuf_chain *);
507 int MBC_MAXBYTES(struct mbuf_chain *);
508 void MBC_SETUP(struct mbuf_chain *, uint32_t);
509 void MBC_INIT(struct mbuf_chain *, uint32_t);
510 void MBC_FLUSH(struct mbuf_chain *);
511 void MBC_ATTACH_MBUF(struct mbuf_chain *, struct mbuf *);
512 void MBC_APPEND_MBUF(struct mbuf_chain *, struct mbuf *);
513 void MBC_ATTACH_BUF(struct mbuf_chain *MBC, unsigned char *BUF, int LEN);
514 int MBC_SHADOW_CHAIN(struct mbuf_chain *SUBMBC, struct mbuf_chain *MBC,
515     int OFF, int LEN);
516 
517 #define	MBC_ROOM_FOR(b, n) (((b)->chain_offset + (n)) <= (b)->max_bytes)
518 
519 #define	OPLOCK_MIN_TIMEOUT	(5 * 1000)
520 #define	OPLOCK_STD_TIMEOUT	(30 * 1000)
521 
522 /*
523  * Oplock break flags:
524  * SMB_OPLOCK_BREAK_EXCLUSIVE - only break exclusive oplock
525  * (type SMB_OPLOCK_EXCLUSIVE or SMB_OPLOCK_BATCH)
526  * SMB_OPLOCK_BREAK_BATCH - only break exclusive BATCH oplock
527  * SMB_OPLOCK_BREAK_NOWAIT - do not wait for oplock break ack
528  */
529 #define	SMB_OPLOCK_NO_BREAK		0x00
530 #define	SMB_OPLOCK_BREAK_TO_NONE	0x01
531 #define	SMB_OPLOCK_BREAK_TO_LEVEL_II	0x02
532 #define	SMB_OPLOCK_BREAK_EXCLUSIVE	0x04
533 #define	SMB_OPLOCK_BREAK_BATCH		0x08
534 #define	SMB_OPLOCK_BREAK_NOWAIT		0x10
535 
536 /*
537  * Oplocks levels are defined to match the levels in the SMB
538  * protocol (nt_create_andx / nt_transact_create) and should
539  * not be changed
540  */
541 #define	SMB_OPLOCK_NONE		0
542 #define	SMB_OPLOCK_EXCLUSIVE	1
543 #define	SMB_OPLOCK_BATCH	2
544 #define	SMB_OPLOCK_LEVEL_II	3
545 
546 typedef struct smb_oplock {
547 	kmutex_t		ol_mutex;
548 	kcondvar_t		ol_cv;
549 	kthread_t		*ol_xthread;
550 	boolean_t		ol_fem;		/* fem monitor installed? */
551 	uint8_t			ol_break;
552 	uint32_t		ol_count;	/* number of grants */
553 	list_t			ol_grants;	/* list of smb_oplock_grant_t */
554 } smb_oplock_t;
555 
556 #define	SMB_OPLOCK_GRANT_MAGIC	0x4F4C4B47	/* OLKG */
557 #define	SMB_OPLOCK_GRANT_VALID(p) \
558 	ASSERT((p)->og_magic == SMB_OPLOCK_GRANT_MAGIC)
559 #define	SMB_OFILE_OPLOCK_GRANTED(p) \
560 	((p)->f_oplock_grant.og_magic == SMB_OPLOCK_GRANT_MAGIC)
561 typedef struct smb_oplock_grant {
562 	uint32_t		og_magic;
563 	list_node_t		og_lnd;
564 	uint8_t			og_level;
565 	uint16_t		og_fid;
566 	uint16_t		og_tid;
567 	uint16_t		og_uid;
568 	struct smb_session	*og_session;
569 	struct smb_ofile	*og_ofile;
570 } smb_oplock_grant_t;
571 
572 #define	SMB_OPLOCK_BREAK_MAGIC	0x4F4C4B42	/* OLKB */
573 #define	SMB_OPLOCK_BREAK_VALID(p) \
574 	ASSERT((p)->ob_magic == SMB_OPLOCK_BREAK_MAGIC)
575 typedef struct smb_oplock_break {
576 	uint32_t	ob_magic;
577 	list_node_t	ob_lnd;
578 	struct smb_node	*ob_node;
579 } smb_oplock_break_t;
580 
581 
582 #define	SMB_VFS_MAGIC	0x534D4256	/* 'SMBV' */
583 
584 typedef struct smb_vfs {
585 	uint32_t		sv_magic;
586 	list_node_t		sv_lnd;
587 	uint32_t		sv_refcnt;
588 	vfs_t			*sv_vfsp;
589 	vnode_t			*sv_rootvp;
590 } smb_vfs_t;
591 
592 #define	SMB_NODE_MAGIC		0x4E4F4445	/* 'NODE' */
593 #define	SMB_NODE_VALID(p)	ASSERT((p)->n_magic == SMB_NODE_MAGIC)
594 
595 typedef enum {
596 	SMB_NODE_STATE_AVAILABLE = 0,
597 	SMB_NODE_STATE_DESTROYING
598 } smb_node_state_t;
599 
600 /*
601  * waiting_event        # of clients requesting FCN
602  * n_timestamps         cached timestamps
603  * n_allocsz            cached file allocation size
604  * n_dnode              directory node
605  * n_unode              unnamed stream node
606  * delete_on_close_cred credentials for delayed delete
607  */
608 typedef struct smb_node {
609 	uint32_t		n_magic;
610 	krwlock_t		n_lock;
611 	kmutex_t		n_mutex;
612 	list_node_t		n_lnd;
613 	smb_node_state_t	n_state;
614 	uint32_t		n_refcnt;
615 	uint32_t		n_hashkey;
616 	smb_llist_t		*n_hash_bucket;
617 	uint32_t		n_open_count;
618 	uint32_t		n_opening_count;
619 	smb_llist_t		n_ofile_list;
620 	smb_llist_t		n_lock_list;
621 	uint32_t		n_pending_dosattr;
622 	volatile int		flags;
623 	u_offset_t		n_allocsz;
624 	smb_node_fcn_t		n_fcn;
625 	smb_oplock_t		n_oplock;
626 	struct smb_node		*n_dnode;
627 	struct smb_node		*n_unode;
628 	cred_t			*delete_on_close_cred;
629 	uint32_t		n_delete_on_close_flags;
630 	char			od_name[MAXNAMELEN];
631 	vnode_t			*vp;
632 	smb_audit_buf_node_t	*n_audit_buf;
633 } smb_node_t;
634 
635 #define	NODE_FLAGS_REPARSE		0x00001000
636 #define	NODE_FLAGS_DFSLINK		0x00002000
637 #define	NODE_FLAGS_VFSROOT		0x00004000
638 #define	NODE_FLAGS_SYSTEM		0x00008000
639 #define	NODE_FLAGS_WRITE_THROUGH	0x00100000
640 #define	NODE_XATTR_DIR			0x01000000
641 #define	NODE_FLAGS_DELETE_ON_CLOSE	0x40000000
642 #define	NODE_FLAGS_EXECUTABLE		0x80000000
643 
644 #define	SMB_NODE_VFS(node)	((node)->vp->v_vfsp)
645 #define	SMB_NODE_FSID(node)	((node)->vp->v_vfsp->vfs_fsid)
646 
647 /* Maximum buffer size for encryption key */
648 #define	SMB_ENCRYPT_KEY_MAXLEN		32
649 
650 #define	SMB_SHARE_MAGIC		0x4B534852	/* KSHR */
651 
652 typedef struct smb_kshare {
653 	uint32_t	shr_magic;
654 	char		*shr_name;
655 	char		*shr_path;
656 	char		*shr_cmnt;
657 	char		*shr_container;
658 	char		*shr_oemname;
659 	uint32_t	shr_flags;
660 	uint32_t	shr_type;
661 	uint32_t	shr_refcnt;
662 	uint32_t	shr_autocnt;
663 	uid_t		shr_uid;
664 	gid_t		shr_gid;
665 	char		*shr_access_none;
666 	char		*shr_access_ro;
667 	char		*shr_access_rw;
668 	avl_node_t	shr_link;
669 	kmem_cache_t	*shr_cache;
670 	kmutex_t	shr_mutex;
671 } smb_kshare_t;
672 
673 
674 typedef struct smb_arg_negotiate {
675 	char		*ni_name;
676 	int		ni_dialect;
677 	int		ni_index;
678 	uint32_t	ni_capabilities;
679 	uint16_t	ni_maxmpxcount;
680 	int16_t		ni_tzcorrection;
681 	uint8_t		ni_keylen;
682 	uint8_t		ni_key[SMB_ENCRYPT_KEY_MAXLEN];
683 	timestruc_t	ni_servertime;
684 } smb_arg_negotiate_t;
685 
686 typedef struct smb_arg_sessionsetup {
687 	char		*ssi_user;
688 	char		*ssi_domain;
689 	uint16_t	ssi_cipwlen;
690 	uint8_t		*ssi_cipwd;
691 	uint16_t	ssi_cspwlen;
692 	uint8_t		*ssi_cspwd;
693 	uint16_t	ssi_maxmpxcount;
694 	uint32_t	ssi_capabilities;
695 	uint32_t	ssi_sesskey;
696 	boolean_t	ssi_guest;
697 } smb_arg_sessionsetup_t;
698 
699 typedef struct tcon {
700 	char		*path;
701 	char		*service;
702 	int		pwdlen;
703 	char		*password;
704 	uint16_t	flags;
705 	uint16_t	optional_support;
706 	smb_kshare_t	*si;
707 } smb_arg_tcon_t;
708 
709 /*
710  * Based on section 2.6.1.2 (Connection Management) of the June 13,
711  * 1996 CIFS spec, a server may terminate the transport connection
712  * due to inactivity. The client software is expected to be able to
713  * automatically reconnect to the server if this happens. Like much
714  * of the useful background information, this section appears to
715  * have been dropped from later revisions of the document.
716  *
717  * Each session has an activity timestamp that's updated whenever a
718  * request is dispatched. If the session is idle, i.e. receives no
719  * requests, for SMB_SESSION_INACTIVITY_TIMEOUT minutes it will be
720  * closed.
721  *
722  * Each session has an I/O semaphore to serialize communication with
723  * the client. For example, after receiving a raw-read request, the
724  * server is not allowed to send an oplock break to the client until
725  * after it has sent the raw-read data.
726  */
727 #define	SMB_SESSION_INACTIVITY_TIMEOUT		(15 * 60)
728 
729 #define	SMB_SESSION_OFILE_MAX			(16 * 1024)
730 
731 /*
732  * When a connection is set up we need to remember both the client
733  * (peer) IP address and the local IP address used to establish the
734  * connection. When a client connects with a vc number of zero, we
735  * are supposed to abort any existing connections with that client
736  * (see notes in smb_negotiate.c and smb_session_setup_andx.c). For
737  * servers with multiple network interfaces or IP aliases, however,
738  * each interface has to be managed independently since the client
739  * is not aware of the server configuration. We have to allow the
740  * client to establish a connection on each interface with a vc
741  * number of zero without aborting the other connections.
742  *
743  * ipaddr:       the client (peer) IP address for the session.
744  * local_ipaddr: the local IP address used to connect to the server.
745  */
746 
747 #define	SMB_MAC_KEYSZ	512
748 
749 struct smb_sign {
750 	unsigned int seqnum;
751 	unsigned int mackey_len;
752 	unsigned int flags;
753 	unsigned char mackey[SMB_MAC_KEYSZ];
754 };
755 
756 #define	SMB_SIGNING_ENABLED	1
757 #define	SMB_SIGNING_CHECK	2
758 
759 /*
760  * Session State Machine
761  * ---------------------
762  *
763  * +-----------------------------+	     +------------------------------+
764  * | SMB_SESSION_STATE_CONNECTED |           | SMB_SESSION_STATE_TERMINATED |
765  * +-----------------------------+           +------------------------------+
766  *		T0|					     ^
767  *		  +--------------------+		     |T13
768  *		  v		       |T14                  |
769  * +-------------------------------+   |    +--------------------------------+
770  * | SMB_SESSION_STATE_ESTABLISHED |---+--->| SMB_SESSION_STATE_DISCONNECTED |
771  * +-------------------------------+        +--------------------------------+
772  *		T1|				^	   ^ ^ ^
773  *		  +----------+			|T9        | | |
774  *                           v			|          | | |
775  *                  +------------------------------+       | | |
776  *                  | SMB_SESSION_STATE_NEGOTIATED |       | | |
777  *                  +------------------------------+       | | |
778  *	                 ^|   ^|   | ^                     | | |
779  *      +----------------+|   ||   | |                     | | |
780  *      |+----------------+   || T7| |T8                   | | |
781  *      ||                    ||   | |                     | | |
782  *      ||   +----------------+|   | |                     | | |
783  *      ||   |+----------------+   | |                     | | |
784  *	||   ||			   v |                     | | |
785  *      ||   ||   +-----------------------------------+ T10| | |
786  *      ||   ||   | SMB_SESSION_STATE_OPLOCK_BREAKING |----+ | |
787  *      ||   ||   +-----------------------------------+      | |
788  *	||   ||T5                                            | |
789  *      ||   |+-->+-----------------------------------+	  T11| |
790  *      ||   |T6  | SMB_SESSION_STATE_READ_RAW_ACTIVE |------+ |
791  *      ||   +----+-----------------------------------+        |
792  *	||T3                                                   |
793  *      |+------->+------------------------------------+    T12|
794  *      |T4       | SMB_SESSION_STATE_WRITE_RAW_ACTIVE |-------+
795  *      +---------+------------------------------------+
796  *
797  * Transition T0
798  *
799  *
800  *
801  * Transition T1
802  *
803  *
804  *
805  * Transition T2
806  *
807  *
808  *
809  * Transition T3
810  *
811  *
812  *
813  * Transition T4
814  *
815  *
816  *
817  * Transition T5
818  *
819  *
820  *
821  * Transition T6
822  *
823  *
824  *
825  * Transition T7
826  *
827  *
828  *
829  * Transition T8
830  *
831  *
832  *
833  * Transition T9
834  *
835  *
836  *
837  * Transition T10
838  *
839  *
840  *
841  * Transition T11
842  *
843  *
844  *
845  * Transition T12
846  *
847  *
848  *
849  * Transition T13
850  *
851  *
852  *
853  * Transition T14
854  *
855  *
856  *
857  */
858 #define	SMB_SESSION_MAGIC	0x53455353	/* 'SESS' */
859 #define	SMB_SESSION_VALID(p)	\
860     ASSERT(((p) != NULL) && ((p)->s_magic == SMB_SESSION_MAGIC))
861 
862 #define	SMB_CHALLENGE_SZ	8
863 
864 typedef enum {
865 	SMB_SESSION_STATE_INITIALIZED = 0,
866 	SMB_SESSION_STATE_DISCONNECTED,
867 	SMB_SESSION_STATE_CONNECTED,
868 	SMB_SESSION_STATE_ESTABLISHED,
869 	SMB_SESSION_STATE_NEGOTIATED,
870 	SMB_SESSION_STATE_OPLOCK_BREAKING,
871 	SMB_SESSION_STATE_WRITE_RAW_ACTIVE,
872 	SMB_SESSION_STATE_READ_RAW_ACTIVE,
873 	SMB_SESSION_STATE_TERMINATED,
874 	SMB_SESSION_STATE_SENTINEL
875 } smb_session_state_t;
876 
877 typedef struct smb_session {
878 	uint32_t		s_magic;
879 	smb_rwx_t		s_lock;
880 	list_node_t		s_lnd;
881 	uint64_t		s_kid;
882 	smb_session_state_t	s_state;
883 	uint32_t		s_flags;
884 	int			s_write_raw_status;
885 	kthread_t		*s_thread;
886 	kt_did_t		s_ktdid;
887 	smb_kmod_cfg_t		s_cfg;
888 	kmem_cache_t		*s_cache;
889 	kmem_cache_t		*s_cache_request;
890 	struct smb_server	*s_server;
891 	int32_t			s_gmtoff;
892 	uint32_t		keep_alive;
893 	uint64_t		opentime;
894 	uint16_t		vcnumber;
895 	uint16_t		s_local_port;
896 	smb_inaddr_t		ipaddr;
897 	smb_inaddr_t		local_ipaddr;
898 	char 			workstation[SMB_PI_MAX_HOST];
899 	int			dialect;
900 	int			native_os;
901 	int			native_lm;
902 
903 	uint32_t		capabilities;
904 	struct smb_sign		signing;
905 
906 	ksocket_t		sock;
907 
908 	smb_slist_t		s_req_list;
909 	smb_llist_t		s_xa_list;
910 	smb_llist_t		s_user_list;
911 	smb_idpool_t		s_uid_pool;
912 	smb_txlst_t		s_txlst;
913 
914 	volatile uint32_t	s_tree_cnt;
915 	volatile uint32_t	s_file_cnt;
916 	volatile uint32_t	s_dir_cnt;
917 
918 	uint16_t		secmode;
919 	uint32_t		sesskey;
920 	uint32_t		challenge_len;
921 	unsigned char		challenge_key[SMB_CHALLENGE_SZ];
922 	unsigned char		MAC_key[44];
923 	int64_t			activity_timestamp;
924 	/*
925 	 * Maximum negotiated buffer size between SMB client and server
926 	 * in SMB_SESSION_SETUP_ANDX
927 	 */
928 	uint16_t		smb_msg_size;
929 	uchar_t			*outpipe_data;
930 	int			outpipe_datalen;
931 	int			outpipe_cookie;
932 	list_t			s_oplock_brkreqs;
933 	smb_srqueue_t		*s_srqueue;
934 } smb_session_t;
935 
936 #define	SMB_USER_MAGIC 0x55534552	/* 'USER' */
937 #define	SMB_USER_VALID(u)	\
938     ASSERT(((u) != NULL) && ((u)->u_magic == SMB_USER_MAGIC))
939 
940 #define	SMB_USER_FLAG_GUEST			SMB_ATF_GUEST
941 #define	SMB_USER_FLAG_IPC			SMB_ATF_ANON
942 #define	SMB_USER_FLAG_ADMIN			SMB_ATF_ADMIN
943 #define	SMB_USER_FLAG_POWER_USER		SMB_ATF_POWERUSER
944 #define	SMB_USER_FLAG_BACKUP_OPERATOR		SMB_ATF_BACKUPOP
945 
946 #define	SMB_USER_IS_ADMIN(U)	(((U)->u_flags & SMB_USER_FLAG_ADMIN) != 0)
947 #define	SMB_USER_IS_GUEST(U)	(((U)->u_flags & SMB_USER_FLAG_GUEST) != 0)
948 
949 #define	SMB_USER_PRIV_TAKE_OWNERSHIP	0x00000001
950 #define	SMB_USER_PRIV_BACKUP		0x00000002
951 #define	SMB_USER_PRIV_RESTORE		0x00000004
952 #define	SMB_USER_PRIV_SECURITY		0x00000008
953 
954 
955 typedef enum {
956 	SMB_USER_STATE_LOGGED_IN = 0,
957 	SMB_USER_STATE_LOGGING_OFF,
958 	SMB_USER_STATE_LOGGED_OFF,
959 	SMB_USER_STATE_SENTINEL
960 } smb_user_state_t;
961 
962 typedef struct smb_user {
963 	uint32_t		u_magic;
964 	list_node_t		u_lnd;
965 	kmutex_t		u_mutex;
966 	smb_user_state_t	u_state;
967 
968 	struct smb_server	*u_server;
969 	smb_session_t		*u_session;
970 	uint16_t		u_name_len;
971 	char			*u_name;
972 	uint16_t		u_domain_len;
973 	char			*u_domain;
974 	time_t			u_logon_time;
975 	cred_t			*u_cred;
976 	cred_t			*u_privcred;
977 
978 	smb_llist_t		u_tree_list;
979 	smb_idpool_t		u_tid_pool;
980 
981 	uint32_t		u_refcnt;
982 	uint32_t		u_flags;
983 	uint32_t		u_privileges;
984 	uint16_t		u_uid;
985 	uint32_t		u_audit_sid;
986 } smb_user_t;
987 
988 #define	SMB_TREE_MAGIC			0x54524545	/* 'TREE' */
989 #define	SMB_TREE_VALID(p)	\
990     ASSERT((p != NULL) && ((p)->t_magic == SMB_TREE_MAGIC))
991 
992 #define	SMB_TYPENAMELEN			_ST_FSTYPSZ
993 #define	SMB_VOLNAMELEN			32
994 
995 #define	SMB_TREE_READONLY		0x00000001
996 #define	SMB_TREE_SUPPORTS_ACLS		0x00000002
997 #define	SMB_TREE_STREAMS		0x00000004
998 #define	SMB_TREE_CASEINSENSITIVE	0x00000008
999 #define	SMB_TREE_NO_CASESENSITIVE	0x00000010
1000 #define	SMB_TREE_NO_EXPORT		0x00000020
1001 #define	SMB_TREE_OPLOCKS		0x00000040
1002 #define	SMB_TREE_SHORTNAMES		0x00000080
1003 #define	SMB_TREE_XVATTR			0x00000100
1004 #define	SMB_TREE_DIRENTFLAGS		0x00000200
1005 #define	SMB_TREE_ACLONCREATE		0x00000400
1006 #define	SMB_TREE_ACEMASKONACCESS	0x00000800
1007 #define	SMB_TREE_NFS_MOUNTED		0x00001000
1008 #define	SMB_TREE_UNICODE_ON_DISK	0x00002000
1009 #define	SMB_TREE_CATIA			0x00004000
1010 #define	SMB_TREE_ABE			0x00008000
1011 #define	SMB_TREE_QUOTA			0x00010000
1012 #define	SMB_TREE_DFSROOT		0x00020000
1013 #define	SMB_TREE_SPARSE			0x00040000
1014 #define	SMB_TREE_TRAVERSE_MOUNTS	0x00080000
1015 
1016 typedef enum {
1017 	SMB_TREE_STATE_CONNECTED = 0,
1018 	SMB_TREE_STATE_DISCONNECTING,
1019 	SMB_TREE_STATE_DISCONNECTED,
1020 	SMB_TREE_STATE_SENTINEL
1021 } smb_tree_state_t;
1022 
1023 typedef struct smb_tree {
1024 	uint32_t		t_magic;
1025 	kmutex_t		t_mutex;
1026 	list_node_t		t_lnd;
1027 	smb_tree_state_t	t_state;
1028 
1029 	struct smb_server	*t_server;
1030 	smb_session_t		*t_session;
1031 	smb_user_t		*t_user;
1032 	smb_node_t		*t_snode;
1033 
1034 	smb_llist_t		t_ofile_list;
1035 	smb_idpool_t		t_fid_pool;
1036 
1037 	smb_llist_t		t_odir_list;
1038 	smb_idpool_t		t_odid_pool;
1039 
1040 	uint32_t		t_refcnt;
1041 	uint32_t		t_flags;
1042 	int32_t			t_res_type;
1043 	uint16_t		t_tid;
1044 	uint16_t		t_umask;
1045 	char			t_sharename[MAXNAMELEN];
1046 	char			t_resource[MAXPATHLEN];
1047 	char			t_typename[SMB_TYPENAMELEN];
1048 	char			t_volume[SMB_VOLNAMELEN];
1049 	acl_type_t		t_acltype;
1050 	uint32_t		t_access;
1051 	uint32_t		t_execflags;
1052 	time_t			t_connect_time;
1053 	volatile uint32_t	t_open_files;
1054 } smb_tree_t;
1055 
1056 #define	SMB_TREE_VFS(tree)	((tree)->t_snode->vp->v_vfsp)
1057 #define	SMB_TREE_FSID(tree)	((tree)->t_snode->vp->v_vfsp->vfs_fsid)
1058 
1059 #define	SMB_TREE_IS_READONLY(sr)					\
1060 	((sr) != NULL && (sr)->tid_tree != NULL &&			\
1061 	!((sr)->tid_tree->t_access & ACE_ALL_WRITE_PERMS))
1062 
1063 #define	SMB_TREE_IS_CASEINSENSITIVE(sr)                                 \
1064 	(((sr) && (sr)->tid_tree) ?                                     \
1065 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CASEINSENSITIVE) : 0)
1066 
1067 #define	SMB_TREE_HAS_ACCESS(sr, acemask)				\
1068 	((sr) == NULL ? ACE_ALL_PERMS : (				\
1069 	(((sr) && (sr)->tid_tree) ?					\
1070 	(((sr)->tid_tree->t_access) & (acemask)) : 0)))
1071 
1072 #define	SMB_TREE_SUPPORTS_CATIA(sr)            				\
1073 	(((sr) && (sr)->tid_tree) ?                                     \
1074 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CATIA) : 0)
1075 
1076 #define	SMB_TREE_SUPPORTS_ABE(sr)            				\
1077 	(((sr) && (sr)->tid_tree) ?                                     \
1078 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_ABE) : 0)
1079 
1080 #define	SMB_TREE_IS_DFSROOT(sr)            				\
1081 	(((sr) && (sr)->tid_tree) ?                                     \
1082 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_DFSROOT) : 0)
1083 
1084 #define	SMB_TREE_SUPPORTS_SHORTNAMES(sr)				\
1085 	(((sr) && (sr)->tid_tree) ?					\
1086 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_SHORTNAMES) : 0)
1087 
1088 /*
1089  * SMB_TREE_CONTAINS_NODE is used to check if a node is on the same
1090  * file system as the tree's root filesystem, or if mount point traversal
1091  * should be allowed.  Note that this is also called in some cases with
1092  * sr=NULL, where it is expected to evaluate to TRUE.
1093  */
1094 
1095 #define	SMB_TREE_CONTAINS_NODE(sr, node)                                \
1096 	((sr) == NULL || (sr)->tid_tree == NULL ||                      \
1097 	SMB_TREE_VFS((sr)->tid_tree) == SMB_NODE_VFS(node) ||           \
1098 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_TRAVERSE_MOUNTS))
1099 
1100 /*
1101  * SMB_OFILE_IS_READONLY reflects whether an ofile is readonly or not.
1102  * The macro takes into account read-only settings in any of:
1103  * the tree, the node (pending) and the file-system object.
1104  * all of this is evaluated in smb_ofile_open() and after that
1105  * we can just test the f_flags & SMB_OFLAGS_READONLY
1106  */
1107 #define	SMB_OFILE_IS_READONLY(of)	\
1108 	((of)->f_flags & SMB_OFLAGS_READONLY)
1109 
1110 /*
1111  * SMB_PATHFILE_IS_READONLY indicates whether or not a file is
1112  * readonly when the caller has a path rather than an ofile.
1113  */
1114 #define	SMB_PATHFILE_IS_READONLY(sr, node)			\
1115 	(SMB_TREE_IS_READONLY((sr)) ||				\
1116 	smb_node_file_is_readonly((node)))
1117 
1118 #define	SMB_OPIPE_MAGIC		0x50495045	/* 'PIPE' */
1119 #define	SMB_OPIPE_VALID(p)	\
1120     ASSERT(((p) != NULL) && (p)->p_magic == SMB_OPIPE_MAGIC)
1121 
1122 /*
1123  * Data structure for SMB_FTYPE_MESG_PIPE ofiles, which is used
1124  * at the interface between SMB and NDR RPC.
1125  */
1126 typedef struct smb_opipe {
1127 	uint32_t		p_magic;
1128 	list_node_t		p_lnd;
1129 	kmutex_t		p_mutex;
1130 	kcondvar_t		p_cv;
1131 	struct smb_server	*p_server;
1132 	struct smb_event	*p_event;
1133 	char			*p_name;
1134 	uint32_t		p_busy;
1135 	smb_doorhdr_t		p_hdr;
1136 	smb_netuserinfo_t	p_user;
1137 	uint8_t			*p_doorbuf;
1138 	uint8_t			*p_data;
1139 } smb_opipe_t;
1140 
1141 /*
1142  * The of_ftype	of an open file should contain the SMB_FTYPE value
1143  * returned when the file/pipe was opened. The following
1144  * assumptions are currently made:
1145  *
1146  * File Type	    Node       PipeInfo
1147  * ---------	    --------   --------
1148  * SMB_FTYPE_DISK       Valid      Null
1149  * SMB_FTYPE_BYTE_PIPE  Undefined  Undefined
1150  * SMB_FTYPE_MESG_PIPE  Null       Valid
1151  * SMB_FTYPE_PRINTER    Undefined  Undefined
1152  * SMB_FTYPE_UNKNOWN    Undefined  Undefined
1153  */
1154 
1155 /*
1156  * Some flags for ofile structure
1157  *
1158  *	SMB_OFLAGS_SET_DELETE_ON_CLOSE
1159  *   Set this flag when the corresponding open operation whose
1160  *   DELETE_ON_CLOSE bit of the CreateOptions is set. If any
1161  *   open file instance has this bit set, the NODE_FLAGS_DELETE_ON_CLOSE
1162  *   will be set for the file node upon close.
1163  */
1164 
1165 #define	SMB_OFLAGS_READONLY		0x0001
1166 #define	SMB_OFLAGS_EXECONLY		0x0002
1167 #define	SMB_OFLAGS_SET_DELETE_ON_CLOSE	0x0004
1168 #define	SMB_OFLAGS_LLF_POS_VALID	0x0008
1169 
1170 #define	SMB_OFILE_MAGIC 	0x4F464C45	/* 'OFLE' */
1171 #define	SMB_OFILE_VALID(p)	\
1172     ASSERT((p != NULL) && ((p)->f_magic == SMB_OFILE_MAGIC))
1173 
1174 typedef enum {
1175 	SMB_OFILE_STATE_OPEN = 0,
1176 	SMB_OFILE_STATE_CLOSING,
1177 	SMB_OFILE_STATE_CLOSED,
1178 	SMB_OFILE_STATE_SENTINEL
1179 } smb_ofile_state_t;
1180 
1181 typedef struct smb_ofile {
1182 	uint32_t		f_magic;
1183 	kmutex_t		f_mutex;
1184 	list_node_t		f_lnd;
1185 	list_node_t		f_nnd;
1186 	smb_ofile_state_t	f_state;
1187 
1188 	struct smb_server	*f_server;
1189 	smb_session_t		*f_session;
1190 	smb_user_t		*f_user;
1191 	smb_tree_t		*f_tree;
1192 	smb_node_t		*f_node;
1193 	smb_opipe_t		*f_pipe;
1194 
1195 	uint32_t		f_uniqid;
1196 	uint32_t		f_refcnt;
1197 	uint64_t		f_seek_pos;
1198 	uint32_t		f_flags;
1199 	uint32_t		f_granted_access;
1200 	uint32_t		f_share_access;
1201 	uint32_t		f_create_options;
1202 	uint16_t		f_fid;
1203 	uint16_t		f_opened_by_pid;
1204 	uint16_t		f_ftype;
1205 	uint64_t		f_llf_pos;
1206 	int			f_mode;
1207 	cred_t			*f_cr;
1208 	pid_t			f_pid;
1209 	smb_attr_t		f_pending_attr;
1210 	boolean_t		f_written;
1211 	char			f_quota_resume[SMB_SID_STRSZ];
1212 	smb_oplock_grant_t	f_oplock_grant;
1213 } smb_ofile_t;
1214 
1215 #define	SMB_ODIR_MAGIC 		0x4F444952	/* 'ODIR' */
1216 #define	SMB_ODIR_VALID(p)	\
1217     ASSERT((p != NULL) && ((p)->d_magic == SMB_ODIR_MAGIC))
1218 
1219 #define	SMB_ODIR_BUFSIZE	(8 * 1024)
1220 
1221 #define	SMB_ODIR_FLAG_WILDCARDS		0x0001
1222 #define	SMB_ODIR_FLAG_IGNORE_CASE	0x0002
1223 #define	SMB_ODIR_FLAG_XATTR		0x0004
1224 #define	SMB_ODIR_FLAG_EDIRENT		0x0008
1225 #define	SMB_ODIR_FLAG_CATIA		0x0010
1226 #define	SMB_ODIR_FLAG_ABE		0x0020
1227 #define	SMB_ODIR_FLAG_SHORTNAMES	0x0040
1228 
1229 typedef enum {
1230 	SMB_ODIR_STATE_OPEN = 0,
1231 	SMB_ODIR_STATE_IN_USE,
1232 	SMB_ODIR_STATE_CLOSING,
1233 	SMB_ODIR_STATE_CLOSED,
1234 	SMB_ODIR_STATE_SENTINEL
1235 } smb_odir_state_t;
1236 
1237 typedef enum {
1238 	SMB_ODIR_RESUME_CONT,
1239 	SMB_ODIR_RESUME_IDX,
1240 	SMB_ODIR_RESUME_COOKIE,
1241 	SMB_ODIR_RESUME_FNAME
1242 } smb_odir_resume_type_t;
1243 
1244 typedef struct smb_odir_resume {
1245 	smb_odir_resume_type_t	or_type;
1246 	int			or_idx;
1247 	uint32_t		or_cookie;
1248 	char			*or_fname;
1249 } smb_odir_resume_t;
1250 
1251 /*
1252  * Flags used when opening an odir
1253  */
1254 #define	SMB_ODIR_OPENF_BACKUP_INTENT	0x01
1255 
1256 typedef struct smb_odir {
1257 	uint32_t		d_magic;
1258 	kmutex_t		d_mutex;
1259 	list_node_t		d_lnd;
1260 	smb_odir_state_t	d_state;
1261 	smb_session_t		*d_session;
1262 	smb_tree_t		*d_tree;
1263 	smb_node_t		*d_dnode;
1264 	cred_t			*d_cred;
1265 	uint16_t		d_odid;
1266 	uint16_t		d_opened_by_pid;
1267 	uint16_t		d_sattr;
1268 	uint32_t		d_refcnt;
1269 	uint32_t		d_flags;
1270 	boolean_t		d_eof;
1271 	int			d_bufsize;
1272 	uint64_t		d_offset;
1273 	union {
1274 		char		*u_bufptr;
1275 		edirent_t	*u_edp;
1276 		dirent64_t	*u_dp;
1277 	} d_u;
1278 	uint32_t		d_last_cookie;
1279 	uint32_t		d_cookies[SMB_MAX_SEARCH];
1280 	char			d_pattern[MAXNAMELEN];
1281 	char			d_buf[SMB_ODIR_BUFSIZE];
1282 	char			d_last_name[MAXNAMELEN];
1283 } smb_odir_t;
1284 #define	d_bufptr	d_u.u_bufptr
1285 #define	d_edp		d_u.u_edp
1286 #define	d_dp		d_u.u_dp
1287 
1288 typedef struct smb_odirent {
1289 	char		od_name[MAXNAMELEN];	/* on disk name */
1290 	ino64_t		od_ino;
1291 	uint32_t	od_eflags;
1292 } smb_odirent_t;
1293 
1294 typedef struct smb_fileinfo {
1295 	char		fi_name[MAXNAMELEN];
1296 	char		fi_shortname[SMB_SHORTNAMELEN];
1297 	uint32_t	fi_cookie;	/* Dir offset (of next entry) */
1298 	uint32_t	fi_dosattr;	/* DOS attributes */
1299 	uint64_t	fi_nodeid;	/* file system node id */
1300 	uint64_t	fi_size;	/* file size in bytes */
1301 	uint64_t	fi_alloc_size;	/* allocation size in bytes */
1302 	timestruc_t	fi_atime;	/* last access */
1303 	timestruc_t	fi_mtime;	/* last modification */
1304 	timestruc_t	fi_ctime;	/* last status change */
1305 	timestruc_t	fi_crtime;	/* file creation */
1306 } smb_fileinfo_t;
1307 
1308 typedef struct smb_streaminfo {
1309 	uint64_t	si_size;
1310 	uint64_t	si_alloc_size;
1311 	char		si_name[MAXPATHLEN];
1312 } smb_streaminfo_t;
1313 
1314 #define	SMB_LOCK_MAGIC 	0x4C4F434B	/* 'LOCK' */
1315 
1316 typedef struct smb_lock {
1317 	uint32_t		l_magic;
1318 	kmutex_t		l_mutex;
1319 	list_node_t		l_lnd;
1320 	kcondvar_t		l_cv;
1321 
1322 	list_node_t		l_conflict_lnd;
1323 	smb_slist_t		l_conflict_list;
1324 
1325 	smb_session_t		*l_session;
1326 	smb_ofile_t		*l_file;
1327 	struct smb_request	*l_sr;
1328 
1329 	uint32_t		l_flags;
1330 	uint64_t		l_session_kid;
1331 	struct smb_lock		*l_blocked_by; /* Debug info only */
1332 
1333 	uint16_t		l_pid;
1334 	uint16_t		l_uid;
1335 	uint32_t		l_type;
1336 	uint64_t		l_start;
1337 	uint64_t		l_length;
1338 	clock_t			l_end_time;
1339 } smb_lock_t;
1340 
1341 #define	SMB_LOCK_FLAG_INDEFINITE	0x0004
1342 #define	SMB_LOCK_INDEFINITE_WAIT(lock) \
1343 	((lock)->l_flags & SMB_LOCK_FLAG_INDEFINITE)
1344 
1345 #define	SMB_LOCK_TYPE_READWRITE		101
1346 #define	SMB_LOCK_TYPE_READONLY		102
1347 
1348 typedef struct vardata_block {
1349 	uint8_t			vdb_tag;
1350 	uint32_t		vdb_len;
1351 	struct uio 		vdb_uio;
1352 	struct iovec		vdb_iovec[MAX_IOVEC];
1353 } smb_vdb_t;
1354 
1355 #define	SMB_WRMODE_WRITE_THRU	0x0001
1356 #define	SMB_WRMODE_IS_STABLE(M)	((M) & SMB_WRMODE_WRITE_THRU)
1357 
1358 #define	SMB_RW_MAGIC		0x52445257	/* 'RDRW' */
1359 
1360 typedef struct smb_rw_param {
1361 	uint32_t rw_magic;
1362 	smb_vdb_t rw_vdb;
1363 	uint64_t rw_offset;
1364 	uint32_t rw_last_write;
1365 	uint16_t rw_mode;
1366 	uint32_t rw_count;		/* bytes in this request */
1367 	uint16_t rw_mincnt;
1368 	uint32_t rw_total;		/* total bytes (write-raw) */
1369 	uint16_t rw_dsoff;		/* SMB data offset */
1370 	uint8_t rw_andx;		/* SMB secondary andx command */
1371 } smb_rw_param_t;
1372 
1373 typedef struct smb_pathname {
1374 	char	*pn_path;
1375 	char	*pn_pname;
1376 	char	*pn_fname;
1377 	char	*pn_sname;
1378 	char	*pn_stype;
1379 } smb_pathname_t;
1380 
1381 /*
1382  * fs_query_info
1383  */
1384 typedef struct smb_fqi {
1385 	smb_pathname_t	fq_path;
1386 	uint16_t	fq_sattr;
1387 	smb_node_t	*fq_dnode;
1388 	smb_node_t	*fq_fnode;
1389 	smb_attr_t	fq_fattr;
1390 	char		fq_last_comp[MAXNAMELEN];
1391 } smb_fqi_t;
1392 
1393 typedef struct dirop {
1394 	smb_fqi_t	fqi;
1395 	smb_fqi_t	dst_fqi;
1396 	uint16_t	info_level;
1397 	uint16_t	flags;
1398 } smb_arg_dirop_t;
1399 
1400 typedef struct {
1401 	uint32_t status;
1402 	uint16_t errcls;
1403 	uint16_t errcode;
1404 } smb_error_t;
1405 
1406 typedef struct open_param {
1407 	smb_fqi_t	fqi;
1408 	uint16_t	omode;
1409 	uint16_t	ofun;
1410 	uint32_t	nt_flags;
1411 	uint32_t	timeo;
1412 	uint32_t	dattr;
1413 	timestruc_t	crtime;
1414 	timestruc_t	mtime;
1415 	uint64_t	dsize;
1416 	uint32_t	desired_access;
1417 	uint32_t	share_access;
1418 	uint32_t	create_options;
1419 	uint32_t	create_disposition;
1420 	boolean_t	created_readonly;
1421 	uint32_t	ftype;
1422 	uint32_t	devstate;
1423 	uint32_t	action_taken;
1424 	uint64_t	fileid;
1425 	uint32_t	rootdirfid;
1426 	smb_ofile_t	*dir;
1427 	/* This is only set by NTTransactCreate */
1428 	struct smb_sd	*sd;
1429 	uint8_t		op_oplock_level;	/* requested/granted level */
1430 	boolean_t	op_oplock_levelII;	/* TRUE if levelII supported */
1431 } smb_arg_open_t;
1432 
1433 /*
1434  * SMB Request State Machine
1435  * -------------------------
1436  *
1437  *                  T4               +------+		T0
1438  *      +--------------------------->| FREE |---------------------------+
1439  *      |                            +------+                           |
1440  * +-----------+                                                        |
1441  * | COMPLETED |                                                        |
1442  * +-----------+
1443  *      ^                                                               |
1444  *      | T15                      +----------+                         v
1445  * +------------+        T6        |          |                 +--------------+
1446  * | CLEANED_UP |<-----------------| CANCELED |                 | INITIALIZING |
1447  * +------------+                  |          |                 +--------------+
1448  *      |    ^                     +----------+                         |
1449  *      |    |                        ^  ^ ^ ^                          |
1450  *      |    |          +-------------+  | | |                          |
1451  *      |    |    T3    |                | | |               T13        | T1
1452  *      |    +-------------------------+ | | +----------------------+   |
1453  *      +----------------------------+ | | |                        |   |
1454  *         T16          |            | | | +-----------+            |   |
1455  *                      |           \/ | | T5          |            |   v
1456  * +-----------------+  |   T12     +--------+         |     T2    +-----------+
1457  * | EVENT_OCCURRED  |------------->| ACTIVE |<--------------------| SUBMITTED |
1458  * +-----------------+  |           +--------+         |           +-----------+
1459  *        ^             |              | ^ |           |
1460  *        |             |           T8 | | |  T7       |
1461  *        | T10      T9 |   +----------+ | +-------+   |  T11
1462  *        |             |   |            +-------+ |   |
1463  *        |             |   |               T14  | |   |
1464  *        |             |   v                    | v   |
1465  *      +----------------------+                +--------------+
1466  *	|     WAITING_EVENT    |                | WAITING_LOCK |
1467  *      +----------------------+                +--------------+
1468  *
1469  *
1470  *
1471  *
1472  *
1473  * Transition T0
1474  *
1475  * This transition occurs when the request is allocated and is still under the
1476  * control of the session thread.
1477  *
1478  * Transition T1
1479  *
1480  * This transition occurs when the session thread dispatches a task to treat the
1481  * request.
1482  *
1483  * Transition T2
1484  *
1485  *
1486  *
1487  * Transition T3
1488  *
1489  * A request completes and smbsr_cleanup is called to release resources
1490  * associated with the request (but not the smb_request_t itself).  This
1491  * includes references on smb_ofile_t, smb_node_t, and other structures.
1492  * CLEANED_UP state exists to detect if we attempt to cleanup a request
1493  * multiple times and to allow us to detect that we are accessing a
1494  * request that has already been cleaned up.
1495  *
1496  * Transition T4
1497  *
1498  *
1499  *
1500  * Transition T5
1501  *
1502  *
1503  *
1504  * Transition T6
1505  *
1506  *
1507  *
1508  * Transition T7
1509  *
1510  *
1511  *
1512  * Transition T8
1513  *
1514  *
1515  *
1516  * Transition T9
1517  *
1518  *
1519  *
1520  * Transition T10
1521  *
1522  *
1523  *
1524  * Transition T11
1525  *
1526  *
1527  *
1528  * Transition T12
1529  *
1530  *
1531  *
1532  * Transition T13
1533  *
1534  *
1535  *
1536  * Transition T14
1537  *
1538  *
1539  *
1540  * Transition T15
1541  *
1542  * Request processing is completed (control returns from smb_dispatch)
1543  *
1544  * Transition T16
1545  *
1546  * Multipart (andx) request was cleaned up with smbsr_cleanup but more "andx"
1547  * sections remain to be processed.
1548  *
1549  */
1550 
1551 #define	SMB_REQ_MAGIC 		0x534D4252	/* 'SMBR' */
1552 #define	SMB_REQ_VALID(p)	ASSERT((p)->sr_magic == SMB_REQ_MAGIC)
1553 
1554 typedef enum smb_req_state {
1555 	SMB_REQ_STATE_FREE = 0,
1556 	SMB_REQ_STATE_INITIALIZING,
1557 	SMB_REQ_STATE_SUBMITTED,
1558 	SMB_REQ_STATE_ACTIVE,
1559 	SMB_REQ_STATE_WAITING_EVENT,
1560 	SMB_REQ_STATE_EVENT_OCCURRED,
1561 	SMB_REQ_STATE_WAITING_LOCK,
1562 	SMB_REQ_STATE_COMPLETED,
1563 	SMB_REQ_STATE_CANCELED,
1564 	SMB_REQ_STATE_CLEANED_UP,
1565 	SMB_REQ_STATE_SENTINEL
1566 } smb_req_state_t;
1567 
1568 typedef struct smb_request {
1569 	uint32_t		sr_magic;
1570 	kmutex_t		sr_mutex;
1571 	list_node_t		sr_session_lnd;
1572 	smb_req_state_t		sr_state;
1573 	kmem_cache_t		*sr_cache;
1574 	struct smb_server	*sr_server;
1575 	pid_t			*sr_pid;
1576 	int32_t			sr_gmtoff;
1577 	smb_session_t		*session;
1578 	smb_kmod_cfg_t		*sr_cfg;
1579 	smb_notify_change_req_t	sr_ncr;
1580 
1581 	/* Info from session service header */
1582 	uint32_t		sr_req_length; /* Excluding NBT header */
1583 
1584 	/* Request buffer excluding NBT header */
1585 	void			*sr_request_buf;
1586 
1587 	smb_lock_t		*sr_awaiting;
1588 	struct mbuf_chain	command;
1589 	struct mbuf_chain	reply;
1590 	struct mbuf_chain	raw_data;
1591 	list_t			sr_storage;
1592 	struct smb_xa		*r_xa;
1593 	int			andx_prev_wct;
1594 	int 			cur_reply_offset;
1595 	int			orig_request_hdr;
1596 	unsigned int		reply_seqnum;	/* reply sequence number */
1597 	unsigned char		first_smb_com;	/* command code */
1598 	unsigned char		smb_com;	/* command code */
1599 
1600 	uint8_t			smb_rcls;	/* error code class */
1601 	uint8_t			smb_reh;	/* rsvd (AH DOS INT-24 ERR) */
1602 	uint16_t		smb_err;	/* error code */
1603 	smb_error_t		smb_error;
1604 
1605 	uint8_t			smb_flg;	/* flags */
1606 	uint16_t		smb_flg2;	/* flags */
1607 	uint16_t		smb_pid_high;	/* high part of pid */
1608 	unsigned char		smb_sig[8];	/* signiture */
1609 	uint16_t		smb_tid;	/* tree id #  */
1610 	uint16_t		smb_pid;	/* caller's process id # */
1611 	uint16_t		smb_uid;	/* user id # */
1612 	uint16_t		smb_mid;	/* mutiplex id #  */
1613 	unsigned char		smb_wct;	/* count of parameter words */
1614 	uint16_t		smb_bcc;	/* data byte count */
1615 
1616 	/* Parameters */
1617 	struct mbuf_chain	smb_vwv;	/* variable width value */
1618 
1619 	/* Data */
1620 	struct mbuf_chain	smb_data;
1621 
1622 	uint16_t		smb_fid;	/* not in hdr, but common */
1623 
1624 	unsigned char		andx_com;
1625 	uint16_t		andx_off;
1626 
1627 	struct smb_tree		*tid_tree;
1628 	struct smb_ofile	*fid_ofile;
1629 	smb_user_t		*uid_user;
1630 
1631 	union {
1632 		smb_arg_negotiate_t	*negprot;
1633 		smb_arg_sessionsetup_t	*ssetup;
1634 		smb_arg_tcon_t		tcon;
1635 		smb_arg_dirop_t		dirop;
1636 		smb_arg_open_t		open;
1637 		smb_rw_param_t		*rw;
1638 		int32_t			timestamp;
1639 	} arg;
1640 
1641 	cred_t			*user_cr;
1642 	kthread_t		*sr_worker;
1643 	hrtime_t		sr_time_submitted;
1644 	hrtime_t		sr_time_active;
1645 	hrtime_t		sr_time_start;
1646 	int32_t			sr_txb;
1647 	uint32_t		sr_seqnum;
1648 } smb_request_t;
1649 
1650 #define	sr_ssetup	arg.ssetup
1651 #define	sr_negprot	arg.negprot
1652 #define	sr_tcon		arg.tcon
1653 #define	sr_dirop	arg.dirop
1654 #define	sr_open		arg.open
1655 #define	sr_rw		arg.rw
1656 #define	sr_timestamp	arg.timestamp
1657 
1658 #define	SMB_READ_PROTOCOL(hdr) \
1659 	LE_IN32(((smb_hdr_t *)(hdr))->protocol)
1660 
1661 #define	SMB_PROTOCOL_MAGIC_INVALID(rd_sr) \
1662 	(SMB_READ_PROTOCOL((rd_sr)->sr_request_buf) != SMB_PROTOCOL_MAGIC)
1663 
1664 #define	SMB_READ_COMMAND(hdr) \
1665 	(((smb_hdr_t *)(hdr))->command)
1666 
1667 #define	SMB_IS_WRITERAW(rd_sr) \
1668 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_WRITE_RAW)
1669 
1670 #define	SMB_IS_NT_CANCEL(rd_sr) \
1671 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_NT_CANCEL)
1672 
1673 #define	SMB_IS_SESSION_SETUP_ANDX(rd_sr) \
1674 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == \
1675 	    SMB_COM_SESSION_SETUP_ANDX)
1676 
1677 #define	SMB_IS_NT_NEGOTIATE(rd_sr) \
1678 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_NEGOTIATE)
1679 
1680 #define	SMB_IS_TREE_CONNECT_ANDX(rd_sr) \
1681 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_TREE_CONNECT_ANDX)
1682 
1683 #define	SMB_XA_FLAG_OPEN	0x0001
1684 #define	SMB_XA_FLAG_CLOSE	0x0002
1685 #define	SMB_XA_FLAG_COMPLETE	0x0004
1686 #define	SMB_XA_CLOSED(xa) (!((xa)->xa_flags & SMB_XA_FLAG_OPEN))
1687 
1688 #define	SMB_XA_MAGIC		0x534D4258	/* 'SMBX' */
1689 
1690 typedef struct smb_xa {
1691 	uint32_t		xa_magic;
1692 	kmutex_t		xa_mutex;
1693 	list_node_t		xa_lnd;
1694 
1695 	uint32_t		xa_refcnt;
1696 	uint32_t		xa_flags;
1697 
1698 	struct smb_session	*xa_session;
1699 
1700 	unsigned char		smb_com;	/* which TRANS type */
1701 	unsigned char		smb_flg;	/* flags */
1702 	uint16_t		smb_flg2;	/* flags */
1703 	uint16_t		smb_tid;	/* tree id number */
1704 	uint16_t		smb_pid;	/* caller's process id number */
1705 	uint16_t		smb_uid;	/* user id number */
1706 	uint32_t		smb_func;	/* NT_TRANS function */
1707 
1708 	uint16_t		xa_smb_mid;	/* mutiplex id number */
1709 	uint16_t		xa_smb_fid;	/* TRANS2 secondary */
1710 
1711 	unsigned int		reply_seqnum;	/* reply sequence number */
1712 
1713 	uint32_t	smb_tpscnt;	/* total parameter bytes being sent */
1714 	uint32_t	smb_tdscnt;	/* total data bytes being sent */
1715 	uint32_t	smb_mprcnt;	/* max parameter bytes to return */
1716 	uint32_t	smb_mdrcnt;	/* max data bytes to return */
1717 	uint32_t	smb_msrcnt;	/* max setup words to return */
1718 	uint32_t	smb_flags;	/* additional information: */
1719 				/*  bit 0 - if set, disconnect TID in smb_tid */
1720 				/*  bit 1 - if set, transaction is one way */
1721 				/*  (no final response) */
1722 	int32_t	smb_timeout;	/* number of milliseconds to await completion */
1723 	uint32_t	smb_suwcnt;	/* set up word count */
1724 
1725 	char			*xa_pipe_name;
1726 
1727 	/*
1728 	 * These are the param and data count received so far,
1729 	 * used to decide if the whole trans is here yet.
1730 	 */
1731 	int			req_disp_param;
1732 	int			req_disp_data;
1733 
1734 	struct mbuf_chain	req_setup_mb;
1735 	struct mbuf_chain	req_param_mb;
1736 	struct mbuf_chain	req_data_mb;
1737 
1738 	struct mbuf_chain	rep_setup_mb;
1739 	struct mbuf_chain	rep_param_mb;
1740 	struct mbuf_chain	rep_data_mb;
1741 } smb_xa_t;
1742 
1743 
1744 #define	SDDF_NO_FLAGS			0
1745 #define	SDDF_SUPPRESS_TID		0x0001
1746 #define	SDDF_SUPPRESS_UID		0x0002
1747 
1748 /*
1749  * SMB dispatch return codes.
1750  */
1751 typedef enum {
1752 	SDRC_SUCCESS = 0,
1753 	SDRC_ERROR,
1754 	SDRC_DROP_VC,
1755 	SDRC_NO_REPLY,
1756 	SDRC_SR_KEPT,
1757 	SDRC_NOT_IMPLEMENTED
1758 } smb_sdrc_t;
1759 
1760 #define	VAR_BCC		((short)-1)
1761 
1762 #define	SMB_SERVER_MAGIC	0x53534552	/* 'SSER' */
1763 #define	SMB_SERVER_VALID(s)	\
1764     ASSERT(((s) != NULL) && ((s)->sv_magic == SMB_SERVER_MAGIC))
1765 
1766 #define	SMB_LISTENER_MAGIC	0x4C53544E	/* 'LSTN' */
1767 #define	SMB_LISTENER_VALID(ld)	\
1768     ASSERT(((ld) != NULL) && ((ld)->ld_magic == SMB_LISTENER_MAGIC))
1769 
1770 typedef struct {
1771 	uint32_t		ld_magic;
1772 	struct smb_server	*ld_sv;
1773 	smb_thread_t		ld_thread;
1774 	ksocket_t		ld_so;
1775 	in_port_t		ld_port;
1776 	int			ld_family;
1777 	struct sockaddr_in	ld_sin;
1778 	struct sockaddr_in6	ld_sin6;
1779 	smb_llist_t		ld_session_list;
1780 } smb_listener_daemon_t;
1781 
1782 #define	SMB_SSETUP_CMD			"authentication"
1783 #define	SMB_TCON_CMD			"share mapping"
1784 #define	SMB_OPIPE_CMD			"pipe open"
1785 #define	SMB_THRESHOLD_REPORT_THROTTLE	50
1786 typedef struct smb_cmd_threshold {
1787 	char			*ct_cmd;
1788 	kmutex_t		ct_mutex;
1789 	volatile uint32_t	ct_active_cnt;
1790 	volatile uint32_t	ct_blocked_cnt;
1791 	volatile uint32_t	ct_error_cnt;
1792 	uint32_t		ct_threshold;
1793 	struct smb_event	*ct_event;
1794 	uint32_t		ct_event_id;
1795 } smb_cmd_threshold_t;
1796 
1797 typedef struct {
1798 	kstat_named_t		ls_files;
1799 	kstat_named_t		ls_trees;
1800 	kstat_named_t		ls_users;
1801 } smb_server_legacy_kstat_t;
1802 
1803 typedef enum smb_server_state {
1804 	SMB_SERVER_STATE_CREATED = 0,
1805 	SMB_SERVER_STATE_CONFIGURED,
1806 	SMB_SERVER_STATE_RUNNING,
1807 	SMB_SERVER_STATE_STOPPING,
1808 	SMB_SERVER_STATE_DELETING,
1809 	SMB_SERVER_STATE_SENTINEL
1810 } smb_server_state_t;
1811 
1812 typedef struct {
1813 	/* protected by sv_mutex */
1814 	kcondvar_t		sp_cv;
1815 	uint32_t 		sp_cnt;
1816 	smb_llist_t		sp_list;
1817 	smb_llist_t		sp_fidlist;
1818 } smb_spool_t;
1819 
1820 #define	SMB_SERVER_STATE_VALID(S)               \
1821     ASSERT(((S) == SMB_SERVER_STATE_CREATED) || \
1822 	    ((S) == SMB_SERVER_STATE_CONFIGURED) || \
1823 	    ((S) == SMB_SERVER_STATE_RUNNING) ||    \
1824 	    ((S) == SMB_SERVER_STATE_STOPPING) ||   \
1825 	    ((S) == SMB_SERVER_STATE_DELETING))
1826 
1827 typedef struct smb_server {
1828 	uint32_t		sv_magic;
1829 	kcondvar_t		sv_cv;
1830 	kmutex_t		sv_mutex;
1831 	list_node_t		sv_lnd;
1832 	smb_server_state_t	sv_state;
1833 	uint32_t		sv_refcnt;
1834 	pid_t			sv_pid;
1835 	zoneid_t		sv_zid;
1836 	smb_listener_daemon_t	sv_nbt_daemon;
1837 	smb_listener_daemon_t	sv_tcp_daemon;
1838 	krwlock_t		sv_cfg_lock;
1839 	smb_kmod_cfg_t		sv_cfg;
1840 	smb_session_t		*sv_session;
1841 
1842 	door_handle_t		sv_lmshrd;
1843 
1844 	int32_t			si_gmtoff;
1845 
1846 	smb_thread_t		si_thread_timers;
1847 
1848 	taskq_t			*sv_worker_pool;
1849 	taskq_t			*sv_receiver_pool;
1850 
1851 	kmem_cache_t		*si_cache_request;
1852 	kmem_cache_t		*si_cache_session;
1853 	kmem_cache_t		*si_cache_user;
1854 	kmem_cache_t		*si_cache_tree;
1855 	kmem_cache_t		*si_cache_ofile;
1856 	kmem_cache_t		*si_cache_odir;
1857 	kmem_cache_t		*si_cache_opipe;
1858 	kmem_cache_t		*si_cache_event;
1859 
1860 	smb_node_t		*si_root_smb_node;
1861 	smb_llist_t		sv_opipe_list;
1862 	smb_llist_t		sv_event_list;
1863 
1864 	/* Statistics */
1865 	hrtime_t		sv_start_time;
1866 	kstat_t			*sv_ksp;
1867 	volatile uint32_t	sv_nbt_sess;
1868 	volatile uint32_t	sv_tcp_sess;
1869 	volatile uint32_t	sv_users;
1870 	volatile uint32_t	sv_trees;
1871 	volatile uint32_t	sv_files;
1872 	volatile uint32_t	sv_pipes;
1873 	volatile uint64_t	sv_txb;
1874 	volatile uint64_t	sv_rxb;
1875 	volatile uint64_t	sv_nreq;
1876 	smb_srqueue_t		sv_srqueue;
1877 	smb_spool_t		sp_info;
1878 	smb_cmd_threshold_t	sv_ssetup_ct;
1879 	smb_cmd_threshold_t	sv_tcon_ct;
1880 	smb_cmd_threshold_t	sv_opipe_ct;
1881 	kstat_t			*sv_legacy_ksp;
1882 	kmutex_t		sv_legacy_ksmtx;
1883 } smb_server_t;
1884 
1885 #define	SMB_EVENT_MAGIC		0x45564E54	/* EVNT */
1886 #define	SMB_EVENT_TIMEOUT	45		/* seconds */
1887 #define	SMB_EVENT_VALID(e)	\
1888     ASSERT(((e) != NULL) && ((e)->se_magic == SMB_EVENT_MAGIC))
1889 typedef struct smb_event {
1890 	uint32_t		se_magic;
1891 	list_node_t		se_lnd;
1892 	kmutex_t		se_mutex;
1893 	kcondvar_t		se_cv;
1894 	smb_server_t		*se_server;
1895 	uint32_t		se_txid;
1896 	boolean_t		se_notified;
1897 	int			se_waittime;
1898 	int			se_timeout;
1899 	int			se_errno;
1900 } smb_event_t;
1901 
1902 typedef struct smb_kspooldoc {
1903 	uint32_t	sd_magic;
1904 	list_node_t	sd_lnd;
1905 	smb_inaddr_t	sd_ipaddr;
1906 	uint32_t	sd_spool_num;
1907 	uint16_t	sd_fid;
1908 	char		sd_username[MAXNAMELEN];
1909 	char		sd_path[MAXPATHLEN];
1910 } smb_kspooldoc_t;
1911 
1912 typedef struct smb_spoolfid {
1913 	uint32_t	sf_magic;
1914 	list_node_t	sf_lnd;
1915 	uint16_t	sf_fid;
1916 } smb_spoolfid_t;
1917 
1918 #define	SMB_INFO_NETBIOS_SESSION_SVC_RUNNING	0x0001
1919 #define	SMB_INFO_NETBIOS_SESSION_SVC_FAILED	0x0002
1920 #define	SMB_INFO_USER_LEVEL_SECURITY		0x40000000
1921 #define	SMB_INFO_ENCRYPT_PASSWORDS		0x80000000
1922 
1923 #define	SMB_NEW_KID()	atomic_inc_64_nv(&smb_kids)
1924 #define	SMB_UNIQ_FID()	atomic_inc_32_nv(&smb_fids)
1925 
1926 #define	SMB_IS_STREAM(node) ((node)->n_unode)
1927 
1928 typedef struct smb_tsd {
1929 	void (*proc)();
1930 	void *arg;
1931 	char name[100];
1932 } smb_tsd_t;
1933 
1934 typedef struct smb_disp_entry {
1935 	char		sdt_name[KSTAT_STRLEN];
1936 	smb_sdrc_t	(*sdt_pre_op)(smb_request_t *);
1937 	smb_sdrc_t	(*sdt_function)(smb_request_t *);
1938 	void		(*sdt_post_op)(smb_request_t *);
1939 	uint8_t		sdt_com;
1940 	char		sdt_dialect;
1941 	uint8_t		sdt_flags;
1942 	volatile uint64_t sdt_txb;
1943 	volatile uint64_t sdt_rxb;
1944 	smb_latency_t	sdt_lat;
1945 } smb_disp_entry_t;
1946 
1947 typedef struct smb_xlate {
1948 	int	code;
1949 	char	*str;
1950 } smb_xlate_t;
1951 
1952 typedef struct smb_export {
1953 	kmutex_t	e_mutex;
1954 	boolean_t	e_ready;
1955 	smb_llist_t	e_vfs_list;
1956 	smb_avl_t	e_share_avl;
1957 	smb_slist_t	e_unexport_list;
1958 
1959 	kmem_cache_t	*e_cache_share;
1960 	kmem_cache_t	*e_cache_vfs;
1961 	kmem_cache_t	*e_cache_unexport;
1962 
1963 	smb_thread_t	e_unexport_thread;
1964 } smb_export_t;
1965 
1966 /*
1967  * This structure is a helper for building RAP NetShareEnum response
1968  *
1969  * es_posix_uid UID of the user requesting the shares list which
1970  *              is used to detect if the user has any autohome
1971  * es_bufsize   size of the response buffer
1972  * es_buf       pointer to the response buffer
1973  * es_ntotal    total number of shares exported by server which
1974  *              their OEM names is less then 13 chars
1975  * es_nsent     number of shares that can fit in the specified buffer
1976  * es_datasize  actual data size (share's data) which was encoded
1977  *              in the response buffer
1978  */
1979 typedef struct smb_enumshare_info {
1980 	uid_t		es_posix_uid;
1981 	uint16_t	es_bufsize;
1982 	char		*es_buf;
1983 	uint16_t	es_ntotal;
1984 	uint16_t	es_nsent;
1985 	uint16_t	es_datasize;
1986 } smb_enumshare_info_t;
1987 
1988 #ifdef	__cplusplus
1989 }
1990 #endif
1991 
1992 #endif /* _SMBSRV_SMB_KTYPES_H */
1993