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