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