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