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