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