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