Commit | Line | Data |
---|---|---|
ec2e4523 PS |
1 | /* |
2 | * fs/cifs/smb2pdu.c | |
3 | * | |
2b80d049 | 4 | * Copyright (C) International Business Machines Corp., 2009, 2013 |
ec2e4523 PS |
5 | * Etersoft, 2012 |
6 | * Author(s): Steve French (sfrench@us.ibm.com) | |
7 | * Pavel Shilovsky (pshilovsky@samba.org) 2012 | |
8 | * | |
9 | * Contains the routines for constructing the SMB2 PDUs themselves | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU Lesser General Public License as published | |
13 | * by the Free Software Foundation; either version 2.1 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
19 | * the GNU Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public License | |
22 | * along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 | */ | |
25 | ||
26 | /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */ | |
27 | /* Note that there are handle based routines which must be */ | |
28 | /* treated slightly differently for reconnection purposes since we never */ | |
29 | /* want to reuse a stale file handle and only the caller knows the file info */ | |
30 | ||
31 | #include <linux/fs.h> | |
32 | #include <linux/kernel.h> | |
33 | #include <linux/vfs.h> | |
09a4707e | 34 | #include <linux/task_io_accounting_ops.h> |
ec2e4523 | 35 | #include <linux/uaccess.h> |
33319141 | 36 | #include <linux/pagemap.h> |
ec2e4523 PS |
37 | #include <linux/xattr.h> |
38 | #include "smb2pdu.h" | |
39 | #include "cifsglob.h" | |
40 | #include "cifsacl.h" | |
41 | #include "cifsproto.h" | |
42 | #include "smb2proto.h" | |
43 | #include "cifs_unicode.h" | |
44 | #include "cifs_debug.h" | |
45 | #include "ntlmssp.h" | |
46 | #include "smb2status.h" | |
09a4707e | 47 | #include "smb2glob.h" |
d324f08d | 48 | #include "cifspdu.h" |
ec2e4523 PS |
49 | |
50 | /* | |
51 | * The following table defines the expected "StructureSize" of SMB2 requests | |
52 | * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests. | |
53 | * | |
54 | * Note that commands are defined in smb2pdu.h in le16 but the array below is | |
55 | * indexed by command in host byte order. | |
56 | */ | |
57 | static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = { | |
58 | /* SMB2_NEGOTIATE */ 36, | |
59 | /* SMB2_SESSION_SETUP */ 25, | |
60 | /* SMB2_LOGOFF */ 4, | |
61 | /* SMB2_TREE_CONNECT */ 9, | |
62 | /* SMB2_TREE_DISCONNECT */ 4, | |
63 | /* SMB2_CREATE */ 57, | |
64 | /* SMB2_CLOSE */ 24, | |
65 | /* SMB2_FLUSH */ 24, | |
66 | /* SMB2_READ */ 49, | |
67 | /* SMB2_WRITE */ 49, | |
68 | /* SMB2_LOCK */ 48, | |
69 | /* SMB2_IOCTL */ 57, | |
70 | /* SMB2_CANCEL */ 4, | |
71 | /* SMB2_ECHO */ 4, | |
72 | /* SMB2_QUERY_DIRECTORY */ 33, | |
73 | /* SMB2_CHANGE_NOTIFY */ 32, | |
74 | /* SMB2_QUERY_INFO */ 41, | |
75 | /* SMB2_SET_INFO */ 33, | |
76 | /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */ | |
77 | }; | |
78 | ||
79 | ||
80 | static void | |
81 | smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ , | |
82 | const struct cifs_tcon *tcon) | |
83 | { | |
84 | struct smb2_pdu *pdu = (struct smb2_pdu *)hdr; | |
85 | char *temp = (char *)hdr; | |
86 | /* lookup word count ie StructureSize from table */ | |
87 | __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)]; | |
88 | ||
89 | /* | |
90 | * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of | |
91 | * largest operations (Create) | |
92 | */ | |
93 | memset(temp, 0, 256); | |
94 | ||
95 | /* Note this is only network field converted to big endian */ | |
96 | hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr) | |
97 | - 4 /* RFC 1001 length field itself not counted */); | |
98 | ||
99 | hdr->ProtocolId[0] = 0xFE; | |
100 | hdr->ProtocolId[1] = 'S'; | |
101 | hdr->ProtocolId[2] = 'M'; | |
102 | hdr->ProtocolId[3] = 'B'; | |
103 | hdr->StructureSize = cpu_to_le16(64); | |
104 | hdr->Command = smb2_cmd; | |
105 | hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */ | |
106 | hdr->ProcessId = cpu_to_le32((__u16)current->tgid); | |
107 | ||
108 | if (!tcon) | |
109 | goto out; | |
110 | ||
2b80d049 SF |
111 | /* BB FIXME when we do write > 64K add +1 for every 64K in req or rsp */ |
112 | /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */ | |
113 | /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */ | |
114 | if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU) | |
115 | hdr->CreditCharge = cpu_to_le16(1); | |
116 | /* else CreditCharge MBZ */ | |
117 | ||
ec2e4523 PS |
118 | hdr->TreeId = tcon->tid; |
119 | /* Uid is not converted */ | |
120 | if (tcon->ses) | |
121 | hdr->SessionId = tcon->ses->Suid; | |
122 | /* BB check following DFS flags BB */ | |
123 | /* BB do we have to add check for SHI1005_FLAGS_DFS_ROOT too? */ | |
faaf946a PS |
124 | if (tcon->share_flags & SHI1005_FLAGS_DFS) |
125 | hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; | |
ec2e4523 PS |
126 | /* BB how does SMB2 do case sensitive? */ |
127 | /* if (tcon->nocase) | |
128 | hdr->Flags |= SMBFLG_CASELESS; */ | |
38d77c50 | 129 | if (tcon->ses && tcon->ses->server && tcon->ses->server->sign) |
3c1bf7e4 | 130 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
ec2e4523 PS |
131 | out: |
132 | pdu->StructureSize2 = cpu_to_le16(parmsize); | |
133 | return; | |
134 | } | |
135 | ||
136 | static int | |
137 | smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon) | |
138 | { | |
139 | int rc = 0; | |
aa24d1e9 PS |
140 | struct nls_table *nls_codepage; |
141 | struct cifs_ses *ses; | |
142 | struct TCP_Server_Info *server; | |
143 | ||
144 | /* | |
145 | * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so | |
146 | * check for tcp and smb session status done differently | |
147 | * for those three - in the calling routine. | |
148 | */ | |
149 | if (tcon == NULL) | |
150 | return rc; | |
151 | ||
152 | if (smb2_command == SMB2_TREE_CONNECT) | |
153 | return rc; | |
154 | ||
155 | if (tcon->tidStatus == CifsExiting) { | |
156 | /* | |
157 | * only tree disconnect, open, and write, | |
158 | * (and ulogoff which does not have tcon) | |
159 | * are allowed as we start force umount. | |
160 | */ | |
161 | if ((smb2_command != SMB2_WRITE) && | |
162 | (smb2_command != SMB2_CREATE) && | |
163 | (smb2_command != SMB2_TREE_DISCONNECT)) { | |
f96637be JP |
164 | cifs_dbg(FYI, "can not send cmd %d while umounting\n", |
165 | smb2_command); | |
aa24d1e9 PS |
166 | return -ENODEV; |
167 | } | |
168 | } | |
169 | if ((!tcon->ses) || (tcon->ses->status == CifsExiting) || | |
170 | (!tcon->ses->server)) | |
171 | return -EIO; | |
172 | ||
173 | ses = tcon->ses; | |
174 | server = ses->server; | |
175 | ||
176 | /* | |
177 | * Give demultiplex thread up to 10 seconds to reconnect, should be | |
178 | * greater than cifs socket timeout which is 7 seconds | |
179 | */ | |
180 | while (server->tcpStatus == CifsNeedReconnect) { | |
181 | /* | |
182 | * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE | |
183 | * here since they are implicitly done when session drops. | |
184 | */ | |
185 | switch (smb2_command) { | |
186 | /* | |
187 | * BB Should we keep oplock break and add flush to exceptions? | |
188 | */ | |
189 | case SMB2_TREE_DISCONNECT: | |
190 | case SMB2_CANCEL: | |
191 | case SMB2_CLOSE: | |
192 | case SMB2_OPLOCK_BREAK: | |
193 | return -EAGAIN; | |
194 | } | |
195 | ||
196 | wait_event_interruptible_timeout(server->response_q, | |
197 | (server->tcpStatus != CifsNeedReconnect), 10 * HZ); | |
198 | ||
199 | /* are we still trying to reconnect? */ | |
200 | if (server->tcpStatus != CifsNeedReconnect) | |
201 | break; | |
202 | ||
203 | /* | |
204 | * on "soft" mounts we wait once. Hard mounts keep | |
205 | * retrying until process is killed or server comes | |
206 | * back on-line | |
207 | */ | |
208 | if (!tcon->retry) { | |
f96637be | 209 | cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n"); |
aa24d1e9 PS |
210 | return -EHOSTDOWN; |
211 | } | |
212 | } | |
213 | ||
214 | if (!tcon->ses->need_reconnect && !tcon->need_reconnect) | |
215 | return rc; | |
216 | ||
217 | nls_codepage = load_nls_default(); | |
218 | ||
219 | /* | |
220 | * need to prevent multiple threads trying to simultaneously reconnect | |
221 | * the same SMB session | |
222 | */ | |
223 | mutex_lock(&tcon->ses->session_mutex); | |
224 | rc = cifs_negotiate_protocol(0, tcon->ses); | |
225 | if (!rc && tcon->ses->need_reconnect) | |
226 | rc = cifs_setup_session(0, tcon->ses, nls_codepage); | |
227 | ||
228 | if (rc || !tcon->need_reconnect) { | |
229 | mutex_unlock(&tcon->ses->session_mutex); | |
230 | goto out; | |
231 | } | |
232 | ||
233 | cifs_mark_open_files_invalid(tcon); | |
234 | rc = SMB2_tcon(0, tcon->ses, tcon->treeName, tcon, nls_codepage); | |
235 | mutex_unlock(&tcon->ses->session_mutex); | |
f96637be | 236 | cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc); |
aa24d1e9 PS |
237 | if (rc) |
238 | goto out; | |
239 | atomic_inc(&tconInfoReconnectCount); | |
240 | /* | |
241 | * BB FIXME add code to check if wsize needs update due to negotiated | |
242 | * smb buffer size shrinking. | |
243 | */ | |
244 | out: | |
245 | /* | |
246 | * Check if handle based operation so we know whether we can continue | |
247 | * or not without returning to caller to reset file handle. | |
248 | */ | |
249 | /* | |
250 | * BB Is flush done by server on drop of tcp session? Should we special | |
251 | * case it and skip above? | |
252 | */ | |
253 | switch (smb2_command) { | |
254 | case SMB2_FLUSH: | |
255 | case SMB2_READ: | |
256 | case SMB2_WRITE: | |
257 | case SMB2_LOCK: | |
258 | case SMB2_IOCTL: | |
259 | case SMB2_QUERY_DIRECTORY: | |
260 | case SMB2_CHANGE_NOTIFY: | |
261 | case SMB2_QUERY_INFO: | |
262 | case SMB2_SET_INFO: | |
263 | return -EAGAIN; | |
264 | } | |
265 | unload_nls(nls_codepage); | |
ec2e4523 PS |
266 | return rc; |
267 | } | |
268 | ||
269 | /* | |
270 | * Allocate and return pointer to an SMB request hdr, and set basic | |
271 | * SMB information in the SMB header. If the return code is zero, this | |
272 | * function must have filled in request_buf pointer. | |
273 | */ | |
274 | static int | |
275 | small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon, | |
276 | void **request_buf) | |
277 | { | |
278 | int rc = 0; | |
279 | ||
280 | rc = smb2_reconnect(smb2_command, tcon); | |
281 | if (rc) | |
282 | return rc; | |
283 | ||
284 | /* BB eventually switch this to SMB2 specific small buf size */ | |
285 | *request_buf = cifs_small_buf_get(); | |
286 | if (*request_buf == NULL) { | |
287 | /* BB should we add a retry in here if not a writepage? */ | |
288 | return -ENOMEM; | |
289 | } | |
290 | ||
291 | smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon); | |
292 | ||
293 | if (tcon != NULL) { | |
294 | #ifdef CONFIG_CIFS_STATS2 | |
ec2e4523 PS |
295 | uint16_t com_code = le16_to_cpu(smb2_command); |
296 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]); | |
ec2e4523 PS |
297 | #endif |
298 | cifs_stats_inc(&tcon->num_smbs_sent); | |
299 | } | |
300 | ||
301 | return rc; | |
302 | } | |
303 | ||
304 | static void | |
305 | free_rsp_buf(int resp_buftype, void *rsp) | |
306 | { | |
307 | if (resp_buftype == CIFS_SMALL_BUFFER) | |
308 | cifs_small_buf_release(rsp); | |
309 | else if (resp_buftype == CIFS_LARGE_BUFFER) | |
310 | cifs_buf_release(rsp); | |
311 | } | |
312 | ||
ec2e4523 PS |
313 | |
314 | /* | |
315 | * | |
316 | * SMB2 Worker functions follow: | |
317 | * | |
318 | * The general structure of the worker functions is: | |
319 | * 1) Call smb2_init (assembles SMB2 header) | |
320 | * 2) Initialize SMB2 command specific fields in fixed length area of SMB | |
321 | * 3) Call smb_sendrcv2 (sends request on socket and waits for response) | |
322 | * 4) Decode SMB2 command specific fields in the fixed length area | |
323 | * 5) Decode variable length data area (if any for this SMB2 command type) | |
324 | * 6) Call free smb buffer | |
325 | * 7) return | |
326 | * | |
327 | */ | |
328 | ||
329 | int | |
330 | SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses) | |
331 | { | |
332 | struct smb2_negotiate_req *req; | |
333 | struct smb2_negotiate_rsp *rsp; | |
334 | struct kvec iov[1]; | |
335 | int rc = 0; | |
336 | int resp_buftype; | |
3534b850 | 337 | struct TCP_Server_Info *server = ses->server; |
ec2e4523 PS |
338 | int blob_offset, blob_length; |
339 | char *security_blob; | |
340 | int flags = CIFS_NEG_OP; | |
341 | ||
f96637be | 342 | cifs_dbg(FYI, "Negotiate protocol\n"); |
ec2e4523 | 343 | |
3534b850 JL |
344 | if (!server) { |
345 | WARN(1, "%s: server is NULL!\n", __func__); | |
346 | return -EIO; | |
ec2e4523 PS |
347 | } |
348 | ||
349 | rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req); | |
350 | if (rc) | |
351 | return rc; | |
352 | ||
ec2e4523 PS |
353 | req->hdr.SessionId = 0; |
354 | ||
e4aa25e7 | 355 | req->Dialects[0] = cpu_to_le16(ses->server->vals->protocol_id); |
ec2e4523 | 356 | |
e4aa25e7 SF |
357 | req->DialectCount = cpu_to_le16(1); /* One vers= at a time for now */ |
358 | inc_rfc1001_len(req, 2); | |
ec2e4523 PS |
359 | |
360 | /* only one of SMB2 signing flags may be set in SMB2 request */ | |
38d77c50 | 361 | if (ses->sign) |
9cd2e62c | 362 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED); |
38d77c50 | 363 | else if (global_secflags & CIFSSEC_MAY_SIGN) |
9cd2e62c | 364 | req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED); |
38d77c50 JL |
365 | else |
366 | req->SecurityMode = 0; | |
ec2e4523 | 367 | |
e4aa25e7 | 368 | req->Capabilities = cpu_to_le32(ses->server->vals->req_capabilities); |
ec2e4523 | 369 | |
b8c32dbb PS |
370 | memcpy(req->ClientGUID, cifs_client_guid, SMB2_CLIENT_GUID_SIZE); |
371 | ||
ec2e4523 PS |
372 | iov[0].iov_base = (char *)req; |
373 | /* 4 for rfc1002 length field */ | |
374 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
375 | ||
376 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags); | |
377 | ||
378 | rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base; | |
379 | /* | |
380 | * No tcon so can't do | |
381 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); | |
382 | */ | |
383 | if (rc != 0) | |
384 | goto neg_exit; | |
385 | ||
f96637be | 386 | cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode); |
ec2e4523 | 387 | |
e4aa25e7 SF |
388 | /* BB we may eventually want to match the negotiated vs. requested |
389 | dialect, even though we are only requesting one at a time */ | |
390 | if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) | |
f96637be | 391 | cifs_dbg(FYI, "negotiated smb2.0 dialect\n"); |
e4aa25e7 | 392 | else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) |
f96637be | 393 | cifs_dbg(FYI, "negotiated smb2.1 dialect\n"); |
e4aa25e7 | 394 | else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID)) |
f96637be | 395 | cifs_dbg(FYI, "negotiated smb3.0 dialect\n"); |
20b6d8b4 SF |
396 | else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID)) |
397 | cifs_dbg(FYI, "negotiated smb3.02 dialect\n"); | |
ec2e4523 | 398 | else { |
f96637be JP |
399 | cifs_dbg(VFS, "Illegal dialect returned by server %d\n", |
400 | le16_to_cpu(rsp->DialectRevision)); | |
ec2e4523 PS |
401 | rc = -EIO; |
402 | goto neg_exit; | |
403 | } | |
404 | server->dialect = le16_to_cpu(rsp->DialectRevision); | |
405 | ||
e598d1d8 JL |
406 | /* SMB2 only has an extended negflavor */ |
407 | server->negflavor = CIFS_NEGFLAVOR_EXTENDED; | |
ec2e4523 PS |
408 | server->maxBuf = le32_to_cpu(rsp->MaxTransactSize); |
409 | server->max_read = le32_to_cpu(rsp->MaxReadSize); | |
410 | server->max_write = le32_to_cpu(rsp->MaxWriteSize); | |
411 | /* BB Do we need to validate the SecurityMode? */ | |
412 | server->sec_mode = le16_to_cpu(rsp->SecurityMode); | |
413 | server->capabilities = le32_to_cpu(rsp->Capabilities); | |
29e20f9c PS |
414 | /* Internal types */ |
415 | server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES; | |
ec2e4523 PS |
416 | |
417 | security_blob = smb2_get_data_area_len(&blob_offset, &blob_length, | |
418 | &rsp->hdr); | |
419 | if (blob_length == 0) { | |
f96637be | 420 | cifs_dbg(VFS, "missing security blob on negprot\n"); |
ec2e4523 PS |
421 | rc = -EIO; |
422 | goto neg_exit; | |
423 | } | |
3c1bf7e4 | 424 | |
38d77c50 | 425 | rc = cifs_enable_signing(server, ses->sign); |
ec2e4523 | 426 | #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */ |
9ddec561 JL |
427 | if (rc) |
428 | goto neg_exit; | |
429 | ||
ec2e4523 PS |
430 | rc = decode_neg_token_init(security_blob, blob_length, |
431 | &server->sec_type); | |
432 | if (rc == 1) | |
433 | rc = 0; | |
434 | else if (rc == 0) { | |
435 | rc = -EIO; | |
436 | goto neg_exit; | |
437 | } | |
438 | #endif | |
439 | ||
440 | neg_exit: | |
441 | free_rsp_buf(resp_buftype, rsp); | |
442 | return rc; | |
443 | } | |
5478f9ba PS |
444 | |
445 | int | |
446 | SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses, | |
447 | const struct nls_table *nls_cp) | |
448 | { | |
449 | struct smb2_sess_setup_req *req; | |
450 | struct smb2_sess_setup_rsp *rsp = NULL; | |
451 | struct kvec iov[2]; | |
452 | int rc = 0; | |
453 | int resp_buftype; | |
454 | __le32 phase = NtLmNegotiate; /* NTLMSSP, if needed, is multistage */ | |
3534b850 | 455 | struct TCP_Server_Info *server = ses->server; |
5478f9ba PS |
456 | u16 blob_length = 0; |
457 | char *security_blob; | |
458 | char *ntlmssp_blob = NULL; | |
459 | bool use_spnego = false; /* else use raw ntlmssp */ | |
460 | ||
f96637be | 461 | cifs_dbg(FYI, "Session Setup\n"); |
5478f9ba | 462 | |
3534b850 JL |
463 | if (!server) { |
464 | WARN(1, "%s: server is NULL!\n", __func__); | |
465 | return -EIO; | |
5478f9ba PS |
466 | } |
467 | ||
468 | /* | |
469 | * If memory allocation is successful, caller of this function | |
470 | * frees it. | |
471 | */ | |
472 | ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL); | |
473 | if (!ses->ntlmssp) | |
474 | return -ENOMEM; | |
475 | ||
3f618223 JL |
476 | /* FIXME: allow for other auth types besides NTLMSSP (e.g. krb5) */ |
477 | ses->sectype = RawNTLMSSP; | |
5478f9ba PS |
478 | |
479 | ssetup_ntlmssp_authenticate: | |
480 | if (phase == NtLmChallenge) | |
481 | phase = NtLmAuthenticate; /* if ntlmssp, now final phase */ | |
482 | ||
483 | rc = small_smb2_init(SMB2_SESSION_SETUP, NULL, (void **) &req); | |
484 | if (rc) | |
485 | return rc; | |
486 | ||
5478f9ba PS |
487 | req->hdr.SessionId = 0; /* First session, not a reauthenticate */ |
488 | req->VcNumber = 0; /* MBZ */ | |
489 | /* to enable echos and oplocks */ | |
490 | req->hdr.CreditRequest = cpu_to_le16(3); | |
491 | ||
492 | /* only one of SMB2 signing flags may be set in SMB2 request */ | |
38d77c50 JL |
493 | if (server->sign) |
494 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED; | |
495 | else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */ | |
496 | req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED; | |
497 | else | |
498 | req->SecurityMode = 0; | |
499 | ||
5478f9ba PS |
500 | req->Capabilities = 0; |
501 | req->Channel = 0; /* MBZ */ | |
502 | ||
503 | iov[0].iov_base = (char *)req; | |
504 | /* 4 for rfc1002 length field and 1 for pad */ | |
505 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
506 | if (phase == NtLmNegotiate) { | |
507 | ntlmssp_blob = kmalloc(sizeof(struct _NEGOTIATE_MESSAGE), | |
508 | GFP_KERNEL); | |
509 | if (ntlmssp_blob == NULL) { | |
510 | rc = -ENOMEM; | |
511 | goto ssetup_exit; | |
512 | } | |
513 | build_ntlmssp_negotiate_blob(ntlmssp_blob, ses); | |
514 | if (use_spnego) { | |
515 | /* blob_length = build_spnego_ntlmssp_blob( | |
516 | &security_blob, | |
517 | sizeof(struct _NEGOTIATE_MESSAGE), | |
518 | ntlmssp_blob); */ | |
519 | /* BB eventually need to add this */ | |
f96637be | 520 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); |
5478f9ba PS |
521 | rc = -EOPNOTSUPP; |
522 | kfree(ntlmssp_blob); | |
523 | goto ssetup_exit; | |
524 | } else { | |
525 | blob_length = sizeof(struct _NEGOTIATE_MESSAGE); | |
526 | /* with raw NTLMSSP we don't encapsulate in SPNEGO */ | |
527 | security_blob = ntlmssp_blob; | |
528 | } | |
529 | } else if (phase == NtLmAuthenticate) { | |
530 | req->hdr.SessionId = ses->Suid; | |
531 | ntlmssp_blob = kzalloc(sizeof(struct _NEGOTIATE_MESSAGE) + 500, | |
532 | GFP_KERNEL); | |
533 | if (ntlmssp_blob == NULL) { | |
5478f9ba PS |
534 | rc = -ENOMEM; |
535 | goto ssetup_exit; | |
536 | } | |
537 | rc = build_ntlmssp_auth_blob(ntlmssp_blob, &blob_length, ses, | |
538 | nls_cp); | |
539 | if (rc) { | |
f96637be JP |
540 | cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", |
541 | rc); | |
5478f9ba PS |
542 | goto ssetup_exit; /* BB double check error handling */ |
543 | } | |
544 | if (use_spnego) { | |
545 | /* blob_length = build_spnego_ntlmssp_blob( | |
546 | &security_blob, | |
547 | blob_length, | |
548 | ntlmssp_blob); */ | |
f96637be | 549 | cifs_dbg(VFS, "spnego not supported for SMB2 yet\n"); |
5478f9ba PS |
550 | rc = -EOPNOTSUPP; |
551 | kfree(ntlmssp_blob); | |
552 | goto ssetup_exit; | |
553 | } else { | |
554 | security_blob = ntlmssp_blob; | |
555 | } | |
556 | } else { | |
f96637be | 557 | cifs_dbg(VFS, "illegal ntlmssp phase\n"); |
5478f9ba PS |
558 | rc = -EIO; |
559 | goto ssetup_exit; | |
560 | } | |
561 | ||
562 | /* Testing shows that buffer offset must be at location of Buffer[0] */ | |
563 | req->SecurityBufferOffset = | |
564 | cpu_to_le16(sizeof(struct smb2_sess_setup_req) - | |
565 | 1 /* pad */ - 4 /* rfc1001 len */); | |
566 | req->SecurityBufferLength = cpu_to_le16(blob_length); | |
567 | iov[1].iov_base = security_blob; | |
568 | iov[1].iov_len = blob_length; | |
569 | ||
570 | inc_rfc1001_len(req, blob_length - 1 /* pad */); | |
571 | ||
572 | /* BB add code to build os and lm fields */ | |
573 | ||
6d8b59d7 SF |
574 | rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, |
575 | CIFS_LOG_ERROR | CIFS_NEG_OP); | |
5478f9ba PS |
576 | |
577 | kfree(security_blob); | |
578 | rsp = (struct smb2_sess_setup_rsp *)iov[0].iov_base; | |
4ca3a99c PS |
579 | if (resp_buftype != CIFS_NO_BUFFER && |
580 | rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED) { | |
5478f9ba | 581 | if (phase != NtLmNegotiate) { |
f96637be | 582 | cifs_dbg(VFS, "Unexpected more processing error\n"); |
5478f9ba PS |
583 | goto ssetup_exit; |
584 | } | |
585 | if (offsetof(struct smb2_sess_setup_rsp, Buffer) - 4 != | |
4ca3a99c | 586 | le16_to_cpu(rsp->SecurityBufferOffset)) { |
f96637be JP |
587 | cifs_dbg(VFS, "Invalid security buffer offset %d\n", |
588 | le16_to_cpu(rsp->SecurityBufferOffset)); | |
5478f9ba PS |
589 | rc = -EIO; |
590 | goto ssetup_exit; | |
591 | } | |
592 | ||
593 | /* NTLMSSP Negotiate sent now processing challenge (response) */ | |
594 | phase = NtLmChallenge; /* process ntlmssp challenge */ | |
595 | rc = 0; /* MORE_PROCESSING is not an error here but expected */ | |
596 | ses->Suid = rsp->hdr.SessionId; | |
597 | rc = decode_ntlmssp_challenge(rsp->Buffer, | |
598 | le16_to_cpu(rsp->SecurityBufferLength), ses); | |
599 | } | |
600 | ||
601 | /* | |
602 | * BB eventually add code for SPNEGO decoding of NtlmChallenge blob, | |
603 | * but at least the raw NTLMSSP case works. | |
604 | */ | |
605 | /* | |
606 | * No tcon so can't do | |
607 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); | |
608 | */ | |
609 | if (rc != 0) | |
610 | goto ssetup_exit; | |
611 | ||
5478f9ba PS |
612 | ses->session_flags = le16_to_cpu(rsp->SessionFlags); |
613 | ssetup_exit: | |
614 | free_rsp_buf(resp_buftype, rsp); | |
615 | ||
616 | /* if ntlmssp, and negotiate succeeded, proceed to authenticate phase */ | |
617 | if ((phase == NtLmChallenge) && (rc == 0)) | |
618 | goto ssetup_ntlmssp_authenticate; | |
619 | return rc; | |
620 | } | |
621 | ||
622 | int | |
623 | SMB2_logoff(const unsigned int xid, struct cifs_ses *ses) | |
624 | { | |
625 | struct smb2_logoff_req *req; /* response is also trivial struct */ | |
626 | int rc = 0; | |
627 | struct TCP_Server_Info *server; | |
628 | ||
f96637be | 629 | cifs_dbg(FYI, "disconnect session %p\n", ses); |
5478f9ba PS |
630 | |
631 | if (ses && (ses->server)) | |
632 | server = ses->server; | |
633 | else | |
634 | return -EIO; | |
635 | ||
636 | rc = small_smb2_init(SMB2_LOGOFF, NULL, (void **) &req); | |
637 | if (rc) | |
638 | return rc; | |
639 | ||
640 | /* since no tcon, smb2_init can not do this, so do here */ | |
641 | req->hdr.SessionId = ses->Suid; | |
38d77c50 | 642 | if (server->sign) |
3c1bf7e4 | 643 | req->hdr.Flags |= SMB2_FLAGS_SIGNED; |
5478f9ba PS |
644 | |
645 | rc = SendReceiveNoRsp(xid, ses, (char *) &req->hdr, 0); | |
646 | /* | |
647 | * No tcon so can't do | |
648 | * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]); | |
649 | */ | |
650 | return rc; | |
651 | } | |
faaf946a PS |
652 | |
653 | static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code) | |
654 | { | |
d60622eb | 655 | cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]); |
faaf946a PS |
656 | } |
657 | ||
658 | #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */) | |
659 | ||
660 | int | |
661 | SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree, | |
662 | struct cifs_tcon *tcon, const struct nls_table *cp) | |
663 | { | |
664 | struct smb2_tree_connect_req *req; | |
665 | struct smb2_tree_connect_rsp *rsp = NULL; | |
666 | struct kvec iov[2]; | |
667 | int rc = 0; | |
668 | int resp_buftype; | |
669 | int unc_path_len; | |
670 | struct TCP_Server_Info *server; | |
671 | __le16 *unc_path = NULL; | |
672 | ||
f96637be | 673 | cifs_dbg(FYI, "TCON\n"); |
faaf946a PS |
674 | |
675 | if ((ses->server) && tree) | |
676 | server = ses->server; | |
677 | else | |
678 | return -EIO; | |
679 | ||
680 | if (tcon && tcon->bad_network_name) | |
681 | return -ENOENT; | |
682 | ||
683 | unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL); | |
684 | if (unc_path == NULL) | |
685 | return -ENOMEM; | |
686 | ||
687 | unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1; | |
688 | unc_path_len *= 2; | |
689 | if (unc_path_len < 2) { | |
690 | kfree(unc_path); | |
691 | return -EINVAL; | |
692 | } | |
693 | ||
694 | rc = small_smb2_init(SMB2_TREE_CONNECT, tcon, (void **) &req); | |
695 | if (rc) { | |
696 | kfree(unc_path); | |
697 | return rc; | |
698 | } | |
699 | ||
700 | if (tcon == NULL) { | |
701 | /* since no tcon, smb2_init can not do this, so do here */ | |
702 | req->hdr.SessionId = ses->Suid; | |
703 | /* if (ses->server->sec_mode & SECMODE_SIGN_REQUIRED) | |
704 | req->hdr.Flags |= SMB2_FLAGS_SIGNED; */ | |
705 | } | |
706 | ||
707 | iov[0].iov_base = (char *)req; | |
708 | /* 4 for rfc1002 length field and 1 for pad */ | |
709 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
710 | ||
711 | /* Testing shows that buffer offset must be at location of Buffer[0] */ | |
712 | req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req) | |
713 | - 1 /* pad */ - 4 /* do not count rfc1001 len field */); | |
714 | req->PathLength = cpu_to_le16(unc_path_len - 2); | |
715 | iov[1].iov_base = unc_path; | |
716 | iov[1].iov_len = unc_path_len; | |
717 | ||
718 | inc_rfc1001_len(req, unc_path_len - 1 /* pad */); | |
719 | ||
720 | rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0); | |
721 | rsp = (struct smb2_tree_connect_rsp *)iov[0].iov_base; | |
722 | ||
723 | if (rc != 0) { | |
724 | if (tcon) { | |
725 | cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE); | |
726 | tcon->need_reconnect = true; | |
727 | } | |
728 | goto tcon_error_exit; | |
729 | } | |
730 | ||
faaf946a PS |
731 | if (tcon == NULL) { |
732 | ses->ipc_tid = rsp->hdr.TreeId; | |
733 | goto tcon_exit; | |
734 | } | |
735 | ||
736 | if (rsp->ShareType & SMB2_SHARE_TYPE_DISK) | |
f96637be | 737 | cifs_dbg(FYI, "connection to disk share\n"); |
faaf946a PS |
738 | else if (rsp->ShareType & SMB2_SHARE_TYPE_PIPE) { |
739 | tcon->ipc = true; | |
f96637be | 740 | cifs_dbg(FYI, "connection to pipe share\n"); |
faaf946a PS |
741 | } else if (rsp->ShareType & SMB2_SHARE_TYPE_PRINT) { |
742 | tcon->print = true; | |
f96637be | 743 | cifs_dbg(FYI, "connection to printer\n"); |
faaf946a | 744 | } else { |
f96637be | 745 | cifs_dbg(VFS, "unknown share type %d\n", rsp->ShareType); |
faaf946a PS |
746 | rc = -EOPNOTSUPP; |
747 | goto tcon_error_exit; | |
748 | } | |
749 | ||
750 | tcon->share_flags = le32_to_cpu(rsp->ShareFlags); | |
769ee6a4 | 751 | tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */ |
faaf946a PS |
752 | tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess); |
753 | tcon->tidStatus = CifsGood; | |
754 | tcon->need_reconnect = false; | |
755 | tcon->tid = rsp->hdr.TreeId; | |
46b51d08 | 756 | strlcpy(tcon->treeName, tree, sizeof(tcon->treeName)); |
faaf946a PS |
757 | |
758 | if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) && | |
759 | ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0)) | |
f96637be | 760 | cifs_dbg(VFS, "DFS capability contradicts DFS flag\n"); |
faaf946a PS |
761 | |
762 | tcon_exit: | |
763 | free_rsp_buf(resp_buftype, rsp); | |
764 | kfree(unc_path); | |
765 | return rc; | |
766 | ||
767 | tcon_error_exit: | |
768 | if (rsp->hdr.Status == STATUS_BAD_NETWORK_NAME) { | |
f96637be | 769 | cifs_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree); |
faaf946a PS |
770 | tcon->bad_network_name = true; |
771 | } | |
772 | goto tcon_exit; | |
773 | } | |
774 | ||
775 | int | |
776 | SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon) | |
777 | { | |
778 | struct smb2_tree_disconnect_req *req; /* response is trivial */ | |
779 | int rc = 0; | |
780 | struct TCP_Server_Info *server; | |
781 | struct cifs_ses *ses = tcon->ses; | |
782 | ||
f96637be | 783 | cifs_dbg(FYI, "Tree Disconnect\n"); |
faaf946a PS |
784 | |
785 | if (ses && (ses->server)) | |
786 | server = ses->server; | |
787 | else | |
788 | return -EIO; | |
789 | ||
790 | if ((tcon->need_reconnect) || (tcon->ses->need_reconnect)) | |
791 | return 0; | |
792 | ||
793 | rc = small_smb2_init(SMB2_TREE_DISCONNECT, tcon, (void **) &req); | |
794 | if (rc) | |
795 | return rc; | |
796 | ||
797 | rc = SendReceiveNoRsp(xid, ses, (char *)&req->hdr, 0); | |
798 | if (rc) | |
799 | cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE); | |
800 | ||
801 | return rc; | |
802 | } | |
2503a0db | 803 | |
b8c32dbb PS |
804 | static struct create_lease * |
805 | create_lease_buf(u8 *lease_key, u8 oplock) | |
806 | { | |
807 | struct create_lease *buf; | |
808 | ||
d455b72b | 809 | buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL); |
b8c32dbb PS |
810 | if (!buf) |
811 | return NULL; | |
812 | ||
b8c32dbb PS |
813 | buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key)); |
814 | buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8))); | |
815 | if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) | |
816 | buf->lcontext.LeaseState = SMB2_LEASE_WRITE_CACHING | | |
817 | SMB2_LEASE_READ_CACHING; | |
818 | else if (oplock == SMB2_OPLOCK_LEVEL_II) | |
819 | buf->lcontext.LeaseState = SMB2_LEASE_READ_CACHING; | |
820 | else if (oplock == SMB2_OPLOCK_LEVEL_BATCH) | |
821 | buf->lcontext.LeaseState = SMB2_LEASE_HANDLE_CACHING | | |
822 | SMB2_LEASE_READ_CACHING | | |
823 | SMB2_LEASE_WRITE_CACHING; | |
824 | ||
825 | buf->ccontext.DataOffset = cpu_to_le16(offsetof | |
826 | (struct create_lease, lcontext)); | |
827 | buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context)); | |
828 | buf->ccontext.NameOffset = cpu_to_le16(offsetof | |
829 | (struct create_lease, Name)); | |
830 | buf->ccontext.NameLength = cpu_to_le16(4); | |
831 | buf->Name[0] = 'R'; | |
832 | buf->Name[1] = 'q'; | |
833 | buf->Name[2] = 'L'; | |
834 | buf->Name[3] = 's'; | |
835 | return buf; | |
836 | } | |
837 | ||
838 | static __u8 | |
839 | parse_lease_state(struct smb2_create_rsp *rsp) | |
840 | { | |
841 | char *data_offset; | |
842 | struct create_lease *lc; | |
b8c32dbb PS |
843 | bool found = false; |
844 | ||
845 | data_offset = (char *)rsp; | |
846 | data_offset += 4 + le32_to_cpu(rsp->CreateContextsOffset); | |
847 | lc = (struct create_lease *)data_offset; | |
848 | do { | |
849 | char *name = le16_to_cpu(lc->ccontext.NameOffset) + (char *)lc; | |
850 | if (le16_to_cpu(lc->ccontext.NameLength) != 4 || | |
851 | strncmp(name, "RqLs", 4)) { | |
852 | lc = (struct create_lease *)((char *)lc | |
853 | + le32_to_cpu(lc->ccontext.Next)); | |
854 | continue; | |
855 | } | |
856 | if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS) | |
857 | return SMB2_OPLOCK_LEVEL_NOCHANGE; | |
858 | found = true; | |
859 | break; | |
860 | } while (le32_to_cpu(lc->ccontext.Next) != 0); | |
861 | ||
862 | if (!found) | |
0822f514 | 863 | return 0; |
b8c32dbb | 864 | |
0822f514 | 865 | return smb2_map_lease_to_oplock(lc->lcontext.LeaseState); |
b8c32dbb PS |
866 | } |
867 | ||
2503a0db PS |
868 | int |
869 | SMB2_open(const unsigned int xid, struct cifs_tcon *tcon, __le16 *path, | |
870 | u64 *persistent_fid, u64 *volatile_fid, __u32 desired_access, | |
f0df737e | 871 | __u32 create_disposition, __u32 file_attributes, __u32 create_options, |
2e44b288 | 872 | __u8 *oplock, struct smb2_file_all_info *buf) |
2503a0db PS |
873 | { |
874 | struct smb2_create_req *req; | |
875 | struct smb2_create_rsp *rsp; | |
876 | struct TCP_Server_Info *server; | |
877 | struct cifs_ses *ses = tcon->ses; | |
b8c32dbb | 878 | struct kvec iov[3]; |
2503a0db PS |
879 | int resp_buftype; |
880 | int uni_path_len; | |
b8c32dbb PS |
881 | __le16 *copy_path = NULL; |
882 | int copy_size; | |
2503a0db PS |
883 | int rc = 0; |
884 | int num_iovecs = 2; | |
885 | ||
f96637be | 886 | cifs_dbg(FYI, "create/open\n"); |
2503a0db PS |
887 | |
888 | if (ses && (ses->server)) | |
889 | server = ses->server; | |
890 | else | |
891 | return -EIO; | |
892 | ||
893 | rc = small_smb2_init(SMB2_CREATE, tcon, (void **) &req); | |
894 | if (rc) | |
895 | return rc; | |
896 | ||
2503a0db PS |
897 | req->ImpersonationLevel = IL_IMPERSONATION; |
898 | req->DesiredAccess = cpu_to_le32(desired_access); | |
899 | /* File attributes ignored on open (used in create though) */ | |
900 | req->FileAttributes = cpu_to_le32(file_attributes); | |
901 | req->ShareAccess = FILE_SHARE_ALL_LE; | |
902 | req->CreateDisposition = cpu_to_le32(create_disposition); | |
903 | req->CreateOptions = cpu_to_le32(create_options); | |
904 | uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2; | |
905 | req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req) | |
b8c32dbb | 906 | - 8 /* pad */ - 4 /* do not count rfc1001 len field */); |
2503a0db PS |
907 | |
908 | iov[0].iov_base = (char *)req; | |
909 | /* 4 for rfc1002 length field */ | |
910 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
911 | ||
912 | /* MUST set path len (NameLength) to 0 opening root of share */ | |
913 | if (uni_path_len >= 4) { | |
914 | req->NameLength = cpu_to_le16(uni_path_len - 2); | |
915 | /* -1 since last byte is buf[0] which is sent below (path) */ | |
916 | iov[0].iov_len--; | |
b8c32dbb PS |
917 | if (uni_path_len % 8 != 0) { |
918 | copy_size = uni_path_len / 8 * 8; | |
919 | if (copy_size < uni_path_len) | |
920 | copy_size += 8; | |
921 | ||
922 | copy_path = kzalloc(copy_size, GFP_KERNEL); | |
923 | if (!copy_path) | |
924 | return -ENOMEM; | |
925 | memcpy((char *)copy_path, (const char *)path, | |
926 | uni_path_len); | |
927 | uni_path_len = copy_size; | |
928 | path = copy_path; | |
929 | } | |
930 | ||
2503a0db PS |
931 | iov[1].iov_len = uni_path_len; |
932 | iov[1].iov_base = path; | |
933 | /* | |
934 | * -1 since last byte is buf[0] which was counted in | |
935 | * smb2_buf_len. | |
936 | */ | |
937 | inc_rfc1001_len(req, uni_path_len - 1); | |
938 | } else { | |
b8c32dbb PS |
939 | iov[0].iov_len += 7; |
940 | req->hdr.smb2_buf_length = cpu_to_be32(be32_to_cpu( | |
941 | req->hdr.smb2_buf_length) + 8 - 1); | |
2503a0db PS |
942 | num_iovecs = 1; |
943 | req->NameLength = 0; | |
944 | } | |
945 | ||
b8c32dbb PS |
946 | if (!server->oplocks) |
947 | *oplock = SMB2_OPLOCK_LEVEL_NONE; | |
948 | ||
949 | if (!(tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING) || | |
950 | *oplock == SMB2_OPLOCK_LEVEL_NONE) | |
951 | req->RequestedOplockLevel = *oplock; | |
952 | else { | |
953 | iov[num_iovecs].iov_base = create_lease_buf(oplock+1, *oplock); | |
954 | if (iov[num_iovecs].iov_base == NULL) { | |
955 | cifs_small_buf_release(req); | |
956 | kfree(copy_path); | |
957 | return -ENOMEM; | |
958 | } | |
959 | iov[num_iovecs].iov_len = sizeof(struct create_lease); | |
960 | req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE; | |
961 | req->CreateContextsOffset = cpu_to_le32( | |
962 | sizeof(struct smb2_create_req) - 4 - 8 + | |
963 | iov[num_iovecs-1].iov_len); | |
964 | req->CreateContextsLength = cpu_to_le32( | |
965 | sizeof(struct create_lease)); | |
966 | inc_rfc1001_len(&req->hdr, sizeof(struct create_lease)); | |
967 | num_iovecs++; | |
968 | } | |
969 | ||
2503a0db PS |
970 | rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0); |
971 | rsp = (struct smb2_create_rsp *)iov[0].iov_base; | |
972 | ||
973 | if (rc != 0) { | |
974 | cifs_stats_fail_inc(tcon, SMB2_CREATE_HE); | |
975 | goto creat_exit; | |
976 | } | |
977 | ||
2503a0db PS |
978 | *persistent_fid = rsp->PersistentFileId; |
979 | *volatile_fid = rsp->VolatileFileId; | |
f0df737e PS |
980 | |
981 | if (buf) { | |
982 | memcpy(buf, &rsp->CreationTime, 32); | |
983 | buf->AllocationSize = rsp->AllocationSize; | |
984 | buf->EndOfFile = rsp->EndofFile; | |
985 | buf->Attributes = rsp->FileAttributes; | |
986 | buf->NumberOfLinks = cpu_to_le32(1); | |
987 | buf->DeletePending = 0; | |
988 | } | |
2e44b288 | 989 | |
b8c32dbb PS |
990 | if (rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) |
991 | *oplock = parse_lease_state(rsp); | |
992 | else | |
993 | *oplock = rsp->OplockLevel; | |
2503a0db | 994 | creat_exit: |
b8c32dbb | 995 | kfree(copy_path); |
2503a0db PS |
996 | free_rsp_buf(resp_buftype, rsp); |
997 | return rc; | |
998 | } | |
999 | ||
4a72dafa SF |
1000 | /* |
1001 | * SMB2 IOCTL is used for both IOCTLs and FSCTLs | |
1002 | */ | |
1003 | int | |
1004 | SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, | |
1005 | u64 volatile_fid, u32 opcode, bool is_fsctl, char *in_data, | |
1006 | u32 indatalen, char **out_data, u32 *plen /* returned data len */) | |
1007 | { | |
1008 | struct smb2_ioctl_req *req; | |
1009 | struct smb2_ioctl_rsp *rsp; | |
1010 | struct TCP_Server_Info *server; | |
1011 | struct cifs_ses *ses = tcon->ses; | |
1012 | struct kvec iov[2]; | |
1013 | int resp_buftype; | |
1014 | int num_iovecs; | |
1015 | int rc = 0; | |
1016 | ||
1017 | cifs_dbg(FYI, "SMB2 IOCTL\n"); | |
1018 | ||
1019 | /* zero out returned data len, in case of error */ | |
1020 | if (plen) | |
1021 | *plen = 0; | |
1022 | ||
1023 | if (ses && (ses->server)) | |
1024 | server = ses->server; | |
1025 | else | |
1026 | return -EIO; | |
1027 | ||
1028 | rc = small_smb2_init(SMB2_IOCTL, tcon, (void **) &req); | |
1029 | if (rc) | |
1030 | return rc; | |
1031 | ||
1032 | req->CtlCode = cpu_to_le32(opcode); | |
1033 | req->PersistentFileId = persistent_fid; | |
1034 | req->VolatileFileId = volatile_fid; | |
1035 | ||
1036 | if (indatalen) { | |
1037 | req->InputCount = cpu_to_le32(indatalen); | |
1038 | /* do not set InputOffset if no input data */ | |
1039 | req->InputOffset = | |
1040 | cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer) - 4); | |
1041 | iov[1].iov_base = in_data; | |
1042 | iov[1].iov_len = indatalen; | |
1043 | num_iovecs = 2; | |
1044 | } else | |
1045 | num_iovecs = 1; | |
1046 | ||
1047 | req->OutputOffset = 0; | |
1048 | req->OutputCount = 0; /* MBZ */ | |
1049 | ||
1050 | /* | |
1051 | * Could increase MaxOutputResponse, but that would require more | |
1052 | * than one credit. Windows typically sets this smaller, but for some | |
1053 | * ioctls it may be useful to allow server to send more. No point | |
1054 | * limiting what the server can send as long as fits in one credit | |
1055 | */ | |
1056 | req->MaxOutputResponse = cpu_to_le32(0xFF00); /* < 64K uses 1 credit */ | |
1057 | ||
1058 | if (is_fsctl) | |
1059 | req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL); | |
1060 | else | |
1061 | req->Flags = 0; | |
1062 | ||
1063 | iov[0].iov_base = (char *)req; | |
1064 | /* 4 for rfc1002 length field */ | |
1065 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1066 | ||
1067 | if (indatalen) | |
1068 | inc_rfc1001_len(req, indatalen); | |
1069 | ||
1070 | rc = SendReceive2(xid, ses, iov, num_iovecs, &resp_buftype, 0); | |
1071 | rsp = (struct smb2_ioctl_rsp *)iov[0].iov_base; | |
1072 | ||
1073 | if (rc != 0) { | |
1074 | if (tcon) | |
1075 | cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE); | |
1076 | goto ioctl_exit; | |
1077 | } | |
1078 | ||
1079 | /* check if caller wants to look at return data or just return rc */ | |
1080 | if ((plen == NULL) || (out_data == NULL)) | |
1081 | goto ioctl_exit; | |
1082 | ||
1083 | *plen = le32_to_cpu(rsp->OutputCount); | |
1084 | ||
1085 | /* We check for obvious errors in the output buffer length and offset */ | |
1086 | if (*plen == 0) | |
1087 | goto ioctl_exit; /* server returned no data */ | |
1088 | else if (*plen > 0xFF00) { | |
1089 | cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen); | |
1090 | *plen = 0; | |
1091 | rc = -EIO; | |
1092 | goto ioctl_exit; | |
1093 | } | |
1094 | ||
1095 | if (get_rfc1002_length(rsp) < le32_to_cpu(rsp->OutputOffset) + *plen) { | |
1096 | cifs_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen, | |
1097 | le32_to_cpu(rsp->OutputOffset)); | |
1098 | *plen = 0; | |
1099 | rc = -EIO; | |
1100 | goto ioctl_exit; | |
1101 | } | |
1102 | ||
1103 | *out_data = kmalloc(*plen, GFP_KERNEL); | |
1104 | if (*out_data == NULL) { | |
1105 | rc = -ENOMEM; | |
1106 | goto ioctl_exit; | |
1107 | } | |
1108 | ||
1109 | memcpy(*out_data, rsp->hdr.ProtocolId + le32_to_cpu(rsp->OutputOffset), | |
1110 | *plen); | |
1111 | ioctl_exit: | |
1112 | free_rsp_buf(resp_buftype, rsp); | |
1113 | return rc; | |
1114 | } | |
1115 | ||
2503a0db PS |
1116 | int |
1117 | SMB2_close(const unsigned int xid, struct cifs_tcon *tcon, | |
1118 | u64 persistent_fid, u64 volatile_fid) | |
1119 | { | |
1120 | struct smb2_close_req *req; | |
1121 | struct smb2_close_rsp *rsp; | |
1122 | struct TCP_Server_Info *server; | |
1123 | struct cifs_ses *ses = tcon->ses; | |
1124 | struct kvec iov[1]; | |
1125 | int resp_buftype; | |
1126 | int rc = 0; | |
1127 | ||
f96637be | 1128 | cifs_dbg(FYI, "Close\n"); |
2503a0db PS |
1129 | |
1130 | if (ses && (ses->server)) | |
1131 | server = ses->server; | |
1132 | else | |
1133 | return -EIO; | |
1134 | ||
1135 | rc = small_smb2_init(SMB2_CLOSE, tcon, (void **) &req); | |
1136 | if (rc) | |
1137 | return rc; | |
1138 | ||
1139 | req->PersistentFileId = persistent_fid; | |
1140 | req->VolatileFileId = volatile_fid; | |
1141 | ||
1142 | iov[0].iov_base = (char *)req; | |
1143 | /* 4 for rfc1002 length field */ | |
1144 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1145 | ||
1146 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0); | |
1147 | rsp = (struct smb2_close_rsp *)iov[0].iov_base; | |
1148 | ||
1149 | if (rc != 0) { | |
1150 | if (tcon) | |
1151 | cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE); | |
1152 | goto close_exit; | |
1153 | } | |
1154 | ||
2503a0db PS |
1155 | /* BB FIXME - decode close response, update inode for caching */ |
1156 | ||
1157 | close_exit: | |
1158 | free_rsp_buf(resp_buftype, rsp); | |
1159 | return rc; | |
1160 | } | |
be4cb9e3 PS |
1161 | |
1162 | static int | |
1163 | validate_buf(unsigned int offset, unsigned int buffer_length, | |
1164 | struct smb2_hdr *hdr, unsigned int min_buf_size) | |
1165 | ||
1166 | { | |
1167 | unsigned int smb_len = be32_to_cpu(hdr->smb2_buf_length); | |
1168 | char *end_of_smb = smb_len + 4 /* RFC1001 length field */ + (char *)hdr; | |
1169 | char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr; | |
1170 | char *end_of_buf = begin_of_buf + buffer_length; | |
1171 | ||
1172 | ||
1173 | if (buffer_length < min_buf_size) { | |
f96637be JP |
1174 | cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n", |
1175 | buffer_length, min_buf_size); | |
be4cb9e3 PS |
1176 | return -EINVAL; |
1177 | } | |
1178 | ||
1179 | /* check if beyond RFC1001 maximum length */ | |
1180 | if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) { | |
f96637be JP |
1181 | cifs_dbg(VFS, "buffer length %d or smb length %d too large\n", |
1182 | buffer_length, smb_len); | |
be4cb9e3 PS |
1183 | return -EINVAL; |
1184 | } | |
1185 | ||
1186 | if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) { | |
f96637be | 1187 | cifs_dbg(VFS, "illegal server response, bad offset to data\n"); |
be4cb9e3 PS |
1188 | return -EINVAL; |
1189 | } | |
1190 | ||
1191 | return 0; | |
1192 | } | |
1193 | ||
1194 | /* | |
1195 | * If SMB buffer fields are valid, copy into temporary buffer to hold result. | |
1196 | * Caller must free buffer. | |
1197 | */ | |
1198 | static int | |
1199 | validate_and_copy_buf(unsigned int offset, unsigned int buffer_length, | |
1200 | struct smb2_hdr *hdr, unsigned int minbufsize, | |
1201 | char *data) | |
1202 | ||
1203 | { | |
1204 | char *begin_of_buf = 4 /* RFC1001 len field */ + offset + (char *)hdr; | |
1205 | int rc; | |
1206 | ||
1207 | if (!data) | |
1208 | return -EINVAL; | |
1209 | ||
1210 | rc = validate_buf(offset, buffer_length, hdr, minbufsize); | |
1211 | if (rc) | |
1212 | return rc; | |
1213 | ||
1214 | memcpy(data, begin_of_buf, buffer_length); | |
1215 | ||
1216 | return 0; | |
1217 | } | |
1218 | ||
f0df737e PS |
1219 | static int |
1220 | query_info(const unsigned int xid, struct cifs_tcon *tcon, | |
1221 | u64 persistent_fid, u64 volatile_fid, u8 info_class, | |
1222 | size_t output_len, size_t min_len, void *data) | |
be4cb9e3 PS |
1223 | { |
1224 | struct smb2_query_info_req *req; | |
1225 | struct smb2_query_info_rsp *rsp = NULL; | |
1226 | struct kvec iov[2]; | |
1227 | int rc = 0; | |
1228 | int resp_buftype; | |
1229 | struct TCP_Server_Info *server; | |
1230 | struct cifs_ses *ses = tcon->ses; | |
1231 | ||
f96637be | 1232 | cifs_dbg(FYI, "Query Info\n"); |
be4cb9e3 PS |
1233 | |
1234 | if (ses && (ses->server)) | |
1235 | server = ses->server; | |
1236 | else | |
1237 | return -EIO; | |
1238 | ||
1239 | rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req); | |
1240 | if (rc) | |
1241 | return rc; | |
1242 | ||
1243 | req->InfoType = SMB2_O_INFO_FILE; | |
f0df737e | 1244 | req->FileInfoClass = info_class; |
be4cb9e3 PS |
1245 | req->PersistentFileId = persistent_fid; |
1246 | req->VolatileFileId = volatile_fid; | |
1247 | /* 4 for rfc1002 length field and 1 for Buffer */ | |
1248 | req->InputBufferOffset = | |
1249 | cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4); | |
f0df737e | 1250 | req->OutputBufferLength = cpu_to_le32(output_len); |
be4cb9e3 PS |
1251 | |
1252 | iov[0].iov_base = (char *)req; | |
1253 | /* 4 for rfc1002 length field */ | |
1254 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1255 | ||
1256 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0); | |
e5d04887 PS |
1257 | rsp = (struct smb2_query_info_rsp *)iov[0].iov_base; |
1258 | ||
be4cb9e3 PS |
1259 | if (rc) { |
1260 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); | |
1261 | goto qinf_exit; | |
1262 | } | |
1263 | ||
be4cb9e3 PS |
1264 | rc = validate_and_copy_buf(le16_to_cpu(rsp->OutputBufferOffset), |
1265 | le32_to_cpu(rsp->OutputBufferLength), | |
f0df737e | 1266 | &rsp->hdr, min_len, data); |
be4cb9e3 PS |
1267 | |
1268 | qinf_exit: | |
1269 | free_rsp_buf(resp_buftype, rsp); | |
1270 | return rc; | |
1271 | } | |
9094fad1 | 1272 | |
f0df737e PS |
1273 | int |
1274 | SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon, | |
1275 | u64 persistent_fid, u64 volatile_fid, | |
1276 | struct smb2_file_all_info *data) | |
1277 | { | |
1278 | return query_info(xid, tcon, persistent_fid, volatile_fid, | |
1279 | FILE_ALL_INFORMATION, | |
1280 | sizeof(struct smb2_file_all_info) + MAX_NAME * 2, | |
1281 | sizeof(struct smb2_file_all_info), data); | |
1282 | } | |
1283 | ||
1284 | int | |
1285 | SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon, | |
1286 | u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid) | |
1287 | { | |
1288 | return query_info(xid, tcon, persistent_fid, volatile_fid, | |
1289 | FILE_INTERNAL_INFORMATION, | |
1290 | sizeof(struct smb2_file_internal_info), | |
1291 | sizeof(struct smb2_file_internal_info), uniqueid); | |
1292 | } | |
1293 | ||
9094fad1 PS |
1294 | /* |
1295 | * This is a no-op for now. We're not really interested in the reply, but | |
1296 | * rather in the fact that the server sent one and that server->lstrp | |
1297 | * gets updated. | |
1298 | * | |
1299 | * FIXME: maybe we should consider checking that the reply matches request? | |
1300 | */ | |
1301 | static void | |
1302 | smb2_echo_callback(struct mid_q_entry *mid) | |
1303 | { | |
1304 | struct TCP_Server_Info *server = mid->callback_data; | |
1305 | struct smb2_echo_rsp *smb2 = (struct smb2_echo_rsp *)mid->resp_buf; | |
1306 | unsigned int credits_received = 1; | |
1307 | ||
1308 | if (mid->mid_state == MID_RESPONSE_RECEIVED) | |
1309 | credits_received = le16_to_cpu(smb2->hdr.CreditRequest); | |
1310 | ||
1311 | DeleteMidQEntry(mid); | |
1312 | add_credits(server, credits_received, CIFS_ECHO_OP); | |
1313 | } | |
1314 | ||
1315 | int | |
1316 | SMB2_echo(struct TCP_Server_Info *server) | |
1317 | { | |
1318 | struct smb2_echo_req *req; | |
1319 | int rc = 0; | |
1320 | struct kvec iov; | |
fec344e3 JL |
1321 | struct smb_rqst rqst = { .rq_iov = &iov, |
1322 | .rq_nvec = 1 }; | |
9094fad1 | 1323 | |
f96637be | 1324 | cifs_dbg(FYI, "In echo request\n"); |
9094fad1 PS |
1325 | |
1326 | rc = small_smb2_init(SMB2_ECHO, NULL, (void **)&req); | |
1327 | if (rc) | |
1328 | return rc; | |
1329 | ||
1330 | req->hdr.CreditRequest = cpu_to_le16(1); | |
1331 | ||
1332 | iov.iov_base = (char *)req; | |
1333 | /* 4 for rfc1002 length field */ | |
1334 | iov.iov_len = get_rfc1002_length(req) + 4; | |
1335 | ||
fec344e3 | 1336 | rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, server, |
9094fad1 PS |
1337 | CIFS_ECHO_OP); |
1338 | if (rc) | |
f96637be | 1339 | cifs_dbg(FYI, "Echo request failed: %d\n", rc); |
9094fad1 PS |
1340 | |
1341 | cifs_small_buf_release(req); | |
1342 | return rc; | |
1343 | } | |
7a5cfb19 PS |
1344 | |
1345 | int | |
1346 | SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, | |
1347 | u64 volatile_fid) | |
1348 | { | |
1349 | struct smb2_flush_req *req; | |
1350 | struct TCP_Server_Info *server; | |
1351 | struct cifs_ses *ses = tcon->ses; | |
1352 | struct kvec iov[1]; | |
1353 | int resp_buftype; | |
1354 | int rc = 0; | |
1355 | ||
f96637be | 1356 | cifs_dbg(FYI, "Flush\n"); |
7a5cfb19 PS |
1357 | |
1358 | if (ses && (ses->server)) | |
1359 | server = ses->server; | |
1360 | else | |
1361 | return -EIO; | |
1362 | ||
1363 | rc = small_smb2_init(SMB2_FLUSH, tcon, (void **) &req); | |
1364 | if (rc) | |
1365 | return rc; | |
1366 | ||
1367 | req->PersistentFileId = persistent_fid; | |
1368 | req->VolatileFileId = volatile_fid; | |
1369 | ||
1370 | iov[0].iov_base = (char *)req; | |
1371 | /* 4 for rfc1002 length field */ | |
1372 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1373 | ||
1374 | rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, 0); | |
1375 | ||
1376 | if ((rc != 0) && tcon) | |
1377 | cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE); | |
1378 | ||
1379 | free_rsp_buf(resp_buftype, iov[0].iov_base); | |
1380 | return rc; | |
1381 | } | |
09a4707e PS |
1382 | |
1383 | /* | |
1384 | * To form a chain of read requests, any read requests after the first should | |
1385 | * have the end_of_chain boolean set to true. | |
1386 | */ | |
1387 | static int | |
1388 | smb2_new_read_req(struct kvec *iov, struct cifs_io_parms *io_parms, | |
1389 | unsigned int remaining_bytes, int request_type) | |
1390 | { | |
1391 | int rc = -EACCES; | |
1392 | struct smb2_read_req *req = NULL; | |
1393 | ||
1394 | rc = small_smb2_init(SMB2_READ, io_parms->tcon, (void **) &req); | |
1395 | if (rc) | |
1396 | return rc; | |
1397 | if (io_parms->tcon->ses->server == NULL) | |
1398 | return -ECONNABORTED; | |
1399 | ||
1400 | req->hdr.ProcessId = cpu_to_le32(io_parms->pid); | |
1401 | ||
1402 | req->PersistentFileId = io_parms->persistent_fid; | |
1403 | req->VolatileFileId = io_parms->volatile_fid; | |
1404 | req->ReadChannelInfoOffset = 0; /* reserved */ | |
1405 | req->ReadChannelInfoLength = 0; /* reserved */ | |
1406 | req->Channel = 0; /* reserved */ | |
1407 | req->MinimumCount = 0; | |
1408 | req->Length = cpu_to_le32(io_parms->length); | |
1409 | req->Offset = cpu_to_le64(io_parms->offset); | |
1410 | ||
1411 | if (request_type & CHAINED_REQUEST) { | |
1412 | if (!(request_type & END_OF_CHAIN)) { | |
1413 | /* 4 for rfc1002 length field */ | |
1414 | req->hdr.NextCommand = | |
1415 | cpu_to_le32(get_rfc1002_length(req) + 4); | |
1416 | } else /* END_OF_CHAIN */ | |
1417 | req->hdr.NextCommand = 0; | |
1418 | if (request_type & RELATED_REQUEST) { | |
1419 | req->hdr.Flags |= SMB2_FLAGS_RELATED_OPERATIONS; | |
1420 | /* | |
1421 | * Related requests use info from previous read request | |
1422 | * in chain. | |
1423 | */ | |
1424 | req->hdr.SessionId = 0xFFFFFFFF; | |
1425 | req->hdr.TreeId = 0xFFFFFFFF; | |
1426 | req->PersistentFileId = 0xFFFFFFFF; | |
1427 | req->VolatileFileId = 0xFFFFFFFF; | |
1428 | } | |
1429 | } | |
1430 | if (remaining_bytes > io_parms->length) | |
1431 | req->RemainingBytes = cpu_to_le32(remaining_bytes); | |
1432 | else | |
1433 | req->RemainingBytes = 0; | |
1434 | ||
1435 | iov[0].iov_base = (char *)req; | |
1436 | /* 4 for rfc1002 length field */ | |
1437 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1438 | return rc; | |
1439 | } | |
1440 | ||
1441 | static void | |
1442 | smb2_readv_callback(struct mid_q_entry *mid) | |
1443 | { | |
1444 | struct cifs_readdata *rdata = mid->callback_data; | |
1445 | struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink); | |
1446 | struct TCP_Server_Info *server = tcon->ses->server; | |
5819575e | 1447 | struct smb2_hdr *buf = (struct smb2_hdr *)rdata->iov.iov_base; |
09a4707e | 1448 | unsigned int credits_received = 1; |
5819575e | 1449 | struct smb_rqst rqst = { .rq_iov = &rdata->iov, |
8321fec4 JL |
1450 | .rq_nvec = 1, |
1451 | .rq_pages = rdata->pages, | |
1452 | .rq_npages = rdata->nr_pages, | |
1453 | .rq_pagesz = rdata->pagesz, | |
1454 | .rq_tailsz = rdata->tailsz }; | |
09a4707e | 1455 | |
f96637be JP |
1456 | cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n", |
1457 | __func__, mid->mid, mid->mid_state, rdata->result, | |
1458 | rdata->bytes); | |
09a4707e PS |
1459 | |
1460 | switch (mid->mid_state) { | |
1461 | case MID_RESPONSE_RECEIVED: | |
1462 | credits_received = le16_to_cpu(buf->CreditRequest); | |
1463 | /* result already set, check signature */ | |
38d77c50 | 1464 | if (server->sign) { |
3c1bf7e4 PS |
1465 | int rc; |
1466 | ||
0b688cfc | 1467 | rc = smb2_verify_signature(&rqst, server); |
3c1bf7e4 | 1468 | if (rc) |
f96637be JP |
1469 | cifs_dbg(VFS, "SMB signature verification returned error = %d\n", |
1470 | rc); | |
3c1bf7e4 | 1471 | } |
09a4707e PS |
1472 | /* FIXME: should this be counted toward the initiating task? */ |
1473 | task_io_account_read(rdata->bytes); | |
1474 | cifs_stats_bytes_read(tcon, rdata->bytes); | |
1475 | break; | |
1476 | case MID_REQUEST_SUBMITTED: | |
1477 | case MID_RETRY_NEEDED: | |
1478 | rdata->result = -EAGAIN; | |
1479 | break; | |
1480 | default: | |
1481 | if (rdata->result != -ENODATA) | |
1482 | rdata->result = -EIO; | |
1483 | } | |
1484 | ||
1485 | if (rdata->result) | |
1486 | cifs_stats_fail_inc(tcon, SMB2_READ_HE); | |
1487 | ||
1488 | queue_work(cifsiod_wq, &rdata->work); | |
1489 | DeleteMidQEntry(mid); | |
1490 | add_credits(server, credits_received, 0); | |
1491 | } | |
1492 | ||
1493 | /* smb2_async_readv - send an async write, and set up mid to handle result */ | |
1494 | int | |
1495 | smb2_async_readv(struct cifs_readdata *rdata) | |
1496 | { | |
1497 | int rc; | |
1498 | struct smb2_hdr *buf; | |
1499 | struct cifs_io_parms io_parms; | |
5819575e | 1500 | struct smb_rqst rqst = { .rq_iov = &rdata->iov, |
fec344e3 | 1501 | .rq_nvec = 1 }; |
09a4707e | 1502 | |
f96637be JP |
1503 | cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n", |
1504 | __func__, rdata->offset, rdata->bytes); | |
09a4707e PS |
1505 | |
1506 | io_parms.tcon = tlink_tcon(rdata->cfile->tlink); | |
1507 | io_parms.offset = rdata->offset; | |
1508 | io_parms.length = rdata->bytes; | |
1509 | io_parms.persistent_fid = rdata->cfile->fid.persistent_fid; | |
1510 | io_parms.volatile_fid = rdata->cfile->fid.volatile_fid; | |
1511 | io_parms.pid = rdata->pid; | |
5819575e | 1512 | rc = smb2_new_read_req(&rdata->iov, &io_parms, 0, 0); |
09a4707e PS |
1513 | if (rc) |
1514 | return rc; | |
1515 | ||
5819575e | 1516 | buf = (struct smb2_hdr *)rdata->iov.iov_base; |
09a4707e | 1517 | /* 4 for rfc1002 length field */ |
5819575e | 1518 | rdata->iov.iov_len = get_rfc1002_length(rdata->iov.iov_base) + 4; |
09a4707e PS |
1519 | |
1520 | kref_get(&rdata->refcount); | |
fec344e3 | 1521 | rc = cifs_call_async(io_parms.tcon->ses->server, &rqst, |
09a4707e PS |
1522 | cifs_readv_receive, smb2_readv_callback, |
1523 | rdata, 0); | |
e5d04887 | 1524 | if (rc) { |
09a4707e | 1525 | kref_put(&rdata->refcount, cifs_readdata_release); |
e5d04887 PS |
1526 | cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE); |
1527 | } | |
09a4707e PS |
1528 | |
1529 | cifs_small_buf_release(buf); | |
1530 | return rc; | |
1531 | } | |
33319141 | 1532 | |
d8e05039 PS |
1533 | int |
1534 | SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms, | |
1535 | unsigned int *nbytes, char **buf, int *buf_type) | |
1536 | { | |
1537 | int resp_buftype, rc = -EACCES; | |
1538 | struct smb2_read_rsp *rsp = NULL; | |
1539 | struct kvec iov[1]; | |
1540 | ||
1541 | *nbytes = 0; | |
1542 | rc = smb2_new_read_req(iov, io_parms, 0, 0); | |
1543 | if (rc) | |
1544 | return rc; | |
1545 | ||
1546 | rc = SendReceive2(xid, io_parms->tcon->ses, iov, 1, | |
1547 | &resp_buftype, CIFS_LOG_ERROR); | |
1548 | ||
1549 | rsp = (struct smb2_read_rsp *)iov[0].iov_base; | |
1550 | ||
1551 | if (rsp->hdr.Status == STATUS_END_OF_FILE) { | |
1552 | free_rsp_buf(resp_buftype, iov[0].iov_base); | |
1553 | return 0; | |
1554 | } | |
1555 | ||
1556 | if (rc) { | |
1557 | cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE); | |
f96637be | 1558 | cifs_dbg(VFS, "Send error in read = %d\n", rc); |
d8e05039 PS |
1559 | } else { |
1560 | *nbytes = le32_to_cpu(rsp->DataLength); | |
1561 | if ((*nbytes > CIFS_MAX_MSGSIZE) || | |
1562 | (*nbytes > io_parms->length)) { | |
f96637be JP |
1563 | cifs_dbg(FYI, "bad length %d for count %d\n", |
1564 | *nbytes, io_parms->length); | |
d8e05039 PS |
1565 | rc = -EIO; |
1566 | *nbytes = 0; | |
1567 | } | |
1568 | } | |
1569 | ||
1570 | if (*buf) { | |
1571 | memcpy(*buf, (char *)rsp->hdr.ProtocolId + rsp->DataOffset, | |
1572 | *nbytes); | |
1573 | free_rsp_buf(resp_buftype, iov[0].iov_base); | |
1574 | } else if (resp_buftype != CIFS_NO_BUFFER) { | |
1575 | *buf = iov[0].iov_base; | |
1576 | if (resp_buftype == CIFS_SMALL_BUFFER) | |
1577 | *buf_type = CIFS_SMALL_BUFFER; | |
1578 | else if (resp_buftype == CIFS_LARGE_BUFFER) | |
1579 | *buf_type = CIFS_LARGE_BUFFER; | |
1580 | } | |
1581 | return rc; | |
1582 | } | |
1583 | ||
33319141 PS |
1584 | /* |
1585 | * Check the mid_state and signature on received buffer (if any), and queue the | |
1586 | * workqueue completion task. | |
1587 | */ | |
1588 | static void | |
1589 | smb2_writev_callback(struct mid_q_entry *mid) | |
1590 | { | |
1591 | struct cifs_writedata *wdata = mid->callback_data; | |
1592 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); | |
1593 | unsigned int written; | |
1594 | struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf; | |
1595 | unsigned int credits_received = 1; | |
1596 | ||
1597 | switch (mid->mid_state) { | |
1598 | case MID_RESPONSE_RECEIVED: | |
1599 | credits_received = le16_to_cpu(rsp->hdr.CreditRequest); | |
1600 | wdata->result = smb2_check_receive(mid, tcon->ses->server, 0); | |
1601 | if (wdata->result != 0) | |
1602 | break; | |
1603 | ||
1604 | written = le32_to_cpu(rsp->DataLength); | |
1605 | /* | |
1606 | * Mask off high 16 bits when bytes written as returned | |
1607 | * by the server is greater than bytes requested by the | |
1608 | * client. OS/2 servers are known to set incorrect | |
1609 | * CountHigh values. | |
1610 | */ | |
1611 | if (written > wdata->bytes) | |
1612 | written &= 0xFFFF; | |
1613 | ||
1614 | if (written < wdata->bytes) | |
1615 | wdata->result = -ENOSPC; | |
1616 | else | |
1617 | wdata->bytes = written; | |
1618 | break; | |
1619 | case MID_REQUEST_SUBMITTED: | |
1620 | case MID_RETRY_NEEDED: | |
1621 | wdata->result = -EAGAIN; | |
1622 | break; | |
1623 | default: | |
1624 | wdata->result = -EIO; | |
1625 | break; | |
1626 | } | |
1627 | ||
1628 | if (wdata->result) | |
1629 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); | |
1630 | ||
1631 | queue_work(cifsiod_wq, &wdata->work); | |
1632 | DeleteMidQEntry(mid); | |
1633 | add_credits(tcon->ses->server, credits_received, 0); | |
1634 | } | |
1635 | ||
1636 | /* smb2_async_writev - send an async write, and set up mid to handle result */ | |
1637 | int | |
1638 | smb2_async_writev(struct cifs_writedata *wdata) | |
1639 | { | |
eddb079d | 1640 | int rc = -EACCES; |
33319141 PS |
1641 | struct smb2_write_req *req = NULL; |
1642 | struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink); | |
eddb079d | 1643 | struct kvec iov; |
fec344e3 | 1644 | struct smb_rqst rqst; |
33319141 PS |
1645 | |
1646 | rc = small_smb2_init(SMB2_WRITE, tcon, (void **) &req); | |
1647 | if (rc) | |
1648 | goto async_writev_out; | |
1649 | ||
33319141 PS |
1650 | req->hdr.ProcessId = cpu_to_le32(wdata->cfile->pid); |
1651 | ||
1652 | req->PersistentFileId = wdata->cfile->fid.persistent_fid; | |
1653 | req->VolatileFileId = wdata->cfile->fid.volatile_fid; | |
1654 | req->WriteChannelInfoOffset = 0; | |
1655 | req->WriteChannelInfoLength = 0; | |
1656 | req->Channel = 0; | |
1657 | req->Offset = cpu_to_le64(wdata->offset); | |
1658 | /* 4 for rfc1002 length field */ | |
1659 | req->DataOffset = cpu_to_le16( | |
1660 | offsetof(struct smb2_write_req, Buffer) - 4); | |
1661 | req->RemainingBytes = 0; | |
1662 | ||
1663 | /* 4 for rfc1002 length field and 1 for Buffer */ | |
eddb079d JL |
1664 | iov.iov_len = get_rfc1002_length(req) + 4 - 1; |
1665 | iov.iov_base = req; | |
33319141 | 1666 | |
eddb079d JL |
1667 | rqst.rq_iov = &iov; |
1668 | rqst.rq_nvec = 1; | |
1669 | rqst.rq_pages = wdata->pages; | |
1670 | rqst.rq_npages = wdata->nr_pages; | |
1671 | rqst.rq_pagesz = wdata->pagesz; | |
1672 | rqst.rq_tailsz = wdata->tailsz; | |
33319141 | 1673 | |
f96637be JP |
1674 | cifs_dbg(FYI, "async write at %llu %u bytes\n", |
1675 | wdata->offset, wdata->bytes); | |
33319141 PS |
1676 | |
1677 | req->Length = cpu_to_le32(wdata->bytes); | |
1678 | ||
1679 | inc_rfc1001_len(&req->hdr, wdata->bytes - 1 /* Buffer */); | |
1680 | ||
1681 | kref_get(&wdata->refcount); | |
fec344e3 JL |
1682 | rc = cifs_call_async(tcon->ses->server, &rqst, NULL, |
1683 | smb2_writev_callback, wdata, 0); | |
33319141 | 1684 | |
e5d04887 | 1685 | if (rc) { |
33319141 | 1686 | kref_put(&wdata->refcount, cifs_writedata_release); |
e5d04887 PS |
1687 | cifs_stats_fail_inc(tcon, SMB2_WRITE_HE); |
1688 | } | |
33319141 | 1689 | |
33319141 PS |
1690 | async_writev_out: |
1691 | cifs_small_buf_release(req); | |
33319141 PS |
1692 | return rc; |
1693 | } | |
009d3443 PS |
1694 | |
1695 | /* | |
1696 | * SMB2_write function gets iov pointer to kvec array with n_vec as a length. | |
1697 | * The length field from io_parms must be at least 1 and indicates a number of | |
1698 | * elements with data to write that begins with position 1 in iov array. All | |
1699 | * data length is specified by count. | |
1700 | */ | |
1701 | int | |
1702 | SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms, | |
1703 | unsigned int *nbytes, struct kvec *iov, int n_vec) | |
1704 | { | |
1705 | int rc = 0; | |
1706 | struct smb2_write_req *req = NULL; | |
1707 | struct smb2_write_rsp *rsp = NULL; | |
1708 | int resp_buftype; | |
1709 | *nbytes = 0; | |
1710 | ||
1711 | if (n_vec < 1) | |
1712 | return rc; | |
1713 | ||
1714 | rc = small_smb2_init(SMB2_WRITE, io_parms->tcon, (void **) &req); | |
1715 | if (rc) | |
1716 | return rc; | |
1717 | ||
1718 | if (io_parms->tcon->ses->server == NULL) | |
1719 | return -ECONNABORTED; | |
1720 | ||
1721 | req->hdr.ProcessId = cpu_to_le32(io_parms->pid); | |
1722 | ||
1723 | req->PersistentFileId = io_parms->persistent_fid; | |
1724 | req->VolatileFileId = io_parms->volatile_fid; | |
1725 | req->WriteChannelInfoOffset = 0; | |
1726 | req->WriteChannelInfoLength = 0; | |
1727 | req->Channel = 0; | |
1728 | req->Length = cpu_to_le32(io_parms->length); | |
1729 | req->Offset = cpu_to_le64(io_parms->offset); | |
1730 | /* 4 for rfc1002 length field */ | |
1731 | req->DataOffset = cpu_to_le16( | |
1732 | offsetof(struct smb2_write_req, Buffer) - 4); | |
1733 | req->RemainingBytes = 0; | |
1734 | ||
1735 | iov[0].iov_base = (char *)req; | |
1736 | /* 4 for rfc1002 length field and 1 for Buffer */ | |
1737 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
1738 | ||
1739 | /* length of entire message including data to be written */ | |
1740 | inc_rfc1001_len(req, io_parms->length - 1 /* Buffer */); | |
1741 | ||
1742 | rc = SendReceive2(xid, io_parms->tcon->ses, iov, n_vec + 1, | |
1743 | &resp_buftype, 0); | |
e5d04887 | 1744 | rsp = (struct smb2_write_rsp *)iov[0].iov_base; |
009d3443 PS |
1745 | |
1746 | if (rc) { | |
1747 | cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE); | |
f96637be | 1748 | cifs_dbg(VFS, "Send error in write = %d\n", rc); |
e5d04887 | 1749 | } else |
009d3443 | 1750 | *nbytes = le32_to_cpu(rsp->DataLength); |
e5d04887 PS |
1751 | |
1752 | free_rsp_buf(resp_buftype, rsp); | |
009d3443 PS |
1753 | return rc; |
1754 | } | |
35143eb5 | 1755 | |
d324f08d PS |
1756 | static unsigned int |
1757 | num_entries(char *bufstart, char *end_of_buf, char **lastentry, size_t size) | |
1758 | { | |
1759 | int len; | |
1760 | unsigned int entrycount = 0; | |
1761 | unsigned int next_offset = 0; | |
1762 | FILE_DIRECTORY_INFO *entryptr; | |
1763 | ||
1764 | if (bufstart == NULL) | |
1765 | return 0; | |
1766 | ||
1767 | entryptr = (FILE_DIRECTORY_INFO *)bufstart; | |
1768 | ||
1769 | while (1) { | |
1770 | entryptr = (FILE_DIRECTORY_INFO *) | |
1771 | ((char *)entryptr + next_offset); | |
1772 | ||
1773 | if ((char *)entryptr + size > end_of_buf) { | |
f96637be | 1774 | cifs_dbg(VFS, "malformed search entry would overflow\n"); |
d324f08d PS |
1775 | break; |
1776 | } | |
1777 | ||
1778 | len = le32_to_cpu(entryptr->FileNameLength); | |
1779 | if ((char *)entryptr + len + size > end_of_buf) { | |
f96637be JP |
1780 | cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n", |
1781 | end_of_buf); | |
d324f08d PS |
1782 | break; |
1783 | } | |
1784 | ||
1785 | *lastentry = (char *)entryptr; | |
1786 | entrycount++; | |
1787 | ||
1788 | next_offset = le32_to_cpu(entryptr->NextEntryOffset); | |
1789 | if (!next_offset) | |
1790 | break; | |
1791 | } | |
1792 | ||
1793 | return entrycount; | |
1794 | } | |
1795 | ||
1796 | /* | |
1797 | * Readdir/FindFirst | |
1798 | */ | |
1799 | int | |
1800 | SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon, | |
1801 | u64 persistent_fid, u64 volatile_fid, int index, | |
1802 | struct cifs_search_info *srch_inf) | |
1803 | { | |
1804 | struct smb2_query_directory_req *req; | |
1805 | struct smb2_query_directory_rsp *rsp = NULL; | |
1806 | struct kvec iov[2]; | |
1807 | int rc = 0; | |
1808 | int len; | |
1809 | int resp_buftype; | |
1810 | unsigned char *bufptr; | |
1811 | struct TCP_Server_Info *server; | |
1812 | struct cifs_ses *ses = tcon->ses; | |
1813 | __le16 asteriks = cpu_to_le16('*'); | |
1814 | char *end_of_smb; | |
1815 | unsigned int output_size = CIFSMaxBufSize; | |
1816 | size_t info_buf_size; | |
1817 | ||
1818 | if (ses && (ses->server)) | |
1819 | server = ses->server; | |
1820 | else | |
1821 | return -EIO; | |
1822 | ||
1823 | rc = small_smb2_init(SMB2_QUERY_DIRECTORY, tcon, (void **) &req); | |
1824 | if (rc) | |
1825 | return rc; | |
1826 | ||
1827 | switch (srch_inf->info_level) { | |
1828 | case SMB_FIND_FILE_DIRECTORY_INFO: | |
1829 | req->FileInformationClass = FILE_DIRECTORY_INFORMATION; | |
1830 | info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1; | |
1831 | break; | |
1832 | case SMB_FIND_FILE_ID_FULL_DIR_INFO: | |
1833 | req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION; | |
1834 | info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1; | |
1835 | break; | |
1836 | default: | |
f96637be JP |
1837 | cifs_dbg(VFS, "info level %u isn't supported\n", |
1838 | srch_inf->info_level); | |
d324f08d PS |
1839 | rc = -EINVAL; |
1840 | goto qdir_exit; | |
1841 | } | |
1842 | ||
1843 | req->FileIndex = cpu_to_le32(index); | |
1844 | req->PersistentFileId = persistent_fid; | |
1845 | req->VolatileFileId = volatile_fid; | |
1846 | ||
1847 | len = 0x2; | |
1848 | bufptr = req->Buffer; | |
1849 | memcpy(bufptr, &asteriks, len); | |
1850 | ||
1851 | req->FileNameOffset = | |
1852 | cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1 - 4); | |
1853 | req->FileNameLength = cpu_to_le16(len); | |
1854 | /* | |
1855 | * BB could be 30 bytes or so longer if we used SMB2 specific | |
1856 | * buffer lengths, but this is safe and close enough. | |
1857 | */ | |
1858 | output_size = min_t(unsigned int, output_size, server->maxBuf); | |
1859 | output_size = min_t(unsigned int, output_size, 2 << 15); | |
1860 | req->OutputBufferLength = cpu_to_le32(output_size); | |
1861 | ||
1862 | iov[0].iov_base = (char *)req; | |
1863 | /* 4 for RFC1001 length and 1 for Buffer */ | |
1864 | iov[0].iov_len = get_rfc1002_length(req) + 4 - 1; | |
1865 | ||
1866 | iov[1].iov_base = (char *)(req->Buffer); | |
1867 | iov[1].iov_len = len; | |
1868 | ||
1869 | inc_rfc1001_len(req, len - 1 /* Buffer */); | |
1870 | ||
1871 | rc = SendReceive2(xid, ses, iov, 2, &resp_buftype, 0); | |
e5d04887 PS |
1872 | rsp = (struct smb2_query_directory_rsp *)iov[0].iov_base; |
1873 | ||
d324f08d PS |
1874 | if (rc) { |
1875 | cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE); | |
1876 | goto qdir_exit; | |
1877 | } | |
d324f08d PS |
1878 | |
1879 | rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset), | |
1880 | le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr, | |
1881 | info_buf_size); | |
1882 | if (rc) | |
1883 | goto qdir_exit; | |
1884 | ||
1885 | srch_inf->unicode = true; | |
1886 | ||
1887 | if (srch_inf->ntwrk_buf_start) { | |
1888 | if (srch_inf->smallBuf) | |
1889 | cifs_small_buf_release(srch_inf->ntwrk_buf_start); | |
1890 | else | |
1891 | cifs_buf_release(srch_inf->ntwrk_buf_start); | |
1892 | } | |
1893 | srch_inf->ntwrk_buf_start = (char *)rsp; | |
1894 | srch_inf->srch_entries_start = srch_inf->last_entry = 4 /* rfclen */ + | |
1895 | (char *)&rsp->hdr + le16_to_cpu(rsp->OutputBufferOffset); | |
1896 | /* 4 for rfc1002 length field */ | |
1897 | end_of_smb = get_rfc1002_length(rsp) + 4 + (char *)&rsp->hdr; | |
1898 | srch_inf->entries_in_buffer = | |
1899 | num_entries(srch_inf->srch_entries_start, end_of_smb, | |
1900 | &srch_inf->last_entry, info_buf_size); | |
1901 | srch_inf->index_of_last_entry += srch_inf->entries_in_buffer; | |
f96637be JP |
1902 | cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n", |
1903 | srch_inf->entries_in_buffer, srch_inf->index_of_last_entry, | |
1904 | srch_inf->srch_entries_start, srch_inf->last_entry); | |
d324f08d PS |
1905 | if (resp_buftype == CIFS_LARGE_BUFFER) |
1906 | srch_inf->smallBuf = false; | |
1907 | else if (resp_buftype == CIFS_SMALL_BUFFER) | |
1908 | srch_inf->smallBuf = true; | |
1909 | else | |
f96637be | 1910 | cifs_dbg(VFS, "illegal search buffer type\n"); |
d324f08d PS |
1911 | |
1912 | if (rsp->hdr.Status == STATUS_NO_MORE_FILES) | |
1913 | srch_inf->endOfSearch = 1; | |
1914 | else | |
1915 | srch_inf->endOfSearch = 0; | |
1916 | ||
1917 | return rc; | |
1918 | ||
1919 | qdir_exit: | |
1920 | free_rsp_buf(resp_buftype, rsp); | |
1921 | return rc; | |
1922 | } | |
1923 | ||
35143eb5 PS |
1924 | static int |
1925 | send_set_info(const unsigned int xid, struct cifs_tcon *tcon, | |
c839ff24 | 1926 | u64 persistent_fid, u64 volatile_fid, u32 pid, int info_class, |
35143eb5 PS |
1927 | unsigned int num, void **data, unsigned int *size) |
1928 | { | |
1929 | struct smb2_set_info_req *req; | |
1930 | struct smb2_set_info_rsp *rsp = NULL; | |
1931 | struct kvec *iov; | |
1932 | int rc = 0; | |
1933 | int resp_buftype; | |
1934 | unsigned int i; | |
1935 | struct TCP_Server_Info *server; | |
1936 | struct cifs_ses *ses = tcon->ses; | |
1937 | ||
1938 | if (ses && (ses->server)) | |
1939 | server = ses->server; | |
1940 | else | |
1941 | return -EIO; | |
1942 | ||
1943 | if (!num) | |
1944 | return -EINVAL; | |
1945 | ||
1946 | iov = kmalloc(sizeof(struct kvec) * num, GFP_KERNEL); | |
1947 | if (!iov) | |
1948 | return -ENOMEM; | |
1949 | ||
1950 | rc = small_smb2_init(SMB2_SET_INFO, tcon, (void **) &req); | |
1951 | if (rc) { | |
1952 | kfree(iov); | |
1953 | return rc; | |
1954 | } | |
1955 | ||
c839ff24 PS |
1956 | req->hdr.ProcessId = cpu_to_le32(pid); |
1957 | ||
35143eb5 PS |
1958 | req->InfoType = SMB2_O_INFO_FILE; |
1959 | req->FileInfoClass = info_class; | |
1960 | req->PersistentFileId = persistent_fid; | |
1961 | req->VolatileFileId = volatile_fid; | |
1962 | ||
1963 | /* 4 for RFC1001 length and 1 for Buffer */ | |
1964 | req->BufferOffset = | |
1965 | cpu_to_le16(sizeof(struct smb2_set_info_req) - 1 - 4); | |
1966 | req->BufferLength = cpu_to_le32(*size); | |
1967 | ||
1968 | inc_rfc1001_len(req, *size - 1 /* Buffer */); | |
1969 | ||
1970 | memcpy(req->Buffer, *data, *size); | |
1971 | ||
1972 | iov[0].iov_base = (char *)req; | |
1973 | /* 4 for RFC1001 length */ | |
1974 | iov[0].iov_len = get_rfc1002_length(req) + 4; | |
1975 | ||
1976 | for (i = 1; i < num; i++) { | |
1977 | inc_rfc1001_len(req, size[i]); | |
1978 | le32_add_cpu(&req->BufferLength, size[i]); | |
1979 | iov[i].iov_base = (char *)data[i]; | |
1980 | iov[i].iov_len = size[i]; | |
1981 | } | |
1982 | ||
1983 | rc = SendReceive2(xid, ses, iov, num, &resp_buftype, 0); | |
1984 | rsp = (struct smb2_set_info_rsp *)iov[0].iov_base; | |
1985 | ||
1986 | if (rc != 0) { | |
1987 | cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE); | |
1988 | goto out; | |
1989 | } | |
35143eb5 PS |
1990 | out: |
1991 | free_rsp_buf(resp_buftype, rsp); | |
1992 | kfree(iov); | |
1993 | return rc; | |
1994 | } | |
1995 | ||
1996 | int | |
1997 | SMB2_rename(const unsigned int xid, struct cifs_tcon *tcon, | |
1998 | u64 persistent_fid, u64 volatile_fid, __le16 *target_file) | |
1999 | { | |
2000 | struct smb2_file_rename_info info; | |
2001 | void **data; | |
2002 | unsigned int size[2]; | |
2003 | int rc; | |
2004 | int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX)); | |
2005 | ||
2006 | data = kmalloc(sizeof(void *) * 2, GFP_KERNEL); | |
2007 | if (!data) | |
2008 | return -ENOMEM; | |
2009 | ||
2010 | info.ReplaceIfExists = 1; /* 1 = replace existing target with new */ | |
2011 | /* 0 = fail if target already exists */ | |
2012 | info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */ | |
2013 | info.FileNameLength = cpu_to_le32(len); | |
2014 | ||
2015 | data[0] = &info; | |
2016 | size[0] = sizeof(struct smb2_file_rename_info); | |
2017 | ||
2018 | data[1] = target_file; | |
2019 | size[1] = len + 2 /* null */; | |
2020 | ||
2021 | rc = send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
c839ff24 PS |
2022 | current->tgid, FILE_RENAME_INFORMATION, 2, data, |
2023 | size); | |
35143eb5 PS |
2024 | kfree(data); |
2025 | return rc; | |
2026 | } | |
568798cc PS |
2027 | |
2028 | int | |
2029 | SMB2_set_hardlink(const unsigned int xid, struct cifs_tcon *tcon, | |
2030 | u64 persistent_fid, u64 volatile_fid, __le16 *target_file) | |
2031 | { | |
2032 | struct smb2_file_link_info info; | |
2033 | void **data; | |
2034 | unsigned int size[2]; | |
2035 | int rc; | |
2036 | int len = (2 * UniStrnlen((wchar_t *)target_file, PATH_MAX)); | |
2037 | ||
2038 | data = kmalloc(sizeof(void *) * 2, GFP_KERNEL); | |
2039 | if (!data) | |
2040 | return -ENOMEM; | |
2041 | ||
2042 | info.ReplaceIfExists = 0; /* 1 = replace existing link with new */ | |
2043 | /* 0 = fail if link already exists */ | |
2044 | info.RootDirectory = 0; /* MBZ for network ops (why does spec say?) */ | |
2045 | info.FileNameLength = cpu_to_le32(len); | |
2046 | ||
2047 | data[0] = &info; | |
2048 | size[0] = sizeof(struct smb2_file_link_info); | |
2049 | ||
2050 | data[1] = target_file; | |
2051 | size[1] = len + 2 /* null */; | |
2052 | ||
2053 | rc = send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
c839ff24 | 2054 | current->tgid, FILE_LINK_INFORMATION, 2, data, size); |
568798cc PS |
2055 | kfree(data); |
2056 | return rc; | |
2057 | } | |
c839ff24 PS |
2058 | |
2059 | int | |
2060 | SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid, | |
2061 | u64 volatile_fid, u32 pid, __le64 *eof) | |
2062 | { | |
2063 | struct smb2_file_eof_info info; | |
2064 | void *data; | |
2065 | unsigned int size; | |
2066 | ||
2067 | info.EndOfFile = *eof; | |
2068 | ||
2069 | data = &info; | |
2070 | size = sizeof(struct smb2_file_eof_info); | |
2071 | ||
2072 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, pid, | |
2073 | FILE_END_OF_FILE_INFORMATION, 1, &data, &size); | |
2074 | } | |
1feeaac7 PS |
2075 | |
2076 | int | |
2077 | SMB2_set_info(const unsigned int xid, struct cifs_tcon *tcon, | |
2078 | u64 persistent_fid, u64 volatile_fid, FILE_BASIC_INFO *buf) | |
2079 | { | |
2080 | unsigned int size; | |
2081 | size = sizeof(FILE_BASIC_INFO); | |
2082 | return send_set_info(xid, tcon, persistent_fid, volatile_fid, | |
2083 | current->tgid, FILE_BASIC_INFORMATION, 1, | |
2084 | (void **)&buf, &size); | |
2085 | } | |
983c88a4 PS |
2086 | |
2087 | int | |
2088 | SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon, | |
2089 | const u64 persistent_fid, const u64 volatile_fid, | |
2090 | __u8 oplock_level) | |
2091 | { | |
2092 | int rc; | |
2093 | struct smb2_oplock_break *req = NULL; | |
2094 | ||
f96637be | 2095 | cifs_dbg(FYI, "SMB2_oplock_break\n"); |
983c88a4 PS |
2096 | rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req); |
2097 | ||
2098 | if (rc) | |
2099 | return rc; | |
2100 | ||
2101 | req->VolatileFid = volatile_fid; | |
2102 | req->PersistentFid = persistent_fid; | |
2103 | req->OplockLevel = oplock_level; | |
2104 | req->hdr.CreditRequest = cpu_to_le16(1); | |
2105 | ||
2106 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP); | |
2107 | /* SMB2 buffer freed by function above */ | |
2108 | ||
2109 | if (rc) { | |
2110 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); | |
f96637be | 2111 | cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc); |
983c88a4 PS |
2112 | } |
2113 | ||
2114 | return rc; | |
2115 | } | |
6fc05c25 PS |
2116 | |
2117 | static void | |
2118 | copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf, | |
2119 | struct kstatfs *kst) | |
2120 | { | |
2121 | kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) * | |
2122 | le32_to_cpu(pfs_inf->SectorsPerAllocationUnit); | |
2123 | kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits); | |
2124 | kst->f_bfree = le64_to_cpu(pfs_inf->ActualAvailableAllocationUnits); | |
2125 | kst->f_bavail = le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits); | |
2126 | return; | |
2127 | } | |
2128 | ||
2129 | static int | |
2130 | build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon, int level, | |
2131 | int outbuf_len, u64 persistent_fid, u64 volatile_fid) | |
2132 | { | |
2133 | int rc; | |
2134 | struct smb2_query_info_req *req; | |
2135 | ||
f96637be | 2136 | cifs_dbg(FYI, "Query FSInfo level %d\n", level); |
6fc05c25 PS |
2137 | |
2138 | if ((tcon->ses == NULL) || (tcon->ses->server == NULL)) | |
2139 | return -EIO; | |
2140 | ||
2141 | rc = small_smb2_init(SMB2_QUERY_INFO, tcon, (void **) &req); | |
2142 | if (rc) | |
2143 | return rc; | |
2144 | ||
2145 | req->InfoType = SMB2_O_INFO_FILESYSTEM; | |
2146 | req->FileInfoClass = level; | |
2147 | req->PersistentFileId = persistent_fid; | |
2148 | req->VolatileFileId = volatile_fid; | |
2149 | /* 4 for rfc1002 length field and 1 for pad */ | |
2150 | req->InputBufferOffset = | |
2151 | cpu_to_le16(sizeof(struct smb2_query_info_req) - 1 - 4); | |
2152 | req->OutputBufferLength = cpu_to_le32( | |
2153 | outbuf_len + sizeof(struct smb2_query_info_rsp) - 1 - 4); | |
2154 | ||
2155 | iov->iov_base = (char *)req; | |
2156 | /* 4 for rfc1002 length field */ | |
2157 | iov->iov_len = get_rfc1002_length(req) + 4; | |
2158 | return 0; | |
2159 | } | |
2160 | ||
2161 | int | |
2162 | SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon, | |
2163 | u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata) | |
2164 | { | |
2165 | struct smb2_query_info_rsp *rsp = NULL; | |
2166 | struct kvec iov; | |
2167 | int rc = 0; | |
2168 | int resp_buftype; | |
2169 | struct cifs_ses *ses = tcon->ses; | |
2170 | struct smb2_fs_full_size_info *info = NULL; | |
2171 | ||
2172 | rc = build_qfs_info_req(&iov, tcon, FS_FULL_SIZE_INFORMATION, | |
2173 | sizeof(struct smb2_fs_full_size_info), | |
2174 | persistent_fid, volatile_fid); | |
2175 | if (rc) | |
2176 | return rc; | |
2177 | ||
2178 | rc = SendReceive2(xid, ses, &iov, 1, &resp_buftype, 0); | |
2179 | if (rc) { | |
2180 | cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE); | |
2181 | goto qinf_exit; | |
2182 | } | |
2183 | rsp = (struct smb2_query_info_rsp *)iov.iov_base; | |
2184 | ||
2185 | info = (struct smb2_fs_full_size_info *)(4 /* RFC1001 len */ + | |
2186 | le16_to_cpu(rsp->OutputBufferOffset) + (char *)&rsp->hdr); | |
2187 | rc = validate_buf(le16_to_cpu(rsp->OutputBufferOffset), | |
2188 | le32_to_cpu(rsp->OutputBufferLength), &rsp->hdr, | |
2189 | sizeof(struct smb2_fs_full_size_info)); | |
2190 | if (!rc) | |
2191 | copy_fs_info_to_kstatfs(info, fsdata); | |
2192 | ||
2193 | qinf_exit: | |
2194 | free_rsp_buf(resp_buftype, iov.iov_base); | |
2195 | return rc; | |
2196 | } | |
f7ba7fe6 PS |
2197 | |
2198 | int | |
2199 | smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon, | |
2200 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, | |
2201 | const __u32 num_lock, struct smb2_lock_element *buf) | |
2202 | { | |
2203 | int rc = 0; | |
2204 | struct smb2_lock_req *req = NULL; | |
2205 | struct kvec iov[2]; | |
2206 | int resp_buf_type; | |
2207 | unsigned int count; | |
2208 | ||
f96637be | 2209 | cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock); |
f7ba7fe6 PS |
2210 | |
2211 | rc = small_smb2_init(SMB2_LOCK, tcon, (void **) &req); | |
2212 | if (rc) | |
2213 | return rc; | |
2214 | ||
2215 | req->hdr.ProcessId = cpu_to_le32(pid); | |
2216 | req->LockCount = cpu_to_le16(num_lock); | |
2217 | ||
2218 | req->PersistentFileId = persist_fid; | |
2219 | req->VolatileFileId = volatile_fid; | |
2220 | ||
2221 | count = num_lock * sizeof(struct smb2_lock_element); | |
2222 | inc_rfc1001_len(req, count - sizeof(struct smb2_lock_element)); | |
2223 | ||
2224 | iov[0].iov_base = (char *)req; | |
2225 | /* 4 for rfc1002 length field and count for all locks */ | |
2226 | iov[0].iov_len = get_rfc1002_length(req) + 4 - count; | |
2227 | iov[1].iov_base = (char *)buf; | |
2228 | iov[1].iov_len = count; | |
2229 | ||
2230 | cifs_stats_inc(&tcon->stats.cifs_stats.num_locks); | |
2231 | rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP); | |
2232 | if (rc) { | |
f96637be | 2233 | cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc); |
f7ba7fe6 PS |
2234 | cifs_stats_fail_inc(tcon, SMB2_LOCK_HE); |
2235 | } | |
2236 | ||
2237 | return rc; | |
2238 | } | |
2239 | ||
2240 | int | |
2241 | SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon, | |
2242 | const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid, | |
2243 | const __u64 length, const __u64 offset, const __u32 lock_flags, | |
2244 | const bool wait) | |
2245 | { | |
2246 | struct smb2_lock_element lock; | |
2247 | ||
2248 | lock.Offset = cpu_to_le64(offset); | |
2249 | lock.Length = cpu_to_le64(length); | |
2250 | lock.Flags = cpu_to_le32(lock_flags); | |
2251 | if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK) | |
2252 | lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY); | |
2253 | ||
2254 | return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock); | |
2255 | } | |
0822f514 PS |
2256 | |
2257 | int | |
2258 | SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon, | |
2259 | __u8 *lease_key, const __le32 lease_state) | |
2260 | { | |
2261 | int rc; | |
2262 | struct smb2_lease_ack *req = NULL; | |
2263 | ||
f96637be | 2264 | cifs_dbg(FYI, "SMB2_lease_break\n"); |
0822f514 PS |
2265 | rc = small_smb2_init(SMB2_OPLOCK_BREAK, tcon, (void **) &req); |
2266 | ||
2267 | if (rc) | |
2268 | return rc; | |
2269 | ||
2270 | req->hdr.CreditRequest = cpu_to_le16(1); | |
2271 | req->StructureSize = cpu_to_le16(36); | |
2272 | inc_rfc1001_len(req, 12); | |
2273 | ||
2274 | memcpy(req->LeaseKey, lease_key, 16); | |
2275 | req->LeaseState = lease_state; | |
2276 | ||
2277 | rc = SendReceiveNoRsp(xid, tcon->ses, (char *) req, CIFS_OBREAK_OP); | |
2278 | /* SMB2 buffer freed by function above */ | |
2279 | ||
2280 | if (rc) { | |
2281 | cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE); | |
f96637be | 2282 | cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc); |
0822f514 PS |
2283 | } |
2284 | ||
2285 | return rc; | |
2286 | } |