xref: /titanic_44/usr/src/uts/common/fs/smbsrv/smb_dispatch.c (revision eed64e98ce34ec2844d4c68b7e5af30cf962e9c8)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  *
26  * Dispatching SMB requests.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * ALMOST EVERYTHING YOU NEED TO KNOW ABOUT A SERVER MESSAGE BLOCK
33  *
34  * Request
35  *   Header
36  *	Magic		0xFF 'S' 'M' 'B'
37  *	smb_com 	a byte, the "first" command
38  *	Error		a 4-byte union, ignored in a request
39  *	smb_flg		a one byte set of eight flags
40  *	smb_flg2	a two byte set of 16 flags
41  *	.		twelve reserved bytes, have a role
42  *			in connectionless transports (IPX, UDP?)
43  *	smb_tid		a 16-bit tree ID, a mount point sorta,
44  *			0xFFFF is this command does not have
45  *			or require a tree context
46  *	smb_pid		a 16-bit process ID
47  *	smb_uid		a 16-bit user ID, specific to this "session"
48  *			and mapped to a system (bona-fide) UID
49  *	smb_mid		a 16-bit multiplex ID, used to differentiate
50  *			multiple simultaneous requests from the same
51  *			process (pid) (ref RPC "xid")
52  *
53  *   Chained (AndX) commands (0 or more)
54  *	smb_wct		a byte, number of 16-bit words containing
55  *			command parameters, min 2 for chained command
56  *	andx_com	a byte, the "next" command, 0xFF for none
57  *	.		an unused byte
58  *	andx_off	a 16-bit offset, byte displacement from &Magic
59  *			to the smb_wct field of the "next" command,
60  *			ignore if andx_com is 0xFF, s/b 0 if no next
61  *	smb_vwv[]	0 or more 16-bit (sorta) parameters for
62  *			"this" command (i.e. smb_com if this is the
63  *			first parameters, or the andx_com of the just
64  *			previous block.
65  *	smb_bcc		a 16-bit count of smb_data[] bytes
66  *	smb_data[]	0 or more bytes, format specific to commands
67  *	padding[]	Optional padding
68  *
69  *   Last command
70  *	smb_wct		a byte, number of 16-bit words containing
71  *			command parameters, min 0 for chained command
72  *	smb_vwv[]	0 or more 16-bit (sorta) parameters for
73  *			"this" command (i.e. smb_com if this is the
74  *			first parameters, or the andx_com of the just
75  *			previous block.
76  *	smb_bcc		a 16-bit count of smb_data[] bytes
77  *	smb_data[]	0 or more bytes, format specific to commands
78  *
79  * Reply
80  *   Header
81  *	Magic		0xFF 'S' 'M' 'B'
82  *	smb_com 	a byte, the "first" command, corresponds
83  *			to request
84  *	Error		a 4-byte union, coding depends on dialect in use
85  *			for "DOS" errors
86  *				a byte for error class
87  *				an unused byte
88  *				a 16-bit word for error code
89  *			for "NT" errors
90  *				a 32-bit error code which
91  *				is a packed class and specifier
92  *			for "OS/2" errors
93  *				I don't know
94  *			The error information is specific to the
95  *			last command in the reply chain.
96  *	smb_flg		a one byte set of eight flags, 0x80 bit set
97  *			indicating this message is a reply
98  *	smb_flg2	a two byte set of 16 flags
99  *	.		twelve reserved bytes, have a role
100  *			in connectionless transports (IPX, UDP?)
101  *	smb_tid		a 16-bit tree ID, a mount point sorta,
102  *			should be the same as the request
103  *	smb_pid		a 16-bit process ID, MUST BE the same as request
104  *	smb_uid		a 16-bit user ID, specific to this "session"
105  *			and mapped to a system (bona-fide) UID,
106  *			should be the same as request
107  *	smb_mid		a 16-bit multiplex ID, used to differentiate
108  *			multiple simultaneous requests from the same
109  *			process (pid) (ref RPC "xid"), MUST BE the
110  *			same as request
111  *	padding[]	Optional padding
112  *
113  *   Chained (AndX) commands (0 or more)
114  *	smb_wct		a byte, number of 16-bit words containing
115  *			command parameters, min 2 for chained command,
116  *	andx_com	a byte, the "next" command, 0xFF for none,
117  *			corresponds to request, if this is the chained
118  *			command that had an error set to 0xFF
119  *	.		an unused byte
120  *	andx_off	a 16-bit offset, byte displacement from &Magic
121  *			to the smb_wct field of the "next" command,
122  *			ignore if andx_com is 0xFF, s/b 0 if no next
123  *	smb_vwv[]	0 or more 16-bit (sorta) parameters for
124  *			"this" command (i.e. smb_com if this is the
125  *			first parameters, or the andx_com of the just
126  *			previous block. Empty if an error.
127  *	smb_bcc		a 16-bit count of smb_data[] bytes
128  *	smb_data[]	0 or more bytes, format specific to commands
129  *			empty if an error.
130  *
131  *   Last command
132  *	smb_wct		a byte, number of 16-bit words containing
133  *			command parameters, min 0 for chained command
134  *	smb_vwv[]	0 or more 16-bit (sorta) parameters for
135  *			"this" command (i.e. smb_com if this is the
136  *			first parameters, or the andx_com of the just
137  *			previous block, empty if an error.
138  *	smb_bcc		a 16-bit count of smb_data[] bytes
139  *	smb_data[]	0 or more bytes, format specific to commands,
140  *			empty if an error.
141  */
142 
143 #include <smbsrv/smb_incl.h>
144 #include <smbsrv/smb_kstat.h>
145 #include <sys/sdt.h>
146 
147 #define	SMB_ALL_DISPATCH_STAT_INCR(stat)	atomic_inc_64(&stat);
148 #define	SMB_COM_NUM	256
149 
150 static kstat_t *smb_dispatch_ksp = NULL;
151 static kmutex_t smb_dispatch_ksmtx;
152 
153 static int is_andx_com(unsigned char);
154 static int smbsr_check_result(struct smb_request *, int, int);
155 
156 static smb_dispatch_table_t	dispatch[SMB_COM_NUM] = {
157 	{ SMB_SDT_OPS(create_directory),			/* 0x00 000 */
158 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
159 	    { "SmbCreateDirectory", KSTAT_DATA_UINT64 } },
160 	{ SMB_SDT_OPS(delete_directory),			/* 0x01 001 */
161 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
162 	    { "SmbDeleteDirectory", KSTAT_DATA_UINT64 } },
163 	{ SMB_SDT_OPS(open),					/* 0x02 002 */
164 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
165 	    { "SmbOpen", KSTAT_DATA_UINT64 } },
166 	{ SMB_SDT_OPS(create),					/* 0x03 003 */
167 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
168 	    { "SmbCreate", KSTAT_DATA_UINT64 } },
169 	{ SMB_SDT_OPS(close),					/* 0x04 004 */
170 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
171 	    { "SmbClose", KSTAT_DATA_UINT64 } },
172 	{ SMB_SDT_OPS(flush),					/* 0x05 005 */
173 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
174 	    { "SmbFlush", KSTAT_DATA_UINT64 } },
175 	{ SMB_SDT_OPS(delete),					/* 0x06 006 */
176 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
177 	    { "SmbDelete", KSTAT_DATA_UINT64 } },
178 	{ SMB_SDT_OPS(rename),					/* 0x07 007 */
179 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
180 	    { "SmbRename", KSTAT_DATA_UINT64 } },
181 	{ SMB_SDT_OPS(query_information),			/* 0x08 008 */
182 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
183 	    { "SmbQueryInformation", KSTAT_DATA_UINT64 } },
184 	{ SMB_SDT_OPS(set_information),				/* 0x09 009 */
185 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
186 	    { "SmbSetInformation", KSTAT_DATA_UINT64 } },
187 	{ SMB_SDT_OPS(read),					/* 0x0A 010 */
188 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
189 	    { "SmbRead", KSTAT_DATA_UINT64 } },
190 	{ SMB_SDT_OPS(write),					/* 0x0B 011 */
191 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
192 	    { "SmbWrite", KSTAT_DATA_UINT64 } },
193 	{ SMB_SDT_OPS(lock_byte_range),				/* 0x0C 012 */
194 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
195 	    { "SmbLockByteRange", KSTAT_DATA_UINT64 } },
196 	{ SMB_SDT_OPS(unlock_byte_range),			/* 0x0D 013 */
197 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
198 	    { "SmbUnlockByteRange", KSTAT_DATA_UINT64 } },
199 	{ SMB_SDT_OPS(create_temporary),			/* 0x0E 014 */
200 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
201 	    { "SmbCreateTemporary", KSTAT_DATA_UINT64 } },
202 	{ SMB_SDT_OPS(create_new),				/* 0x0F 015 */
203 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
204 	    { "SmbCreateNew",	KSTAT_DATA_UINT64 } },
205 	{ SMB_SDT_OPS(check_directory),				/* 0x10 016 */
206 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
207 	    { "SmbCheckDirectory", KSTAT_DATA_UINT64 } },
208 	{ SMB_SDT_OPS(process_exit),				/* 0x11 017 */
209 	    PC_NETWORK_PROGRAM_1_0, SDDF_SUPPRESS_TID | SDDF_SUPPRESS_UID,
210 	    RW_READER,
211 	    { "SmbProcessExit", KSTAT_DATA_UINT64 } },
212 	{ SMB_SDT_OPS(seek),					/* 0x12 018 */
213 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
214 	    { "SmbSeek", KSTAT_DATA_UINT64 } },
215 	{ SMB_SDT_OPS(lock_and_read),				/* 0x13 019 */
216 	    LANMAN1_0, 0, RW_READER,
217 	    { "SmbLockAndRead", KSTAT_DATA_UINT64 } },
218 	{ SMB_SDT_OPS(write_and_unlock),			/* 0x14 020 */
219 	    LANMAN1_0, 0, RW_READER,
220 	    { "SmbWriteAndUnlock", KSTAT_DATA_UINT64 } },
221 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x15 021 */
222 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x16 022 */
223 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x17 023 */
224 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x18 024 */
225 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x19 025 */
226 	{ SMB_SDT_OPS(read_raw),				/* 0x1A 026 */
227 	    LANMAN1_0, 0, RW_WRITER,
228 	    { "SmbReadRaw", KSTAT_DATA_UINT64 } },
229 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x1B 027 */
230 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x1C 028 */
231 	{ SMB_SDT_OPS(write_raw),				/* 0x1D 029 */
232 	    LANMAN1_0, 0, RW_WRITER,
233 	    { "SmbWriteRaw", KSTAT_DATA_UINT64 } },
234 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x1E 030 */
235 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x1F 031 */
236 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x20 032 */
237 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x21 033 */
238 	{ SMB_SDT_OPS(set_information2),			/* 0x22 034 */
239 	    LANMAN1_0, 0, RW_READER,
240 	    { "SmbSetInformation2", KSTAT_DATA_UINT64 } },
241 	{ SMB_SDT_OPS(query_information2),			/* 0x23 035 */
242 	    LANMAN1_0, 0, RW_READER,
243 	    { "SmbQueryInformation2",	KSTAT_DATA_UINT64 } },
244 	{ SMB_SDT_OPS(locking_andx),				/* 0x24 036 */
245 	    LANMAN1_0, 0, RW_READER,
246 	    { "SmbLockingX", KSTAT_DATA_UINT64 } },
247 	{ SMB_SDT_OPS(transaction),				/* 0x25 037 */
248 	    LANMAN1_0, 0, RW_READER,
249 	    { "SmbTransaction", KSTAT_DATA_UINT64 } },
250 	{ SMB_SDT_OPS(transaction_secondary),			/* 0x26 038 */
251 	    LANMAN1_0, 0, RW_READER,
252 	    { "SmbTransactionSecondary", KSTAT_DATA_UINT64 } },
253 	{ SMB_SDT_OPS(ioctl),					/* 0x27 039 */
254 	    LANMAN1_0, 0, RW_READER,
255 	    { "SmbIoctl", KSTAT_DATA_UINT64 } },
256 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x28 040 */
257 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x29 041 */
258 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x2A 042 */
259 	{ SMB_SDT_OPS(echo),					/* 0x2B 043 */
260 	    LANMAN1_0, SDDF_SUPPRESS_TID | SDDF_SUPPRESS_UID,
261 	    RW_READER,
262 	    { "SmbEcho", KSTAT_DATA_UINT64 } },
263 	{ SMB_SDT_OPS(write_and_close),				/* 0x2C 044 */
264 	    LANMAN1_0, 0, RW_READER,
265 	    { "SmbWriteAndClose", KSTAT_DATA_UINT64 } },
266 	{ SMB_SDT_OPS(open_andx),				/* 0x2D 045 */
267 	    LANMAN1_0, 0, RW_READER,
268 	    { "SmbOpenX", KSTAT_DATA_UINT64 } },
269 	{ SMB_SDT_OPS(read_andx),				/* 0x2E 046 */
270 	    LANMAN1_0, 0, RW_READER,
271 	    { "SmbReadX", KSTAT_DATA_UINT64 } },
272 	{ SMB_SDT_OPS(write_andx),				/* 0x2F 047 */
273 	    LANMAN1_0, 0, RW_READER,
274 	    { "SmbWriteX",	KSTAT_DATA_UINT64 } },
275 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x30 048 */
276 	{ SMB_SDT_OPS(close_and_tree_disconnect),		/* 0x31 049 */
277 	    LANMAN1_0, 0, RW_READER,
278 	    { "SmbCloseAndTreeDisconnect", KSTAT_DATA_UINT64 } },
279 	{ SMB_SDT_OPS(transaction2),				/* 0x32 050 */
280 	    LM1_2X002, 0, RW_READER,
281 	    { "SmbTransaction2", KSTAT_DATA_UINT64 } },
282 	{ SMB_SDT_OPS(transaction2_secondary),			/* 0x33 051 */
283 	    LM1_2X002, 0, RW_READER,
284 	    { "SmbTransaction2Secondary", KSTAT_DATA_UINT64 } },
285 	{ SMB_SDT_OPS(find_close2),				/* 0x34 052 */
286 	    LM1_2X002, 0, RW_READER,
287 	    { "SmbFindClose2", KSTAT_DATA_UINT64 } },
288 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x35 053 */
289 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x36 054 */
290 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x37 055 */
291 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x38 056 */
292 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x39 057 */
293 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x3A 058 */
294 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x3B 059 */
295 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x3C 060 */
296 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x3D 061 */
297 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x3E 062 */
298 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x3F 063 */
299 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x40 064 */
300 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x41 065 */
301 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x42 066 */
302 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x43 067 */
303 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x44 068 */
304 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x45 069 */
305 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x46 070 */
306 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x47 071 */
307 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x48 072 */
308 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x49 073 */
309 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x4A 074 */
310 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x4B 075 */
311 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x4C 076 */
312 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x4D 077 */
313 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x4E 078 */
314 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x4F 079 */
315 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x50 080 */
316 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x51 081 */
317 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x52 082 */
318 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x53 083 */
319 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x54 084 */
320 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x55 085 */
321 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x56 086 */
322 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x57 087 */
323 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x58 088 */
324 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x59 089 */
325 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x5A 090 */
326 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x5B 091 */
327 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x5C 092 */
328 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x5D 093 */
329 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x5E 094 */
330 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x5F 095 */
331 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x60 096 */
332 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x61 097 */
333 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x62 098 */
334 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x63 099 */
335 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x64 100 */
336 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x65 101 */
337 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x66 102 */
338 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x67 103 */
339 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x68 104 */
340 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x69 105 */
341 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x6A 106 */
342 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x6B 107 */
343 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x6C 108 */
344 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x6D 109 */
345 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x6E 110 */
346 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x6F 111 */
347 	{ SMB_SDT_OPS(tree_connect),				/* 0x70 112 */
348 	    PC_NETWORK_PROGRAM_1_0, SDDF_SUPPRESS_TID, RW_READER,
349 	    { "SmbTreeConnect", KSTAT_DATA_UINT64 } },
350 	{ SMB_SDT_OPS(tree_disconnect),				/* 0x71 113 */
351 	    PC_NETWORK_PROGRAM_1_0, SDDF_SUPPRESS_TID | SDDF_SUPPRESS_UID,
352 	    RW_READER,
353 	    { "SmbTreeDisconnect", KSTAT_DATA_UINT64 } },
354 	{ SMB_SDT_OPS(negotiate),				/* 0x72 114 */
355 	    PC_NETWORK_PROGRAM_1_0, SDDF_SUPPRESS_TID | SDDF_SUPPRESS_UID,
356 	    RW_WRITER,
357 	    { "SmbNegotiate", KSTAT_DATA_UINT64 } },
358 	{ SMB_SDT_OPS(session_setup_andx),			/* 0x73 115 */
359 	    LANMAN1_0, SDDF_SUPPRESS_TID | SDDF_SUPPRESS_UID,
360 	    RW_READER,
361 	    { "SmbSessionSetupX",	KSTAT_DATA_UINT64 } },
362 	{ SMB_SDT_OPS(logoff_andx),				/* 0x74 116 */
363 	    LM1_2X002, SDDF_SUPPRESS_TID, RW_READER,
364 	    { "SmbLogoffX", KSTAT_DATA_UINT64 } },
365 	{ SMB_SDT_OPS(tree_connect_andx),			/* 0x75 117 */
366 	    LANMAN1_0, SDDF_SUPPRESS_TID, RW_READER,
367 	    { "SmbTreeConnectX", KSTAT_DATA_UINT64 } },
368 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x76 118 */
369 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x77 119 */
370 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x78 120 */
371 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x79 121 */
372 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x7A 122 */
373 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x7B 123 */
374 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x7C 124 */
375 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x7D 125 */
376 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x7E 126 */
377 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x7F 127 */
378 	{ SMB_SDT_OPS(query_information_disk),			/* 0x80 128 */
379 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
380 	    { "SmbQueryInformationDisk", KSTAT_DATA_UINT64 } },
381 	{ SMB_SDT_OPS(search),					/* 0x81 129 */
382 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
383 	    { "SmbSearch", KSTAT_DATA_UINT64 } },
384 	{ SMB_SDT_OPS(find),					/* 0x82 130 */
385 	    LANMAN1_0, 0, RW_READER,
386 	    { "SmbFind", KSTAT_DATA_UINT64 } },
387 	{ SMB_SDT_OPS(find_unique),				/* 0x83 131 */
388 	    LANMAN1_0, 0, RW_READER,
389 	    { "SmbFindUnique", KSTAT_DATA_UINT64 } },
390 	{ SMB_SDT_OPS(find_close),				/* 0x84 132 */
391 	    LANMAN1_0, 0, RW_READER,
392 	    { "SmbFindClose", KSTAT_DATA_UINT64 } },
393 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x85 133 */
394 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x86 134 */
395 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x87 135 */
396 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x88 136 */
397 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x89 137 */
398 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x8A 138 */
399 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x8B 139 */
400 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x8C 140 */
401 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x8D 141 */
402 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x8E 142 */
403 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x8F 143 */
404 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x90 144 */
405 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x91 145 */
406 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x92 146 */
407 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x93 147 */
408 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x94 148 */
409 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x95 149 */
410 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x96 150 */
411 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x97 151 */
412 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x98 152 */
413 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x99 153 */
414 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x9A 154 */
415 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x9B 155 */
416 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x9C 156 */
417 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x9D 157 */
418 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x9E 158 */
419 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0x9F 159 */
420 	{ SMB_SDT_OPS(nt_transact),				/* 0xA0 160 */
421 	    NT_LM_0_12, 0, RW_READER,
422 	    { "SmbNtTransact",	KSTAT_DATA_UINT64 } },
423 	{ SMB_SDT_OPS(nt_transact_secondary),			/* 0xA1 161 */
424 	    NT_LM_0_12, 0, RW_READER,
425 	    { "SmbNtTransactSecondary",	KSTAT_DATA_UINT64 } },
426 	{ SMB_SDT_OPS(nt_create_andx),				/* 0xA2 162 */
427 	    NT_LM_0_12, 0, RW_READER,
428 	    { "SmbNtCreateX",	KSTAT_DATA_UINT64 } },
429 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xA3 163 */
430 	{ SMB_SDT_OPS(nt_cancel),				/* 0xA4 164 */
431 	    NT_LM_0_12, 0, RW_READER,
432 	    { "SmbNtCancel",	KSTAT_DATA_UINT64 } },
433 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xA5 165 */
434 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xA6 166 */
435 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xA7 167 */
436 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xA8 168 */
437 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xA9 169 */
438 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xAA 170 */
439 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xAB 171 */
440 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xAC 172 */
441 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xAD 173 */
442 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xAE 174 */
443 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xAF 175 */
444 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB0 176 */
445 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB1 177 */
446 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB2 178 */
447 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB3 179 */
448 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB4 180 */
449 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB5 181 */
450 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB6 182 */
451 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB7 183 */
452 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB8 184 */
453 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xB9 185 */
454 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xBA 186 */
455 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xBB 187 */
456 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xBC 188 */
457 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xBD 189 */
458 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xBE 190 */
459 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xBF 191 */
460 	{ SMB_SDT_OPS(open_print_file),				/* 0xC0 192 */
461 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
462 	    { "SmbOpenPrintFile", KSTAT_DATA_UINT64 } },
463 	{ SMB_SDT_OPS(write_print_file),			/* 0xC1 193 */
464 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
465 	    { "SmbWritePrintFile", KSTAT_DATA_UINT64 } },
466 	{ SMB_SDT_OPS(close_print_file),			/* 0xC2 194 */
467 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
468 	    { "SmbClosePrintFile", KSTAT_DATA_UINT64 } },
469 	{ SMB_SDT_OPS(get_print_queue),				/* 0xC3 195 */
470 	    PC_NETWORK_PROGRAM_1_0, 0, RW_READER,
471 	    { "SmbGetPrintQueue", KSTAT_DATA_UINT64 } },
472 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xC4 196 */
473 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xC5 197 */
474 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xC6 198 */
475 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xC7 199 */
476 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xC8 200 */
477 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xC9 201 */
478 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xCA 202 */
479 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xCB 203 */
480 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xCC 204 */
481 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xCD 205 */
482 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xCE 206 */
483 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xCF 207 */
484 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD0 208 */
485 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD1 209 */
486 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD2 210 */
487 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD3 211 */
488 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD4 212 */
489 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD5 213 */
490 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD6 214 */
491 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD7 215 */
492 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD8 216 */
493 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xD9 217 */
494 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xDA 218 */
495 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xDB 219 */
496 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xDC 220 */
497 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xDD 221 */
498 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xDE 222 */
499 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xDF 223 */
500 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE0 224 */
501 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE1 225 */
502 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE2 226 */
503 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE3 227 */
504 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE4 228 */
505 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE5 229 */
506 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE6 230 */
507 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE7 231 */
508 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE8 232 */
509 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xE9 233 */
510 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xEA 234 */
511 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xEB 235 */
512 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xEC 236 */
513 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xED 237 */
514 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xEE 238 */
515 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xEF 239 */
516 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF0 240 */
517 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF1 241 */
518 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF2 242 */
519 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF3 243 */
520 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF4 244 */
521 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF5 245 */
522 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF6 246 */
523 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF7 247 */
524 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF8 248 */
525 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xF9 249 */
526 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xFA 250 */
527 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xFB 251 */
528 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xFC 252 */
529 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 },		/* 0xFD 253 */
530 	{ SMB_SDT_OPS(invalid), LANMAN1_0, 0, RW_READER, 	/* 0xFE 254 */
531 	    { "SmbInvalidCommand", KSTAT_DATA_UINT64 } },
532 	{ SMB_SDT_OPS(invalid), 0, 0, RW_READER, 0 }		/* 0xFF 255 */
533 };
534 
535 /*
536  * smbsr_cleanup
537  *
538  * If any user/tree/file is used by given request then
539  * the reference count for that resource has been incremented.
540  * This function decrements the reference count and close
541  * the resource if it's needed.
542  */
543 
544 void
545 smbsr_cleanup(smb_request_t *sr)
546 {
547 	ASSERT((sr->sr_state != SMB_REQ_STATE_CLEANED_UP) &&
548 	    (sr->sr_state != SMB_REQ_STATE_COMPLETED));
549 
550 	if (sr->fid_ofile)
551 		smbsr_disconnect_file(sr);
552 
553 	if (sr->sid_odir)
554 		smbsr_disconnect_dir(sr);
555 
556 	if (sr->r_xa) {
557 		if (sr->r_xa->xa_flags & SMB_XA_FLAG_COMPLETE)
558 			smb_xa_close(sr->r_xa);
559 		smb_xa_rele(sr->session, sr->r_xa);
560 		sr->r_xa = NULL;
561 	}
562 
563 	/*
564 	 * Mark this request so we know that we've already cleaned it up.
565 	 * A request should only get cleaned up once so multiple calls to
566 	 * smbsr_cleanup for the same request indicate a bug.
567 	 */
568 	mutex_enter(&sr->sr_mutex);
569 	if (sr->sr_state != SMB_REQ_STATE_CANCELED)
570 		sr->sr_state = SMB_REQ_STATE_CLEANED_UP;
571 	mutex_exit(&sr->sr_mutex);
572 }
573 
574 void
575 smb_dispatch_request(struct smb_request *sr)
576 {
577 	smb_sdrc_t		sdrc;
578 	smb_dispatch_table_t	*sdd;
579 	boolean_t		disconnect = B_FALSE;
580 
581 	ASSERT(sr->tid_tree == 0);
582 	ASSERT(sr->uid_user == 0);
583 	ASSERT(sr->fid_ofile == 0);
584 	ASSERT(sr->sid_odir == 0);
585 	sr->smb_fid = (uint16_t)-1;
586 	sr->smb_sid = (uint16_t)-1;
587 
588 	/* temporary until we identify a user */
589 	sr->user_cr = kcred;
590 	sr->orig_request_hdr = sr->command.chain_offset;
591 
592 	/* If this connection is shutting down just kill request */
593 	if (smb_decode_mbc(&sr->command, SMB_HEADER_ED_FMT,
594 	    &sr->smb_com,
595 	    &sr->smb_rcls,
596 	    &sr->smb_reh,
597 	    &sr->smb_err,
598 	    &sr->smb_flg,
599 	    &sr->smb_flg2,
600 	    &sr->smb_pid_high,
601 	    sr->smb_sig,
602 	    &sr->smb_tid,
603 	    &sr->smb_pid,
604 	    &sr->smb_uid,
605 	    &sr->smb_mid) != 0) {
606 		disconnect = B_TRUE;
607 		goto drop_connection;
608 	}
609 
610 	/*
611 	 * The reply "header" is filled in now even though it will,
612 	 * most likely, be rewritten under reply_ready below.  We
613 	 * could reserve the space but this is convenient in case
614 	 * the dialect dispatcher has to send a special reply (like
615 	 * TRANSACT).
616 	 *
617 	 * Ensure that the 32-bit error code flag is turned off.
618 	 * Clients seem to set it in transact requests and they may
619 	 * get confused if we return success or a 16-bit SMB code.
620 	 */
621 	sr->smb_rcls = 0;
622 	sr->smb_reh = 0;
623 	sr->smb_err = 0;
624 	sr->smb_flg2 &= ~SMB_FLAGS2_NT_STATUS;
625 
626 	(void) smb_encode_mbc(&sr->reply, SMB_HEADER_ED_FMT,
627 	    sr->smb_com,
628 	    sr->smb_rcls,
629 	    sr->smb_reh,
630 	    sr->smb_err,
631 	    sr->smb_flg,
632 	    sr->smb_flg2,
633 	    sr->smb_pid_high,
634 	    sr->smb_sig,
635 	    sr->smb_tid,
636 	    sr->smb_pid,
637 	    sr->smb_uid,
638 	    sr->smb_mid);
639 	sr->first_smb_com = sr->smb_com;
640 
641 	/*
642 	 * Verify SMB signature if signing is enabled, dialect is NT LM 0.12,
643 	 * signing was negotiated and authentication has occurred.
644 	 */
645 	if (sr->session->signing.flags & SMB_SIGNING_ENABLED) {
646 		if (smb_sign_check_request(sr) != 0) {
647 			smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
648 			    ERRDOS, ERROR_ACCESS_DENIED);
649 			disconnect = B_TRUE;
650 			smb_rwx_rwenter(&sr->session->s_lock, RW_READER);
651 			goto report_error;
652 		}
653 	}
654 
655 andx_more:
656 	sdd = &dispatch[sr->smb_com];
657 	ASSERT(sdd->sdt_function);
658 
659 	smb_rwx_rwenter(&sr->session->s_lock, sdd->sdt_slock_mode);
660 
661 	if (smb_decode_mbc(&sr->command, "b", &sr->smb_wct) != 0) {
662 		disconnect = B_TRUE;
663 		goto report_error;
664 	}
665 
666 	(void) MBC_SHADOW_CHAIN(&sr->smb_vwv, &sr->command,
667 	    sr->command.chain_offset, sr->smb_wct * 2);
668 
669 	if (smb_decode_mbc(&sr->command, "#.w", sr->smb_wct*2, &sr->smb_bcc)) {
670 		disconnect = B_TRUE;
671 		goto report_error;
672 	}
673 
674 	(void) MBC_SHADOW_CHAIN(&sr->smb_data, &sr->command,
675 	    sr->command.chain_offset, sr->smb_bcc);
676 
677 	sr->command.chain_offset += sr->smb_bcc;
678 	if (sr->command.chain_offset > sr->command.max_bytes) {
679 		disconnect = B_TRUE;
680 		goto report_error;
681 	}
682 
683 	/* Store pointers for later */
684 	sr->cur_reply_offset = sr->reply.chain_offset;
685 
686 	if (is_andx_com(sr->smb_com)) {
687 		/* Peek ahead and don't disturb vwv */
688 		if (smb_peek_mbc(&sr->smb_vwv, sr->smb_vwv.chain_offset, "b.w",
689 		    &sr->andx_com, &sr->andx_off) < 0) {
690 			disconnect = B_TRUE;
691 			goto report_error;
692 		}
693 	} else {
694 		sr->andx_com = (unsigned char)-1;
695 	}
696 
697 	mutex_enter(&sr->sr_mutex);
698 	switch (sr->sr_state) {
699 	case SMB_REQ_STATE_SUBMITTED:
700 	case SMB_REQ_STATE_CLEANED_UP:
701 		sr->sr_state = SMB_REQ_STATE_ACTIVE;
702 		break;
703 	case SMB_REQ_STATE_CANCELED:
704 		break;
705 	default:
706 		ASSERT(0);
707 		break;
708 	}
709 	mutex_exit(&sr->sr_mutex);
710 
711 	/*
712 	 * Setup UID and TID information (if required). Both functions
713 	 * will set the sr credentials. In domain mode, the user and
714 	 * tree credentials should be the same. In share mode, the
715 	 * tree credentials (defined in the share definition) should
716 	 * override the user credentials.
717 	 */
718 	if (!(sdd->sdt_flags & SDDF_SUPPRESS_UID) && (sr->uid_user == NULL)) {
719 		sr->uid_user = smb_user_lookup_by_uid(sr->session,
720 		    &sr->user_cr, sr->smb_uid);
721 		if (sr->uid_user == NULL) {
722 			smbsr_error(sr, 0, ERRSRV, ERRbaduid);
723 			smbsr_cleanup(sr);
724 			goto report_error;
725 		}
726 
727 		if (!(sdd->sdt_flags & SDDF_SUPPRESS_TID) &&
728 		    (sr->tid_tree == NULL)) {
729 			sr->tid_tree = smb_tree_lookup_by_tid(
730 			    sr->uid_user, sr->smb_tid);
731 			if (sr->tid_tree == NULL) {
732 				smbsr_error(sr, 0, ERRSRV, ERRinvnid);
733 				smbsr_cleanup(sr);
734 				goto report_error;
735 			}
736 		}
737 	}
738 
739 	/*
740 	 * If the command is not a read raw request we can set the
741 	 * state of the session back to SMB_SESSION_STATE_NEGOTIATED
742 	 * (if the current state is SMB_SESSION_STATE_OPLOCK_BREAKING).
743 	 * Otherwise we let the read raw handler to deal with it.
744 	 */
745 	if ((sr->session->s_state == SMB_SESSION_STATE_OPLOCK_BREAKING) &&
746 	    (sr->smb_com != SMB_COM_READ_RAW)) {
747 		krw_t	mode;
748 		/*
749 		 * The lock may have to be upgraded because, at this
750 		 * point, we don't know how it was entered. We just
751 		 * know that it has to be entered in writer mode here.
752 		 * Whatever mode was used to enter the lock, it will
753 		 * be restored.
754 		 */
755 		mode = smb_rwx_rwupgrade(&sr->session->s_lock);
756 		if (sr->session->s_state == SMB_SESSION_STATE_OPLOCK_BREAKING)
757 			sr->session->s_state = SMB_SESSION_STATE_NEGOTIATED;
758 
759 		smb_rwx_rwdowngrade(&sr->session->s_lock, mode);
760 	}
761 
762 	/*
763 	 * Increment method invocation count. This value is exposed
764 	 * via kstats, and it represents a count of all the dispatched
765 	 * requests, including the ones that have a return value, other
766 	 * than SDRC_SUCCESS.
767 	 */
768 	SMB_ALL_DISPATCH_STAT_INCR(sdd->sdt_dispatch_stats.value.ui64);
769 
770 	if ((sdrc = (*sdd->sdt_pre_op)(sr)) == SDRC_SUCCESS)
771 		sdrc = (*sdd->sdt_function)(sr);
772 
773 	if (sdrc != SDRC_SUCCESS) {
774 		/*
775 		 * Handle errors from raw write.
776 		 */
777 		if (sr->session->s_state ==
778 		    SMB_SESSION_STATE_WRITE_RAW_ACTIVE) {
779 			/*
780 			 * Set state so that the netbios session
781 			 * daemon will start accepting data again.
782 			 */
783 			sr->session->s_write_raw_status = 0;
784 			sr->session->s_state = SMB_SESSION_STATE_NEGOTIATED;
785 		}
786 	}
787 
788 	(*sdd->sdt_post_op)(sr);
789 
790 	/*
791 	 * Only call smbsr_cleanup if smb->sr_keep is not set.
792 	 * smb_nt_transact_notify_change will set smb->sr_keep if it
793 	 * retains control of the request when it returns.  In that
794 	 * case the notify change code will call smbsr_cleanup later
795 	 * when the request has completed.
796 	 */
797 	if (sr->sr_keep == 0)
798 		smbsr_cleanup(sr);
799 
800 	switch (sdrc) {
801 	case SDRC_SUCCESS:
802 		break;
803 
804 	case SDRC_DROP_VC:
805 		disconnect = B_TRUE;
806 		goto drop_connection;
807 
808 	case SDRC_NO_REPLY:
809 		smb_rwx_rwexit(&sr->session->s_lock);
810 		return;
811 
812 	case SDRC_ERROR:
813 		goto report_error;
814 
815 	case SDRC_NOT_IMPLEMENTED:
816 	default:
817 		smbsr_error(sr, 0, ERRDOS, ERRbadfunc);
818 		goto report_error;
819 	}
820 
821 	/*
822 	 * If there's no AndX command, we're done.
823 	 */
824 	if (sr->andx_com == 0xff)
825 		goto reply_ready;
826 
827 	/*
828 	 * Otherwise, we have to back-patch the AndXCommand and AndXOffset
829 	 * and continue processing.
830 	 */
831 	sr->andx_prev_wct = sr->cur_reply_offset;
832 	(void) smb_poke_mbc(&sr->reply, sr->andx_prev_wct + 1, "b.w",
833 	    sr->andx_com, MBC_LENGTH(&sr->reply));
834 
835 	smb_rwx_rwexit(&sr->session->s_lock);
836 
837 	sr->command.chain_offset = sr->orig_request_hdr + sr->andx_off;
838 	sr->smb_com = sr->andx_com;
839 	goto andx_more;
840 
841 report_error:
842 	sr->reply.chain_offset = sr->cur_reply_offset;
843 	(void) smb_encode_mbc(&sr->reply, "bw", 0, 0);
844 
845 	sr->smb_wct = 0;
846 	sr->smb_bcc = 0;
847 
848 	if (sr->smb_rcls == 0 && sr->smb_reh == 0 && sr->smb_err == 0)
849 		smbsr_error(sr, 0, ERRSRV, ERRerror);
850 
851 reply_ready:
852 	smbsr_send_reply(sr);
853 
854 drop_connection:
855 	if (disconnect) {
856 		switch (sr->session->s_state) {
857 		case SMB_SESSION_STATE_DISCONNECTED:
858 		case SMB_SESSION_STATE_TERMINATED:
859 			break;
860 		default:
861 			smb_soshutdown(sr->session->sock);
862 			sr->session->s_state = SMB_SESSION_STATE_DISCONNECTED;
863 			break;
864 		}
865 	}
866 
867 	smb_rwx_rwexit(&sr->session->s_lock);
868 }
869 
870 int
871 smbsr_encode_empty_result(struct smb_request *sr)
872 {
873 	return (smbsr_encode_result(sr, 0, 0, "bw", 0, 0));
874 }
875 
876 int
877 smbsr_encode_result(struct smb_request *sr, int wct, int bcc, char *fmt, ...)
878 {
879 	va_list ap;
880 
881 	if (MBC_LENGTH(&sr->reply) != sr->cur_reply_offset)
882 		return (-1);
883 
884 	va_start(ap, fmt);
885 	(void) smb_mbc_encode(&sr->reply, fmt, ap);
886 	va_end(ap);
887 
888 	sr->smb_wct = (unsigned char)wct;
889 	sr->smb_bcc = (uint16_t)bcc;
890 
891 	if (smbsr_check_result(sr, wct, bcc) != 0)
892 		return (-1);
893 
894 	return (0);
895 }
896 
897 static int
898 smbsr_check_result(struct smb_request *sr, int wct, int bcc)
899 {
900 	int		offset = sr->cur_reply_offset;
901 	int		total_bytes;
902 	unsigned char	temp, temp1;
903 	struct mbuf	*m;
904 
905 	total_bytes = 0;
906 	m = sr->reply.chain;
907 	while (m != 0) {
908 		total_bytes += m->m_len;
909 		m = m->m_next;
910 	}
911 
912 	if ((offset + 3) > total_bytes)
913 		return (-1);
914 
915 	(void) smb_peek_mbc(&sr->reply, offset, "b", &temp);
916 	if (temp != wct)
917 		return (-1);
918 
919 	if ((offset + (wct * 2 + 1)) > total_bytes)
920 		return (-1);
921 
922 	/* reply wct & vwv seem ok, consider data now */
923 	offset += wct * 2 + 1;
924 
925 	if ((offset + 2) > total_bytes)
926 		return (-1);
927 
928 	(void) smb_peek_mbc(&sr->reply, offset, "bb", &temp, &temp1);
929 	if (bcc == VAR_BCC) {
930 		if ((temp != 0xFF) || (temp1 != 0xFF)) {
931 			return (-1);
932 		} else {
933 			bcc = (total_bytes - offset) - 2;
934 			(void) smb_poke_mbc(&sr->reply, offset, "bb",
935 			    bcc, bcc >> 8);
936 		}
937 	} else {
938 		if ((temp != (bcc&0xFF)) || (temp1 != ((bcc>>8)&0xFF)))
939 			return (-1);
940 	}
941 
942 	offset += bcc + 2;
943 
944 	if (offset != total_bytes)
945 		return (-1);
946 
947 	sr->smb_wct = (unsigned char)wct;
948 	sr->smb_bcc = (uint16_t)bcc;
949 	return (0);
950 }
951 
952 int
953 smbsr_decode_vwv(struct smb_request *sr, char *fmt, ...)
954 {
955 	int rc;
956 	va_list ap;
957 
958 	va_start(ap, fmt);
959 	rc = smb_mbc_decode(&sr->smb_vwv, fmt, ap);
960 	va_end(ap);
961 
962 	if (rc)
963 		smbsr_error(sr, 0, ERRSRV, ERRerror);
964 	return (rc);
965 }
966 
967 int
968 smbsr_decode_data(struct smb_request *sr, char *fmt, ...)
969 {
970 	int rc;
971 	va_list ap;
972 
973 	va_start(ap, fmt);
974 	rc = smb_mbc_decode(&sr->smb_data, fmt, ap);
975 	va_end(ap);
976 
977 	if (rc)
978 		smbsr_error(sr, 0, ERRSRV, ERRerror);
979 	return (rc);
980 }
981 
982 void
983 smbsr_send_reply(struct smb_request *sr)
984 {
985 	if (SMB_TREE_CASE_INSENSITIVE(sr))
986 		sr->smb_flg |= SMB_FLAGS_CASE_INSENSITIVE;
987 	else
988 		sr->smb_flg &= ~SMB_FLAGS_CASE_INSENSITIVE;
989 
990 	(void) smb_poke_mbc(&sr->reply, 0, SMB_HEADER_ED_FMT,
991 	    sr->first_smb_com,
992 	    sr->smb_rcls,
993 	    sr->smb_reh,
994 	    sr->smb_err,
995 	    sr->smb_flg | SMB_FLAGS_REPLY,
996 	    sr->smb_flg2,
997 	    sr->smb_pid_high,
998 	    sr->smb_sig,
999 	    sr->smb_tid,
1000 	    sr->smb_pid,
1001 	    sr->smb_uid,
1002 	    sr->smb_mid);
1003 
1004 	if (sr->session->signing.flags & SMB_SIGNING_ENABLED)
1005 		smb_sign_reply(sr, NULL);
1006 
1007 	if (smb_session_send(sr->session, 0, &sr->reply) == 0)
1008 		sr->reply.chain = 0;
1009 }
1010 
1011 /*
1012  * Map errno values to SMB and NT status values.
1013  * Note: ESRCH is a special case to handle a streams lookup failure.
1014  */
1015 static struct {
1016 	int errnum;
1017 	int errcls;
1018 	int errcode;
1019 	DWORD status32;
1020 } smb_errno_map[] = {
1021 	{ ENOSPC,	ERRDOS, ERROR_DISK_FULL, NT_STATUS_DISK_FULL },
1022 	{ EDQUOT,	ERRDOS, ERROR_DISK_FULL, NT_STATUS_DISK_FULL },
1023 	{ EPERM,	ERRSRV, ERRaccess, NT_STATUS_ACCESS_DENIED },
1024 	{ ENOTDIR,	ERRDOS, ERRbadpath, NT_STATUS_OBJECT_PATH_NOT_FOUND },
1025 	{ EISDIR,	ERRDOS, ERRbadpath, NT_STATUS_FILE_IS_A_DIRECTORY },
1026 	{ ENOENT,	ERRDOS, ERRbadfile, NT_STATUS_NO_SUCH_FILE },
1027 	{ ENOTEMPTY,	ERRDOS, ERROR_DIR_NOT_EMPTY,
1028 	    NT_STATUS_DIRECTORY_NOT_EMPTY },
1029 	{ EACCES,	ERRDOS, ERRnoaccess, NT_STATUS_ACCESS_DENIED },
1030 	{ ENOMEM,	ERRDOS, ERRnomem, NT_STATUS_NO_MEMORY },
1031 	{ EIO,		ERRHRD, ERRgeneral, NT_STATUS_IO_DEVICE_ERROR },
1032 	{ EXDEV, 	ERRSRV, ERRdiffdevice, NT_STATUS_NOT_SAME_DEVICE },
1033 	{ EROFS,	ERRHRD, ERRnowrite, NT_STATUS_ACCESS_DENIED },
1034 	{ ESTALE,	ERRDOS, ERRbadfid, NT_STATUS_INVALID_HANDLE},
1035 	{ EBADF,	ERRDOS, ERRbadfid, NT_STATUS_INVALID_HANDLE},
1036 	{ EEXIST,	ERRDOS, ERRfilexists, NT_STATUS_OBJECT_NAME_COLLISION},
1037 	{ ENXIO,	ERRSRV, ERRinvdevice, NT_STATUS_BAD_DEVICE_TYPE},
1038 	{ ESRCH,	ERRDOS, ERROR_FILE_NOT_FOUND,
1039 	    NT_STATUS_OBJECT_NAME_NOT_FOUND },
1040 	/*
1041 	 * It's not clear why smb_read_common effectively returns
1042 	 * ERRnoaccess if a range lock prevents access and smb_write_common
1043 	 * effectively returns ERRaccess.  This table entry is used by
1044 	 * smb_read_common and preserves the behavior that was there before.
1045 	 */
1046 	{ ERANGE,	ERRDOS, ERRnoaccess, NT_STATUS_FILE_LOCK_CONFLICT }
1047 };
1048 
1049 void
1050 smbsr_map_errno(int errnum, smb_error_t *err)
1051 {
1052 	int i;
1053 
1054 	for (i = 0; i < sizeof (smb_errno_map)/sizeof (smb_errno_map[0]); ++i) {
1055 		if (smb_errno_map[i].errnum == errnum) {
1056 			err->severity = ERROR_SEVERITY_ERROR;
1057 			err->status   = smb_errno_map[i].status32;
1058 			err->errcls   = smb_errno_map[i].errcls;
1059 			err->errcode  = smb_errno_map[i].errcode;
1060 			return;
1061 		}
1062 	}
1063 
1064 	err->severity = ERROR_SEVERITY_ERROR;
1065 	err->status   = NT_STATUS_INTERNAL_ERROR;
1066 	err->errcls   = ERRDOS;
1067 	err->errcode  = ERROR_INTERNAL_ERROR;
1068 }
1069 
1070 void
1071 smbsr_errno(struct smb_request *sr, int errnum)
1072 {
1073 	smbsr_map_errno(errnum, &sr->smb_error);
1074 	smbsr_set_error(sr, &sr->smb_error);
1075 }
1076 
1077 /*
1078  * Report a request processing warning.
1079  */
1080 void
1081 smbsr_warn(smb_request_t *sr, DWORD status, uint16_t errcls, uint16_t errcode)
1082 {
1083 	sr->smb_error.severity = ERROR_SEVERITY_WARNING;
1084 	sr->smb_error.status   = status;
1085 	sr->smb_error.errcls   = errcls;
1086 	sr->smb_error.errcode  = errcode;
1087 
1088 	smbsr_set_error(sr, &sr->smb_error);
1089 }
1090 
1091 /*
1092  * Report a request processing error.  This function will not return.
1093  */
1094 void
1095 smbsr_error(smb_request_t *sr, DWORD status, uint16_t errcls, uint16_t errcode)
1096 {
1097 	sr->smb_error.severity = ERROR_SEVERITY_ERROR;
1098 	sr->smb_error.status   = status;
1099 	sr->smb_error.errcls   = errcls;
1100 	sr->smb_error.errcode  = errcode;
1101 
1102 	smbsr_set_error(sr, &sr->smb_error);
1103 }
1104 
1105 /*
1106  * Setup a request processing error.  This function can be used to
1107  * report 32-bit status codes or DOS errors.  Set the status code
1108  * to 0 (NT_STATUS_SUCCESS) to explicitly report a DOS error,
1109  * regardless of the client capabilities.
1110  *
1111  * If status is non-zero and the client supports 32-bit status
1112  * codes, report the status.  Otherwise, report the DOS error.
1113  */
1114 void
1115 smbsr_set_error(smb_request_t *sr, smb_error_t *err)
1116 {
1117 	uint32_t status;
1118 	uint32_t severity;
1119 	uint32_t capabilities;
1120 
1121 	ASSERT(sr);
1122 	ASSERT(err);
1123 
1124 	status = err->status;
1125 	severity = (err->severity == 0) ? ERROR_SEVERITY_ERROR : err->severity;
1126 	capabilities = sr->session->capabilities;
1127 
1128 	if ((err->errcls == 0) && (err->errcode == 0)) {
1129 		capabilities |= CAP_STATUS32;
1130 		if (status == 0)
1131 			status = NT_STATUS_INTERNAL_ERROR;
1132 	}
1133 
1134 	if ((capabilities & CAP_STATUS32) && (status != 0)) {
1135 		status |= severity;
1136 		sr->smb_rcls = status & 0xff;
1137 		sr->smb_reh = (status >> 8) & 0xff;
1138 		sr->smb_err  = status >> 16;
1139 		sr->smb_flg2 |= SMB_FLAGS2_NT_STATUS;
1140 	} else {
1141 		if ((err->errcls == 0) || (err->errcode == 0)) {
1142 			sr->smb_rcls = ERRSRV;
1143 			sr->smb_err  = ERRerror;
1144 		} else {
1145 			sr->smb_rcls = (uint8_t)err->errcls;
1146 			sr->smb_err  = (uint16_t)err->errcode;
1147 		}
1148 	}
1149 }
1150 
1151 smb_xa_t *
1152 smbsr_lookup_xa(smb_request_t *sr)
1153 {
1154 	ASSERT(sr->r_xa == 0);
1155 
1156 	sr->r_xa = smb_xa_find(sr->session, sr->smb_pid, sr->smb_mid);
1157 	return (sr->r_xa);
1158 }
1159 
1160 void
1161 smbsr_disconnect_file(smb_request_t *sr)
1162 {
1163 	smb_ofile_t	*of = sr->fid_ofile;
1164 
1165 	sr->fid_ofile = NULL;
1166 	(void) smb_ofile_release(of);
1167 }
1168 
1169 void
1170 smbsr_disconnect_dir(smb_request_t *sr)
1171 {
1172 	smb_odir_t	*od = sr->sid_odir;
1173 
1174 	sr->sid_odir = NULL;
1175 	smb_odir_release(od);
1176 }
1177 
1178 static int
1179 is_andx_com(unsigned char com)
1180 {
1181 	switch (com) {
1182 	case SMB_COM_LOCKING_ANDX:
1183 	case SMB_COM_OPEN_ANDX:
1184 	case SMB_COM_READ_ANDX:
1185 	case SMB_COM_WRITE_ANDX:
1186 	case SMB_COM_SESSION_SETUP_ANDX:
1187 	case SMB_COM_LOGOFF_ANDX:
1188 	case SMB_COM_TREE_CONNECT_ANDX:
1189 	case SMB_COM_NT_CREATE_ANDX:
1190 		return (1);
1191 	}
1192 	return (0);
1193 }
1194 
1195 /*
1196  * Invalid command stubs.
1197  *
1198  * SmbWriteComplete is sent to acknowledge completion of raw write requests.
1199  * We never send raw write commands to other servers so, if we receive
1200  * SmbWriteComplete, we treat it as an error.
1201  *
1202  * The Read/Write Block Multiplexed (mpx) protocol is used to maximize
1203  * performance when reading/writing a large block of data: it can be
1204  * used in parallel with other client/server operations.  The mpx sub-
1205  * protocol is not supported because we support only connection oriented
1206  * transports and NT supports mpx only over connectionless transports.
1207  */
1208 smb_sdrc_t
1209 smb_pre_invalid(smb_request_t *sr)
1210 {
1211 	DTRACE_SMB_1(op__Invalid__start, smb_request_t *, sr);
1212 	return (SDRC_SUCCESS);
1213 }
1214 
1215 void
1216 smb_post_invalid(smb_request_t *sr)
1217 {
1218 	DTRACE_SMB_1(op__Invalid__done, smb_request_t *, sr);
1219 }
1220 
1221 smb_sdrc_t
1222 smb_com_invalid(smb_request_t *sr)
1223 {
1224 	smb_sdrc_t sdrc;
1225 
1226 	switch (sr->smb_com) {
1227 	case SMB_COM_WRITE_COMPLETE:
1228 		smbsr_error(sr, 0, ERRSRV, ERRerror);
1229 		sdrc = SDRC_ERROR;
1230 		break;
1231 
1232 	default:
1233 		smbsr_error(sr, NT_STATUS_NOT_IMPLEMENTED,
1234 		    ERRDOS, ERROR_INVALID_FUNCTION);
1235 		sdrc = SDRC_NOT_IMPLEMENTED;
1236 		break;
1237 	}
1238 
1239 	return (sdrc);
1240 }
1241 
1242 /*
1243  * smb_dispatch_kstat_update
1244  *
1245  * This callback function updates the smb_dispatch_kstat_data when kstat
1246  * command is invoked.
1247  */
1248 static int
1249 smb_dispatch_kstat_update(kstat_t *ksp, int rw)
1250 {
1251 	smb_dispatch_table_t *sdd;
1252 	kstat_named_t *ks_named;
1253 	int i;
1254 
1255 	if (rw == KSTAT_WRITE)
1256 		return (EACCES);
1257 
1258 	ASSERT(MUTEX_HELD(ksp->ks_lock));
1259 
1260 	ks_named = ksp->ks_data;
1261 	for (i = 0; i < SMB_COM_NUM; i++) {
1262 		sdd = &dispatch[i];
1263 
1264 		if (sdd->sdt_function != smb_com_invalid) {
1265 			bcopy(&sdd->sdt_dispatch_stats, ks_named,
1266 			    sizeof (kstat_named_t));
1267 			++ks_named;
1268 		}
1269 	}
1270 
1271 	return (0);
1272 }
1273 
1274 /*
1275  * smb_dispatch_kstat_init
1276  *
1277  * Initialize dispatch kstats.
1278  */
1279 void
1280 smb_dispatch_kstat_init(void)
1281 {
1282 	int ks_ndata;
1283 	int i;
1284 
1285 	for (i = 0, ks_ndata = 0; i < SMB_COM_NUM; i++) {
1286 		if (dispatch[i].sdt_function != smb_com_invalid)
1287 			ks_ndata++;
1288 	}
1289 
1290 	smb_dispatch_ksp = kstat_create(SMBSRV_KSTAT_MODULE, 0,
1291 	    SMBSRV_KSTAT_NAME_CMDS, SMBSRV_KSTAT_CLASS,
1292 	    KSTAT_TYPE_NAMED, ks_ndata, 0);
1293 
1294 	if (smb_dispatch_ksp) {
1295 		mutex_init(&smb_dispatch_ksmtx, NULL, MUTEX_DEFAULT, NULL);
1296 		smb_dispatch_ksp->ks_update = smb_dispatch_kstat_update;
1297 		smb_dispatch_ksp->ks_lock = &smb_dispatch_ksmtx;
1298 		kstat_install(smb_dispatch_ksp);
1299 	}
1300 }
1301 
1302 /*
1303  * smb_dispatch_kstat_fini
1304  *
1305  * Remove dispatch kstats.
1306  */
1307 void
1308 smb_dispatch_kstat_fini(void)
1309 {
1310 	if (smb_dispatch_ksp != NULL) {
1311 		kstat_delete(smb_dispatch_ksp);
1312 		mutex_destroy(&smb_dispatch_ksmtx);
1313 		smb_dispatch_ksp = NULL;
1314 	}
1315 }
1316