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