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