]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/cifs/smb2pdu.c
net: hns: add the code for cleaning pkt in chip
[mirror_ubuntu-bionic-kernel.git] / fs / cifs / smb2pdu.c
1 /*
2 * fs/cifs/smb2pdu.c
3 *
4 * Copyright (C) International Business Machines Corp., 2009, 2013
5 * Etersoft, 2012
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Pavel Shilovsky (pshilovsky@samba.org) 2012
8 *
9 * Contains the routines for constructing the SMB2 PDUs themselves
10 *
11 * This library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published
13 * by the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
27 /* Note that there are handle based routines which must be */
28 /* treated slightly differently for reconnection purposes since we never */
29 /* want to reuse a stale file handle and only the caller knows the file info */
30
31 #include <linux/fs.h>
32 #include <linux/kernel.h>
33 #include <linux/vfs.h>
34 #include <linux/task_io_accounting_ops.h>
35 #include <linux/uaccess.h>
36 #include <linux/uuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/xattr.h>
39 #include "smb2pdu.h"
40 #include "cifsglob.h"
41 #include "cifsacl.h"
42 #include "cifsproto.h"
43 #include "smb2proto.h"
44 #include "cifs_unicode.h"
45 #include "cifs_debug.h"
46 #include "ntlmssp.h"
47 #include "smb2status.h"
48 #include "smb2glob.h"
49 #include "cifspdu.h"
50 #include "cifs_spnego.h"
51
52 /*
53 * The following table defines the expected "StructureSize" of SMB2 requests
54 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
55 *
56 * Note that commands are defined in smb2pdu.h in le16 but the array below is
57 * indexed by command in host byte order.
58 */
59 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
60 /* SMB2_NEGOTIATE */ 36,
61 /* SMB2_SESSION_SETUP */ 25,
62 /* SMB2_LOGOFF */ 4,
63 /* SMB2_TREE_CONNECT */ 9,
64 /* SMB2_TREE_DISCONNECT */ 4,
65 /* SMB2_CREATE */ 57,
66 /* SMB2_CLOSE */ 24,
67 /* SMB2_FLUSH */ 24,
68 /* SMB2_READ */ 49,
69 /* SMB2_WRITE */ 49,
70 /* SMB2_LOCK */ 48,
71 /* SMB2_IOCTL */ 57,
72 /* SMB2_CANCEL */ 4,
73 /* SMB2_ECHO */ 4,
74 /* SMB2_QUERY_DIRECTORY */ 33,
75 /* SMB2_CHANGE_NOTIFY */ 32,
76 /* SMB2_QUERY_INFO */ 41,
77 /* SMB2_SET_INFO */ 33,
78 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
79 };
80
81 static int encryption_required(const struct cifs_tcon *tcon)
82 {
83 if (!tcon)
84 return 0;
85 if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
86 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
87 return 1;
88 if (tcon->seal &&
89 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
90 return 1;
91 return 0;
92 }
93
94 static void
95 smb2_hdr_assemble(struct smb2_sync_hdr *shdr, __le16 smb2_cmd,
96 const struct cifs_tcon *tcon)
97 {
98 shdr->ProtocolId = SMB2_PROTO_NUMBER;
99 shdr->StructureSize = cpu_to_le16(64);
100 shdr->Command = smb2_cmd;
101 if (tcon && tcon->ses && tcon->ses->server) {
102 struct TCP_Server_Info *server = tcon->ses->server;
103
104 spin_lock(&server->req_lock);
105 /* Request up to 2 credits but don't go over the limit. */
106 if (server->credits >= server->max_credits)
107 shdr->CreditRequest = cpu_to_le16(0);
108 else
109 shdr->CreditRequest = cpu_to_le16(
110 min_t(int, server->max_credits -
111 server->credits, 2));
112 spin_unlock(&server->req_lock);
113 } else {
114 shdr->CreditRequest = cpu_to_le16(2);
115 }
116 shdr->ProcessId = cpu_to_le32((__u16)current->tgid);
117
118 if (!tcon)
119 goto out;
120
121 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
122 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
123 if ((tcon->ses) && (tcon->ses->server) &&
124 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
125 shdr->CreditCharge = cpu_to_le16(1);
126 /* else CreditCharge MBZ */
127
128 shdr->TreeId = tcon->tid;
129 /* Uid is not converted */
130 if (tcon->ses)
131 shdr->SessionId = tcon->ses->Suid;
132
133 /*
134 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
135 * to pass the path on the Open SMB prefixed by \\server\share.
136 * Not sure when we would need to do the augmented path (if ever) and
137 * setting this flag breaks the SMB2 open operation since it is
138 * illegal to send an empty path name (without \\server\share prefix)
139 * when the DFS flag is set in the SMB open header. We could
140 * consider setting the flag on all operations other than open
141 * but it is safer to net set it for now.
142 */
143 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
144 shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
145
146 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign &&
147 !encryption_required(tcon))
148 shdr->Flags |= SMB2_FLAGS_SIGNED;
149 out:
150 return;
151 }
152
153 static int
154 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
155 {
156 int rc = 0;
157 struct nls_table *nls_codepage;
158 struct cifs_ses *ses;
159 struct TCP_Server_Info *server;
160
161 /*
162 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
163 * check for tcp and smb session status done differently
164 * for those three - in the calling routine.
165 */
166 if (tcon == NULL)
167 return rc;
168
169 if (smb2_command == SMB2_TREE_CONNECT)
170 return rc;
171
172 if (tcon->tidStatus == CifsExiting) {
173 /*
174 * only tree disconnect, open, and write,
175 * (and ulogoff which does not have tcon)
176 * are allowed as we start force umount.
177 */
178 if ((smb2_command != SMB2_WRITE) &&
179 (smb2_command != SMB2_CREATE) &&
180 (smb2_command != SMB2_TREE_DISCONNECT)) {
181 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
182 smb2_command);
183 return -ENODEV;
184 }
185 }
186 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
187 (!tcon->ses->server))
188 return -EIO;
189
190 ses = tcon->ses;
191 server = ses->server;
192
193 /*
194 * Give demultiplex thread up to 10 seconds to reconnect, should be
195 * greater than cifs socket timeout which is 7 seconds
196 */
197 while (server->tcpStatus == CifsNeedReconnect) {
198 /*
199 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
200 * here since they are implicitly done when session drops.
201 */
202 switch (smb2_command) {
203 /*
204 * BB Should we keep oplock break and add flush to exceptions?
205 */
206 case SMB2_TREE_DISCONNECT:
207 case SMB2_CANCEL:
208 case SMB2_CLOSE:
209 case SMB2_OPLOCK_BREAK:
210 return -EAGAIN;
211 }
212
213 wait_event_interruptible_timeout(server->response_q,
214 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
215
216 /* are we still trying to reconnect? */
217 if (server->tcpStatus != CifsNeedReconnect)
218 break;
219
220 /*
221 * on "soft" mounts we wait once. Hard mounts keep
222 * retrying until process is killed or server comes
223 * back on-line
224 */
225 if (!tcon->retry) {
226 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
227 return -EHOSTDOWN;
228 }
229 }
230
231 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
232 return rc;
233
234 nls_codepage = load_nls_default();
235
236 /*
237 * need to prevent multiple threads trying to simultaneously reconnect
238 * the same SMB session
239 */
240 mutex_lock(&tcon->ses->session_mutex);
241
242 /*
243 * Recheck after acquire mutex. If another thread is negotiating
244 * and the server never sends an answer the socket will be closed
245 * and tcpStatus set to reconnect.
246 */
247 if (server->tcpStatus == CifsNeedReconnect) {
248 rc = -EHOSTDOWN;
249 mutex_unlock(&tcon->ses->session_mutex);
250 goto out;
251 }
252
253 rc = cifs_negotiate_protocol(0, tcon->ses);
254 if (!rc && tcon->ses->need_reconnect)
255 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
256
257 if (rc || !tcon->need_reconnect) {
258 mutex_unlock(&tcon->ses->session_mutex);
259 goto out;
260 }
261
262 cifs_mark_open_files_invalid(tcon);
263 if (tcon->use_persistent)
264 tcon->need_reopen_files = true;
265
266 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
267 mutex_unlock(&tcon->ses->session_mutex);
268
269 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
270 if (rc)
271 goto out;
272
273 if (smb2_command != SMB2_INTERNAL_CMD)
274 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
275
276 atomic_inc(&tconInfoReconnectCount);
277 out:
278 /*
279 * Check if handle based operation so we know whether we can continue
280 * or not without returning to caller to reset file handle.
281 */
282 /*
283 * BB Is flush done by server on drop of tcp session? Should we special
284 * case it and skip above?
285 */
286 switch (smb2_command) {
287 case SMB2_FLUSH:
288 case SMB2_READ:
289 case SMB2_WRITE:
290 case SMB2_LOCK:
291 case SMB2_IOCTL:
292 case SMB2_QUERY_DIRECTORY:
293 case SMB2_CHANGE_NOTIFY:
294 case SMB2_QUERY_INFO:
295 case SMB2_SET_INFO:
296 rc = -EAGAIN;
297 }
298 unload_nls(nls_codepage);
299 return rc;
300 }
301
302 static void
303 fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon, void *buf,
304 unsigned int *total_len)
305 {
306 struct smb2_sync_pdu *spdu = (struct smb2_sync_pdu *)buf;
307 /* lookup word count ie StructureSize from table */
308 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
309
310 /*
311 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
312 * largest operations (Create)
313 */
314 memset(buf, 0, 256);
315
316 smb2_hdr_assemble(&spdu->sync_hdr, smb2_command, tcon);
317 spdu->StructureSize2 = cpu_to_le16(parmsize);
318
319 *total_len = parmsize + sizeof(struct smb2_sync_hdr);
320 }
321
322 /* init request without RFC1001 length at the beginning */
323 static int
324 smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
325 void **request_buf, unsigned int *total_len)
326 {
327 int rc;
328 struct smb2_sync_hdr *shdr;
329
330 rc = smb2_reconnect(smb2_command, tcon);
331 if (rc)
332 return rc;
333
334 /* BB eventually switch this to SMB2 specific small buf size */
335 *request_buf = cifs_small_buf_get();
336 if (*request_buf == NULL) {
337 /* BB should we add a retry in here if not a writepage? */
338 return -ENOMEM;
339 }
340
341 shdr = (struct smb2_sync_hdr *)(*request_buf);
342
343 fill_small_buf(smb2_command, tcon, shdr, total_len);
344
345 if (tcon != NULL) {
346 #ifdef CONFIG_CIFS_STATS2
347 uint16_t com_code = le16_to_cpu(smb2_command);
348
349 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
350 #endif
351 cifs_stats_inc(&tcon->num_smbs_sent);
352 }
353
354 return rc;
355 }
356
357 /*
358 * Allocate and return pointer to an SMB request hdr, and set basic
359 * SMB information in the SMB header. If the return code is zero, this
360 * function must have filled in request_buf pointer. The returned buffer
361 * has RFC1001 length at the beginning.
362 */
363 static int
364 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
365 void **request_buf)
366 {
367 int rc;
368 unsigned int total_len;
369 struct smb2_pdu *pdu;
370
371 rc = smb2_reconnect(smb2_command, tcon);
372 if (rc)
373 return rc;
374
375 /* BB eventually switch this to SMB2 specific small buf size */
376 *request_buf = cifs_small_buf_get();
377 if (*request_buf == NULL) {
378 /* BB should we add a retry in here if not a writepage? */
379 return -ENOMEM;
380 }
381
382 pdu = (struct smb2_pdu *)(*request_buf);
383
384 fill_small_buf(smb2_command, tcon, get_sync_hdr(pdu), &total_len);
385
386 /* Note this is only network field converted to big endian */
387 pdu->hdr.smb2_buf_length = cpu_to_be32(total_len);
388
389 if (tcon != NULL) {
390 #ifdef CONFIG_CIFS_STATS2
391 uint16_t com_code = le16_to_cpu(smb2_command);
392 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
393 #endif
394 cifs_stats_inc(&tcon->num_smbs_sent);
395 }
396
397 return rc;
398 }
399
400 #ifdef CONFIG_CIFS_SMB311
401 /* offset is sizeof smb2_negotiate_req - 4 but rounded up to 8 bytes */
402 #define OFFSET_OF_NEG_CONTEXT 0x68 /* sizeof(struct smb2_negotiate_req) - 4 */
403
404
405 #define SMB2_PREAUTH_INTEGRITY_CAPABILITIES cpu_to_le16(1)
406 #define SMB2_ENCRYPTION_CAPABILITIES cpu_to_le16(2)
407
408 static void
409 build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
410 {
411 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
412 pneg_ctxt->DataLength = cpu_to_le16(38);
413 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
414 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
415 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
416 pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
417 }
418
419 static void
420 build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
421 {
422 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
423 pneg_ctxt->DataLength = cpu_to_le16(6);
424 pneg_ctxt->CipherCount = cpu_to_le16(2);
425 pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
426 pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
427 }
428
429 static void
430 assemble_neg_contexts(struct smb2_negotiate_req *req)
431 {
432
433 /* +4 is to account for the RFC1001 len field */
434 char *pneg_ctxt = (char *)req + OFFSET_OF_NEG_CONTEXT + 4;
435
436 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
437 /* Add 2 to size to round to 8 byte boundary */
438 pneg_ctxt += 2 + sizeof(struct smb2_preauth_neg_context);
439 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
440 req->NegotiateContextOffset = cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
441 req->NegotiateContextCount = cpu_to_le16(2);
442 inc_rfc1001_len(req, 4 + sizeof(struct smb2_preauth_neg_context)
443 + sizeof(struct smb2_encryption_neg_context)); /* calculate hash */
444 }
445 #else
446 static void assemble_neg_contexts(struct smb2_negotiate_req *req)
447 {
448 return;
449 }
450 #endif /* SMB311 */
451
452 /*
453 *
454 * SMB2 Worker functions follow:
455 *
456 * The general structure of the worker functions is:
457 * 1) Call smb2_init (assembles SMB2 header)
458 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
459 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
460 * 4) Decode SMB2 command specific fields in the fixed length area
461 * 5) Decode variable length data area (if any for this SMB2 command type)
462 * 6) Call free smb buffer
463 * 7) return
464 *
465 */
466
467 int
468 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
469 {
470 struct smb2_negotiate_req *req;
471 struct smb2_negotiate_rsp *rsp;
472 struct kvec iov[1];
473 struct kvec rsp_iov;
474 int rc = 0;
475 int resp_buftype;
476 struct TCP_Server_Info *server = ses->server;
477 int blob_offset, blob_length;
478 char *security_blob;
479 int flags = CIFS_NEG_OP;
480
481 cifs_dbg(FYI, "Negotiate protocol\n");
482
483 if (!server) {
484 WARN(1, "%s: server is NULL!\n", __func__);
485 return -EIO;
486 }
487
488 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
489 if (rc)
490 return rc;
491
492 req->hdr.sync_hdr.SessionId = 0;
493
494 if (strcmp(ses->server->vals->version_string,
495 SMB3ANY_VERSION_STRING) == 0) {
496 req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
497 req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
498 req->DialectCount = cpu_to_le16(2);
499 inc_rfc1001_len(req, 4);
500 } else if (strcmp(ses->server->vals->version_string,
501 SMBDEFAULT_VERSION_STRING) == 0) {
502 req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
503 req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
504 req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
505 req->DialectCount = cpu_to_le16(3);
506 inc_rfc1001_len(req, 6);
507 } else {
508 /* otherwise send specific dialect */
509 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
510 req->DialectCount = cpu_to_le16(1);
511 inc_rfc1001_len(req, 2);
512 }
513
514 /* only one of SMB2 signing flags may be set in SMB2 request */
515 if (ses->sign)
516 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
517 else if (global_secflags & CIFSSEC_MAY_SIGN)
518 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
519 else
520 req->SecurityMode = 0;
521
522 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
523
524 /* ClientGUID must be zero for SMB2.02 dialect */
525 if (ses->server->vals->protocol_id == SMB20_PROT_ID)
526 memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
527 else {
528 memcpy(req->ClientGUID, server->client_guid,
529 SMB2_CLIENT_GUID_SIZE);
530 if (ses->server->vals->protocol_id == SMB311_PROT_ID)
531 assemble_neg_contexts(req);
532 }
533 iov[0].iov_base = (char *)req;
534 /* 4 for rfc1002 length field */
535 iov[0].iov_len = get_rfc1002_length(req) + 4;
536
537 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
538 cifs_small_buf_release(req);
539 rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
540 /*
541 * No tcon so can't do
542 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
543 */
544 if (rc == -EOPNOTSUPP) {
545 cifs_dbg(VFS, "Dialect not supported by server. Consider "
546 "specifying vers=1.0 or vers=2.0 on mount for accessing"
547 " older servers\n");
548 goto neg_exit;
549 } else if (rc != 0)
550 goto neg_exit;
551
552 if (strcmp(ses->server->vals->version_string,
553 SMB3ANY_VERSION_STRING) == 0) {
554 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
555 cifs_dbg(VFS,
556 "SMB2 dialect returned but not requested\n");
557 return -EIO;
558 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
559 cifs_dbg(VFS,
560 "SMB2.1 dialect returned but not requested\n");
561 return -EIO;
562 }
563 } else if (strcmp(ses->server->vals->version_string,
564 SMBDEFAULT_VERSION_STRING) == 0) {
565 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
566 cifs_dbg(VFS,
567 "SMB2 dialect returned but not requested\n");
568 return -EIO;
569 } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
570 /* ops set to 3.0 by default for default so update */
571 ses->server->ops = &smb21_operations;
572 }
573 } else if (le16_to_cpu(rsp->DialectRevision) !=
574 ses->server->vals->protocol_id) {
575 /* if requested single dialect ensure returned dialect matched */
576 cifs_dbg(VFS, "Illegal 0x%x dialect returned: not requested\n",
577 le16_to_cpu(rsp->DialectRevision));
578 return -EIO;
579 }
580
581 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
582
583 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
584 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
585 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
586 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
587 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
588 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
589 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
590 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
591 #ifdef CONFIG_CIFS_SMB311
592 else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
593 cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
594 #endif /* SMB311 */
595 else {
596 cifs_dbg(VFS, "Illegal dialect returned by server 0x%x\n",
597 le16_to_cpu(rsp->DialectRevision));
598 rc = -EIO;
599 goto neg_exit;
600 }
601 server->dialect = le16_to_cpu(rsp->DialectRevision);
602
603 /* BB: add check that dialect was valid given dialect(s) we asked for */
604
605 /* SMB2 only has an extended negflavor */
606 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
607 /* set it to the maximum buffer size value we can send with 1 credit */
608 server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
609 SMB2_MAX_BUFFER_SIZE);
610 server->max_read = le32_to_cpu(rsp->MaxReadSize);
611 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
612 /* BB Do we need to validate the SecurityMode? */
613 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
614 server->capabilities = le32_to_cpu(rsp->Capabilities);
615 /* Internal types */
616 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
617
618 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
619 &rsp->hdr);
620 /*
621 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
622 * for us will be
623 * ses->sectype = RawNTLMSSP;
624 * but for time being this is our only auth choice so doesn't matter.
625 * We just found a server which sets blob length to zero expecting raw.
626 */
627 if (blob_length == 0) {
628 cifs_dbg(FYI, "missing security blob on negprot\n");
629 server->sec_ntlmssp = true;
630 }
631
632 rc = cifs_enable_signing(server, ses->sign);
633 if (rc)
634 goto neg_exit;
635 if (blob_length) {
636 rc = decode_negTokenInit(security_blob, blob_length, server);
637 if (rc == 1)
638 rc = 0;
639 else if (rc == 0)
640 rc = -EIO;
641 }
642 neg_exit:
643 free_rsp_buf(resp_buftype, rsp);
644 return rc;
645 }
646
647 int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
648 {
649 int rc;
650 struct validate_negotiate_info_req *pneg_inbuf;
651 struct validate_negotiate_info_rsp *pneg_rsp = NULL;
652 u32 rsplen;
653 u32 inbuflen; /* max of 4 dialects */
654
655 cifs_dbg(FYI, "validate negotiate\n");
656
657 /*
658 * validation ioctl must be signed, so no point sending this if we
659 * can not sign it (ie are not known user). Even if signing is not
660 * required (enabled but not negotiated), in those cases we selectively
661 * sign just this, the first and only signed request on a connection.
662 * Having validation of negotiate info helps reduce attack vectors.
663 */
664 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
665 return 0; /* validation requires signing */
666
667 if (tcon->ses->user_name == NULL) {
668 cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
669 return 0; /* validation requires signing */
670 }
671
672 if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
673 cifs_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
674
675 pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
676 if (!pneg_inbuf)
677 return -ENOMEM;
678
679 pneg_inbuf->Capabilities =
680 cpu_to_le32(tcon->ses->server->vals->req_capabilities);
681 memcpy(pneg_inbuf->Guid, tcon->ses->server->client_guid,
682 SMB2_CLIENT_GUID_SIZE);
683
684 if (tcon->ses->sign)
685 pneg_inbuf->SecurityMode =
686 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
687 else if (global_secflags & CIFSSEC_MAY_SIGN)
688 pneg_inbuf->SecurityMode =
689 cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
690 else
691 pneg_inbuf->SecurityMode = 0;
692
693
694 if (strcmp(tcon->ses->server->vals->version_string,
695 SMB3ANY_VERSION_STRING) == 0) {
696 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
697 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
698 pneg_inbuf->DialectCount = cpu_to_le16(2);
699 /* structure is big enough for 3 dialects, sending only 2 */
700 inbuflen = sizeof(*pneg_inbuf) -
701 sizeof(pneg_inbuf->Dialects[0]);
702 } else if (strcmp(tcon->ses->server->vals->version_string,
703 SMBDEFAULT_VERSION_STRING) == 0) {
704 pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
705 pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
706 pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
707 pneg_inbuf->DialectCount = cpu_to_le16(3);
708 /* structure is big enough for 3 dialects */
709 inbuflen = sizeof(*pneg_inbuf);
710 } else {
711 /* otherwise specific dialect was requested */
712 pneg_inbuf->Dialects[0] =
713 cpu_to_le16(tcon->ses->server->vals->protocol_id);
714 pneg_inbuf->DialectCount = cpu_to_le16(1);
715 /* structure is big enough for 3 dialects, sending only 1 */
716 inbuflen = sizeof(*pneg_inbuf) -
717 sizeof(pneg_inbuf->Dialects[0]) * 2;
718 }
719
720 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
721 FSCTL_VALIDATE_NEGOTIATE_INFO, true /* is_fsctl */,
722 (char *)pneg_inbuf, inbuflen, (char **)&pneg_rsp, &rsplen);
723
724 if (rc != 0) {
725 cifs_dbg(VFS, "validate protocol negotiate failed: %d\n", rc);
726 rc = -EIO;
727 goto out_free_inbuf;
728 }
729
730 rc = -EIO;
731 if (rsplen != sizeof(*pneg_rsp)) {
732 cifs_dbg(VFS, "invalid protocol negotiate response size: %d\n",
733 rsplen);
734
735 /* relax check since Mac returns max bufsize allowed on ioctl */
736 if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
737 goto out_free_rsp;
738 }
739
740 /* check validate negotiate info response matches what we got earlier */
741 if (pneg_rsp->Dialect != cpu_to_le16(tcon->ses->server->dialect))
742 goto vneg_out;
743
744 if (pneg_rsp->SecurityMode != cpu_to_le16(tcon->ses->server->sec_mode))
745 goto vneg_out;
746
747 /* do not validate server guid because not saved at negprot time yet */
748
749 if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
750 SMB2_LARGE_FILES) != tcon->ses->server->capabilities)
751 goto vneg_out;
752
753 /* validate negotiate successful */
754 rc = 0;
755 cifs_dbg(FYI, "validate negotiate info successful\n");
756 goto out_free_rsp;
757
758 vneg_out:
759 cifs_dbg(VFS, "protocol revalidation - security settings mismatch\n");
760 out_free_rsp:
761 kfree(pneg_rsp);
762 out_free_inbuf:
763 kfree(pneg_inbuf);
764 return rc;
765 }
766
767 enum securityEnum
768 smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
769 {
770 switch (requested) {
771 case Kerberos:
772 case RawNTLMSSP:
773 return requested;
774 case NTLMv2:
775 return RawNTLMSSP;
776 case Unspecified:
777 if (server->sec_ntlmssp &&
778 (global_secflags & CIFSSEC_MAY_NTLMSSP))
779 return RawNTLMSSP;
780 if ((server->sec_kerberos || server->sec_mskerberos) &&
781 (global_secflags & CIFSSEC_MAY_KRB5))
782 return Kerberos;
783 /* Fallthrough */
784 default:
785 return Unspecified;
786 }
787 }
788
789 struct SMB2_sess_data {
790 unsigned int xid;
791 struct cifs_ses *ses;
792 struct nls_table *nls_cp;
793 void (*func)(struct SMB2_sess_data *);
794 int result;
795 u64 previous_session;
796
797 /* we will send the SMB in three pieces:
798 * a fixed length beginning part, an optional
799 * SPNEGO blob (which can be zero length), and a
800 * last part which will include the strings
801 * and rest of bcc area. This allows us to avoid
802 * a large buffer 17K allocation
803 */
804 int buf0_type;
805 struct kvec iov[2];
806 };
807
808 static int
809 SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
810 {
811 int rc;
812 struct cifs_ses *ses = sess_data->ses;
813 struct smb2_sess_setup_req *req;
814 struct TCP_Server_Info *server = ses->server;
815
816 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
817 if (rc)
818 return rc;
819
820 /* First session, not a reauthenticate */
821 req->hdr.sync_hdr.SessionId = 0;
822
823 /* if reconnect, we need to send previous sess id, otherwise it is 0 */
824 req->PreviousSessionId = sess_data->previous_session;
825
826 req->Flags = 0; /* MBZ */
827 /* to enable echos and oplocks */
828 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(3);
829
830 /* only one of SMB2 signing flags may be set in SMB2 request */
831 if (server->sign)
832 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
833 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
834 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
835 else
836 req->SecurityMode = 0;
837
838 req->Capabilities = 0;
839 req->Channel = 0; /* MBZ */
840
841 sess_data->iov[0].iov_base = (char *)req;
842 /* 4 for rfc1002 length field and 1 for pad */
843 sess_data->iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
844 /*
845 * This variable will be used to clear the buffer
846 * allocated above in case of any error in the calling function.
847 */
848 sess_data->buf0_type = CIFS_SMALL_BUFFER;
849
850 return 0;
851 }
852
853 static void
854 SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
855 {
856 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
857 sess_data->buf0_type = CIFS_NO_BUFFER;
858 }
859
860 static int
861 SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
862 {
863 int rc;
864 struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
865 struct kvec rsp_iov = { NULL, 0 };
866
867 /* Testing shows that buffer offset must be at location of Buffer[0] */
868 req->SecurityBufferOffset =
869 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
870 1 /* pad */ - 4 /* rfc1001 len */);
871 req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
872
873 inc_rfc1001_len(req, sess_data->iov[1].iov_len - 1 /* pad */);
874
875 /* BB add code to build os and lm fields */
876
877 rc = SendReceive2(sess_data->xid, sess_data->ses,
878 sess_data->iov, 2,
879 &sess_data->buf0_type,
880 CIFS_LOG_ERROR | CIFS_NEG_OP, &rsp_iov);
881 cifs_small_buf_release(sess_data->iov[0].iov_base);
882 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
883
884 return rc;
885 }
886
887 static int
888 SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
889 {
890 int rc = 0;
891 struct cifs_ses *ses = sess_data->ses;
892
893 mutex_lock(&ses->server->srv_mutex);
894 if (ses->server->ops->generate_signingkey) {
895 rc = ses->server->ops->generate_signingkey(ses);
896 if (rc) {
897 cifs_dbg(FYI,
898 "SMB3 session key generation failed\n");
899 mutex_unlock(&ses->server->srv_mutex);
900 return rc;
901 }
902 }
903 if (!ses->server->session_estab) {
904 ses->server->sequence_number = 0x2;
905 ses->server->session_estab = true;
906 }
907 mutex_unlock(&ses->server->srv_mutex);
908
909 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
910 spin_lock(&GlobalMid_Lock);
911 ses->status = CifsGood;
912 ses->need_reconnect = false;
913 spin_unlock(&GlobalMid_Lock);
914 return rc;
915 }
916
917 #ifdef CONFIG_CIFS_UPCALL
918 static void
919 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
920 {
921 int rc;
922 struct cifs_ses *ses = sess_data->ses;
923 struct cifs_spnego_msg *msg;
924 struct key *spnego_key = NULL;
925 struct smb2_sess_setup_rsp *rsp = NULL;
926
927 rc = SMB2_sess_alloc_buffer(sess_data);
928 if (rc)
929 goto out;
930
931 spnego_key = cifs_get_spnego_key(ses);
932 if (IS_ERR(spnego_key)) {
933 rc = PTR_ERR(spnego_key);
934 spnego_key = NULL;
935 goto out;
936 }
937
938 msg = spnego_key->payload.data[0];
939 /*
940 * check version field to make sure that cifs.upcall is
941 * sending us a response in an expected form
942 */
943 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
944 cifs_dbg(VFS,
945 "bad cifs.upcall version. Expected %d got %d",
946 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
947 rc = -EKEYREJECTED;
948 goto out_put_spnego_key;
949 }
950
951 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
952 GFP_KERNEL);
953 if (!ses->auth_key.response) {
954 cifs_dbg(VFS,
955 "Kerberos can't allocate (%u bytes) memory",
956 msg->sesskey_len);
957 rc = -ENOMEM;
958 goto out_put_spnego_key;
959 }
960 ses->auth_key.len = msg->sesskey_len;
961
962 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
963 sess_data->iov[1].iov_len = msg->secblob_len;
964
965 rc = SMB2_sess_sendreceive(sess_data);
966 if (rc)
967 goto out_put_spnego_key;
968
969 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
970 ses->Suid = rsp->hdr.sync_hdr.SessionId;
971
972 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
973
974 rc = SMB2_sess_establish_session(sess_data);
975 out_put_spnego_key:
976 key_invalidate(spnego_key);
977 key_put(spnego_key);
978 out:
979 sess_data->result = rc;
980 sess_data->func = NULL;
981 SMB2_sess_free_buffer(sess_data);
982 }
983 #else
984 static void
985 SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
986 {
987 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
988 sess_data->result = -EOPNOTSUPP;
989 sess_data->func = NULL;
990 }
991 #endif
992
993 static void
994 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
995
996 static void
997 SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
998 {
999 int rc;
1000 struct cifs_ses *ses = sess_data->ses;
1001 struct smb2_sess_setup_rsp *rsp = NULL;
1002 char *ntlmssp_blob = NULL;
1003 bool use_spnego = false; /* else use raw ntlmssp */
1004 u16 blob_length = 0;
1005
1006 /*
1007 * If memory allocation is successful, caller of this function
1008 * frees it.
1009 */
1010 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1011 if (!ses->ntlmssp) {
1012 rc = -ENOMEM;
1013 goto out_err;
1014 }
1015 ses->ntlmssp->sesskey_per_smbsess = true;
1016
1017 rc = SMB2_sess_alloc_buffer(sess_data);
1018 if (rc)
1019 goto out_err;
1020
1021 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
1022 GFP_KERNEL);
1023 if (ntlmssp_blob == NULL) {
1024 rc = -ENOMEM;
1025 goto out;
1026 }
1027
1028 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
1029 if (use_spnego) {
1030 /* BB eventually need to add this */
1031 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1032 rc = -EOPNOTSUPP;
1033 goto out;
1034 } else {
1035 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
1036 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
1037 }
1038 sess_data->iov[1].iov_base = ntlmssp_blob;
1039 sess_data->iov[1].iov_len = blob_length;
1040
1041 rc = SMB2_sess_sendreceive(sess_data);
1042 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1043
1044 /* If true, rc here is expected and not an error */
1045 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1046 rsp->hdr.sync_hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
1047 rc = 0;
1048
1049 if (rc)
1050 goto out;
1051
1052 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
1053 le16_to_cpu(rsp->SecurityBufferOffset)) {
1054 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
1055 le16_to_cpu(rsp->SecurityBufferOffset));
1056 rc = -EIO;
1057 goto out;
1058 }
1059 rc = decode_ntlmssp_challenge(rsp->Buffer,
1060 le16_to_cpu(rsp->SecurityBufferLength), ses);
1061 if (rc)
1062 goto out;
1063
1064 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1065
1066
1067 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1068 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1069
1070 out:
1071 kfree(ntlmssp_blob);
1072 SMB2_sess_free_buffer(sess_data);
1073 if (!rc) {
1074 sess_data->result = 0;
1075 sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
1076 return;
1077 }
1078 out_err:
1079 kfree(ses->ntlmssp);
1080 ses->ntlmssp = NULL;
1081 sess_data->result = rc;
1082 sess_data->func = NULL;
1083 }
1084
1085 static void
1086 SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
1087 {
1088 int rc;
1089 struct cifs_ses *ses = sess_data->ses;
1090 struct smb2_sess_setup_req *req;
1091 struct smb2_sess_setup_rsp *rsp = NULL;
1092 unsigned char *ntlmssp_blob = NULL;
1093 bool use_spnego = false; /* else use raw ntlmssp */
1094 u16 blob_length = 0;
1095
1096 rc = SMB2_sess_alloc_buffer(sess_data);
1097 if (rc)
1098 goto out;
1099
1100 req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
1101 req->hdr.sync_hdr.SessionId = ses->Suid;
1102
1103 rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length, ses,
1104 sess_data->nls_cp);
1105 if (rc) {
1106 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
1107 goto out;
1108 }
1109
1110 if (use_spnego) {
1111 /* BB eventually need to add this */
1112 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
1113 rc = -EOPNOTSUPP;
1114 goto out;
1115 }
1116 sess_data->iov[1].iov_base = ntlmssp_blob;
1117 sess_data->iov[1].iov_len = blob_length;
1118
1119 rc = SMB2_sess_sendreceive(sess_data);
1120 if (rc)
1121 goto out;
1122
1123 rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
1124
1125 ses->Suid = rsp->hdr.sync_hdr.SessionId;
1126 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
1127
1128 rc = SMB2_sess_establish_session(sess_data);
1129 out:
1130 kfree(ntlmssp_blob);
1131 SMB2_sess_free_buffer(sess_data);
1132 kfree(ses->ntlmssp);
1133 ses->ntlmssp = NULL;
1134 sess_data->result = rc;
1135 sess_data->func = NULL;
1136 }
1137
1138 static int
1139 SMB2_select_sec(struct cifs_ses *ses, struct SMB2_sess_data *sess_data)
1140 {
1141 int type;
1142
1143 type = smb2_select_sectype(ses->server, ses->sectype);
1144 cifs_dbg(FYI, "sess setup type %d\n", type);
1145 if (type == Unspecified) {
1146 cifs_dbg(VFS,
1147 "Unable to select appropriate authentication method!");
1148 return -EINVAL;
1149 }
1150
1151 switch (type) {
1152 case Kerberos:
1153 sess_data->func = SMB2_auth_kerberos;
1154 break;
1155 case RawNTLMSSP:
1156 sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
1157 break;
1158 default:
1159 cifs_dbg(VFS, "secType %d not supported!\n", type);
1160 return -EOPNOTSUPP;
1161 }
1162
1163 return 0;
1164 }
1165
1166 int
1167 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
1168 const struct nls_table *nls_cp)
1169 {
1170 int rc = 0;
1171 struct TCP_Server_Info *server = ses->server;
1172 struct SMB2_sess_data *sess_data;
1173
1174 cifs_dbg(FYI, "Session Setup\n");
1175
1176 if (!server) {
1177 WARN(1, "%s: server is NULL!\n", __func__);
1178 return -EIO;
1179 }
1180
1181 sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
1182 if (!sess_data)
1183 return -ENOMEM;
1184
1185 rc = SMB2_select_sec(ses, sess_data);
1186 if (rc)
1187 goto out;
1188 sess_data->xid = xid;
1189 sess_data->ses = ses;
1190 sess_data->buf0_type = CIFS_NO_BUFFER;
1191 sess_data->nls_cp = (struct nls_table *) nls_cp;
1192 sess_data->previous_session = ses->Suid;
1193
1194 while (sess_data->func)
1195 sess_data->func(sess_data);
1196
1197 if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
1198 cifs_dbg(VFS, "signing requested but authenticated as guest\n");
1199 rc = sess_data->result;
1200 out:
1201 kfree(sess_data);
1202 return rc;
1203 }
1204
1205 int
1206 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
1207 {
1208 struct smb2_logoff_req *req; /* response is also trivial struct */
1209 int rc = 0;
1210 struct TCP_Server_Info *server;
1211 int flags = 0;
1212
1213 cifs_dbg(FYI, "disconnect session %p\n", ses);
1214
1215 if (ses && (ses->server))
1216 server = ses->server;
1217 else
1218 return -EIO;
1219
1220 /* no need to send SMB logoff if uid already closed due to reconnect */
1221 if (ses->need_reconnect)
1222 goto smb2_session_already_dead;
1223
1224 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
1225 if (rc)
1226 return rc;
1227
1228 /* since no tcon, smb2_init can not do this, so do here */
1229 req->hdr.sync_hdr.SessionId = ses->Suid;
1230
1231 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
1232 flags |= CIFS_TRANSFORM_REQ;
1233 else if (server->sign)
1234 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1235
1236 rc = SendReceiveNoRsp(xid, ses, (char *) req, flags);
1237 cifs_small_buf_release(req);
1238 /*
1239 * No tcon so can't do
1240 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
1241 */
1242
1243 smb2_session_already_dead:
1244 return rc;
1245 }
1246
1247 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
1248 {
1249 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
1250 }
1251
1252 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
1253
1254 /* These are similar values to what Windows uses */
1255 static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
1256 {
1257 tcon->max_chunks = 256;
1258 tcon->max_bytes_chunk = 1048576;
1259 tcon->max_bytes_copy = 16777216;
1260 }
1261
1262 int
1263 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
1264 struct cifs_tcon *tcon, const struct nls_table *cp)
1265 {
1266 struct smb2_tree_connect_req *req;
1267 struct smb2_tree_connect_rsp *rsp = NULL;
1268 struct kvec iov[2];
1269 struct kvec rsp_iov = { NULL, 0 };
1270 int rc = 0;
1271 int resp_buftype;
1272 int unc_path_len;
1273 __le16 *unc_path = NULL;
1274 int flags = 0;
1275
1276 cifs_dbg(FYI, "TCON\n");
1277
1278 if (!(ses->server) || !tree)
1279 return -EIO;
1280
1281 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
1282 if (unc_path == NULL)
1283 return -ENOMEM;
1284
1285 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
1286 unc_path_len *= 2;
1287 if (unc_path_len < 2) {
1288 kfree(unc_path);
1289 return -EINVAL;
1290 }
1291
1292 /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
1293 tcon->tid = 0;
1294
1295 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
1296 if (rc) {
1297 kfree(unc_path);
1298 return rc;
1299 }
1300
1301 if (encryption_required(tcon))
1302 flags |= CIFS_TRANSFORM_REQ;
1303
1304 iov[0].iov_base = (char *)req;
1305 /* 4 for rfc1002 length field and 1 for pad */
1306 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1307
1308 /* Testing shows that buffer offset must be at location of Buffer[0] */
1309 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
1310 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
1311 req->PathLength = cpu_to_le16(unc_path_len - 2);
1312 iov[1].iov_base = unc_path;
1313 iov[1].iov_len = unc_path_len;
1314
1315 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
1316
1317 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
1318 cifs_small_buf_release(req);
1319 rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
1320
1321 if (rc != 0) {
1322 if (tcon) {
1323 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
1324 tcon->need_reconnect = true;
1325 }
1326 goto tcon_error_exit;
1327 }
1328
1329 switch (rsp->ShareType) {
1330 case SMB2_SHARE_TYPE_DISK:
1331 cifs_dbg(FYI, "connection to disk share\n");
1332 break;
1333 case SMB2_SHARE_TYPE_PIPE:
1334 tcon->pipe = true;
1335 cifs_dbg(FYI, "connection to pipe share\n");
1336 break;
1337 case SMB2_SHARE_TYPE_PRINT:
1338 tcon->print = true;
1339 cifs_dbg(FYI, "connection to printer\n");
1340 break;
1341 default:
1342 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
1343 rc = -EOPNOTSUPP;
1344 goto tcon_error_exit;
1345 }
1346
1347 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
1348 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
1349 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1350 tcon->tidStatus = CifsGood;
1351 tcon->need_reconnect = false;
1352 tcon->tid = rsp->hdr.sync_hdr.TreeId;
1353 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
1354
1355 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
1356 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
1357 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
1358
1359 if (tcon->seal &&
1360 !(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
1361 cifs_dbg(VFS, "Encryption is requested but not supported\n");
1362
1363 init_copy_chunk_defaults(tcon);
1364 if (tcon->ses->server->ops->validate_negotiate)
1365 rc = tcon->ses->server->ops->validate_negotiate(xid, tcon);
1366 tcon_exit:
1367 free_rsp_buf(resp_buftype, rsp);
1368 kfree(unc_path);
1369 return rc;
1370
1371 tcon_error_exit:
1372 if (rsp && rsp->hdr.sync_hdr.Status == STATUS_BAD_NETWORK_NAME) {
1373 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
1374 }
1375 goto tcon_exit;
1376 }
1377
1378 int
1379 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
1380 {
1381 struct smb2_tree_disconnect_req *req; /* response is trivial */
1382 int rc = 0;
1383 struct cifs_ses *ses = tcon->ses;
1384 int flags = 0;
1385
1386 cifs_dbg(FYI, "Tree Disconnect\n");
1387
1388 if (!ses || !(ses->server))
1389 return -EIO;
1390
1391 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
1392 return 0;
1393
1394 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
1395 if (rc)
1396 return rc;
1397
1398 if (encryption_required(tcon))
1399 flags |= CIFS_TRANSFORM_REQ;
1400
1401 rc = SendReceiveNoRsp(xid, ses, (char *)req, flags);
1402 cifs_small_buf_release(req);
1403 if (rc)
1404 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
1405
1406 return rc;
1407 }
1408
1409
1410 static struct create_durable *
1411 create_durable_buf(void)
1412 {
1413 struct create_durable *buf;
1414
1415 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1416 if (!buf)
1417 return NULL;
1418
1419 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1420 (struct create_durable, Data));
1421 buf->ccontext.DataLength = cpu_to_le32(16);
1422 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1423 (struct create_durable, Name));
1424 buf->ccontext.NameLength = cpu_to_le16(4);
1425 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
1426 buf->Name[0] = 'D';
1427 buf->Name[1] = 'H';
1428 buf->Name[2] = 'n';
1429 buf->Name[3] = 'Q';
1430 return buf;
1431 }
1432
1433 static struct create_durable *
1434 create_reconnect_durable_buf(struct cifs_fid *fid)
1435 {
1436 struct create_durable *buf;
1437
1438 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
1439 if (!buf)
1440 return NULL;
1441
1442 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1443 (struct create_durable, Data));
1444 buf->ccontext.DataLength = cpu_to_le32(16);
1445 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1446 (struct create_durable, Name));
1447 buf->ccontext.NameLength = cpu_to_le16(4);
1448 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
1449 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
1450 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
1451 buf->Name[0] = 'D';
1452 buf->Name[1] = 'H';
1453 buf->Name[2] = 'n';
1454 buf->Name[3] = 'C';
1455 return buf;
1456 }
1457
1458 static __u8
1459 parse_lease_state(struct TCP_Server_Info *server, struct smb2_create_rsp *rsp,
1460 unsigned int *epoch)
1461 {
1462 char *data_offset;
1463 struct create_context *cc;
1464 unsigned int next;
1465 unsigned int remaining;
1466 char *name;
1467
1468 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
1469 remaining = le32_to_cpu(rsp->CreateContextsLength);
1470 cc = (struct create_context *)data_offset;
1471 while (remaining >= sizeof(struct create_context)) {
1472 name = le16_to_cpu(cc->NameOffset) + (char *)cc;
1473 if (le16_to_cpu(cc->NameLength) == 4 &&
1474 strncmp(name, "RqLs", 4) == 0)
1475 return server->ops->parse_lease_buf(cc, epoch);
1476
1477 next = le32_to_cpu(cc->Next);
1478 if (!next)
1479 break;
1480 remaining -= next;
1481 cc = (struct create_context *)((char *)cc + next);
1482 }
1483
1484 return 0;
1485 }
1486
1487 static int
1488 add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
1489 unsigned int *num_iovec, __u8 *oplock)
1490 {
1491 struct smb2_create_req *req = iov[0].iov_base;
1492 unsigned int num = *num_iovec;
1493
1494 iov[num].iov_base = server->ops->create_lease_buf(oplock+1, *oplock);
1495 if (iov[num].iov_base == NULL)
1496 return -ENOMEM;
1497 iov[num].iov_len = server->vals->create_lease_size;
1498 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
1499 if (!req->CreateContextsOffset)
1500 req->CreateContextsOffset = cpu_to_le32(
1501 sizeof(struct smb2_create_req) - 4 +
1502 iov[num - 1].iov_len);
1503 le32_add_cpu(&req->CreateContextsLength,
1504 server->vals->create_lease_size);
1505 inc_rfc1001_len(&req->hdr, server->vals->create_lease_size);
1506 *num_iovec = num + 1;
1507 return 0;
1508 }
1509
1510 static struct create_durable_v2 *
1511 create_durable_v2_buf(struct cifs_fid *pfid)
1512 {
1513 struct create_durable_v2 *buf;
1514
1515 buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
1516 if (!buf)
1517 return NULL;
1518
1519 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1520 (struct create_durable_v2, dcontext));
1521 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
1522 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1523 (struct create_durable_v2, Name));
1524 buf->ccontext.NameLength = cpu_to_le16(4);
1525
1526 buf->dcontext.Timeout = 0; /* Should this be configurable by workload */
1527 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1528 generate_random_uuid(buf->dcontext.CreateGuid);
1529 memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
1530
1531 /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
1532 buf->Name[0] = 'D';
1533 buf->Name[1] = 'H';
1534 buf->Name[2] = '2';
1535 buf->Name[3] = 'Q';
1536 return buf;
1537 }
1538
1539 static struct create_durable_handle_reconnect_v2 *
1540 create_reconnect_durable_v2_buf(struct cifs_fid *fid)
1541 {
1542 struct create_durable_handle_reconnect_v2 *buf;
1543
1544 buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
1545 GFP_KERNEL);
1546 if (!buf)
1547 return NULL;
1548
1549 buf->ccontext.DataOffset =
1550 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1551 dcontext));
1552 buf->ccontext.DataLength =
1553 cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
1554 buf->ccontext.NameOffset =
1555 cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
1556 Name));
1557 buf->ccontext.NameLength = cpu_to_le16(4);
1558
1559 buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
1560 buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
1561 buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
1562 memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
1563
1564 /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
1565 buf->Name[0] = 'D';
1566 buf->Name[1] = 'H';
1567 buf->Name[2] = '2';
1568 buf->Name[3] = 'C';
1569 return buf;
1570 }
1571
1572 static int
1573 add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
1574 struct cifs_open_parms *oparms)
1575 {
1576 struct smb2_create_req *req = iov[0].iov_base;
1577 unsigned int num = *num_iovec;
1578
1579 iov[num].iov_base = create_durable_v2_buf(oparms->fid);
1580 if (iov[num].iov_base == NULL)
1581 return -ENOMEM;
1582 iov[num].iov_len = sizeof(struct create_durable_v2);
1583 if (!req->CreateContextsOffset)
1584 req->CreateContextsOffset =
1585 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1586 iov[1].iov_len);
1587 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
1588 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable_v2));
1589 *num_iovec = num + 1;
1590 return 0;
1591 }
1592
1593 static int
1594 add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
1595 struct cifs_open_parms *oparms)
1596 {
1597 struct smb2_create_req *req = iov[0].iov_base;
1598 unsigned int num = *num_iovec;
1599
1600 /* indicate that we don't need to relock the file */
1601 oparms->reconnect = false;
1602
1603 iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
1604 if (iov[num].iov_base == NULL)
1605 return -ENOMEM;
1606 iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
1607 if (!req->CreateContextsOffset)
1608 req->CreateContextsOffset =
1609 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1610 iov[1].iov_len);
1611 le32_add_cpu(&req->CreateContextsLength,
1612 sizeof(struct create_durable_handle_reconnect_v2));
1613 inc_rfc1001_len(&req->hdr,
1614 sizeof(struct create_durable_handle_reconnect_v2));
1615 *num_iovec = num + 1;
1616 return 0;
1617 }
1618
1619 static int
1620 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
1621 struct cifs_open_parms *oparms, bool use_persistent)
1622 {
1623 struct smb2_create_req *req = iov[0].iov_base;
1624 unsigned int num = *num_iovec;
1625
1626 if (use_persistent) {
1627 if (oparms->reconnect)
1628 return add_durable_reconnect_v2_context(iov, num_iovec,
1629 oparms);
1630 else
1631 return add_durable_v2_context(iov, num_iovec, oparms);
1632 }
1633
1634 if (oparms->reconnect) {
1635 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
1636 /* indicate that we don't need to relock the file */
1637 oparms->reconnect = false;
1638 } else
1639 iov[num].iov_base = create_durable_buf();
1640 if (iov[num].iov_base == NULL)
1641 return -ENOMEM;
1642 iov[num].iov_len = sizeof(struct create_durable);
1643 if (!req->CreateContextsOffset)
1644 req->CreateContextsOffset =
1645 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1646 iov[1].iov_len);
1647 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1648 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1649 *num_iovec = num + 1;
1650 return 0;
1651 }
1652
1653 static int
1654 alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
1655 const char *treename, const __le16 *path)
1656 {
1657 int treename_len, path_len;
1658 struct nls_table *cp;
1659 const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
1660
1661 /*
1662 * skip leading "\\"
1663 */
1664 treename_len = strlen(treename);
1665 if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
1666 return -EINVAL;
1667
1668 treename += 2;
1669 treename_len -= 2;
1670
1671 path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
1672
1673 /*
1674 * make room for one path separator between the treename and
1675 * path
1676 */
1677 *out_len = treename_len + 1 + path_len;
1678
1679 /*
1680 * final path needs to be null-terminated UTF16 with a
1681 * size aligned to 8
1682 */
1683
1684 *out_size = roundup((*out_len+1)*2, 8);
1685 *out_path = kzalloc(*out_size, GFP_KERNEL);
1686 if (!*out_path)
1687 return -ENOMEM;
1688
1689 cp = load_nls_default();
1690 cifs_strtoUTF16(*out_path, treename, treename_len, cp);
1691 UniStrcat(*out_path, sep);
1692 UniStrcat(*out_path, path);
1693 unload_nls(cp);
1694
1695 return 0;
1696 }
1697
1698 int
1699 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1700 __u8 *oplock, struct smb2_file_all_info *buf,
1701 struct smb2_err_rsp **err_buf)
1702 {
1703 struct smb2_create_req *req;
1704 struct smb2_create_rsp *rsp;
1705 struct TCP_Server_Info *server;
1706 struct cifs_tcon *tcon = oparms->tcon;
1707 struct cifs_ses *ses = tcon->ses;
1708 struct kvec iov[4];
1709 struct kvec rsp_iov = {NULL, 0};
1710 int resp_buftype;
1711 int uni_path_len;
1712 __le16 *copy_path = NULL;
1713 int copy_size;
1714 int rc = 0;
1715 unsigned int n_iov = 2;
1716 __u32 file_attributes = 0;
1717 char *dhc_buf = NULL, *lc_buf = NULL;
1718 int flags = 0;
1719
1720 cifs_dbg(FYI, "create/open\n");
1721
1722 if (ses && (ses->server))
1723 server = ses->server;
1724 else
1725 return -EIO;
1726
1727 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1728 if (rc)
1729 return rc;
1730
1731 if (encryption_required(tcon))
1732 flags |= CIFS_TRANSFORM_REQ;
1733
1734 if (oparms->create_options & CREATE_OPTION_READONLY)
1735 file_attributes |= ATTR_READONLY;
1736 if (oparms->create_options & CREATE_OPTION_SPECIAL)
1737 file_attributes |= ATTR_SYSTEM;
1738
1739 req->ImpersonationLevel = IL_IMPERSONATION;
1740 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1741 /* File attributes ignored on open (used in create though) */
1742 req->FileAttributes = cpu_to_le32(file_attributes);
1743 req->ShareAccess = FILE_SHARE_ALL_LE;
1744 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1745 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1746
1747 iov[0].iov_base = (char *)req;
1748 /* 4 for rfc1002 length field */
1749 iov[0].iov_len = get_rfc1002_length(req) + 4;
1750 /* -1 since last byte is buf[0] which is sent below (path) */
1751 iov[0].iov_len--;
1752
1753 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1754
1755 /* [MS-SMB2] 2.2.13 NameOffset:
1756 * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
1757 * the SMB2 header, the file name includes a prefix that will
1758 * be processed during DFS name normalization as specified in
1759 * section 3.3.5.9. Otherwise, the file name is relative to
1760 * the share that is identified by the TreeId in the SMB2
1761 * header.
1762 */
1763 if (tcon->share_flags & SHI1005_FLAGS_DFS) {
1764 int name_len;
1765
1766 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
1767 rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
1768 &name_len,
1769 tcon->treeName, path);
1770 if (rc) {
1771 cifs_small_buf_release(req);
1772 return rc;
1773 }
1774 req->NameLength = cpu_to_le16(name_len * 2);
1775 uni_path_len = copy_size;
1776 path = copy_path;
1777 } else {
1778 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1779 /* MUST set path len (NameLength) to 0 opening root of share */
1780 req->NameLength = cpu_to_le16(uni_path_len - 2);
1781 if (uni_path_len % 8 != 0) {
1782 copy_size = roundup(uni_path_len, 8);
1783 copy_path = kzalloc(copy_size, GFP_KERNEL);
1784 if (!copy_path) {
1785 cifs_small_buf_release(req);
1786 return -ENOMEM;
1787 }
1788 memcpy((char *)copy_path, (const char *)path,
1789 uni_path_len);
1790 uni_path_len = copy_size;
1791 path = copy_path;
1792 }
1793 }
1794
1795 iov[1].iov_len = uni_path_len;
1796 iov[1].iov_base = path;
1797 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1798 inc_rfc1001_len(req, uni_path_len - 1);
1799
1800 if (!server->oplocks)
1801 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1802
1803 if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1804 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1805 req->RequestedOplockLevel = *oplock;
1806 else {
1807 rc = add_lease_context(server, iov, &n_iov, oplock);
1808 if (rc) {
1809 cifs_small_buf_release(req);
1810 kfree(copy_path);
1811 return rc;
1812 }
1813 lc_buf = iov[n_iov-1].iov_base;
1814 }
1815
1816 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1817 /* need to set Next field of lease context if we request it */
1818 if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1819 struct create_context *ccontext =
1820 (struct create_context *)iov[n_iov-1].iov_base;
1821 ccontext->Next =
1822 cpu_to_le32(server->vals->create_lease_size);
1823 }
1824
1825 rc = add_durable_context(iov, &n_iov, oparms,
1826 tcon->use_persistent);
1827 if (rc) {
1828 cifs_small_buf_release(req);
1829 kfree(copy_path);
1830 kfree(lc_buf);
1831 return rc;
1832 }
1833 dhc_buf = iov[n_iov-1].iov_base;
1834 }
1835
1836 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
1837 cifs_small_buf_release(req);
1838 rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
1839
1840 if (rc != 0) {
1841 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1842 if (err_buf && rsp)
1843 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1844 GFP_KERNEL);
1845 goto creat_exit;
1846 }
1847
1848 oparms->fid->persistent_fid = rsp->PersistentFileId;
1849 oparms->fid->volatile_fid = rsp->VolatileFileId;
1850
1851 if (buf) {
1852 memcpy(buf, &rsp->CreationTime, 32);
1853 buf->AllocationSize = rsp->AllocationSize;
1854 buf->EndOfFile = rsp->EndofFile;
1855 buf->Attributes = rsp->FileAttributes;
1856 buf->NumberOfLinks = cpu_to_le32(1);
1857 buf->DeletePending = 0;
1858 }
1859
1860 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1861 *oplock = parse_lease_state(server, rsp, &oparms->fid->epoch);
1862 else
1863 *oplock = rsp->OplockLevel;
1864 creat_exit:
1865 kfree(copy_path);
1866 kfree(lc_buf);
1867 kfree(dhc_buf);
1868 free_rsp_buf(resp_buftype, rsp);
1869 return rc;
1870 }
1871
1872 /*
1873 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1874 */
1875 int
1876 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1877 u64 volatile_fid, u32 opcode, bool is_fsctl,
1878 char *in_data, u32 indatalen,
1879 char **out_data, u32 *plen /* returned data len */)
1880 {
1881 struct smb2_ioctl_req *req;
1882 struct smb2_ioctl_rsp *rsp;
1883 struct smb2_sync_hdr *shdr;
1884 struct cifs_ses *ses;
1885 struct kvec iov[2];
1886 struct kvec rsp_iov;
1887 int resp_buftype;
1888 int n_iov;
1889 int rc = 0;
1890 int flags = 0;
1891
1892 cifs_dbg(FYI, "SMB2 IOCTL\n");
1893
1894 if (out_data != NULL)
1895 *out_data = NULL;
1896
1897 /* zero out returned data len, in case of error */
1898 if (plen)
1899 *plen = 0;
1900
1901 if (tcon)
1902 ses = tcon->ses;
1903 else
1904 return -EIO;
1905
1906 if (!ses || !(ses->server))
1907 return -EIO;
1908
1909 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1910 if (rc)
1911 return rc;
1912
1913 if (encryption_required(tcon))
1914 flags |= CIFS_TRANSFORM_REQ;
1915
1916 req->CtlCode = cpu_to_le32(opcode);
1917 req->PersistentFileId = persistent_fid;
1918 req->VolatileFileId = volatile_fid;
1919
1920 if (indatalen) {
1921 req->InputCount = cpu_to_le32(indatalen);
1922 /* do not set InputOffset if no input data */
1923 req->InputOffset =
1924 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1925 iov[1].iov_base = in_data;
1926 iov[1].iov_len = indatalen;
1927 n_iov = 2;
1928 } else
1929 n_iov = 1;
1930
1931 req->OutputOffset = 0;
1932 req->OutputCount = 0; /* MBZ */
1933
1934 /*
1935 * Could increase MaxOutputResponse, but that would require more
1936 * than one credit. Windows typically sets this smaller, but for some
1937 * ioctls it may be useful to allow server to send more. No point
1938 * limiting what the server can send as long as fits in one credit
1939 * Unfortunately - we can not handle more than CIFS_MAX_MSG_SIZE
1940 * (by default, note that it can be overridden to make max larger)
1941 * in responses (except for read responses which can be bigger.
1942 * We may want to bump this limit up
1943 */
1944 req->MaxOutputResponse = cpu_to_le32(CIFSMaxBufSize);
1945
1946 if (is_fsctl)
1947 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1948 else
1949 req->Flags = 0;
1950
1951 iov[0].iov_base = (char *)req;
1952
1953 /*
1954 * If no input data, the size of ioctl struct in
1955 * protocol spec still includes a 1 byte data buffer,
1956 * but if input data passed to ioctl, we do not
1957 * want to double count this, so we do not send
1958 * the dummy one byte of data in iovec[0] if sending
1959 * input data (in iovec[1]). We also must add 4 bytes
1960 * in first iovec to allow for rfc1002 length field.
1961 */
1962
1963 if (indatalen) {
1964 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1965 inc_rfc1001_len(req, indatalen - 1);
1966 } else
1967 iov[0].iov_len = get_rfc1002_length(req) + 4;
1968
1969 /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
1970 if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
1971 req->hdr.sync_hdr.Flags |= SMB2_FLAGS_SIGNED;
1972
1973 rc = SendReceive2(xid, ses, iov, n_iov, &resp_buftype, flags, &rsp_iov);
1974 cifs_small_buf_release(req);
1975 rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
1976
1977 if ((rc != 0) && (rc != -EINVAL)) {
1978 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1979 goto ioctl_exit;
1980 } else if (rc == -EINVAL) {
1981 if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
1982 (opcode != FSCTL_SRV_COPYCHUNK)) {
1983 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1984 goto ioctl_exit;
1985 }
1986 }
1987
1988 /* check if caller wants to look at return data or just return rc */
1989 if ((plen == NULL) || (out_data == NULL))
1990 goto ioctl_exit;
1991
1992 *plen = le32_to_cpu(rsp->OutputCount);
1993
1994 /* We check for obvious errors in the output buffer length and offset */
1995 if (*plen == 0)
1996 goto ioctl_exit; /* server returned no data */
1997 else if (*plen > 0xFF00) {
1998 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1999 *plen = 0;
2000 rc = -EIO;
2001 goto ioctl_exit;
2002 }
2003
2004 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
2005 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
2006 le32_to_cpu(rsp->OutputOffset));
2007 *plen = 0;
2008 rc = -EIO;
2009 goto ioctl_exit;
2010 }
2011
2012 *out_data = kmalloc(*plen, GFP_KERNEL);
2013 if (*out_data == NULL) {
2014 rc = -ENOMEM;
2015 goto ioctl_exit;
2016 }
2017
2018 shdr = get_sync_hdr(rsp);
2019 memcpy(*out_data, (char *)shdr + le32_to_cpu(rsp->OutputOffset), *plen);
2020 ioctl_exit:
2021 free_rsp_buf(resp_buftype, rsp);
2022 return rc;
2023 }
2024
2025 /*
2026 * Individual callers to ioctl worker function follow
2027 */
2028
2029 int
2030 SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2031 u64 persistent_fid, u64 volatile_fid)
2032 {
2033 int rc;
2034 struct compress_ioctl fsctl_input;
2035 char *ret_data = NULL;
2036
2037 fsctl_input.CompressionState =
2038 cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
2039
2040 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
2041 FSCTL_SET_COMPRESSION, true /* is_fsctl */,
2042 (char *)&fsctl_input /* data input */,
2043 2 /* in data len */, &ret_data /* out data */, NULL);
2044
2045 cifs_dbg(FYI, "set compression rc %d\n", rc);
2046
2047 return rc;
2048 }
2049
2050 int
2051 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
2052 u64 persistent_fid, u64 volatile_fid)
2053 {
2054 struct smb2_close_req *req;
2055 struct smb2_close_rsp *rsp;
2056 struct cifs_ses *ses = tcon->ses;
2057 struct kvec iov[1];
2058 struct kvec rsp_iov;
2059 int resp_buftype;
2060 int rc = 0;
2061 int flags = 0;
2062
2063 cifs_dbg(FYI, "Close\n");
2064
2065 if (!ses || !(ses->server))
2066 return -EIO;
2067
2068 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
2069 if (rc)
2070 return rc;
2071
2072 if (encryption_required(tcon))
2073 flags |= CIFS_TRANSFORM_REQ;
2074
2075 req->PersistentFileId = persistent_fid;
2076 req->VolatileFileId = volatile_fid;
2077
2078 iov[0].iov_base = (char *)req;
2079 /* 4 for rfc1002 length field */
2080 iov[0].iov_len = get_rfc1002_length(req) + 4;
2081
2082 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2083 cifs_small_buf_release(req);
2084 rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
2085
2086 if (rc != 0) {
2087 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
2088 goto close_exit;
2089 }
2090
2091 /* BB FIXME - decode close response, update inode for caching */
2092
2093 close_exit:
2094 free_rsp_buf(resp_buftype, rsp);
2095 return rc;
2096 }
2097
2098 static int
2099 validate_buf(unsigned int offset, unsigned int buffer_length,
2100 struct smb2_hdr *hdr, unsigned int min_buf_size)
2101
2102 {
2103 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
2104 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
2105 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2106 char *end_of_buf = begin_of_buf + buffer_length;
2107
2108
2109 if (buffer_length < min_buf_size) {
2110 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
2111 buffer_length, min_buf_size);
2112 return -EINVAL;
2113 }
2114
2115 /* check if beyond RFC1001 maximum length */
2116 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
2117 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
2118 buffer_length, smb_len);
2119 return -EINVAL;
2120 }
2121
2122 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
2123 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
2124 return -EINVAL;
2125 }
2126
2127 return 0;
2128 }
2129
2130 /*
2131 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
2132 * Caller must free buffer.
2133 */
2134 static int
2135 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
2136 struct smb2_hdr *hdr, unsigned int minbufsize,
2137 char *data)
2138
2139 {
2140 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
2141 int rc;
2142
2143 if (!data)
2144 return -EINVAL;
2145
2146 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
2147 if (rc)
2148 return rc;
2149
2150 memcpy(data, begin_of_buf, buffer_length);
2151
2152 return 0;
2153 }
2154
2155 static int
2156 query_info(const unsigned int xid, struct cifs_tcon *tcon,
2157 u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
2158 u32 additional_info, size_t output_len, size_t min_len, void **data,
2159 u32 *dlen)
2160 {
2161 struct smb2_query_info_req *req;
2162 struct smb2_query_info_rsp *rsp = NULL;
2163 struct kvec iov[2];
2164 struct kvec rsp_iov;
2165 int rc = 0;
2166 int resp_buftype;
2167 struct cifs_ses *ses = tcon->ses;
2168 int flags = 0;
2169
2170 cifs_dbg(FYI, "Query Info\n");
2171
2172 if (!ses || !(ses->server))
2173 return -EIO;
2174
2175 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2176 if (rc)
2177 return rc;
2178
2179 if (encryption_required(tcon))
2180 flags |= CIFS_TRANSFORM_REQ;
2181
2182 req->InfoType = info_type;
2183 req->FileInfoClass = info_class;
2184 req->PersistentFileId = persistent_fid;
2185 req->VolatileFileId = volatile_fid;
2186 req->AdditionalInformation = cpu_to_le32(additional_info);
2187
2188 /*
2189 * We do not use the input buffer (do not send extra byte)
2190 */
2191 req->InputBufferOffset = 0;
2192 inc_rfc1001_len(req, -1);
2193
2194 req->OutputBufferLength = cpu_to_le32(output_len);
2195
2196 iov[0].iov_base = (char *)req;
2197 /* 4 for rfc1002 length field */
2198 iov[0].iov_len = get_rfc1002_length(req) + 4;
2199
2200 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2201 cifs_small_buf_release(req);
2202 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2203
2204 if (rc) {
2205 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2206 goto qinf_exit;
2207 }
2208
2209 if (dlen) {
2210 *dlen = le32_to_cpu(rsp->OutputBufferLength);
2211 if (!*data) {
2212 *data = kmalloc(*dlen, GFP_KERNEL);
2213 if (!*data) {
2214 cifs_dbg(VFS,
2215 "Error %d allocating memory for acl\n",
2216 rc);
2217 *dlen = 0;
2218 goto qinf_exit;
2219 }
2220 }
2221 }
2222
2223 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
2224 le32_to_cpu(rsp->OutputBufferLength),
2225 &rsp->hdr, min_len, *data);
2226
2227 qinf_exit:
2228 free_rsp_buf(resp_buftype, rsp);
2229 return rc;
2230 }
2231
2232 int SMB2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
2233 u64 persistent_fid, u64 volatile_fid,
2234 int ea_buf_size, struct smb2_file_full_ea_info *data)
2235 {
2236 return query_info(xid, tcon, persistent_fid, volatile_fid,
2237 FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE, 0,
2238 ea_buf_size,
2239 sizeof(struct smb2_file_full_ea_info),
2240 (void **)&data,
2241 NULL);
2242 }
2243
2244 int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
2245 u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
2246 {
2247 return query_info(xid, tcon, persistent_fid, volatile_fid,
2248 FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
2249 sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
2250 sizeof(struct smb2_file_all_info), (void **)&data,
2251 NULL);
2252 }
2253
2254 int
2255 SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
2256 u64 persistent_fid, u64 volatile_fid,
2257 void **data, u32 *plen)
2258 {
2259 __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO;
2260 *plen = 0;
2261
2262 return query_info(xid, tcon, persistent_fid, volatile_fid,
2263 0, SMB2_O_INFO_SECURITY, additional_info,
2264 SMB2_MAX_BUFFER_SIZE,
2265 sizeof(struct smb2_file_all_info), data, plen);
2266 }
2267
2268 int
2269 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
2270 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
2271 {
2272 return query_info(xid, tcon, persistent_fid, volatile_fid,
2273 FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
2274 sizeof(struct smb2_file_internal_info),
2275 sizeof(struct smb2_file_internal_info),
2276 (void **)&uniqueid, NULL);
2277 }
2278
2279 /*
2280 * This is a no-op for now. We're not really interested in the reply, but
2281 * rather in the fact that the server sent one and that server->lstrp
2282 * gets updated.
2283 *
2284 * FIXME: maybe we should consider checking that the reply matches request?
2285 */
2286 static void
2287 smb2_echo_callback(struct mid_q_entry *mid)
2288 {
2289 struct TCP_Server_Info *server = mid->callback_data;
2290 struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
2291 unsigned int credits_received = 1;
2292
2293 if (mid->mid_state == MID_RESPONSE_RECEIVED)
2294 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2295
2296 DeleteMidQEntry(mid);
2297 add_credits(server, credits_received, CIFS_ECHO_OP);
2298 }
2299
2300 void smb2_reconnect_server(struct work_struct *work)
2301 {
2302 struct TCP_Server_Info *server = container_of(work,
2303 struct TCP_Server_Info, reconnect.work);
2304 struct cifs_ses *ses;
2305 struct cifs_tcon *tcon, *tcon2;
2306 struct list_head tmp_list;
2307 int tcon_exist = false;
2308 int rc;
2309 int resched = false;
2310
2311
2312 /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
2313 mutex_lock(&server->reconnect_mutex);
2314
2315 INIT_LIST_HEAD(&tmp_list);
2316 cifs_dbg(FYI, "Need negotiate, reconnecting tcons\n");
2317
2318 spin_lock(&cifs_tcp_ses_lock);
2319 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2320 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2321 if (tcon->need_reconnect || tcon->need_reopen_files) {
2322 tcon->tc_count++;
2323 list_add_tail(&tcon->rlist, &tmp_list);
2324 tcon_exist = true;
2325 }
2326 }
2327 if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
2328 list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
2329 tcon_exist = true;
2330 }
2331 }
2332 /*
2333 * Get the reference to server struct to be sure that the last call of
2334 * cifs_put_tcon() in the loop below won't release the server pointer.
2335 */
2336 if (tcon_exist)
2337 server->srv_count++;
2338
2339 spin_unlock(&cifs_tcp_ses_lock);
2340
2341 list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
2342 rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon);
2343 if (!rc)
2344 cifs_reopen_persistent_handles(tcon);
2345 else
2346 resched = true;
2347 list_del_init(&tcon->rlist);
2348 cifs_put_tcon(tcon);
2349 }
2350
2351 cifs_dbg(FYI, "Reconnecting tcons finished\n");
2352 if (resched)
2353 queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
2354 mutex_unlock(&server->reconnect_mutex);
2355
2356 /* now we can safely release srv struct */
2357 if (tcon_exist)
2358 cifs_put_tcp_session(server, 1);
2359 }
2360
2361 int
2362 SMB2_echo(struct TCP_Server_Info *server)
2363 {
2364 struct smb2_echo_req *req;
2365 int rc = 0;
2366 struct kvec iov[2];
2367 struct smb_rqst rqst = { .rq_iov = iov,
2368 .rq_nvec = 2 };
2369
2370 cifs_dbg(FYI, "In echo request\n");
2371
2372 if (server->tcpStatus == CifsNeedNegotiate) {
2373 /* No need to send echo on newly established connections */
2374 queue_delayed_work(cifsiod_wq, &server->reconnect, 0);
2375 return rc;
2376 }
2377
2378 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
2379 if (rc)
2380 return rc;
2381
2382 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
2383
2384 /* 4 for rfc1002 length field */
2385 iov[0].iov_len = 4;
2386 iov[0].iov_base = (char *)req;
2387 iov[1].iov_len = get_rfc1002_length(req);
2388 iov[1].iov_base = (char *)req + 4;
2389
2390 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
2391 server, CIFS_ECHO_OP);
2392 if (rc)
2393 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
2394
2395 cifs_small_buf_release(req);
2396 return rc;
2397 }
2398
2399 int
2400 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2401 u64 volatile_fid)
2402 {
2403 struct smb2_flush_req *req;
2404 struct cifs_ses *ses = tcon->ses;
2405 struct kvec iov[1];
2406 struct kvec rsp_iov;
2407 int resp_buftype;
2408 int rc = 0;
2409 int flags = 0;
2410
2411 cifs_dbg(FYI, "Flush\n");
2412
2413 if (!ses || !(ses->server))
2414 return -EIO;
2415
2416 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
2417 if (rc)
2418 return rc;
2419
2420 if (encryption_required(tcon))
2421 flags |= CIFS_TRANSFORM_REQ;
2422
2423 req->PersistentFileId = persistent_fid;
2424 req->VolatileFileId = volatile_fid;
2425
2426 iov[0].iov_base = (char *)req;
2427 /* 4 for rfc1002 length field */
2428 iov[0].iov_len = get_rfc1002_length(req) + 4;
2429
2430 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags, &rsp_iov);
2431 cifs_small_buf_release(req);
2432
2433 if (rc != 0)
2434 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
2435
2436 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2437 return rc;
2438 }
2439
2440 /*
2441 * To form a chain of read requests, any read requests after the first should
2442 * have the end_of_chain boolean set to true.
2443 */
2444 static int
2445 smb2_new_read_req(void **buf, unsigned int *total_len,
2446 struct cifs_io_parms *io_parms, unsigned int remaining_bytes,
2447 int request_type)
2448 {
2449 int rc = -EACCES;
2450 struct smb2_read_plain_req *req = NULL;
2451 struct smb2_sync_hdr *shdr;
2452
2453 rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, (void **) &req,
2454 total_len);
2455 if (rc)
2456 return rc;
2457 if (io_parms->tcon->ses->server == NULL)
2458 return -ECONNABORTED;
2459
2460 shdr = &req->sync_hdr;
2461 shdr->ProcessId = cpu_to_le32(io_parms->pid);
2462
2463 req->PersistentFileId = io_parms->persistent_fid;
2464 req->VolatileFileId = io_parms->volatile_fid;
2465 req->ReadChannelInfoOffset = 0; /* reserved */
2466 req->ReadChannelInfoLength = 0; /* reserved */
2467 req->Channel = 0; /* reserved */
2468 req->MinimumCount = 0;
2469 req->Length = cpu_to_le32(io_parms->length);
2470 req->Offset = cpu_to_le64(io_parms->offset);
2471
2472 if (request_type & CHAINED_REQUEST) {
2473 if (!(request_type & END_OF_CHAIN)) {
2474 /* next 8-byte aligned request */
2475 *total_len = DIV_ROUND_UP(*total_len, 8) * 8;
2476 shdr->NextCommand = cpu_to_le32(*total_len);
2477 } else /* END_OF_CHAIN */
2478 shdr->NextCommand = 0;
2479 if (request_type & RELATED_REQUEST) {
2480 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2481 /*
2482 * Related requests use info from previous read request
2483 * in chain.
2484 */
2485 shdr->SessionId = 0xFFFFFFFF;
2486 shdr->TreeId = 0xFFFFFFFF;
2487 req->PersistentFileId = 0xFFFFFFFF;
2488 req->VolatileFileId = 0xFFFFFFFF;
2489 }
2490 }
2491 if (remaining_bytes > io_parms->length)
2492 req->RemainingBytes = cpu_to_le32(remaining_bytes);
2493 else
2494 req->RemainingBytes = 0;
2495
2496 *buf = req;
2497 return rc;
2498 }
2499
2500 static void
2501 smb2_readv_callback(struct mid_q_entry *mid)
2502 {
2503 struct cifs_readdata *rdata = mid->callback_data;
2504 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
2505 struct TCP_Server_Info *server = tcon->ses->server;
2506 struct smb2_sync_hdr *shdr =
2507 (struct smb2_sync_hdr *)rdata->iov[1].iov_base;
2508 unsigned int credits_received = 1;
2509 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2510 .rq_nvec = 2,
2511 .rq_pages = rdata->pages,
2512 .rq_npages = rdata->nr_pages,
2513 .rq_pagesz = rdata->pagesz,
2514 .rq_tailsz = rdata->tailsz };
2515
2516 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
2517 __func__, mid->mid, mid->mid_state, rdata->result,
2518 rdata->bytes);
2519
2520 switch (mid->mid_state) {
2521 case MID_RESPONSE_RECEIVED:
2522 credits_received = le16_to_cpu(shdr->CreditRequest);
2523 /* result already set, check signature */
2524 if (server->sign && !mid->decrypted) {
2525 int rc;
2526
2527 rc = smb2_verify_signature(&rqst, server);
2528 if (rc)
2529 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
2530 rc);
2531 }
2532 /* FIXME: should this be counted toward the initiating task? */
2533 task_io_account_read(rdata->got_bytes);
2534 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2535 break;
2536 case MID_REQUEST_SUBMITTED:
2537 case MID_RETRY_NEEDED:
2538 rdata->result = -EAGAIN;
2539 if (server->sign && rdata->got_bytes)
2540 /* reset bytes number since we can not check a sign */
2541 rdata->got_bytes = 0;
2542 /* FIXME: should this be counted toward the initiating task? */
2543 task_io_account_read(rdata->got_bytes);
2544 cifs_stats_bytes_read(tcon, rdata->got_bytes);
2545 break;
2546 default:
2547 if (rdata->result != -ENODATA)
2548 rdata->result = -EIO;
2549 }
2550
2551 if (rdata->result)
2552 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
2553
2554 queue_work(cifsiod_wq, &rdata->work);
2555 DeleteMidQEntry(mid);
2556 add_credits(server, credits_received, 0);
2557 }
2558
2559 /* smb2_async_readv - send an async read, and set up mid to handle result */
2560 int
2561 smb2_async_readv(struct cifs_readdata *rdata)
2562 {
2563 int rc, flags = 0;
2564 char *buf;
2565 struct smb2_sync_hdr *shdr;
2566 struct cifs_io_parms io_parms;
2567 struct smb_rqst rqst = { .rq_iov = rdata->iov,
2568 .rq_nvec = 2 };
2569 struct TCP_Server_Info *server;
2570 unsigned int total_len;
2571 __be32 req_len;
2572
2573 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
2574 __func__, rdata->offset, rdata->bytes);
2575
2576 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
2577 io_parms.offset = rdata->offset;
2578 io_parms.length = rdata->bytes;
2579 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
2580 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
2581 io_parms.pid = rdata->pid;
2582
2583 server = io_parms.tcon->ses->server;
2584
2585 rc = smb2_new_read_req((void **) &buf, &total_len, &io_parms, 0, 0);
2586 if (rc) {
2587 if (rc == -EAGAIN && rdata->credits) {
2588 /* credits was reset by reconnect */
2589 rdata->credits = 0;
2590 /* reduce in_flight value since we won't send the req */
2591 spin_lock(&server->req_lock);
2592 server->in_flight--;
2593 spin_unlock(&server->req_lock);
2594 }
2595 return rc;
2596 }
2597
2598 if (encryption_required(io_parms.tcon))
2599 flags |= CIFS_TRANSFORM_REQ;
2600
2601 req_len = cpu_to_be32(total_len);
2602
2603 rdata->iov[0].iov_base = &req_len;
2604 rdata->iov[0].iov_len = sizeof(__be32);
2605 rdata->iov[1].iov_base = buf;
2606 rdata->iov[1].iov_len = total_len;
2607
2608 shdr = (struct smb2_sync_hdr *)buf;
2609
2610 if (rdata->credits) {
2611 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
2612 SMB2_MAX_BUFFER_SIZE));
2613 shdr->CreditRequest = shdr->CreditCharge;
2614 spin_lock(&server->req_lock);
2615 server->credits += rdata->credits -
2616 le16_to_cpu(shdr->CreditCharge);
2617 spin_unlock(&server->req_lock);
2618 wake_up(&server->request_q);
2619 flags |= CIFS_HAS_CREDITS;
2620 }
2621
2622 kref_get(&rdata->refcount);
2623 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
2624 cifs_readv_receive, smb2_readv_callback,
2625 smb3_handle_read_data, rdata, flags);
2626 if (rc) {
2627 kref_put(&rdata->refcount, cifs_readdata_release);
2628 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
2629 }
2630
2631 cifs_small_buf_release(buf);
2632 return rc;
2633 }
2634
2635 int
2636 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
2637 unsigned int *nbytes, char **buf, int *buf_type)
2638 {
2639 int resp_buftype, rc = -EACCES;
2640 struct smb2_read_plain_req *req = NULL;
2641 struct smb2_read_rsp *rsp = NULL;
2642 struct smb2_sync_hdr *shdr;
2643 struct kvec iov[2];
2644 struct kvec rsp_iov;
2645 unsigned int total_len;
2646 __be32 req_len;
2647 struct smb_rqst rqst = { .rq_iov = iov,
2648 .rq_nvec = 2 };
2649 int flags = CIFS_LOG_ERROR;
2650 struct cifs_ses *ses = io_parms->tcon->ses;
2651
2652 *nbytes = 0;
2653 rc = smb2_new_read_req((void **)&req, &total_len, io_parms, 0, 0);
2654 if (rc)
2655 return rc;
2656
2657 if (encryption_required(io_parms->tcon))
2658 flags |= CIFS_TRANSFORM_REQ;
2659
2660 req_len = cpu_to_be32(total_len);
2661
2662 iov[0].iov_base = &req_len;
2663 iov[0].iov_len = sizeof(__be32);
2664 iov[1].iov_base = req;
2665 iov[1].iov_len = total_len;
2666
2667 rc = cifs_send_recv(xid, ses, &rqst, &resp_buftype, flags, &rsp_iov);
2668 cifs_small_buf_release(req);
2669
2670 rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
2671
2672 if (rc) {
2673 if (rc != -ENODATA) {
2674 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
2675 cifs_dbg(VFS, "Send error in read = %d\n", rc);
2676 }
2677 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2678 return rc == -ENODATA ? 0 : rc;
2679 }
2680
2681 *nbytes = le32_to_cpu(rsp->DataLength);
2682 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
2683 (*nbytes > io_parms->length)) {
2684 cifs_dbg(FYI, "bad length %d for count %d\n",
2685 *nbytes, io_parms->length);
2686 rc = -EIO;
2687 *nbytes = 0;
2688 }
2689
2690 shdr = get_sync_hdr(rsp);
2691
2692 if (*buf) {
2693 memcpy(*buf, (char *)shdr + rsp->DataOffset, *nbytes);
2694 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
2695 } else if (resp_buftype != CIFS_NO_BUFFER) {
2696 *buf = rsp_iov.iov_base;
2697 if (resp_buftype == CIFS_SMALL_BUFFER)
2698 *buf_type = CIFS_SMALL_BUFFER;
2699 else if (resp_buftype == CIFS_LARGE_BUFFER)
2700 *buf_type = CIFS_LARGE_BUFFER;
2701 }
2702 return rc;
2703 }
2704
2705 /*
2706 * Check the mid_state and signature on received buffer (if any), and queue the
2707 * workqueue completion task.
2708 */
2709 static void
2710 smb2_writev_callback(struct mid_q_entry *mid)
2711 {
2712 struct cifs_writedata *wdata = mid->callback_data;
2713 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2714 unsigned int written;
2715 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
2716 unsigned int credits_received = 1;
2717
2718 switch (mid->mid_state) {
2719 case MID_RESPONSE_RECEIVED:
2720 credits_received = le16_to_cpu(rsp->hdr.sync_hdr.CreditRequest);
2721 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
2722 if (wdata->result != 0)
2723 break;
2724
2725 written = le32_to_cpu(rsp->DataLength);
2726 /*
2727 * Mask off high 16 bits when bytes written as returned
2728 * by the server is greater than bytes requested by the
2729 * client. OS/2 servers are known to set incorrect
2730 * CountHigh values.
2731 */
2732 if (written > wdata->bytes)
2733 written &= 0xFFFF;
2734
2735 if (written < wdata->bytes)
2736 wdata->result = -ENOSPC;
2737 else
2738 wdata->bytes = written;
2739 break;
2740 case MID_REQUEST_SUBMITTED:
2741 case MID_RETRY_NEEDED:
2742 wdata->result = -EAGAIN;
2743 break;
2744 default:
2745 wdata->result = -EIO;
2746 break;
2747 }
2748
2749 if (wdata->result)
2750 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2751
2752 queue_work(cifsiod_wq, &wdata->work);
2753 DeleteMidQEntry(mid);
2754 add_credits(tcon->ses->server, credits_received, 0);
2755 }
2756
2757 /* smb2_async_writev - send an async write, and set up mid to handle result */
2758 int
2759 smb2_async_writev(struct cifs_writedata *wdata,
2760 void (*release)(struct kref *kref))
2761 {
2762 int rc = -EACCES, flags = 0;
2763 struct smb2_write_req *req = NULL;
2764 struct smb2_sync_hdr *shdr;
2765 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
2766 struct TCP_Server_Info *server = tcon->ses->server;
2767 struct kvec iov[2];
2768 struct smb_rqst rqst = { };
2769
2770 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
2771 if (rc) {
2772 if (rc == -EAGAIN && wdata->credits) {
2773 /* credits was reset by reconnect */
2774 wdata->credits = 0;
2775 /* reduce in_flight value since we won't send the req */
2776 spin_lock(&server->req_lock);
2777 server->in_flight--;
2778 spin_unlock(&server->req_lock);
2779 }
2780 goto async_writev_out;
2781 }
2782
2783 if (encryption_required(tcon))
2784 flags |= CIFS_TRANSFORM_REQ;
2785
2786 shdr = get_sync_hdr(req);
2787 shdr->ProcessId = cpu_to_le32(wdata->cfile->pid);
2788
2789 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
2790 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
2791 req->WriteChannelInfoOffset = 0;
2792 req->WriteChannelInfoLength = 0;
2793 req->Channel = 0;
2794 req->Offset = cpu_to_le64(wdata->offset);
2795 /* 4 for rfc1002 length field */
2796 req->DataOffset = cpu_to_le16(
2797 offsetof(struct smb2_write_req, Buffer) - 4);
2798 req->RemainingBytes = 0;
2799
2800 /* 4 for rfc1002 length field and 1 for Buffer */
2801 iov[0].iov_len = 4;
2802 iov[0].iov_base = req;
2803 iov[1].iov_len = get_rfc1002_length(req) - 1;
2804 iov[1].iov_base = (char *)req + 4;
2805
2806 rqst.rq_iov = iov;
2807 rqst.rq_nvec = 2;
2808 rqst.rq_pages = wdata->pages;
2809 rqst.rq_npages = wdata->nr_pages;
2810 rqst.rq_pagesz = wdata->pagesz;
2811 rqst.rq_tailsz = wdata->tailsz;
2812
2813 cifs_dbg(FYI, "async write at %llu %u bytes\n",
2814 wdata->offset, wdata->bytes);
2815
2816 req->Length = cpu_to_le32(wdata->bytes);
2817
2818 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
2819
2820 if (wdata->credits) {
2821 shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
2822 SMB2_MAX_BUFFER_SIZE));
2823 shdr->CreditRequest = shdr->CreditCharge;
2824 spin_lock(&server->req_lock);
2825 server->credits += wdata->credits -
2826 le16_to_cpu(shdr->CreditCharge);
2827 spin_unlock(&server->req_lock);
2828 wake_up(&server->request_q);
2829 flags |= CIFS_HAS_CREDITS;
2830 }
2831
2832 kref_get(&wdata->refcount);
2833 rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
2834 wdata, flags);
2835
2836 if (rc) {
2837 kref_put(&wdata->refcount, release);
2838 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
2839 }
2840
2841 async_writev_out:
2842 cifs_small_buf_release(req);
2843 return rc;
2844 }
2845
2846 /*
2847 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
2848 * The length field from io_parms must be at least 1 and indicates a number of
2849 * elements with data to write that begins with position 1 in iov array. All
2850 * data length is specified by count.
2851 */
2852 int
2853 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
2854 unsigned int *nbytes, struct kvec *iov, int n_vec)
2855 {
2856 int rc = 0;
2857 struct smb2_write_req *req = NULL;
2858 struct smb2_write_rsp *rsp = NULL;
2859 int resp_buftype;
2860 struct kvec rsp_iov;
2861 int flags = 0;
2862
2863 *nbytes = 0;
2864
2865 if (n_vec < 1)
2866 return rc;
2867
2868 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
2869 if (rc)
2870 return rc;
2871
2872 if (io_parms->tcon->ses->server == NULL)
2873 return -ECONNABORTED;
2874
2875 if (encryption_required(io_parms->tcon))
2876 flags |= CIFS_TRANSFORM_REQ;
2877
2878 req->hdr.sync_hdr.ProcessId = cpu_to_le32(io_parms->pid);
2879
2880 req->PersistentFileId = io_parms->persistent_fid;
2881 req->VolatileFileId = io_parms->volatile_fid;
2882 req->WriteChannelInfoOffset = 0;
2883 req->WriteChannelInfoLength = 0;
2884 req->Channel = 0;
2885 req->Length = cpu_to_le32(io_parms->length);
2886 req->Offset = cpu_to_le64(io_parms->offset);
2887 /* 4 for rfc1002 length field */
2888 req->DataOffset = cpu_to_le16(
2889 offsetof(struct smb2_write_req, Buffer) - 4);
2890 req->RemainingBytes = 0;
2891
2892 iov[0].iov_base = (char *)req;
2893 /* 4 for rfc1002 length field and 1 for Buffer */
2894 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2895
2896 /* length of entire message including data to be written */
2897 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
2898
2899 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
2900 &resp_buftype, flags, &rsp_iov);
2901 cifs_small_buf_release(req);
2902 rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
2903
2904 if (rc) {
2905 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
2906 cifs_dbg(VFS, "Send error in write = %d\n", rc);
2907 } else
2908 *nbytes = le32_to_cpu(rsp->DataLength);
2909
2910 free_rsp_buf(resp_buftype, rsp);
2911 return rc;
2912 }
2913
2914 static unsigned int
2915 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
2916 {
2917 int len;
2918 unsigned int entrycount = 0;
2919 unsigned int next_offset = 0;
2920 FILE_DIRECTORY_INFO *entryptr;
2921
2922 if (bufstart == NULL)
2923 return 0;
2924
2925 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
2926
2927 while (1) {
2928 entryptr = (FILE_DIRECTORY_INFO *)
2929 ((char *)entryptr + next_offset);
2930
2931 if ((char *)entryptr + size > end_of_buf) {
2932 cifs_dbg(VFS, "malformed search entry would overflow\n");
2933 break;
2934 }
2935
2936 len = le32_to_cpu(entryptr->FileNameLength);
2937 if ((char *)entryptr + len + size > end_of_buf) {
2938 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
2939 end_of_buf);
2940 break;
2941 }
2942
2943 *lastentry = (char *)entryptr;
2944 entrycount++;
2945
2946 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
2947 if (!next_offset)
2948 break;
2949 }
2950
2951 return entrycount;
2952 }
2953
2954 /*
2955 * Readdir/FindFirst
2956 */
2957 int
2958 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
2959 u64 persistent_fid, u64 volatile_fid, int index,
2960 struct cifs_search_info *srch_inf)
2961 {
2962 struct smb2_query_directory_req *req;
2963 struct smb2_query_directory_rsp *rsp = NULL;
2964 struct kvec iov[2];
2965 struct kvec rsp_iov;
2966 int rc = 0;
2967 int len;
2968 int resp_buftype = CIFS_NO_BUFFER;
2969 unsigned char *bufptr;
2970 struct TCP_Server_Info *server;
2971 struct cifs_ses *ses = tcon->ses;
2972 __le16 asteriks = cpu_to_le16('*');
2973 char *end_of_smb;
2974 unsigned int output_size = CIFSMaxBufSize;
2975 size_t info_buf_size;
2976 int flags = 0;
2977
2978 if (ses && (ses->server))
2979 server = ses->server;
2980 else
2981 return -EIO;
2982
2983 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
2984 if (rc)
2985 return rc;
2986
2987 if (encryption_required(tcon))
2988 flags |= CIFS_TRANSFORM_REQ;
2989
2990 switch (srch_inf->info_level) {
2991 case SMB_FIND_FILE_DIRECTORY_INFO:
2992 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
2993 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
2994 break;
2995 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
2996 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
2997 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
2998 break;
2999 default:
3000 cifs_dbg(VFS, "info level %u isn't supported\n",
3001 srch_inf->info_level);
3002 rc = -EINVAL;
3003 goto qdir_exit;
3004 }
3005
3006 req->FileIndex = cpu_to_le32(index);
3007 req->PersistentFileId = persistent_fid;
3008 req->VolatileFileId = volatile_fid;
3009
3010 len = 0x2;
3011 bufptr = req->Buffer;
3012 memcpy(bufptr, &asteriks, len);
3013
3014 req->FileNameOffset =
3015 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
3016 req->FileNameLength = cpu_to_le16(len);
3017 /*
3018 * BB could be 30 bytes or so longer if we used SMB2 specific
3019 * buffer lengths, but this is safe and close enough.
3020 */
3021 output_size = min_t(unsigned int, output_size, server->maxBuf);
3022 output_size = min_t(unsigned int, output_size, 2 << 15);
3023 req->OutputBufferLength = cpu_to_le32(output_size);
3024
3025 iov[0].iov_base = (char *)req;
3026 /* 4 for RFC1001 length and 1 for Buffer */
3027 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
3028
3029 iov[1].iov_base = (char *)(req->Buffer);
3030 iov[1].iov_len = len;
3031
3032 inc_rfc1001_len(req, len - 1 /* Buffer */);
3033
3034 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, flags, &rsp_iov);
3035 cifs_small_buf_release(req);
3036 rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
3037
3038 if (rc) {
3039 if (rc == -ENODATA &&
3040 rsp->hdr.sync_hdr.Status == STATUS_NO_MORE_FILES) {
3041 srch_inf->endOfSearch = true;
3042 rc = 0;
3043 }
3044 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
3045 goto qdir_exit;
3046 }
3047
3048 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3049 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3050 info_buf_size);
3051 if (rc)
3052 goto qdir_exit;
3053
3054 srch_inf->unicode = true;
3055
3056 if (srch_inf->ntwrk_buf_start) {
3057 if (srch_inf->smallBuf)
3058 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
3059 else
3060 cifs_buf_release(srch_inf->ntwrk_buf_start);
3061 }
3062 srch_inf->ntwrk_buf_start = (char *)rsp;
3063 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
3064 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
3065 /* 4 for rfc1002 length field */
3066 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
3067 srch_inf->entries_in_buffer =
3068 num_entries(srch_inf->srch_entries_start, end_of_smb,
3069 &srch_inf->last_entry, info_buf_size);
3070 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
3071 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
3072 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
3073 srch_inf->srch_entries_start, srch_inf->last_entry);
3074 if (resp_buftype == CIFS_LARGE_BUFFER)
3075 srch_inf->smallBuf = false;
3076 else if (resp_buftype == CIFS_SMALL_BUFFER)
3077 srch_inf->smallBuf = true;
3078 else
3079 cifs_dbg(VFS, "illegal search buffer type\n");
3080
3081 return rc;
3082
3083 qdir_exit:
3084 free_rsp_buf(resp_buftype, rsp);
3085 return rc;
3086 }
3087
3088 static int
3089 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3090 u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
3091 u8 info_type, u32 additional_info, unsigned int num,
3092 void **data, unsigned int *size)
3093 {
3094 struct smb2_set_info_req *req;
3095 struct smb2_set_info_rsp *rsp = NULL;
3096 struct kvec *iov;
3097 struct kvec rsp_iov;
3098 int rc = 0;
3099 int resp_buftype;
3100 unsigned int i;
3101 struct cifs_ses *ses = tcon->ses;
3102 int flags = 0;
3103
3104 if (!ses || !(ses->server))
3105 return -EIO;
3106
3107 if (!num)
3108 return -EINVAL;
3109
3110 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
3111 if (!iov)
3112 return -ENOMEM;
3113
3114 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
3115 if (rc) {
3116 kfree(iov);
3117 return rc;
3118 }
3119
3120 if (encryption_required(tcon))
3121 flags |= CIFS_TRANSFORM_REQ;
3122
3123 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
3124
3125 req->InfoType = info_type;
3126 req->FileInfoClass = info_class;
3127 req->PersistentFileId = persistent_fid;
3128 req->VolatileFileId = volatile_fid;
3129 req->AdditionalInformation = cpu_to_le32(additional_info);
3130
3131 /* 4 for RFC1001 length and 1 for Buffer */
3132 req->BufferOffset =
3133 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
3134 req->BufferLength = cpu_to_le32(*size);
3135
3136 inc_rfc1001_len(req, *size - 1 /* Buffer */);
3137
3138 memcpy(req->Buffer, *data, *size);
3139
3140 iov[0].iov_base = (char *)req;
3141 /* 4 for RFC1001 length */
3142 iov[0].iov_len = get_rfc1002_length(req) + 4;
3143
3144 for (i = 1; i < num; i++) {
3145 inc_rfc1001_len(req, size[i]);
3146 le32_add_cpu(&req->BufferLength, size[i]);
3147 iov[i].iov_base = (char *)data[i];
3148 iov[i].iov_len = size[i];
3149 }
3150
3151 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, flags, &rsp_iov);
3152 cifs_small_buf_release(req);
3153 rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
3154
3155 if (rc != 0)
3156 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
3157
3158 free_rsp_buf(resp_buftype, rsp);
3159 kfree(iov);
3160 return rc;
3161 }
3162
3163 int
3164 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
3165 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3166 {
3167 struct smb2_file_rename_info info;
3168 void **data;
3169 unsigned int size[2];
3170 int rc;
3171 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3172
3173 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3174 if (!data)
3175 return -ENOMEM;
3176
3177 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
3178 /* 0 = fail if target already exists */
3179 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3180 info.FileNameLength = cpu_to_le32(len);
3181
3182 data[0] = &info;
3183 size[0] = sizeof(struct smb2_file_rename_info);
3184
3185 data[1] = target_file;
3186 size[1] = len + 2 /* null */;
3187
3188 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3189 current->tgid, FILE_RENAME_INFORMATION, SMB2_O_INFO_FILE,
3190 0, 2, data, size);
3191 kfree(data);
3192 return rc;
3193 }
3194
3195 int
3196 SMB2_rmdir(const unsigned int xid, struct cifs_tcon *tcon,
3197 u64 persistent_fid, u64 volatile_fid)
3198 {
3199 __u8 delete_pending = 1;
3200 void *data;
3201 unsigned int size;
3202
3203 data = &delete_pending;
3204 size = 1; /* sizeof __u8 */
3205
3206 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3207 current->tgid, FILE_DISPOSITION_INFORMATION, SMB2_O_INFO_FILE,
3208 0, 1, &data, &size);
3209 }
3210
3211 int
3212 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
3213 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
3214 {
3215 struct smb2_file_link_info info;
3216 void **data;
3217 unsigned int size[2];
3218 int rc;
3219 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
3220
3221 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
3222 if (!data)
3223 return -ENOMEM;
3224
3225 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
3226 /* 0 = fail if link already exists */
3227 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
3228 info.FileNameLength = cpu_to_le32(len);
3229
3230 data[0] = &info;
3231 size[0] = sizeof(struct smb2_file_link_info);
3232
3233 data[1] = target_file;
3234 size[1] = len + 2 /* null */;
3235
3236 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
3237 current->tgid, FILE_LINK_INFORMATION, SMB2_O_INFO_FILE,
3238 0, 2, data, size);
3239 kfree(data);
3240 return rc;
3241 }
3242
3243 int
3244 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
3245 u64 volatile_fid, u32 pid, __le64 *eof, bool is_falloc)
3246 {
3247 struct smb2_file_eof_info info;
3248 void *data;
3249 unsigned int size;
3250
3251 info.EndOfFile = *eof;
3252
3253 data = &info;
3254 size = sizeof(struct smb2_file_eof_info);
3255
3256 if (is_falloc)
3257 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3258 pid, FILE_ALLOCATION_INFORMATION, SMB2_O_INFO_FILE,
3259 0, 1, &data, &size);
3260 else
3261 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3262 pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
3263 0, 1, &data, &size);
3264 }
3265
3266 int
3267 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
3268 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
3269 {
3270 unsigned int size;
3271 size = sizeof(FILE_BASIC_INFO);
3272 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3273 current->tgid, FILE_BASIC_INFORMATION, SMB2_O_INFO_FILE,
3274 0, 1, (void **)&buf, &size);
3275 }
3276
3277 int
3278 SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
3279 u64 persistent_fid, u64 volatile_fid,
3280 struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
3281 {
3282 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3283 current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
3284 1, (void **)&pnntsd, &pacllen);
3285 }
3286
3287 int
3288 SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
3289 u64 persistent_fid, u64 volatile_fid,
3290 struct smb2_file_full_ea_info *buf, int len)
3291 {
3292 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
3293 current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
3294 0, 1, (void **)&buf, &len);
3295 }
3296
3297 int
3298 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
3299 const u64 persistent_fid, const u64 volatile_fid,
3300 __u8 oplock_level)
3301 {
3302 int rc;
3303 struct smb2_oplock_break *req = NULL;
3304 int flags = CIFS_OBREAK_OP;
3305
3306 cifs_dbg(FYI, "SMB2_oplock_break\n");
3307 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3308 if (rc)
3309 return rc;
3310
3311 if (encryption_required(tcon))
3312 flags |= CIFS_TRANSFORM_REQ;
3313
3314 req->VolatileFid = volatile_fid;
3315 req->PersistentFid = persistent_fid;
3316 req->OplockLevel = oplock_level;
3317 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3318
3319 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3320 cifs_small_buf_release(req);
3321
3322 if (rc) {
3323 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3324 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
3325 }
3326
3327 return rc;
3328 }
3329
3330 static void
3331 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
3332 struct kstatfs *kst)
3333 {
3334 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
3335 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
3336 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
3337 kst->f_bfree = kst->f_bavail =
3338 le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
3339 return;
3340 }
3341
3342 static int
3343 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
3344 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
3345 {
3346 int rc;
3347 struct smb2_query_info_req *req;
3348
3349 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
3350
3351 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
3352 return -EIO;
3353
3354 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
3355 if (rc)
3356 return rc;
3357
3358 req->InfoType = SMB2_O_INFO_FILESYSTEM;
3359 req->FileInfoClass = level;
3360 req->PersistentFileId = persistent_fid;
3361 req->VolatileFileId = volatile_fid;
3362 /* 4 for rfc1002 length field and 1 for pad */
3363 req->InputBufferOffset =
3364 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
3365 req->OutputBufferLength = cpu_to_le32(
3366 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
3367
3368 iov->iov_base = (char *)req;
3369 /* 4 for rfc1002 length field */
3370 iov->iov_len = get_rfc1002_length(req) + 4;
3371 return 0;
3372 }
3373
3374 int
3375 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
3376 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
3377 {
3378 struct smb2_query_info_rsp *rsp = NULL;
3379 struct kvec iov;
3380 struct kvec rsp_iov;
3381 int rc = 0;
3382 int resp_buftype;
3383 struct cifs_ses *ses = tcon->ses;
3384 struct smb2_fs_full_size_info *info = NULL;
3385 int flags = 0;
3386
3387 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
3388 sizeof(struct smb2_fs_full_size_info),
3389 persistent_fid, volatile_fid);
3390 if (rc)
3391 return rc;
3392
3393 if (encryption_required(tcon))
3394 flags |= CIFS_TRANSFORM_REQ;
3395
3396 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3397 cifs_small_buf_release(iov.iov_base);
3398 if (rc) {
3399 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3400 goto qfsinf_exit;
3401 }
3402 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3403
3404 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
3405 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
3406 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
3407 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
3408 sizeof(struct smb2_fs_full_size_info));
3409 if (!rc)
3410 copy_fs_info_to_kstatfs(info, fsdata);
3411
3412 qfsinf_exit:
3413 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3414 return rc;
3415 }
3416
3417 int
3418 SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
3419 u64 persistent_fid, u64 volatile_fid, int level)
3420 {
3421 struct smb2_query_info_rsp *rsp = NULL;
3422 struct kvec iov;
3423 struct kvec rsp_iov;
3424 int rc = 0;
3425 int resp_buftype, max_len, min_len;
3426 struct cifs_ses *ses = tcon->ses;
3427 unsigned int rsp_len, offset;
3428 int flags = 0;
3429
3430 if (level == FS_DEVICE_INFORMATION) {
3431 max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3432 min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
3433 } else if (level == FS_ATTRIBUTE_INFORMATION) {
3434 max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
3435 min_len = MIN_FS_ATTR_INFO_SIZE;
3436 } else if (level == FS_SECTOR_SIZE_INFORMATION) {
3437 max_len = sizeof(struct smb3_fs_ss_info);
3438 min_len = sizeof(struct smb3_fs_ss_info);
3439 } else {
3440 cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
3441 return -EINVAL;
3442 }
3443
3444 rc = build_qfs_info_req(&iov, tcon, level, max_len,
3445 persistent_fid, volatile_fid);
3446 if (rc)
3447 return rc;
3448
3449 if (encryption_required(tcon))
3450 flags |= CIFS_TRANSFORM_REQ;
3451
3452 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, flags, &rsp_iov);
3453 cifs_small_buf_release(iov.iov_base);
3454 if (rc) {
3455 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
3456 goto qfsattr_exit;
3457 }
3458 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
3459
3460 rsp_len = le32_to_cpu(rsp->OutputBufferLength);
3461 offset = le16_to_cpu(rsp->OutputBufferOffset);
3462 rc = validate_buf(offset, rsp_len, &rsp->hdr, min_len);
3463 if (rc)
3464 goto qfsattr_exit;
3465
3466 if (level == FS_ATTRIBUTE_INFORMATION)
3467 memcpy(&tcon->fsAttrInfo, 4 /* RFC1001 len */ + offset
3468 + (char *)&rsp->hdr, min_t(unsigned int,
3469 rsp_len, max_len));
3470 else if (level == FS_DEVICE_INFORMATION)
3471 memcpy(&tcon->fsDevInfo, 4 /* RFC1001 len */ + offset
3472 + (char *)&rsp->hdr, sizeof(FILE_SYSTEM_DEVICE_INFO));
3473 else if (level == FS_SECTOR_SIZE_INFORMATION) {
3474 struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
3475 (4 /* RFC1001 len */ + offset + (char *)&rsp->hdr);
3476 tcon->ss_flags = le32_to_cpu(ss_info->Flags);
3477 tcon->perf_sector_size =
3478 le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
3479 }
3480
3481 qfsattr_exit:
3482 free_rsp_buf(resp_buftype, rsp_iov.iov_base);
3483 return rc;
3484 }
3485
3486 int
3487 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
3488 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3489 const __u32 num_lock, struct smb2_lock_element *buf)
3490 {
3491 int rc = 0;
3492 struct smb2_lock_req *req = NULL;
3493 struct kvec iov[2];
3494 struct kvec rsp_iov;
3495 int resp_buf_type;
3496 unsigned int count;
3497 int flags = CIFS_NO_RESP;
3498
3499 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
3500
3501 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
3502 if (rc)
3503 return rc;
3504
3505 if (encryption_required(tcon))
3506 flags |= CIFS_TRANSFORM_REQ;
3507
3508 req->hdr.sync_hdr.ProcessId = cpu_to_le32(pid);
3509 req->LockCount = cpu_to_le16(num_lock);
3510
3511 req->PersistentFileId = persist_fid;
3512 req->VolatileFileId = volatile_fid;
3513
3514 count = num_lock * sizeof(struct smb2_lock_element);
3515 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
3516
3517 iov[0].iov_base = (char *)req;
3518 /* 4 for rfc1002 length field and count for all locks */
3519 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
3520 iov[1].iov_base = (char *)buf;
3521 iov[1].iov_len = count;
3522
3523 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
3524 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, flags,
3525 &rsp_iov);
3526 cifs_small_buf_release(req);
3527 if (rc) {
3528 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
3529 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
3530 }
3531
3532 return rc;
3533 }
3534
3535 int
3536 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
3537 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
3538 const __u64 length, const __u64 offset, const __u32 lock_flags,
3539 const bool wait)
3540 {
3541 struct smb2_lock_element lock;
3542
3543 lock.Offset = cpu_to_le64(offset);
3544 lock.Length = cpu_to_le64(length);
3545 lock.Flags = cpu_to_le32(lock_flags);
3546 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
3547 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
3548
3549 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
3550 }
3551
3552 int
3553 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
3554 __u8 *lease_key, const __le32 lease_state)
3555 {
3556 int rc;
3557 struct smb2_lease_ack *req = NULL;
3558 int flags = CIFS_OBREAK_OP;
3559
3560 cifs_dbg(FYI, "SMB2_lease_break\n");
3561 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
3562 if (rc)
3563 return rc;
3564
3565 if (encryption_required(tcon))
3566 flags |= CIFS_TRANSFORM_REQ;
3567
3568 req->hdr.sync_hdr.CreditRequest = cpu_to_le16(1);
3569 req->StructureSize = cpu_to_le16(36);
3570 inc_rfc1001_len(req, 12);
3571
3572 memcpy(req->LeaseKey, lease_key, 16);
3573 req->LeaseState = lease_state;
3574
3575 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, flags);
3576 cifs_small_buf_release(req);
3577
3578 if (rc) {
3579 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
3580 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
3581 }
3582
3583 return rc;
3584 }