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