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