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