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