]>
Commit | Line | Data |
---|---|---|
093b2bda PS |
1 | /* |
2 | * fs/cifs/smb2misc.c | |
3 | * | |
4 | * Copyright (C) International Business Machines Corp., 2002,2011 | |
5 | * Etersoft, 2012 | |
6 | * Author(s): Steve French (sfrench@us.ibm.com) | |
7 | * Pavel Shilovsky (pshilovsky@samba.org) 2012 | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU Lesser General Public License as published | |
11 | * by the Free Software Foundation; either version 2.1 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
17 | * the GNU Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public License | |
20 | * along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
23 | #include <linux/ctype.h> | |
24 | #include "smb2pdu.h" | |
25 | #include "cifsglob.h" | |
26 | #include "cifsproto.h" | |
27 | #include "smb2proto.h" | |
28 | #include "cifs_debug.h" | |
29 | #include "cifs_unicode.h" | |
30 | #include "smb2status.h" | |
31473fc4 | 31 | #include "smb2glob.h" |
093b2bda PS |
32 | |
33 | static int | |
31473fc4 | 34 | check_smb2_hdr(struct smb2_sync_hdr *shdr, __u64 mid) |
093b2bda | 35 | { |
31473fc4 | 36 | __u64 wire_mid = le64_to_cpu(shdr->MessageId); |
9235d098 | 37 | |
093b2bda PS |
38 | /* |
39 | * Make sure that this really is an SMB, that it is a response, | |
40 | * and that the message ids match. | |
41 | */ | |
31473fc4 | 42 | if ((shdr->ProtocolId == SMB2_PROTO_NUMBER) && |
9235d098 | 43 | (mid == wire_mid)) { |
31473fc4 | 44 | if (shdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) |
093b2bda PS |
45 | return 0; |
46 | else { | |
47 | /* only one valid case where server sends us request */ | |
31473fc4 | 48 | if (shdr->Command == SMB2_OPLOCK_BREAK) |
093b2bda PS |
49 | return 0; |
50 | else | |
f96637be | 51 | cifs_dbg(VFS, "Received Request not response\n"); |
093b2bda PS |
52 | } |
53 | } else { /* bad signature or mid */ | |
31473fc4 | 54 | if (shdr->ProtocolId != SMB2_PROTO_NUMBER) |
f96637be | 55 | cifs_dbg(VFS, "Bad protocol string signature header %x\n", |
31473fc4 | 56 | le32_to_cpu(shdr->ProtocolId)); |
9235d098 | 57 | if (mid != wire_mid) |
f96637be | 58 | cifs_dbg(VFS, "Mids do not match: %llu and %llu\n", |
9235d098 | 59 | mid, wire_mid); |
093b2bda | 60 | } |
9235d098 | 61 | cifs_dbg(VFS, "Bad SMB detected. The Mid=%llu\n", wire_mid); |
093b2bda PS |
62 | return 1; |
63 | } | |
64 | ||
65 | /* | |
66 | * The following table defines the expected "StructureSize" of SMB2 responses | |
67 | * in order by SMB2 command. This is similar to "wct" in SMB/CIFS responses. | |
68 | * | |
69 | * Note that commands are defined in smb2pdu.h in le16 but the array below is | |
70 | * indexed by command in host byte order | |
71 | */ | |
72 | static const __le16 smb2_rsp_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = { | |
bc09d141 FF |
73 | /* SMB2_NEGOTIATE */ cpu_to_le16(65), |
74 | /* SMB2_SESSION_SETUP */ cpu_to_le16(9), | |
75 | /* SMB2_LOGOFF */ cpu_to_le16(4), | |
76 | /* SMB2_TREE_CONNECT */ cpu_to_le16(16), | |
77 | /* SMB2_TREE_DISCONNECT */ cpu_to_le16(4), | |
78 | /* SMB2_CREATE */ cpu_to_le16(89), | |
79 | /* SMB2_CLOSE */ cpu_to_le16(60), | |
80 | /* SMB2_FLUSH */ cpu_to_le16(4), | |
81 | /* SMB2_READ */ cpu_to_le16(17), | |
82 | /* SMB2_WRITE */ cpu_to_le16(17), | |
83 | /* SMB2_LOCK */ cpu_to_le16(4), | |
84 | /* SMB2_IOCTL */ cpu_to_le16(49), | |
093b2bda | 85 | /* BB CHECK this ... not listed in documentation */ |
bc09d141 FF |
86 | /* SMB2_CANCEL */ cpu_to_le16(0), |
87 | /* SMB2_ECHO */ cpu_to_le16(4), | |
88 | /* SMB2_QUERY_DIRECTORY */ cpu_to_le16(9), | |
89 | /* SMB2_CHANGE_NOTIFY */ cpu_to_le16(9), | |
90 | /* SMB2_QUERY_INFO */ cpu_to_le16(9), | |
91 | /* SMB2_SET_INFO */ cpu_to_le16(2), | |
093b2bda | 92 | /* BB FIXME can also be 44 for lease break */ |
bc09d141 | 93 | /* SMB2_OPLOCK_BREAK */ cpu_to_le16(24) |
093b2bda PS |
94 | }; |
95 | ||
136ff1b4 | 96 | #ifdef CONFIG_CIFS_SMB311 |
98170fb5 RS |
97 | static __u32 get_neg_ctxt_len(struct smb2_sync_hdr *hdr, __u32 len, |
98 | __u32 non_ctxlen, | |
0d4b46ba | 99 | size_t hdr_preamble_size) |
136ff1b4 SF |
100 | { |
101 | __u16 neg_count; | |
102 | __u32 nc_offset, size_of_pad_before_neg_ctxts; | |
103 | struct smb2_negotiate_rsp *pneg_rsp = (struct smb2_negotiate_rsp *)hdr; | |
104 | ||
105 | /* Negotiate contexts are only valid for latest dialect SMB3.11 */ | |
106 | neg_count = le16_to_cpu(pneg_rsp->NegotiateContextCount); | |
107 | if ((neg_count == 0) || | |
108 | (pneg_rsp->DialectRevision != cpu_to_le16(SMB311_PROT_ID))) | |
109 | return 0; | |
110 | ||
111 | /* Make sure that negotiate contexts start after gss security blob */ | |
112 | nc_offset = le32_to_cpu(pneg_rsp->NegotiateContextOffset); | |
0d4b46ba | 113 | if (nc_offset < non_ctxlen - hdr_preamble_size /* RFC1001 len */) { |
136ff1b4 SF |
114 | printk_once(KERN_WARNING "invalid negotiate context offset\n"); |
115 | return 0; | |
116 | } | |
0d4b46ba SF |
117 | size_of_pad_before_neg_ctxts = nc_offset - |
118 | (non_ctxlen - hdr_preamble_size); | |
136ff1b4 SF |
119 | |
120 | /* Verify that at least minimal negotiate contexts fit within frame */ | |
121 | if (len < nc_offset + (neg_count * sizeof(struct smb2_neg_context))) { | |
122 | printk_once(KERN_WARNING "negotiate context goes beyond end\n"); | |
123 | return 0; | |
124 | } | |
125 | ||
126 | cifs_dbg(FYI, "length of negcontexts %d pad %d\n", | |
127 | len - nc_offset, size_of_pad_before_neg_ctxts); | |
128 | ||
129 | /* length of negcontexts including pad from end of sec blob to them */ | |
130 | return (len - nc_offset) + size_of_pad_before_neg_ctxts; | |
131 | } | |
132 | #endif /* CIFS_SMB311 */ | |
133 | ||
093b2bda | 134 | int |
98170fb5 | 135 | smb2_check_message(char *buf, unsigned int len, struct TCP_Server_Info *srvr) |
093b2bda | 136 | { |
98170fb5 RS |
137 | struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)(buf + srvr->vals->header_preamble_size); |
138 | struct smb2_sync_pdu *pdu = (struct smb2_sync_pdu *)shdr; | |
373512ec | 139 | __u64 mid; |
093b2bda PS |
140 | __u32 clc_len; /* calculated length */ |
141 | int command; | |
98170fb5 RS |
142 | int pdu_size = sizeof(struct smb2_sync_pdu); |
143 | int hdr_size = sizeof(struct smb2_sync_hdr); | |
093b2bda PS |
144 | |
145 | /* | |
146 | * Add function to do table lookup of StructureSize by command | |
147 | * ie Validate the wct via smb2_struct_sizes table above | |
148 | */ | |
31473fc4 | 149 | if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) { |
373512ec SF |
150 | struct smb2_transform_hdr *thdr = |
151 | (struct smb2_transform_hdr *)buf; | |
152 | struct cifs_ses *ses = NULL; | |
153 | struct list_head *tmp; | |
154 | ||
155 | /* decrypt frame now that it is completely read in */ | |
156 | spin_lock(&cifs_tcp_ses_lock); | |
157 | list_for_each(tmp, &srvr->smb_ses_list) { | |
158 | ses = list_entry(tmp, struct cifs_ses, smb_ses_list); | |
159 | if (ses->Suid == thdr->SessionId) | |
160 | break; | |
161 | ||
162 | ses = NULL; | |
163 | } | |
164 | spin_unlock(&cifs_tcp_ses_lock); | |
165 | if (ses == NULL) { | |
166 | cifs_dbg(VFS, "no decryption - session id not found\n"); | |
167 | return 1; | |
168 | } | |
169 | } | |
170 | ||
31473fc4 | 171 | mid = le64_to_cpu(shdr->MessageId); |
98170fb5 RS |
172 | if (len < pdu_size) { |
173 | if ((len >= hdr_size) | |
31473fc4 | 174 | && (shdr->Status != 0)) { |
093b2bda PS |
175 | pdu->StructureSize2 = 0; |
176 | /* | |
177 | * As with SMB/CIFS, on some error cases servers may | |
178 | * not return wct properly | |
179 | */ | |
180 | return 0; | |
181 | } else { | |
f96637be | 182 | cifs_dbg(VFS, "Length less than SMB header size\n"); |
093b2bda PS |
183 | } |
184 | return 1; | |
185 | } | |
93012bf9 RS |
186 | if (len > CIFSMaxBufSize + MAX_SMB2_HDR_SIZE - |
187 | srvr->vals->header_preamble_size) { | |
f96637be JP |
188 | cifs_dbg(VFS, "SMB length greater than maximum, mid=%llu\n", |
189 | mid); | |
093b2bda PS |
190 | return 1; |
191 | } | |
192 | ||
31473fc4 | 193 | if (check_smb2_hdr(shdr, mid)) |
093b2bda PS |
194 | return 1; |
195 | ||
31473fc4 | 196 | if (shdr->StructureSize != SMB2_HEADER_STRUCTURE_SIZE) { |
f96637be | 197 | cifs_dbg(VFS, "Illegal structure size %u\n", |
31473fc4 | 198 | le16_to_cpu(shdr->StructureSize)); |
093b2bda PS |
199 | return 1; |
200 | } | |
201 | ||
31473fc4 | 202 | command = le16_to_cpu(shdr->Command); |
093b2bda | 203 | if (command >= NUMBER_OF_SMB2_COMMANDS) { |
f96637be | 204 | cifs_dbg(VFS, "Illegal SMB2 command %d\n", command); |
093b2bda PS |
205 | return 1; |
206 | } | |
207 | ||
208 | if (smb2_rsp_struct_sizes[command] != pdu->StructureSize2) { | |
31473fc4 | 209 | if (command != SMB2_OPLOCK_BREAK_HE && (shdr->Status == 0 || |
983c88a4 | 210 | pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2)) { |
093b2bda | 211 | /* error packets have 9 byte structure size */ |
f96637be JP |
212 | cifs_dbg(VFS, "Illegal response size %u for command %d\n", |
213 | le16_to_cpu(pdu->StructureSize2), command); | |
093b2bda | 214 | return 1; |
31473fc4 PS |
215 | } else if (command == SMB2_OPLOCK_BREAK_HE |
216 | && (shdr->Status == 0) | |
0822f514 PS |
217 | && (le16_to_cpu(pdu->StructureSize2) != 44) |
218 | && (le16_to_cpu(pdu->StructureSize2) != 36)) { | |
219 | /* special case for SMB2.1 lease break message */ | |
f96637be JP |
220 | cifs_dbg(VFS, "Illegal response size %d for oplock break\n", |
221 | le16_to_cpu(pdu->StructureSize2)); | |
0822f514 | 222 | return 1; |
093b2bda PS |
223 | } |
224 | } | |
225 | ||
98170fb5 | 226 | clc_len = smb2_calc_size(buf, srvr); |
093b2bda | 227 | |
136ff1b4 SF |
228 | #ifdef CONFIG_CIFS_SMB311 |
229 | if (shdr->Command == SMB2_NEGOTIATE) | |
98170fb5 RS |
230 | clc_len += get_neg_ctxt_len(shdr, len, clc_len, |
231 | srvr->vals->header_preamble_size); | |
136ff1b4 | 232 | #endif /* SMB311 */ |
98170fb5 RS |
233 | if (len != clc_len) { |
234 | cifs_dbg(FYI, "Calculated size %u length %u mismatch mid %llu\n", | |
235 | clc_len, len, mid); | |
b42bf888 PS |
236 | /* create failed on symlink */ |
237 | if (command == SMB2_CREATE_HE && | |
31473fc4 | 238 | shdr->Status == STATUS_STOPPED_ON_SYMLINK) |
b42bf888 | 239 | return 0; |
983c88a4 | 240 | /* Windows 7 server returns 24 bytes more */ |
98170fb5 | 241 | if (clc_len + 24 == len && command == SMB2_OPLOCK_BREAK_HE) |
983c88a4 | 242 | return 0; |
754789a1 | 243 | /* server can return one byte more due to implied bcc[0] */ |
98170fb5 | 244 | if (clc_len == len + 1) |
74112860 | 245 | return 0; |
754789a1 SF |
246 | |
247 | /* | |
248 | * MacOS server pads after SMB2.1 write response with 3 bytes | |
249 | * of junk. Other servers match RFC1001 len to actual | |
250 | * SMB2/SMB3 frame length (header + smb2 response specific data) | |
251 | * Log the server error (once), but allow it and continue | |
252 | * since the frame is parseable. | |
253 | */ | |
98170fb5 | 254 | if (clc_len < len) { |
754789a1 | 255 | printk_once(KERN_WARNING |
98170fb5 RS |
256 | "SMB2 server sent bad RFC1001 len %d not %u\n", |
257 | len, clc_len); | |
754789a1 SF |
258 | return 0; |
259 | } | |
260 | ||
093b2bda PS |
261 | return 1; |
262 | } | |
263 | return 0; | |
264 | } | |
265 | ||
266 | /* | |
267 | * The size of the variable area depends on the offset and length fields | |
268 | * located in different fields for various SMB2 responses. SMB2 responses | |
269 | * with no variable length info, show an offset of zero for the offset field. | |
270 | */ | |
271 | static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = { | |
272 | /* SMB2_NEGOTIATE */ true, | |
273 | /* SMB2_SESSION_SETUP */ true, | |
274 | /* SMB2_LOGOFF */ false, | |
275 | /* SMB2_TREE_CONNECT */ false, | |
276 | /* SMB2_TREE_DISCONNECT */ false, | |
277 | /* SMB2_CREATE */ true, | |
278 | /* SMB2_CLOSE */ false, | |
279 | /* SMB2_FLUSH */ false, | |
280 | /* SMB2_READ */ true, | |
281 | /* SMB2_WRITE */ false, | |
282 | /* SMB2_LOCK */ false, | |
283 | /* SMB2_IOCTL */ true, | |
284 | /* SMB2_CANCEL */ false, /* BB CHECK this not listed in documentation */ | |
285 | /* SMB2_ECHO */ false, | |
286 | /* SMB2_QUERY_DIRECTORY */ true, | |
287 | /* SMB2_CHANGE_NOTIFY */ true, | |
288 | /* SMB2_QUERY_INFO */ true, | |
289 | /* SMB2_SET_INFO */ false, | |
290 | /* SMB2_OPLOCK_BREAK */ false | |
291 | }; | |
292 | ||
293 | /* | |
294 | * Returns the pointer to the beginning of the data area. Length of the data | |
295 | * area and the offset to it (from the beginning of the smb are also returned. | |
296 | */ | |
ec2e4523 | 297 | char * |
093b2bda PS |
298 | smb2_get_data_area_len(int *off, int *len, struct smb2_hdr *hdr) |
299 | { | |
31473fc4 | 300 | struct smb2_sync_hdr *shdr = get_sync_hdr(hdr); |
093b2bda PS |
301 | *off = 0; |
302 | *len = 0; | |
303 | ||
304 | /* error responses do not have data area */ | |
31473fc4 | 305 | if (shdr->Status && shdr->Status != STATUS_MORE_PROCESSING_REQUIRED && |
093b2bda PS |
306 | (((struct smb2_err_rsp *)hdr)->StructureSize) == |
307 | SMB2_ERROR_STRUCTURE_SIZE2) | |
308 | return NULL; | |
309 | ||
310 | /* | |
311 | * Following commands have data areas so we have to get the location | |
312 | * of the data buffer offset and data buffer length for the particular | |
313 | * command. | |
314 | */ | |
31473fc4 | 315 | switch (shdr->Command) { |
093b2bda | 316 | case SMB2_NEGOTIATE: |
ec2e4523 PS |
317 | *off = le16_to_cpu( |
318 | ((struct smb2_negotiate_rsp *)hdr)->SecurityBufferOffset); | |
319 | *len = le16_to_cpu( | |
320 | ((struct smb2_negotiate_rsp *)hdr)->SecurityBufferLength); | |
321 | break; | |
093b2bda | 322 | case SMB2_SESSION_SETUP: |
5478f9ba PS |
323 | *off = le16_to_cpu( |
324 | ((struct smb2_sess_setup_rsp *)hdr)->SecurityBufferOffset); | |
325 | *len = le16_to_cpu( | |
326 | ((struct smb2_sess_setup_rsp *)hdr)->SecurityBufferLength); | |
327 | break; | |
093b2bda | 328 | case SMB2_CREATE: |
2503a0db PS |
329 | *off = le32_to_cpu( |
330 | ((struct smb2_create_rsp *)hdr)->CreateContextsOffset); | |
331 | *len = le32_to_cpu( | |
332 | ((struct smb2_create_rsp *)hdr)->CreateContextsLength); | |
333 | break; | |
093b2bda | 334 | case SMB2_QUERY_INFO: |
be4cb9e3 PS |
335 | *off = le16_to_cpu( |
336 | ((struct smb2_query_info_rsp *)hdr)->OutputBufferOffset); | |
337 | *len = le32_to_cpu( | |
338 | ((struct smb2_query_info_rsp *)hdr)->OutputBufferLength); | |
339 | break; | |
340 | case SMB2_READ: | |
09a4707e PS |
341 | *off = ((struct smb2_read_rsp *)hdr)->DataOffset; |
342 | *len = le32_to_cpu(((struct smb2_read_rsp *)hdr)->DataLength); | |
343 | break; | |
093b2bda | 344 | case SMB2_QUERY_DIRECTORY: |
d324f08d PS |
345 | *off = le16_to_cpu( |
346 | ((struct smb2_query_directory_rsp *)hdr)->OutputBufferOffset); | |
347 | *len = le32_to_cpu( | |
348 | ((struct smb2_query_directory_rsp *)hdr)->OutputBufferLength); | |
349 | break; | |
093b2bda | 350 | case SMB2_IOCTL: |
4a72dafa SF |
351 | *off = le32_to_cpu( |
352 | ((struct smb2_ioctl_rsp *)hdr)->OutputOffset); | |
353 | *len = le32_to_cpu(((struct smb2_ioctl_rsp *)hdr)->OutputCount); | |
354 | break; | |
093b2bda PS |
355 | case SMB2_CHANGE_NOTIFY: |
356 | default: | |
357 | /* BB FIXME for unimplemented cases above */ | |
f96637be | 358 | cifs_dbg(VFS, "no length check for command\n"); |
093b2bda PS |
359 | break; |
360 | } | |
361 | ||
362 | /* | |
363 | * Invalid length or offset probably means data area is invalid, but | |
364 | * we have little choice but to ignore the data area in this case. | |
365 | */ | |
366 | if (*off > 4096) { | |
f96637be | 367 | cifs_dbg(VFS, "offset %d too large, data area ignored\n", *off); |
093b2bda PS |
368 | *len = 0; |
369 | *off = 0; | |
370 | } else if (*off < 0) { | |
f96637be JP |
371 | cifs_dbg(VFS, "negative offset %d to data invalid ignore data area\n", |
372 | *off); | |
093b2bda PS |
373 | *off = 0; |
374 | *len = 0; | |
375 | } else if (*len < 0) { | |
f96637be JP |
376 | cifs_dbg(VFS, "negative data length %d invalid, data area ignored\n", |
377 | *len); | |
093b2bda PS |
378 | *len = 0; |
379 | } else if (*len > 128 * 1024) { | |
f96637be | 380 | cifs_dbg(VFS, "data area larger than 128K: %d\n", *len); |
093b2bda PS |
381 | *len = 0; |
382 | } | |
383 | ||
384 | /* return pointer to beginning of data area, ie offset from SMB start */ | |
385 | if ((*off != 0) && (*len != 0)) | |
31473fc4 | 386 | return (char *)shdr + *off; |
093b2bda PS |
387 | else |
388 | return NULL; | |
389 | } | |
390 | ||
391 | /* | |
392 | * Calculate the size of the SMB message based on the fixed header | |
393 | * portion, the number of word parameters and the data portion of the message. | |
394 | */ | |
395 | unsigned int | |
9ec672bd | 396 | smb2_calc_size(void *buf, struct TCP_Server_Info *srvr) |
093b2bda | 397 | { |
31473fc4 PS |
398 | struct smb2_pdu *pdu = (struct smb2_pdu *)buf; |
399 | struct smb2_hdr *hdr = &pdu->hdr; | |
400 | struct smb2_sync_hdr *shdr = get_sync_hdr(hdr); | |
093b2bda PS |
401 | int offset; /* the offset from the beginning of SMB to data area */ |
402 | int data_length; /* the length of the variable length data area */ | |
403 | /* Structure Size has already been checked to make sure it is 64 */ | |
9ec672bd | 404 | int len = srvr->vals->header_preamble_size + le16_to_cpu(shdr->StructureSize); |
093b2bda PS |
405 | |
406 | /* | |
407 | * StructureSize2, ie length of fixed parameter area has already | |
408 | * been checked to make sure it is the correct length. | |
409 | */ | |
410 | len += le16_to_cpu(pdu->StructureSize2); | |
411 | ||
31473fc4 | 412 | if (has_smb2_data_area[le16_to_cpu(shdr->Command)] == false) |
093b2bda PS |
413 | goto calc_size_exit; |
414 | ||
415 | smb2_get_data_area_len(&offset, &data_length, hdr); | |
f96637be | 416 | cifs_dbg(FYI, "SMB2 data length %d offset %d\n", data_length, offset); |
093b2bda PS |
417 | |
418 | if (data_length > 0) { | |
419 | /* | |
420 | * Check to make sure that data area begins after fixed area, | |
421 | * Note that last byte of the fixed area is part of data area | |
422 | * for some commands, typically those with odd StructureSize, | |
423 | * so we must add one to the calculation (and 4 to account for | |
424 | * the size of the RFC1001 hdr. | |
425 | */ | |
9ec672bd RS |
426 | if (offset + srvr->vals->header_preamble_size + 1 < len) { |
427 | cifs_dbg(VFS, "data area offset %zu overlaps SMB2 header %d\n", | |
428 | offset + srvr->vals->header_preamble_size + 1, len); | |
093b2bda PS |
429 | data_length = 0; |
430 | } else { | |
9ec672bd | 431 | len = srvr->vals->header_preamble_size + offset + data_length; |
093b2bda PS |
432 | } |
433 | } | |
434 | calc_size_exit: | |
f96637be | 435 | cifs_dbg(FYI, "SMB2 len %d\n", len); |
093b2bda PS |
436 | return len; |
437 | } | |
2503a0db PS |
438 | |
439 | /* Note: caller must free return buffer */ | |
440 | __le16 * | |
441 | cifs_convert_path_to_utf16(const char *from, struct cifs_sb_info *cifs_sb) | |
442 | { | |
443 | int len; | |
444 | const char *start_of_path; | |
445 | __le16 *to; | |
a4153cb1 SF |
446 | int map_type; |
447 | ||
448 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SFM_CHR) | |
449 | map_type = SFM_MAP_UNI_RSVD; | |
450 | else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR) | |
451 | map_type = SFU_MAP_UNI_RSVD; | |
452 | else | |
453 | map_type = NO_MAP_UNI_RSVD; | |
2503a0db PS |
454 | |
455 | /* Windows doesn't allow paths beginning with \ */ | |
456 | if (from[0] == '\\') | |
457 | start_of_path = from + 1; | |
458 | else | |
459 | start_of_path = from; | |
460 | to = cifs_strndup_to_utf16(start_of_path, PATH_MAX, &len, | |
a4153cb1 | 461 | cifs_sb->local_nls, map_type); |
2503a0db PS |
462 | return to; |
463 | } | |
983c88a4 | 464 | |
0822f514 PS |
465 | __le32 |
466 | smb2_get_lease_state(struct cifsInodeInfo *cinode) | |
467 | { | |
53ef1016 | 468 | __le32 lease = 0; |
0822f514 | 469 | |
53ef1016 PS |
470 | if (CIFS_CACHE_WRITE(cinode)) |
471 | lease |= SMB2_LEASE_WRITE_CACHING; | |
472 | if (CIFS_CACHE_HANDLE(cinode)) | |
473 | lease |= SMB2_LEASE_HANDLE_CACHING; | |
474 | if (CIFS_CACHE_READ(cinode)) | |
475 | lease |= SMB2_LEASE_READ_CACHING; | |
476 | return lease; | |
0822f514 PS |
477 | } |
478 | ||
233839b1 PS |
479 | struct smb2_lease_break_work { |
480 | struct work_struct lease_break; | |
481 | struct tcon_link *tlink; | |
482 | __u8 lease_key[16]; | |
483 | __le32 lease_state; | |
484 | }; | |
485 | ||
486 | static void | |
487 | cifs_ses_oplock_break(struct work_struct *work) | |
488 | { | |
489 | struct smb2_lease_break_work *lw = container_of(work, | |
490 | struct smb2_lease_break_work, lease_break); | |
491 | int rc; | |
492 | ||
493 | rc = SMB2_lease_break(0, tlink_tcon(lw->tlink), lw->lease_key, | |
494 | lw->lease_state); | |
f96637be | 495 | cifs_dbg(FYI, "Lease release rc %d\n", rc); |
233839b1 PS |
496 | cifs_put_tlink(lw->tlink); |
497 | kfree(lw); | |
498 | } | |
499 | ||
0822f514 | 500 | static bool |
933d4b36 PS |
501 | smb2_tcon_has_lease(struct cifs_tcon *tcon, struct smb2_lease_break *rsp, |
502 | struct smb2_lease_break_work *lw) | |
503 | { | |
504 | bool found; | |
505 | __u8 lease_state; | |
506 | struct list_head *tmp; | |
507 | struct cifsFileInfo *cfile; | |
42873b0a | 508 | struct TCP_Server_Info *server = tcon->ses->server; |
933d4b36 PS |
509 | struct cifs_pending_open *open; |
510 | struct cifsInodeInfo *cinode; | |
511 | int ack_req = le32_to_cpu(rsp->Flags & | |
512 | SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED); | |
513 | ||
53ef1016 | 514 | lease_state = le32_to_cpu(rsp->NewLeaseState); |
933d4b36 PS |
515 | |
516 | list_for_each(tmp, &tcon->openFileList) { | |
517 | cfile = list_entry(tmp, struct cifsFileInfo, tlist); | |
2b0143b5 | 518 | cinode = CIFS_I(d_inode(cfile->dentry)); |
933d4b36 PS |
519 | |
520 | if (memcmp(cinode->lease_key, rsp->LeaseKey, | |
521 | SMB2_LEASE_KEY_SIZE)) | |
522 | continue; | |
523 | ||
524 | cifs_dbg(FYI, "found in the open list\n"); | |
59b04c5d | 525 | cifs_dbg(FYI, "lease key match, lease break 0x%x\n", |
933d4b36 PS |
526 | le32_to_cpu(rsp->NewLeaseState)); |
527 | ||
42873b0a | 528 | server->ops->set_oplock_level(cinode, lease_state, 0, NULL); |
933d4b36 PS |
529 | |
530 | if (ack_req) | |
531 | cfile->oplock_break_cancelled = false; | |
532 | else | |
533 | cfile->oplock_break_cancelled = true; | |
534 | ||
3998e6b8 | 535 | queue_work(cifsoplockd_wq, &cfile->oplock_break); |
933d4b36 PS |
536 | kfree(lw); |
537 | return true; | |
538 | } | |
539 | ||
540 | found = false; | |
541 | list_for_each_entry(open, &tcon->pending_opens, olist) { | |
542 | if (memcmp(open->lease_key, rsp->LeaseKey, | |
543 | SMB2_LEASE_KEY_SIZE)) | |
544 | continue; | |
545 | ||
546 | if (!found && ack_req) { | |
547 | found = true; | |
548 | memcpy(lw->lease_key, open->lease_key, | |
549 | SMB2_LEASE_KEY_SIZE); | |
550 | lw->tlink = cifs_get_tlink(open->tlink); | |
551 | queue_work(cifsiod_wq, &lw->lease_break); | |
552 | } | |
553 | ||
554 | cifs_dbg(FYI, "found in the pending open list\n"); | |
59b04c5d | 555 | cifs_dbg(FYI, "lease key match, lease break 0x%x\n", |
933d4b36 PS |
556 | le32_to_cpu(rsp->NewLeaseState)); |
557 | ||
558 | open->oplock = lease_state; | |
559 | } | |
560 | return found; | |
561 | } | |
562 | ||
563 | static bool | |
564 | smb2_is_valid_lease_break(char *buffer) | |
0822f514 PS |
565 | { |
566 | struct smb2_lease_break *rsp = (struct smb2_lease_break *)buffer; | |
567 | struct list_head *tmp, *tmp1, *tmp2; | |
933d4b36 | 568 | struct TCP_Server_Info *server; |
0822f514 PS |
569 | struct cifs_ses *ses; |
570 | struct cifs_tcon *tcon; | |
233839b1 | 571 | struct smb2_lease_break_work *lw; |
233839b1 PS |
572 | |
573 | lw = kmalloc(sizeof(struct smb2_lease_break_work), GFP_KERNEL); | |
f96637be | 574 | if (!lw) |
233839b1 | 575 | return false; |
233839b1 PS |
576 | |
577 | INIT_WORK(&lw->lease_break, cifs_ses_oplock_break); | |
578 | lw->lease_state = rsp->NewLeaseState; | |
0822f514 | 579 | |
f96637be | 580 | cifs_dbg(FYI, "Checking for lease break\n"); |
0822f514 PS |
581 | |
582 | /* look up tcon based on tid & uid */ | |
583 | spin_lock(&cifs_tcp_ses_lock); | |
933d4b36 PS |
584 | list_for_each(tmp, &cifs_tcp_ses_list) { |
585 | server = list_entry(tmp, struct TCP_Server_Info, tcp_ses_list); | |
0822f514 | 586 | |
933d4b36 PS |
587 | list_for_each(tmp1, &server->smb_ses_list) { |
588 | ses = list_entry(tmp1, struct cifs_ses, smb_ses_list); | |
0822f514 | 589 | |
933d4b36 PS |
590 | list_for_each(tmp2, &ses->tcon_list) { |
591 | tcon = list_entry(tmp2, struct cifs_tcon, | |
592 | tcon_list); | |
3afca265 | 593 | spin_lock(&tcon->open_file_lock); |
933d4b36 PS |
594 | cifs_stats_inc( |
595 | &tcon->stats.cifs_stats.num_oplock_brks); | |
596 | if (smb2_tcon_has_lease(tcon, rsp, lw)) { | |
3afca265 | 597 | spin_unlock(&tcon->open_file_lock); |
933d4b36 PS |
598 | spin_unlock(&cifs_tcp_ses_lock); |
599 | return true; | |
233839b1 | 600 | } |
3afca265 | 601 | spin_unlock(&tcon->open_file_lock); |
233839b1 | 602 | } |
0822f514 PS |
603 | } |
604 | } | |
605 | spin_unlock(&cifs_tcp_ses_lock); | |
233839b1 | 606 | kfree(lw); |
f96637be | 607 | cifs_dbg(FYI, "Can not process lease break - no lease matched\n"); |
0822f514 PS |
608 | return false; |
609 | } | |
610 | ||
983c88a4 PS |
611 | bool |
612 | smb2_is_valid_oplock_break(char *buffer, struct TCP_Server_Info *server) | |
613 | { | |
21ad9487 | 614 | struct smb2_oplock_break_rsp *rsp = (struct smb2_oplock_break_rsp *)buffer; |
983c88a4 PS |
615 | struct list_head *tmp, *tmp1, *tmp2; |
616 | struct cifs_ses *ses; | |
617 | struct cifs_tcon *tcon; | |
618 | struct cifsInodeInfo *cinode; | |
619 | struct cifsFileInfo *cfile; | |
620 | ||
f96637be | 621 | cifs_dbg(FYI, "Checking for oplock break\n"); |
983c88a4 | 622 | |
31473fc4 | 623 | if (rsp->hdr.sync_hdr.Command != SMB2_OPLOCK_BREAK) |
983c88a4 PS |
624 | return false; |
625 | ||
12e8a208 | 626 | if (rsp->StructureSize != |
983c88a4 | 627 | smb2_rsp_struct_sizes[SMB2_OPLOCK_BREAK_HE]) { |
0822f514 | 628 | if (le16_to_cpu(rsp->StructureSize) == 44) |
933d4b36 | 629 | return smb2_is_valid_lease_break(buffer); |
0822f514 PS |
630 | else |
631 | return false; | |
983c88a4 PS |
632 | } |
633 | ||
59b04c5d | 634 | cifs_dbg(FYI, "oplock level 0x%x\n", rsp->OplockLevel); |
983c88a4 PS |
635 | |
636 | /* look up tcon based on tid & uid */ | |
637 | spin_lock(&cifs_tcp_ses_lock); | |
638 | list_for_each(tmp, &server->smb_ses_list) { | |
639 | ses = list_entry(tmp, struct cifs_ses, smb_ses_list); | |
640 | list_for_each(tmp1, &ses->tcon_list) { | |
641 | tcon = list_entry(tmp1, struct cifs_tcon, tcon_list); | |
642 | ||
643 | cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks); | |
3afca265 | 644 | spin_lock(&tcon->open_file_lock); |
983c88a4 PS |
645 | list_for_each(tmp2, &tcon->openFileList) { |
646 | cfile = list_entry(tmp2, struct cifsFileInfo, | |
647 | tlist); | |
648 | if (rsp->PersistentFid != | |
649 | cfile->fid.persistent_fid || | |
650 | rsp->VolatileFid != | |
651 | cfile->fid.volatile_fid) | |
652 | continue; | |
653 | ||
f96637be | 654 | cifs_dbg(FYI, "file id match, oplock break\n"); |
2b0143b5 | 655 | cinode = CIFS_I(d_inode(cfile->dentry)); |
3afca265 | 656 | spin_lock(&cfile->file_info_lock); |
18cceb6a | 657 | if (!CIFS_CACHE_WRITE(cinode) && |
983c88a4 PS |
658 | rsp->OplockLevel == SMB2_OPLOCK_LEVEL_NONE) |
659 | cfile->oplock_break_cancelled = true; | |
660 | else | |
661 | cfile->oplock_break_cancelled = false; | |
662 | ||
c11f1df5 SP |
663 | set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, |
664 | &cinode->flags); | |
665 | ||
666 | /* | |
667 | * Set flag if the server downgrades the oplock | |
668 | * to L2 else clear. | |
669 | */ | |
670 | if (rsp->OplockLevel) | |
671 | set_bit( | |
672 | CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2, | |
673 | &cinode->flags); | |
674 | else | |
675 | clear_bit( | |
676 | CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2, | |
677 | &cinode->flags); | |
3afca265 | 678 | spin_unlock(&cfile->file_info_lock); |
3998e6b8 RV |
679 | queue_work(cifsoplockd_wq, |
680 | &cfile->oplock_break); | |
983c88a4 | 681 | |
3afca265 | 682 | spin_unlock(&tcon->open_file_lock); |
983c88a4 PS |
683 | spin_unlock(&cifs_tcp_ses_lock); |
684 | return true; | |
685 | } | |
3afca265 | 686 | spin_unlock(&tcon->open_file_lock); |
983c88a4 | 687 | spin_unlock(&cifs_tcp_ses_lock); |
f96637be | 688 | cifs_dbg(FYI, "No matching file for oplock break\n"); |
983c88a4 PS |
689 | return true; |
690 | } | |
691 | } | |
692 | spin_unlock(&cifs_tcp_ses_lock); | |
f96637be | 693 | cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n"); |
983c88a4 PS |
694 | return false; |
695 | } | |
38bd4906 SP |
696 | |
697 | void | |
698 | smb2_cancelled_close_fid(struct work_struct *work) | |
699 | { | |
700 | struct close_cancelled_open *cancelled = container_of(work, | |
701 | struct close_cancelled_open, work); | |
702 | ||
703 | cifs_dbg(VFS, "Close unmatched open\n"); | |
704 | ||
705 | SMB2_close(0, cancelled->tcon, cancelled->fid.persistent_fid, | |
706 | cancelled->fid.volatile_fid); | |
707 | cifs_put_tcon(cancelled->tcon); | |
708 | kfree(cancelled); | |
709 | } | |
710 | ||
711 | int | |
712 | smb2_handle_cancelled_mid(char *buffer, struct TCP_Server_Info *server) | |
713 | { | |
714 | struct smb2_sync_hdr *sync_hdr = get_sync_hdr(buffer); | |
715 | struct smb2_create_rsp *rsp = (struct smb2_create_rsp *)buffer; | |
716 | struct cifs_tcon *tcon; | |
717 | struct close_cancelled_open *cancelled; | |
718 | ||
719 | if (sync_hdr->Command != SMB2_CREATE || | |
720 | sync_hdr->Status != STATUS_SUCCESS) | |
721 | return 0; | |
722 | ||
723 | cancelled = kzalloc(sizeof(*cancelled), GFP_KERNEL); | |
724 | if (!cancelled) | |
725 | return -ENOMEM; | |
726 | ||
727 | tcon = smb2_find_smb_tcon(server, sync_hdr->SessionId, | |
728 | sync_hdr->TreeId); | |
729 | if (!tcon) { | |
730 | kfree(cancelled); | |
731 | return -ENOENT; | |
732 | } | |
733 | ||
734 | cancelled->fid.persistent_fid = rsp->PersistentFileId; | |
735 | cancelled->fid.volatile_fid = rsp->VolatileFileId; | |
736 | cancelled->tcon = tcon; | |
737 | INIT_WORK(&cancelled->work, smb2_cancelled_close_fid); | |
738 | queue_work(cifsiod_wq, &cancelled->work); | |
739 | ||
740 | return 0; | |
741 | } | |
8bd68c6e AA |
742 | |
743 | #ifdef CONFIG_CIFS_SMB311 | |
744 | /** | |
745 | * smb311_update_preauth_hash - update @ses hash with the packet data in @iov | |
746 | * | |
747 | * Assumes @iov does not contain the rfc1002 length and iov[0] has the | |
748 | * SMB2 header. | |
749 | */ | |
750 | int | |
751 | smb311_update_preauth_hash(struct cifs_ses *ses, struct kvec *iov, int nvec) | |
752 | { | |
753 | int i, rc; | |
754 | struct sdesc *d; | |
755 | struct smb2_sync_hdr *hdr; | |
756 | ||
757 | if (ses->server->tcpStatus == CifsGood) { | |
758 | /* skip non smb311 connections */ | |
759 | if (ses->server->dialect != SMB311_PROT_ID) | |
760 | return 0; | |
761 | ||
762 | /* skip last sess setup response */ | |
763 | hdr = (struct smb2_sync_hdr *)iov[0].iov_base; | |
764 | if (hdr->Flags & SMB2_FLAGS_SIGNED) | |
765 | return 0; | |
766 | } | |
767 | ||
768 | rc = smb311_crypto_shash_allocate(ses->server); | |
769 | if (rc) | |
770 | return rc; | |
771 | ||
772 | d = ses->server->secmech.sdescsha512; | |
773 | rc = crypto_shash_init(&d->shash); | |
774 | if (rc) { | |
775 | cifs_dbg(VFS, "%s: could not init sha512 shash\n", __func__); | |
776 | return rc; | |
777 | } | |
778 | ||
779 | rc = crypto_shash_update(&d->shash, ses->preauth_sha_hash, | |
780 | SMB2_PREAUTH_HASH_SIZE); | |
781 | if (rc) { | |
782 | cifs_dbg(VFS, "%s: could not update sha512 shash\n", __func__); | |
783 | return rc; | |
784 | } | |
785 | ||
786 | for (i = 0; i < nvec; i++) { | |
787 | rc = crypto_shash_update(&d->shash, | |
788 | iov[i].iov_base, iov[i].iov_len); | |
789 | if (rc) { | |
790 | cifs_dbg(VFS, "%s: could not update sha512 shash\n", | |
791 | __func__); | |
792 | return rc; | |
793 | } | |
794 | } | |
795 | ||
796 | rc = crypto_shash_final(&d->shash, ses->preauth_sha_hash); | |
797 | if (rc) { | |
798 | cifs_dbg(VFS, "%s: could not finalize sha512 shash\n", | |
799 | __func__); | |
800 | return rc; | |
801 | } | |
802 | ||
803 | return 0; | |
804 | } | |
805 | #endif |