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