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