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