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