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