blob: 43f29621e51ff901ec2a755698863e7ddc7177db [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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/uuid.h>
37#include <linux/pagemap.h>
38#include <linux/xattr.h>
39#include "smb2pdu.h"
40#include "cifsglob.h"
41#include "cifsacl.h"
42#include "cifsproto.h"
43#include "smb2proto.h"
44#include "cifs_unicode.h"
45#include "cifs_debug.h"
46#include "ntlmssp.h"
47#include "smb2status.h"
48#include "smb2glob.h"
49#include "cifspdu.h"
50#include "cifs_spnego.h"
51#include "smbdirect.h"
52#include "trace.h"
53
54/*
55 * The following table defines the expected "StructureSize" of SMB2 requests
56 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
57 *
58 * Note that commands are defined in smb2pdu.h in le16 but the array below is
59 * indexed by command in host byte order.
60 */
61static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
62 /* SMB2_NEGOTIATE */ 36,
63 /* SMB2_SESSION_SETUP */ 25,
64 /* SMB2_LOGOFF */ 4,
65 /* SMB2_TREE_CONNECT */ 9,
66 /* SMB2_TREE_DISCONNECT */ 4,
67 /* SMB2_CREATE */ 57,
68 /* SMB2_CLOSE */ 24,
69 /* SMB2_FLUSH */ 24,
70 /* SMB2_READ */ 49,
71 /* SMB2_WRITE */ 49,
72 /* SMB2_LOCK */ 48,
73 /* SMB2_IOCTL */ 57,
74 /* SMB2_CANCEL */ 4,
75 /* SMB2_ECHO */ 4,
76 /* SMB2_QUERY_DIRECTORY */ 33,
77 /* SMB2_CHANGE_NOTIFY */ 32,
78 /* SMB2_QUERY_INFO */ 41,
79 /* SMB2_SET_INFO */ 33,
80 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
81};
82
83int smb3_encryption_required(const struct cifs_tcon *tcon)
84{
85 if (!tcon)
86 return 0;
87 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
88 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
89 return 1;
90 if (tcon->seal &&
91 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
92 return 1;
93 return 0;
94}
95
96static void
97smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
98 const struct cifs_tcon *tcon)
99{
100 shdr->ProtocolId = SMB2_PROTO_NUMBER;
101 shdr->StructureSize = cpu_to_le16(64);
102 shdr->Command = smb2_cmd;
103 if (tcon && tcon->ses && tcon->ses->server) {
104 struct TCP_Server_Info *server = tcon->ses->server;
105
106 spin_lock(&server->req_lock);
107 /* Request up to 2 credits but don't go over the limit. */
108 if (server->credits >= server->max_credits)
109 shdr->CreditRequest = cpu_to_le16(0);
110 else
111 shdr->CreditRequest = cpu_to_le16(
112 min_t(int, server->max_credits -
113 server->credits, 2));
114 spin_unlock(&server->req_lock);
115 } else {
116 shdr->CreditRequest = cpu_to_le16(2);
117 }
118 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
119
120 if (!tcon)
121 goto out;
122
123 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
124 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
125 if ((tcon->ses) && (tcon->ses->server) &&
126 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
127 shdr->CreditCharge = cpu_to_le16(1);
128 /* else CreditCharge MBZ */
129
130 shdr->TreeId = tcon->tid;
131 /* Uid is not converted */
132 if (tcon->ses)
133 shdr->SessionId = tcon->ses->Suid;
134
135 /*
136 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
137 * to pass the path on the Open SMB prefixed by \\server\share.
138 * Not sure when we would need to do the augmented path (if ever) and
139 * setting this flag breaks the SMB2 open operation since it is
140 * illegal to send an empty path name (without \\server\share prefix)
141 * when the DFS flag is set in the SMB open header. We could
142 * consider setting the flag on all operations other than open
143 * but it is safer to net set it for now.
144 */
145/* if (tcon->share_flags & SHI1005_FLAGS_DFS)
146 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
147
148 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
149 !smb3_encryption_required(tcon))
150 shdr->Flags |= SMB2_FLAGS_SIGNED;
151out:
152 return;
153}
154
155static int
156smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
157{
158 int rc;
159 struct nls_table *nls_codepage;
160 struct cifs_ses *ses;
161 struct TCP_Server_Info *server;
162
163 /*
164 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
165 * check for tcp and smb session status done differently
166 * for those three - in the calling routine.
167 */
168 if (tcon == NULL)
169 return 0;
170
171 if (smb2_command == SMB2_TREE_CONNECT)
172 return 0;
173
174 if (tcon->tidStatus == CifsExiting) {
175 /*
176 * only tree disconnect, open, and write,
177 * (and ulogoff which does not have tcon)
178 * are allowed as we start force umount.
179 */
180 if ((smb2_command != SMB2_WRITE) &&
181 (smb2_command != SMB2_CREATE) &&
182 (smb2_command != SMB2_TREE_DISCONNECT)) {
183 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
184 smb2_command);
185 return -ENODEV;
186 }
187 }
188 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
189 (!tcon->ses->server))
190 return -EIO;
191
192 ses = tcon->ses;
193 server = ses->server;
194
195 /*
196 * Give demultiplex thread up to 10 seconds to reconnect, should be
197 * greater than cifs socket timeout which is 7 seconds
198 */
199 while (server->tcpStatus == CifsNeedReconnect) {
200 /*
201 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
202 * here since they are implicitly done when session drops.
203 */
204 switch (smb2_command) {
205 /*
206 * BB Should we keep oplock break and add flush to exceptions?
207 */
208 case SMB2_TREE_DISCONNECT:
209 case SMB2_CANCEL:
210 case SMB2_CLOSE:
211 case SMB2_OPLOCK_BREAK:
212 return -EAGAIN;
213 }
214
215 rc = wait_event_interruptible_timeout(server->response_q,
216 (server->tcpStatus != CifsNeedReconnect),
217 10 * HZ);
218 if (rc < 0) {
219 cifs_dbg(FYI, "%s: aborting reconnect due to a received"
220 " signal by the process\n", __func__);
221 return -ERESTARTSYS;
222 }
223
224 /* are we still trying to reconnect? */
225 if (server->tcpStatus != CifsNeedReconnect)
226 break;
227
228 /*
229 * on "soft" mounts we wait once. Hard mounts keep
230 * retrying until process is killed or server comes
231 * back on-line
232 */
233 if (!tcon->retry) {
234 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
235 return -EHOSTDOWN;
236 }
237 }
238
239 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
240 return 0;
241
242 nls_codepage = load_nls_default();
243
244 /*
245 * need to prevent multiple threads trying to simultaneously reconnect
246 * the same SMB session
247 */
248 mutex_lock(&tcon->ses->session_mutex);
249
250 /*
251 * Recheck after acquire mutex. If another thread is negotiating
252 * and the server never sends an answer the socket will be closed
253 * and tcpStatus set to reconnect.
254 */
255 if (server->tcpStatus == CifsNeedReconnect) {
256 rc = -EHOSTDOWN;
257 mutex_unlock(&tcon->ses->session_mutex);
258 goto out;
259 }
260
261 rc = cifs_negotiate_protocol(0, tcon->ses);
262 if (!rc && tcon->ses->need_reconnect)
263 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
264
265 if (rc || !tcon->need_reconnect) {
266 mutex_unlock(&tcon->ses->session_mutex);
267 goto out;
268 }
269
270 cifs_mark_open_files_invalid(tcon);
271 if (tcon->use_persistent)
272 tcon->need_reopen_files = true;
273
274 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
275 mutex_unlock(&tcon->ses->session_mutex);
276
277 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
278 if (rc) {
279 /* If sess reconnected but tcon didn't, something strange ... */
280 printk_once(KERN_WARNING "reconnect tcon failed rc = %d\n", rc);
281 goto out;
282 }
283
284 if (smb2_command != SMB2_INTERNAL_CMD)
285 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
286
287 atomic_inc(&tconInfoReconnectCount);
288out:
289 /*
290 * Check if handle based operation so we know whether we can continue
291 * or not without returning to caller to reset file handle.
292 */
293 /*
294 * BB Is flush done by server on drop of tcp session? Should we special
295 * case it and skip above?
296 */
297 switch (smb2_command) {
298 case SMB2_FLUSH:
299 case SMB2_READ:
300 case SMB2_WRITE:
301 case SMB2_LOCK:
302 case SMB2_IOCTL:
303 case SMB2_QUERY_DIRECTORY:
304 case SMB2_CHANGE_NOTIFY:
305 case SMB2_QUERY_INFO:
306 case SMB2_SET_INFO:
307 rc = -EAGAIN;
308 }
309 unload_nls(nls_codepage);
310 return rc;
311}
312
313static void
314fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
315 unsigned int *total_len)
316{
317 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
318 /* lookup word count ie StructureSize from table */
319 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
320
321 /*
322 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
323 * largest operations (Create)
324 */
325 memset(buf, 0, 256);
326
327 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
328 spdu->StructureSize2 = cpu_to_le16(parmsize);
329
330 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
331}
332
333/*
334 * Allocate and return pointer to an SMB request hdr, and set basic
335 * SMB information in the SMB header. If the return code is zero, this
336 * function must have filled in request_buf pointer.
337 */
338static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
339 void **request_buf, unsigned int *total_len)
340{
341 /* BB eventually switch this to SMB2 specific small buf size */
342 if (smb2_command == SMB2_SET_INFO)
343 *request_buf = cifs_buf_get();
344 else
345 *request_buf = cifs_small_buf_get();
346 if (*request_buf == NULL) {
347 /* BB should we add a retry in here if not a writepage? */
348 return -ENOMEM;
349 }
350
351 fill_small_buf(smb2_command, tcon,
352 (struct smb2_sync_hdr *)(*request_buf),
353 total_len);
354
355 if (tcon != NULL) {
356 uint16_t com_code = le16_to_cpu(smb2_command);
357 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
358 cifs_stats_inc(&tcon->num_smbs_sent);
359 }
360
361 return 0;
362}
363
364static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
365 void **request_buf, unsigned int *total_len)
366{
367 int rc;
368
369 rc = smb2_reconnect(smb2_command, tcon);
370 if (rc)
371 return rc;
372
373 return __smb2_plain_req_init(smb2_command, tcon, request_buf,
374 total_len);
375}
376
377static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
378 void **request_buf, unsigned int *total_len)
379{
380 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
381 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
382 return __smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf,
383 total_len);
384 }
385 return smb2_plain_req_init(SMB2_IOCTL, tcon, request_buf, total_len);
386}
387
388
389/* offset is sizeof smb2_negotiate_req but rounded up to 8 bytes */
390#define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) */
391
392
393#define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
394#define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
395#define SMB2_POSIX_EXTENSIONS_AVAILABLE cpu_to_le16(0x100)
396
397static void
398build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
399{
400 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
401 pneg_ctxt->DataLength = cpu_to_le16(38);
402 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
403 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
404 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
405 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
406}
407
408static void
409build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
410{
411 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
412 pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + le16 cipher */
413 pneg_ctxt->CipherCount = cpu_to_le16(1);
414/* pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;*/ /* not supported yet */
415 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_CCM;
416}
417
418static void
419build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
420{
421 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
422 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
423}
424
425static void
426assemble_neg_contexts(struct smb2_negotiate_req *req,
427 unsigned int *total_len)
428{
429 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT;
430 unsigned int ctxt_len;
431
432 *total_len += 2; /* Add 2 due to round to 8 byte boundary for 1st ctxt */
433 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
434 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
435 *total_len += ctxt_len;
436 pneg_ctxt += ctxt_len;
437
438 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
439 ctxt_len = DIV_ROUND_UP(sizeof(struct smb2_encryption_neg_context), 8) * 8;
440 *total_len += ctxt_len;
441 pneg_ctxt += ctxt_len;
442
443 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
444 *total_len += sizeof(struct smb2_posix_neg_context);
445
446 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
447 req->NegotiateContextCount = cpu_to_le16(3);
448}
449
450static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
451{
452 unsigned int len = le16_to_cpu(ctxt->DataLength);
453
454 /* If invalid preauth context warn but use what we requested, SHA-512 */
455 if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
456 printk_once(KERN_WARNING "server sent bad preauth context\n");
457 return;
458 }
459 if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
460 printk_once(KERN_WARNING "illegal SMB3 hash algorithm count\n");
461 if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
462 printk_once(KERN_WARNING "unknown SMB3 hash algorithm\n");
463}
464
465static int decode_encrypt_ctx(struct TCP_Server_Info *server,
466 struct smb2_encryption_neg_context *ctxt)
467{
468 unsigned int len = le16_to_cpu(ctxt->DataLength);
469
470 cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
471 if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
472 printk_once(KERN_WARNING "server sent bad crypto ctxt len\n");
473 return -EINVAL;
474 }
475
476 if (le16_to_cpu(ctxt->CipherCount) != 1) {
477 printk_once(KERN_WARNING "illegal SMB3.11 cipher count\n");
478 return -EINVAL;
479 }
480 cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
481 if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
482 (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM)) {
483 printk_once(KERN_WARNING "invalid SMB3.11 cipher returned\n");
484 return -EINVAL;
485 }
486 server->cipher_type = ctxt->Ciphers[0];
487 server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
488 return 0;
489}
490
491static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
492 struct TCP_Server_Info *server,
493 unsigned int len_of_smb)
494{
495 struct smb2_neg_context *pctx;
496 unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
497 unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
498 unsigned int len_of_ctxts, i;
499 int rc = 0;
500
501 cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
502 if (len_of_smb <= offset) {
503 cifs_dbg(VFS, "Invalid response: negotiate context offset\n");
504 return -EINVAL;
505 }
506
507 len_of_ctxts = len_of_smb - offset;
508
509 for (i = 0; i < ctxt_cnt; i++) {
510 int clen;
511 /* check that offset is not beyond end of SMB */
512 if (len_of_ctxts == 0)
513 break;
514
515 if (len_of_ctxts < sizeof(struct smb2_neg_context))
516 break;
517
518 pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
519 clen = le16_to_cpu(pctx->DataLength);
520 if (clen > len_of_ctxts)
521 break;
522
523 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
524 decode_preauth_context(
525 (struct smb2_preauth_neg_context *)pctx);
526 else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
527 rc = decode_encrypt_ctx(server,
528 (struct smb2_encryption_neg_context *)pctx);
529 else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
530 server->posix_ext_supported = true;
531 else
532 cifs_dbg(VFS, "unknown negcontext of type %d ignored\n",
533 le16_to_cpu(pctx->ContextType));
534
535 if (rc)
536 break;
537 /* offsets must be 8 byte aligned */
538 clen = (clen + 7) & ~0x7;
539 offset += clen + sizeof(struct smb2_neg_context);
540 len_of_ctxts -= clen;
541 }
542 return rc;
543}
544
545static struct create_posix *
546create_posix_buf(umode_t mode)
547{
548 struct create_posix *buf;
549
550 buf = kzalloc(sizeof(struct create_posix),
551 GFP_KERNEL);
552 if (!buf)
553 return NULL;
554
555 buf->ccontext.DataOffset =
556 cpu_to_le16(offsetof(struct create_posix, Mode));
557 buf->ccontext.DataLength = cpu_to_le32(4);
558 buf->ccontext.NameOffset =
559 cpu_to_le16(offsetof(struct create_posix, Name));
560 buf->ccontext.NameLength = cpu_to_le16(16);
561
562 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
563 buf->Name[0] = 0x93;
564 buf->Name[1] = 0xAD;
565 buf->Name[2] = 0x25;
566 buf->Name[3] = 0x50;
567 buf->Name[4] = 0x9C;
568 buf->Name[5] = 0xB4;
569 buf->Name[6] = 0x11;
570 buf->Name[7] = 0xE7;
571 buf->Name[8] = 0xB4;
572 buf->Name[9] = 0x23;
573 buf->Name[10] = 0x83;
574 buf->Name[11] = 0xDE;
575 buf->Name[12] = 0x96;
576 buf->Name[13] = 0x8B;
577 buf->Name[14] = 0xCD;
578 buf->Name[15] = 0x7C;
579 buf->Mode = cpu_to_le32(mode);
580 cifs_dbg(FYI, "mode on posix create 0%o", mode);
581 return buf;
582}
583
584static int
585add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
586{
587 struct smb2_create_req *req = iov[0].iov_base;
588 unsigned int num = *num_iovec;
589
590 iov[num].iov_base = create_posix_buf(mode);
591 if (iov[num].iov_base == NULL)
592 return -ENOMEM;
593 iov[num].iov_len = sizeof(struct create_posix);
594 if (!req->CreateContextsOffset)
595 req->CreateContextsOffset = cpu_to_le32(
596 sizeof(struct smb2_create_req) +
597 iov[num - 1].iov_len);
598 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
599 *num_iovec = num + 1;
600 return 0;
601}
602
603
604/*
605 *
606 * SMB2 Worker functions follow:
607 *
608 * The general structure of the worker functions is:
609 * 1) Call smb2_init (assembles SMB2 header)
610 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
611 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
612 * 4) Decode SMB2 command specific fields in the fixed length area
613 * 5) Decode variable length data area (if any for this SMB2 command type)
614 * 6) Call free smb buffer
615 * 7) return
616 *
617 */
618
619int
620SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
621{
622 struct smb_rqst rqst;
623 struct smb2_negotiate_req *req;
624 struct smb2_negotiate_rsp *rsp;
625 struct kvec iov[1];
626 struct kvec rsp_iov;
627 int rc = 0;
628 int resp_buftype;
629 struct TCP_Server_Info *server = ses->server;
630 int blob_offset, blob_length;
631 char *security_blob;
632 int flags = CIFS_NEG_OP;
633 unsigned int total_len;
634
635 cifs_dbg(FYI, "Negotiate protocol\n");
636
637 if (!server) {
638 WARN(1, "%s: server is NULL!\n", __func__);
639 return -EIO;
640 }
641
642 rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, (void **) &req, &total_len);
643 if (rc)
644 return rc;
645
646 req->sync_hdr.SessionId = 0;
647
648 memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
649 memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
650
651 if (strcmp(ses->server->vals->version_string,
652 SMB3ANY_VERSION_STRING) == 0) {
653 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
654 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
655 req->DialectCount = cpu_to_le16(2);
656 total_len += 4;
657 } else if (strcmp(ses->server->vals->version_string,
658 SMBDEFAULT_VERSION_STRING) == 0) {
659 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
660 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
661 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
662 req->DialectCount = cpu_to_le16(3);
663 total_len += 6;
664 } else {
665 /* otherwise send specific dialect */
666 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
667 req->DialectCount = cpu_to_le16(1);
668 total_len += 2;
669 }
670
671 /* only one of SMB2 signing flags may be set in SMB2 request */
672 if (ses->sign)
673 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
674 else if (global_secflags & CIFSSEC_MAY_SIGN)
675 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
676 else
677 req->SecurityMode = 0;
678
679 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
680
681 /* ClientGUID must be zero for SMB2.02 dialect */
682 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
683 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
684 else {
685 memcpy(req->ClientGUID, server->client_guid,
686 SMB2_CLIENT_GUID_SIZE);
687 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
688 assemble_neg_contexts(req, &total_len);
689 }
690 iov[0].iov_base = (char *)req;
691 iov[0].iov_len = total_len;
692
693 memset(&rqst, 0, sizeof(struct smb_rqst));
694 rqst.rq_iov = iov;
695 rqst.rq_nvec = 1;
696
697 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
698 cifs_small_buf_release(req);
699 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
700 /*
701 * No tcon so can't do
702 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
703 */
704 if (rc == -EOPNOTSUPP) {
705 cifs_dbg(VFS, "Dialect not supported by server. Consider "
706 "specifying vers=1.0 or vers=2.0 on mount for accessing"
707 " older servers\n");
708 goto neg_exit;
709 } else if (rc != 0)
710 goto neg_exit;
711
712 if (strcmp(ses->server->vals->version_string,
713 SMB3ANY_VERSION_STRING) == 0) {
714 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
715 cifs_dbg(VFS,
716 "SMB2 dialect returned but not requested\n");
717 return -EIO;
718 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
719 cifs_dbg(VFS,
720 "SMB2.1 dialect returned but not requested\n");
721 return -EIO;
722 }
723 } else if (strcmp(ses->server->vals->version_string,
724 SMBDEFAULT_VERSION_STRING) == 0) {
725 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
726 cifs_dbg(VFS,
727 "SMB2 dialect returned but not requested\n");
728 return -EIO;
729 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
730 /* ops set to 3.0 by default for default so update */
731 ses->server->ops = &smb21_operations;
732 ses->server->vals = &smb21_values;
733 }
734 } else if (le16_to_cpu(rsp->DialectRevision) !=
735 ses->server->vals->protocol_id) {
736 /* if requested single dialect ensure returned dialect matched */
737 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
738 le16_to_cpu(rsp->DialectRevision));
739 return -EIO;
740 }
741
742 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
743
744 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
745 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
746 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
747 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
748 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
749 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
750 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
751 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
752 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
753 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
754 else {
755 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
756 le16_to_cpu(rsp->DialectRevision));
757 rc = -EIO;
758 goto neg_exit;
759 }
760 server->dialect = le16_to_cpu(rsp->DialectRevision);
761
762 /*
763 * Keep a copy of the hash after negprot. This hash will be
764 * the starting hash value for all sessions made from this
765 * server.
766 */
767 memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
768 SMB2_PREAUTH_HASH_SIZE);
769
770 /* SMB2 only has an extended negflavor */
771 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
772 /* set it to the maximum buffer size value we can send with 1 credit */
773 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
774 SMB2_MAX_BUFFER_SIZE);
775 server->max_read = le32_to_cpu(rsp->MaxReadSize);
776 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
777 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
778 if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
779 cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
780 server->sec_mode);
781 server->capabilities = le32_to_cpu(rsp->Capabilities);
782 /* Internal types */
783 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
784
785 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
786 (struct smb2_sync_hdr *)rsp);
787 /*
788 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
789 * for us will be
790 * ses->sectype = RawNTLMSSP;
791 * but for time being this is our only auth choice so doesn't matter.
792 * We just found a server which sets blob length to zero expecting raw.
793 */
794 if (blob_length == 0) {
795 cifs_dbg(FYI, "missing security blob on negprot\n");
796 server->sec_ntlmssp = true;
797 }
798
799 rc = cifs_enable_signing(server, ses->sign);
800 if (rc)
801 goto neg_exit;
802 if (blob_length) {
803 rc = decode_negTokenInit(security_blob, blob_length, server);
804 if (rc == 1)
805 rc = 0;
806 else if (rc == 0)
807 rc = -EIO;
808 }
809
810 if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
811 if (rsp->NegotiateContextCount)
812 rc = smb311_decode_neg_context(rsp, server,
813 rsp_iov.iov_len);
814 else
815 cifs_dbg(VFS, "Missing expected negotiate contexts\n");
816 }
817neg_exit:
818 free_rsp_buf(resp_buftype, rsp);
819 return rc;
820}
821
822int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
823{
824 int rc;
825 struct validate_negotiate_info_req *pneg_inbuf;
826 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
827 u32 rsplen;
828 u32 inbuflen; /* max of 4 dialects */
829
830 cifs_dbg(FYI, "validate negotiate\n");
831
832 /* In SMB3.11 preauth integrity supersedes validate negotiate */
833 if (tcon->ses->server->dialect == SMB311_PROT_ID)
834 return 0;
835
836 /*
837 * validation ioctl must be signed, so no point sending this if we
838 * can not sign it (ie are not known user). Even if signing is not
839 * required (enabled but not negotiated), in those cases we selectively
840 * sign just this, the first and only signed request on a connection.
841 * Having validation of negotiate info helps reduce attack vectors.
842 */
843 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
844 return 0; /* validation requires signing */
845
846 if (tcon->ses->user_name == NULL) {
847 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
848 return 0; /* validation requires signing */
849 }
850
851 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
852 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
853
854 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
855 if (!pneg_inbuf)
856 return -ENOMEM;
857
858 pneg_inbuf->Capabilities =
859 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
860 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
861 SMB2_CLIENT_GUID_SIZE);
862
863 if (tcon->ses->sign)
864 pneg_inbuf->SecurityMode =
865 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
866 else if (global_secflags & CIFSSEC_MAY_SIGN)
867 pneg_inbuf->SecurityMode =
868 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
869 else
870 pneg_inbuf->SecurityMode = 0;
871
872
873 if (strcmp(tcon->ses->server->vals->version_string,
874 SMB3ANY_VERSION_STRING) == 0) {
875 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
876 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
877 pneg_inbuf->DialectCount = cpu_to_le16(2);
878 /* structure is big enough for 3 dialects, sending only 2 */
879 inbuflen = sizeof(*pneg_inbuf) -
880 sizeof(pneg_inbuf->Dialects[0]);
881 } else if (strcmp(tcon->ses->server->vals->version_string,
882 SMBDEFAULT_VERSION_STRING) == 0) {
883 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
884 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
885 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
886 pneg_inbuf->DialectCount = cpu_to_le16(3);
887 /* structure is big enough for 3 dialects */
888 inbuflen = sizeof(*pneg_inbuf);
889 } else {
890 /* otherwise specific dialect was requested */
891 pneg_inbuf->Dialects[0] =
892 cpu_to_le16(tcon->ses->server->vals->protocol_id);
893 pneg_inbuf->DialectCount = cpu_to_le16(1);
894 /* structure is big enough for 3 dialects, sending only 1 */
895 inbuflen = sizeof(*pneg_inbuf) -
896 sizeof(pneg_inbuf->Dialects[0]) * 2;
897 }
898
899 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
900 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
901 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
902 if (rc == -EOPNOTSUPP) {
903 /*
904 * Old Windows versions or Netapp SMB server can return
905 * not supported error. Client should accept it.
906 */
907 cifs_dbg(VFS, "Server does not support validate negotiate\n");
908 rc = 0;
909 goto out_free_inbuf;
910 } else if (rc != 0) {
911 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
912 rc = -EIO;
913 goto out_free_inbuf;
914 }
915
916 rc = -EIO;
917 if (rsplen != sizeof(*pneg_rsp)) {
918 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
919 rsplen);
920
921 /* relax check since Mac returns max bufsize allowed on ioctl */
922 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
923 goto out_free_rsp;
924 }
925
926 /* check validate negotiate info response matches what we got earlier */
927 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
928 goto vneg_out;
929
930 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
931 goto vneg_out;
932
933 /* do not validate server guid because not saved at negprot time yet */
934
935 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
936 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
937 goto vneg_out;
938
939 /* validate negotiate successful */
940 rc = 0;
941 cifs_dbg(FYI, "validate negotiate info successful\n");
942 goto out_free_rsp;
943
944vneg_out:
945 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
946out_free_rsp:
947 kfree(pneg_rsp);
948out_free_inbuf:
949 kfree(pneg_inbuf);
950 return rc;
951}
952
953enum securityEnum
954smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
955{
956 switch (requested) {
957 case Kerberos:
958 case RawNTLMSSP:
959 return requested;
960 case NTLMv2:
961 return RawNTLMSSP;
962 case Unspecified:
963 if (server->sec_ntlmssp &&
964 (global_secflags & CIFSSEC_MAY_NTLMSSP))
965 return RawNTLMSSP;
966 if ((server->sec_kerberos || server->sec_mskerberos) &&
967 (global_secflags & CIFSSEC_MAY_KRB5))
968 return Kerberos;
969 /* Fallthrough */
970 default:
971 return Unspecified;
972 }
973}
974
975struct SMB2_sess_data {
976 unsigned int xid;
977 struct cifs_ses *ses;
978 struct nls_table *nls_cp;
979 void (*func)(struct SMB2_sess_data *);
980 int result;
981 u64 previous_session;
982
983 /* we will send the SMB in three pieces:
984 * a fixed length beginning part, an optional
985 * SPNEGO blob (which can be zero length), and a
986 * last part which will include the strings
987 * and rest of bcc area. This allows us to avoid
988 * a large buffer 17K allocation
989 */
990 int buf0_type;
991 struct kvec iov[2];
992};
993
994static int
995SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
996{
997 int rc;
998 struct cifs_ses *ses = sess_data->ses;
999 struct smb2_sess_setup_req *req;
1000 struct TCP_Server_Info *server = ses->server;
1001 unsigned int total_len;
1002
1003 rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, (void **) &req,
1004 &total_len);
1005 if (rc)
1006 return rc;
1007
1008 /* First session, not a reauthenticate */
1009 req->sync_hdr.SessionId = 0;
1010
1011 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
1012 req->PreviousSessionId = sess_data->previous_session;
1013
1014 req->Flags = 0; /* MBZ */
1015
1016 /* enough to enable echos and oplocks and one max size write */
1017 req->sync_hdr.CreditRequest = cpu_to_le16(130);
1018
1019 /* only one of SMB2 signing flags may be set in SMB2 request */
1020 if (server->sign)
1021 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
1022 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
1023 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
1024 else
1025 req->SecurityMode = 0;
1026
1027#ifdef CONFIG_CIFS_DFS_UPCALL
1028 req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
1029#else
1030 req->Capabilities = 0;
1031#endif /* DFS_UPCALL */
1032
1033 req->Channel = 0; /* MBZ */
1034
1035 sess_data->iov[0].iov_base = (char *)req;
1036 /* 1 for pad */
1037 sess_data->iov[0].iov_len = total_len - 1;
1038 /*
1039 * This variable will be used to clear the buffer
1040 * allocated above in case of any error in the calling function.
1041 */
1042 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1043
1044 return 0;
1045}
1046
1047static void
1048SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
1049{
1050 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1051 sess_data->buf0_type = CIFS_NO_BUFFER;
1052}
1053
1054static int
1055SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
1056{
1057 int rc;
1058 struct smb_rqst rqst;
1059 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
1060 struct kvec rsp_iov = { NULL, 0 };
1061
1062 /* Testing shows that buffer offset must be at location of Buffer[0] */
1063 req->SecurityBufferOffset =
1064 cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
1065 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
1066
1067 memset(&rqst, 0, sizeof(struct smb_rqst));
1068 rqst.rq_iov = sess_data->iov;
1069 rqst.rq_nvec = 2;
1070
1071 /* BB add code to build os and lm fields */
1072 rc = cifs_send_recv(sess_data->xid, sess_data->ses,
1073 &rqst,
1074 &sess_data->buf0_type,
1075 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
1076 cifs_small_buf_release(sess_data->iov[0].iov_base);
1077 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1078
1079 return rc;
1080}
1081
1082static int
1083SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
1084{
1085 int rc = 0;
1086 struct cifs_ses *ses = sess_data->ses;
1087
1088 mutex_lock(&ses->server->srv_mutex);
1089 if (ses->server->ops->generate_signingkey) {
1090 rc = ses->server->ops->generate_signingkey(ses);
1091 if (rc) {
1092 cifs_dbg(FYI,
1093 "SMB3 session key generation failed\n");
1094 mutex_unlock(&ses->server->srv_mutex);
1095 return rc;
1096 }
1097 }
1098 if (!ses->server->session_estab) {
1099 ses->server->sequence_number = 0x2;
1100 ses->server->session_estab = true;
1101 }
1102 mutex_unlock(&ses->server->srv_mutex);
1103
1104 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
1105 spin_lock(&GlobalMid_Lock);
1106 ses->status = CifsGood;
1107 ses->need_reconnect = false;
1108 spin_unlock(&GlobalMid_Lock);
1109 return rc;
1110}
1111
1112#ifdef CONFIG_CIFS_UPCALL
1113static void
1114SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1115{
1116 int rc;
1117 struct cifs_ses *ses = sess_data->ses;
1118 struct cifs_spnego_msg *msg;
1119 struct key *spnego_key = NULL;
1120 struct smb2_sess_setup_rsp *rsp = NULL;
1121
1122 rc = SMB2_sess_alloc_buffer(sess_data);
1123 if (rc)
1124 goto out;
1125
1126 spnego_key = cifs_get_spnego_key(ses);
1127 if (IS_ERR(spnego_key)) {
1128 rc = PTR_ERR(spnego_key);
1129 spnego_key = NULL;
1130 goto out;
1131 }
1132
1133 msg = spnego_key->payload.data[0];
1134 /*
1135 * check version field to make sure that cifs.upcall is
1136 * sending us a response in an expected form
1137 */
1138 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1139 cifs_dbg(VFS,
1140 "bad cifs.upcall version. Expected %d got %d",
1141 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1142 rc = -EKEYREJECTED;
1143 goto out_put_spnego_key;
1144 }
1145
1146 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1147 GFP_KERNEL);
1148 if (!ses->auth_key.response) {
1149 cifs_dbg(VFS,
1150 "Kerberos can't allocate (%u bytes) memory",
1151 msg->sesskey_len);
1152 rc = -ENOMEM;
1153 goto out_put_spnego_key;
1154 }
1155 ses->auth_key.len = msg->sesskey_len;
1156
1157 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1158 sess_data->iov[1].iov_len = msg->secblob_len;
1159
1160 rc = SMB2_sess_sendreceive(sess_data);
1161 if (rc)
1162 goto out_put_spnego_key;
1163
1164 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1165 ses->Suid = rsp->sync_hdr.SessionId;
1166
1167 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1168
1169 rc = SMB2_sess_establish_session(sess_data);
1170out_put_spnego_key:
1171 key_invalidate(spnego_key);
1172 key_put(spnego_key);
1173out:
1174 sess_data->result = rc;
1175 sess_data->func = NULL;
1176 SMB2_sess_free_buffer(sess_data);
1177}
1178#else
1179static void
1180SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
1181{
1182 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1183 sess_data->result = -EOPNOTSUPP;
1184 sess_data->func = NULL;
1185}
1186#endif
1187
1188static void
1189SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
1190
1191static void
1192SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
1193{
1194 int rc;
1195 struct cifs_ses *ses = sess_data->ses;
1196 struct smb2_sess_setup_rsp *rsp = NULL;
1197 char *ntlmssp_blob = NULL;
1198 bool use_spnego = false; /* else use raw ntlmssp */
1199 u16 blob_length = 0;
1200
1201 /*
1202 * If memory allocation is successful, caller of this function
1203 * frees it.
1204 */
1205 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1206 if (!ses->ntlmssp) {
1207 rc = -ENOMEM;
1208 goto out_err;
1209 }
1210 ses->ntlmssp->sesskey_per_smbsess = true;
1211
1212 rc = SMB2_sess_alloc_buffer(sess_data);
1213 if (rc)
1214 goto out_err;
1215
1216 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1217 GFP_KERNEL);
1218 if (ntlmssp_blob == NULL) {
1219 rc = -ENOMEM;
1220 goto out;
1221 }
1222
1223 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1224 if (use_spnego) {
1225 /* BB eventually need to add this */
1226 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1227 rc = -EOPNOTSUPP;
1228 goto out;
1229 } else {
1230 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1231 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1232 }
1233 sess_data->iov[1].iov_base = ntlmssp_blob;
1234 sess_data->iov[1].iov_len = blob_length;
1235
1236 rc = SMB2_sess_sendreceive(sess_data);
1237 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1238
1239 /* If true, rc here is expected and not an error */
1240 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1241 rsp->sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1242 rc = 0;
1243
1244 if (rc)
1245 goto out;
1246
1247 if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
1248 le16_to_cpu(rsp->SecurityBufferOffset)) {
1249 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1250 le16_to_cpu(rsp->SecurityBufferOffset));
1251 rc = -EIO;
1252 goto out;
1253 }
1254 rc = decode_ntlmssp_challenge(rsp->Buffer,
1255 le16_to_cpu(rsp->SecurityBufferLength), ses);
1256 if (rc)
1257 goto out;
1258
1259 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1260
1261
1262 ses->Suid = rsp->sync_hdr.SessionId;
1263 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1264
1265out:
1266 kfree(ntlmssp_blob);
1267 SMB2_sess_free_buffer(sess_data);
1268 if (!rc) {
1269 sess_data->result = 0;
1270 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1271 return;
1272 }
1273out_err:
1274 kfree(ses->ntlmssp);
1275 ses->ntlmssp = NULL;
1276 sess_data->result = rc;
1277 sess_data->func = NULL;
1278}
1279
1280static void
1281SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1282{
1283 int rc;
1284 struct cifs_ses *ses = sess_data->ses;
1285 struct smb2_sess_setup_req *req;
1286 struct smb2_sess_setup_rsp *rsp = NULL;
1287 unsigned char *ntlmssp_blob = NULL;
1288 bool use_spnego = false; /* else use raw ntlmssp */
1289 u16 blob_length = 0;
1290
1291 rc = SMB2_sess_alloc_buffer(sess_data);
1292 if (rc)
1293 goto out;
1294
1295 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1296 req->sync_hdr.SessionId = ses->Suid;
1297
1298 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1299 sess_data->nls_cp);
1300 if (rc) {
1301 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1302 goto out;
1303 }
1304
1305 if (use_spnego) {
1306 /* BB eventually need to add this */
1307 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1308 rc = -EOPNOTSUPP;
1309 goto out;
1310 }
1311 sess_data->iov[1].iov_base = ntlmssp_blob;
1312 sess_data->iov[1].iov_len = blob_length;
1313
1314 rc = SMB2_sess_sendreceive(sess_data);
1315 if (rc)
1316 goto out;
1317
1318 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1319
1320 ses->Suid = rsp->sync_hdr.SessionId;
1321 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1322
1323 rc = SMB2_sess_establish_session(sess_data);
1324out:
1325 kfree(ntlmssp_blob);
1326 SMB2_sess_free_buffer(sess_data);
1327 kfree(ses->ntlmssp);
1328 ses->ntlmssp = NULL;
1329 sess_data->result = rc;
1330 sess_data->func = NULL;
1331}
1332
1333static int
1334SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1335{
1336 int type;
1337
1338 type = smb2_select_sectype(ses->server, ses->sectype);
1339 cifs_dbg(FYI, "sess setup type %d\n", type);
1340 if (type == Unspecified) {
1341 cifs_dbg(VFS,
1342 "Unable to select appropriate authentication method!");
1343 return -EINVAL;
1344 }
1345
1346 switch (type) {
1347 case Kerberos:
1348 sess_data->func = SMB2_auth_kerberos;
1349 break;
1350 case RawNTLMSSP:
1351 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1352 break;
1353 default:
1354 cifs_dbg(VFS, "secType %d not supported!\n", type);
1355 return -EOPNOTSUPP;
1356 }
1357
1358 return 0;
1359}
1360
1361int
1362SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1363 const struct nls_table *nls_cp)
1364{
1365 int rc = 0;
1366 struct TCP_Server_Info *server = ses->server;
1367 struct SMB2_sess_data *sess_data;
1368
1369 cifs_dbg(FYI, "Session Setup\n");
1370
1371 if (!server) {
1372 WARN(1, "%s: server is NULL!\n", __func__);
1373 return -EIO;
1374 }
1375
1376 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1377 if (!sess_data)
1378 return -ENOMEM;
1379
1380 rc = SMB2_select_sec(ses, sess_data);
1381 if (rc)
1382 goto out;
1383 sess_data->xid = xid;
1384 sess_data->ses = ses;
1385 sess_data->buf0_type = CIFS_NO_BUFFER;
1386 sess_data->nls_cp = (struct nls_table *) nls_cp;
1387 sess_data->previous_session = ses->Suid;
1388
1389 /*
1390 * Initialize the session hash with the server one.
1391 */
1392 memcpy(ses->preauth_sha_hash, ses->server->preauth_sha_hash,
1393 SMB2_PREAUTH_HASH_SIZE);
1394
1395 while (sess_data->func)
1396 sess_data->func(sess_data);
1397
1398 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1399 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1400 rc = sess_data->result;
1401out:
1402 kfree(sess_data);
1403 return rc;
1404}
1405
1406int
1407SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1408{
1409 struct smb_rqst rqst;
1410 struct smb2_logoff_req *req; /* response is also trivial struct */
1411 int rc = 0;
1412 struct TCP_Server_Info *server;
1413 int flags = 0;
1414 unsigned int total_len;
1415 struct kvec iov[1];
1416 struct kvec rsp_iov;
1417 int resp_buf_type;
1418
1419 cifs_dbg(FYI, "disconnect session %p\n", ses);
1420
1421 if (ses && (ses->server))
1422 server = ses->server;
1423 else
1424 return -EIO;
1425
1426 /* no need to send SMB logoff if uid already closed due to reconnect */
1427 if (ses->need_reconnect)
1428 goto smb2_session_already_dead;
1429
1430 rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, (void **) &req, &total_len);
1431 if (rc)
1432 return rc;
1433
1434 /* since no tcon, smb2_init can not do this, so do here */
1435 req->sync_hdr.SessionId = ses->Suid;
1436
1437 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1438 flags |= CIFS_TRANSFORM_REQ;
1439 else if (server->sign)
1440 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1441
1442 flags |= CIFS_NO_RESP;
1443
1444 iov[0].iov_base = (char *)req;
1445 iov[0].iov_len = total_len;
1446
1447 memset(&rqst, 0, sizeof(struct smb_rqst));
1448 rqst.rq_iov = iov;
1449 rqst.rq_nvec = 1;
1450
1451 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1452 cifs_small_buf_release(req);
1453 /*
1454 * No tcon so can't do
1455 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1456 */
1457
1458smb2_session_already_dead:
1459 return rc;
1460}
1461
1462static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1463{
1464 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1465}
1466
1467#define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1468
1469/* These are similar values to what Windows uses */
1470static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1471{
1472 tcon->max_chunks = 256;
1473 tcon->max_bytes_chunk = 1048576;
1474 tcon->max_bytes_copy = 16777216;
1475}
1476
1477int
1478SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1479 struct cifs_tcon *tcon, const struct nls_table *cp)
1480{
1481 struct smb_rqst rqst;
1482 struct smb2_tree_connect_req *req;
1483 struct smb2_tree_connect_rsp *rsp = NULL;
1484 struct kvec iov[2];
1485 struct kvec rsp_iov = { NULL, 0 };
1486 int rc = 0;
1487 int resp_buftype;
1488 int unc_path_len;
1489 __le16 *unc_path = NULL;
1490 int flags = 0;
1491 unsigned int total_len;
1492
1493 cifs_dbg(FYI, "TCON\n");
1494
1495 if (!(ses->server) || !tree)
1496 return -EIO;
1497
1498 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1499 if (unc_path == NULL)
1500 return -ENOMEM;
1501
1502 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1503 unc_path_len *= 2;
1504 if (unc_path_len < 2) {
1505 kfree(unc_path);
1506 return -EINVAL;
1507 }
1508
1509 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1510 tcon->tid = 0;
1511
1512 rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, (void **) &req,
1513 &total_len);
1514 if (rc) {
1515 kfree(unc_path);
1516 return rc;
1517 }
1518
1519 if (smb3_encryption_required(tcon))
1520 flags |= CIFS_TRANSFORM_REQ;
1521
1522 iov[0].iov_base = (char *)req;
1523 /* 1 for pad */
1524 iov[0].iov_len = total_len - 1;
1525
1526 /* Testing shows that buffer offset must be at location of Buffer[0] */
1527 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1528 - 1 /* pad */);
1529 req->PathLength = cpu_to_le16(unc_path_len - 2);
1530 iov[1].iov_base = unc_path;
1531 iov[1].iov_len = unc_path_len;
1532
1533 /*
1534 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
1535 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
1536 * (Samba servers don't always set the flag so also check if null user)
1537 */
1538 if ((ses->server->dialect == SMB311_PROT_ID) &&
1539 !smb3_encryption_required(tcon) &&
1540 !(ses->session_flags &
1541 (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
1542 ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
1543 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1544
1545 memset(&rqst, 0, sizeof(struct smb_rqst));
1546 rqst.rq_iov = iov;
1547 rqst.rq_nvec = 2;
1548
1549 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
1550 cifs_small_buf_release(req);
1551 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1552
1553 if (rc != 0) {
1554 if (tcon) {
1555 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1556 tcon->need_reconnect = true;
1557 }
1558 goto tcon_error_exit;
1559 }
1560
1561 switch (rsp->ShareType) {
1562 case SMB2_SHARE_TYPE_DISK:
1563 cifs_dbg(FYI, "connection to disk share\n");
1564 break;
1565 case SMB2_SHARE_TYPE_PIPE:
1566 tcon->pipe = true;
1567 cifs_dbg(FYI, "connection to pipe share\n");
1568 break;
1569 case SMB2_SHARE_TYPE_PRINT:
1570 tcon->print = true;
1571 cifs_dbg(FYI, "connection to printer\n");
1572 break;
1573 default:
1574 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1575 rc = -EOPNOTSUPP;
1576 goto tcon_error_exit;
1577 }
1578
1579 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1580 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1581 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1582 tcon->tidStatus = CifsGood;
1583 tcon->need_reconnect = false;
1584 tcon->tid = rsp->sync_hdr.TreeId;
1585 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1586
1587 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1588 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1589 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1590
1591 if (tcon->seal &&
1592 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1593 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1594
1595 init_copy_chunk_defaults(tcon);
1596 if (tcon->ses->server->ops->validate_negotiate)
1597 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1598tcon_exit:
1599 free_rsp_buf(resp_buftype, rsp);
1600 kfree(unc_path);
1601 return rc;
1602
1603tcon_error_exit:
1604 if (rsp && rsp->sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1605 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1606 }
1607 goto tcon_exit;
1608}
1609
1610int
1611SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1612{
1613 struct smb_rqst rqst;
1614 struct smb2_tree_disconnect_req *req; /* response is trivial */
1615 int rc = 0;
1616 struct cifs_ses *ses = tcon->ses;
1617 int flags = 0;
1618 unsigned int total_len;
1619 struct kvec iov[1];
1620 struct kvec rsp_iov;
1621 int resp_buf_type;
1622
1623 cifs_dbg(FYI, "Tree Disconnect\n");
1624
1625 if (!ses || !(ses->server))
1626 return -EIO;
1627
1628 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1629 return 0;
1630
1631 rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req,
1632 &total_len);
1633 if (rc)
1634 return rc;
1635
1636 if (smb3_encryption_required(tcon))
1637 flags |= CIFS_TRANSFORM_REQ;
1638
1639 flags |= CIFS_NO_RESP;
1640
1641 iov[0].iov_base = (char *)req;
1642 iov[0].iov_len = total_len;
1643
1644 memset(&rqst, 0, sizeof(struct smb_rqst));
1645 rqst.rq_iov = iov;
1646 rqst.rq_nvec = 1;
1647
1648 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
1649 cifs_small_buf_release(req);
1650 if (rc)
1651 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1652
1653 return rc;
1654}
1655
1656
1657static struct create_durable *
1658create_durable_buf(void)
1659{
1660 struct create_durable *buf;
1661
1662 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1663 if (!buf)
1664 return NULL;
1665
1666 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1667 (struct create_durable, Data));
1668 buf->ccontext.DataLength = cpu_to_le32(16);
1669 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1670 (struct create_durable, Name));
1671 buf->ccontext.NameLength = cpu_to_le16(4);
1672 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1673 buf->Name[0] = 'D';
1674 buf->Name[1] = 'H';
1675 buf->Name[2] = 'n';
1676 buf->Name[3] = 'Q';
1677 return buf;
1678}
1679
1680static struct create_durable *
1681create_reconnect_durable_buf(struct cifs_fid *fid)
1682{
1683 struct create_durable *buf;
1684
1685 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1686 if (!buf)
1687 return NULL;
1688
1689 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1690 (struct create_durable, Data));
1691 buf->ccontext.DataLength = cpu_to_le32(16);
1692 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1693 (struct create_durable, Name));
1694 buf->ccontext.NameLength = cpu_to_le16(4);
1695 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1696 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1697 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1698 buf->Name[0] = 'D';
1699 buf->Name[1] = 'H';
1700 buf->Name[2] = 'n';
1701 buf->Name[3] = 'C';
1702 return buf;
1703}
1704
1705static __u8
1706parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1707 unsigned int *epoch, char *lease_key)
1708{
1709 char *data_offset;
1710 struct create_context *cc;
1711 unsigned int next;
1712 unsigned int remaining;
1713 char *name;
1714
1715 data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
1716 remaining = le32_to_cpu(rsp->CreateContextsLength);
1717 cc = (struct create_context *)data_offset;
1718 while (remaining >= sizeof(struct create_context)) {
1719 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1720 if (le16_to_cpu(cc->NameLength) == 4 &&
1721 strncmp(name, "RqLs", 4) == 0)
1722 return server->ops->parse_lease_buf(cc, epoch,
1723 lease_key);
1724
1725 next = le32_to_cpu(cc->Next);
1726 if (!next)
1727 break;
1728 remaining -= next;
1729 cc = (struct create_context *)((char *)cc + next);
1730 }
1731
1732 return 0;
1733}
1734
1735static int
1736add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1737 unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
1738{
1739 struct smb2_create_req *req = iov[0].iov_base;
1740 unsigned int num = *num_iovec;
1741
1742 iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
1743 if (iov[num].iov_base == NULL)
1744 return -ENOMEM;
1745 iov[num].iov_len = server->vals->create_lease_size;
1746 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1747 if (!req->CreateContextsOffset)
1748 req->CreateContextsOffset = cpu_to_le32(
1749 sizeof(struct smb2_create_req) +
1750 iov[num - 1].iov_len);
1751 le32_add_cpu(&req->CreateContextsLength,
1752 server->vals->create_lease_size);
1753 *num_iovec = num + 1;
1754 return 0;
1755}
1756
1757static struct create_durable_v2 *
1758create_durable_v2_buf(struct cifs_fid *pfid)
1759{
1760 struct create_durable_v2 *buf;
1761
1762 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1763 if (!buf)
1764 return NULL;
1765
1766 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1767 (struct create_durable_v2, dcontext));
1768 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1769 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1770 (struct create_durable_v2, Name));
1771 buf->ccontext.NameLength = cpu_to_le16(4);
1772
1773 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1774 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1775 generate_random_uuid(buf->dcontext.CreateGuid);
1776 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1777
1778 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1779 buf->Name[0] = 'D';
1780 buf->Name[1] = 'H';
1781 buf->Name[2] = '2';
1782 buf->Name[3] = 'Q';
1783 return buf;
1784}
1785
1786static struct create_durable_handle_reconnect_v2 *
1787create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1788{
1789 struct create_durable_handle_reconnect_v2 *buf;
1790
1791 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1792 GFP_KERNEL);
1793 if (!buf)
1794 return NULL;
1795
1796 buf->ccontext.DataOffset =
1797 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1798 dcontext));
1799 buf->ccontext.DataLength =
1800 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1801 buf->ccontext.NameOffset =
1802 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1803 Name));
1804 buf->ccontext.NameLength = cpu_to_le16(4);
1805
1806 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1807 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1808 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1809 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1810
1811 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1812 buf->Name[0] = 'D';
1813 buf->Name[1] = 'H';
1814 buf->Name[2] = '2';
1815 buf->Name[3] = 'C';
1816 return buf;
1817}
1818
1819static int
1820add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1821 struct cifs_open_parms *oparms)
1822{
1823 struct smb2_create_req *req = iov[0].iov_base;
1824 unsigned int num = *num_iovec;
1825
1826 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1827 if (iov[num].iov_base == NULL)
1828 return -ENOMEM;
1829 iov[num].iov_len = sizeof(struct create_durable_v2);
1830 if (!req->CreateContextsOffset)
1831 req->CreateContextsOffset =
1832 cpu_to_le32(sizeof(struct smb2_create_req) +
1833 iov[1].iov_len);
1834 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1835 *num_iovec = num + 1;
1836 return 0;
1837}
1838
1839static int
1840add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1841 struct cifs_open_parms *oparms)
1842{
1843 struct smb2_create_req *req = iov[0].iov_base;
1844 unsigned int num = *num_iovec;
1845
1846 /* indicate that we don't need to relock the file */
1847 oparms->reconnect = false;
1848
1849 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1850 if (iov[num].iov_base == NULL)
1851 return -ENOMEM;
1852 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1853 if (!req->CreateContextsOffset)
1854 req->CreateContextsOffset =
1855 cpu_to_le32(sizeof(struct smb2_create_req) +
1856 iov[1].iov_len);
1857 le32_add_cpu(&req->CreateContextsLength,
1858 sizeof(struct create_durable_handle_reconnect_v2));
1859 *num_iovec = num + 1;
1860 return 0;
1861}
1862
1863static int
1864add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1865 struct cifs_open_parms *oparms, bool use_persistent)
1866{
1867 struct smb2_create_req *req = iov[0].iov_base;
1868 unsigned int num = *num_iovec;
1869
1870 if (use_persistent) {
1871 if (oparms->reconnect)
1872 return add_durable_reconnect_v2_context(iov, num_iovec,
1873 oparms);
1874 else
1875 return add_durable_v2_context(iov, num_iovec, oparms);
1876 }
1877
1878 if (oparms->reconnect) {
1879 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1880 /* indicate that we don't need to relock the file */
1881 oparms->reconnect = false;
1882 } else
1883 iov[num].iov_base = create_durable_buf();
1884 if (iov[num].iov_base == NULL)
1885 return -ENOMEM;
1886 iov[num].iov_len = sizeof(struct create_durable);
1887 if (!req->CreateContextsOffset)
1888 req->CreateContextsOffset =
1889 cpu_to_le32(sizeof(struct smb2_create_req) +
1890 iov[1].iov_len);
1891 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1892 *num_iovec = num + 1;
1893 return 0;
1894}
1895
1896/* See MS-SMB2 2.2.13.2.7 */
1897static struct crt_twarp_ctxt *
1898create_twarp_buf(__u64 timewarp)
1899{
1900 struct crt_twarp_ctxt *buf;
1901
1902 buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
1903 if (!buf)
1904 return NULL;
1905
1906 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1907 (struct crt_twarp_ctxt, Timestamp));
1908 buf->ccontext.DataLength = cpu_to_le32(8);
1909 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1910 (struct crt_twarp_ctxt, Name));
1911 buf->ccontext.NameLength = cpu_to_le16(4);
1912 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
1913 buf->Name[0] = 'T';
1914 buf->Name[1] = 'W';
1915 buf->Name[2] = 'r';
1916 buf->Name[3] = 'p';
1917 buf->Timestamp = cpu_to_le64(timewarp);
1918 return buf;
1919}
1920
1921/* See MS-SMB2 2.2.13.2.7 */
1922static int
1923add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
1924{
1925 struct smb2_create_req *req = iov[0].iov_base;
1926 unsigned int num = *num_iovec;
1927
1928 iov[num].iov_base = create_twarp_buf(timewarp);
1929 if (iov[num].iov_base == NULL)
1930 return -ENOMEM;
1931 iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
1932 if (!req->CreateContextsOffset)
1933 req->CreateContextsOffset = cpu_to_le32(
1934 sizeof(struct smb2_create_req) +
1935 iov[num - 1].iov_len);
1936 le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
1937 *num_iovec = num + 1;
1938 return 0;
1939}
1940
1941static int
1942alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1943 const char *treename, const __le16 *path)
1944{
1945 int treename_len, path_len;
1946 struct nls_table *cp;
1947 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1948
1949 /*
1950 * skip leading "\\"
1951 */
1952 treename_len = strlen(treename);
1953 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1954 return -EINVAL;
1955
1956 treename += 2;
1957 treename_len -= 2;
1958
1959 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1960
1961 /*
1962 * make room for one path separator between the treename and
1963 * path
1964 */
1965 *out_len = treename_len + 1 + path_len;
1966
1967 /*
1968 * final path needs to be null-terminated UTF16 with a
1969 * size aligned to 8
1970 */
1971
1972 *out_size = roundup((*out_len+1)*2, 8);
1973 *out_path = kzalloc(*out_size, GFP_KERNEL);
1974 if (!*out_path)
1975 return -ENOMEM;
1976
1977 cp = load_nls_default();
1978 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1979 UniStrcat(*out_path, sep);
1980 UniStrcat(*out_path, path);
1981 unload_nls(cp);
1982
1983 return 0;
1984}
1985
1986int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
1987 umode_t mode, struct cifs_tcon *tcon,
1988 const char *full_path,
1989 struct cifs_sb_info *cifs_sb)
1990{
1991 struct smb_rqst rqst;
1992 struct smb2_create_req *req;
1993 struct smb2_create_rsp *rsp = NULL;
1994 struct TCP_Server_Info *server;
1995 struct cifs_ses *ses = tcon->ses;
1996 struct kvec iov[3]; /* make sure at least one for each open context */
1997 struct kvec rsp_iov = {NULL, 0};
1998 int resp_buftype;
1999 int uni_path_len;
2000 __le16 *copy_path = NULL;
2001 int copy_size;
2002 int rc = 0;
2003 unsigned int n_iov = 2;
2004 __u32 file_attributes = 0;
2005 char *pc_buf = NULL;
2006 int flags = 0;
2007 unsigned int total_len;
2008 __le16 *utf16_path = NULL;
2009
2010 cifs_dbg(FYI, "mkdir\n");
2011
2012 /* resource #1: path allocation */
2013 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2014 if (!utf16_path)
2015 return -ENOMEM;
2016
2017 if (ses && (ses->server))
2018 server = ses->server;
2019 else {
2020 rc = -EIO;
2021 goto err_free_path;
2022 }
2023
2024 /* resource #2: request */
2025 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2026 if (rc)
2027 goto err_free_path;
2028
2029
2030 if (smb3_encryption_required(tcon))
2031 flags |= CIFS_TRANSFORM_REQ;
2032
2033 req->ImpersonationLevel = IL_IMPERSONATION;
2034 req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
2035 /* File attributes ignored on open (used in create though) */
2036 req->FileAttributes = cpu_to_le32(file_attributes);
2037 req->ShareAccess = FILE_SHARE_ALL_LE;
2038 req->CreateDisposition = cpu_to_le32(FILE_CREATE);
2039 req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
2040
2041 iov[0].iov_base = (char *)req;
2042 /* -1 since last byte is buf[0] which is sent below (path) */
2043 iov[0].iov_len = total_len - 1;
2044
2045 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2046
2047 /* [MS-SMB2] 2.2.13 NameOffset:
2048 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2049 * the SMB2 header, the file name includes a prefix that will
2050 * be processed during DFS name normalization as specified in
2051 * section 3.3.5.9. Otherwise, the file name is relative to
2052 * the share that is identified by the TreeId in the SMB2
2053 * header.
2054 */
2055 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2056 int name_len;
2057
2058 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2059 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2060 &name_len,
2061 tcon->treeName, utf16_path);
2062 if (rc)
2063 goto err_free_req;
2064
2065 req->NameLength = cpu_to_le16(name_len * 2);
2066 uni_path_len = copy_size;
2067 /* free before overwriting resource */
2068 kfree(utf16_path);
2069 utf16_path = copy_path;
2070 } else {
2071 uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
2072 /* MUST set path len (NameLength) to 0 opening root of share */
2073 req->NameLength = cpu_to_le16(uni_path_len - 2);
2074 if (uni_path_len % 8 != 0) {
2075 copy_size = roundup(uni_path_len, 8);
2076 copy_path = kzalloc(copy_size, GFP_KERNEL);
2077 if (!copy_path) {
2078 rc = -ENOMEM;
2079 goto err_free_req;
2080 }
2081 memcpy((char *)copy_path, (const char *)utf16_path,
2082 uni_path_len);
2083 uni_path_len = copy_size;
2084 /* free before overwriting resource */
2085 kfree(utf16_path);
2086 utf16_path = copy_path;
2087 }
2088 }
2089
2090 iov[1].iov_len = uni_path_len;
2091 iov[1].iov_base = utf16_path;
2092 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2093
2094 if (tcon->posix_extensions) {
2095 /* resource #3: posix buf */
2096 rc = add_posix_context(iov, &n_iov, mode);
2097 if (rc)
2098 goto err_free_req;
2099 pc_buf = iov[n_iov-1].iov_base;
2100 }
2101
2102
2103 memset(&rqst, 0, sizeof(struct smb_rqst));
2104 rqst.rq_iov = iov;
2105 rqst.rq_nvec = n_iov;
2106
2107 /* resource #4: response buffer */
2108 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2109 if (rc) {
2110 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2111 trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
2112 CREATE_NOT_FILE,
2113 FILE_WRITE_ATTRIBUTES, rc);
2114 goto err_free_rsp_buf;
2115 }
2116
2117 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2118 trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid,
2119 ses->Suid, CREATE_NOT_FILE,
2120 FILE_WRITE_ATTRIBUTES);
2121
2122 SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
2123
2124 /* Eventually save off posix specific response info and timestaps */
2125
2126err_free_rsp_buf:
2127 free_rsp_buf(resp_buftype, rsp);
2128 kfree(pc_buf);
2129err_free_req:
2130 cifs_small_buf_release(req);
2131err_free_path:
2132 kfree(utf16_path);
2133 return rc;
2134}
2135
2136int
2137SMB2_open_init(struct cifs_tcon *tcon, struct smb_rqst *rqst, __u8 *oplock,
2138 struct cifs_open_parms *oparms, __le16 *path)
2139{
2140 struct TCP_Server_Info *server = tcon->ses->server;
2141 struct smb2_create_req *req;
2142 unsigned int n_iov = 2;
2143 __u32 file_attributes = 0;
2144 int copy_size;
2145 int uni_path_len;
2146 unsigned int total_len;
2147 struct kvec *iov = rqst->rq_iov;
2148 __le16 *copy_path;
2149 int rc;
2150
2151 rc = smb2_plain_req_init(SMB2_CREATE, tcon, (void **) &req, &total_len);
2152 if (rc)
2153 return rc;
2154
2155 iov[0].iov_base = (char *)req;
2156 /* -1 since last byte is buf[0] which is sent below (path) */
2157 iov[0].iov_len = total_len - 1;
2158
2159 if (oparms->create_options & CREATE_OPTION_READONLY)
2160 file_attributes |= ATTR_READONLY;
2161 if (oparms->create_options & CREATE_OPTION_SPECIAL)
2162 file_attributes |= ATTR_SYSTEM;
2163
2164 req->ImpersonationLevel = IL_IMPERSONATION;
2165 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
2166 /* File attributes ignored on open (used in create though) */
2167 req->FileAttributes = cpu_to_le32(file_attributes);
2168 req->ShareAccess = FILE_SHARE_ALL_LE;
2169 req->CreateDisposition = cpu_to_le32(oparms->disposition);
2170 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
2171 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
2172
2173 /* [MS-SMB2] 2.2.13 NameOffset:
2174 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2175 * the SMB2 header, the file name includes a prefix that will
2176 * be processed during DFS name normalization as specified in
2177 * section 3.3.5.9. Otherwise, the file name is relative to
2178 * the share that is identified by the TreeId in the SMB2
2179 * header.
2180 */
2181 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
2182 int name_len;
2183
2184 req->sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
2185 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
2186 &name_len,
2187 tcon->treeName, path);
2188 if (rc)
2189 return rc;
2190 req->NameLength = cpu_to_le16(name_len * 2);
2191 uni_path_len = copy_size;
2192 path = copy_path;
2193 } else {
2194 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
2195 /* MUST set path len (NameLength) to 0 opening root of share */
2196 req->NameLength = cpu_to_le16(uni_path_len - 2);
2197 copy_size = uni_path_len;
2198 if (copy_size % 8 != 0)
2199 copy_size = roundup(copy_size, 8);
2200 copy_path = kzalloc(copy_size, GFP_KERNEL);
2201 if (!copy_path)
2202 return -ENOMEM;
2203 memcpy((char *)copy_path, (const char *)path,
2204 uni_path_len);
2205 uni_path_len = copy_size;
2206 path = copy_path;
2207 }
2208
2209 iov[1].iov_len = uni_path_len;
2210 iov[1].iov_base = path;
2211
2212 if ((!server->oplocks) || (tcon->no_lease))
2213 *oplock = SMB2_OPLOCK_LEVEL_NONE;
2214
2215 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
2216 *oplock == SMB2_OPLOCK_LEVEL_NONE)
2217 req->RequestedOplockLevel = *oplock;
2218 else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
2219 (oparms->create_options & CREATE_NOT_FILE))
2220 req->RequestedOplockLevel = *oplock; /* no srv lease support */
2221 else {
2222 rc = add_lease_context(server, iov, &n_iov,
2223 oparms->fid->lease_key, oplock);
2224 if (rc)
2225 return rc;
2226 }
2227
2228 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
2229 /* need to set Next field of lease context if we request it */
2230 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
2231 struct create_context *ccontext =
2232 (struct create_context *)iov[n_iov-1].iov_base;
2233 ccontext->Next =
2234 cpu_to_le32(server->vals->create_lease_size);
2235 }
2236
2237 rc = add_durable_context(iov, &n_iov, oparms,
2238 tcon->use_persistent);
2239 if (rc)
2240 return rc;
2241 }
2242
2243 if (tcon->posix_extensions) {
2244 if (n_iov > 2) {
2245 struct create_context *ccontext =
2246 (struct create_context *)iov[n_iov-1].iov_base;
2247 ccontext->Next =
2248 cpu_to_le32(iov[n_iov-1].iov_len);
2249 }
2250
2251 rc = add_posix_context(iov, &n_iov, oparms->mode);
2252 if (rc)
2253 return rc;
2254 }
2255
2256 if (tcon->snapshot_time) {
2257 cifs_dbg(FYI, "adding snapshot context\n");
2258 if (n_iov > 2) {
2259 struct create_context *ccontext =
2260 (struct create_context *)iov[n_iov-1].iov_base;
2261 ccontext->Next =
2262 cpu_to_le32(iov[n_iov-1].iov_len);
2263 }
2264
2265 rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
2266 if (rc)
2267 return rc;
2268 }
2269
2270
2271 rqst->rq_nvec = n_iov;
2272 return 0;
2273}
2274
2275/* rq_iov[0] is the request and is released by cifs_small_buf_release().
2276 * All other vectors are freed by kfree().
2277 */
2278void
2279SMB2_open_free(struct smb_rqst *rqst)
2280{
2281 int i;
2282
2283 if (rqst && rqst->rq_iov) {
2284 cifs_small_buf_release(rqst->rq_iov[0].iov_base);
2285 for (i = 1; i < rqst->rq_nvec; i++)
2286 if (rqst->rq_iov[i].iov_base != smb2_padding)
2287 kfree(rqst->rq_iov[i].iov_base);
2288 }
2289}
2290
2291int
2292SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
2293 __u8 *oplock, struct smb2_file_all_info *buf,
2294 struct kvec *err_iov, int *buftype)
2295{
2296 struct smb_rqst rqst;
2297 struct smb2_create_rsp *rsp = NULL;
2298 struct TCP_Server_Info *server;
2299 struct cifs_tcon *tcon = oparms->tcon;
2300 struct cifs_ses *ses = tcon->ses;
2301 struct kvec iov[SMB2_CREATE_IOV_SIZE];
2302 struct kvec rsp_iov = {NULL, 0};
2303 int resp_buftype = CIFS_NO_BUFFER;
2304 int rc = 0;
2305 int flags = 0;
2306
2307 cifs_dbg(FYI, "create/open\n");
2308 if (ses && (ses->server))
2309 server = ses->server;
2310 else
2311 return -EIO;
2312
2313 if (smb3_encryption_required(tcon))
2314 flags |= CIFS_TRANSFORM_REQ;
2315
2316 memset(&rqst, 0, sizeof(struct smb_rqst));
2317 memset(&iov, 0, sizeof(iov));
2318 rqst.rq_iov = iov;
2319 rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
2320
2321 rc = SMB2_open_init(tcon, &rqst, oplock, oparms, path);
2322 if (rc)
2323 goto creat_exit;
2324
2325 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2326 &rsp_iov);
2327 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
2328
2329 if (rc != 0) {
2330 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
2331 if (err_iov && rsp) {
2332 *err_iov = rsp_iov;
2333 *buftype = resp_buftype;
2334 resp_buftype = CIFS_NO_BUFFER;
2335 rsp = NULL;
2336 }
2337 trace_smb3_open_err(xid, tcon->tid, ses->Suid,
2338 oparms->create_options, oparms->desired_access, rc);
2339 goto creat_exit;
2340 } else
2341 trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid,
2342 ses->Suid, oparms->create_options,
2343 oparms->desired_access);
2344
2345 oparms->fid->persistent_fid = rsp->PersistentFileId;
2346 oparms->fid->volatile_fid = rsp->VolatileFileId;
2347
2348 if (buf) {
2349 memcpy(buf, &rsp->CreationTime, 32);
2350 buf->AllocationSize = rsp->AllocationSize;
2351 buf->EndOfFile = rsp->EndofFile;
2352 buf->Attributes = rsp->FileAttributes;
2353 buf->NumberOfLinks = cpu_to_le32(1);
2354 buf->DeletePending = 0;
2355 }
2356
2357 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
2358 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch,
2359 oparms->fid->lease_key);
2360 else
2361 *oplock = rsp->OplockLevel;
2362creat_exit:
2363 SMB2_open_free(&rqst);
2364 free_rsp_buf(resp_buftype, rsp);
2365 return rc;
2366}
2367
2368/*
2369 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
2370 */
2371int
2372SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2373 u64 volatile_fid, u32 opcode, bool is_fsctl,
2374 char *in_data, u32 indatalen,
2375 char **out_data, u32 *plen /* returned data len */)
2376{
2377 struct smb_rqst rqst;
2378 struct smb2_ioctl_req *req;
2379 struct smb2_ioctl_rsp *rsp;
2380 struct cifs_ses *ses;
2381 struct kvec iov[2];
2382 struct kvec rsp_iov;
2383 int resp_buftype;
2384 int n_iov;
2385 int rc = 0;
2386 int flags = 0;
2387 unsigned int total_len;
2388
2389 cifs_dbg(FYI, "SMB2 IOCTL\n");
2390
2391 if (out_data != NULL)
2392 *out_data = NULL;
2393
2394 /* zero out returned data len, in case of error */
2395 if (plen)
2396 *plen = 0;
2397
2398 if (tcon)
2399 ses = tcon->ses;
2400 else
2401 return -EIO;
2402
2403 if (!ses || !(ses->server))
2404 return -EIO;
2405
2406 rc = smb2_ioctl_req_init(opcode, tcon, (void **) &req, &total_len);
2407 if (rc)
2408 return rc;
2409
2410 if (smb3_encryption_required(tcon))
2411 flags |= CIFS_TRANSFORM_REQ;
2412
2413 req->CtlCode = cpu_to_le32(opcode);
2414 req->PersistentFileId = persistent_fid;
2415 req->VolatileFileId = volatile_fid;
2416
2417 if (indatalen) {
2418 req->InputCount = cpu_to_le32(indatalen);
2419 /* do not set InputOffset if no input data */
2420 req->InputOffset =
2421 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
2422 iov[1].iov_base = in_data;
2423 iov[1].iov_len = indatalen;
2424 n_iov = 2;
2425 } else
2426 n_iov = 1;
2427
2428 req->OutputOffset = 0;
2429 req->OutputCount = 0; /* MBZ */
2430
2431 /*
2432 * Could increase MaxOutputResponse, but that would require more
2433 * than one credit. Windows typically sets this smaller, but for some
2434 * ioctls it may be useful to allow server to send more. No point
2435 * limiting what the server can send as long as fits in one credit
2436 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
2437 * (by default, note that it can be overridden to make max larger)
2438 * in responses (except for read responses which can be bigger.
2439 * We may want to bump this limit up
2440 */
2441 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
2442
2443 if (is_fsctl)
2444 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
2445 else
2446 req->Flags = 0;
2447
2448 iov[0].iov_base = (char *)req;
2449
2450 /*
2451 * If no input data, the size of ioctl struct in
2452 * protocol spec still includes a 1 byte data buffer,
2453 * but if input data passed to ioctl, we do not
2454 * want to double count this, so we do not send
2455 * the dummy one byte of data in iovec[0] if sending
2456 * input data (in iovec[1]).
2457 */
2458
2459 if (indatalen) {
2460 iov[0].iov_len = total_len - 1;
2461 } else
2462 iov[0].iov_len = total_len;
2463
2464 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
2465 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
2466 req->sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
2467
2468 memset(&rqst, 0, sizeof(struct smb_rqst));
2469 rqst.rq_iov = iov;
2470 rqst.rq_nvec = n_iov;
2471
2472 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
2473 &rsp_iov);
2474 cifs_small_buf_release(req);
2475 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
2476
2477 if (rc != 0)
2478 trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
2479 ses->Suid, 0, opcode, rc);
2480
2481 if ((rc != 0) && (rc != -EINVAL)) {
2482 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2483 goto ioctl_exit;
2484 } else if (rc == -EINVAL) {
2485 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
2486 (opcode != FSCTL_SRV_COPYCHUNK)) {
2487 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
2488 goto ioctl_exit;
2489 }
2490 }
2491
2492 /* check if caller wants to look at return data or just return rc */
2493 if ((plen == NULL) || (out_data == NULL))
2494 goto ioctl_exit;
2495
2496 *plen = le32_to_cpu(rsp->OutputCount);
2497
2498 /* We check for obvious errors in the output buffer length and offset */
2499 if (*plen == 0)
2500 goto ioctl_exit; /* server returned no data */
2501 else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
2502 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
2503 *plen = 0;
2504 rc = -EIO;
2505 goto ioctl_exit;
2506 }
2507
2508 if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
2509 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2510 le32_to_cpu(rsp->OutputOffset));
2511 *plen = 0;
2512 rc = -EIO;
2513 goto ioctl_exit;
2514 }
2515
2516 *out_data = kmalloc(*plen, GFP_KERNEL);
2517 if (*out_data == NULL) {
2518 rc = -ENOMEM;
2519 goto ioctl_exit;
2520 }
2521
2522 memcpy(*out_data, (char *)rsp + le32_to_cpu(rsp->OutputOffset), *plen);
2523ioctl_exit:
2524 free_rsp_buf(resp_buftype, rsp);
2525 return rc;
2526}
2527
2528/*
2529 * Individual callers to ioctl worker function follow
2530 */
2531
2532int
2533SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2534 u64 persistent_fid, u64 volatile_fid)
2535{
2536 int rc;
2537 struct compress_ioctl fsctl_input;
2538 char *ret_data = NULL;
2539
2540 fsctl_input.CompressionState =
2541 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2542
2543 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2544 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2545 (char *)&fsctl_input /* data input */,
2546 2 /* in data len */, &ret_data /* out data */, NULL);
2547
2548 cifs_dbg(FYI, "set compression rc %d\n", rc);
2549
2550 return rc;
2551}
2552
2553int
2554SMB2_close_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2555 u64 persistent_fid, u64 volatile_fid)
2556{
2557 struct smb2_close_req *req;
2558 struct kvec *iov = rqst->rq_iov;
2559 unsigned int total_len;
2560 int rc;
2561
2562 rc = smb2_plain_req_init(SMB2_CLOSE, tcon, (void **) &req, &total_len);
2563 if (rc)
2564 return rc;
2565
2566 req->PersistentFileId = persistent_fid;
2567 req->VolatileFileId = volatile_fid;
2568 iov[0].iov_base = (char *)req;
2569 iov[0].iov_len = total_len;
2570
2571 return 0;
2572}
2573
2574void
2575SMB2_close_free(struct smb_rqst *rqst)
2576{
2577 if (rqst && rqst->rq_iov)
2578 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2579}
2580
2581int
2582SMB2_close_flags(const unsigned int xid, struct cifs_tcon *tcon,
2583 u64 persistent_fid, u64 volatile_fid, int flags)
2584{
2585 struct smb_rqst rqst;
2586 struct smb2_close_rsp *rsp = NULL;
2587 struct cifs_ses *ses = tcon->ses;
2588 struct kvec iov[1];
2589 struct kvec rsp_iov;
2590 int resp_buftype = CIFS_NO_BUFFER;
2591 int rc = 0;
2592
2593 cifs_dbg(FYI, "Close\n");
2594
2595 if (!ses || !(ses->server))
2596 return -EIO;
2597
2598 if (smb3_encryption_required(tcon))
2599 flags |= CIFS_TRANSFORM_REQ;
2600
2601 memset(&rqst, 0, sizeof(struct smb_rqst));
2602 memset(&iov, 0, sizeof(iov));
2603 rqst.rq_iov = iov;
2604 rqst.rq_nvec = 1;
2605
2606 rc = SMB2_close_init(tcon, &rqst, persistent_fid, volatile_fid);
2607 if (rc)
2608 goto close_exit;
2609
2610 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2611 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2612
2613 if (rc != 0) {
2614 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2615 trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
2616 rc);
2617 goto close_exit;
2618 }
2619
2620 /* BB FIXME - decode close response, update inode for caching */
2621
2622close_exit:
2623 SMB2_close_free(&rqst);
2624 free_rsp_buf(resp_buftype, rsp);
2625 return rc;
2626}
2627
2628int
2629SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2630 u64 persistent_fid, u64 volatile_fid)
2631{
2632 int rc;
2633 int tmp_rc;
2634
2635 rc = SMB2_close_flags(xid, tcon, persistent_fid, volatile_fid, 0);
2636
2637 /* retry close in a worker thread if this one is interrupted */
2638 if (rc == -EINTR) {
2639 tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
2640 volatile_fid);
2641 if (tmp_rc)
2642 cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
2643 persistent_fid, tmp_rc);
2644 }
2645
2646 return rc;
2647}
2648
2649int
2650smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
2651 struct kvec *iov, unsigned int min_buf_size)
2652{
2653 unsigned int smb_len = iov->iov_len;
2654 char *end_of_smb = smb_len + (char *)iov->iov_base;
2655 char *begin_of_buf = offset + (char *)iov->iov_base;
2656 char *end_of_buf = begin_of_buf + buffer_length;
2657
2658
2659 if (buffer_length < min_buf_size) {
2660 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2661 buffer_length, min_buf_size);
2662 return -EINVAL;
2663 }
2664
2665 /* check if beyond RFC1001 maximum length */
2666 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2667 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2668 buffer_length, smb_len);
2669 return -EINVAL;
2670 }
2671
2672 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2673 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2674 return -EINVAL;
2675 }
2676
2677 return 0;
2678}
2679
2680/*
2681 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2682 * Caller must free buffer.
2683 */
2684static int
2685validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
2686 struct kvec *iov, unsigned int minbufsize,
2687 char *data)
2688{
2689 char *begin_of_buf = offset + (char *)iov->iov_base;
2690 int rc;
2691
2692 if (!data)
2693 return -EINVAL;
2694
2695 rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
2696 if (rc)
2697 return rc;
2698
2699 memcpy(data, begin_of_buf, buffer_length);
2700
2701 return 0;
2702}
2703
2704int
2705SMB2_query_info_init(struct cifs_tcon *tcon, struct smb_rqst *rqst,
2706 u64 persistent_fid, u64 volatile_fid,
2707 u8 info_class, u8 info_type, u32 additional_info,
2708 size_t output_len)
2709{
2710 struct smb2_query_info_req *req;
2711 struct kvec *iov = rqst->rq_iov;
2712 unsigned int total_len;
2713 int rc;
2714
2715 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
2716 &total_len);
2717 if (rc)
2718 return rc;
2719
2720 req->InfoType = info_type;
2721 req->FileInfoClass = info_class;
2722 req->PersistentFileId = persistent_fid;
2723 req->VolatileFileId = volatile_fid;
2724 req->AdditionalInformation = cpu_to_le32(additional_info);
2725
2726 /*
2727 * We do not use the input buffer (do not send extra byte)
2728 */
2729 req->InputBufferOffset = 0;
2730
2731 req->OutputBufferLength = cpu_to_le32(output_len);
2732
2733 iov[0].iov_base = (char *)req;
2734 /* 1 for Buffer */
2735 iov[0].iov_len = total_len - 1;
2736 return 0;
2737}
2738
2739void
2740SMB2_query_info_free(struct smb_rqst *rqst)
2741{
2742 if (rqst && rqst->rq_iov)
2743 cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
2744}
2745
2746static int
2747query_info(const unsigned int xid, struct cifs_tcon *tcon,
2748 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2749 u32 additional_info, size_t output_len, size_t min_len, void **data,
2750 u32 *dlen)
2751{
2752 struct smb_rqst rqst;
2753 struct smb2_query_info_rsp *rsp = NULL;
2754 struct kvec iov[1];
2755 struct kvec rsp_iov;
2756 int rc = 0;
2757 int resp_buftype = CIFS_NO_BUFFER;
2758 struct cifs_ses *ses = tcon->ses;
2759 int flags = 0;
2760
2761 cifs_dbg(FYI, "Query Info\n");
2762
2763 if (!ses || !(ses->server))
2764 return -EIO;
2765
2766 if (smb3_encryption_required(tcon))
2767 flags |= CIFS_TRANSFORM_REQ;
2768
2769 memset(&rqst, 0, sizeof(struct smb_rqst));
2770 memset(&iov, 0, sizeof(iov));
2771 rqst.rq_iov = iov;
2772 rqst.rq_nvec = 1;
2773
2774 rc = SMB2_query_info_init(tcon, &rqst, persistent_fid, volatile_fid,
2775 info_class, info_type, additional_info,
2776 output_len);
2777 if (rc)
2778 goto qinf_exit;
2779
2780 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2781 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2782
2783 if (rc) {
2784 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2785 trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
2786 ses->Suid, info_class, (__u32)info_type, rc);
2787 goto qinf_exit;
2788 }
2789
2790 if (dlen) {
2791 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2792 if (!*data) {
2793 *data = kmalloc(*dlen, GFP_KERNEL);
2794 if (!*data) {
2795 cifs_dbg(VFS,
2796 "Error %d allocating memory for acl\n",
2797 rc);
2798 *dlen = 0;
2799 goto qinf_exit;
2800 }
2801 }
2802 }
2803
2804 rc = validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
2805 le32_to_cpu(rsp->OutputBufferLength),
2806 &rsp_iov, min_len, *data);
2807
2808qinf_exit:
2809 SMB2_query_info_free(&rqst);
2810 free_rsp_buf(resp_buftype, rsp);
2811 return rc;
2812}
2813
2814int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
2815 u64 persistent_fid, u64 volatile_fid,
2816 int ea_buf_size, struct smb2_file_full_ea_info *data)
2817{
2818 return query_info(xid, tcon, persistent_fid, volatile_fid,
2819 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
2820 ea_buf_size,
2821 sizeof(struct smb2_file_full_ea_info),
2822 (void **)&data,
2823 NULL);
2824}
2825
2826int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2827 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2828{
2829 return query_info(xid, tcon, persistent_fid, volatile_fid,
2830 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2831 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2832 sizeof(struct smb2_file_all_info), (void **)&data,
2833 NULL);
2834}
2835
2836int
2837SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2838 u64 persistent_fid, u64 volatile_fid,
2839 void **data, u32 *plen)
2840{
2841 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2842 *plen = 0;
2843
2844 return query_info(xid, tcon, persistent_fid, volatile_fid,
2845 0, SMB2_O_INFO_SECURITY, additional_info,
2846 SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
2847}
2848
2849int
2850SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2851 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2852{
2853 return query_info(xid, tcon, persistent_fid, volatile_fid,
2854 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2855 sizeof(struct smb2_file_internal_info),
2856 sizeof(struct smb2_file_internal_info),
2857 (void **)&uniqueid, NULL);
2858}
2859
2860/*
2861 * This is a no-op for now. We're not really interested in the reply, but
2862 * rather in the fact that the server sent one and that server->lstrp
2863 * gets updated.
2864 *
2865 * FIXME: maybe we should consider checking that the reply matches request?
2866 */
2867static void
2868smb2_echo_callback(struct mid_q_entry *mid)
2869{
2870 struct TCP_Server_Info *server = mid->callback_data;
2871 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2872 unsigned int credits_received = 0;
2873
2874 if (mid->mid_state == MID_RESPONSE_RECEIVED
2875 || mid->mid_state == MID_RESPONSE_MALFORMED)
2876 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
2877
2878 DeleteMidQEntry(mid);
2879 add_credits(server, credits_received, CIFS_ECHO_OP);
2880}
2881
2882void smb2_reconnect_server(struct work_struct *work)
2883{
2884 struct TCP_Server_Info *server = container_of(work,
2885 struct TCP_Server_Info, reconnect.work);
2886 struct cifs_ses *ses;
2887 struct cifs_tcon *tcon, *tcon2;
2888 struct list_head tmp_list;
2889 int tcon_exist = false;
2890 int rc;
2891 int resched = false;
2892
2893
2894 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2895 mutex_lock(&server->reconnect_mutex);
2896
2897 INIT_LIST_HEAD(&tmp_list);
2898 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2899
2900 spin_lock(&cifs_tcp_ses_lock);
2901 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2902 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2903 if (tcon->need_reconnect || tcon->need_reopen_files) {
2904 tcon->tc_count++;
2905 list_add_tail(&tcon->rlist, &tmp_list);
2906 tcon_exist = true;
2907 }
2908 }
2909 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2910 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2911 tcon_exist = true;
2912 }
2913 }
2914 /*
2915 * Get the reference to server struct to be sure that the last call of
2916 * cifs_put_tcon() in the loop below won't release the server pointer.
2917 */
2918 if (tcon_exist)
2919 server->srv_count++;
2920
2921 spin_unlock(&cifs_tcp_ses_lock);
2922
2923 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2924 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2925 if (!rc)
2926 cifs_reopen_persistent_handles(tcon);
2927 else
2928 resched = true;
2929 list_del_init(&tcon->rlist);
2930 cifs_put_tcon(tcon);
2931 }
2932
2933 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2934 if (resched)
2935 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2936 mutex_unlock(&server->reconnect_mutex);
2937
2938 /* now we can safely release srv struct */
2939 if (tcon_exist)
2940 cifs_put_tcp_session(server, 1);
2941}
2942
2943int
2944SMB2_echo(struct TCP_Server_Info *server)
2945{
2946 struct smb2_echo_req *req;
2947 int rc = 0;
2948 struct kvec iov[1];
2949 struct smb_rqst rqst = { .rq_iov = iov,
2950 .rq_nvec = 1 };
2951 unsigned int total_len;
2952
2953 cifs_dbg(FYI, "In echo request\n");
2954
2955 if (server->tcpStatus == CifsNeedNegotiate) {
2956 /* No need to send echo on newly established connections */
2957 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2958 return rc;
2959 }
2960
2961 rc = smb2_plain_req_init(SMB2_ECHO, NULL, (void **)&req, &total_len);
2962 if (rc)
2963 return rc;
2964
2965 req->sync_hdr.CreditRequest = cpu_to_le16(1);
2966
2967 iov[0].iov_len = total_len;
2968 iov[0].iov_base = (char *)req;
2969
2970 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2971 server, CIFS_ECHO_OP);
2972 if (rc)
2973 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2974
2975 cifs_small_buf_release(req);
2976 return rc;
2977}
2978
2979int
2980SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2981 u64 volatile_fid)
2982{
2983 struct smb_rqst rqst;
2984 struct smb2_flush_req *req;
2985 struct cifs_ses *ses = tcon->ses;
2986 struct kvec iov[1];
2987 struct kvec rsp_iov;
2988 int resp_buftype;
2989 int rc = 0;
2990 int flags = 0;
2991 unsigned int total_len;
2992
2993 cifs_dbg(FYI, "Flush\n");
2994
2995 if (!ses || !(ses->server))
2996 return -EIO;
2997
2998 rc = smb2_plain_req_init(SMB2_FLUSH, tcon, (void **) &req, &total_len);
2999 if (rc)
3000 return rc;
3001
3002 if (smb3_encryption_required(tcon))
3003 flags |= CIFS_TRANSFORM_REQ;
3004
3005 req->PersistentFileId = persistent_fid;
3006 req->VolatileFileId = volatile_fid;
3007
3008 iov[0].iov_base = (char *)req;
3009 iov[0].iov_len = total_len;
3010
3011 memset(&rqst, 0, sizeof(struct smb_rqst));
3012 rqst.rq_iov = iov;
3013 rqst.rq_nvec = 1;
3014
3015 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3016 cifs_small_buf_release(req);
3017
3018 if (rc != 0) {
3019 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
3020 trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
3021 rc);
3022 }
3023
3024 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3025 return rc;
3026}
3027
3028/*
3029 * To form a chain of read requests, any read requests after the first should
3030 * have the end_of_chain boolean set to true.
3031 */
3032static int
3033smb2_new_read_req(void **buf, unsigned int *total_len,
3034 struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
3035 unsigned int remaining_bytes, int request_type)
3036{
3037 int rc = -EACCES;
3038 struct smb2_read_plain_req *req = NULL;
3039 struct smb2_sync_hdr *shdr;
3040 struct TCP_Server_Info *server;
3041
3042 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
3043 total_len);
3044 if (rc)
3045 return rc;
3046
3047 server = io_parms->tcon->ses->server;
3048 if (server == NULL)
3049 return -ECONNABORTED;
3050
3051 shdr = &req->sync_hdr;
3052 shdr->ProcessId = cpu_to_le32(io_parms->pid);
3053
3054 req->PersistentFileId = io_parms->persistent_fid;
3055 req->VolatileFileId = io_parms->volatile_fid;
3056 req->ReadChannelInfoOffset = 0; /* reserved */
3057 req->ReadChannelInfoLength = 0; /* reserved */
3058 req->Channel = 0; /* reserved */
3059 req->MinimumCount = 0;
3060 req->Length = cpu_to_le32(io_parms->length);
3061 req->Offset = cpu_to_le64(io_parms->offset);
3062#ifdef CONFIG_CIFS_SMB_DIRECT
3063 /*
3064 * If we want to do a RDMA write, fill in and append
3065 * smbd_buffer_descriptor_v1 to the end of read request
3066 */
3067 if (server->rdma && rdata && !server->sign &&
3068 rdata->bytes >= server->smbd_conn->rdma_readwrite_threshold) {
3069
3070 struct smbd_buffer_descriptor_v1 *v1;
3071 bool need_invalidate =
3072 io_parms->tcon->ses->server->dialect == SMB30_PROT_ID;
3073
3074 rdata->mr = smbd_register_mr(
3075 server->smbd_conn, rdata->pages,
3076 rdata->nr_pages, rdata->page_offset,
3077 rdata->tailsz, true, need_invalidate);
3078 if (!rdata->mr)
3079 return -ENOBUFS;
3080
3081 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3082 if (need_invalidate)
3083 req->Channel = SMB2_CHANNEL_RDMA_V1;
3084 req->ReadChannelInfoOffset =
3085 cpu_to_le16(offsetof(struct smb2_read_plain_req, Buffer));
3086 req->ReadChannelInfoLength =
3087 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3088 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3089 v1->offset = cpu_to_le64(rdata->mr->mr->iova);
3090 v1->token = cpu_to_le32(rdata->mr->mr->rkey);
3091 v1->length = cpu_to_le32(rdata->mr->mr->length);
3092
3093 *total_len += sizeof(*v1) - 1;
3094 }
3095#endif
3096 if (request_type & CHAINED_REQUEST) {
3097 if (!(request_type & END_OF_CHAIN)) {
3098 /* next 8-byte aligned request */
3099 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
3100 shdr->NextCommand = cpu_to_le32(*total_len);
3101 } else /* END_OF_CHAIN */
3102 shdr->NextCommand = 0;
3103 if (request_type & RELATED_REQUEST) {
3104 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
3105 /*
3106 * Related requests use info from previous read request
3107 * in chain.
3108 */
3109 shdr->SessionId = 0xFFFFFFFF;
3110 shdr->TreeId = 0xFFFFFFFF;
3111 req->PersistentFileId = 0xFFFFFFFF;
3112 req->VolatileFileId = 0xFFFFFFFF;
3113 }
3114 }
3115 if (remaining_bytes > io_parms->length)
3116 req->RemainingBytes = cpu_to_le32(remaining_bytes);
3117 else
3118 req->RemainingBytes = 0;
3119
3120 *buf = req;
3121 return rc;
3122}
3123
3124static void
3125smb2_readv_callback(struct mid_q_entry *mid)
3126{
3127 struct cifs_readdata *rdata = mid->callback_data;
3128 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
3129 struct TCP_Server_Info *server = tcon->ses->server;
3130 struct smb2_sync_hdr *shdr =
3131 (struct smb2_sync_hdr *)rdata->iov[0].iov_base;
3132 unsigned int credits_received = 0;
3133 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3134 .rq_nvec = 2,
3135 .rq_pages = rdata->pages,
3136 .rq_offset = rdata->page_offset,
3137 .rq_npages = rdata->nr_pages,
3138 .rq_pagesz = rdata->pagesz,
3139 .rq_tailsz = rdata->tailsz };
3140
3141 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
3142 __func__, mid->mid, mid->mid_state, rdata->result,
3143 rdata->bytes);
3144
3145 switch (mid->mid_state) {
3146 case MID_RESPONSE_RECEIVED:
3147 credits_received = le16_to_cpu(shdr->CreditRequest);
3148 /* result already set, check signature */
3149 if (server->sign && !mid->decrypted) {
3150 int rc;
3151
3152 rc = smb2_verify_signature(&rqst, server);
3153 if (rc)
3154 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
3155 rc);
3156 }
3157 /* FIXME: should this be counted toward the initiating task? */
3158 task_io_account_read(rdata->got_bytes);
3159 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3160 break;
3161 case MID_REQUEST_SUBMITTED:
3162 case MID_RETRY_NEEDED:
3163 rdata->result = -EAGAIN;
3164 if (server->sign && rdata->got_bytes)
3165 /* reset bytes number since we can not check a sign */
3166 rdata->got_bytes = 0;
3167 /* FIXME: should this be counted toward the initiating task? */
3168 task_io_account_read(rdata->got_bytes);
3169 cifs_stats_bytes_read(tcon, rdata->got_bytes);
3170 break;
3171 case MID_RESPONSE_MALFORMED:
3172 credits_received = le16_to_cpu(shdr->CreditRequest);
3173 /* fall through */
3174 default:
3175 if (rdata->result != -ENODATA)
3176 rdata->result = -EIO;
3177 }
3178#ifdef CONFIG_CIFS_SMB_DIRECT
3179 /*
3180 * If this rdata has a memmory registered, the MR can be freed
3181 * MR needs to be freed as soon as I/O finishes to prevent deadlock
3182 * because they have limited number and are used for future I/Os
3183 */
3184 if (rdata->mr) {
3185 smbd_deregister_mr(rdata->mr);
3186 rdata->mr = NULL;
3187 }
3188#endif
3189 if (rdata->result && rdata->result != -ENODATA) {
3190 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
3191 trace_smb3_read_err(0 /* xid */,
3192 rdata->cfile->fid.persistent_fid,
3193 tcon->tid, tcon->ses->Suid, rdata->offset,
3194 rdata->bytes, rdata->result);
3195 } else
3196 trace_smb3_read_done(0 /* xid */,
3197 rdata->cfile->fid.persistent_fid,
3198 tcon->tid, tcon->ses->Suid,
3199 rdata->offset, rdata->got_bytes);
3200
3201 queue_work(cifsiod_wq, &rdata->work);
3202 DeleteMidQEntry(mid);
3203 add_credits(server, credits_received, 0);
3204}
3205
3206/* smb2_async_readv - send an async read, and set up mid to handle result */
3207int
3208smb2_async_readv(struct cifs_readdata *rdata)
3209{
3210 int rc, flags = 0;
3211 char *buf;
3212 struct smb2_sync_hdr *shdr;
3213 struct cifs_io_parms io_parms;
3214 struct smb_rqst rqst = { .rq_iov = rdata->iov,
3215 .rq_nvec = 1 };
3216 struct TCP_Server_Info *server;
3217 unsigned int total_len;
3218
3219 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
3220 __func__, rdata->offset, rdata->bytes);
3221
3222 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
3223 io_parms.offset = rdata->offset;
3224 io_parms.length = rdata->bytes;
3225 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
3226 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
3227 io_parms.pid = rdata->pid;
3228
3229 server = io_parms.tcon->ses->server;
3230
3231 rc = smb2_new_read_req(
3232 (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
3233 if (rc) {
3234 if (rc == -EAGAIN && rdata->credits) {
3235 /* credits was reset by reconnect */
3236 rdata->credits = 0;
3237 /* reduce in_flight value since we won't send the req */
3238 spin_lock(&server->req_lock);
3239 server->in_flight--;
3240 spin_unlock(&server->req_lock);
3241 }
3242 return rc;
3243 }
3244
3245 if (smb3_encryption_required(io_parms.tcon))
3246 flags |= CIFS_TRANSFORM_REQ;
3247
3248 rdata->iov[0].iov_base = buf;
3249 rdata->iov[0].iov_len = total_len;
3250
3251 shdr = (struct smb2_sync_hdr *)buf;
3252
3253 if (rdata->credits) {
3254 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
3255 SMB2_MAX_BUFFER_SIZE));
3256 shdr->CreditRequest =
3257 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3258 spin_lock(&server->req_lock);
3259 server->credits += rdata->credits -
3260 le16_to_cpu(shdr->CreditCharge);
3261 spin_unlock(&server->req_lock);
3262 wake_up(&server->request_q);
3263 rdata->credits = le16_to_cpu(shdr->CreditCharge);
3264 flags |= CIFS_HAS_CREDITS;
3265 }
3266
3267 kref_get(&rdata->refcount);
3268 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
3269 cifs_readv_receive, smb2_readv_callback,
3270 smb3_handle_read_data, rdata, flags);
3271 if (rc) {
3272 kref_put(&rdata->refcount, cifs_readdata_release);
3273 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
3274 trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
3275 io_parms.tcon->tid,
3276 io_parms.tcon->ses->Suid,
3277 io_parms.offset, io_parms.length, rc);
3278 }
3279
3280 cifs_small_buf_release(buf);
3281 return rc;
3282}
3283
3284int
3285SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
3286 unsigned int *nbytes, char **buf, int *buf_type)
3287{
3288 struct smb_rqst rqst;
3289 int resp_buftype, rc = -EACCES;
3290 struct smb2_read_plain_req *req = NULL;
3291 struct smb2_read_rsp *rsp = NULL;
3292 struct kvec iov[1];
3293 struct kvec rsp_iov;
3294 unsigned int total_len;
3295 int flags = CIFS_LOG_ERROR;
3296 struct cifs_ses *ses = io_parms->tcon->ses;
3297
3298 *nbytes = 0;
3299 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
3300 if (rc)
3301 return rc;
3302
3303 if (smb3_encryption_required(io_parms->tcon))
3304 flags |= CIFS_TRANSFORM_REQ;
3305
3306 iov[0].iov_base = (char *)req;
3307 iov[0].iov_len = total_len;
3308
3309 memset(&rqst, 0, sizeof(struct smb_rqst));
3310 rqst.rq_iov = iov;
3311 rqst.rq_nvec = 1;
3312
3313 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3314 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
3315
3316 if (rc) {
3317 if (rc != -ENODATA) {
3318 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
3319 cifs_dbg(VFS, "Send error in read = %d\n", rc);
3320 trace_smb3_read_err(xid, req->PersistentFileId,
3321 io_parms->tcon->tid, ses->Suid,
3322 io_parms->offset, io_parms->length,
3323 rc);
3324 }
3325 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3326 cifs_small_buf_release(req);
3327 return rc == -ENODATA ? 0 : rc;
3328 } else
3329 trace_smb3_read_done(xid, req->PersistentFileId,
3330 io_parms->tcon->tid, ses->Suid,
3331 io_parms->offset, io_parms->length);
3332
3333 cifs_small_buf_release(req);
3334
3335 *nbytes = le32_to_cpu(rsp->DataLength);
3336 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
3337 (*nbytes > io_parms->length)) {
3338 cifs_dbg(FYI, "bad length %d for count %d\n",
3339 *nbytes, io_parms->length);
3340 rc = -EIO;
3341 *nbytes = 0;
3342 }
3343
3344 if (*buf) {
3345 memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
3346 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3347 } else if (resp_buftype != CIFS_NO_BUFFER) {
3348 *buf = rsp_iov.iov_base;
3349 if (resp_buftype == CIFS_SMALL_BUFFER)
3350 *buf_type = CIFS_SMALL_BUFFER;
3351 else if (resp_buftype == CIFS_LARGE_BUFFER)
3352 *buf_type = CIFS_LARGE_BUFFER;
3353 }
3354 return rc;
3355}
3356
3357/*
3358 * Check the mid_state and signature on received buffer (if any), and queue the
3359 * workqueue completion task.
3360 */
3361static void
3362smb2_writev_callback(struct mid_q_entry *mid)
3363{
3364 struct cifs_writedata *wdata = mid->callback_data;
3365 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3366 unsigned int written;
3367 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
3368 unsigned int credits_received = 0;
3369
3370 switch (mid->mid_state) {
3371 case MID_RESPONSE_RECEIVED:
3372 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3373 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
3374 if (wdata->result != 0)
3375 break;
3376
3377 written = le32_to_cpu(rsp->DataLength);
3378 /*
3379 * Mask off high 16 bits when bytes written as returned
3380 * by the server is greater than bytes requested by the
3381 * client. OS/2 servers are known to set incorrect
3382 * CountHigh values.
3383 */
3384 if (written > wdata->bytes)
3385 written &= 0xFFFF;
3386
3387 if (written < wdata->bytes)
3388 wdata->result = -ENOSPC;
3389 else
3390 wdata->bytes = written;
3391 break;
3392 case MID_REQUEST_SUBMITTED:
3393 case MID_RETRY_NEEDED:
3394 wdata->result = -EAGAIN;
3395 break;
3396 case MID_RESPONSE_MALFORMED:
3397 credits_received = le16_to_cpu(rsp->sync_hdr.CreditRequest);
3398 /* fall through */
3399 default:
3400 wdata->result = -EIO;
3401 break;
3402 }
3403#ifdef CONFIG_CIFS_SMB_DIRECT
3404 /*
3405 * If this wdata has a memory registered, the MR can be freed
3406 * The number of MRs available is limited, it's important to recover
3407 * used MR as soon as I/O is finished. Hold MR longer in the later
3408 * I/O process can possibly result in I/O deadlock due to lack of MR
3409 * to send request on I/O retry
3410 */
3411 if (wdata->mr) {
3412 smbd_deregister_mr(wdata->mr);
3413 wdata->mr = NULL;
3414 }
3415#endif
3416 if (wdata->result) {
3417 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3418 trace_smb3_write_err(0 /* no xid */,
3419 wdata->cfile->fid.persistent_fid,
3420 tcon->tid, tcon->ses->Suid, wdata->offset,
3421 wdata->bytes, wdata->result);
3422 } else
3423 trace_smb3_write_done(0 /* no xid */,
3424 wdata->cfile->fid.persistent_fid,
3425 tcon->tid, tcon->ses->Suid,
3426 wdata->offset, wdata->bytes);
3427
3428 queue_work(cifsiod_wq, &wdata->work);
3429 DeleteMidQEntry(mid);
3430 add_credits(tcon->ses->server, credits_received, 0);
3431}
3432
3433/* smb2_async_writev - send an async write, and set up mid to handle result */
3434int
3435smb2_async_writev(struct cifs_writedata *wdata,
3436 void (*release)(struct kref *kref))
3437{
3438 int rc = -EACCES, flags = 0;
3439 struct smb2_write_req *req = NULL;
3440 struct smb2_sync_hdr *shdr;
3441 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
3442 struct TCP_Server_Info *server = tcon->ses->server;
3443 struct kvec iov[1];
3444 struct smb_rqst rqst = { };
3445 unsigned int total_len;
3446
3447 rc = smb2_plain_req_init(SMB2_WRITE, tcon, (void **) &req, &total_len);
3448 if (rc) {
3449 if (rc == -EAGAIN && wdata->credits) {
3450 /* credits was reset by reconnect */
3451 wdata->credits = 0;
3452 /* reduce in_flight value since we won't send the req */
3453 spin_lock(&server->req_lock);
3454 server->in_flight--;
3455 spin_unlock(&server->req_lock);
3456 }
3457 goto async_writev_out;
3458 }
3459
3460 if (smb3_encryption_required(tcon))
3461 flags |= CIFS_TRANSFORM_REQ;
3462
3463 shdr = (struct smb2_sync_hdr *)req;
3464 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
3465
3466 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
3467 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
3468 req->WriteChannelInfoOffset = 0;
3469 req->WriteChannelInfoLength = 0;
3470 req->Channel = 0;
3471 req->Offset = cpu_to_le64(wdata->offset);
3472 req->DataOffset = cpu_to_le16(
3473 offsetof(struct smb2_write_req, Buffer));
3474 req->RemainingBytes = 0;
3475#ifdef CONFIG_CIFS_SMB_DIRECT
3476 /*
3477 * If we want to do a server RDMA read, fill in and append
3478 * smbd_buffer_descriptor_v1 to the end of write request
3479 */
3480 if (server->rdma && !server->sign && wdata->bytes >=
3481 server->smbd_conn->rdma_readwrite_threshold) {
3482
3483 struct smbd_buffer_descriptor_v1 *v1;
3484 bool need_invalidate = server->dialect == SMB30_PROT_ID;
3485
3486 wdata->mr = smbd_register_mr(
3487 server->smbd_conn, wdata->pages,
3488 wdata->nr_pages, wdata->page_offset,
3489 wdata->tailsz, false, need_invalidate);
3490 if (!wdata->mr) {
3491 rc = -ENOBUFS;
3492 goto async_writev_out;
3493 }
3494 req->Length = 0;
3495 req->DataOffset = 0;
3496 if (wdata->nr_pages > 1)
3497 req->RemainingBytes =
3498 cpu_to_le32(
3499 (wdata->nr_pages - 1) * wdata->pagesz -
3500 wdata->page_offset + wdata->tailsz
3501 );
3502 else
3503 req->RemainingBytes = cpu_to_le32(wdata->tailsz);
3504 req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
3505 if (need_invalidate)
3506 req->Channel = SMB2_CHANNEL_RDMA_V1;
3507 req->WriteChannelInfoOffset =
3508 cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
3509 req->WriteChannelInfoLength =
3510 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
3511 v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
3512 v1->offset = cpu_to_le64(wdata->mr->mr->iova);
3513 v1->token = cpu_to_le32(wdata->mr->mr->rkey);
3514 v1->length = cpu_to_le32(wdata->mr->mr->length);
3515 }
3516#endif
3517 iov[0].iov_len = total_len - 1;
3518 iov[0].iov_base = (char *)req;
3519
3520 rqst.rq_iov = iov;
3521 rqst.rq_nvec = 1;
3522 rqst.rq_pages = wdata->pages;
3523 rqst.rq_offset = wdata->page_offset;
3524 rqst.rq_npages = wdata->nr_pages;
3525 rqst.rq_pagesz = wdata->pagesz;
3526 rqst.rq_tailsz = wdata->tailsz;
3527#ifdef CONFIG_CIFS_SMB_DIRECT
3528 if (wdata->mr) {
3529 iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
3530 rqst.rq_npages = 0;
3531 }
3532#endif
3533 cifs_dbg(FYI, "async write at %llu %u bytes\n",
3534 wdata->offset, wdata->bytes);
3535
3536#ifdef CONFIG_CIFS_SMB_DIRECT
3537 /* For RDMA read, I/O size is in RemainingBytes not in Length */
3538 if (!wdata->mr)
3539 req->Length = cpu_to_le32(wdata->bytes);
3540#else
3541 req->Length = cpu_to_le32(wdata->bytes);
3542#endif
3543
3544 if (wdata->credits) {
3545 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
3546 SMB2_MAX_BUFFER_SIZE));
3547 shdr->CreditRequest =
3548 cpu_to_le16(le16_to_cpu(shdr->CreditCharge) + 1);
3549 spin_lock(&server->req_lock);
3550 server->credits += wdata->credits -
3551 le16_to_cpu(shdr->CreditCharge);
3552 spin_unlock(&server->req_lock);
3553 wake_up(&server->request_q);
3554 wdata->credits = le16_to_cpu(shdr->CreditCharge);
3555 flags |= CIFS_HAS_CREDITS;
3556 }
3557
3558 kref_get(&wdata->refcount);
3559 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
3560 wdata, flags);
3561
3562 if (rc) {
3563 trace_smb3_write_err(0 /* no xid */, req->PersistentFileId,
3564 tcon->tid, tcon->ses->Suid, wdata->offset,
3565 wdata->bytes, rc);
3566 kref_put(&wdata->refcount, release);
3567 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
3568 }
3569
3570async_writev_out:
3571 cifs_small_buf_release(req);
3572 return rc;
3573}
3574
3575/*
3576 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
3577 * The length field from io_parms must be at least 1 and indicates a number of
3578 * elements with data to write that begins with position 1 in iov array. All
3579 * data length is specified by count.
3580 */
3581int
3582SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
3583 unsigned int *nbytes, struct kvec *iov, int n_vec)
3584{
3585 struct smb_rqst rqst;
3586 int rc = 0;
3587 struct smb2_write_req *req = NULL;
3588 struct smb2_write_rsp *rsp = NULL;
3589 int resp_buftype;
3590 struct kvec rsp_iov;
3591 int flags = 0;
3592 unsigned int total_len;
3593
3594 *nbytes = 0;
3595
3596 if (n_vec < 1)
3597 return rc;
3598
3599 rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, (void **) &req,
3600 &total_len);
3601 if (rc)
3602 return rc;
3603
3604 if (io_parms->tcon->ses->server == NULL)
3605 return -ECONNABORTED;
3606
3607 if (smb3_encryption_required(io_parms->tcon))
3608 flags |= CIFS_TRANSFORM_REQ;
3609
3610 req->sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
3611
3612 req->PersistentFileId = io_parms->persistent_fid;
3613 req->VolatileFileId = io_parms->volatile_fid;
3614 req->WriteChannelInfoOffset = 0;
3615 req->WriteChannelInfoLength = 0;
3616 req->Channel = 0;
3617 req->Length = cpu_to_le32(io_parms->length);
3618 req->Offset = cpu_to_le64(io_parms->offset);
3619 req->DataOffset = cpu_to_le16(
3620 offsetof(struct smb2_write_req, Buffer));
3621 req->RemainingBytes = 0;
3622
3623 iov[0].iov_base = (char *)req;
3624 /* 1 for Buffer */
3625 iov[0].iov_len = total_len - 1;
3626
3627 memset(&rqst, 0, sizeof(struct smb_rqst));
3628 rqst.rq_iov = iov;
3629 rqst.rq_nvec = n_vec + 1;
3630
3631 rc = cifs_send_recv(xid, io_parms->tcon->ses, &rqst,
3632 &resp_buftype, flags, &rsp_iov);
3633 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
3634
3635 if (rc) {
3636 trace_smb3_write_err(xid, req->PersistentFileId,
3637 io_parms->tcon->tid,
3638 io_parms->tcon->ses->Suid,
3639 io_parms->offset, io_parms->length, rc);
3640 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
3641 cifs_dbg(VFS, "Send error in write = %d\n", rc);
3642 } else {
3643 *nbytes = le32_to_cpu(rsp->DataLength);
3644 trace_smb3_write_done(xid, req->PersistentFileId,
3645 io_parms->tcon->tid,
3646 io_parms->tcon->ses->Suid,
3647 io_parms->offset, *nbytes);
3648 }
3649
3650 cifs_small_buf_release(req);
3651 free_rsp_buf(resp_buftype, rsp);
3652 return rc;
3653}
3654
3655static unsigned int
3656num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
3657{
3658 int len;
3659 unsigned int entrycount = 0;
3660 unsigned int next_offset = 0;
3661 char *entryptr;
3662 FILE_DIRECTORY_INFO *dir_info;
3663
3664 if (bufstart == NULL)
3665 return 0;
3666
3667 entryptr = bufstart;
3668
3669 while (1) {
3670 if (entryptr + next_offset < entryptr ||
3671 entryptr + next_offset > end_of_buf ||
3672 entryptr + next_offset + size > end_of_buf) {
3673 cifs_dbg(VFS, "malformed search entry would overflow\n");
3674 break;
3675 }
3676
3677 entryptr = entryptr + next_offset;
3678 dir_info = (FILE_DIRECTORY_INFO *)entryptr;
3679
3680 len = le32_to_cpu(dir_info->FileNameLength);
3681 if (entryptr + len < entryptr ||
3682 entryptr + len > end_of_buf ||
3683 entryptr + len + size > end_of_buf) {
3684 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
3685 end_of_buf);
3686 break;
3687 }
3688
3689 *lastentry = entryptr;
3690 entrycount++;
3691
3692 next_offset = le32_to_cpu(dir_info->NextEntryOffset);
3693 if (!next_offset)
3694 break;
3695 }
3696
3697 return entrycount;
3698}
3699
3700/*
3701 * Readdir/FindFirst
3702 */
3703int
3704SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
3705 u64 persistent_fid, u64 volatile_fid, int index,
3706 struct cifs_search_info *srch_inf)
3707{
3708 struct smb_rqst rqst;
3709 struct smb2_query_directory_req *req;
3710 struct smb2_query_directory_rsp *rsp = NULL;
3711 struct kvec iov[2];
3712 struct kvec rsp_iov;
3713 int rc = 0;
3714 int len;
3715 int resp_buftype = CIFS_NO_BUFFER;
3716 unsigned char *bufptr;
3717 struct TCP_Server_Info *server;
3718 struct cifs_ses *ses = tcon->ses;
3719 __le16 asteriks = cpu_to_le16('*');
3720 char *end_of_smb;
3721 unsigned int output_size = CIFSMaxBufSize;
3722 size_t info_buf_size;
3723 int flags = 0;
3724 unsigned int total_len;
3725
3726 if (ses && (ses->server))
3727 server = ses->server;
3728 else
3729 return -EIO;
3730
3731 rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req,
3732 &total_len);
3733 if (rc)
3734 return rc;
3735
3736 if (smb3_encryption_required(tcon))
3737 flags |= CIFS_TRANSFORM_REQ;
3738
3739 switch (srch_inf->info_level) {
3740 case SMB_FIND_FILE_DIRECTORY_INFO:
3741 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
3742 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
3743 break;
3744 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
3745 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
3746 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
3747 break;
3748 default:
3749 cifs_dbg(VFS, "info level %u isn't supported\n",
3750 srch_inf->info_level);
3751 rc = -EINVAL;
3752 goto qdir_exit;
3753 }
3754
3755 req->FileIndex = cpu_to_le32(index);
3756 req->PersistentFileId = persistent_fid;
3757 req->VolatileFileId = volatile_fid;
3758
3759 len = 0x2;
3760 bufptr = req->Buffer;
3761 memcpy(bufptr, &asteriks, len);
3762
3763 req->FileNameOffset =
3764 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
3765 req->FileNameLength = cpu_to_le16(len);
3766 /*
3767 * BB could be 30 bytes or so longer if we used SMB2 specific
3768 * buffer lengths, but this is safe and close enough.
3769 */
3770 output_size = min_t(unsigned int, output_size, server->maxBuf);
3771 output_size = min_t(unsigned int, output_size, 2 << 15);
3772 req->OutputBufferLength = cpu_to_le32(output_size);
3773
3774 iov[0].iov_base = (char *)req;
3775 /* 1 for Buffer */
3776 iov[0].iov_len = total_len - 1;
3777
3778 iov[1].iov_base = (char *)(req->Buffer);
3779 iov[1].iov_len = len;
3780
3781 memset(&rqst, 0, sizeof(struct smb_rqst));
3782 rqst.rq_iov = iov;
3783 rqst.rq_nvec = 2;
3784
3785 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
3786 cifs_small_buf_release(req);
3787 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3788
3789 if (rc) {
3790 if (rc == -ENODATA &&
3791 rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
3792 srch_inf->endOfSearch = true;
3793 rc = 0;
3794 } else
3795 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3796 goto qdir_exit;
3797 }
3798
3799 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
3800 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
3801 info_buf_size);
3802 if (rc)
3803 goto qdir_exit;
3804
3805 srch_inf->unicode = true;
3806
3807 if (srch_inf->ntwrk_buf_start) {
3808 if (srch_inf->smallBuf)
3809 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3810 else
3811 cifs_buf_release(srch_inf->ntwrk_buf_start);
3812 }
3813 srch_inf->ntwrk_buf_start = (char *)rsp;
3814 srch_inf->srch_entries_start = srch_inf->last_entry =
3815 (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
3816 end_of_smb = rsp_iov.iov_len + (char *)rsp;
3817 srch_inf->entries_in_buffer =
3818 num_entries(srch_inf->srch_entries_start, end_of_smb,
3819 &srch_inf->last_entry, info_buf_size);
3820 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3821 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3822 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3823 srch_inf->srch_entries_start, srch_inf->last_entry);
3824 if (resp_buftype == CIFS_LARGE_BUFFER)
3825 srch_inf->smallBuf = false;
3826 else if (resp_buftype == CIFS_SMALL_BUFFER)
3827 srch_inf->smallBuf = true;
3828 else
3829 cifs_dbg(VFS, "illegal search buffer type\n");
3830
3831 return rc;
3832
3833qdir_exit:
3834 free_rsp_buf(resp_buftype, rsp);
3835 return rc;
3836}
3837
3838static int
3839send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3840 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3841 u8 info_type, u32 additional_info, unsigned int num,
3842 void **data, unsigned int *size)
3843{
3844 struct smb_rqst rqst;
3845 struct smb2_set_info_req *req;
3846 struct smb2_set_info_rsp *rsp = NULL;
3847 struct kvec *iov;
3848 struct kvec rsp_iov;
3849 int rc = 0;
3850 int resp_buftype;
3851 unsigned int i;
3852 struct cifs_ses *ses = tcon->ses;
3853 int flags = 0;
3854 unsigned int total_len;
3855
3856 if (!ses || !(ses->server))
3857 return -EIO;
3858
3859 if (!num)
3860 return -EINVAL;
3861
3862 iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
3863 if (!iov)
3864 return -ENOMEM;
3865
3866 rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, (void **) &req, &total_len);
3867 if (rc) {
3868 kfree(iov);
3869 return rc;
3870 }
3871
3872 if (smb3_encryption_required(tcon))
3873 flags |= CIFS_TRANSFORM_REQ;
3874
3875 req->sync_hdr.ProcessId = cpu_to_le32(pid);
3876
3877 req->InfoType = info_type;
3878 req->FileInfoClass = info_class;
3879 req->PersistentFileId = persistent_fid;
3880 req->VolatileFileId = volatile_fid;
3881 req->AdditionalInformation = cpu_to_le32(additional_info);
3882
3883 req->BufferOffset =
3884 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
3885 req->BufferLength = cpu_to_le32(*size);
3886
3887 memcpy(req->Buffer, *data, *size);
3888 total_len += *size;
3889
3890 iov[0].iov_base = (char *)req;
3891 /* 1 for Buffer */
3892 iov[0].iov_len = total_len - 1;
3893
3894 for (i = 1; i < num; i++) {
3895 le32_add_cpu(&req->BufferLength, size[i]);
3896 iov[i].iov_base = (char *)data[i];
3897 iov[i].iov_len = size[i];
3898 }
3899
3900 memset(&rqst, 0, sizeof(struct smb_rqst));
3901 rqst.rq_iov = iov;
3902 rqst.rq_nvec = num;
3903
3904 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags,
3905 &rsp_iov);
3906 cifs_buf_release(req);
3907 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3908
3909 if (rc != 0) {
3910 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3911 trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
3912 ses->Suid, info_class, (__u32)info_type, rc);
3913 }
3914
3915 free_rsp_buf(resp_buftype, rsp);
3916 kfree(iov);
3917 return rc;
3918}
3919
3920int
3921SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3922 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3923{
3924 struct smb2_file_rename_info info;
3925 void **data;
3926 unsigned int size[2];
3927 int rc;
3928 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3929
3930 data = kmalloc_array(2, sizeof(void *), GFP_KERNEL);
3931 if (!data)
3932 return -ENOMEM;
3933
3934 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3935 /* 0 = fail if target already exists */
3936 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3937 info.FileNameLength = cpu_to_le32(len);
3938
3939 data[0] = &info;
3940 size[0] = sizeof(struct smb2_file_rename_info);
3941
3942 data[1] = target_file;
3943 size[1] = len + 2 /* null */;
3944
3945 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3946 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3947 0, 2, data, size);
3948 kfree(data);
3949 return rc;
3950}
3951
3952int
3953SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3954 u64 persistent_fid, u64 volatile_fid)
3955{
3956 __u8 delete_pending = 1;
3957 void *data;
3958 unsigned int size;
3959
3960 data = &delete_pending;
3961 size = 1; /* sizeof __u8 */
3962
3963 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3964 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3965 0, 1, &data, &size);
3966}
3967
3968int
3969SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3970 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3971{
3972 struct smb2_file_link_info info;
3973 void **data;
3974 unsigned int size[2];
3975 int rc;
3976 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3977
3978 data = kmalloc_array(2, sizeof(void *), GFP_KERNEL);
3979 if (!data)
3980 return -ENOMEM;
3981
3982 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3983 /* 0 = fail if link already exists */
3984 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3985 info.FileNameLength = cpu_to_le32(len);
3986
3987 data[0] = &info;
3988 size[0] = sizeof(struct smb2_file_link_info);
3989
3990 data[1] = target_file;
3991 size[1] = len + 2 /* null */;
3992
3993 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3994 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3995 0, 2, data, size);
3996 kfree(data);
3997 return rc;
3998}
3999
4000int
4001SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
4002 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
4003{
4004 struct smb2_file_eof_info info;
4005 void *data;
4006 unsigned int size;
4007
4008 info.EndOfFile = *eof;
4009
4010 data = &info;
4011 size = sizeof(struct smb2_file_eof_info);
4012
4013 if (is_falloc)
4014 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4015 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
4016 0, 1, &data, &size);
4017 else
4018 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4019 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
4020 0, 1, &data, &size);
4021}
4022
4023int
4024SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
4025 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
4026{
4027 unsigned int size;
4028 size = sizeof(FILE_BASIC_INFO);
4029 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4030 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
4031 0, 1, (void **)&buf, &size);
4032}
4033
4034int
4035SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
4036 u64 persistent_fid, u64 volatile_fid,
4037 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
4038{
4039 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4040 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
4041 1, (void **)&pnntsd, &pacllen);
4042}
4043
4044int
4045SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
4046 u64 persistent_fid, u64 volatile_fid,
4047 struct smb2_file_full_ea_info *buf, int len)
4048{
4049 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
4050 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
4051 0, 1, (void **)&buf, &len);
4052}
4053
4054int
4055SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
4056 const u64 persistent_fid, const u64 volatile_fid,
4057 __u8 oplock_level)
4058{
4059 struct smb_rqst rqst;
4060 int rc;
4061 struct smb2_oplock_break *req = NULL;
4062 struct cifs_ses *ses = tcon->ses;
4063 int flags = CIFS_OBREAK_OP;
4064 unsigned int total_len;
4065 struct kvec iov[1];
4066 struct kvec rsp_iov;
4067 int resp_buf_type;
4068
4069 cifs_dbg(FYI, "SMB2_oplock_break\n");
4070 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4071 &total_len);
4072 if (rc)
4073 return rc;
4074
4075 if (smb3_encryption_required(tcon))
4076 flags |= CIFS_TRANSFORM_REQ;
4077
4078 req->VolatileFid = volatile_fid;
4079 req->PersistentFid = persistent_fid;
4080 req->OplockLevel = oplock_level;
4081 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4082
4083 flags |= CIFS_NO_RESP;
4084
4085 iov[0].iov_base = (char *)req;
4086 iov[0].iov_len = total_len;
4087
4088 memset(&rqst, 0, sizeof(struct smb_rqst));
4089 rqst.rq_iov = iov;
4090 rqst.rq_nvec = 1;
4091
4092 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4093 cifs_small_buf_release(req);
4094
4095 if (rc) {
4096 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4097 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
4098 }
4099
4100 return rc;
4101}
4102
4103void
4104smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
4105 struct kstatfs *kst)
4106{
4107 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
4108 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
4109 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
4110 kst->f_bfree = kst->f_bavail =
4111 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
4112 return;
4113}
4114
4115static void
4116copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
4117 struct kstatfs *kst)
4118{
4119 kst->f_bsize = le32_to_cpu(response_data->BlockSize);
4120 kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
4121 kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
4122 if (response_data->UserBlocksAvail == cpu_to_le64(-1))
4123 kst->f_bavail = kst->f_bfree;
4124 else
4125 kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
4126 if (response_data->TotalFileNodes != cpu_to_le64(-1))
4127 kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
4128 if (response_data->FreeFileNodes != cpu_to_le64(-1))
4129 kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
4130
4131 return;
4132}
4133
4134static int
4135build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
4136 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
4137{
4138 struct TCP_Server_Info *server;
4139 int rc;
4140 struct smb2_query_info_req *req;
4141 unsigned int total_len;
4142
4143 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
4144
4145 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
4146 return -EIO;
4147
4148 server = tcon->ses->server;
4149
4150 rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, (void **) &req,
4151 &total_len);
4152 if (rc)
4153 return rc;
4154
4155 req->InfoType = SMB2_O_INFO_FILESYSTEM;
4156 req->FileInfoClass = level;
4157 req->PersistentFileId = persistent_fid;
4158 req->VolatileFileId = volatile_fid;
4159 /* 1 for pad */
4160 req->InputBufferOffset =
4161 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
4162 req->OutputBufferLength = cpu_to_le32(
4163 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
4164
4165 iov->iov_base = (char *)req;
4166 iov->iov_len = total_len;
4167 return 0;
4168}
4169
4170int
4171SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
4172 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4173{
4174 struct smb_rqst rqst;
4175 struct smb2_query_info_rsp *rsp = NULL;
4176 struct kvec iov;
4177 struct kvec rsp_iov;
4178 int rc = 0;
4179 int resp_buftype;
4180 struct cifs_ses *ses = tcon->ses;
4181 FILE_SYSTEM_POSIX_INFO *info = NULL;
4182 int flags = 0;
4183
4184 rc = build_qfs_info_req(&iov, tcon, FS_POSIX_INFORMATION,
4185 sizeof(FILE_SYSTEM_POSIX_INFO),
4186 persistent_fid, volatile_fid);
4187 if (rc)
4188 return rc;
4189
4190 if (smb3_encryption_required(tcon))
4191 flags |= CIFS_TRANSFORM_REQ;
4192
4193 memset(&rqst, 0, sizeof(struct smb_rqst));
4194 rqst.rq_iov = &iov;
4195 rqst.rq_nvec = 1;
4196
4197 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4198 cifs_small_buf_release(iov.iov_base);
4199 if (rc) {
4200 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4201 goto posix_qfsinf_exit;
4202 }
4203 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4204
4205 info = (FILE_SYSTEM_POSIX_INFO *)(
4206 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4207 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4208 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4209 sizeof(FILE_SYSTEM_POSIX_INFO));
4210 if (!rc)
4211 copy_posix_fs_info_to_kstatfs(info, fsdata);
4212
4213posix_qfsinf_exit:
4214 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4215 return rc;
4216}
4217
4218int
4219SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
4220 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
4221{
4222 struct smb_rqst rqst;
4223 struct smb2_query_info_rsp *rsp = NULL;
4224 struct kvec iov;
4225 struct kvec rsp_iov;
4226 int rc = 0;
4227 int resp_buftype;
4228 struct cifs_ses *ses = tcon->ses;
4229 struct smb2_fs_full_size_info *info = NULL;
4230 int flags = 0;
4231
4232 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
4233 sizeof(struct smb2_fs_full_size_info),
4234 persistent_fid, volatile_fid);
4235 if (rc)
4236 return rc;
4237
4238 if (smb3_encryption_required(tcon))
4239 flags |= CIFS_TRANSFORM_REQ;
4240
4241 memset(&rqst, 0, sizeof(struct smb_rqst));
4242 rqst.rq_iov = &iov;
4243 rqst.rq_nvec = 1;
4244
4245 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4246 cifs_small_buf_release(iov.iov_base);
4247 if (rc) {
4248 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4249 goto qfsinf_exit;
4250 }
4251 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4252
4253 info = (struct smb2_fs_full_size_info *)(
4254 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
4255 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
4256 le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
4257 sizeof(struct smb2_fs_full_size_info));
4258 if (!rc)
4259 smb2_copy_fs_info_to_kstatfs(info, fsdata);
4260
4261qfsinf_exit:
4262 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4263 return rc;
4264}
4265
4266int
4267SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
4268 u64 persistent_fid, u64 volatile_fid, int level)
4269{
4270 struct smb_rqst rqst;
4271 struct smb2_query_info_rsp *rsp = NULL;
4272 struct kvec iov;
4273 struct kvec rsp_iov;
4274 int rc = 0;
4275 int resp_buftype, max_len, min_len;
4276 struct cifs_ses *ses = tcon->ses;
4277 unsigned int rsp_len, offset;
4278 int flags = 0;
4279
4280 if (level == FS_DEVICE_INFORMATION) {
4281 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4282 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
4283 } else if (level == FS_ATTRIBUTE_INFORMATION) {
4284 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
4285 min_len = MIN_FS_ATTR_INFO_SIZE;
4286 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
4287 max_len = sizeof(struct smb3_fs_ss_info);
4288 min_len = sizeof(struct smb3_fs_ss_info);
4289 } else if (level == FS_VOLUME_INFORMATION) {
4290 max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
4291 min_len = sizeof(struct smb3_fs_vol_info);
4292 } else {
4293 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
4294 return -EINVAL;
4295 }
4296
4297 rc = build_qfs_info_req(&iov, tcon, level, max_len,
4298 persistent_fid, volatile_fid);
4299 if (rc)
4300 return rc;
4301
4302 if (smb3_encryption_required(tcon))
4303 flags |= CIFS_TRANSFORM_REQ;
4304
4305 memset(&rqst, 0, sizeof(struct smb_rqst));
4306 rqst.rq_iov = &iov;
4307 rqst.rq_nvec = 1;
4308
4309 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
4310 cifs_small_buf_release(iov.iov_base);
4311 if (rc) {
4312 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
4313 goto qfsattr_exit;
4314 }
4315 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
4316
4317 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
4318 offset = le16_to_cpu(rsp->OutputBufferOffset);
4319 rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
4320 if (rc)
4321 goto qfsattr_exit;
4322
4323 if (level == FS_ATTRIBUTE_INFORMATION)
4324 memcpy(&tcon->fsAttrInfo, offset
4325 + (char *)rsp, min_t(unsigned int,
4326 rsp_len, max_len));
4327 else if (level == FS_DEVICE_INFORMATION)
4328 memcpy(&tcon->fsDevInfo, offset
4329 + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
4330 else if (level == FS_SECTOR_SIZE_INFORMATION) {
4331 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
4332 (offset + (char *)rsp);
4333 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
4334 tcon->perf_sector_size =
4335 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
4336 } else if (level == FS_VOLUME_INFORMATION) {
4337 struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
4338 (offset + (char *)rsp);
4339 tcon->vol_serial_number = vol_info->VolumeSerialNumber;
4340 tcon->vol_create_time = vol_info->VolumeCreationTime;
4341 }
4342
4343qfsattr_exit:
4344 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
4345 return rc;
4346}
4347
4348int
4349smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
4350 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4351 const __u32 num_lock, struct smb2_lock_element *buf)
4352{
4353 struct smb_rqst rqst;
4354 int rc = 0;
4355 struct smb2_lock_req *req = NULL;
4356 struct kvec iov[2];
4357 struct kvec rsp_iov;
4358 int resp_buf_type;
4359 unsigned int count;
4360 int flags = CIFS_NO_RESP;
4361 unsigned int total_len;
4362
4363 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
4364
4365 rc = smb2_plain_req_init(SMB2_LOCK, tcon, (void **) &req, &total_len);
4366 if (rc)
4367 return rc;
4368
4369 if (smb3_encryption_required(tcon))
4370 flags |= CIFS_TRANSFORM_REQ;
4371
4372 req->sync_hdr.ProcessId = cpu_to_le32(pid);
4373 req->LockCount = cpu_to_le16(num_lock);
4374
4375 req->PersistentFileId = persist_fid;
4376 req->VolatileFileId = volatile_fid;
4377
4378 count = num_lock * sizeof(struct smb2_lock_element);
4379
4380 iov[0].iov_base = (char *)req;
4381 iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
4382 iov[1].iov_base = (char *)buf;
4383 iov[1].iov_len = count;
4384
4385 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
4386
4387 memset(&rqst, 0, sizeof(struct smb_rqst));
4388 rqst.rq_iov = iov;
4389 rqst.rq_nvec = 2;
4390
4391 rc = cifs_send_recv(xid, tcon->ses, &rqst, &resp_buf_type, flags,
4392 &rsp_iov);
4393 cifs_small_buf_release(req);
4394 if (rc) {
4395 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
4396 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
4397 trace_smb3_lock_err(xid, persist_fid, tcon->tid,
4398 tcon->ses->Suid, rc);
4399 }
4400
4401 return rc;
4402}
4403
4404int
4405SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
4406 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
4407 const __u64 length, const __u64 offset, const __u32 lock_flags,
4408 const bool wait)
4409{
4410 struct smb2_lock_element lock;
4411
4412 lock.Offset = cpu_to_le64(offset);
4413 lock.Length = cpu_to_le64(length);
4414 lock.Flags = cpu_to_le32(lock_flags);
4415 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
4416 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
4417
4418 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
4419}
4420
4421int
4422SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
4423 __u8 *lease_key, const __le32 lease_state)
4424{
4425 struct smb_rqst rqst;
4426 int rc;
4427 struct smb2_lease_ack *req = NULL;
4428 struct cifs_ses *ses = tcon->ses;
4429 int flags = CIFS_OBREAK_OP;
4430 unsigned int total_len;
4431 struct kvec iov[1];
4432 struct kvec rsp_iov;
4433 int resp_buf_type;
4434
4435 cifs_dbg(FYI, "SMB2_lease_break\n");
4436 rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req,
4437 &total_len);
4438 if (rc)
4439 return rc;
4440
4441 if (smb3_encryption_required(tcon))
4442 flags |= CIFS_TRANSFORM_REQ;
4443
4444 req->sync_hdr.CreditRequest = cpu_to_le16(1);
4445 req->StructureSize = cpu_to_le16(36);
4446 total_len += 12;
4447
4448 memcpy(req->LeaseKey, lease_key, 16);
4449 req->LeaseState = lease_state;
4450
4451 flags |= CIFS_NO_RESP;
4452
4453 iov[0].iov_base = (char *)req;
4454 iov[0].iov_len = total_len;
4455
4456 memset(&rqst, 0, sizeof(struct smb_rqst));
4457 rqst.rq_iov = iov;
4458 rqst.rq_nvec = 1;
4459
4460 rc = cifs_send_recv(xid, ses, &rqst, &resp_buf_type, flags, &rsp_iov);
4461 cifs_small_buf_release(req);
4462
4463 if (rc) {
4464 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
4465 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
4466 }
4467
4468 return rc;
4469}