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