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