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