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