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