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