]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/cifs/smb2pdu.c
CIFS: Store lease state itself rather than a mapped oplock value
[mirror_ubuntu-artful-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/pagemap.h>
37 #include <linux/xattr.h>
38 #include "smb2pdu.h"
39 #include "cifsglob.h"
40 #include "cifsacl.h"
41 #include "cifsproto.h"
42 #include "smb2proto.h"
43 #include "cifs_unicode.h"
44 #include "cifs_debug.h"
45 #include "ntlmssp.h"
46 #include "smb2status.h"
47 #include "smb2glob.h"
48 #include "cifspdu.h"
49
50 /*
51 * The following table defines the expected "StructureSize" of SMB2 requests
52 * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
53 *
54 * Note that commands are defined in smb2pdu.h in le16 but the array below is
55 * indexed by command in host byte order.
56 */
57 static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
58 /* SMB2_NEGOTIATE */ 36,
59 /* SMB2_SESSION_SETUP */ 25,
60 /* SMB2_LOGOFF */ 4,
61 /* SMB2_TREE_CONNECT */ 9,
62 /* SMB2_TREE_DISCONNECT */ 4,
63 /* SMB2_CREATE */ 57,
64 /* SMB2_CLOSE */ 24,
65 /* SMB2_FLUSH */ 24,
66 /* SMB2_READ */ 49,
67 /* SMB2_WRITE */ 49,
68 /* SMB2_LOCK */ 48,
69 /* SMB2_IOCTL */ 57,
70 /* SMB2_CANCEL */ 4,
71 /* SMB2_ECHO */ 4,
72 /* SMB2_QUERY_DIRECTORY */ 33,
73 /* SMB2_CHANGE_NOTIFY */ 32,
74 /* SMB2_QUERY_INFO */ 41,
75 /* SMB2_SET_INFO */ 33,
76 /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
77 };
78
79
80 static void
81 smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
82 const struct cifs_tcon *tcon)
83 {
84 struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
85 char *temp = (char *)hdr;
86 /* lookup word count ie StructureSize from table */
87 __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
88
89 /*
90 * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
91 * largest operations (Create)
92 */
93 memset(temp, 0, 256);
94
95 /* Note this is only network field converted to big endian */
96 hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
97 - 4 /* RFC 1001 length field itself not counted */);
98
99 hdr->ProtocolId[0] = 0xFE;
100 hdr->ProtocolId[1] = 'S';
101 hdr->ProtocolId[2] = 'M';
102 hdr->ProtocolId[3] = 'B';
103 hdr->StructureSize = cpu_to_le16(64);
104 hdr->Command = smb2_cmd;
105 hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
106 hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
107
108 if (!tcon)
109 goto out;
110
111 /* BB FIXME when we do write > 64K add +1 for every 64K in req or rsp */
112 /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
113 /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
114 if ((tcon->ses) &&
115 (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
116 hdr->CreditCharge = cpu_to_le16(1);
117 /* else CreditCharge MBZ */
118
119 hdr->TreeId = tcon->tid;
120 /* Uid is not converted */
121 if (tcon->ses)
122 hdr->SessionId = tcon->ses->Suid;
123
124 /*
125 * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
126 * to pass the path on the Open SMB prefixed by \\server\share.
127 * Not sure when we would need to do the augmented path (if ever) and
128 * setting this flag breaks the SMB2 open operation since it is
129 * illegal to send an empty path name (without \\server\share prefix)
130 * when the DFS flag is set in the SMB open header. We could
131 * consider setting the flag on all operations other than open
132 * but it is safer to net set it for now.
133 */
134 /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
135 hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
136
137 if (tcon->ses && tcon->ses->server && tcon->ses->server->sign)
138 hdr->Flags |= SMB2_FLAGS_SIGNED;
139 out:
140 pdu->StructureSize2 = cpu_to_le16(parmsize);
141 return;
142 }
143
144 static int
145 smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
146 {
147 int rc = 0;
148 struct nls_table *nls_codepage;
149 struct cifs_ses *ses;
150 struct TCP_Server_Info *server;
151
152 /*
153 * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
154 * check for tcp and smb session status done differently
155 * for those three - in the calling routine.
156 */
157 if (tcon == NULL)
158 return rc;
159
160 if (smb2_command == SMB2_TREE_CONNECT)
161 return rc;
162
163 if (tcon->tidStatus == CifsExiting) {
164 /*
165 * only tree disconnect, open, and write,
166 * (and ulogoff which does not have tcon)
167 * are allowed as we start force umount.
168 */
169 if ((smb2_command != SMB2_WRITE) &&
170 (smb2_command != SMB2_CREATE) &&
171 (smb2_command != SMB2_TREE_DISCONNECT)) {
172 cifs_dbg(FYI, "can not send cmd %d while umounting\n",
173 smb2_command);
174 return -ENODEV;
175 }
176 }
177 if ((!tcon->ses) || (tcon->ses->status == CifsExiting) ||
178 (!tcon->ses->server))
179 return -EIO;
180
181 ses = tcon->ses;
182 server = ses->server;
183
184 /*
185 * Give demultiplex thread up to 10 seconds to reconnect, should be
186 * greater than cifs socket timeout which is 7 seconds
187 */
188 while (server->tcpStatus == CifsNeedReconnect) {
189 /*
190 * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
191 * here since they are implicitly done when session drops.
192 */
193 switch (smb2_command) {
194 /*
195 * BB Should we keep oplock break and add flush to exceptions?
196 */
197 case SMB2_TREE_DISCONNECT:
198 case SMB2_CANCEL:
199 case SMB2_CLOSE:
200 case SMB2_OPLOCK_BREAK:
201 return -EAGAIN;
202 }
203
204 wait_event_interruptible_timeout(server->response_q,
205 (server->tcpStatus != CifsNeedReconnect), 10 * HZ);
206
207 /* are we still trying to reconnect? */
208 if (server->tcpStatus != CifsNeedReconnect)
209 break;
210
211 /*
212 * on "soft" mounts we wait once. Hard mounts keep
213 * retrying until process is killed or server comes
214 * back on-line
215 */
216 if (!tcon->retry) {
217 cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
218 return -EHOSTDOWN;
219 }
220 }
221
222 if (!tcon->ses->need_reconnect && !tcon->need_reconnect)
223 return rc;
224
225 nls_codepage = load_nls_default();
226
227 /*
228 * need to prevent multiple threads trying to simultaneously reconnect
229 * the same SMB session
230 */
231 mutex_lock(&tcon->ses->session_mutex);
232 rc = cifs_negotiate_protocol(0, tcon->ses);
233 if (!rc && tcon->ses->need_reconnect)
234 rc = cifs_setup_session(0, tcon->ses, nls_codepage);
235
236 if (rc || !tcon->need_reconnect) {
237 mutex_unlock(&tcon->ses->session_mutex);
238 goto out;
239 }
240
241 cifs_mark_open_files_invalid(tcon);
242 rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage);
243 mutex_unlock(&tcon->ses->session_mutex);
244 cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
245 if (rc)
246 goto out;
247 atomic_inc(&tconInfoReconnectCount);
248 /*
249 * BB FIXME add code to check if wsize needs update due to negotiated
250 * smb buffer size shrinking.
251 */
252 out:
253 /*
254 * Check if handle based operation so we know whether we can continue
255 * or not without returning to caller to reset file handle.
256 */
257 /*
258 * BB Is flush done by server on drop of tcp session? Should we special
259 * case it and skip above?
260 */
261 switch (smb2_command) {
262 case SMB2_FLUSH:
263 case SMB2_READ:
264 case SMB2_WRITE:
265 case SMB2_LOCK:
266 case SMB2_IOCTL:
267 case SMB2_QUERY_DIRECTORY:
268 case SMB2_CHANGE_NOTIFY:
269 case SMB2_QUERY_INFO:
270 case SMB2_SET_INFO:
271 return -EAGAIN;
272 }
273 unload_nls(nls_codepage);
274 return rc;
275 }
276
277 /*
278 * Allocate and return pointer to an SMB request hdr, and set basic
279 * SMB information in the SMB header. If the return code is zero, this
280 * function must have filled in request_buf pointer.
281 */
282 static int
283 small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
284 void **request_buf)
285 {
286 int rc = 0;
287
288 rc = smb2_reconnect(smb2_command, tcon);
289 if (rc)
290 return rc;
291
292 /* BB eventually switch this to SMB2 specific small buf size */
293 *request_buf = cifs_small_buf_get();
294 if (*request_buf == NULL) {
295 /* BB should we add a retry in here if not a writepage? */
296 return -ENOMEM;
297 }
298
299 smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
300
301 if (tcon != NULL) {
302 #ifdef CONFIG_CIFS_STATS2
303 uint16_t com_code = le16_to_cpu(smb2_command);
304 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
305 #endif
306 cifs_stats_inc(&tcon->num_smbs_sent);
307 }
308
309 return rc;
310 }
311
312 static void
313 free_rsp_buf(int resp_buftype, void *rsp)
314 {
315 if (resp_buftype == CIFS_SMALL_BUFFER)
316 cifs_small_buf_release(rsp);
317 else if (resp_buftype == CIFS_LARGE_BUFFER)
318 cifs_buf_release(rsp);
319 }
320
321
322 /*
323 *
324 * SMB2 Worker functions follow:
325 *
326 * The general structure of the worker functions is:
327 * 1) Call smb2_init (assembles SMB2 header)
328 * 2) Initialize SMB2 command specific fields in fixed length area of SMB
329 * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
330 * 4) Decode SMB2 command specific fields in the fixed length area
331 * 5) Decode variable length data area (if any for this SMB2 command type)
332 * 6) Call free smb buffer
333 * 7) return
334 *
335 */
336
337 int
338 SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
339 {
340 struct smb2_negotiate_req *req;
341 struct smb2_negotiate_rsp *rsp;
342 struct kvec iov[1];
343 int rc = 0;
344 int resp_buftype;
345 struct TCP_Server_Info *server = ses->server;
346 int blob_offset, blob_length;
347 char *security_blob;
348 int flags = CIFS_NEG_OP;
349
350 cifs_dbg(FYI, "Negotiate protocol\n");
351
352 if (!server) {
353 WARN(1, "%s: server is NULL!\n", __func__);
354 return -EIO;
355 }
356
357 rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
358 if (rc)
359 return rc;
360
361 req->hdr.SessionId = 0;
362
363 req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id);
364
365 req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */
366 inc_rfc1001_len(req, 2);
367
368 /* only one of SMB2 signing flags may be set in SMB2 request */
369 if (ses->sign)
370 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
371 else if (global_secflags & CIFSSEC_MAY_SIGN)
372 req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
373 else
374 req->SecurityMode = 0;
375
376 req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities);
377
378 memcpy(req->ClientGUID, cifs_client_guid, SMB2_CLIENT_GUID_SIZE);
379
380 iov[0].iov_base = (char *)req;
381 /* 4 for rfc1002 length field */
382 iov[0].iov_len = get_rfc1002_length(req) + 4;
383
384 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
385
386 rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
387 /*
388 * No tcon so can't do
389 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
390 */
391 if (rc != 0)
392 goto neg_exit;
393
394 cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
395
396 /* BB we may eventually want to match the negotiated vs. requested
397 dialect, even though we are only requesting one at a time */
398 if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
399 cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
400 else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
401 cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
402 else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
403 cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
404 else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
405 cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
406 else {
407 cifs_dbg(VFS, "Illegal dialect returned by server %d\n",
408 le16_to_cpu(rsp->DialectRevision));
409 rc = -EIO;
410 goto neg_exit;
411 }
412 server->dialect = le16_to_cpu(rsp->DialectRevision);
413
414 /* SMB2 only has an extended negflavor */
415 server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
416 server->maxBuf = le32_to_cpu(rsp->MaxTransactSize);
417 server->max_read = le32_to_cpu(rsp->MaxReadSize);
418 server->max_write = le32_to_cpu(rsp->MaxWriteSize);
419 /* BB Do we need to validate the SecurityMode? */
420 server->sec_mode = le16_to_cpu(rsp->SecurityMode);
421 server->capabilities = le32_to_cpu(rsp->Capabilities);
422 /* Internal types */
423 server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
424
425 security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
426 &rsp->hdr);
427 /*
428 * See MS-SMB2 section 2.2.4: if no blob, client picks default which
429 * for us will be
430 * ses->sectype = RawNTLMSSP;
431 * but for time being this is our only auth choice so doesn't matter.
432 * We just found a server which sets blob length to zero expecting raw.
433 */
434 if (blob_length == 0)
435 cifs_dbg(FYI, "missing security blob on negprot\n");
436
437 rc = cifs_enable_signing(server, ses->sign);
438 #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
439 if (rc)
440 goto neg_exit;
441 if (blob_length)
442 rc = decode_neg_token_init(security_blob, blob_length,
443 &server->sec_type);
444 if (rc == 1)
445 rc = 0;
446 else if (rc == 0) {
447 rc = -EIO;
448 goto neg_exit;
449 }
450 #endif
451
452 neg_exit:
453 free_rsp_buf(resp_buftype, rsp);
454 return rc;
455 }
456
457 int
458 SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
459 const struct nls_table *nls_cp)
460 {
461 struct smb2_sess_setup_req *req;
462 struct smb2_sess_setup_rsp *rsp = NULL;
463 struct kvec iov[2];
464 int rc = 0;
465 int resp_buftype;
466 __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */
467 struct TCP_Server_Info *server = ses->server;
468 u16 blob_length = 0;
469 char *security_blob;
470 char *ntlmssp_blob = NULL;
471 bool use_spnego = false; /* else use raw ntlmssp */
472
473 cifs_dbg(FYI, "Session Setup\n");
474
475 if (!server) {
476 WARN(1, "%s: server is NULL!\n", __func__);
477 return -EIO;
478 }
479
480 /*
481 * If we are here due to reconnect, free per-smb session key
482 * in case signing was required.
483 */
484 kfree(ses->auth_key.response);
485 ses->auth_key.response = NULL;
486
487 /*
488 * If memory allocation is successful, caller of this function
489 * frees it.
490 */
491 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
492 if (!ses->ntlmssp)
493 return -ENOMEM;
494 ses->ntlmssp->sesskey_per_smbsess = true;
495
496 /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */
497 ses->sectype = RawNTLMSSP;
498
499 ssetup_ntlmssp_authenticate:
500 if (phase == NtLmChallenge)
501 phase = NtLmAuthenticate; /* if ntlmssp, now final phase */
502
503 rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req);
504 if (rc)
505 return rc;
506
507 req->hdr.SessionId = 0; /* First session, not a reauthenticate */
508 req->VcNumber = 0; /* MBZ */
509 /* to enable echos and oplocks */
510 req->hdr.CreditRequest = cpu_to_le16(3);
511
512 /* only one of SMB2 signing flags may be set in SMB2 request */
513 if (server->sign)
514 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
515 else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
516 req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
517 else
518 req->SecurityMode = 0;
519
520 req->Capabilities = 0;
521 req->Channel = 0; /* MBZ */
522
523 iov[0].iov_base = (char *)req;
524 /* 4 for rfc1002 length field and 1 for pad */
525 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
526 if (phase == NtLmNegotiate) {
527 ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE),
528 GFP_KERNEL);
529 if (ntlmssp_blob == NULL) {
530 rc = -ENOMEM;
531 goto ssetup_exit;
532 }
533 build_ntlmssp_negotiate_blob(ntlmssp_blob, ses);
534 if (use_spnego) {
535 /* blob_length = build_spnego_ntlmssp_blob(
536 &security_blob,
537 sizeof(struct _NEGOTIATE_MESSAGE),
538 ntlmssp_blob); */
539 /* BB eventually need to add this */
540 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
541 rc = -EOPNOTSUPP;
542 kfree(ntlmssp_blob);
543 goto ssetup_exit;
544 } else {
545 blob_length = sizeof(struct _NEGOTIATE_MESSAGE);
546 /* with raw NTLMSSP we don't encapsulate in SPNEGO */
547 security_blob = ntlmssp_blob;
548 }
549 } else if (phase == NtLmAuthenticate) {
550 req->hdr.SessionId = ses->Suid;
551 ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500,
552 GFP_KERNEL);
553 if (ntlmssp_blob == NULL) {
554 rc = -ENOMEM;
555 goto ssetup_exit;
556 }
557 rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses,
558 nls_cp);
559 if (rc) {
560 cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n",
561 rc);
562 goto ssetup_exit; /* BB double check error handling */
563 }
564 if (use_spnego) {
565 /* blob_length = build_spnego_ntlmssp_blob(
566 &security_blob,
567 blob_length,
568 ntlmssp_blob); */
569 cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
570 rc = -EOPNOTSUPP;
571 kfree(ntlmssp_blob);
572 goto ssetup_exit;
573 } else {
574 security_blob = ntlmssp_blob;
575 }
576 } else {
577 cifs_dbg(VFS, "illegal ntlmssp phase\n");
578 rc = -EIO;
579 goto ssetup_exit;
580 }
581
582 /* Testing shows that buffer offset must be at location of Buffer[0] */
583 req->SecurityBufferOffset =
584 cpu_to_le16(sizeof(struct smb2_sess_setup_req) -
585 1 /* pad */ - 4 /* rfc1001 len */);
586 req->SecurityBufferLength = cpu_to_le16(blob_length);
587 iov[1].iov_base = security_blob;
588 iov[1].iov_len = blob_length;
589
590 inc_rfc1001_len(req, blob_length - 1 /* pad */);
591
592 /* BB add code to build os and lm fields */
593
594 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype,
595 CIFS_LOG_ERROR | CIFS_NEG_OP);
596
597 kfree(security_blob);
598 rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base;
599 if (resp_buftype != CIFS_NO_BUFFER &&
600 rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) {
601 if (phase != NtLmNegotiate) {
602 cifs_dbg(VFS, "Unexpected more processing error\n");
603 goto ssetup_exit;
604 }
605 if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 !=
606 le16_to_cpu(rsp->SecurityBufferOffset)) {
607 cifs_dbg(VFS, "Invalid security buffer offset %d\n",
608 le16_to_cpu(rsp->SecurityBufferOffset));
609 rc = -EIO;
610 goto ssetup_exit;
611 }
612
613 /* NTLMSSP Negotiate sent now processing challenge (response) */
614 phase = NtLmChallenge; /* process ntlmssp challenge */
615 rc = 0; /* MORE_PROCESSING is not an error here but expected */
616 ses->Suid = rsp->hdr.SessionId;
617 rc = decode_ntlmssp_challenge(rsp->Buffer,
618 le16_to_cpu(rsp->SecurityBufferLength), ses);
619 }
620
621 /*
622 * BB eventually add code for SPNEGO decoding of NtlmChallenge blob,
623 * but at least the raw NTLMSSP case works.
624 */
625 /*
626 * No tcon so can't do
627 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
628 */
629 if (rc != 0)
630 goto ssetup_exit;
631
632 ses->session_flags = le16_to_cpu(rsp->SessionFlags);
633 ssetup_exit:
634 free_rsp_buf(resp_buftype, rsp);
635
636 /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */
637 if ((phase == NtLmChallenge) && (rc == 0))
638 goto ssetup_ntlmssp_authenticate;
639
640 if (!rc) {
641 mutex_lock(&server->srv_mutex);
642 if (server->sign && server->ops->generate_signingkey) {
643 rc = server->ops->generate_signingkey(ses);
644 kfree(ses->auth_key.response);
645 ses->auth_key.response = NULL;
646 if (rc) {
647 cifs_dbg(FYI,
648 "SMB3 session key generation failed\n");
649 mutex_unlock(&server->srv_mutex);
650 goto keygen_exit;
651 }
652 }
653 if (!server->session_estab) {
654 server->sequence_number = 0x2;
655 server->session_estab = true;
656 }
657 mutex_unlock(&server->srv_mutex);
658
659 cifs_dbg(FYI, "SMB2/3 session established successfully\n");
660 spin_lock(&GlobalMid_Lock);
661 ses->status = CifsGood;
662 ses->need_reconnect = false;
663 spin_unlock(&GlobalMid_Lock);
664 }
665
666 keygen_exit:
667 if (!server->sign) {
668 kfree(ses->auth_key.response);
669 ses->auth_key.response = NULL;
670 }
671 kfree(ses->ntlmssp);
672
673 return rc;
674 }
675
676 int
677 SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
678 {
679 struct smb2_logoff_req *req; /* response is also trivial struct */
680 int rc = 0;
681 struct TCP_Server_Info *server;
682
683 cifs_dbg(FYI, "disconnect session %p\n", ses);
684
685 if (ses && (ses->server))
686 server = ses->server;
687 else
688 return -EIO;
689
690 rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req);
691 if (rc)
692 return rc;
693
694 /* since no tcon, smb2_init can not do this, so do here */
695 req->hdr.SessionId = ses->Suid;
696 if (server->sign)
697 req->hdr.Flags |= SMB2_FLAGS_SIGNED;
698
699 rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0);
700 /*
701 * No tcon so can't do
702 * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
703 */
704 return rc;
705 }
706
707 static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
708 {
709 cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
710 }
711
712 #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
713
714 int
715 SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
716 struct cifs_tcon *tcon, const struct nls_table *cp)
717 {
718 struct smb2_tree_connect_req *req;
719 struct smb2_tree_connect_rsp *rsp = NULL;
720 struct kvec iov[2];
721 int rc = 0;
722 int resp_buftype;
723 int unc_path_len;
724 struct TCP_Server_Info *server;
725 __le16 *unc_path = NULL;
726
727 cifs_dbg(FYI, "TCON\n");
728
729 if ((ses->server) && tree)
730 server = ses->server;
731 else
732 return -EIO;
733
734 if (tcon && tcon->bad_network_name)
735 return -ENOENT;
736
737 unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
738 if (unc_path == NULL)
739 return -ENOMEM;
740
741 unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
742 unc_path_len *= 2;
743 if (unc_path_len < 2) {
744 kfree(unc_path);
745 return -EINVAL;
746 }
747
748 rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req);
749 if (rc) {
750 kfree(unc_path);
751 return rc;
752 }
753
754 if (tcon == NULL) {
755 /* since no tcon, smb2_init can not do this, so do here */
756 req->hdr.SessionId = ses->Suid;
757 /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED)
758 req->hdr.Flags |= SMB2_FLAGS_SIGNED; */
759 }
760
761 iov[0].iov_base = (char *)req;
762 /* 4 for rfc1002 length field and 1 for pad */
763 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
764
765 /* Testing shows that buffer offset must be at location of Buffer[0] */
766 req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
767 - 1 /* pad */ - 4 /* do not count rfc1001 len field */);
768 req->PathLength = cpu_to_le16(unc_path_len - 2);
769 iov[1].iov_base = unc_path;
770 iov[1].iov_len = unc_path_len;
771
772 inc_rfc1001_len(req, unc_path_len - 1 /* pad */);
773
774 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
775 rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base;
776
777 if (rc != 0) {
778 if (tcon) {
779 cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
780 tcon->need_reconnect = true;
781 }
782 goto tcon_error_exit;
783 }
784
785 if (tcon == NULL) {
786 ses->ipc_tid = rsp->hdr.TreeId;
787 goto tcon_exit;
788 }
789
790 if (rsp->ShareType & SMB2_SHARE_TYPE_DISK)
791 cifs_dbg(FYI, "connection to disk share\n");
792 else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) {
793 tcon->ipc = true;
794 cifs_dbg(FYI, "connection to pipe share\n");
795 } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) {
796 tcon->print = true;
797 cifs_dbg(FYI, "connection to printer\n");
798 } else {
799 cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
800 rc = -EOPNOTSUPP;
801 goto tcon_error_exit;
802 }
803
804 tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
805 tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
806 tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
807 tcon->tidStatus = CifsGood;
808 tcon->need_reconnect = false;
809 tcon->tid = rsp->hdr.TreeId;
810 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
811
812 if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
813 ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
814 cifs_dbg(VFS, "DFS capability contradicts DFS flag\n");
815
816 tcon_exit:
817 free_rsp_buf(resp_buftype, rsp);
818 kfree(unc_path);
819 return rc;
820
821 tcon_error_exit:
822 if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) {
823 cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
824 tcon->bad_network_name = true;
825 }
826 goto tcon_exit;
827 }
828
829 int
830 SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
831 {
832 struct smb2_tree_disconnect_req *req; /* response is trivial */
833 int rc = 0;
834 struct TCP_Server_Info *server;
835 struct cifs_ses *ses = tcon->ses;
836
837 cifs_dbg(FYI, "Tree Disconnect\n");
838
839 if (ses && (ses->server))
840 server = ses->server;
841 else
842 return -EIO;
843
844 if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
845 return 0;
846
847 rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req);
848 if (rc)
849 return rc;
850
851 rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0);
852 if (rc)
853 cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
854
855 return rc;
856 }
857
858 static struct create_lease *
859 create_lease_buf(u8 *lease_key, u8 oplock)
860 {
861 struct create_lease *buf;
862
863 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
864 if (!buf)
865 return NULL;
866
867 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
868 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
869 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
870 buf->lcontext.LeaseState = SMB2_LEASE_WRITE_CACHING |
871 SMB2_LEASE_READ_CACHING;
872 else if (oplock == SMB2_OPLOCK_LEVEL_II)
873 buf->lcontext.LeaseState = SMB2_LEASE_READ_CACHING;
874 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
875 buf->lcontext.LeaseState = SMB2_LEASE_HANDLE_CACHING |
876 SMB2_LEASE_READ_CACHING |
877 SMB2_LEASE_WRITE_CACHING;
878
879 buf->ccontext.DataOffset = cpu_to_le16(offsetof
880 (struct create_lease, lcontext));
881 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
882 buf->ccontext.NameOffset = cpu_to_le16(offsetof
883 (struct create_lease, Name));
884 buf->ccontext.NameLength = cpu_to_le16(4);
885 buf->Name[0] = 'R';
886 buf->Name[1] = 'q';
887 buf->Name[2] = 'L';
888 buf->Name[3] = 's';
889 return buf;
890 }
891
892 static struct create_durable *
893 create_durable_buf(void)
894 {
895 struct create_durable *buf;
896
897 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
898 if (!buf)
899 return NULL;
900
901 buf->ccontext.DataOffset = cpu_to_le16(offsetof
902 (struct create_durable, Data));
903 buf->ccontext.DataLength = cpu_to_le32(16);
904 buf->ccontext.NameOffset = cpu_to_le16(offsetof
905 (struct create_durable, Name));
906 buf->ccontext.NameLength = cpu_to_le16(4);
907 buf->Name[0] = 'D';
908 buf->Name[1] = 'H';
909 buf->Name[2] = 'n';
910 buf->Name[3] = 'Q';
911 return buf;
912 }
913
914 static struct create_durable *
915 create_reconnect_durable_buf(struct cifs_fid *fid)
916 {
917 struct create_durable *buf;
918
919 buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
920 if (!buf)
921 return NULL;
922
923 buf->ccontext.DataOffset = cpu_to_le16(offsetof
924 (struct create_durable, Data));
925 buf->ccontext.DataLength = cpu_to_le32(16);
926 buf->ccontext.NameOffset = cpu_to_le16(offsetof
927 (struct create_durable, Name));
928 buf->ccontext.NameLength = cpu_to_le16(4);
929 buf->Data.Fid.PersistentFileId = fid->persistent_fid;
930 buf->Data.Fid.VolatileFileId = fid->volatile_fid;
931 buf->Name[0] = 'D';
932 buf->Name[1] = 'H';
933 buf->Name[2] = 'n';
934 buf->Name[3] = 'C';
935 return buf;
936 }
937
938 static __u8
939 parse_lease_state(struct smb2_create_rsp *rsp)
940 {
941 char *data_offset;
942 struct create_lease *lc;
943 bool found = false;
944 unsigned int next = 0;
945 char *name;
946
947 data_offset = (char *)rsp + 4 + le32_to_cpu(rsp->CreateContextsOffset);
948 lc = (struct create_lease *)data_offset;
949 do {
950 lc = (struct create_lease *)((char *)lc + next);
951 name = le16_to_cpu(lc->ccontext.NameOffset) + (char *)lc;
952 if (le16_to_cpu(lc->ccontext.NameLength) != 4 ||
953 strncmp(name, "RqLs", 4)) {
954 next = le32_to_cpu(lc->ccontext.Next);
955 continue;
956 }
957 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
958 return SMB2_OPLOCK_LEVEL_NOCHANGE;
959 found = true;
960 break;
961 } while (next != 0);
962
963 if (!found)
964 return 0;
965
966 return le32_to_cpu(lc->lcontext.LeaseState);
967 }
968
969 static int
970 add_lease_context(struct kvec *iov, unsigned int *num_iovec, __u8 *oplock)
971 {
972 struct smb2_create_req *req = iov[0].iov_base;
973 unsigned int num = *num_iovec;
974
975 iov[num].iov_base = create_lease_buf(oplock+1, *oplock);
976 if (iov[num].iov_base == NULL)
977 return -ENOMEM;
978 iov[num].iov_len = sizeof(struct create_lease);
979 req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
980 if (!req->CreateContextsOffset)
981 req->CreateContextsOffset = cpu_to_le32(
982 sizeof(struct smb2_create_req) - 4 +
983 iov[num - 1].iov_len);
984 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_lease));
985 inc_rfc1001_len(&req->hdr, sizeof(struct create_lease));
986 *num_iovec = num + 1;
987 return 0;
988 }
989
990 static int
991 add_durable_context(struct kvec *iov, unsigned int *num_iovec,
992 struct cifs_open_parms *oparms)
993 {
994 struct smb2_create_req *req = iov[0].iov_base;
995 unsigned int num = *num_iovec;
996
997 if (oparms->reconnect) {
998 iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
999 /* indicate that we don't need to relock the file */
1000 oparms->reconnect = false;
1001 } else
1002 iov[num].iov_base = create_durable_buf();
1003 if (iov[num].iov_base == NULL)
1004 return -ENOMEM;
1005 iov[num].iov_len = sizeof(struct create_durable);
1006 if (!req->CreateContextsOffset)
1007 req->CreateContextsOffset =
1008 cpu_to_le32(sizeof(struct smb2_create_req) - 4 +
1009 iov[1].iov_len);
1010 le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
1011 inc_rfc1001_len(&req->hdr, sizeof(struct create_durable));
1012 *num_iovec = num + 1;
1013 return 0;
1014 }
1015
1016 int
1017 SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
1018 __u8 *oplock, struct smb2_file_all_info *buf,
1019 struct smb2_err_rsp **err_buf)
1020 {
1021 struct smb2_create_req *req;
1022 struct smb2_create_rsp *rsp;
1023 struct TCP_Server_Info *server;
1024 struct cifs_tcon *tcon = oparms->tcon;
1025 struct cifs_ses *ses = tcon->ses;
1026 struct kvec iov[4];
1027 int resp_buftype;
1028 int uni_path_len;
1029 __le16 *copy_path = NULL;
1030 int copy_size;
1031 int rc = 0;
1032 unsigned int num_iovecs = 2;
1033 __u32 file_attributes = 0;
1034
1035 cifs_dbg(FYI, "create/open\n");
1036
1037 if (ses && (ses->server))
1038 server = ses->server;
1039 else
1040 return -EIO;
1041
1042 rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req);
1043 if (rc)
1044 return rc;
1045
1046 if (oparms->create_options & CREATE_OPTION_READONLY)
1047 file_attributes |= ATTR_READONLY;
1048
1049 req->ImpersonationLevel = IL_IMPERSONATION;
1050 req->DesiredAccess = cpu_to_le32(oparms->desired_access);
1051 /* File attributes ignored on open (used in create though) */
1052 req->FileAttributes = cpu_to_le32(file_attributes);
1053 req->ShareAccess = FILE_SHARE_ALL_LE;
1054 req->CreateDisposition = cpu_to_le32(oparms->disposition);
1055 req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
1056 uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
1057 /* do not count rfc1001 len field */
1058 req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) - 4);
1059
1060 iov[0].iov_base = (char *)req;
1061 /* 4 for rfc1002 length field */
1062 iov[0].iov_len = get_rfc1002_length(req) + 4;
1063
1064 /* MUST set path len (NameLength) to 0 opening root of share */
1065 req->NameLength = cpu_to_le16(uni_path_len - 2);
1066 /* -1 since last byte is buf[0] which is sent below (path) */
1067 iov[0].iov_len--;
1068 if (uni_path_len % 8 != 0) {
1069 copy_size = uni_path_len / 8 * 8;
1070 if (copy_size < uni_path_len)
1071 copy_size += 8;
1072
1073 copy_path = kzalloc(copy_size, GFP_KERNEL);
1074 if (!copy_path)
1075 return -ENOMEM;
1076 memcpy((char *)copy_path, (const char *)path,
1077 uni_path_len);
1078 uni_path_len = copy_size;
1079 path = copy_path;
1080 }
1081
1082 iov[1].iov_len = uni_path_len;
1083 iov[1].iov_base = path;
1084 /* -1 since last byte is buf[0] which was counted in smb2_buf_len */
1085 inc_rfc1001_len(req, uni_path_len - 1);
1086
1087 if (!server->oplocks)
1088 *oplock = SMB2_OPLOCK_LEVEL_NONE;
1089
1090 if (!(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
1091 *oplock == SMB2_OPLOCK_LEVEL_NONE)
1092 req->RequestedOplockLevel = *oplock;
1093 else {
1094 rc = add_lease_context(iov, &num_iovecs, oplock);
1095 if (rc) {
1096 cifs_small_buf_release(req);
1097 kfree(copy_path);
1098 return rc;
1099 }
1100 }
1101
1102 if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1103 /* need to set Next field of lease context if we request it */
1104 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
1105 struct create_context *ccontext =
1106 (struct create_context *)iov[num_iovecs-1].iov_base;
1107 ccontext->Next =
1108 cpu_to_le32(sizeof(struct create_lease));
1109 }
1110 rc = add_durable_context(iov, &num_iovecs, oparms);
1111 if (rc) {
1112 cifs_small_buf_release(req);
1113 kfree(copy_path);
1114 kfree(iov[num_iovecs-1].iov_base);
1115 return rc;
1116 }
1117 }
1118
1119 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1120 rsp = (struct smb2_create_rsp *)iov[0].iov_base;
1121
1122 if (rc != 0) {
1123 cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
1124 if (err_buf)
1125 *err_buf = kmemdup(rsp, get_rfc1002_length(rsp) + 4,
1126 GFP_KERNEL);
1127 goto creat_exit;
1128 }
1129
1130 oparms->fid->persistent_fid = rsp->PersistentFileId;
1131 oparms->fid->volatile_fid = rsp->VolatileFileId;
1132
1133 if (buf) {
1134 memcpy(buf, &rsp->CreationTime, 32);
1135 buf->AllocationSize = rsp->AllocationSize;
1136 buf->EndOfFile = rsp->EndofFile;
1137 buf->Attributes = rsp->FileAttributes;
1138 buf->NumberOfLinks = cpu_to_le32(1);
1139 buf->DeletePending = 0;
1140 }
1141
1142 if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
1143 *oplock = parse_lease_state(rsp);
1144 else
1145 *oplock = rsp->OplockLevel;
1146 creat_exit:
1147 kfree(copy_path);
1148 free_rsp_buf(resp_buftype, rsp);
1149 return rc;
1150 }
1151
1152 /*
1153 * SMB2 IOCTL is used for both IOCTLs and FSCTLs
1154 */
1155 int
1156 SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1157 u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data,
1158 u32 indatalen, char **out_data, u32 *plen /* returned data len */)
1159 {
1160 struct smb2_ioctl_req *req;
1161 struct smb2_ioctl_rsp *rsp;
1162 struct TCP_Server_Info *server;
1163 struct cifs_ses *ses = tcon->ses;
1164 struct kvec iov[2];
1165 int resp_buftype;
1166 int num_iovecs;
1167 int rc = 0;
1168
1169 cifs_dbg(FYI, "SMB2 IOCTL\n");
1170
1171 /* zero out returned data len, in case of error */
1172 if (plen)
1173 *plen = 0;
1174
1175 if (ses && (ses->server))
1176 server = ses->server;
1177 else
1178 return -EIO;
1179
1180 rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req);
1181 if (rc)
1182 return rc;
1183
1184 req->CtlCode = cpu_to_le32(opcode);
1185 req->PersistentFileId = persistent_fid;
1186 req->VolatileFileId = volatile_fid;
1187
1188 if (indatalen) {
1189 req->InputCount = cpu_to_le32(indatalen);
1190 /* do not set InputOffset if no input data */
1191 req->InputOffset =
1192 cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4);
1193 iov[1].iov_base = in_data;
1194 iov[1].iov_len = indatalen;
1195 num_iovecs = 2;
1196 } else
1197 num_iovecs = 1;
1198
1199 req->OutputOffset = 0;
1200 req->OutputCount = 0; /* MBZ */
1201
1202 /*
1203 * Could increase MaxOutputResponse, but that would require more
1204 * than one credit. Windows typically sets this smaller, but for some
1205 * ioctls it may be useful to allow server to send more. No point
1206 * limiting what the server can send as long as fits in one credit
1207 */
1208 req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */
1209
1210 if (is_fsctl)
1211 req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
1212 else
1213 req->Flags = 0;
1214
1215 iov[0].iov_base = (char *)req;
1216 /* 4 for rfc1002 length field */
1217 iov[0].iov_len = get_rfc1002_length(req) + 4;
1218
1219 if (indatalen)
1220 inc_rfc1001_len(req, indatalen);
1221
1222 rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0);
1223 rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base;
1224
1225 if (rc != 0) {
1226 if (tcon)
1227 cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
1228 goto ioctl_exit;
1229 }
1230
1231 /* check if caller wants to look at return data or just return rc */
1232 if ((plen == NULL) || (out_data == NULL))
1233 goto ioctl_exit;
1234
1235 *plen = le32_to_cpu(rsp->OutputCount);
1236
1237 /* We check for obvious errors in the output buffer length and offset */
1238 if (*plen == 0)
1239 goto ioctl_exit; /* server returned no data */
1240 else if (*plen > 0xFF00) {
1241 cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
1242 *plen = 0;
1243 rc = -EIO;
1244 goto ioctl_exit;
1245 }
1246
1247 if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) {
1248 cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
1249 le32_to_cpu(rsp->OutputOffset));
1250 *plen = 0;
1251 rc = -EIO;
1252 goto ioctl_exit;
1253 }
1254
1255 *out_data = kmalloc(*plen, GFP_KERNEL);
1256 if (*out_data == NULL) {
1257 rc = -ENOMEM;
1258 goto ioctl_exit;
1259 }
1260
1261 memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset),
1262 *plen);
1263 ioctl_exit:
1264 free_rsp_buf(resp_buftype, rsp);
1265 return rc;
1266 }
1267
1268 int
1269 SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
1270 u64 persistent_fid, u64 volatile_fid)
1271 {
1272 struct smb2_close_req *req;
1273 struct smb2_close_rsp *rsp;
1274 struct TCP_Server_Info *server;
1275 struct cifs_ses *ses = tcon->ses;
1276 struct kvec iov[1];
1277 int resp_buftype;
1278 int rc = 0;
1279
1280 cifs_dbg(FYI, "Close\n");
1281
1282 if (ses && (ses->server))
1283 server = ses->server;
1284 else
1285 return -EIO;
1286
1287 rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req);
1288 if (rc)
1289 return rc;
1290
1291 req->PersistentFileId = persistent_fid;
1292 req->VolatileFileId = volatile_fid;
1293
1294 iov[0].iov_base = (char *)req;
1295 /* 4 for rfc1002 length field */
1296 iov[0].iov_len = get_rfc1002_length(req) + 4;
1297
1298 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1299 rsp = (struct smb2_close_rsp *)iov[0].iov_base;
1300
1301 if (rc != 0) {
1302 if (tcon)
1303 cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
1304 goto close_exit;
1305 }
1306
1307 /* BB FIXME - decode close response, update inode for caching */
1308
1309 close_exit:
1310 free_rsp_buf(resp_buftype, rsp);
1311 return rc;
1312 }
1313
1314 static int
1315 validate_buf(unsigned int offset, unsigned int buffer_length,
1316 struct smb2_hdr *hdr, unsigned int min_buf_size)
1317
1318 {
1319 unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length);
1320 char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr;
1321 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1322 char *end_of_buf = begin_of_buf + buffer_length;
1323
1324
1325 if (buffer_length < min_buf_size) {
1326 cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
1327 buffer_length, min_buf_size);
1328 return -EINVAL;
1329 }
1330
1331 /* check if beyond RFC1001 maximum length */
1332 if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
1333 cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
1334 buffer_length, smb_len);
1335 return -EINVAL;
1336 }
1337
1338 if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
1339 cifs_dbg(VFS, "illegal server response, bad offset to data\n");
1340 return -EINVAL;
1341 }
1342
1343 return 0;
1344 }
1345
1346 /*
1347 * If SMB buffer fields are valid, copy into temporary buffer to hold result.
1348 * Caller must free buffer.
1349 */
1350 static int
1351 validate_and_copy_buf(unsigned int offset, unsigned int buffer_length,
1352 struct smb2_hdr *hdr, unsigned int minbufsize,
1353 char *data)
1354
1355 {
1356 char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr;
1357 int rc;
1358
1359 if (!data)
1360 return -EINVAL;
1361
1362 rc = validate_buf(offset, buffer_length, hdr, minbufsize);
1363 if (rc)
1364 return rc;
1365
1366 memcpy(data, begin_of_buf, buffer_length);
1367
1368 return 0;
1369 }
1370
1371 static int
1372 query_info(const unsigned int xid, struct cifs_tcon *tcon,
1373 u64 persistent_fid, u64 volatile_fid, u8 info_class,
1374 size_t output_len, size_t min_len, void *data)
1375 {
1376 struct smb2_query_info_req *req;
1377 struct smb2_query_info_rsp *rsp = NULL;
1378 struct kvec iov[2];
1379 int rc = 0;
1380 int resp_buftype;
1381 struct TCP_Server_Info *server;
1382 struct cifs_ses *ses = tcon->ses;
1383
1384 cifs_dbg(FYI, "Query Info\n");
1385
1386 if (ses && (ses->server))
1387 server = ses->server;
1388 else
1389 return -EIO;
1390
1391 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
1392 if (rc)
1393 return rc;
1394
1395 req->InfoType = SMB2_O_INFO_FILE;
1396 req->FileInfoClass = info_class;
1397 req->PersistentFileId = persistent_fid;
1398 req->VolatileFileId = volatile_fid;
1399 /* 4 for rfc1002 length field and 1 for Buffer */
1400 req->InputBufferOffset =
1401 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
1402 req->OutputBufferLength = cpu_to_le32(output_len);
1403
1404 iov[0].iov_base = (char *)req;
1405 /* 4 for rfc1002 length field */
1406 iov[0].iov_len = get_rfc1002_length(req) + 4;
1407
1408 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1409 rsp = (struct smb2_query_info_rsp *)iov[0].iov_base;
1410
1411 if (rc) {
1412 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
1413 goto qinf_exit;
1414 }
1415
1416 rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset),
1417 le32_to_cpu(rsp->OutputBufferLength),
1418 &rsp->hdr, min_len, data);
1419
1420 qinf_exit:
1421 free_rsp_buf(resp_buftype, rsp);
1422 return rc;
1423 }
1424
1425 int
1426 SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
1427 u64 persistent_fid, u64 volatile_fid,
1428 struct smb2_file_all_info *data)
1429 {
1430 return query_info(xid, tcon, persistent_fid, volatile_fid,
1431 FILE_ALL_INFORMATION,
1432 sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
1433 sizeof(struct smb2_file_all_info), data);
1434 }
1435
1436 int
1437 SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
1438 u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
1439 {
1440 return query_info(xid, tcon, persistent_fid, volatile_fid,
1441 FILE_INTERNAL_INFORMATION,
1442 sizeof(struct smb2_file_internal_info),
1443 sizeof(struct smb2_file_internal_info), uniqueid);
1444 }
1445
1446 /*
1447 * This is a no-op for now. We're not really interested in the reply, but
1448 * rather in the fact that the server sent one and that server->lstrp
1449 * gets updated.
1450 *
1451 * FIXME: maybe we should consider checking that the reply matches request?
1452 */
1453 static void
1454 smb2_echo_callback(struct mid_q_entry *mid)
1455 {
1456 struct TCP_Server_Info *server = mid->callback_data;
1457 struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf;
1458 unsigned int credits_received = 1;
1459
1460 if (mid->mid_state == MID_RESPONSE_RECEIVED)
1461 credits_received = le16_to_cpu(smb2->hdr.CreditRequest);
1462
1463 DeleteMidQEntry(mid);
1464 add_credits(server, credits_received, CIFS_ECHO_OP);
1465 }
1466
1467 int
1468 SMB2_echo(struct TCP_Server_Info *server)
1469 {
1470 struct smb2_echo_req *req;
1471 int rc = 0;
1472 struct kvec iov;
1473 struct smb_rqst rqst = { .rq_iov = &iov,
1474 .rq_nvec = 1 };
1475
1476 cifs_dbg(FYI, "In echo request\n");
1477
1478 rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req);
1479 if (rc)
1480 return rc;
1481
1482 req->hdr.CreditRequest = cpu_to_le16(1);
1483
1484 iov.iov_base = (char *)req;
1485 /* 4 for rfc1002 length field */
1486 iov.iov_len = get_rfc1002_length(req) + 4;
1487
1488 rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server,
1489 CIFS_ECHO_OP);
1490 if (rc)
1491 cifs_dbg(FYI, "Echo request failed: %d\n", rc);
1492
1493 cifs_small_buf_release(req);
1494 return rc;
1495 }
1496
1497 int
1498 SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
1499 u64 volatile_fid)
1500 {
1501 struct smb2_flush_req *req;
1502 struct TCP_Server_Info *server;
1503 struct cifs_ses *ses = tcon->ses;
1504 struct kvec iov[1];
1505 int resp_buftype;
1506 int rc = 0;
1507
1508 cifs_dbg(FYI, "Flush\n");
1509
1510 if (ses && (ses->server))
1511 server = ses->server;
1512 else
1513 return -EIO;
1514
1515 rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req);
1516 if (rc)
1517 return rc;
1518
1519 req->PersistentFileId = persistent_fid;
1520 req->VolatileFileId = volatile_fid;
1521
1522 iov[0].iov_base = (char *)req;
1523 /* 4 for rfc1002 length field */
1524 iov[0].iov_len = get_rfc1002_length(req) + 4;
1525
1526 rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0);
1527
1528 if ((rc != 0) && tcon)
1529 cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
1530
1531 free_rsp_buf(resp_buftype, iov[0].iov_base);
1532 return rc;
1533 }
1534
1535 /*
1536 * To form a chain of read requests, any read requests after the first should
1537 * have the end_of_chain boolean set to true.
1538 */
1539 static int
1540 smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms,
1541 unsigned int remaining_bytes, int request_type)
1542 {
1543 int rc = -EACCES;
1544 struct smb2_read_req *req = NULL;
1545
1546 rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req);
1547 if (rc)
1548 return rc;
1549 if (io_parms->tcon->ses->server == NULL)
1550 return -ECONNABORTED;
1551
1552 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1553
1554 req->PersistentFileId = io_parms->persistent_fid;
1555 req->VolatileFileId = io_parms->volatile_fid;
1556 req->ReadChannelInfoOffset = 0; /* reserved */
1557 req->ReadChannelInfoLength = 0; /* reserved */
1558 req->Channel = 0; /* reserved */
1559 req->MinimumCount = 0;
1560 req->Length = cpu_to_le32(io_parms->length);
1561 req->Offset = cpu_to_le64(io_parms->offset);
1562
1563 if (request_type & CHAINED_REQUEST) {
1564 if (!(request_type & END_OF_CHAIN)) {
1565 /* 4 for rfc1002 length field */
1566 req->hdr.NextCommand =
1567 cpu_to_le32(get_rfc1002_length(req) + 4);
1568 } else /* END_OF_CHAIN */
1569 req->hdr.NextCommand = 0;
1570 if (request_type & RELATED_REQUEST) {
1571 req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
1572 /*
1573 * Related requests use info from previous read request
1574 * in chain.
1575 */
1576 req->hdr.SessionId = 0xFFFFFFFF;
1577 req->hdr.TreeId = 0xFFFFFFFF;
1578 req->PersistentFileId = 0xFFFFFFFF;
1579 req->VolatileFileId = 0xFFFFFFFF;
1580 }
1581 }
1582 if (remaining_bytes > io_parms->length)
1583 req->RemainingBytes = cpu_to_le32(remaining_bytes);
1584 else
1585 req->RemainingBytes = 0;
1586
1587 iov[0].iov_base = (char *)req;
1588 /* 4 for rfc1002 length field */
1589 iov[0].iov_len = get_rfc1002_length(req) + 4;
1590 return rc;
1591 }
1592
1593 static void
1594 smb2_readv_callback(struct mid_q_entry *mid)
1595 {
1596 struct cifs_readdata *rdata = mid->callback_data;
1597 struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
1598 struct TCP_Server_Info *server = tcon->ses->server;
1599 struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base;
1600 unsigned int credits_received = 1;
1601 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1602 .rq_nvec = 1,
1603 .rq_pages = rdata->pages,
1604 .rq_npages = rdata->nr_pages,
1605 .rq_pagesz = rdata->pagesz,
1606 .rq_tailsz = rdata->tailsz };
1607
1608 cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
1609 __func__, mid->mid, mid->mid_state, rdata->result,
1610 rdata->bytes);
1611
1612 switch (mid->mid_state) {
1613 case MID_RESPONSE_RECEIVED:
1614 credits_received = le16_to_cpu(buf->CreditRequest);
1615 /* result already set, check signature */
1616 if (server->sign) {
1617 int rc;
1618
1619 rc = smb2_verify_signature(&rqst, server);
1620 if (rc)
1621 cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
1622 rc);
1623 }
1624 /* FIXME: should this be counted toward the initiating task? */
1625 task_io_account_read(rdata->bytes);
1626 cifs_stats_bytes_read(tcon, rdata->bytes);
1627 break;
1628 case MID_REQUEST_SUBMITTED:
1629 case MID_RETRY_NEEDED:
1630 rdata->result = -EAGAIN;
1631 break;
1632 default:
1633 if (rdata->result != -ENODATA)
1634 rdata->result = -EIO;
1635 }
1636
1637 if (rdata->result)
1638 cifs_stats_fail_inc(tcon, SMB2_READ_HE);
1639
1640 queue_work(cifsiod_wq, &rdata->work);
1641 DeleteMidQEntry(mid);
1642 add_credits(server, credits_received, 0);
1643 }
1644
1645 /* smb2_async_readv - send an async write, and set up mid to handle result */
1646 int
1647 smb2_async_readv(struct cifs_readdata *rdata)
1648 {
1649 int rc;
1650 struct smb2_hdr *buf;
1651 struct cifs_io_parms io_parms;
1652 struct smb_rqst rqst = { .rq_iov = &rdata->iov,
1653 .rq_nvec = 1 };
1654
1655 cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
1656 __func__, rdata->offset, rdata->bytes);
1657
1658 io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
1659 io_parms.offset = rdata->offset;
1660 io_parms.length = rdata->bytes;
1661 io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
1662 io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
1663 io_parms.pid = rdata->pid;
1664 rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0);
1665 if (rc)
1666 return rc;
1667
1668 buf = (struct smb2_hdr *)rdata->iov.iov_base;
1669 /* 4 for rfc1002 length field */
1670 rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4;
1671
1672 kref_get(&rdata->refcount);
1673 rc = cifs_call_async(io_parms.tcon->ses->server, &rqst,
1674 cifs_readv_receive, smb2_readv_callback,
1675 rdata, 0);
1676 if (rc) {
1677 kref_put(&rdata->refcount, cifs_readdata_release);
1678 cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
1679 }
1680
1681 cifs_small_buf_release(buf);
1682 return rc;
1683 }
1684
1685 int
1686 SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
1687 unsigned int *nbytes, char **buf, int *buf_type)
1688 {
1689 int resp_buftype, rc = -EACCES;
1690 struct smb2_read_rsp *rsp = NULL;
1691 struct kvec iov[1];
1692
1693 *nbytes = 0;
1694 rc = smb2_new_read_req(iov, io_parms, 0, 0);
1695 if (rc)
1696 return rc;
1697
1698 rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1,
1699 &resp_buftype, CIFS_LOG_ERROR);
1700
1701 rsp = (struct smb2_read_rsp *)iov[0].iov_base;
1702
1703 if (rsp->hdr.Status == STATUS_END_OF_FILE) {
1704 free_rsp_buf(resp_buftype, iov[0].iov_base);
1705 return 0;
1706 }
1707
1708 if (rc) {
1709 cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
1710 cifs_dbg(VFS, "Send error in read = %d\n", rc);
1711 } else {
1712 *nbytes = le32_to_cpu(rsp->DataLength);
1713 if ((*nbytes > CIFS_MAX_MSGSIZE) ||
1714 (*nbytes > io_parms->length)) {
1715 cifs_dbg(FYI, "bad length %d for count %d\n",
1716 *nbytes, io_parms->length);
1717 rc = -EIO;
1718 *nbytes = 0;
1719 }
1720 }
1721
1722 if (*buf) {
1723 memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset,
1724 *nbytes);
1725 free_rsp_buf(resp_buftype, iov[0].iov_base);
1726 } else if (resp_buftype != CIFS_NO_BUFFER) {
1727 *buf = iov[0].iov_base;
1728 if (resp_buftype == CIFS_SMALL_BUFFER)
1729 *buf_type = CIFS_SMALL_BUFFER;
1730 else if (resp_buftype == CIFS_LARGE_BUFFER)
1731 *buf_type = CIFS_LARGE_BUFFER;
1732 }
1733 return rc;
1734 }
1735
1736 /*
1737 * Check the mid_state and signature on received buffer (if any), and queue the
1738 * workqueue completion task.
1739 */
1740 static void
1741 smb2_writev_callback(struct mid_q_entry *mid)
1742 {
1743 struct cifs_writedata *wdata = mid->callback_data;
1744 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1745 unsigned int written;
1746 struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
1747 unsigned int credits_received = 1;
1748
1749 switch (mid->mid_state) {
1750 case MID_RESPONSE_RECEIVED:
1751 credits_received = le16_to_cpu(rsp->hdr.CreditRequest);
1752 wdata->result = smb2_check_receive(mid, tcon->ses->server, 0);
1753 if (wdata->result != 0)
1754 break;
1755
1756 written = le32_to_cpu(rsp->DataLength);
1757 /*
1758 * Mask off high 16 bits when bytes written as returned
1759 * by the server is greater than bytes requested by the
1760 * client. OS/2 servers are known to set incorrect
1761 * CountHigh values.
1762 */
1763 if (written > wdata->bytes)
1764 written &= 0xFFFF;
1765
1766 if (written < wdata->bytes)
1767 wdata->result = -ENOSPC;
1768 else
1769 wdata->bytes = written;
1770 break;
1771 case MID_REQUEST_SUBMITTED:
1772 case MID_RETRY_NEEDED:
1773 wdata->result = -EAGAIN;
1774 break;
1775 default:
1776 wdata->result = -EIO;
1777 break;
1778 }
1779
1780 if (wdata->result)
1781 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
1782
1783 queue_work(cifsiod_wq, &wdata->work);
1784 DeleteMidQEntry(mid);
1785 add_credits(tcon->ses->server, credits_received, 0);
1786 }
1787
1788 /* smb2_async_writev - send an async write, and set up mid to handle result */
1789 int
1790 smb2_async_writev(struct cifs_writedata *wdata)
1791 {
1792 int rc = -EACCES;
1793 struct smb2_write_req *req = NULL;
1794 struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
1795 struct kvec iov;
1796 struct smb_rqst rqst;
1797
1798 rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req);
1799 if (rc)
1800 goto async_writev_out;
1801
1802 req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid);
1803
1804 req->PersistentFileId = wdata->cfile->fid.persistent_fid;
1805 req->VolatileFileId = wdata->cfile->fid.volatile_fid;
1806 req->WriteChannelInfoOffset = 0;
1807 req->WriteChannelInfoLength = 0;
1808 req->Channel = 0;
1809 req->Offset = cpu_to_le64(wdata->offset);
1810 /* 4 for rfc1002 length field */
1811 req->DataOffset = cpu_to_le16(
1812 offsetof(struct smb2_write_req, Buffer) - 4);
1813 req->RemainingBytes = 0;
1814
1815 /* 4 for rfc1002 length field and 1 for Buffer */
1816 iov.iov_len = get_rfc1002_length(req) + 4 - 1;
1817 iov.iov_base = req;
1818
1819 rqst.rq_iov = &iov;
1820 rqst.rq_nvec = 1;
1821 rqst.rq_pages = wdata->pages;
1822 rqst.rq_npages = wdata->nr_pages;
1823 rqst.rq_pagesz = wdata->pagesz;
1824 rqst.rq_tailsz = wdata->tailsz;
1825
1826 cifs_dbg(FYI, "async write at %llu %u bytes\n",
1827 wdata->offset, wdata->bytes);
1828
1829 req->Length = cpu_to_le32(wdata->bytes);
1830
1831 inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */);
1832
1833 kref_get(&wdata->refcount);
1834 rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
1835 smb2_writev_callback, wdata, 0);
1836
1837 if (rc) {
1838 kref_put(&wdata->refcount, cifs_writedata_release);
1839 cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
1840 }
1841
1842 async_writev_out:
1843 cifs_small_buf_release(req);
1844 return rc;
1845 }
1846
1847 /*
1848 * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
1849 * The length field from io_parms must be at least 1 and indicates a number of
1850 * elements with data to write that begins with position 1 in iov array. All
1851 * data length is specified by count.
1852 */
1853 int
1854 SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
1855 unsigned int *nbytes, struct kvec *iov, int n_vec)
1856 {
1857 int rc = 0;
1858 struct smb2_write_req *req = NULL;
1859 struct smb2_write_rsp *rsp = NULL;
1860 int resp_buftype;
1861 *nbytes = 0;
1862
1863 if (n_vec < 1)
1864 return rc;
1865
1866 rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req);
1867 if (rc)
1868 return rc;
1869
1870 if (io_parms->tcon->ses->server == NULL)
1871 return -ECONNABORTED;
1872
1873 req->hdr.ProcessId = cpu_to_le32(io_parms->pid);
1874
1875 req->PersistentFileId = io_parms->persistent_fid;
1876 req->VolatileFileId = io_parms->volatile_fid;
1877 req->WriteChannelInfoOffset = 0;
1878 req->WriteChannelInfoLength = 0;
1879 req->Channel = 0;
1880 req->Length = cpu_to_le32(io_parms->length);
1881 req->Offset = cpu_to_le64(io_parms->offset);
1882 /* 4 for rfc1002 length field */
1883 req->DataOffset = cpu_to_le16(
1884 offsetof(struct smb2_write_req, Buffer) - 4);
1885 req->RemainingBytes = 0;
1886
1887 iov[0].iov_base = (char *)req;
1888 /* 4 for rfc1002 length field and 1 for Buffer */
1889 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
1890
1891 /* length of entire message including data to be written */
1892 inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */);
1893
1894 rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1,
1895 &resp_buftype, 0);
1896 rsp = (struct smb2_write_rsp *)iov[0].iov_base;
1897
1898 if (rc) {
1899 cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
1900 cifs_dbg(VFS, "Send error in write = %d\n", rc);
1901 } else
1902 *nbytes = le32_to_cpu(rsp->DataLength);
1903
1904 free_rsp_buf(resp_buftype, rsp);
1905 return rc;
1906 }
1907
1908 static unsigned int
1909 num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size)
1910 {
1911 int len;
1912 unsigned int entrycount = 0;
1913 unsigned int next_offset = 0;
1914 FILE_DIRECTORY_INFO *entryptr;
1915
1916 if (bufstart == NULL)
1917 return 0;
1918
1919 entryptr = (FILE_DIRECTORY_INFO *)bufstart;
1920
1921 while (1) {
1922 entryptr = (FILE_DIRECTORY_INFO *)
1923 ((char *)entryptr + next_offset);
1924
1925 if ((char *)entryptr + size > end_of_buf) {
1926 cifs_dbg(VFS, "malformed search entry would overflow\n");
1927 break;
1928 }
1929
1930 len = le32_to_cpu(entryptr->FileNameLength);
1931 if ((char *)entryptr + len + size > end_of_buf) {
1932 cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
1933 end_of_buf);
1934 break;
1935 }
1936
1937 *lastentry = (char *)entryptr;
1938 entrycount++;
1939
1940 next_offset = le32_to_cpu(entryptr->NextEntryOffset);
1941 if (!next_offset)
1942 break;
1943 }
1944
1945 return entrycount;
1946 }
1947
1948 /*
1949 * Readdir/FindFirst
1950 */
1951 int
1952 SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
1953 u64 persistent_fid, u64 volatile_fid, int index,
1954 struct cifs_search_info *srch_inf)
1955 {
1956 struct smb2_query_directory_req *req;
1957 struct smb2_query_directory_rsp *rsp = NULL;
1958 struct kvec iov[2];
1959 int rc = 0;
1960 int len;
1961 int resp_buftype;
1962 unsigned char *bufptr;
1963 struct TCP_Server_Info *server;
1964 struct cifs_ses *ses = tcon->ses;
1965 __le16 asteriks = cpu_to_le16('*');
1966 char *end_of_smb;
1967 unsigned int output_size = CIFSMaxBufSize;
1968 size_t info_buf_size;
1969
1970 if (ses && (ses->server))
1971 server = ses->server;
1972 else
1973 return -EIO;
1974
1975 rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req);
1976 if (rc)
1977 return rc;
1978
1979 switch (srch_inf->info_level) {
1980 case SMB_FIND_FILE_DIRECTORY_INFO:
1981 req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
1982 info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
1983 break;
1984 case SMB_FIND_FILE_ID_FULL_DIR_INFO:
1985 req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
1986 info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
1987 break;
1988 default:
1989 cifs_dbg(VFS, "info level %u isn't supported\n",
1990 srch_inf->info_level);
1991 rc = -EINVAL;
1992 goto qdir_exit;
1993 }
1994
1995 req->FileIndex = cpu_to_le32(index);
1996 req->PersistentFileId = persistent_fid;
1997 req->VolatileFileId = volatile_fid;
1998
1999 len = 0x2;
2000 bufptr = req->Buffer;
2001 memcpy(bufptr, &asteriks, len);
2002
2003 req->FileNameOffset =
2004 cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4);
2005 req->FileNameLength = cpu_to_le16(len);
2006 /*
2007 * BB could be 30 bytes or so longer if we used SMB2 specific
2008 * buffer lengths, but this is safe and close enough.
2009 */
2010 output_size = min_t(unsigned int, output_size, server->maxBuf);
2011 output_size = min_t(unsigned int, output_size, 2 << 15);
2012 req->OutputBufferLength = cpu_to_le32(output_size);
2013
2014 iov[0].iov_base = (char *)req;
2015 /* 4 for RFC1001 length and 1 for Buffer */
2016 iov[0].iov_len = get_rfc1002_length(req) + 4 - 1;
2017
2018 iov[1].iov_base = (char *)(req->Buffer);
2019 iov[1].iov_len = len;
2020
2021 inc_rfc1001_len(req, len - 1 /* Buffer */);
2022
2023 rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0);
2024 rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base;
2025
2026 if (rc) {
2027 cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
2028 goto qdir_exit;
2029 }
2030
2031 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2032 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2033 info_buf_size);
2034 if (rc)
2035 goto qdir_exit;
2036
2037 srch_inf->unicode = true;
2038
2039 if (srch_inf->ntwrk_buf_start) {
2040 if (srch_inf->smallBuf)
2041 cifs_small_buf_release(srch_inf->ntwrk_buf_start);
2042 else
2043 cifs_buf_release(srch_inf->ntwrk_buf_start);
2044 }
2045 srch_inf->ntwrk_buf_start = (char *)rsp;
2046 srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ +
2047 (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset);
2048 /* 4 for rfc1002 length field */
2049 end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr;
2050 srch_inf->entries_in_buffer =
2051 num_entries(srch_inf->srch_entries_start, end_of_smb,
2052 &srch_inf->last_entry, info_buf_size);
2053 srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
2054 cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
2055 srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
2056 srch_inf->srch_entries_start, srch_inf->last_entry);
2057 if (resp_buftype == CIFS_LARGE_BUFFER)
2058 srch_inf->smallBuf = false;
2059 else if (resp_buftype == CIFS_SMALL_BUFFER)
2060 srch_inf->smallBuf = true;
2061 else
2062 cifs_dbg(VFS, "illegal search buffer type\n");
2063
2064 if (rsp->hdr.Status == STATUS_NO_MORE_FILES)
2065 srch_inf->endOfSearch = 1;
2066 else
2067 srch_inf->endOfSearch = 0;
2068
2069 return rc;
2070
2071 qdir_exit:
2072 free_rsp_buf(resp_buftype, rsp);
2073 return rc;
2074 }
2075
2076 static int
2077 send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2078 u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class,
2079 unsigned int num, void **data, unsigned int *size)
2080 {
2081 struct smb2_set_info_req *req;
2082 struct smb2_set_info_rsp *rsp = NULL;
2083 struct kvec *iov;
2084 int rc = 0;
2085 int resp_buftype;
2086 unsigned int i;
2087 struct TCP_Server_Info *server;
2088 struct cifs_ses *ses = tcon->ses;
2089
2090 if (ses && (ses->server))
2091 server = ses->server;
2092 else
2093 return -EIO;
2094
2095 if (!num)
2096 return -EINVAL;
2097
2098 iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL);
2099 if (!iov)
2100 return -ENOMEM;
2101
2102 rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req);
2103 if (rc) {
2104 kfree(iov);
2105 return rc;
2106 }
2107
2108 req->hdr.ProcessId = cpu_to_le32(pid);
2109
2110 req->InfoType = SMB2_O_INFO_FILE;
2111 req->FileInfoClass = info_class;
2112 req->PersistentFileId = persistent_fid;
2113 req->VolatileFileId = volatile_fid;
2114
2115 /* 4 for RFC1001 length and 1 for Buffer */
2116 req->BufferOffset =
2117 cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4);
2118 req->BufferLength = cpu_to_le32(*size);
2119
2120 inc_rfc1001_len(req, *size - 1 /* Buffer */);
2121
2122 memcpy(req->Buffer, *data, *size);
2123
2124 iov[0].iov_base = (char *)req;
2125 /* 4 for RFC1001 length */
2126 iov[0].iov_len = get_rfc1002_length(req) + 4;
2127
2128 for (i = 1; i < num; i++) {
2129 inc_rfc1001_len(req, size[i]);
2130 le32_add_cpu(&req->BufferLength, size[i]);
2131 iov[i].iov_base = (char *)data[i];
2132 iov[i].iov_len = size[i];
2133 }
2134
2135 rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0);
2136 rsp = (struct smb2_set_info_rsp *)iov[0].iov_base;
2137
2138 if (rc != 0) {
2139 cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
2140 goto out;
2141 }
2142 out:
2143 free_rsp_buf(resp_buftype, rsp);
2144 kfree(iov);
2145 return rc;
2146 }
2147
2148 int
2149 SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon,
2150 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2151 {
2152 struct smb2_file_rename_info info;
2153 void **data;
2154 unsigned int size[2];
2155 int rc;
2156 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2157
2158 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2159 if (!data)
2160 return -ENOMEM;
2161
2162 info.ReplaceIfExists = 1; /* 1 = replace existing target with new */
2163 /* 0 = fail if target already exists */
2164 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2165 info.FileNameLength = cpu_to_le32(len);
2166
2167 data[0] = &info;
2168 size[0] = sizeof(struct smb2_file_rename_info);
2169
2170 data[1] = target_file;
2171 size[1] = len + 2 /* null */;
2172
2173 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2174 current->tgid, FILE_RENAME_INFORMATION, 2, data,
2175 size);
2176 kfree(data);
2177 return rc;
2178 }
2179
2180 int
2181 SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon,
2182 u64 persistent_fid, u64 volatile_fid, __le16 *target_file)
2183 {
2184 struct smb2_file_link_info info;
2185 void **data;
2186 unsigned int size[2];
2187 int rc;
2188 int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX));
2189
2190 data = kmalloc(sizeof(void *) * 2, GFP_KERNEL);
2191 if (!data)
2192 return -ENOMEM;
2193
2194 info.ReplaceIfExists = 0; /* 1 = replace existing link with new */
2195 /* 0 = fail if link already exists */
2196 info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */
2197 info.FileNameLength = cpu_to_le32(len);
2198
2199 data[0] = &info;
2200 size[0] = sizeof(struct smb2_file_link_info);
2201
2202 data[1] = target_file;
2203 size[1] = len + 2 /* null */;
2204
2205 rc = send_set_info(xid, tcon, persistent_fid, volatile_fid,
2206 current->tgid, FILE_LINK_INFORMATION, 2, data, size);
2207 kfree(data);
2208 return rc;
2209 }
2210
2211 int
2212 SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
2213 u64 volatile_fid, u32 pid, __le64 *eof)
2214 {
2215 struct smb2_file_eof_info info;
2216 void *data;
2217 unsigned int size;
2218
2219 info.EndOfFile = *eof;
2220
2221 data = &info;
2222 size = sizeof(struct smb2_file_eof_info);
2223
2224 return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid,
2225 FILE_END_OF_FILE_INFORMATION, 1, &data, &size);
2226 }
2227
2228 int
2229 SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon,
2230 u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf)
2231 {
2232 unsigned int size;
2233 size = sizeof(FILE_BASIC_INFO);
2234 return send_set_info(xid, tcon, persistent_fid, volatile_fid,
2235 current->tgid, FILE_BASIC_INFORMATION, 1,
2236 (void **)&buf, &size);
2237 }
2238
2239 int
2240 SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
2241 const u64 persistent_fid, const u64 volatile_fid,
2242 __u8 oplock_level)
2243 {
2244 int rc;
2245 struct smb2_oplock_break *req = NULL;
2246
2247 cifs_dbg(FYI, "SMB2_oplock_break\n");
2248 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2249
2250 if (rc)
2251 return rc;
2252
2253 req->VolatileFid = volatile_fid;
2254 req->PersistentFid = persistent_fid;
2255 req->OplockLevel = oplock_level;
2256 req->hdr.CreditRequest = cpu_to_le16(1);
2257
2258 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2259 /* SMB2 buffer freed by function above */
2260
2261 if (rc) {
2262 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2263 cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
2264 }
2265
2266 return rc;
2267 }
2268
2269 static void
2270 copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
2271 struct kstatfs *kst)
2272 {
2273 kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
2274 le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
2275 kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
2276 kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits);
2277 kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
2278 return;
2279 }
2280
2281 static int
2282 build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level,
2283 int outbuf_len, u64 persistent_fid, u64 volatile_fid)
2284 {
2285 int rc;
2286 struct smb2_query_info_req *req;
2287
2288 cifs_dbg(FYI, "Query FSInfo level %d\n", level);
2289
2290 if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
2291 return -EIO;
2292
2293 rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req);
2294 if (rc)
2295 return rc;
2296
2297 req->InfoType = SMB2_O_INFO_FILESYSTEM;
2298 req->FileInfoClass = level;
2299 req->PersistentFileId = persistent_fid;
2300 req->VolatileFileId = volatile_fid;
2301 /* 4 for rfc1002 length field and 1 for pad */
2302 req->InputBufferOffset =
2303 cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4);
2304 req->OutputBufferLength = cpu_to_le32(
2305 outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4);
2306
2307 iov->iov_base = (char *)req;
2308 /* 4 for rfc1002 length field */
2309 iov->iov_len = get_rfc1002_length(req) + 4;
2310 return 0;
2311 }
2312
2313 int
2314 SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
2315 u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
2316 {
2317 struct smb2_query_info_rsp *rsp = NULL;
2318 struct kvec iov;
2319 int rc = 0;
2320 int resp_buftype;
2321 struct cifs_ses *ses = tcon->ses;
2322 struct smb2_fs_full_size_info *info = NULL;
2323
2324 rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION,
2325 sizeof(struct smb2_fs_full_size_info),
2326 persistent_fid, volatile_fid);
2327 if (rc)
2328 return rc;
2329
2330 rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0);
2331 if (rc) {
2332 cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
2333 goto qinf_exit;
2334 }
2335 rsp = (struct smb2_query_info_rsp *)iov.iov_base;
2336
2337 info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ +
2338 le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr);
2339 rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset),
2340 le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr,
2341 sizeof(struct smb2_fs_full_size_info));
2342 if (!rc)
2343 copy_fs_info_to_kstatfs(info, fsdata);
2344
2345 qinf_exit:
2346 free_rsp_buf(resp_buftype, iov.iov_base);
2347 return rc;
2348 }
2349
2350 int
2351 smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
2352 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2353 const __u32 num_lock, struct smb2_lock_element *buf)
2354 {
2355 int rc = 0;
2356 struct smb2_lock_req *req = NULL;
2357 struct kvec iov[2];
2358 int resp_buf_type;
2359 unsigned int count;
2360
2361 cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
2362
2363 rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req);
2364 if (rc)
2365 return rc;
2366
2367 req->hdr.ProcessId = cpu_to_le32(pid);
2368 req->LockCount = cpu_to_le16(num_lock);
2369
2370 req->PersistentFileId = persist_fid;
2371 req->VolatileFileId = volatile_fid;
2372
2373 count = num_lock * sizeof(struct smb2_lock_element);
2374 inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element));
2375
2376 iov[0].iov_base = (char *)req;
2377 /* 4 for rfc1002 length field and count for all locks */
2378 iov[0].iov_len = get_rfc1002_length(req) + 4 - count;
2379 iov[1].iov_base = (char *)buf;
2380 iov[1].iov_len = count;
2381
2382 cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
2383 rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
2384 if (rc) {
2385 cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
2386 cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
2387 }
2388
2389 return rc;
2390 }
2391
2392 int
2393 SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
2394 const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
2395 const __u64 length, const __u64 offset, const __u32 lock_flags,
2396 const bool wait)
2397 {
2398 struct smb2_lock_element lock;
2399
2400 lock.Offset = cpu_to_le64(offset);
2401 lock.Length = cpu_to_le64(length);
2402 lock.Flags = cpu_to_le32(lock_flags);
2403 if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
2404 lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
2405
2406 return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
2407 }
2408
2409 int
2410 SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
2411 __u8 *lease_key, const __le32 lease_state)
2412 {
2413 int rc;
2414 struct smb2_lease_ack *req = NULL;
2415
2416 cifs_dbg(FYI, "SMB2_lease_break\n");
2417 rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req);
2418
2419 if (rc)
2420 return rc;
2421
2422 req->hdr.CreditRequest = cpu_to_le16(1);
2423 req->StructureSize = cpu_to_le16(36);
2424 inc_rfc1001_len(req, 12);
2425
2426 memcpy(req->LeaseKey, lease_key, 16);
2427 req->LeaseState = lease_state;
2428
2429 rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP);
2430 /* SMB2 buffer freed by function above */
2431
2432 if (rc) {
2433 cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
2434 cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
2435 }
2436
2437 return rc;
2438 }