xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_dispatch.c (revision d0f40dc6a997c84bacf5f9ba83d57a95495c399b)
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 2010 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_kproto.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(nt_rename),				/* 0xA5 165 */
424 	    NT_LM_0_12, 0,
425 	    { "SmbNtRename",	KSTAT_DATA_UINT64 } },
426 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xA6 166 */
427 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xA7 167 */
428 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xA8 168 */
429 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xA9 169 */
430 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xAA 170 */
431 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xAB 171 */
432 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xAC 172 */
433 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xAD 173 */
434 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xAE 174 */
435 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xAF 175 */
436 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB0 176 */
437 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB1 177 */
438 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB2 178 */
439 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB3 179 */
440 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB4 180 */
441 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB5 181 */
442 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB6 182 */
443 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB7 183 */
444 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB8 184 */
445 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xB9 185 */
446 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xBA 186 */
447 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xBB 187 */
448 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xBC 188 */
449 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xBD 189 */
450 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xBE 190 */
451 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xBF 191 */
452 	{ SMB_SDT_OPS(open_print_file),				/* 0xC0 192 */
453 	    PC_NETWORK_PROGRAM_1_0, 0,
454 	    { "SmbOpenPrintFile", KSTAT_DATA_UINT64 } },
455 	{ SMB_SDT_OPS(write_print_file),			/* 0xC1 193 */
456 	    PC_NETWORK_PROGRAM_1_0, 0,
457 	    { "SmbWritePrintFile", KSTAT_DATA_UINT64 } },
458 	{ SMB_SDT_OPS(close_print_file),			/* 0xC2 194 */
459 	    PC_NETWORK_PROGRAM_1_0, 0,
460 	    { "SmbClosePrintFile", KSTAT_DATA_UINT64 } },
461 	{ SMB_SDT_OPS(get_print_queue),				/* 0xC3 195 */
462 	    PC_NETWORK_PROGRAM_1_0, 0,
463 	    { "SmbGetPrintQueue", KSTAT_DATA_UINT64 } },
464 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xC4 196 */
465 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xC5 197 */
466 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xC6 198 */
467 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xC7 199 */
468 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xC8 200 */
469 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xC9 201 */
470 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xCA 202 */
471 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xCB 203 */
472 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xCC 204 */
473 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xCD 205 */
474 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xCE 206 */
475 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xCF 207 */
476 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD0 208 */
477 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD1 209 */
478 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD2 210 */
479 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD3 211 */
480 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD4 212 */
481 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD5 213 */
482 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD6 214 */
483 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD7 215 */
484 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD8 216 */
485 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xD9 217 */
486 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xDA 218 */
487 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xDB 219 */
488 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xDC 220 */
489 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xDD 221 */
490 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xDE 222 */
491 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xDF 223 */
492 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE0 224 */
493 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE1 225 */
494 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE2 226 */
495 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE3 227 */
496 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE4 228 */
497 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE5 229 */
498 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE6 230 */
499 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE7 231 */
500 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE8 232 */
501 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xE9 233 */
502 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xEA 234 */
503 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xEB 235 */
504 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xEC 236 */
505 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xED 237 */
506 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xEE 238 */
507 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xEF 239 */
508 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF0 240 */
509 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF1 241 */
510 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF2 242 */
511 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF3 243 */
512 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF4 244 */
513 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF5 245 */
514 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF6 246 */
515 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF7 247 */
516 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF8 248 */
517 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xF9 249 */
518 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xFA 250 */
519 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xFB 251 */
520 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xFC 252 */
521 	{ SMB_SDT_OPS(invalid), 0, 0, 0 },			/* 0xFD 253 */
522 	{ SMB_SDT_OPS(invalid), LANMAN1_0, 0,			/* 0xFE 254 */
523 	    { "SmbInvalidCommand", KSTAT_DATA_UINT64 } },
524 	{ SMB_SDT_OPS(invalid), 0, 0, 0 }			/* 0xFF 255 */
525 };
526 
527 /*
528  * smbsr_cleanup
529  *
530  * If any user/tree/file is used by given request then
531  * the reference count for that resource has been incremented.
532  * This function decrements the reference count and close
533  * the resource if it's needed.
534  */
535 
536 void
537 smbsr_cleanup(smb_request_t *sr)
538 {
539 	ASSERT((sr->sr_state != SMB_REQ_STATE_CLEANED_UP) &&
540 	    (sr->sr_state != SMB_REQ_STATE_COMPLETED));
541 
542 	if (sr->r_xa) {
543 		if (sr->r_xa->xa_flags & SMB_XA_FLAG_COMPLETE)
544 			smb_xa_close(sr->r_xa);
545 		smb_xa_rele(sr->session, sr->r_xa);
546 		sr->r_xa = NULL;
547 	}
548 
549 	/*
550 	 * Mark this request so we know that we've already cleaned it up.
551 	 * A request should only get cleaned up once so multiple calls to
552 	 * smbsr_cleanup for the same request indicate a bug.
553 	 */
554 	mutex_enter(&sr->sr_mutex);
555 	if (sr->sr_state != SMB_REQ_STATE_CANCELED)
556 		sr->sr_state = SMB_REQ_STATE_CLEANED_UP;
557 	mutex_exit(&sr->sr_mutex);
558 }
559 /*
560  * smb_dispatch_request
561  *
562  * Returns:
563  *
564  *    B_TRUE	The caller must free the smb request passed in.
565  *    B_FALSE	The caller must not access the smb request passed in. It has
566  *		been kept in an internal queue and may have already been freed.
567  */
568 boolean_t
569 smb_dispatch_request(struct smb_request *sr)
570 {
571 	smb_sdrc_t		sdrc;
572 	smb_disp_entry_t	*sdd;
573 	boolean_t		disconnect = B_FALSE;
574 	smb_session_t		*session;
575 	uint32_t		capabilities;
576 	uint32_t		byte_count;
577 
578 	session = sr->session;
579 	capabilities = session->capabilities;
580 
581 	ASSERT(sr->tid_tree == 0);
582 	ASSERT(sr->uid_user == 0);
583 	ASSERT(sr->fid_ofile == 0);
584 	sr->smb_fid = (uint16_t)-1;
585 
586 	/* temporary until we identify a user */
587 	sr->user_cr = kcred;
588 	sr->orig_request_hdr = sr->command.chain_offset;
589 
590 	/* If this connection is shutting down just kill request */
591 	if (smb_mbc_decodef(&sr->command, SMB_HEADER_ED_FMT,
592 	    &sr->smb_com,
593 	    &sr->smb_rcls,
594 	    &sr->smb_reh,
595 	    &sr->smb_err,
596 	    &sr->smb_flg,
597 	    &sr->smb_flg2,
598 	    &sr->smb_pid_high,
599 	    sr->smb_sig,
600 	    &sr->smb_tid,
601 	    &sr->smb_pid,
602 	    &sr->smb_uid,
603 	    &sr->smb_mid) != 0) {
604 		disconnect = B_TRUE;
605 		goto drop_connection;
606 	}
607 
608 	/*
609 	 * The reply "header" is filled in now even though it will,
610 	 * most likely, be rewritten under reply_ready below.  We
611 	 * could reserve the space but this is convenient in case
612 	 * the dialect dispatcher has to send a special reply (like
613 	 * TRANSACT).
614 	 *
615 	 * Ensure that the 32-bit error code flag is turned off.
616 	 * Clients seem to set it in transact requests and they may
617 	 * get confused if we return success or a 16-bit SMB code.
618 	 */
619 	sr->smb_rcls = 0;
620 	sr->smb_reh = 0;
621 	sr->smb_err = 0;
622 	sr->smb_flg2 &= ~SMB_FLAGS2_NT_STATUS;
623 
624 	(void) smb_mbc_encodef(&sr->reply, SMB_HEADER_ED_FMT,
625 	    sr->smb_com,
626 	    sr->smb_rcls,
627 	    sr->smb_reh,
628 	    sr->smb_err,
629 	    sr->smb_flg,
630 	    sr->smb_flg2,
631 	    sr->smb_pid_high,
632 	    sr->smb_sig,
633 	    sr->smb_tid,
634 	    sr->smb_pid,
635 	    sr->smb_uid,
636 	    sr->smb_mid);
637 	sr->first_smb_com = sr->smb_com;
638 
639 	/*
640 	 * Verify SMB signature if signing is enabled, dialect is NT LM 0.12,
641 	 * signing was negotiated and authentication has occurred.
642 	 */
643 	if (session->signing.flags & SMB_SIGNING_ENABLED) {
644 		if (smb_sign_check_request(sr) != 0) {
645 			smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
646 			    ERRDOS, ERROR_ACCESS_DENIED);
647 			disconnect = B_TRUE;
648 			goto report_error;
649 		}
650 	}
651 
652 andx_more:
653 	sdd = &dispatch[sr->smb_com];
654 	ASSERT(sdd->sdt_function);
655 
656 	if (smb_mbc_decodef(&sr->command, "b", &sr->smb_wct) != 0) {
657 		disconnect = B_TRUE;
658 		goto report_error;
659 	}
660 
661 	(void) MBC_SHADOW_CHAIN(&sr->smb_vwv, &sr->command,
662 	    sr->command.chain_offset, sr->smb_wct * 2);
663 
664 	if (smb_mbc_decodef(&sr->command, "#.w", sr->smb_wct*2, &sr->smb_bcc)) {
665 		disconnect = B_TRUE;
666 		goto report_error;
667 	}
668 
669 	/*
670 	 * Ignore smb_bcc if CAP_LARGE_READX/CAP_LARGE_WRITEX
671 	 * and this is SmbReadX/SmbWriteX since this enables
672 	 * large reads/write and bcc is only 16-bits.
673 	 */
674 	if (((sr->smb_com == SMB_COM_READ_ANDX) &&
675 	    (capabilities & CAP_LARGE_READX)) ||
676 	    ((sr->smb_com == SMB_COM_WRITE_ANDX) &&
677 	    (capabilities & CAP_LARGE_WRITEX))) {
678 		byte_count = sr->command.max_bytes - sr->command.chain_offset;
679 	} else {
680 		byte_count = (uint32_t)sr->smb_bcc;
681 	}
682 
683 	(void) MBC_SHADOW_CHAIN(&sr->smb_data, &sr->command,
684 	    sr->command.chain_offset, byte_count);
685 
686 	sr->command.chain_offset += byte_count;
687 	if (sr->command.chain_offset > sr->command.max_bytes) {
688 		disconnect = B_TRUE;
689 		goto report_error;
690 	}
691 
692 	/* Store pointers for later */
693 	sr->cur_reply_offset = sr->reply.chain_offset;
694 
695 	if (is_andx_com(sr->smb_com)) {
696 		/* Peek ahead and don't disturb vwv */
697 		if (smb_mbc_peek(&sr->smb_vwv, sr->smb_vwv.chain_offset, "b.w",
698 		    &sr->andx_com, &sr->andx_off) < 0) {
699 			disconnect = B_TRUE;
700 			goto report_error;
701 		}
702 	} else {
703 		sr->andx_com = (unsigned char)-1;
704 	}
705 
706 	mutex_enter(&sr->sr_mutex);
707 	switch (sr->sr_state) {
708 	case SMB_REQ_STATE_SUBMITTED:
709 	case SMB_REQ_STATE_CLEANED_UP:
710 		sr->sr_state = SMB_REQ_STATE_ACTIVE;
711 		break;
712 	case SMB_REQ_STATE_CANCELED:
713 		break;
714 	default:
715 		ASSERT(0);
716 		break;
717 	}
718 	mutex_exit(&sr->sr_mutex);
719 
720 	/*
721 	 * Setup UID and TID information (if required). Both functions
722 	 * will set the sr credentials. In domain mode, the user and
723 	 * tree credentials should be the same. In share mode, the
724 	 * tree credentials (defined in the share definition) should
725 	 * override the user credentials.
726 	 */
727 	if (!(sdd->sdt_flags & SDDF_SUPPRESS_UID) && (sr->uid_user == NULL)) {
728 		sr->uid_user = smb_session_lookup_uid(session, sr->smb_uid);
729 		if (sr->uid_user == NULL) {
730 			smbsr_error(sr, 0, ERRSRV, ERRbaduid);
731 			smbsr_cleanup(sr);
732 			goto report_error;
733 		}
734 
735 		sr->user_cr = smb_user_getcred(sr->uid_user);
736 
737 		if (!(sdd->sdt_flags & SDDF_SUPPRESS_TID) &&
738 		    (sr->tid_tree == NULL)) {
739 			sr->tid_tree = smb_user_lookup_tree(
740 			    sr->uid_user, sr->smb_tid);
741 			if (sr->tid_tree == NULL) {
742 				smbsr_error(sr, 0, ERRSRV, ERRinvnid);
743 				smbsr_cleanup(sr);
744 				goto report_error;
745 			}
746 		}
747 	}
748 
749 	/*
750 	 * If the command is not a read raw request we can set the
751 	 * state of the session back to SMB_SESSION_STATE_NEGOTIATED
752 	 * (if the current state is SMB_SESSION_STATE_OPLOCK_BREAKING).
753 	 * Otherwise we let the read raw handler to deal with it.
754 	 */
755 	smb_rwx_rwenter(&session->s_lock, RW_READER);
756 	if ((session->s_state == SMB_SESSION_STATE_OPLOCK_BREAKING) &&
757 	    (sr->smb_com != SMB_COM_READ_RAW)) {
758 		(void) smb_rwx_rwupgrade(&session->s_lock);
759 		if (session->s_state == SMB_SESSION_STATE_OPLOCK_BREAKING)
760 			session->s_state = SMB_SESSION_STATE_NEGOTIATED;
761 	}
762 	smb_rwx_rwexit(&session->s_lock);
763 
764 	/*
765 	 * Increment method invocation count. This value is exposed
766 	 * via kstats, and it represents a count of all the dispatched
767 	 * requests, including the ones that have a return value, other
768 	 * than SDRC_SUCCESS.
769 	 */
770 	SMB_ALL_DISPATCH_STAT_INCR(sdd->sdt_dispatch_stats.value.ui64);
771 
772 	if ((sdrc = (*sdd->sdt_pre_op)(sr)) == SDRC_SUCCESS)
773 		sdrc = (*sdd->sdt_function)(sr);
774 
775 	if (sdrc != SDRC_SR_KEPT) {
776 		(*sdd->sdt_post_op)(sr);
777 		smbsr_cleanup(sr);
778 	}
779 
780 	if (sdrc != SDRC_SUCCESS) {
781 		/*
782 		 * Handle errors from raw write.
783 		 */
784 		smb_rwx_rwenter(&session->s_lock, RW_WRITER);
785 		if (session->s_state == SMB_SESSION_STATE_WRITE_RAW_ACTIVE) {
786 			/*
787 			 * Set state so that the netbios session
788 			 * daemon will start accepting data again.
789 			 */
790 			session->s_write_raw_status = 0;
791 			session->s_state = SMB_SESSION_STATE_NEGOTIATED;
792 		}
793 		smb_rwx_rwexit(&session->s_lock);
794 	}
795 
796 	switch (sdrc) {
797 	case SDRC_SUCCESS:
798 		break;
799 
800 	case SDRC_DROP_VC:
801 		disconnect = B_TRUE;
802 		goto drop_connection;
803 
804 	case SDRC_NO_REPLY:
805 		return (B_TRUE);
806 
807 	case SDRC_SR_KEPT:
808 		return (B_FALSE);
809 
810 	case SDRC_ERROR:
811 		goto report_error;
812 
813 	case SDRC_NOT_IMPLEMENTED:
814 	default:
815 		smbsr_error(sr, 0, ERRDOS, ERRbadfunc);
816 		goto report_error;
817 	}
818 
819 	/*
820 	 * If there's no AndX command, we're done.
821 	 */
822 	if (sr->andx_com == 0xff)
823 		goto reply_ready;
824 
825 	/*
826 	 * Otherwise, we have to back-patch the AndXCommand and AndXOffset
827 	 * and continue processing.
828 	 */
829 	sr->andx_prev_wct = sr->cur_reply_offset;
830 	(void) smb_mbc_poke(&sr->reply, sr->andx_prev_wct + 1, "b.w",
831 	    sr->andx_com, MBC_LENGTH(&sr->reply));
832 
833 	sr->command.chain_offset = sr->orig_request_hdr + sr->andx_off;
834 	sr->smb_com = sr->andx_com;
835 	goto andx_more;
836 
837 report_error:
838 	sr->reply.chain_offset = sr->cur_reply_offset;
839 	(void) smb_mbc_encodef(&sr->reply, "bw", 0, 0);
840 
841 	sr->smb_wct = 0;
842 	sr->smb_bcc = 0;
843 
844 	if (sr->smb_rcls == 0 && sr->smb_reh == 0 && sr->smb_err == 0)
845 		smbsr_error(sr, 0, ERRSRV, ERRerror);
846 
847 reply_ready:
848 	smbsr_send_reply(sr);
849 
850 drop_connection:
851 	if (disconnect) {
852 		smb_rwx_rwenter(&session->s_lock, RW_WRITER);
853 		switch (session->s_state) {
854 		case SMB_SESSION_STATE_DISCONNECTED:
855 		case SMB_SESSION_STATE_TERMINATED:
856 			break;
857 		default:
858 			smb_soshutdown(session->sock);
859 			session->s_state = SMB_SESSION_STATE_DISCONNECTED;
860 			break;
861 		}
862 		smb_rwx_rwexit(&session->s_lock);
863 	}
864 
865 	return (B_TRUE);
866 }
867 
868 int
869 smbsr_encode_empty_result(struct smb_request *sr)
870 {
871 	return (smbsr_encode_result(sr, 0, 0, "bw", 0, 0));
872 }
873 
874 int
875 smbsr_encode_result(struct smb_request *sr, int wct, int bcc, char *fmt, ...)
876 {
877 	va_list ap;
878 
879 	if (MBC_LENGTH(&sr->reply) != sr->cur_reply_offset)
880 		return (-1);
881 
882 	va_start(ap, fmt);
883 	(void) smb_mbc_vencodef(&sr->reply, fmt, ap);
884 	va_end(ap);
885 
886 	sr->smb_wct = (unsigned char)wct;
887 	sr->smb_bcc = (uint16_t)bcc;
888 
889 	if (smbsr_check_result(sr, wct, bcc) != 0)
890 		return (-1);
891 
892 	return (0);
893 }
894 
895 static int
896 smbsr_check_result(struct smb_request *sr, int wct, int bcc)
897 {
898 	int		offset = sr->cur_reply_offset;
899 	int		total_bytes;
900 	unsigned char	temp, temp1;
901 	struct mbuf	*m;
902 
903 	total_bytes = 0;
904 	m = sr->reply.chain;
905 	while (m != 0) {
906 		total_bytes += m->m_len;
907 		m = m->m_next;
908 	}
909 
910 	if ((offset + 3) > total_bytes)
911 		return (-1);
912 
913 	(void) smb_mbc_peek(&sr->reply, offset, "b", &temp);
914 	if (temp != wct)
915 		return (-1);
916 
917 	if ((offset + (wct * 2 + 1)) > total_bytes)
918 		return (-1);
919 
920 	/* reply wct & vwv seem ok, consider data now */
921 	offset += wct * 2 + 1;
922 
923 	if ((offset + 2) > total_bytes)
924 		return (-1);
925 
926 	(void) smb_mbc_peek(&sr->reply, offset, "bb", &temp, &temp1);
927 	if (bcc == VAR_BCC) {
928 		if ((temp != 0xFF) || (temp1 != 0xFF)) {
929 			return (-1);
930 		} else {
931 			bcc = (total_bytes - offset) - 2;
932 			(void) smb_mbc_poke(&sr->reply, offset, "bb",
933 			    bcc, bcc >> 8);
934 		}
935 	} else {
936 		if ((temp != (bcc&0xFF)) || (temp1 != ((bcc>>8)&0xFF)))
937 			return (-1);
938 	}
939 
940 	offset += bcc + 2;
941 
942 	if (offset != total_bytes)
943 		return (-1);
944 
945 	sr->smb_wct = (unsigned char)wct;
946 	sr->smb_bcc = (uint16_t)bcc;
947 	return (0);
948 }
949 
950 int
951 smbsr_decode_vwv(struct smb_request *sr, char *fmt, ...)
952 {
953 	int rc;
954 	va_list ap;
955 
956 	va_start(ap, fmt);
957 	rc = smb_mbc_vdecodef(&sr->smb_vwv, fmt, ap);
958 	va_end(ap);
959 
960 	if (rc)
961 		smbsr_error(sr, 0, ERRSRV, ERRerror);
962 	return (rc);
963 }
964 
965 int
966 smbsr_decode_data(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_data, fmt, ap);
973 	va_end(ap);
974 
975 	if (rc)
976 		smbsr_error(sr, 0, ERRSRV, ERRerror);
977 	return (rc);
978 }
979 
980 void
981 smbsr_send_reply(struct smb_request *sr)
982 {
983 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
984 		sr->smb_flg |= SMB_FLAGS_CASE_INSENSITIVE;
985 	else
986 		sr->smb_flg &= ~SMB_FLAGS_CASE_INSENSITIVE;
987 
988 	(void) smb_mbc_poke(&sr->reply, 0, SMB_HEADER_ED_FMT,
989 	    sr->first_smb_com,
990 	    sr->smb_rcls,
991 	    sr->smb_reh,
992 	    sr->smb_err,
993 	    sr->smb_flg | SMB_FLAGS_REPLY,
994 	    sr->smb_flg2,
995 	    sr->smb_pid_high,
996 	    sr->smb_sig,
997 	    sr->smb_tid,
998 	    sr->smb_pid,
999 	    sr->smb_uid,
1000 	    sr->smb_mid);
1001 
1002 	if (sr->session->signing.flags & SMB_SIGNING_ENABLED)
1003 		smb_sign_reply(sr, NULL);
1004 
1005 	if (smb_session_send(sr->session, 0, &sr->reply) == 0)
1006 		sr->reply.chain = 0;
1007 }
1008 
1009 /*
1010  * Map errno values to SMB and NT status values.
1011  * Note: ESRCH is a special case to handle a streams lookup failure.
1012  */
1013 static struct {
1014 	int errnum;
1015 	int errcls;
1016 	int errcode;
1017 	DWORD status32;
1018 } smb_errno_map[] = {
1019 	{ ENOSPC,	ERRDOS, ERROR_DISK_FULL, NT_STATUS_DISK_FULL },
1020 	{ EDQUOT,	ERRDOS, ERROR_DISK_FULL, NT_STATUS_DISK_FULL },
1021 	{ EPERM,	ERRSRV, ERRaccess, NT_STATUS_ACCESS_DENIED },
1022 	{ ENOTDIR,	ERRDOS, ERRbadpath, NT_STATUS_OBJECT_PATH_NOT_FOUND },
1023 	{ EISDIR,	ERRDOS, ERRbadpath, NT_STATUS_FILE_IS_A_DIRECTORY },
1024 	{ ENOENT,	ERRDOS, ERRbadfile, NT_STATUS_NO_SUCH_FILE },
1025 	{ ENOTEMPTY,	ERRDOS, ERROR_DIR_NOT_EMPTY,
1026 	    NT_STATUS_DIRECTORY_NOT_EMPTY },
1027 	{ EILSEQ,	ERRDOS, ERROR_INVALID_NAME,
1028 	    NT_STATUS_OBJECT_NAME_INVALID },
1029 	{ EACCES,	ERRDOS, ERRnoaccess, NT_STATUS_ACCESS_DENIED },
1030 	{ ENOMEM,	ERRDOS, ERRnomem, NT_STATUS_NO_MEMORY },
1031 	{ EIO,		ERRHRD, ERRgeneral, NT_STATUS_IO_DEVICE_ERROR },
1032 	{ EXDEV, 	ERRSRV, ERRdiffdevice, NT_STATUS_NOT_SAME_DEVICE },
1033 	{ EREMOTE, 	ERRSRV, ERRbadpath, NT_STATUS_PATH_NOT_COVERED},
1034 	{ EROFS,	ERRHRD, ERRnowrite, NT_STATUS_ACCESS_DENIED },
1035 	{ ESTALE,	ERRDOS, ERRbadfid, NT_STATUS_INVALID_HANDLE },
1036 	{ EBADF,	ERRDOS, ERRbadfid, NT_STATUS_INVALID_HANDLE },
1037 	{ EEXIST,	ERRDOS, ERRfilexists, NT_STATUS_OBJECT_NAME_COLLISION },
1038 	{ ENXIO,	ERRSRV, ERRinvdevice, NT_STATUS_BAD_DEVICE_TYPE },
1039 	{ ESRCH,	ERRDOS, ERROR_FILE_NOT_FOUND,
1040 	    NT_STATUS_OBJECT_NAME_NOT_FOUND },
1041 	/*
1042 	 * It's not clear why smb_read_common effectively returns
1043 	 * ERRnoaccess if a range lock prevents access and smb_write_common
1044 	 * effectively returns ERRaccess.  This table entry is used by
1045 	 * smb_read_common and preserves the behavior that was there before.
1046 	 */
1047 	{ ERANGE,	ERRDOS, ERRnoaccess, NT_STATUS_FILE_LOCK_CONFLICT }
1048 };
1049 
1050 void
1051 smbsr_map_errno(int errnum, smb_error_t *err)
1052 {
1053 	int i;
1054 
1055 	for (i = 0; i < sizeof (smb_errno_map)/sizeof (smb_errno_map[0]); ++i) {
1056 		if (smb_errno_map[i].errnum == errnum) {
1057 			err->severity = ERROR_SEVERITY_ERROR;
1058 			err->status   = smb_errno_map[i].status32;
1059 			err->errcls   = smb_errno_map[i].errcls;
1060 			err->errcode  = smb_errno_map[i].errcode;
1061 			return;
1062 		}
1063 	}
1064 
1065 	err->severity = ERROR_SEVERITY_ERROR;
1066 	err->status   = NT_STATUS_INTERNAL_ERROR;
1067 	err->errcls   = ERRDOS;
1068 	err->errcode  = ERROR_INTERNAL_ERROR;
1069 }
1070 
1071 void
1072 smbsr_errno(struct smb_request *sr, int errnum)
1073 {
1074 	smbsr_map_errno(errnum, &sr->smb_error);
1075 	smbsr_set_error(sr, &sr->smb_error);
1076 }
1077 
1078 /*
1079  * Report a request processing warning.
1080  */
1081 void
1082 smbsr_warn(smb_request_t *sr, DWORD status, uint16_t errcls, uint16_t errcode)
1083 {
1084 	sr->smb_error.severity = ERROR_SEVERITY_WARNING;
1085 	sr->smb_error.status   = status;
1086 	sr->smb_error.errcls   = errcls;
1087 	sr->smb_error.errcode  = errcode;
1088 
1089 	smbsr_set_error(sr, &sr->smb_error);
1090 }
1091 
1092 /*
1093  * Report a request processing error.  This function will not return.
1094  */
1095 void
1096 smbsr_error(smb_request_t *sr, DWORD status, uint16_t errcls, uint16_t errcode)
1097 {
1098 	sr->smb_error.severity = ERROR_SEVERITY_ERROR;
1099 	sr->smb_error.status   = status;
1100 	sr->smb_error.errcls   = errcls;
1101 	sr->smb_error.errcode  = errcode;
1102 
1103 	smbsr_set_error(sr, &sr->smb_error);
1104 }
1105 
1106 /*
1107  * Setup a request processing error.  This function can be used to
1108  * report 32-bit status codes or DOS errors.  Set the status code
1109  * to 0 (NT_STATUS_SUCCESS) to explicitly report a DOS error,
1110  * regardless of the client capabilities.
1111  *
1112  * If status is non-zero and the client supports 32-bit status
1113  * codes, report the status.  Otherwise, report the DOS error.
1114  */
1115 void
1116 smbsr_set_error(smb_request_t *sr, smb_error_t *err)
1117 {
1118 	uint32_t status;
1119 	uint32_t severity;
1120 	uint32_t capabilities;
1121 
1122 	ASSERT(sr);
1123 	ASSERT(err);
1124 
1125 	status = err->status;
1126 	severity = (err->severity == 0) ? ERROR_SEVERITY_ERROR : err->severity;
1127 	capabilities = sr->session->capabilities;
1128 
1129 	if ((err->errcls == 0) && (err->errcode == 0)) {
1130 		capabilities |= CAP_STATUS32;
1131 		if (status == 0)
1132 			status = NT_STATUS_INTERNAL_ERROR;
1133 	}
1134 
1135 	if ((capabilities & CAP_STATUS32) && (status != 0)) {
1136 		status |= severity;
1137 		sr->smb_rcls = status & 0xff;
1138 		sr->smb_reh = (status >> 8) & 0xff;
1139 		sr->smb_err  = status >> 16;
1140 		sr->smb_flg2 |= SMB_FLAGS2_NT_STATUS;
1141 	} else {
1142 		if ((err->errcls == 0) || (err->errcode == 0)) {
1143 			sr->smb_rcls = ERRSRV;
1144 			sr->smb_err  = ERRerror;
1145 		} else {
1146 			sr->smb_rcls = (uint8_t)err->errcls;
1147 			sr->smb_err  = (uint16_t)err->errcode;
1148 		}
1149 	}
1150 }
1151 
1152 smb_xa_t *
1153 smbsr_lookup_xa(smb_request_t *sr)
1154 {
1155 	ASSERT(sr->r_xa == 0);
1156 
1157 	sr->r_xa = smb_xa_find(sr->session, sr->smb_pid, sr->smb_mid);
1158 	return (sr->r_xa);
1159 }
1160 
1161 void
1162 smbsr_release_file(smb_request_t *sr)
1163 {
1164 	smb_ofile_t	*of = sr->fid_ofile;
1165 
1166 	sr->fid_ofile = NULL;
1167 	(void) smb_ofile_release(of);
1168 }
1169 
1170 void
1171 smbsr_lookup_file(smb_request_t *sr)
1172 {
1173 	if (sr->fid_ofile == NULL)
1174 		sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree,
1175 		    sr->smb_fid);
1176 }
1177 
1178 static int
1179 is_andx_com(unsigned char com)
1180 {
1181 	switch (com) {
1182 	case SMB_COM_LOCKING_ANDX:
1183 	case SMB_COM_OPEN_ANDX:
1184 	case SMB_COM_READ_ANDX:
1185 	case SMB_COM_WRITE_ANDX:
1186 	case SMB_COM_SESSION_SETUP_ANDX:
1187 	case SMB_COM_LOGOFF_ANDX:
1188 	case SMB_COM_TREE_CONNECT_ANDX:
1189 	case SMB_COM_NT_CREATE_ANDX:
1190 		return (1);
1191 	}
1192 	return (0);
1193 }
1194 
1195 /*
1196  * Invalid command stubs.
1197  *
1198  * SmbWriteComplete is sent to acknowledge completion of raw write requests.
1199  * We never send raw write commands to other servers so, if we receive
1200  * SmbWriteComplete, we treat it as an error.
1201  *
1202  * The Read/Write Block Multiplexed (mpx) protocol is used to maximize
1203  * performance when reading/writing a large block of data: it can be
1204  * used in parallel with other client/server operations.  The mpx sub-
1205  * protocol is not supported because we support only connection oriented
1206  * transports and NT supports mpx only over connectionless transports.
1207  */
1208 smb_sdrc_t
1209 smb_pre_invalid(smb_request_t *sr)
1210 {
1211 	DTRACE_SMB_1(op__Invalid__start, smb_request_t *, sr);
1212 	return (SDRC_SUCCESS);
1213 }
1214 
1215 void
1216 smb_post_invalid(smb_request_t *sr)
1217 {
1218 	DTRACE_SMB_1(op__Invalid__done, smb_request_t *, sr);
1219 }
1220 
1221 smb_sdrc_t
1222 smb_com_invalid(smb_request_t *sr)
1223 {
1224 	smb_sdrc_t sdrc;
1225 
1226 	switch (sr->smb_com) {
1227 	case SMB_COM_WRITE_COMPLETE:
1228 		smbsr_error(sr, 0, ERRSRV, ERRerror);
1229 		sdrc = SDRC_ERROR;
1230 		break;
1231 
1232 	default:
1233 		smbsr_error(sr, NT_STATUS_NOT_IMPLEMENTED,
1234 		    ERRDOS, ERROR_INVALID_FUNCTION);
1235 		sdrc = SDRC_NOT_IMPLEMENTED;
1236 		break;
1237 	}
1238 
1239 	return (sdrc);
1240 }
1241 
1242 /*
1243  * smb_dispatch_kstat_update
1244  *
1245  * This callback function updates the smb_dispatch_kstat_data when kstat
1246  * command is invoked.
1247  */
1248 static int
1249 smb_dispatch_kstat_update(kstat_t *ksp, int rw)
1250 {
1251 	smb_disp_entry_t	*sdd;
1252 	kstat_named_t		*ks_named;
1253 	int i;
1254 
1255 	if (rw == KSTAT_WRITE)
1256 		return (EACCES);
1257 
1258 	ASSERT(MUTEX_HELD(ksp->ks_lock));
1259 
1260 	ks_named = ksp->ks_data;
1261 	for (i = 0; i < SMB_COM_NUM; i++) {
1262 		sdd = &dispatch[i];
1263 
1264 		if (sdd->sdt_function != smb_com_invalid) {
1265 			bcopy(&sdd->sdt_dispatch_stats, ks_named,
1266 			    sizeof (kstat_named_t));
1267 			++ks_named;
1268 		}
1269 	}
1270 
1271 	return (0);
1272 }
1273 
1274 /*
1275  * smb_dispatch_kstat_init
1276  *
1277  * Initialize dispatch kstats.
1278  */
1279 void
1280 smb_dispatch_kstat_init(void)
1281 {
1282 	int ks_ndata;
1283 	int i;
1284 
1285 	for (i = 0, ks_ndata = 0; i < SMB_COM_NUM; i++) {
1286 		if (dispatch[i].sdt_function != smb_com_invalid)
1287 			ks_ndata++;
1288 	}
1289 
1290 	smb_dispatch_ksp = kstat_create(SMBSRV_KSTAT_MODULE, 0,
1291 	    SMBSRV_KSTAT_NAME_CMDS, SMBSRV_KSTAT_CLASS,
1292 	    KSTAT_TYPE_NAMED, ks_ndata, 0);
1293 
1294 	if (smb_dispatch_ksp) {
1295 		mutex_init(&smb_dispatch_ksmtx, NULL, MUTEX_DEFAULT, NULL);
1296 		smb_dispatch_ksp->ks_update = smb_dispatch_kstat_update;
1297 		smb_dispatch_ksp->ks_lock = &smb_dispatch_ksmtx;
1298 		kstat_install(smb_dispatch_ksp);
1299 	}
1300 }
1301 
1302 /*
1303  * smb_dispatch_kstat_fini
1304  *
1305  * Remove dispatch kstats.
1306  */
1307 void
1308 smb_dispatch_kstat_fini(void)
1309 {
1310 	if (smb_dispatch_ksp != NULL) {
1311 		kstat_delete(smb_dispatch_ksp);
1312 		mutex_destroy(&smb_dispatch_ksmtx);
1313 		smb_dispatch_ksp = NULL;
1314 	}
1315 }
1316