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