1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2009, 2013
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
9 * Contains the routines for constructing the SMB2 PDUs themselves
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 */
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>
28 #include "cifsproto.h"
29 #include "smb2proto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
33 #include "smb2status.h"
36 #include "cifs_spnego.h"
37 #include "smbdirect.h"
39 #ifdef CONFIG_CIFS_DFS_UPCALL
40 #include "dfs_cache.h"
42 #include "cached_dir.h"
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.
48 * Note that commands are defined in smb2pdu.h in le16 but the array below is
49 * indexed by command in host byte order.
51 static const int smb2_req_struct_sizes
[NUMBER_OF_SMB2_COMMANDS
] = {
52 /* SMB2_NEGOTIATE */ 36,
53 /* SMB2_SESSION_SETUP */ 25,
55 /* SMB2_TREE_CONNECT */ 9,
56 /* SMB2_TREE_DISCONNECT */ 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 */
73 int smb3_encryption_required(const struct cifs_tcon
*tcon
)
75 if (!tcon
|| !tcon
->ses
)
77 if ((tcon
->ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) ||
78 (tcon
->share_flags
& SHI1005_FLAGS_ENCRYPT_DATA
))
81 (tcon
->ses
->server
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
))
87 smb2_hdr_assemble(struct smb2_hdr
*shdr
, __le16 smb2_cmd
,
88 const struct cifs_tcon
*tcon
,
89 struct TCP_Server_Info
*server
)
91 struct smb3_hdr_req
*smb3_hdr
;
93 shdr
->ProtocolId
= SMB2_PROTO_NUMBER
;
94 shdr
->StructureSize
= cpu_to_le16(64);
95 shdr
->Command
= smb2_cmd
;
98 /* After reconnect SMB3 must set ChannelSequence on subsequent reqs */
99 if (server
->dialect
>= SMB30_PROT_ID
) {
100 smb3_hdr
= (struct smb3_hdr_req
*)shdr
;
102 * if primary channel is not set yet, use default
103 * channel for chan sequence num
105 if (SERVER_IS_CHAN(server
))
106 smb3_hdr
->ChannelSequence
=
107 cpu_to_le16(server
->primary_server
->channel_sequence_num
);
109 smb3_hdr
->ChannelSequence
=
110 cpu_to_le16(server
->channel_sequence_num
);
112 spin_lock(&server
->req_lock
);
113 /* Request up to 10 credits but don't go over the limit. */
114 if (server
->credits
>= server
->max_credits
)
115 shdr
->CreditRequest
= cpu_to_le16(0);
117 shdr
->CreditRequest
= cpu_to_le16(
118 min_t(int, server
->max_credits
-
119 server
->credits
, 10));
120 spin_unlock(&server
->req_lock
);
122 shdr
->CreditRequest
= cpu_to_le16(2);
124 shdr
->Id
.SyncId
.ProcessId
= cpu_to_le32((__u16
)current
->tgid
);
129 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
130 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
131 if (server
&& (server
->capabilities
& SMB2_GLOBAL_CAP_LARGE_MTU
))
132 shdr
->CreditCharge
= cpu_to_le16(1);
133 /* else CreditCharge MBZ */
135 shdr
->Id
.SyncId
.TreeId
= cpu_to_le32(tcon
->tid
);
136 /* Uid is not converted */
138 shdr
->SessionId
= cpu_to_le64(tcon
->ses
->Suid
);
141 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
142 * to pass the path on the Open SMB prefixed by \\server\share.
143 * Not sure when we would need to do the augmented path (if ever) and
144 * setting this flag breaks the SMB2 open operation since it is
145 * illegal to send an empty path name (without \\server\share prefix)
146 * when the DFS flag is set in the SMB open header. We could
147 * consider setting the flag on all operations other than open
148 * but it is safer to net set it for now.
150 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
151 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
153 if (server
&& server
->sign
&& !smb3_encryption_required(tcon
))
154 shdr
->Flags
|= SMB2_FLAGS_SIGNED
;
160 smb2_reconnect(__le16 smb2_command
, struct cifs_tcon
*tcon
,
161 struct TCP_Server_Info
*server
, bool from_reconnect
)
164 struct nls_table
*nls_codepage
= NULL
;
165 struct cifs_ses
*ses
;
167 struct TCP_Server_Info
*pserver
;
168 unsigned int chan_index
;
171 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
172 * check for tcp and smb session status done differently
173 * for those three - in the calling routine.
179 * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
180 * cifs_tree_connect().
182 if (smb2_command
== SMB2_TREE_CONNECT
|| smb2_command
== SMB2_IOCTL
)
185 spin_lock(&tcon
->tc_lock
);
186 if (tcon
->status
== TID_EXITING
) {
188 * only tree disconnect allowed when disconnecting ...
190 if (smb2_command
!= SMB2_TREE_DISCONNECT
) {
191 spin_unlock(&tcon
->tc_lock
);
192 cifs_dbg(FYI
, "can not send cmd %d while umounting\n",
197 spin_unlock(&tcon
->tc_lock
);
202 spin_lock(&ses
->ses_lock
);
203 if (ses
->ses_status
== SES_EXITING
) {
204 spin_unlock(&ses
->ses_lock
);
207 spin_unlock(&ses
->ses_lock
);
208 if (!ses
->server
|| !server
)
211 spin_lock(&server
->srv_lock
);
212 if (server
->tcpStatus
== CifsNeedReconnect
) {
214 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
215 * here since they are implicitly done when session drops.
217 switch (smb2_command
) {
219 * BB Should we keep oplock break and add flush to exceptions?
221 case SMB2_TREE_DISCONNECT
:
224 case SMB2_OPLOCK_BREAK
:
225 spin_unlock(&server
->srv_lock
);
230 /* if server is marked for termination, cifsd will cleanup */
231 if (server
->terminate
) {
232 spin_unlock(&server
->srv_lock
);
235 spin_unlock(&server
->srv_lock
);
238 rc
= cifs_wait_for_server_reconnect(server
, tcon
->retry
);
242 spin_lock(&ses
->chan_lock
);
243 if (!cifs_chan_needs_reconnect(ses
, server
) && !tcon
->need_reconnect
) {
244 spin_unlock(&ses
->chan_lock
);
247 spin_unlock(&ses
->chan_lock
);
248 cifs_dbg(FYI
, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
249 tcon
->ses
->chans_need_reconnect
,
250 tcon
->need_reconnect
);
252 mutex_lock(&ses
->session_mutex
);
254 * if this is called by delayed work, and the channel has been disabled
255 * in parallel, the delayed work can continue to execute in parallel
256 * there's a chance that this channel may not exist anymore
258 spin_lock(&server
->srv_lock
);
259 if (server
->tcpStatus
== CifsExiting
) {
260 spin_unlock(&server
->srv_lock
);
261 mutex_unlock(&ses
->session_mutex
);
267 * Recheck after acquire mutex. If another thread is negotiating
268 * and the server never sends an answer the socket will be closed
269 * and tcpStatus set to reconnect.
271 if (server
->tcpStatus
== CifsNeedReconnect
) {
272 spin_unlock(&server
->srv_lock
);
273 mutex_unlock(&ses
->session_mutex
);
281 spin_unlock(&server
->srv_lock
);
283 nls_codepage
= ses
->local_nls
;
286 * need to prevent multiple threads trying to simultaneously
287 * reconnect the same SMB session
289 spin_lock(&ses
->ses_lock
);
290 spin_lock(&ses
->chan_lock
);
291 if (!cifs_chan_needs_reconnect(ses
, server
) &&
292 ses
->ses_status
== SES_GOOD
) {
293 spin_unlock(&ses
->chan_lock
);
294 spin_unlock(&ses
->ses_lock
);
295 /* this means that we only need to tree connect */
296 if (tcon
->need_reconnect
)
297 goto skip_sess_setup
;
299 mutex_unlock(&ses
->session_mutex
);
302 spin_unlock(&ses
->chan_lock
);
303 spin_unlock(&ses
->ses_lock
);
305 rc
= cifs_negotiate_protocol(0, ses
, server
);
308 * if server stopped supporting multichannel
309 * and the first channel reconnected, disable all the others.
311 if (ses
->chan_count
> 1 &&
312 !(server
->capabilities
& SMB2_GLOBAL_CAP_MULTI_CHANNEL
)) {
313 if (SERVER_IS_CHAN(server
)) {
314 cifs_dbg(VFS
, "server %s does not support " \
315 "multichannel anymore. skipping secondary channel\n",
316 ses
->server
->hostname
);
318 spin_lock(&ses
->chan_lock
);
319 chan_index
= cifs_ses_get_chan_index(ses
, server
);
320 if (chan_index
== CIFS_INVAL_CHAN_INDEX
) {
321 spin_unlock(&ses
->chan_lock
);
325 ses
->chans
[chan_index
].server
= NULL
;
326 spin_unlock(&ses
->chan_lock
);
329 * the above reference of server by channel
330 * needs to be dropped without holding chan_lock
331 * as cifs_put_tcp_session takes a higher lock
332 * i.e. cifs_tcp_ses_lock
334 cifs_put_tcp_session(server
, from_reconnect
);
336 server
->terminate
= true;
337 cifs_signal_cifsd_for_reconnect(server
, false);
339 /* mark primary server as needing reconnect */
340 pserver
= server
->primary_server
;
341 cifs_signal_cifsd_for_reconnect(pserver
, false);
344 mutex_unlock(&ses
->session_mutex
);
348 cifs_server_dbg(VFS
, "does not support " \
349 "multichannel anymore. disabling all other channels\n");
350 cifs_disable_secondary_channels(ses
);
354 rc
= cifs_setup_session(0, ses
, server
, nls_codepage
);
355 if ((rc
== -EACCES
) && !tcon
->retry
) {
356 mutex_unlock(&ses
->session_mutex
);
360 mutex_unlock(&ses
->session_mutex
);
364 mutex_unlock(&ses
->session_mutex
);
369 if (!tcon
->need_reconnect
) {
370 mutex_unlock(&ses
->session_mutex
);
373 cifs_mark_open_files_invalid(tcon
);
374 if (tcon
->use_persistent
)
375 tcon
->need_reopen_files
= true;
377 rc
= cifs_tree_connect(0, tcon
, nls_codepage
);
379 cifs_dbg(FYI
, "reconnect tcon rc = %d\n", rc
);
381 /* If sess reconnected but tcon didn't, something strange ... */
382 mutex_unlock(&ses
->session_mutex
);
383 cifs_dbg(VFS
, "reconnect tcon failed rc = %d\n", rc
);
388 (server
->capabilities
& SMB2_GLOBAL_CAP_MULTI_CHANNEL
)) {
389 mutex_unlock(&ses
->session_mutex
);
392 * query server network interfaces, in case they change
395 rc
= SMB3_request_interfaces(xid
, tcon
, false);
399 cifs_dbg(FYI
, "%s: failed to query server interfaces: %d\n",
402 if (ses
->chan_max
> ses
->chan_count
&&
403 !SERVER_IS_CHAN(server
)) {
404 if (ses
->chan_count
== 1)
405 cifs_server_dbg(VFS
, "supports multichannel now\n");
407 cifs_try_adding_channels(ses
);
410 mutex_unlock(&ses
->session_mutex
);
413 if (smb2_command
!= SMB2_INTERNAL_CMD
)
414 if (mod_delayed_work(cifsiod_wq
, &server
->reconnect
, 0))
415 cifs_put_tcp_session(server
, false);
417 atomic_inc(&tconInfoReconnectCount
);
420 * Check if handle based operation so we know whether we can continue
421 * or not without returning to caller to reset file handle.
424 * BB Is flush done by server on drop of tcp session? Should we special
425 * case it and skip above?
427 switch (smb2_command
) {
432 case SMB2_QUERY_DIRECTORY
:
433 case SMB2_CHANGE_NOTIFY
:
434 case SMB2_QUERY_INFO
:
443 fill_small_buf(__le16 smb2_command
, struct cifs_tcon
*tcon
,
444 struct TCP_Server_Info
*server
,
446 unsigned int *total_len
)
448 struct smb2_pdu
*spdu
= buf
;
449 /* lookup word count ie StructureSize from table */
450 __u16 parmsize
= smb2_req_struct_sizes
[le16_to_cpu(smb2_command
)];
453 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
454 * largest operations (Create)
458 smb2_hdr_assemble(&spdu
->hdr
, smb2_command
, tcon
, server
);
459 spdu
->StructureSize2
= cpu_to_le16(parmsize
);
461 *total_len
= parmsize
+ sizeof(struct smb2_hdr
);
465 * Allocate and return pointer to an SMB request hdr, and set basic
466 * SMB information in the SMB header. If the return code is zero, this
467 * function must have filled in request_buf pointer.
469 static int __smb2_plain_req_init(__le16 smb2_command
, struct cifs_tcon
*tcon
,
470 struct TCP_Server_Info
*server
,
471 void **request_buf
, unsigned int *total_len
)
473 /* BB eventually switch this to SMB2 specific small buf size */
474 if (smb2_command
== SMB2_SET_INFO
)
475 *request_buf
= cifs_buf_get();
477 *request_buf
= cifs_small_buf_get();
478 if (*request_buf
== NULL
) {
479 /* BB should we add a retry in here if not a writepage? */
483 fill_small_buf(smb2_command
, tcon
, server
,
484 (struct smb2_hdr
*)(*request_buf
),
488 uint16_t com_code
= le16_to_cpu(smb2_command
);
489 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_sent
[com_code
]);
490 cifs_stats_inc(&tcon
->num_smbs_sent
);
496 static int smb2_plain_req_init(__le16 smb2_command
, struct cifs_tcon
*tcon
,
497 struct TCP_Server_Info
*server
,
498 void **request_buf
, unsigned int *total_len
)
502 rc
= smb2_reconnect(smb2_command
, tcon
, server
, false);
506 return __smb2_plain_req_init(smb2_command
, tcon
, server
, request_buf
,
510 static int smb2_ioctl_req_init(u32 opcode
, struct cifs_tcon
*tcon
,
511 struct TCP_Server_Info
*server
,
512 void **request_buf
, unsigned int *total_len
)
514 /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
515 if (opcode
== FSCTL_VALIDATE_NEGOTIATE_INFO
) {
516 return __smb2_plain_req_init(SMB2_IOCTL
, tcon
, server
,
517 request_buf
, total_len
);
519 return smb2_plain_req_init(SMB2_IOCTL
, tcon
, server
,
520 request_buf
, total_len
);
523 /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
526 build_preauth_ctxt(struct smb2_preauth_neg_context
*pneg_ctxt
)
528 pneg_ctxt
->ContextType
= SMB2_PREAUTH_INTEGRITY_CAPABILITIES
;
529 pneg_ctxt
->DataLength
= cpu_to_le16(38);
530 pneg_ctxt
->HashAlgorithmCount
= cpu_to_le16(1);
531 pneg_ctxt
->SaltLength
= cpu_to_le16(SMB311_SALT_SIZE
);
532 get_random_bytes(pneg_ctxt
->Salt
, SMB311_SALT_SIZE
);
533 pneg_ctxt
->HashAlgorithms
= SMB2_PREAUTH_INTEGRITY_SHA512
;
537 build_compression_ctxt(struct smb2_compression_capabilities_context
*pneg_ctxt
)
539 pneg_ctxt
->ContextType
= SMB2_COMPRESSION_CAPABILITIES
;
540 pneg_ctxt
->DataLength
=
541 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context
)
542 - sizeof(struct smb2_neg_context
));
543 pneg_ctxt
->CompressionAlgorithmCount
= cpu_to_le16(3);
544 pneg_ctxt
->CompressionAlgorithms
[0] = SMB3_COMPRESS_LZ77
;
545 pneg_ctxt
->CompressionAlgorithms
[1] = SMB3_COMPRESS_LZ77_HUFF
;
546 pneg_ctxt
->CompressionAlgorithms
[2] = SMB3_COMPRESS_LZNT1
;
550 build_signing_ctxt(struct smb2_signing_capabilities
*pneg_ctxt
)
552 unsigned int ctxt_len
= sizeof(struct smb2_signing_capabilities
);
553 unsigned short num_algs
= 1; /* number of signing algorithms sent */
555 pneg_ctxt
->ContextType
= SMB2_SIGNING_CAPABILITIES
;
557 * Context Data length must be rounded to multiple of 8 for some servers
559 pneg_ctxt
->DataLength
= cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities
) -
560 sizeof(struct smb2_neg_context
) +
561 (num_algs
* sizeof(u16
)), 8));
562 pneg_ctxt
->SigningAlgorithmCount
= cpu_to_le16(num_algs
);
563 pneg_ctxt
->SigningAlgorithms
[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC
);
565 ctxt_len
+= sizeof(__le16
) * num_algs
;
566 ctxt_len
= ALIGN(ctxt_len
, 8);
568 /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
572 build_encrypt_ctxt(struct smb2_encryption_neg_context
*pneg_ctxt
)
574 pneg_ctxt
->ContextType
= SMB2_ENCRYPTION_CAPABILITIES
;
575 if (require_gcm_256
) {
576 pneg_ctxt
->DataLength
= cpu_to_le16(4); /* Cipher Count + 1 cipher */
577 pneg_ctxt
->CipherCount
= cpu_to_le16(1);
578 pneg_ctxt
->Ciphers
[0] = SMB2_ENCRYPTION_AES256_GCM
;
579 } else if (enable_gcm_256
) {
580 pneg_ctxt
->DataLength
= cpu_to_le16(8); /* Cipher Count + 3 ciphers */
581 pneg_ctxt
->CipherCount
= cpu_to_le16(3);
582 pneg_ctxt
->Ciphers
[0] = SMB2_ENCRYPTION_AES128_GCM
;
583 pneg_ctxt
->Ciphers
[1] = SMB2_ENCRYPTION_AES256_GCM
;
584 pneg_ctxt
->Ciphers
[2] = SMB2_ENCRYPTION_AES128_CCM
;
586 pneg_ctxt
->DataLength
= cpu_to_le16(6); /* Cipher Count + 2 ciphers */
587 pneg_ctxt
->CipherCount
= cpu_to_le16(2);
588 pneg_ctxt
->Ciphers
[0] = SMB2_ENCRYPTION_AES128_GCM
;
589 pneg_ctxt
->Ciphers
[1] = SMB2_ENCRYPTION_AES128_CCM
;
594 build_netname_ctxt(struct smb2_netname_neg_context
*pneg_ctxt
, char *hostname
)
596 struct nls_table
*cp
= load_nls_default();
598 pneg_ctxt
->ContextType
= SMB2_NETNAME_NEGOTIATE_CONTEXT_ID
;
600 /* copy up to max of first 100 bytes of server name to NetName field */
601 pneg_ctxt
->DataLength
= cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt
->NetName
, hostname
, 100, cp
));
602 /* context size is DataLength + minimal smb2_neg_context */
603 return ALIGN(le16_to_cpu(pneg_ctxt
->DataLength
) + sizeof(struct smb2_neg_context
), 8);
607 build_posix_ctxt(struct smb2_posix_neg_context
*pneg_ctxt
)
609 pneg_ctxt
->ContextType
= SMB2_POSIX_EXTENSIONS_AVAILABLE
;
610 pneg_ctxt
->DataLength
= cpu_to_le16(POSIX_CTXT_DATA_LEN
);
611 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
612 pneg_ctxt
->Name
[0] = 0x93;
613 pneg_ctxt
->Name
[1] = 0xAD;
614 pneg_ctxt
->Name
[2] = 0x25;
615 pneg_ctxt
->Name
[3] = 0x50;
616 pneg_ctxt
->Name
[4] = 0x9C;
617 pneg_ctxt
->Name
[5] = 0xB4;
618 pneg_ctxt
->Name
[6] = 0x11;
619 pneg_ctxt
->Name
[7] = 0xE7;
620 pneg_ctxt
->Name
[8] = 0xB4;
621 pneg_ctxt
->Name
[9] = 0x23;
622 pneg_ctxt
->Name
[10] = 0x83;
623 pneg_ctxt
->Name
[11] = 0xDE;
624 pneg_ctxt
->Name
[12] = 0x96;
625 pneg_ctxt
->Name
[13] = 0x8B;
626 pneg_ctxt
->Name
[14] = 0xCD;
627 pneg_ctxt
->Name
[15] = 0x7C;
631 assemble_neg_contexts(struct smb2_negotiate_req
*req
,
632 struct TCP_Server_Info
*server
, unsigned int *total_len
)
634 unsigned int ctxt_len
, neg_context_count
;
635 struct TCP_Server_Info
*pserver
;
639 if (*total_len
> 200) {
640 /* In case length corrupted don't want to overrun smb buffer */
641 cifs_server_dbg(VFS
, "Bad frame length assembling neg contexts\n");
646 * round up total_len of fixed part of SMB3 negotiate request to 8
647 * byte boundary before adding negotiate contexts
649 *total_len
= ALIGN(*total_len
, 8);
651 pneg_ctxt
= (*total_len
) + (char *)req
;
652 req
->NegotiateContextOffset
= cpu_to_le32(*total_len
);
654 build_preauth_ctxt((struct smb2_preauth_neg_context
*)pneg_ctxt
);
655 ctxt_len
= ALIGN(sizeof(struct smb2_preauth_neg_context
), 8);
656 *total_len
+= ctxt_len
;
657 pneg_ctxt
+= ctxt_len
;
659 build_encrypt_ctxt((struct smb2_encryption_neg_context
*)pneg_ctxt
);
660 ctxt_len
= ALIGN(sizeof(struct smb2_encryption_neg_context
), 8);
661 *total_len
+= ctxt_len
;
662 pneg_ctxt
+= ctxt_len
;
665 * secondary channels don't have the hostname field populated
666 * use the hostname field in the primary channel instead
668 pserver
= SERVER_IS_CHAN(server
) ? server
->primary_server
: server
;
669 cifs_server_lock(pserver
);
670 hostname
= pserver
->hostname
;
671 if (hostname
&& (hostname
[0] != 0)) {
672 ctxt_len
= build_netname_ctxt((struct smb2_netname_neg_context
*)pneg_ctxt
,
674 *total_len
+= ctxt_len
;
675 pneg_ctxt
+= ctxt_len
;
676 neg_context_count
= 3;
678 neg_context_count
= 2;
679 cifs_server_unlock(pserver
);
681 build_posix_ctxt((struct smb2_posix_neg_context
*)pneg_ctxt
);
682 *total_len
+= sizeof(struct smb2_posix_neg_context
);
683 pneg_ctxt
+= sizeof(struct smb2_posix_neg_context
);
686 if (server
->compress_algorithm
) {
687 build_compression_ctxt((struct smb2_compression_capabilities_context
*)
689 ctxt_len
= ALIGN(sizeof(struct smb2_compression_capabilities_context
), 8);
690 *total_len
+= ctxt_len
;
691 pneg_ctxt
+= ctxt_len
;
695 if (enable_negotiate_signing
) {
696 ctxt_len
= build_signing_ctxt((struct smb2_signing_capabilities
*)
698 *total_len
+= ctxt_len
;
699 pneg_ctxt
+= ctxt_len
;
703 /* check for and add transport_capabilities and signing capabilities */
704 req
->NegotiateContextCount
= cpu_to_le16(neg_context_count
);
708 /* If invalid preauth context warn but use what we requested, SHA-512 */
709 static void decode_preauth_context(struct smb2_preauth_neg_context
*ctxt
)
711 unsigned int len
= le16_to_cpu(ctxt
->DataLength
);
714 * Caller checked that DataLength remains within SMB boundary. We still
715 * need to confirm that one HashAlgorithms member is accounted for.
717 if (len
< MIN_PREAUTH_CTXT_DATA_LEN
) {
718 pr_warn_once("server sent bad preauth context\n");
720 } else if (len
< MIN_PREAUTH_CTXT_DATA_LEN
+ le16_to_cpu(ctxt
->SaltLength
)) {
721 pr_warn_once("server sent invalid SaltLength\n");
724 if (le16_to_cpu(ctxt
->HashAlgorithmCount
) != 1)
725 pr_warn_once("Invalid SMB3 hash algorithm count\n");
726 if (ctxt
->HashAlgorithms
!= SMB2_PREAUTH_INTEGRITY_SHA512
)
727 pr_warn_once("unknown SMB3 hash algorithm\n");
730 static void decode_compress_ctx(struct TCP_Server_Info
*server
,
731 struct smb2_compression_capabilities_context
*ctxt
)
733 unsigned int len
= le16_to_cpu(ctxt
->DataLength
);
736 * Caller checked that DataLength remains within SMB boundary. We still
737 * need to confirm that one CompressionAlgorithms member is accounted
741 pr_warn_once("server sent bad compression cntxt\n");
744 if (le16_to_cpu(ctxt
->CompressionAlgorithmCount
) != 1) {
745 pr_warn_once("Invalid SMB3 compress algorithm count\n");
748 if (le16_to_cpu(ctxt
->CompressionAlgorithms
[0]) > 3) {
749 pr_warn_once("unknown compression algorithm\n");
752 server
->compress_algorithm
= ctxt
->CompressionAlgorithms
[0];
755 static int decode_encrypt_ctx(struct TCP_Server_Info
*server
,
756 struct smb2_encryption_neg_context
*ctxt
)
758 unsigned int len
= le16_to_cpu(ctxt
->DataLength
);
760 cifs_dbg(FYI
, "decode SMB3.11 encryption neg context of len %d\n", len
);
762 * Caller checked that DataLength remains within SMB boundary. We still
763 * need to confirm that one Cipher flexible array member is accounted
766 if (len
< MIN_ENCRYPT_CTXT_DATA_LEN
) {
767 pr_warn_once("server sent bad crypto ctxt len\n");
771 if (le16_to_cpu(ctxt
->CipherCount
) != 1) {
772 pr_warn_once("Invalid SMB3.11 cipher count\n");
775 cifs_dbg(FYI
, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt
->Ciphers
[0]));
776 if (require_gcm_256
) {
777 if (ctxt
->Ciphers
[0] != SMB2_ENCRYPTION_AES256_GCM
) {
778 cifs_dbg(VFS
, "Server does not support requested encryption type (AES256 GCM)\n");
781 } else if (ctxt
->Ciphers
[0] == 0) {
783 * e.g. if server only supported AES256_CCM (very unlikely)
784 * or server supported no encryption types or had all disabled.
785 * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
786 * in which mount requested encryption ("seal") checks later
787 * on during tree connection will return proper rc, but if
788 * seal not requested by client, since server is allowed to
789 * return 0 to indicate no supported cipher, we can't fail here
791 server
->cipher_type
= 0;
792 server
->capabilities
&= ~SMB2_GLOBAL_CAP_ENCRYPTION
;
793 pr_warn_once("Server does not support requested encryption types\n");
795 } else if ((ctxt
->Ciphers
[0] != SMB2_ENCRYPTION_AES128_CCM
) &&
796 (ctxt
->Ciphers
[0] != SMB2_ENCRYPTION_AES128_GCM
) &&
797 (ctxt
->Ciphers
[0] != SMB2_ENCRYPTION_AES256_GCM
)) {
798 /* server returned a cipher we didn't ask for */
799 pr_warn_once("Invalid SMB3.11 cipher returned\n");
802 server
->cipher_type
= ctxt
->Ciphers
[0];
803 server
->capabilities
|= SMB2_GLOBAL_CAP_ENCRYPTION
;
807 static void decode_signing_ctx(struct TCP_Server_Info
*server
,
808 struct smb2_signing_capabilities
*pctxt
)
810 unsigned int len
= le16_to_cpu(pctxt
->DataLength
);
813 * Caller checked that DataLength remains within SMB boundary. We still
814 * need to confirm that one SigningAlgorithms flexible array member is
817 if ((len
< 4) || (len
> 16)) {
818 pr_warn_once("server sent bad signing negcontext\n");
821 if (le16_to_cpu(pctxt
->SigningAlgorithmCount
) != 1) {
822 pr_warn_once("Invalid signing algorithm count\n");
825 if (le16_to_cpu(pctxt
->SigningAlgorithms
[0]) > 2) {
826 pr_warn_once("unknown signing algorithm\n");
830 server
->signing_negotiated
= true;
831 server
->signing_algorithm
= le16_to_cpu(pctxt
->SigningAlgorithms
[0]);
832 cifs_dbg(FYI
, "signing algorithm %d chosen\n",
833 server
->signing_algorithm
);
837 static int smb311_decode_neg_context(struct smb2_negotiate_rsp
*rsp
,
838 struct TCP_Server_Info
*server
,
839 unsigned int len_of_smb
)
841 struct smb2_neg_context
*pctx
;
842 unsigned int offset
= le32_to_cpu(rsp
->NegotiateContextOffset
);
843 unsigned int ctxt_cnt
= le16_to_cpu(rsp
->NegotiateContextCount
);
844 unsigned int len_of_ctxts
, i
;
847 cifs_dbg(FYI
, "decoding %d negotiate contexts\n", ctxt_cnt
);
848 if (len_of_smb
<= offset
) {
849 cifs_server_dbg(VFS
, "Invalid response: negotiate context offset\n");
853 len_of_ctxts
= len_of_smb
- offset
;
855 for (i
= 0; i
< ctxt_cnt
; i
++) {
857 /* check that offset is not beyond end of SMB */
858 if (len_of_ctxts
< sizeof(struct smb2_neg_context
))
861 pctx
= (struct smb2_neg_context
*)(offset
+ (char *)rsp
);
862 clen
= sizeof(struct smb2_neg_context
)
863 + le16_to_cpu(pctx
->DataLength
);
865 * 2.2.4 SMB2 NEGOTIATE Response
866 * Subsequent negotiate contexts MUST appear at the first 8-byte
867 * aligned offset following the previous negotiate context.
869 if (i
+ 1 != ctxt_cnt
)
870 clen
= ALIGN(clen
, 8);
871 if (clen
> len_of_ctxts
)
874 if (pctx
->ContextType
== SMB2_PREAUTH_INTEGRITY_CAPABILITIES
)
875 decode_preauth_context(
876 (struct smb2_preauth_neg_context
*)pctx
);
877 else if (pctx
->ContextType
== SMB2_ENCRYPTION_CAPABILITIES
)
878 rc
= decode_encrypt_ctx(server
,
879 (struct smb2_encryption_neg_context
*)pctx
);
880 else if (pctx
->ContextType
== SMB2_COMPRESSION_CAPABILITIES
)
881 decode_compress_ctx(server
,
882 (struct smb2_compression_capabilities_context
*)pctx
);
883 else if (pctx
->ContextType
== SMB2_POSIX_EXTENSIONS_AVAILABLE
)
884 server
->posix_ext_supported
= true;
885 else if (pctx
->ContextType
== SMB2_SIGNING_CAPABILITIES
)
886 decode_signing_ctx(server
,
887 (struct smb2_signing_capabilities
*)pctx
);
889 cifs_server_dbg(VFS
, "unknown negcontext of type %d ignored\n",
890 le16_to_cpu(pctx
->ContextType
));
895 len_of_ctxts
-= clen
;
900 static struct create_posix
*
901 create_posix_buf(umode_t mode
)
903 struct create_posix
*buf
;
905 buf
= kzalloc(sizeof(struct create_posix
),
910 buf
->ccontext
.DataOffset
=
911 cpu_to_le16(offsetof(struct create_posix
, Mode
));
912 buf
->ccontext
.DataLength
= cpu_to_le32(4);
913 buf
->ccontext
.NameOffset
=
914 cpu_to_le16(offsetof(struct create_posix
, Name
));
915 buf
->ccontext
.NameLength
= cpu_to_le16(16);
917 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
928 buf
->Name
[10] = 0x83;
929 buf
->Name
[11] = 0xDE;
930 buf
->Name
[12] = 0x96;
931 buf
->Name
[13] = 0x8B;
932 buf
->Name
[14] = 0xCD;
933 buf
->Name
[15] = 0x7C;
934 buf
->Mode
= cpu_to_le32(mode
);
935 cifs_dbg(FYI
, "mode on posix create 0%o\n", mode
);
940 add_posix_context(struct kvec
*iov
, unsigned int *num_iovec
, umode_t mode
)
942 unsigned int num
= *num_iovec
;
944 iov
[num
].iov_base
= create_posix_buf(mode
);
945 if (mode
== ACL_NO_MODE
)
946 cifs_dbg(FYI
, "%s: no mode\n", __func__
);
947 if (iov
[num
].iov_base
== NULL
)
949 iov
[num
].iov_len
= sizeof(struct create_posix
);
950 *num_iovec
= num
+ 1;
957 * SMB2 Worker functions follow:
959 * The general structure of the worker functions is:
960 * 1) Call smb2_init (assembles SMB2 header)
961 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
962 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
963 * 4) Decode SMB2 command specific fields in the fixed length area
964 * 5) Decode variable length data area (if any for this SMB2 command type)
965 * 6) Call free smb buffer
971 SMB2_negotiate(const unsigned int xid
,
972 struct cifs_ses
*ses
,
973 struct TCP_Server_Info
*server
)
975 struct smb_rqst rqst
;
976 struct smb2_negotiate_req
*req
;
977 struct smb2_negotiate_rsp
*rsp
;
982 int blob_offset
, blob_length
;
984 int flags
= CIFS_NEG_OP
;
985 unsigned int total_len
;
987 cifs_dbg(FYI
, "Negotiate protocol\n");
990 WARN(1, "%s: server is NULL!\n", __func__
);
994 rc
= smb2_plain_req_init(SMB2_NEGOTIATE
, NULL
, server
,
995 (void **) &req
, &total_len
);
999 req
->hdr
.SessionId
= 0;
1001 memset(server
->preauth_sha_hash
, 0, SMB2_PREAUTH_HASH_SIZE
);
1002 memset(ses
->preauth_sha_hash
, 0, SMB2_PREAUTH_HASH_SIZE
);
1004 if (strcmp(server
->vals
->version_string
,
1005 SMB3ANY_VERSION_STRING
) == 0) {
1006 req
->Dialects
[0] = cpu_to_le16(SMB30_PROT_ID
);
1007 req
->Dialects
[1] = cpu_to_le16(SMB302_PROT_ID
);
1008 req
->Dialects
[2] = cpu_to_le16(SMB311_PROT_ID
);
1009 req
->DialectCount
= cpu_to_le16(3);
1011 } else if (strcmp(server
->vals
->version_string
,
1012 SMBDEFAULT_VERSION_STRING
) == 0) {
1013 req
->Dialects
[0] = cpu_to_le16(SMB21_PROT_ID
);
1014 req
->Dialects
[1] = cpu_to_le16(SMB30_PROT_ID
);
1015 req
->Dialects
[2] = cpu_to_le16(SMB302_PROT_ID
);
1016 req
->Dialects
[3] = cpu_to_le16(SMB311_PROT_ID
);
1017 req
->DialectCount
= cpu_to_le16(4);
1020 /* otherwise send specific dialect */
1021 req
->Dialects
[0] = cpu_to_le16(server
->vals
->protocol_id
);
1022 req
->DialectCount
= cpu_to_le16(1);
1026 /* only one of SMB2 signing flags may be set in SMB2 request */
1028 req
->SecurityMode
= cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED
);
1029 else if (global_secflags
& CIFSSEC_MAY_SIGN
)
1030 req
->SecurityMode
= cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED
);
1032 req
->SecurityMode
= 0;
1034 req
->Capabilities
= cpu_to_le32(server
->vals
->req_capabilities
);
1035 if (ses
->chan_max
> 1)
1036 req
->Capabilities
|= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL
);
1038 /* ClientGUID must be zero for SMB2.02 dialect */
1039 if (server
->vals
->protocol_id
== SMB20_PROT_ID
)
1040 memset(req
->ClientGUID
, 0, SMB2_CLIENT_GUID_SIZE
);
1042 memcpy(req
->ClientGUID
, server
->client_guid
,
1043 SMB2_CLIENT_GUID_SIZE
);
1044 if ((server
->vals
->protocol_id
== SMB311_PROT_ID
) ||
1045 (strcmp(server
->vals
->version_string
,
1046 SMB3ANY_VERSION_STRING
) == 0) ||
1047 (strcmp(server
->vals
->version_string
,
1048 SMBDEFAULT_VERSION_STRING
) == 0))
1049 assemble_neg_contexts(req
, server
, &total_len
);
1051 iov
[0].iov_base
= (char *)req
;
1052 iov
[0].iov_len
= total_len
;
1054 memset(&rqst
, 0, sizeof(struct smb_rqst
));
1058 rc
= cifs_send_recv(xid
, ses
, server
,
1059 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
1060 cifs_small_buf_release(req
);
1061 rsp
= (struct smb2_negotiate_rsp
*)rsp_iov
.iov_base
;
1063 * No tcon so can't do
1064 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1066 if (rc
== -EOPNOTSUPP
) {
1067 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");
1073 if (strcmp(server
->vals
->version_string
,
1074 SMB3ANY_VERSION_STRING
) == 0) {
1075 if (rsp
->DialectRevision
== cpu_to_le16(SMB20_PROT_ID
)) {
1076 cifs_server_dbg(VFS
,
1077 "SMB2 dialect returned but not requested\n");
1079 } else if (rsp
->DialectRevision
== cpu_to_le16(SMB21_PROT_ID
)) {
1080 cifs_server_dbg(VFS
,
1081 "SMB2.1 dialect returned but not requested\n");
1083 } else if (rsp
->DialectRevision
== cpu_to_le16(SMB311_PROT_ID
)) {
1084 /* ops set to 3.0 by default for default so update */
1085 server
->ops
= &smb311_operations
;
1086 server
->vals
= &smb311_values
;
1088 } else if (strcmp(server
->vals
->version_string
,
1089 SMBDEFAULT_VERSION_STRING
) == 0) {
1090 if (rsp
->DialectRevision
== cpu_to_le16(SMB20_PROT_ID
)) {
1091 cifs_server_dbg(VFS
,
1092 "SMB2 dialect returned but not requested\n");
1094 } else if (rsp
->DialectRevision
== cpu_to_le16(SMB21_PROT_ID
)) {
1095 /* ops set to 3.0 by default for default so update */
1096 server
->ops
= &smb21_operations
;
1097 server
->vals
= &smb21_values
;
1098 } else if (rsp
->DialectRevision
== cpu_to_le16(SMB311_PROT_ID
)) {
1099 server
->ops
= &smb311_operations
;
1100 server
->vals
= &smb311_values
;
1102 } else if (le16_to_cpu(rsp
->DialectRevision
) !=
1103 server
->vals
->protocol_id
) {
1104 /* if requested single dialect ensure returned dialect matched */
1105 cifs_server_dbg(VFS
, "Invalid 0x%x dialect returned: not requested\n",
1106 le16_to_cpu(rsp
->DialectRevision
));
1110 cifs_dbg(FYI
, "mode 0x%x\n", rsp
->SecurityMode
);
1112 if (rsp
->DialectRevision
== cpu_to_le16(SMB20_PROT_ID
))
1113 cifs_dbg(FYI
, "negotiated smb2.0 dialect\n");
1114 else if (rsp
->DialectRevision
== cpu_to_le16(SMB21_PROT_ID
))
1115 cifs_dbg(FYI
, "negotiated smb2.1 dialect\n");
1116 else if (rsp
->DialectRevision
== cpu_to_le16(SMB30_PROT_ID
))
1117 cifs_dbg(FYI
, "negotiated smb3.0 dialect\n");
1118 else if (rsp
->DialectRevision
== cpu_to_le16(SMB302_PROT_ID
))
1119 cifs_dbg(FYI
, "negotiated smb3.02 dialect\n");
1120 else if (rsp
->DialectRevision
== cpu_to_le16(SMB311_PROT_ID
))
1121 cifs_dbg(FYI
, "negotiated smb3.1.1 dialect\n");
1123 cifs_server_dbg(VFS
, "Invalid dialect returned by server 0x%x\n",
1124 le16_to_cpu(rsp
->DialectRevision
));
1129 server
->dialect
= le16_to_cpu(rsp
->DialectRevision
);
1132 * Keep a copy of the hash after negprot. This hash will be
1133 * the starting hash value for all sessions made from this
1136 memcpy(server
->preauth_sha_hash
, ses
->preauth_sha_hash
,
1137 SMB2_PREAUTH_HASH_SIZE
);
1139 /* SMB2 only has an extended negflavor */
1140 server
->negflavor
= CIFS_NEGFLAVOR_EXTENDED
;
1141 /* set it to the maximum buffer size value we can send with 1 credit */
1142 server
->maxBuf
= min_t(unsigned int, le32_to_cpu(rsp
->MaxTransactSize
),
1143 SMB2_MAX_BUFFER_SIZE
);
1144 server
->max_read
= le32_to_cpu(rsp
->MaxReadSize
);
1145 server
->max_write
= le32_to_cpu(rsp
->MaxWriteSize
);
1146 server
->sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
1147 if ((server
->sec_mode
& SMB2_SEC_MODE_FLAGS_ALL
) != server
->sec_mode
)
1148 cifs_dbg(FYI
, "Server returned unexpected security mode 0x%x\n",
1150 server
->capabilities
= le32_to_cpu(rsp
->Capabilities
);
1151 /* Internal types */
1152 server
->capabilities
|= SMB2_NT_FIND
| SMB2_LARGE_FILES
;
1155 * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
1156 * Set the cipher type manually.
1158 if (server
->dialect
== SMB30_PROT_ID
&& (server
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
))
1159 server
->cipher_type
= SMB2_ENCRYPTION_AES128_CCM
;
1161 security_blob
= smb2_get_data_area_len(&blob_offset
, &blob_length
,
1162 (struct smb2_hdr
*)rsp
);
1164 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
1166 * ses->sectype = RawNTLMSSP;
1167 * but for time being this is our only auth choice so doesn't matter.
1168 * We just found a server which sets blob length to zero expecting raw.
1170 if (blob_length
== 0) {
1171 cifs_dbg(FYI
, "missing security blob on negprot\n");
1172 server
->sec_ntlmssp
= true;
1175 rc
= cifs_enable_signing(server
, ses
->sign
);
1179 rc
= decode_negTokenInit(security_blob
, blob_length
, server
);
1186 if (rsp
->DialectRevision
== cpu_to_le16(SMB311_PROT_ID
)) {
1187 if (rsp
->NegotiateContextCount
)
1188 rc
= smb311_decode_neg_context(rsp
, server
,
1191 cifs_server_dbg(VFS
, "Missing expected negotiate contexts\n");
1194 free_rsp_buf(resp_buftype
, rsp
);
1198 int smb3_validate_negotiate(const unsigned int xid
, struct cifs_tcon
*tcon
)
1201 struct validate_negotiate_info_req
*pneg_inbuf
;
1202 struct validate_negotiate_info_rsp
*pneg_rsp
= NULL
;
1204 u32 inbuflen
; /* max of 4 dialects */
1205 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
1207 cifs_dbg(FYI
, "validate negotiate\n");
1209 /* In SMB3.11 preauth integrity supersedes validate negotiate */
1210 if (server
->dialect
== SMB311_PROT_ID
)
1214 * validation ioctl must be signed, so no point sending this if we
1215 * can not sign it (ie are not known user). Even if signing is not
1216 * required (enabled but not negotiated), in those cases we selectively
1217 * sign just this, the first and only signed request on a connection.
1218 * Having validation of negotiate info helps reduce attack vectors.
1220 if (tcon
->ses
->session_flags
& SMB2_SESSION_FLAG_IS_GUEST
)
1221 return 0; /* validation requires signing */
1223 if (tcon
->ses
->user_name
== NULL
) {
1224 cifs_dbg(FYI
, "Can't validate negotiate: null user mount\n");
1225 return 0; /* validation requires signing */
1228 if (tcon
->ses
->session_flags
& SMB2_SESSION_FLAG_IS_NULL
)
1229 cifs_tcon_dbg(VFS
, "Unexpected null user (anonymous) auth flag sent by server\n");
1231 pneg_inbuf
= kmalloc(sizeof(*pneg_inbuf
), GFP_NOFS
);
1235 pneg_inbuf
->Capabilities
=
1236 cpu_to_le32(server
->vals
->req_capabilities
);
1237 if (tcon
->ses
->chan_max
> 1)
1238 pneg_inbuf
->Capabilities
|= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL
);
1240 memcpy(pneg_inbuf
->Guid
, server
->client_guid
,
1241 SMB2_CLIENT_GUID_SIZE
);
1243 if (tcon
->ses
->sign
)
1244 pneg_inbuf
->SecurityMode
=
1245 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED
);
1246 else if (global_secflags
& CIFSSEC_MAY_SIGN
)
1247 pneg_inbuf
->SecurityMode
=
1248 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED
);
1250 pneg_inbuf
->SecurityMode
= 0;
1253 if (strcmp(server
->vals
->version_string
,
1254 SMB3ANY_VERSION_STRING
) == 0) {
1255 pneg_inbuf
->Dialects
[0] = cpu_to_le16(SMB30_PROT_ID
);
1256 pneg_inbuf
->Dialects
[1] = cpu_to_le16(SMB302_PROT_ID
);
1257 pneg_inbuf
->Dialects
[2] = cpu_to_le16(SMB311_PROT_ID
);
1258 pneg_inbuf
->DialectCount
= cpu_to_le16(3);
1259 /* SMB 2.1 not included so subtract one dialect from len */
1260 inbuflen
= sizeof(*pneg_inbuf
) -
1261 (sizeof(pneg_inbuf
->Dialects
[0]));
1262 } else if (strcmp(server
->vals
->version_string
,
1263 SMBDEFAULT_VERSION_STRING
) == 0) {
1264 pneg_inbuf
->Dialects
[0] = cpu_to_le16(SMB21_PROT_ID
);
1265 pneg_inbuf
->Dialects
[1] = cpu_to_le16(SMB30_PROT_ID
);
1266 pneg_inbuf
->Dialects
[2] = cpu_to_le16(SMB302_PROT_ID
);
1267 pneg_inbuf
->Dialects
[3] = cpu_to_le16(SMB311_PROT_ID
);
1268 pneg_inbuf
->DialectCount
= cpu_to_le16(4);
1269 /* structure is big enough for 4 dialects */
1270 inbuflen
= sizeof(*pneg_inbuf
);
1272 /* otherwise specific dialect was requested */
1273 pneg_inbuf
->Dialects
[0] =
1274 cpu_to_le16(server
->vals
->protocol_id
);
1275 pneg_inbuf
->DialectCount
= cpu_to_le16(1);
1276 /* structure is big enough for 4 dialects, sending only 1 */
1277 inbuflen
= sizeof(*pneg_inbuf
) -
1278 sizeof(pneg_inbuf
->Dialects
[0]) * 3;
1281 rc
= SMB2_ioctl(xid
, tcon
, NO_FILE_ID
, NO_FILE_ID
,
1282 FSCTL_VALIDATE_NEGOTIATE_INFO
,
1283 (char *)pneg_inbuf
, inbuflen
, CIFSMaxBufSize
,
1284 (char **)&pneg_rsp
, &rsplen
);
1285 if (rc
== -EOPNOTSUPP
) {
1287 * Old Windows versions or Netapp SMB server can return
1288 * not supported error. Client should accept it.
1290 cifs_tcon_dbg(VFS
, "Server does not support validate negotiate\n");
1292 goto out_free_inbuf
;
1293 } else if (rc
!= 0) {
1294 cifs_tcon_dbg(VFS
, "validate protocol negotiate failed: %d\n",
1297 goto out_free_inbuf
;
1301 if (rsplen
!= sizeof(*pneg_rsp
)) {
1302 cifs_tcon_dbg(VFS
, "Invalid protocol negotiate response size: %d\n",
1305 /* relax check since Mac returns max bufsize allowed on ioctl */
1306 if (rsplen
> CIFSMaxBufSize
|| rsplen
< sizeof(*pneg_rsp
))
1310 /* check validate negotiate info response matches what we got earlier */
1311 if (pneg_rsp
->Dialect
!= cpu_to_le16(server
->dialect
))
1314 if (pneg_rsp
->SecurityMode
!= cpu_to_le16(server
->sec_mode
))
1317 /* do not validate server guid because not saved at negprot time yet */
1319 if ((le32_to_cpu(pneg_rsp
->Capabilities
) | SMB2_NT_FIND
|
1320 SMB2_LARGE_FILES
) != server
->capabilities
)
1323 /* validate negotiate successful */
1325 cifs_dbg(FYI
, "validate negotiate info successful\n");
1329 cifs_tcon_dbg(VFS
, "protocol revalidation - security settings mismatch\n");
1338 smb2_select_sectype(struct TCP_Server_Info
*server
, enum securityEnum requested
)
1340 switch (requested
) {
1347 if (server
->sec_ntlmssp
&&
1348 (global_secflags
& CIFSSEC_MAY_NTLMSSP
))
1350 if ((server
->sec_kerberos
|| server
->sec_mskerberos
) &&
1351 (global_secflags
& CIFSSEC_MAY_KRB5
))
1359 struct SMB2_sess_data
{
1361 struct cifs_ses
*ses
;
1362 struct TCP_Server_Info
*server
;
1363 struct nls_table
*nls_cp
;
1364 void (*func
)(struct SMB2_sess_data
*);
1366 u64 previous_session
;
1368 /* we will send the SMB in three pieces:
1369 * a fixed length beginning part, an optional
1370 * SPNEGO blob (which can be zero length), and a
1371 * last part which will include the strings
1372 * and rest of bcc area. This allows us to avoid
1373 * a large buffer 17K allocation
1380 SMB2_sess_alloc_buffer(struct SMB2_sess_data
*sess_data
)
1383 struct cifs_ses
*ses
= sess_data
->ses
;
1384 struct TCP_Server_Info
*server
= sess_data
->server
;
1385 struct smb2_sess_setup_req
*req
;
1386 unsigned int total_len
;
1387 bool is_binding
= false;
1389 rc
= smb2_plain_req_init(SMB2_SESSION_SETUP
, NULL
, server
,
1395 spin_lock(&ses
->ses_lock
);
1396 is_binding
= (ses
->ses_status
== SES_GOOD
);
1397 spin_unlock(&ses
->ses_lock
);
1400 req
->hdr
.SessionId
= cpu_to_le64(ses
->Suid
);
1401 req
->hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
1402 req
->PreviousSessionId
= 0;
1403 req
->Flags
= SMB2_SESSION_REQ_FLAG_BINDING
;
1404 cifs_dbg(FYI
, "Binding to sess id: %llx\n", ses
->Suid
);
1406 /* First session, not a reauthenticate */
1407 req
->hdr
.SessionId
= 0;
1409 * if reconnect, we need to send previous sess id
1412 req
->PreviousSessionId
= cpu_to_le64(sess_data
->previous_session
);
1413 req
->Flags
= 0; /* MBZ */
1414 cifs_dbg(FYI
, "Fresh session. Previous: %llx\n",
1415 sess_data
->previous_session
);
1418 /* enough to enable echos and oplocks and one max size write */
1419 if (server
->credits
>= server
->max_credits
)
1420 req
->hdr
.CreditRequest
= cpu_to_le16(0);
1422 req
->hdr
.CreditRequest
= cpu_to_le16(
1423 min_t(int, server
->max_credits
-
1424 server
->credits
, 130));
1426 /* only one of SMB2 signing flags may be set in SMB2 request */
1428 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
1429 else if (global_secflags
& CIFSSEC_MAY_SIGN
) /* one flag unlike MUST_ */
1430 req
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
1432 req
->SecurityMode
= 0;
1434 #ifdef CONFIG_CIFS_DFS_UPCALL
1435 req
->Capabilities
= cpu_to_le32(SMB2_GLOBAL_CAP_DFS
);
1437 req
->Capabilities
= 0;
1438 #endif /* DFS_UPCALL */
1440 req
->Channel
= 0; /* MBZ */
1442 sess_data
->iov
[0].iov_base
= (char *)req
;
1444 sess_data
->iov
[0].iov_len
= total_len
- 1;
1446 * This variable will be used to clear the buffer
1447 * allocated above in case of any error in the calling function.
1449 sess_data
->buf0_type
= CIFS_SMALL_BUFFER
;
1455 SMB2_sess_free_buffer(struct SMB2_sess_data
*sess_data
)
1457 struct kvec
*iov
= sess_data
->iov
;
1459 /* iov[1] is already freed by caller */
1460 if (sess_data
->buf0_type
!= CIFS_NO_BUFFER
&& iov
[0].iov_base
)
1461 memzero_explicit(iov
[0].iov_base
, iov
[0].iov_len
);
1463 free_rsp_buf(sess_data
->buf0_type
, iov
[0].iov_base
);
1464 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
1468 SMB2_sess_sendreceive(struct SMB2_sess_data
*sess_data
)
1471 struct smb_rqst rqst
;
1472 struct smb2_sess_setup_req
*req
= sess_data
->iov
[0].iov_base
;
1473 struct kvec rsp_iov
= { NULL
, 0 };
1475 /* Testing shows that buffer offset must be at location of Buffer[0] */
1476 req
->SecurityBufferOffset
=
1477 cpu_to_le16(sizeof(struct smb2_sess_setup_req
));
1478 req
->SecurityBufferLength
= cpu_to_le16(sess_data
->iov
[1].iov_len
);
1480 memset(&rqst
, 0, sizeof(struct smb_rqst
));
1481 rqst
.rq_iov
= sess_data
->iov
;
1484 /* BB add code to build os and lm fields */
1485 rc
= cifs_send_recv(sess_data
->xid
, sess_data
->ses
,
1488 &sess_data
->buf0_type
,
1489 CIFS_LOG_ERROR
| CIFS_SESS_OP
, &rsp_iov
);
1490 cifs_small_buf_release(sess_data
->iov
[0].iov_base
);
1491 memcpy(&sess_data
->iov
[0], &rsp_iov
, sizeof(struct kvec
));
1497 SMB2_sess_establish_session(struct SMB2_sess_data
*sess_data
)
1500 struct cifs_ses
*ses
= sess_data
->ses
;
1501 struct TCP_Server_Info
*server
= sess_data
->server
;
1503 cifs_server_lock(server
);
1504 if (server
->ops
->generate_signingkey
) {
1505 rc
= server
->ops
->generate_signingkey(ses
, server
);
1508 "SMB3 session key generation failed\n");
1509 cifs_server_unlock(server
);
1513 if (!server
->session_estab
) {
1514 server
->sequence_number
= 0x2;
1515 server
->session_estab
= true;
1517 cifs_server_unlock(server
);
1519 cifs_dbg(FYI
, "SMB2/3 session established successfully\n");
1523 #ifdef CONFIG_CIFS_UPCALL
1525 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
1528 struct cifs_ses
*ses
= sess_data
->ses
;
1529 struct TCP_Server_Info
*server
= sess_data
->server
;
1530 struct cifs_spnego_msg
*msg
;
1531 struct key
*spnego_key
= NULL
;
1532 struct smb2_sess_setup_rsp
*rsp
= NULL
;
1533 bool is_binding
= false;
1535 rc
= SMB2_sess_alloc_buffer(sess_data
);
1539 spnego_key
= cifs_get_spnego_key(ses
, server
);
1540 if (IS_ERR(spnego_key
)) {
1541 rc
= PTR_ERR(spnego_key
);
1543 cifs_dbg(VFS
, "Verify user has a krb5 ticket and keyutils is installed\n");
1548 msg
= spnego_key
->payload
.data
[0];
1550 * check version field to make sure that cifs.upcall is
1551 * sending us a response in an expected form
1553 if (msg
->version
!= CIFS_SPNEGO_UPCALL_VERSION
) {
1554 cifs_dbg(VFS
, "bad cifs.upcall version. Expected %d got %d\n",
1555 CIFS_SPNEGO_UPCALL_VERSION
, msg
->version
);
1557 goto out_put_spnego_key
;
1560 spin_lock(&ses
->ses_lock
);
1561 is_binding
= (ses
->ses_status
== SES_GOOD
);
1562 spin_unlock(&ses
->ses_lock
);
1564 /* keep session key if binding */
1566 kfree_sensitive(ses
->auth_key
.response
);
1567 ses
->auth_key
.response
= kmemdup(msg
->data
, msg
->sesskey_len
,
1569 if (!ses
->auth_key
.response
) {
1570 cifs_dbg(VFS
, "Kerberos can't allocate (%u bytes) memory\n",
1573 goto out_put_spnego_key
;
1575 ses
->auth_key
.len
= msg
->sesskey_len
;
1578 sess_data
->iov
[1].iov_base
= msg
->data
+ msg
->sesskey_len
;
1579 sess_data
->iov
[1].iov_len
= msg
->secblob_len
;
1581 rc
= SMB2_sess_sendreceive(sess_data
);
1583 goto out_put_spnego_key
;
1585 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
1586 /* keep session id and flags if binding */
1588 ses
->Suid
= le64_to_cpu(rsp
->hdr
.SessionId
);
1589 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
1592 rc
= SMB2_sess_establish_session(sess_data
);
1594 key_invalidate(spnego_key
);
1595 key_put(spnego_key
);
1597 kfree_sensitive(ses
->auth_key
.response
);
1598 ses
->auth_key
.response
= NULL
;
1599 ses
->auth_key
.len
= 0;
1602 sess_data
->result
= rc
;
1603 sess_data
->func
= NULL
;
1604 SMB2_sess_free_buffer(sess_data
);
1608 SMB2_auth_kerberos(struct SMB2_sess_data
*sess_data
)
1610 cifs_dbg(VFS
, "Kerberos negotiated but upcall support disabled!\n");
1611 sess_data
->result
= -EOPNOTSUPP
;
1612 sess_data
->func
= NULL
;
1617 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
);
1620 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data
*sess_data
)
1623 struct cifs_ses
*ses
= sess_data
->ses
;
1624 struct TCP_Server_Info
*server
= sess_data
->server
;
1625 struct smb2_sess_setup_rsp
*rsp
= NULL
;
1626 unsigned char *ntlmssp_blob
= NULL
;
1627 bool use_spnego
= false; /* else use raw ntlmssp */
1628 u16 blob_length
= 0;
1629 bool is_binding
= false;
1632 * If memory allocation is successful, caller of this function
1635 ses
->ntlmssp
= kmalloc(sizeof(struct ntlmssp_auth
), GFP_KERNEL
);
1636 if (!ses
->ntlmssp
) {
1640 ses
->ntlmssp
->sesskey_per_smbsess
= true;
1642 rc
= SMB2_sess_alloc_buffer(sess_data
);
1646 rc
= build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob
,
1647 &blob_length
, ses
, server
,
1653 /* BB eventually need to add this */
1654 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
1658 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
1659 sess_data
->iov
[1].iov_len
= blob_length
;
1661 rc
= SMB2_sess_sendreceive(sess_data
);
1662 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
1664 /* If true, rc here is expected and not an error */
1665 if (sess_data
->buf0_type
!= CIFS_NO_BUFFER
&&
1666 rsp
->hdr
.Status
== STATUS_MORE_PROCESSING_REQUIRED
)
1672 if (offsetof(struct smb2_sess_setup_rsp
, Buffer
) !=
1673 le16_to_cpu(rsp
->SecurityBufferOffset
)) {
1674 cifs_dbg(VFS
, "Invalid security buffer offset %d\n",
1675 le16_to_cpu(rsp
->SecurityBufferOffset
));
1679 rc
= decode_ntlmssp_challenge(rsp
->Buffer
,
1680 le16_to_cpu(rsp
->SecurityBufferLength
), ses
);
1684 cifs_dbg(FYI
, "rawntlmssp session setup challenge phase\n");
1686 spin_lock(&ses
->ses_lock
);
1687 is_binding
= (ses
->ses_status
== SES_GOOD
);
1688 spin_unlock(&ses
->ses_lock
);
1690 /* keep existing ses id and flags if binding */
1692 ses
->Suid
= le64_to_cpu(rsp
->hdr
.SessionId
);
1693 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
1697 kfree_sensitive(ntlmssp_blob
);
1698 SMB2_sess_free_buffer(sess_data
);
1700 sess_data
->result
= 0;
1701 sess_data
->func
= SMB2_sess_auth_rawntlmssp_authenticate
;
1705 kfree_sensitive(ses
->ntlmssp
);
1706 ses
->ntlmssp
= NULL
;
1707 sess_data
->result
= rc
;
1708 sess_data
->func
= NULL
;
1712 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data
*sess_data
)
1715 struct cifs_ses
*ses
= sess_data
->ses
;
1716 struct TCP_Server_Info
*server
= sess_data
->server
;
1717 struct smb2_sess_setup_req
*req
;
1718 struct smb2_sess_setup_rsp
*rsp
= NULL
;
1719 unsigned char *ntlmssp_blob
= NULL
;
1720 bool use_spnego
= false; /* else use raw ntlmssp */
1721 u16 blob_length
= 0;
1722 bool is_binding
= false;
1724 rc
= SMB2_sess_alloc_buffer(sess_data
);
1728 req
= (struct smb2_sess_setup_req
*) sess_data
->iov
[0].iov_base
;
1729 req
->hdr
.SessionId
= cpu_to_le64(ses
->Suid
);
1731 rc
= build_ntlmssp_auth_blob(&ntlmssp_blob
, &blob_length
,
1735 cifs_dbg(FYI
, "build_ntlmssp_auth_blob failed %d\n", rc
);
1740 /* BB eventually need to add this */
1741 cifs_dbg(VFS
, "spnego not supported for SMB2 yet\n");
1745 sess_data
->iov
[1].iov_base
= ntlmssp_blob
;
1746 sess_data
->iov
[1].iov_len
= blob_length
;
1748 rc
= SMB2_sess_sendreceive(sess_data
);
1752 rsp
= (struct smb2_sess_setup_rsp
*)sess_data
->iov
[0].iov_base
;
1754 spin_lock(&ses
->ses_lock
);
1755 is_binding
= (ses
->ses_status
== SES_GOOD
);
1756 spin_unlock(&ses
->ses_lock
);
1758 /* keep existing ses id and flags if binding */
1760 ses
->Suid
= le64_to_cpu(rsp
->hdr
.SessionId
);
1761 ses
->session_flags
= le16_to_cpu(rsp
->SessionFlags
);
1764 rc
= SMB2_sess_establish_session(sess_data
);
1765 #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
1766 if (ses
->server
->dialect
< SMB30_PROT_ID
) {
1767 cifs_dbg(VFS
, "%s: dumping generated SMB2 session keys\n", __func__
);
1769 * The session id is opaque in terms of endianness, so we can't
1770 * print it as a long long. we dump it as we got it on the wire
1772 cifs_dbg(VFS
, "Session Id %*ph\n", (int)sizeof(ses
->Suid
),
1774 cifs_dbg(VFS
, "Session Key %*ph\n",
1775 SMB2_NTLMV2_SESSKEY_SIZE
, ses
->auth_key
.response
);
1776 cifs_dbg(VFS
, "Signing Key %*ph\n",
1777 SMB3_SIGN_KEY_SIZE
, ses
->auth_key
.response
);
1781 kfree_sensitive(ntlmssp_blob
);
1782 SMB2_sess_free_buffer(sess_data
);
1783 kfree_sensitive(ses
->ntlmssp
);
1784 ses
->ntlmssp
= NULL
;
1785 sess_data
->result
= rc
;
1786 sess_data
->func
= NULL
;
1790 SMB2_select_sec(struct SMB2_sess_data
*sess_data
)
1793 struct cifs_ses
*ses
= sess_data
->ses
;
1794 struct TCP_Server_Info
*server
= sess_data
->server
;
1796 type
= smb2_select_sectype(server
, ses
->sectype
);
1797 cifs_dbg(FYI
, "sess setup type %d\n", type
);
1798 if (type
== Unspecified
) {
1799 cifs_dbg(VFS
, "Unable to select appropriate authentication method!\n");
1805 sess_data
->func
= SMB2_auth_kerberos
;
1808 sess_data
->func
= SMB2_sess_auth_rawntlmssp_negotiate
;
1811 cifs_dbg(VFS
, "secType %d not supported!\n", type
);
1819 SMB2_sess_setup(const unsigned int xid
, struct cifs_ses
*ses
,
1820 struct TCP_Server_Info
*server
,
1821 const struct nls_table
*nls_cp
)
1824 struct SMB2_sess_data
*sess_data
;
1826 cifs_dbg(FYI
, "Session Setup\n");
1829 WARN(1, "%s: server is NULL!\n", __func__
);
1833 sess_data
= kzalloc(sizeof(struct SMB2_sess_data
), GFP_KERNEL
);
1837 sess_data
->xid
= xid
;
1838 sess_data
->ses
= ses
;
1839 sess_data
->server
= server
;
1840 sess_data
->buf0_type
= CIFS_NO_BUFFER
;
1841 sess_data
->nls_cp
= (struct nls_table
*) nls_cp
;
1842 sess_data
->previous_session
= ses
->Suid
;
1844 rc
= SMB2_select_sec(sess_data
);
1849 * Initialize the session hash with the server one.
1851 memcpy(ses
->preauth_sha_hash
, server
->preauth_sha_hash
,
1852 SMB2_PREAUTH_HASH_SIZE
);
1854 while (sess_data
->func
)
1855 sess_data
->func(sess_data
);
1857 if ((ses
->session_flags
& SMB2_SESSION_FLAG_IS_GUEST
) && (ses
->sign
))
1858 cifs_server_dbg(VFS
, "signing requested but authenticated as guest\n");
1859 rc
= sess_data
->result
;
1861 kfree_sensitive(sess_data
);
1866 SMB2_logoff(const unsigned int xid
, struct cifs_ses
*ses
)
1868 struct smb_rqst rqst
;
1869 struct smb2_logoff_req
*req
; /* response is also trivial struct */
1871 struct TCP_Server_Info
*server
;
1873 unsigned int total_len
;
1875 struct kvec rsp_iov
;
1878 cifs_dbg(FYI
, "disconnect session %p\n", ses
);
1880 if (ses
&& (ses
->server
))
1881 server
= ses
->server
;
1885 /* no need to send SMB logoff if uid already closed due to reconnect */
1886 spin_lock(&ses
->chan_lock
);
1887 if (CIFS_ALL_CHANS_NEED_RECONNECT(ses
)) {
1888 spin_unlock(&ses
->chan_lock
);
1889 goto smb2_session_already_dead
;
1891 spin_unlock(&ses
->chan_lock
);
1893 rc
= smb2_plain_req_init(SMB2_LOGOFF
, NULL
, ses
->server
,
1894 (void **) &req
, &total_len
);
1898 /* since no tcon, smb2_init can not do this, so do here */
1899 req
->hdr
.SessionId
= cpu_to_le64(ses
->Suid
);
1901 if (ses
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
)
1902 flags
|= CIFS_TRANSFORM_REQ
;
1903 else if (server
->sign
)
1904 req
->hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
1906 flags
|= CIFS_NO_RSP_BUF
;
1908 iov
[0].iov_base
= (char *)req
;
1909 iov
[0].iov_len
= total_len
;
1911 memset(&rqst
, 0, sizeof(struct smb_rqst
));
1915 rc
= cifs_send_recv(xid
, ses
, ses
->server
,
1916 &rqst
, &resp_buf_type
, flags
, &rsp_iov
);
1917 cifs_small_buf_release(req
);
1919 * No tcon so can't do
1920 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1923 smb2_session_already_dead
:
1927 static inline void cifs_stats_fail_inc(struct cifs_tcon
*tcon
, uint16_t code
)
1929 cifs_stats_inc(&tcon
->stats
.smb2_stats
.smb2_com_failed
[code
]);
1932 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1934 /* These are similar values to what Windows uses */
1935 static inline void init_copy_chunk_defaults(struct cifs_tcon
*tcon
)
1937 tcon
->max_chunks
= 256;
1938 tcon
->max_bytes_chunk
= 1048576;
1939 tcon
->max_bytes_copy
= 16777216;
1943 SMB2_tcon(const unsigned int xid
, struct cifs_ses
*ses
, const char *tree
,
1944 struct cifs_tcon
*tcon
, const struct nls_table
*cp
)
1946 struct smb_rqst rqst
;
1947 struct smb2_tree_connect_req
*req
;
1948 struct smb2_tree_connect_rsp
*rsp
= NULL
;
1950 struct kvec rsp_iov
= { NULL
, 0 };
1954 __le16
*unc_path
= NULL
;
1956 unsigned int total_len
;
1957 struct TCP_Server_Info
*server
;
1959 /* always use master channel */
1960 server
= ses
->server
;
1962 cifs_dbg(FYI
, "TCON\n");
1964 if (!server
|| !tree
)
1967 unc_path
= kmalloc(MAX_SHARENAME_LENGTH
* 2, GFP_KERNEL
);
1968 if (unc_path
== NULL
)
1971 unc_path_len
= cifs_strtoUTF16(unc_path
, tree
, strlen(tree
), cp
);
1972 if (unc_path_len
<= 0) {
1978 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1980 atomic_set(&tcon
->num_remote_opens
, 0);
1981 rc
= smb2_plain_req_init(SMB2_TREE_CONNECT
, tcon
, server
,
1982 (void **) &req
, &total_len
);
1988 if (smb3_encryption_required(tcon
))
1989 flags
|= CIFS_TRANSFORM_REQ
;
1991 iov
[0].iov_base
= (char *)req
;
1993 iov
[0].iov_len
= total_len
- 1;
1995 /* Testing shows that buffer offset must be at location of Buffer[0] */
1996 req
->PathOffset
= cpu_to_le16(sizeof(struct smb2_tree_connect_req
));
1997 req
->PathLength
= cpu_to_le16(unc_path_len
);
1998 iov
[1].iov_base
= unc_path
;
1999 iov
[1].iov_len
= unc_path_len
;
2002 * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
2003 * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
2004 * (Samba servers don't always set the flag so also check if null user)
2006 if ((server
->dialect
== SMB311_PROT_ID
) &&
2007 !smb3_encryption_required(tcon
) &&
2008 !(ses
->session_flags
&
2009 (SMB2_SESSION_FLAG_IS_GUEST
|SMB2_SESSION_FLAG_IS_NULL
)) &&
2010 ((ses
->user_name
!= NULL
) || (ses
->sectype
== Kerberos
)))
2011 req
->hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
2013 memset(&rqst
, 0, sizeof(struct smb_rqst
));
2017 /* Need 64 for max size write so ask for more in case not there yet */
2018 if (server
->credits
>= server
->max_credits
)
2019 req
->hdr
.CreditRequest
= cpu_to_le16(0);
2021 req
->hdr
.CreditRequest
= cpu_to_le16(
2022 min_t(int, server
->max_credits
-
2023 server
->credits
, 64));
2025 rc
= cifs_send_recv(xid
, ses
, server
,
2026 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
2027 cifs_small_buf_release(req
);
2028 rsp
= (struct smb2_tree_connect_rsp
*)rsp_iov
.iov_base
;
2029 trace_smb3_tcon(xid
, tcon
->tid
, ses
->Suid
, tree
, rc
);
2030 if ((rc
!= 0) || (rsp
== NULL
)) {
2031 cifs_stats_fail_inc(tcon
, SMB2_TREE_CONNECT_HE
);
2032 tcon
->need_reconnect
= true;
2033 goto tcon_error_exit
;
2036 switch (rsp
->ShareType
) {
2037 case SMB2_SHARE_TYPE_DISK
:
2038 cifs_dbg(FYI
, "connection to disk share\n");
2040 case SMB2_SHARE_TYPE_PIPE
:
2042 cifs_dbg(FYI
, "connection to pipe share\n");
2044 case SMB2_SHARE_TYPE_PRINT
:
2046 cifs_dbg(FYI
, "connection to printer\n");
2049 cifs_server_dbg(VFS
, "unknown share type %d\n", rsp
->ShareType
);
2051 goto tcon_error_exit
;
2054 tcon
->share_flags
= le32_to_cpu(rsp
->ShareFlags
);
2055 tcon
->capabilities
= rsp
->Capabilities
; /* we keep caps little endian */
2056 tcon
->maximal_access
= le32_to_cpu(rsp
->MaximalAccess
);
2057 tcon
->tid
= le32_to_cpu(rsp
->hdr
.Id
.SyncId
.TreeId
);
2058 strscpy(tcon
->tree_name
, tree
, sizeof(tcon
->tree_name
));
2060 if ((rsp
->Capabilities
& SMB2_SHARE_CAP_DFS
) &&
2061 ((tcon
->share_flags
& SHI1005_FLAGS_DFS
) == 0))
2062 cifs_tcon_dbg(VFS
, "DFS capability contradicts DFS flag\n");
2065 !(server
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
))
2066 cifs_tcon_dbg(VFS
, "Encryption is requested but not supported\n");
2068 init_copy_chunk_defaults(tcon
);
2069 if (server
->ops
->validate_negotiate
)
2070 rc
= server
->ops
->validate_negotiate(xid
, tcon
);
2071 if (rc
== 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
2072 if (tcon
->share_flags
& SMB2_SHAREFLAG_ISOLATED_TRANSPORT
)
2073 server
->nosharesock
= true;
2076 free_rsp_buf(resp_buftype
, rsp
);
2081 if (rsp
&& rsp
->hdr
.Status
== STATUS_BAD_NETWORK_NAME
)
2082 cifs_tcon_dbg(VFS
, "BAD_NETWORK_NAME: %s\n", tree
);
2087 SMB2_tdis(const unsigned int xid
, struct cifs_tcon
*tcon
)
2089 struct smb_rqst rqst
;
2090 struct smb2_tree_disconnect_req
*req
; /* response is trivial */
2092 struct cifs_ses
*ses
= tcon
->ses
;
2094 unsigned int total_len
;
2096 struct kvec rsp_iov
;
2099 cifs_dbg(FYI
, "Tree Disconnect\n");
2101 if (!ses
|| !(ses
->server
))
2104 trace_smb3_tdis_enter(xid
, tcon
->tid
, ses
->Suid
, tcon
->tree_name
);
2105 spin_lock(&ses
->chan_lock
);
2106 if ((tcon
->need_reconnect
) ||
2107 (CIFS_ALL_CHANS_NEED_RECONNECT(tcon
->ses
))) {
2108 spin_unlock(&ses
->chan_lock
);
2111 spin_unlock(&ses
->chan_lock
);
2113 invalidate_all_cached_dirs(tcon
);
2115 rc
= smb2_plain_req_init(SMB2_TREE_DISCONNECT
, tcon
, ses
->server
,
2121 if (smb3_encryption_required(tcon
))
2122 flags
|= CIFS_TRANSFORM_REQ
;
2124 flags
|= CIFS_NO_RSP_BUF
;
2126 iov
[0].iov_base
= (char *)req
;
2127 iov
[0].iov_len
= total_len
;
2129 memset(&rqst
, 0, sizeof(struct smb_rqst
));
2133 rc
= cifs_send_recv(xid
, ses
, ses
->server
,
2134 &rqst
, &resp_buf_type
, flags
, &rsp_iov
);
2135 cifs_small_buf_release(req
);
2137 cifs_stats_fail_inc(tcon
, SMB2_TREE_DISCONNECT_HE
);
2138 trace_smb3_tdis_err(xid
, tcon
->tid
, ses
->Suid
, rc
);
2140 trace_smb3_tdis_done(xid
, tcon
->tid
, ses
->Suid
);
2146 static struct create_durable
*
2147 create_durable_buf(void)
2149 struct create_durable
*buf
;
2151 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
2155 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
2156 (struct create_durable
, Data
));
2157 buf
->ccontext
.DataLength
= cpu_to_le32(16);
2158 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
2159 (struct create_durable
, Name
));
2160 buf
->ccontext
.NameLength
= cpu_to_le16(4);
2161 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
2169 static struct create_durable
*
2170 create_reconnect_durable_buf(struct cifs_fid
*fid
)
2172 struct create_durable
*buf
;
2174 buf
= kzalloc(sizeof(struct create_durable
), GFP_KERNEL
);
2178 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
2179 (struct create_durable
, Data
));
2180 buf
->ccontext
.DataLength
= cpu_to_le32(16);
2181 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
2182 (struct create_durable
, Name
));
2183 buf
->ccontext
.NameLength
= cpu_to_le16(4);
2184 buf
->Data
.Fid
.PersistentFileId
= fid
->persistent_fid
;
2185 buf
->Data
.Fid
.VolatileFileId
= fid
->volatile_fid
;
2186 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
2195 parse_query_id_ctxt(struct create_context
*cc
, struct smb2_file_all_info
*buf
)
2197 struct create_disk_id_rsp
*pdisk_id
= (struct create_disk_id_rsp
*)cc
;
2199 cifs_dbg(FYI
, "parse query id context 0x%llx 0x%llx\n",
2200 pdisk_id
->DiskFileId
, pdisk_id
->VolumeId
);
2201 buf
->IndexNumber
= pdisk_id
->DiskFileId
;
2205 parse_posix_ctxt(struct create_context
*cc
, struct smb2_file_all_info
*info
,
2206 struct create_posix_rsp
*posix
)
2209 u8
*beg
= (u8
*)cc
+ le16_to_cpu(cc
->DataOffset
);
2210 u8
*end
= beg
+ le32_to_cpu(cc
->DataLength
);
2213 memset(posix
, 0, sizeof(*posix
));
2215 posix
->nlink
= le32_to_cpu(*(__le32
*)(beg
+ 0));
2216 posix
->reparse_tag
= le32_to_cpu(*(__le32
*)(beg
+ 4));
2217 posix
->mode
= le32_to_cpu(*(__le32
*)(beg
+ 8));
2220 sid_len
= posix_info_sid_size(sid
, end
);
2222 cifs_dbg(VFS
, "bad owner sid in posix create response\n");
2225 memcpy(&posix
->owner
, sid
, sid_len
);
2227 sid
= sid
+ sid_len
;
2228 sid_len
= posix_info_sid_size(sid
, end
);
2230 cifs_dbg(VFS
, "bad group sid in posix create response\n");
2233 memcpy(&posix
->group
, sid
, sid_len
);
2235 cifs_dbg(FYI
, "nlink=%d mode=%o reparse_tag=%x\n",
2236 posix
->nlink
, posix
->mode
, posix
->reparse_tag
);
2239 int smb2_parse_contexts(struct TCP_Server_Info
*server
,
2240 struct kvec
*rsp_iov
,
2241 unsigned int *epoch
,
2242 char *lease_key
, __u8
*oplock
,
2243 struct smb2_file_all_info
*buf
,
2244 struct create_posix_rsp
*posix
)
2246 struct smb2_create_rsp
*rsp
= rsp_iov
->iov_base
;
2247 struct create_context
*cc
;
2248 size_t rem
, off
, len
;
2252 static const char smb3_create_tag_posix
[] = {
2253 0x93, 0xAD, 0x25, 0x50, 0x9C,
2254 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
2255 0xDE, 0x96, 0x8B, 0xCD, 0x7C
2260 off
= le32_to_cpu(rsp
->CreateContextsOffset
);
2261 rem
= le32_to_cpu(rsp
->CreateContextsLength
);
2262 if (check_add_overflow(off
, rem
, &len
) || len
> rsp_iov
->iov_len
)
2264 cc
= (struct create_context
*)((u8
*)rsp
+ off
);
2266 /* Initialize inode number to 0 in case no valid data in qfid context */
2268 buf
->IndexNumber
= 0;
2270 while (rem
>= sizeof(*cc
)) {
2271 doff
= le16_to_cpu(cc
->DataOffset
);
2272 dlen
= le32_to_cpu(cc
->DataLength
);
2273 if (check_add_overflow(doff
, dlen
, &len
) || len
> rem
)
2276 noff
= le16_to_cpu(cc
->NameOffset
);
2277 nlen
= le16_to_cpu(cc
->NameLength
);
2278 if (noff
+ nlen
>= doff
)
2281 name
= (char *)cc
+ noff
;
2284 if (!strncmp(name
, SMB2_CREATE_REQUEST_LEASE
, 4)) {
2285 *oplock
= server
->ops
->parse_lease_buf(cc
, epoch
,
2288 !strncmp(name
, SMB2_CREATE_QUERY_ON_DISK_ID
, 4)) {
2289 parse_query_id_ctxt(cc
, buf
);
2293 if (posix
&& !memcmp(name
, smb3_create_tag_posix
, 16))
2294 parse_posix_ctxt(cc
, buf
, posix
);
2297 cifs_dbg(FYI
, "%s: unhandled context (nlen=%zu dlen=%zu)\n",
2298 __func__
, nlen
, dlen
);
2299 if (IS_ENABLED(CONFIG_CIFS_DEBUG2
))
2300 cifs_dump_mem("context data: ", cc
, dlen
);
2304 off
= le32_to_cpu(cc
->Next
);
2307 if (check_sub_overflow(rem
, off
, &rem
))
2309 cc
= (struct create_context
*)((u8
*)cc
+ off
);
2312 if (rsp
->OplockLevel
!= SMB2_OPLOCK_LEVEL_LEASE
)
2313 *oplock
= rsp
->OplockLevel
;
2319 add_lease_context(struct TCP_Server_Info
*server
,
2320 struct smb2_create_req
*req
,
2322 unsigned int *num_iovec
, u8
*lease_key
, __u8
*oplock
)
2324 unsigned int num
= *num_iovec
;
2326 iov
[num
].iov_base
= server
->ops
->create_lease_buf(lease_key
, *oplock
);
2327 if (iov
[num
].iov_base
== NULL
)
2329 iov
[num
].iov_len
= server
->vals
->create_lease_size
;
2330 req
->RequestedOplockLevel
= SMB2_OPLOCK_LEVEL_LEASE
;
2331 *num_iovec
= num
+ 1;
2335 static struct create_durable_v2
*
2336 create_durable_v2_buf(struct cifs_open_parms
*oparms
)
2338 struct cifs_fid
*pfid
= oparms
->fid
;
2339 struct create_durable_v2
*buf
;
2341 buf
= kzalloc(sizeof(struct create_durable_v2
), GFP_KERNEL
);
2345 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
2346 (struct create_durable_v2
, dcontext
));
2347 buf
->ccontext
.DataLength
= cpu_to_le32(sizeof(struct durable_context_v2
));
2348 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
2349 (struct create_durable_v2
, Name
));
2350 buf
->ccontext
.NameLength
= cpu_to_le16(4);
2353 * NB: Handle timeout defaults to 0, which allows server to choose
2354 * (most servers default to 120 seconds) and most clients default to 0.
2355 * This can be overridden at mount ("handletimeout=") if the user wants
2356 * a different persistent (or resilient) handle timeout for all opens
2357 * on a particular SMB3 mount.
2359 buf
->dcontext
.Timeout
= cpu_to_le32(oparms
->tcon
->handle_timeout
);
2360 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
2361 generate_random_uuid(buf
->dcontext
.CreateGuid
);
2362 memcpy(pfid
->create_guid
, buf
->dcontext
.CreateGuid
, 16);
2364 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
2372 static struct create_durable_handle_reconnect_v2
*
2373 create_reconnect_durable_v2_buf(struct cifs_fid
*fid
)
2375 struct create_durable_handle_reconnect_v2
*buf
;
2377 buf
= kzalloc(sizeof(struct create_durable_handle_reconnect_v2
),
2382 buf
->ccontext
.DataOffset
=
2383 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
2385 buf
->ccontext
.DataLength
=
2386 cpu_to_le32(sizeof(struct durable_reconnect_context_v2
));
2387 buf
->ccontext
.NameOffset
=
2388 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2
,
2390 buf
->ccontext
.NameLength
= cpu_to_le16(4);
2392 buf
->dcontext
.Fid
.PersistentFileId
= fid
->persistent_fid
;
2393 buf
->dcontext
.Fid
.VolatileFileId
= fid
->volatile_fid
;
2394 buf
->dcontext
.Flags
= cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT
);
2395 memcpy(buf
->dcontext
.CreateGuid
, fid
->create_guid
, 16);
2397 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
2406 add_durable_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
2407 struct cifs_open_parms
*oparms
)
2409 unsigned int num
= *num_iovec
;
2411 iov
[num
].iov_base
= create_durable_v2_buf(oparms
);
2412 if (iov
[num
].iov_base
== NULL
)
2414 iov
[num
].iov_len
= sizeof(struct create_durable_v2
);
2415 *num_iovec
= num
+ 1;
2420 add_durable_reconnect_v2_context(struct kvec
*iov
, unsigned int *num_iovec
,
2421 struct cifs_open_parms
*oparms
)
2423 unsigned int num
= *num_iovec
;
2425 /* indicate that we don't need to relock the file */
2426 oparms
->reconnect
= false;
2428 iov
[num
].iov_base
= create_reconnect_durable_v2_buf(oparms
->fid
);
2429 if (iov
[num
].iov_base
== NULL
)
2431 iov
[num
].iov_len
= sizeof(struct create_durable_handle_reconnect_v2
);
2432 *num_iovec
= num
+ 1;
2437 add_durable_context(struct kvec
*iov
, unsigned int *num_iovec
,
2438 struct cifs_open_parms
*oparms
, bool use_persistent
)
2440 unsigned int num
= *num_iovec
;
2442 if (use_persistent
) {
2443 if (oparms
->reconnect
)
2444 return add_durable_reconnect_v2_context(iov
, num_iovec
,
2447 return add_durable_v2_context(iov
, num_iovec
, oparms
);
2450 if (oparms
->reconnect
) {
2451 iov
[num
].iov_base
= create_reconnect_durable_buf(oparms
->fid
);
2452 /* indicate that we don't need to relock the file */
2453 oparms
->reconnect
= false;
2455 iov
[num
].iov_base
= create_durable_buf();
2456 if (iov
[num
].iov_base
== NULL
)
2458 iov
[num
].iov_len
= sizeof(struct create_durable
);
2459 *num_iovec
= num
+ 1;
2463 /* See MS-SMB2 2.2.13.2.7 */
2464 static struct crt_twarp_ctxt
*
2465 create_twarp_buf(__u64 timewarp
)
2467 struct crt_twarp_ctxt
*buf
;
2469 buf
= kzalloc(sizeof(struct crt_twarp_ctxt
), GFP_KERNEL
);
2473 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
2474 (struct crt_twarp_ctxt
, Timestamp
));
2475 buf
->ccontext
.DataLength
= cpu_to_le32(8);
2476 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
2477 (struct crt_twarp_ctxt
, Name
));
2478 buf
->ccontext
.NameLength
= cpu_to_le16(4);
2479 /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
2484 buf
->Timestamp
= cpu_to_le64(timewarp
);
2488 /* See MS-SMB2 2.2.13.2.7 */
2490 add_twarp_context(struct kvec
*iov
, unsigned int *num_iovec
, __u64 timewarp
)
2492 unsigned int num
= *num_iovec
;
2494 iov
[num
].iov_base
= create_twarp_buf(timewarp
);
2495 if (iov
[num
].iov_base
== NULL
)
2497 iov
[num
].iov_len
= sizeof(struct crt_twarp_ctxt
);
2498 *num_iovec
= num
+ 1;
2502 /* See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
2503 static void setup_owner_group_sids(char *buf
)
2505 struct owner_group_sids
*sids
= (struct owner_group_sids
*)buf
;
2507 /* Populate the user ownership fields S-1-5-88-1 */
2508 sids
->owner
.Revision
= 1;
2509 sids
->owner
.NumAuth
= 3;
2510 sids
->owner
.Authority
[5] = 5;
2511 sids
->owner
.SubAuthorities
[0] = cpu_to_le32(88);
2512 sids
->owner
.SubAuthorities
[1] = cpu_to_le32(1);
2513 sids
->owner
.SubAuthorities
[2] = cpu_to_le32(current_fsuid().val
);
2515 /* Populate the group ownership fields S-1-5-88-2 */
2516 sids
->group
.Revision
= 1;
2517 sids
->group
.NumAuth
= 3;
2518 sids
->group
.Authority
[5] = 5;
2519 sids
->group
.SubAuthorities
[0] = cpu_to_le32(88);
2520 sids
->group
.SubAuthorities
[1] = cpu_to_le32(2);
2521 sids
->group
.SubAuthorities
[2] = cpu_to_le32(current_fsgid().val
);
2523 cifs_dbg(FYI
, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val
, current_fsgid().val
);
2526 /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
2527 static struct crt_sd_ctxt
*
2528 create_sd_buf(umode_t mode
, bool set_owner
, unsigned int *len
)
2530 struct crt_sd_ctxt
*buf
;
2532 unsigned int acelen
, acl_size
, ace_count
;
2533 unsigned int owner_offset
= 0;
2534 unsigned int group_offset
= 0;
2535 struct smb3_acl acl
= {};
2537 *len
= round_up(sizeof(struct crt_sd_ctxt
) + (sizeof(struct cifs_ace
) * 4), 8);
2540 /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
2541 *len
+= sizeof(struct owner_group_sids
);
2544 buf
= kzalloc(*len
, GFP_KERNEL
);
2548 ptr
= (__u8
*)&buf
[1];
2550 /* offset fields are from beginning of security descriptor not of create context */
2551 owner_offset
= ptr
- (__u8
*)&buf
->sd
;
2552 buf
->sd
.OffsetOwner
= cpu_to_le32(owner_offset
);
2553 group_offset
= owner_offset
+ offsetof(struct owner_group_sids
, group
);
2554 buf
->sd
.OffsetGroup
= cpu_to_le32(group_offset
);
2556 setup_owner_group_sids(ptr
);
2557 ptr
+= sizeof(struct owner_group_sids
);
2559 buf
->sd
.OffsetOwner
= 0;
2560 buf
->sd
.OffsetGroup
= 0;
2563 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof(struct crt_sd_ctxt
, sd
));
2564 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof(struct crt_sd_ctxt
, Name
));
2565 buf
->ccontext
.NameLength
= cpu_to_le16(4);
2566 /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
2571 buf
->sd
.Revision
= 1; /* Must be one see MS-DTYP 2.4.6 */
2574 * ACL is "self relative" ie ACL is stored in contiguous block of memory
2575 * and "DP" ie the DACL is present
2577 buf
->sd
.Control
= cpu_to_le16(ACL_CONTROL_SR
| ACL_CONTROL_DP
);
2579 /* offset owner, group and Sbz1 and SACL are all zero */
2580 buf
->sd
.OffsetDacl
= cpu_to_le32(ptr
- (__u8
*)&buf
->sd
);
2581 /* Ship the ACL for now. we will copy it into buf later. */
2583 ptr
+= sizeof(struct smb3_acl
);
2585 /* create one ACE to hold the mode embedded in reserved special SID */
2586 acelen
= setup_special_mode_ACE((struct cifs_ace
*)ptr
, (__u64
)mode
);
2588 acl_size
= acelen
+ sizeof(struct smb3_acl
);
2592 /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
2593 acelen
= setup_special_user_owner_ACE((struct cifs_ace
*)ptr
);
2599 /* and one more ACE to allow access for authenticated users */
2600 acelen
= setup_authusers_ACE((struct cifs_ace
*)ptr
);
2605 acl
.AclRevision
= ACL_REVISION
; /* See 2.4.4.1 of MS-DTYP */
2606 acl
.AclSize
= cpu_to_le16(acl_size
);
2607 acl
.AceCount
= cpu_to_le16(ace_count
);
2608 /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
2609 memcpy(aclptr
, &acl
, sizeof(struct smb3_acl
));
2611 buf
->ccontext
.DataLength
= cpu_to_le32(ptr
- (__u8
*)&buf
->sd
);
2612 *len
= round_up((unsigned int)(ptr
- (__u8
*)buf
), 8);
2618 add_sd_context(struct kvec
*iov
, unsigned int *num_iovec
, umode_t mode
, bool set_owner
)
2620 unsigned int num
= *num_iovec
;
2621 unsigned int len
= 0;
2623 iov
[num
].iov_base
= create_sd_buf(mode
, set_owner
, &len
);
2624 if (iov
[num
].iov_base
== NULL
)
2626 iov
[num
].iov_len
= len
;
2627 *num_iovec
= num
+ 1;
2631 static struct crt_query_id_ctxt
*
2632 create_query_id_buf(void)
2634 struct crt_query_id_ctxt
*buf
;
2636 buf
= kzalloc(sizeof(struct crt_query_id_ctxt
), GFP_KERNEL
);
2640 buf
->ccontext
.DataOffset
= cpu_to_le16(0);
2641 buf
->ccontext
.DataLength
= cpu_to_le32(0);
2642 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
2643 (struct crt_query_id_ctxt
, Name
));
2644 buf
->ccontext
.NameLength
= cpu_to_le16(4);
2645 /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
2653 /* See MS-SMB2 2.2.13.2.9 */
2655 add_query_id_context(struct kvec
*iov
, unsigned int *num_iovec
)
2657 unsigned int num
= *num_iovec
;
2659 iov
[num
].iov_base
= create_query_id_buf();
2660 if (iov
[num
].iov_base
== NULL
)
2662 iov
[num
].iov_len
= sizeof(struct crt_query_id_ctxt
);
2663 *num_iovec
= num
+ 1;
2668 alloc_path_with_tree_prefix(__le16
**out_path
, int *out_size
, int *out_len
,
2669 const char *treename
, const __le16
*path
)
2671 int treename_len
, path_len
;
2672 struct nls_table
*cp
;
2673 const __le16 sep
[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
2678 treename_len
= strlen(treename
);
2679 if (treename_len
< 2 || !(treename
[0] == '\\' && treename
[1] == '\\'))
2685 path_len
= UniStrnlen((wchar_t *)path
, PATH_MAX
);
2687 /* make room for one path separator only if @path isn't empty */
2688 *out_len
= treename_len
+ (path
[0] ? 1 : 0) + path_len
;
2691 * final path needs to be 8-byte aligned as specified in
2692 * MS-SMB2 2.2.13 SMB2 CREATE Request.
2694 *out_size
= round_up(*out_len
* sizeof(__le16
), 8);
2695 *out_path
= kzalloc(*out_size
+ sizeof(__le16
) /* null */, GFP_KERNEL
);
2699 cp
= load_nls_default();
2700 cifs_strtoUTF16(*out_path
, treename
, treename_len
, cp
);
2702 /* Do not append the separator if the path is empty */
2703 if (path
[0] != cpu_to_le16(0x0000)) {
2704 UniStrcat((wchar_t *)*out_path
, (wchar_t *)sep
);
2705 UniStrcat((wchar_t *)*out_path
, (wchar_t *)path
);
2713 int smb311_posix_mkdir(const unsigned int xid
, struct inode
*inode
,
2714 umode_t mode
, struct cifs_tcon
*tcon
,
2715 const char *full_path
,
2716 struct cifs_sb_info
*cifs_sb
)
2718 struct smb_rqst rqst
;
2719 struct smb2_create_req
*req
;
2720 struct smb2_create_rsp
*rsp
= NULL
;
2721 struct cifs_ses
*ses
= tcon
->ses
;
2722 struct kvec iov
[3]; /* make sure at least one for each open context */
2723 struct kvec rsp_iov
= {NULL
, 0};
2726 __le16
*copy_path
= NULL
;
2729 unsigned int n_iov
= 2;
2730 __u32 file_attributes
= 0;
2731 char *pc_buf
= NULL
;
2733 unsigned int total_len
;
2734 __le16
*utf16_path
= NULL
;
2735 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
2737 cifs_dbg(FYI
, "mkdir\n");
2739 /* resource #1: path allocation */
2740 utf16_path
= cifs_convert_path_to_utf16(full_path
, cifs_sb
);
2744 if (!ses
|| !server
) {
2749 /* resource #2: request */
2750 rc
= smb2_plain_req_init(SMB2_CREATE
, tcon
, server
,
2751 (void **) &req
, &total_len
);
2756 if (smb3_encryption_required(tcon
))
2757 flags
|= CIFS_TRANSFORM_REQ
;
2759 req
->ImpersonationLevel
= IL_IMPERSONATION
;
2760 req
->DesiredAccess
= cpu_to_le32(FILE_WRITE_ATTRIBUTES
);
2761 /* File attributes ignored on open (used in create though) */
2762 req
->FileAttributes
= cpu_to_le32(file_attributes
);
2763 req
->ShareAccess
= FILE_SHARE_ALL_LE
;
2764 req
->CreateDisposition
= cpu_to_le32(FILE_CREATE
);
2765 req
->CreateOptions
= cpu_to_le32(CREATE_NOT_FILE
);
2767 iov
[0].iov_base
= (char *)req
;
2768 /* -1 since last byte is buf[0] which is sent below (path) */
2769 iov
[0].iov_len
= total_len
- 1;
2771 req
->NameOffset
= cpu_to_le16(sizeof(struct smb2_create_req
));
2773 /* [MS-SMB2] 2.2.13 NameOffset:
2774 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2775 * the SMB2 header, the file name includes a prefix that will
2776 * be processed during DFS name normalization as specified in
2777 * section 3.3.5.9. Otherwise, the file name is relative to
2778 * the share that is identified by the TreeId in the SMB2
2781 if (tcon
->share_flags
& SHI1005_FLAGS_DFS
) {
2784 req
->hdr
.Flags
|= SMB2_FLAGS_DFS_OPERATIONS
;
2785 rc
= alloc_path_with_tree_prefix(©_path
, ©_size
,
2787 tcon
->tree_name
, utf16_path
);
2791 req
->NameLength
= cpu_to_le16(name_len
* 2);
2792 uni_path_len
= copy_size
;
2793 /* free before overwriting resource */
2795 utf16_path
= copy_path
;
2797 uni_path_len
= (2 * UniStrnlen((wchar_t *)utf16_path
, PATH_MAX
)) + 2;
2798 /* MUST set path len (NameLength) to 0 opening root of share */
2799 req
->NameLength
= cpu_to_le16(uni_path_len
- 2);
2800 if (uni_path_len
% 8 != 0) {
2801 copy_size
= roundup(uni_path_len
, 8);
2802 copy_path
= kzalloc(copy_size
, GFP_KERNEL
);
2807 memcpy((char *)copy_path
, (const char *)utf16_path
,
2809 uni_path_len
= copy_size
;
2810 /* free before overwriting resource */
2812 utf16_path
= copy_path
;
2816 iov
[1].iov_len
= uni_path_len
;
2817 iov
[1].iov_base
= utf16_path
;
2818 req
->RequestedOplockLevel
= SMB2_OPLOCK_LEVEL_NONE
;
2820 if (tcon
->posix_extensions
) {
2821 /* resource #3: posix buf */
2822 rc
= add_posix_context(iov
, &n_iov
, mode
);
2825 req
->CreateContextsOffset
= cpu_to_le32(
2826 sizeof(struct smb2_create_req
) +
2828 pc_buf
= iov
[n_iov
-1].iov_base
;
2832 memset(&rqst
, 0, sizeof(struct smb_rqst
));
2834 rqst
.rq_nvec
= n_iov
;
2836 /* no need to inc num_remote_opens because we close it just below */
2837 trace_smb3_posix_mkdir_enter(xid
, tcon
->tid
, ses
->Suid
, full_path
, CREATE_NOT_FILE
,
2838 FILE_WRITE_ATTRIBUTES
);
2839 /* resource #4: response buffer */
2840 rc
= cifs_send_recv(xid
, ses
, server
,
2841 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
2843 cifs_stats_fail_inc(tcon
, SMB2_CREATE_HE
);
2844 trace_smb3_posix_mkdir_err(xid
, tcon
->tid
, ses
->Suid
,
2846 FILE_WRITE_ATTRIBUTES
, rc
);
2847 goto err_free_rsp_buf
;
2851 * Although unlikely to be possible for rsp to be null and rc not set,
2852 * adding check below is slightly safer long term (and quiets Coverity
2855 rsp
= (struct smb2_create_rsp
*)rsp_iov
.iov_base
;
2862 trace_smb3_posix_mkdir_done(xid
, rsp
->PersistentFileId
, tcon
->tid
, ses
->Suid
,
2863 CREATE_NOT_FILE
, FILE_WRITE_ATTRIBUTES
);
2865 SMB2_close(xid
, tcon
, rsp
->PersistentFileId
, rsp
->VolatileFileId
);
2867 /* Eventually save off posix specific response info and timestaps */
2870 free_rsp_buf(resp_buftype
, rsp
);
2873 cifs_small_buf_release(req
);
2880 SMB2_open_init(struct cifs_tcon
*tcon
, struct TCP_Server_Info
*server
,
2881 struct smb_rqst
*rqst
, __u8
*oplock
,
2882 struct cifs_open_parms
*oparms
, __le16
*path
)
2884 struct smb2_create_req
*req
;
2885 unsigned int n_iov
= 2;
2886 __u32 file_attributes
= 0;
2889 unsigned int total_len
;
2890 struct kvec
*iov
= rqst
->rq_iov
;
2894 rc
= smb2_plain_req_init(SMB2_CREATE
, tcon
, server
,
2895 (void **) &req
, &total_len
);
2899 iov
[0].iov_base
= (char *)req
;
2900 /* -1 since last byte is buf[0] which is sent below (path) */
2901 iov
[0].iov_len
= total_len
- 1;
2903 if (oparms
->create_options
& CREATE_OPTION_READONLY
)
2904 file_attributes
|= ATTR_READONLY
;
2905 if (oparms
->create_options
& CREATE_OPTION_SPECIAL
)
2906 file_attributes
|= ATTR_SYSTEM
;
2908 req
->ImpersonationLevel
= IL_IMPERSONATION
;
2909 req
->DesiredAccess
= cpu_to_le32(oparms
->desired_access
);
2910 /* File attributes ignored on open (used in create though) */
2911 req
->FileAttributes
= cpu_to_le32(file_attributes
);
2912 req
->ShareAccess
= FILE_SHARE_ALL_LE
;
2914 req
->CreateDisposition
= cpu_to_le32(oparms
->disposition
);
2915 req
->CreateOptions
= cpu_to_le32(oparms
->create_options
& CREATE_OPTIONS_MASK
);
2916 req
->NameOffset
= cpu_to_le16(sizeof(struct smb2_create_req
));
2918 /* [MS-SMB2] 2.2.13 NameOffset:
2919 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
2920 * the SMB2 header, the file name includes a prefix that will
2921 * be processed during DFS name normalization as specified in
2922 * section 3.3.5.9. Otherwise, the file name is relative to
2923 * the share that is identified by the TreeId in the SMB2
2926 if (tcon
->share_flags
& SHI1005_FLAGS_DFS
) {
2929 req
->hdr
.Flags
|= SMB2_FLAGS_DFS_OPERATIONS
;
2930 rc
= alloc_path_with_tree_prefix(©_path
, ©_size
,
2932 tcon
->tree_name
, path
);
2935 req
->NameLength
= cpu_to_le16(name_len
* 2);
2936 uni_path_len
= copy_size
;
2939 uni_path_len
= (2 * UniStrnlen((wchar_t *)path
, PATH_MAX
)) + 2;
2940 /* MUST set path len (NameLength) to 0 opening root of share */
2941 req
->NameLength
= cpu_to_le16(uni_path_len
- 2);
2942 copy_size
= round_up(uni_path_len
, 8);
2943 copy_path
= kzalloc(copy_size
, GFP_KERNEL
);
2946 memcpy((char *)copy_path
, (const char *)path
,
2948 uni_path_len
= copy_size
;
2952 iov
[1].iov_len
= uni_path_len
;
2953 iov
[1].iov_base
= path
;
2955 if ((!server
->oplocks
) || (tcon
->no_lease
))
2956 *oplock
= SMB2_OPLOCK_LEVEL_NONE
;
2958 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
) ||
2959 *oplock
== SMB2_OPLOCK_LEVEL_NONE
)
2960 req
->RequestedOplockLevel
= *oplock
;
2961 else if (!(server
->capabilities
& SMB2_GLOBAL_CAP_DIRECTORY_LEASING
) &&
2962 (oparms
->create_options
& CREATE_NOT_FILE
))
2963 req
->RequestedOplockLevel
= *oplock
; /* no srv lease support */
2965 rc
= add_lease_context(server
, req
, iov
, &n_iov
,
2966 oparms
->fid
->lease_key
, oplock
);
2971 if (*oplock
== SMB2_OPLOCK_LEVEL_BATCH
) {
2972 rc
= add_durable_context(iov
, &n_iov
, oparms
,
2973 tcon
->use_persistent
);
2978 if (tcon
->posix_extensions
) {
2979 rc
= add_posix_context(iov
, &n_iov
, oparms
->mode
);
2984 if (tcon
->snapshot_time
) {
2985 cifs_dbg(FYI
, "adding snapshot context\n");
2986 rc
= add_twarp_context(iov
, &n_iov
, tcon
->snapshot_time
);
2991 if ((oparms
->disposition
!= FILE_OPEN
) && (oparms
->cifs_sb
)) {
2995 if ((oparms
->cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_MODE_FROM_SID
) &&
2996 (oparms
->mode
!= ACL_NO_MODE
))
3000 oparms
->mode
= ACL_NO_MODE
;
3003 if (oparms
->cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UID_FROM_ACL
)
3008 if (set_owner
| set_mode
) {
3009 cifs_dbg(FYI
, "add sd with mode 0x%x\n", oparms
->mode
);
3010 rc
= add_sd_context(iov
, &n_iov
, oparms
->mode
, set_owner
);
3016 add_query_id_context(iov
, &n_iov
);
3020 * We have create contexts behind iov[1] (the file
3021 * name), point at them from the main create request
3023 req
->CreateContextsOffset
= cpu_to_le32(
3024 sizeof(struct smb2_create_req
) +
3026 req
->CreateContextsLength
= 0;
3028 for (unsigned int i
= 2; i
< (n_iov
-1); i
++) {
3029 struct kvec
*v
= &iov
[i
];
3030 size_t len
= v
->iov_len
;
3031 struct create_context
*cctx
=
3032 (struct create_context
*)v
->iov_base
;
3034 cctx
->Next
= cpu_to_le32(len
);
3035 le32_add_cpu(&req
->CreateContextsLength
, len
);
3037 le32_add_cpu(&req
->CreateContextsLength
,
3038 iov
[n_iov
-1].iov_len
);
3041 rqst
->rq_nvec
= n_iov
;
3045 /* rq_iov[0] is the request and is released by cifs_small_buf_release().
3046 * All other vectors are freed by kfree().
3049 SMB2_open_free(struct smb_rqst
*rqst
)
3053 if (rqst
&& rqst
->rq_iov
) {
3054 cifs_small_buf_release(rqst
->rq_iov
[0].iov_base
);
3055 for (i
= 1; i
< rqst
->rq_nvec
; i
++)
3056 if (rqst
->rq_iov
[i
].iov_base
!= smb2_padding
)
3057 kfree(rqst
->rq_iov
[i
].iov_base
);
3062 SMB2_open(const unsigned int xid
, struct cifs_open_parms
*oparms
, __le16
*path
,
3063 __u8
*oplock
, struct smb2_file_all_info
*buf
,
3064 struct create_posix_rsp
*posix
,
3065 struct kvec
*err_iov
, int *buftype
)
3067 struct smb_rqst rqst
;
3068 struct smb2_create_rsp
*rsp
= NULL
;
3069 struct cifs_tcon
*tcon
= oparms
->tcon
;
3070 struct cifs_ses
*ses
= tcon
->ses
;
3071 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
3072 struct kvec iov
[SMB2_CREATE_IOV_SIZE
];
3073 struct kvec rsp_iov
= {NULL
, 0};
3074 int resp_buftype
= CIFS_NO_BUFFER
;
3078 cifs_dbg(FYI
, "create/open\n");
3079 if (!ses
|| !server
)
3082 if (smb3_encryption_required(tcon
))
3083 flags
|= CIFS_TRANSFORM_REQ
;
3085 memset(&rqst
, 0, sizeof(struct smb_rqst
));
3086 memset(&iov
, 0, sizeof(iov
));
3088 rqst
.rq_nvec
= SMB2_CREATE_IOV_SIZE
;
3090 rc
= SMB2_open_init(tcon
, server
,
3091 &rqst
, oplock
, oparms
, path
);
3095 trace_smb3_open_enter(xid
, tcon
->tid
, tcon
->ses
->Suid
, oparms
->path
,
3096 oparms
->create_options
, oparms
->desired_access
);
3098 rc
= cifs_send_recv(xid
, ses
, server
,
3099 &rqst
, &resp_buftype
, flags
,
3101 rsp
= (struct smb2_create_rsp
*)rsp_iov
.iov_base
;
3104 cifs_stats_fail_inc(tcon
, SMB2_CREATE_HE
);
3105 if (err_iov
&& rsp
) {
3107 *buftype
= resp_buftype
;
3108 resp_buftype
= CIFS_NO_BUFFER
;
3111 trace_smb3_open_err(xid
, tcon
->tid
, ses
->Suid
,
3112 oparms
->create_options
, oparms
->desired_access
, rc
);
3113 if (rc
== -EREMCHG
) {
3114 pr_warn_once("server share %s deleted\n",
3116 tcon
->need_reconnect
= true;
3119 } else if (rsp
== NULL
) /* unlikely to happen, but safer to check */
3122 trace_smb3_open_done(xid
, rsp
->PersistentFileId
, tcon
->tid
, ses
->Suid
,
3123 oparms
->create_options
, oparms
->desired_access
);
3125 atomic_inc(&tcon
->num_remote_opens
);
3126 oparms
->fid
->persistent_fid
= rsp
->PersistentFileId
;
3127 oparms
->fid
->volatile_fid
= rsp
->VolatileFileId
;
3128 oparms
->fid
->access
= oparms
->desired_access
;
3129 #ifdef CONFIG_CIFS_DEBUG2
3130 oparms
->fid
->mid
= le64_to_cpu(rsp
->hdr
.MessageId
);
3131 #endif /* CIFS_DEBUG2 */
3134 buf
->CreationTime
= rsp
->CreationTime
;
3135 buf
->LastAccessTime
= rsp
->LastAccessTime
;
3136 buf
->LastWriteTime
= rsp
->LastWriteTime
;
3137 buf
->ChangeTime
= rsp
->ChangeTime
;
3138 buf
->AllocationSize
= rsp
->AllocationSize
;
3139 buf
->EndOfFile
= rsp
->EndofFile
;
3140 buf
->Attributes
= rsp
->FileAttributes
;
3141 buf
->NumberOfLinks
= cpu_to_le32(1);
3142 buf
->DeletePending
= 0;
3146 rc
= smb2_parse_contexts(server
, &rsp_iov
, &oparms
->fid
->epoch
,
3147 oparms
->fid
->lease_key
, oplock
, buf
, posix
);
3149 SMB2_open_free(&rqst
);
3150 free_rsp_buf(resp_buftype
, rsp
);
3155 SMB2_ioctl_init(struct cifs_tcon
*tcon
, struct TCP_Server_Info
*server
,
3156 struct smb_rqst
*rqst
,
3157 u64 persistent_fid
, u64 volatile_fid
, u32 opcode
,
3158 char *in_data
, u32 indatalen
,
3159 __u32 max_response_size
)
3161 struct smb2_ioctl_req
*req
;
3162 struct kvec
*iov
= rqst
->rq_iov
;
3163 unsigned int total_len
;
3167 rc
= smb2_ioctl_req_init(opcode
, tcon
, server
,
3168 (void **) &req
, &total_len
);
3174 * indatalen is usually small at a couple of bytes max, so
3175 * just allocate through generic pool
3177 in_data_buf
= kmemdup(in_data
, indatalen
, GFP_NOFS
);
3179 cifs_small_buf_release(req
);
3184 req
->CtlCode
= cpu_to_le32(opcode
);
3185 req
->PersistentFileId
= persistent_fid
;
3186 req
->VolatileFileId
= volatile_fid
;
3188 iov
[0].iov_base
= (char *)req
;
3190 * If no input data, the size of ioctl struct in
3191 * protocol spec still includes a 1 byte data buffer,
3192 * but if input data passed to ioctl, we do not
3193 * want to double count this, so we do not send
3194 * the dummy one byte of data in iovec[0] if sending
3195 * input data (in iovec[1]).
3198 req
->InputCount
= cpu_to_le32(indatalen
);
3199 /* do not set InputOffset if no input data */
3201 cpu_to_le32(offsetof(struct smb2_ioctl_req
, Buffer
));
3203 iov
[0].iov_len
= total_len
- 1;
3204 iov
[1].iov_base
= in_data_buf
;
3205 iov
[1].iov_len
= indatalen
;
3208 iov
[0].iov_len
= total_len
;
3211 req
->OutputOffset
= 0;
3212 req
->OutputCount
= 0; /* MBZ */
3215 * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
3216 * We Could increase default MaxOutputResponse, but that could require
3217 * more credits. Windows typically sets this smaller, but for some
3218 * ioctls it may be useful to allow server to send more. No point
3219 * limiting what the server can send as long as fits in one credit
3220 * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
3221 * to increase this limit up in the future.
3222 * Note that for snapshot queries that servers like Azure expect that
3223 * the first query be minimal size (and just used to get the number/size
3224 * of previous versions) so response size must be specified as EXACTLY
3225 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
3226 * of eight bytes. Currently that is the only case where we set max
3227 * response size smaller.
3229 req
->MaxOutputResponse
= cpu_to_le32(max_response_size
);
3230 req
->hdr
.CreditCharge
=
3231 cpu_to_le16(DIV_ROUND_UP(max(indatalen
, max_response_size
),
3232 SMB2_MAX_BUFFER_SIZE
));
3233 /* always an FSCTL (for now) */
3234 req
->Flags
= cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL
);
3236 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
3237 if (opcode
== FSCTL_VALIDATE_NEGOTIATE_INFO
)
3238 req
->hdr
.Flags
|= SMB2_FLAGS_SIGNED
;
3244 SMB2_ioctl_free(struct smb_rqst
*rqst
)
3248 if (rqst
&& rqst
->rq_iov
) {
3249 cifs_small_buf_release(rqst
->rq_iov
[0].iov_base
); /* request */
3250 for (i
= 1; i
< rqst
->rq_nvec
; i
++)
3251 if (rqst
->rq_iov
[i
].iov_base
!= smb2_padding
)
3252 kfree(rqst
->rq_iov
[i
].iov_base
);
3258 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
3261 SMB2_ioctl(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
3262 u64 volatile_fid
, u32 opcode
, char *in_data
, u32 indatalen
,
3263 u32 max_out_data_len
, char **out_data
,
3264 u32
*plen
/* returned data len */)
3266 struct smb_rqst rqst
;
3267 struct smb2_ioctl_rsp
*rsp
= NULL
;
3268 struct cifs_ses
*ses
;
3269 struct TCP_Server_Info
*server
;
3270 struct kvec iov
[SMB2_IOCTL_IOV_SIZE
];
3271 struct kvec rsp_iov
= {NULL
, 0};
3272 int resp_buftype
= CIFS_NO_BUFFER
;
3276 cifs_dbg(FYI
, "SMB2 IOCTL\n");
3278 if (out_data
!= NULL
)
3281 /* zero out returned data len, in case of error */
3292 server
= cifs_pick_channel(ses
);
3296 if (smb3_encryption_required(tcon
))
3297 flags
|= CIFS_TRANSFORM_REQ
;
3299 memset(&rqst
, 0, sizeof(struct smb_rqst
));
3300 memset(&iov
, 0, sizeof(iov
));
3302 rqst
.rq_nvec
= SMB2_IOCTL_IOV_SIZE
;
3304 rc
= SMB2_ioctl_init(tcon
, server
,
3305 &rqst
, persistent_fid
, volatile_fid
, opcode
,
3306 in_data
, indatalen
, max_out_data_len
);
3310 rc
= cifs_send_recv(xid
, ses
, server
,
3311 &rqst
, &resp_buftype
, flags
,
3313 rsp
= (struct smb2_ioctl_rsp
*)rsp_iov
.iov_base
;
3316 trace_smb3_fsctl_err(xid
, persistent_fid
, tcon
->tid
,
3317 ses
->Suid
, 0, opcode
, rc
);
3319 if ((rc
!= 0) && (rc
!= -EINVAL
) && (rc
!= -E2BIG
)) {
3320 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
3322 } else if (rc
== -EINVAL
) {
3323 if ((opcode
!= FSCTL_SRV_COPYCHUNK_WRITE
) &&
3324 (opcode
!= FSCTL_SRV_COPYCHUNK
)) {
3325 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
3328 } else if (rc
== -E2BIG
) {
3329 if (opcode
!= FSCTL_QUERY_ALLOCATED_RANGES
) {
3330 cifs_stats_fail_inc(tcon
, SMB2_IOCTL_HE
);
3335 /* check if caller wants to look at return data or just return rc */
3336 if ((plen
== NULL
) || (out_data
== NULL
))
3340 * Although unlikely to be possible for rsp to be null and rc not set,
3341 * adding check below is slightly safer long term (and quiets Coverity
3349 *plen
= le32_to_cpu(rsp
->OutputCount
);
3351 /* We check for obvious errors in the output buffer length and offset */
3353 goto ioctl_exit
; /* server returned no data */
3354 else if (*plen
> rsp_iov
.iov_len
|| *plen
> 0xFF00) {
3355 cifs_tcon_dbg(VFS
, "srv returned invalid ioctl length: %d\n", *plen
);
3361 if (rsp_iov
.iov_len
- *plen
< le32_to_cpu(rsp
->OutputOffset
)) {
3362 cifs_tcon_dbg(VFS
, "Malformed ioctl resp: len %d offset %d\n", *plen
,
3363 le32_to_cpu(rsp
->OutputOffset
));
3369 *out_data
= kmemdup((char *)rsp
+ le32_to_cpu(rsp
->OutputOffset
),
3371 if (*out_data
== NULL
) {
3377 SMB2_ioctl_free(&rqst
);
3378 free_rsp_buf(resp_buftype
, rsp
);
3383 * Individual callers to ioctl worker function follow
3387 SMB2_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
3388 u64 persistent_fid
, u64 volatile_fid
)
3391 struct compress_ioctl fsctl_input
;
3392 char *ret_data
= NULL
;
3394 fsctl_input
.CompressionState
=
3395 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT
);
3397 rc
= SMB2_ioctl(xid
, tcon
, persistent_fid
, volatile_fid
,
3398 FSCTL_SET_COMPRESSION
,
3399 (char *)&fsctl_input
/* data input */,
3400 2 /* in data len */, CIFSMaxBufSize
/* max out data */,
3401 &ret_data
/* out data */, NULL
);
3403 cifs_dbg(FYI
, "set compression rc %d\n", rc
);
3409 SMB2_close_init(struct cifs_tcon
*tcon
, struct TCP_Server_Info
*server
,
3410 struct smb_rqst
*rqst
,
3411 u64 persistent_fid
, u64 volatile_fid
, bool query_attrs
)
3413 struct smb2_close_req
*req
;
3414 struct kvec
*iov
= rqst
->rq_iov
;
3415 unsigned int total_len
;
3418 rc
= smb2_plain_req_init(SMB2_CLOSE
, tcon
, server
,
3419 (void **) &req
, &total_len
);
3423 req
->PersistentFileId
= persistent_fid
;
3424 req
->VolatileFileId
= volatile_fid
;
3426 req
->Flags
= SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB
;
3429 iov
[0].iov_base
= (char *)req
;
3430 iov
[0].iov_len
= total_len
;
3436 SMB2_close_free(struct smb_rqst
*rqst
)
3438 if (rqst
&& rqst
->rq_iov
)
3439 cifs_small_buf_release(rqst
->rq_iov
[0].iov_base
); /* request */
3443 __SMB2_close(const unsigned int xid
, struct cifs_tcon
*tcon
,
3444 u64 persistent_fid
, u64 volatile_fid
,
3445 struct smb2_file_network_open_info
*pbuf
)
3447 struct smb_rqst rqst
;
3448 struct smb2_close_rsp
*rsp
= NULL
;
3449 struct cifs_ses
*ses
= tcon
->ses
;
3450 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
3452 struct kvec rsp_iov
;
3453 int resp_buftype
= CIFS_NO_BUFFER
;
3456 bool query_attrs
= false;
3458 cifs_dbg(FYI
, "Close\n");
3460 if (!ses
|| !server
)
3463 if (smb3_encryption_required(tcon
))
3464 flags
|= CIFS_TRANSFORM_REQ
;
3466 memset(&rqst
, 0, sizeof(struct smb_rqst
));
3467 memset(&iov
, 0, sizeof(iov
));
3471 /* check if need to ask server to return timestamps in close response */
3475 trace_smb3_close_enter(xid
, persistent_fid
, tcon
->tid
, ses
->Suid
);
3476 rc
= SMB2_close_init(tcon
, server
,
3477 &rqst
, persistent_fid
, volatile_fid
,
3482 rc
= cifs_send_recv(xid
, ses
, server
,
3483 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
3484 rsp
= (struct smb2_close_rsp
*)rsp_iov
.iov_base
;
3487 cifs_stats_fail_inc(tcon
, SMB2_CLOSE_HE
);
3488 trace_smb3_close_err(xid
, persistent_fid
, tcon
->tid
, ses
->Suid
,
3492 trace_smb3_close_done(xid
, persistent_fid
, tcon
->tid
,
3495 memcpy(&pbuf
->network_open_info
,
3496 &rsp
->network_open_info
,
3497 sizeof(pbuf
->network_open_info
));
3500 atomic_dec(&tcon
->num_remote_opens
);
3502 SMB2_close_free(&rqst
);
3503 free_rsp_buf(resp_buftype
, rsp
);
3505 /* retry close in a worker thread if this one is interrupted */
3506 if (is_interrupt_error(rc
)) {
3509 tmp_rc
= smb2_handle_cancelled_close(tcon
, persistent_fid
,
3512 cifs_dbg(VFS
, "handle cancelled close fid 0x%llx returned error %d\n",
3513 persistent_fid
, tmp_rc
);
3519 SMB2_close(const unsigned int xid
, struct cifs_tcon
*tcon
,
3520 u64 persistent_fid
, u64 volatile_fid
)
3522 return __SMB2_close(xid
, tcon
, persistent_fid
, volatile_fid
, NULL
);
3526 smb2_validate_iov(unsigned int offset
, unsigned int buffer_length
,
3527 struct kvec
*iov
, unsigned int min_buf_size
)
3529 unsigned int smb_len
= iov
->iov_len
;
3530 char *end_of_smb
= smb_len
+ (char *)iov
->iov_base
;
3531 char *begin_of_buf
= offset
+ (char *)iov
->iov_base
;
3532 char *end_of_buf
= begin_of_buf
+ buffer_length
;
3535 if (buffer_length
< min_buf_size
) {
3536 cifs_dbg(VFS
, "buffer length %d smaller than minimum size %d\n",
3537 buffer_length
, min_buf_size
);
3541 /* check if beyond RFC1001 maximum length */
3542 if ((smb_len
> 0x7FFFFF) || (buffer_length
> 0x7FFFFF)) {
3543 cifs_dbg(VFS
, "buffer length %d or smb length %d too large\n",
3544 buffer_length
, smb_len
);
3548 if ((begin_of_buf
> end_of_smb
) || (end_of_buf
> end_of_smb
)) {
3549 cifs_dbg(VFS
, "Invalid server response, bad offset to data\n");
3557 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
3558 * Caller must free buffer.
3561 smb2_validate_and_copy_iov(unsigned int offset
, unsigned int buffer_length
,
3562 struct kvec
*iov
, unsigned int minbufsize
,
3565 char *begin_of_buf
= offset
+ (char *)iov
->iov_base
;
3571 rc
= smb2_validate_iov(offset
, buffer_length
, iov
, minbufsize
);
3575 memcpy(data
, begin_of_buf
, minbufsize
);
3581 SMB2_query_info_init(struct cifs_tcon
*tcon
, struct TCP_Server_Info
*server
,
3582 struct smb_rqst
*rqst
,
3583 u64 persistent_fid
, u64 volatile_fid
,
3584 u8 info_class
, u8 info_type
, u32 additional_info
,
3585 size_t output_len
, size_t input_len
, void *input
)
3587 struct smb2_query_info_req
*req
;
3588 struct kvec
*iov
= rqst
->rq_iov
;
3589 unsigned int total_len
;
3592 rc
= smb2_plain_req_init(SMB2_QUERY_INFO
, tcon
, server
,
3593 (void **) &req
, &total_len
);
3597 req
->InfoType
= info_type
;
3598 req
->FileInfoClass
= info_class
;
3599 req
->PersistentFileId
= persistent_fid
;
3600 req
->VolatileFileId
= volatile_fid
;
3601 req
->AdditionalInformation
= cpu_to_le32(additional_info
);
3603 req
->OutputBufferLength
= cpu_to_le32(output_len
);
3605 req
->InputBufferLength
= cpu_to_le32(input_len
);
3606 /* total_len for smb query request never close to le16 max */
3607 req
->InputBufferOffset
= cpu_to_le16(total_len
- 1);
3608 memcpy(req
->Buffer
, input
, input_len
);
3611 iov
[0].iov_base
= (char *)req
;
3613 iov
[0].iov_len
= total_len
- 1 + input_len
;
3618 SMB2_query_info_free(struct smb_rqst
*rqst
)
3620 if (rqst
&& rqst
->rq_iov
)
3621 cifs_small_buf_release(rqst
->rq_iov
[0].iov_base
); /* request */
3625 query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3626 u64 persistent_fid
, u64 volatile_fid
, u8 info_class
, u8 info_type
,
3627 u32 additional_info
, size_t output_len
, size_t min_len
, void **data
,
3630 struct smb_rqst rqst
;
3631 struct smb2_query_info_rsp
*rsp
= NULL
;
3633 struct kvec rsp_iov
;
3635 int resp_buftype
= CIFS_NO_BUFFER
;
3636 struct cifs_ses
*ses
= tcon
->ses
;
3637 struct TCP_Server_Info
*server
;
3639 bool allocated
= false;
3641 cifs_dbg(FYI
, "Query Info\n");
3645 server
= cifs_pick_channel(ses
);
3649 if (smb3_encryption_required(tcon
))
3650 flags
|= CIFS_TRANSFORM_REQ
;
3652 memset(&rqst
, 0, sizeof(struct smb_rqst
));
3653 memset(&iov
, 0, sizeof(iov
));
3657 rc
= SMB2_query_info_init(tcon
, server
,
3658 &rqst
, persistent_fid
, volatile_fid
,
3659 info_class
, info_type
, additional_info
,
3660 output_len
, 0, NULL
);
3664 trace_smb3_query_info_enter(xid
, persistent_fid
, tcon
->tid
,
3665 ses
->Suid
, info_class
, (__u32
)info_type
);
3667 rc
= cifs_send_recv(xid
, ses
, server
,
3668 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
3669 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
3672 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
3673 trace_smb3_query_info_err(xid
, persistent_fid
, tcon
->tid
,
3674 ses
->Suid
, info_class
, (__u32
)info_type
, rc
);
3678 trace_smb3_query_info_done(xid
, persistent_fid
, tcon
->tid
,
3679 ses
->Suid
, info_class
, (__u32
)info_type
);
3682 *dlen
= le32_to_cpu(rsp
->OutputBufferLength
);
3684 *data
= kmalloc(*dlen
, GFP_KERNEL
);
3687 "Error %d allocating memory for acl\n",
3697 rc
= smb2_validate_and_copy_iov(le16_to_cpu(rsp
->OutputBufferOffset
),
3698 le32_to_cpu(rsp
->OutputBufferLength
),
3699 &rsp_iov
, dlen
? *dlen
: min_len
, *data
);
3700 if (rc
&& allocated
) {
3707 SMB2_query_info_free(&rqst
);
3708 free_rsp_buf(resp_buftype
, rsp
);
3712 int SMB2_query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3713 u64 persistent_fid
, u64 volatile_fid
, struct smb2_file_all_info
*data
)
3715 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3716 FILE_ALL_INFORMATION
, SMB2_O_INFO_FILE
, 0,
3717 sizeof(struct smb2_file_all_info
) + PATH_MAX
* 2,
3718 sizeof(struct smb2_file_all_info
), (void **)&data
,
3723 /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
3725 SMB311_posix_query_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
3726 u64 persistent_fid
, u64 volatile_fid
, struct smb311_posix_qinfo
*data
, u32
*plen
)
3728 size_t output_len
= sizeof(struct smb311_posix_qinfo
*) +
3729 (sizeof(struct cifs_sid
) * 2) + (PATH_MAX
* 2);
3732 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3733 SMB_FIND_FILE_POSIX_INFO
, SMB2_O_INFO_FILE
, 0,
3734 output_len
, sizeof(struct smb311_posix_qinfo
), (void **)&data
, plen
);
3735 /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
3740 SMB2_query_acl(const unsigned int xid
, struct cifs_tcon
*tcon
,
3741 u64 persistent_fid
, u64 volatile_fid
,
3742 void **data
, u32
*plen
, u32 extra_info
)
3744 __u32 additional_info
= OWNER_SECINFO
| GROUP_SECINFO
| DACL_SECINFO
|
3748 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3749 0, SMB2_O_INFO_SECURITY
, additional_info
,
3750 SMB2_MAX_BUFFER_SIZE
, MIN_SEC_DESC_LEN
, data
, plen
);
3754 SMB2_get_srv_num(const unsigned int xid
, struct cifs_tcon
*tcon
,
3755 u64 persistent_fid
, u64 volatile_fid
, __le64
*uniqueid
)
3757 return query_info(xid
, tcon
, persistent_fid
, volatile_fid
,
3758 FILE_INTERNAL_INFORMATION
, SMB2_O_INFO_FILE
, 0,
3759 sizeof(struct smb2_file_internal_info
),
3760 sizeof(struct smb2_file_internal_info
),
3761 (void **)&uniqueid
, NULL
);
3765 * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
3766 * See MS-SMB2 2.2.35 and 2.2.36
3770 SMB2_notify_init(const unsigned int xid
, struct smb_rqst
*rqst
,
3771 struct cifs_tcon
*tcon
, struct TCP_Server_Info
*server
,
3772 u64 persistent_fid
, u64 volatile_fid
,
3773 u32 completion_filter
, bool watch_tree
)
3775 struct smb2_change_notify_req
*req
;
3776 struct kvec
*iov
= rqst
->rq_iov
;
3777 unsigned int total_len
;
3780 rc
= smb2_plain_req_init(SMB2_CHANGE_NOTIFY
, tcon
, server
,
3781 (void **) &req
, &total_len
);
3785 req
->PersistentFileId
= persistent_fid
;
3786 req
->VolatileFileId
= volatile_fid
;
3787 /* See note 354 of MS-SMB2, 64K max */
3788 req
->OutputBufferLength
=
3789 cpu_to_le32(SMB2_MAX_BUFFER_SIZE
- MAX_SMB2_HDR_SIZE
);
3790 req
->CompletionFilter
= cpu_to_le32(completion_filter
);
3792 req
->Flags
= cpu_to_le16(SMB2_WATCH_TREE
);
3796 iov
[0].iov_base
= (char *)req
;
3797 iov
[0].iov_len
= total_len
;
3803 SMB2_change_notify(const unsigned int xid
, struct cifs_tcon
*tcon
,
3804 u64 persistent_fid
, u64 volatile_fid
, bool watch_tree
,
3805 u32 completion_filter
, u32 max_out_data_len
, char **out_data
,
3806 u32
*plen
/* returned data len */)
3808 struct cifs_ses
*ses
= tcon
->ses
;
3809 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
3810 struct smb_rqst rqst
;
3811 struct smb2_change_notify_rsp
*smb_rsp
;
3813 struct kvec rsp_iov
= {NULL
, 0};
3814 int resp_buftype
= CIFS_NO_BUFFER
;
3818 cifs_dbg(FYI
, "change notify\n");
3819 if (!ses
|| !server
)
3822 if (smb3_encryption_required(tcon
))
3823 flags
|= CIFS_TRANSFORM_REQ
;
3825 memset(&rqst
, 0, sizeof(struct smb_rqst
));
3826 memset(&iov
, 0, sizeof(iov
));
3833 rc
= SMB2_notify_init(xid
, &rqst
, tcon
, server
,
3834 persistent_fid
, volatile_fid
,
3835 completion_filter
, watch_tree
);
3839 trace_smb3_notify_enter(xid
, persistent_fid
, tcon
->tid
, ses
->Suid
,
3840 (u8
)watch_tree
, completion_filter
);
3841 rc
= cifs_send_recv(xid
, ses
, server
,
3842 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
3845 cifs_stats_fail_inc(tcon
, SMB2_CHANGE_NOTIFY_HE
);
3846 trace_smb3_notify_err(xid
, persistent_fid
, tcon
->tid
, ses
->Suid
,
3847 (u8
)watch_tree
, completion_filter
, rc
);
3849 trace_smb3_notify_done(xid
, persistent_fid
, tcon
->tid
,
3850 ses
->Suid
, (u8
)watch_tree
, completion_filter
);
3851 /* validate that notify information is plausible */
3852 if ((rsp_iov
.iov_base
== NULL
) ||
3853 (rsp_iov
.iov_len
< sizeof(struct smb2_change_notify_rsp
) + 1))
3856 smb_rsp
= (struct smb2_change_notify_rsp
*)rsp_iov
.iov_base
;
3858 smb2_validate_iov(le16_to_cpu(smb_rsp
->OutputBufferOffset
),
3859 le32_to_cpu(smb_rsp
->OutputBufferLength
), &rsp_iov
,
3860 sizeof(struct file_notify_information
));
3862 *out_data
= kmemdup((char *)smb_rsp
+ le16_to_cpu(smb_rsp
->OutputBufferOffset
),
3863 le32_to_cpu(smb_rsp
->OutputBufferLength
), GFP_KERNEL
);
3864 if (*out_data
== NULL
) {
3868 *plen
= le32_to_cpu(smb_rsp
->OutputBufferLength
);
3873 cifs_small_buf_release(rqst
.rq_iov
[0].iov_base
); /* request */
3874 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
3881 * This is a no-op for now. We're not really interested in the reply, but
3882 * rather in the fact that the server sent one and that server->lstrp
3885 * FIXME: maybe we should consider checking that the reply matches request?
3888 smb2_echo_callback(struct mid_q_entry
*mid
)
3890 struct TCP_Server_Info
*server
= mid
->callback_data
;
3891 struct smb2_echo_rsp
*rsp
= (struct smb2_echo_rsp
*)mid
->resp_buf
;
3892 struct cifs_credits credits
= { .value
= 0, .instance
= 0 };
3894 if (mid
->mid_state
== MID_RESPONSE_RECEIVED
3895 || mid
->mid_state
== MID_RESPONSE_MALFORMED
) {
3896 credits
.value
= le16_to_cpu(rsp
->hdr
.CreditRequest
);
3897 credits
.instance
= server
->reconnect_instance
;
3901 add_credits(server
, &credits
, CIFS_ECHO_OP
);
3904 void smb2_reconnect_server(struct work_struct
*work
)
3906 struct TCP_Server_Info
*server
= container_of(work
,
3907 struct TCP_Server_Info
, reconnect
.work
);
3908 struct TCP_Server_Info
*pserver
;
3909 struct cifs_ses
*ses
, *ses2
;
3910 struct cifs_tcon
*tcon
, *tcon2
;
3911 struct list_head tmp_list
, tmp_ses_list
;
3912 bool tcon_exist
= false, ses_exist
= false;
3913 bool tcon_selected
= false;
3915 bool resched
= false;
3917 /* first check if ref count has reached 0, if not inc ref count */
3918 spin_lock(&cifs_tcp_ses_lock
);
3919 if (!server
->srv_count
) {
3920 spin_unlock(&cifs_tcp_ses_lock
);
3923 server
->srv_count
++;
3924 spin_unlock(&cifs_tcp_ses_lock
);
3926 /* If server is a channel, select the primary channel */
3927 pserver
= SERVER_IS_CHAN(server
) ? server
->primary_server
: server
;
3929 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
3930 mutex_lock(&pserver
->reconnect_mutex
);
3932 /* if the server is marked for termination, drop the ref count here */
3933 if (server
->terminate
) {
3934 cifs_put_tcp_session(server
, true);
3935 mutex_unlock(&pserver
->reconnect_mutex
);
3939 INIT_LIST_HEAD(&tmp_list
);
3940 INIT_LIST_HEAD(&tmp_ses_list
);
3941 cifs_dbg(FYI
, "Reconnecting tcons and channels\n");
3943 spin_lock(&cifs_tcp_ses_lock
);
3944 list_for_each_entry(ses
, &pserver
->smb_ses_list
, smb_ses_list
) {
3945 spin_lock(&ses
->ses_lock
);
3946 if (ses
->ses_status
== SES_EXITING
) {
3947 spin_unlock(&ses
->ses_lock
);
3950 spin_unlock(&ses
->ses_lock
);
3952 tcon_selected
= false;
3954 list_for_each_entry(tcon
, &ses
->tcon_list
, tcon_list
) {
3955 if (tcon
->need_reconnect
|| tcon
->need_reopen_files
) {
3957 list_add_tail(&tcon
->rlist
, &tmp_list
);
3958 tcon_selected
= tcon_exist
= true;
3962 * IPC has the same lifetime as its session and uses its
3965 if (ses
->tcon_ipc
&& ses
->tcon_ipc
->need_reconnect
) {
3966 list_add_tail(&ses
->tcon_ipc
->rlist
, &tmp_list
);
3967 tcon_selected
= tcon_exist
= true;
3968 cifs_smb_ses_inc_refcount(ses
);
3971 * handle the case where channel needs to reconnect
3972 * binding session, but tcon is healthy (some other channel
3975 spin_lock(&ses
->chan_lock
);
3976 if (!tcon_selected
&& cifs_chan_needs_reconnect(ses
, server
)) {
3977 list_add_tail(&ses
->rlist
, &tmp_ses_list
);
3979 cifs_smb_ses_inc_refcount(ses
);
3981 spin_unlock(&ses
->chan_lock
);
3983 spin_unlock(&cifs_tcp_ses_lock
);
3985 list_for_each_entry_safe(tcon
, tcon2
, &tmp_list
, rlist
) {
3986 rc
= smb2_reconnect(SMB2_INTERNAL_CMD
, tcon
, server
, true);
3988 cifs_reopen_persistent_handles(tcon
);
3991 list_del_init(&tcon
->rlist
);
3993 cifs_put_smb_ses(tcon
->ses
);
3995 cifs_put_tcon(tcon
);
4001 /* allocate a dummy tcon struct used for reconnect */
4002 tcon
= tcon_info_alloc(false);
4005 list_for_each_entry_safe(ses
, ses2
, &tmp_ses_list
, rlist
) {
4006 list_del_init(&ses
->rlist
);
4007 cifs_put_smb_ses(ses
);
4012 tcon
->status
= TID_GOOD
;
4013 tcon
->retry
= false;
4014 tcon
->need_reconnect
= false;
4016 /* now reconnect sessions for necessary channels */
4017 list_for_each_entry_safe(ses
, ses2
, &tmp_ses_list
, rlist
) {
4019 rc
= smb2_reconnect(SMB2_INTERNAL_CMD
, tcon
, server
, true);
4022 list_del_init(&ses
->rlist
);
4023 cifs_put_smb_ses(ses
);
4028 cifs_dbg(FYI
, "Reconnecting tcons and channels finished\n");
4030 queue_delayed_work(cifsiod_wq
, &server
->reconnect
, 2 * HZ
);
4031 mutex_unlock(&pserver
->reconnect_mutex
);
4033 /* now we can safely release srv struct */
4034 cifs_put_tcp_session(server
, true);
4038 SMB2_echo(struct TCP_Server_Info
*server
)
4040 struct smb2_echo_req
*req
;
4043 struct smb_rqst rqst
= { .rq_iov
= iov
,
4045 unsigned int total_len
;
4047 cifs_dbg(FYI
, "In echo request for conn_id %lld\n", server
->conn_id
);
4049 spin_lock(&server
->srv_lock
);
4050 if (server
->ops
->need_neg
&&
4051 server
->ops
->need_neg(server
)) {
4052 spin_unlock(&server
->srv_lock
);
4053 /* No need to send echo on newly established connections */
4054 mod_delayed_work(cifsiod_wq
, &server
->reconnect
, 0);
4057 spin_unlock(&server
->srv_lock
);
4059 rc
= smb2_plain_req_init(SMB2_ECHO
, NULL
, server
,
4060 (void **)&req
, &total_len
);
4064 req
->hdr
.CreditRequest
= cpu_to_le16(1);
4066 iov
[0].iov_len
= total_len
;
4067 iov
[0].iov_base
= (char *)req
;
4069 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_echo_callback
, NULL
,
4070 server
, CIFS_ECHO_OP
, NULL
);
4072 cifs_dbg(FYI
, "Echo request failed: %d\n", rc
);
4074 cifs_small_buf_release(req
);
4079 SMB2_flush_free(struct smb_rqst
*rqst
)
4081 if (rqst
&& rqst
->rq_iov
)
4082 cifs_small_buf_release(rqst
->rq_iov
[0].iov_base
); /* request */
4086 SMB2_flush_init(const unsigned int xid
, struct smb_rqst
*rqst
,
4087 struct cifs_tcon
*tcon
, struct TCP_Server_Info
*server
,
4088 u64 persistent_fid
, u64 volatile_fid
)
4090 struct smb2_flush_req
*req
;
4091 struct kvec
*iov
= rqst
->rq_iov
;
4092 unsigned int total_len
;
4095 rc
= smb2_plain_req_init(SMB2_FLUSH
, tcon
, server
,
4096 (void **) &req
, &total_len
);
4100 req
->PersistentFileId
= persistent_fid
;
4101 req
->VolatileFileId
= volatile_fid
;
4103 iov
[0].iov_base
= (char *)req
;
4104 iov
[0].iov_len
= total_len
;
4110 SMB2_flush(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
4113 struct cifs_ses
*ses
= tcon
->ses
;
4114 struct smb_rqst rqst
;
4116 struct kvec rsp_iov
= {NULL
, 0};
4117 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
4118 int resp_buftype
= CIFS_NO_BUFFER
;
4122 cifs_dbg(FYI
, "flush\n");
4123 if (!ses
|| !(ses
->server
))
4126 if (smb3_encryption_required(tcon
))
4127 flags
|= CIFS_TRANSFORM_REQ
;
4129 memset(&rqst
, 0, sizeof(struct smb_rqst
));
4130 memset(&iov
, 0, sizeof(iov
));
4134 rc
= SMB2_flush_init(xid
, &rqst
, tcon
, server
,
4135 persistent_fid
, volatile_fid
);
4139 trace_smb3_flush_enter(xid
, persistent_fid
, tcon
->tid
, ses
->Suid
);
4140 rc
= cifs_send_recv(xid
, ses
, server
,
4141 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
4144 cifs_stats_fail_inc(tcon
, SMB2_FLUSH_HE
);
4145 trace_smb3_flush_err(xid
, persistent_fid
, tcon
->tid
, ses
->Suid
,
4148 trace_smb3_flush_done(xid
, persistent_fid
, tcon
->tid
,
4152 SMB2_flush_free(&rqst
);
4153 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
4157 #ifdef CONFIG_CIFS_SMB_DIRECT
4158 static inline bool smb3_use_rdma_offload(struct cifs_io_parms
*io_parms
)
4160 struct TCP_Server_Info
*server
= io_parms
->server
;
4161 struct cifs_tcon
*tcon
= io_parms
->tcon
;
4163 /* we can only offload if we're connected */
4164 if (!server
|| !tcon
)
4167 /* we can only offload on an rdma connection */
4168 if (!server
->rdma
|| !server
->smbd_conn
)
4171 /* we don't support signed offload yet */
4175 /* we don't support encrypted offload yet */
4176 if (smb3_encryption_required(tcon
))
4179 /* offload also has its overhead, so only do it if desired */
4180 if (io_parms
->length
< server
->smbd_conn
->rdma_readwrite_threshold
)
4185 #endif /* CONFIG_CIFS_SMB_DIRECT */
4188 * To form a chain of read requests, any read requests after the first should
4189 * have the end_of_chain boolean set to true.
4192 smb2_new_read_req(void **buf
, unsigned int *total_len
,
4193 struct cifs_io_parms
*io_parms
, struct cifs_readdata
*rdata
,
4194 unsigned int remaining_bytes
, int request_type
)
4197 struct smb2_read_req
*req
= NULL
;
4198 struct smb2_hdr
*shdr
;
4199 struct TCP_Server_Info
*server
= io_parms
->server
;
4201 rc
= smb2_plain_req_init(SMB2_READ
, io_parms
->tcon
, server
,
4202 (void **) &req
, total_len
);
4207 return -ECONNABORTED
;
4210 shdr
->Id
.SyncId
.ProcessId
= cpu_to_le32(io_parms
->pid
);
4212 req
->PersistentFileId
= io_parms
->persistent_fid
;
4213 req
->VolatileFileId
= io_parms
->volatile_fid
;
4214 req
->ReadChannelInfoOffset
= 0; /* reserved */
4215 req
->ReadChannelInfoLength
= 0; /* reserved */
4216 req
->Channel
= 0; /* reserved */
4217 req
->MinimumCount
= 0;
4218 req
->Length
= cpu_to_le32(io_parms
->length
);
4219 req
->Offset
= cpu_to_le64(io_parms
->offset
);
4221 trace_smb3_read_enter(0 /* xid */,
4222 io_parms
->persistent_fid
,
4223 io_parms
->tcon
->tid
, io_parms
->tcon
->ses
->Suid
,
4224 io_parms
->offset
, io_parms
->length
);
4225 #ifdef CONFIG_CIFS_SMB_DIRECT
4227 * If we want to do a RDMA write, fill in and append
4228 * smbd_buffer_descriptor_v1 to the end of read request
4230 if (smb3_use_rdma_offload(io_parms
)) {
4231 struct smbd_buffer_descriptor_v1
*v1
;
4232 bool need_invalidate
= server
->dialect
== SMB30_PROT_ID
;
4234 rdata
->mr
= smbd_register_mr(server
->smbd_conn
, &rdata
->iter
,
4235 true, need_invalidate
);
4239 req
->Channel
= SMB2_CHANNEL_RDMA_V1_INVALIDATE
;
4240 if (need_invalidate
)
4241 req
->Channel
= SMB2_CHANNEL_RDMA_V1
;
4242 req
->ReadChannelInfoOffset
=
4243 cpu_to_le16(offsetof(struct smb2_read_req
, Buffer
));
4244 req
->ReadChannelInfoLength
=
4245 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1
));
4246 v1
= (struct smbd_buffer_descriptor_v1
*) &req
->Buffer
[0];
4247 v1
->offset
= cpu_to_le64(rdata
->mr
->mr
->iova
);
4248 v1
->token
= cpu_to_le32(rdata
->mr
->mr
->rkey
);
4249 v1
->length
= cpu_to_le32(rdata
->mr
->mr
->length
);
4251 *total_len
+= sizeof(*v1
) - 1;
4254 if (request_type
& CHAINED_REQUEST
) {
4255 if (!(request_type
& END_OF_CHAIN
)) {
4256 /* next 8-byte aligned request */
4257 *total_len
= ALIGN(*total_len
, 8);
4258 shdr
->NextCommand
= cpu_to_le32(*total_len
);
4259 } else /* END_OF_CHAIN */
4260 shdr
->NextCommand
= 0;
4261 if (request_type
& RELATED_REQUEST
) {
4262 shdr
->Flags
|= SMB2_FLAGS_RELATED_OPERATIONS
;
4264 * Related requests use info from previous read request
4267 shdr
->SessionId
= cpu_to_le64(0xFFFFFFFFFFFFFFFF);
4268 shdr
->Id
.SyncId
.TreeId
= cpu_to_le32(0xFFFFFFFF);
4269 req
->PersistentFileId
= (u64
)-1;
4270 req
->VolatileFileId
= (u64
)-1;
4273 if (remaining_bytes
> io_parms
->length
)
4274 req
->RemainingBytes
= cpu_to_le32(remaining_bytes
);
4276 req
->RemainingBytes
= 0;
4283 smb2_readv_callback(struct mid_q_entry
*mid
)
4285 struct cifs_readdata
*rdata
= mid
->callback_data
;
4286 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
4287 struct TCP_Server_Info
*server
= rdata
->server
;
4288 struct smb2_hdr
*shdr
=
4289 (struct smb2_hdr
*)rdata
->iov
[0].iov_base
;
4290 struct cifs_credits credits
= { .value
= 0, .instance
= 0 };
4291 struct smb_rqst rqst
= { .rq_iov
= &rdata
->iov
[1], .rq_nvec
= 1 };
4293 if (rdata
->got_bytes
) {
4294 rqst
.rq_iter
= rdata
->iter
;
4295 rqst
.rq_iter_size
= iov_iter_count(&rdata
->iter
);
4298 WARN_ONCE(rdata
->server
!= mid
->server
,
4299 "rdata server %p != mid server %p",
4300 rdata
->server
, mid
->server
);
4302 cifs_dbg(FYI
, "%s: mid=%llu state=%d result=%d bytes=%u\n",
4303 __func__
, mid
->mid
, mid
->mid_state
, rdata
->result
,
4306 switch (mid
->mid_state
) {
4307 case MID_RESPONSE_RECEIVED
:
4308 credits
.value
= le16_to_cpu(shdr
->CreditRequest
);
4309 credits
.instance
= server
->reconnect_instance
;
4310 /* result already set, check signature */
4311 if (server
->sign
&& !mid
->decrypted
) {
4314 iov_iter_revert(&rqst
.rq_iter
, rdata
->got_bytes
);
4315 iov_iter_truncate(&rqst
.rq_iter
, rdata
->got_bytes
);
4316 rc
= smb2_verify_signature(&rqst
, server
);
4318 cifs_tcon_dbg(VFS
, "SMB signature verification returned error = %d\n",
4321 /* FIXME: should this be counted toward the initiating task? */
4322 task_io_account_read(rdata
->got_bytes
);
4323 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
4325 case MID_REQUEST_SUBMITTED
:
4326 case MID_RETRY_NEEDED
:
4327 rdata
->result
= -EAGAIN
;
4328 if (server
->sign
&& rdata
->got_bytes
)
4329 /* reset bytes number since we can not check a sign */
4330 rdata
->got_bytes
= 0;
4331 /* FIXME: should this be counted toward the initiating task? */
4332 task_io_account_read(rdata
->got_bytes
);
4333 cifs_stats_bytes_read(tcon
, rdata
->got_bytes
);
4335 case MID_RESPONSE_MALFORMED
:
4336 credits
.value
= le16_to_cpu(shdr
->CreditRequest
);
4337 credits
.instance
= server
->reconnect_instance
;
4340 rdata
->result
= -EIO
;
4342 #ifdef CONFIG_CIFS_SMB_DIRECT
4344 * If this rdata has a memmory registered, the MR can be freed
4345 * MR needs to be freed as soon as I/O finishes to prevent deadlock
4346 * because they have limited number and are used for future I/Os
4349 smbd_deregister_mr(rdata
->mr
);
4353 if (rdata
->result
&& rdata
->result
!= -ENODATA
) {
4354 cifs_stats_fail_inc(tcon
, SMB2_READ_HE
);
4355 trace_smb3_read_err(0 /* xid */,
4356 rdata
->cfile
->fid
.persistent_fid
,
4357 tcon
->tid
, tcon
->ses
->Suid
, rdata
->offset
,
4358 rdata
->bytes
, rdata
->result
);
4360 trace_smb3_read_done(0 /* xid */,
4361 rdata
->cfile
->fid
.persistent_fid
,
4362 tcon
->tid
, tcon
->ses
->Suid
,
4363 rdata
->offset
, rdata
->got_bytes
);
4365 queue_work(cifsiod_wq
, &rdata
->work
);
4367 add_credits(server
, &credits
, 0);
4370 /* smb2_async_readv - send an async read, and set up mid to handle result */
4372 smb2_async_readv(struct cifs_readdata
*rdata
)
4376 struct smb2_hdr
*shdr
;
4377 struct cifs_io_parms io_parms
;
4378 struct smb_rqst rqst
= { .rq_iov
= rdata
->iov
,
4380 struct TCP_Server_Info
*server
;
4381 struct cifs_tcon
*tcon
= tlink_tcon(rdata
->cfile
->tlink
);
4382 unsigned int total_len
;
4385 cifs_dbg(FYI
, "%s: offset=%llu bytes=%u\n",
4386 __func__
, rdata
->offset
, rdata
->bytes
);
4389 rdata
->server
= cifs_pick_channel(tcon
->ses
);
4391 io_parms
.tcon
= tlink_tcon(rdata
->cfile
->tlink
);
4392 io_parms
.server
= server
= rdata
->server
;
4393 io_parms
.offset
= rdata
->offset
;
4394 io_parms
.length
= rdata
->bytes
;
4395 io_parms
.persistent_fid
= rdata
->cfile
->fid
.persistent_fid
;
4396 io_parms
.volatile_fid
= rdata
->cfile
->fid
.volatile_fid
;
4397 io_parms
.pid
= rdata
->pid
;
4399 rc
= smb2_new_read_req(
4400 (void **) &buf
, &total_len
, &io_parms
, rdata
, 0, 0);
4404 if (smb3_encryption_required(io_parms
.tcon
))
4405 flags
|= CIFS_TRANSFORM_REQ
;
4407 rdata
->iov
[0].iov_base
= buf
;
4408 rdata
->iov
[0].iov_len
= total_len
;
4410 shdr
= (struct smb2_hdr
*)buf
;
4412 if (rdata
->credits
.value
> 0) {
4413 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(rdata
->bytes
,
4414 SMB2_MAX_BUFFER_SIZE
));
4415 credit_request
= le16_to_cpu(shdr
->CreditCharge
) + 8;
4416 if (server
->credits
>= server
->max_credits
)
4417 shdr
->CreditRequest
= cpu_to_le16(0);
4419 shdr
->CreditRequest
= cpu_to_le16(
4420 min_t(int, server
->max_credits
-
4421 server
->credits
, credit_request
));
4423 rc
= adjust_credits(server
, &rdata
->credits
, rdata
->bytes
);
4425 goto async_readv_out
;
4427 flags
|= CIFS_HAS_CREDITS
;
4430 kref_get(&rdata
->refcount
);
4431 rc
= cifs_call_async(server
, &rqst
,
4432 cifs_readv_receive
, smb2_readv_callback
,
4433 smb3_handle_read_data
, rdata
, flags
,
4436 kref_put(&rdata
->refcount
, cifs_readdata_release
);
4437 cifs_stats_fail_inc(io_parms
.tcon
, SMB2_READ_HE
);
4438 trace_smb3_read_err(0 /* xid */, io_parms
.persistent_fid
,
4440 io_parms
.tcon
->ses
->Suid
,
4441 io_parms
.offset
, io_parms
.length
, rc
);
4445 cifs_small_buf_release(buf
);
4450 SMB2_read(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
4451 unsigned int *nbytes
, char **buf
, int *buf_type
)
4453 struct smb_rqst rqst
;
4454 int resp_buftype
, rc
;
4455 struct smb2_read_req
*req
= NULL
;
4456 struct smb2_read_rsp
*rsp
= NULL
;
4458 struct kvec rsp_iov
;
4459 unsigned int total_len
;
4460 int flags
= CIFS_LOG_ERROR
;
4461 struct cifs_ses
*ses
= io_parms
->tcon
->ses
;
4463 if (!io_parms
->server
)
4464 io_parms
->server
= cifs_pick_channel(io_parms
->tcon
->ses
);
4467 rc
= smb2_new_read_req((void **)&req
, &total_len
, io_parms
, NULL
, 0, 0);
4471 if (smb3_encryption_required(io_parms
->tcon
))
4472 flags
|= CIFS_TRANSFORM_REQ
;
4474 iov
[0].iov_base
= (char *)req
;
4475 iov
[0].iov_len
= total_len
;
4477 memset(&rqst
, 0, sizeof(struct smb_rqst
));
4481 rc
= cifs_send_recv(xid
, ses
, io_parms
->server
,
4482 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
4483 rsp
= (struct smb2_read_rsp
*)rsp_iov
.iov_base
;
4486 if (rc
!= -ENODATA
) {
4487 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_READ_HE
);
4488 cifs_dbg(VFS
, "Send error in read = %d\n", rc
);
4489 trace_smb3_read_err(xid
,
4490 req
->PersistentFileId
,
4491 io_parms
->tcon
->tid
, ses
->Suid
,
4492 io_parms
->offset
, io_parms
->length
,
4495 trace_smb3_read_done(xid
, req
->PersistentFileId
, io_parms
->tcon
->tid
,
4496 ses
->Suid
, io_parms
->offset
, 0);
4497 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
4498 cifs_small_buf_release(req
);
4499 return rc
== -ENODATA
? 0 : rc
;
4501 trace_smb3_read_done(xid
,
4502 req
->PersistentFileId
,
4503 io_parms
->tcon
->tid
, ses
->Suid
,
4504 io_parms
->offset
, io_parms
->length
);
4506 cifs_small_buf_release(req
);
4508 *nbytes
= le32_to_cpu(rsp
->DataLength
);
4509 if ((*nbytes
> CIFS_MAX_MSGSIZE
) ||
4510 (*nbytes
> io_parms
->length
)) {
4511 cifs_dbg(FYI
, "bad length %d for count %d\n",
4512 *nbytes
, io_parms
->length
);
4518 memcpy(*buf
, (char *)rsp
+ rsp
->DataOffset
, *nbytes
);
4519 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
4520 } else if (resp_buftype
!= CIFS_NO_BUFFER
) {
4521 *buf
= rsp_iov
.iov_base
;
4522 if (resp_buftype
== CIFS_SMALL_BUFFER
)
4523 *buf_type
= CIFS_SMALL_BUFFER
;
4524 else if (resp_buftype
== CIFS_LARGE_BUFFER
)
4525 *buf_type
= CIFS_LARGE_BUFFER
;
4531 * Check the mid_state and signature on received buffer (if any), and queue the
4532 * workqueue completion task.
4535 smb2_writev_callback(struct mid_q_entry
*mid
)
4537 struct cifs_writedata
*wdata
= mid
->callback_data
;
4538 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
4539 struct TCP_Server_Info
*server
= wdata
->server
;
4540 unsigned int written
;
4541 struct smb2_write_rsp
*rsp
= (struct smb2_write_rsp
*)mid
->resp_buf
;
4542 struct cifs_credits credits
= { .value
= 0, .instance
= 0 };
4544 WARN_ONCE(wdata
->server
!= mid
->server
,
4545 "wdata server %p != mid server %p",
4546 wdata
->server
, mid
->server
);
4548 switch (mid
->mid_state
) {
4549 case MID_RESPONSE_RECEIVED
:
4550 credits
.value
= le16_to_cpu(rsp
->hdr
.CreditRequest
);
4551 credits
.instance
= server
->reconnect_instance
;
4552 wdata
->result
= smb2_check_receive(mid
, server
, 0);
4553 if (wdata
->result
!= 0)
4556 written
= le32_to_cpu(rsp
->DataLength
);
4558 * Mask off high 16 bits when bytes written as returned
4559 * by the server is greater than bytes requested by the
4560 * client. OS/2 servers are known to set incorrect
4563 if (written
> wdata
->bytes
)
4566 if (written
< wdata
->bytes
)
4567 wdata
->result
= -ENOSPC
;
4569 wdata
->bytes
= written
;
4571 case MID_REQUEST_SUBMITTED
:
4572 case MID_RETRY_NEEDED
:
4573 wdata
->result
= -EAGAIN
;
4575 case MID_RESPONSE_MALFORMED
:
4576 credits
.value
= le16_to_cpu(rsp
->hdr
.CreditRequest
);
4577 credits
.instance
= server
->reconnect_instance
;
4580 wdata
->result
= -EIO
;
4583 #ifdef CONFIG_CIFS_SMB_DIRECT
4585 * If this wdata has a memory registered, the MR can be freed
4586 * The number of MRs available is limited, it's important to recover
4587 * used MR as soon as I/O is finished. Hold MR longer in the later
4588 * I/O process can possibly result in I/O deadlock due to lack of MR
4589 * to send request on I/O retry
4592 smbd_deregister_mr(wdata
->mr
);
4596 if (wdata
->result
) {
4597 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
4598 trace_smb3_write_err(0 /* no xid */,
4599 wdata
->cfile
->fid
.persistent_fid
,
4600 tcon
->tid
, tcon
->ses
->Suid
, wdata
->offset
,
4601 wdata
->bytes
, wdata
->result
);
4602 if (wdata
->result
== -ENOSPC
)
4603 pr_warn_once("Out of space writing to %s\n",
4606 trace_smb3_write_done(0 /* no xid */,
4607 wdata
->cfile
->fid
.persistent_fid
,
4608 tcon
->tid
, tcon
->ses
->Suid
,
4609 wdata
->offset
, wdata
->bytes
);
4611 queue_work(cifsiod_wq
, &wdata
->work
);
4613 add_credits(server
, &credits
, 0);
4616 /* smb2_async_writev - send an async write, and set up mid to handle result */
4618 smb2_async_writev(struct cifs_writedata
*wdata
,
4619 void (*release
)(struct kref
*kref
))
4621 int rc
= -EACCES
, flags
= 0;
4622 struct smb2_write_req
*req
= NULL
;
4623 struct smb2_hdr
*shdr
;
4624 struct cifs_tcon
*tcon
= tlink_tcon(wdata
->cfile
->tlink
);
4625 struct TCP_Server_Info
*server
= wdata
->server
;
4627 struct smb_rqst rqst
= { };
4628 unsigned int total_len
;
4629 struct cifs_io_parms _io_parms
;
4630 struct cifs_io_parms
*io_parms
= NULL
;
4634 server
= wdata
->server
= cifs_pick_channel(tcon
->ses
);
4637 * in future we may get cifs_io_parms passed in from the caller,
4638 * but for now we construct it here...
4640 _io_parms
= (struct cifs_io_parms
) {
4643 .offset
= wdata
->offset
,
4644 .length
= wdata
->bytes
,
4645 .persistent_fid
= wdata
->cfile
->fid
.persistent_fid
,
4646 .volatile_fid
= wdata
->cfile
->fid
.volatile_fid
,
4649 io_parms
= &_io_parms
;
4651 rc
= smb2_plain_req_init(SMB2_WRITE
, tcon
, server
,
4652 (void **) &req
, &total_len
);
4656 if (smb3_encryption_required(tcon
))
4657 flags
|= CIFS_TRANSFORM_REQ
;
4659 shdr
= (struct smb2_hdr
*)req
;
4660 shdr
->Id
.SyncId
.ProcessId
= cpu_to_le32(io_parms
->pid
);
4662 req
->PersistentFileId
= io_parms
->persistent_fid
;
4663 req
->VolatileFileId
= io_parms
->volatile_fid
;
4664 req
->WriteChannelInfoOffset
= 0;
4665 req
->WriteChannelInfoLength
= 0;
4666 req
->Channel
= SMB2_CHANNEL_NONE
;
4667 req
->Offset
= cpu_to_le64(io_parms
->offset
);
4668 req
->DataOffset
= cpu_to_le16(
4669 offsetof(struct smb2_write_req
, Buffer
));
4670 req
->RemainingBytes
= 0;
4672 trace_smb3_write_enter(0 /* xid */,
4673 io_parms
->persistent_fid
,
4674 io_parms
->tcon
->tid
,
4675 io_parms
->tcon
->ses
->Suid
,
4679 #ifdef CONFIG_CIFS_SMB_DIRECT
4681 * If we want to do a server RDMA read, fill in and append
4682 * smbd_buffer_descriptor_v1 to the end of write request
4684 if (smb3_use_rdma_offload(io_parms
)) {
4685 struct smbd_buffer_descriptor_v1
*v1
;
4686 size_t data_size
= iov_iter_count(&wdata
->iter
);
4687 bool need_invalidate
= server
->dialect
== SMB30_PROT_ID
;
4689 wdata
->mr
= smbd_register_mr(server
->smbd_conn
, &wdata
->iter
,
4690 false, need_invalidate
);
4693 goto async_writev_out
;
4696 req
->DataOffset
= 0;
4697 req
->RemainingBytes
= cpu_to_le32(data_size
);
4698 req
->Channel
= SMB2_CHANNEL_RDMA_V1_INVALIDATE
;
4699 if (need_invalidate
)
4700 req
->Channel
= SMB2_CHANNEL_RDMA_V1
;
4701 req
->WriteChannelInfoOffset
=
4702 cpu_to_le16(offsetof(struct smb2_write_req
, Buffer
));
4703 req
->WriteChannelInfoLength
=
4704 cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1
));
4705 v1
= (struct smbd_buffer_descriptor_v1
*) &req
->Buffer
[0];
4706 v1
->offset
= cpu_to_le64(wdata
->mr
->mr
->iova
);
4707 v1
->token
= cpu_to_le32(wdata
->mr
->mr
->rkey
);
4708 v1
->length
= cpu_to_le32(wdata
->mr
->mr
->length
);
4711 iov
[0].iov_len
= total_len
- 1;
4712 iov
[0].iov_base
= (char *)req
;
4716 rqst
.rq_iter
= wdata
->iter
;
4717 rqst
.rq_iter_size
= iov_iter_count(&rqst
.rq_iter
);
4718 #ifdef CONFIG_CIFS_SMB_DIRECT
4720 iov
[0].iov_len
+= sizeof(struct smbd_buffer_descriptor_v1
);
4722 cifs_dbg(FYI
, "async write at %llu %u bytes iter=%zx\n",
4723 io_parms
->offset
, io_parms
->length
, iov_iter_count(&rqst
.rq_iter
));
4725 #ifdef CONFIG_CIFS_SMB_DIRECT
4726 /* For RDMA read, I/O size is in RemainingBytes not in Length */
4728 req
->Length
= cpu_to_le32(io_parms
->length
);
4730 req
->Length
= cpu_to_le32(io_parms
->length
);
4733 if (wdata
->credits
.value
> 0) {
4734 shdr
->CreditCharge
= cpu_to_le16(DIV_ROUND_UP(wdata
->bytes
,
4735 SMB2_MAX_BUFFER_SIZE
));
4736 credit_request
= le16_to_cpu(shdr
->CreditCharge
) + 8;
4737 if (server
->credits
>= server
->max_credits
)
4738 shdr
->CreditRequest
= cpu_to_le16(0);
4740 shdr
->CreditRequest
= cpu_to_le16(
4741 min_t(int, server
->max_credits
-
4742 server
->credits
, credit_request
));
4744 rc
= adjust_credits(server
, &wdata
->credits
, io_parms
->length
);
4746 goto async_writev_out
;
4748 flags
|= CIFS_HAS_CREDITS
;
4751 kref_get(&wdata
->refcount
);
4752 rc
= cifs_call_async(server
, &rqst
, NULL
, smb2_writev_callback
, NULL
,
4753 wdata
, flags
, &wdata
->credits
);
4756 trace_smb3_write_err(0 /* no xid */,
4757 io_parms
->persistent_fid
,
4758 io_parms
->tcon
->tid
,
4759 io_parms
->tcon
->ses
->Suid
,
4763 kref_put(&wdata
->refcount
, release
);
4764 cifs_stats_fail_inc(tcon
, SMB2_WRITE_HE
);
4768 cifs_small_buf_release(req
);
4773 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
4774 * The length field from io_parms must be at least 1 and indicates a number of
4775 * elements with data to write that begins with position 1 in iov array. All
4776 * data length is specified by count.
4779 SMB2_write(const unsigned int xid
, struct cifs_io_parms
*io_parms
,
4780 unsigned int *nbytes
, struct kvec
*iov
, int n_vec
)
4782 struct smb_rqst rqst
;
4784 struct smb2_write_req
*req
= NULL
;
4785 struct smb2_write_rsp
*rsp
= NULL
;
4787 struct kvec rsp_iov
;
4789 unsigned int total_len
;
4790 struct TCP_Server_Info
*server
;
4797 if (!io_parms
->server
)
4798 io_parms
->server
= cifs_pick_channel(io_parms
->tcon
->ses
);
4799 server
= io_parms
->server
;
4801 return -ECONNABORTED
;
4803 rc
= smb2_plain_req_init(SMB2_WRITE
, io_parms
->tcon
, server
,
4804 (void **) &req
, &total_len
);
4808 if (smb3_encryption_required(io_parms
->tcon
))
4809 flags
|= CIFS_TRANSFORM_REQ
;
4811 req
->hdr
.Id
.SyncId
.ProcessId
= cpu_to_le32(io_parms
->pid
);
4813 req
->PersistentFileId
= io_parms
->persistent_fid
;
4814 req
->VolatileFileId
= io_parms
->volatile_fid
;
4815 req
->WriteChannelInfoOffset
= 0;
4816 req
->WriteChannelInfoLength
= 0;
4818 req
->Length
= cpu_to_le32(io_parms
->length
);
4819 req
->Offset
= cpu_to_le64(io_parms
->offset
);
4820 req
->DataOffset
= cpu_to_le16(
4821 offsetof(struct smb2_write_req
, Buffer
));
4822 req
->RemainingBytes
= 0;
4824 trace_smb3_write_enter(xid
, io_parms
->persistent_fid
,
4825 io_parms
->tcon
->tid
, io_parms
->tcon
->ses
->Suid
,
4826 io_parms
->offset
, io_parms
->length
);
4828 iov
[0].iov_base
= (char *)req
;
4830 iov
[0].iov_len
= total_len
- 1;
4832 memset(&rqst
, 0, sizeof(struct smb_rqst
));
4834 rqst
.rq_nvec
= n_vec
+ 1;
4836 rc
= cifs_send_recv(xid
, io_parms
->tcon
->ses
, server
,
4838 &resp_buftype
, flags
, &rsp_iov
);
4839 rsp
= (struct smb2_write_rsp
*)rsp_iov
.iov_base
;
4842 trace_smb3_write_err(xid
,
4843 req
->PersistentFileId
,
4844 io_parms
->tcon
->tid
,
4845 io_parms
->tcon
->ses
->Suid
,
4846 io_parms
->offset
, io_parms
->length
, rc
);
4847 cifs_stats_fail_inc(io_parms
->tcon
, SMB2_WRITE_HE
);
4848 cifs_dbg(VFS
, "Send error in write = %d\n", rc
);
4850 *nbytes
= le32_to_cpu(rsp
->DataLength
);
4851 trace_smb3_write_done(xid
,
4852 req
->PersistentFileId
,
4853 io_parms
->tcon
->tid
,
4854 io_parms
->tcon
->ses
->Suid
,
4855 io_parms
->offset
, *nbytes
);
4858 cifs_small_buf_release(req
);
4859 free_rsp_buf(resp_buftype
, rsp
);
4863 int posix_info_sid_size(const void *beg
, const void *end
)
4871 subauth
= *(u8
*)(beg
+1);
4872 if (subauth
< 1 || subauth
> 15)
4875 total
= 1 + 1 + 6 + 4*subauth
;
4876 if (beg
+ total
> end
)
4882 int posix_info_parse(const void *beg
, const void *end
,
4883 struct smb2_posix_info_parsed
*out
)
4887 int owner_len
, group_len
;
4889 const void *owner_sid
;
4890 const void *group_sid
;
4893 /* if no end bound given, assume payload to be correct */
4895 const struct smb2_posix_info
*p
= beg
;
4897 end
= beg
+ le32_to_cpu(p
->NextEntryOffset
);
4898 /* last element will have a 0 offset, pick a sensible bound */
4903 /* check base buf */
4904 if (beg
+ sizeof(struct smb2_posix_info
) > end
)
4906 total_len
= sizeof(struct smb2_posix_info
);
4908 /* check owner sid */
4909 owner_sid
= beg
+ total_len
;
4910 owner_len
= posix_info_sid_size(owner_sid
, end
);
4913 total_len
+= owner_len
;
4915 /* check group sid */
4916 group_sid
= beg
+ total_len
;
4917 group_len
= posix_info_sid_size(group_sid
, end
);
4920 total_len
+= group_len
;
4922 /* check name len */
4923 if (beg
+ total_len
+ 4 > end
)
4925 name_len
= le32_to_cpu(*(__le32
*)(beg
+ total_len
));
4926 if (name_len
< 1 || name_len
> 0xFFFF)
4931 name
= beg
+ total_len
;
4932 if (name
+ name_len
> end
)
4934 total_len
+= name_len
;
4938 out
->size
= total_len
;
4939 out
->name_len
= name_len
;
4941 memcpy(&out
->owner
, owner_sid
, owner_len
);
4942 memcpy(&out
->group
, group_sid
, group_len
);
4947 static int posix_info_extra_size(const void *beg
, const void *end
)
4949 int len
= posix_info_parse(beg
, end
, NULL
);
4953 return len
- sizeof(struct smb2_posix_info
);
4957 num_entries(int infotype
, char *bufstart
, char *end_of_buf
, char **lastentry
,
4961 unsigned int entrycount
= 0;
4962 unsigned int next_offset
= 0;
4964 FILE_DIRECTORY_INFO
*dir_info
;
4966 if (bufstart
== NULL
)
4969 entryptr
= bufstart
;
4972 if (entryptr
+ next_offset
< entryptr
||
4973 entryptr
+ next_offset
> end_of_buf
||
4974 entryptr
+ next_offset
+ size
> end_of_buf
) {
4975 cifs_dbg(VFS
, "malformed search entry would overflow\n");
4979 entryptr
= entryptr
+ next_offset
;
4980 dir_info
= (FILE_DIRECTORY_INFO
*)entryptr
;
4982 if (infotype
== SMB_FIND_FILE_POSIX_INFO
)
4983 len
= posix_info_extra_size(entryptr
, end_of_buf
);
4985 len
= le32_to_cpu(dir_info
->FileNameLength
);
4988 entryptr
+ len
< entryptr
||
4989 entryptr
+ len
> end_of_buf
||
4990 entryptr
+ len
+ size
> end_of_buf
) {
4991 cifs_dbg(VFS
, "directory entry name would overflow frame end of buf %p\n",
4996 *lastentry
= entryptr
;
4999 next_offset
= le32_to_cpu(dir_info
->NextEntryOffset
);
5010 int SMB2_query_directory_init(const unsigned int xid
,
5011 struct cifs_tcon
*tcon
,
5012 struct TCP_Server_Info
*server
,
5013 struct smb_rqst
*rqst
,
5014 u64 persistent_fid
, u64 volatile_fid
,
5015 int index
, int info_level
)
5017 struct smb2_query_directory_req
*req
;
5018 unsigned char *bufptr
;
5019 __le16 asteriks
= cpu_to_le16('*');
5020 unsigned int output_size
= CIFSMaxBufSize
-
5021 MAX_SMB2_CREATE_RESPONSE_SIZE
-
5022 MAX_SMB2_CLOSE_RESPONSE_SIZE
;
5023 unsigned int total_len
;
5024 struct kvec
*iov
= rqst
->rq_iov
;
5027 rc
= smb2_plain_req_init(SMB2_QUERY_DIRECTORY
, tcon
, server
,
5028 (void **) &req
, &total_len
);
5032 switch (info_level
) {
5033 case SMB_FIND_FILE_DIRECTORY_INFO
:
5034 req
->FileInformationClass
= FILE_DIRECTORY_INFORMATION
;
5036 case SMB_FIND_FILE_ID_FULL_DIR_INFO
:
5037 req
->FileInformationClass
= FILEID_FULL_DIRECTORY_INFORMATION
;
5039 case SMB_FIND_FILE_POSIX_INFO
:
5040 req
->FileInformationClass
= SMB_FIND_FILE_POSIX_INFO
;
5043 cifs_tcon_dbg(VFS
, "info level %u isn't supported\n",
5048 req
->FileIndex
= cpu_to_le32(index
);
5049 req
->PersistentFileId
= persistent_fid
;
5050 req
->VolatileFileId
= volatile_fid
;
5053 bufptr
= req
->Buffer
;
5054 memcpy(bufptr
, &asteriks
, len
);
5056 req
->FileNameOffset
=
5057 cpu_to_le16(sizeof(struct smb2_query_directory_req
));
5058 req
->FileNameLength
= cpu_to_le16(len
);
5060 * BB could be 30 bytes or so longer if we used SMB2 specific
5061 * buffer lengths, but this is safe and close enough.
5063 output_size
= min_t(unsigned int, output_size
, server
->maxBuf
);
5064 output_size
= min_t(unsigned int, output_size
, 2 << 15);
5065 req
->OutputBufferLength
= cpu_to_le32(output_size
);
5067 iov
[0].iov_base
= (char *)req
;
5069 iov
[0].iov_len
= total_len
- 1;
5071 iov
[1].iov_base
= (char *)(req
->Buffer
);
5072 iov
[1].iov_len
= len
;
5074 trace_smb3_query_dir_enter(xid
, persistent_fid
, tcon
->tid
,
5075 tcon
->ses
->Suid
, index
, output_size
);
5080 void SMB2_query_directory_free(struct smb_rqst
*rqst
)
5082 if (rqst
&& rqst
->rq_iov
) {
5083 cifs_small_buf_release(rqst
->rq_iov
[0].iov_base
); /* request */
5088 smb2_parse_query_directory(struct cifs_tcon
*tcon
,
5089 struct kvec
*rsp_iov
,
5091 struct cifs_search_info
*srch_inf
)
5093 struct smb2_query_directory_rsp
*rsp
;
5094 size_t info_buf_size
;
5098 rsp
= (struct smb2_query_directory_rsp
*)rsp_iov
->iov_base
;
5100 switch (srch_inf
->info_level
) {
5101 case SMB_FIND_FILE_DIRECTORY_INFO
:
5102 info_buf_size
= sizeof(FILE_DIRECTORY_INFO
);
5104 case SMB_FIND_FILE_ID_FULL_DIR_INFO
:
5105 info_buf_size
= sizeof(SEARCH_ID_FULL_DIR_INFO
);
5107 case SMB_FIND_FILE_POSIX_INFO
:
5108 /* note that posix payload are variable size */
5109 info_buf_size
= sizeof(struct smb2_posix_info
);
5112 cifs_tcon_dbg(VFS
, "info level %u isn't supported\n",
5113 srch_inf
->info_level
);
5117 rc
= smb2_validate_iov(le16_to_cpu(rsp
->OutputBufferOffset
),
5118 le32_to_cpu(rsp
->OutputBufferLength
), rsp_iov
,
5121 cifs_tcon_dbg(VFS
, "bad info payload");
5125 srch_inf
->unicode
= true;
5127 if (srch_inf
->ntwrk_buf_start
) {
5128 if (srch_inf
->smallBuf
)
5129 cifs_small_buf_release(srch_inf
->ntwrk_buf_start
);
5131 cifs_buf_release(srch_inf
->ntwrk_buf_start
);
5133 srch_inf
->ntwrk_buf_start
= (char *)rsp
;
5134 srch_inf
->srch_entries_start
= srch_inf
->last_entry
=
5135 (char *)rsp
+ le16_to_cpu(rsp
->OutputBufferOffset
);
5136 end_of_smb
= rsp_iov
->iov_len
+ (char *)rsp
;
5138 srch_inf
->entries_in_buffer
= num_entries(
5139 srch_inf
->info_level
,
5140 srch_inf
->srch_entries_start
,
5142 &srch_inf
->last_entry
,
5145 srch_inf
->index_of_last_entry
+= srch_inf
->entries_in_buffer
;
5146 cifs_dbg(FYI
, "num entries %d last_index %lld srch start %p srch end %p\n",
5147 srch_inf
->entries_in_buffer
, srch_inf
->index_of_last_entry
,
5148 srch_inf
->srch_entries_start
, srch_inf
->last_entry
);
5149 if (resp_buftype
== CIFS_LARGE_BUFFER
)
5150 srch_inf
->smallBuf
= false;
5151 else if (resp_buftype
== CIFS_SMALL_BUFFER
)
5152 srch_inf
->smallBuf
= true;
5154 cifs_tcon_dbg(VFS
, "Invalid search buffer type\n");
5160 SMB2_query_directory(const unsigned int xid
, struct cifs_tcon
*tcon
,
5161 u64 persistent_fid
, u64 volatile_fid
, int index
,
5162 struct cifs_search_info
*srch_inf
)
5164 struct smb_rqst rqst
;
5165 struct kvec iov
[SMB2_QUERY_DIRECTORY_IOV_SIZE
];
5166 struct smb2_query_directory_rsp
*rsp
= NULL
;
5167 int resp_buftype
= CIFS_NO_BUFFER
;
5168 struct kvec rsp_iov
;
5170 struct cifs_ses
*ses
= tcon
->ses
;
5171 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
5174 if (!ses
|| !(ses
->server
))
5177 if (smb3_encryption_required(tcon
))
5178 flags
|= CIFS_TRANSFORM_REQ
;
5180 memset(&rqst
, 0, sizeof(struct smb_rqst
));
5181 memset(&iov
, 0, sizeof(iov
));
5183 rqst
.rq_nvec
= SMB2_QUERY_DIRECTORY_IOV_SIZE
;
5185 rc
= SMB2_query_directory_init(xid
, tcon
, server
,
5186 &rqst
, persistent_fid
,
5187 volatile_fid
, index
,
5188 srch_inf
->info_level
);
5192 rc
= cifs_send_recv(xid
, ses
, server
,
5193 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
5194 rsp
= (struct smb2_query_directory_rsp
*)rsp_iov
.iov_base
;
5197 if (rc
== -ENODATA
&&
5198 rsp
->hdr
.Status
== STATUS_NO_MORE_FILES
) {
5199 trace_smb3_query_dir_done(xid
, persistent_fid
,
5200 tcon
->tid
, tcon
->ses
->Suid
, index
, 0);
5201 srch_inf
->endOfSearch
= true;
5204 trace_smb3_query_dir_err(xid
, persistent_fid
, tcon
->tid
,
5205 tcon
->ses
->Suid
, index
, 0, rc
);
5206 cifs_stats_fail_inc(tcon
, SMB2_QUERY_DIRECTORY_HE
);
5211 rc
= smb2_parse_query_directory(tcon
, &rsp_iov
, resp_buftype
,
5214 trace_smb3_query_dir_err(xid
, persistent_fid
, tcon
->tid
,
5215 tcon
->ses
->Suid
, index
, 0, rc
);
5218 resp_buftype
= CIFS_NO_BUFFER
;
5220 trace_smb3_query_dir_done(xid
, persistent_fid
, tcon
->tid
,
5221 tcon
->ses
->Suid
, index
, srch_inf
->entries_in_buffer
);
5224 SMB2_query_directory_free(&rqst
);
5225 free_rsp_buf(resp_buftype
, rsp
);
5230 SMB2_set_info_init(struct cifs_tcon
*tcon
, struct TCP_Server_Info
*server
,
5231 struct smb_rqst
*rqst
,
5232 u64 persistent_fid
, u64 volatile_fid
, u32 pid
,
5233 u8 info_class
, u8 info_type
, u32 additional_info
,
5234 void **data
, unsigned int *size
)
5236 struct smb2_set_info_req
*req
;
5237 struct kvec
*iov
= rqst
->rq_iov
;
5238 unsigned int i
, total_len
;
5241 rc
= smb2_plain_req_init(SMB2_SET_INFO
, tcon
, server
,
5242 (void **) &req
, &total_len
);
5246 req
->hdr
.Id
.SyncId
.ProcessId
= cpu_to_le32(pid
);
5247 req
->InfoType
= info_type
;
5248 req
->FileInfoClass
= info_class
;
5249 req
->PersistentFileId
= persistent_fid
;
5250 req
->VolatileFileId
= volatile_fid
;
5251 req
->AdditionalInformation
= cpu_to_le32(additional_info
);
5253 req
->BufferOffset
= cpu_to_le16(sizeof(struct smb2_set_info_req
));
5254 req
->BufferLength
= cpu_to_le32(*size
);
5256 memcpy(req
->Buffer
, *data
, *size
);
5259 iov
[0].iov_base
= (char *)req
;
5261 iov
[0].iov_len
= total_len
- 1;
5263 for (i
= 1; i
< rqst
->rq_nvec
; i
++) {
5264 le32_add_cpu(&req
->BufferLength
, size
[i
]);
5265 iov
[i
].iov_base
= (char *)data
[i
];
5266 iov
[i
].iov_len
= size
[i
];
5273 SMB2_set_info_free(struct smb_rqst
*rqst
)
5275 if (rqst
&& rqst
->rq_iov
)
5276 cifs_buf_release(rqst
->rq_iov
[0].iov_base
); /* request */
5280 send_set_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
5281 u64 persistent_fid
, u64 volatile_fid
, u32 pid
, u8 info_class
,
5282 u8 info_type
, u32 additional_info
, unsigned int num
,
5283 void **data
, unsigned int *size
)
5285 struct smb_rqst rqst
;
5286 struct smb2_set_info_rsp
*rsp
= NULL
;
5288 struct kvec rsp_iov
;
5291 struct cifs_ses
*ses
= tcon
->ses
;
5292 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
5295 if (!ses
|| !server
)
5301 if (smb3_encryption_required(tcon
))
5302 flags
|= CIFS_TRANSFORM_REQ
;
5304 iov
= kmalloc_array(num
, sizeof(struct kvec
), GFP_KERNEL
);
5308 memset(&rqst
, 0, sizeof(struct smb_rqst
));
5312 rc
= SMB2_set_info_init(tcon
, server
,
5313 &rqst
, persistent_fid
, volatile_fid
, pid
,
5314 info_class
, info_type
, additional_info
,
5322 rc
= cifs_send_recv(xid
, ses
, server
,
5323 &rqst
, &resp_buftype
, flags
,
5325 SMB2_set_info_free(&rqst
);
5326 rsp
= (struct smb2_set_info_rsp
*)rsp_iov
.iov_base
;
5329 cifs_stats_fail_inc(tcon
, SMB2_SET_INFO_HE
);
5330 trace_smb3_set_info_err(xid
, persistent_fid
, tcon
->tid
,
5331 ses
->Suid
, info_class
, (__u32
)info_type
, rc
);
5334 free_rsp_buf(resp_buftype
, rsp
);
5340 SMB2_set_eof(const unsigned int xid
, struct cifs_tcon
*tcon
, u64 persistent_fid
,
5341 u64 volatile_fid
, u32 pid
, __le64
*eof
)
5343 struct smb2_file_eof_info info
;
5347 info
.EndOfFile
= *eof
;
5350 size
= sizeof(struct smb2_file_eof_info
);
5352 trace_smb3_set_eof(xid
, persistent_fid
, tcon
->tid
, tcon
->ses
->Suid
, le64_to_cpu(*eof
));
5354 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
5355 pid
, FILE_END_OF_FILE_INFORMATION
, SMB2_O_INFO_FILE
,
5356 0, 1, &data
, &size
);
5360 SMB2_set_acl(const unsigned int xid
, struct cifs_tcon
*tcon
,
5361 u64 persistent_fid
, u64 volatile_fid
,
5362 struct cifs_ntsd
*pnntsd
, int pacllen
, int aclflag
)
5364 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
5365 current
->tgid
, 0, SMB2_O_INFO_SECURITY
, aclflag
,
5366 1, (void **)&pnntsd
, &pacllen
);
5370 SMB2_set_ea(const unsigned int xid
, struct cifs_tcon
*tcon
,
5371 u64 persistent_fid
, u64 volatile_fid
,
5372 struct smb2_file_full_ea_info
*buf
, int len
)
5374 return send_set_info(xid
, tcon
, persistent_fid
, volatile_fid
,
5375 current
->tgid
, FILE_FULL_EA_INFORMATION
, SMB2_O_INFO_FILE
,
5376 0, 1, (void **)&buf
, &len
);
5380 SMB2_oplock_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
5381 const u64 persistent_fid
, const u64 volatile_fid
,
5384 struct smb_rqst rqst
;
5386 struct smb2_oplock_break
*req
= NULL
;
5387 struct cifs_ses
*ses
= tcon
->ses
;
5388 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
5389 int flags
= CIFS_OBREAK_OP
;
5390 unsigned int total_len
;
5392 struct kvec rsp_iov
;
5395 cifs_dbg(FYI
, "SMB2_oplock_break\n");
5396 rc
= smb2_plain_req_init(SMB2_OPLOCK_BREAK
, tcon
, server
,
5397 (void **) &req
, &total_len
);
5401 if (smb3_encryption_required(tcon
))
5402 flags
|= CIFS_TRANSFORM_REQ
;
5404 req
->VolatileFid
= volatile_fid
;
5405 req
->PersistentFid
= persistent_fid
;
5406 req
->OplockLevel
= oplock_level
;
5407 req
->hdr
.CreditRequest
= cpu_to_le16(1);
5409 flags
|= CIFS_NO_RSP_BUF
;
5411 iov
[0].iov_base
= (char *)req
;
5412 iov
[0].iov_len
= total_len
;
5414 memset(&rqst
, 0, sizeof(struct smb_rqst
));
5418 rc
= cifs_send_recv(xid
, ses
, server
,
5419 &rqst
, &resp_buf_type
, flags
, &rsp_iov
);
5420 cifs_small_buf_release(req
);
5423 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
5424 cifs_dbg(FYI
, "Send error in Oplock Break = %d\n", rc
);
5431 smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info
*pfs_inf
,
5432 struct kstatfs
*kst
)
5434 kst
->f_bsize
= le32_to_cpu(pfs_inf
->BytesPerSector
) *
5435 le32_to_cpu(pfs_inf
->SectorsPerAllocationUnit
);
5436 kst
->f_blocks
= le64_to_cpu(pfs_inf
->TotalAllocationUnits
);
5437 kst
->f_bfree
= kst
->f_bavail
=
5438 le64_to_cpu(pfs_inf
->CallerAvailableAllocationUnits
);
5443 copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO
*response_data
,
5444 struct kstatfs
*kst
)
5446 kst
->f_bsize
= le32_to_cpu(response_data
->BlockSize
);
5447 kst
->f_blocks
= le64_to_cpu(response_data
->TotalBlocks
);
5448 kst
->f_bfree
= le64_to_cpu(response_data
->BlocksAvail
);
5449 if (response_data
->UserBlocksAvail
== cpu_to_le64(-1))
5450 kst
->f_bavail
= kst
->f_bfree
;
5452 kst
->f_bavail
= le64_to_cpu(response_data
->UserBlocksAvail
);
5453 if (response_data
->TotalFileNodes
!= cpu_to_le64(-1))
5454 kst
->f_files
= le64_to_cpu(response_data
->TotalFileNodes
);
5455 if (response_data
->FreeFileNodes
!= cpu_to_le64(-1))
5456 kst
->f_ffree
= le64_to_cpu(response_data
->FreeFileNodes
);
5462 build_qfs_info_req(struct kvec
*iov
, struct cifs_tcon
*tcon
,
5463 struct TCP_Server_Info
*server
,
5464 int level
, int outbuf_len
, u64 persistent_fid
,
5468 struct smb2_query_info_req
*req
;
5469 unsigned int total_len
;
5471 cifs_dbg(FYI
, "Query FSInfo level %d\n", level
);
5473 if ((tcon
->ses
== NULL
) || server
== NULL
)
5476 rc
= smb2_plain_req_init(SMB2_QUERY_INFO
, tcon
, server
,
5477 (void **) &req
, &total_len
);
5481 req
->InfoType
= SMB2_O_INFO_FILESYSTEM
;
5482 req
->FileInfoClass
= level
;
5483 req
->PersistentFileId
= persistent_fid
;
5484 req
->VolatileFileId
= volatile_fid
;
5486 req
->InputBufferOffset
=
5487 cpu_to_le16(sizeof(struct smb2_query_info_req
));
5488 req
->OutputBufferLength
= cpu_to_le32(
5489 outbuf_len
+ sizeof(struct smb2_query_info_rsp
));
5491 iov
->iov_base
= (char *)req
;
5492 iov
->iov_len
= total_len
;
5497 SMB311_posix_qfs_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
5498 u64 persistent_fid
, u64 volatile_fid
, struct kstatfs
*fsdata
)
5500 struct smb_rqst rqst
;
5501 struct smb2_query_info_rsp
*rsp
= NULL
;
5503 struct kvec rsp_iov
;
5506 struct cifs_ses
*ses
= tcon
->ses
;
5507 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
5508 FILE_SYSTEM_POSIX_INFO
*info
= NULL
;
5511 rc
= build_qfs_info_req(&iov
, tcon
, server
,
5512 FS_POSIX_INFORMATION
,
5513 sizeof(FILE_SYSTEM_POSIX_INFO
),
5514 persistent_fid
, volatile_fid
);
5518 if (smb3_encryption_required(tcon
))
5519 flags
|= CIFS_TRANSFORM_REQ
;
5521 memset(&rqst
, 0, sizeof(struct smb_rqst
));
5525 rc
= cifs_send_recv(xid
, ses
, server
,
5526 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
5527 cifs_small_buf_release(iov
.iov_base
);
5529 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
5530 goto posix_qfsinf_exit
;
5532 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
5534 info
= (FILE_SYSTEM_POSIX_INFO
*)(
5535 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)rsp
);
5536 rc
= smb2_validate_iov(le16_to_cpu(rsp
->OutputBufferOffset
),
5537 le32_to_cpu(rsp
->OutputBufferLength
), &rsp_iov
,
5538 sizeof(FILE_SYSTEM_POSIX_INFO
));
5540 copy_posix_fs_info_to_kstatfs(info
, fsdata
);
5543 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
5548 SMB2_QFS_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
5549 u64 persistent_fid
, u64 volatile_fid
, struct kstatfs
*fsdata
)
5551 struct smb_rqst rqst
;
5552 struct smb2_query_info_rsp
*rsp
= NULL
;
5554 struct kvec rsp_iov
;
5557 struct cifs_ses
*ses
= tcon
->ses
;
5558 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
5559 struct smb2_fs_full_size_info
*info
= NULL
;
5562 rc
= build_qfs_info_req(&iov
, tcon
, server
,
5563 FS_FULL_SIZE_INFORMATION
,
5564 sizeof(struct smb2_fs_full_size_info
),
5565 persistent_fid
, volatile_fid
);
5569 if (smb3_encryption_required(tcon
))
5570 flags
|= CIFS_TRANSFORM_REQ
;
5572 memset(&rqst
, 0, sizeof(struct smb_rqst
));
5576 rc
= cifs_send_recv(xid
, ses
, server
,
5577 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
5578 cifs_small_buf_release(iov
.iov_base
);
5580 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
5583 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
5585 info
= (struct smb2_fs_full_size_info
*)(
5586 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)rsp
);
5587 rc
= smb2_validate_iov(le16_to_cpu(rsp
->OutputBufferOffset
),
5588 le32_to_cpu(rsp
->OutputBufferLength
), &rsp_iov
,
5589 sizeof(struct smb2_fs_full_size_info
));
5591 smb2_copy_fs_info_to_kstatfs(info
, fsdata
);
5594 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
5599 SMB2_QFS_attr(const unsigned int xid
, struct cifs_tcon
*tcon
,
5600 u64 persistent_fid
, u64 volatile_fid
, int level
)
5602 struct smb_rqst rqst
;
5603 struct smb2_query_info_rsp
*rsp
= NULL
;
5605 struct kvec rsp_iov
;
5607 int resp_buftype
, max_len
, min_len
;
5608 struct cifs_ses
*ses
= tcon
->ses
;
5609 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
5610 unsigned int rsp_len
, offset
;
5613 if (level
== FS_DEVICE_INFORMATION
) {
5614 max_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
5615 min_len
= sizeof(FILE_SYSTEM_DEVICE_INFO
);
5616 } else if (level
== FS_ATTRIBUTE_INFORMATION
) {
5617 max_len
= sizeof(FILE_SYSTEM_ATTRIBUTE_INFO
);
5618 min_len
= MIN_FS_ATTR_INFO_SIZE
;
5619 } else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
5620 max_len
= sizeof(struct smb3_fs_ss_info
);
5621 min_len
= sizeof(struct smb3_fs_ss_info
);
5622 } else if (level
== FS_VOLUME_INFORMATION
) {
5623 max_len
= sizeof(struct smb3_fs_vol_info
) + MAX_VOL_LABEL_LEN
;
5624 min_len
= sizeof(struct smb3_fs_vol_info
);
5626 cifs_dbg(FYI
, "Invalid qfsinfo level %d\n", level
);
5630 rc
= build_qfs_info_req(&iov
, tcon
, server
,
5632 persistent_fid
, volatile_fid
);
5636 if (smb3_encryption_required(tcon
))
5637 flags
|= CIFS_TRANSFORM_REQ
;
5639 memset(&rqst
, 0, sizeof(struct smb_rqst
));
5643 rc
= cifs_send_recv(xid
, ses
, server
,
5644 &rqst
, &resp_buftype
, flags
, &rsp_iov
);
5645 cifs_small_buf_release(iov
.iov_base
);
5647 cifs_stats_fail_inc(tcon
, SMB2_QUERY_INFO_HE
);
5650 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
5652 rsp_len
= le32_to_cpu(rsp
->OutputBufferLength
);
5653 offset
= le16_to_cpu(rsp
->OutputBufferOffset
);
5654 rc
= smb2_validate_iov(offset
, rsp_len
, &rsp_iov
, min_len
);
5658 if (level
== FS_ATTRIBUTE_INFORMATION
)
5659 memcpy(&tcon
->fsAttrInfo
, offset
5660 + (char *)rsp
, min_t(unsigned int,
5662 else if (level
== FS_DEVICE_INFORMATION
)
5663 memcpy(&tcon
->fsDevInfo
, offset
5664 + (char *)rsp
, sizeof(FILE_SYSTEM_DEVICE_INFO
));
5665 else if (level
== FS_SECTOR_SIZE_INFORMATION
) {
5666 struct smb3_fs_ss_info
*ss_info
= (struct smb3_fs_ss_info
*)
5667 (offset
+ (char *)rsp
);
5668 tcon
->ss_flags
= le32_to_cpu(ss_info
->Flags
);
5669 tcon
->perf_sector_size
=
5670 le32_to_cpu(ss_info
->PhysicalBytesPerSectorForPerf
);
5671 } else if (level
== FS_VOLUME_INFORMATION
) {
5672 struct smb3_fs_vol_info
*vol_info
= (struct smb3_fs_vol_info
*)
5673 (offset
+ (char *)rsp
);
5674 tcon
->vol_serial_number
= vol_info
->VolumeSerialNumber
;
5675 tcon
->vol_create_time
= vol_info
->VolumeCreationTime
;
5679 free_rsp_buf(resp_buftype
, rsp_iov
.iov_base
);
5684 smb2_lockv(const unsigned int xid
, struct cifs_tcon
*tcon
,
5685 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
5686 const __u32 num_lock
, struct smb2_lock_element
*buf
)
5688 struct smb_rqst rqst
;
5690 struct smb2_lock_req
*req
= NULL
;
5692 struct kvec rsp_iov
;
5695 int flags
= CIFS_NO_RSP_BUF
;
5696 unsigned int total_len
;
5697 struct TCP_Server_Info
*server
= cifs_pick_channel(tcon
->ses
);
5699 cifs_dbg(FYI
, "smb2_lockv num lock %d\n", num_lock
);
5701 rc
= smb2_plain_req_init(SMB2_LOCK
, tcon
, server
,
5702 (void **) &req
, &total_len
);
5706 if (smb3_encryption_required(tcon
))
5707 flags
|= CIFS_TRANSFORM_REQ
;
5709 req
->hdr
.Id
.SyncId
.ProcessId
= cpu_to_le32(pid
);
5710 req
->LockCount
= cpu_to_le16(num_lock
);
5712 req
->PersistentFileId
= persist_fid
;
5713 req
->VolatileFileId
= volatile_fid
;
5715 count
= num_lock
* sizeof(struct smb2_lock_element
);
5717 iov
[0].iov_base
= (char *)req
;
5718 iov
[0].iov_len
= total_len
- sizeof(struct smb2_lock_element
);
5719 iov
[1].iov_base
= (char *)buf
;
5720 iov
[1].iov_len
= count
;
5722 cifs_stats_inc(&tcon
->stats
.cifs_stats
.num_locks
);
5724 memset(&rqst
, 0, sizeof(struct smb_rqst
));
5728 rc
= cifs_send_recv(xid
, tcon
->ses
, server
,
5729 &rqst
, &resp_buf_type
, flags
,
5731 cifs_small_buf_release(req
);
5733 cifs_dbg(FYI
, "Send error in smb2_lockv = %d\n", rc
);
5734 cifs_stats_fail_inc(tcon
, SMB2_LOCK_HE
);
5735 trace_smb3_lock_err(xid
, persist_fid
, tcon
->tid
,
5736 tcon
->ses
->Suid
, rc
);
5743 SMB2_lock(const unsigned int xid
, struct cifs_tcon
*tcon
,
5744 const __u64 persist_fid
, const __u64 volatile_fid
, const __u32 pid
,
5745 const __u64 length
, const __u64 offset
, const __u32 lock_flags
,
5748 struct smb2_lock_element lock
;
5750 lock
.Offset
= cpu_to_le64(offset
);
5751 lock
.Length
= cpu_to_le64(length
);
5752 lock
.Flags
= cpu_to_le32(lock_flags
);
5753 if (!wait
&& lock_flags
!= SMB2_LOCKFLAG_UNLOCK
)
5754 lock
.Flags
|= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY
);
5756 return smb2_lockv(xid
, tcon
, persist_fid
, volatile_fid
, pid
, 1, &lock
);
5760 SMB2_lease_break(const unsigned int xid
, struct cifs_tcon
*tcon
,
5761 __u8
*lease_key
, const __le32 lease_state
)
5763 struct smb_rqst rqst
;
5765 struct smb2_lease_ack
*req
= NULL
;
5766 struct cifs_ses
*ses
= tcon
->ses
;
5767 int flags
= CIFS_OBREAK_OP
;
5768 unsigned int total_len
;
5770 struct kvec rsp_iov
;
5772 __u64
*please_key_high
;
5773 __u64
*please_key_low
;
5774 struct TCP_Server_Info
*server
= cifs_pick_channel(tcon
->ses
);
5776 cifs_dbg(FYI
, "SMB2_lease_break\n");
5777 rc
= smb2_plain_req_init(SMB2_OPLOCK_BREAK
, tcon
, server
,
5778 (void **) &req
, &total_len
);
5782 if (smb3_encryption_required(tcon
))
5783 flags
|= CIFS_TRANSFORM_REQ
;
5785 req
->hdr
.CreditRequest
= cpu_to_le16(1);
5786 req
->StructureSize
= cpu_to_le16(36);
5789 memcpy(req
->LeaseKey
, lease_key
, 16);
5790 req
->LeaseState
= lease_state
;
5792 flags
|= CIFS_NO_RSP_BUF
;
5794 iov
[0].iov_base
= (char *)req
;
5795 iov
[0].iov_len
= total_len
;
5797 memset(&rqst
, 0, sizeof(struct smb_rqst
));
5801 rc
= cifs_send_recv(xid
, ses
, server
,
5802 &rqst
, &resp_buf_type
, flags
, &rsp_iov
);
5803 cifs_small_buf_release(req
);
5805 please_key_low
= (__u64
*)lease_key
;
5806 please_key_high
= (__u64
*)(lease_key
+8);
5808 cifs_stats_fail_inc(tcon
, SMB2_OPLOCK_BREAK_HE
);
5809 trace_smb3_lease_err(le32_to_cpu(lease_state
), tcon
->tid
,
5810 ses
->Suid
, *please_key_low
, *please_key_high
, rc
);
5811 cifs_dbg(FYI
, "Send error in Lease Break = %d\n", rc
);
5813 trace_smb3_lease_done(le32_to_cpu(lease_state
), tcon
->tid
,
5814 ses
->Suid
, *please_key_low
, *please_key_high
);