]>
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 | ||
ef65aaed SP |
660 | enum securityEnum |
661 | smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested) | |
662 | { | |
663 | switch (requested) { | |
664 | case Kerberos: | |
665 | case RawNTLMSSP: | |
666 | return requested; | |
667 | case NTLMv2: | |
668 | return RawNTLMSSP; | |
669 | case Unspecified: | |
670 | if (server->sec_ntlmssp && | |
671 | (global_secflags & CIFSSEC_MAY_NTLMSSP)) | |
672 | return RawNTLMSSP; | |
673 | if ((server->sec_kerberos || server->sec_mskerberos) && | |
674 | (global_secflags & CIFSSEC_MAY_KRB5)) | |
675 | return Kerberos; | |
676 | /* Fallthrough */ | |
677 | default: | |
678 | return Unspecified; | |
679 | } | |
680 | } | |
681 | ||
3baf1a7b SP |
682 | struct SMB2_sess_data { |
683 | unsigned int xid; | |
684 | struct cifs_ses *ses; | |
685 | struct nls_table *nls_cp; | |
686 | void (*func)(struct SMB2_sess_data *); | |
687 | int result; | |
688 | u64 previous_session; | |
689 | ||
690 | /* we will send the SMB in three pieces: | |
691 | * a fixed length beginning part, an optional | |
692 | * SPNEGO blob (which can be zero length), and a | |
693 | * last part which will include the strings | |
694 | * and rest of bcc area. This allows us to avoid | |
695 | * a large buffer 17K allocation | |
696 | */ | |
697 | int buf0_type; | |
698 | struct kvec iov[2]; | |
699 | }; | |
700 | ||
701 | static int | |
702 | SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data) | |
703 | { | |
704 | int rc; | |
705 | struct cifs_ses *ses = sess_data->ses; | |
706 | struct smb2_sess_setup_req *req; | |
707 | struct TCP_Server_Info *server = ses->server; | |
708 | ||
709 | rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req); | |
710 | if (rc) | |
711 | return rc; | |
712 | ||
31473fc4 PS |
713 | /* First session, not a reauthenticate */ |
714 | req->hdr.sync_hdr.SessionId = 0; | |
3baf1a7b SP |
715 | |
716 | /* if reconnect, we need to send previous sess id, otherwise it is 0 */ | |
717 | req->PreviousSessionId = sess_data->previous_session; | |
718 | ||
719 | req->Flags = 0; /* MBZ */ | |
720 | /* to enable echos and oplocks */ | |
31473fc4 | 721 | req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3); |
3baf1a7b SP |
722 | |
723 | /* only one of SMB2 signing flags may be set in SMB2 request */ | |
724 | if (server->sign) | |
725 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED; | |
726 | else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */ | |
727 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED; | |
728 | else | |
729 | req->SecurityMode = 0; | |
730 | ||
731 | req->Capabilities = 0; | |
732 | req->Channel = 0; /* MBZ */ | |
733 | ||
734 | sess_data->iov[0].iov_base = (char *)req; | |
735 | /* 4 for rfc1002 length field and 1 for pad */ | |
736 | sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
737 | /* | |
738 | * This variable will be used to clear the buffer | |
739 | * allocated above in case of any error in the calling function. | |
740 | */ | |
741 | sess_data->buf0_type = CIFS_SMALL_BUFFER; | |
742 | ||
743 | return 0; | |
744 | } | |
745 | ||
746 | static void | |
747 | SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data) | |
748 | { | |
749 | free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base); | |
750 | sess_data->buf0_type = CIFS_NO_BUFFER; | |
751 | } | |
752 | ||
753 | static int | |
754 | SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data) | |
755 | { | |
756 | int rc; | |
757 | struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base; | |
da502f7d | 758 | struct kvec rsp_iov = { NULL, 0 }; |
3baf1a7b SP |
759 | |
760 | /* Testing shows that buffer offset must be at location of Buffer[0] */ | |
761 | req->SecurityBufferOffset = | |
762 | cpu_to_le16(sizeof(struct smb2_sess_setup_req) - | |
763 | 1 /* pad */ - 4 /* rfc1001 len */); | |
764 | req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len); | |
765 | ||
766 | inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */); | |
767 | ||
768 | /* BB add code to build os and lm fields */ | |
769 | ||
770 | rc = SendReceive2(sess_data->xid, sess_data->ses, | |
771 | sess_data->iov, 2, | |
772 | &sess_data->buf0_type, | |
da502f7d PS |
773 | CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov); |
774 | cifs_small_buf_release(sess_data->iov[0].iov_base); | |
775 | memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec)); | |
3baf1a7b SP |
776 | |
777 | return rc; | |
778 | } | |
779 | ||
780 | static int | |
781 | SMB2_sess_establish_session(struct SMB2_sess_data *sess_data) | |
782 | { | |
783 | int rc = 0; | |
784 | struct cifs_ses *ses = sess_data->ses; | |
785 | ||
786 | mutex_lock(&ses->server->srv_mutex); | |
cabfb368 | 787 | if (ses->server->ops->generate_signingkey) { |
3baf1a7b | 788 | rc = ses->server->ops->generate_signingkey(ses); |
3baf1a7b SP |
789 | if (rc) { |
790 | cifs_dbg(FYI, | |
791 | "SMB3 session key generation failed\n"); | |
792 | mutex_unlock(&ses->server->srv_mutex); | |
cabfb368 | 793 | return rc; |
3baf1a7b SP |
794 | } |
795 | } | |
796 | if (!ses->server->session_estab) { | |
797 | ses->server->sequence_number = 0x2; | |
798 | ses->server->session_estab = true; | |
799 | } | |
800 | mutex_unlock(&ses->server->srv_mutex); | |
801 | ||
802 | cifs_dbg(FYI, "SMB2/3 session established successfully\n"); | |
803 | spin_lock(&GlobalMid_Lock); | |
804 | ses->status = CifsGood; | |
805 | ses->need_reconnect = false; | |
806 | spin_unlock(&GlobalMid_Lock); | |
3baf1a7b SP |
807 | return rc; |
808 | } | |
809 | ||
810 | #ifdef CONFIG_CIFS_UPCALL | |
811 | static void | |
812 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) | |
813 | { | |
814 | int rc; | |
815 | struct cifs_ses *ses = sess_data->ses; | |
816 | struct cifs_spnego_msg *msg; | |
817 | struct key *spnego_key = NULL; | |
818 | struct smb2_sess_setup_rsp *rsp = NULL; | |
819 | ||
820 | rc = SMB2_sess_alloc_buffer(sess_data); | |
821 | if (rc) | |
822 | goto out; | |
823 | ||
824 | spnego_key = cifs_get_spnego_key(ses); | |
825 | if (IS_ERR(spnego_key)) { | |
826 | rc = PTR_ERR(spnego_key); | |
827 | spnego_key = NULL; | |
828 | goto out; | |
829 | } | |
830 | ||
831 | msg = spnego_key->payload.data[0]; | |
832 | /* | |
833 | * check version field to make sure that cifs.upcall is | |
834 | * sending us a response in an expected form | |
835 | */ | |
836 | if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) { | |
837 | cifs_dbg(VFS, | |
838 | "bad cifs.upcall version. Expected %d got %d", | |
839 | CIFS_SPNEGO_UPCALL_VERSION, msg->version); | |
840 | rc = -EKEYREJECTED; | |
841 | goto out_put_spnego_key; | |
842 | } | |
843 | ||
844 | ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len, | |
845 | GFP_KERNEL); | |
846 | if (!ses->auth_key.response) { | |
847 | cifs_dbg(VFS, | |
848 | "Kerberos can't allocate (%u bytes) memory", | |
849 | msg->sesskey_len); | |
850 | rc = -ENOMEM; | |
851 | goto out_put_spnego_key; | |
852 | } | |
853 | ses->auth_key.len = msg->sesskey_len; | |
854 | ||
855 | sess_data->iov[1].iov_base = msg->data + msg->sesskey_len; | |
856 | sess_data->iov[1].iov_len = msg->secblob_len; | |
857 | ||
858 | rc = SMB2_sess_sendreceive(sess_data); | |
859 | if (rc) | |
860 | goto out_put_spnego_key; | |
861 | ||
862 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; | |
31473fc4 | 863 | ses->Suid = rsp->hdr.sync_hdr.SessionId; |
3baf1a7b SP |
864 | |
865 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); | |
3baf1a7b SP |
866 | |
867 | rc = SMB2_sess_establish_session(sess_data); | |
868 | out_put_spnego_key: | |
869 | key_invalidate(spnego_key); | |
870 | key_put(spnego_key); | |
871 | out: | |
872 | sess_data->result = rc; | |
873 | sess_data->func = NULL; | |
874 | SMB2_sess_free_buffer(sess_data); | |
875 | } | |
876 | #else | |
877 | static void | |
878 | SMB2_auth_kerberos(struct SMB2_sess_data *sess_data) | |
879 | { | |
880 | cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n"); | |
881 | sess_data->result = -EOPNOTSUPP; | |
882 | sess_data->func = NULL; | |
883 | } | |
884 | #endif | |
885 | ||
166cea4d SP |
886 | static void |
887 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data); | |
888 | ||
889 | static void | |
890 | SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data) | |
5478f9ba | 891 | { |
166cea4d SP |
892 | int rc; |
893 | struct cifs_ses *ses = sess_data->ses; | |
5478f9ba | 894 | struct smb2_sess_setup_rsp *rsp = NULL; |
166cea4d | 895 | char *ntlmssp_blob = NULL; |
5478f9ba | 896 | bool use_spnego = false; /* else use raw ntlmssp */ |
166cea4d | 897 | u16 blob_length = 0; |
d4e63bd6 | 898 | |
5478f9ba PS |
899 | /* |
900 | * If memory allocation is successful, caller of this function | |
901 | * frees it. | |
902 | */ | |
903 | ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL); | |
166cea4d SP |
904 | if (!ses->ntlmssp) { |
905 | rc = -ENOMEM; | |
906 | goto out_err; | |
907 | } | |
5c234aa5 | 908 | ses->ntlmssp->sesskey_per_smbsess = true; |
5478f9ba | 909 | |
166cea4d | 910 | rc = SMB2_sess_alloc_buffer(sess_data); |
5478f9ba | 911 | if (rc) |
166cea4d | 912 | goto out_err; |
5478f9ba | 913 | |
166cea4d SP |
914 | ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE), |
915 | GFP_KERNEL); | |
916 | if (ntlmssp_blob == NULL) { | |
917 | rc = -ENOMEM; | |
918 | goto out; | |
919 | } | |
c2afb814 | 920 | |
166cea4d SP |
921 | build_ntlmssp_negotiate_blob(ntlmssp_blob, ses); |
922 | if (use_spnego) { | |
923 | /* BB eventually need to add this */ | |
924 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); | |
925 | rc = -EOPNOTSUPP; | |
926 | goto out; | |
927 | } else { | |
928 | blob_length = sizeof(struct _NEGOTIATE_MESSAGE); | |
929 | /* with raw NTLMSSP we don't encapsulate in SPNEGO */ | |
930 | } | |
931 | sess_data->iov[1].iov_base = ntlmssp_blob; | |
932 | sess_data->iov[1].iov_len = blob_length; | |
c2afb814 | 933 | |
166cea4d SP |
934 | rc = SMB2_sess_sendreceive(sess_data); |
935 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; | |
5478f9ba | 936 | |
166cea4d SP |
937 | /* If true, rc here is expected and not an error */ |
938 | if (sess_data->buf0_type != CIFS_NO_BUFFER && | |
31473fc4 | 939 | rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) |
166cea4d | 940 | rc = 0; |
38d77c50 | 941 | |
166cea4d SP |
942 | if (rc) |
943 | goto out; | |
5478f9ba | 944 | |
166cea4d SP |
945 | if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 != |
946 | le16_to_cpu(rsp->SecurityBufferOffset)) { | |
947 | cifs_dbg(VFS, "Invalid security buffer offset %d\n", | |
948 | le16_to_cpu(rsp->SecurityBufferOffset)); | |
5478f9ba | 949 | rc = -EIO; |
166cea4d | 950 | goto out; |
5478f9ba | 951 | } |
166cea4d SP |
952 | rc = decode_ntlmssp_challenge(rsp->Buffer, |
953 | le16_to_cpu(rsp->SecurityBufferLength), ses); | |
954 | if (rc) | |
955 | goto out; | |
5478f9ba | 956 | |
166cea4d | 957 | cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n"); |
5478f9ba | 958 | |
5478f9ba | 959 | |
31473fc4 | 960 | ses->Suid = rsp->hdr.sync_hdr.SessionId; |
166cea4d | 961 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
166cea4d SP |
962 | |
963 | out: | |
964 | kfree(ntlmssp_blob); | |
965 | SMB2_sess_free_buffer(sess_data); | |
966 | if (!rc) { | |
967 | sess_data->result = 0; | |
968 | sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate; | |
969 | return; | |
970 | } | |
971 | out_err: | |
972 | kfree(ses->ntlmssp); | |
973 | ses->ntlmssp = NULL; | |
974 | sess_data->result = rc; | |
975 | sess_data->func = NULL; | |
976 | } | |
5478f9ba | 977 | |
166cea4d SP |
978 | static void |
979 | SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data) | |
980 | { | |
981 | int rc; | |
982 | struct cifs_ses *ses = sess_data->ses; | |
983 | struct smb2_sess_setup_req *req; | |
984 | struct smb2_sess_setup_rsp *rsp = NULL; | |
985 | unsigned char *ntlmssp_blob = NULL; | |
986 | bool use_spnego = false; /* else use raw ntlmssp */ | |
987 | u16 blob_length = 0; | |
5478f9ba | 988 | |
166cea4d SP |
989 | rc = SMB2_sess_alloc_buffer(sess_data); |
990 | if (rc) | |
991 | goto out; | |
5478f9ba | 992 | |
166cea4d | 993 | req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base; |
31473fc4 | 994 | req->hdr.sync_hdr.SessionId = ses->Suid; |
166cea4d SP |
995 | |
996 | rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses, | |
997 | sess_data->nls_cp); | |
998 | if (rc) { | |
999 | cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc); | |
1000 | goto out; | |
5478f9ba PS |
1001 | } |
1002 | ||
166cea4d SP |
1003 | if (use_spnego) { |
1004 | /* BB eventually need to add this */ | |
1005 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); | |
1006 | rc = -EOPNOTSUPP; | |
1007 | goto out; | |
1008 | } | |
1009 | sess_data->iov[1].iov_base = ntlmssp_blob; | |
1010 | sess_data->iov[1].iov_len = blob_length; | |
5478f9ba | 1011 | |
166cea4d SP |
1012 | rc = SMB2_sess_sendreceive(sess_data); |
1013 | if (rc) | |
1014 | goto out; | |
1015 | ||
1016 | rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base; | |
1017 | ||
31473fc4 | 1018 | ses->Suid = rsp->hdr.sync_hdr.SessionId; |
5478f9ba | 1019 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
5478f9ba | 1020 | |
166cea4d SP |
1021 | rc = SMB2_sess_establish_session(sess_data); |
1022 | out: | |
1023 | kfree(ntlmssp_blob); | |
1024 | SMB2_sess_free_buffer(sess_data); | |
1025 | kfree(ses->ntlmssp); | |
1026 | ses->ntlmssp = NULL; | |
1027 | sess_data->result = rc; | |
1028 | sess_data->func = NULL; | |
1029 | } | |
d4e63bd6 | 1030 | |
166cea4d SP |
1031 | static int |
1032 | SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data) | |
1033 | { | |
ef65aaed SP |
1034 | int type; |
1035 | ||
1036 | type = smb2_select_sectype(ses->server, ses->sectype); | |
1037 | cifs_dbg(FYI, "sess setup type %d\n", type); | |
1038 | if (type == Unspecified) { | |
1039 | cifs_dbg(VFS, | |
1040 | "Unable to select appropriate authentication method!"); | |
1041 | return -EINVAL; | |
1042 | } | |
d4e63bd6 | 1043 | |
ef65aaed | 1044 | switch (type) { |
166cea4d SP |
1045 | case Kerberos: |
1046 | sess_data->func = SMB2_auth_kerberos; | |
1047 | break; | |
1048 | case RawNTLMSSP: | |
1049 | sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate; | |
1050 | break; | |
1051 | default: | |
ef65aaed | 1052 | cifs_dbg(VFS, "secType %d not supported!\n", type); |
166cea4d | 1053 | return -EOPNOTSUPP; |
d4e63bd6 SP |
1054 | } |
1055 | ||
166cea4d SP |
1056 | return 0; |
1057 | } | |
1058 | ||
1059 | int | |
1060 | SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, | |
1061 | const struct nls_table *nls_cp) | |
1062 | { | |
1063 | int rc = 0; | |
1064 | struct TCP_Server_Info *server = ses->server; | |
1065 | struct SMB2_sess_data *sess_data; | |
1066 | ||
1067 | cifs_dbg(FYI, "Session Setup\n"); | |
1068 | ||
1069 | if (!server) { | |
1070 | WARN(1, "%s: server is NULL!\n", __func__); | |
1071 | return -EIO; | |
d4e63bd6 | 1072 | } |
d4e63bd6 | 1073 | |
166cea4d SP |
1074 | sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL); |
1075 | if (!sess_data) | |
1076 | return -ENOMEM; | |
1077 | ||
1078 | rc = SMB2_select_sec(ses, sess_data); | |
1079 | if (rc) | |
1080 | goto out; | |
1081 | sess_data->xid = xid; | |
1082 | sess_data->ses = ses; | |
1083 | sess_data->buf0_type = CIFS_NO_BUFFER; | |
1084 | sess_data->nls_cp = (struct nls_table *) nls_cp; | |
1085 | ||
1086 | while (sess_data->func) | |
1087 | sess_data->func(sess_data); | |
1088 | ||
3baf1a7b | 1089 | rc = sess_data->result; |
166cea4d | 1090 | out: |
3baf1a7b | 1091 | kfree(sess_data); |
5478f9ba PS |
1092 | return rc; |
1093 | } | |
1094 | ||
1095 | int | |
1096 | SMB2_logoff(const unsigned int xid, struct cifs_ses *ses) | |
1097 | { | |
1098 | struct smb2_logoff_req *req; /* response is also trivial struct */ | |
1099 | int rc = 0; | |
1100 | struct TCP_Server_Info *server; | |
7fb8986e | 1101 | int flags = 0; |
5478f9ba | 1102 | |
f96637be | 1103 | cifs_dbg(FYI, "disconnect session %p\n", ses); |
5478f9ba PS |
1104 | |
1105 | if (ses && (ses->server)) | |
1106 | server = ses->server; | |
1107 | else | |
1108 | return -EIO; | |
1109 | ||
eb4c7df6 SP |
1110 | /* no need to send SMB logoff if uid already closed due to reconnect */ |
1111 | if (ses->need_reconnect) | |
1112 | goto smb2_session_already_dead; | |
1113 | ||
5478f9ba PS |
1114 | rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req); |
1115 | if (rc) | |
1116 | return rc; | |
1117 | ||
1118 | /* since no tcon, smb2_init can not do this, so do here */ | |
31473fc4 | 1119 | req->hdr.sync_hdr.SessionId = ses->Suid; |
7fb8986e PS |
1120 | |
1121 | if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) | |
1122 | flags |= CIFS_TRANSFORM_REQ; | |
1123 | else if (server->sign) | |
31473fc4 | 1124 | req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED; |
5478f9ba | 1125 | |
7fb8986e | 1126 | rc = SendReceiveNoRsp(xid, ses, (char *) req, flags); |
da502f7d | 1127 | cifs_small_buf_release(req); |
5478f9ba PS |
1128 | /* |
1129 | * No tcon so can't do | |
1130 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); | |
1131 | */ | |
eb4c7df6 SP |
1132 | |
1133 | smb2_session_already_dead: | |
5478f9ba PS |
1134 | return rc; |
1135 | } | |
faaf946a PS |
1136 | |
1137 | static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code) | |
1138 | { | |
d60622eb | 1139 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]); |
faaf946a PS |
1140 | } |
1141 | ||
1142 | #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */) | |
1143 | ||
de9f68df SF |
1144 | /* These are similar values to what Windows uses */ |
1145 | static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon) | |
1146 | { | |
1147 | tcon->max_chunks = 256; | |
1148 | tcon->max_bytes_chunk = 1048576; | |
1149 | tcon->max_bytes_copy = 16777216; | |
1150 | } | |
1151 | ||
faaf946a PS |
1152 | int |
1153 | SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree, | |
1154 | struct cifs_tcon *tcon, const struct nls_table *cp) | |
1155 | { | |
1156 | struct smb2_tree_connect_req *req; | |
1157 | struct smb2_tree_connect_rsp *rsp = NULL; | |
1158 | struct kvec iov[2]; | |
da502f7d | 1159 | struct kvec rsp_iov; |
faaf946a PS |
1160 | int rc = 0; |
1161 | int resp_buftype; | |
1162 | int unc_path_len; | |
1163 | struct TCP_Server_Info *server; | |
1164 | __le16 *unc_path = NULL; | |
7fb8986e | 1165 | int flags = 0; |
faaf946a | 1166 | |
f96637be | 1167 | cifs_dbg(FYI, "TCON\n"); |
faaf946a PS |
1168 | |
1169 | if ((ses->server) && tree) | |
1170 | server = ses->server; | |
1171 | else | |
1172 | return -EIO; | |
1173 | ||
1174 | if (tcon && tcon->bad_network_name) | |
1175 | return -ENOENT; | |
1176 | ||
1177 | unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL); | |
1178 | if (unc_path == NULL) | |
1179 | return -ENOMEM; | |
1180 | ||
1181 | unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1; | |
1182 | unc_path_len *= 2; | |
1183 | if (unc_path_len < 2) { | |
1184 | kfree(unc_path); | |
1185 | return -EINVAL; | |
1186 | } | |
1187 | ||
1188 | rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req); | |
1189 | if (rc) { | |
1190 | kfree(unc_path); | |
1191 | return rc; | |
1192 | } | |
1193 | ||
1194 | if (tcon == NULL) { | |
ae6f8dd4 PS |
1195 | if ((ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)) |
1196 | flags |= CIFS_TRANSFORM_REQ; | |
1197 | ||
faaf946a | 1198 | /* since no tcon, smb2_init can not do this, so do here */ |
31473fc4 | 1199 | req->hdr.sync_hdr.SessionId = ses->Suid; |
b9043cc5 AA |
1200 | if (ses->server->sign) |
1201 | req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED; | |
ae6f8dd4 PS |
1202 | } else if (encryption_required(tcon)) |
1203 | flags |= CIFS_TRANSFORM_REQ; | |
faaf946a PS |
1204 | |
1205 | iov[0].iov_base = (char *)req; | |
1206 | /* 4 for rfc1002 length field and 1 for pad */ | |
1207 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
1208 | ||
1209 | /* Testing shows that buffer offset must be at location of Buffer[0] */ | |
1210 | req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req) | |
1211 | - 1 /* pad */ - 4 /* do not count rfc1001 len field */); | |
1212 | req->PathLength = cpu_to_le16(unc_path_len - 2); | |
1213 | iov[1].iov_base = unc_path; | |
1214 | iov[1].iov_len = unc_path_len; | |
1215 | ||
1216 | inc_rfc1001_len(req, unc_path_len - 1 /* pad */); | |
1217 | ||
7fb8986e | 1218 | rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
1219 | cifs_small_buf_release(req); |
1220 | rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base; | |
faaf946a PS |
1221 | |
1222 | if (rc != 0) { | |
1223 | if (tcon) { | |
1224 | cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE); | |
1225 | tcon->need_reconnect = true; | |
1226 | } | |
1227 | goto tcon_error_exit; | |
1228 | } | |
1229 | ||
faaf946a | 1230 | if (tcon == NULL) { |
31473fc4 | 1231 | ses->ipc_tid = rsp->hdr.sync_hdr.TreeId; |
faaf946a PS |
1232 | goto tcon_exit; |
1233 | } | |
1234 | ||
1235 | if (rsp->ShareType & SMB2_SHARE_TYPE_DISK) | |
f96637be | 1236 | cifs_dbg(FYI, "connection to disk share\n"); |
faaf946a PS |
1237 | else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) { |
1238 | tcon->ipc = true; | |
f96637be | 1239 | cifs_dbg(FYI, "connection to pipe share\n"); |
faaf946a PS |
1240 | } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) { |
1241 | tcon->print = true; | |
f96637be | 1242 | cifs_dbg(FYI, "connection to printer\n"); |
faaf946a | 1243 | } else { |
f96637be | 1244 | cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType); |
faaf946a PS |
1245 | rc = -EOPNOTSUPP; |
1246 | goto tcon_error_exit; | |
1247 | } | |
1248 | ||
1249 | tcon->share_flags = le32_to_cpu(rsp->ShareFlags); | |
769ee6a4 | 1250 | tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */ |
faaf946a PS |
1251 | tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
1252 | tcon->tidStatus = CifsGood; | |
1253 | tcon->need_reconnect = false; | |
31473fc4 | 1254 | tcon->tid = rsp->hdr.sync_hdr.TreeId; |
46b51d08 | 1255 | strlcpy(tcon->treeName, tree, sizeof(tcon->treeName)); |
faaf946a PS |
1256 | |
1257 | if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) && | |
1258 | ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0)) | |
f96637be | 1259 | cifs_dbg(VFS, "DFS capability contradicts DFS flag\n"); |
ae6f8dd4 PS |
1260 | |
1261 | if (tcon->seal && | |
1262 | !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)) | |
1263 | cifs_dbg(VFS, "Encryption is requested but not supported\n"); | |
1264 | ||
de9f68df | 1265 | init_copy_chunk_defaults(tcon); |
ff1c038a SF |
1266 | if (tcon->ses->server->ops->validate_negotiate) |
1267 | rc = tcon->ses->server->ops->validate_negotiate(xid, tcon); | |
faaf946a PS |
1268 | tcon_exit: |
1269 | free_rsp_buf(resp_buftype, rsp); | |
1270 | kfree(unc_path); | |
1271 | return rc; | |
1272 | ||
1273 | tcon_error_exit: | |
31473fc4 | 1274 | if (rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) { |
f96637be | 1275 | cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree); |
18f39e7b SF |
1276 | if (tcon) |
1277 | tcon->bad_network_name = true; | |
faaf946a PS |
1278 | } |
1279 | goto tcon_exit; | |
1280 | } | |
1281 | ||
1282 | int | |
1283 | SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon) | |
1284 | { | |
1285 | struct smb2_tree_disconnect_req *req; /* response is trivial */ | |
1286 | int rc = 0; | |
1287 | struct TCP_Server_Info *server; | |
1288 | struct cifs_ses *ses = tcon->ses; | |
7fb8986e | 1289 | int flags = 0; |
faaf946a | 1290 | |
f96637be | 1291 | cifs_dbg(FYI, "Tree Disconnect\n"); |
faaf946a PS |
1292 | |
1293 | if (ses && (ses->server)) | |
1294 | server = ses->server; | |
1295 | else | |
1296 | return -EIO; | |
1297 | ||
1298 | if ((tcon->need_reconnect) || (tcon->ses->need_reconnect)) | |
1299 | return 0; | |
1300 | ||
1301 | rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req); | |
1302 | if (rc) | |
1303 | return rc; | |
1304 | ||
7fb8986e PS |
1305 | if (encryption_required(tcon)) |
1306 | flags |= CIFS_TRANSFORM_REQ; | |
1307 | ||
1308 | rc = SendReceiveNoRsp(xid, ses, (char *)req, flags); | |
da502f7d | 1309 | cifs_small_buf_release(req); |
faaf946a PS |
1310 | if (rc) |
1311 | cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE); | |
1312 | ||
1313 | return rc; | |
1314 | } | |
2503a0db | 1315 | |
b8c32dbb | 1316 | |
63eb3def PS |
1317 | static struct create_durable * |
1318 | create_durable_buf(void) | |
1319 | { | |
1320 | struct create_durable *buf; | |
1321 | ||
1322 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); | |
1323 | if (!buf) | |
1324 | return NULL; | |
1325 | ||
1326 | buf->ccontext.DataOffset = cpu_to_le16(offsetof | |
9cbc0b73 | 1327 | (struct create_durable, Data)); |
63eb3def PS |
1328 | buf->ccontext.DataLength = cpu_to_le32(16); |
1329 | buf->ccontext.NameOffset = cpu_to_le16(offsetof | |
1330 | (struct create_durable, Name)); | |
1331 | buf->ccontext.NameLength = cpu_to_le16(4); | |
12197a7f | 1332 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */ |
63eb3def PS |
1333 | buf->Name[0] = 'D'; |
1334 | buf->Name[1] = 'H'; | |
1335 | buf->Name[2] = 'n'; | |
1336 | buf->Name[3] = 'Q'; | |
1337 | return buf; | |
1338 | } | |
1339 | ||
9cbc0b73 PS |
1340 | static struct create_durable * |
1341 | create_reconnect_durable_buf(struct cifs_fid *fid) | |
1342 | { | |
1343 | struct create_durable *buf; | |
1344 | ||
1345 | buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL); | |
1346 | if (!buf) | |
1347 | return NULL; | |
1348 | ||
1349 | buf->ccontext.DataOffset = cpu_to_le16(offsetof | |
1350 | (struct create_durable, Data)); | |
1351 | buf->ccontext.DataLength = cpu_to_le32(16); | |
1352 | buf->ccontext.NameOffset = cpu_to_le16(offsetof | |
1353 | (struct create_durable, Name)); | |
1354 | buf->ccontext.NameLength = cpu_to_le16(4); | |
1355 | buf->Data.Fid.PersistentFileId = fid->persistent_fid; | |
1356 | buf->Data.Fid.VolatileFileId = fid->volatile_fid; | |
12197a7f | 1357 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */ |
9cbc0b73 PS |
1358 | buf->Name[0] = 'D'; |
1359 | buf->Name[1] = 'H'; | |
1360 | buf->Name[2] = 'n'; | |
1361 | buf->Name[3] = 'C'; | |
1362 | return buf; | |
1363 | } | |
1364 | ||
b8c32dbb | 1365 | static __u8 |
42873b0a PS |
1366 | parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp, |
1367 | unsigned int *epoch) | |
b8c32dbb PS |
1368 | { |
1369 | char *data_offset; | |
b5c7cde3 | 1370 | struct create_context *cc; |
deb7deff JM |
1371 | unsigned int next; |
1372 | unsigned int remaining; | |
fd554396 | 1373 | char *name; |
b8c32dbb | 1374 | |
fd554396 | 1375 | data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset); |
deb7deff | 1376 | remaining = le32_to_cpu(rsp->CreateContextsLength); |
b5c7cde3 | 1377 | cc = (struct create_context *)data_offset; |
deb7deff | 1378 | while (remaining >= sizeof(struct create_context)) { |
b5c7cde3 | 1379 | name = le16_to_cpu(cc->NameOffset) + (char *)cc; |
deb7deff JM |
1380 | if (le16_to_cpu(cc->NameLength) == 4 && |
1381 | strncmp(name, "RqLs", 4) == 0) | |
1382 | return server->ops->parse_lease_buf(cc, epoch); | |
1383 | ||
1384 | next = le32_to_cpu(cc->Next); | |
1385 | if (!next) | |
1386 | break; | |
1387 | remaining -= next; | |
1388 | cc = (struct create_context *)((char *)cc + next); | |
1389 | } | |
b8c32dbb | 1390 | |
b5c7cde3 | 1391 | return 0; |
b8c32dbb PS |
1392 | } |
1393 | ||
d22cbfec | 1394 | static int |
a41a28bd PS |
1395 | add_lease_context(struct TCP_Server_Info *server, struct kvec *iov, |
1396 | unsigned int *num_iovec, __u8 *oplock) | |
d22cbfec PS |
1397 | { |
1398 | struct smb2_create_req *req = iov[0].iov_base; | |
1399 | unsigned int num = *num_iovec; | |
1400 | ||
a41a28bd | 1401 | iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock); |
d22cbfec PS |
1402 | if (iov[num].iov_base == NULL) |
1403 | return -ENOMEM; | |
a41a28bd | 1404 | iov[num].iov_len = server->vals->create_lease_size; |
d22cbfec PS |
1405 | req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
1406 | if (!req->CreateContextsOffset) | |
1407 | req->CreateContextsOffset = cpu_to_le32( | |
1408 | sizeof(struct smb2_create_req) - 4 + | |
1409 | iov[num - 1].iov_len); | |
a41a28bd PS |
1410 | le32_add_cpu(&req->CreateContextsLength, |
1411 | server->vals->create_lease_size); | |
1412 | inc_rfc1001_len(&req->hdr, server->vals->create_lease_size); | |
d22cbfec PS |
1413 | *num_iovec = num + 1; |
1414 | return 0; | |
1415 | } | |
1416 | ||
b56eae4d SF |
1417 | static struct create_durable_v2 * |
1418 | create_durable_v2_buf(struct cifs_fid *pfid) | |
1419 | { | |
1420 | struct create_durable_v2 *buf; | |
1421 | ||
1422 | buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL); | |
1423 | if (!buf) | |
1424 | return NULL; | |
1425 | ||
1426 | buf->ccontext.DataOffset = cpu_to_le16(offsetof | |
1427 | (struct create_durable_v2, dcontext)); | |
1428 | buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2)); | |
1429 | buf->ccontext.NameOffset = cpu_to_le16(offsetof | |
1430 | (struct create_durable_v2, Name)); | |
1431 | buf->ccontext.NameLength = cpu_to_le16(4); | |
1432 | ||
1433 | buf->dcontext.Timeout = 0; /* Should this be configurable by workload */ | |
1434 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); | |
fa70b87c | 1435 | generate_random_uuid(buf->dcontext.CreateGuid); |
b56eae4d SF |
1436 | memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16); |
1437 | ||
1438 | /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */ | |
1439 | buf->Name[0] = 'D'; | |
1440 | buf->Name[1] = 'H'; | |
1441 | buf->Name[2] = '2'; | |
1442 | buf->Name[3] = 'Q'; | |
1443 | return buf; | |
1444 | } | |
1445 | ||
1446 | static struct create_durable_handle_reconnect_v2 * | |
1447 | create_reconnect_durable_v2_buf(struct cifs_fid *fid) | |
1448 | { | |
1449 | struct create_durable_handle_reconnect_v2 *buf; | |
1450 | ||
1451 | buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2), | |
1452 | GFP_KERNEL); | |
1453 | if (!buf) | |
1454 | return NULL; | |
1455 | ||
1456 | buf->ccontext.DataOffset = | |
1457 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, | |
1458 | dcontext)); | |
1459 | buf->ccontext.DataLength = | |
1460 | cpu_to_le32(sizeof(struct durable_reconnect_context_v2)); | |
1461 | buf->ccontext.NameOffset = | |
1462 | cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2, | |
1463 | Name)); | |
1464 | buf->ccontext.NameLength = cpu_to_le16(4); | |
1465 | ||
1466 | buf->dcontext.Fid.PersistentFileId = fid->persistent_fid; | |
1467 | buf->dcontext.Fid.VolatileFileId = fid->volatile_fid; | |
1468 | buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT); | |
1469 | memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16); | |
1470 | ||
1471 | /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */ | |
1472 | buf->Name[0] = 'D'; | |
1473 | buf->Name[1] = 'H'; | |
1474 | buf->Name[2] = '2'; | |
1475 | buf->Name[3] = 'C'; | |
1476 | return buf; | |
1477 | } | |
1478 | ||
63eb3def | 1479 | static int |
b56eae4d SF |
1480 | add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec, |
1481 | struct cifs_open_parms *oparms) | |
1482 | { | |
1483 | struct smb2_create_req *req = iov[0].iov_base; | |
1484 | unsigned int num = *num_iovec; | |
1485 | ||
1486 | iov[num].iov_base = create_durable_v2_buf(oparms->fid); | |
1487 | if (iov[num].iov_base == NULL) | |
1488 | return -ENOMEM; | |
1489 | iov[num].iov_len = sizeof(struct create_durable_v2); | |
1490 | if (!req->CreateContextsOffset) | |
1491 | req->CreateContextsOffset = | |
1492 | cpu_to_le32(sizeof(struct smb2_create_req) - 4 + | |
1493 | iov[1].iov_len); | |
1494 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2)); | |
1495 | inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2)); | |
1496 | *num_iovec = num + 1; | |
1497 | return 0; | |
1498 | } | |
1499 | ||
1500 | static int | |
1501 | add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec, | |
9cbc0b73 | 1502 | struct cifs_open_parms *oparms) |
63eb3def PS |
1503 | { |
1504 | struct smb2_create_req *req = iov[0].iov_base; | |
1505 | unsigned int num = *num_iovec; | |
1506 | ||
b56eae4d SF |
1507 | /* indicate that we don't need to relock the file */ |
1508 | oparms->reconnect = false; | |
1509 | ||
1510 | iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid); | |
1511 | if (iov[num].iov_base == NULL) | |
1512 | return -ENOMEM; | |
1513 | iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2); | |
1514 | if (!req->CreateContextsOffset) | |
1515 | req->CreateContextsOffset = | |
1516 | cpu_to_le32(sizeof(struct smb2_create_req) - 4 + | |
1517 | iov[1].iov_len); | |
1518 | le32_add_cpu(&req->CreateContextsLength, | |
1519 | sizeof(struct create_durable_handle_reconnect_v2)); | |
1520 | inc_rfc1001_len(&req->hdr, | |
1521 | sizeof(struct create_durable_handle_reconnect_v2)); | |
1522 | *num_iovec = num + 1; | |
1523 | return 0; | |
1524 | } | |
1525 | ||
1526 | static int | |
1527 | add_durable_context(struct kvec *iov, unsigned int *num_iovec, | |
1528 | struct cifs_open_parms *oparms, bool use_persistent) | |
1529 | { | |
1530 | struct smb2_create_req *req = iov[0].iov_base; | |
1531 | unsigned int num = *num_iovec; | |
1532 | ||
1533 | if (use_persistent) { | |
1534 | if (oparms->reconnect) | |
1535 | return add_durable_reconnect_v2_context(iov, num_iovec, | |
1536 | oparms); | |
1537 | else | |
1538 | return add_durable_v2_context(iov, num_iovec, oparms); | |
1539 | } | |
1540 | ||
9cbc0b73 PS |
1541 | if (oparms->reconnect) { |
1542 | iov[num].iov_base = create_reconnect_durable_buf(oparms->fid); | |
1543 | /* indicate that we don't need to relock the file */ | |
1544 | oparms->reconnect = false; | |
1545 | } else | |
1546 | iov[num].iov_base = create_durable_buf(); | |
63eb3def PS |
1547 | if (iov[num].iov_base == NULL) |
1548 | return -ENOMEM; | |
1549 | iov[num].iov_len = sizeof(struct create_durable); | |
1550 | if (!req->CreateContextsOffset) | |
1551 | req->CreateContextsOffset = | |
1552 | cpu_to_le32(sizeof(struct smb2_create_req) - 4 + | |
1553 | iov[1].iov_len); | |
31f92e9a | 1554 | le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable)); |
63eb3def PS |
1555 | inc_rfc1001_len(&req->hdr, sizeof(struct create_durable)); |
1556 | *num_iovec = num + 1; | |
1557 | return 0; | |
1558 | } | |
1559 | ||
f0712928 AA |
1560 | static int |
1561 | alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len, | |
1562 | const char *treename, const __le16 *path) | |
1563 | { | |
1564 | int treename_len, path_len; | |
1565 | struct nls_table *cp; | |
1566 | const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)}; | |
1567 | ||
1568 | /* | |
1569 | * skip leading "\\" | |
1570 | */ | |
1571 | treename_len = strlen(treename); | |
1572 | if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\')) | |
1573 | return -EINVAL; | |
1574 | ||
1575 | treename += 2; | |
1576 | treename_len -= 2; | |
1577 | ||
1578 | path_len = UniStrnlen((wchar_t *)path, PATH_MAX); | |
1579 | ||
1580 | /* | |
1581 | * make room for one path separator between the treename and | |
1582 | * path | |
1583 | */ | |
1584 | *out_len = treename_len + 1 + path_len; | |
1585 | ||
1586 | /* | |
1587 | * final path needs to be null-terminated UTF16 with a | |
1588 | * size aligned to 8 | |
1589 | */ | |
1590 | ||
1591 | *out_size = roundup((*out_len+1)*2, 8); | |
1592 | *out_path = kzalloc(*out_size, GFP_KERNEL); | |
1593 | if (!*out_path) | |
1594 | return -ENOMEM; | |
1595 | ||
1596 | cp = load_nls_default(); | |
1597 | cifs_strtoUTF16(*out_path, treename, treename_len, cp); | |
1598 | UniStrcat(*out_path, sep); | |
1599 | UniStrcat(*out_path, path); | |
1600 | unload_nls(cp); | |
1601 | ||
1602 | return 0; | |
1603 | } | |
1604 | ||
2503a0db | 1605 | int |
064f6047 | 1606 | SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path, |
b42bf888 PS |
1607 | __u8 *oplock, struct smb2_file_all_info *buf, |
1608 | struct smb2_err_rsp **err_buf) | |
2503a0db PS |
1609 | { |
1610 | struct smb2_create_req *req; | |
1611 | struct smb2_create_rsp *rsp; | |
1612 | struct TCP_Server_Info *server; | |
064f6047 | 1613 | struct cifs_tcon *tcon = oparms->tcon; |
2503a0db | 1614 | struct cifs_ses *ses = tcon->ses; |
63eb3def | 1615 | struct kvec iov[4]; |
da502f7d | 1616 | struct kvec rsp_iov; |
2503a0db PS |
1617 | int resp_buftype; |
1618 | int uni_path_len; | |
b8c32dbb PS |
1619 | __le16 *copy_path = NULL; |
1620 | int copy_size; | |
2503a0db | 1621 | int rc = 0; |
da502f7d | 1622 | unsigned int n_iov = 2; |
ca81983f | 1623 | __u32 file_attributes = 0; |
663a9621 | 1624 | char *dhc_buf = NULL, *lc_buf = NULL; |
7fb8986e | 1625 | int flags = 0; |
2503a0db | 1626 | |
f96637be | 1627 | cifs_dbg(FYI, "create/open\n"); |
2503a0db PS |
1628 | |
1629 | if (ses && (ses->server)) | |
1630 | server = ses->server; | |
1631 | else | |
1632 | return -EIO; | |
1633 | ||
1634 | rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req); | |
1635 | if (rc) | |
1636 | return rc; | |
1637 | ||
7fb8986e PS |
1638 | if (encryption_required(tcon)) |
1639 | flags |= CIFS_TRANSFORM_REQ; | |
1640 | ||
064f6047 | 1641 | if (oparms->create_options & CREATE_OPTION_READONLY) |
ca81983f | 1642 | file_attributes |= ATTR_READONLY; |
db8b631d SF |
1643 | if (oparms->create_options & CREATE_OPTION_SPECIAL) |
1644 | file_attributes |= ATTR_SYSTEM; | |
ca81983f | 1645 | |
2503a0db | 1646 | req->ImpersonationLevel = IL_IMPERSONATION; |
064f6047 | 1647 | req->DesiredAccess = cpu_to_le32(oparms->desired_access); |
2503a0db PS |
1648 | /* File attributes ignored on open (used in create though) */ |
1649 | req->FileAttributes = cpu_to_le32(file_attributes); | |
1650 | req->ShareAccess = FILE_SHARE_ALL_LE; | |
064f6047 PS |
1651 | req->CreateDisposition = cpu_to_le32(oparms->disposition); |
1652 | req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK); | |
2503a0db PS |
1653 | |
1654 | iov[0].iov_base = (char *)req; | |
1655 | /* 4 for rfc1002 length field */ | |
1656 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
59aa3718 PS |
1657 | /* -1 since last byte is buf[0] which is sent below (path) */ |
1658 | iov[0].iov_len--; | |
f0712928 AA |
1659 | |
1660 | req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4); | |
1661 | ||
1662 | /* [MS-SMB2] 2.2.13 NameOffset: | |
1663 | * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of | |
1664 | * the SMB2 header, the file name includes a prefix that will | |
1665 | * be processed during DFS name normalization as specified in | |
1666 | * section 3.3.5.9. Otherwise, the file name is relative to | |
1667 | * the share that is identified by the TreeId in the SMB2 | |
1668 | * header. | |
1669 | */ | |
1670 | if (tcon->share_flags & SHI1005_FLAGS_DFS) { | |
1671 | int name_len; | |
1672 | ||
1673 | req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS; | |
1674 | rc = alloc_path_with_tree_prefix(©_path, ©_size, | |
1675 | &name_len, | |
1676 | tcon->treeName, path); | |
1677 | if (rc) | |
1678 | return rc; | |
1679 | req->NameLength = cpu_to_le16(name_len * 2); | |
59aa3718 PS |
1680 | uni_path_len = copy_size; |
1681 | path = copy_path; | |
f0712928 AA |
1682 | } else { |
1683 | uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2; | |
1684 | /* MUST set path len (NameLength) to 0 opening root of share */ | |
1685 | req->NameLength = cpu_to_le16(uni_path_len - 2); | |
1686 | if (uni_path_len % 8 != 0) { | |
1687 | copy_size = roundup(uni_path_len, 8); | |
1688 | copy_path = kzalloc(copy_size, GFP_KERNEL); | |
1689 | if (!copy_path) | |
1690 | return -ENOMEM; | |
1691 | memcpy((char *)copy_path, (const char *)path, | |
1692 | uni_path_len); | |
1693 | uni_path_len = copy_size; | |
1694 | path = copy_path; | |
1695 | } | |
2503a0db PS |
1696 | } |
1697 | ||
59aa3718 PS |
1698 | iov[1].iov_len = uni_path_len; |
1699 | iov[1].iov_base = path; | |
1700 | /* -1 since last byte is buf[0] which was counted in smb2_buf_len */ | |
1701 | inc_rfc1001_len(req, uni_path_len - 1); | |
1702 | ||
b8c32dbb PS |
1703 | if (!server->oplocks) |
1704 | *oplock = SMB2_OPLOCK_LEVEL_NONE; | |
1705 | ||
a41a28bd | 1706 | if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) || |
b8c32dbb PS |
1707 | *oplock == SMB2_OPLOCK_LEVEL_NONE) |
1708 | req->RequestedOplockLevel = *oplock; | |
1709 | else { | |
da502f7d | 1710 | rc = add_lease_context(server, iov, &n_iov, oplock); |
d22cbfec | 1711 | if (rc) { |
b8c32dbb PS |
1712 | cifs_small_buf_release(req); |
1713 | kfree(copy_path); | |
d22cbfec | 1714 | return rc; |
b8c32dbb | 1715 | } |
da502f7d | 1716 | lc_buf = iov[n_iov-1].iov_base; |
b8c32dbb PS |
1717 | } |
1718 | ||
63eb3def PS |
1719 | if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) { |
1720 | /* need to set Next field of lease context if we request it */ | |
a41a28bd | 1721 | if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) { |
63eb3def | 1722 | struct create_context *ccontext = |
da502f7d | 1723 | (struct create_context *)iov[n_iov-1].iov_base; |
1c46943f | 1724 | ccontext->Next = |
a41a28bd | 1725 | cpu_to_le32(server->vals->create_lease_size); |
63eb3def | 1726 | } |
b56eae4d | 1727 | |
da502f7d | 1728 | rc = add_durable_context(iov, &n_iov, oparms, |
b56eae4d | 1729 | tcon->use_persistent); |
63eb3def PS |
1730 | if (rc) { |
1731 | cifs_small_buf_release(req); | |
1732 | kfree(copy_path); | |
663a9621 | 1733 | kfree(lc_buf); |
63eb3def PS |
1734 | return rc; |
1735 | } | |
da502f7d | 1736 | dhc_buf = iov[n_iov-1].iov_base; |
63eb3def PS |
1737 | } |
1738 | ||
7fb8986e | 1739 | rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
1740 | cifs_small_buf_release(req); |
1741 | rsp = (struct smb2_create_rsp *)rsp_iov.iov_base; | |
2503a0db PS |
1742 | |
1743 | if (rc != 0) { | |
1744 | cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); | |
b42bf888 PS |
1745 | if (err_buf) |
1746 | *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4, | |
1747 | GFP_KERNEL); | |
2503a0db PS |
1748 | goto creat_exit; |
1749 | } | |
1750 | ||
064f6047 PS |
1751 | oparms->fid->persistent_fid = rsp->PersistentFileId; |
1752 | oparms->fid->volatile_fid = rsp->VolatileFileId; | |
f0df737e PS |
1753 | |
1754 | if (buf) { | |
1755 | memcpy(buf, &rsp->CreationTime, 32); | |
1756 | buf->AllocationSize = rsp->AllocationSize; | |
1757 | buf->EndOfFile = rsp->EndofFile; | |
1758 | buf->Attributes = rsp->FileAttributes; | |
1759 | buf->NumberOfLinks = cpu_to_le32(1); | |
1760 | buf->DeletePending = 0; | |
1761 | } | |
2e44b288 | 1762 | |
b8c32dbb | 1763 | if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) |
42873b0a | 1764 | *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch); |
b8c32dbb PS |
1765 | else |
1766 | *oplock = rsp->OplockLevel; | |
2503a0db | 1767 | creat_exit: |
b8c32dbb | 1768 | kfree(copy_path); |
663a9621 PS |
1769 | kfree(lc_buf); |
1770 | kfree(dhc_buf); | |
2503a0db PS |
1771 | free_rsp_buf(resp_buftype, rsp); |
1772 | return rc; | |
1773 | } | |
1774 | ||
4a72dafa SF |
1775 | /* |
1776 | * SMB2 IOCTL is used for both IOCTLs and FSCTLs | |
1777 | */ | |
1778 | int | |
1779 | SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, | |
51146625 AA |
1780 | u64 volatile_fid, u32 opcode, bool is_fsctl, bool use_ipc, |
1781 | char *in_data, u32 indatalen, | |
1782 | char **out_data, u32 *plen /* returned data len */) | |
4a72dafa SF |
1783 | { |
1784 | struct smb2_ioctl_req *req; | |
1785 | struct smb2_ioctl_rsp *rsp; | |
31473fc4 | 1786 | struct smb2_sync_hdr *shdr; |
4a72dafa | 1787 | struct TCP_Server_Info *server; |
8e353106 | 1788 | struct cifs_ses *ses; |
4a72dafa | 1789 | struct kvec iov[2]; |
da502f7d | 1790 | struct kvec rsp_iov; |
4a72dafa | 1791 | int resp_buftype; |
da502f7d | 1792 | int n_iov; |
4a72dafa | 1793 | int rc = 0; |
7fb8986e | 1794 | int flags = 0; |
4a72dafa SF |
1795 | |
1796 | cifs_dbg(FYI, "SMB2 IOCTL\n"); | |
1797 | ||
3d1a3745 SF |
1798 | if (out_data != NULL) |
1799 | *out_data = NULL; | |
1800 | ||
4a72dafa SF |
1801 | /* zero out returned data len, in case of error */ |
1802 | if (plen) | |
1803 | *plen = 0; | |
1804 | ||
8e353106 SF |
1805 | if (tcon) |
1806 | ses = tcon->ses; | |
1807 | else | |
1808 | return -EIO; | |
1809 | ||
4a72dafa SF |
1810 | if (ses && (ses->server)) |
1811 | server = ses->server; | |
1812 | else | |
1813 | return -EIO; | |
1814 | ||
1815 | rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req); | |
1816 | if (rc) | |
1817 | return rc; | |
1818 | ||
51146625 AA |
1819 | if (use_ipc) { |
1820 | if (ses->ipc_tid == 0) { | |
1821 | cifs_small_buf_release(req); | |
1822 | return -ENOTCONN; | |
1823 | } | |
1824 | ||
1825 | cifs_dbg(FYI, "replacing tid 0x%x with IPC tid 0x%x\n", | |
1826 | req->hdr.sync_hdr.TreeId, ses->ipc_tid); | |
1827 | req->hdr.sync_hdr.TreeId = ses->ipc_tid; | |
1828 | } | |
7fb8986e PS |
1829 | if (encryption_required(tcon)) |
1830 | flags |= CIFS_TRANSFORM_REQ; | |
1831 | ||
4a72dafa SF |
1832 | req->CtlCode = cpu_to_le32(opcode); |
1833 | req->PersistentFileId = persistent_fid; | |
1834 | req->VolatileFileId = volatile_fid; | |
1835 | ||
1836 | if (indatalen) { | |
1837 | req->InputCount = cpu_to_le32(indatalen); | |
1838 | /* do not set InputOffset if no input data */ | |
1839 | req->InputOffset = | |
1840 | cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4); | |
1841 | iov[1].iov_base = in_data; | |
1842 | iov[1].iov_len = indatalen; | |
da502f7d | 1843 | n_iov = 2; |
4a72dafa | 1844 | } else |
da502f7d | 1845 | n_iov = 1; |
4a72dafa SF |
1846 | |
1847 | req->OutputOffset = 0; | |
1848 | req->OutputCount = 0; /* MBZ */ | |
1849 | ||
1850 | /* | |
1851 | * Could increase MaxOutputResponse, but that would require more | |
1852 | * than one credit. Windows typically sets this smaller, but for some | |
1853 | * ioctls it may be useful to allow server to send more. No point | |
1854 | * limiting what the server can send as long as fits in one credit | |
1855 | */ | |
1856 | req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */ | |
1857 | ||
1858 | if (is_fsctl) | |
1859 | req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL); | |
1860 | else | |
1861 | req->Flags = 0; | |
1862 | ||
1863 | iov[0].iov_base = (char *)req; | |
4a72dafa | 1864 | |
7ff8d45c SF |
1865 | /* |
1866 | * If no input data, the size of ioctl struct in | |
1867 | * protocol spec still includes a 1 byte data buffer, | |
1868 | * but if input data passed to ioctl, we do not | |
1869 | * want to double count this, so we do not send | |
1870 | * the dummy one byte of data in iovec[0] if sending | |
1871 | * input data (in iovec[1]). We also must add 4 bytes | |
1872 | * in first iovec to allow for rfc1002 length field. | |
1873 | */ | |
1874 | ||
1875 | if (indatalen) { | |
1876 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
1877 | inc_rfc1001_len(req, indatalen - 1); | |
1878 | } else | |
1879 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1880 | ||
4a72dafa | 1881 | |
7fb8986e | 1882 | rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
1883 | cifs_small_buf_release(req); |
1884 | rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base; | |
4a72dafa | 1885 | |
9bf0c9cd | 1886 | if ((rc != 0) && (rc != -EINVAL)) { |
8e353106 | 1887 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
4a72dafa | 1888 | goto ioctl_exit; |
9bf0c9cd SF |
1889 | } else if (rc == -EINVAL) { |
1890 | if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) && | |
1891 | (opcode != FSCTL_SRV_COPYCHUNK)) { | |
8e353106 | 1892 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); |
9bf0c9cd SF |
1893 | goto ioctl_exit; |
1894 | } | |
4a72dafa SF |
1895 | } |
1896 | ||
1897 | /* check if caller wants to look at return data or just return rc */ | |
1898 | if ((plen == NULL) || (out_data == NULL)) | |
1899 | goto ioctl_exit; | |
1900 | ||
1901 | *plen = le32_to_cpu(rsp->OutputCount); | |
1902 | ||
1903 | /* We check for obvious errors in the output buffer length and offset */ | |
1904 | if (*plen == 0) | |
1905 | goto ioctl_exit; /* server returned no data */ | |
1906 | else if (*plen > 0xFF00) { | |
1907 | cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen); | |
1908 | *plen = 0; | |
1909 | rc = -EIO; | |
1910 | goto ioctl_exit; | |
1911 | } | |
1912 | ||
1913 | if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) { | |
1914 | cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen, | |
1915 | le32_to_cpu(rsp->OutputOffset)); | |
1916 | *plen = 0; | |
1917 | rc = -EIO; | |
1918 | goto ioctl_exit; | |
1919 | } | |
1920 | ||
1921 | *out_data = kmalloc(*plen, GFP_KERNEL); | |
1922 | if (*out_data == NULL) { | |
1923 | rc = -ENOMEM; | |
1924 | goto ioctl_exit; | |
1925 | } | |
1926 | ||
31473fc4 PS |
1927 | shdr = get_sync_hdr(rsp); |
1928 | memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen); | |
4a72dafa SF |
1929 | ioctl_exit: |
1930 | free_rsp_buf(resp_buftype, rsp); | |
1931 | return rc; | |
1932 | } | |
1933 | ||
64a5cfa6 SF |
1934 | /* |
1935 | * Individual callers to ioctl worker function follow | |
1936 | */ | |
1937 | ||
1938 | int | |
1939 | SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon, | |
1940 | u64 persistent_fid, u64 volatile_fid) | |
1941 | { | |
1942 | int rc; | |
64a5cfa6 SF |
1943 | struct compress_ioctl fsctl_input; |
1944 | char *ret_data = NULL; | |
1945 | ||
1946 | fsctl_input.CompressionState = | |
bc09d141 | 1947 | cpu_to_le16(COMPRESSION_FORMAT_DEFAULT); |
64a5cfa6 SF |
1948 | |
1949 | rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid, | |
1950 | FSCTL_SET_COMPRESSION, true /* is_fsctl */, | |
51146625 | 1951 | false /* use_ipc */, |
64a5cfa6 SF |
1952 | (char *)&fsctl_input /* data input */, |
1953 | 2 /* in data len */, &ret_data /* out data */, NULL); | |
1954 | ||
1955 | cifs_dbg(FYI, "set compression rc %d\n", rc); | |
64a5cfa6 SF |
1956 | |
1957 | return rc; | |
1958 | } | |
1959 | ||
2503a0db PS |
1960 | int |
1961 | SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, | |
1962 | u64 persistent_fid, u64 volatile_fid) | |
1963 | { | |
1964 | struct smb2_close_req *req; | |
1965 | struct smb2_close_rsp *rsp; | |
1966 | struct TCP_Server_Info *server; | |
1967 | struct cifs_ses *ses = tcon->ses; | |
1968 | struct kvec iov[1]; | |
da502f7d | 1969 | struct kvec rsp_iov; |
2503a0db PS |
1970 | int resp_buftype; |
1971 | int rc = 0; | |
7fb8986e | 1972 | int flags = 0; |
2503a0db | 1973 | |
f96637be | 1974 | cifs_dbg(FYI, "Close\n"); |
2503a0db PS |
1975 | |
1976 | if (ses && (ses->server)) | |
1977 | server = ses->server; | |
1978 | else | |
1979 | return -EIO; | |
1980 | ||
1981 | rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req); | |
1982 | if (rc) | |
1983 | return rc; | |
1984 | ||
7fb8986e PS |
1985 | if (encryption_required(tcon)) |
1986 | flags |= CIFS_TRANSFORM_REQ; | |
1987 | ||
2503a0db PS |
1988 | req->PersistentFileId = persistent_fid; |
1989 | req->VolatileFileId = volatile_fid; | |
1990 | ||
1991 | iov[0].iov_base = (char *)req; | |
1992 | /* 4 for rfc1002 length field */ | |
1993 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1994 | ||
7fb8986e | 1995 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
1996 | cifs_small_buf_release(req); |
1997 | rsp = (struct smb2_close_rsp *)rsp_iov.iov_base; | |
2503a0db PS |
1998 | |
1999 | if (rc != 0) { | |
d4a029d2 | 2000 | cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE); |
2503a0db PS |
2001 | goto close_exit; |
2002 | } | |
2003 | ||
2503a0db PS |
2004 | /* BB FIXME - decode close response, update inode for caching */ |
2005 | ||
2006 | close_exit: | |
2007 | free_rsp_buf(resp_buftype, rsp); | |
2008 | return rc; | |
2009 | } | |
be4cb9e3 PS |
2010 | |
2011 | static int | |
2012 | validate_buf(unsigned int offset, unsigned int buffer_length, | |
2013 | struct smb2_hdr *hdr, unsigned int min_buf_size) | |
2014 | ||
2015 | { | |
2016 | unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length); | |
2017 | char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr; | |
2018 | char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr; | |
2019 | char *end_of_buf = begin_of_buf + buffer_length; | |
2020 | ||
2021 | ||
2022 | if (buffer_length < min_buf_size) { | |
f96637be JP |
2023 | cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n", |
2024 | buffer_length, min_buf_size); | |
be4cb9e3 PS |
2025 | return -EINVAL; |
2026 | } | |
2027 | ||
2028 | /* check if beyond RFC1001 maximum length */ | |
2029 | if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) { | |
f96637be JP |
2030 | cifs_dbg(VFS, "buffer length %d or smb length %d too large\n", |
2031 | buffer_length, smb_len); | |
be4cb9e3 PS |
2032 | return -EINVAL; |
2033 | } | |
2034 | ||
2035 | if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) { | |
f96637be | 2036 | cifs_dbg(VFS, "illegal server response, bad offset to data\n"); |
be4cb9e3 PS |
2037 | return -EINVAL; |
2038 | } | |
2039 | ||
2040 | return 0; | |
2041 | } | |
2042 | ||
2043 | /* | |
2044 | * If SMB buffer fields are valid, copy into temporary buffer to hold result. | |
2045 | * Caller must free buffer. | |
2046 | */ | |
2047 | static int | |
2048 | validate_and_copy_buf(unsigned int offset, unsigned int buffer_length, | |
2049 | struct smb2_hdr *hdr, unsigned int minbufsize, | |
2050 | char *data) | |
2051 | ||
2052 | { | |
2053 | char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr; | |
2054 | int rc; | |
2055 | ||
2056 | if (!data) | |
2057 | return -EINVAL; | |
2058 | ||
2059 | rc = validate_buf(offset, buffer_length, hdr, minbufsize); | |
2060 | if (rc) | |
2061 | return rc; | |
2062 | ||
2063 | memcpy(data, begin_of_buf, buffer_length); | |
2064 | ||
2065 | return 0; | |
2066 | } | |
2067 | ||
f0df737e PS |
2068 | static int |
2069 | query_info(const unsigned int xid, struct cifs_tcon *tcon, | |
2070 | u64 persistent_fid, u64 volatile_fid, u8 info_class, | |
2071 | size_t output_len, size_t min_len, void *data) | |
be4cb9e3 PS |
2072 | { |
2073 | struct smb2_query_info_req *req; | |
2074 | struct smb2_query_info_rsp *rsp = NULL; | |
2075 | struct kvec iov[2]; | |
da502f7d | 2076 | struct kvec rsp_iov; |
be4cb9e3 PS |
2077 | int rc = 0; |
2078 | int resp_buftype; | |
2079 | struct TCP_Server_Info *server; | |
2080 | struct cifs_ses *ses = tcon->ses; | |
7fb8986e | 2081 | int flags = 0; |
be4cb9e3 | 2082 | |
f96637be | 2083 | cifs_dbg(FYI, "Query Info\n"); |
be4cb9e3 PS |
2084 | |
2085 | if (ses && (ses->server)) | |
2086 | server = ses->server; | |
2087 | else | |
2088 | return -EIO; | |
2089 | ||
2090 | rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req); | |
2091 | if (rc) | |
2092 | return rc; | |
2093 | ||
7fb8986e PS |
2094 | if (encryption_required(tcon)) |
2095 | flags |= CIFS_TRANSFORM_REQ; | |
2096 | ||
be4cb9e3 | 2097 | req->InfoType = SMB2_O_INFO_FILE; |
f0df737e | 2098 | req->FileInfoClass = info_class; |
be4cb9e3 PS |
2099 | req->PersistentFileId = persistent_fid; |
2100 | req->VolatileFileId = volatile_fid; | |
2101 | /* 4 for rfc1002 length field and 1 for Buffer */ | |
2102 | req->InputBufferOffset = | |
2103 | cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4); | |
f0df737e | 2104 | req->OutputBufferLength = cpu_to_le32(output_len); |
be4cb9e3 PS |
2105 | |
2106 | iov[0].iov_base = (char *)req; | |
2107 | /* 4 for rfc1002 length field */ | |
2108 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
2109 | ||
7fb8986e | 2110 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
2111 | cifs_small_buf_release(req); |
2112 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; | |
e5d04887 | 2113 | |
be4cb9e3 PS |
2114 | if (rc) { |
2115 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); | |
2116 | goto qinf_exit; | |
2117 | } | |
2118 | ||
be4cb9e3 PS |
2119 | rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset), |
2120 | le32_to_cpu(rsp->OutputBufferLength), | |
f0df737e | 2121 | &rsp->hdr, min_len, data); |
be4cb9e3 PS |
2122 | |
2123 | qinf_exit: | |
2124 | free_rsp_buf(resp_buftype, rsp); | |
2125 | return rc; | |
2126 | } | |
9094fad1 | 2127 | |
f0df737e PS |
2128 | int |
2129 | SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon, | |
2130 | u64 persistent_fid, u64 volatile_fid, | |
2131 | struct smb2_file_all_info *data) | |
2132 | { | |
2133 | return query_info(xid, tcon, persistent_fid, volatile_fid, | |
2134 | FILE_ALL_INFORMATION, | |
1bbe4997 | 2135 | sizeof(struct smb2_file_all_info) + PATH_MAX * 2, |
f0df737e PS |
2136 | sizeof(struct smb2_file_all_info), data); |
2137 | } | |
2138 | ||
2139 | int | |
2140 | SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon, | |
2141 | u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid) | |
2142 | { | |
2143 | return query_info(xid, tcon, persistent_fid, volatile_fid, | |
2144 | FILE_INTERNAL_INFORMATION, | |
2145 | sizeof(struct smb2_file_internal_info), | |
2146 | sizeof(struct smb2_file_internal_info), uniqueid); | |
2147 | } | |
2148 | ||
9094fad1 PS |
2149 | /* |
2150 | * This is a no-op for now. We're not really interested in the reply, but | |
2151 | * rather in the fact that the server sent one and that server->lstrp | |
2152 | * gets updated. | |
2153 | * | |
2154 | * FIXME: maybe we should consider checking that the reply matches request? | |
2155 | */ | |
2156 | static void | |
2157 | smb2_echo_callback(struct mid_q_entry *mid) | |
2158 | { | |
2159 | struct TCP_Server_Info *server = mid->callback_data; | |
31473fc4 | 2160 | struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf; |
9094fad1 PS |
2161 | unsigned int credits_received = 1; |
2162 | ||
2163 | if (mid->mid_state == MID_RESPONSE_RECEIVED) | |
31473fc4 | 2164 | credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest); |
9094fad1 | 2165 | |
5fb4e288 | 2166 | mutex_lock(&server->srv_mutex); |
9094fad1 | 2167 | DeleteMidQEntry(mid); |
5fb4e288 | 2168 | mutex_unlock(&server->srv_mutex); |
9094fad1 PS |
2169 | add_credits(server, credits_received, CIFS_ECHO_OP); |
2170 | } | |
2171 | ||
53e0e11e PS |
2172 | void smb2_reconnect_server(struct work_struct *work) |
2173 | { | |
2174 | struct TCP_Server_Info *server = container_of(work, | |
2175 | struct TCP_Server_Info, reconnect.work); | |
2176 | struct cifs_ses *ses; | |
2177 | struct cifs_tcon *tcon, *tcon2; | |
2178 | struct list_head tmp_list; | |
2179 | int tcon_exist = false; | |
2180 | ||
2181 | /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */ | |
2182 | mutex_lock(&server->reconnect_mutex); | |
2183 | ||
2184 | INIT_LIST_HEAD(&tmp_list); | |
2185 | cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n"); | |
2186 | ||
2187 | spin_lock(&cifs_tcp_ses_lock); | |
2188 | list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { | |
2189 | list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { | |
96a988ff | 2190 | if (tcon->need_reconnect || tcon->need_reopen_files) { |
53e0e11e PS |
2191 | tcon->tc_count++; |
2192 | list_add_tail(&tcon->rlist, &tmp_list); | |
2193 | tcon_exist = true; | |
2194 | } | |
2195 | } | |
2196 | } | |
2197 | /* | |
2198 | * Get the reference to server struct to be sure that the last call of | |
2199 | * cifs_put_tcon() in the loop below won't release the server pointer. | |
2200 | */ | |
2201 | if (tcon_exist) | |
2202 | server->srv_count++; | |
2203 | ||
2204 | spin_unlock(&cifs_tcp_ses_lock); | |
2205 | ||
2206 | list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) { | |
96a988ff PS |
2207 | if (!smb2_reconnect(SMB2_INTERNAL_CMD, tcon)) |
2208 | cifs_reopen_persistent_handles(tcon); | |
53e0e11e PS |
2209 | list_del_init(&tcon->rlist); |
2210 | cifs_put_tcon(tcon); | |
2211 | } | |
2212 | ||
2213 | cifs_dbg(FYI, "Reconnecting tcons finished\n"); | |
2214 | mutex_unlock(&server->reconnect_mutex); | |
2215 | ||
2216 | /* now we can safely release srv struct */ | |
2217 | if (tcon_exist) | |
2218 | cifs_put_tcp_session(server, 1); | |
2219 | } | |
2220 | ||
9094fad1 PS |
2221 | int |
2222 | SMB2_echo(struct TCP_Server_Info *server) | |
2223 | { | |
2224 | struct smb2_echo_req *req; | |
2225 | int rc = 0; | |
738f9de5 PS |
2226 | struct kvec iov[2]; |
2227 | struct smb_rqst rqst = { .rq_iov = iov, | |
2228 | .rq_nvec = 2 }; | |
9094fad1 | 2229 | |
f96637be | 2230 | cifs_dbg(FYI, "In echo request\n"); |
9094fad1 | 2231 | |
4fcd1813 | 2232 | if (server->tcpStatus == CifsNeedNegotiate) { |
53e0e11e PS |
2233 | /* No need to send echo on newly established connections */ |
2234 | queue_delayed_work(cifsiod_wq, &server->reconnect, 0); | |
2235 | return rc; | |
4fcd1813 SF |
2236 | } |
2237 | ||
9094fad1 PS |
2238 | rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req); |
2239 | if (rc) | |
2240 | return rc; | |
2241 | ||
31473fc4 | 2242 | req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1); |
9094fad1 | 2243 | |
9094fad1 | 2244 | /* 4 for rfc1002 length field */ |
738f9de5 PS |
2245 | iov[0].iov_len = 4; |
2246 | iov[0].iov_base = (char *)req; | |
2247 | iov[1].iov_len = get_rfc1002_length(req); | |
2248 | iov[1].iov_base = (char *)req + 4; | |
9094fad1 | 2249 | |
9b7c18a2 PS |
2250 | rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL, |
2251 | server, CIFS_ECHO_OP); | |
9094fad1 | 2252 | if (rc) |
f96637be | 2253 | cifs_dbg(FYI, "Echo request failed: %d\n", rc); |
9094fad1 PS |
2254 | |
2255 | cifs_small_buf_release(req); | |
2256 | return rc; | |
2257 | } | |
7a5cfb19 PS |
2258 | |
2259 | int | |
2260 | SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, | |
2261 | u64 volatile_fid) | |
2262 | { | |
2263 | struct smb2_flush_req *req; | |
2264 | struct TCP_Server_Info *server; | |
2265 | struct cifs_ses *ses = tcon->ses; | |
2266 | struct kvec iov[1]; | |
da502f7d | 2267 | struct kvec rsp_iov; |
7a5cfb19 PS |
2268 | int resp_buftype; |
2269 | int rc = 0; | |
7fb8986e | 2270 | int flags = 0; |
7a5cfb19 | 2271 | |
f96637be | 2272 | cifs_dbg(FYI, "Flush\n"); |
7a5cfb19 PS |
2273 | |
2274 | if (ses && (ses->server)) | |
2275 | server = ses->server; | |
2276 | else | |
2277 | return -EIO; | |
2278 | ||
2279 | rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req); | |
2280 | if (rc) | |
2281 | return rc; | |
2282 | ||
7fb8986e PS |
2283 | if (encryption_required(tcon)) |
2284 | flags |= CIFS_TRANSFORM_REQ; | |
2285 | ||
7a5cfb19 PS |
2286 | req->PersistentFileId = persistent_fid; |
2287 | req->VolatileFileId = volatile_fid; | |
2288 | ||
2289 | iov[0].iov_base = (char *)req; | |
2290 | /* 4 for rfc1002 length field */ | |
2291 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
2292 | ||
7fb8986e | 2293 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov); |
da502f7d | 2294 | cifs_small_buf_release(req); |
7a5cfb19 | 2295 | |
dfebe400 | 2296 | if (rc != 0) |
7a5cfb19 PS |
2297 | cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE); |
2298 | ||
da502f7d | 2299 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
7a5cfb19 PS |
2300 | return rc; |
2301 | } | |
09a4707e PS |
2302 | |
2303 | /* | |
2304 | * To form a chain of read requests, any read requests after the first should | |
2305 | * have the end_of_chain boolean set to true. | |
2306 | */ | |
2307 | static int | |
738f9de5 PS |
2308 | smb2_new_read_req(void **buf, unsigned int *total_len, |
2309 | struct cifs_io_parms *io_parms, unsigned int remaining_bytes, | |
2310 | int request_type) | |
09a4707e PS |
2311 | { |
2312 | int rc = -EACCES; | |
b8f57ee8 | 2313 | struct smb2_read_plain_req *req = NULL; |
31473fc4 | 2314 | struct smb2_sync_hdr *shdr; |
09a4707e | 2315 | |
b8f57ee8 PS |
2316 | rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req, |
2317 | total_len); | |
09a4707e PS |
2318 | if (rc) |
2319 | return rc; | |
2320 | if (io_parms->tcon->ses->server == NULL) | |
2321 | return -ECONNABORTED; | |
2322 | ||
b8f57ee8 | 2323 | shdr = &req->sync_hdr; |
31473fc4 | 2324 | shdr->ProcessId = cpu_to_le32(io_parms->pid); |
09a4707e PS |
2325 | |
2326 | req->PersistentFileId = io_parms->persistent_fid; | |
2327 | req->VolatileFileId = io_parms->volatile_fid; | |
2328 | req->ReadChannelInfoOffset = 0; /* reserved */ | |
2329 | req->ReadChannelInfoLength = 0; /* reserved */ | |
2330 | req->Channel = 0; /* reserved */ | |
2331 | req->MinimumCount = 0; | |
2332 | req->Length = cpu_to_le32(io_parms->length); | |
2333 | req->Offset = cpu_to_le64(io_parms->offset); | |
2334 | ||
2335 | if (request_type & CHAINED_REQUEST) { | |
2336 | if (!(request_type & END_OF_CHAIN)) { | |
b8f57ee8 PS |
2337 | /* next 8-byte aligned request */ |
2338 | *total_len = DIV_ROUND_UP(*total_len, 8) * 8; | |
2339 | shdr->NextCommand = cpu_to_le32(*total_len); | |
09a4707e | 2340 | } else /* END_OF_CHAIN */ |
31473fc4 | 2341 | shdr->NextCommand = 0; |
09a4707e | 2342 | if (request_type & RELATED_REQUEST) { |
31473fc4 | 2343 | shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS; |
09a4707e PS |
2344 | /* |
2345 | * Related requests use info from previous read request | |
2346 | * in chain. | |
2347 | */ | |
31473fc4 PS |
2348 | shdr->SessionId = 0xFFFFFFFF; |
2349 | shdr->TreeId = 0xFFFFFFFF; | |
09a4707e PS |
2350 | req->PersistentFileId = 0xFFFFFFFF; |
2351 | req->VolatileFileId = 0xFFFFFFFF; | |
2352 | } | |
2353 | } | |
2354 | if (remaining_bytes > io_parms->length) | |
2355 | req->RemainingBytes = cpu_to_le32(remaining_bytes); | |
2356 | else | |
2357 | req->RemainingBytes = 0; | |
2358 | ||
738f9de5 | 2359 | *buf = req; |
09a4707e PS |
2360 | return rc; |
2361 | } | |
2362 | ||
2363 | static void | |
2364 | smb2_readv_callback(struct mid_q_entry *mid) | |
2365 | { | |
2366 | struct cifs_readdata *rdata = mid->callback_data; | |
2367 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); | |
2368 | struct TCP_Server_Info *server = tcon->ses->server; | |
738f9de5 PS |
2369 | struct smb2_sync_hdr *shdr = |
2370 | (struct smb2_sync_hdr *)rdata->iov[1].iov_base; | |
09a4707e | 2371 | unsigned int credits_received = 1; |
738f9de5 PS |
2372 | struct smb_rqst rqst = { .rq_iov = rdata->iov, |
2373 | .rq_nvec = 2, | |
8321fec4 JL |
2374 | .rq_pages = rdata->pages, |
2375 | .rq_npages = rdata->nr_pages, | |
2376 | .rq_pagesz = rdata->pagesz, | |
2377 | .rq_tailsz = rdata->tailsz }; | |
09a4707e | 2378 | |
f96637be JP |
2379 | cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n", |
2380 | __func__, mid->mid, mid->mid_state, rdata->result, | |
2381 | rdata->bytes); | |
09a4707e PS |
2382 | |
2383 | switch (mid->mid_state) { | |
2384 | case MID_RESPONSE_RECEIVED: | |
31473fc4 | 2385 | credits_received = le16_to_cpu(shdr->CreditRequest); |
09a4707e | 2386 | /* result already set, check signature */ |
4326ed2f | 2387 | if (server->sign && !mid->decrypted) { |
3c1bf7e4 PS |
2388 | int rc; |
2389 | ||
0b688cfc | 2390 | rc = smb2_verify_signature(&rqst, server); |
3c1bf7e4 | 2391 | if (rc) |
f96637be JP |
2392 | cifs_dbg(VFS, "SMB signature verification returned error = %d\n", |
2393 | rc); | |
3c1bf7e4 | 2394 | } |
09a4707e | 2395 | /* FIXME: should this be counted toward the initiating task? */ |
34a54d61 PS |
2396 | task_io_account_read(rdata->got_bytes); |
2397 | cifs_stats_bytes_read(tcon, rdata->got_bytes); | |
09a4707e PS |
2398 | break; |
2399 | case MID_REQUEST_SUBMITTED: | |
2400 | case MID_RETRY_NEEDED: | |
2401 | rdata->result = -EAGAIN; | |
d913ed17 PS |
2402 | if (server->sign && rdata->got_bytes) |
2403 | /* reset bytes number since we can not check a sign */ | |
2404 | rdata->got_bytes = 0; | |
2405 | /* FIXME: should this be counted toward the initiating task? */ | |
2406 | task_io_account_read(rdata->got_bytes); | |
2407 | cifs_stats_bytes_read(tcon, rdata->got_bytes); | |
09a4707e PS |
2408 | break; |
2409 | default: | |
2410 | if (rdata->result != -ENODATA) | |
2411 | rdata->result = -EIO; | |
2412 | } | |
2413 | ||
2414 | if (rdata->result) | |
2415 | cifs_stats_fail_inc(tcon, SMB2_READ_HE); | |
2416 | ||
2417 | queue_work(cifsiod_wq, &rdata->work); | |
5fb4e288 | 2418 | mutex_lock(&server->srv_mutex); |
09a4707e | 2419 | DeleteMidQEntry(mid); |
5fb4e288 | 2420 | mutex_unlock(&server->srv_mutex); |
09a4707e PS |
2421 | add_credits(server, credits_received, 0); |
2422 | } | |
2423 | ||
738f9de5 | 2424 | /* smb2_async_readv - send an async read, and set up mid to handle result */ |
09a4707e PS |
2425 | int |
2426 | smb2_async_readv(struct cifs_readdata *rdata) | |
2427 | { | |
bed9da02 | 2428 | int rc, flags = 0; |
31473fc4 PS |
2429 | char *buf; |
2430 | struct smb2_sync_hdr *shdr; | |
09a4707e | 2431 | struct cifs_io_parms io_parms; |
738f9de5 PS |
2432 | struct smb_rqst rqst = { .rq_iov = rdata->iov, |
2433 | .rq_nvec = 2 }; | |
bed9da02 | 2434 | struct TCP_Server_Info *server; |
738f9de5 | 2435 | unsigned int total_len; |
b8f57ee8 | 2436 | __be32 req_len; |
09a4707e | 2437 | |
f96637be JP |
2438 | cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n", |
2439 | __func__, rdata->offset, rdata->bytes); | |
09a4707e PS |
2440 | |
2441 | io_parms.tcon = tlink_tcon(rdata->cfile->tlink); | |
2442 | io_parms.offset = rdata->offset; | |
2443 | io_parms.length = rdata->bytes; | |
2444 | io_parms.persistent_fid = rdata->cfile->fid.persistent_fid; | |
2445 | io_parms.volatile_fid = rdata->cfile->fid.volatile_fid; | |
2446 | io_parms.pid = rdata->pid; | |
bed9da02 PS |
2447 | |
2448 | server = io_parms.tcon->ses->server; | |
2449 | ||
738f9de5 | 2450 | rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0); |
bed9da02 PS |
2451 | if (rc) { |
2452 | if (rc == -EAGAIN && rdata->credits) { | |
2453 | /* credits was reset by reconnect */ | |
2454 | rdata->credits = 0; | |
2455 | /* reduce in_flight value since we won't send the req */ | |
2456 | spin_lock(&server->req_lock); | |
2457 | server->in_flight--; | |
2458 | spin_unlock(&server->req_lock); | |
2459 | } | |
09a4707e | 2460 | return rc; |
bed9da02 | 2461 | } |
09a4707e | 2462 | |
7fb8986e PS |
2463 | if (encryption_required(io_parms.tcon)) |
2464 | flags |= CIFS_TRANSFORM_REQ; | |
2465 | ||
b8f57ee8 PS |
2466 | req_len = cpu_to_be32(total_len); |
2467 | ||
2468 | rdata->iov[0].iov_base = &req_len; | |
2469 | rdata->iov[0].iov_len = sizeof(__be32); | |
2470 | rdata->iov[1].iov_base = buf; | |
2471 | rdata->iov[1].iov_len = total_len; | |
2472 | ||
2473 | shdr = (struct smb2_sync_hdr *)buf; | |
09a4707e | 2474 | |
bed9da02 | 2475 | if (rdata->credits) { |
31473fc4 | 2476 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes, |
bed9da02 | 2477 | SMB2_MAX_BUFFER_SIZE)); |
31473fc4 | 2478 | shdr->CreditRequest = shdr->CreditCharge; |
bed9da02 PS |
2479 | spin_lock(&server->req_lock); |
2480 | server->credits += rdata->credits - | |
31473fc4 | 2481 | le16_to_cpu(shdr->CreditCharge); |
bed9da02 PS |
2482 | spin_unlock(&server->req_lock); |
2483 | wake_up(&server->request_q); | |
7fb8986e | 2484 | flags |= CIFS_HAS_CREDITS; |
bed9da02 PS |
2485 | } |
2486 | ||
09a4707e | 2487 | kref_get(&rdata->refcount); |
fec344e3 | 2488 | rc = cifs_call_async(io_parms.tcon->ses->server, &rqst, |
09a4707e | 2489 | cifs_readv_receive, smb2_readv_callback, |
4326ed2f | 2490 | smb3_handle_read_data, rdata, flags); |
e5d04887 | 2491 | if (rc) { |
09a4707e | 2492 | kref_put(&rdata->refcount, cifs_readdata_release); |
e5d04887 PS |
2493 | cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE); |
2494 | } | |
09a4707e PS |
2495 | |
2496 | cifs_small_buf_release(buf); | |
2497 | return rc; | |
2498 | } | |
33319141 | 2499 | |
d8e05039 PS |
2500 | int |
2501 | SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, | |
2502 | unsigned int *nbytes, char **buf, int *buf_type) | |
2503 | { | |
2504 | int resp_buftype, rc = -EACCES; | |
b8f57ee8 | 2505 | struct smb2_read_plain_req *req = NULL; |
d8e05039 | 2506 | struct smb2_read_rsp *rsp = NULL; |
31473fc4 | 2507 | struct smb2_sync_hdr *shdr; |
b8f57ee8 | 2508 | struct kvec iov[2]; |
da502f7d | 2509 | struct kvec rsp_iov; |
738f9de5 | 2510 | unsigned int total_len; |
b8f57ee8 PS |
2511 | __be32 req_len; |
2512 | struct smb_rqst rqst = { .rq_iov = iov, | |
2513 | .rq_nvec = 2 }; | |
7fb8986e PS |
2514 | int flags = CIFS_LOG_ERROR; |
2515 | struct cifs_ses *ses = io_parms->tcon->ses; | |
d8e05039 PS |
2516 | |
2517 | *nbytes = 0; | |
738f9de5 | 2518 | rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0); |
d8e05039 PS |
2519 | if (rc) |
2520 | return rc; | |
2521 | ||
7fb8986e PS |
2522 | if (encryption_required(io_parms->tcon)) |
2523 | flags |= CIFS_TRANSFORM_REQ; | |
2524 | ||
b8f57ee8 | 2525 | req_len = cpu_to_be32(total_len); |
738f9de5 | 2526 | |
b8f57ee8 PS |
2527 | iov[0].iov_base = &req_len; |
2528 | iov[0].iov_len = sizeof(__be32); | |
2529 | iov[1].iov_base = req; | |
2530 | iov[1].iov_len = total_len; | |
2531 | ||
7fb8986e | 2532 | rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov); |
b8f57ee8 | 2533 | cifs_small_buf_release(req); |
d8e05039 | 2534 | |
da502f7d | 2535 | rsp = (struct smb2_read_rsp *)rsp_iov.iov_base; |
31473fc4 | 2536 | shdr = get_sync_hdr(rsp); |
d8e05039 | 2537 | |
31473fc4 | 2538 | if (shdr->Status == STATUS_END_OF_FILE) { |
da502f7d | 2539 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
d8e05039 PS |
2540 | return 0; |
2541 | } | |
2542 | ||
2543 | if (rc) { | |
2544 | cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE); | |
f96637be | 2545 | cifs_dbg(VFS, "Send error in read = %d\n", rc); |
d8e05039 PS |
2546 | } else { |
2547 | *nbytes = le32_to_cpu(rsp->DataLength); | |
2548 | if ((*nbytes > CIFS_MAX_MSGSIZE) || | |
2549 | (*nbytes > io_parms->length)) { | |
f96637be JP |
2550 | cifs_dbg(FYI, "bad length %d for count %d\n", |
2551 | *nbytes, io_parms->length); | |
d8e05039 PS |
2552 | rc = -EIO; |
2553 | *nbytes = 0; | |
2554 | } | |
2555 | } | |
2556 | ||
2557 | if (*buf) { | |
31473fc4 | 2558 | memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes); |
da502f7d | 2559 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
d8e05039 | 2560 | } else if (resp_buftype != CIFS_NO_BUFFER) { |
da502f7d | 2561 | *buf = rsp_iov.iov_base; |
d8e05039 PS |
2562 | if (resp_buftype == CIFS_SMALL_BUFFER) |
2563 | *buf_type = CIFS_SMALL_BUFFER; | |
2564 | else if (resp_buftype == CIFS_LARGE_BUFFER) | |
2565 | *buf_type = CIFS_LARGE_BUFFER; | |
2566 | } | |
2567 | return rc; | |
2568 | } | |
2569 | ||
33319141 PS |
2570 | /* |
2571 | * Check the mid_state and signature on received buffer (if any), and queue the | |
2572 | * workqueue completion task. | |
2573 | */ | |
2574 | static void | |
2575 | smb2_writev_callback(struct mid_q_entry *mid) | |
2576 | { | |
2577 | struct cifs_writedata *wdata = mid->callback_data; | |
2578 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); | |
5fb4e288 | 2579 | struct TCP_Server_Info *server = tcon->ses->server; |
33319141 PS |
2580 | unsigned int written; |
2581 | struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf; | |
2582 | unsigned int credits_received = 1; | |
2583 | ||
2584 | switch (mid->mid_state) { | |
2585 | case MID_RESPONSE_RECEIVED: | |
31473fc4 | 2586 | credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest); |
33319141 PS |
2587 | wdata->result = smb2_check_receive(mid, tcon->ses->server, 0); |
2588 | if (wdata->result != 0) | |
2589 | break; | |
2590 | ||
2591 | written = le32_to_cpu(rsp->DataLength); | |
2592 | /* | |
2593 | * Mask off high 16 bits when bytes written as returned | |
2594 | * by the server is greater than bytes requested by the | |
2595 | * client. OS/2 servers are known to set incorrect | |
2596 | * CountHigh values. | |
2597 | */ | |
2598 | if (written > wdata->bytes) | |
2599 | written &= 0xFFFF; | |
2600 | ||
2601 | if (written < wdata->bytes) | |
2602 | wdata->result = -ENOSPC; | |
2603 | else | |
2604 | wdata->bytes = written; | |
2605 | break; | |
2606 | case MID_REQUEST_SUBMITTED: | |
2607 | case MID_RETRY_NEEDED: | |
2608 | wdata->result = -EAGAIN; | |
2609 | break; | |
2610 | default: | |
2611 | wdata->result = -EIO; | |
2612 | break; | |
2613 | } | |
2614 | ||
2615 | if (wdata->result) | |
2616 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); | |
2617 | ||
2618 | queue_work(cifsiod_wq, &wdata->work); | |
5fb4e288 | 2619 | mutex_lock(&server->srv_mutex); |
33319141 | 2620 | DeleteMidQEntry(mid); |
5fb4e288 | 2621 | mutex_unlock(&server->srv_mutex); |
33319141 PS |
2622 | add_credits(tcon->ses->server, credits_received, 0); |
2623 | } | |
2624 | ||
2625 | /* smb2_async_writev - send an async write, and set up mid to handle result */ | |
2626 | int | |
4a5c80d7 SF |
2627 | smb2_async_writev(struct cifs_writedata *wdata, |
2628 | void (*release)(struct kref *kref)) | |
33319141 | 2629 | { |
cb7e9eab | 2630 | int rc = -EACCES, flags = 0; |
33319141 | 2631 | struct smb2_write_req *req = NULL; |
31473fc4 | 2632 | struct smb2_sync_hdr *shdr; |
33319141 | 2633 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); |
cb7e9eab | 2634 | struct TCP_Server_Info *server = tcon->ses->server; |
738f9de5 PS |
2635 | struct kvec iov[2]; |
2636 | struct smb_rqst rqst = { }; | |
33319141 PS |
2637 | |
2638 | rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req); | |
cb7e9eab PS |
2639 | if (rc) { |
2640 | if (rc == -EAGAIN && wdata->credits) { | |
2641 | /* credits was reset by reconnect */ | |
2642 | wdata->credits = 0; | |
2643 | /* reduce in_flight value since we won't send the req */ | |
2644 | spin_lock(&server->req_lock); | |
2645 | server->in_flight--; | |
2646 | spin_unlock(&server->req_lock); | |
2647 | } | |
33319141 | 2648 | goto async_writev_out; |
cb7e9eab | 2649 | } |
33319141 | 2650 | |
7fb8986e PS |
2651 | if (encryption_required(tcon)) |
2652 | flags |= CIFS_TRANSFORM_REQ; | |
2653 | ||
31473fc4 PS |
2654 | shdr = get_sync_hdr(req); |
2655 | shdr->ProcessId = cpu_to_le32(wdata->cfile->pid); | |
33319141 PS |
2656 | |
2657 | req->PersistentFileId = wdata->cfile->fid.persistent_fid; | |
2658 | req->VolatileFileId = wdata->cfile->fid.volatile_fid; | |
2659 | req->WriteChannelInfoOffset = 0; | |
2660 | req->WriteChannelInfoLength = 0; | |
2661 | req->Channel = 0; | |
2662 | req->Offset = cpu_to_le64(wdata->offset); | |
2663 | /* 4 for rfc1002 length field */ | |
2664 | req->DataOffset = cpu_to_le16( | |
2665 | offsetof(struct smb2_write_req, Buffer) - 4); | |
2666 | req->RemainingBytes = 0; | |
2667 | ||
2668 | /* 4 for rfc1002 length field and 1 for Buffer */ | |
738f9de5 PS |
2669 | iov[0].iov_len = 4; |
2670 | iov[0].iov_base = req; | |
2671 | iov[1].iov_len = get_rfc1002_length(req) - 1; | |
2672 | iov[1].iov_base = (char *)req + 4; | |
33319141 | 2673 | |
738f9de5 PS |
2674 | rqst.rq_iov = iov; |
2675 | rqst.rq_nvec = 2; | |
eddb079d JL |
2676 | rqst.rq_pages = wdata->pages; |
2677 | rqst.rq_npages = wdata->nr_pages; | |
2678 | rqst.rq_pagesz = wdata->pagesz; | |
2679 | rqst.rq_tailsz = wdata->tailsz; | |
33319141 | 2680 | |
f96637be JP |
2681 | cifs_dbg(FYI, "async write at %llu %u bytes\n", |
2682 | wdata->offset, wdata->bytes); | |
33319141 PS |
2683 | |
2684 | req->Length = cpu_to_le32(wdata->bytes); | |
2685 | ||
2686 | inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */); | |
2687 | ||
cb7e9eab | 2688 | if (wdata->credits) { |
31473fc4 | 2689 | shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes, |
cb7e9eab | 2690 | SMB2_MAX_BUFFER_SIZE)); |
31473fc4 | 2691 | shdr->CreditRequest = shdr->CreditCharge; |
cb7e9eab PS |
2692 | spin_lock(&server->req_lock); |
2693 | server->credits += wdata->credits - | |
31473fc4 | 2694 | le16_to_cpu(shdr->CreditCharge); |
cb7e9eab PS |
2695 | spin_unlock(&server->req_lock); |
2696 | wake_up(&server->request_q); | |
7fb8986e | 2697 | flags |= CIFS_HAS_CREDITS; |
cb7e9eab PS |
2698 | } |
2699 | ||
33319141 | 2700 | kref_get(&wdata->refcount); |
9b7c18a2 PS |
2701 | rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL, |
2702 | wdata, flags); | |
33319141 | 2703 | |
e5d04887 | 2704 | if (rc) { |
4a5c80d7 | 2705 | kref_put(&wdata->refcount, release); |
e5d04887 PS |
2706 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); |
2707 | } | |
33319141 | 2708 | |
33319141 PS |
2709 | async_writev_out: |
2710 | cifs_small_buf_release(req); | |
33319141 PS |
2711 | return rc; |
2712 | } | |
009d3443 PS |
2713 | |
2714 | /* | |
2715 | * SMB2_write function gets iov pointer to kvec array with n_vec as a length. | |
2716 | * The length field from io_parms must be at least 1 and indicates a number of | |
2717 | * elements with data to write that begins with position 1 in iov array. All | |
2718 | * data length is specified by count. | |
2719 | */ | |
2720 | int | |
2721 | SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, | |
2722 | unsigned int *nbytes, struct kvec *iov, int n_vec) | |
2723 | { | |
2724 | int rc = 0; | |
2725 | struct smb2_write_req *req = NULL; | |
2726 | struct smb2_write_rsp *rsp = NULL; | |
2727 | int resp_buftype; | |
da502f7d | 2728 | struct kvec rsp_iov; |
7fb8986e | 2729 | int flags = 0; |
da502f7d | 2730 | |
009d3443 PS |
2731 | *nbytes = 0; |
2732 | ||
2733 | if (n_vec < 1) | |
2734 | return rc; | |
2735 | ||
2736 | rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req); | |
2737 | if (rc) | |
2738 | return rc; | |
2739 | ||
2740 | if (io_parms->tcon->ses->server == NULL) | |
2741 | return -ECONNABORTED; | |
2742 | ||
7fb8986e PS |
2743 | if (encryption_required(io_parms->tcon)) |
2744 | flags |= CIFS_TRANSFORM_REQ; | |
2745 | ||
31473fc4 | 2746 | req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid); |
009d3443 PS |
2747 | |
2748 | req->PersistentFileId = io_parms->persistent_fid; | |
2749 | req->VolatileFileId = io_parms->volatile_fid; | |
2750 | req->WriteChannelInfoOffset = 0; | |
2751 | req->WriteChannelInfoLength = 0; | |
2752 | req->Channel = 0; | |
2753 | req->Length = cpu_to_le32(io_parms->length); | |
2754 | req->Offset = cpu_to_le64(io_parms->offset); | |
2755 | /* 4 for rfc1002 length field */ | |
2756 | req->DataOffset = cpu_to_le16( | |
2757 | offsetof(struct smb2_write_req, Buffer) - 4); | |
2758 | req->RemainingBytes = 0; | |
2759 | ||
2760 | iov[0].iov_base = (char *)req; | |
2761 | /* 4 for rfc1002 length field and 1 for Buffer */ | |
2762 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
2763 | ||
2764 | /* length of entire message including data to be written */ | |
2765 | inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */); | |
2766 | ||
2767 | rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1, | |
7fb8986e | 2768 | &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
2769 | cifs_small_buf_release(req); |
2770 | rsp = (struct smb2_write_rsp *)rsp_iov.iov_base; | |
009d3443 PS |
2771 | |
2772 | if (rc) { | |
2773 | cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE); | |
f96637be | 2774 | cifs_dbg(VFS, "Send error in write = %d\n", rc); |
e5d04887 | 2775 | } else |
009d3443 | 2776 | *nbytes = le32_to_cpu(rsp->DataLength); |
e5d04887 PS |
2777 | |
2778 | free_rsp_buf(resp_buftype, rsp); | |
009d3443 PS |
2779 | return rc; |
2780 | } | |
35143eb5 | 2781 | |
d324f08d PS |
2782 | static unsigned int |
2783 | num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size) | |
2784 | { | |
2785 | int len; | |
2786 | unsigned int entrycount = 0; | |
2787 | unsigned int next_offset = 0; | |
2788 | FILE_DIRECTORY_INFO *entryptr; | |
2789 | ||
2790 | if (bufstart == NULL) | |
2791 | return 0; | |
2792 | ||
2793 | entryptr = (FILE_DIRECTORY_INFO *)bufstart; | |
2794 | ||
2795 | while (1) { | |
2796 | entryptr = (FILE_DIRECTORY_INFO *) | |
2797 | ((char *)entryptr + next_offset); | |
2798 | ||
2799 | if ((char *)entryptr + size > end_of_buf) { | |
f96637be | 2800 | cifs_dbg(VFS, "malformed search entry would overflow\n"); |
d324f08d PS |
2801 | break; |
2802 | } | |
2803 | ||
2804 | len = le32_to_cpu(entryptr->FileNameLength); | |
2805 | if ((char *)entryptr + len + size > end_of_buf) { | |
f96637be JP |
2806 | cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n", |
2807 | end_of_buf); | |
d324f08d PS |
2808 | break; |
2809 | } | |
2810 | ||
2811 | *lastentry = (char *)entryptr; | |
2812 | entrycount++; | |
2813 | ||
2814 | next_offset = le32_to_cpu(entryptr->NextEntryOffset); | |
2815 | if (!next_offset) | |
2816 | break; | |
2817 | } | |
2818 | ||
2819 | return entrycount; | |
2820 | } | |
2821 | ||
2822 | /* | |
2823 | * Readdir/FindFirst | |
2824 | */ | |
2825 | int | |
2826 | SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, | |
2827 | u64 persistent_fid, u64 volatile_fid, int index, | |
2828 | struct cifs_search_info *srch_inf) | |
2829 | { | |
2830 | struct smb2_query_directory_req *req; | |
2831 | struct smb2_query_directory_rsp *rsp = NULL; | |
2832 | struct kvec iov[2]; | |
da502f7d | 2833 | struct kvec rsp_iov; |
d324f08d PS |
2834 | int rc = 0; |
2835 | int len; | |
75fdfc84 | 2836 | int resp_buftype = CIFS_NO_BUFFER; |
d324f08d PS |
2837 | unsigned char *bufptr; |
2838 | struct TCP_Server_Info *server; | |
2839 | struct cifs_ses *ses = tcon->ses; | |
2840 | __le16 asteriks = cpu_to_le16('*'); | |
2841 | char *end_of_smb; | |
2842 | unsigned int output_size = CIFSMaxBufSize; | |
2843 | size_t info_buf_size; | |
7fb8986e | 2844 | int flags = 0; |
d324f08d PS |
2845 | |
2846 | if (ses && (ses->server)) | |
2847 | server = ses->server; | |
2848 | else | |
2849 | return -EIO; | |
2850 | ||
2851 | rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req); | |
2852 | if (rc) | |
2853 | return rc; | |
2854 | ||
7fb8986e PS |
2855 | if (encryption_required(tcon)) |
2856 | flags |= CIFS_TRANSFORM_REQ; | |
2857 | ||
d324f08d PS |
2858 | switch (srch_inf->info_level) { |
2859 | case SMB_FIND_FILE_DIRECTORY_INFO: | |
2860 | req->FileInformationClass = FILE_DIRECTORY_INFORMATION; | |
2861 | info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1; | |
2862 | break; | |
2863 | case SMB_FIND_FILE_ID_FULL_DIR_INFO: | |
2864 | req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION; | |
2865 | info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1; | |
2866 | break; | |
2867 | default: | |
f96637be JP |
2868 | cifs_dbg(VFS, "info level %u isn't supported\n", |
2869 | srch_inf->info_level); | |
d324f08d PS |
2870 | rc = -EINVAL; |
2871 | goto qdir_exit; | |
2872 | } | |
2873 | ||
2874 | req->FileIndex = cpu_to_le32(index); | |
2875 | req->PersistentFileId = persistent_fid; | |
2876 | req->VolatileFileId = volatile_fid; | |
2877 | ||
2878 | len = 0x2; | |
2879 | bufptr = req->Buffer; | |
2880 | memcpy(bufptr, &asteriks, len); | |
2881 | ||
2882 | req->FileNameOffset = | |
2883 | cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4); | |
2884 | req->FileNameLength = cpu_to_le16(len); | |
2885 | /* | |
2886 | * BB could be 30 bytes or so longer if we used SMB2 specific | |
2887 | * buffer lengths, but this is safe and close enough. | |
2888 | */ | |
2889 | output_size = min_t(unsigned int, output_size, server->maxBuf); | |
2890 | output_size = min_t(unsigned int, output_size, 2 << 15); | |
2891 | req->OutputBufferLength = cpu_to_le32(output_size); | |
2892 | ||
2893 | iov[0].iov_base = (char *)req; | |
2894 | /* 4 for RFC1001 length and 1 for Buffer */ | |
2895 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
2896 | ||
2897 | iov[1].iov_base = (char *)(req->Buffer); | |
2898 | iov[1].iov_len = len; | |
2899 | ||
2900 | inc_rfc1001_len(req, len - 1 /* Buffer */); | |
2901 | ||
7fb8986e | 2902 | rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
2903 | cifs_small_buf_release(req); |
2904 | rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base; | |
e5d04887 | 2905 | |
d324f08d | 2906 | if (rc) { |
31473fc4 PS |
2907 | if (rc == -ENODATA && |
2908 | rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) { | |
52755808 PS |
2909 | srch_inf->endOfSearch = true; |
2910 | rc = 0; | |
2911 | } | |
d324f08d PS |
2912 | cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE); |
2913 | goto qdir_exit; | |
2914 | } | |
d324f08d PS |
2915 | |
2916 | rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset), | |
2917 | le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr, | |
2918 | info_buf_size); | |
2919 | if (rc) | |
2920 | goto qdir_exit; | |
2921 | ||
2922 | srch_inf->unicode = true; | |
2923 | ||
2924 | if (srch_inf->ntwrk_buf_start) { | |
2925 | if (srch_inf->smallBuf) | |
2926 | cifs_small_buf_release(srch_inf->ntwrk_buf_start); | |
2927 | else | |
2928 | cifs_buf_release(srch_inf->ntwrk_buf_start); | |
2929 | } | |
2930 | srch_inf->ntwrk_buf_start = (char *)rsp; | |
2931 | srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ + | |
2932 | (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset); | |
2933 | /* 4 for rfc1002 length field */ | |
2934 | end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr; | |
2935 | srch_inf->entries_in_buffer = | |
2936 | num_entries(srch_inf->srch_entries_start, end_of_smb, | |
2937 | &srch_inf->last_entry, info_buf_size); | |
2938 | srch_inf->index_of_last_entry += srch_inf->entries_in_buffer; | |
f96637be JP |
2939 | cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n", |
2940 | srch_inf->entries_in_buffer, srch_inf->index_of_last_entry, | |
2941 | srch_inf->srch_entries_start, srch_inf->last_entry); | |
d324f08d PS |
2942 | if (resp_buftype == CIFS_LARGE_BUFFER) |
2943 | srch_inf->smallBuf = false; | |
2944 | else if (resp_buftype == CIFS_SMALL_BUFFER) | |
2945 | srch_inf->smallBuf = true; | |
2946 | else | |
f96637be | 2947 | cifs_dbg(VFS, "illegal search buffer type\n"); |
d324f08d | 2948 | |
d324f08d PS |
2949 | return rc; |
2950 | ||
2951 | qdir_exit: | |
2952 | free_rsp_buf(resp_buftype, rsp); | |
2953 | return rc; | |
2954 | } | |
2955 | ||
35143eb5 PS |
2956 | static int |
2957 | send_set_info(const unsigned int xid, struct cifs_tcon *tcon, | |
c839ff24 | 2958 | u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class, |
35143eb5 PS |
2959 | unsigned int num, void **data, unsigned int *size) |
2960 | { | |
2961 | struct smb2_set_info_req *req; | |
2962 | struct smb2_set_info_rsp *rsp = NULL; | |
2963 | struct kvec *iov; | |
da502f7d | 2964 | struct kvec rsp_iov; |
35143eb5 PS |
2965 | int rc = 0; |
2966 | int resp_buftype; | |
2967 | unsigned int i; | |
2968 | struct TCP_Server_Info *server; | |
2969 | struct cifs_ses *ses = tcon->ses; | |
7fb8986e | 2970 | int flags = 0; |
35143eb5 PS |
2971 | |
2972 | if (ses && (ses->server)) | |
2973 | server = ses->server; | |
2974 | else | |
2975 | return -EIO; | |
2976 | ||
2977 | if (!num) | |
2978 | return -EINVAL; | |
2979 | ||
2980 | iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL); | |
2981 | if (!iov) | |
2982 | return -ENOMEM; | |
2983 | ||
2984 | rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req); | |
2985 | if (rc) { | |
2986 | kfree(iov); | |
2987 | return rc; | |
2988 | } | |
2989 | ||
7fb8986e PS |
2990 | if (encryption_required(tcon)) |
2991 | flags |= CIFS_TRANSFORM_REQ; | |
2992 | ||
31473fc4 | 2993 | req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid); |
c839ff24 | 2994 | |
35143eb5 PS |
2995 | req->InfoType = SMB2_O_INFO_FILE; |
2996 | req->FileInfoClass = info_class; | |
2997 | req->PersistentFileId = persistent_fid; | |
2998 | req->VolatileFileId = volatile_fid; | |
2999 | ||
3000 | /* 4 for RFC1001 length and 1 for Buffer */ | |
3001 | req->BufferOffset = | |
3002 | cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4); | |
3003 | req->BufferLength = cpu_to_le32(*size); | |
3004 | ||
3005 | inc_rfc1001_len(req, *size - 1 /* Buffer */); | |
3006 | ||
3007 | memcpy(req->Buffer, *data, *size); | |
3008 | ||
3009 | iov[0].iov_base = (char *)req; | |
3010 | /* 4 for RFC1001 length */ | |
3011 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
3012 | ||
3013 | for (i = 1; i < num; i++) { | |
3014 | inc_rfc1001_len(req, size[i]); | |
3015 | le32_add_cpu(&req->BufferLength, size[i]); | |
3016 | iov[i].iov_base = (char *)data[i]; | |
3017 | iov[i].iov_len = size[i]; | |
3018 | } | |
3019 | ||
7fb8986e | 3020 | rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov); |
da502f7d PS |
3021 | cifs_small_buf_release(req); |
3022 | rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base; | |
35143eb5 | 3023 | |
7d3fb24b | 3024 | if (rc != 0) |
35143eb5 | 3025 | cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE); |
7d3fb24b | 3026 | |
35143eb5 PS |
3027 | free_rsp_buf(resp_buftype, rsp); |
3028 | kfree(iov); | |
3029 | return rc; | |
3030 | } | |
3031 | ||
3032 | int | |
3033 | SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon, | |
3034 | u64 persistent_fid, u64 volatile_fid, __le16 *target_file) | |
3035 | { | |
3036 | struct smb2_file_rename_info info; | |
3037 | void **data; | |
3038 | unsigned int size[2]; | |
3039 | int rc; | |
3040 | int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX)); | |
3041 | ||
3042 | data = kmalloc(sizeof(void *) * 2, GFP_KERNEL); | |
3043 | if (!data) | |
3044 | return -ENOMEM; | |
3045 | ||
3046 | info.ReplaceIfExists = 1; /* 1 = replace existing target with new */ | |
3047 | /* 0 = fail if target already exists */ | |
3048 | info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */ | |
3049 | info.FileNameLength = cpu_to_le32(len); | |
3050 | ||
3051 | data[0] = &info; | |
3052 | size[0] = sizeof(struct smb2_file_rename_info); | |
3053 | ||
3054 | data[1] = target_file; | |
3055 | size[1] = len + 2 /* null */; | |
3056 | ||
3057 | rc = send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
c839ff24 PS |
3058 | current->tgid, FILE_RENAME_INFORMATION, 2, data, |
3059 | size); | |
35143eb5 PS |
3060 | kfree(data); |
3061 | return rc; | |
3062 | } | |
568798cc | 3063 | |
897fba11 SF |
3064 | int |
3065 | SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon, | |
3066 | u64 persistent_fid, u64 volatile_fid) | |
3067 | { | |
3068 | __u8 delete_pending = 1; | |
3069 | void *data; | |
3070 | unsigned int size; | |
3071 | ||
3072 | data = &delete_pending; | |
3073 | size = 1; /* sizeof __u8 */ | |
3074 | ||
3075 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
3076 | current->tgid, FILE_DISPOSITION_INFORMATION, 1, &data, | |
3077 | &size); | |
3078 | } | |
3079 | ||
568798cc PS |
3080 | int |
3081 | SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon, | |
3082 | u64 persistent_fid, u64 volatile_fid, __le16 *target_file) | |
3083 | { | |
3084 | struct smb2_file_link_info info; | |
3085 | void **data; | |
3086 | unsigned int size[2]; | |
3087 | int rc; | |
3088 | int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX)); | |
3089 | ||
3090 | data = kmalloc(sizeof(void *) * 2, GFP_KERNEL); | |
3091 | if (!data) | |
3092 | return -ENOMEM; | |
3093 | ||
3094 | info.ReplaceIfExists = 0; /* 1 = replace existing link with new */ | |
3095 | /* 0 = fail if link already exists */ | |
3096 | info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */ | |
3097 | info.FileNameLength = cpu_to_le32(len); | |
3098 | ||
3099 | data[0] = &info; | |
3100 | size[0] = sizeof(struct smb2_file_link_info); | |
3101 | ||
3102 | data[1] = target_file; | |
3103 | size[1] = len + 2 /* null */; | |
3104 | ||
3105 | rc = send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
c839ff24 | 3106 | current->tgid, FILE_LINK_INFORMATION, 2, data, size); |
568798cc PS |
3107 | kfree(data); |
3108 | return rc; | |
3109 | } | |
c839ff24 PS |
3110 | |
3111 | int | |
3112 | SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, | |
f29ebb47 | 3113 | u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc) |
c839ff24 PS |
3114 | { |
3115 | struct smb2_file_eof_info info; | |
3116 | void *data; | |
3117 | unsigned int size; | |
3118 | ||
3119 | info.EndOfFile = *eof; | |
3120 | ||
3121 | data = &info; | |
3122 | size = sizeof(struct smb2_file_eof_info); | |
3123 | ||
f29ebb47 SF |
3124 | if (is_falloc) |
3125 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
3126 | pid, FILE_ALLOCATION_INFORMATION, 1, &data, &size); | |
3127 | else | |
3128 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
3129 | pid, FILE_END_OF_FILE_INFORMATION, 1, &data, &size); | |
c839ff24 | 3130 | } |
1feeaac7 PS |
3131 | |
3132 | int | |
3133 | SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon, | |
3134 | u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf) | |
3135 | { | |
3136 | unsigned int size; | |
3137 | size = sizeof(FILE_BASIC_INFO); | |
3138 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
3139 | current->tgid, FILE_BASIC_INFORMATION, 1, | |
3140 | (void **)&buf, &size); | |
3141 | } | |
983c88a4 PS |
3142 | |
3143 | int | |
3144 | SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, | |
3145 | const u64 persistent_fid, const u64 volatile_fid, | |
3146 | __u8 oplock_level) | |
3147 | { | |
3148 | int rc; | |
3149 | struct smb2_oplock_break *req = NULL; | |
7fb8986e | 3150 | int flags = CIFS_OBREAK_OP; |
983c88a4 | 3151 | |
f96637be | 3152 | cifs_dbg(FYI, "SMB2_oplock_break\n"); |
983c88a4 | 3153 | rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req); |
983c88a4 PS |
3154 | if (rc) |
3155 | return rc; | |
3156 | ||
7fb8986e PS |
3157 | if (encryption_required(tcon)) |
3158 | flags |= CIFS_TRANSFORM_REQ; | |
3159 | ||
983c88a4 PS |
3160 | req->VolatileFid = volatile_fid; |
3161 | req->PersistentFid = persistent_fid; | |
3162 | req->OplockLevel = oplock_level; | |
31473fc4 | 3163 | req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1); |
983c88a4 | 3164 | |
7fb8986e | 3165 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags); |
da502f7d | 3166 | cifs_small_buf_release(req); |
983c88a4 PS |
3167 | |
3168 | if (rc) { | |
3169 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); | |
f96637be | 3170 | cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc); |
983c88a4 PS |
3171 | } |
3172 | ||
3173 | return rc; | |
3174 | } | |
6fc05c25 PS |
3175 | |
3176 | static void | |
3177 | copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf, | |
3178 | struct kstatfs *kst) | |
3179 | { | |
3180 | kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) * | |
3181 | le32_to_cpu(pfs_inf->SectorsPerAllocationUnit); | |
3182 | kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits); | |
3183 | kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits); | |
3184 | kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits); | |
3185 | return; | |
3186 | } | |
3187 | ||
3188 | static int | |
3189 | build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level, | |
3190 | int outbuf_len, u64 persistent_fid, u64 volatile_fid) | |
3191 | { | |
3192 | int rc; | |
3193 | struct smb2_query_info_req *req; | |
3194 | ||
f96637be | 3195 | cifs_dbg(FYI, "Query FSInfo level %d\n", level); |
6fc05c25 PS |
3196 | |
3197 | if ((tcon->ses == NULL) || (tcon->ses->server == NULL)) | |
3198 | return -EIO; | |
3199 | ||
3200 | rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req); | |
3201 | if (rc) | |
3202 | return rc; | |
3203 | ||
3204 | req->InfoType = SMB2_O_INFO_FILESYSTEM; | |
3205 | req->FileInfoClass = level; | |
3206 | req->PersistentFileId = persistent_fid; | |
3207 | req->VolatileFileId = volatile_fid; | |
3208 | /* 4 for rfc1002 length field and 1 for pad */ | |
3209 | req->InputBufferOffset = | |
3210 | cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4); | |
3211 | req->OutputBufferLength = cpu_to_le32( | |
3212 | outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4); | |
3213 | ||
3214 | iov->iov_base = (char *)req; | |
3215 | /* 4 for rfc1002 length field */ | |
3216 | iov->iov_len = get_rfc1002_length(req) + 4; | |
3217 | return 0; | |
3218 | } | |
3219 | ||
3220 | int | |
3221 | SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon, | |
3222 | u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) | |
3223 | { | |
3224 | struct smb2_query_info_rsp *rsp = NULL; | |
3225 | struct kvec iov; | |
da502f7d | 3226 | struct kvec rsp_iov; |
6fc05c25 PS |
3227 | int rc = 0; |
3228 | int resp_buftype; | |
3229 | struct cifs_ses *ses = tcon->ses; | |
3230 | struct smb2_fs_full_size_info *info = NULL; | |
7fb8986e | 3231 | int flags = 0; |
6fc05c25 PS |
3232 | |
3233 | rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION, | |
3234 | sizeof(struct smb2_fs_full_size_info), | |
3235 | persistent_fid, volatile_fid); | |
3236 | if (rc) | |
3237 | return rc; | |
3238 | ||
7fb8986e PS |
3239 | if (encryption_required(tcon)) |
3240 | flags |= CIFS_TRANSFORM_REQ; | |
3241 | ||
3242 | rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov); | |
da502f7d | 3243 | cifs_small_buf_release(iov.iov_base); |
6fc05c25 PS |
3244 | if (rc) { |
3245 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); | |
34f62640 | 3246 | goto qfsinf_exit; |
6fc05c25 | 3247 | } |
da502f7d | 3248 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
6fc05c25 PS |
3249 | |
3250 | info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ + | |
3251 | le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr); | |
3252 | rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset), | |
3253 | le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr, | |
3254 | sizeof(struct smb2_fs_full_size_info)); | |
3255 | if (!rc) | |
3256 | copy_fs_info_to_kstatfs(info, fsdata); | |
3257 | ||
34f62640 | 3258 | qfsinf_exit: |
da502f7d | 3259 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
34f62640 SF |
3260 | return rc; |
3261 | } | |
3262 | ||
3263 | int | |
3264 | SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, | |
2167114c | 3265 | u64 persistent_fid, u64 volatile_fid, int level) |
34f62640 SF |
3266 | { |
3267 | struct smb2_query_info_rsp *rsp = NULL; | |
3268 | struct kvec iov; | |
da502f7d | 3269 | struct kvec rsp_iov; |
34f62640 | 3270 | int rc = 0; |
2167114c | 3271 | int resp_buftype, max_len, min_len; |
34f62640 SF |
3272 | struct cifs_ses *ses = tcon->ses; |
3273 | unsigned int rsp_len, offset; | |
7fb8986e | 3274 | int flags = 0; |
34f62640 | 3275 | |
2167114c SF |
3276 | if (level == FS_DEVICE_INFORMATION) { |
3277 | max_len = sizeof(FILE_SYSTEM_DEVICE_INFO); | |
3278 | min_len = sizeof(FILE_SYSTEM_DEVICE_INFO); | |
3279 | } else if (level == FS_ATTRIBUTE_INFORMATION) { | |
3280 | max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO); | |
3281 | min_len = MIN_FS_ATTR_INFO_SIZE; | |
af6a12ea SF |
3282 | } else if (level == FS_SECTOR_SIZE_INFORMATION) { |
3283 | max_len = sizeof(struct smb3_fs_ss_info); | |
3284 | min_len = sizeof(struct smb3_fs_ss_info); | |
2167114c | 3285 | } else { |
af6a12ea | 3286 | cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level); |
2167114c SF |
3287 | return -EINVAL; |
3288 | } | |
3289 | ||
3290 | rc = build_qfs_info_req(&iov, tcon, level, max_len, | |
34f62640 SF |
3291 | persistent_fid, volatile_fid); |
3292 | if (rc) | |
3293 | return rc; | |
3294 | ||
7fb8986e PS |
3295 | if (encryption_required(tcon)) |
3296 | flags |= CIFS_TRANSFORM_REQ; | |
3297 | ||
3298 | rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov); | |
da502f7d | 3299 | cifs_small_buf_release(iov.iov_base); |
34f62640 SF |
3300 | if (rc) { |
3301 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); | |
3302 | goto qfsattr_exit; | |
3303 | } | |
da502f7d | 3304 | rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base; |
34f62640 SF |
3305 | |
3306 | rsp_len = le32_to_cpu(rsp->OutputBufferLength); | |
3307 | offset = le16_to_cpu(rsp->OutputBufferOffset); | |
2167114c SF |
3308 | rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len); |
3309 | if (rc) | |
3310 | goto qfsattr_exit; | |
3311 | ||
3312 | if (level == FS_ATTRIBUTE_INFORMATION) | |
34f62640 SF |
3313 | memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset |
3314 | + (char *)&rsp->hdr, min_t(unsigned int, | |
2167114c SF |
3315 | rsp_len, max_len)); |
3316 | else if (level == FS_DEVICE_INFORMATION) | |
3317 | memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset | |
3318 | + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO)); | |
af6a12ea SF |
3319 | else if (level == FS_SECTOR_SIZE_INFORMATION) { |
3320 | struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *) | |
3321 | (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr); | |
3322 | tcon->ss_flags = le32_to_cpu(ss_info->Flags); | |
3323 | tcon->perf_sector_size = | |
3324 | le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf); | |
3325 | } | |
34f62640 SF |
3326 | |
3327 | qfsattr_exit: | |
da502f7d | 3328 | free_rsp_buf(resp_buftype, rsp_iov.iov_base); |
6fc05c25 PS |
3329 | return rc; |
3330 | } | |
f7ba7fe6 PS |
3331 | |
3332 | int | |
3333 | smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon, | |
3334 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, | |
3335 | const __u32 num_lock, struct smb2_lock_element *buf) | |
3336 | { | |
3337 | int rc = 0; | |
3338 | struct smb2_lock_req *req = NULL; | |
3339 | struct kvec iov[2]; | |
da502f7d | 3340 | struct kvec rsp_iov; |
f7ba7fe6 PS |
3341 | int resp_buf_type; |
3342 | unsigned int count; | |
7fb8986e | 3343 | int flags = CIFS_NO_RESP; |
f7ba7fe6 | 3344 | |
f96637be | 3345 | cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock); |
f7ba7fe6 PS |
3346 | |
3347 | rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req); | |
3348 | if (rc) | |
3349 | return rc; | |
3350 | ||
7fb8986e PS |
3351 | if (encryption_required(tcon)) |
3352 | flags |= CIFS_TRANSFORM_REQ; | |
3353 | ||
31473fc4 | 3354 | req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid); |
f7ba7fe6 PS |
3355 | req->LockCount = cpu_to_le16(num_lock); |
3356 | ||
3357 | req->PersistentFileId = persist_fid; | |
3358 | req->VolatileFileId = volatile_fid; | |
3359 | ||
3360 | count = num_lock * sizeof(struct smb2_lock_element); | |
3361 | inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element)); | |
3362 | ||
3363 | iov[0].iov_base = (char *)req; | |
3364 | /* 4 for rfc1002 length field and count for all locks */ | |
3365 | iov[0].iov_len = get_rfc1002_length(req) + 4 - count; | |
3366 | iov[1].iov_base = (char *)buf; | |
3367 | iov[1].iov_len = count; | |
3368 | ||
3369 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); | |
7fb8986e | 3370 | rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags, |
da502f7d PS |
3371 | &rsp_iov); |
3372 | cifs_small_buf_release(req); | |
f7ba7fe6 | 3373 | if (rc) { |
f96637be | 3374 | cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc); |
f7ba7fe6 PS |
3375 | cifs_stats_fail_inc(tcon, SMB2_LOCK_HE); |
3376 | } | |
3377 | ||
3378 | return rc; | |
3379 | } | |
3380 | ||
3381 | int | |
3382 | SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon, | |
3383 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, | |
3384 | const __u64 length, const __u64 offset, const __u32 lock_flags, | |
3385 | const bool wait) | |
3386 | { | |
3387 | struct smb2_lock_element lock; | |
3388 | ||
3389 | lock.Offset = cpu_to_le64(offset); | |
3390 | lock.Length = cpu_to_le64(length); | |
3391 | lock.Flags = cpu_to_le32(lock_flags); | |
3392 | if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK) | |
3393 | lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY); | |
3394 | ||
3395 | return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock); | |
3396 | } | |
0822f514 PS |
3397 | |
3398 | int | |
3399 | SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon, | |
3400 | __u8 *lease_key, const __le32 lease_state) | |
3401 | { | |
3402 | int rc; | |
3403 | struct smb2_lease_ack *req = NULL; | |
7fb8986e | 3404 | int flags = CIFS_OBREAK_OP; |
0822f514 | 3405 | |
f96637be | 3406 | cifs_dbg(FYI, "SMB2_lease_break\n"); |
0822f514 | 3407 | rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req); |
0822f514 PS |
3408 | if (rc) |
3409 | return rc; | |
3410 | ||
7fb8986e PS |
3411 | if (encryption_required(tcon)) |
3412 | flags |= CIFS_TRANSFORM_REQ; | |
3413 | ||
31473fc4 | 3414 | req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1); |
0822f514 PS |
3415 | req->StructureSize = cpu_to_le16(36); |
3416 | inc_rfc1001_len(req, 12); | |
3417 | ||
3418 | memcpy(req->LeaseKey, lease_key, 16); | |
3419 | req->LeaseState = lease_state; | |
3420 | ||
7fb8986e | 3421 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags); |
da502f7d | 3422 | cifs_small_buf_release(req); |
0822f514 PS |
3423 | |
3424 | if (rc) { | |
3425 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); | |
f96637be | 3426 | cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc); |
0822f514 PS |
3427 | } |
3428 | ||
3429 | return rc; | |
3430 | } |