xref: /illumos-gate/usr/src/uts/common/smbsrv/smb_ktypes.h (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
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/ntifs.h>
62 
63 struct smb_disp_entry;
64 struct smb_request;
65 struct smb_server;
66 
67 int smb_noop(void *, size_t, int);
68 
69 #define	SMB_AUDIT_STACK_DEPTH	16
70 #define	SMB_AUDIT_BUF_MAX_REC	16
71 #define	SMB_AUDIT_NODE		0x00000001
72 
73 /*
74  * Maximum number of records returned in SMBsearch, SMBfind
75  * and SMBfindunique response. Value set to 10 for compatibility
76  * with Windows.
77  */
78 #define	SMB_MAX_SEARCH		10
79 
80 #define	SMB_SEARCH_ATTRIBUTES    \
81 	(FILE_ATTRIBUTE_HIDDEN | \
82 	FILE_ATTRIBUTE_SYSTEM |  \
83 	FILE_ATTRIBUTE_DIRECTORY)
84 
85 #define	SMB_SEARCH_HIDDEN(sattr) ((sattr) & FILE_ATTRIBUTE_HIDDEN)
86 #define	SMB_SEARCH_SYSTEM(sattr) ((sattr) & FILE_ATTRIBUTE_SYSTEM)
87 #define	SMB_SEARCH_DIRECTORY(sattr) ((sattr) & FILE_ATTRIBUTE_DIRECTORY)
88 #define	SMB_SEARCH_ALL(sattr) ((sattr) & SMB_SEARCH_ATTRIBUTES)
89 
90 typedef struct {
91 	uint32_t		anr_refcnt;
92 	int			anr_depth;
93 	pc_t			anr_stack[SMB_AUDIT_STACK_DEPTH];
94 } smb_audit_record_node_t;
95 
96 typedef struct {
97 	int			anb_index;
98 	int			anb_max_index;
99 	smb_audit_record_node_t	anb_records[SMB_AUDIT_BUF_MAX_REC];
100 } smb_audit_buf_node_t;
101 
102 #define	SMB_WORKER_PRIORITY	99
103 /*
104  * Thread State Machine
105  * --------------------
106  *
107  *			    T5			   T0
108  * smb_thread_destroy()	<-------+		+------- smb_thread_init()
109  *                              |		|
110  *				|		v
111  *			+-----------------------------+
112  *			|   SMB_THREAD_STATE_EXITED   |<---+
113  *			+-----------------------------+	   |
114  *				      | T1		   |
115  *				      v			   |
116  *			+-----------------------------+	   |
117  *			|  SMB_THREAD_STATE_STARTING  |	   |
118  *			+-----------------------------+	   |
119  *				     | T2		   | T4
120  *				     v			   |
121  *			+-----------------------------+	   |
122  *			|  SMB_THREAD_STATE_RUNNING   |	   |
123  *			+-----------------------------+	   |
124  *				     | T3		   |
125  *				     v			   |
126  *			+-----------------------------+	   |
127  *			|  SMB_THREAD_STATE_EXITING   |----+
128  *			+-----------------------------+
129  *
130  * Transition T0
131  *
132  *    This transition is executed in smb_thread_init().
133  *
134  * Transition T1
135  *
136  *    This transition is executed in smb_thread_start().
137  *
138  * Transition T2
139  *
140  *    This transition is executed by the thread itself when it starts running.
141  *
142  * Transition T3
143  *
144  *    This transition is executed by the thread itself in
145  *    smb_thread_entry_point() just before calling thread_exit().
146  *
147  *
148  * Transition T4
149  *
150  *    This transition is executed in smb_thread_stop().
151  *
152  * Transition T5
153  *
154  *    This transition is executed in smb_thread_destroy().
155  *
156  * Comments
157  * --------
158  *
159  *    The field smb_thread_aw_t contains a function pointer that knows how to
160  *    awake the thread. It is a temporary solution to work around the fact that
161  *    kernel threads (not part of a userspace process) cannot be signaled.
162  */
163 typedef enum smb_thread_state {
164 	SMB_THREAD_STATE_STARTING = 0,
165 	SMB_THREAD_STATE_RUNNING,
166 	SMB_THREAD_STATE_EXITING,
167 	SMB_THREAD_STATE_EXITED
168 } smb_thread_state_t;
169 
170 struct _smb_thread;
171 
172 typedef void (*smb_thread_ep_t)(struct _smb_thread *, void *ep_arg);
173 typedef void (*smb_thread_aw_t)(struct _smb_thread *, void *aw_arg);
174 
175 #define	SMB_THREAD_MAGIC	0x534D4254	/* SMBT */
176 
177 typedef struct _smb_thread {
178 	uint32_t		sth_magic;
179 	char			sth_name[16];
180 	smb_thread_state_t	sth_state;
181 	kthread_t		*sth_th;
182 	kt_did_t		sth_did;
183 	smb_thread_ep_t		sth_ep;
184 	void			*sth_ep_arg;
185 	smb_thread_aw_t		sth_aw;
186 	void			*sth_aw_arg;
187 	boolean_t		sth_kill;
188 	kmutex_t		sth_mtx;
189 	kcondvar_t		sth_cv;
190 } smb_thread_t;
191 
192 /*
193  * Pool of IDs
194  * -----------
195  *
196  *    A pool of IDs is a pool of 16 bit numbers. It is implemented as a bitmap.
197  *    A bit set to '1' indicates that that particular value has been allocated.
198  *    The allocation process is done shifting a bit through the whole bitmap.
199  *    The current position of that index bit is kept in the smb_idpool_t
200  *    structure and represented by a byte index (0 to buffer size minus 1) and
201  *    a bit index (0 to 7).
202  *
203  *    The pools start with a size of 8 bytes or 64 IDs. Each time the pool runs
204  *    out of IDs its current size is doubled until it reaches its maximum size
205  *    (8192 bytes or 65536 IDs). The IDs 0 and 65535 are never given out which
206  *    means that a pool can have a maximum number of 65534 IDs available.
207  */
208 #define	SMB_IDPOOL_MAGIC	0x4944504C	/* IDPL */
209 #define	SMB_IDPOOL_MIN_SIZE	64	/* Number of IDs to begin with */
210 #define	SMB_IDPOOL_MAX_SIZE	64 * 1024
211 
212 typedef struct smb_idpool {
213 	uint32_t	id_magic;
214 	kmutex_t	id_mutex;
215 	uint8_t		*id_pool;
216 	uint32_t	id_size;
217 	uint8_t		id_bit;
218 	uint8_t		id_bit_idx;
219 	uint32_t	id_idx;
220 	uint32_t	id_idx_msk;
221 	uint32_t	id_free_counter;
222 	uint32_t	id_max_free_counter;
223 } smb_idpool_t;
224 
225 /*
226  * Maximum size of a Transport Data Unit when CAP_LARGE_READX and
227  * CAP_LARGE_WRITEX are not set.  CAP_LARGE_READX/CAP_LARGE_WRITEX
228  * allow the payload to exceed the negotiated buffer size.
229  *     4 --> NBT/TCP Transport Header.
230  *    32 --> SMB Header
231  *     1 --> Word Count byte
232  *   510 --> Maximum Number of bytes of the Word Table (2 * 255)
233  *     2 --> Byte count of the data
234  * 65535 --> Maximum size of the data
235  * -----
236  * 66084
237  */
238 #define	SMB_REQ_MAX_SIZE	66560		/* 65KB */
239 #define	SMB_XPRT_MAX_SIZE	(SMB_REQ_MAX_SIZE + NETBIOS_HDR_SZ)
240 
241 #define	SMB_TXREQ_MAGIC		0X54524251	/* 'TREQ' */
242 typedef struct {
243 	uint32_t	tr_magic;
244 	list_node_t	tr_lnd;
245 	int		tr_len;
246 	uint8_t		tr_buf[SMB_XPRT_MAX_SIZE];
247 } smb_txreq_t;
248 
249 #define	SMB_TXLST_MAGIC		0X544C5354	/* 'TLST' */
250 typedef struct {
251 	uint32_t	tl_magic;
252 	kmutex_t	tl_mutex;
253 	boolean_t	tl_active;
254 	list_t		tl_list;
255 } smb_txlst_t;
256 
257 /*
258  * Maximum buffer size for NT is 37KB.  If all clients are Windows 2000, this
259  * can be changed to 64KB.  37KB must be used with a mix of NT/Windows 2000
260  * clients because NT loses directory entries when values greater than 37KB are
261  * used.
262  *
263  * Note: NBT_MAXBUF will be subtracted from the specified max buffer size to
264  * account for the NBT header.
265  */
266 #define	NBT_MAXBUF		8
267 #define	SMB_NT_MAXBUF		(37 * 1024)
268 
269 #define	OUTBUFSIZE		(65 * 1024)
270 #define	SMBHEADERSIZE		32
271 #define	SMBND_HASH_MASK		(0xFF)
272 #define	MAX_IOVEC		512
273 #define	MAX_READREF		(8 * 1024)
274 
275 #define	SMB_WORKER_MIN		4
276 #define	SMB_WORKER_DEFAULT	64
277 #define	SMB_WORKER_MAX		1024
278 
279 /*
280  * Fix align a pointer or offset appropriately so that fields will not
281  * cross word boundaries.
282  */
283 #define	PTRALIGN(x) \
284 	(((uintptr_t)(x) + (uintptr_t)(_POINTER_ALIGNMENT) - 1l) & \
285 	    ~((uintptr_t)(_POINTER_ALIGNMENT) - 1l))
286 
287 /*
288  * native os types are defined in win32/smbinfo.h
289  */
290 
291 /*
292  * All 4 different time / date formats that will bee seen in SMB
293  */
294 typedef struct {
295 	uint16_t	Day	: 5;
296 	uint16_t	Month	: 4;
297 	uint16_t	Year	: 7;
298 } SMB_DATE;
299 
300 typedef struct {
301 	uint16_t	TwoSeconds : 5;
302 	uint16_t	Minutes	   : 6;
303 	uint16_t	Hours	   : 5;
304 } SMB_TIME;
305 
306 
307 typedef uint32_t 	UTIME;		/* seconds since Jan 1 1970 */
308 
309 typedef struct smb_malloc_list {
310 	struct smb_malloc_list	*forw;
311 	struct smb_malloc_list	*back;
312 } smb_malloc_list;
313 
314 typedef struct smb_llist {
315 	krwlock_t	ll_lock;
316 	list_t		ll_list;
317 	uint32_t	ll_count;
318 	uint64_t	ll_wrop;
319 } smb_llist_t;
320 
321 typedef struct smb_slist {
322 	kmutex_t	sl_mutex;
323 	kcondvar_t	sl_cv;
324 	list_t		sl_list;
325 	uint32_t	sl_count;
326 	boolean_t	sl_waiting;
327 } smb_slist_t;
328 
329 typedef struct smb_session_list {
330 	krwlock_t	se_lock;
331 	uint64_t	se_wrop;
332 	struct {
333 		list_t		lst;
334 		uint32_t	count;
335 	} se_rdy;
336 	struct {
337 		list_t		lst;
338 		uint32_t	count;
339 	} se_act;
340 } smb_session_list_t;
341 
342 typedef struct {
343 	kcondvar_t	rwx_cv;
344 	kmutex_t	rwx_mutex;
345 	krwlock_t	rwx_lock;
346 	boolean_t	rwx_waiting;
347 } smb_rwx_t;
348 
349 /* NOTIFY CHANGE */
350 
351 typedef struct smb_notify_change_req {
352 	list_node_t		nc_lnd;
353 	struct smb_node		*nc_node;
354 	uint32_t		nc_reply_type;
355 	uint32_t		nc_flags;
356 } smb_notify_change_req_t;
357 
358 /*
359  * SMB operates over a NetBIOS-over-TCP transport (NBT) or directly
360  * over TCP, which is also known as direct hosted NetBIOS-less SMB
361  * or SMB-over-TCP.
362  *
363  * NBT messages have a 4-byte header that defines the message type
364  * (8-bits), a 7-bit flags field and a 17-bit length.
365  *
366  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
367  * |      TYPE     |     FLAGS   |E|            LENGTH             |
368  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
369  *
370  * 8-bit type      Defined in RFC 1002
371  * 7-bit flags     Bits 0-6 reserved (must be 0)
372  *                 Bit 7: Length extension bit (E)
373  * 17-bit length   Includes bit 7 of the flags byte
374  *
375  *
376  * SMB-over-TCP is defined to use a modified version of the NBT header
377  * containing an 8-bit message type and 24-bit message length.
378  *
379  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
380  * |      TYPE     |                  LENGTH                       |
381  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
382  *
383  * 8-bit type      Must be 0
384  * 24-bit length
385  *
386  * The following structure is used to represent a generic, in-memory
387  * SMB transport header; it is not intended to map directly to either
388  * of the over-the-wire formats.
389  */
390 typedef struct {
391 	uint8_t		xh_type;
392 	uint32_t	xh_length;
393 } smb_xprt_t;
394 
395 int MBC_LENGTH(struct mbuf_chain *);
396 int MBC_MAXBYTES(struct mbuf_chain *);
397 void MBC_SETUP(struct mbuf_chain *, uint32_t);
398 void MBC_INIT(struct mbuf_chain *, uint32_t);
399 void MBC_FLUSH(struct mbuf_chain *);
400 void MBC_ATTACH_MBUF(struct mbuf_chain *, struct mbuf *);
401 void MBC_APPEND_MBUF(struct mbuf_chain *, struct mbuf *);
402 void MBC_ATTACH_BUF(struct mbuf_chain *MBC, unsigned char *BUF, int LEN);
403 int MBC_SHADOW_CHAIN(struct mbuf_chain *SUBMBC, struct mbuf_chain *MBC,
404     int OFF, int LEN);
405 
406 #define	MBC_ROOM_FOR(b, n) (((b)->chain_offset + (n)) <= (b)->max_bytes)
407 
408 /*
409  * ol_sess_id:
410  *
411  *	ID of the session holding the oplock (if an oplock was granted).
412  *
413  * ol_xthread:
414  *
415  *	Worker thread treating the command that was granted the oplock. Until
416  *	that thread is done with that command and has submitted the response
417  *	to the network stack, all the other threads will be suspended in
418  *	smb_oplock_enter(). They will be awaken when the worker thread
419  *	referenced in 'ol_xthread' calls smb_oplock_broadcast().
420  *
421  *	The purpose of this mechanism is to prevent another thread from
422  *	triggering a oplock break before the response conveying the grant
423  *	has been sent.
424  *
425  * ol_ofile
426  *
427  *	Open file that was granted the oplock.
428  *
429  * ol_waiters_count
430  *
431  *	Number of threads waiting for a call to smb_oplock_broadcast().
432  *
433  * ol_level
434  *
435  *	Level of the oplock granted.
436  */
437 typedef struct smb_oplock {
438 	uint64_t		ol_sess_id;
439 	kcondvar_t		ol_cv;
440 	kthread_t		*ol_xthread;
441 	struct smb_ofile	*ol_ofile;
442 	uint8_t			ol_level;
443 } smb_oplock_t;
444 
445 #define	DOS_ATTR_VALID	0x80000000
446 
447 #define	SMB_VFS_MAGIC	0x534D4256	/* 'SMBV' */
448 
449 typedef struct smb_vfs {
450 	uint32_t		sv_magic;
451 	list_node_t		sv_lnd;
452 	uint32_t		sv_refcnt;
453 	vfs_t			*sv_vfsp;
454 	vnode_t			*sv_rootvp;
455 } smb_vfs_t;
456 
457 typedef struct smb_unexport {
458 	list_node_t	ux_lnd;
459 	char		ux_sharename[MAXNAMELEN];
460 } smb_unexport_t;
461 
462 /*
463  * Solaris file systems handle timestamps differently from NTFS.
464  * In order to provide a more similar view of an open file's
465  * timestamps, we cache the timestamps in the node and manipulate
466  * them in a manner more consistent with windows.
467  * t_cached is B_TRUE when timestamps are cached.
468  * Timestamps remain cached while there are open ofiles for the node.
469  * This includes open ofiles for named streams.  t_open_ofiles is a
470  * count of open ofiles on the node, including named streams' ofiles,
471  * n_open_ofiles cannot be used as it doesn't include ofiles opened
472  * for the node's named streams.
473  */
474 typedef struct smb_times {
475 	uint32_t		t_open_ofiles;
476 	boolean_t		t_cached;
477 	timestruc_t		t_atime;
478 	timestruc_t		t_mtime;
479 	timestruc_t		t_ctime;
480 	timestruc_t		t_crtime;
481 } smb_times_t;
482 
483 #define	SMB_NODE_MAGIC		0x4E4F4445	/* 'NODE' */
484 #define	SMB_NODE_VALID(p)	ASSERT((p)->n_magic == SMB_NODE_MAGIC)
485 
486 typedef enum {
487 	SMB_NODE_STATE_AVAILABLE = 0,
488 	SMB_NODE_STATE_OPLOCK_GRANTED,
489 	SMB_NODE_STATE_OPLOCK_BREAKING,
490 	SMB_NODE_STATE_DESTROYING
491 } smb_node_state_t;
492 
493 typedef struct smb_node {
494 	uint32_t		n_magic;
495 	krwlock_t		n_lock;
496 	kmutex_t		n_mutex;
497 	list_node_t		n_lnd;
498 	smb_node_state_t	n_state;
499 	uint32_t		n_refcnt;
500 	uint32_t		n_hashkey;
501 	smb_llist_t		*n_hash_bucket;
502 	uint32_t		n_orig_uid;
503 	uint32_t		n_open_count;
504 	smb_llist_t		n_ofile_list;
505 	smb_llist_t		n_lock_list;
506 	struct smb_ofile	*readonly_creator;
507 	volatile int		flags;	/* FILE_NOTIFY_CHANGE_* */
508 	volatile int		waiting_event; /* # of clients requesting FCN */
509 	smb_times_t		n_timestamps; /* cached timestamps */
510 	smb_oplock_t		n_oplock;
511 	struct smb_node		*n_dnode; /* directory node */
512 	struct smb_node		*n_unode; /* unnamed stream node */
513 	/* Credentials for delayed delete */
514 	cred_t			*delete_on_close_cred;
515 	uint32_t		n_delete_on_close_flags;
516 	char			od_name[MAXNAMELEN];
517 	vnode_t			*vp;
518 	smb_audit_buf_node_t	*n_audit_buf;
519 } smb_node_t;
520 
521 #define	NODE_FLAGS_NOTIFY_CHANGE	0x10000fff
522 #define	NODE_OPLOCKS_IN_FORCE		0x0000f000
523 #define	NODE_OPLOCK_NONE		0x00000000
524 #define	NODE_EXCLUSIVE_OPLOCK		0x00001000
525 #define	NODE_BATCH_OPLOCK		0x00002000
526 #define	NODE_LEVEL_II_OPLOCK		0x00003000
527 #define	NODE_CAP_LEVEL_II		0x00010000
528 #define	NODE_PROTOCOL_LOCK		0x00020000
529 #define	NODE_FLAGS_WRITE_THROUGH	0x00100000
530 #define	NODE_FLAGS_SYNCATIME		0x00200000
531 #define	NODE_FLAGS_LOCKED		0x00400000
532 #define	NODE_XATTR_DIR			0x01000000
533 #define	NODE_FLAGS_CREATED		0x04000000
534 #define	NODE_FLAGS_CHANGED		0x08000000
535 #define	NODE_FLAGS_WATCH_TREE		0x10000000
536 #define	NODE_FLAGS_SET_SIZE		0x20000000
537 #define	NODE_FLAGS_DELETE_ON_CLOSE	0x40000000
538 #define	NODE_FLAGS_EXECUTABLE		0x80000000
539 
540 #define	OPLOCK_TYPE(n)			((n)->flags & NODE_OPLOCKS_IN_FORCE)
541 #define	OPLOCKS_IN_FORCE(n)		(OPLOCK_TYPE(n) != NODE_OPLOCK_NONE)
542 #define	EXCLUSIVE_OPLOCK_IN_FORCE(n)	\
543 	(OPLOCK_TYPE(n) == NODE_EXCLUSIVE_OPLOCK)
544 #define	BATCH_OPLOCK_IN_FORCE(n)	(OPLOCK_TYPE(n) == NODE_BATCH_OPLOCK)
545 #define	LEVEL_II_OPLOCK_IN_FORCE(n)	(OPLOCK_TYPE(n) == NODE_LEVEL_II_OPLOCK)
546 
547 #define	SMB_NODE_VFS(node)	((node)->vp->v_vfsp)
548 #define	SMB_NODE_FSID(node)	((node)->vp->v_vfsp->vfs_fsid)
549 
550 /*
551  * Based on section 2.6.1.2 (Connection Management) of the June 13,
552  * 1996 CIFS spec, a server may terminate the transport connection
553  * due to inactivity. The client software is expected to be able to
554  * automatically reconnect to the server if this happens. Like much
555  * of the useful background information, this section appears to
556  * have been dropped from later revisions of the document.
557  *
558  * Each session has an activity timestamp that's updated whenever a
559  * request is dispatched. If the session is idle, i.e. receives no
560  * requests, for SMB_SESSION_INACTIVITY_TIMEOUT minutes it will be
561  * closed.
562  *
563  * Each session has an I/O semaphore to serialize communication with
564  * the client. For example, after receiving a raw-read request, the
565  * server is not allowed to send an oplock break to the client until
566  * after it has sent the raw-read data.
567  */
568 #define	SMB_SESSION_INACTIVITY_TIMEOUT		(15 * 60)
569 
570 #define	SMB_SESSION_OFILE_MAX				(16 * 1024)
571 
572 /*
573  * When a connection is set up we need to remember both the client
574  * (peer) IP address and the local IP address used to establish the
575  * connection. When a client connects with a vc number of zero, we
576  * are supposed to abort any existing connections with that client
577  * (see notes in smb_negotiate.c and smb_session_setup_andx.c). For
578  * servers with multiple network interfaces or IP aliases, however,
579  * each interface has to be managed independently since the client
580  * is not aware of the server configuration. We have to allow the
581  * client to establish a connection on each interface with a vc
582  * number of zero without aborting the other connections.
583  *
584  * ipaddr:       the client (peer) IP address for the session.
585  * local_ipaddr: the local IP address used to connect to the server.
586  */
587 
588 #define	SMB_MAC_KEYSZ	512
589 
590 struct smb_sign {
591 	unsigned int seqnum;
592 	unsigned int mackey_len;
593 	unsigned int flags;
594 	unsigned char mackey[SMB_MAC_KEYSZ];
595 };
596 
597 #define	SMB_SIGNING_ENABLED	1
598 #define	SMB_SIGNING_CHECK	2
599 
600 /*
601  * Session State Machine
602  * ---------------------
603  *
604  * +-----------------------------+	     +------------------------------+
605  * | SMB_SESSION_STATE_CONNECTED |           | SMB_SESSION_STATE_TERMINATED |
606  * +-----------------------------+           +------------------------------+
607  *		T0|					     ^
608  *		  +--------------------+		     |T13
609  *		  v		       |T14                  |
610  * +-------------------------------+   |    +--------------------------------+
611  * | SMB_SESSION_STATE_ESTABLISHED |---+--->| SMB_SESSION_STATE_DISCONNECTED |
612  * +-------------------------------+        +--------------------------------+
613  *		T1|				^	   ^ ^ ^
614  *		  +----------+			|T9        | | |
615  *                           v			|          | | |
616  *                  +------------------------------+       | | |
617  *                  | SMB_SESSION_STATE_NEGOTIATED |       | | |
618  *                  +------------------------------+       | | |
619  *	                 ^|   ^|   | ^                     | | |
620  *      +----------------+|   ||   | |                     | | |
621  *      |+----------------+   || T7| |T8                   | | |
622  *      ||                    ||   | |                     | | |
623  *      ||   +----------------+|   | |                     | | |
624  *      ||   |+----------------+   | |                     | | |
625  *	||   ||			   v |                     | | |
626  *      ||   ||   +-----------------------------------+ T10| | |
627  *      ||   ||   | SMB_SESSION_STATE_OPLOCK_BREAKING |----+ | |
628  *      ||   ||   +-----------------------------------+      | |
629  *	||   ||T5                                            | |
630  *      ||   |+-->+-----------------------------------+	  T11| |
631  *      ||   |T6  | SMB_SESSION_STATE_READ_RAW_ACTIVE |------+ |
632  *      ||   +----+-----------------------------------+        |
633  *	||T3                                                   |
634  *      |+------->+------------------------------------+    T12|
635  *      |T4       | SMB_SESSION_STATE_WRITE_RAW_ACTIVE |-------+
636  *      +---------+------------------------------------+
637  *
638  * Transition T0
639  *
640  *
641  *
642  * Transition T1
643  *
644  *
645  *
646  * Transition T2
647  *
648  *
649  *
650  * Transition T3
651  *
652  *
653  *
654  * Transition T4
655  *
656  *
657  *
658  * Transition T5
659  *
660  *
661  *
662  * Transition T6
663  *
664  *
665  *
666  * Transition T7
667  *
668  *
669  *
670  * Transition T8
671  *
672  *
673  *
674  * Transition T9
675  *
676  *
677  *
678  * Transition T10
679  *
680  *
681  *
682  * Transition T11
683  *
684  *
685  *
686  * Transition T12
687  *
688  *
689  *
690  * Transition T13
691  *
692  *
693  *
694  * Transition T14
695  *
696  *
697  *
698  */
699 #define	SMB_SESSION_MAGIC	0x53455353	/* 'SESS' */
700 #define	SMB_SESSION_VALID(p)	ASSERT((p)->s_magic == SMB_SESSION_MAGIC)
701 
702 typedef enum {
703 	SMB_SESSION_STATE_INITIALIZED = 0,
704 	SMB_SESSION_STATE_DISCONNECTED,
705 	SMB_SESSION_STATE_CONNECTED,
706 	SMB_SESSION_STATE_ESTABLISHED,
707 	SMB_SESSION_STATE_NEGOTIATED,
708 	SMB_SESSION_STATE_OPLOCK_BREAKING,
709 	SMB_SESSION_STATE_WRITE_RAW_ACTIVE,
710 	SMB_SESSION_STATE_READ_RAW_ACTIVE,
711 	SMB_SESSION_STATE_TERMINATED,
712 	SMB_SESSION_STATE_SENTINEL
713 } smb_session_state_t;
714 
715 typedef struct smb_session {
716 	uint32_t		s_magic;
717 	smb_rwx_t		s_lock;
718 	list_node_t		s_lnd;
719 	uint64_t		s_kid;
720 	smb_session_state_t	s_state;
721 	uint32_t		s_flags;
722 	int			s_write_raw_status;
723 	kthread_t		*s_thread;
724 	kt_did_t		s_ktdid;
725 	smb_kmod_cfg_t		s_cfg;
726 	kmem_cache_t		*s_cache;
727 	kmem_cache_t		*s_cache_request;
728 	struct smb_server	*s_server;
729 	int32_t			s_gmtoff;
730 	uint32_t		keep_alive;
731 	uint64_t		opentime;
732 	uint16_t		vcnumber;
733 	uint16_t		s_local_port;
734 	smb_inaddr_t		ipaddr;
735 	smb_inaddr_t		local_ipaddr;
736 	char 			workstation[SMB_PI_MAX_HOST];
737 	int			dialect;
738 	int			native_os;
739 	uint32_t		capabilities;
740 	struct smb_sign		signing;
741 
742 	ksocket_t		sock;
743 
744 	smb_slist_t		s_req_list;
745 	smb_llist_t		s_xa_list;
746 	smb_llist_t		s_user_list;
747 	smb_idpool_t		s_uid_pool;
748 	smb_txlst_t		s_txlst;
749 
750 	volatile uint32_t	s_tree_cnt;
751 	volatile uint32_t	s_file_cnt;
752 	volatile uint32_t	s_dir_cnt;
753 
754 	uint16_t		secmode;
755 	uint32_t		sesskey;
756 	uint32_t		challenge_len;
757 	unsigned char		challenge_key[8];
758 	unsigned char		MAC_key[44];
759 	int64_t			activity_timestamp;
760 	/*
761 	 * Maximum negotiated buffer size between SMB client and server
762 	 * in SMB_SESSION_SETUP_ANDX
763 	 */
764 	uint16_t		smb_msg_size;
765 	uchar_t			*outpipe_data;
766 	int			outpipe_datalen;
767 	int			outpipe_cookie;
768 	list_t			s_oplock_brkreqs;
769 } smb_session_t;
770 
771 #define	SMB_USER_MAGIC 0x55534552	/* 'USER' */
772 
773 #define	SMB_USER_FLAG_GUEST			SMB_ATF_GUEST
774 #define	SMB_USER_FLAG_IPC			SMB_ATF_ANON
775 #define	SMB_USER_FLAG_ADMIN			SMB_ATF_ADMIN
776 #define	SMB_USER_FLAG_POWER_USER		SMB_ATF_POWERUSER
777 #define	SMB_USER_FLAG_BACKUP_OPERATOR		SMB_ATF_BACKUPOP
778 
779 #define	SMB_USER_PRIV_TAKE_OWNERSHIP	0x00000001
780 #define	SMB_USER_PRIV_BACKUP		0x00000002
781 #define	SMB_USER_PRIV_RESTORE		0x00000004
782 #define	SMB_USER_PRIV_SECURITY		0x00000008
783 
784 
785 typedef enum {
786 	SMB_USER_STATE_LOGGED_IN = 0,
787 	SMB_USER_STATE_LOGGING_OFF,
788 	SMB_USER_STATE_LOGGED_OFF,
789 	SMB_USER_STATE_SENTINEL
790 } smb_user_state_t;
791 
792 typedef struct smb_user {
793 	uint32_t		u_magic;
794 	list_node_t		u_lnd;
795 	kmutex_t		u_mutex;
796 	smb_user_state_t	u_state;
797 
798 	struct smb_server	*u_server;
799 	smb_session_t		*u_session;
800 	uint16_t		u_name_len;
801 	char			*u_name;
802 	uint16_t		u_domain_len;
803 	char			*u_domain;
804 	time_t			u_logon_time;
805 	cred_t			*u_cred;
806 	cred_t			*u_privcred;
807 
808 	smb_llist_t		u_tree_list;
809 	smb_idpool_t		u_tid_pool;
810 
811 	uint32_t		u_refcnt;
812 	uint32_t		u_flags;
813 	uint32_t		u_privileges;
814 	uint16_t		u_uid;
815 	uint32_t		u_audit_sid;
816 } smb_user_t;
817 
818 #define	SMB_TREE_MAGIC			0x54524545	/* 'TREE' */
819 
820 #define	SMB_TYPENAMELEN			_ST_FSTYPSZ
821 #define	SMB_VOLNAMELEN			32
822 
823 #define	SMB_TREE_READONLY		0x00000001
824 #define	SMB_TREE_SUPPORTS_ACLS		0x00000002
825 #define	SMB_TREE_STREAMS		0x00000004
826 #define	SMB_TREE_CASEINSENSITIVE	0x00000008
827 #define	SMB_TREE_NO_CASESENSITIVE	0x00000010
828 #define	SMB_TREE_NO_EXPORT		0x00000020
829 #define	SMB_TREE_NO_OPLOCKS		0x00000040
830 #define	SMB_TREE_NO_ATIME		0x00000080
831 #define	SMB_TREE_XVATTR			0x00000100
832 #define	SMB_TREE_DIRENTFLAGS		0x00000200
833 #define	SMB_TREE_ACLONCREATE		0x00000400
834 #define	SMB_TREE_ACEMASKONACCESS	0x00000800
835 #define	SMB_TREE_NFS_MOUNTED		0x00001000
836 #define	SMB_TREE_UNICODE_ON_DISK	0x00002000
837 #define	SMB_TREE_CATIA			0x00004000
838 
839 typedef enum {
840 	SMB_TREE_STATE_CONNECTED = 0,
841 	SMB_TREE_STATE_DISCONNECTING,
842 	SMB_TREE_STATE_DISCONNECTED,
843 	SMB_TREE_STATE_SENTINEL
844 } smb_tree_state_t;
845 
846 typedef struct smb_tree {
847 	uint32_t		t_magic;
848 	kmutex_t		t_mutex;
849 	list_node_t		t_lnd;
850 	smb_tree_state_t	t_state;
851 
852 	struct smb_server	*t_server;
853 	smb_session_t		*t_session;
854 	smb_user_t		*t_user;
855 	smb_node_t		*t_snode;
856 
857 	smb_llist_t		t_ofile_list;
858 	smb_idpool_t		t_fid_pool;
859 
860 	smb_llist_t		t_odir_list;
861 	smb_idpool_t		t_odid_pool;
862 
863 	uint32_t		t_refcnt;
864 	uint32_t		t_flags;
865 	int32_t			t_res_type;
866 	uint16_t		t_tid;
867 	uint16_t		t_umask;
868 	char			t_sharename[MAXNAMELEN];
869 	char			t_resource[MAXPATHLEN];
870 	char			t_typename[SMB_TYPENAMELEN];
871 	char			t_volume[SMB_VOLNAMELEN];
872 	acl_type_t		t_acltype;
873 	uint32_t		t_access;
874 	uint32_t		t_shr_flags;
875 	time_t			t_connect_time;
876 	volatile uint32_t	t_open_files;
877 } smb_tree_t;
878 
879 #define	SMB_TREE_VFS(tree)	((tree)->t_snode->vp->v_vfsp)
880 #define	SMB_TREE_FSID(tree)	((tree)->t_snode->vp->v_vfsp->vfs_fsid)
881 
882 #define	SMB_TREE_IS_READONLY(sr)					\
883 	((sr) != NULL && (sr)->tid_tree != NULL &&			\
884 	!((sr)->tid_tree->t_access & ACE_ALL_WRITE_PERMS))
885 
886 #define	SMB_TREE_IS_CASEINSENSITIVE(sr)                                 \
887 	(((sr) && (sr)->tid_tree) ?                                     \
888 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CASEINSENSITIVE) : 0)
889 
890 #define	SMB_TREE_HAS_ACCESS(sr, acemask)				\
891 	((sr) == NULL ? ACE_ALL_PERMS : (				\
892 	(((sr) && (sr)->tid_tree) ?					\
893 	(((sr)->tid_tree->t_access) & (acemask)) : 0)))
894 
895 #define	SMB_TREE_SUPPORTS_CATIA(sr)            				\
896 	(((sr) && (sr)->tid_tree) ?                                     \
897 	smb_tree_has_feature((sr)->tid_tree, SMB_TREE_CATIA) : 0)
898 
899 /*
900  * SMB_TREE_CONTAINS_NODE is used to check that a node is in the same
901  * file system as the tree.
902  */
903 #define	SMB_TREE_CONTAINS_NODE(sr, node)                                \
904 	(((sr) && (sr)->tid_tree) ?                                     \
905 	(SMB_TREE_VFS((sr)->tid_tree) == SMB_NODE_VFS(node)) : 1)
906 
907 /*
908  * SMB_OFILE_IS_READONLY reflects whether an ofile is readonly or not.
909  * The macro takes into account
910  *      - the tree readonly state
911  *      - the node readonly state
912  *      - whether the specified ofile is the readonly creator
913  * The readonly creator has write permission until the ofile is closed.
914  */
915 
916 #define	SMB_OFILE_IS_READONLY(of)                               \
917 	(((of)->f_flags & SMB_OFLAGS_READONLY) ||               \
918 	smb_node_file_is_readonly((of)->f_node) ||                   \
919 	(((of)->f_node->readonly_creator) &&                    \
920 	((of)->f_node->readonly_creator != (of))))
921 
922 /*
923  * SMB_PATHFILE_IS_READONLY indicates whether or not a file is
924  * readonly when the caller has a path rather than an ofile.  Unlike
925  * SMB_OFILE_IS_READONLY, the caller cannot be the readonly creator,
926  * since that requires an ofile.
927  */
928 
929 #define	SMB_PATHFILE_IS_READONLY(sr, node)                       \
930 	(SMB_TREE_IS_READONLY((sr)) ||                           \
931 	smb_node_file_is_readonly((node)) ||                          \
932 	((node)->readonly_creator))
933 
934 #define	PIPE_STATE_AUTH_VERIFY	0x00000001
935 
936 /*
937  * Data structure for SMB_FTYPE_MESG_PIPE ofiles, which is used
938  * at the interface between SMB and NDR RPC.
939  */
940 typedef struct smb_opipe {
941 	kmutex_t p_mutex;
942 	kcondvar_t p_cv;
943 	char *p_name;
944 	uint32_t p_busy;
945 	smb_opipe_hdr_t p_hdr;
946 	smb_netuserinfo_t p_user;
947 	uint8_t *p_doorbuf;
948 	uint8_t *p_data;
949 } smb_opipe_t;
950 
951 /*
952  * The of_ftype	of an open file should contain the SMB_FTYPE value
953  * (cifs.h) returned when the file/pipe was opened. The following
954  * assumptions are currently made:
955  *
956  * File Type	    Node       PipeInfo
957  * ---------	    --------   --------
958  * SMB_FTYPE_DISK       Valid      Null
959  * SMB_FTYPE_BYTE_PIPE  Undefined  Undefined
960  * SMB_FTYPE_MESG_PIPE  Null       Valid
961  * SMB_FTYPE_PRINTER    Undefined  Undefined
962  * SMB_FTYPE_UNKNOWN    Undefined  Undefined
963  */
964 
965 /*
966  * Some flags for ofile structure
967  *
968  *	SMB_OFLAGS_SET_DELETE_ON_CLOSE
969  *   Set this flag when the corresponding open operation whose
970  *   DELETE_ON_CLOSE bit of the CreateOptions is set. If any
971  *   open file instance has this bit set, the NODE_FLAGS_DELETE_ON_CLOSE
972  *   will be set for the file node upon close.
973  *
974  *	SMB_OFLAGS_TIMESTAMPS_PENDING
975  *   This flag gets set when a write operation is performed on the
976  *   ofile. The timestamps will be updated, and the flags cleared,
977  *   when the ofile gets closed or a setattr is performed on the ofile.
978  */
979 
980 #define	SMB_OFLAGS_READONLY		0x0001
981 #define	SMB_OFLAGS_EXECONLY		0x0002
982 #define	SMB_OFLAGS_SET_DELETE_ON_CLOSE	0x0004
983 #define	SMB_OFLAGS_LLF_POS_VALID	0x0008
984 #define	SMB_OFLAGS_TIMESTAMPS_PENDING	0x0010
985 
986 #define	SMB_OFILE_MAGIC 	0x4F464C45	/* 'OFLE' */
987 #define	SMB_OFILE_VALID(p)	ASSERT((p)->f_magic == SMB_OFILE_MAGIC)
988 
989 typedef enum {
990 	SMB_OFILE_STATE_OPEN = 0,
991 	SMB_OFILE_STATE_CLOSING,
992 	SMB_OFILE_STATE_CLOSED,
993 	SMB_OFILE_STATE_SENTINEL
994 } smb_ofile_state_t;
995 
996 typedef struct smb_ofile {
997 	uint32_t		f_magic;
998 	kmutex_t		f_mutex;
999 	list_node_t		f_lnd;
1000 	list_node_t		f_nnd;
1001 	smb_ofile_state_t	f_state;
1002 
1003 	struct smb_server	*f_server;
1004 	smb_session_t		*f_session;
1005 	smb_user_t		*f_user;
1006 	smb_tree_t		*f_tree;
1007 	smb_node_t		*f_node;
1008 	smb_opipe_t		*f_pipe;
1009 
1010 	uint32_t		f_uniqid;
1011 	uint32_t		f_refcnt;
1012 	uint64_t		f_seek_pos;
1013 	uint32_t		f_flags;
1014 	uint32_t		f_granted_access;
1015 	uint32_t		f_share_access;
1016 	uint32_t		f_create_options;
1017 	uint16_t		f_fid;
1018 	uint16_t		f_opened_by_pid;
1019 	uint16_t		f_ftype;
1020 	uint64_t		f_llf_pos;
1021 	int			f_mode;
1022 	cred_t			*f_cr;
1023 	pid_t			f_pid;
1024 	boolean_t		f_oplock_granted;
1025 	boolean_t		f_oplock_exit;
1026 	uint32_t		f_explicit_times;
1027 
1028 } smb_ofile_t;
1029 
1030 #define	SMB_ODIR_MAGIC 		0x4F444952	/* 'ODIR' */
1031 #define	SMB_ODIR_BUFSIZE	(8 * 1024)
1032 
1033 #define	SMB_ODIR_FLAG_WILDCARDS		0x0001
1034 #define	SMB_ODIR_FLAG_IGNORE_CASE	0x0002
1035 #define	SMB_ODIR_FLAG_XATTR		0x0004
1036 #define	SMB_ODIR_FLAG_EDIRENT		0x0008
1037 #define	SMB_ODIR_FLAG_CATIA		0x0010
1038 
1039 typedef enum {
1040 	SMB_ODIR_STATE_OPEN = 0,
1041 	SMB_ODIR_STATE_IN_USE,
1042 	SMB_ODIR_STATE_CLOSING,
1043 	SMB_ODIR_STATE_CLOSED,
1044 	SMB_ODIR_STATE_SENTINEL
1045 } smb_odir_state_t;
1046 
1047 typedef enum {
1048 	SMB_ODIR_RESUME_IDX,
1049 	SMB_ODIR_RESUME_COOKIE,
1050 	SMB_ODIR_RESUME_FNAME
1051 } smb_odir_resume_type_t;
1052 
1053 typedef struct smb_odir_resume {
1054 	smb_odir_resume_type_t	or_type;
1055 	int			or_idx;
1056 	uint32_t		or_cookie;
1057 	char			*or_fname;
1058 } smb_odir_resume_t;
1059 
1060 /*
1061  * Flags used when opening an odir
1062  */
1063 #define	SMB_ODIR_OPENF_BACKUP_INTENT	0x01
1064 
1065 typedef struct smb_odir {
1066 	uint32_t		d_magic;
1067 	kmutex_t		d_mutex;
1068 	list_node_t		d_lnd;
1069 	smb_odir_state_t	d_state;
1070 	smb_session_t		*d_session;
1071 	smb_tree_t		*d_tree;
1072 	smb_node_t		*d_dnode;
1073 	cred_t			*d_cred;
1074 	uint16_t		d_odid;
1075 	uint16_t		d_opened_by_pid;
1076 	uint16_t		d_sattr;
1077 	uint32_t		d_refcnt;
1078 	uint32_t		d_flags;
1079 	boolean_t		d_eof;
1080 	int			d_bufsize;
1081 	uint64_t		d_offset;
1082 	union {
1083 		char		*u_bufptr;
1084 		edirent_t	*u_edp;
1085 		dirent64_t	*u_dp;
1086 	} d_u;
1087 	uint32_t		d_cookies[SMB_MAX_SEARCH];
1088 	char			d_pattern[MAXNAMELEN];
1089 	char			d_buf[SMB_ODIR_BUFSIZE];
1090 } smb_odir_t;
1091 #define	d_bufptr	d_u.u_bufptr
1092 #define	d_edp		d_u.u_edp
1093 #define	d_dp		d_u.u_dp
1094 
1095 typedef struct smb_odirent {
1096 	char		od_name[MAXNAMELEN];	/* on disk name */
1097 	ino64_t		od_ino;
1098 	uint32_t	od_eflags;
1099 } smb_odirent_t;
1100 
1101 typedef struct smb_fileinfo {
1102 	char		fi_name[MAXNAMELEN];
1103 	char		fi_name83[SMB_SHORTNAMELEN];
1104 	char		fi_shortname[SMB_SHORTNAMELEN];
1105 	uint32_t	fi_cookie;
1106 	uint32_t	fi_dosattr;	/* DOS attributes */
1107 	uint64_t	fi_nodeid;	/* file system node id */
1108 	uint64_t	fi_size;	/* file size in bytes */
1109 	uint64_t	fi_alloc_size;	/* allocation size in bytes */
1110 	timestruc_t	fi_atime;	/* last access */
1111 	timestruc_t	fi_mtime;	/* last modification */
1112 	timestruc_t	fi_ctime;	/* last status change */
1113 	timestruc_t	fi_crtime;	/* file creation */
1114 } smb_fileinfo_t;
1115 
1116 typedef struct smb_streaminfo {
1117 	uint64_t	si_size;
1118 	uint64_t	si_alloc_size;
1119 	char		si_name[MAXPATHLEN];
1120 } smb_streaminfo_t;
1121 
1122 #define	SMB_LOCK_MAGIC 	0x4C4F434B	/* 'LOCK' */
1123 
1124 typedef struct smb_lock {
1125 	uint32_t		l_magic;
1126 	kmutex_t		l_mutex;
1127 	list_node_t		l_lnd;
1128 	kcondvar_t		l_cv;
1129 
1130 	list_node_t		l_conflict_lnd;
1131 	smb_slist_t		l_conflict_list;
1132 
1133 	smb_session_t		*l_session;
1134 	smb_ofile_t		*l_file;
1135 	struct smb_request	*l_sr;
1136 
1137 	uint32_t		l_flags;
1138 	uint64_t		l_session_kid;
1139 	struct smb_lock		*l_blocked_by; /* Debug info only */
1140 
1141 	uint16_t		l_pid;
1142 	uint16_t		l_uid;
1143 	uint32_t		l_type;
1144 	uint64_t		l_start;
1145 	uint64_t		l_length;
1146 	clock_t			l_end_time;
1147 } smb_lock_t;
1148 
1149 #define	SMB_LOCK_FLAG_INDEFINITE	0x0004
1150 #define	SMB_LOCK_INDEFINITE_WAIT(lock) \
1151 	((lock)->l_flags & SMB_LOCK_FLAG_INDEFINITE)
1152 
1153 #define	SMB_LOCK_TYPE_READWRITE		101
1154 #define	SMB_LOCK_TYPE_READONLY		102
1155 
1156 typedef struct vardata_block {
1157 	uint8_t			vdb_tag;
1158 	uint32_t		vdb_len;
1159 	struct uio 		vdb_uio;
1160 	struct iovec		vdb_iovec[MAX_IOVEC];
1161 } smb_vdb_t;
1162 
1163 #define	SMB_RW_MAGIC		0x52445257	/* 'RDRW' */
1164 
1165 typedef struct smb_rw_param {
1166 	uint32_t rw_magic;
1167 	smb_vdb_t rw_vdb;
1168 	uint64_t rw_offset;
1169 	uint32_t rw_last_write;
1170 	uint16_t rw_mode;
1171 	uint32_t rw_count;
1172 	uint16_t rw_mincnt;
1173 	uint16_t rw_dsoff;		/* SMB data offset */
1174 	uint8_t rw_andx;		/* SMB secondary andx command */
1175 } smb_rw_param_t;
1176 
1177 typedef struct smb_pathname {
1178 	char	*pn_path;
1179 	char	*pn_pname;
1180 	char	*pn_fname;
1181 	char	*pn_sname;
1182 	char	*pn_stype;
1183 } smb_pathname_t;
1184 
1185 /*
1186  * fs_query_info
1187  */
1188 typedef struct smb_fqi {
1189 	smb_pathname_t	fq_path;
1190 	uint16_t	fq_sattr;
1191 	smb_node_t	*fq_dnode;
1192 	smb_node_t	*fq_fnode;
1193 	smb_attr_t	fq_fattr;
1194 	char		fq_last_comp[MAXNAMELEN];
1195 	char		fq_od_name[MAXNAMELEN];
1196 } smb_fqi_t;
1197 
1198 #define	SMB_NULL_FQI_NODES(fqi) \
1199 	(fqi).fq_fnode = NULL;	\
1200 	(fqi).fq_dnode = NULL;
1201 
1202 #define	FQM_DIR_MUST_EXIST	1
1203 #define	FQM_PATH_MUST_EXIST	2
1204 #define	FQM_PATH_MUST_NOT_EXIST 3
1205 
1206 #define	OPLOCK_MIN_TIMEOUT	(5 * 1000)
1207 #define	OPLOCK_STD_TIMEOUT	(15 * 1000)
1208 #define	OPLOCK_RETRIES		2
1209 
1210 typedef struct {
1211 	uint32_t severity;
1212 	uint32_t status;
1213 	uint16_t errcls;
1214 	uint16_t errcode;
1215 } smb_error_t;
1216 
1217 typedef struct open_param {
1218 	smb_fqi_t	fqi;
1219 	uint16_t	omode;
1220 	uint16_t	ofun;
1221 	uint32_t	nt_flags;
1222 	uint32_t	timeo;
1223 	uint32_t	dattr;
1224 	timestruc_t	crtime;
1225 	timestruc_t	mtime;
1226 	uint64_t	dsize;
1227 	uint32_t	desired_access;
1228 	uint32_t	share_access;
1229 	uint32_t	create_options;
1230 	uint32_t	create_disposition;
1231 	boolean_t	created_readonly;
1232 	uint32_t	ftype;
1233 	uint32_t	devstate;
1234 	uint32_t	action_taken;
1235 	uint64_t	fileid;
1236 	uint32_t	rootdirfid;
1237 	/* This is only set by NTTransactCreate */
1238 	struct smb_sd	*sd;
1239 	uint8_t		op_oplock_level;
1240 } open_param_t;
1241 
1242 #define	SMB_OPLOCK_NONE		0
1243 #define	SMB_OPLOCK_EXCLUSIVE	1
1244 #define	SMB_OPLOCK_BATCH	2
1245 #define	SMB_OPLOCK_LEVEL_II	3
1246 
1247 /*
1248  * SMB Request State Machine
1249  * -------------------------
1250  *
1251  *                  T4               +------+		T0
1252  *      +--------------------------->| FREE |---------------------------+
1253  *      |                            +------+                           |
1254  * +-----------+                                                        |
1255  * | COMPLETED |                                                        |
1256  * +-----------+
1257  *      ^                                                               |
1258  *      | T15                      +----------+                         v
1259  * +------------+        T6        |          |                 +--------------+
1260  * | CLEANED_UP |<-----------------| CANCELED |                 | INITIALIZING |
1261  * +------------+                  |          |                 +--------------+
1262  *      |    ^                     +----------+                         |
1263  *      |    |                        ^  ^ ^ ^                          |
1264  *      |    |          +-------------+  | | |                          |
1265  *      |    |    T3    |                | | |               T13        | T1
1266  *      |    +-------------------------+ | | +----------------------+   |
1267  *      +----------------------------+ | | |                        |   |
1268  *         T16          |            | | | +-----------+            |   |
1269  *                      |           \/ | | T5          |            |   v
1270  * +-----------------+  |   T12     +--------+         |     T2    +-----------+
1271  * | EVENT_OCCURRED  |------------->| ACTIVE |<--------------------| SUBMITTED |
1272  * +-----------------+  |           +--------+         |           +-----------+
1273  *        ^             |              | ^ |           |
1274  *        |             |           T8 | | |  T7       |
1275  *        | T10      T9 |   +----------+ | +-------+   |  T11
1276  *        |             |   |            +-------+ |   |
1277  *        |             |   |               T14  | |   |
1278  *        |             |   v                    | v   |
1279  *      +----------------------+                +--------------+
1280  *	|     WAITING_EVENT    |                | WAITING_LOCK |
1281  *      +----------------------+                +--------------+
1282  *
1283  *
1284  *
1285  *
1286  *
1287  * Transition T0
1288  *
1289  * This transition occurs when the request is allocated and is still under the
1290  * control of the session thread.
1291  *
1292  * Transition T1
1293  *
1294  * This transition occurs when the session thread dispatches a task to treat the
1295  * request.
1296  *
1297  * Transition T2
1298  *
1299  *
1300  *
1301  * Transition T3
1302  *
1303  * A request completes and smbsr_cleanup is called to release resources
1304  * associated with the request (but not the smb_request_t itself).  This
1305  * includes references on smb_ofile_t, smb_node_t, and other structures.
1306  * CLEANED_UP state exists to detect if we attempt to cleanup a request
1307  * multiple times and to allow us to detect that we are accessing a
1308  * request that has already been cleaned up.
1309  *
1310  * Transition T4
1311  *
1312  *
1313  *
1314  * Transition T5
1315  *
1316  *
1317  *
1318  * Transition T6
1319  *
1320  *
1321  *
1322  * Transition T7
1323  *
1324  *
1325  *
1326  * Transition T8
1327  *
1328  *
1329  *
1330  * Transition T9
1331  *
1332  *
1333  *
1334  * Transition T10
1335  *
1336  *
1337  *
1338  * Transition T11
1339  *
1340  *
1341  *
1342  * Transition T12
1343  *
1344  *
1345  *
1346  * Transition T13
1347  *
1348  *
1349  *
1350  * Transition T14
1351  *
1352  *
1353  *
1354  * Transition T15
1355  *
1356  * Request processing is completed (control returns from smb_dispatch)
1357  *
1358  * Transition T16
1359  *
1360  * Multipart (andx) request was cleaned up with smbsr_cleanup but more "andx"
1361  * sections remain to be processed.
1362  *
1363  */
1364 
1365 #define	SMB_REQ_MAGIC 		0x534D4252	/* 'SMBR' */
1366 #define	SMB_REQ_VALID(p)	ASSERT((p)->sr_magic == SMB_REQ_MAGIC)
1367 
1368 typedef enum smb_req_state {
1369 	SMB_REQ_STATE_FREE = 0,
1370 	SMB_REQ_STATE_INITIALIZING,
1371 	SMB_REQ_STATE_SUBMITTED,
1372 	SMB_REQ_STATE_ACTIVE,
1373 	SMB_REQ_STATE_WAITING_EVENT,
1374 	SMB_REQ_STATE_EVENT_OCCURRED,
1375 	SMB_REQ_STATE_WAITING_LOCK,
1376 	SMB_REQ_STATE_COMPLETED,
1377 	SMB_REQ_STATE_CANCELED,
1378 	SMB_REQ_STATE_CLEANED_UP,
1379 	SMB_REQ_STATE_SENTINEL
1380 } smb_req_state_t;
1381 
1382 typedef struct smb_request {
1383 	uint32_t		sr_magic;
1384 	kmutex_t		sr_mutex;
1385 	list_node_t		sr_session_lnd;
1386 	smb_req_state_t		sr_state;
1387 	boolean_t		sr_keep;
1388 	kmem_cache_t		*sr_cache;
1389 	struct smb_server	*sr_server;
1390 	pid_t			*sr_pid;
1391 	int32_t			sr_gmtoff;
1392 	smb_session_t		*session;
1393 	smb_kmod_cfg_t		*sr_cfg;
1394 	smb_notify_change_req_t	sr_ncr;
1395 
1396 	/* Info from session service header */
1397 	uint32_t		sr_req_length; /* Excluding NBT header */
1398 
1399 	/* Request buffer excluding NBT header */
1400 	void			*sr_request_buf;
1401 
1402 	/* Fields for raw writes */
1403 	uint32_t		sr_raw_data_length;
1404 	void			*sr_raw_data_buf;
1405 
1406 	smb_lock_t		*sr_awaiting;
1407 	struct mbuf_chain	command;
1408 	struct mbuf_chain	reply;
1409 	struct mbuf_chain	raw_data;
1410 	smb_malloc_list		request_storage;
1411 	struct smb_xa		*r_xa;
1412 	int			andx_prev_wct;
1413 	int 			cur_reply_offset;
1414 	int			orig_request_hdr;
1415 	unsigned int		reply_seqnum;	/* reply sequence number */
1416 	unsigned char		first_smb_com;	/* command code */
1417 	unsigned char		smb_com;	/* command code */
1418 
1419 	uint8_t			smb_rcls;	/* error code class */
1420 	uint8_t			smb_reh;	/* rsvd (AH DOS INT-24 ERR) */
1421 	uint16_t		smb_err;	/* error code */
1422 	smb_error_t		smb_error;
1423 
1424 	uint8_t			smb_flg;	/* flags */
1425 	uint16_t		smb_flg2;	/* flags */
1426 	uint16_t		smb_pid_high;	/* high part of pid */
1427 	unsigned char		smb_sig[8];	/* signiture */
1428 	uint16_t		smb_tid;	/* tree id #  */
1429 	uint16_t		smb_pid;	/* caller's process id # */
1430 	uint16_t		smb_uid;	/* user id # */
1431 	uint16_t		smb_mid;	/* mutiplex id #  */
1432 	unsigned char		smb_wct;	/* count of parameter words */
1433 	uint16_t		smb_bcc;	/* data byte count */
1434 
1435 	/* Parameters */
1436 	struct mbuf_chain	smb_vwv;	/* variable width value */
1437 
1438 	/* Data */
1439 	struct mbuf_chain	smb_data;
1440 
1441 	uint16_t		smb_fid;	/* not in hdr, but common */
1442 
1443 	unsigned char		andx_com;
1444 	uint16_t		andx_off;
1445 
1446 	struct smb_tree		*tid_tree;
1447 	struct smb_ofile	*fid_ofile;
1448 	smb_user_t		*uid_user;
1449 
1450 	union {
1451 	    struct tcon {
1452 		char		*path;
1453 		char		*service;
1454 		int		pwdlen;
1455 		char		*password;
1456 		uint16_t	flags;
1457 		uint16_t	optional_support;
1458 	    } tcon;
1459 
1460 	    struct dirop {
1461 		smb_fqi_t	fqi;
1462 		smb_fqi_t	dst_fqi;
1463 		uint16_t	info_level;
1464 	    } dirop;
1465 
1466 	    open_param_t	open;
1467 	    smb_rw_param_t	*rw;
1468 	    uint32_t		timestamp;
1469 	} arg;
1470 
1471 	cred_t			*user_cr;
1472 	kthread_t		*sr_worker;
1473 } smb_request_t;
1474 
1475 #define	SMB_READ_PROTOCOL(smb_nh_ptr) \
1476 	LE_IN32(((smb_nethdr_t *)(smb_nh_ptr))->sh_protocol)
1477 
1478 #define	SMB_PROTOCOL_MAGIC_INVALID(rd_sr) \
1479 	(SMB_READ_PROTOCOL((rd_sr)->sr_request_buf) != SMB_PROTOCOL_MAGIC)
1480 
1481 #define	SMB_READ_COMMAND(smb_nh_ptr) \
1482 	(((smb_nethdr_t *)(smb_nh_ptr))->sh_command)
1483 
1484 #define	SMB_IS_WRITERAW(rd_sr) \
1485 	(SMB_READ_COMMAND((rd_sr)->sr_request_buf) == SMB_COM_WRITE_RAW)
1486 
1487 
1488 #define	SR_FLG_OFFSET			9
1489 
1490 #define	MAX_TRANS_NAME	64
1491 
1492 #define	SMB_XA_FLAG_OPEN	0x0001
1493 #define	SMB_XA_FLAG_CLOSE	0x0002
1494 #define	SMB_XA_FLAG_COMPLETE	0x0004
1495 #define	SMB_XA_CLOSED(xa) (!((xa)->xa_flags & SMB_XA_FLAG_OPEN))
1496 
1497 #define	SMB_XA_MAGIC		0x534D4258	/* 'SMBX' */
1498 
1499 typedef struct smb_xa {
1500 	uint32_t		xa_magic;
1501 	kmutex_t		xa_mutex;
1502 	list_node_t		xa_lnd;
1503 
1504 	uint32_t		xa_refcnt;
1505 	uint32_t		xa_flags;
1506 
1507 	struct smb_session	*xa_session;
1508 
1509 	unsigned char		smb_com;	/* which TRANS type */
1510 	unsigned char		smb_flg;	/* flags */
1511 	uint16_t		smb_flg2;	/* flags */
1512 	uint16_t		smb_tid;	/* tree id number */
1513 	uint16_t		smb_pid;	/* caller's process id number */
1514 	uint16_t		smb_uid;	/* user id number */
1515 	uint32_t		smb_func;	/* NT_TRANS function */
1516 
1517 	uint16_t		xa_smb_mid;	/* mutiplex id number */
1518 	uint16_t		xa_smb_fid;	/* TRANS2 secondary */
1519 
1520 	unsigned int		reply_seqnum;	/* reply sequence number */
1521 
1522 	uint32_t	smb_tpscnt;	/* total parameter bytes being sent */
1523 	uint32_t	smb_tdscnt;	/* total data bytes being sent */
1524 	uint32_t	smb_mprcnt;	/* max parameter bytes to return */
1525 	uint32_t	smb_mdrcnt;	/* max data bytes to return */
1526 	uint32_t	smb_msrcnt;	/* max setup words to return */
1527 	uint32_t	smb_flags;	/* additional information: */
1528 				/*  bit 0 - if set, disconnect TID in smb_tid */
1529 				/*  bit 1 - if set, transaction is one way */
1530 				/*  (no final response) */
1531 	int32_t	smb_timeout;	/* number of milliseconds to await completion */
1532 	uint32_t	smb_suwcnt;	/* set up word count */
1533 
1534 
1535 	char			*xa_smb_trans_name;
1536 
1537 	int			req_disp_param;
1538 	int			req_disp_data;
1539 
1540 	struct mbuf_chain	req_setup_mb;
1541 	struct mbuf_chain	req_param_mb;
1542 	struct mbuf_chain	req_data_mb;
1543 
1544 	struct mbuf_chain	rep_setup_mb;
1545 	struct mbuf_chain	rep_param_mb;
1546 	struct mbuf_chain	rep_data_mb;
1547 } smb_xa_t;
1548 
1549 
1550 #define	SDDF_NO_FLAGS			0
1551 #define	SDDF_SUPPRESS_TID		0x0001
1552 #define	SDDF_SUPPRESS_UID		0x0002
1553 
1554 /*
1555  * SMB dispatch return codes.
1556  */
1557 typedef enum {
1558 	SDRC_SUCCESS = 0,
1559 	SDRC_ERROR,
1560 	SDRC_DROP_VC,
1561 	SDRC_NO_REPLY,
1562 	SDRC_SR_KEPT,
1563 	SDRC_NOT_IMPLEMENTED
1564 } smb_sdrc_t;
1565 
1566 #define	VAR_BCC		((short)-1)
1567 
1568 #define	SMB_SERVER_MAGIC	0x53534552	/* 'SSER' */
1569 
1570 typedef struct {
1571 	kstat_named_t	open_files;
1572 	kstat_named_t	open_trees;
1573 	kstat_named_t	open_users;
1574 } smb_server_stats_t;
1575 
1576 typedef struct {
1577 	kthread_t		*ld_kth;
1578 	kt_did_t		ld_ktdid;
1579 	ksocket_t		ld_so;
1580 	struct sockaddr_in	ld_sin;
1581 	struct sockaddr_in6	ld_sin6;
1582 	smb_session_list_t	ld_session_list;
1583 } smb_listener_daemon_t;
1584 
1585 typedef enum smb_server_state {
1586 	SMB_SERVER_STATE_CREATED = 0,
1587 	SMB_SERVER_STATE_CONFIGURED,
1588 	SMB_SERVER_STATE_RUNNING,
1589 	SMB_SERVER_STATE_DELETING,
1590 	SMB_SERVER_STATE_SENTINEL
1591 } smb_server_state_t;
1592 
1593 typedef struct smb_server {
1594 	uint32_t		sv_magic;
1595 	kcondvar_t		sv_cv;
1596 	kmutex_t		sv_mutex;
1597 	list_node_t		sv_lnd;
1598 	smb_server_state_t	sv_state;
1599 	uint32_t		sv_refcnt;
1600 	pid_t			sv_pid;
1601 	zoneid_t		sv_zid;
1602 	smb_listener_daemon_t	sv_nbt_daemon;
1603 	smb_listener_daemon_t	sv_tcp_daemon;
1604 	krwlock_t		sv_cfg_lock;
1605 	smb_kmod_cfg_t		sv_cfg;
1606 	smb_session_t		*sv_session;
1607 
1608 	kstat_t			*sv_ksp;
1609 	kmutex_t		sv_ksp_mutex;
1610 	char			sv_ksp_name[KSTAT_STRLEN];
1611 	smb_server_stats_t	sv_ks_data;
1612 
1613 	door_handle_t		sv_lmshrd;
1614 
1615 	int32_t			si_gmtoff;
1616 
1617 	smb_thread_t		si_thread_timers;
1618 	smb_thread_t		si_thread_unexport;
1619 
1620 	taskq_t			*sv_thread_pool;
1621 
1622 	kmem_cache_t		*si_cache_unexport;
1623 	kmem_cache_t		*si_cache_vfs;
1624 	kmem_cache_t		*si_cache_request;
1625 	kmem_cache_t		*si_cache_session;
1626 	kmem_cache_t		*si_cache_user;
1627 	kmem_cache_t		*si_cache_tree;
1628 	kmem_cache_t		*si_cache_ofile;
1629 	kmem_cache_t		*si_cache_odir;
1630 
1631 	volatile uint32_t	sv_open_trees;
1632 	volatile uint32_t	sv_open_files;
1633 	volatile uint32_t	sv_open_users;
1634 
1635 	smb_node_t		*si_root_smb_node;
1636 	smb_llist_t		sv_vfs_list;
1637 	smb_slist_t		sv_unexport_list;
1638 } smb_server_t;
1639 
1640 #define	SMB_INFO_NETBIOS_SESSION_SVC_RUNNING	0x0001
1641 #define	SMB_INFO_NETBIOS_SESSION_SVC_FAILED	0x0002
1642 #define	SMB_INFO_USER_LEVEL_SECURITY		0x40000000
1643 #define	SMB_INFO_ENCRYPT_PASSWORDS		0x80000000
1644 
1645 #define	SMB_NEW_KID()	atomic_inc_64_nv(&smb_kids)
1646 #define	SMB_UNIQ_FID()	atomic_inc_32_nv(&smb_fids)
1647 
1648 /*
1649  * This is to be used by Trans2SetFileInfo
1650  * and Trans2SetPathInfo
1651  */
1652 typedef struct smb_trans2_setinfo {
1653 	uint16_t level;
1654 	struct smb_xa *ts_xa;
1655 	struct smb_node *node;
1656 	char *path;
1657 	char name[MAXNAMELEN];
1658 } smb_trans2_setinfo_t;
1659 
1660 #define	SMB_IS_STREAM(node) ((node)->n_unode)
1661 
1662 typedef struct smb_tsd {
1663 	void (*proc)();
1664 	void *arg;
1665 	char name[100];
1666 } smb_tsd_t;
1667 
1668 typedef struct smb_disp_entry {
1669 	smb_sdrc_t		(*sdt_pre_op)(smb_request_t *);
1670 	smb_sdrc_t		(*sdt_function)(smb_request_t *);
1671 	void			(*sdt_post_op)(smb_request_t *);
1672 	char			sdt_dialect;
1673 	unsigned char		sdt_flags;
1674 	kstat_named_t		sdt_dispatch_stats; /* invocations */
1675 } smb_disp_entry_t;
1676 
1677 #ifdef	__cplusplus
1678 }
1679 #endif
1680 
1681 #endif /* _SMBSRV_SMB_KTYPES_H */
1682