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