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