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