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