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