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