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