Commit | Line | Data |
---|---|---|
ec2e4523 PS |
1 | /* |
2 | * fs/cifs/smb2pdu.c | |
3 | * | |
2b80d049 | 4 | * Copyright (C) International Business Machines Corp., 2009, 2013 |
ec2e4523 PS |
5 | * Etersoft, 2012 |
6 | * Author(s): Steve French (sfrench@us.ibm.com) | |
7 | * Pavel Shilovsky (pshilovsky@samba.org) 2012 | |
8 | * | |
9 | * Contains the routines for constructing the SMB2 PDUs themselves | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU Lesser General Public License as published | |
13 | * by the Free Software Foundation; either version 2.1 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
19 | * the GNU Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public License | |
22 | * along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 | */ | |
25 | ||
26 | /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */ | |
27 | /* Note that there are handle based routines which must be */ | |
28 | /* treated slightly differently for reconnection purposes since we never */ | |
29 | /* want to reuse a stale file handle and only the caller knows the file info */ | |
30 | ||
31 | #include <linux/fs.h> | |
32 | #include <linux/kernel.h> | |
33 | #include <linux/vfs.h> | |
09a4707e | 34 | #include <linux/task_io_accounting_ops.h> |
ec2e4523 | 35 | #include <linux/uaccess.h> |
33319141 | 36 | #include <linux/pagemap.h> |
ec2e4523 PS |
37 | #include <linux/xattr.h> |
38 | #include "smb2pdu.h" | |
39 | #include "cifsglob.h" | |
40 | #include "cifsacl.h" | |
41 | #include "cifsproto.h" | |
42 | #include "smb2proto.h" | |
43 | #include "cifs_unicode.h" | |
44 | #include "cifs_debug.h" | |
45 | #include "ntlmssp.h" | |
46 | #include "smb2status.h" | |
09a4707e | 47 | #include "smb2glob.h" |
d324f08d | 48 | #include "cifspdu.h" |
ceb1b0b9 | 49 | #include "cifs_spnego.h" |
ec2e4523 PS |
50 | |
51 | /* | |
52 | * The following table defines the expected "StructureSize" of SMB2 requests | |
53 | * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests. | |
54 | * | |
55 | * Note that commands are defined in smb2pdu.h in le16 but the array below is | |
56 | * indexed by command in host byte order. | |
57 | */ | |
58 | static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = { | |
59 | /* SMB2_NEGOTIATE */ 36, | |
60 | /* SMB2_SESSION_SETUP */ 25, | |
61 | /* SMB2_LOGOFF */ 4, | |
62 | /* SMB2_TREE_CONNECT */ 9, | |
63 | /* SMB2_TREE_DISCONNECT */ 4, | |
64 | /* SMB2_CREATE */ 57, | |
65 | /* SMB2_CLOSE */ 24, | |
66 | /* SMB2_FLUSH */ 24, | |
67 | /* SMB2_READ */ 49, | |
68 | /* SMB2_WRITE */ 49, | |
69 | /* SMB2_LOCK */ 48, | |
70 | /* SMB2_IOCTL */ 57, | |
71 | /* SMB2_CANCEL */ 4, | |
72 | /* SMB2_ECHO */ 4, | |
73 | /* SMB2_QUERY_DIRECTORY */ 33, | |
74 | /* SMB2_CHANGE_NOTIFY */ 32, | |
75 | /* SMB2_QUERY_INFO */ 41, | |
76 | /* SMB2_SET_INFO */ 33, | |
77 | /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */ | |
78 | }; | |
79 | ||
7fb8986e PS |
80 | static int encryption_required(const struct cifs_tcon *tcon) |
81 | { | |
ae6f8dd4 PS |
82 | if (!tcon) |
83 | return 0; | |
7fb8986e PS |
84 | if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) || |
85 | (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA)) | |
86 | return 1; | |
ae6f8dd4 PS |
87 | if (tcon->seal && |
88 | (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) | |
89 | return 1; | |
7fb8986e PS |
90 | return 0; |
91 | } | |
ec2e4523 PS |
92 | |
93 | static void | |
cb200bd6 | 94 | smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd, |
ec2e4523 PS |
95 | const struct cifs_tcon *tcon) |
96 | { | |
31473fc4 PS |
97 | shdr->ProtocolId = SMB2_PROTO_NUMBER; |
98 | shdr->StructureSize = cpu_to_le16(64); | |
99 | shdr->Command = smb2_cmd; | |
7d414f39 RL |
100 | if (tcon && tcon->ses && tcon->ses->server) { |
101 | struct TCP_Server_Info *server = tcon->ses->server; | |
102 | ||
103 | spin_lock(&server->req_lock); | |
104 | /* Request up to 2 credits but don't go over the limit. */ | |
141891f4 | 105 | if (server->credits >= server->max_credits) |
31473fc4 | 106 | shdr->CreditRequest = cpu_to_le16(0); |
7d414f39 | 107 | else |
31473fc4 | 108 | shdr->CreditRequest = cpu_to_le16( |
141891f4 | 109 | min_t(int, server->max_credits - |
7d414f39 RL |
110 | server->credits, 2)); |
111 | spin_unlock(&server->req_lock); | |
112 | } else { | |
31473fc4 | 113 | shdr->CreditRequest = cpu_to_le16(2); |
7d414f39 | 114 | } |
31473fc4 | 115 | shdr->ProcessId = cpu_to_le32((__u16)current->tgid); |
ec2e4523 PS |
116 | |
117 | if (!tcon) | |
118 | goto out; | |
119 | ||
2b80d049 SF |
120 | /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */ |
121 | /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */ | |
1dc92c45 | 122 | if ((tcon->ses) && (tcon->ses->server) && |
84ceeb96 | 123 | (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU)) |
31473fc4 | 124 | shdr->CreditCharge = cpu_to_le16(1); |
2b80d049 SF |
125 | /* else CreditCharge MBZ */ |
126 | ||
31473fc4 | 127 | shdr->TreeId = tcon->tid; |
ec2e4523 PS |
128 | /* Uid is not converted */ |
129 | if (tcon->ses) | |
31473fc4 | 130 | shdr->SessionId = tcon->ses->Suid; |
f87ab88b SF |
131 | |
132 | /* | |
133 | * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have | |
134 | * to pass the path on the Open SMB prefixed by \\server\share. | |
135 | * Not sure when we would need to do the augmented path (if ever) and | |
136 | * setting this flag breaks the SMB2 open operation since it is | |
137 | * illegal to send an empty path name (without \\server\share prefix) | |
138 | * when the DFS flag is set in the SMB open header. We could | |
139 | * consider setting the flag on all operations other than open | |
140 | * but it is safer to net set it for now. | |
141 | */ | |
142 | /* if (tcon->share_flags & SHI1005_FLAGS_DFS) | |
31473fc4 | 143 | shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */ |
f87ab88b | 144 | |
7fb8986e PS |
145 | if (tcon->ses && tcon->ses->server && tcon->ses->server->sign && |
146 | !encryption_required(tcon)) | |
31473fc4 | 147 | shdr->Flags |= SMB2_FLAGS_SIGNED; |
ec2e4523 | 148 | out: |
ec2e4523 PS |
149 | return; |
150 | } | |
151 | ||
152 | static int | |
153 | smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon) | |
154 | { | |
155 | int rc = 0; | |
aa24d1e9 PS |
156 | struct nls_table *nls_codepage; |
157 | struct cifs_ses *ses; | |
158 | struct TCP_Server_Info *server; | |
159 | ||
160 | /* | |
161 | * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so | |
162 | * check for tcp and smb session status done differently | |
163 | * for those three - in the calling routine. | |
164 | */ | |
165 | if (tcon == NULL) | |
166 | return rc; | |
167 | ||
168 | if (smb2_command == SMB2_TREE_CONNECT) | |
169 | return rc; | |
170 | ||
171 | if (tcon->tidStatus == CifsExiting) { | |
172 | /* | |
173 | * only tree disconnect, open, and write, | |
174 | * (and ulogoff which does not have tcon) | |
175 | * are allowed as we start force umount. | |
176 | */ | |
177 | if ((smb2_command != SMB2_WRITE) && | |
178 | (smb2_command != SMB2_CREATE) && | |
179 | (smb2_command != SMB2_TREE_DISCONNECT)) { | |
f96637be JP |
180 | cifs_dbg(FYI, "can not send cmd %d while umounting\n", |
181 | smb2_command); | |
aa24d1e9 PS |
182 | return -ENODEV; |
183 | } | |
184 | } | |
185 | if ((!tcon->ses) || (tcon->ses->status == CifsExiting) || | |
186 | (!tcon->ses->server)) | |
187 | return -EIO; | |
188 | ||
189 | ses = tcon->ses; | |
190 | server = ses->server; | |
191 | ||
192 | /* | |
193 | * Give demultiplex thread up to 10 seconds to reconnect, should be | |
194 | * greater than cifs socket timeout which is 7 seconds | |
195 | */ | |
196 | while (server->tcpStatus == CifsNeedReconnect) { | |
197 | /* | |
198 | * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE | |
199 | * here since they are implicitly done when session drops. | |
200 | */ | |
201 | switch (smb2_command) { | |
202 | /* | |
203 | * BB Should we keep oplock break and add flush to exceptions? | |
204 | */ | |
205 | case SMB2_TREE_DISCONNECT: | |
206 | case SMB2_CANCEL: | |
207 | case SMB2_CLOSE: | |
208 | case SMB2_OPLOCK_BREAK: | |
209 | return -EAGAIN; | |
210 | } | |
211 | ||
212 | wait_event_interruptible_timeout(server->response_q, | |
213 | (server->tcpStatus != CifsNeedReconnect), 10 * HZ); | |
214 | ||
215 | /* are we still trying to reconnect? */ | |
216 | if (server->tcpStatus != CifsNeedReconnect) | |
217 | break; | |
218 | ||
219 | /* | |
220 | * on "soft" mounts we wait once. Hard mounts keep | |
221 | * retrying until process is killed or server comes | |
222 | * back on-line | |
223 | */ | |
224 | if (!tcon->retry) { | |
f96637be | 225 | cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n"); |
aa24d1e9 PS |
226 | return -EHOSTDOWN; |
227 | } | |
228 | } | |
229 | ||
230 | if (!tcon->ses->need_reconnect && !tcon->need_reconnect) | |
231 | return rc; | |
232 | ||
233 | nls_codepage = load_nls_default(); | |
234 | ||
235 | /* | |
236 | * need to prevent multiple threads trying to simultaneously reconnect | |
237 | * the same SMB session | |
238 | */ | |
239 | mutex_lock(&tcon->ses->session_mutex); | |
240 | rc = cifs_negotiate_protocol(0, tcon->ses); | |
241 | if (!rc && tcon->ses->need_reconnect) | |
242 | rc = cifs_setup_session(0, tcon->ses, nls_codepage); | |
243 | ||
244 | if (rc || !tcon->need_reconnect) { | |
245 | mutex_unlock(&tcon->ses->session_mutex); | |
246 | goto out; | |
247 | } | |
248 | ||
249 | cifs_mark_open_files_invalid(tcon); | |
96a988ff PS |
250 | if (tcon->use_persistent) |
251 | tcon->need_reopen_files = true; | |
52ace1ef | 252 | |
aa24d1e9 PS |
253 | rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage); |
254 | mutex_unlock(&tcon->ses->session_mutex); | |
52ace1ef | 255 | |
f96637be | 256 | cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc); |
aa24d1e9 PS |
257 | if (rc) |
258 | goto out; | |
96a988ff PS |
259 | |
260 | if (smb2_command != SMB2_INTERNAL_CMD) | |
261 | queue_delayed_work(cifsiod_wq, &server->reconnect, 0); | |
262 | ||
aa24d1e9 | 263 | atomic_inc(&tconInfoReconnectCount); |
aa24d1e9 PS |
264 | out: |
265 | /* | |
266 | * Check if handle based operation so we know whether we can continue | |
267 | * or not without returning to caller to reset file handle. | |
268 | */ | |
269 | /* | |
270 | * BB Is flush done by server on drop of tcp session? Should we special | |
271 | * case it and skip above? | |
272 | */ | |
273 | switch (smb2_command) { | |
274 | case SMB2_FLUSH: | |
275 | case SMB2_READ: | |
276 | case SMB2_WRITE: | |
277 | case SMB2_LOCK: | |
278 | case SMB2_IOCTL: | |
279 | case SMB2_QUERY_DIRECTORY: | |
280 | case SMB2_CHANGE_NOTIFY: | |
281 | case SMB2_QUERY_INFO: | |
282 | case SMB2_SET_INFO: | |
4772c795 | 283 | rc = -EAGAIN; |
aa24d1e9 PS |
284 | } |
285 | unload_nls(nls_codepage); | |
ec2e4523 PS |
286 | return rc; |
287 | } | |
288 | ||
cb200bd6 PS |
289 | static void |
290 | fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf, | |
291 | unsigned int *total_len) | |
292 | { | |
293 | struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf; | |
294 | /* lookup word count ie StructureSize from table */ | |
295 | __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)]; | |
296 | ||
297 | /* | |
298 | * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of | |
299 | * largest operations (Create) | |
300 | */ | |
301 | memset(buf, 0, 256); | |
302 | ||
303 | smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon); | |
304 | spdu->StructureSize2 = cpu_to_le16(parmsize); | |
305 | ||
306 | *total_len = parmsize + sizeof(struct smb2_sync_hdr); | |
307 | } | |
308 | ||
b8f57ee8 PS |
309 | /* init request without RFC1001 length at the beginning */ |
310 | static int | |
311 | smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon, | |
312 | void **request_buf, unsigned int *total_len) | |
313 | { | |
314 | int rc; | |
315 | struct smb2_sync_hdr *shdr; | |
316 | ||
317 | rc = smb2_reconnect(smb2_command, tcon); | |
318 | if (rc) | |
319 | return rc; | |
320 | ||
321 | /* BB eventually switch this to SMB2 specific small buf size */ | |
322 | *request_buf = cifs_small_buf_get(); | |
323 | if (*request_buf == NULL) { | |
324 | /* BB should we add a retry in here if not a writepage? */ | |
325 | return -ENOMEM; | |
326 | } | |
327 | ||
328 | shdr = (struct smb2_sync_hdr *)(*request_buf); | |
329 | ||
330 | fill_small_buf(smb2_command, tcon, shdr, total_len); | |
331 | ||
332 | if (tcon != NULL) { | |
333 | #ifdef CONFIG_CIFS_STATS2 | |
334 | uint16_t com_code = le16_to_cpu(smb2_command); | |
335 | ||
336 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]); | |
337 | #endif | |
338 | cifs_stats_inc(&tcon->num_smbs_sent); | |
339 | } | |
340 | ||
341 | return rc; | |
342 | } | |
343 | ||
ec2e4523 PS |
344 | /* |
345 | * Allocate and return pointer to an SMB request hdr, and set basic | |
346 | * SMB information in the SMB header. If the return code is zero, this | |
b8f57ee8 PS |
347 | * function must have filled in request_buf pointer. The returned buffer |
348 | * has RFC1001 length at the beginning. | |
ec2e4523 PS |
349 | */ |
350 | static int | |
351 | small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon, | |
352 | void **request_buf) | |
353 | { | |
cb200bd6 PS |
354 | int rc; |
355 | unsigned int total_len; | |
356 | struct smb2_pdu *pdu; | |
ec2e4523 PS |
357 | |
358 | rc = smb2_reconnect(smb2_command, tcon); | |
359 | if (rc) | |
360 | return rc; | |
361 | ||
362 | /* BB eventually switch this to SMB2 specific small buf size */ | |
363 | *request_buf = cifs_small_buf_get(); | |
364 | if (*request_buf == NULL) { | |
365 | /* BB should we add a retry in here if not a writepage? */ | |
366 | return -ENOMEM; | |
367 | } | |
368 | ||
cb200bd6 PS |
369 | pdu = (struct smb2_pdu *)(*request_buf); |
370 | ||
371 | fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len); | |
372 | ||
373 | /* Note this is only network field converted to big endian */ | |
374 | pdu->hdr.smb2_buf_length = cpu_to_be32(total_len); | |
ec2e4523 PS |
375 | |
376 | if (tcon != NULL) { | |
377 | #ifdef CONFIG_CIFS_STATS2 | |
ec2e4523 PS |
378 | uint16_t com_code = le16_to_cpu(smb2_command); |
379 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]); | |
ec2e4523 PS |
380 | #endif |
381 | cifs_stats_inc(&tcon->num_smbs_sent); | |
382 | } | |
383 | ||
384 | return rc; | |
385 | } | |
386 | ||
ebb3a9d4 SF |
387 | #ifdef CONFIG_CIFS_SMB311 |
388 | /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */ | |
389 | #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */ | |
390 | ||
391 | ||
392 | #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1) | |
393 | #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2) | |
394 | ||
395 | static void | |
396 | build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt) | |
397 | { | |
398 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; | |
399 | pneg_ctxt->DataLength = cpu_to_le16(38); | |
400 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); | |
401 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); | |
402 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); | |
403 | pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512; | |
404 | } | |
405 | ||
406 | static void | |
407 | build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt) | |
408 | { | |
409 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; | |
410 | pneg_ctxt->DataLength = cpu_to_le16(6); | |
411 | pneg_ctxt->CipherCount = cpu_to_le16(2); | |
412 | pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM; | |
413 | pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM; | |
414 | } | |
415 | ||
416 | static void | |
417 | assemble_neg_contexts(struct smb2_negotiate_req *req) | |
418 | { | |
419 | ||
420 | /* +4 is to account for the RFC1001 len field */ | |
421 | char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4; | |
422 | ||
423 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt); | |
424 | /* Add 2 to size to round to 8 byte boundary */ | |
425 | pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context); | |
426 | build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt); | |
427 | req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT); | |
428 | req->NegotiateContextCount = cpu_to_le16(2); | |
429 | inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context) + 2 | |
430 | + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */ | |
431 | } | |
432 | #else | |
433 | static void assemble_neg_contexts(struct smb2_negotiate_req *req) | |
434 | { | |
435 | return; | |
436 | } | |
437 | #endif /* SMB311 */ | |
438 | ||
ec2e4523 PS |
439 | /* |
440 | * | |
441 | * SMB2 Worker functions follow: | |
442 | * | |
443 | * The general structure of the worker functions is: | |
444 | * 1) Call smb2_init (assembles SMB2 header) | |
445 | * 2) Initialize SMB2 command specific fields in fixed length area of SMB | |
446 | * 3) Call smb_sendrcv2 (sends request on socket and waits for response) | |
447 | * 4) Decode SMB2 command specific fields in the fixed length area | |
448 | * 5) Decode variable length data area (if any for this SMB2 command type) | |
449 | * 6) Call free smb buffer | |
450 | * 7) return | |
451 | * | |
452 | */ | |
453 | ||
454 | int | |
455 | SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses) | |
456 | { | |
457 | struct smb2_negotiate_req *req; | |
458 | struct smb2_negotiate_rsp *rsp; | |
459 | struct kvec iov[1]; | |
da502f7d | 460 | struct kvec rsp_iov; |
ec2e4523 PS |
461 | int rc = 0; |
462 | int resp_buftype; | |
3534b850 | 463 | struct TCP_Server_Info *server = ses->server; |
ec2e4523 PS |
464 | int blob_offset, blob_length; |
465 | char *security_blob; | |
466 | int flags = CIFS_NEG_OP; | |
467 | ||
f96637be | 468 | cifs_dbg(FYI, "Negotiate protocol\n"); |
ec2e4523 | 469 | |
3534b850 JL |
470 | if (!server) { |
471 | WARN(1, "%s: server is NULL!\n", __func__); | |
472 | return -EIO; | |
ec2e4523 PS |
473 | } |
474 | ||
475 | rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req); | |
476 | if (rc) | |
477 | return rc; | |
478 | ||
31473fc4 | 479 | req->hdr.sync_hdr.SessionId = 0; |
ec2e4523 | 480 | |
e4aa25e7 | 481 | req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id); |
ec2e4523 | 482 | |
e4aa25e7 SF |
483 | req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */ |
484 | inc_rfc1001_len(req, 2); | |
ec2e4523 PS |
485 | |
486 | /* only one of SMB2 signing flags may be set in SMB2 request */ | |
38d77c50 | 487 | if (ses->sign) |
9cd2e62c | 488 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); |
38d77c50 | 489 | else if (global_secflags & CIFSSEC_MAY_SIGN) |
9cd2e62c | 490 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); |
38d77c50 JL |
491 | else |
492 | req->SecurityMode = 0; | |
ec2e4523 | 493 | |
e4aa25e7 | 494 | req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities); |
ec2e4523 | 495 | |
3c5f9be1 SF |
496 | /* ClientGUID must be zero for SMB2.02 dialect */ |
497 | if (ses->server->vals->protocol_id == SMB20_PROT_ID) | |
498 | memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE); | |
ebb3a9d4 | 499 | else { |
3c5f9be1 SF |
500 | memcpy(req->ClientGUID, server->client_guid, |
501 | SMB2_CLIENT_GUID_SIZE); | |
ebb3a9d4 SF |
502 | if (ses->server->vals->protocol_id == SMB311_PROT_ID) |
503 | assemble_neg_contexts(req); | |
504 | } | |
ec2e4523 PS |
505 | iov[0].iov_base = (char *)req; |
506 | /* 4 for rfc1002 length field */ | |
507 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
508 | ||
da502f7d PS |
509 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov); |
510 | cifs_small_buf_release(req); | |
511 | rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base; | |
ec2e4523 PS |
512 | /* |
513 | * No tcon so can't do | |
514 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); | |
515 | */ | |
516 | if (rc != 0) | |
517 | goto neg_exit; | |
518 | ||
f96637be | 519 | cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode); |
ec2e4523 | 520 | |
e4aa25e7 SF |
521 | /* BB we may eventually want to match the negotiated vs. requested |
522 | dialect, even though we are only requesting one at a time */ | |
523 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) | |
f96637be | 524 | cifs_dbg(FYI, "negotiated smb2.0 dialect\n"); |
e4aa25e7 | 525 | else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) |
f96637be | 526 | cifs_dbg(FYI, "negotiated smb2.1 dialect\n"); |
e4aa25e7 | 527 | else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID)) |
f96637be | 528 | cifs_dbg(FYI, "negotiated smb3.0 dialect\n"); |
20b6d8b4 SF |
529 | else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID)) |
530 | cifs_dbg(FYI, "negotiated smb3.02 dialect\n"); | |
5f7fbf73 SF |
531 | #ifdef CONFIG_CIFS_SMB311 |
532 | else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) | |
533 | cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n"); | |
534 | #endif /* SMB311 */ | |
ec2e4523 | 535 | else { |
f799d623 | 536 | cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n", |
f96637be | 537 | le16_to_cpu(rsp->DialectRevision)); |
ec2e4523 PS |
538 | rc = -EIO; |
539 | goto neg_exit; | |
540 | } | |
541 | server->dialect = le16_to_cpu(rsp->DialectRevision); | |
542 | ||
e598d1d8 JL |
543 | /* SMB2 only has an extended negflavor */ |
544 | server->negflavor = CIFS_NEGFLAVOR_EXTENDED; | |
2365c4ea PS |
545 | /* set it to the maximum buffer size value we can send with 1 credit */ |
546 | server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize), | |
547 | SMB2_MAX_BUFFER_SIZE); | |
ec2e4523 PS |
548 | server->max_read = le32_to_cpu(rsp->MaxReadSize); |
549 | server->max_write = le32_to_cpu(rsp->MaxWriteSize); | |
550 | /* BB Do we need to validate the SecurityMode? */ | |
551 | server->sec_mode = le16_to_cpu(rsp->SecurityMode); | |
552 | server->capabilities = le32_to_cpu(rsp->Capabilities); | |
29e20f9c PS |
553 | /* Internal types */ |
554 | server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES; | |
ec2e4523 PS |
555 | |
556 | security_blob = smb2_get_data_area_len(&blob_offset, &blob_length, | |
557 | &rsp->hdr); | |
5d875cc9 SF |
558 | /* |
559 | * See MS-SMB2 section 2.2.4: if no blob, client picks default which | |
560 | * for us will be | |
561 | * ses->sectype = RawNTLMSSP; | |
562 | * but for time being this is our only auth choice so doesn't matter. | |
563 | * We just found a server which sets blob length to zero expecting raw. | |
564 | */ | |
565 | if (blob_length == 0) | |
566 | cifs_dbg(FYI, "missing security blob on negprot\n"); | |
3c1bf7e4 | 567 | |
38d77c50 | 568 | rc = cifs_enable_signing(server, ses->sign); |
9ddec561 JL |
569 | if (rc) |
570 | goto neg_exit; | |
ceb1b0b9 | 571 | if (blob_length) { |
ebdd207e | 572 | rc = decode_negTokenInit(security_blob, blob_length, server); |
ceb1b0b9 SF |
573 | if (rc == 1) |
574 | rc = 0; | |
575 | else if (rc == 0) | |
576 | rc = -EIO; | |
ec2e4523 | 577 | } |
ec2e4523 PS |
578 | neg_exit: |
579 | free_rsp_buf(resp_buftype, rsp); | |
580 | return rc; | |
581 | } | |
5478f9ba | 582 | |
ff1c038a SF |
583 | int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon) |
584 | { | |
585 | int rc = 0; | |
586 | struct validate_negotiate_info_req vneg_inbuf; | |
587 | struct validate_negotiate_info_rsp *pneg_rsp; | |
588 | u32 rsplen; | |
589 | ||
590 | cifs_dbg(FYI, "validate negotiate\n"); | |
591 | ||
592 | /* | |
593 | * validation ioctl must be signed, so no point sending this if we | |
594 | * can not sign it. We could eventually change this to selectively | |
595 | * sign just this, the first and only signed request on a connection. | |
596 | * This is good enough for now since a user who wants better security | |
597 | * would also enable signing on the mount. Having validation of | |
598 | * negotiate info for signed connections helps reduce attack vectors | |
599 | */ | |
600 | if (tcon->ses->server->sign == false) | |
601 | return 0; /* validation requires signing */ | |
602 | ||
603 | vneg_inbuf.Capabilities = | |
604 | cpu_to_le32(tcon->ses->server->vals->req_capabilities); | |
39552ea8 SP |
605 | memcpy(vneg_inbuf.Guid, tcon->ses->server->client_guid, |
606 | SMB2_CLIENT_GUID_SIZE); | |
ff1c038a SF |
607 | |
608 | if (tcon->ses->sign) | |
609 | vneg_inbuf.SecurityMode = | |
610 | cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); | |
611 | else if (global_secflags & CIFSSEC_MAY_SIGN) | |
612 | vneg_inbuf.SecurityMode = | |
613 | cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); | |
614 | else | |
615 | vneg_inbuf.SecurityMode = 0; | |
616 | ||
617 | vneg_inbuf.DialectCount = cpu_to_le16(1); | |
618 | vneg_inbuf.Dialects[0] = | |
619 | cpu_to_le16(tcon->ses->server->vals->protocol_id); | |
620 | ||
621 | rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID, | |
622 | FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */, | |
51146625 | 623 | false /* use_ipc */, |
ff1c038a SF |
624 | (char *)&vneg_inbuf, sizeof(struct validate_negotiate_info_req), |
625 | (char **)&pneg_rsp, &rsplen); | |
626 | ||
627 | if (rc != 0) { | |
628 | cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc); | |
629 | return -EIO; | |
630 | } | |
631 | ||
632 | if (rsplen != sizeof(struct validate_negotiate_info_rsp)) { | |
633 | cifs_dbg(VFS, "invalid size of protocol negotiate response\n"); | |
634 | return -EIO; | |
635 | } | |
636 | ||
637 | /* check validate negotiate info response matches what we got earlier */ | |
638 | if (pneg_rsp->Dialect != | |
639 | cpu_to_le16(tcon->ses->server->vals->protocol_id)) | |
640 | goto vneg_out; | |
641 | ||
642 | if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode)) | |
643 | goto vneg_out; | |
644 | ||
645 | /* do not validate server guid because not saved at negprot time yet */ | |
646 | ||
647 | if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND | | |
648 | SMB2_LARGE_FILES) != tcon->ses->server->capabilities) | |
649 | goto vneg_out; | |
650 | ||
651 | /* validate negotiate successful */ | |
652 | cifs_dbg(FYI, "validate negotiate info successful\n"); | |
653 | return 0; | |
654 | ||
655 | vneg_out: | |
656 | cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n"); | |
657 | return -EIO; | |
658 | } | |
659 | ||
3baf1a7b SP |
660 | struct SMB2_sess_data { |
661 | unsigned int xid; | |
662 | struct cifs_ses *ses; | |
663 | struct nls_table *nls_cp; | |
664 | void (*func)(struct SMB2_sess_data *); | |
665 | int result; | |
666 | u64 previous_session; | |
667 | ||
668 | /* we will send the SMB in three pieces: | |
669 | * a fixed length beginning part, an optional | |
670 | * SPNEGO blob (which can be zero length), and a | |
671 | * last part which will include the strings | |
672 | * and rest of bcc area. This allows us to avoid | |
673 | * a large buffer 17K allocation | |
674 | */ | |
675 | int buf0_type; | |
676 | struct kvec iov[2]; | |
677 | }; | |
678 | ||
679 | static int | |
680 | SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data) | |
681 | { | |
682 | int rc; | |
683 | struct cifs_ses *ses = sess_data->ses; | |
684 | struct smb2_sess_setup_req *req; | |
685 | struct TCP_Server_Info *server = ses->server; | |
686 | ||
687 | rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req); | |
688 | if (rc) | |
689 | return rc; | |
690 | ||
31473fc4 PS |
691 | /* First session, not a reauthenticate */ |
692 | req->hdr.sync_hdr.SessionId = 0; | |
3baf1a7b SP |
693 | |
694 | /* if reconnect, we need to send previous sess id, otherwise it is 0 */ | |
695 | req->PreviousSessionId = sess_data->previous_session; | |
696 | ||
697 | req->Flags = 0; /* MBZ */ | |
698 | /* to enable echos and oplocks */ | |
31473fc4 | 699 | req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3); |
3baf1a7b SP |
700 | |
701 | /* only one of SMB2 signing flags may be set in SMB2 request */ | |
702 | if (server->sign) | |
703 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED; | |
704 | else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */ | |
705 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED; | |
706 | else | |
707 | req->SecurityMode = 0; | |
708 | ||
709 | req->Capabilities = 0; | |
710 | req->Channel = 0; /* MBZ */ | |
711 | ||
712 | sess_data->iov[0].iov_base = (char *)req; | |
713 | /* 4 for rfc1002 length field and 1 for pad */ | |
714 | sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
715 | /* | |
716 | * This variable will be used to clear the buffer | |
717 | * allocated above in case of any error in the calling function. | |
718 | */ | |
719 | sess_data->buf0_type = CIFS_SMALL_BUFFER; | |
720 | ||
721 | return 0; | |
722 | } | |
723 | ||
724 | static void | |
725 | SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data) | |
726 | { | |
727 | free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base); | |
728 | sess_data->buf0_type = CIFS_NO_BUFFER; | |
729 | } | |
730 | ||
731 | static int | |
732 | SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data) | |
733 | { | |
734 | int rc; | |
735 | struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base; | |
da502f7d | 736 | struct kvec rsp_iov = { NULL, 0 }; |
3baf1a7b SP |
737 | |
738 | /* Testing shows that buffer offset must be at location of Buffer[0] */ | |
739 | req->SecurityBufferOffset = | |
740 | cpu_to_le16(sizeof(struct smb2_sess_setup_req) - | |
741 | 1 /* pad */ - 4 /* rfc1001 len */); | |
742 | req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len); | |
743 | ||
744 | inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */); | |
745 | ||
746 | /* BB add code to build os and lm fields */ | |
747 | ||
748 | rc = SendReceive2(sess_data->xid, sess_data->ses, | |
749 | sess_data->iov, 2, | |
750 | &sess_data->buf0_type, | |
da502f7d PS |
751 | CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov); |
752 | cifs_small_buf_release(sess_data->iov[0].iov_base); | |
753 | memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec)); | |
3baf1a7b SP |
754 | |
755 | return rc; | |
756 | } | |
757 | ||
758 | static int | |
759 | SMB2_sess_establish_session(struct SMB2_sess_data *sess_data) | |
760 | { | |
761 | int rc = 0; | |
762 | struct cifs_ses *ses = sess_data->ses; | |
763 | ||
764 | mutex_lock(&ses->server->srv_mutex); | |
cabfb368 | 765 | if (ses->server->ops->generate_signingkey) { |
3baf1a7b | 766 | rc = ses->server->ops->generate_signingkey(ses); |
3baf1a7b SP |
767 | if (rc) { |
768 | cifs_dbg(FYI, | |
769 | "SMB3 session key generation failed\n"); | |
770 | mutex_unlock(&ses->server->srv_mutex); | |
cabfb368 | 771 | return rc; |
3baf1a7b SP |
772 | } |
773 | } | |
774 | if (!ses->server->session_estab) { | |
775 | ses->server->sequence_number = 0x2; | |
776 | ses->server->session_estab = true; | |
777 | } | |
778 | mutex_unlock(&ses->server->srv_mutex); | |
779 | ||
780 | cifs_dbg(FYI, "SMB2/3 session established successfully\n"); | |
781 | spin_lock(&GlobalMid_Lock); | |
782 | ses->status = CifsGood; | |
783 | ses->need_reconnect = false; | |
784 | spin_unlock(&GlobalMid_Lock); | |
3baf1a7b SP |
785 | return rc; |
786 | } | |
787 | ||
788 | #ifdef CONFIG_CIFS_UPCALL | |
789 | static void | |
790 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) | |
791 | { | |
792 | int rc; | |
793 | struct cifs_ses *ses = sess_data->ses; | |
794 | struct cifs_spnego_msg *msg; | |
795 | struct key *spnego_key = NULL; | |
796 | struct smb2_sess_setup_rsp *rsp = NULL; | |
797 | ||
798 | rc = SMB2_sess_alloc_buffer(sess_data); | |
799 | if (rc) | |
800 | goto out; | |
801 | ||
802 | spnego_key = cifs_get_spnego_key(ses); | |
803 | if (IS_ERR(spnego_key)) { | |
804 | rc = PTR_ERR(spnego_key); | |
805 | spnego_key = NULL; | |
806 | goto out; | |
807 | } | |
808 | ||
809 | msg = spnego_key->payload.data[0]; | |
810 | /* | |
811 | * check version field to make sure that cifs.upcall is | |
812 | * sending us a response in an expected form | |
813 | */ | |
814 | if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) { | |
815 | cifs_dbg(VFS, | |
816 | "bad cifs.upcall version. Expected %d got %d", | |
817 | CIFS_SPNEGO_UPCALL_VERSION, msg->version); | |
818 | rc = -EKEYREJECTED; | |
819 | goto out_put_spnego_key; | |
820 | } | |
821 | ||
822 | ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len, | |
823 | GFP_KERNEL); | |
824 | if (!ses->auth_key.response) { | |
825 | cifs_dbg(VFS, | |
826 | "Kerberos can't allocate (%u bytes) memory", | |
827 | msg->sesskey_len); | |
828 | rc = -ENOMEM; | |
829 | goto out_put_spnego_key; | |
830 | } | |
831 | ses->auth_key.len = msg->sesskey_len; | |
832 | ||
833 | sess_data->iov[1].iov_base = msg->data + msg->sesskey_len; | |
834 | sess_data->iov[1].iov_len = msg->secblob_len; | |
835 | ||
836 | rc = SMB2_sess_sendreceive(sess_data); | |
837 | if (rc) | |
838 | goto out_put_spnego_key; | |
839 | ||
840 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; | |
31473fc4 | 841 | ses->Suid = rsp->hdr.sync_hdr.SessionId; |
3baf1a7b SP |
842 | |
843 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); | |
3baf1a7b SP |
844 | |
845 | rc = SMB2_sess_establish_session(sess_data); | |
846 | out_put_spnego_key: | |
847 | key_invalidate(spnego_key); | |
848 | key_put(spnego_key); | |
849 | out: | |
850 | sess_data->result = rc; | |
851 | sess_data->func = NULL; | |
852 | SMB2_sess_free_buffer(sess_data); | |
853 | } | |
854 | #else | |
855 | static void | |
856 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) | |
857 | { | |
858 | cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n"); | |
859 | sess_data->result = -EOPNOTSUPP; | |
860 | sess_data->func = NULL; | |
861 | } | |
862 | #endif | |
863 | ||
166cea4d SP |
864 | static void |
865 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data); | |
866 | ||
867 | static void | |
868 | SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data) | |
5478f9ba | 869 | { |
166cea4d SP |
870 | int rc; |
871 | struct cifs_ses *ses = sess_data->ses; | |
5478f9ba | 872 | struct smb2_sess_setup_rsp *rsp = NULL; |
166cea4d | 873 | char *ntlmssp_blob = NULL; |
5478f9ba | 874 | bool use_spnego = false; /* else use raw ntlmssp */ |
166cea4d | 875 | u16 blob_length = 0; |
d4e63bd6 | 876 | |
5478f9ba PS |
877 | /* |
878 | * If memory allocation is successful, caller of this function | |
879 | * frees it. | |
880 | */ | |
881 | ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL); | |
166cea4d SP |
882 | if (!ses->ntlmssp) { |
883 | rc = -ENOMEM; | |
884 | goto out_err; | |
885 | } | |
5c234aa5 | 886 | ses->ntlmssp->sesskey_per_smbsess = true; |
5478f9ba | 887 | |
166cea4d | 888 | rc = SMB2_sess_alloc_buffer(sess_data); |
5478f9ba | 889 | if (rc) |
166cea4d | 890 | goto out_err; |
5478f9ba | 891 | |
166cea4d SP |
892 | ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE), |
893 | GFP_KERNEL); | |
894 | if (ntlmssp_blob == NULL) { | |
895 | rc = -ENOMEM; | |
896 | goto out; | |
897 | } | |
c2afb814 | 898 | |
166cea4d SP |
899 | build_ntlmssp_negotiate_blob(ntlmssp_blob, ses); |
900 | if (use_spnego) { | |
901 | /* BB eventually need to add this */ | |
902 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); | |
903 | rc = -EOPNOTSUPP; | |
904 | goto out; | |
905 | } else { | |
906 | blob_length = sizeof(struct _NEGOTIATE_MESSAGE); | |
907 | /* with raw NTLMSSP we don't encapsulate in SPNEGO */ | |
908 | } | |
909 | sess_data->iov[1].iov_base = ntlmssp_blob; | |
910 | sess_data->iov[1].iov_len = blob_length; | |
c2afb814 | 911 | |
166cea4d SP |
912 | rc = SMB2_sess_sendreceive(sess_data); |
913 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; | |
5478f9ba | 914 | |
166cea4d SP |
915 | /* If true, rc here is expected and not an error */ |
916 | if (sess_data->buf0_type != CIFS_NO_BUFFER && | |
31473fc4 | 917 | rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) |
166cea4d | 918 | rc = 0; |
38d77c50 | 919 | |
166cea4d SP |
920 | if (rc) |
921 | goto out; | |
5478f9ba | 922 | |
166cea4d SP |
923 | if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 != |
924 | le16_to_cpu(rsp->SecurityBufferOffset)) { | |
925 | cifs_dbg(VFS, "Invalid security buffer offset %d\n", | |
926 | le16_to_cpu(rsp->SecurityBufferOffset)); | |
5478f9ba | 927 | rc = -EIO; |
166cea4d | 928 | goto out; |
5478f9ba | 929 | } |
166cea4d SP |
930 | rc = decode_ntlmssp_challenge(rsp->Buffer, |
931 | le16_to_cpu(rsp->SecurityBufferLength), ses); | |
932 | if (rc) | |
933 | goto out; | |
5478f9ba | 934 | |
166cea4d | 935 | cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n"); |
5478f9ba | 936 | |
5478f9ba | 937 | |
31473fc4 | 938 | ses->Suid = rsp->hdr.sync_hdr.SessionId; |
166cea4d | 939 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
166cea4d SP |
940 | |
941 | out: | |
942 | kfree(ntlmssp_blob); | |
943 | SMB2_sess_free_buffer(sess_data); | |
944 | if (!rc) { | |
945 | sess_data->result = 0; | |
946 | sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate; | |
947 | return; | |
948 | } | |
949 | out_err: | |
950 | kfree(ses->ntlmssp); | |
951 | ses->ntlmssp = NULL; | |
952 | sess_data->result = rc; | |
953 | sess_data->func = NULL; | |
954 | } | |
5478f9ba | 955 | |
166cea4d SP |
956 | static void |
957 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data) | |
958 | { | |
959 | int rc; | |
960 | struct cifs_ses *ses = sess_data->ses; | |
961 | struct smb2_sess_setup_req *req; | |
962 | struct smb2_sess_setup_rsp *rsp = NULL; | |
963 | unsigned char *ntlmssp_blob = NULL; | |
964 | bool use_spnego = false; /* else use raw ntlmssp */ | |
965 | u16 blob_length = 0; | |
5478f9ba | 966 | |
166cea4d SP |
967 | rc = SMB2_sess_alloc_buffer(sess_data); |
968 | if (rc) | |
969 | goto out; | |
5478f9ba | 970 | |
166cea4d | 971 | req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base; |
31473fc4 | 972 | req->hdr.sync_hdr.SessionId = ses->Suid; |
166cea4d SP |
973 | |
974 | rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses, | |
975 | sess_data->nls_cp); | |
976 | if (rc) { | |
977 | cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc); | |
978 | goto out; | |
5478f9ba PS |
979 | } |
980 | ||
166cea4d SP |
981 | if (use_spnego) { |
982 | /* BB eventually need to add this */ | |
983 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); | |
984 | rc = -EOPNOTSUPP; | |
985 | goto out; | |
986 | } | |
987 | sess_data->iov[1].iov_base = ntlmssp_blob; | |
988 | sess_data->iov[1].iov_len = blob_length; | |
5478f9ba | 989 | |
166cea4d SP |
990 | rc = SMB2_sess_sendreceive(sess_data); |
991 | if (rc) | |
992 | goto out; | |
993 | ||
994 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; | |
995 | ||
31473fc4 | 996 | ses->Suid = rsp->hdr.sync_hdr.SessionId; |
5478f9ba | 997 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
5478f9ba | 998 | |
166cea4d SP |
999 | rc = SMB2_sess_establish_session(sess_data); |
1000 | out: | |
1001 | kfree(ntlmssp_blob); | |
1002 | SMB2_sess_free_buffer(sess_data); | |
1003 | kfree(ses->ntlmssp); | |
1004 | ses->ntlmssp = NULL; | |
1005 | sess_data->result = rc; | |
1006 | sess_data->func = NULL; | |
1007 | } | |
d4e63bd6 | 1008 | |
166cea4d SP |
1009 | static int |
1010 | SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data) | |
1011 | { | |
1012 | if (ses->sectype != Kerberos && ses->sectype != RawNTLMSSP) | |
1013 | ses->sectype = RawNTLMSSP; | |
d4e63bd6 | 1014 | |
166cea4d SP |
1015 | switch (ses->sectype) { |
1016 | case Kerberos: | |
1017 | sess_data->func = SMB2_auth_kerberos; | |
1018 | break; | |
1019 | case RawNTLMSSP: | |
1020 | sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate; | |
1021 | break; | |
1022 | default: | |
1023 | cifs_dbg(VFS, "secType %d not supported!\n", ses->sectype); | |
1024 | return -EOPNOTSUPP; | |
d4e63bd6 SP |
1025 | } |
1026 | ||
166cea4d SP |
1027 | return 0; |
1028 | } | |
1029 | ||
1030 | int | |
1031 | SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, | |
1032 | const struct nls_table *nls_cp) | |
1033 | { | |
1034 | int rc = 0; | |
1035 | struct TCP_Server_Info *server = ses->server; | |
1036 | struct SMB2_sess_data *sess_data; | |
1037 | ||
1038 | cifs_dbg(FYI, "Session Setup\n"); | |
1039 | ||
1040 | if (!server) { | |
1041 | WARN(1, "%s: server is NULL!\n", __func__); | |
1042 | return -EIO; | |
d4e63bd6 | 1043 | } |
d4e63bd6 | 1044 | |
166cea4d SP |
1045 | sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL); |
1046 | if (!sess_data) | |
1047 | return -ENOMEM; | |
1048 | ||
1049 | rc = SMB2_select_sec(ses, sess_data); | |
1050 | if (rc) | |
1051 | goto out; | |
1052 | sess_data->xid = xid; | |
1053 | sess_data->ses = ses; | |
1054 | sess_data->buf0_type = CIFS_NO_BUFFER; | |
1055 | sess_data->nls_cp = (struct nls_table *) nls_cp; | |
1056 | ||
1057 | while (sess_data->func) | |
1058 | sess_data->func(sess_data); | |
1059 | ||
3baf1a7b | 1060 | rc = sess_data->result; |
166cea4d | 1061 | out: |
3baf1a7b | 1062 | kfree(sess_data); |
5478f9ba PS |
1063 | return rc; |
1064 | } | |
1065 | ||
1066 | int | |
1067 | SMB2_logoff(const unsigned int xid, struct cifs_ses *ses) | |
1068 | { | |
1069 | struct smb2_logoff_req *req; /* response is also trivial struct */ | |
1070 | int rc = 0; | |
1071 | struct TCP_Server_Info *server; | |
7fb8986e | 1072 | int flags = 0; |
5478f9ba | 1073 | |
f96637be | 1074 | cifs_dbg(FYI, "disconnect session %p\n", ses); |
5478f9ba PS |
1075 | |
1076 | if (ses && (ses->server)) | |
1077 | server = ses->server; | |
1078 | else | |
1079 | return -EIO; | |
1080 | ||
eb4c7df6 SP |
1081 | /* no need to send SMB logoff if uid already closed due to reconnect */ |
1082 | if (ses->need_reconnect) | |
1083 | goto smb2_session_already_dead; | |
1084 | ||
5478f9ba PS |
1085 | rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req); |
1086 | if (rc) | |
1087 | return rc; | |
1088 | ||
1089 | /* since no tcon, smb2_init can not do this, so do here */ | |
31473fc4 | 1090 | req->hdr.sync_hdr.SessionId = ses->Suid; |
7fb8986e PS |
1091 | |
1092 | if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) | |
1093 | flags |= CIFS_TRANSFORM_REQ; | |
1094 | else if (server->sign) | |
31473fc4 | 1095 | req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
5478f9ba | 1096 | |
7fb8986e | 1097 | rc = SendReceiveNoRsp(xid, ses, (char *) req, flags); |
da502f7d | 1098 | cifs_small_buf_release(req); |
5478f9ba PS |
1099 | /* |
1100 | * No tcon so can't do | |
1101 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); | |
1102 | */ | |
eb4c7df6 SP |
1103 | |
1104 | smb2_session_already_dead: | |
5478f9ba PS |
1105 | return rc; |
1106 | } | |
faaf946a PS |
1107 | |
1108 | static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code) | |
1109 | { | |
d60622eb | 1110 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]); |
faaf946a PS |
1111 | } |
1112 | ||
1113 | #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */) | |
1114 | ||
de9f68df SF |
1115 | /* These are similar values to what Windows uses */ |
1116 | static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon) | |
1117 | { | |
1118 | tcon->max_chunks = 256; | |
1119 | tcon->max_bytes_chunk = 1048576; | |
1120 | tcon->max_bytes_copy = 16777216; | |
1121 | } | |
1122 | ||
faaf946a PS |
1123 | int |
1124 | SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree, | |
1125 | struct cifs_tcon *tcon, const struct nls_table *cp) | |
1126 | { | |
1127 | struct smb2_tree_connect_req *req; | |
1128 | struct smb2_tree_connect_rsp *rsp = NULL; | |
1129 | struct kvec iov[2]; | |
da502f7d | 1130 | struct kvec rsp_iov; |
faaf946a PS |
1131 | int rc = 0; |
1132 | int resp_buftype; | |
1133 | int unc_path_len; | |
1134 | struct TCP_Server_Info *server; | |
1135 | __le16 *unc_path = NULL; | |
7fb8986e | 1136 | int flags = 0; |
faaf946a | 1137 | |
f96637be | 1138 | cifs_dbg(FYI, "TCON\n"); |
faaf946a PS |
1139 | |
1140 | if ((ses->server) && tree) | |
1141 | server = ses->server; | |
1142 | else | |
1143 | return -EIO; | |
1144 | ||
1145 | if (tcon && tcon->bad_network_name) | |
1146 | return -ENOENT; | |
1147 | ||
1148 | unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL); | |
1149 | if (unc_path == NULL) | |
1150 | return -ENOMEM; | |
1151 | ||
1152 | unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1; | |
1153 | unc_path_len *= 2; | |
1154 | if (unc_path_len < 2) { | |
1155 | kfree(unc_path); | |
1156 | return -EINVAL; | |
1157 | } | |
1158 | ||
1159 | rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req); | |
1160 | if (rc) { | |
1161 | kfree(unc_path); | |
1162 | return rc; | |
1163 | } | |
1164 | ||
1165 | if (tcon == NULL) { | |
ae6f8dd4 PS |
1166 | if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)) |
1167 | flags |= CIFS_TRANSFORM_REQ; | |
1168 | ||
faaf946a | 1169 | /* since no tcon, smb2_init can not do this, so do here */ |
31473fc4 | 1170 | req->hdr.sync_hdr.SessionId = ses->Suid; |
faaf946a PS |
1171 | /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED) |
1172 | req->hdr.Flags |= SMB2_FLAGS_SIGNED; */ | |
ae6f8dd4 PS |
1173 | } else if (encryption_required(tcon)) |
1174 | flags |= CIFS_TRANSFORM_REQ; | |
faaf946a PS |
1175 | |
1176 | iov[0].iov_base = (char *)req; | |
1177 | /* 4 for rfc1002 length field and 1 for pad */ | |
1178 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
1179 | ||
1180 | /* Testing shows that buffer offset must be at location of Buffer[0] */ | |
1181 | req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req) | |
1182 | - 1 /* pad */ - 4 /* do not count rfc1001 len field */); | |
1183 | req->PathLength = cpu_to_le16(unc_path_len - 2); | |
1184 | iov[1].iov_base = unc_path; | |
1185 | iov[1].iov_len = unc_path_len; | |
1186 | ||
1187 | inc_rfc1001_len(req, unc_path_len - 1 /* pad */); | |
1188 | ||
7fb8986e | 1189 | rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
1190 | cifs_small_buf_release(req); |
1191 | rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base; | |
faaf946a PS |
1192 | |
1193 | if (rc != 0) { | |
1194 | if (tcon) { | |
1195 | cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE); | |
1196 | tcon->need_reconnect = true; | |
1197 | } | |
1198 | goto tcon_error_exit; | |
1199 | } | |
1200 | ||
faaf946a | 1201 | if (tcon == NULL) { |
31473fc4 | 1202 | ses->ipc_tid = rsp->hdr.sync_hdr.TreeId; |
faaf946a PS |
1203 | goto tcon_exit; |
1204 | } | |
1205 | ||
1206 | if (rsp->ShareType & SMB2_SHARE_TYPE_DISK) | |
f96637be | 1207 | cifs_dbg(FYI, "connection to disk share\n"); |
faaf946a PS |
1208 | else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) { |
1209 | tcon->ipc = true; | |
f96637be | 1210 | cifs_dbg(FYI, "connection to pipe share\n"); |
faaf946a PS |
1211 | } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) { |
1212 | tcon->print = true; | |
f96637be | 1213 | cifs_dbg(FYI, "connection to printer\n"); |
faaf946a | 1214 | } else { |
f96637be | 1215 | cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType); |
faaf946a PS |
1216 | rc = -EOPNOTSUPP; |
1217 | goto tcon_error_exit; | |
1218 | } | |
1219 | ||
1220 | tcon->share_flags = le32_to_cpu(rsp->ShareFlags); | |
769ee6a4 | 1221 | tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */ |
faaf946a PS |
1222 | tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
1223 | tcon->tidStatus = CifsGood; | |
1224 | tcon->need_reconnect = false; | |
31473fc4 | 1225 | tcon->tid = rsp->hdr.sync_hdr.TreeId; |
46b51d08 | 1226 | strlcpy(tcon->treeName, tree, sizeof(tcon->treeName)); |
faaf946a PS |
1227 | |
1228 | if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) && | |
1229 | ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0)) | |
f96637be | 1230 | cifs_dbg(VFS, "DFS capability contradicts DFS flag\n"); |
ae6f8dd4 PS |
1231 | |
1232 | if (tcon->seal && | |
1233 | !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) | |
1234 | cifs_dbg(VFS, "Encryption is requested but not supported\n"); | |
1235 | ||
de9f68df | 1236 | init_copy_chunk_defaults(tcon); |
ff1c038a SF |
1237 | if (tcon->ses->server->ops->validate_negotiate) |
1238 | rc = tcon->ses->server->ops->validate_negotiate(xid, tcon); | |
faaf946a PS |
1239 | tcon_exit: |
1240 | free_rsp_buf(resp_buftype, rsp); | |
1241 | kfree(unc_path); | |
1242 | return rc; | |
1243 | ||
1244 | tcon_error_exit: | |
31473fc4 | 1245 | if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) { |
f96637be | 1246 | cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree); |
18f39e7b SF |
1247 | if (tcon) |
1248 | tcon->bad_network_name = true; | |
faaf946a PS |
1249 | } |
1250 | goto tcon_exit; | |
1251 | } | |
1252 | ||
1253 | int | |
1254 | SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon) | |
1255 | { | |
1256 | struct smb2_tree_disconnect_req *req; /* response is trivial */ | |
1257 | int rc = 0; | |
1258 | struct TCP_Server_Info *server; | |
1259 | struct cifs_ses *ses = tcon->ses; | |
7fb8986e | 1260 | int flags = 0; |
faaf946a | 1261 | |
f96637be | 1262 | cifs_dbg(FYI, "Tree Disconnect\n"); |
faaf946a PS |
1263 | |
1264 | if (ses && (ses->server)) | |
1265 | server = ses->server; | |
1266 | else | |
1267 | return -EIO; | |
1268 | ||
1269 | if ((tcon->need_reconnect) || (tcon->ses->need_reconnect)) | |
1270 | return 0; | |
1271 | ||
1272 | rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req); | |
1273 | if (rc) | |
1274 | return rc; | |
1275 | ||
7fb8986e PS |
1276 | if (encryption_required(tcon)) |
1277 | flags |= CIFS_TRANSFORM_REQ; | |
1278 | ||
1279 | rc = SendReceiveNoRsp(xid, ses, (char *)req, flags); | |
da502f7d | 1280 | cifs_small_buf_release(req); |
faaf946a PS |
1281 | if (rc) |
1282 | cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE); | |
1283 | ||
1284 | return rc; | |
1285 | } | |
2503a0db | 1286 | |
b8c32dbb | 1287 | |
63eb3def PS |
1288 | static struct create_durable * |
1289 | create_durable_buf(void) | |
1290 | { | |
1291 | struct create_durable *buf; | |
1292 | ||
1293 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); | |
1294 | if (!buf) | |
1295 | return NULL; | |
1296 | ||
1297 | buf->ccontext.DataOffset = cpu_to_le16(offsetof | |
9cbc0b73 | 1298 | (struct create_durable, Data)); |
63eb3def PS |
1299 | buf->ccontext.DataLength = cpu_to_le32(16); |
1300 | buf->ccontext.NameOffset = cpu_to_le16(offsetof | |
1301 | (struct create_durable, Name)); | |
1302 | buf->ccontext.NameLength = cpu_to_le16(4); | |
12197a7f | 1303 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */ |
63eb3def PS |
1304 | buf->Name[0] = 'D'; |
1305 | buf->Name[1] = 'H'; | |
1306 | buf->Name[2] = 'n'; | |
1307 | buf->Name[3] = 'Q'; | |
1308 | return buf; | |
1309 | } | |
1310 | ||
9cbc0b73 PS |
1311 | static struct create_durable * |
1312 | create_reconnect_durable_buf(struct cifs_fid *fid) | |
1313 | { | |
1314 | struct create_durable *buf; | |
1315 | ||
1316 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); | |
1317 | if (!buf) | |
1318 | return NULL; | |
1319 | ||
1320 | buf->ccontext.DataOffset = cpu_to_le16(offsetof | |
1321 | (struct create_durable, Data)); | |
1322 | buf->ccontext.DataLength = cpu_to_le32(16); | |
1323 | buf->ccontext.NameOffset = cpu_to_le16(offsetof | |
1324 | (struct create_durable, Name)); | |
1325 | buf->ccontext.NameLength = cpu_to_le16(4); | |
1326 | buf->Data.Fid.PersistentFileId = fid->persistent_fid; | |
1327 | buf->Data.Fid.VolatileFileId = fid->volatile_fid; | |
12197a7f | 1328 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */ |
9cbc0b73 PS |
1329 | buf->Name[0] = 'D'; |
1330 | buf->Name[1] = 'H'; | |
1331 | buf->Name[2] = 'n'; | |
1332 | buf->Name[3] = 'C'; | |
1333 | return buf; | |
1334 | } | |
1335 | ||
b8c32dbb | 1336 | static __u8 |
42873b0a PS |
1337 | parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp, |
1338 | unsigned int *epoch) | |
b8c32dbb PS |
1339 | { |
1340 | char *data_offset; | |
b5c7cde3 | 1341 | struct create_context *cc; |
deb7deff JM |
1342 | unsigned int next; |
1343 | unsigned int remaining; | |
fd554396 | 1344 | char *name; |
b8c32dbb | 1345 | |
fd554396 | 1346 | data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset); |
deb7deff | 1347 | remaining = le32_to_cpu(rsp->CreateContextsLength); |
b5c7cde3 | 1348 | cc = (struct create_context *)data_offset; |
deb7deff | 1349 | while (remaining >= sizeof(struct create_context)) { |
b5c7cde3 | 1350 | name = le16_to_cpu(cc->NameOffset) + (char *)cc; |
deb7deff JM |
1351 | if (le16_to_cpu(cc->NameLength) == 4 && |
1352 | strncmp(name, "RqLs", 4) == 0) | |
1353 | return server->ops->parse_lease_buf(cc, epoch); | |
1354 | ||
1355 | next = le32_to_cpu(cc->Next); | |
1356 | if (!next) | |
1357 | break; | |
1358 | remaining -= next; | |
1359 | cc = (struct create_context *)((char *)cc + next); | |
1360 | } | |
b8c32dbb | 1361 | |
b5c7cde3 | 1362 | return 0; |
b8c32dbb PS |
1363 | } |
1364 | ||
d22cbfec | 1365 | static int |
a41a28bd PS |
1366 | add_lease_context(struct TCP_Server_Info *server, struct kvec *iov, |
1367 | unsigned int *num_iovec, __u8 *oplock) | |
d22cbfec PS |
1368 | { |
1369 | struct smb2_create_req *req = iov[0].iov_base; | |
1370 | unsigned int num = *num_iovec; | |
1371 | ||
a41a28bd | 1372 | iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock); |
d22cbfec PS |
1373 | if (iov[num].iov_base == NULL) |
1374 | return -ENOMEM; | |
a41a28bd | 1375 | iov[num].iov_len = server->vals->create_lease_size; |
d22cbfec PS |
1376 | req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
1377 | if (!req->CreateContextsOffset) | |
1378 | req->CreateContextsOffset = cpu_to_le32( | |
1379 | sizeof(struct smb2_create_req) - 4 + | |
1380 | iov[num - 1].iov_len); | |
a41a28bd PS |
1381 | le32_add_cpu(&req->CreateContextsLength, |
1382 | server->vals->create_lease_size); | |
1383 | inc_rfc1001_len(&req->hdr, server->vals->create_lease_size); | |
d22cbfec PS |
1384 | *num_iovec = num + 1; |
1385 | return 0; | |
1386 | } | |
1387 | ||
b56eae4d SF |
1388 | static struct create_durable_v2 * |
1389 | create_durable_v2_buf(struct cifs_fid *pfid) | |
1390 | { | |
1391 | struct create_durable_v2 *buf; | |
1392 | ||
1393 | buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL); | |
1394 | if (!buf) | |
1395 | return NULL; | |
1396 | ||
1397 | buf->ccontext.DataOffset = cpu_to_le16(offsetof | |
1398 | (struct create_durable_v2, dcontext)); | |
1399 | buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2)); | |
1400 | buf->ccontext.NameOffset = cpu_to_le16(offsetof | |
1401 | (struct create_durable_v2, Name)); | |
1402 | buf->ccontext.NameLength = cpu_to_le16(4); | |
1403 | ||
1404 | buf->dcontext.Timeout = 0; /* Should this be configurable by workload */ | |
1405 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); | |
fa70b87c | 1406 | generate_random_uuid(buf->dcontext.CreateGuid); |
b56eae4d SF |
1407 | memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16); |
1408 | ||
1409 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */ | |
1410 | buf->Name[0] = 'D'; | |
1411 | buf->Name[1] = 'H'; | |
1412 | buf->Name[2] = '2'; | |
1413 | buf->Name[3] = 'Q'; | |
1414 | return buf; | |
1415 | } | |
1416 | ||
1417 | static struct create_durable_handle_reconnect_v2 * | |
1418 | create_reconnect_durable_v2_buf(struct cifs_fid *fid) | |
1419 | { | |
1420 | struct create_durable_handle_reconnect_v2 *buf; | |
1421 | ||
1422 | buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2), | |
1423 | GFP_KERNEL); | |
1424 | if (!buf) | |
1425 | return NULL; | |
1426 | ||
1427 | buf->ccontext.DataOffset = | |
1428 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, | |
1429 | dcontext)); | |
1430 | buf->ccontext.DataLength = | |
1431 | cpu_to_le32(sizeof(struct durable_reconnect_context_v2)); | |
1432 | buf->ccontext.NameOffset = | |
1433 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, | |
1434 | Name)); | |
1435 | buf->ccontext.NameLength = cpu_to_le16(4); | |
1436 | ||
1437 | buf->dcontext.Fid.PersistentFileId = fid->persistent_fid; | |
1438 | buf->dcontext.Fid.VolatileFileId = fid->volatile_fid; | |
1439 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); | |
1440 | memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16); | |
1441 | ||
1442 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */ | |
1443 | buf->Name[0] = 'D'; | |
1444 | buf->Name[1] = 'H'; | |
1445 | buf->Name[2] = '2'; | |
1446 | buf->Name[3] = 'C'; | |
1447 | return buf; | |
1448 | } | |
1449 | ||
63eb3def | 1450 | static int |
b56eae4d SF |
1451 | add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec, |
1452 | struct cifs_open_parms *oparms) | |
1453 | { | |
1454 | struct smb2_create_req *req = iov[0].iov_base; | |
1455 | unsigned int num = *num_iovec; | |
1456 | ||
1457 | iov[num].iov_base = create_durable_v2_buf(oparms->fid); | |
1458 | if (iov[num].iov_base == NULL) | |
1459 | return -ENOMEM; | |
1460 | iov[num].iov_len = sizeof(struct create_durable_v2); | |
1461 | if (!req->CreateContextsOffset) | |
1462 | req->CreateContextsOffset = | |
1463 | cpu_to_le32(sizeof(struct smb2_create_req) - 4 + | |
1464 | iov[1].iov_len); | |
1465 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2)); | |
1466 | inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2)); | |
1467 | *num_iovec = num + 1; | |
1468 | return 0; | |
1469 | } | |
1470 | ||
1471 | static int | |
1472 | add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec, | |
9cbc0b73 | 1473 | struct cifs_open_parms *oparms) |
63eb3def PS |
1474 | { |
1475 | struct smb2_create_req *req = iov[0].iov_base; | |
1476 | unsigned int num = *num_iovec; | |
1477 | ||
b56eae4d SF |
1478 | /* indicate that we don't need to relock the file */ |
1479 | oparms->reconnect = false; | |
1480 | ||
1481 | iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid); | |
1482 | if (iov[num].iov_base == NULL) | |
1483 | return -ENOMEM; | |
1484 | iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2); | |
1485 | if (!req->CreateContextsOffset) | |
1486 | req->CreateContextsOffset = | |
1487 | cpu_to_le32(sizeof(struct smb2_create_req) - 4 + | |
1488 | iov[1].iov_len); | |
1489 | le32_add_cpu(&req->CreateContextsLength, | |
1490 | sizeof(struct create_durable_handle_reconnect_v2)); | |
1491 | inc_rfc1001_len(&req->hdr, | |
1492 | sizeof(struct create_durable_handle_reconnect_v2)); | |
1493 | *num_iovec = num + 1; | |
1494 | return 0; | |
1495 | } | |
1496 | ||
1497 | static int | |
1498 | add_durable_context(struct kvec *iov, unsigned int *num_iovec, | |
1499 | struct cifs_open_parms *oparms, bool use_persistent) | |
1500 | { | |
1501 | struct smb2_create_req *req = iov[0].iov_base; | |
1502 | unsigned int num = *num_iovec; | |
1503 | ||
1504 | if (use_persistent) { | |
1505 | if (oparms->reconnect) | |
1506 | return add_durable_reconnect_v2_context(iov, num_iovec, | |
1507 | oparms); | |
1508 | else | |
1509 | return add_durable_v2_context(iov, num_iovec, oparms); | |
1510 | } | |
1511 | ||
9cbc0b73 PS |
1512 | if (oparms->reconnect) { |
1513 | iov[num].iov_base = create_reconnect_durable_buf(oparms->fid); | |
1514 | /* indicate that we don't need to relock the file */ | |
1515 | oparms->reconnect = false; | |
1516 | } else | |
1517 | iov[num].iov_base = create_durable_buf(); | |
63eb3def PS |
1518 | if (iov[num].iov_base == NULL) |
1519 | return -ENOMEM; | |
1520 | iov[num].iov_len = sizeof(struct create_durable); | |
1521 | if (!req->CreateContextsOffset) | |
1522 | req->CreateContextsOffset = | |
1523 | cpu_to_le32(sizeof(struct smb2_create_req) - 4 + | |
1524 | iov[1].iov_len); | |
31f92e9a | 1525 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable)); |
63eb3def PS |
1526 | inc_rfc1001_len(&req->hdr, sizeof(struct create_durable)); |
1527 | *num_iovec = num + 1; | |
1528 | return 0; | |
1529 | } | |
1530 | ||
2503a0db | 1531 | int |
064f6047 | 1532 | SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, |
b42bf888 PS |
1533 | __u8 *oplock, struct smb2_file_all_info *buf, |
1534 | struct smb2_err_rsp **err_buf) | |
2503a0db PS |
1535 | { |
1536 | struct smb2_create_req *req; | |
1537 | struct smb2_create_rsp *rsp; | |
1538 | struct TCP_Server_Info *server; | |
064f6047 | 1539 | struct cifs_tcon *tcon = oparms->tcon; |
2503a0db | 1540 | struct cifs_ses *ses = tcon->ses; |
63eb3def | 1541 | struct kvec iov[4]; |
da502f7d | 1542 | struct kvec rsp_iov; |
2503a0db PS |
1543 | int resp_buftype; |
1544 | int uni_path_len; | |
b8c32dbb PS |
1545 | __le16 *copy_path = NULL; |
1546 | int copy_size; | |
2503a0db | 1547 | int rc = 0; |
da502f7d | 1548 | unsigned int n_iov = 2; |
ca81983f | 1549 | __u32 file_attributes = 0; |
663a9621 | 1550 | char *dhc_buf = NULL, *lc_buf = NULL; |
7fb8986e | 1551 | int flags = 0; |
2503a0db | 1552 | |
f96637be | 1553 | cifs_dbg(FYI, "create/open\n"); |
2503a0db PS |
1554 | |
1555 | if (ses && (ses->server)) | |
1556 | server = ses->server; | |
1557 | else | |
1558 | return -EIO; | |
1559 | ||
1560 | rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req); | |
1561 | if (rc) | |
1562 | return rc; | |
1563 | ||
7fb8986e PS |
1564 | if (encryption_required(tcon)) |
1565 | flags |= CIFS_TRANSFORM_REQ; | |
1566 | ||
064f6047 | 1567 | if (oparms->create_options & CREATE_OPTION_READONLY) |
ca81983f | 1568 | file_attributes |= ATTR_READONLY; |
db8b631d SF |
1569 | if (oparms->create_options & CREATE_OPTION_SPECIAL) |
1570 | file_attributes |= ATTR_SYSTEM; | |
ca81983f | 1571 | |
2503a0db | 1572 | req->ImpersonationLevel = IL_IMPERSONATION; |
064f6047 | 1573 | req->DesiredAccess = cpu_to_le32(oparms->desired_access); |
2503a0db PS |
1574 | /* File attributes ignored on open (used in create though) */ |
1575 | req->FileAttributes = cpu_to_le32(file_attributes); | |
1576 | req->ShareAccess = FILE_SHARE_ALL_LE; | |
064f6047 PS |
1577 | req->CreateDisposition = cpu_to_le32(oparms->disposition); |
1578 | req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK); | |
2503a0db | 1579 | uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2; |
59aa3718 PS |
1580 | /* do not count rfc1001 len field */ |
1581 | req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4); | |
2503a0db PS |
1582 | |
1583 | iov[0].iov_base = (char *)req; | |
1584 | /* 4 for rfc1002 length field */ | |
1585 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1586 | ||
1587 | /* MUST set path len (NameLength) to 0 opening root of share */ | |
59aa3718 PS |
1588 | req->NameLength = cpu_to_le16(uni_path_len - 2); |
1589 | /* -1 since last byte is buf[0] which is sent below (path) */ | |
1590 | iov[0].iov_len--; | |
1591 | if (uni_path_len % 8 != 0) { | |
1592 | copy_size = uni_path_len / 8 * 8; | |
1593 | if (copy_size < uni_path_len) | |
1594 | copy_size += 8; | |
1595 | ||
1596 | copy_path = kzalloc(copy_size, GFP_KERNEL); | |
1597 | if (!copy_path) | |
1598 | return -ENOMEM; | |
1599 | memcpy((char *)copy_path, (const char *)path, | |
1600 | uni_path_len); | |
1601 | uni_path_len = copy_size; | |
1602 | path = copy_path; | |
2503a0db PS |
1603 | } |
1604 | ||
59aa3718 PS |
1605 | iov[1].iov_len = uni_path_len; |
1606 | iov[1].iov_base = path; | |
1607 | /* -1 since last byte is buf[0] which was counted in smb2_buf_len */ | |
1608 | inc_rfc1001_len(req, uni_path_len - 1); | |
1609 | ||
b8c32dbb PS |
1610 | if (!server->oplocks) |
1611 | *oplock = SMB2_OPLOCK_LEVEL_NONE; | |
1612 | ||
a41a28bd | 1613 | if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) || |
b8c32dbb PS |
1614 | *oplock == SMB2_OPLOCK_LEVEL_NONE) |
1615 | req->RequestedOplockLevel = *oplock; | |
1616 | else { | |
da502f7d | 1617 | rc = add_lease_context(server, iov, &n_iov, oplock); |
d22cbfec | 1618 | if (rc) { |
b8c32dbb PS |
1619 | cifs_small_buf_release(req); |
1620 | kfree(copy_path); | |
d22cbfec | 1621 | return rc; |
b8c32dbb | 1622 | } |
da502f7d | 1623 | lc_buf = iov[n_iov-1].iov_base; |
b8c32dbb PS |
1624 | } |
1625 | ||
63eb3def PS |
1626 | if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) { |
1627 | /* need to set Next field of lease context if we request it */ | |
a41a28bd | 1628 | if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) { |
63eb3def | 1629 | struct create_context *ccontext = |
da502f7d | 1630 | (struct create_context *)iov[n_iov-1].iov_base; |
1c46943f | 1631 | ccontext->Next = |
a41a28bd | 1632 | cpu_to_le32(server->vals->create_lease_size); |
63eb3def | 1633 | } |
b56eae4d | 1634 | |
da502f7d | 1635 | rc = add_durable_context(iov, &n_iov, oparms, |
b56eae4d | 1636 | tcon->use_persistent); |
63eb3def PS |
1637 | if (rc) { |
1638 | cifs_small_buf_release(req); | |
1639 | kfree(copy_path); | |
663a9621 | 1640 | kfree(lc_buf); |
63eb3def PS |
1641 | return rc; |
1642 | } | |
da502f7d | 1643 | dhc_buf = iov[n_iov-1].iov_base; |
63eb3def PS |
1644 | } |
1645 | ||
7fb8986e | 1646 | rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
1647 | cifs_small_buf_release(req); |
1648 | rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; | |
2503a0db PS |
1649 | |
1650 | if (rc != 0) { | |
1651 | cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); | |
b42bf888 PS |
1652 | if (err_buf) |
1653 | *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4, | |
1654 | GFP_KERNEL); | |
2503a0db PS |
1655 | goto creat_exit; |
1656 | } | |
1657 | ||
064f6047 PS |
1658 | oparms->fid->persistent_fid = rsp->PersistentFileId; |
1659 | oparms->fid->volatile_fid = rsp->VolatileFileId; | |
f0df737e PS |
1660 | |
1661 | if (buf) { | |
1662 | memcpy(buf, &rsp->CreationTime, 32); | |
1663 | buf->AllocationSize = rsp->AllocationSize; | |
1664 | buf->EndOfFile = rsp->EndofFile; | |
1665 | buf->Attributes = rsp->FileAttributes; | |
1666 | buf->NumberOfLinks = cpu_to_le32(1); | |
1667 | buf->DeletePending = 0; | |
1668 | } | |
2e44b288 | 1669 | |
b8c32dbb | 1670 | if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) |
42873b0a | 1671 | *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch); |
b8c32dbb PS |
1672 | else |
1673 | *oplock = rsp->OplockLevel; | |
2503a0db | 1674 | creat_exit: |
b8c32dbb | 1675 | kfree(copy_path); |
663a9621 PS |
1676 | kfree(lc_buf); |
1677 | kfree(dhc_buf); | |
2503a0db PS |
1678 | free_rsp_buf(resp_buftype, rsp); |
1679 | return rc; | |
1680 | } | |
1681 | ||
4a72dafa SF |
1682 | /* |
1683 | * SMB2 IOCTL is used for both IOCTLs and FSCTLs | |
1684 | */ | |
1685 | int | |
1686 | SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, | |
51146625 AA |
1687 | u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc, |
1688 | char *in_data, u32 indatalen, | |
1689 | char **out_data, u32 *plen /* returned data len */) | |
4a72dafa SF |
1690 | { |
1691 | struct smb2_ioctl_req *req; | |
1692 | struct smb2_ioctl_rsp *rsp; | |
31473fc4 | 1693 | struct smb2_sync_hdr *shdr; |
4a72dafa | 1694 | struct TCP_Server_Info *server; |
8e353106 | 1695 | struct cifs_ses *ses; |
4a72dafa | 1696 | struct kvec iov[2]; |
da502f7d | 1697 | struct kvec rsp_iov; |
4a72dafa | 1698 | int resp_buftype; |
da502f7d | 1699 | int n_iov; |
4a72dafa | 1700 | int rc = 0; |
7fb8986e | 1701 | int flags = 0; |
4a72dafa SF |
1702 | |
1703 | cifs_dbg(FYI, "SMB2 IOCTL\n"); | |
1704 | ||
3d1a3745 SF |
1705 | if (out_data != NULL) |
1706 | *out_data = NULL; | |
1707 | ||
4a72dafa SF |
1708 | /* zero out returned data len, in case of error */ |
1709 | if (plen) | |
1710 | *plen = 0; | |
1711 | ||
8e353106 SF |
1712 | if (tcon) |
1713 | ses = tcon->ses; | |
1714 | else | |
1715 | return -EIO; | |
1716 | ||
4a72dafa SF |
1717 | if (ses && (ses->server)) |
1718 | server = ses->server; | |
1719 | else | |
1720 | return -EIO; | |
1721 | ||
1722 | rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req); | |
1723 | if (rc) | |
1724 | return rc; | |
1725 | ||
51146625 AA |
1726 | if (use_ipc) { |
1727 | if (ses->ipc_tid == 0) { | |
1728 | cifs_small_buf_release(req); | |
1729 | return -ENOTCONN; | |
1730 | } | |
1731 | ||
1732 | cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n", | |
1733 | req->hdr.sync_hdr.TreeId, ses->ipc_tid); | |
1734 | req->hdr.sync_hdr.TreeId = ses->ipc_tid; | |
1735 | } | |
7fb8986e PS |
1736 | if (encryption_required(tcon)) |
1737 | flags |= CIFS_TRANSFORM_REQ; | |
1738 | ||
4a72dafa SF |
1739 | req->CtlCode = cpu_to_le32(opcode); |
1740 | req->PersistentFileId = persistent_fid; | |
1741 | req->VolatileFileId = volatile_fid; | |
1742 | ||
1743 | if (indatalen) { | |
1744 | req->InputCount = cpu_to_le32(indatalen); | |
1745 | /* do not set InputOffset if no input data */ | |
1746 | req->InputOffset = | |
1747 | cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4); | |
1748 | iov[1].iov_base = in_data; | |
1749 | iov[1].iov_len = indatalen; | |
da502f7d | 1750 | n_iov = 2; |
4a72dafa | 1751 | } else |
da502f7d | 1752 | n_iov = 1; |
4a72dafa SF |
1753 | |
1754 | req->OutputOffset = 0; | |
1755 | req->OutputCount = 0; /* MBZ */ | |
1756 | ||
1757 | /* | |
1758 | * Could increase MaxOutputResponse, but that would require more | |
1759 | * than one credit. Windows typically sets this smaller, but for some | |
1760 | * ioctls it may be useful to allow server to send more. No point | |
1761 | * limiting what the server can send as long as fits in one credit | |
1762 | */ | |
1763 | req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */ | |
1764 | ||
1765 | if (is_fsctl) | |
1766 | req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL); | |
1767 | else | |
1768 | req->Flags = 0; | |
1769 | ||
1770 | iov[0].iov_base = (char *)req; | |
4a72dafa | 1771 | |
7ff8d45c SF |
1772 | /* |
1773 | * If no input data, the size of ioctl struct in | |
1774 | * protocol spec still includes a 1 byte data buffer, | |
1775 | * but if input data passed to ioctl, we do not | |
1776 | * want to double count this, so we do not send | |
1777 | * the dummy one byte of data in iovec[0] if sending | |
1778 | * input data (in iovec[1]). We also must add 4 bytes | |
1779 | * in first iovec to allow for rfc1002 length field. | |
1780 | */ | |
1781 | ||
1782 | if (indatalen) { | |
1783 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
1784 | inc_rfc1001_len(req, indatalen - 1); | |
1785 | } else | |
1786 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1787 | ||
4a72dafa | 1788 | |
7fb8986e | 1789 | rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
1790 | cifs_small_buf_release(req); |
1791 | rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base; | |
4a72dafa | 1792 | |
9bf0c9cd | 1793 | if ((rc != 0) && (rc != -EINVAL)) { |
8e353106 | 1794 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
4a72dafa | 1795 | goto ioctl_exit; |
9bf0c9cd SF |
1796 | } else if (rc == -EINVAL) { |
1797 | if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) && | |
1798 | (opcode != FSCTL_SRV_COPYCHUNK)) { | |
8e353106 | 1799 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
9bf0c9cd SF |
1800 | goto ioctl_exit; |
1801 | } | |
4a72dafa SF |
1802 | } |
1803 | ||
1804 | /* check if caller wants to look at return data or just return rc */ | |
1805 | if ((plen == NULL) || (out_data == NULL)) | |
1806 | goto ioctl_exit; | |
1807 | ||
1808 | *plen = le32_to_cpu(rsp->OutputCount); | |
1809 | ||
1810 | /* We check for obvious errors in the output buffer length and offset */ | |
1811 | if (*plen == 0) | |
1812 | goto ioctl_exit; /* server returned no data */ | |
1813 | else if (*plen > 0xFF00) { | |
1814 | cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen); | |
1815 | *plen = 0; | |
1816 | rc = -EIO; | |
1817 | goto ioctl_exit; | |
1818 | } | |
1819 | ||
1820 | if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) { | |
1821 | cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen, | |
1822 | le32_to_cpu(rsp->OutputOffset)); | |
1823 | *plen = 0; | |
1824 | rc = -EIO; | |
1825 | goto ioctl_exit; | |
1826 | } | |
1827 | ||
1828 | *out_data = kmalloc(*plen, GFP_KERNEL); | |
1829 | if (*out_data == NULL) { | |
1830 | rc = -ENOMEM; | |
1831 | goto ioctl_exit; | |
1832 | } | |
1833 | ||
31473fc4 PS |
1834 | shdr = get_sync_hdr(rsp); |
1835 | memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen); | |
4a72dafa SF |
1836 | ioctl_exit: |
1837 | free_rsp_buf(resp_buftype, rsp); | |
1838 | return rc; | |
1839 | } | |
1840 | ||
64a5cfa6 SF |
1841 | /* |
1842 | * Individual callers to ioctl worker function follow | |
1843 | */ | |
1844 | ||
1845 | int | |
1846 | SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon, | |
1847 | u64 persistent_fid, u64 volatile_fid) | |
1848 | { | |
1849 | int rc; | |
64a5cfa6 SF |
1850 | struct compress_ioctl fsctl_input; |
1851 | char *ret_data = NULL; | |
1852 | ||
1853 | fsctl_input.CompressionState = | |
bc09d141 | 1854 | cpu_to_le16(COMPRESSION_FORMAT_DEFAULT); |
64a5cfa6 SF |
1855 | |
1856 | rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid, | |
1857 | FSCTL_SET_COMPRESSION, true /* is_fsctl */, | |
51146625 | 1858 | false /* use_ipc */, |
64a5cfa6 SF |
1859 | (char *)&fsctl_input /* data input */, |
1860 | 2 /* in data len */, &ret_data /* out data */, NULL); | |
1861 | ||
1862 | cifs_dbg(FYI, "set compression rc %d\n", rc); | |
64a5cfa6 SF |
1863 | |
1864 | return rc; | |
1865 | } | |
1866 | ||
2503a0db PS |
1867 | int |
1868 | SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, | |
1869 | u64 persistent_fid, u64 volatile_fid) | |
1870 | { | |
1871 | struct smb2_close_req *req; | |
1872 | struct smb2_close_rsp *rsp; | |
1873 | struct TCP_Server_Info *server; | |
1874 | struct cifs_ses *ses = tcon->ses; | |
1875 | struct kvec iov[1]; | |
da502f7d | 1876 | struct kvec rsp_iov; |
2503a0db PS |
1877 | int resp_buftype; |
1878 | int rc = 0; | |
7fb8986e | 1879 | int flags = 0; |
2503a0db | 1880 | |
f96637be | 1881 | cifs_dbg(FYI, "Close\n"); |
2503a0db PS |
1882 | |
1883 | if (ses && (ses->server)) | |
1884 | server = ses->server; | |
1885 | else | |
1886 | return -EIO; | |
1887 | ||
1888 | rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req); | |
1889 | if (rc) | |
1890 | return rc; | |
1891 | ||
7fb8986e PS |
1892 | if (encryption_required(tcon)) |
1893 | flags |= CIFS_TRANSFORM_REQ; | |
1894 | ||
2503a0db PS |
1895 | req->PersistentFileId = persistent_fid; |
1896 | req->VolatileFileId = volatile_fid; | |
1897 | ||
1898 | iov[0].iov_base = (char *)req; | |
1899 | /* 4 for rfc1002 length field */ | |
1900 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1901 | ||
7fb8986e | 1902 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
1903 | cifs_small_buf_release(req); |
1904 | rsp = (struct smb2_close_rsp *)rsp_iov.iov_base; | |
2503a0db PS |
1905 | |
1906 | if (rc != 0) { | |
d4a029d2 | 1907 | cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE); |
2503a0db PS |
1908 | goto close_exit; |
1909 | } | |
1910 | ||
2503a0db PS |
1911 | /* BB FIXME - decode close response, update inode for caching */ |
1912 | ||
1913 | close_exit: | |
1914 | free_rsp_buf(resp_buftype, rsp); | |
1915 | return rc; | |
1916 | } | |
be4cb9e3 PS |
1917 | |
1918 | static int | |
1919 | validate_buf(unsigned int offset, unsigned int buffer_length, | |
1920 | struct smb2_hdr *hdr, unsigned int min_buf_size) | |
1921 | ||
1922 | { | |
1923 | unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length); | |
1924 | char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr; | |
1925 | char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr; | |
1926 | char *end_of_buf = begin_of_buf + buffer_length; | |
1927 | ||
1928 | ||
1929 | if (buffer_length < min_buf_size) { | |
f96637be JP |
1930 | cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n", |
1931 | buffer_length, min_buf_size); | |
be4cb9e3 PS |
1932 | return -EINVAL; |
1933 | } | |
1934 | ||
1935 | /* check if beyond RFC1001 maximum length */ | |
1936 | if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) { | |
f96637be JP |
1937 | cifs_dbg(VFS, "buffer length %d or smb length %d too large\n", |
1938 | buffer_length, smb_len); | |
be4cb9e3 PS |
1939 | return -EINVAL; |
1940 | } | |
1941 | ||
1942 | if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) { | |
f96637be | 1943 | cifs_dbg(VFS, "illegal server response, bad offset to data\n"); |
be4cb9e3 PS |
1944 | return -EINVAL; |
1945 | } | |
1946 | ||
1947 | return 0; | |
1948 | } | |
1949 | ||
1950 | /* | |
1951 | * If SMB buffer fields are valid, copy into temporary buffer to hold result. | |
1952 | * Caller must free buffer. | |
1953 | */ | |
1954 | static int | |
1955 | validate_and_copy_buf(unsigned int offset, unsigned int buffer_length, | |
1956 | struct smb2_hdr *hdr, unsigned int minbufsize, | |
1957 | char *data) | |
1958 | ||
1959 | { | |
1960 | char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr; | |
1961 | int rc; | |
1962 | ||
1963 | if (!data) | |
1964 | return -EINVAL; | |
1965 | ||
1966 | rc = validate_buf(offset, buffer_length, hdr, minbufsize); | |
1967 | if (rc) | |
1968 | return rc; | |
1969 | ||
1970 | memcpy(data, begin_of_buf, buffer_length); | |
1971 | ||
1972 | return 0; | |
1973 | } | |
1974 | ||
f0df737e PS |
1975 | static int |
1976 | query_info(const unsigned int xid, struct cifs_tcon *tcon, | |
1977 | u64 persistent_fid, u64 volatile_fid, u8 info_class, | |
1978 | size_t output_len, size_t min_len, void *data) | |
be4cb9e3 PS |
1979 | { |
1980 | struct smb2_query_info_req *req; | |
1981 | struct smb2_query_info_rsp *rsp = NULL; | |
1982 | struct kvec iov[2]; | |
da502f7d | 1983 | struct kvec rsp_iov; |
be4cb9e3 PS |
1984 | int rc = 0; |
1985 | int resp_buftype; | |
1986 | struct TCP_Server_Info *server; | |
1987 | struct cifs_ses *ses = tcon->ses; | |
7fb8986e | 1988 | int flags = 0; |
be4cb9e3 | 1989 | |
f96637be | 1990 | cifs_dbg(FYI, "Query Info\n"); |
be4cb9e3 PS |
1991 | |
1992 | if (ses && (ses->server)) | |
1993 | server = ses->server; | |
1994 | else | |
1995 | return -EIO; | |
1996 | ||
1997 | rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req); | |
1998 | if (rc) | |
1999 | return rc; | |
2000 | ||
7fb8986e PS |
2001 | if (encryption_required(tcon)) |
2002 | flags |= CIFS_TRANSFORM_REQ; | |
2003 | ||
be4cb9e3 | 2004 | req->InfoType = SMB2_O_INFO_FILE; |
f0df737e | 2005 | req->FileInfoClass = info_class; |
be4cb9e3 PS |
2006 | req->PersistentFileId = persistent_fid; |
2007 | req->VolatileFileId = volatile_fid; | |
2008 | /* 4 for rfc1002 length field and 1 for Buffer */ | |
2009 | req->InputBufferOffset = | |
2010 | cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4); | |
f0df737e | 2011 | req->OutputBufferLength = cpu_to_le32(output_len); |
be4cb9e3 PS |
2012 | |
2013 | iov[0].iov_base = (char *)req; | |
2014 | /* 4 for rfc1002 length field */ | |
2015 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
2016 | ||
7fb8986e | 2017 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
2018 | cifs_small_buf_release(req); |
2019 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; | |
e5d04887 | 2020 | |
be4cb9e3 PS |
2021 | if (rc) { |
2022 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); | |
2023 | goto qinf_exit; | |
2024 | } | |
2025 | ||
be4cb9e3 PS |
2026 | rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset), |
2027 | le32_to_cpu(rsp->OutputBufferLength), | |
f0df737e | 2028 | &rsp->hdr, min_len, data); |
be4cb9e3 PS |
2029 | |
2030 | qinf_exit: | |
2031 | free_rsp_buf(resp_buftype, rsp); | |
2032 | return rc; | |
2033 | } | |
9094fad1 | 2034 | |
f0df737e PS |
2035 | int |
2036 | SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon, | |
2037 | u64 persistent_fid, u64 volatile_fid, | |
2038 | struct smb2_file_all_info *data) | |
2039 | { | |
2040 | return query_info(xid, tcon, persistent_fid, volatile_fid, | |
2041 | FILE_ALL_INFORMATION, | |
1bbe4997 | 2042 | sizeof(struct smb2_file_all_info) + PATH_MAX * 2, |
f0df737e PS |
2043 | sizeof(struct smb2_file_all_info), data); |
2044 | } | |
2045 | ||
2046 | int | |
2047 | SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon, | |
2048 | u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid) | |
2049 | { | |
2050 | return query_info(xid, tcon, persistent_fid, volatile_fid, | |
2051 | FILE_INTERNAL_INFORMATION, | |
2052 | sizeof(struct smb2_file_internal_info), | |
2053 | sizeof(struct smb2_file_internal_info), uniqueid); | |
2054 | } | |
2055 | ||
9094fad1 PS |
2056 | /* |
2057 | * This is a no-op for now. We're not really interested in the reply, but | |
2058 | * rather in the fact that the server sent one and that server->lstrp | |
2059 | * gets updated. | |
2060 | * | |
2061 | * FIXME: maybe we should consider checking that the reply matches request? | |
2062 | */ | |
2063 | static void | |
2064 | smb2_echo_callback(struct mid_q_entry *mid) | |
2065 | { | |
2066 | struct TCP_Server_Info *server = mid->callback_data; | |
31473fc4 | 2067 | struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf; |
9094fad1 PS |
2068 | unsigned int credits_received = 1; |
2069 | ||
2070 | if (mid->mid_state == MID_RESPONSE_RECEIVED) | |
31473fc4 | 2071 | credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest); |
9094fad1 | 2072 | |
5fb4e288 | 2073 | mutex_lock(&server->srv_mutex); |
9094fad1 | 2074 | DeleteMidQEntry(mid); |
5fb4e288 | 2075 | mutex_unlock(&server->srv_mutex); |
9094fad1 PS |
2076 | add_credits(server, credits_received, CIFS_ECHO_OP); |
2077 | } | |
2078 | ||
53e0e11e PS |
2079 | void smb2_reconnect_server(struct work_struct *work) |
2080 | { | |
2081 | struct TCP_Server_Info *server = container_of(work, | |
2082 | struct TCP_Server_Info, reconnect.work); | |
2083 | struct cifs_ses *ses; | |
2084 | struct cifs_tcon *tcon, *tcon2; | |
2085 | struct list_head tmp_list; | |
2086 | int tcon_exist = false; | |
2087 | ||
2088 | /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */ | |
2089 | mutex_lock(&server->reconnect_mutex); | |
2090 | ||
2091 | INIT_LIST_HEAD(&tmp_list); | |
2092 | cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n"); | |
2093 | ||
2094 | spin_lock(&cifs_tcp_ses_lock); | |
2095 | list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { | |
2096 | list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { | |
96a988ff | 2097 | if (tcon->need_reconnect || tcon->need_reopen_files) { |
53e0e11e PS |
2098 | tcon->tc_count++; |
2099 | list_add_tail(&tcon->rlist, &tmp_list); | |
2100 | tcon_exist = true; | |
2101 | } | |
2102 | } | |
2103 | } | |
2104 | /* | |
2105 | * Get the reference to server struct to be sure that the last call of | |
2106 | * cifs_put_tcon() in the loop below won't release the server pointer. | |
2107 | */ | |
2108 | if (tcon_exist) | |
2109 | server->srv_count++; | |
2110 | ||
2111 | spin_unlock(&cifs_tcp_ses_lock); | |
2112 | ||
2113 | list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) { | |
96a988ff PS |
2114 | if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon)) |
2115 | cifs_reopen_persistent_handles(tcon); | |
53e0e11e PS |
2116 | list_del_init(&tcon->rlist); |
2117 | cifs_put_tcon(tcon); | |
2118 | } | |
2119 | ||
2120 | cifs_dbg(FYI, "Reconnecting tcons finished\n"); | |
2121 | mutex_unlock(&server->reconnect_mutex); | |
2122 | ||
2123 | /* now we can safely release srv struct */ | |
2124 | if (tcon_exist) | |
2125 | cifs_put_tcp_session(server, 1); | |
2126 | } | |
2127 | ||
9094fad1 PS |
2128 | int |
2129 | SMB2_echo(struct TCP_Server_Info *server) | |
2130 | { | |
2131 | struct smb2_echo_req *req; | |
2132 | int rc = 0; | |
738f9de5 PS |
2133 | struct kvec iov[2]; |
2134 | struct smb_rqst rqst = { .rq_iov = iov, | |
2135 | .rq_nvec = 2 }; | |
9094fad1 | 2136 | |
f96637be | 2137 | cifs_dbg(FYI, "In echo request\n"); |
9094fad1 | 2138 | |
4fcd1813 | 2139 | if (server->tcpStatus == CifsNeedNegotiate) { |
53e0e11e PS |
2140 | /* No need to send echo on newly established connections */ |
2141 | queue_delayed_work(cifsiod_wq, &server->reconnect, 0); | |
2142 | return rc; | |
4fcd1813 SF |
2143 | } |
2144 | ||
9094fad1 PS |
2145 | rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req); |
2146 | if (rc) | |
2147 | return rc; | |
2148 | ||
31473fc4 | 2149 | req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1); |
9094fad1 | 2150 | |
9094fad1 | 2151 | /* 4 for rfc1002 length field */ |
738f9de5 PS |
2152 | iov[0].iov_len = 4; |
2153 | iov[0].iov_base = (char *)req; | |
2154 | iov[1].iov_len = get_rfc1002_length(req); | |
2155 | iov[1].iov_base = (char *)req + 4; | |
9094fad1 | 2156 | |
9b7c18a2 PS |
2157 | rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL, |
2158 | server, CIFS_ECHO_OP); | |
9094fad1 | 2159 | if (rc) |
f96637be | 2160 | cifs_dbg(FYI, "Echo request failed: %d\n", rc); |
9094fad1 PS |
2161 | |
2162 | cifs_small_buf_release(req); | |
2163 | return rc; | |
2164 | } | |
7a5cfb19 PS |
2165 | |
2166 | int | |
2167 | SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, | |
2168 | u64 volatile_fid) | |
2169 | { | |
2170 | struct smb2_flush_req *req; | |
2171 | struct TCP_Server_Info *server; | |
2172 | struct cifs_ses *ses = tcon->ses; | |
2173 | struct kvec iov[1]; | |
da502f7d | 2174 | struct kvec rsp_iov; |
7a5cfb19 PS |
2175 | int resp_buftype; |
2176 | int rc = 0; | |
7fb8986e | 2177 | int flags = 0; |
7a5cfb19 | 2178 | |
f96637be | 2179 | cifs_dbg(FYI, "Flush\n"); |
7a5cfb19 PS |
2180 | |
2181 | if (ses && (ses->server)) | |
2182 | server = ses->server; | |
2183 | else | |
2184 | return -EIO; | |
2185 | ||
2186 | rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req); | |
2187 | if (rc) | |
2188 | return rc; | |
2189 | ||
7fb8986e PS |
2190 | if (encryption_required(tcon)) |
2191 | flags |= CIFS_TRANSFORM_REQ; | |
2192 | ||
7a5cfb19 PS |
2193 | req->PersistentFileId = persistent_fid; |
2194 | req->VolatileFileId = volatile_fid; | |
2195 | ||
2196 | iov[0].iov_base = (char *)req; | |
2197 | /* 4 for rfc1002 length field */ | |
2198 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
2199 | ||
7fb8986e | 2200 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov); |
da502f7d | 2201 | cifs_small_buf_release(req); |
7a5cfb19 | 2202 | |
dfebe400 | 2203 | if (rc != 0) |
7a5cfb19 PS |
2204 | cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE); |
2205 | ||
da502f7d | 2206 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
7a5cfb19 PS |
2207 | return rc; |
2208 | } | |
09a4707e PS |
2209 | |
2210 | /* | |
2211 | * To form a chain of read requests, any read requests after the first should | |
2212 | * have the end_of_chain boolean set to true. | |
2213 | */ | |
2214 | static int | |
738f9de5 PS |
2215 | smb2_new_read_req(void **buf, unsigned int *total_len, |
2216 | struct cifs_io_parms *io_parms, unsigned int remaining_bytes, | |
2217 | int request_type) | |
09a4707e PS |
2218 | { |
2219 | int rc = -EACCES; | |
b8f57ee8 | 2220 | struct smb2_read_plain_req *req = NULL; |
31473fc4 | 2221 | struct smb2_sync_hdr *shdr; |
09a4707e | 2222 | |
b8f57ee8 PS |
2223 | rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req, |
2224 | total_len); | |
09a4707e PS |
2225 | if (rc) |
2226 | return rc; | |
2227 | if (io_parms->tcon->ses->server == NULL) | |
2228 | return -ECONNABORTED; | |
2229 | ||
b8f57ee8 | 2230 | shdr = &req->sync_hdr; |
31473fc4 | 2231 | shdr->ProcessId = cpu_to_le32(io_parms->pid); |
09a4707e PS |
2232 | |
2233 | req->PersistentFileId = io_parms->persistent_fid; | |
2234 | req->VolatileFileId = io_parms->volatile_fid; | |
2235 | req->ReadChannelInfoOffset = 0; /* reserved */ | |
2236 | req->ReadChannelInfoLength = 0; /* reserved */ | |
2237 | req->Channel = 0; /* reserved */ | |
2238 | req->MinimumCount = 0; | |
2239 | req->Length = cpu_to_le32(io_parms->length); | |
2240 | req->Offset = cpu_to_le64(io_parms->offset); | |
2241 | ||
2242 | if (request_type & CHAINED_REQUEST) { | |
2243 | if (!(request_type & END_OF_CHAIN)) { | |
b8f57ee8 PS |
2244 | /* next 8-byte aligned request */ |
2245 | *total_len = DIV_ROUND_UP(*total_len, 8) * 8; | |
2246 | shdr->NextCommand = cpu_to_le32(*total_len); | |
09a4707e | 2247 | } else /* END_OF_CHAIN */ |
31473fc4 | 2248 | shdr->NextCommand = 0; |
09a4707e | 2249 | if (request_type & RELATED_REQUEST) { |
31473fc4 | 2250 | shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS; |
09a4707e PS |
2251 | /* |
2252 | * Related requests use info from previous read request | |
2253 | * in chain. | |
2254 | */ | |
31473fc4 PS |
2255 | shdr->SessionId = 0xFFFFFFFF; |
2256 | shdr->TreeId = 0xFFFFFFFF; | |
09a4707e PS |
2257 | req->PersistentFileId = 0xFFFFFFFF; |
2258 | req->VolatileFileId = 0xFFFFFFFF; | |
2259 | } | |
2260 | } | |
2261 | if (remaining_bytes > io_parms->length) | |
2262 | req->RemainingBytes = cpu_to_le32(remaining_bytes); | |
2263 | else | |
2264 | req->RemainingBytes = 0; | |
2265 | ||
738f9de5 | 2266 | *buf = req; |
09a4707e PS |
2267 | return rc; |
2268 | } | |
2269 | ||
2270 | static void | |
2271 | smb2_readv_callback(struct mid_q_entry *mid) | |
2272 | { | |
2273 | struct cifs_readdata *rdata = mid->callback_data; | |
2274 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); | |
2275 | struct TCP_Server_Info *server = tcon->ses->server; | |
738f9de5 PS |
2276 | struct smb2_sync_hdr *shdr = |
2277 | (struct smb2_sync_hdr *)rdata->iov[1].iov_base; | |
09a4707e | 2278 | unsigned int credits_received = 1; |
738f9de5 PS |
2279 | struct smb_rqst rqst = { .rq_iov = rdata->iov, |
2280 | .rq_nvec = 2, | |
8321fec4 JL |
2281 | .rq_pages = rdata->pages, |
2282 | .rq_npages = rdata->nr_pages, | |
2283 | .rq_pagesz = rdata->pagesz, | |
2284 | .rq_tailsz = rdata->tailsz }; | |
09a4707e | 2285 | |
f96637be JP |
2286 | cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n", |
2287 | __func__, mid->mid, mid->mid_state, rdata->result, | |
2288 | rdata->bytes); | |
09a4707e PS |
2289 | |
2290 | switch (mid->mid_state) { | |
2291 | case MID_RESPONSE_RECEIVED: | |
31473fc4 | 2292 | credits_received = le16_to_cpu(shdr->CreditRequest); |
09a4707e | 2293 | /* result already set, check signature */ |
4326ed2f | 2294 | if (server->sign && !mid->decrypted) { |
3c1bf7e4 PS |
2295 | int rc; |
2296 | ||
0b688cfc | 2297 | rc = smb2_verify_signature(&rqst, server); |
3c1bf7e4 | 2298 | if (rc) |
f96637be JP |
2299 | cifs_dbg(VFS, "SMB signature verification returned error = %d\n", |
2300 | rc); | |
3c1bf7e4 | 2301 | } |
09a4707e | 2302 | /* FIXME: should this be counted toward the initiating task? */ |
34a54d61 PS |
2303 | task_io_account_read(rdata->got_bytes); |
2304 | cifs_stats_bytes_read(tcon, rdata->got_bytes); | |
09a4707e PS |
2305 | break; |
2306 | case MID_REQUEST_SUBMITTED: | |
2307 | case MID_RETRY_NEEDED: | |
2308 | rdata->result = -EAGAIN; | |
d913ed17 PS |
2309 | if (server->sign && rdata->got_bytes) |
2310 | /* reset bytes number since we can not check a sign */ | |
2311 | rdata->got_bytes = 0; | |
2312 | /* FIXME: should this be counted toward the initiating task? */ | |
2313 | task_io_account_read(rdata->got_bytes); | |
2314 | cifs_stats_bytes_read(tcon, rdata->got_bytes); | |
09a4707e PS |
2315 | break; |
2316 | default: | |
2317 | if (rdata->result != -ENODATA) | |
2318 | rdata->result = -EIO; | |
2319 | } | |
2320 | ||
2321 | if (rdata->result) | |
2322 | cifs_stats_fail_inc(tcon, SMB2_READ_HE); | |
2323 | ||
2324 | queue_work(cifsiod_wq, &rdata->work); | |
5fb4e288 | 2325 | mutex_lock(&server->srv_mutex); |
09a4707e | 2326 | DeleteMidQEntry(mid); |
5fb4e288 | 2327 | mutex_unlock(&server->srv_mutex); |
09a4707e PS |
2328 | add_credits(server, credits_received, 0); |
2329 | } | |
2330 | ||
738f9de5 | 2331 | /* smb2_async_readv - send an async read, and set up mid to handle result */ |
09a4707e PS |
2332 | int |
2333 | smb2_async_readv(struct cifs_readdata *rdata) | |
2334 | { | |
bed9da02 | 2335 | int rc, flags = 0; |
31473fc4 PS |
2336 | char *buf; |
2337 | struct smb2_sync_hdr *shdr; | |
09a4707e | 2338 | struct cifs_io_parms io_parms; |
738f9de5 PS |
2339 | struct smb_rqst rqst = { .rq_iov = rdata->iov, |
2340 | .rq_nvec = 2 }; | |
bed9da02 | 2341 | struct TCP_Server_Info *server; |
738f9de5 | 2342 | unsigned int total_len; |
b8f57ee8 | 2343 | __be32 req_len; |
09a4707e | 2344 | |
f96637be JP |
2345 | cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n", |
2346 | __func__, rdata->offset, rdata->bytes); | |
09a4707e PS |
2347 | |
2348 | io_parms.tcon = tlink_tcon(rdata->cfile->tlink); | |
2349 | io_parms.offset = rdata->offset; | |
2350 | io_parms.length = rdata->bytes; | |
2351 | io_parms.persistent_fid = rdata->cfile->fid.persistent_fid; | |
2352 | io_parms.volatile_fid = rdata->cfile->fid.volatile_fid; | |
2353 | io_parms.pid = rdata->pid; | |
bed9da02 PS |
2354 | |
2355 | server = io_parms.tcon->ses->server; | |
2356 | ||
738f9de5 | 2357 | rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0); |
bed9da02 PS |
2358 | if (rc) { |
2359 | if (rc == -EAGAIN && rdata->credits) { | |
2360 | /* credits was reset by reconnect */ | |
2361 | rdata->credits = 0; | |
2362 | /* reduce in_flight value since we won't send the req */ | |
2363 | spin_lock(&server->req_lock); | |
2364 | server->in_flight--; | |
2365 | spin_unlock(&server->req_lock); | |
2366 | } | |
09a4707e | 2367 | return rc; |
bed9da02 | 2368 | } |
09a4707e | 2369 | |
7fb8986e PS |
2370 | if (encryption_required(io_parms.tcon)) |
2371 | flags |= CIFS_TRANSFORM_REQ; | |
2372 | ||
b8f57ee8 PS |
2373 | req_len = cpu_to_be32(total_len); |
2374 | ||
2375 | rdata->iov[0].iov_base = &req_len; | |
2376 | rdata->iov[0].iov_len = sizeof(__be32); | |
2377 | rdata->iov[1].iov_base = buf; | |
2378 | rdata->iov[1].iov_len = total_len; | |
2379 | ||
2380 | shdr = (struct smb2_sync_hdr *)buf; | |
09a4707e | 2381 | |
bed9da02 | 2382 | if (rdata->credits) { |
31473fc4 | 2383 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes, |
bed9da02 | 2384 | SMB2_MAX_BUFFER_SIZE)); |
31473fc4 | 2385 | shdr->CreditRequest = shdr->CreditCharge; |
bed9da02 PS |
2386 | spin_lock(&server->req_lock); |
2387 | server->credits += rdata->credits - | |
31473fc4 | 2388 | le16_to_cpu(shdr->CreditCharge); |
bed9da02 PS |
2389 | spin_unlock(&server->req_lock); |
2390 | wake_up(&server->request_q); | |
7fb8986e | 2391 | flags |= CIFS_HAS_CREDITS; |
bed9da02 PS |
2392 | } |
2393 | ||
09a4707e | 2394 | kref_get(&rdata->refcount); |
fec344e3 | 2395 | rc = cifs_call_async(io_parms.tcon->ses->server, &rqst, |
09a4707e | 2396 | cifs_readv_receive, smb2_readv_callback, |
4326ed2f | 2397 | smb3_handle_read_data, rdata, flags); |
e5d04887 | 2398 | if (rc) { |
09a4707e | 2399 | kref_put(&rdata->refcount, cifs_readdata_release); |
e5d04887 PS |
2400 | cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE); |
2401 | } | |
09a4707e PS |
2402 | |
2403 | cifs_small_buf_release(buf); | |
2404 | return rc; | |
2405 | } | |
33319141 | 2406 | |
d8e05039 PS |
2407 | int |
2408 | SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, | |
2409 | unsigned int *nbytes, char **buf, int *buf_type) | |
2410 | { | |
2411 | int resp_buftype, rc = -EACCES; | |
b8f57ee8 | 2412 | struct smb2_read_plain_req *req = NULL; |
d8e05039 | 2413 | struct smb2_read_rsp *rsp = NULL; |
31473fc4 | 2414 | struct smb2_sync_hdr *shdr; |
b8f57ee8 | 2415 | struct kvec iov[2]; |
da502f7d | 2416 | struct kvec rsp_iov; |
738f9de5 | 2417 | unsigned int total_len; |
b8f57ee8 PS |
2418 | __be32 req_len; |
2419 | struct smb_rqst rqst = { .rq_iov = iov, | |
2420 | .rq_nvec = 2 }; | |
7fb8986e PS |
2421 | int flags = CIFS_LOG_ERROR; |
2422 | struct cifs_ses *ses = io_parms->tcon->ses; | |
d8e05039 PS |
2423 | |
2424 | *nbytes = 0; | |
738f9de5 | 2425 | rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0); |
d8e05039 PS |
2426 | if (rc) |
2427 | return rc; | |
2428 | ||
7fb8986e PS |
2429 | if (encryption_required(io_parms->tcon)) |
2430 | flags |= CIFS_TRANSFORM_REQ; | |
2431 | ||
b8f57ee8 | 2432 | req_len = cpu_to_be32(total_len); |
738f9de5 | 2433 | |
b8f57ee8 PS |
2434 | iov[0].iov_base = &req_len; |
2435 | iov[0].iov_len = sizeof(__be32); | |
2436 | iov[1].iov_base = req; | |
2437 | iov[1].iov_len = total_len; | |
2438 | ||
7fb8986e | 2439 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
b8f57ee8 | 2440 | cifs_small_buf_release(req); |
d8e05039 | 2441 | |
da502f7d | 2442 | rsp = (struct smb2_read_rsp *)rsp_iov.iov_base; |
31473fc4 | 2443 | shdr = get_sync_hdr(rsp); |
d8e05039 | 2444 | |
31473fc4 | 2445 | if (shdr->Status == STATUS_END_OF_FILE) { |
da502f7d | 2446 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
d8e05039 PS |
2447 | return 0; |
2448 | } | |
2449 | ||
2450 | if (rc) { | |
2451 | cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE); | |
f96637be | 2452 | cifs_dbg(VFS, "Send error in read = %d\n", rc); |
d8e05039 PS |
2453 | } else { |
2454 | *nbytes = le32_to_cpu(rsp->DataLength); | |
2455 | if ((*nbytes > CIFS_MAX_MSGSIZE) || | |
2456 | (*nbytes > io_parms->length)) { | |
f96637be JP |
2457 | cifs_dbg(FYI, "bad length %d for count %d\n", |
2458 | *nbytes, io_parms->length); | |
d8e05039 PS |
2459 | rc = -EIO; |
2460 | *nbytes = 0; | |
2461 | } | |
2462 | } | |
2463 | ||
2464 | if (*buf) { | |
31473fc4 | 2465 | memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes); |
da502f7d | 2466 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
d8e05039 | 2467 | } else if (resp_buftype != CIFS_NO_BUFFER) { |
da502f7d | 2468 | *buf = rsp_iov.iov_base; |
d8e05039 PS |
2469 | if (resp_buftype == CIFS_SMALL_BUFFER) |
2470 | *buf_type = CIFS_SMALL_BUFFER; | |
2471 | else if (resp_buftype == CIFS_LARGE_BUFFER) | |
2472 | *buf_type = CIFS_LARGE_BUFFER; | |
2473 | } | |
2474 | return rc; | |
2475 | } | |
2476 | ||
33319141 PS |
2477 | /* |
2478 | * Check the mid_state and signature on received buffer (if any), and queue the | |
2479 | * workqueue completion task. | |
2480 | */ | |
2481 | static void | |
2482 | smb2_writev_callback(struct mid_q_entry *mid) | |
2483 | { | |
2484 | struct cifs_writedata *wdata = mid->callback_data; | |
2485 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); | |
5fb4e288 | 2486 | struct TCP_Server_Info *server = tcon->ses->server; |
33319141 PS |
2487 | unsigned int written; |
2488 | struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf; | |
2489 | unsigned int credits_received = 1; | |
2490 | ||
2491 | switch (mid->mid_state) { | |
2492 | case MID_RESPONSE_RECEIVED: | |
31473fc4 | 2493 | credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest); |
33319141 PS |
2494 | wdata->result = smb2_check_receive(mid, tcon->ses->server, 0); |
2495 | if (wdata->result != 0) | |
2496 | break; | |
2497 | ||
2498 | written = le32_to_cpu(rsp->DataLength); | |
2499 | /* | |
2500 | * Mask off high 16 bits when bytes written as returned | |
2501 | * by the server is greater than bytes requested by the | |
2502 | * client. OS/2 servers are known to set incorrect | |
2503 | * CountHigh values. | |
2504 | */ | |
2505 | if (written > wdata->bytes) | |
2506 | written &= 0xFFFF; | |
2507 | ||
2508 | if (written < wdata->bytes) | |
2509 | wdata->result = -ENOSPC; | |
2510 | else | |
2511 | wdata->bytes = written; | |
2512 | break; | |
2513 | case MID_REQUEST_SUBMITTED: | |
2514 | case MID_RETRY_NEEDED: | |
2515 | wdata->result = -EAGAIN; | |
2516 | break; | |
2517 | default: | |
2518 | wdata->result = -EIO; | |
2519 | break; | |
2520 | } | |
2521 | ||
2522 | if (wdata->result) | |
2523 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); | |
2524 | ||
2525 | queue_work(cifsiod_wq, &wdata->work); | |
5fb4e288 | 2526 | mutex_lock(&server->srv_mutex); |
33319141 | 2527 | DeleteMidQEntry(mid); |
5fb4e288 | 2528 | mutex_unlock(&server->srv_mutex); |
33319141 PS |
2529 | add_credits(tcon->ses->server, credits_received, 0); |
2530 | } | |
2531 | ||
2532 | /* smb2_async_writev - send an async write, and set up mid to handle result */ | |
2533 | int | |
4a5c80d7 SF |
2534 | smb2_async_writev(struct cifs_writedata *wdata, |
2535 | void (*release)(struct kref *kref)) | |
33319141 | 2536 | { |
cb7e9eab | 2537 | int rc = -EACCES, flags = 0; |
33319141 | 2538 | struct smb2_write_req *req = NULL; |
31473fc4 | 2539 | struct smb2_sync_hdr *shdr; |
33319141 | 2540 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
cb7e9eab | 2541 | struct TCP_Server_Info *server = tcon->ses->server; |
738f9de5 PS |
2542 | struct kvec iov[2]; |
2543 | struct smb_rqst rqst = { }; | |
33319141 PS |
2544 | |
2545 | rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req); | |
cb7e9eab PS |
2546 | if (rc) { |
2547 | if (rc == -EAGAIN && wdata->credits) { | |
2548 | /* credits was reset by reconnect */ | |
2549 | wdata->credits = 0; | |
2550 | /* reduce in_flight value since we won't send the req */ | |
2551 | spin_lock(&server->req_lock); | |
2552 | server->in_flight--; | |
2553 | spin_unlock(&server->req_lock); | |
2554 | } | |
33319141 | 2555 | goto async_writev_out; |
cb7e9eab | 2556 | } |
33319141 | 2557 | |
7fb8986e PS |
2558 | if (encryption_required(tcon)) |
2559 | flags |= CIFS_TRANSFORM_REQ; | |
2560 | ||
31473fc4 PS |
2561 | shdr = get_sync_hdr(req); |
2562 | shdr->ProcessId = cpu_to_le32(wdata->cfile->pid); | |
33319141 PS |
2563 | |
2564 | req->PersistentFileId = wdata->cfile->fid.persistent_fid; | |
2565 | req->VolatileFileId = wdata->cfile->fid.volatile_fid; | |
2566 | req->WriteChannelInfoOffset = 0; | |
2567 | req->WriteChannelInfoLength = 0; | |
2568 | req->Channel = 0; | |
2569 | req->Offset = cpu_to_le64(wdata->offset); | |
2570 | /* 4 for rfc1002 length field */ | |
2571 | req->DataOffset = cpu_to_le16( | |
2572 | offsetof(struct smb2_write_req, Buffer) - 4); | |
2573 | req->RemainingBytes = 0; | |
2574 | ||
2575 | /* 4 for rfc1002 length field and 1 for Buffer */ | |
738f9de5 PS |
2576 | iov[0].iov_len = 4; |
2577 | iov[0].iov_base = req; | |
2578 | iov[1].iov_len = get_rfc1002_length(req) - 1; | |
2579 | iov[1].iov_base = (char *)req + 4; | |
33319141 | 2580 | |
738f9de5 PS |
2581 | rqst.rq_iov = iov; |
2582 | rqst.rq_nvec = 2; | |
eddb079d JL |
2583 | rqst.rq_pages = wdata->pages; |
2584 | rqst.rq_npages = wdata->nr_pages; | |
2585 | rqst.rq_pagesz = wdata->pagesz; | |
2586 | rqst.rq_tailsz = wdata->tailsz; | |
33319141 | 2587 | |
f96637be JP |
2588 | cifs_dbg(FYI, "async write at %llu %u bytes\n", |
2589 | wdata->offset, wdata->bytes); | |
33319141 PS |
2590 | |
2591 | req->Length = cpu_to_le32(wdata->bytes); | |
2592 | ||
2593 | inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */); | |
2594 | ||
cb7e9eab | 2595 | if (wdata->credits) { |
31473fc4 | 2596 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes, |
cb7e9eab | 2597 | SMB2_MAX_BUFFER_SIZE)); |
31473fc4 | 2598 | shdr->CreditRequest = shdr->CreditCharge; |
cb7e9eab PS |
2599 | spin_lock(&server->req_lock); |
2600 | server->credits += wdata->credits - | |
31473fc4 | 2601 | le16_to_cpu(shdr->CreditCharge); |
cb7e9eab PS |
2602 | spin_unlock(&server->req_lock); |
2603 | wake_up(&server->request_q); | |
7fb8986e | 2604 | flags |= CIFS_HAS_CREDITS; |
cb7e9eab PS |
2605 | } |
2606 | ||
33319141 | 2607 | kref_get(&wdata->refcount); |
9b7c18a2 PS |
2608 | rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL, |
2609 | wdata, flags); | |
33319141 | 2610 | |
e5d04887 | 2611 | if (rc) { |
4a5c80d7 | 2612 | kref_put(&wdata->refcount, release); |
e5d04887 PS |
2613 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); |
2614 | } | |
33319141 | 2615 | |
33319141 PS |
2616 | async_writev_out: |
2617 | cifs_small_buf_release(req); | |
33319141 PS |
2618 | return rc; |
2619 | } | |
009d3443 PS |
2620 | |
2621 | /* | |
2622 | * SMB2_write function gets iov pointer to kvec array with n_vec as a length. | |
2623 | * The length field from io_parms must be at least 1 and indicates a number of | |
2624 | * elements with data to write that begins with position 1 in iov array. All | |
2625 | * data length is specified by count. | |
2626 | */ | |
2627 | int | |
2628 | SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, | |
2629 | unsigned int *nbytes, struct kvec *iov, int n_vec) | |
2630 | { | |
2631 | int rc = 0; | |
2632 | struct smb2_write_req *req = NULL; | |
2633 | struct smb2_write_rsp *rsp = NULL; | |
2634 | int resp_buftype; | |
da502f7d | 2635 | struct kvec rsp_iov; |
7fb8986e | 2636 | int flags = 0; |
da502f7d | 2637 | |
009d3443 PS |
2638 | *nbytes = 0; |
2639 | ||
2640 | if (n_vec < 1) | |
2641 | return rc; | |
2642 | ||
2643 | rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req); | |
2644 | if (rc) | |
2645 | return rc; | |
2646 | ||
2647 | if (io_parms->tcon->ses->server == NULL) | |
2648 | return -ECONNABORTED; | |
2649 | ||
7fb8986e PS |
2650 | if (encryption_required(io_parms->tcon)) |
2651 | flags |= CIFS_TRANSFORM_REQ; | |
2652 | ||
31473fc4 | 2653 | req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid); |
009d3443 PS |
2654 | |
2655 | req->PersistentFileId = io_parms->persistent_fid; | |
2656 | req->VolatileFileId = io_parms->volatile_fid; | |
2657 | req->WriteChannelInfoOffset = 0; | |
2658 | req->WriteChannelInfoLength = 0; | |
2659 | req->Channel = 0; | |
2660 | req->Length = cpu_to_le32(io_parms->length); | |
2661 | req->Offset = cpu_to_le64(io_parms->offset); | |
2662 | /* 4 for rfc1002 length field */ | |
2663 | req->DataOffset = cpu_to_le16( | |
2664 | offsetof(struct smb2_write_req, Buffer) - 4); | |
2665 | req->RemainingBytes = 0; | |
2666 | ||
2667 | iov[0].iov_base = (char *)req; | |
2668 | /* 4 for rfc1002 length field and 1 for Buffer */ | |
2669 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
2670 | ||
2671 | /* length of entire message including data to be written */ | |
2672 | inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */); | |
2673 | ||
2674 | rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1, | |
7fb8986e | 2675 | &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
2676 | cifs_small_buf_release(req); |
2677 | rsp = (struct smb2_write_rsp *)rsp_iov.iov_base; | |
009d3443 PS |
2678 | |
2679 | if (rc) { | |
2680 | cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE); | |
f96637be | 2681 | cifs_dbg(VFS, "Send error in write = %d\n", rc); |
e5d04887 | 2682 | } else |
009d3443 | 2683 | *nbytes = le32_to_cpu(rsp->DataLength); |
e5d04887 PS |
2684 | |
2685 | free_rsp_buf(resp_buftype, rsp); | |
009d3443 PS |
2686 | return rc; |
2687 | } | |
35143eb5 | 2688 | |
d324f08d PS |
2689 | static unsigned int |
2690 | num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size) | |
2691 | { | |
2692 | int len; | |
2693 | unsigned int entrycount = 0; | |
2694 | unsigned int next_offset = 0; | |
2695 | FILE_DIRECTORY_INFO *entryptr; | |
2696 | ||
2697 | if (bufstart == NULL) | |
2698 | return 0; | |
2699 | ||
2700 | entryptr = (FILE_DIRECTORY_INFO *)bufstart; | |
2701 | ||
2702 | while (1) { | |
2703 | entryptr = (FILE_DIRECTORY_INFO *) | |
2704 | ((char *)entryptr + next_offset); | |
2705 | ||
2706 | if ((char *)entryptr + size > end_of_buf) { | |
f96637be | 2707 | cifs_dbg(VFS, "malformed search entry would overflow\n"); |
d324f08d PS |
2708 | break; |
2709 | } | |
2710 | ||
2711 | len = le32_to_cpu(entryptr->FileNameLength); | |
2712 | if ((char *)entryptr + len + size > end_of_buf) { | |
f96637be JP |
2713 | cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n", |
2714 | end_of_buf); | |
d324f08d PS |
2715 | break; |
2716 | } | |
2717 | ||
2718 | *lastentry = (char *)entryptr; | |
2719 | entrycount++; | |
2720 | ||
2721 | next_offset = le32_to_cpu(entryptr->NextEntryOffset); | |
2722 | if (!next_offset) | |
2723 | break; | |
2724 | } | |
2725 | ||
2726 | return entrycount; | |
2727 | } | |
2728 | ||
2729 | /* | |
2730 | * Readdir/FindFirst | |
2731 | */ | |
2732 | int | |
2733 | SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, | |
2734 | u64 persistent_fid, u64 volatile_fid, int index, | |
2735 | struct cifs_search_info *srch_inf) | |
2736 | { | |
2737 | struct smb2_query_directory_req *req; | |
2738 | struct smb2_query_directory_rsp *rsp = NULL; | |
2739 | struct kvec iov[2]; | |
da502f7d | 2740 | struct kvec rsp_iov; |
d324f08d PS |
2741 | int rc = 0; |
2742 | int len; | |
75fdfc84 | 2743 | int resp_buftype = CIFS_NO_BUFFER; |
d324f08d PS |
2744 | unsigned char *bufptr; |
2745 | struct TCP_Server_Info *server; | |
2746 | struct cifs_ses *ses = tcon->ses; | |
2747 | __le16 asteriks = cpu_to_le16('*'); | |
2748 | char *end_of_smb; | |
2749 | unsigned int output_size = CIFSMaxBufSize; | |
2750 | size_t info_buf_size; | |
7fb8986e | 2751 | int flags = 0; |
d324f08d PS |
2752 | |
2753 | if (ses && (ses->server)) | |
2754 | server = ses->server; | |
2755 | else | |
2756 | return -EIO; | |
2757 | ||
2758 | rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req); | |
2759 | if (rc) | |
2760 | return rc; | |
2761 | ||
7fb8986e PS |
2762 | if (encryption_required(tcon)) |
2763 | flags |= CIFS_TRANSFORM_REQ; | |
2764 | ||
d324f08d PS |
2765 | switch (srch_inf->info_level) { |
2766 | case SMB_FIND_FILE_DIRECTORY_INFO: | |
2767 | req->FileInformationClass = FILE_DIRECTORY_INFORMATION; | |
2768 | info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1; | |
2769 | break; | |
2770 | case SMB_FIND_FILE_ID_FULL_DIR_INFO: | |
2771 | req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION; | |
2772 | info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1; | |
2773 | break; | |
2774 | default: | |
f96637be JP |
2775 | cifs_dbg(VFS, "info level %u isn't supported\n", |
2776 | srch_inf->info_level); | |
d324f08d PS |
2777 | rc = -EINVAL; |
2778 | goto qdir_exit; | |
2779 | } | |
2780 | ||
2781 | req->FileIndex = cpu_to_le32(index); | |
2782 | req->PersistentFileId = persistent_fid; | |
2783 | req->VolatileFileId = volatile_fid; | |
2784 | ||
2785 | len = 0x2; | |
2786 | bufptr = req->Buffer; | |
2787 | memcpy(bufptr, &asteriks, len); | |
2788 | ||
2789 | req->FileNameOffset = | |
2790 | cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4); | |
2791 | req->FileNameLength = cpu_to_le16(len); | |
2792 | /* | |
2793 | * BB could be 30 bytes or so longer if we used SMB2 specific | |
2794 | * buffer lengths, but this is safe and close enough. | |
2795 | */ | |
2796 | output_size = min_t(unsigned int, output_size, server->maxBuf); | |
2797 | output_size = min_t(unsigned int, output_size, 2 << 15); | |
2798 | req->OutputBufferLength = cpu_to_le32(output_size); | |
2799 | ||
2800 | iov[0].iov_base = (char *)req; | |
2801 | /* 4 for RFC1001 length and 1 for Buffer */ | |
2802 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
2803 | ||
2804 | iov[1].iov_base = (char *)(req->Buffer); | |
2805 | iov[1].iov_len = len; | |
2806 | ||
2807 | inc_rfc1001_len(req, len - 1 /* Buffer */); | |
2808 | ||
7fb8986e | 2809 | rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
2810 | cifs_small_buf_release(req); |
2811 | rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base; | |
e5d04887 | 2812 | |
d324f08d | 2813 | if (rc) { |
31473fc4 PS |
2814 | if (rc == -ENODATA && |
2815 | rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) { | |
52755808 PS |
2816 | srch_inf->endOfSearch = true; |
2817 | rc = 0; | |
2818 | } | |
d324f08d PS |
2819 | cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE); |
2820 | goto qdir_exit; | |
2821 | } | |
d324f08d PS |
2822 | |
2823 | rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset), | |
2824 | le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr, | |
2825 | info_buf_size); | |
2826 | if (rc) | |
2827 | goto qdir_exit; | |
2828 | ||
2829 | srch_inf->unicode = true; | |
2830 | ||
2831 | if (srch_inf->ntwrk_buf_start) { | |
2832 | if (srch_inf->smallBuf) | |
2833 | cifs_small_buf_release(srch_inf->ntwrk_buf_start); | |
2834 | else | |
2835 | cifs_buf_release(srch_inf->ntwrk_buf_start); | |
2836 | } | |
2837 | srch_inf->ntwrk_buf_start = (char *)rsp; | |
2838 | srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ + | |
2839 | (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset); | |
2840 | /* 4 for rfc1002 length field */ | |
2841 | end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr; | |
2842 | srch_inf->entries_in_buffer = | |
2843 | num_entries(srch_inf->srch_entries_start, end_of_smb, | |
2844 | &srch_inf->last_entry, info_buf_size); | |
2845 | srch_inf->index_of_last_entry += srch_inf->entries_in_buffer; | |
f96637be JP |
2846 | cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n", |
2847 | srch_inf->entries_in_buffer, srch_inf->index_of_last_entry, | |
2848 | srch_inf->srch_entries_start, srch_inf->last_entry); | |
d324f08d PS |
2849 | if (resp_buftype == CIFS_LARGE_BUFFER) |
2850 | srch_inf->smallBuf = false; | |
2851 | else if (resp_buftype == CIFS_SMALL_BUFFER) | |
2852 | srch_inf->smallBuf = true; | |
2853 | else | |
f96637be | 2854 | cifs_dbg(VFS, "illegal search buffer type\n"); |
d324f08d | 2855 | |
d324f08d PS |
2856 | return rc; |
2857 | ||
2858 | qdir_exit: | |
2859 | free_rsp_buf(resp_buftype, rsp); | |
2860 | return rc; | |
2861 | } | |
2862 | ||
35143eb5 PS |
2863 | static int |
2864 | send_set_info(const unsigned int xid, struct cifs_tcon *tcon, | |
c839ff24 | 2865 | u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class, |
35143eb5 PS |
2866 | unsigned int num, void **data, unsigned int *size) |
2867 | { | |
2868 | struct smb2_set_info_req *req; | |
2869 | struct smb2_set_info_rsp *rsp = NULL; | |
2870 | struct kvec *iov; | |
da502f7d | 2871 | struct kvec rsp_iov; |
35143eb5 PS |
2872 | int rc = 0; |
2873 | int resp_buftype; | |
2874 | unsigned int i; | |
2875 | struct TCP_Server_Info *server; | |
2876 | struct cifs_ses *ses = tcon->ses; | |
7fb8986e | 2877 | int flags = 0; |
35143eb5 PS |
2878 | |
2879 | if (ses && (ses->server)) | |
2880 | server = ses->server; | |
2881 | else | |
2882 | return -EIO; | |
2883 | ||
2884 | if (!num) | |
2885 | return -EINVAL; | |
2886 | ||
2887 | iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL); | |
2888 | if (!iov) | |
2889 | return -ENOMEM; | |
2890 | ||
2891 | rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req); | |
2892 | if (rc) { | |
2893 | kfree(iov); | |
2894 | return rc; | |
2895 | } | |
2896 | ||
7fb8986e PS |
2897 | if (encryption_required(tcon)) |
2898 | flags |= CIFS_TRANSFORM_REQ; | |
2899 | ||
31473fc4 | 2900 | req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid); |
c839ff24 | 2901 | |
35143eb5 PS |
2902 | req->InfoType = SMB2_O_INFO_FILE; |
2903 | req->FileInfoClass = info_class; | |
2904 | req->PersistentFileId = persistent_fid; | |
2905 | req->VolatileFileId = volatile_fid; | |
2906 | ||
2907 | /* 4 for RFC1001 length and 1 for Buffer */ | |
2908 | req->BufferOffset = | |
2909 | cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4); | |
2910 | req->BufferLength = cpu_to_le32(*size); | |
2911 | ||
2912 | inc_rfc1001_len(req, *size - 1 /* Buffer */); | |
2913 | ||
2914 | memcpy(req->Buffer, *data, *size); | |
2915 | ||
2916 | iov[0].iov_base = (char *)req; | |
2917 | /* 4 for RFC1001 length */ | |
2918 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
2919 | ||
2920 | for (i = 1; i < num; i++) { | |
2921 | inc_rfc1001_len(req, size[i]); | |
2922 | le32_add_cpu(&req->BufferLength, size[i]); | |
2923 | iov[i].iov_base = (char *)data[i]; | |
2924 | iov[i].iov_len = size[i]; | |
2925 | } | |
2926 | ||
7fb8986e | 2927 | rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
2928 | cifs_small_buf_release(req); |
2929 | rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base; | |
35143eb5 | 2930 | |
7d3fb24b | 2931 | if (rc != 0) |
35143eb5 | 2932 | cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE); |
7d3fb24b | 2933 | |
35143eb5 PS |
2934 | free_rsp_buf(resp_buftype, rsp); |
2935 | kfree(iov); | |
2936 | return rc; | |
2937 | } | |
2938 | ||
2939 | int | |
2940 | SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon, | |
2941 | u64 persistent_fid, u64 volatile_fid, __le16 *target_file) | |
2942 | { | |
2943 | struct smb2_file_rename_info info; | |
2944 | void **data; | |
2945 | unsigned int size[2]; | |
2946 | int rc; | |
2947 | int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX)); | |
2948 | ||
2949 | data = kmalloc(sizeof(void *) * 2, GFP_KERNEL); | |
2950 | if (!data) | |
2951 | return -ENOMEM; | |
2952 | ||
2953 | info.ReplaceIfExists = 1; /* 1 = replace existing target with new */ | |
2954 | /* 0 = fail if target already exists */ | |
2955 | info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */ | |
2956 | info.FileNameLength = cpu_to_le32(len); | |
2957 | ||
2958 | data[0] = &info; | |
2959 | size[0] = sizeof(struct smb2_file_rename_info); | |
2960 | ||
2961 | data[1] = target_file; | |
2962 | size[1] = len + 2 /* null */; | |
2963 | ||
2964 | rc = send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
c839ff24 PS |
2965 | current->tgid, FILE_RENAME_INFORMATION, 2, data, |
2966 | size); | |
35143eb5 PS |
2967 | kfree(data); |
2968 | return rc; | |
2969 | } | |
568798cc | 2970 | |
897fba11 SF |
2971 | int |
2972 | SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, | |
2973 | u64 persistent_fid, u64 volatile_fid) | |
2974 | { | |
2975 | __u8 delete_pending = 1; | |
2976 | void *data; | |
2977 | unsigned int size; | |
2978 | ||
2979 | data = &delete_pending; | |
2980 | size = 1; /* sizeof __u8 */ | |
2981 | ||
2982 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
2983 | current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data, | |
2984 | &size); | |
2985 | } | |
2986 | ||
568798cc PS |
2987 | int |
2988 | SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon, | |
2989 | u64 persistent_fid, u64 volatile_fid, __le16 *target_file) | |
2990 | { | |
2991 | struct smb2_file_link_info info; | |
2992 | void **data; | |
2993 | unsigned int size[2]; | |
2994 | int rc; | |
2995 | int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX)); | |
2996 | ||
2997 | data = kmalloc(sizeof(void *) * 2, GFP_KERNEL); | |
2998 | if (!data) | |
2999 | return -ENOMEM; | |
3000 | ||
3001 | info.ReplaceIfExists = 0; /* 1 = replace existing link with new */ | |
3002 | /* 0 = fail if link already exists */ | |
3003 | info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */ | |
3004 | info.FileNameLength = cpu_to_le32(len); | |
3005 | ||
3006 | data[0] = &info; | |
3007 | size[0] = sizeof(struct smb2_file_link_info); | |
3008 | ||
3009 | data[1] = target_file; | |
3010 | size[1] = len + 2 /* null */; | |
3011 | ||
3012 | rc = send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
c839ff24 | 3013 | current->tgid, FILE_LINK_INFORMATION, 2, data, size); |
568798cc PS |
3014 | kfree(data); |
3015 | return rc; | |
3016 | } | |
c839ff24 PS |
3017 | |
3018 | int | |
3019 | SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, | |
f29ebb47 | 3020 | u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc) |
c839ff24 PS |
3021 | { |
3022 | struct smb2_file_eof_info info; | |
3023 | void *data; | |
3024 | unsigned int size; | |
3025 | ||
3026 | info.EndOfFile = *eof; | |
3027 | ||
3028 | data = &info; | |
3029 | size = sizeof(struct smb2_file_eof_info); | |
3030 | ||
f29ebb47 SF |
3031 | if (is_falloc) |
3032 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
3033 | pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size); | |
3034 | else | |
3035 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
3036 | pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size); | |
c839ff24 | 3037 | } |
1feeaac7 PS |
3038 | |
3039 | int | |
3040 | SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon, | |
3041 | u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf) | |
3042 | { | |
3043 | unsigned int size; | |
3044 | size = sizeof(FILE_BASIC_INFO); | |
3045 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
3046 | current->tgid, FILE_BASIC_INFORMATION, 1, | |
3047 | (void **)&buf, &size); | |
3048 | } | |
983c88a4 PS |
3049 | |
3050 | int | |
3051 | SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, | |
3052 | const u64 persistent_fid, const u64 volatile_fid, | |
3053 | __u8 oplock_level) | |
3054 | { | |
3055 | int rc; | |
3056 | struct smb2_oplock_break *req = NULL; | |
7fb8986e | 3057 | int flags = CIFS_OBREAK_OP; |
983c88a4 | 3058 | |
f96637be | 3059 | cifs_dbg(FYI, "SMB2_oplock_break\n"); |
983c88a4 | 3060 | rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req); |
983c88a4 PS |
3061 | if (rc) |
3062 | return rc; | |
3063 | ||
7fb8986e PS |
3064 | if (encryption_required(tcon)) |
3065 | flags |= CIFS_TRANSFORM_REQ; | |
3066 | ||
983c88a4 PS |
3067 | req->VolatileFid = volatile_fid; |
3068 | req->PersistentFid = persistent_fid; | |
3069 | req->OplockLevel = oplock_level; | |
31473fc4 | 3070 | req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1); |
983c88a4 | 3071 | |
7fb8986e | 3072 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags); |
da502f7d | 3073 | cifs_small_buf_release(req); |
983c88a4 PS |
3074 | |
3075 | if (rc) { | |
3076 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); | |
f96637be | 3077 | cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc); |
983c88a4 PS |
3078 | } |
3079 | ||
3080 | return rc; | |
3081 | } | |
6fc05c25 PS |
3082 | |
3083 | static void | |
3084 | copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf, | |
3085 | struct kstatfs *kst) | |
3086 | { | |
3087 | kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) * | |
3088 | le32_to_cpu(pfs_inf->SectorsPerAllocationUnit); | |
3089 | kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits); | |
3090 | kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits); | |
3091 | kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits); | |
3092 | return; | |
3093 | } | |
3094 | ||
3095 | static int | |
3096 | build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level, | |
3097 | int outbuf_len, u64 persistent_fid, u64 volatile_fid) | |
3098 | { | |
3099 | int rc; | |
3100 | struct smb2_query_info_req *req; | |
3101 | ||
f96637be | 3102 | cifs_dbg(FYI, "Query FSInfo level %d\n", level); |
6fc05c25 PS |
3103 | |
3104 | if ((tcon->ses == NULL) || (tcon->ses->server == NULL)) | |
3105 | return -EIO; | |
3106 | ||
3107 | rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req); | |
3108 | if (rc) | |
3109 | return rc; | |
3110 | ||
3111 | req->InfoType = SMB2_O_INFO_FILESYSTEM; | |
3112 | req->FileInfoClass = level; | |
3113 | req->PersistentFileId = persistent_fid; | |
3114 | req->VolatileFileId = volatile_fid; | |
3115 | /* 4 for rfc1002 length field and 1 for pad */ | |
3116 | req->InputBufferOffset = | |
3117 | cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4); | |
3118 | req->OutputBufferLength = cpu_to_le32( | |
3119 | outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4); | |
3120 | ||
3121 | iov->iov_base = (char *)req; | |
3122 | /* 4 for rfc1002 length field */ | |
3123 | iov->iov_len = get_rfc1002_length(req) + 4; | |
3124 | return 0; | |
3125 | } | |
3126 | ||
3127 | int | |
3128 | SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon, | |
3129 | u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) | |
3130 | { | |
3131 | struct smb2_query_info_rsp *rsp = NULL; | |
3132 | struct kvec iov; | |
da502f7d | 3133 | struct kvec rsp_iov; |
6fc05c25 PS |
3134 | int rc = 0; |
3135 | int resp_buftype; | |
3136 | struct cifs_ses *ses = tcon->ses; | |
3137 | struct smb2_fs_full_size_info *info = NULL; | |
7fb8986e | 3138 | int flags = 0; |
6fc05c25 PS |
3139 | |
3140 | rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION, | |
3141 | sizeof(struct smb2_fs_full_size_info), | |
3142 | persistent_fid, volatile_fid); | |
3143 | if (rc) | |
3144 | return rc; | |
3145 | ||
7fb8986e PS |
3146 | if (encryption_required(tcon)) |
3147 | flags |= CIFS_TRANSFORM_REQ; | |
3148 | ||
3149 | rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov); | |
da502f7d | 3150 | cifs_small_buf_release(iov.iov_base); |
6fc05c25 PS |
3151 | if (rc) { |
3152 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); | |
34f62640 | 3153 | goto qfsinf_exit; |
6fc05c25 | 3154 | } |
da502f7d | 3155 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
6fc05c25 PS |
3156 | |
3157 | info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ + | |
3158 | le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr); | |
3159 | rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset), | |
3160 | le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr, | |
3161 | sizeof(struct smb2_fs_full_size_info)); | |
3162 | if (!rc) | |
3163 | copy_fs_info_to_kstatfs(info, fsdata); | |
3164 | ||
34f62640 | 3165 | qfsinf_exit: |
da502f7d | 3166 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
34f62640 SF |
3167 | return rc; |
3168 | } | |
3169 | ||
3170 | int | |
3171 | SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, | |
2167114c | 3172 | u64 persistent_fid, u64 volatile_fid, int level) |
34f62640 SF |
3173 | { |
3174 | struct smb2_query_info_rsp *rsp = NULL; | |
3175 | struct kvec iov; | |
da502f7d | 3176 | struct kvec rsp_iov; |
34f62640 | 3177 | int rc = 0; |
2167114c | 3178 | int resp_buftype, max_len, min_len; |
34f62640 SF |
3179 | struct cifs_ses *ses = tcon->ses; |
3180 | unsigned int rsp_len, offset; | |
7fb8986e | 3181 | int flags = 0; |
34f62640 | 3182 | |
2167114c SF |
3183 | if (level == FS_DEVICE_INFORMATION) { |
3184 | max_len = sizeof(FILE_SYSTEM_DEVICE_INFO); | |
3185 | min_len = sizeof(FILE_SYSTEM_DEVICE_INFO); | |
3186 | } else if (level == FS_ATTRIBUTE_INFORMATION) { | |
3187 | max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO); | |
3188 | min_len = MIN_FS_ATTR_INFO_SIZE; | |
af6a12ea SF |
3189 | } else if (level == FS_SECTOR_SIZE_INFORMATION) { |
3190 | max_len = sizeof(struct smb3_fs_ss_info); | |
3191 | min_len = sizeof(struct smb3_fs_ss_info); | |
2167114c | 3192 | } else { |
af6a12ea | 3193 | cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level); |
2167114c SF |
3194 | return -EINVAL; |
3195 | } | |
3196 | ||
3197 | rc = build_qfs_info_req(&iov, tcon, level, max_len, | |
34f62640 SF |
3198 | persistent_fid, volatile_fid); |
3199 | if (rc) | |
3200 | return rc; | |
3201 | ||
7fb8986e PS |
3202 | if (encryption_required(tcon)) |
3203 | flags |= CIFS_TRANSFORM_REQ; | |
3204 | ||
3205 | rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov); | |
da502f7d | 3206 | cifs_small_buf_release(iov.iov_base); |
34f62640 SF |
3207 | if (rc) { |
3208 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); | |
3209 | goto qfsattr_exit; | |
3210 | } | |
da502f7d | 3211 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
34f62640 SF |
3212 | |
3213 | rsp_len = le32_to_cpu(rsp->OutputBufferLength); | |
3214 | offset = le16_to_cpu(rsp->OutputBufferOffset); | |
2167114c SF |
3215 | rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len); |
3216 | if (rc) | |
3217 | goto qfsattr_exit; | |
3218 | ||
3219 | if (level == FS_ATTRIBUTE_INFORMATION) | |
34f62640 SF |
3220 | memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset |
3221 | + (char *)&rsp->hdr, min_t(unsigned int, | |
2167114c SF |
3222 | rsp_len, max_len)); |
3223 | else if (level == FS_DEVICE_INFORMATION) | |
3224 | memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset | |
3225 | + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO)); | |
af6a12ea SF |
3226 | else if (level == FS_SECTOR_SIZE_INFORMATION) { |
3227 | struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *) | |
3228 | (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr); | |
3229 | tcon->ss_flags = le32_to_cpu(ss_info->Flags); | |
3230 | tcon->perf_sector_size = | |
3231 | le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf); | |
3232 | } | |
34f62640 SF |
3233 | |
3234 | qfsattr_exit: | |
da502f7d | 3235 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
6fc05c25 PS |
3236 | return rc; |
3237 | } | |
f7ba7fe6 PS |
3238 | |
3239 | int | |
3240 | smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon, | |
3241 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, | |
3242 | const __u32 num_lock, struct smb2_lock_element *buf) | |
3243 | { | |
3244 | int rc = 0; | |
3245 | struct smb2_lock_req *req = NULL; | |
3246 | struct kvec iov[2]; | |
da502f7d | 3247 | struct kvec rsp_iov; |
f7ba7fe6 PS |
3248 | int resp_buf_type; |
3249 | unsigned int count; | |
7fb8986e | 3250 | int flags = CIFS_NO_RESP; |
f7ba7fe6 | 3251 | |
f96637be | 3252 | cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock); |
f7ba7fe6 PS |
3253 | |
3254 | rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req); | |
3255 | if (rc) | |
3256 | return rc; | |
3257 | ||
7fb8986e PS |
3258 | if (encryption_required(tcon)) |
3259 | flags |= CIFS_TRANSFORM_REQ; | |
3260 | ||
31473fc4 | 3261 | req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid); |
f7ba7fe6 PS |
3262 | req->LockCount = cpu_to_le16(num_lock); |
3263 | ||
3264 | req->PersistentFileId = persist_fid; | |
3265 | req->VolatileFileId = volatile_fid; | |
3266 | ||
3267 | count = num_lock * sizeof(struct smb2_lock_element); | |
3268 | inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element)); | |
3269 | ||
3270 | iov[0].iov_base = (char *)req; | |
3271 | /* 4 for rfc1002 length field and count for all locks */ | |
3272 | iov[0].iov_len = get_rfc1002_length(req) + 4 - count; | |
3273 | iov[1].iov_base = (char *)buf; | |
3274 | iov[1].iov_len = count; | |
3275 | ||
3276 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); | |
7fb8986e | 3277 | rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags, |
da502f7d PS |
3278 | &rsp_iov); |
3279 | cifs_small_buf_release(req); | |
f7ba7fe6 | 3280 | if (rc) { |
f96637be | 3281 | cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc); |
f7ba7fe6 PS |
3282 | cifs_stats_fail_inc(tcon, SMB2_LOCK_HE); |
3283 | } | |
3284 | ||
3285 | return rc; | |
3286 | } | |
3287 | ||
3288 | int | |
3289 | SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon, | |
3290 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, | |
3291 | const __u64 length, const __u64 offset, const __u32 lock_flags, | |
3292 | const bool wait) | |
3293 | { | |
3294 | struct smb2_lock_element lock; | |
3295 | ||
3296 | lock.Offset = cpu_to_le64(offset); | |
3297 | lock.Length = cpu_to_le64(length); | |
3298 | lock.Flags = cpu_to_le32(lock_flags); | |
3299 | if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK) | |
3300 | lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY); | |
3301 | ||
3302 | return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock); | |
3303 | } | |
0822f514 PS |
3304 | |
3305 | int | |
3306 | SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon, | |
3307 | __u8 *lease_key, const __le32 lease_state) | |
3308 | { | |
3309 | int rc; | |
3310 | struct smb2_lease_ack *req = NULL; | |
7fb8986e | 3311 | int flags = CIFS_OBREAK_OP; |
0822f514 | 3312 | |
f96637be | 3313 | cifs_dbg(FYI, "SMB2_lease_break\n"); |
0822f514 | 3314 | rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req); |
0822f514 PS |
3315 | if (rc) |
3316 | return rc; | |
3317 | ||
7fb8986e PS |
3318 | if (encryption_required(tcon)) |
3319 | flags |= CIFS_TRANSFORM_REQ; | |
3320 | ||
31473fc4 | 3321 | req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1); |
0822f514 PS |
3322 | req->StructureSize = cpu_to_le16(36); |
3323 | inc_rfc1001_len(req, 12); | |
3324 | ||
3325 | memcpy(req->LeaseKey, lease_key, 16); | |
3326 | req->LeaseState = lease_state; | |
3327 | ||
7fb8986e | 3328 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags); |
da502f7d | 3329 | cifs_small_buf_release(req); |
0822f514 PS |
3330 | |
3331 | if (rc) { | |
3332 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); | |
f96637be | 3333 | cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc); |
0822f514 PS |
3334 | } |
3335 | ||
3336 | return rc; | |
3337 | } |