]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/cifsd/smb2pdu.c
cifsd: add ksmbd/nfsd interoperability to feature table
[mirror_ubuntu-jammy-kernel.git] / fs / cifsd / smb2pdu.c
CommitLineData
e2f34481
NJ
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
5 */
6
7#include <linux/inetdevice.h>
8#include <net/addrconf.h>
9#include <linux/syscalls.h>
10#include <linux/namei.h>
11#include <linux/statfs.h>
12#include <linux/ethtool.h>
13
14#include "glob.h"
15#include "smb2pdu.h"
16#include "smbfsctl.h"
17#include "oplock.h"
18#include "smbacl.h"
19
20#include "auth.h"
21#include "asn1.h"
22#include "buffer_pool.h"
23#include "connection.h"
24#include "transport_ipc.h"
25#include "vfs.h"
26#include "vfs_cache.h"
27#include "misc.h"
28
e2f34481
NJ
29#include "server.h"
30#include "smb_common.h"
31#include "smbstatus.h"
32#include "ksmbd_work.h"
33#include "mgmt/user_config.h"
34#include "mgmt/share_config.h"
35#include "mgmt/tree_connect.h"
36#include "mgmt/user_session.h"
37#include "mgmt/ksmbd_ida.h"
38#include "ndr.h"
39
40static void __wbuf(struct ksmbd_work *work, void **req, void **rsp)
41{
42 if (work->next_smb2_rcv_hdr_off) {
43 *req = REQUEST_BUF_NEXT(work);
44 *rsp = RESPONSE_BUF_NEXT(work);
45 } else {
e5066499
NJ
46 *req = work->request_buf;
47 *rsp = work->response_buf;
e2f34481
NJ
48 }
49}
50
51#define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs))
52
53/**
54 * check_session_id() - check for valid session id in smb header
55 * @conn: connection instance
56 * @id: session id from smb header
57 *
58 * Return: 1 if valid session id, otherwise 0
59 */
64b39f4a 60static inline int check_session_id(struct ksmbd_conn *conn, u64 id)
e2f34481
NJ
61{
62 struct ksmbd_session *sess;
63
64 if (id == 0 || id == -1)
65 return 0;
66
67 sess = ksmbd_session_lookup(conn, id);
68 if (sess)
69 return 1;
70 ksmbd_err("Invalid user session id: %llu\n", id);
71 return 0;
72}
73
74struct channel *lookup_chann_list(struct ksmbd_session *sess)
75{
76 struct channel *chann;
77 struct list_head *t;
78
79 list_for_each(t, &sess->ksmbd_chann_list) {
80 chann = list_entry(t, struct channel, chann_list);
81 if (chann && chann->conn == sess->conn)
82 return chann;
83 }
84
85 return NULL;
86}
87
88/**
89 * smb2_get_ksmbd_tcon() - get tree connection information for a tree id
95fa1ce9 90 * @work: smb work
e2f34481
NJ
91 *
92 * Return: matching tree connection on success, otherwise error
93 */
94int smb2_get_ksmbd_tcon(struct ksmbd_work *work)
95{
e5066499 96 struct smb2_hdr *req_hdr = work->request_buf;
e2f34481
NJ
97 int tree_id;
98
99 work->tcon = NULL;
64b39f4a
NJ
100 if (work->conn->ops->get_cmd_val(work) == SMB2_TREE_CONNECT_HE ||
101 work->conn->ops->get_cmd_val(work) == SMB2_CANCEL_HE ||
102 work->conn->ops->get_cmd_val(work) == SMB2_LOGOFF_HE) {
e2f34481
NJ
103 ksmbd_debug(SMB, "skip to check tree connect request\n");
104 return 0;
105 }
106
02b68b20 107 if (xa_empty(&work->sess->tree_conns)) {
e2f34481
NJ
108 ksmbd_debug(SMB, "NO tree connected\n");
109 return -1;
110 }
111
112 tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId);
113 work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id);
114 if (!work->tcon) {
115 ksmbd_err("Invalid tid %d\n", tree_id);
116 return -1;
117 }
118
119 return 1;
120}
121
122/**
123 * smb2_set_err_rsp() - set error response code on smb response
124 * @work: smb work containing response buffer
125 */
126void smb2_set_err_rsp(struct ksmbd_work *work)
127{
128 struct smb2_err_rsp *err_rsp;
129
130 if (work->next_smb2_rcv_hdr_off)
131 err_rsp = RESPONSE_BUF_NEXT(work);
132 else
e5066499 133 err_rsp = work->response_buf;
e2f34481
NJ
134
135 if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) {
136 err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE;
137 err_rsp->ErrorContextCount = 0;
138 err_rsp->Reserved = 0;
139 err_rsp->ByteCount = 0;
140 err_rsp->ErrorData[0] = 0;
e5066499 141 inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2);
e2f34481
NJ
142 }
143}
144
145/**
146 * is_smb2_neg_cmd() - is it smb2 negotiation command
147 * @work: smb work containing smb header
148 *
149 * Return: 1 if smb2 negotiation command, otherwise 0
150 */
151int is_smb2_neg_cmd(struct ksmbd_work *work)
152{
e5066499 153 struct smb2_hdr *hdr = work->request_buf;
e2f34481
NJ
154
155 /* is it SMB2 header ? */
156 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
157 return 0;
158
159 /* make sure it is request not response message */
160 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
161 return 0;
162
163 if (hdr->Command != SMB2_NEGOTIATE)
164 return 0;
165
166 return 1;
167}
168
169/**
170 * is_smb2_rsp() - is it smb2 response
171 * @work: smb work containing smb response buffer
172 *
173 * Return: 1 if smb2 response, otherwise 0
174 */
175int is_smb2_rsp(struct ksmbd_work *work)
176{
e5066499 177 struct smb2_hdr *hdr = work->response_buf;
e2f34481
NJ
178
179 /* is it SMB2 header ? */
180 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
181 return 0;
182
183 /* make sure it is response not request message */
184 if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR))
185 return 0;
186
187 return 1;
188}
189
190/**
191 * get_smb2_cmd_val() - get smb command code from smb header
192 * @work: smb work containing smb request buffer
193 *
194 * Return: smb2 request command value
195 */
196uint16_t get_smb2_cmd_val(struct ksmbd_work *work)
197{
198 struct smb2_hdr *rcv_hdr;
199
200 if (work->next_smb2_rcv_hdr_off)
201 rcv_hdr = REQUEST_BUF_NEXT(work);
202 else
e5066499 203 rcv_hdr = work->request_buf;
e2f34481
NJ
204 return le16_to_cpu(rcv_hdr->Command);
205}
206
207/**
208 * set_smb2_rsp_status() - set error response code on smb2 header
209 * @work: smb work containing response buffer
95fa1ce9 210 * @err: error response code
e2f34481
NJ
211 */
212void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err)
213{
214 struct smb2_hdr *rsp_hdr;
215
216 if (work->next_smb2_rcv_hdr_off)
217 rsp_hdr = RESPONSE_BUF_NEXT(work);
218 else
e5066499 219 rsp_hdr = work->response_buf;
e2f34481
NJ
220 rsp_hdr->Status = err;
221 smb2_set_err_rsp(work);
222}
223
224/**
225 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command
226 * @work: smb work containing smb request buffer
227 *
228 * smb2 negotiate response is sent in reply of smb1 negotiate command for
229 * dialect auto-negotiation.
230 */
231int init_smb2_neg_rsp(struct ksmbd_work *work)
232{
233 struct smb2_hdr *rsp_hdr;
234 struct smb2_negotiate_rsp *rsp;
235 struct ksmbd_conn *conn = work->conn;
236
237 if (conn->need_neg == false)
238 return -EINVAL;
239 if (!(conn->dialect >= SMB20_PROT_ID &&
64b39f4a 240 conn->dialect <= SMB311_PROT_ID))
e2f34481
NJ
241 return -EINVAL;
242
e5066499 243 rsp_hdr = work->response_buf;
e2f34481
NJ
244
245 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
246
247 rsp_hdr->smb2_buf_length =
248 cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
249
250 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
251 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
252 rsp_hdr->CreditRequest = cpu_to_le16(2);
253 rsp_hdr->Command = SMB2_NEGOTIATE;
254 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
255 rsp_hdr->NextCommand = 0;
256 rsp_hdr->MessageId = 0;
257 rsp_hdr->Id.SyncId.ProcessId = 0;
258 rsp_hdr->Id.SyncId.TreeId = 0;
259 rsp_hdr->SessionId = 0;
260 memset(rsp_hdr->Signature, 0, 16);
261
e5066499 262 rsp = work->response_buf;
e2f34481
NJ
263
264 WARN_ON(ksmbd_conn_good(work));
265
266 rsp->StructureSize = cpu_to_le16(65);
267 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
268 rsp->DialectRevision = cpu_to_le16(conn->dialect);
269 /* Not setting conn guid rsp->ServerGUID, as it
270 * not used by client for identifying connection
271 */
272 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
273 /* Default Max Message Size till SMB2.0, 64K*/
274 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
275 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
276 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
277
278 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
279 rsp->ServerStartTime = 0;
280
281 rsp->SecurityBufferOffset = cpu_to_le16(128);
282 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
283 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
284 sizeof(rsp->hdr.smb2_buf_length)) +
285 le16_to_cpu(rsp->SecurityBufferOffset));
286 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
287 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
288 AUTH_GSS_LENGTH);
289 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
290 if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY)
291 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
292 conn->use_spnego = true;
293
294 ksmbd_conn_set_need_negotiate(work);
295 return 0;
296}
297
298static int smb2_consume_credit_charge(struct ksmbd_work *work,
299 unsigned short credit_charge)
300{
301 struct ksmbd_conn *conn = work->conn;
302 unsigned int rsp_credits = 1;
303
304 if (!conn->total_credits)
305 return 0;
306
307 if (credit_charge > 0)
308 rsp_credits = credit_charge;
309
310 conn->total_credits -= rsp_credits;
311 return rsp_credits;
312}
313
314/**
315 * smb2_set_rsp_credits() - set number of credits in response buffer
316 * @work: smb work containing smb response buffer
317 */
318int smb2_set_rsp_credits(struct ksmbd_work *work)
319{
320 struct smb2_hdr *req_hdr = REQUEST_BUF_NEXT(work);
321 struct smb2_hdr *hdr = RESPONSE_BUF_NEXT(work);
322 struct ksmbd_conn *conn = work->conn;
323 unsigned short credits_requested = le16_to_cpu(req_hdr->CreditRequest);
324 unsigned short credit_charge = 1, credits_granted = 0;
325 unsigned short aux_max, aux_credits, min_credits;
326 int rsp_credit_charge;
327
328 if (hdr->Command == SMB2_CANCEL)
329 goto out;
330
331 /* get default minimum credits by shifting maximum credits by 4 */
332 min_credits = conn->max_credits >> 4;
333
334 if (conn->total_credits >= conn->max_credits) {
335 ksmbd_err("Total credits overflow: %d\n", conn->total_credits);
336 conn->total_credits = min_credits;
337 }
338
339 rsp_credit_charge = smb2_consume_credit_charge(work,
340 le16_to_cpu(req_hdr->CreditCharge));
341 if (rsp_credit_charge < 0)
342 return -EINVAL;
343
344 hdr->CreditCharge = cpu_to_le16(rsp_credit_charge);
345
346 if (credits_requested > 0) {
347 aux_credits = credits_requested - 1;
348 aux_max = 32;
349 if (hdr->Command == SMB2_NEGOTIATE)
350 aux_max = 0;
351 aux_credits = (aux_credits < aux_max) ? aux_credits : aux_max;
352 credits_granted = aux_credits + credit_charge;
353
354 /* if credits granted per client is getting bigger than default
355 * minimum credits then we should wrap it up within the limits.
356 */
357 if ((conn->total_credits + credits_granted) > min_credits)
358 credits_granted = min_credits - conn->total_credits;
359 /*
360 * TODO: Need to adjuct CreditRequest value according to
361 * current cpu load
362 */
363 } else if (conn->total_credits == 0) {
364 credits_granted = 1;
365 }
366
367 conn->total_credits += credits_granted;
368 work->credits_granted += credits_granted;
369
370 if (!req_hdr->NextCommand) {
371 /* Update CreditRequest in last request */
372 hdr->CreditRequest = cpu_to_le16(work->credits_granted);
373 }
374out:
375 ksmbd_debug(SMB,
376 "credits: requested[%d] granted[%d] total_granted[%d]\n",
377 credits_requested, credits_granted,
378 conn->total_credits);
379 return 0;
380}
381
382/**
383 * init_chained_smb2_rsp() - initialize smb2 chained response
384 * @work: smb work containing smb response buffer
385 */
386static void init_chained_smb2_rsp(struct ksmbd_work *work)
387{
388 struct smb2_hdr *req = REQUEST_BUF_NEXT(work);
389 struct smb2_hdr *rsp = RESPONSE_BUF_NEXT(work);
390 struct smb2_hdr *rsp_hdr;
391 struct smb2_hdr *rcv_hdr;
392 int next_hdr_offset = 0;
393 int len, new_len;
394
395 /* Len of this response = updated RFC len - offset of previous cmd
396 * in the compound rsp
397 */
398
399 /* Storing the current local FID which may be needed by subsequent
400 * command in the compound request
401 */
402 if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) {
403 work->compound_fid =
404 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
405 VolatileFileId);
406 work->compound_pfid =
407 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
408 PersistentFileId);
409 work->compound_sid = le64_to_cpu(rsp->SessionId);
410 }
411
e5066499 412 len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off;
e2f34481
NJ
413 next_hdr_offset = le32_to_cpu(req->NextCommand);
414
415 new_len = ALIGN(len, 8);
e5066499 416 inc_rfc1001_len(work->response_buf, ((sizeof(struct smb2_hdr) - 4)
e2f34481
NJ
417 + new_len - len));
418 rsp->NextCommand = cpu_to_le32(new_len);
419
420 work->next_smb2_rcv_hdr_off += next_hdr_offset;
421 work->next_smb2_rsp_hdr_off += new_len;
422 ksmbd_debug(SMB,
423 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
424 new_len, work->next_smb2_rcv_hdr_off,
425 work->next_smb2_rsp_hdr_off);
426
427 rsp_hdr = RESPONSE_BUF_NEXT(work);
428 rcv_hdr = REQUEST_BUF_NEXT(work);
429
430 if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
431 ksmbd_debug(SMB, "related flag should be set\n");
432 work->compound_fid = KSMBD_NO_FID;
433 work->compound_pfid = KSMBD_NO_FID;
434 }
435 memset((char *)rsp_hdr + 4, 0, sizeof(struct smb2_hdr) + 2);
436 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
437 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
438 rsp_hdr->Command = rcv_hdr->Command;
439
440 /*
441 * Message is response. We don't grant oplock yet.
442 */
443 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR |
444 SMB2_FLAGS_RELATED_OPERATIONS);
445 rsp_hdr->NextCommand = 0;
446 rsp_hdr->MessageId = rcv_hdr->MessageId;
447 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
448 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
449 rsp_hdr->SessionId = rcv_hdr->SessionId;
450 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
451}
452
453/**
454 * is_chained_smb2_message() - check for chained command
455 * @work: smb work containing smb request buffer
456 *
457 * Return: true if chained request, otherwise false
458 */
459bool is_chained_smb2_message(struct ksmbd_work *work)
460{
e5066499 461 struct smb2_hdr *hdr = work->request_buf;
e2f34481
NJ
462 unsigned int len;
463
464 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
465 return false;
466
467 hdr = REQUEST_BUF_NEXT(work);
468 if (le32_to_cpu(hdr->NextCommand) > 0) {
469 ksmbd_debug(SMB, "got SMB2 chained command\n");
470 init_chained_smb2_rsp(work);
471 return true;
472 } else if (work->next_smb2_rcv_hdr_off) {
473 /*
474 * This is last request in chained command,
475 * align response to 8 byte
476 */
e5066499
NJ
477 len = ALIGN(get_rfc1002_len(work->response_buf), 8);
478 len = len - get_rfc1002_len(work->response_buf);
e2f34481
NJ
479 if (len) {
480 ksmbd_debug(SMB, "padding len %u\n", len);
e5066499
NJ
481 inc_rfc1001_len(work->response_buf, len);
482 if (work->aux_payload_sz)
e2f34481
NJ
483 work->aux_payload_sz += len;
484 }
485 }
486 return false;
487}
488
489/**
490 * init_smb2_rsp_hdr() - initialize smb2 response
491 * @work: smb work containing smb request buffer
492 *
493 * Return: 0
494 */
495int init_smb2_rsp_hdr(struct ksmbd_work *work)
496{
e5066499
NJ
497 struct smb2_hdr *rsp_hdr = work->response_buf;
498 struct smb2_hdr *rcv_hdr = work->request_buf;
e2f34481
NJ
499 struct ksmbd_conn *conn = work->conn;
500
501 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
502 rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
503 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
504 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
505 rsp_hdr->Command = rcv_hdr->Command;
506
507 /*
508 * Message is response. We don't grant oplock yet.
509 */
510 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
511 rsp_hdr->NextCommand = 0;
512 rsp_hdr->MessageId = rcv_hdr->MessageId;
513 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
514 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
515 rsp_hdr->SessionId = rcv_hdr->SessionId;
516 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
517
518 work->syncronous = true;
519 if (work->async_id) {
d40012a8 520 ksmbd_release_id(&conn->async_ida, work->async_id);
e2f34481
NJ
521 work->async_id = 0;
522 }
523
524 return 0;
525}
526
527/**
528 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
529 * @work: smb work containing smb request buffer
530 *
531 * Return: 0 on success, otherwise -ENOMEM
532 */
533int smb2_allocate_rsp_buf(struct ksmbd_work *work)
534{
e5066499 535 struct smb2_hdr *hdr = work->request_buf;
e2f34481
NJ
536 size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
537 size_t large_sz = work->conn->vals->max_trans_size + MAX_SMB2_HDR_SIZE;
538 size_t sz = small_sz;
539 int cmd = le16_to_cpu(hdr->Command);
540
541 if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE) {
542 sz = large_sz;
543 work->set_trans_buf = true;
544 }
545
546 if (cmd == SMB2_QUERY_INFO_HE) {
547 struct smb2_query_info_req *req;
548
e5066499 549 req = work->request_buf;
e2f34481 550 if (req->InfoType == SMB2_O_INFO_FILE &&
64b39f4a
NJ
551 (req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
552 req->FileInfoClass == FILE_ALL_INFORMATION)) {
e2f34481
NJ
553 sz = large_sz;
554 work->set_trans_buf = true;
555 }
556 }
557
558 /* allocate large response buf for chained commands */
559 if (le32_to_cpu(hdr->NextCommand) > 0)
560 sz = large_sz;
561
562 if (server_conf.flags & KSMBD_GLOBAL_FLAG_CACHE_TBUF &&
563 work->set_trans_buf)
564 work->response_buf = ksmbd_find_buffer(sz);
565 else
79f6b11a 566 work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
e2f34481 567
e5066499 568 if (!work->response_buf) {
e2f34481
NJ
569 ksmbd_err("Failed to allocate %zu bytes buffer\n", sz);
570 return -ENOMEM;
571 }
572
573 work->response_sz = sz;
574 return 0;
575}
576
577/**
578 * smb2_check_user_session() - check for valid session for a user
579 * @work: smb work containing smb request buffer
580 *
581 * Return: 0 on success, otherwise error
582 */
583int smb2_check_user_session(struct ksmbd_work *work)
584{
e5066499 585 struct smb2_hdr *req_hdr = work->request_buf;
e2f34481
NJ
586 struct ksmbd_conn *conn = work->conn;
587 unsigned int cmd = conn->ops->get_cmd_val(work);
588 unsigned long long sess_id;
589
590 work->sess = NULL;
591 /*
592 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
593 * require a session id, so no need to validate user session's for
594 * these commands.
595 */
596 if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE ||
64b39f4a 597 cmd == SMB2_SESSION_SETUP_HE)
e2f34481
NJ
598 return 0;
599
600 if (!ksmbd_conn_good(work))
601 return -EINVAL;
602
603 sess_id = le64_to_cpu(req_hdr->SessionId);
604 /* Check for validity of user session */
605 work->sess = ksmbd_session_lookup(conn, sess_id);
606 if (work->sess)
607 return 1;
608 ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id);
609 return -EINVAL;
610}
611
64b39f4a 612static void destroy_previous_session(struct ksmbd_user *user, u64 id)
e2f34481
NJ
613{
614 struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id);
615 struct ksmbd_user *prev_user;
616
617 if (!prev_sess)
618 return;
619
620 prev_user = prev_sess->user;
621
622 if (strcmp(user->name, prev_user->name) ||
623 user->passkey_sz != prev_user->passkey_sz ||
624 memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) {
625 put_session(prev_sess);
626 return;
627 }
628
629 put_session(prev_sess);
630 ksmbd_session_destroy(prev_sess);
631}
632
633/**
634 * smb2_get_name() - get filename string from on the wire smb format
95fa1ce9 635 * @share: ksmbd_share_config pointer
e2f34481
NJ
636 * @src: source buffer
637 * @maxlen: maxlen of source string
95fa1ce9 638 * @nls_table: nls_table pointer
e2f34481
NJ
639 *
640 * Return: matching converted filename on success, otherwise error ptr
641 */
642static char *
64b39f4a
NJ
643smb2_get_name(struct ksmbd_share_config *share, const char *src,
644 const int maxlen, struct nls_table *local_nls)
e2f34481
NJ
645{
646 char *name, *unixname;
647
64b39f4a 648 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls);
e2f34481
NJ
649 if (IS_ERR(name)) {
650 ksmbd_err("failed to get name %ld\n", PTR_ERR(name));
651 return name;
652 }
653
654 /* change it to absolute unix name */
655 ksmbd_conv_path_to_unix(name);
656 ksmbd_strip_last_slash(name);
657
658 unixname = convert_to_unix_name(share, name);
659 kfree(name);
660 if (!unixname) {
661 ksmbd_err("can not convert absolute name\n");
662 return ERR_PTR(-ENOMEM);
663 }
664
665 ksmbd_debug(SMB, "absolute name = %s\n", unixname);
666 return unixname;
667}
668
e2f34481
NJ
669int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
670{
671 struct smb2_hdr *rsp_hdr;
672 struct ksmbd_conn *conn = work->conn;
673 int id;
674
e5066499 675 rsp_hdr = work->response_buf;
e2f34481
NJ
676 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
677
d40012a8 678 id = ksmbd_acquire_async_msg_id(&conn->async_ida);
e2f34481
NJ
679 if (id < 0) {
680 ksmbd_err("Failed to alloc async message id\n");
681 return id;
682 }
683 work->syncronous = false;
684 work->async_id = id;
685 rsp_hdr->Id.AsyncId = cpu_to_le64(id);
686
687 ksmbd_debug(SMB,
688 "Send interim Response to inform async request id : %d\n",
689 work->async_id);
690
691 work->cancel_fn = fn;
692 work->cancel_argv = arg;
693
694 spin_lock(&conn->request_lock);
695 list_add_tail(&work->async_request_entry, &conn->async_requests);
696 spin_unlock(&conn->request_lock);
697
698 return 0;
699}
700
701void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
702{
703 struct smb2_hdr *rsp_hdr;
704
e5066499 705 rsp_hdr = work->response_buf;
e2f34481
NJ
706 smb2_set_err_rsp(work);
707 rsp_hdr->Status = status;
708
709 work->multiRsp = 1;
710 ksmbd_conn_write(work);
711 rsp_hdr->Status = 0;
712 work->multiRsp = 0;
713}
714
715static __le32 smb2_get_reparse_tag_special_file(umode_t mode)
716{
717 if (S_ISDIR(mode) || S_ISREG(mode))
718 return 0;
719
720 if (S_ISLNK(mode))
721 return IO_REPARSE_TAG_LX_SYMLINK_LE;
722 else if (S_ISFIFO(mode))
723 return IO_REPARSE_TAG_LX_FIFO_LE;
724 else if (S_ISSOCK(mode))
725 return IO_REPARSE_TAG_AF_UNIX_LE;
726 else if (S_ISCHR(mode))
727 return IO_REPARSE_TAG_LX_CHR_LE;
728 else if (S_ISBLK(mode))
729 return IO_REPARSE_TAG_LX_BLK_LE;
730
731 return 0;
732}
733
734/**
735 * smb2_get_dos_mode() - get file mode in dos format from unix mode
736 * @stat: kstat containing file mode
95fa1ce9 737 * @attribute: attribute flags
e2f34481
NJ
738 *
739 * Return: converted dos mode
740 */
741static int smb2_get_dos_mode(struct kstat *stat, int attribute)
742{
743 int attr = 0;
744
64b39f4a 745 if (S_ISDIR(stat->mode)) {
e2f34481
NJ
746 attr = ATTR_DIRECTORY |
747 (attribute & (ATTR_HIDDEN | ATTR_SYSTEM));
64b39f4a 748 } else {
e2f34481
NJ
749 attr = (attribute & 0x00005137) | ATTR_ARCHIVE;
750 attr &= ~(ATTR_DIRECTORY);
751 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
752 FILE_SUPPORTS_SPARSE_FILES))
753 attr |= ATTR_SPARSE;
754
755 if (smb2_get_reparse_tag_special_file(stat->mode))
756 attr |= ATTR_REPARSE;
757 }
758
759 return attr;
760}
761
762static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
64b39f4a 763 __le16 hash_id)
e2f34481
NJ
764{
765 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
766 pneg_ctxt->DataLength = cpu_to_le16(38);
767 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
768 pneg_ctxt->Reserved = cpu_to_le32(0);
769 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
770 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
771 pneg_ctxt->HashAlgorithms = hash_id;
772}
773
774static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
64b39f4a 775 __le16 cipher_type)
e2f34481
NJ
776{
777 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
778 pneg_ctxt->DataLength = cpu_to_le16(4);
779 pneg_ctxt->Reserved = cpu_to_le32(0);
780 pneg_ctxt->CipherCount = cpu_to_le16(1);
781 pneg_ctxt->Ciphers[0] = cipher_type;
782}
783
784static void build_compression_ctxt(struct smb2_compression_ctx *pneg_ctxt,
64b39f4a 785 __le16 comp_algo)
e2f34481
NJ
786{
787 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
788 pneg_ctxt->DataLength =
789 cpu_to_le16(sizeof(struct smb2_compression_ctx)
790 - sizeof(struct smb2_neg_context));
791 pneg_ctxt->Reserved = cpu_to_le32(0);
792 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1);
793 pneg_ctxt->Reserved1 = cpu_to_le32(0);
794 pneg_ctxt->CompressionAlgorithms[0] = comp_algo;
795}
796
64b39f4a 797static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
e2f34481
NJ
798{
799 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
800 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
801 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
802 pneg_ctxt->Name[0] = 0x93;
803 pneg_ctxt->Name[1] = 0xAD;
804 pneg_ctxt->Name[2] = 0x25;
805 pneg_ctxt->Name[3] = 0x50;
806 pneg_ctxt->Name[4] = 0x9C;
807 pneg_ctxt->Name[5] = 0xB4;
808 pneg_ctxt->Name[6] = 0x11;
809 pneg_ctxt->Name[7] = 0xE7;
810 pneg_ctxt->Name[8] = 0xB4;
811 pneg_ctxt->Name[9] = 0x23;
812 pneg_ctxt->Name[10] = 0x83;
813 pneg_ctxt->Name[11] = 0xDE;
814 pneg_ctxt->Name[12] = 0x96;
815 pneg_ctxt->Name[13] = 0x8B;
816 pneg_ctxt->Name[14] = 0xCD;
817 pneg_ctxt->Name[15] = 0x7C;
818}
819
64b39f4a
NJ
820static void assemble_neg_contexts(struct ksmbd_conn *conn,
821 struct smb2_negotiate_rsp *rsp)
e2f34481
NJ
822{
823 /* +4 is to account for the RFC1001 len field */
824 char *pneg_ctxt = (char *)rsp +
825 le32_to_cpu(rsp->NegotiateContextOffset) + 4;
826 int neg_ctxt_cnt = 1;
827 int ctxt_size;
828
829 ksmbd_debug(SMB,
830 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
831 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
832 conn->preauth_info->Preauth_HashId);
833 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt);
834 inc_rfc1001_len(rsp, AUTH_GSS_PADDING);
835 ctxt_size = sizeof(struct smb2_preauth_neg_context);
836 /* Round to 8 byte boundary */
837 pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8);
838
839 if (conn->cipher_type) {
840 ctxt_size = round_up(ctxt_size, 8);
841 ksmbd_debug(SMB,
842 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
64b39f4a 843 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt,
e2f34481
NJ
844 conn->cipher_type);
845 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
846 ctxt_size += sizeof(struct smb2_encryption_neg_context);
847 /* Round to 8 byte boundary */
848 pneg_ctxt +=
849 round_up(sizeof(struct smb2_encryption_neg_context),
850 8);
851 }
852
853 if (conn->compress_algorithm) {
854 ctxt_size = round_up(ctxt_size, 8);
855 ksmbd_debug(SMB,
856 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
857 /* Temporarily set to SMB3_COMPRESS_NONE */
858 build_compression_ctxt((struct smb2_compression_ctx *)pneg_ctxt,
859 conn->compress_algorithm);
860 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
861 ctxt_size += sizeof(struct smb2_compression_ctx);
862 /* Round to 8 byte boundary */
863 pneg_ctxt += round_up(sizeof(struct smb2_compression_ctx), 8);
864 }
865
866 if (conn->posix_ext_supported) {
867 ctxt_size = round_up(ctxt_size, 8);
868 ksmbd_debug(SMB,
869 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
870 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
871 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
872 ctxt_size += sizeof(struct smb2_posix_neg_context);
873 }
874
875 inc_rfc1001_len(rsp, ctxt_size);
876}
877
64b39f4a
NJ
878static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
879 struct smb2_preauth_neg_context *pneg_ctxt)
e2f34481
NJ
880{
881 __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
882
883 if (pneg_ctxt->HashAlgorithms ==
884 SMB2_PREAUTH_INTEGRITY_SHA512) {
885 conn->preauth_info->Preauth_HashId =
886 SMB2_PREAUTH_INTEGRITY_SHA512;
887 err = STATUS_SUCCESS;
888 }
889
890 return err;
891}
892
893static int decode_encrypt_ctxt(struct ksmbd_conn *conn,
64b39f4a 894 struct smb2_encryption_neg_context *pneg_ctxt)
e2f34481
NJ
895{
896 int i;
897 int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount);
898
899 conn->cipher_type = 0;
900
901 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION))
902 goto out;
903
904 for (i = 0; i < cph_cnt; i++) {
905 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM ||
64b39f4a 906 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM) {
e2f34481
NJ
907 ksmbd_debug(SMB, "Cipher ID = 0x%x\n",
908 pneg_ctxt->Ciphers[i]);
909 conn->cipher_type = pneg_ctxt->Ciphers[i];
910 break;
911 }
912 }
913
914out:
915 /*
916 * Return encrypt context size in request.
917 * So need to plus extra number of ciphers size.
918 */
919 return sizeof(struct smb2_encryption_neg_context) +
920 ((cph_cnt - 1) * 2);
921}
922
923static int decode_compress_ctxt(struct ksmbd_conn *conn,
64b39f4a 924 struct smb2_compression_ctx *pneg_ctxt)
e2f34481
NJ
925{
926 int algo_cnt = le16_to_cpu(pneg_ctxt->CompressionAlgorithmCount);
927
928 conn->compress_algorithm = SMB3_COMPRESS_NONE;
929
930 /*
931 * Return compression context size in request.
932 * So need to plus extra number of CompressionAlgorithms size.
933 */
934 return sizeof(struct smb2_encryption_neg_context) +
935 ((algo_cnt - 1) * 2);
936}
937
938static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
64b39f4a 939 struct smb2_negotiate_req *req)
e2f34481
NJ
940{
941 int i = 0;
942 __le32 status = 0;
943 /* +4 is to account for the RFC1001 len field */
944 char *pneg_ctxt = (char *)req +
945 le32_to_cpu(req->NegotiateContextOffset) + 4;
946 __le16 *ContextType = (__le16 *)pneg_ctxt;
947 int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
948 int ctxt_size;
949
950 ksmbd_debug(SMB, "negotiate context count = %d\n", neg_ctxt_cnt);
951 status = STATUS_INVALID_PARAMETER;
952 while (i++ < neg_ctxt_cnt) {
953 if (*ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
954 ksmbd_debug(SMB,
955 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
956 if (conn->preauth_info->Preauth_HashId)
957 break;
958
959 status = decode_preauth_ctxt(conn,
960 (struct smb2_preauth_neg_context *)pneg_ctxt);
64b39f4a 961 pneg_ctxt += DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
e2f34481
NJ
962 } else if (*ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
963 ksmbd_debug(SMB,
964 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
965 if (conn->cipher_type)
966 break;
967
968 ctxt_size = decode_encrypt_ctxt(conn,
64b39f4a 969 (struct smb2_encryption_neg_context *)pneg_ctxt);
e2f34481
NJ
970 pneg_ctxt += DIV_ROUND_UP(ctxt_size, 8) * 8;
971 } else if (*ContextType == SMB2_COMPRESSION_CAPABILITIES) {
972 ksmbd_debug(SMB,
973 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
974 if (conn->compress_algorithm)
975 break;
976
977 ctxt_size = decode_compress_ctxt(conn,
64b39f4a 978 (struct smb2_compression_ctx *) pneg_ctxt);
e2f34481
NJ
979 pneg_ctxt += DIV_ROUND_UP(ctxt_size, 8) * 8;
980 } else if (*ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) {
981 ksmbd_debug(SMB,
982 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
983 ctxt_size = sizeof(struct smb2_netname_neg_context);
64b39f4a 984 ctxt_size += DIV_ROUND_UP(le16_to_cpu(((struct smb2_netname_neg_context *)
e2f34481
NJ
985 pneg_ctxt)->DataLength), 8) * 8;
986 pneg_ctxt += ctxt_size;
987 } else if (*ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) {
988 ksmbd_debug(SMB,
989 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
990 conn->posix_ext_supported = true;
64b39f4a 991 pneg_ctxt += DIV_ROUND_UP(sizeof(struct smb2_posix_neg_context), 8) * 8;
e2f34481
NJ
992 }
993 ContextType = (__le16 *)pneg_ctxt;
994
995 if (status != STATUS_SUCCESS)
996 break;
997 }
998 return status;
999}
1000
1001/**
1002 * smb2_handle_negotiate() - handler for smb2 negotiate command
1003 * @work: smb work containing smb request buffer
1004 *
1005 * Return: 0
1006 */
1007int smb2_handle_negotiate(struct ksmbd_work *work)
1008{
1009 struct ksmbd_conn *conn = work->conn;
e5066499
NJ
1010 struct smb2_negotiate_req *req = work->request_buf;
1011 struct smb2_negotiate_rsp *rsp = work->response_buf;
e2f34481
NJ
1012 int rc = 0;
1013 __le32 status;
1014
1015 ksmbd_debug(SMB, "Received negotiate request\n");
1016 conn->need_neg = false;
1017 if (ksmbd_conn_good(work)) {
1018 ksmbd_err("conn->tcp_status is already in CifsGood State\n");
1019 work->send_no_response = 1;
1020 return rc;
1021 }
1022
1023 if (req->DialectCount == 0) {
1024 ksmbd_err("malformed packet\n");
1025 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1026 rc = -EINVAL;
1027 goto err_out;
1028 }
1029
1030 conn->cli_cap = le32_to_cpu(req->Capabilities);
1031 switch (conn->dialect) {
1032 case SMB311_PROT_ID:
1033 conn->preauth_info =
1034 kzalloc(sizeof(struct preauth_integrity_info),
1035 GFP_KERNEL);
1036 if (!conn->preauth_info) {
1037 rc = -ENOMEM;
1038 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1039 goto err_out;
1040 }
1041
1042 status = deassemble_neg_contexts(conn, req);
1043 if (status != STATUS_SUCCESS) {
1044 ksmbd_err("deassemble_neg_contexts error(0x%x)\n",
1045 status);
1046 rsp->hdr.Status = status;
1047 rc = -EINVAL;
1048 goto err_out;
1049 }
1050
1051 rc = init_smb3_11_server(conn);
1052 if (rc < 0) {
1053 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1054 goto err_out;
1055 }
1056
1057 ksmbd_gen_preauth_integrity_hash(conn,
e5066499 1058 work->request_buf,
e2f34481
NJ
1059 conn->preauth_info->Preauth_HashValue);
1060 rsp->NegotiateContextOffset =
1061 cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
1062 assemble_neg_contexts(conn, rsp);
1063 break;
1064 case SMB302_PROT_ID:
1065 init_smb3_02_server(conn);
1066 break;
1067 case SMB30_PROT_ID:
1068 init_smb3_0_server(conn);
1069 break;
1070 case SMB21_PROT_ID:
1071 init_smb2_1_server(conn);
1072 break;
1073 case SMB20_PROT_ID:
1074 rc = init_smb2_0_server(conn);
1075 if (rc) {
1076 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1077 goto err_out;
1078 }
1079 break;
1080 case SMB2X_PROT_ID:
1081 case BAD_PROT_ID:
1082 default:
1083 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n",
1084 conn->dialect);
1085 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1086 rc = -EINVAL;
1087 goto err_out;
1088 }
1089 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
1090
1091 /* For stats */
1092 conn->connection_type = conn->dialect;
1093
1094 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
1095 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
1096 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
1097
1098 if (conn->dialect > SMB20_PROT_ID) {
1099 memcpy(conn->ClientGUID, req->ClientGUID,
1100 SMB2_CLIENT_GUID_SIZE);
1101 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
1102 }
1103
1104 rsp->StructureSize = cpu_to_le16(65);
1105 rsp->DialectRevision = cpu_to_le16(conn->dialect);
1106 /* Not setting conn guid rsp->ServerGUID, as it
1107 * not used by client for identifying server
1108 */
1109 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
1110
1111 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
1112 rsp->ServerStartTime = 0;
1113 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n",
1114 le32_to_cpu(rsp->NegotiateContextOffset),
1115 le16_to_cpu(rsp->NegotiateContextCount));
1116
1117 rsp->SecurityBufferOffset = cpu_to_le16(128);
1118 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
1119 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
1120 sizeof(rsp->hdr.smb2_buf_length)) +
1121 le16_to_cpu(rsp->SecurityBufferOffset));
1122 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
1123 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
1124 AUTH_GSS_LENGTH);
1125 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
1126 conn->use_spnego = true;
1127
1128 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
64b39f4a
NJ
1129 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) &&
1130 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE)
e2f34481
NJ
1131 conn->sign = true;
1132 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
1133 server_conf.enforced_signing = true;
1134 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
1135 conn->sign = true;
1136 }
1137
1138 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode);
1139 ksmbd_conn_set_need_negotiate(work);
1140
1141err_out:
1142 if (rc < 0)
1143 smb2_set_err_rsp(work);
1144
1145 return rc;
1146}
1147
1148static int alloc_preauth_hash(struct ksmbd_session *sess,
64b39f4a 1149 struct ksmbd_conn *conn)
e2f34481
NJ
1150{
1151 if (sess->Preauth_HashValue)
1152 return 0;
1153
86f52978 1154 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue,
1155 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL);
e2f34481
NJ
1156 if (!sess->Preauth_HashValue)
1157 return -ENOMEM;
1158
e2f34481
NJ
1159 return 0;
1160}
1161
1162static int generate_preauth_hash(struct ksmbd_work *work)
1163{
1164 struct ksmbd_conn *conn = work->conn;
1165 struct ksmbd_session *sess = work->sess;
1166
1167 if (conn->dialect != SMB311_PROT_ID)
1168 return 0;
1169
1170 if (!sess->Preauth_HashValue) {
1171 if (alloc_preauth_hash(sess, conn))
1172 return -ENOMEM;
1173 }
1174
1175 ksmbd_gen_preauth_integrity_hash(conn,
e5066499 1176 work->request_buf,
e2f34481
NJ
1177 sess->Preauth_HashValue);
1178 return 0;
1179}
1180
1181static int decode_negotiation_token(struct ksmbd_work *work,
64b39f4a 1182 struct negotiate_message *negblob)
e2f34481
NJ
1183{
1184 struct ksmbd_conn *conn = work->conn;
1185 struct smb2_sess_setup_req *req;
1186 int sz;
1187
1188 if (!conn->use_spnego)
1189 return -EINVAL;
1190
e5066499 1191 req = work->request_buf;
e2f34481
NJ
1192 sz = le16_to_cpu(req->SecurityBufferLength);
1193
1194 if (!ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) {
1195 if (!ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) {
1196 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP;
1197 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP;
1198 conn->use_spnego = false;
1199 }
1200 }
1201 return 0;
1202}
1203
1204static int ntlm_negotiate(struct ksmbd_work *work,
64b39f4a 1205 struct negotiate_message *negblob)
e2f34481 1206{
e5066499
NJ
1207 struct smb2_sess_setup_req *req = work->request_buf;
1208 struct smb2_sess_setup_rsp *rsp = work->response_buf;
e2f34481
NJ
1209 struct challenge_message *chgblob;
1210 unsigned char *spnego_blob = NULL;
1211 u16 spnego_blob_len;
1212 char *neg_blob;
1213 int sz, rc;
1214
1215 ksmbd_debug(SMB, "negotiate phase\n");
1216 sz = le16_to_cpu(req->SecurityBufferLength);
1217 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, sz, work->sess);
1218 if (rc)
1219 return rc;
1220
1221 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1222 chgblob =
1223 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);
1224 memset(chgblob, 0, sizeof(struct challenge_message));
1225
1226 if (!work->conn->use_spnego) {
1227 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1228 if (sz < 0)
1229 return -ENOMEM;
1230
1231 rsp->SecurityBufferLength = cpu_to_le16(sz);
1232 return 0;
1233 }
1234
1235 sz = sizeof(struct challenge_message);
1236 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1237
1238 neg_blob = kzalloc(sz, GFP_KERNEL);
1239 if (!neg_blob)
1240 return -ENOMEM;
1241
1242 chgblob = (struct challenge_message *)neg_blob;
1243 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1244 if (sz < 0) {
1245 rc = -ENOMEM;
1246 goto out;
1247 }
1248
1249 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob,
1250 &spnego_blob_len,
1251 neg_blob,
1252 sz);
1253 if (rc) {
1254 rc = -ENOMEM;
1255 goto out;
1256 }
1257
1258 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1259 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1260 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1261
1262out:
1263 kfree(spnego_blob);
1264 kfree(neg_blob);
1265 return rc;
1266}
1267
1268static struct authenticate_message *user_authblob(struct ksmbd_conn *conn,
64b39f4a 1269 struct smb2_sess_setup_req *req)
e2f34481
NJ
1270{
1271 int sz;
1272
1273 if (conn->use_spnego && conn->mechToken)
1274 return (struct authenticate_message *)conn->mechToken;
1275
1276 sz = le16_to_cpu(req->SecurityBufferOffset);
1277 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId
1278 + sz);
1279}
1280
1281static struct ksmbd_user *session_user(struct ksmbd_conn *conn,
64b39f4a 1282 struct smb2_sess_setup_req *req)
e2f34481
NJ
1283{
1284 struct authenticate_message *authblob;
1285 struct ksmbd_user *user;
1286 char *name;
1287 int sz;
1288
1289 authblob = user_authblob(conn, req);
1290 sz = le32_to_cpu(authblob->UserName.BufferOffset);
1291 name = smb_strndup_from_utf16((const char *)authblob + sz,
1292 le16_to_cpu(authblob->UserName.Length),
1293 true,
1294 conn->local_nls);
1295 if (IS_ERR(name)) {
1296 ksmbd_err("cannot allocate memory\n");
1297 return NULL;
1298 }
1299
1300 ksmbd_debug(SMB, "session setup request for user %s\n", name);
1301 user = ksmbd_login_user(name);
1302 kfree(name);
1303 return user;
1304}
1305
1306static int ntlm_authenticate(struct ksmbd_work *work)
1307{
e5066499
NJ
1308 struct smb2_sess_setup_req *req = work->request_buf;
1309 struct smb2_sess_setup_rsp *rsp = work->response_buf;
e2f34481
NJ
1310 struct ksmbd_conn *conn = work->conn;
1311 struct ksmbd_session *sess = work->sess;
1312 struct channel *chann = NULL;
1313 struct ksmbd_user *user;
64b39f4a 1314 u64 prev_id;
e2f34481
NJ
1315 int sz, rc;
1316
1317 ksmbd_debug(SMB, "authenticate phase\n");
1318 if (conn->use_spnego) {
1319 unsigned char *spnego_blob;
1320 u16 spnego_blob_len;
1321
1322 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob,
1323 &spnego_blob_len,
1324 0);
1325 if (rc)
1326 return -ENOMEM;
1327
1328 sz = le16_to_cpu(rsp->SecurityBufferOffset);
64b39f4a 1329 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
e2f34481
NJ
1330 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1331 kfree(spnego_blob);
1332 inc_rfc1001_len(rsp, spnego_blob_len - 1);
1333 }
1334
1335 user = session_user(conn, req);
1336 if (!user) {
1337 ksmbd_debug(SMB, "Unknown user name or an error\n");
1338 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1339 return -EINVAL;
1340 }
1341
1342 /* Check for previous session */
1343 prev_id = le64_to_cpu(req->PreviousSessionId);
1344 if (prev_id && prev_id != sess->id)
1345 destroy_previous_session(user, prev_id);
1346
1347 if (sess->state == SMB2_SESSION_VALID) {
1348 /*
1349 * Reuse session if anonymous try to connect
1350 * on reauthetication.
1351 */
1352 if (ksmbd_anonymous_user(user)) {
1353 ksmbd_free_user(user);
1354 return 0;
1355 }
1356 ksmbd_free_user(sess->user);
1357 }
1358
1359 sess->user = user;
1360 if (user_guest(sess->user)) {
1361 if (conn->sign) {
64b39f4a 1362 ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n");
e2f34481
NJ
1363 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1364 return -EACCES;
1365 }
1366
1367 rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE;
1368 } else {
1369 struct authenticate_message *authblob;
1370
1371 authblob = user_authblob(conn, req);
1372 sz = le16_to_cpu(req->SecurityBufferLength);
1373 rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess);
1374 if (rc) {
1375 set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD);
1376 ksmbd_debug(SMB, "authentication failed\n");
1377 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1378 return -EINVAL;
1379 }
1380
1381 /*
1382 * If session state is SMB2_SESSION_VALID, We can assume
1383 * that it is reauthentication. And the user/password
1384 * has been verified, so return it here.
1385 */
1386 if (sess->state == SMB2_SESSION_VALID)
1387 return 0;
1388
1389 if ((conn->sign || server_conf.enforced_signing) ||
64b39f4a 1390 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
e2f34481
NJ
1391 sess->sign = true;
1392
1393 if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION &&
64b39f4a 1394 conn->ops->generate_encryptionkey) {
e2f34481
NJ
1395 rc = conn->ops->generate_encryptionkey(sess);
1396 if (rc) {
1397 ksmbd_debug(SMB,
1398 "SMB3 encryption key generation failed\n");
1399 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1400 return rc;
1401 }
1402 sess->enc = true;
1403 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1404 /*
1405 * signing is disable if encryption is enable
1406 * on this session
1407 */
1408 sess->sign = false;
1409 }
1410 }
1411
1412 if (conn->dialect >= SMB30_PROT_ID) {
1413 chann = lookup_chann_list(sess);
1414 if (!chann) {
1415 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1416 if (!chann)
1417 return -ENOMEM;
1418
1419 chann->conn = conn;
1420 INIT_LIST_HEAD(&chann->chann_list);
1421 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1422 }
1423 }
1424
1425 if (conn->ops->generate_signingkey) {
1426 rc = conn->ops->generate_signingkey(sess);
1427 if (rc) {
64b39f4a 1428 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
e2f34481
NJ
1429 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1430 return rc;
1431 }
1432 }
1433
1434 if (conn->dialect > SMB20_PROT_ID) {
1435 if (!ksmbd_conn_lookup_dialect(conn)) {
1436 ksmbd_err("fail to verify the dialect\n");
1437 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1438 return -EPERM;
1439 }
1440 }
1441 return 0;
1442}
1443
1444#ifdef CONFIG_SMB_SERVER_KERBEROS5
1445static int krb5_authenticate(struct ksmbd_work *work)
1446{
e5066499
NJ
1447 struct smb2_sess_setup_req *req = work->request_buf;
1448 struct smb2_sess_setup_rsp *rsp = work->response_buf;
e2f34481
NJ
1449 struct ksmbd_conn *conn = work->conn;
1450 struct ksmbd_session *sess = work->sess;
1451 char *in_blob, *out_blob;
1452 struct channel *chann = NULL;
64b39f4a 1453 u64 prev_sess_id;
e2f34481
NJ
1454 int in_len, out_len;
1455 int retval;
1456
1457 in_blob = (char *)&req->hdr.ProtocolId +
1458 le16_to_cpu(req->SecurityBufferOffset);
1459 in_len = le16_to_cpu(req->SecurityBufferLength);
1460 out_blob = (char *)&rsp->hdr.ProtocolId +
1461 le16_to_cpu(rsp->SecurityBufferOffset);
1462 out_len = work->response_sz -
1463 offsetof(struct smb2_hdr, smb2_buf_length) -
1464 le16_to_cpu(rsp->SecurityBufferOffset);
1465
1466 /* Check previous session */
1467 prev_sess_id = le64_to_cpu(req->PreviousSessionId);
1468 if (prev_sess_id && prev_sess_id != sess->id)
1469 destroy_previous_session(sess->user, prev_sess_id);
1470
1471 if (sess->state == SMB2_SESSION_VALID)
1472 ksmbd_free_user(sess->user);
1473
1474 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len,
1475 out_blob, &out_len);
1476 if (retval) {
1477 ksmbd_debug(SMB, "krb5 authentication failed\n");
1478 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1479 return retval;
1480 }
1481 rsp->SecurityBufferLength = cpu_to_le16(out_len);
1482 inc_rfc1001_len(rsp, out_len - 1);
1483
1484 if ((conn->sign || server_conf.enforced_signing) ||
64b39f4a 1485 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
e2f34481
NJ
1486 sess->sign = true;
1487
1488 if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) &&
64b39f4a 1489 conn->ops->generate_encryptionkey) {
e2f34481
NJ
1490 retval = conn->ops->generate_encryptionkey(sess);
1491 if (retval) {
1492 ksmbd_debug(SMB,
1493 "SMB3 encryption key generation failed\n");
1494 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1495 return retval;
1496 }
1497 sess->enc = true;
1498 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1499 sess->sign = false;
1500 }
1501
1502 if (conn->dialect >= SMB30_PROT_ID) {
1503 chann = lookup_chann_list(sess);
1504 if (!chann) {
1505 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1506 if (!chann)
1507 return -ENOMEM;
1508
1509 chann->conn = conn;
1510 INIT_LIST_HEAD(&chann->chann_list);
1511 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1512 }
1513 }
1514
1515 if (conn->ops->generate_signingkey) {
1516 retval = conn->ops->generate_signingkey(sess);
1517 if (retval) {
64b39f4a 1518 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
e2f34481
NJ
1519 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1520 return retval;
1521 }
1522 }
1523
1524 if (conn->dialect > SMB20_PROT_ID) {
1525 if (!ksmbd_conn_lookup_dialect(conn)) {
1526 ksmbd_err("fail to verify the dialect\n");
1527 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1528 return -EPERM;
1529 }
1530 }
1531 return 0;
1532}
1533#else
1534static int krb5_authenticate(struct ksmbd_work *work)
1535{
1536 return -EOPNOTSUPP;
1537}
1538#endif
1539
1540int smb2_sess_setup(struct ksmbd_work *work)
1541{
1542 struct ksmbd_conn *conn = work->conn;
e5066499
NJ
1543 struct smb2_sess_setup_req *req = work->request_buf;
1544 struct smb2_sess_setup_rsp *rsp = work->response_buf;
e2f34481
NJ
1545 struct ksmbd_session *sess;
1546 struct negotiate_message *negblob;
1547 int rc = 0;
1548
1549 ksmbd_debug(SMB, "Received request for session setup\n");
1550
1551 rsp->StructureSize = cpu_to_le16(9);
1552 rsp->SessionFlags = 0;
1553 rsp->SecurityBufferOffset = cpu_to_le16(72);
1554 rsp->SecurityBufferLength = 0;
1555 inc_rfc1001_len(rsp, 9);
1556
1557 if (!req->hdr.SessionId) {
1558 sess = ksmbd_smb2_session_create();
1559 if (!sess) {
1560 rc = -ENOMEM;
1561 goto out_err;
1562 }
1563 rsp->hdr.SessionId = cpu_to_le64(sess->id);
1564 ksmbd_session_register(conn, sess);
1565 } else {
1566 sess = ksmbd_session_lookup(conn,
1567 le64_to_cpu(req->hdr.SessionId));
1568 if (!sess) {
1569 rc = -ENOENT;
1570 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1571 goto out_err;
1572 }
1573 }
1574 work->sess = sess;
1575
1576 if (sess->state == SMB2_SESSION_EXPIRED)
1577 sess->state = SMB2_SESSION_IN_PROGRESS;
1578
1579 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1580 le16_to_cpu(req->SecurityBufferOffset));
1581
1582 if (decode_negotiation_token(work, negblob) == 0) {
1583 if (conn->mechToken)
1584 negblob = (struct negotiate_message *)conn->mechToken;
1585 }
1586
1587 if (server_conf.auth_mechs & conn->auth_mechs) {
1588 if (conn->preferred_auth_mech &
1589 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
1590 rc = generate_preauth_hash(work);
1591 if (rc)
1592 goto out_err;
1593
1594 rc = krb5_authenticate(work);
1595 if (rc) {
1596 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1597 goto out_err;
1598 }
1599
1600 ksmbd_conn_set_good(work);
1601 sess->state = SMB2_SESSION_VALID;
822bc8ea 1602 kfree(sess->Preauth_HashValue);
e2f34481
NJ
1603 sess->Preauth_HashValue = NULL;
1604 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
1605 rc = generate_preauth_hash(work);
1606 if (rc)
1607 goto out_err;
1608
1609 if (negblob->MessageType == NtLmNegotiate) {
1610 rc = ntlm_negotiate(work, negblob);
1611 if (rc)
1612 goto out_err;
1613 rsp->hdr.Status =
1614 STATUS_MORE_PROCESSING_REQUIRED;
1615 /*
1616 * Note: here total size -1 is done as an
1617 * adjustment for 0 size blob
1618 */
64b39f4a 1619 inc_rfc1001_len(rsp, le16_to_cpu(rsp->SecurityBufferLength) - 1);
e2f34481
NJ
1620
1621 } else if (negblob->MessageType == NtLmAuthenticate) {
1622 rc = ntlm_authenticate(work);
1623 if (rc)
1624 goto out_err;
1625
1626 ksmbd_conn_set_good(work);
1627 sess->state = SMB2_SESSION_VALID;
822bc8ea 1628 kfree(sess->Preauth_HashValue);
e2f34481
NJ
1629 sess->Preauth_HashValue = NULL;
1630 }
1631 } else {
1632 /* TODO: need one more negotiation */
1633 ksmbd_err("Not support the preferred authentication\n");
1634 rc = -EINVAL;
1635 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1636 }
1637 } else {
1638 ksmbd_err("Not support authentication\n");
1639 rc = -EINVAL;
1640 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1641 }
1642
1643out_err:
1644 if (conn->use_spnego && conn->mechToken) {
1645 kfree(conn->mechToken);
1646 conn->mechToken = NULL;
1647 }
1648
1649 if (rc < 0 && sess) {
1650 ksmbd_session_destroy(sess);
1651 work->sess = NULL;
1652 }
1653
1654 return rc;
1655}
1656
1657/**
1658 * smb2_tree_connect() - handler for smb2 tree connect command
1659 * @work: smb work containing smb request buffer
1660 *
1661 * Return: 0 on success, otherwise error
1662 */
1663int smb2_tree_connect(struct ksmbd_work *work)
1664{
1665 struct ksmbd_conn *conn = work->conn;
e5066499
NJ
1666 struct smb2_tree_connect_req *req = work->request_buf;
1667 struct smb2_tree_connect_rsp *rsp = work->response_buf;
e2f34481
NJ
1668 struct ksmbd_session *sess = work->sess;
1669 char *treename = NULL, *name = NULL;
1670 struct ksmbd_tree_conn_status status;
1671 struct ksmbd_share_config *share;
1672 int rc = -EINVAL;
1673
1674 treename = smb_strndup_from_utf16(req->Buffer,
64b39f4a 1675 le16_to_cpu(req->PathLength), true, conn->local_nls);
e2f34481
NJ
1676 if (IS_ERR(treename)) {
1677 ksmbd_err("treename is NULL\n");
1678 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1679 goto out_err1;
1680 }
1681
36ba3866 1682 name = ksmbd_extract_sharename(treename);
e2f34481
NJ
1683 if (IS_ERR(name)) {
1684 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1685 goto out_err1;
1686 }
1687
1688 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
1689 name, treename);
1690
1691 status = ksmbd_tree_conn_connect(sess, name);
1692 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1693 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1694 else
1695 goto out_err1;
1696
1697 share = status.tree_conn->share_conf;
1698 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1699 ksmbd_debug(SMB, "IPC share path request\n");
1700 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1701 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1702 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1703 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1704 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1705 FILE_SYNCHRONIZE_LE;
1706 } else {
1707 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1708 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1709 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1710 if (test_tree_conn_flag(status.tree_conn,
1711 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1712 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1713 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
1714 FILE_DELETE_CHILD_LE | FILE_DELETE_LE |
1715 FILE_WRITE_ATTRIBUTES_LE | FILE_DELETE_LE |
1716 FILE_READ_CONTROL_LE | FILE_WRITE_DAC_LE |
1717 FILE_WRITE_OWNER_LE | FILE_SYNCHRONIZE_LE;
1718 }
1719 }
1720
1721 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1722 if (conn->posix_ext_supported)
1723 status.tree_conn->posix_extensions = true;
1724
1725out_err1:
1726 rsp->StructureSize = cpu_to_le16(16);
1727 rsp->Capabilities = 0;
1728 rsp->Reserved = 0;
1729 /* default manual caching */
1730 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
1731 inc_rfc1001_len(rsp, 16);
1732
1733 if (!IS_ERR(treename))
1734 kfree(treename);
1735 if (!IS_ERR(name))
1736 kfree(name);
1737
1738 switch (status.ret) {
1739 case KSMBD_TREE_CONN_STATUS_OK:
1740 rsp->hdr.Status = STATUS_SUCCESS;
1741 rc = 0;
1742 break;
1743 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1744 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1745 break;
1746 case -ENOMEM:
1747 case KSMBD_TREE_CONN_STATUS_NOMEM:
1748 rsp->hdr.Status = STATUS_NO_MEMORY;
1749 break;
1750 case KSMBD_TREE_CONN_STATUS_ERROR:
1751 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1752 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1753 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1754 break;
1755 case -EINVAL:
1756 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1757 break;
1758 default:
1759 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1760 }
1761
1762 return rc;
1763}
1764
1765/**
1766 * smb2_create_open_flags() - convert smb open flags to unix open flags
1767 * @file_present: is file already present
1768 * @access: file access flags
1769 * @disposition: file disposition flags
e2f34481
NJ
1770 *
1771 * Return: file open flags
1772 */
1773static int smb2_create_open_flags(bool file_present, __le32 access,
1774 __le32 disposition)
1775{
1776 int oflags = O_NONBLOCK | O_LARGEFILE;
1777
1778 if (access & FILE_READ_DESIRED_ACCESS_LE &&
64b39f4a 1779 access & FILE_WRITE_DESIRE_ACCESS_LE)
e2f34481
NJ
1780 oflags |= O_RDWR;
1781 else if (access & FILE_WRITE_DESIRE_ACCESS_LE)
1782 oflags |= O_WRONLY;
1783 else
1784 oflags |= O_RDONLY;
1785
1786 if (access == FILE_READ_ATTRIBUTES_LE)
1787 oflags |= O_PATH;
1788
1789 if (file_present) {
1790 switch (disposition & FILE_CREATE_MASK_LE) {
1791 case FILE_OPEN_LE:
1792 case FILE_CREATE_LE:
1793 break;
1794 case FILE_SUPERSEDE_LE:
1795 case FILE_OVERWRITE_LE:
1796 case FILE_OVERWRITE_IF_LE:
1797 oflags |= O_TRUNC;
1798 break;
1799 default:
1800 break;
1801 }
1802 } else {
1803 switch (disposition & FILE_CREATE_MASK_LE) {
1804 case FILE_SUPERSEDE_LE:
1805 case FILE_CREATE_LE:
1806 case FILE_OPEN_IF_LE:
1807 case FILE_OVERWRITE_IF_LE:
1808 oflags |= O_CREAT;
1809 break;
1810 case FILE_OPEN_LE:
1811 case FILE_OVERWRITE_LE:
1812 oflags &= ~O_CREAT;
1813 break;
1814 default:
1815 break;
1816 }
1817 }
1818 return oflags;
1819}
1820
1821/**
1822 * smb2_tree_disconnect() - handler for smb tree connect request
1823 * @work: smb work containing request buffer
1824 *
1825 * Return: 0
1826 */
1827int smb2_tree_disconnect(struct ksmbd_work *work)
1828{
e5066499 1829 struct smb2_tree_disconnect_rsp *rsp = work->response_buf;
e2f34481
NJ
1830 struct ksmbd_session *sess = work->sess;
1831 struct ksmbd_tree_connect *tcon = work->tcon;
1832
1833 rsp->StructureSize = cpu_to_le16(4);
1834 inc_rfc1001_len(rsp, 4);
1835
1836 ksmbd_debug(SMB, "request\n");
1837
1838 if (!tcon) {
e5066499 1839 struct smb2_tree_disconnect_req *req = work->request_buf;
e2f34481
NJ
1840
1841 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
1842 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
1843 smb2_set_err_rsp(work);
1844 return 0;
1845 }
1846
1847 ksmbd_close_tree_conn_fds(work);
1848 ksmbd_tree_conn_disconnect(sess, tcon);
1849 return 0;
1850}
1851
1852/**
1853 * smb2_session_logoff() - handler for session log off request
1854 * @work: smb work containing request buffer
1855 *
1856 * Return: 0
1857 */
1858int smb2_session_logoff(struct ksmbd_work *work)
1859{
1860 struct ksmbd_conn *conn = work->conn;
e5066499 1861 struct smb2_logoff_rsp *rsp = work->response_buf;
e2f34481
NJ
1862 struct ksmbd_session *sess = work->sess;
1863
1864 rsp->StructureSize = cpu_to_le16(4);
1865 inc_rfc1001_len(rsp, 4);
1866
1867 ksmbd_debug(SMB, "request\n");
1868
1869 /* Got a valid session, set connection state */
1870 WARN_ON(sess->conn != conn);
1871
1872 /* setting CifsExiting here may race with start_tcp_sess */
1873 ksmbd_conn_set_need_reconnect(work);
1874 ksmbd_close_session_fds(work);
1875 ksmbd_conn_wait_idle(conn);
1876
1877 if (ksmbd_tree_conn_session_logoff(sess)) {
e5066499 1878 struct smb2_logoff_req *req = work->request_buf;
e2f34481
NJ
1879
1880 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
1881 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
1882 smb2_set_err_rsp(work);
1883 return 0;
1884 }
1885
1886 ksmbd_destroy_file_table(&sess->file_table);
1887 sess->state = SMB2_SESSION_EXPIRED;
1888
1889 ksmbd_free_user(sess->user);
1890 sess->user = NULL;
1891
1892 /* let start_tcp_sess free connection info now */
1893 ksmbd_conn_set_need_negotiate(work);
1894 return 0;
1895}
1896
1897/**
1898 * create_smb2_pipe() - create IPC pipe
1899 * @work: smb work containing request buffer
1900 *
1901 * Return: 0 on success, otherwise error
1902 */
1903static noinline int create_smb2_pipe(struct ksmbd_work *work)
1904{
e5066499
NJ
1905 struct smb2_create_rsp *rsp = work->response_buf;
1906 struct smb2_create_req *req = work->request_buf;
e2f34481
NJ
1907 int id;
1908 int err;
1909 char *name;
1910
1911 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
1912 1, work->conn->local_nls);
1913 if (IS_ERR(name)) {
1914 rsp->hdr.Status = STATUS_NO_MEMORY;
1915 err = PTR_ERR(name);
1916 goto out;
1917 }
1918
1919 id = ksmbd_session_rpc_open(work->sess, name);
1920 if (id < 0)
1921 ksmbd_err("Unable to open RPC pipe: %d\n", id);
1922
1923 rsp->StructureSize = cpu_to_le16(89);
1924 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
1925 rsp->Reserved = 0;
1926 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
1927
1928 rsp->CreationTime = cpu_to_le64(0);
1929 rsp->LastAccessTime = cpu_to_le64(0);
1930 rsp->ChangeTime = cpu_to_le64(0);
1931 rsp->AllocationSize = cpu_to_le64(0);
1932 rsp->EndofFile = cpu_to_le64(0);
1933 rsp->FileAttributes = ATTR_NORMAL_LE;
1934 rsp->Reserved2 = 0;
1935 rsp->VolatileFileId = cpu_to_le64(id);
1936 rsp->PersistentFileId = 0;
1937 rsp->CreateContextsOffset = 0;
1938 rsp->CreateContextsLength = 0;
1939
1940 inc_rfc1001_len(rsp, 88); /* StructureSize - 1*/
1941 kfree(name);
1942 return 0;
1943
1944out:
1945 smb2_set_err_rsp(work);
1946 return err;
1947}
1948
e2f34481
NJ
1949/**
1950 * smb2_set_ea() - handler for setting extended attributes using set
1951 * info command
1952 * @eabuf: set info command buffer
1953 * @path: dentry path for get ea
1954 *
1955 * Return: 0 on success, otherwise error
1956 */
1957static int smb2_set_ea(struct smb2_ea_info *eabuf, struct path *path)
1958{
1959 char *attr_name = NULL, *value;
1960 int rc = 0;
1961 int next = 0;
1962
1963 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
1964 if (!attr_name)
1965 return -ENOMEM;
1966
1967 do {
1968 if (!eabuf->EaNameLength)
1969 goto next;
1970
1971 ksmbd_debug(SMB,
1972 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
1973 eabuf->name, eabuf->EaNameLength,
1974 le16_to_cpu(eabuf->EaValueLength),
1975 le32_to_cpu(eabuf->NextEntryOffset));
1976
1977 if (eabuf->EaNameLength >
1978 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
1979 rc = -EINVAL;
1980 break;
1981 }
1982
1983 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
1984 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
1985 eabuf->EaNameLength);
1986 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
1987 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
1988
1989 if (!eabuf->EaValueLength) {
1990 rc = ksmbd_vfs_casexattr_len(path->dentry,
1991 attr_name,
1992 XATTR_USER_PREFIX_LEN +
1993 eabuf->EaNameLength);
1994
1995 /* delete the EA only when it exits */
1996 if (rc > 0) {
1997 rc = ksmbd_vfs_remove_xattr(path->dentry,
1998 attr_name);
1999
2000 if (rc < 0) {
2001 ksmbd_debug(SMB,
2002 "remove xattr failed(%d)\n",
2003 rc);
2004 break;
2005 }
2006 }
2007
2008 /* if the EA doesn't exist, just do nothing. */
2009 rc = 0;
2010 } else {
2011 rc = ksmbd_vfs_setxattr(path->dentry, attr_name, value,
2012 le16_to_cpu(eabuf->EaValueLength), 0);
2013 if (rc < 0) {
2014 ksmbd_debug(SMB,
2015 "ksmbd_vfs_setxattr is failed(%d)\n",
2016 rc);
2017 break;
2018 }
2019 }
2020
2021next:
2022 next = le32_to_cpu(eabuf->NextEntryOffset);
2023 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
2024 } while (next != 0);
2025
2026 kfree(attr_name);
2027 return rc;
2028}
2029
2030static inline int check_context_err(void *ctx, char *str)
2031{
2032 int err;
2033
2034 err = PTR_ERR(ctx);
2035 ksmbd_debug(SMB, "find context %s err %d\n", str, err);
2036
2037 if (err == -EINVAL) {
2038 ksmbd_err("bad name length\n");
2039 return err;
2040 }
2041
2042 return 0;
2043}
2044
2045static noinline int smb2_set_stream_name_xattr(struct path *path,
64b39f4a 2046 struct ksmbd_file *fp, char *stream_name, int s_type)
e2f34481
NJ
2047{
2048 size_t xattr_stream_size;
2049 char *xattr_stream_name;
2050 int rc;
2051
2052 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2053 &xattr_stream_name,
2054 &xattr_stream_size,
2055 s_type);
2056 if (rc)
2057 return rc;
2058
2059 fp->stream.name = xattr_stream_name;
2060 fp->stream.size = xattr_stream_size;
2061
2062 /* Check if there is stream prefix in xattr space */
2063 rc = ksmbd_vfs_casexattr_len(path->dentry,
2064 xattr_stream_name,
2065 xattr_stream_size);
2066 if (rc >= 0)
2067 return 0;
2068
2069 if (fp->cdoption == FILE_OPEN_LE) {
2070 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2071 return -EBADF;
2072 }
2073
2074 rc = ksmbd_vfs_setxattr(path->dentry, xattr_stream_name, NULL, 0, 0);
2075 if (rc < 0)
2076 ksmbd_err("Failed to store XATTR stream name :%d\n", rc);
2077 return 0;
2078}
2079
2080static int smb2_remove_smb_xattrs(struct dentry *dentry)
2081{
2082 char *name, *xattr_list = NULL;
2083 ssize_t xattr_list_len;
2084 int err = 0;
2085
2086 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
2087 if (xattr_list_len < 0) {
2088 goto out;
2089 } else if (!xattr_list_len) {
2090 ksmbd_debug(SMB, "empty xattr in the file\n");
2091 goto out;
2092 }
2093
2094 for (name = xattr_list; name - xattr_list < xattr_list_len;
2095 name += strlen(name) + 1) {
2096 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2097
2098 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
64b39f4a
NJ
2099 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2100 DOS_ATTRIBUTE_PREFIX_LEN) &&
2101 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
e2f34481
NJ
2102 continue;
2103
2104 err = ksmbd_vfs_remove_xattr(dentry, name);
2105 if (err)
2106 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2107 }
2108out:
79f6b11a 2109 kvfree(xattr_list);
e2f34481
NJ
2110 return err;
2111}
2112
2113static int smb2_create_truncate(struct path *path)
2114{
2115 int rc = vfs_truncate(path, 0);
2116
2117 if (rc) {
2118 ksmbd_err("vfs_truncate failed, rc %d\n", rc);
2119 return rc;
2120 }
2121
2122 rc = smb2_remove_smb_xattrs(path->dentry);
2123 if (rc == -EOPNOTSUPP)
2124 rc = 0;
2125 if (rc)
2126 ksmbd_debug(SMB,
2127 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2128 rc);
2129 return rc;
2130}
2131
64b39f4a
NJ
2132static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
2133 struct ksmbd_file *fp)
e2f34481
NJ
2134{
2135 struct xattr_dos_attrib da = {0};
2136 int rc;
2137
2138 if (!test_share_config_flag(tcon->share_conf,
2139 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2140 return;
2141
2142 da.version = 4;
2143 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2144 da.itime = da.create_time = fp->create_time;
2145 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2146 XATTR_DOSINFO_ITIME;
2147
2148 rc = ksmbd_vfs_set_dos_attrib_xattr(path->dentry, &da);
2149 if (rc)
2150 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2151}
2152
2153static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
64b39f4a 2154 struct path *path, struct ksmbd_file *fp)
e2f34481
NJ
2155{
2156 struct xattr_dos_attrib da;
2157 int rc;
2158
2159 fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE);
2160
2161 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2162 if (!test_share_config_flag(tcon->share_conf,
64b39f4a 2163 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
e2f34481
NJ
2164 return;
2165
2166 rc = ksmbd_vfs_get_dos_attrib_xattr(path->dentry, &da);
2167 if (rc > 0) {
2168 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2169 fp->create_time = da.create_time;
2170 fp->itime = da.itime;
2171 }
2172}
2173
64b39f4a
NJ
2174static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
2175 int open_flags, umode_t posix_mode, bool is_dir)
e2f34481
NJ
2176{
2177 struct ksmbd_tree_connect *tcon = work->tcon;
2178 struct ksmbd_share_config *share = tcon->share_conf;
2179 umode_t mode;
2180 int rc;
2181
2182 if (!(open_flags & O_CREAT))
2183 return -EBADF;
2184
2185 ksmbd_debug(SMB, "file does not exist, so creating\n");
2186 if (is_dir == true) {
2187 ksmbd_debug(SMB, "creating directory\n");
2188
2189 mode = share_config_directory_mode(share, posix_mode);
2190 rc = ksmbd_vfs_mkdir(work, name, mode);
2191 if (rc)
2192 return rc;
2193 } else {
2194 ksmbd_debug(SMB, "creating regular file\n");
2195
2196 mode = share_config_create_mode(share, posix_mode);
2197 rc = ksmbd_vfs_create(work, name, mode);
2198 if (rc)
2199 return rc;
2200 }
2201
2202 rc = ksmbd_vfs_kern_path(name, 0, path, 0);
2203 if (rc) {
2204 ksmbd_err("cannot get linux path (%s), err = %d\n",
2205 name, rc);
2206 return rc;
2207 }
2208 return 0;
2209}
2210
2211static int smb2_create_sd_buffer(struct ksmbd_work *work,
2212 struct smb2_create_req *req, struct dentry *dentry)
2213{
2214 struct create_context *context;
2215 int rc = -ENOENT;
2216
2217 if (!req->CreateContextsOffset)
2218 return rc;
2219
2220 /* Parse SD BUFFER create contexts */
2221 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
2222 if (context && !IS_ERR(context)) {
2223 struct create_sd_buf_req *sd_buf;
2224
2225 ksmbd_debug(SMB,
2226 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2227 sd_buf = (struct create_sd_buf_req *)context;
2228 rc = set_info_sec(work->conn, work->tcon, dentry, &sd_buf->ntsd,
2229 le32_to_cpu(sd_buf->ccontext.DataLength), true);
2230 }
2231
2232 return rc;
2233}
2234
2235/**
2236 * smb2_open() - handler for smb file open request
2237 * @work: smb work containing request buffer
2238 *
2239 * Return: 0 on success, otherwise error
2240 */
2241int smb2_open(struct ksmbd_work *work)
2242{
2243 struct ksmbd_conn *conn = work->conn;
2244 struct ksmbd_session *sess = work->sess;
2245 struct ksmbd_tree_connect *tcon = work->tcon;
2246 struct smb2_create_req *req;
2247 struct smb2_create_rsp *rsp, *rsp_org;
2248 struct path path;
2249 struct ksmbd_share_config *share = tcon->share_conf;
2250 struct ksmbd_file *fp = NULL;
2251 struct file *filp = NULL;
2252 struct kstat stat;
2253 struct create_context *context;
2254 struct lease_ctx_info *lc = NULL;
2255 struct create_ea_buf_req *ea_buf = NULL;
2256 struct oplock_info *opinfo;
2257 __le32 *next_ptr = NULL;
2258 int req_op_level = 0, open_flags = 0, file_info = 0;
2259 int rc = 0, len = 0;
2260 int contxt_cnt = 0, query_disk_id = 0;
2261 int maximal_access_ctxt = 0, posix_ctxt = 0;
2262 int s_type = 0;
2263 int next_off = 0;
2264 char *name = NULL;
2265 char *stream_name = NULL;
2266 bool file_present = false, created = false, already_permitted = false;
e2f34481
NJ
2267 int share_ret, need_truncate = 0;
2268 u64 time;
2269 umode_t posix_mode = 0;
2270 __le32 daccess, maximal_access = 0;
2271
e5066499 2272 rsp_org = work->response_buf;
e2f34481
NJ
2273 WORK_BUFFERS(work, req, rsp);
2274
2275 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
64b39f4a 2276 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
e2f34481
NJ
2277 ksmbd_debug(SMB, "invalid flag in chained command\n");
2278 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2279 smb2_set_err_rsp(work);
2280 return -EINVAL;
2281 }
2282
2283 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2284 ksmbd_debug(SMB, "IPC pipe create request\n");
2285 return create_smb2_pipe(work);
2286 }
2287
2288 if (req->NameLength) {
2289 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
64b39f4a 2290 *(char *)req->Buffer == '\\') {
1e853b93 2291 ksmbd_err("not allow directory name included leading slash\n");
e2f34481
NJ
2292 rc = -EINVAL;
2293 goto err_out1;
2294 }
2295
2296 name = smb2_get_name(share,
2297 req->Buffer,
2298 le16_to_cpu(req->NameLength),
2299 work->conn->local_nls);
2300 if (IS_ERR(name)) {
2301 rc = PTR_ERR(name);
2302 if (rc != -ENOMEM)
2303 rc = -ENOENT;
2304 goto err_out1;
2305 }
2306
2307 ksmbd_debug(SMB, "converted name = %s\n", name);
2308 if (strchr(name, ':')) {
2309 if (!test_share_config_flag(work->tcon->share_conf,
64b39f4a 2310 KSMBD_SHARE_FLAG_STREAMS)) {
e2f34481
NJ
2311 rc = -EBADF;
2312 goto err_out1;
2313 }
2314 rc = parse_stream_name(name, &stream_name, &s_type);
2315 if (rc < 0)
2316 goto err_out1;
2317 }
2318
2319 rc = ksmbd_validate_filename(name);
2320 if (rc < 0)
2321 goto err_out1;
2322
2323 if (ksmbd_share_veto_filename(share, name)) {
2324 rc = -ENOENT;
2325 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
2326 name);
2327 goto err_out1;
2328 }
2329 } else {
2330 len = strlen(share->path);
2331 ksmbd_debug(SMB, "share path len %d\n", len);
2332 name = kmalloc(len + 1, GFP_KERNEL);
2333 if (!name) {
2334 rsp->hdr.Status = STATUS_NO_MEMORY;
2335 rc = -ENOMEM;
2336 goto err_out1;
2337 }
2338
2339 memcpy(name, share->path, len);
2340 *(name + len) = '\0';
2341 }
2342
2343 req_op_level = req->RequestedOplockLevel;
73f9dad5 2344 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
e2f34481 2345 lc = parse_lease_state(req);
e2f34481 2346
64b39f4a 2347 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) {
e2f34481
NJ
2348 ksmbd_err("Invalid impersonationlevel : 0x%x\n",
2349 le32_to_cpu(req->ImpersonationLevel));
2350 rc = -EIO;
2351 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2352 goto err_out1;
2353 }
2354
2355 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) {
2356 ksmbd_err("Invalid create options : 0x%x\n",
2357 le32_to_cpu(req->CreateOptions));
2358 rc = -EINVAL;
2359 goto err_out1;
2360 } else {
2361
2362 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
64b39f4a 2363 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
e2f34481
NJ
2364 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2365
2366 if (req->CreateOptions & (FILE_OPEN_BY_FILE_ID_LE |
2367 CREATE_TREE_CONNECTION | FILE_RESERVE_OPFILTER_LE)) {
2368 rc = -EOPNOTSUPP;
2369 goto err_out1;
2370 }
2371
2372 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2373 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2374 rc = -EINVAL;
2375 goto err_out1;
64b39f4a 2376 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
e2f34481 2377 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
64b39f4a 2378 }
e2f34481
NJ
2379 }
2380 }
2381
2382 if (le32_to_cpu(req->CreateDisposition) >
2383 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
2384 ksmbd_err("Invalid create disposition : 0x%x\n",
2385 le32_to_cpu(req->CreateDisposition));
2386 rc = -EINVAL;
2387 goto err_out1;
2388 }
2389
2390 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
1e853b93 2391 ksmbd_err("Invalid desired access : 0x%x\n",
e2f34481
NJ
2392 le32_to_cpu(req->DesiredAccess));
2393 rc = -EACCES;
2394 goto err_out1;
2395 }
2396
64b39f4a 2397 if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) {
e2f34481
NJ
2398 ksmbd_err("Invalid file attribute : 0x%x\n",
2399 le32_to_cpu(req->FileAttributes));
2400 rc = -EINVAL;
2401 goto err_out1;
2402 }
2403
2404 if (req->CreateContextsOffset) {
2405 /* Parse non-durable handle create contexts */
2406 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
2407 if (IS_ERR(context)) {
2408 rc = check_context_err(context, SMB2_CREATE_EA_BUFFER);
2409 if (rc < 0)
2410 goto err_out1;
2411 } else {
2412 ea_buf = (struct create_ea_buf_req *)context;
2413 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2414 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2415 rc = -EACCES;
2416 goto err_out1;
2417 }
2418 }
2419
2420 context = smb2_find_context_vals(req,
2421 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
2422 if (IS_ERR(context)) {
2423 rc = check_context_err(context,
2424 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
2425 if (rc < 0)
2426 goto err_out1;
2427 } else {
2428 ksmbd_debug(SMB,
2429 "get query maximal access context\n");
2430 maximal_access_ctxt = 1;
2431 }
2432
2433 context = smb2_find_context_vals(req,
2434 SMB2_CREATE_TIMEWARP_REQUEST);
2435 if (IS_ERR(context)) {
2436 rc = check_context_err(context,
2437 SMB2_CREATE_TIMEWARP_REQUEST);
2438 if (rc < 0)
2439 goto err_out1;
2440 } else {
2441 ksmbd_debug(SMB, "get timewarp context\n");
2442 rc = -EBADF;
2443 goto err_out1;
2444 }
2445
2446 if (tcon->posix_extensions) {
2447 context = smb2_find_context_vals(req,
2448 SMB2_CREATE_TAG_POSIX);
2449 if (IS_ERR(context)) {
2450 rc = check_context_err(context,
2451 SMB2_CREATE_TAG_POSIX);
2452 if (rc < 0)
2453 goto err_out1;
2454 } else {
2455 struct create_posix *posix =
2456 (struct create_posix *)context;
2457 ksmbd_debug(SMB, "get posix context\n");
2458
2459 posix_mode = le32_to_cpu(posix->Mode);
2460 posix_ctxt = 1;
2461 }
2462 }
2463 }
2464
2465 if (ksmbd_override_fsids(work)) {
2466 rc = -ENOMEM;
2467 goto err_out1;
2468 }
2469
2470 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
2471 /*
2472 * On delete request, instead of following up, need to
2473 * look the current entity
2474 */
2475 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2476 if (!rc) {
2477 /*
2478 * If file exists with under flags, return access
2479 * denied error.
2480 */
2481 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
64b39f4a 2482 req->CreateDisposition == FILE_OPEN_IF_LE) {
e2f34481
NJ
2483 rc = -EACCES;
2484 path_put(&path);
2485 goto err_out;
2486 }
2487
64b39f4a 2488 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
e2f34481
NJ
2489 ksmbd_debug(SMB,
2490 "User does not have write permission\n");
2491 rc = -EACCES;
2492 path_put(&path);
2493 goto err_out;
2494 }
2495 }
2496 } else {
2497 if (test_share_config_flag(work->tcon->share_conf,
64b39f4a 2498 KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS)) {
e2f34481
NJ
2499 /*
2500 * Use LOOKUP_FOLLOW to follow the path of
2501 * symlink in path buildup
2502 */
2503 rc = ksmbd_vfs_kern_path(name, LOOKUP_FOLLOW, &path, 1);
2504 if (rc) { /* Case for broken link ?*/
2505 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2506 }
2507 } else {
2508 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2509 if (!rc && d_is_symlink(path.dentry)) {
2510 rc = -EACCES;
2511 path_put(&path);
2512 goto err_out;
2513 }
2514 }
2515 }
2516
2517 if (rc) {
2518 if (rc == -EACCES) {
2519 ksmbd_debug(SMB,
2520 "User does not have right permission\n");
2521 goto err_out;
2522 }
2523 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
2524 name, rc);
2525 rc = 0;
2526 } else {
2527 file_present = true;
2528 generic_fillattr(&init_user_ns, d_inode(path.dentry), &stat);
2529 }
2530 if (stream_name) {
2531 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2532 if (s_type == DATA_STREAM) {
2533 rc = -EIO;
2534 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2535 }
2536 } else {
2537 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2538 rc = -EIO;
2539 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2540 }
2541 }
2542
2543 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
64b39f4a 2544 req->FileAttributes & ATTR_NORMAL_LE) {
e2f34481
NJ
2545 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2546 rc = -EIO;
2547 }
2548
2549 if (rc < 0)
2550 goto err_out;
2551 }
2552
64b39f4a
NJ
2553 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2554 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
e2f34481
NJ
2555 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
2556 name, req->CreateOptions);
2557 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2558 rc = -EIO;
2559 goto err_out;
2560 }
2561
2562 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
64b39f4a
NJ
2563 !(req->CreateDisposition == FILE_CREATE_LE) &&
2564 !S_ISDIR(stat.mode)) {
e2f34481
NJ
2565 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2566 rc = -EIO;
2567 goto err_out;
2568 }
2569
2570 if (!stream_name && file_present &&
64b39f4a 2571 req->CreateDisposition == FILE_CREATE_LE) {
e2f34481
NJ
2572 rc = -EEXIST;
2573 goto err_out;
2574 }
2575
e2f34481
NJ
2576 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2577
2578 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
2579 rc = smb_check_perm_dacl(conn, path.dentry, &daccess,
2580 sess->user->uid);
2581 if (rc)
2582 goto err_out;
2583 }
2584
2585 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2586 if (!file_present) {
2587 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2588 } else {
2589 rc = ksmbd_vfs_query_maximal_access(path.dentry,
2590 &daccess);
2591 if (rc)
2592 goto err_out;
2593 already_permitted = true;
2594 }
2595 maximal_access = daccess;
2596 }
2597
2598 open_flags = smb2_create_open_flags(file_present,
2599 daccess, req->CreateDisposition);
2600
2601 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2602 if (open_flags & O_CREAT) {
2603 ksmbd_debug(SMB,
2604 "User does not have write permission\n");
2605 rc = -EACCES;
2606 goto err_out;
2607 }
2608 }
2609
2610 /*create file if not present */
2611 if (!file_present) {
2612 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
2613 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
2614 if (rc)
2615 goto err_out;
2616
2617 created = true;
2618 if (ea_buf) {
2619 rc = smb2_set_ea(&ea_buf->ea, &path);
2620 if (rc == -EOPNOTSUPP)
2621 rc = 0;
2622 else if (rc)
2623 goto err_out;
2624 }
2625 } else if (!already_permitted) {
2626 bool may_delete;
2627
2628 may_delete = daccess & FILE_DELETE_LE ||
2629 req->CreateOptions & FILE_DELETE_ON_CLOSE_LE;
2630
2631 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2632 * because execute(search) permission on a parent directory,
2633 * is already granted.
2634 */
64b39f4a 2635 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
ff1d5727
NJ
2636 rc = ksmbd_vfs_inode_permission(path.dentry,
2637 open_flags & O_ACCMODE, may_delete);
2638 if (rc)
e2f34481 2639 goto err_out;
e2f34481
NJ
2640 }
2641 }
2642
2643 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2644 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2645 rc = -EBUSY;
2646 goto err_out;
2647 }
2648
2649 rc = 0;
2650 filp = dentry_open(&path, open_flags, current_cred());
2651 if (IS_ERR(filp)) {
2652 rc = PTR_ERR(filp);
2653 ksmbd_err("dentry open for dir failed, rc %d\n", rc);
2654 goto err_out;
2655 }
2656
2657 if (file_present) {
2658 if (!(open_flags & O_TRUNC))
2659 file_info = FILE_OPENED;
2660 else
2661 file_info = FILE_OVERWRITTEN;
2662
2663 if ((req->CreateDisposition & FILE_CREATE_MASK_LE)
2664 == FILE_SUPERSEDE_LE)
2665 file_info = FILE_SUPERSEDED;
64b39f4a 2666 } else if (open_flags & O_CREAT) {
e2f34481 2667 file_info = FILE_CREATED;
64b39f4a 2668 }
e2f34481
NJ
2669
2670 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2671
2672 /* Obtain Volatile-ID */
2673 fp = ksmbd_open_fd(work, filp);
2674 if (IS_ERR(fp)) {
2675 fput(filp);
2676 rc = PTR_ERR(fp);
2677 fp = NULL;
2678 goto err_out;
2679 }
2680
2681 /* Get Persistent-ID */
2682 ksmbd_open_durable_fd(fp);
2683 if (!HAS_FILE_ID(fp->persistent_id)) {
2684 rc = -ENOMEM;
2685 goto err_out;
2686 }
2687
2688 fp->filename = name;
2689 fp->cdoption = req->CreateDisposition;
2690 fp->daccess = daccess;
2691 fp->saccess = req->ShareAccess;
2692 fp->coption = req->CreateOptions;
2693
2694 /* Set default windows and posix acls if creating new file */
2695 if (created) {
2696 int posix_acl_rc;
fba08fa0 2697 struct inode *inode = d_inode(path.dentry);
e2f34481 2698
fba08fa0 2699 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(inode, d_inode(path.dentry->d_parent));
e2f34481
NJ
2700 if (posix_acl_rc)
2701 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2702
2703 if (test_share_config_flag(work->tcon->share_conf,
64b39f4a 2704 KSMBD_SHARE_FLAG_ACL_XATTR)) {
e2f34481
NJ
2705 rc = smb_inherit_dacl(conn, path.dentry, sess->user->uid,
2706 sess->user->gid);
2707 }
2708
2709 if (rc) {
2710 rc = smb2_create_sd_buffer(work, req, path.dentry);
2711 if (rc) {
2712 if (posix_acl_rc)
2713 ksmbd_vfs_set_init_posix_acl(inode);
2714
2715 if (test_share_config_flag(work->tcon->share_conf,
64b39f4a 2716 KSMBD_SHARE_FLAG_ACL_XATTR)) {
e2f34481
NJ
2717 struct smb_fattr fattr;
2718 struct smb_ntsd *pntsd;
2719 int pntsd_size, ace_num;
2720
2721 fattr.cf_uid = inode->i_uid;
2722 fattr.cf_gid = inode->i_gid;
2723 fattr.cf_mode = inode->i_mode;
2724 fattr.cf_dacls = NULL;
e6b1059f 2725 ace_num = 0;
e2f34481
NJ
2726
2727 fattr.cf_acls = ksmbd_vfs_get_acl(inode, ACL_TYPE_ACCESS);
e6b1059f
MM
2728 if (fattr.cf_acls)
2729 ace_num = fattr.cf_acls->a_count;
e2f34481
NJ
2730 if (S_ISDIR(inode->i_mode)) {
2731 fattr.cf_dacls =
2732 ksmbd_vfs_get_acl(inode, ACL_TYPE_DEFAULT);
e6b1059f
MM
2733 if (fattr.cf_dacls)
2734 ace_num += fattr.cf_dacls->a_count;
e2f34481
NJ
2735 }
2736
2737 pntsd = kmalloc(sizeof(struct smb_ntsd) +
64b39f4a 2738 sizeof(struct smb_sid) * 3 +
e2f34481 2739 sizeof(struct smb_acl) +
64b39f4a 2740 sizeof(struct smb_ace) * ace_num * 2,
e2f34481
NJ
2741 GFP_KERNEL);
2742 if (!pntsd)
2743 goto err_out;
2744
2745 rc = build_sec_desc(pntsd, NULL,
2746 OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO,
2747 &pntsd_size, &fattr);
2748 posix_acl_release(fattr.cf_acls);
2749 posix_acl_release(fattr.cf_dacls);
2750
2751 rc = ksmbd_vfs_set_sd_xattr(conn,
2752 path.dentry, pntsd, pntsd_size);
2753 if (rc)
2754 ksmbd_err("failed to store ntacl in xattr : %d\n",
2755 rc);
2756 }
2757 }
2758 }
2759 rc = 0;
2760 }
2761
2762 if (stream_name) {
2763 rc = smb2_set_stream_name_xattr(&path,
2764 fp,
2765 stream_name,
2766 s_type);
2767 if (rc)
2768 goto err_out;
2769 file_info = FILE_CREATED;
2770 }
2771
2772 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
2773 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
64b39f4a
NJ
2774 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
2775 !fp->attrib_only && !stream_name) {
e2f34481
NJ
2776 smb_break_all_oplock(work, fp);
2777 need_truncate = 1;
2778 }
2779
2780 /* fp should be searchable through ksmbd_inode.m_fp_list
2781 * after daccess, saccess, attrib_only, and stream are
2782 * initialized.
2783 */
2784 write_lock(&fp->f_ci->m_lock);
2785 list_add(&fp->node, &fp->f_ci->m_fp_list);
2786 write_unlock(&fp->f_ci->m_lock);
2787
2788 rc = ksmbd_vfs_getattr(&path, &stat);
2789 if (rc) {
2790 generic_fillattr(&init_user_ns, d_inode(path.dentry), &stat);
2791 rc = 0;
2792 }
2793
2794 /* Check delete pending among previous fp before oplock break */
2795 if (ksmbd_inode_pending_delete(fp)) {
2796 rc = -EBUSY;
2797 goto err_out;
2798 }
2799
2800 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
64b39f4a
NJ
2801 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
2802 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
2803 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
e2f34481
NJ
2804 if (share_ret < 0 && !S_ISDIR(FP_INODE(fp)->i_mode)) {
2805 rc = share_ret;
2806 goto err_out;
2807 }
2808 } else {
2809 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
2810 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
2811 ksmbd_debug(SMB,
2812 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
2813 name, req_op_level, lc->req_state);
2814 rc = find_same_lease_key(sess, fp->f_ci, lc);
2815 if (rc)
2816 goto err_out;
2817 } else if (open_flags == O_RDONLY &&
64b39f4a
NJ
2818 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
2819 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
e2f34481
NJ
2820 req_op_level = SMB2_OPLOCK_LEVEL_II;
2821
2822 rc = smb_grant_oplock(work, req_op_level,
2823 fp->persistent_id, fp,
2824 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
2825 lc, share_ret);
2826 if (rc < 0)
2827 goto err_out;
2828 }
2829
2830 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
2831 ksmbd_fd_set_delete_on_close(fp, file_info);
2832
2833 if (need_truncate) {
2834 rc = smb2_create_truncate(&path);
2835 if (rc)
2836 goto err_out;
2837 }
2838
2839 if (req->CreateContextsOffset) {
2840 struct create_alloc_size_req *az_req;
2841
2842 az_req = (struct create_alloc_size_req *)
2843 smb2_find_context_vals(req,
2844 SMB2_CREATE_ALLOCATION_SIZE);
2845 if (IS_ERR(az_req)) {
2846 rc = check_context_err(az_req,
2847 SMB2_CREATE_ALLOCATION_SIZE);
2848 if (rc < 0)
2849 goto err_out;
2850 } else {
2851 loff_t alloc_size = le64_to_cpu(az_req->AllocationSize);
2852 int err;
2853
2854 ksmbd_debug(SMB,
2855 "request smb2 create allocate size : %llu\n",
2856 alloc_size);
2857 err = ksmbd_vfs_alloc_size(work, fp, alloc_size);
2858 if (err < 0)
2859 ksmbd_debug(SMB,
2860 "ksmbd_vfs_alloc_size is failed : %d\n",
2861 err);
2862 }
2863
64b39f4a 2864 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
e2f34481 2865 if (IS_ERR(context)) {
64b39f4a 2866 rc = check_context_err(context, SMB2_CREATE_QUERY_ON_DISK_ID);
e2f34481
NJ
2867 if (rc < 0)
2868 goto err_out;
2869 } else {
2870 ksmbd_debug(SMB, "get query on disk id context\n");
2871 query_disk_id = 1;
2872 }
2873 }
2874
2875 if (stat.result_mask & STATX_BTIME)
2876 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
2877 else
2878 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
2879 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
2880 fp->f_ci->m_fattr = cpu_to_le32(smb2_get_dos_mode(&stat,
2881 le32_to_cpu(req->FileAttributes)));
2882
2883 if (!created)
2884 smb2_update_xattrs(tcon, &path, fp);
2885 else
2886 smb2_new_xattrs(tcon, &path, fp);
2887
2888 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
2889
e2f34481
NJ
2890 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
2891
2892 rsp->StructureSize = cpu_to_le16(89);
2893 rcu_read_lock();
2894 opinfo = rcu_dereference(fp->f_opinfo);
2895 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
2896 rcu_read_unlock();
2897 rsp->Reserved = 0;
2898 rsp->CreateAction = cpu_to_le32(file_info);
2899 rsp->CreationTime = cpu_to_le64(fp->create_time);
2900 time = ksmbd_UnixTimeToNT(stat.atime);
2901 rsp->LastAccessTime = cpu_to_le64(time);
2902 time = ksmbd_UnixTimeToNT(stat.mtime);
2903 rsp->LastWriteTime = cpu_to_le64(time);
2904 time = ksmbd_UnixTimeToNT(stat.ctime);
2905 rsp->ChangeTime = cpu_to_le64(time);
2906 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
2907 cpu_to_le64(stat.blocks << 9);
2908 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
2909 rsp->FileAttributes = fp->f_ci->m_fattr;
2910
2911 rsp->Reserved2 = 0;
2912
2913 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
2914 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
2915
2916 rsp->CreateContextsOffset = 0;
2917 rsp->CreateContextsLength = 0;
2918 inc_rfc1001_len(rsp_org, 88); /* StructureSize - 1*/
2919
2920 /* If lease is request send lease context response */
2921 if (opinfo && opinfo->is_lease) {
2922 struct create_context *lease_ccontext;
2923
2924 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
2925 name, opinfo->o_lease->state);
2926 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2927
2928 lease_ccontext = (struct create_context *)rsp->Buffer;
2929 contxt_cnt++;
2930 create_lease_buf(rsp->Buffer, opinfo->o_lease);
2931 le32_add_cpu(&rsp->CreateContextsLength,
2932 conn->vals->create_lease_size);
2933 inc_rfc1001_len(rsp_org, conn->vals->create_lease_size);
2934 next_ptr = &lease_ccontext->Next;
2935 next_off = conn->vals->create_lease_size;
2936 }
2937
e2f34481
NJ
2938 if (maximal_access_ctxt) {
2939 struct create_context *mxac_ccontext;
2940
2941 if (maximal_access == 0)
2942 ksmbd_vfs_query_maximal_access(path.dentry,
2943 &maximal_access);
2944 mxac_ccontext = (struct create_context *)(rsp->Buffer +
2945 le32_to_cpu(rsp->CreateContextsLength));
2946 contxt_cnt++;
2947 create_mxac_rsp_buf(rsp->Buffer +
2948 le32_to_cpu(rsp->CreateContextsLength),
2949 le32_to_cpu(maximal_access));
2950 le32_add_cpu(&rsp->CreateContextsLength,
2951 conn->vals->create_mxac_size);
2952 inc_rfc1001_len(rsp_org, conn->vals->create_mxac_size);
2953 if (next_ptr)
2954 *next_ptr = cpu_to_le32(next_off);
2955 next_ptr = &mxac_ccontext->Next;
2956 next_off = conn->vals->create_mxac_size;
2957 }
2958
2959 if (query_disk_id) {
2960 struct create_context *disk_id_ccontext;
2961
2962 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
2963 le32_to_cpu(rsp->CreateContextsLength));
2964 contxt_cnt++;
2965 create_disk_id_rsp_buf(rsp->Buffer +
2966 le32_to_cpu(rsp->CreateContextsLength),
2967 stat.ino, tcon->id);
2968 le32_add_cpu(&rsp->CreateContextsLength,
2969 conn->vals->create_disk_id_size);
2970 inc_rfc1001_len(rsp_org, conn->vals->create_disk_id_size);
2971 if (next_ptr)
2972 *next_ptr = cpu_to_le32(next_off);
2973 next_ptr = &disk_id_ccontext->Next;
2974 next_off = conn->vals->create_disk_id_size;
2975 }
2976
2977 if (posix_ctxt) {
e2f34481
NJ
2978 contxt_cnt++;
2979 create_posix_rsp_buf(rsp->Buffer +
2980 le32_to_cpu(rsp->CreateContextsLength),
2981 fp);
2982 le32_add_cpu(&rsp->CreateContextsLength,
2983 conn->vals->create_posix_size);
2984 inc_rfc1001_len(rsp_org, conn->vals->create_posix_size);
2985 if (next_ptr)
2986 *next_ptr = cpu_to_le32(next_off);
2987 }
2988
2989 if (contxt_cnt > 0) {
2990 rsp->CreateContextsOffset =
2991 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)
2992 - 4);
2993 }
2994
2995err_out:
2996 if (file_present || created)
2997 path_put(&path);
2998 ksmbd_revert_fsids(work);
2999err_out1:
3000 if (rc) {
3001 if (rc == -EINVAL)
3002 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3003 else if (rc == -EOPNOTSUPP)
3004 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
ff1d5727 3005 else if (rc == -EACCES || rc == -ESTALE)
e2f34481
NJ
3006 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3007 else if (rc == -ENOENT)
3008 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3009 else if (rc == -EPERM)
3010 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3011 else if (rc == -EBUSY)
3012 rsp->hdr.Status = STATUS_DELETE_PENDING;
3013 else if (rc == -EBADF)
3014 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3015 else if (rc == -ENOEXEC)
3016 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3017 else if (rc == -ENXIO)
3018 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3019 else if (rc == -EEXIST)
3020 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3021 else if (rc == -EMFILE)
3022 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3023 if (!rsp->hdr.Status)
3024 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3025
3026 if (!fp || !fp->filename)
3027 kfree(name);
3028 if (fp)
3029 ksmbd_fd_put(work, fp);
3030 smb2_set_err_rsp(work);
3031 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3032 }
3033
3034 kfree(lc);
3035
3036 return 0;
3037}
3038
3039static int readdir_info_level_struct_sz(int info_level)
3040{
3041 switch (info_level) {
3042 case FILE_FULL_DIRECTORY_INFORMATION:
3043 return sizeof(struct file_full_directory_info);
3044 case FILE_BOTH_DIRECTORY_INFORMATION:
3045 return sizeof(struct file_both_directory_info);
3046 case FILE_DIRECTORY_INFORMATION:
3047 return sizeof(struct file_directory_info);
3048 case FILE_NAMES_INFORMATION:
3049 return sizeof(struct file_names_info);
3050 case FILEID_FULL_DIRECTORY_INFORMATION:
3051 return sizeof(struct file_id_full_dir_info);
3052 case FILEID_BOTH_DIRECTORY_INFORMATION:
3053 return sizeof(struct file_id_both_directory_info);
3054 case SMB_FIND_FILE_POSIX_INFO:
3055 return sizeof(struct smb2_posix_info);
3056 default:
3057 return -EOPNOTSUPP;
3058 }
3059}
3060
3061static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3062{
3063 switch (info_level) {
3064 case FILE_FULL_DIRECTORY_INFORMATION:
3065 {
3066 struct file_full_directory_info *ffdinfo;
3067
3068 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3069 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3070 d_info->name = ffdinfo->FileName;
3071 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3072 return 0;
3073 }
3074 case FILE_BOTH_DIRECTORY_INFORMATION:
3075 {
3076 struct file_both_directory_info *fbdinfo;
3077
3078 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3079 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3080 d_info->name = fbdinfo->FileName;
3081 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3082 return 0;
3083 }
3084 case FILE_DIRECTORY_INFORMATION:
3085 {
3086 struct file_directory_info *fdinfo;
3087
3088 fdinfo = (struct file_directory_info *)d_info->rptr;
3089 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3090 d_info->name = fdinfo->FileName;
3091 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3092 return 0;
3093 }
3094 case FILE_NAMES_INFORMATION:
3095 {
3096 struct file_names_info *fninfo;
3097
3098 fninfo = (struct file_names_info *)d_info->rptr;
3099 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3100 d_info->name = fninfo->FileName;
3101 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3102 return 0;
3103 }
3104 case FILEID_FULL_DIRECTORY_INFORMATION:
3105 {
3106 struct file_id_full_dir_info *dinfo;
3107
3108 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3109 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3110 d_info->name = dinfo->FileName;
3111 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3112 return 0;
3113 }
3114 case FILEID_BOTH_DIRECTORY_INFORMATION:
3115 {
3116 struct file_id_both_directory_info *fibdinfo;
3117
3118 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3119 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3120 d_info->name = fibdinfo->FileName;
3121 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3122 return 0;
3123 }
3124 case SMB_FIND_FILE_POSIX_INFO:
3125 {
3126 struct smb2_posix_info *posix_info;
3127
3128 posix_info = (struct smb2_posix_info *)d_info->rptr;
3129 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3130 d_info->name = posix_info->name;
3131 d_info->name_len = le32_to_cpu(posix_info->name_len);
3132 return 0;
3133 }
3134 default:
3135 return -EINVAL;
3136 }
3137}
3138
3139/**
3140 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3141 * buffer
3142 * @conn: connection instance
3143 * @info_level: smb information level
3144 * @d_info: structure included variables for query dir
3145 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3146 *
3147 * if directory has many entries, find first can't read it fully.
3148 * find next might be called multiple times to read remaining dir entries
3149 *
3150 * Return: 0 on success, otherwise error
3151 */
64b39f4a
NJ
3152static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
3153 struct ksmbd_dir_info *d_info, struct ksmbd_kstat *ksmbd_kstat)
e2f34481
NJ
3154{
3155 int next_entry_offset = 0;
3156 char *conv_name;
3157 int conv_len;
3158 void *kstat;
3159 int struct_sz;
3160
3161 conv_name = ksmbd_convert_dir_info_name(d_info,
3162 conn->local_nls,
3163 &conv_len);
3164 if (!conv_name)
3165 return -ENOMEM;
3166
3167 /* Somehow the name has only terminating NULL bytes */
3168 if (conv_len < 0) {
3169 kfree(conv_name);
3170 return -EINVAL;
3171 }
3172
3173 struct_sz = readdir_info_level_struct_sz(info_level);
3174 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3175 KSMBD_DIR_INFO_ALIGNMENT);
3176
3177 if (next_entry_offset > d_info->out_buf_len) {
3178 d_info->out_buf_len = 0;
3179 return -ENOSPC;
3180 }
3181
3182 kstat = d_info->wptr;
3183 if (info_level != FILE_NAMES_INFORMATION)
3184 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3185
3186 switch (info_level) {
3187 case FILE_FULL_DIRECTORY_INFORMATION:
3188 {
3189 struct file_full_directory_info *ffdinfo;
3190
3191 ffdinfo = (struct file_full_directory_info *)kstat;
3192 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3193 ffdinfo->EaSize =
3194 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3195 if (ffdinfo->EaSize)
3196 ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3197 if (d_info->hide_dot_file && d_info->name[0] == '.')
3198 ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3199 memcpy(ffdinfo->FileName, conv_name, conv_len);
3200 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3201 break;
3202 }
3203 case FILE_BOTH_DIRECTORY_INFORMATION:
3204 {
3205 struct file_both_directory_info *fbdinfo;
3206
3207 fbdinfo = (struct file_both_directory_info *)kstat;
3208 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3209 fbdinfo->EaSize =
3210 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3211 if (fbdinfo->EaSize)
3212 fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3213 fbdinfo->ShortNameLength = 0;
3214 fbdinfo->Reserved = 0;
3215 if (d_info->hide_dot_file && d_info->name[0] == '.')
3216 fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3217 memcpy(fbdinfo->FileName, conv_name, conv_len);
3218 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3219 break;
3220 }
3221 case FILE_DIRECTORY_INFORMATION:
3222 {
3223 struct file_directory_info *fdinfo;
3224
3225 fdinfo = (struct file_directory_info *)kstat;
3226 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3227 if (d_info->hide_dot_file && d_info->name[0] == '.')
3228 fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3229 memcpy(fdinfo->FileName, conv_name, conv_len);
3230 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3231 break;
3232 }
3233 case FILE_NAMES_INFORMATION:
3234 {
3235 struct file_names_info *fninfo;
3236
3237 fninfo = (struct file_names_info *)kstat;
3238 fninfo->FileNameLength = cpu_to_le32(conv_len);
3239 memcpy(fninfo->FileName, conv_name, conv_len);
3240 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3241 break;
3242 }
3243 case FILEID_FULL_DIRECTORY_INFORMATION:
3244 {
3245 struct file_id_full_dir_info *dinfo;
3246
3247 dinfo = (struct file_id_full_dir_info *)kstat;
3248 dinfo->FileNameLength = cpu_to_le32(conv_len);
3249 dinfo->EaSize =
3250 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3251 if (dinfo->EaSize)
3252 dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3253 dinfo->Reserved = 0;
3254 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3255 if (d_info->hide_dot_file && d_info->name[0] == '.')
3256 dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3257 memcpy(dinfo->FileName, conv_name, conv_len);
3258 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3259 break;
3260 }
3261 case FILEID_BOTH_DIRECTORY_INFORMATION:
3262 {
3263 struct file_id_both_directory_info *fibdinfo;
3264
3265 fibdinfo = (struct file_id_both_directory_info *)kstat;
3266 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3267 fibdinfo->EaSize =
3268 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3269 if (fibdinfo->EaSize)
3270 fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3271 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3272 fibdinfo->ShortNameLength = 0;
3273 fibdinfo->Reserved = 0;
3274 fibdinfo->Reserved2 = cpu_to_le16(0);
3275 if (d_info->hide_dot_file && d_info->name[0] == '.')
3276 fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3277 memcpy(fibdinfo->FileName, conv_name, conv_len);
3278 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3279 break;
3280 }
3281 case SMB_FIND_FILE_POSIX_INFO:
3282 {
3283 struct smb2_posix_info *posix_info;
3284 u64 time;
3285
3286 posix_info = (struct smb2_posix_info *)kstat;
3287 posix_info->Ignored = 0;
3288 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3289 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3290 posix_info->ChangeTime = cpu_to_le64(time);
3291 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3292 posix_info->LastAccessTime = cpu_to_le64(time);
3293 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3294 posix_info->LastWriteTime = cpu_to_le64(time);
3295 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3296 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3297 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3298 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3299 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3300 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3301 posix_info->DosAttributes =
3302 S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE;
3303 if (d_info->hide_dot_file && d_info->name[0] == '.')
3304 posix_info->DosAttributes |= ATTR_HIDDEN_LE;
3305 id_to_sid(from_kuid(&init_user_ns, ksmbd_kstat->kstat->uid),
3306 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
3307 id_to_sid(from_kgid(&init_user_ns, ksmbd_kstat->kstat->gid),
3308 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
3309 memcpy(posix_info->name, conv_name, conv_len);
3310 posix_info->name_len = cpu_to_le32(conv_len);
3311 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3312 break;
3313 }
3314
3315 } /* switch (info_level) */
3316
3317 d_info->last_entry_offset = d_info->data_count;
3318 d_info->data_count += next_entry_offset;
3319 d_info->wptr += next_entry_offset;
3320 kfree(conv_name);
3321
3322 ksmbd_debug(SMB,
3323 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3324 info_level, d_info->out_buf_len,
3325 next_entry_offset, d_info->data_count);
3326
3327 return 0;
3328}
3329
3330struct smb2_query_dir_private {
3331 struct ksmbd_work *work;
3332 char *search_pattern;
3333 struct ksmbd_file *dir_fp;
3334
3335 struct ksmbd_dir_info *d_info;
3336 int info_level;
3337};
3338
3339static void lock_dir(struct ksmbd_file *dir_fp)
3340{
3341 struct dentry *dir = dir_fp->filp->f_path.dentry;
3342
3343 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3344}
3345
3346static void unlock_dir(struct ksmbd_file *dir_fp)
3347{
3348 struct dentry *dir = dir_fp->filp->f_path.dentry;
3349
3350 inode_unlock(d_inode(dir));
3351}
3352
3353static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3354{
3355 struct kstat kstat;
3356 struct ksmbd_kstat ksmbd_kstat;
3357 int rc;
3358 int i;
3359
3360 for (i = 0; i < priv->d_info->num_entry; i++) {
3361 struct dentry *dent;
3362
3363 if (dentry_name(priv->d_info, priv->info_level))
3364 return -EINVAL;
3365
3366 lock_dir(priv->dir_fp);
3367 dent = lookup_one_len(priv->d_info->name,
3368 priv->dir_fp->filp->f_path.dentry,
3369 priv->d_info->name_len);
3370 unlock_dir(priv->dir_fp);
3371
3372 if (IS_ERR(dent)) {
3373 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
3374 priv->d_info->name,
3375 PTR_ERR(dent));
3376 continue;
3377 }
3378 if (unlikely(d_is_negative(dent))) {
3379 dput(dent);
3380 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3381 priv->d_info->name);
3382 continue;
3383 }
3384
3385 ksmbd_kstat.kstat = &kstat;
3386 if (priv->info_level != FILE_NAMES_INFORMATION)
3387 ksmbd_vfs_fill_dentry_attrs(priv->work,
3388 dent,
3389 &ksmbd_kstat);
3390
3391 rc = smb2_populate_readdir_entry(priv->work->conn,
3392 priv->info_level,
3393 priv->d_info,
3394 &ksmbd_kstat);
3395 dput(dent);
3396 if (rc)
3397 return rc;
3398 }
3399 return 0;
3400}
3401
3402static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
64b39f4a 3403 int info_level)
e2f34481
NJ
3404{
3405 int struct_sz;
3406 int conv_len;
3407 int next_entry_offset;
3408
3409 struct_sz = readdir_info_level_struct_sz(info_level);
3410 if (struct_sz == -EOPNOTSUPP)
3411 return -EOPNOTSUPP;
3412
3413 conv_len = (d_info->name_len + 1) * 2;
3414 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3415 KSMBD_DIR_INFO_ALIGNMENT);
3416
3417 if (next_entry_offset > d_info->out_buf_len) {
3418 d_info->out_buf_len = 0;
3419 return -ENOSPC;
3420 }
3421
3422 switch (info_level) {
3423 case FILE_FULL_DIRECTORY_INFORMATION:
3424 {
3425 struct file_full_directory_info *ffdinfo;
3426
3427 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3428 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3429 ffdinfo->FileName[d_info->name_len] = 0x00;
3430 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3431 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3432 break;
3433 }
3434 case FILE_BOTH_DIRECTORY_INFORMATION:
3435 {
3436 struct file_both_directory_info *fbdinfo;
3437
3438 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3439 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3440 fbdinfo->FileName[d_info->name_len] = 0x00;
3441 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3442 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3443 break;
3444 }
3445 case FILE_DIRECTORY_INFORMATION:
3446 {
3447 struct file_directory_info *fdinfo;
3448
3449 fdinfo = (struct file_directory_info *)d_info->wptr;
3450 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3451 fdinfo->FileName[d_info->name_len] = 0x00;
3452 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3453 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3454 break;
3455 }
3456 case FILE_NAMES_INFORMATION:
3457 {
3458 struct file_names_info *fninfo;
3459
3460 fninfo = (struct file_names_info *)d_info->wptr;
3461 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3462 fninfo->FileName[d_info->name_len] = 0x00;
3463 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3464 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3465 break;
3466 }
3467 case FILEID_FULL_DIRECTORY_INFORMATION:
3468 {
3469 struct file_id_full_dir_info *dinfo;
3470
3471 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3472 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3473 dinfo->FileName[d_info->name_len] = 0x00;
3474 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3475 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3476 break;
3477 }
3478 case FILEID_BOTH_DIRECTORY_INFORMATION:
3479 {
3480 struct file_id_both_directory_info *fibdinfo;
3481
3482 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3483 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3484 fibdinfo->FileName[d_info->name_len] = 0x00;
3485 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3486 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3487 break;
3488 }
3489 case SMB_FIND_FILE_POSIX_INFO:
3490 {
3491 struct smb2_posix_info *posix_info;
3492
3493 posix_info = (struct smb2_posix_info *)d_info->wptr;
3494 memcpy(posix_info->name, d_info->name, d_info->name_len);
3495 posix_info->name[d_info->name_len] = 0x00;
3496 posix_info->name_len = cpu_to_le32(d_info->name_len);
3497 posix_info->NextEntryOffset =
3498 cpu_to_le32(next_entry_offset);
3499 break;
3500 }
3501 } /* switch (info_level) */
3502
3503 d_info->num_entry++;
3504 d_info->out_buf_len -= next_entry_offset;
3505 d_info->wptr += next_entry_offset;
3506 return 0;
3507}
3508
64b39f4a
NJ
3509static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
3510 loff_t offset, u64 ino, unsigned int d_type)
e2f34481
NJ
3511{
3512 struct ksmbd_readdir_data *buf;
3513 struct smb2_query_dir_private *priv;
3514 struct ksmbd_dir_info *d_info;
3515 int rc;
3516
3517 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3518 priv = buf->private;
3519 d_info = priv->d_info;
3520
3521 /* dot and dotdot entries are already reserved */
3522 if (!strcmp(".", name) || !strcmp("..", name))
3523 return 0;
3524 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3525 return 0;
b24c9335 3526 if (!match_pattern(name, namlen, priv->search_pattern))
e2f34481
NJ
3527 return 0;
3528
3529 d_info->name = name;
3530 d_info->name_len = namlen;
3531 rc = reserve_populate_dentry(d_info, priv->info_level);
3532 if (rc)
3533 return rc;
3534 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3535 d_info->out_buf_len = 0;
3536 return 0;
3537 }
3538 return 0;
3539}
3540
3541static void restart_ctx(struct dir_context *ctx)
3542{
3543 ctx->pos = 0;
3544}
3545
3546static int verify_info_level(int info_level)
3547{
3548 switch (info_level) {
3549 case FILE_FULL_DIRECTORY_INFORMATION:
3550 case FILE_BOTH_DIRECTORY_INFORMATION:
3551 case FILE_DIRECTORY_INFORMATION:
3552 case FILE_NAMES_INFORMATION:
3553 case FILEID_FULL_DIRECTORY_INFORMATION:
3554 case FILEID_BOTH_DIRECTORY_INFORMATION:
3555 case SMB_FIND_FILE_POSIX_INFO:
3556 break;
3557 default:
3558 return -EOPNOTSUPP;
3559 }
3560
3561 return 0;
3562}
3563
3564int smb2_query_dir(struct ksmbd_work *work)
3565{
3566 struct ksmbd_conn *conn = work->conn;
3567 struct smb2_query_directory_req *req;
3568 struct smb2_query_directory_rsp *rsp, *rsp_org;
3569 struct ksmbd_share_config *share = work->tcon->share_conf;
3570 struct ksmbd_file *dir_fp = NULL;
3571 struct ksmbd_dir_info d_info;
3572 int rc = 0;
3573 char *srch_ptr = NULL;
3574 unsigned char srch_flag;
3575 int buffer_sz;
3576 struct smb2_query_dir_private query_dir_private = {NULL, };
3577
e5066499 3578 rsp_org = work->response_buf;
e2f34481
NJ
3579 WORK_BUFFERS(work, req, rsp);
3580
3581 if (ksmbd_override_fsids(work)) {
3582 rsp->hdr.Status = STATUS_NO_MEMORY;
3583 smb2_set_err_rsp(work);
3584 return -ENOMEM;
3585 }
3586
3587 rc = verify_info_level(req->FileInformationClass);
3588 if (rc) {
3589 rc = -EFAULT;
3590 goto err_out2;
3591 }
3592
3593 dir_fp = ksmbd_lookup_fd_slow(work,
3594 le64_to_cpu(req->VolatileFileId),
3595 le64_to_cpu(req->PersistentFileId));
3596 if (!dir_fp) {
3597 rc = -EBADF;
3598 goto err_out2;
3599 }
3600
3601 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
64b39f4a 3602 inode_permission(&init_user_ns, file_inode(dir_fp->filp), MAY_READ | MAY_EXEC)) {
e2f34481
NJ
3603 ksmbd_err("no right to enumerate directory (%s)\n",
3604 FP_FILENAME(dir_fp));
3605 rc = -EACCES;
3606 goto err_out2;
3607 }
3608
3609 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
3610 ksmbd_err("can't do query dir for a file\n");
3611 rc = -EINVAL;
3612 goto err_out2;
3613 }
3614
3615 srch_flag = req->Flags;
3616 srch_ptr = smb_strndup_from_utf16(req->Buffer,
3617 le16_to_cpu(req->FileNameLength), 1,
3618 conn->local_nls);
3619 if (IS_ERR(srch_ptr)) {
3620 ksmbd_debug(SMB, "Search Pattern not found\n");
3621 rc = -EINVAL;
3622 goto err_out2;
64b39f4a 3623 } else {
e2f34481 3624 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
64b39f4a 3625 }
e2f34481
NJ
3626
3627 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3628
3629 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3630 ksmbd_debug(SMB, "Restart directory scan\n");
3631 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3632 restart_ctx(&dir_fp->readdir_data.ctx);
3633 }
3634
3635 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3636 d_info.wptr = (char *)rsp->Buffer;
3637 d_info.rptr = (char *)rsp->Buffer;
64b39f4a 3638 d_info.out_buf_len = (work->response_sz - (get_rfc1002_len(rsp_org) + 4));
e2f34481 3639 d_info.out_buf_len = min_t(int, d_info.out_buf_len,
64b39f4a 3640 le32_to_cpu(req->OutputBufferLength)) - sizeof(struct smb2_query_directory_rsp);
e2f34481
NJ
3641 d_info.flags = srch_flag;
3642
3643 /*
3644 * reserve dot and dotdot entries in head of buffer
3645 * in first response
3646 */
3647 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
3648 dir_fp, &d_info, srch_ptr, smb2_populate_readdir_entry);
3649 if (rc == -ENOSPC)
3650 rc = 0;
3651 else if (rc)
3652 goto err_out;
3653
3654 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3655 d_info.hide_dot_file = true;
3656
3657 buffer_sz = d_info.out_buf_len;
3658 d_info.rptr = d_info.wptr;
3659 query_dir_private.work = work;
3660 query_dir_private.search_pattern = srch_ptr;
3661 query_dir_private.dir_fp = dir_fp;
3662 query_dir_private.d_info = &d_info;
3663 query_dir_private.info_level = req->FileInformationClass;
3664 dir_fp->readdir_data.private = &query_dir_private;
3665 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3666
3667 rc = ksmbd_vfs_readdir(dir_fp->filp, &dir_fp->readdir_data);
3668 if (rc == 0)
3669 restart_ctx(&dir_fp->readdir_data.ctx);
3670 if (rc == -ENOSPC)
3671 rc = 0;
3672 if (rc)
3673 goto err_out;
3674
3675 d_info.wptr = d_info.rptr;
3676 d_info.out_buf_len = buffer_sz;
3677 rc = process_query_dir_entries(&query_dir_private);
3678 if (rc)
3679 goto err_out;
3680
3681 if (!d_info.data_count && d_info.out_buf_len >= 0) {
64b39f4a 3682 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
e2f34481 3683 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
64b39f4a 3684 } else {
e2f34481
NJ
3685 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3686 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3687 }
3688 rsp->StructureSize = cpu_to_le16(9);
3689 rsp->OutputBufferOffset = cpu_to_le16(0);
3690 rsp->OutputBufferLength = cpu_to_le32(0);
3691 rsp->Buffer[0] = 0;
3692 inc_rfc1001_len(rsp_org, 9);
3693 } else {
3694 ((struct file_directory_info *)
3695 ((char *)rsp->Buffer + d_info.last_entry_offset))
3696 ->NextEntryOffset = 0;
3697
3698 rsp->StructureSize = cpu_to_le16(9);
3699 rsp->OutputBufferOffset = cpu_to_le16(72);
3700 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
3701 inc_rfc1001_len(rsp_org, 8 + d_info.data_count);
3702 }
3703
3704 kfree(srch_ptr);
3705 ksmbd_fd_put(work, dir_fp);
3706 ksmbd_revert_fsids(work);
3707 return 0;
3708
3709err_out:
3710 ksmbd_err("error while processing smb2 query dir rc = %d\n", rc);
3711 kfree(srch_ptr);
3712
3713err_out2:
3714 if (rc == -EINVAL)
3715 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3716 else if (rc == -EACCES)
3717 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3718 else if (rc == -ENOENT)
3719 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3720 else if (rc == -EBADF)
3721 rsp->hdr.Status = STATUS_FILE_CLOSED;
3722 else if (rc == -ENOMEM)
3723 rsp->hdr.Status = STATUS_NO_MEMORY;
3724 else if (rc == -EFAULT)
3725 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3726 if (!rsp->hdr.Status)
3727 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3728
3729 smb2_set_err_rsp(work);
3730 ksmbd_fd_put(work, dir_fp);
3731 ksmbd_revert_fsids(work);
3732 return 0;
3733}
3734
3735/**
3736 * buffer_check_err() - helper function to check buffer errors
3737 * @reqOutputBufferLength: max buffer length expected in command response
3738 * @rsp: query info response buffer contains output buffer length
3739 * @infoclass_size: query info class response buffer size
3740 *
3741 * Return: 0 on success, otherwise error
3742 */
3743static int buffer_check_err(int reqOutputBufferLength,
64b39f4a 3744 struct smb2_query_info_rsp *rsp, int infoclass_size)
e2f34481
NJ
3745{
3746 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
3747 if (reqOutputBufferLength < infoclass_size) {
3748 ksmbd_err("Invalid Buffer Size Requested\n");
3749 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
64b39f4a 3750 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4);
e2f34481
NJ
3751 return -EINVAL;
3752 }
3753
3754 ksmbd_debug(SMB, "Buffer Overflow\n");
3755 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
64b39f4a
NJ
3756 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4 +
3757 reqOutputBufferLength);
3758 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
e2f34481
NJ
3759 }
3760 return 0;
3761}
3762
3763static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp)
3764{
3765 struct smb2_file_standard_info *sinfo;
3766
3767 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
3768
3769 sinfo->AllocationSize = cpu_to_le64(4096);
3770 sinfo->EndOfFile = cpu_to_le64(0);
3771 sinfo->NumberOfLinks = cpu_to_le32(1);
3772 sinfo->DeletePending = 1;
3773 sinfo->Directory = 0;
3774 rsp->OutputBufferLength =
3775 cpu_to_le32(sizeof(struct smb2_file_standard_info));
3776 inc_rfc1001_len(rsp, sizeof(struct smb2_file_standard_info));
3777}
3778
3779static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp,
64b39f4a 3780 u64 num)
e2f34481
NJ
3781{
3782 struct smb2_file_internal_info *file_info;
3783
3784 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
3785
3786 /* any unique number */
3787 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
3788 rsp->OutputBufferLength =
3789 cpu_to_le32(sizeof(struct smb2_file_internal_info));
3790 inc_rfc1001_len(rsp, sizeof(struct smb2_file_internal_info));
3791}
3792
e2f34481 3793static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
64b39f4a
NJ
3794 struct smb2_query_info_req *req,
3795 struct smb2_query_info_rsp *rsp)
e2f34481 3796{
64b39f4a 3797 u64 id;
e2f34481
NJ
3798 int rc;
3799
3800 /*
3801 * Windows can sometime send query file info request on
3802 * pipe without opening it, checking error condition here
3803 */
3804 id = le64_to_cpu(req->VolatileFileId);
3805 if (!ksmbd_session_rpc_method(sess, id))
3806 return -ENOENT;
3807
3808 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
3809 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
3810
3811 switch (req->FileInfoClass) {
3812 case FILE_STANDARD_INFORMATION:
3813 get_standard_info_pipe(rsp);
3814 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
3815 rsp, FILE_STANDARD_INFORMATION_SIZE);
3816 break;
3817 case FILE_INTERNAL_INFORMATION:
3818 get_internal_info_pipe(rsp, id);
3819 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
3820 rsp, FILE_INTERNAL_INFORMATION_SIZE);
3821 break;
3822 default:
3823 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
3824 req->FileInfoClass);
3825 rc = -EOPNOTSUPP;
3826 }
3827 return rc;
3828}
3829
3830/**
3831 * smb2_get_ea() - handler for smb2 get extended attribute command
3832 * @work: smb work containing query info command buffer
95fa1ce9
HL
3833 * @fp: ksmbd_file pointer
3834 * @req: get extended attribute request
3835 * @rsp: response buffer pointer
3836 * @rsp_org: base response buffer pointer in case of chained response
e2f34481
NJ
3837 *
3838 * Return: 0 on success, otherwise error
3839 */
64b39f4a
NJ
3840static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
3841 struct smb2_query_info_req *req,
3842 struct smb2_query_info_rsp *rsp, void *rsp_org)
e2f34481
NJ
3843{
3844 struct smb2_ea_info *eainfo, *prev_eainfo;
3845 char *name, *ptr, *xattr_list = NULL, *buf;
3846 int rc, name_len, value_len, xattr_list_len, idx;
3847 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
3848 struct smb2_ea_info_req *ea_req = NULL;
3849 struct path *path;
3850
3851 if (!(fp->daccess & FILE_READ_EA_LE)) {
3852 ksmbd_err("Not permitted to read ext attr : 0x%x\n",
3853 fp->daccess);
3854 return -EACCES;
3855 }
3856
3857 path = &fp->filp->f_path;
3858 /* single EA entry is requested with given user.* name */
64b39f4a 3859 if (req->InputBufferLength) {
e2f34481 3860 ea_req = (struct smb2_ea_info_req *)req->Buffer;
64b39f4a 3861 } else {
e2f34481
NJ
3862 /* need to send all EAs, if no specific EA is requested*/
3863 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
3864 ksmbd_debug(SMB,
3865 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
3866 le32_to_cpu(req->Flags));
3867 }
3868
3869 buf_free_len = work->response_sz -
3870 (get_rfc1002_len(rsp_org) + 4) -
3871 sizeof(struct smb2_query_info_rsp);
3872
3873 if (le32_to_cpu(req->OutputBufferLength) < buf_free_len)
3874 buf_free_len = le32_to_cpu(req->OutputBufferLength);
3875
3876 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
3877 if (rc < 0) {
3878 rsp->hdr.Status = STATUS_INVALID_HANDLE;
3879 goto out;
3880 } else if (!rc) { /* there is no EA in the file */
3881 ksmbd_debug(SMB, "no ea data in the file\n");
3882 goto done;
3883 }
3884 xattr_list_len = rc;
3885
3886 ptr = (char *)rsp->Buffer;
3887 eainfo = (struct smb2_ea_info *)ptr;
3888 prev_eainfo = eainfo;
3889 idx = 0;
3890
3891 while (idx < xattr_list_len) {
3892 name = xattr_list + idx;
3893 name_len = strlen(name);
3894
3895 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
3896 idx += name_len + 1;
3897
3898 /*
3899 * CIFS does not support EA other than user.* namespace,
3900 * still keep the framework generic, to list other attrs
3901 * in future.
3902 */
3903 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
3904 continue;
3905
3906 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
64b39f4a 3907 STREAM_PREFIX_LEN))
e2f34481
NJ
3908 continue;
3909
3910 if (req->InputBufferLength &&
64b39f4a
NJ
3911 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
3912 ea_req->EaNameLength))
e2f34481
NJ
3913 continue;
3914
3915 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
64b39f4a 3916 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
e2f34481
NJ
3917 continue;
3918
3919 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
3920 name_len -= XATTR_USER_PREFIX_LEN;
3921
3922 ptr = (char *)(&eainfo->name + name_len + 1);
3923 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
3924 name_len + 1);
3925 /* bailout if xattr can't fit in buf_free_len */
3926 value_len = ksmbd_vfs_getxattr(path->dentry, name, &buf);
3927 if (value_len <= 0) {
3928 rc = -ENOENT;
3929 rsp->hdr.Status = STATUS_INVALID_HANDLE;
3930 goto out;
3931 }
3932
3933 buf_free_len -= value_len;
3934 if (buf_free_len < 0) {
79f6b11a 3935 kfree(buf);
e2f34481
NJ
3936 break;
3937 }
3938
3939 memcpy(ptr, buf, value_len);
79f6b11a 3940 kfree(buf);
e2f34481
NJ
3941
3942 ptr += value_len;
3943 eainfo->Flags = 0;
3944 eainfo->EaNameLength = name_len;
3945
64b39f4a 3946 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
e2f34481
NJ
3947 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
3948 name_len);
3949 else
3950 memcpy(eainfo->name, name, name_len);
3951
3952 eainfo->name[name_len] = '\0';
3953 eainfo->EaValueLength = cpu_to_le16(value_len);
3954 next_offset = offsetof(struct smb2_ea_info, name) +
3955 name_len + 1 + value_len;
3956
3957 /* align next xattr entry at 4 byte bundary */
3958 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
3959 if (alignment_bytes) {
3960 memset(ptr, '\0', alignment_bytes);
3961 ptr += alignment_bytes;
3962 next_offset += alignment_bytes;
3963 buf_free_len -= alignment_bytes;
3964 }
3965 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
3966 prev_eainfo = eainfo;
3967 eainfo = (struct smb2_ea_info *)ptr;
3968 rsp_data_cnt += next_offset;
3969
3970 if (req->InputBufferLength) {
3971 ksmbd_debug(SMB, "single entry requested\n");
3972 break;
3973 }
3974 }
3975
3976 /* no more ea entries */
3977 prev_eainfo->NextEntryOffset = 0;
3978done:
3979 rc = 0;
3980 if (rsp_data_cnt == 0)
3981 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
3982 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
3983 inc_rfc1001_len(rsp_org, rsp_data_cnt);
3984out:
79f6b11a 3985 kvfree(xattr_list);
e2f34481
NJ
3986 return rc;
3987}
3988
3989static void get_file_access_info(struct smb2_query_info_rsp *rsp,
64b39f4a 3990 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
3991{
3992 struct smb2_file_access_info *file_info;
3993
3994 file_info = (struct smb2_file_access_info *)rsp->Buffer;
3995 file_info->AccessFlags = fp->daccess;
3996 rsp->OutputBufferLength =
3997 cpu_to_le32(sizeof(struct smb2_file_access_info));
3998 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
3999}
4000
4001static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4002 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
4003{
4004 struct smb2_file_all_info *basic_info;
4005 struct kstat stat;
4006 u64 time;
4007
4008 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4009 ksmbd_err("no right to read the attributes : 0x%x\n",
4010 fp->daccess);
4011 return -EACCES;
4012 }
4013
4014 basic_info = (struct smb2_file_all_info *)rsp->Buffer;
4015 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4016 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4017 time = ksmbd_UnixTimeToNT(stat.atime);
4018 basic_info->LastAccessTime = cpu_to_le64(time);
4019 time = ksmbd_UnixTimeToNT(stat.mtime);
4020 basic_info->LastWriteTime = cpu_to_le64(time);
4021 time = ksmbd_UnixTimeToNT(stat.ctime);
4022 basic_info->ChangeTime = cpu_to_le64(time);
4023 basic_info->Attributes = fp->f_ci->m_fattr;
4024 basic_info->Pad1 = 0;
4025 rsp->OutputBufferLength =
64b39f4a 4026 cpu_to_le32(offsetof(struct smb2_file_all_info, AllocationSize));
e2f34481
NJ
4027 inc_rfc1001_len(rsp_org, offsetof(struct smb2_file_all_info,
4028 AllocationSize));
4029 return 0;
4030}
4031
4032static unsigned long long get_allocation_size(struct inode *inode,
4033 struct kstat *stat)
4034{
4035 unsigned long long alloc_size = 0;
4036
4037 if (!S_ISDIR(stat->mode)) {
4038 if ((inode->i_blocks << 9) <= stat->size)
4039 alloc_size = stat->size;
4040 else
4041 alloc_size = inode->i_blocks << 9;
e2f34481
NJ
4042 }
4043
4044 return alloc_size;
4045}
4046
4047static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4048 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
4049{
4050 struct smb2_file_standard_info *sinfo;
4051 unsigned int delete_pending;
4052 struct inode *inode;
4053 struct kstat stat;
4054
4055 inode = FP_INODE(fp);
4056 generic_fillattr(&init_user_ns, inode, &stat);
4057
4058 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4059 delete_pending = ksmbd_inode_pending_delete(fp);
4060
4061 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4062 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4063 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4064 sinfo->DeletePending = delete_pending;
4065 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4066 rsp->OutputBufferLength =
4067 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4068 inc_rfc1001_len(rsp_org,
4069 sizeof(struct smb2_file_standard_info));
4070}
4071
4072static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4073 void *rsp_org)
e2f34481
NJ
4074{
4075 struct smb2_file_alignment_info *file_info;
4076
4077 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4078 file_info->AlignmentRequirement = 0;
4079 rsp->OutputBufferLength =
4080 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4081 inc_rfc1001_len(rsp_org,
4082 sizeof(struct smb2_file_alignment_info));
4083}
4084
4085static int get_file_all_info(struct ksmbd_work *work,
64b39f4a
NJ
4086 struct smb2_query_info_rsp *rsp, struct ksmbd_file *fp,
4087 void *rsp_org)
e2f34481
NJ
4088{
4089 struct ksmbd_conn *conn = work->conn;
4090 struct smb2_file_all_info *file_info;
4091 unsigned int delete_pending;
4092 struct inode *inode;
4093 struct kstat stat;
4094 int conv_len;
4095 char *filename;
4096 u64 time;
4097
4098 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4099 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
4100 fp->daccess);
4101 return -EACCES;
4102 }
4103
4104 filename = convert_to_nt_pathname(fp->filename,
4105 work->tcon->share_conf->path);
4106 if (!filename)
4107 return -ENOMEM;
4108
4109 inode = FP_INODE(fp);
4110 generic_fillattr(&init_user_ns, inode, &stat);
4111
4112 ksmbd_debug(SMB, "filename = %s\n", filename);
4113 delete_pending = ksmbd_inode_pending_delete(fp);
4114 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4115
4116 file_info->CreationTime = cpu_to_le64(fp->create_time);
4117 time = ksmbd_UnixTimeToNT(stat.atime);
4118 file_info->LastAccessTime = cpu_to_le64(time);
4119 time = ksmbd_UnixTimeToNT(stat.mtime);
4120 file_info->LastWriteTime = cpu_to_le64(time);
4121 time = ksmbd_UnixTimeToNT(stat.ctime);
4122 file_info->ChangeTime = cpu_to_le64(time);
4123 file_info->Attributes = fp->f_ci->m_fattr;
4124 file_info->Pad1 = 0;
4125 file_info->AllocationSize =
4126 cpu_to_le64(get_allocation_size(inode, &stat));
4127 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4128 file_info->NumberOfLinks =
4129 cpu_to_le32(get_nlink(&stat) - delete_pending);
4130 file_info->DeletePending = delete_pending;
4131 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4132 file_info->Pad2 = 0;
4133 file_info->IndexNumber = cpu_to_le64(stat.ino);
4134 file_info->EASize = 0;
4135 file_info->AccessFlags = fp->daccess;
4136 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4137 file_info->Mode = fp->coption;
4138 file_info->AlignmentRequirement = 0;
4139 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName,
4140 filename,
4141 PATH_MAX,
4142 conn->local_nls,
4143 0);
4144 conv_len *= 2;
4145 file_info->FileNameLength = cpu_to_le32(conv_len);
4146 rsp->OutputBufferLength =
4147 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4148 kfree(filename);
4149 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4150 return 0;
4151}
4152
4153static void get_file_alternate_info(struct ksmbd_work *work,
64b39f4a
NJ
4154 struct smb2_query_info_rsp *rsp, struct ksmbd_file *fp,
4155 void *rsp_org)
e2f34481
NJ
4156{
4157 struct ksmbd_conn *conn = work->conn;
4158 struct smb2_file_alt_name_info *file_info;
4159 int conv_len;
4160 char *filename;
4161
4162 filename = (char *)FP_FILENAME(fp);
4163 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4164 conv_len = ksmbd_extract_shortname(conn,
4165 filename,
4166 file_info->FileName);
4167 file_info->FileNameLength = cpu_to_le32(conv_len);
4168 rsp->OutputBufferLength =
4169 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4170 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4171}
4172
4173static void get_file_stream_info(struct ksmbd_work *work,
64b39f4a
NJ
4174 struct smb2_query_info_rsp *rsp, struct ksmbd_file *fp,
4175 void *rsp_org)
e2f34481
NJ
4176{
4177 struct ksmbd_conn *conn = work->conn;
4178 struct smb2_file_stream_info *file_info;
4179 char *stream_name, *xattr_list = NULL, *stream_buf;
4180 struct kstat stat;
4181 struct path *path = &fp->filp->f_path;
4182 ssize_t xattr_list_len;
4183 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
4184
4185 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4186 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4187
4188 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4189 if (xattr_list_len < 0) {
4190 goto out;
4191 } else if (!xattr_list_len) {
4192 ksmbd_debug(SMB, "empty xattr in the file\n");
4193 goto out;
4194 }
4195
4196 while (idx < xattr_list_len) {
4197 stream_name = xattr_list + idx;
4198 streamlen = strlen(stream_name);
4199 idx += streamlen + 1;
4200
4201 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4202
4203 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
64b39f4a 4204 STREAM_PREFIX, STREAM_PREFIX_LEN))
e2f34481
NJ
4205 continue;
4206
4207 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4208 STREAM_PREFIX_LEN);
4209 streamlen = stream_name_len;
4210
4211 /* plus : size */
4212 streamlen += 1;
4213 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4214 if (!stream_buf)
4215 break;
4216
4217 streamlen = snprintf(stream_buf, streamlen + 1,
4218 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
4219
4220 file_info = (struct smb2_file_stream_info *)
4221 &rsp->Buffer[nbytes];
4222 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
4223 stream_buf,
4224 streamlen,
4225 conn->local_nls,
4226 0);
4227 streamlen *= 2;
4228 kfree(stream_buf);
4229 file_info->StreamNameLength = cpu_to_le32(streamlen);
4230 file_info->StreamSize = cpu_to_le64(stream_name_len);
4231 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4232
4233 next = sizeof(struct smb2_file_stream_info) + streamlen;
4234 nbytes += next;
4235 file_info->NextEntryOffset = cpu_to_le32(next);
4236 }
4237
4238 if (nbytes) {
4239 file_info = (struct smb2_file_stream_info *)
4240 &rsp->Buffer[nbytes];
4241 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
4242 "::$DATA", 7, conn->local_nls, 0);
4243 streamlen *= 2;
4244 file_info->StreamNameLength = cpu_to_le32(streamlen);
4245 file_info->StreamSize = S_ISDIR(stat.mode) ? 0 :
4246 cpu_to_le64(stat.size);
4247 file_info->StreamAllocationSize = S_ISDIR(stat.mode) ? 0 :
4248 cpu_to_le64(stat.size);
4249 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4250 }
4251
4252 /* last entry offset should be 0 */
4253 file_info->NextEntryOffset = 0;
4254out:
79f6b11a 4255 kvfree(xattr_list);
e2f34481
NJ
4256
4257 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4258 inc_rfc1001_len(rsp_org, nbytes);
4259}
4260
4261static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4262 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
4263{
4264 struct smb2_file_internal_info *file_info;
4265 struct kstat stat;
4266
4267 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4268 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4269 file_info->IndexNumber = cpu_to_le64(stat.ino);
4270 rsp->OutputBufferLength =
4271 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4272 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4273}
4274
4275static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4276 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
4277{
4278 struct smb2_file_ntwrk_info *file_info;
4279 struct inode *inode;
4280 struct kstat stat;
4281 u64 time;
4282
4283 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4284 ksmbd_err("no right to read the attributes : 0x%x\n",
4285 fp->daccess);
4286 return -EACCES;
4287 }
4288
4289 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4290
4291 inode = FP_INODE(fp);
4292 generic_fillattr(&init_user_ns, inode, &stat);
4293
4294 file_info->CreationTime = cpu_to_le64(fp->create_time);
4295 time = ksmbd_UnixTimeToNT(stat.atime);
4296 file_info->LastAccessTime = cpu_to_le64(time);
4297 time = ksmbd_UnixTimeToNT(stat.mtime);
4298 file_info->LastWriteTime = cpu_to_le64(time);
4299 time = ksmbd_UnixTimeToNT(stat.ctime);
4300 file_info->ChangeTime = cpu_to_le64(time);
4301 file_info->Attributes = fp->f_ci->m_fattr;
4302 file_info->AllocationSize =
4303 cpu_to_le64(get_allocation_size(inode, &stat));
4304 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4305 file_info->Reserved = cpu_to_le32(0);
4306 rsp->OutputBufferLength =
4307 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4308 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4309 return 0;
4310}
4311
64b39f4a 4312static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
e2f34481
NJ
4313{
4314 struct smb2_file_ea_info *file_info;
4315
4316 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4317 file_info->EASize = 0;
4318 rsp->OutputBufferLength =
4319 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4320 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4321}
4322
4323static void get_file_position_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4324 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
4325{
4326 struct smb2_file_pos_info *file_info;
4327
4328 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4329 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4330 rsp->OutputBufferLength =
4331 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4332 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4333}
4334
4335static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4336 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
4337{
4338 struct smb2_file_mode_info *file_info;
4339
4340 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4341 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4342 rsp->OutputBufferLength =
4343 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4344 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4345}
4346
4347static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4348 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
4349{
4350 struct smb2_file_comp_info *file_info;
4351 struct kstat stat;
4352
4353 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4354
4355 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4356 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4357 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4358 file_info->CompressionUnitShift = 0;
4359 file_info->ChunkShift = 0;
4360 file_info->ClusterShift = 0;
4361 memset(&file_info->Reserved[0], 0, 3);
4362
4363 rsp->OutputBufferLength =
4364 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4365 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4366}
4367
4368static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4369 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
4370{
4371 struct smb2_file_attr_tag_info *file_info;
4372
4373 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4374 ksmbd_err("no right to read the attributes : 0x%x\n",
4375 fp->daccess);
4376 return -EACCES;
4377 }
4378
4379 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4380 file_info->FileAttributes = fp->f_ci->m_fattr;
4381 file_info->ReparseTag = 0;
4382 rsp->OutputBufferLength =
4383 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
4384 inc_rfc1001_len(rsp_org,
4385 sizeof(struct smb2_file_attr_tag_info));
4386 return 0;
4387}
4388
4389static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
64b39f4a 4390 struct ksmbd_file *fp, void *rsp_org)
e2f34481
NJ
4391{
4392 struct smb311_posix_qinfo *file_info;
4393 struct inode *inode = FP_INODE(fp);
4394 u64 time;
4395
4396 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4397 file_info->CreationTime = cpu_to_le64(fp->create_time);
4398 time = ksmbd_UnixTimeToNT(inode->i_atime);
4399 file_info->LastAccessTime = cpu_to_le64(time);
4400 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4401 file_info->LastWriteTime = cpu_to_le64(time);
4402 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4403 file_info->ChangeTime = cpu_to_le64(time);
4404 file_info->DosAttributes = fp->f_ci->m_fattr;
4405 file_info->Inode = cpu_to_le64(inode->i_ino);
4406 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4407 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4408 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4409 file_info->Mode = cpu_to_le32(inode->i_mode);
4410 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4411 rsp->OutputBufferLength =
4412 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
64b39f4a 4413 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
e2f34481
NJ
4414 return 0;
4415}
4416
e2f34481 4417static int smb2_get_info_file(struct ksmbd_work *work,
64b39f4a
NJ
4418 struct smb2_query_info_req *req,
4419 struct smb2_query_info_rsp *rsp, void *rsp_org)
e2f34481
NJ
4420{
4421 struct ksmbd_file *fp;
4422 int fileinfoclass = 0;
4423 int rc = 0;
4424 int file_infoclass_size;
4425 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4426
4427 if (test_share_config_flag(work->tcon->share_conf,
64b39f4a 4428 KSMBD_SHARE_FLAG_PIPE)) {
e2f34481
NJ
4429 /* smb2 info file called for pipe */
4430 return smb2_get_info_file_pipe(work->sess, req, rsp);
4431 }
4432
4433 if (work->next_smb2_rcv_hdr_off) {
4434 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
4435 ksmbd_debug(SMB, "Compound request set FID = %u\n",
4436 work->compound_fid);
4437 id = work->compound_fid;
4438 pid = work->compound_pfid;
4439 }
4440 }
4441
4442 if (!HAS_FILE_ID(id)) {
4443 id = le64_to_cpu(req->VolatileFileId);
4444 pid = le64_to_cpu(req->PersistentFileId);
4445 }
4446
4447 fp = ksmbd_lookup_fd_slow(work, id, pid);
4448 if (!fp)
4449 return -ENOENT;
4450
4451 fileinfoclass = req->FileInfoClass;
4452
4453 switch (fileinfoclass) {
4454 case FILE_ACCESS_INFORMATION:
4455 get_file_access_info(rsp, fp, rsp_org);
4456 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4457 break;
4458
4459 case FILE_BASIC_INFORMATION:
4460 rc = get_file_basic_info(rsp, fp, rsp_org);
4461 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4462 break;
4463
4464 case FILE_STANDARD_INFORMATION:
4465 get_file_standard_info(rsp, fp, rsp_org);
4466 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4467 break;
4468
4469 case FILE_ALIGNMENT_INFORMATION:
4470 get_file_alignment_info(rsp, rsp_org);
4471 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4472 break;
4473
4474 case FILE_ALL_INFORMATION:
4475 rc = get_file_all_info(work, rsp, fp, rsp_org);
4476 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4477 break;
4478
4479 case FILE_ALTERNATE_NAME_INFORMATION:
4480 get_file_alternate_info(work, rsp, fp, rsp_org);
4481 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4482 break;
4483
4484 case FILE_STREAM_INFORMATION:
4485 get_file_stream_info(work, rsp, fp, rsp_org);
4486 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4487 break;
4488
4489 case FILE_INTERNAL_INFORMATION:
4490 get_file_internal_info(rsp, fp, rsp_org);
4491 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4492 break;
4493
4494 case FILE_NETWORK_OPEN_INFORMATION:
4495 rc = get_file_network_open_info(rsp, fp, rsp_org);
4496 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4497 break;
4498
4499 case FILE_EA_INFORMATION:
4500 get_file_ea_info(rsp, rsp_org);
4501 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4502 break;
4503
4504 case FILE_FULL_EA_INFORMATION:
4505 rc = smb2_get_ea(work, fp, req, rsp, rsp_org);
4506 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4507 break;
4508
4509 case FILE_POSITION_INFORMATION:
4510 get_file_position_info(rsp, fp, rsp_org);
4511 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4512 break;
4513
4514 case FILE_MODE_INFORMATION:
4515 get_file_mode_info(rsp, fp, rsp_org);
4516 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4517 break;
4518
4519 case FILE_COMPRESSION_INFORMATION:
4520 get_file_compression_info(rsp, fp, rsp_org);
4521 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4522 break;
4523
4524 case FILE_ATTRIBUTE_TAG_INFORMATION:
4525 rc = get_file_attribute_tag_info(rsp, fp, rsp_org);
4526 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4527 break;
4528 case SMB_FIND_FILE_POSIX_INFO:
4529 if (!work->tcon->posix_extensions) {
4530 ksmbd_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4531 rc = -EOPNOTSUPP;
4532 } else {
4533 rc = find_file_posix_info(rsp, fp, rsp_org);
4534 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4535 }
4536 break;
4537 default:
4538 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4539 fileinfoclass);
4540 rc = -EOPNOTSUPP;
4541 }
4542 if (!rc)
4543 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4544 rsp,
4545 file_infoclass_size);
4546 ksmbd_fd_put(work, fp);
4547 return rc;
4548}
4549
e2f34481 4550static int smb2_get_info_filesystem(struct ksmbd_work *work,
64b39f4a
NJ
4551 struct smb2_query_info_req *req,
4552 struct smb2_query_info_rsp *rsp, void *rsp_org)
e2f34481
NJ
4553{
4554 struct ksmbd_session *sess = work->sess;
4555 struct ksmbd_conn *conn = sess->conn;
4556 struct ksmbd_share_config *share = work->tcon->share_conf;
4557 int fsinfoclass = 0;
4558 struct kstatfs stfs;
4559 struct path path;
4560 int rc = 0, len;
4561 int fs_infoclass_size = 0;
4562
4563 rc = ksmbd_vfs_kern_path(share->path, LOOKUP_FOLLOW, &path, 0);
4564 if (rc) {
4565 ksmbd_err("cannot create vfs path\n");
4566 return -EIO;
4567 }
4568
4569 rc = vfs_statfs(&path, &stfs);
4570 if (rc) {
4571 ksmbd_err("cannot do stat of path %s\n", share->path);
4572 path_put(&path);
4573 return -EIO;
4574 }
4575
4576 fsinfoclass = req->FileInfoClass;
4577
4578 switch (fsinfoclass) {
4579 case FS_DEVICE_INFORMATION:
4580 {
4581 struct filesystem_device_info *info;
4582
4583 info = (struct filesystem_device_info *)rsp->Buffer;
4584
4585 info->DeviceType = cpu_to_le32(stfs.f_type);
4586 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4587 rsp->OutputBufferLength = cpu_to_le32(8);
4588 inc_rfc1001_len(rsp_org, 8);
4589 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4590 break;
4591 }
4592 case FS_ATTRIBUTE_INFORMATION:
4593 {
4594 struct filesystem_attribute_info *info;
4595 size_t sz;
4596
4597 info = (struct filesystem_attribute_info *)rsp->Buffer;
4598 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4599 FILE_PERSISTENT_ACLS |
4600 FILE_UNICODE_ON_DISK |
4601 FILE_CASE_PRESERVED_NAMES |
4602 FILE_CASE_SENSITIVE_SEARCH);
4603
4604 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4605
4606 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4607 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4608 "NTFS", PATH_MAX, conn->local_nls, 0);
4609 len = len * 2;
4610 info->FileSystemNameLen = cpu_to_le32(len);
4611 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4612 rsp->OutputBufferLength = cpu_to_le32(sz);
4613 inc_rfc1001_len(rsp_org, sz);
4614 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4615 break;
4616 }
4617 case FS_VOLUME_INFORMATION:
4618 {
4619 struct filesystem_vol_info *info;
4620 size_t sz;
4621
4622 info = (struct filesystem_vol_info *)(rsp->Buffer);
4623 info->VolumeCreationTime = 0;
4624 /* Taking dummy value of serial number*/
4625 info->SerialNumber = cpu_to_le32(0xbc3ac512);
4626 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4627 share->name, PATH_MAX,
4628 conn->local_nls, 0);
4629 len = len * 2;
4630 info->VolumeLabelSize = cpu_to_le32(len);
4631 info->Reserved = 0;
4632 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4633 rsp->OutputBufferLength = cpu_to_le32(sz);
4634 inc_rfc1001_len(rsp_org, sz);
4635 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4636 break;
4637 }
4638 case FS_SIZE_INFORMATION:
4639 {
4640 struct filesystem_info *info;
4641 unsigned short logical_sector_size;
4642
4643 info = (struct filesystem_info *)(rsp->Buffer);
4644 logical_sector_size =
4645 ksmbd_vfs_logical_sector_size(d_inode(path.dentry));
4646
4647 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4648 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
4649 info->SectorsPerAllocationUnit = cpu_to_le32(stfs.f_bsize >> 9);
4650 info->BytesPerSector = cpu_to_le32(logical_sector_size);
4651 rsp->OutputBufferLength = cpu_to_le32(24);
4652 inc_rfc1001_len(rsp_org, 24);
4653 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4654 break;
4655 }
4656 case FS_FULL_SIZE_INFORMATION:
4657 {
4658 struct smb2_fs_full_size_info *info;
4659 unsigned short logical_sector_size;
4660
4661 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
4662 logical_sector_size =
4663 ksmbd_vfs_logical_sector_size(d_inode(path.dentry));
4664
4665 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4666 info->CallerAvailableAllocationUnits =
4667 cpu_to_le64(stfs.f_bavail);
4668 info->ActualAvailableAllocationUnits =
4669 cpu_to_le64(stfs.f_bfree);
4670 info->SectorsPerAllocationUnit = cpu_to_le32(stfs.f_bsize >> 9);
4671 info->BytesPerSector = cpu_to_le32(logical_sector_size);
4672 rsp->OutputBufferLength = cpu_to_le32(32);
4673 inc_rfc1001_len(rsp_org, 32);
4674 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4675 break;
4676 }
4677 case FS_OBJECT_ID_INFORMATION:
4678 {
4679 struct object_id_info *info;
4680
4681 info = (struct object_id_info *)(rsp->Buffer);
4682
4683 if (!user_guest(sess->user))
4684 memcpy(info->objid, user_passkey(sess->user), 16);
4685 else
4686 memset(info->objid, 0, 16);
4687
4688 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4689 info->extended_info.version = cpu_to_le32(1);
4690 info->extended_info.release = cpu_to_le32(1);
4691 info->extended_info.rel_date = 0;
64b39f4a 4692 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
e2f34481
NJ
4693 rsp->OutputBufferLength = cpu_to_le32(64);
4694 inc_rfc1001_len(rsp_org, 64);
4695 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4696 break;
4697 }
4698 case FS_SECTOR_SIZE_INFORMATION:
4699 {
4700 struct smb3_fs_ss_info *info;
4701 struct ksmbd_fs_sector_size fs_ss;
4702
4703 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
4704 ksmbd_vfs_smb2_sector_size(d_inode(path.dentry), &fs_ss);
4705
4706 info->LogicalBytesPerSector =
4707 cpu_to_le32(fs_ss.logical_sector_size);
4708 info->PhysicalBytesPerSectorForAtomicity =
4709 cpu_to_le32(fs_ss.physical_sector_size);
4710 info->PhysicalBytesPerSectorForPerf =
4711 cpu_to_le32(fs_ss.optimal_io_size);
4712 info->FSEffPhysicalBytesPerSectorForAtomicity =
4713 cpu_to_le32(fs_ss.optimal_io_size);
4714 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4715 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
4716 info->ByteOffsetForSectorAlignment = 0;
4717 info->ByteOffsetForPartitionAlignment = 0;
4718 rsp->OutputBufferLength = cpu_to_le32(28);
4719 inc_rfc1001_len(rsp_org, 28);
4720 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
4721 break;
4722 }
4723 case FS_CONTROL_INFORMATION:
4724 {
4725 /*
4726 * TODO : The current implementation is based on
4727 * test result with win7(NTFS) server. It's need to
4728 * modify this to get valid Quota values
4729 * from Linux kernel
4730 */
4731 struct smb2_fs_control_info *info;
4732
4733 info = (struct smb2_fs_control_info *)(rsp->Buffer);
4734 info->FreeSpaceStartFiltering = 0;
4735 info->FreeSpaceThreshold = 0;
4736 info->FreeSpaceStopFiltering = 0;
4737 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
4738 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
4739 info->Padding = 0;
4740 rsp->OutputBufferLength = cpu_to_le32(48);
4741 inc_rfc1001_len(rsp_org, 48);
4742 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
4743 break;
4744 }
4745 case FS_POSIX_INFORMATION:
4746 {
4747 struct filesystem_posix_info *info;
4748 unsigned short logical_sector_size;
4749
4750 if (!work->tcon->posix_extensions) {
4751 ksmbd_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4752 rc = -EOPNOTSUPP;
4753 } else {
4754 info = (struct filesystem_posix_info *)(rsp->Buffer);
4755 logical_sector_size =
4756 ksmbd_vfs_logical_sector_size(d_inode(path.dentry));
4757 info->OptimalTransferSize = cpu_to_le32(logical_sector_size);
4758 info->BlockSize = cpu_to_le32(stfs.f_bsize);
4759 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
4760 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
4761 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
4762 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
4763 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
4764 rsp->OutputBufferLength = cpu_to_le32(56);
4765 inc_rfc1001_len(rsp_org, 56);
4766 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
4767 }
4768 break;
4769 }
4770 default:
4771 path_put(&path);
4772 return -EOPNOTSUPP;
4773 }
4774 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4775 rsp,
4776 fs_infoclass_size);
4777 path_put(&path);
4778 return rc;
4779}
4780
4781static int smb2_get_info_sec(struct ksmbd_work *work,
64b39f4a
NJ
4782 struct smb2_query_info_req *req,
4783 struct smb2_query_info_rsp *rsp, void *rsp_org)
e2f34481
NJ
4784{
4785 struct ksmbd_file *fp;
4786 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
4787 struct smb_fattr fattr = {{0}};
4788 struct inode *inode;
4789 __u32 secdesclen;
4790 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4791 int addition_info = le32_to_cpu(req->AdditionalInformation);
4792 int rc;
4793
ced2b26a
SG
4794 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO)) {
4795 ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n",
4796 addition_info);
4797
4798 pntsd->revision = cpu_to_le16(1);
4799 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
4800 pntsd->osidoffset = 0;
4801 pntsd->gsidoffset = 0;
4802 pntsd->sacloffset = 0;
4803 pntsd->dacloffset = 0;
4804
4805 secdesclen = sizeof(struct smb_ntsd);
4806 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4807 inc_rfc1001_len(rsp_org, secdesclen);
4808
4809 return 0;
4810 }
4811
e2f34481
NJ
4812 if (work->next_smb2_rcv_hdr_off) {
4813 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
4814 ksmbd_debug(SMB, "Compound request set FID = %u\n",
4815 work->compound_fid);
4816 id = work->compound_fid;
4817 pid = work->compound_pfid;
4818 }
4819 }
4820
4821 if (!HAS_FILE_ID(id)) {
4822 id = le64_to_cpu(req->VolatileFileId);
4823 pid = le64_to_cpu(req->PersistentFileId);
4824 }
4825
4826 fp = ksmbd_lookup_fd_slow(work, id, pid);
4827 if (!fp)
4828 return -ENOENT;
4829
4830 inode = FP_INODE(fp);
4831 fattr.cf_uid = inode->i_uid;
4832 fattr.cf_gid = inode->i_gid;
4833 fattr.cf_mode = inode->i_mode;
4834 fattr.cf_dacls = NULL;
4835
4836 fattr.cf_acls = ksmbd_vfs_get_acl(inode, ACL_TYPE_ACCESS);
4837 if (S_ISDIR(inode->i_mode))
4838 fattr.cf_dacls = ksmbd_vfs_get_acl(inode, ACL_TYPE_DEFAULT);
4839
4840 if (test_share_config_flag(work->tcon->share_conf,
64b39f4a 4841 KSMBD_SHARE_FLAG_ACL_XATTR))
e2f34481
NJ
4842 ksmbd_vfs_get_sd_xattr(work->conn, fp->filp->f_path.dentry, &ppntsd);
4843
4844 rc = build_sec_desc(pntsd, ppntsd, addition_info, &secdesclen, &fattr);
4845 posix_acl_release(fattr.cf_acls);
4846 posix_acl_release(fattr.cf_dacls);
4847 kfree(ppntsd);
4848 ksmbd_fd_put(work, fp);
4849 if (rc)
4850 return rc;
4851
4852 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4853 inc_rfc1001_len(rsp_org, secdesclen);
4854 return 0;
4855}
4856
4857/**
4858 * smb2_query_info() - handler for smb2 query info command
4859 * @work: smb work containing query info request buffer
4860 *
4861 * Return: 0 on success, otherwise error
4862 */
4863int smb2_query_info(struct ksmbd_work *work)
4864{
4865 struct smb2_query_info_req *req;
4866 struct smb2_query_info_rsp *rsp, *rsp_org;
4867 int rc = 0;
4868
e5066499 4869 rsp_org = work->response_buf;
e2f34481
NJ
4870 WORK_BUFFERS(work, req, rsp);
4871
4872 ksmbd_debug(SMB, "GOT query info request\n");
4873
4874 switch (req->InfoType) {
4875 case SMB2_O_INFO_FILE:
4876 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
4877 rc = smb2_get_info_file(work, req, rsp, (void *)rsp_org);
4878 break;
4879 case SMB2_O_INFO_FILESYSTEM:
4880 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
4881 rc = smb2_get_info_filesystem(work, req, rsp, (void *)rsp_org);
4882 break;
4883 case SMB2_O_INFO_SECURITY:
4884 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
4885 rc = smb2_get_info_sec(work, req, rsp, (void *)rsp_org);
4886 break;
4887 default:
4888 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
4889 req->InfoType);
4890 rc = -EOPNOTSUPP;
4891 }
4892
4893 if (rc < 0) {
4894 if (rc == -EACCES)
4895 rsp->hdr.Status = STATUS_ACCESS_DENIED;
4896 else if (rc == -ENOENT)
4897 rsp->hdr.Status = STATUS_FILE_CLOSED;
4898 else if (rc == -EIO)
4899 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
4900 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
4901 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
4902 smb2_set_err_rsp(work);
4903
4904 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
4905 rc);
4906 return rc;
4907 }
4908 rsp->StructureSize = cpu_to_le16(9);
4909 rsp->OutputBufferOffset = cpu_to_le16(72);
4910 inc_rfc1001_len(rsp_org, 8);
4911 return 0;
4912}
4913
4914/**
4915 * smb2_close_pipe() - handler for closing IPC pipe
4916 * @work: smb work containing close request buffer
4917 *
4918 * Return: 0
4919 */
4920static noinline int smb2_close_pipe(struct ksmbd_work *work)
4921{
64b39f4a 4922 u64 id;
e5066499
NJ
4923 struct smb2_close_req *req = work->request_buf;
4924 struct smb2_close_rsp *rsp = work->response_buf;
e2f34481
NJ
4925
4926 id = le64_to_cpu(req->VolatileFileId);
4927 ksmbd_session_rpc_close(work->sess, id);
4928
4929 rsp->StructureSize = cpu_to_le16(60);
4930 rsp->Flags = 0;
4931 rsp->Reserved = 0;
4932 rsp->CreationTime = 0;
4933 rsp->LastAccessTime = 0;
4934 rsp->LastWriteTime = 0;
4935 rsp->ChangeTime = 0;
4936 rsp->AllocationSize = 0;
4937 rsp->EndOfFile = 0;
4938 rsp->Attributes = 0;
4939 inc_rfc1001_len(rsp, 60);
4940 return 0;
4941}
4942
4943/**
4944 * smb2_close() - handler for smb2 close file command
4945 * @work: smb work containing close request buffer
4946 *
4947 * Return: 0
4948 */
4949int smb2_close(struct ksmbd_work *work)
4950{
4951 unsigned int volatile_id = KSMBD_NO_FID;
64b39f4a 4952 u64 sess_id;
e2f34481
NJ
4953 struct smb2_close_req *req;
4954 struct smb2_close_rsp *rsp;
4955 struct smb2_close_rsp *rsp_org;
4956 struct ksmbd_conn *conn = work->conn;
4957 struct ksmbd_file *fp;
4958 struct inode *inode;
4959 u64 time;
4960 int err = 0;
4961
e5066499 4962 rsp_org = work->response_buf;
e2f34481
NJ
4963 WORK_BUFFERS(work, req, rsp);
4964
4965 if (test_share_config_flag(work->tcon->share_conf,
4966 KSMBD_SHARE_FLAG_PIPE)) {
4967 ksmbd_debug(SMB, "IPC pipe close request\n");
4968 return smb2_close_pipe(work);
4969 }
4970
4971 sess_id = le64_to_cpu(req->hdr.SessionId);
4972 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
4973 sess_id = work->compound_sid;
4974
4975 work->compound_sid = 0;
64b39f4a 4976 if (check_session_id(conn, sess_id)) {
e2f34481 4977 work->compound_sid = sess_id;
64b39f4a 4978 } else {
e2f34481
NJ
4979 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
4980 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
4981 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
4982 err = -EBADF;
4983 goto out;
4984 }
4985
4986 if (work->next_smb2_rcv_hdr_off &&
64b39f4a 4987 !HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
e2f34481
NJ
4988 if (!HAS_FILE_ID(work->compound_fid)) {
4989 /* file already closed, return FILE_CLOSED */
4990 ksmbd_debug(SMB, "file already closed\n");
4991 rsp->hdr.Status = STATUS_FILE_CLOSED;
4992 err = -EBADF;
4993 goto out;
4994 } else {
4995 ksmbd_debug(SMB, "Compound request set FID = %u:%u\n",
4996 work->compound_fid,
4997 work->compound_pfid);
4998 volatile_id = work->compound_fid;
4999
5000 /* file closed, stored id is not valid anymore */
5001 work->compound_fid = KSMBD_NO_FID;
5002 work->compound_pfid = KSMBD_NO_FID;
5003 }
5004 } else {
5005 volatile_id = le64_to_cpu(req->VolatileFileId);
5006 }
5007 ksmbd_debug(SMB, "volatile_id = %u\n", volatile_id);
5008
5009 rsp->StructureSize = cpu_to_le16(60);
5010 rsp->Reserved = 0;
5011
5012 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5013 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5014 if (!fp) {
5015 err = -ENOENT;
5016 goto out;
5017 }
5018
5019 inode = FP_INODE(fp);
5020 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5021 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5022 cpu_to_le64(inode->i_blocks << 9);
5023 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5024 rsp->Attributes = fp->f_ci->m_fattr;
5025 rsp->CreationTime = cpu_to_le64(fp->create_time);
5026 time = ksmbd_UnixTimeToNT(inode->i_atime);
5027 rsp->LastAccessTime = cpu_to_le64(time);
5028 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5029 rsp->LastWriteTime = cpu_to_le64(time);
5030 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5031 rsp->ChangeTime = cpu_to_le64(time);
5032 ksmbd_fd_put(work, fp);
5033 } else {
5034 rsp->Flags = 0;
5035 rsp->AllocationSize = 0;
5036 rsp->EndOfFile = 0;
5037 rsp->Attributes = 0;
5038 rsp->CreationTime = 0;
5039 rsp->LastAccessTime = 0;
5040 rsp->LastWriteTime = 0;
5041 rsp->ChangeTime = 0;
5042 }
5043
5044 err = ksmbd_close_fd(work, volatile_id);
5045out:
5046 if (err) {
5047 if (rsp->hdr.Status == 0)
5048 rsp->hdr.Status = STATUS_FILE_CLOSED;
5049 smb2_set_err_rsp(work);
5050 } else {
5051 inc_rfc1001_len(rsp_org, 60);
5052 }
5053
5054 return 0;
5055}
5056
5057/**
5058 * smb2_echo() - handler for smb2 echo(ping) command
5059 * @work: smb work containing echo request buffer
5060 *
5061 * Return: 0
5062 */
5063int smb2_echo(struct ksmbd_work *work)
5064{
e5066499 5065 struct smb2_echo_rsp *rsp = work->response_buf;
e2f34481
NJ
5066
5067 rsp->StructureSize = cpu_to_le16(4);
5068 rsp->Reserved = 0;
5069 inc_rfc1001_len(rsp, 4);
5070 return 0;
5071}
5072
e2f34481 5073static int smb2_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
64b39f4a
NJ
5074 struct smb2_file_rename_info *file_info,
5075 struct nls_table *local_nls)
e2f34481
NJ
5076{
5077 struct ksmbd_share_config *share = fp->tcon->share_conf;
5078 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5079 char *pathname = NULL;
5080 struct path path;
5081 bool file_present = true;
5082 int rc;
5083
5084 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5085 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5086 if (!pathname)
5087 return -ENOMEM;
5088
5089 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5090 if (IS_ERR(abs_oldname)) {
5091 rc = -EINVAL;
5092 goto out;
5093 }
5094 old_name = strrchr(abs_oldname, '/');
64b39f4a 5095 if (old_name && old_name[1] != '\0') {
e2f34481 5096 old_name++;
64b39f4a 5097 } else {
e2f34481
NJ
5098 ksmbd_debug(SMB, "can't get last component in path %s\n",
5099 abs_oldname);
5100 rc = -ENOENT;
5101 goto out;
5102 }
5103
5104 new_name = smb2_get_name(share,
5105 file_info->FileName,
5106 le32_to_cpu(file_info->FileNameLength),
5107 local_nls);
5108 if (IS_ERR(new_name)) {
5109 rc = PTR_ERR(new_name);
5110 goto out;
5111 }
5112
5113 if (strchr(new_name, ':')) {
5114 int s_type;
5115 char *xattr_stream_name, *stream_name = NULL;
5116 size_t xattr_stream_size;
5117 int len;
5118
5119 rc = parse_stream_name(new_name, &stream_name, &s_type);
5120 if (rc < 0)
5121 goto out;
5122
5123 len = strlen(new_name);
5124 if (new_name[len - 1] != '/') {
5125 ksmbd_err("not allow base filename in rename\n");
5126 rc = -ESHARE;
5127 goto out;
5128 }
5129
5130 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5131 &xattr_stream_name,
5132 &xattr_stream_size,
5133 s_type);
5134 if (rc)
5135 goto out;
5136
5137 rc = ksmbd_vfs_setxattr(fp->filp->f_path.dentry,
5138 xattr_stream_name,
5139 NULL, 0, 0);
5140 if (rc < 0) {
5141 ksmbd_err("failed to store stream name in xattr: %d\n",
5142 rc);
5143 rc = -EINVAL;
5144 goto out;
5145 }
5146
5147 goto out;
5148 }
5149
5150 ksmbd_debug(SMB, "new name %s\n", new_name);
5151 rc = ksmbd_vfs_kern_path(new_name, 0, &path, 1);
5152 if (rc)
5153 file_present = false;
5154 else
5155 path_put(&path);
5156
5157 if (ksmbd_share_veto_filename(share, new_name)) {
5158 rc = -ENOENT;
5159 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5160 goto out;
5161 }
5162
5163 if (file_info->ReplaceIfExists) {
5164 if (file_present) {
5165 rc = ksmbd_vfs_remove_file(work, new_name);
5166 if (rc) {
5167 if (rc != -ENOTEMPTY)
5168 rc = -EINVAL;
5169 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
5170 new_name, rc);
5171 goto out;
5172 }
5173 }
5174 } else {
5175 if (file_present &&
64b39f4a 5176 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
e2f34481
NJ
5177 rc = -EEXIST;
5178 ksmbd_debug(SMB,
5179 "cannot rename already existing file\n");
5180 goto out;
5181 }
5182 }
5183
5184 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5185out:
5186 kfree(pathname);
5187 if (!IS_ERR(new_name))
915f570a 5188 kfree(new_name);
e2f34481
NJ
5189 return rc;
5190}
5191
e2f34481 5192static int smb2_create_link(struct ksmbd_work *work,
64b39f4a
NJ
5193 struct ksmbd_share_config *share,
5194 struct smb2_file_link_info *file_info, struct file *filp,
5195 struct nls_table *local_nls)
e2f34481
NJ
5196{
5197 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5198 struct path path;
5199 bool file_present = true;
5200 int rc;
5201
5202 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5203 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5204 if (!pathname)
5205 return -ENOMEM;
5206
5207 link_name = smb2_get_name(share,
5208 file_info->FileName,
5209 le32_to_cpu(file_info->FileNameLength),
5210 local_nls);
5211 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5212 rc = -EINVAL;
5213 goto out;
5214 }
5215
5216 ksmbd_debug(SMB, "link name is %s\n", link_name);
5217 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5218 if (IS_ERR(target_name)) {
5219 rc = -EINVAL;
5220 goto out;
5221 }
5222
5223 ksmbd_debug(SMB, "target name is %s\n", target_name);
5224 rc = ksmbd_vfs_kern_path(link_name, 0, &path, 0);
5225 if (rc)
5226 file_present = false;
5227 else
5228 path_put(&path);
5229
5230 if (file_info->ReplaceIfExists) {
5231 if (file_present) {
5232 rc = ksmbd_vfs_remove_file(work, link_name);
5233 if (rc) {
5234 rc = -EINVAL;
5235 ksmbd_debug(SMB, "cannot delete %s\n",
5236 link_name);
5237 goto out;
5238 }
5239 }
5240 } else {
5241 if (file_present) {
5242 rc = -EEXIST;
5243 ksmbd_debug(SMB, "link already exists\n");
5244 goto out;
5245 }
5246 }
5247
5248 rc = ksmbd_vfs_link(work, target_name, link_name);
5249 if (rc)
5250 rc = -EINVAL;
5251out:
5252 if (!IS_ERR(link_name))
915f570a 5253 kfree(link_name);
e2f34481
NJ
5254 kfree(pathname);
5255 return rc;
5256}
5257
64b39f4a
NJ
5258static int set_file_basic_info(struct ksmbd_file *fp, char *buf,
5259 struct ksmbd_share_config *share)
e2f34481
NJ
5260{
5261 struct smb2_file_all_info *file_info;
5262 struct iattr attrs;
5263 struct iattr temp_attrs;
5264 struct file *filp;
5265 struct inode *inode;
5266 int rc;
5267
7adfd4f6 5268 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
e2f34481
NJ
5269 return -EACCES;
5270
5271 file_info = (struct smb2_file_all_info *)buf;
5272 attrs.ia_valid = 0;
5273 filp = fp->filp;
5274 inode = file_inode(filp);
5275
5276 if (file_info->CreationTime)
5277 fp->create_time = le64_to_cpu(file_info->CreationTime);
5278
5279 if (file_info->LastAccessTime) {
5280 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5281 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5282 }
5283
5284 if (file_info->ChangeTime) {
5285 temp_attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
5286 attrs.ia_ctime = temp_attrs.ia_ctime;
5287 attrs.ia_valid |= ATTR_CTIME;
64b39f4a 5288 } else {
e2f34481 5289 temp_attrs.ia_ctime = inode->i_ctime;
64b39f4a 5290 }
e2f34481
NJ
5291
5292 if (file_info->LastWriteTime) {
5293 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5294 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5295 }
5296
5297 if (file_info->Attributes) {
5298 if (!S_ISDIR(inode->i_mode) &&
64b39f4a 5299 file_info->Attributes & ATTR_DIRECTORY_LE) {
e2f34481
NJ
5300 ksmbd_err("can't change a file to a directory\n");
5301 return -EINVAL;
5302 }
5303
5304 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE))
5305 fp->f_ci->m_fattr = file_info->Attributes |
5306 (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE);
5307 }
5308
5309 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5310 (file_info->CreationTime || file_info->Attributes)) {
5311 struct xattr_dos_attrib da = {0};
5312
5313 da.version = 4;
5314 da.itime = fp->itime;
5315 da.create_time = fp->create_time;
5316 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5317 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5318 XATTR_DOSINFO_ITIME;
5319
5320 rc = ksmbd_vfs_set_dos_attrib_xattr(filp->f_path.dentry, &da);
5321 if (rc)
5322 ksmbd_debug(SMB,
5323 "failed to restore file attribute in EA\n");
5324 rc = 0;
5325 }
5326
5327 /*
5328 * HACK : set ctime here to avoid ctime changed
5329 * when file_info->ChangeTime is zero.
5330 */
5331 attrs.ia_ctime = temp_attrs.ia_ctime;
5332 attrs.ia_valid |= ATTR_CTIME;
5333
5334 if (attrs.ia_valid) {
5335 struct dentry *dentry = filp->f_path.dentry;
5336 struct inode *inode = d_inode(dentry);
5337
5338 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5339 return -EACCES;
5340
5341 rc = setattr_prepare(&init_user_ns, dentry, &attrs);
5342 if (rc)
5343 return -EINVAL;
5344
5345 inode_lock(inode);
5346 setattr_copy(&init_user_ns, inode, &attrs);
5347 attrs.ia_valid &= ~ATTR_CTIME;
5348 rc = notify_change(&init_user_ns, dentry, &attrs, NULL);
5349 inode_unlock(inode);
5350 }
5351 return 0;
5352}
5353
5354static int set_file_allocation_info(struct ksmbd_work *work,
64b39f4a 5355 struct ksmbd_file *fp, char *buf)
e2f34481
NJ
5356{
5357 /*
5358 * TODO : It's working fine only when store dos attributes
5359 * is not yes. need to implement a logic which works
5360 * properly with any smb.conf option
5361 */
5362
5363 struct smb2_file_alloc_info *file_alloc_info;
5364 loff_t alloc_blks;
5365 struct inode *inode;
5366 int rc;
5367
a299669b 5368 if (!(fp->daccess & FILE_WRITE_DATA_LE))
e2f34481
NJ
5369 return -EACCES;
5370
5371 file_alloc_info = (struct smb2_file_alloc_info *)buf;
5372 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5373 inode = file_inode(fp->filp);
5374
5375 if (alloc_blks > inode->i_blocks) {
5376 rc = ksmbd_vfs_alloc_size(work, fp, alloc_blks * 512);
5377 if (rc && rc != -EOPNOTSUPP) {
5378 ksmbd_err("ksmbd_vfs_alloc_size is failed : %d\n", rc);
5379 return rc;
5380 }
5381 } else if (alloc_blks < inode->i_blocks) {
5382 loff_t size;
5383
5384 /*
5385 * Allocation size could be smaller than original one
5386 * which means allocated blocks in file should be
5387 * deallocated. use truncate to cut out it, but inode
5388 * size is also updated with truncate offset.
5389 * inode size is retained by backup inode size.
5390 */
5391 size = i_size_read(inode);
5392 rc = ksmbd_vfs_truncate(work, NULL, fp, alloc_blks * 512);
5393 if (rc) {
5394 ksmbd_err("truncate failed! filename : %s, err %d\n",
5395 fp->filename, rc);
5396 return rc;
5397 }
5398 if (size < alloc_blks * 512)
5399 i_size_write(inode, size);
5400 }
5401 return 0;
5402}
5403
64b39f4a
NJ
5404static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
5405 char *buf)
e2f34481
NJ
5406{
5407 struct smb2_file_eof_info *file_eof_info;
5408 loff_t newsize;
5409 struct inode *inode;
5410 int rc;
5411
a299669b 5412 if (!(fp->daccess & FILE_WRITE_DATA_LE))
e2f34481
NJ
5413 return -EACCES;
5414
5415 file_eof_info = (struct smb2_file_eof_info *)buf;
5416 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5417 inode = file_inode(fp->filp);
5418
5419 /*
5420 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5421 * on FAT32 shared device, truncate execution time is too long
5422 * and network error could cause from windows client. because
5423 * truncate of some filesystem like FAT32 fill zero data in
5424 * truncated range.
5425 */
5426 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5427 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
5428 fp->filename, newsize);
5429 rc = ksmbd_vfs_truncate(work, NULL, fp, newsize);
5430 if (rc) {
64b39f4a 5431 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
e2f34481
NJ
5432 fp->filename, rc);
5433 if (rc != -EAGAIN)
5434 rc = -EBADF;
5435 return rc;
5436 }
5437 }
5438 return 0;
5439}
5440
64b39f4a
NJ
5441static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
5442 char *buf)
e2f34481
NJ
5443{
5444 struct ksmbd_file *parent_fp;
5445
5446 if (!(fp->daccess & FILE_DELETE_LE)) {
5447 ksmbd_err("no right to delete : 0x%x\n", fp->daccess);
5448 return -EACCES;
5449 }
5450
5451 if (ksmbd_stream_fd(fp))
5452 goto next;
5453
5454 parent_fp = ksmbd_lookup_fd_inode(PARENT_INODE(fp));
5455 if (parent_fp) {
5456 if (parent_fp->daccess & FILE_DELETE_LE) {
5457 ksmbd_err("parent dir is opened with delete access\n");
5458 return -ESHARE;
5459 }
5460 }
5461next:
5462 return smb2_rename(work, fp,
5463 (struct smb2_file_rename_info *)buf,
5464 work->sess->conn->local_nls);
5465}
5466
64b39f4a 5467static int set_file_disposition_info(struct ksmbd_file *fp, char *buf)
e2f34481
NJ
5468{
5469 struct smb2_file_disposition_info *file_info;
5470 struct inode *inode;
5471
5472 if (!(fp->daccess & FILE_DELETE_LE)) {
5473 ksmbd_err("no right to delete : 0x%x\n", fp->daccess);
5474 return -EACCES;
5475 }
5476
5477 inode = file_inode(fp->filp);
5478 file_info = (struct smb2_file_disposition_info *)buf;
5479 if (file_info->DeletePending) {
5480 if (S_ISDIR(inode->i_mode) &&
64b39f4a 5481 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
e2f34481
NJ
5482 return -EBUSY;
5483 ksmbd_set_inode_pending_delete(fp);
5484 } else {
5485 ksmbd_clear_inode_pending_delete(fp);
5486 }
5487 return 0;
5488}
5489
64b39f4a 5490static int set_file_position_info(struct ksmbd_file *fp, char *buf)
e2f34481
NJ
5491{
5492 struct smb2_file_pos_info *file_info;
5493 loff_t current_byte_offset;
5494 unsigned short sector_size;
5495 struct inode *inode;
5496
5497 inode = file_inode(fp->filp);
5498 file_info = (struct smb2_file_pos_info *)buf;
5499 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
5500 sector_size = ksmbd_vfs_logical_sector_size(inode);
5501
5502 if (current_byte_offset < 0 ||
64b39f4a
NJ
5503 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5504 current_byte_offset & (sector_size - 1))) {
e2f34481
NJ
5505 ksmbd_err("CurrentByteOffset is not valid : %llu\n",
5506 current_byte_offset);
5507 return -EINVAL;
5508 }
5509
5510 fp->filp->f_pos = current_byte_offset;
5511 return 0;
5512}
5513
64b39f4a 5514static int set_file_mode_info(struct ksmbd_file *fp, char *buf)
e2f34481
NJ
5515{
5516 struct smb2_file_mode_info *file_info;
5517 __le32 mode;
5518
5519 file_info = (struct smb2_file_mode_info *)buf;
5520 mode = file_info->Mode;
5521
64b39f4a
NJ
5522 if ((mode & ~FILE_MODE_INFO_MASK) ||
5523 (mode & FILE_SYNCHRONOUS_IO_ALERT_LE &&
5524 mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) {
e2f34481
NJ
5525 ksmbd_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
5526 return -EINVAL;
5527 }
5528
5529 /*
5530 * TODO : need to implement consideration for
5531 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5532 */
5533 ksmbd_vfs_set_fadvise(fp->filp, mode);
5534 fp->coption = mode;
5535 return 0;
5536}
5537
5538/**
5539 * smb2_set_info_file() - handler for smb2 set info command
5540 * @work: smb work containing set info command buffer
95fa1ce9
HL
5541 * @fp: ksmbd_file pointer
5542 * @info_class: smb2 set info class
5543 * @share: ksmbd_share_config pointer
e2f34481
NJ
5544 *
5545 * Return: 0 on success, otherwise error
5546 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5547 */
64b39f4a
NJ
5548static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
5549 int info_class, char *buf, struct ksmbd_share_config *share)
e2f34481
NJ
5550{
5551 switch (info_class) {
5552 case FILE_BASIC_INFORMATION:
5553 return set_file_basic_info(fp, buf, share);
5554
5555 case FILE_ALLOCATION_INFORMATION:
5556 return set_file_allocation_info(work, fp, buf);
5557
5558 case FILE_END_OF_FILE_INFORMATION:
5559 return set_end_of_file_info(work, fp, buf);
5560
5561 case FILE_RENAME_INFORMATION:
64b39f4a 5562 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
e2f34481
NJ
5563 ksmbd_debug(SMB,
5564 "User does not have write permission\n");
5565 return -EACCES;
5566 }
5567 return set_rename_info(work, fp, buf);
5568
5569 case FILE_LINK_INFORMATION:
5570 return smb2_create_link(work, work->tcon->share_conf,
5571 (struct smb2_file_link_info *)buf, fp->filp,
5572 work->sess->conn->local_nls);
5573
5574 case FILE_DISPOSITION_INFORMATION:
64b39f4a 5575 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
e2f34481
NJ
5576 ksmbd_debug(SMB,
5577 "User does not have write permission\n");
5578 return -EACCES;
5579 }
5580 return set_file_disposition_info(fp, buf);
5581
5582 case FILE_FULL_EA_INFORMATION:
5583 {
5584 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
5585 ksmbd_err("Not permitted to write ext attr: 0x%x\n",
5586 fp->daccess);
5587 return -EACCES;
5588 }
5589
5590 return smb2_set_ea((struct smb2_ea_info *)buf,
5591 &fp->filp->f_path);
5592 }
5593
5594 case FILE_POSITION_INFORMATION:
5595 return set_file_position_info(fp, buf);
5596
5597 case FILE_MODE_INFORMATION:
5598 return set_file_mode_info(fp, buf);
5599 }
5600
5601 ksmbd_err("Unimplemented Fileinfoclass :%d\n", info_class);
5602 return -EOPNOTSUPP;
5603}
5604
64b39f4a
NJ
5605static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
5606 char *buffer, int buf_len)
e2f34481
NJ
5607{
5608 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5609
5610 fp->saccess |= FILE_SHARE_DELETE_LE;
5611
5612 return set_info_sec(fp->conn, fp->tcon, fp->filp->f_path.dentry, pntsd,
5613 buf_len, false);
5614}
5615
5616/**
5617 * smb2_set_info() - handler for smb2 set info command handler
5618 * @work: smb work containing set info request buffer
5619 *
5620 * Return: 0 on success, otherwise error
5621 */
5622int smb2_set_info(struct ksmbd_work *work)
5623{
5624 struct smb2_set_info_req *req;
5625 struct smb2_set_info_rsp *rsp, *rsp_org;
5626 struct ksmbd_file *fp;
5627 int rc = 0;
5628 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5629
5630 ksmbd_debug(SMB, "Received set info request\n");
5631
e5066499 5632 rsp_org = work->response_buf;
e2f34481
NJ
5633 if (work->next_smb2_rcv_hdr_off) {
5634 req = REQUEST_BUF_NEXT(work);
5635 rsp = RESPONSE_BUF_NEXT(work);
5636 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
5637 ksmbd_debug(SMB, "Compound request set FID = %u\n",
5638 work->compound_fid);
5639 id = work->compound_fid;
5640 pid = work->compound_pfid;
5641 }
5642 } else {
e5066499
NJ
5643 req = work->request_buf;
5644 rsp = work->response_buf;
e2f34481
NJ
5645 }
5646
5647 if (!HAS_FILE_ID(id)) {
5648 id = le64_to_cpu(req->VolatileFileId);
5649 pid = le64_to_cpu(req->PersistentFileId);
5650 }
5651
5652 fp = ksmbd_lookup_fd_slow(work, id, pid);
5653 if (!fp) {
5654 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5655 rc = -ENOENT;
5656 goto err_out;
5657 }
5658
5659 switch (req->InfoType) {
5660 case SMB2_O_INFO_FILE:
5661 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5662 rc = smb2_set_info_file(work, fp, req->FileInfoClass,
5663 req->Buffer, work->tcon->share_conf);
5664 break;
5665 case SMB2_O_INFO_SECURITY:
5666 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5667 rc = smb2_set_info_sec(fp,
5668 le32_to_cpu(req->AdditionalInformation), req->Buffer,
5669 le32_to_cpu(req->BufferLength));
5670 break;
5671 default:
5672 rc = -EOPNOTSUPP;
5673 }
5674
5675 if (rc < 0)
5676 goto err_out;
5677
5678 rsp->StructureSize = cpu_to_le16(2);
5679 inc_rfc1001_len(rsp_org, 2);
5680 ksmbd_fd_put(work, fp);
5681 return 0;
5682
5683err_out:
5684 if (rc == -EACCES || rc == -EPERM)
5685 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5686 else if (rc == -EINVAL)
5687 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5688 else if (rc == -ESHARE)
5689 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
5690 else if (rc == -ENOENT)
5691 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
5692 else if (rc == -EBUSY || rc == -ENOTEMPTY)
5693 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
5694 else if (rc == -EAGAIN)
5695 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
ff1d5727 5696 else if (rc == -EBADF || rc == -ESTALE)
e2f34481
NJ
5697 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5698 else if (rc == -EEXIST)
5699 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
5700 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
5701 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5702 smb2_set_err_rsp(work);
5703 ksmbd_fd_put(work, fp);
5704 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
5705 rc);
5706 return rc;
5707}
5708
5709/**
5710 * smb2_read_pipe() - handler for smb2 read from IPC pipe
5711 * @work: smb work containing read IPC pipe command buffer
5712 *
5713 * Return: 0 on success, otherwise error
5714 */
5715static noinline int smb2_read_pipe(struct ksmbd_work *work)
5716{
5717 int nbytes = 0, err;
64b39f4a 5718 u64 id;
e2f34481 5719 struct ksmbd_rpc_command *rpc_resp;
e5066499
NJ
5720 struct smb2_read_req *req = work->request_buf;
5721 struct smb2_read_rsp *rsp = work->response_buf;
e2f34481
NJ
5722
5723 id = le64_to_cpu(req->VolatileFileId);
5724
5725 inc_rfc1001_len(rsp, 16);
5726 rpc_resp = ksmbd_rpc_read(work->sess, id);
5727 if (rpc_resp) {
5728 if (rpc_resp->flags != KSMBD_RPC_OK) {
5729 err = -EINVAL;
5730 goto out;
5731 }
5732
5733 work->aux_payload_buf =
79f6b11a 5734 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
e2f34481
NJ
5735 if (!work->aux_payload_buf) {
5736 err = -ENOMEM;
5737 goto out;
5738 }
5739
5740 memcpy(work->aux_payload_buf, rpc_resp->payload,
5741 rpc_resp->payload_sz);
5742
5743 nbytes = rpc_resp->payload_sz;
5744 work->resp_hdr_sz = get_rfc1002_len(rsp) + 4;
5745 work->aux_payload_sz = nbytes;
79f6b11a 5746 kvfree(rpc_resp);
e2f34481
NJ
5747 }
5748
5749 rsp->StructureSize = cpu_to_le16(17);
5750 rsp->DataOffset = 80;
5751 rsp->Reserved = 0;
5752 rsp->DataLength = cpu_to_le32(nbytes);
5753 rsp->DataRemaining = 0;
5754 rsp->Reserved2 = 0;
5755 inc_rfc1001_len(rsp, nbytes);
5756 return 0;
5757
5758out:
5759 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5760 smb2_set_err_rsp(work);
79f6b11a 5761 kvfree(rpc_resp);
e2f34481
NJ
5762 return err;
5763}
5764
5765static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
64b39f4a 5766 struct smb2_read_req *req, void *data_buf, size_t length)
e2f34481
NJ
5767{
5768 struct smb2_buffer_desc_v1 *desc =
5769 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
5770 int err;
5771
64b39f4a
NJ
5772 if (work->conn->dialect == SMB30_PROT_ID &&
5773 req->Channel != SMB2_CHANNEL_RDMA_V1)
e2f34481
NJ
5774 return -EINVAL;
5775
64b39f4a
NJ
5776 if (req->ReadChannelInfoOffset == 0 ||
5777 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
e2f34481
NJ
5778 return -EINVAL;
5779
5780 work->need_invalidate_rkey =
5781 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
5782 work->remote_key = le32_to_cpu(desc->token);
5783
64b39f4a
NJ
5784 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
5785 le32_to_cpu(desc->token), le64_to_cpu(desc->offset),
5786 le32_to_cpu(desc->length));
e2f34481
NJ
5787 if (err)
5788 return err;
5789
5790 return length;
5791}
5792
5793/**
5794 * smb2_read() - handler for smb2 read from file
5795 * @work: smb work containing read command buffer
5796 *
5797 * Return: 0 on success, otherwise error
5798 */
5799int smb2_read(struct ksmbd_work *work)
5800{
5801 struct ksmbd_conn *conn = work->conn;
5802 struct smb2_read_req *req;
5803 struct smb2_read_rsp *rsp, *rsp_org;
5804 struct ksmbd_file *fp;
5805 loff_t offset;
5806 size_t length, mincount;
5807 ssize_t nbytes = 0, remain_bytes = 0;
5808 int err = 0;
5809
e5066499 5810 rsp_org = work->response_buf;
e2f34481
NJ
5811 WORK_BUFFERS(work, req, rsp);
5812
5813 if (test_share_config_flag(work->tcon->share_conf,
5814 KSMBD_SHARE_FLAG_PIPE)) {
5815 ksmbd_debug(SMB, "IPC pipe read request\n");
5816 return smb2_read_pipe(work);
5817 }
5818
5819 fp = ksmbd_lookup_fd_slow(work,
5820 le64_to_cpu(req->VolatileFileId),
5821 le64_to_cpu(req->PersistentFileId));
5822 if (!fp) {
5823 rsp->hdr.Status = STATUS_FILE_CLOSED;
5824 return -ENOENT;
5825 }
5826
5827 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
5828 ksmbd_err("Not permitted to read : 0x%x\n", fp->daccess);
5829 err = -EACCES;
5830 goto out;
5831 }
5832
5833 offset = le64_to_cpu(req->Offset);
5834 length = le32_to_cpu(req->Length);
5835 mincount = le32_to_cpu(req->MinimumCount);
5836
5837 if (length > conn->vals->max_read_size) {
5838 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
5839 conn->vals->max_read_size);
5840 err = -EINVAL;
5841 goto out;
5842 }
5843
5844 ksmbd_debug(SMB, "filename %s, offset %lld, len %zu\n", FP_FILENAME(fp),
5845 offset, length);
5846
5847 if (server_conf.flags & KSMBD_GLOBAL_FLAG_CACHE_RBUF) {
5848 work->aux_payload_buf =
5849 ksmbd_find_buffer(conn->vals->max_read_size);
5850 work->set_read_buf = true;
5851 } else {
79f6b11a 5852 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
e2f34481
NJ
5853 }
5854 if (!work->aux_payload_buf) {
c1ea111f 5855 err = -ENOMEM;
e2f34481
NJ
5856 goto out;
5857 }
5858
5859 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
5860 if (nbytes < 0) {
5861 err = nbytes;
5862 goto out;
5863 }
5864
5865 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
5866 if (server_conf.flags & KSMBD_GLOBAL_FLAG_CACHE_RBUF)
e5066499 5867 ksmbd_release_buffer(work->aux_payload_buf);
e2f34481 5868 else
79f6b11a 5869 kvfree(work->aux_payload_buf);
e5066499 5870 work->aux_payload_buf = NULL;
e2f34481
NJ
5871 rsp->hdr.Status = STATUS_END_OF_FILE;
5872 smb2_set_err_rsp(work);
5873 ksmbd_fd_put(work, fp);
5874 return 0;
5875 }
5876
5877 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
5878 nbytes, offset, mincount);
5879
5880 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
64b39f4a 5881 req->Channel == SMB2_CHANNEL_RDMA_V1) {
e2f34481
NJ
5882 /* write data to the client using rdma channel */
5883 remain_bytes = smb2_read_rdma_channel(work, req,
e5066499 5884 work->aux_payload_buf, nbytes);
e2f34481 5885 if (server_conf.flags & KSMBD_GLOBAL_FLAG_CACHE_RBUF)
e5066499 5886 ksmbd_release_buffer(work->aux_payload_buf);
e2f34481 5887 else
79f6b11a 5888 kvfree(work->aux_payload_buf);
e5066499 5889 work->aux_payload_buf = NULL;
e2f34481
NJ
5890
5891 nbytes = 0;
5892 if (remain_bytes < 0) {
5893 err = (int)remain_bytes;
5894 goto out;
5895 }
5896 }
5897
5898 rsp->StructureSize = cpu_to_le16(17);
5899 rsp->DataOffset = 80;
5900 rsp->Reserved = 0;
5901 rsp->DataLength = cpu_to_le32(nbytes);
5902 rsp->DataRemaining = cpu_to_le32(remain_bytes);
5903 rsp->Reserved2 = 0;
5904 inc_rfc1001_len(rsp_org, 16);
5905 work->resp_hdr_sz = get_rfc1002_len(rsp_org) + 4;
5906 work->aux_payload_sz = nbytes;
5907 inc_rfc1001_len(rsp_org, nbytes);
5908 ksmbd_fd_put(work, fp);
5909 return 0;
5910
5911out:
5912 if (err) {
5913 if (err == -EISDIR)
5914 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
5915 else if (err == -EAGAIN)
5916 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
5917 else if (err == -ENOENT)
5918 rsp->hdr.Status = STATUS_FILE_CLOSED;
5919 else if (err == -EACCES)
5920 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5921 else if (err == -ESHARE)
5922 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
5923 else if (err == -EINVAL)
5924 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5925 else
5926 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5927
5928 smb2_set_err_rsp(work);
5929 }
5930 ksmbd_fd_put(work, fp);
5931 return err;
5932}
5933
5934/**
5935 * smb2_write_pipe() - handler for smb2 write on IPC pipe
5936 * @work: smb work containing write IPC pipe command buffer
5937 *
5938 * Return: 0 on success, otherwise error
5939 */
5940static noinline int smb2_write_pipe(struct ksmbd_work *work)
5941{
e5066499
NJ
5942 struct smb2_write_req *req = work->request_buf;
5943 struct smb2_write_rsp *rsp = work->response_buf;
e2f34481 5944 struct ksmbd_rpc_command *rpc_resp;
64b39f4a 5945 u64 id = 0;
e2f34481
NJ
5946 int err = 0, ret = 0;
5947 char *data_buf;
5948 size_t length;
5949
5950 length = le32_to_cpu(req->Length);
5951 id = le64_to_cpu(req->VolatileFileId);
5952
5953 if (le16_to_cpu(req->DataOffset) ==
64b39f4a 5954 (offsetof(struct smb2_write_req, Buffer) - 4)) {
e2f34481
NJ
5955 data_buf = (char *)&req->Buffer[0];
5956 } else {
5957 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
64b39f4a 5958 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
e2f34481 5959 ksmbd_err("invalid write data offset %u, smb_len %u\n",
64b39f4a 5960 le16_to_cpu(req->DataOffset), get_rfc1002_len(req));
e2f34481
NJ
5961 err = -EINVAL;
5962 goto out;
5963 }
5964
5965 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
5966 le16_to_cpu(req->DataOffset));
5967 }
5968
5969 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
5970 if (rpc_resp) {
5971 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
5972 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
79f6b11a 5973 kvfree(rpc_resp);
e2f34481
NJ
5974 smb2_set_err_rsp(work);
5975 return -EOPNOTSUPP;
5976 }
5977 if (rpc_resp->flags != KSMBD_RPC_OK) {
5978 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5979 smb2_set_err_rsp(work);
79f6b11a 5980 kvfree(rpc_resp);
e2f34481
NJ
5981 return ret;
5982 }
79f6b11a 5983 kvfree(rpc_resp);
e2f34481
NJ
5984 }
5985
5986 rsp->StructureSize = cpu_to_le16(17);
5987 rsp->DataOffset = 0;
5988 rsp->Reserved = 0;
5989 rsp->DataLength = cpu_to_le32(length);
5990 rsp->DataRemaining = 0;
5991 rsp->Reserved2 = 0;
5992 inc_rfc1001_len(rsp, 16);
5993 return 0;
5994out:
5995 if (err) {
5996 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5997 smb2_set_err_rsp(work);
5998 }
5999
6000 return err;
6001}
6002
6003static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
64b39f4a
NJ
6004 struct smb2_write_req *req, struct ksmbd_file *fp,
6005 loff_t offset, size_t length, bool sync)
e2f34481
NJ
6006{
6007 struct smb2_buffer_desc_v1 *desc;
6008 char *data_buf;
6009 int ret;
6010 ssize_t nbytes;
6011
6012 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6013
6014 if (work->conn->dialect == SMB30_PROT_ID &&
64b39f4a 6015 req->Channel != SMB2_CHANNEL_RDMA_V1)
e2f34481
NJ
6016 return -EINVAL;
6017
6018 if (req->Length != 0 || req->DataOffset != 0)
6019 return -EINVAL;
6020
64b39f4a
NJ
6021 if (req->WriteChannelInfoOffset == 0 ||
6022 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
e2f34481
NJ
6023 return -EINVAL;
6024
6025 work->need_invalidate_rkey =
6026 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6027 work->remote_key = le32_to_cpu(desc->token);
6028
79f6b11a 6029 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
e2f34481
NJ
6030 if (!data_buf)
6031 return -ENOMEM;
6032
6033 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
6034 le32_to_cpu(desc->token),
6035 le64_to_cpu(desc->offset),
6036 le32_to_cpu(desc->length));
e2f34481 6037 if (ret < 0) {
79f6b11a 6038 kvfree(data_buf);
e2f34481
NJ
6039 return ret;
6040 }
6041
64b39f4a 6042 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
79f6b11a 6043 kvfree(data_buf);
e2f34481
NJ
6044 if (ret < 0)
6045 return ret;
6046
6047 return nbytes;
6048}
6049
6050/**
6051 * smb2_write() - handler for smb2 write from file
6052 * @work: smb work containing write command buffer
6053 *
6054 * Return: 0 on success, otherwise error
6055 */
6056int smb2_write(struct ksmbd_work *work)
6057{
6058 struct smb2_write_req *req;
6059 struct smb2_write_rsp *rsp, *rsp_org;
6060 struct ksmbd_file *fp = NULL;
6061 loff_t offset;
6062 size_t length;
6063 ssize_t nbytes;
6064 char *data_buf;
6065 bool writethrough = false;
6066 int err = 0;
6067
e5066499 6068 rsp_org = work->response_buf;
e2f34481
NJ
6069 WORK_BUFFERS(work, req, rsp);
6070
64b39f4a 6071 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
e2f34481
NJ
6072 ksmbd_debug(SMB, "IPC pipe write request\n");
6073 return smb2_write_pipe(work);
6074 }
6075
6076 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6077 ksmbd_debug(SMB, "User does not have write permission\n");
6078 err = -EACCES;
6079 goto out;
6080 }
6081
64b39f4a
NJ
6082 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
6083 le64_to_cpu(req->PersistentFileId));
e2f34481
NJ
6084 if (!fp) {
6085 rsp->hdr.Status = STATUS_FILE_CLOSED;
6086 return -ENOENT;
6087 }
6088
6089 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
6090 ksmbd_err("Not permitted to write : 0x%x\n", fp->daccess);
6091 err = -EACCES;
6092 goto out;
6093 }
6094
6095 offset = le64_to_cpu(req->Offset);
6096 length = le32_to_cpu(req->Length);
6097
6098 if (length > work->conn->vals->max_write_size) {
6099 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6100 work->conn->vals->max_write_size);
6101 err = -EINVAL;
6102 goto out;
6103 }
6104
6105 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6106 writethrough = true;
6107
6108 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
64b39f4a 6109 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
e2f34481
NJ
6110 if (le16_to_cpu(req->DataOffset) ==
6111 (offsetof(struct smb2_write_req, Buffer) - 4)) {
6112 data_buf = (char *)&req->Buffer[0];
6113 } else {
64b39f4a
NJ
6114 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
6115 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
e2f34481
NJ
6116 ksmbd_err("invalid write data offset %u, smb_len %u\n",
6117 le16_to_cpu(req->DataOffset),
6118 get_rfc1002_len(req));
6119 err = -EINVAL;
6120 goto out;
6121 }
6122
6123 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6124 le16_to_cpu(req->DataOffset));
6125 }
6126
6127 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6128 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6129 writethrough = true;
6130
6131 ksmbd_debug(SMB, "filename %s, offset %lld, len %zu\n",
6132 FP_FILENAME(fp), offset, length);
6133 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6134 writethrough, &nbytes);
6135 if (err < 0)
6136 goto out;
6137 } else {
6138 /* read data from the client using rdma channel, and
6139 * write the data.
6140 */
6141 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
6142 le32_to_cpu(req->RemainingBytes),
6143 writethrough);
6144 if (nbytes < 0) {
6145 err = (int)nbytes;
6146 goto out;
6147 }
6148 }
6149
6150 rsp->StructureSize = cpu_to_le16(17);
6151 rsp->DataOffset = 0;
6152 rsp->Reserved = 0;
6153 rsp->DataLength = cpu_to_le32(nbytes);
6154 rsp->DataRemaining = 0;
6155 rsp->Reserved2 = 0;
6156 inc_rfc1001_len(rsp_org, 16);
6157 ksmbd_fd_put(work, fp);
6158 return 0;
6159
6160out:
6161 if (err == -EAGAIN)
6162 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6163 else if (err == -ENOSPC || err == -EFBIG)
6164 rsp->hdr.Status = STATUS_DISK_FULL;
6165 else if (err == -ENOENT)
6166 rsp->hdr.Status = STATUS_FILE_CLOSED;
6167 else if (err == -EACCES)
6168 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6169 else if (err == -ESHARE)
6170 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6171 else if (err == -EINVAL)
6172 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6173 else
6174 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6175
6176 smb2_set_err_rsp(work);
6177 ksmbd_fd_put(work, fp);
6178 return err;
6179}
6180
6181/**
6182 * smb2_flush() - handler for smb2 flush file - fsync
6183 * @work: smb work containing flush command buffer
6184 *
6185 * Return: 0 on success, otherwise error
6186 */
6187int smb2_flush(struct ksmbd_work *work)
6188{
6189 struct smb2_flush_req *req;
6190 struct smb2_flush_rsp *rsp, *rsp_org;
6191 int err;
6192
e5066499 6193 rsp_org = work->response_buf;
e2f34481
NJ
6194 WORK_BUFFERS(work, req, rsp);
6195
6196 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
6197 le64_to_cpu(req->VolatileFileId));
6198
6199 err = ksmbd_vfs_fsync(work,
6200 le64_to_cpu(req->VolatileFileId),
6201 le64_to_cpu(req->PersistentFileId));
6202 if (err)
6203 goto out;
6204
6205 rsp->StructureSize = cpu_to_le16(4);
6206 rsp->Reserved = 0;
6207 inc_rfc1001_len(rsp_org, 4);
6208 return 0;
6209
6210out:
6211 if (err) {
6212 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6213 smb2_set_err_rsp(work);
6214 }
6215
6216 return err;
6217}
6218
6219/**
6220 * smb2_cancel() - handler for smb2 cancel command
6221 * @work: smb work containing cancel command buffer
6222 *
6223 * Return: 0 on success, otherwise error
6224 */
6225int smb2_cancel(struct ksmbd_work *work)
6226{
6227 struct ksmbd_conn *conn = work->conn;
e5066499 6228 struct smb2_hdr *hdr = work->request_buf;
e2f34481
NJ
6229 struct smb2_hdr *chdr;
6230 struct ksmbd_work *cancel_work = NULL;
6231 struct list_head *tmp;
6232 int canceled = 0;
6233 struct list_head *command_list;
6234
6235 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
6236 hdr->MessageId, hdr->Flags);
6237
6238 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6239 command_list = &conn->async_requests;
6240
6241 spin_lock(&conn->request_lock);
6242 list_for_each(tmp, command_list) {
6243 cancel_work = list_entry(tmp, struct ksmbd_work,
6244 async_request_entry);
e5066499 6245 chdr = cancel_work->request_buf;
e2f34481
NJ
6246
6247 if (cancel_work->async_id !=
64b39f4a 6248 le64_to_cpu(hdr->Id.AsyncId))
e2f34481
NJ
6249 continue;
6250
6251 ksmbd_debug(SMB,
6252 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6253 le64_to_cpu(hdr->Id.AsyncId),
6254 le16_to_cpu(chdr->Command));
6255 canceled = 1;
6256 break;
6257 }
6258 spin_unlock(&conn->request_lock);
6259 } else {
6260 command_list = &conn->requests;
6261
6262 spin_lock(&conn->request_lock);
6263 list_for_each(tmp, command_list) {
6264 cancel_work = list_entry(tmp, struct ksmbd_work,
6265 request_entry);
e5066499 6266 chdr = cancel_work->request_buf;
e2f34481
NJ
6267
6268 if (chdr->MessageId != hdr->MessageId ||
64b39f4a 6269 cancel_work == work)
e2f34481
NJ
6270 continue;
6271
6272 ksmbd_debug(SMB,
6273 "smb2 with mid %llu cancelled command = 0x%x\n",
6274 le64_to_cpu(hdr->MessageId),
6275 le16_to_cpu(chdr->Command));
6276 canceled = 1;
6277 break;
6278 }
6279 spin_unlock(&conn->request_lock);
6280 }
6281
6282 if (canceled) {
6283 cancel_work->state = KSMBD_WORK_CANCELLED;
6284 if (cancel_work->cancel_fn)
6285 cancel_work->cancel_fn(cancel_work->cancel_argv);
6286 }
6287
6288 /* For SMB2_CANCEL command itself send no response*/
6289 work->send_no_response = 1;
6290 return 0;
6291}
6292
6293struct file_lock *smb_flock_init(struct file *f)
6294{
6295 struct file_lock *fl;
6296
6297 fl = locks_alloc_lock();
6298 if (!fl)
6299 goto out;
6300
6301 locks_init_lock(fl);
6302
6303 fl->fl_owner = f;
6304 fl->fl_pid = current->tgid;
6305 fl->fl_file = f;
6306 fl->fl_flags = FL_POSIX;
6307 fl->fl_ops = NULL;
6308 fl->fl_lmops = NULL;
6309
6310out:
6311 return fl;
6312}
6313
6314static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6315{
6316 int cmd = -EINVAL;
6317
6318 /* Checking for wrong flag combination during lock request*/
6319 switch (flags) {
6320 case SMB2_LOCKFLAG_SHARED:
6321 ksmbd_debug(SMB, "received shared request\n");
6322 cmd = F_SETLKW;
6323 flock->fl_type = F_RDLCK;
6324 flock->fl_flags |= FL_SLEEP;
6325 break;
6326 case SMB2_LOCKFLAG_EXCLUSIVE:
6327 ksmbd_debug(SMB, "received exclusive request\n");
6328 cmd = F_SETLKW;
6329 flock->fl_type = F_WRLCK;
6330 flock->fl_flags |= FL_SLEEP;
6331 break;
64b39f4a 6332 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
e2f34481
NJ
6333 ksmbd_debug(SMB,
6334 "received shared & fail immediately request\n");
6335 cmd = F_SETLK;
6336 flock->fl_type = F_RDLCK;
6337 break;
64b39f4a 6338 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
e2f34481
NJ
6339 ksmbd_debug(SMB,
6340 "received exclusive & fail immediately request\n");
6341 cmd = F_SETLK;
6342 flock->fl_type = F_WRLCK;
6343 break;
6344 case SMB2_LOCKFLAG_UNLOCK:
6345 ksmbd_debug(SMB, "received unlock request\n");
6346 flock->fl_type = F_UNLCK;
6347 cmd = 0;
6348 break;
6349 }
6350
6351 return cmd;
6352}
6353
6354static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
64b39f4a 6355 unsigned int cmd, int flags, struct list_head *lock_list)
e2f34481
NJ
6356{
6357 struct ksmbd_lock *lock;
6358
6359 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6360 if (!lock)
6361 return NULL;
6362
6363 lock->cmd = cmd;
6364 lock->fl = flock;
6365 lock->start = flock->fl_start;
6366 lock->end = flock->fl_end;
6367 lock->flags = flags;
6368 if (lock->start == lock->end)
6369 lock->zero_len = 1;
6370 INIT_LIST_HEAD(&lock->llist);
6371 INIT_LIST_HEAD(&lock->glist);
6372 list_add_tail(&lock->llist, lock_list);
6373
6374 return lock;
6375}
6376
6377static void smb2_remove_blocked_lock(void **argv)
6378{
6379 struct file_lock *flock = (struct file_lock *)argv[0];
6380
6381 ksmbd_vfs_posix_lock_unblock(flock);
6382 wake_up(&flock->fl_wait);
6383}
6384
6385static inline bool lock_defer_pending(struct file_lock *fl)
6386{
6387 /* check pending lock waiters */
6388 return waitqueue_active(&fl->fl_wait);
6389}
6390
6391/**
6392 * smb2_lock() - handler for smb2 file lock command
6393 * @work: smb work containing lock command buffer
6394 *
6395 * Return: 0 on success, otherwise error
6396 */
6397int smb2_lock(struct ksmbd_work *work)
6398{
e5066499
NJ
6399 struct smb2_lock_req *req = work->request_buf;
6400 struct smb2_lock_rsp *rsp = work->response_buf;
e2f34481
NJ
6401 struct smb2_lock_element *lock_ele;
6402 struct ksmbd_file *fp = NULL;
6403 struct file_lock *flock = NULL;
6404 struct file *filp = NULL;
6405 int lock_count;
6406 int flags = 0;
6407 int cmd = 0;
6408 int err = 0, i;
64b39f4a 6409 u64 lock_length;
e2f34481
NJ
6410 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp;
6411 int nolock = 0;
6412 LIST_HEAD(lock_list);
6413 LIST_HEAD(rollback_list);
6414 int prior_lock = 0;
6415
6416 ksmbd_debug(SMB, "Received lock request\n");
6417 fp = ksmbd_lookup_fd_slow(work,
64b39f4a
NJ
6418 le64_to_cpu(req->VolatileFileId),
6419 le64_to_cpu(req->PersistentFileId));
e2f34481
NJ
6420 if (!fp) {
6421 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
6422 le64_to_cpu(req->VolatileFileId));
6423 rsp->hdr.Status = STATUS_FILE_CLOSED;
6424 goto out2;
6425 }
6426
6427 filp = fp->filp;
6428 lock_count = le16_to_cpu(req->LockCount);
6429 lock_ele = req->locks;
6430
6431 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
6432 if (!lock_count) {
6433 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6434 goto out2;
6435 }
6436
6437 for (i = 0; i < lock_count; i++) {
6438 flags = le32_to_cpu(lock_ele[i].Flags);
6439
6440 flock = smb_flock_init(filp);
6441 if (!flock) {
6442 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6443 goto out;
6444 }
6445
6446 cmd = smb2_set_flock_flags(flock, flags);
6447
6448 flock->fl_start = le64_to_cpu(lock_ele[i].Offset);
6449 if (flock->fl_start > OFFSET_MAX) {
6450 ksmbd_err("Invalid lock range requested\n");
6451 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6452 goto out;
6453 }
6454
6455 lock_length = le64_to_cpu(lock_ele[i].Length);
6456 if (lock_length > 0) {
64b39f4a 6457 if (lock_length > OFFSET_MAX - flock->fl_start) {
e2f34481
NJ
6458 ksmbd_debug(SMB,
6459 "Invalid lock range requested\n");
6460 lock_length = OFFSET_MAX - flock->fl_start;
6461 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6462 goto out;
6463 }
64b39f4a 6464 } else {
e2f34481 6465 lock_length = 0;
64b39f4a 6466 }
e2f34481
NJ
6467
6468 flock->fl_end = flock->fl_start + lock_length;
6469
6470 if (flock->fl_end < flock->fl_start) {
6471 ksmbd_debug(SMB,
6472 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6473 flock->fl_end, flock->fl_start);
6474 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6475 goto out;
6476 }
6477
6478 /* Check conflict locks in one request */
6479 list_for_each_entry(cmp_lock, &lock_list, llist) {
6480 if (cmp_lock->fl->fl_start <= flock->fl_start &&
64b39f4a 6481 cmp_lock->fl->fl_end >= flock->fl_end) {
e2f34481 6482 if (cmp_lock->fl->fl_type != F_UNLCK &&
64b39f4a 6483 flock->fl_type != F_UNLCK) {
e2f34481
NJ
6484 ksmbd_err("conflict two locks in one request\n");
6485 rsp->hdr.Status =
6486 STATUS_INVALID_PARAMETER;
6487 goto out;
6488 }
6489 }
6490 }
6491
6492 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6493 if (!smb_lock) {
6494 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6495 goto out;
6496 }
6497 }
6498
6499 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6500 if (smb_lock->cmd < 0) {
6501 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6502 goto out;
6503 }
6504
6505 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
6506 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6507 goto out;
6508 }
6509
64b39f4a
NJ
6510 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6511 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6512 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6513 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
e2f34481
NJ
6514 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6515 goto out;
6516 }
6517
6518 prior_lock = smb_lock->flags;
6519
6520 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
64b39f4a 6521 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
e2f34481
NJ
6522 goto no_check_gl;
6523
6524 nolock = 1;
6525 /* check locks in global list */
6526 list_for_each_entry(cmp_lock, &global_lock_list, glist) {
6527 if (file_inode(cmp_lock->fl->fl_file) !=
64b39f4a 6528 file_inode(smb_lock->fl->fl_file))
e2f34481
NJ
6529 continue;
6530
6531 if (smb_lock->fl->fl_type == F_UNLCK) {
64b39f4a
NJ
6532 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6533 cmp_lock->start == smb_lock->start &&
6534 cmp_lock->end == smb_lock->end &&
6535 !lock_defer_pending(cmp_lock->fl)) {
e2f34481
NJ
6536 nolock = 0;
6537 locks_free_lock(cmp_lock->fl);
6538 list_del(&cmp_lock->glist);
6539 kfree(cmp_lock);
6540 break;
6541 }
6542 continue;
6543 }
6544
6545 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6546 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6547 continue;
6548 } else {
6549 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6550 continue;
6551 }
6552
6553 /* check zero byte lock range */
6554 if (cmp_lock->zero_len && !smb_lock->zero_len &&
64b39f4a
NJ
6555 cmp_lock->start > smb_lock->start &&
6556 cmp_lock->start < smb_lock->end) {
e2f34481
NJ
6557 ksmbd_err("previous lock conflict with zero byte lock range\n");
6558 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6559 goto out;
6560 }
6561
6562 if (smb_lock->zero_len && !cmp_lock->zero_len &&
64b39f4a
NJ
6563 smb_lock->start > cmp_lock->start &&
6564 smb_lock->start < cmp_lock->end) {
e2f34481
NJ
6565 ksmbd_err("current lock conflict with zero byte lock range\n");
6566 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6567 goto out;
6568 }
6569
6570 if (((cmp_lock->start <= smb_lock->start &&
64b39f4a
NJ
6571 cmp_lock->end > smb_lock->start) ||
6572 (cmp_lock->start < smb_lock->end && cmp_lock->end >= smb_lock->end)) &&
6573 !cmp_lock->zero_len && !smb_lock->zero_len) {
e2f34481
NJ
6574 ksmbd_err("Not allow lock operation on exclusive lock range\n");
6575 rsp->hdr.Status =
6576 STATUS_LOCK_NOT_GRANTED;
6577 goto out;
6578 }
6579 }
6580
6581 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
6582 ksmbd_err("Try to unlock nolocked range\n");
6583 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6584 goto out;
6585 }
6586
6587no_check_gl:
6588 if (smb_lock->zero_len) {
6589 err = 0;
6590 goto skip;
6591 }
6592
6593 flock = smb_lock->fl;
6594 list_del(&smb_lock->llist);
6595retry:
6596 err = ksmbd_vfs_lock(filp, smb_lock->cmd, flock);
6597skip:
6598 if (flags & SMB2_LOCKFLAG_UNLOCK) {
64b39f4a 6599 if (!err) {
e2f34481 6600 ksmbd_debug(SMB, "File unlocked\n");
64b39f4a 6601 } else if (err == -ENOENT) {
e2f34481
NJ
6602 rsp->hdr.Status = STATUS_NOT_LOCKED;
6603 goto out;
6604 }
6605 locks_free_lock(flock);
6606 kfree(smb_lock);
6607 } else {
6608 if (err == FILE_LOCK_DEFERRED) {
6609 void **argv;
6610
6611 ksmbd_debug(SMB,
6612 "would have to wait for getting lock\n");
6613 list_add_tail(&smb_lock->glist,
6614 &global_lock_list);
6615 list_add(&smb_lock->llist, &rollback_list);
6616
6617 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6618 if (!argv) {
6619 err = -ENOMEM;
6620 goto out;
6621 }
6622 argv[0] = flock;
6623
6624 err = setup_async_work(work,
6625 smb2_remove_blocked_lock, argv);
6626 if (err) {
6627 rsp->hdr.Status =
6628 STATUS_INSUFFICIENT_RESOURCES;
6629 goto out;
6630 }
6631 spin_lock(&fp->f_lock);
6632 list_add(&work->fp_entry, &fp->blocked_works);
6633 spin_unlock(&fp->f_lock);
6634
6635 smb2_send_interim_resp(work, STATUS_PENDING);
6636
6637 err = ksmbd_vfs_posix_lock_wait(flock);
6638
6639 if (!WORK_ACTIVE(work)) {
6640 list_del(&smb_lock->llist);
6641 list_del(&smb_lock->glist);
6642 locks_free_lock(flock);
6643
6644 if (WORK_CANCELLED(work)) {
6645 spin_lock(&fp->f_lock);
6646 list_del(&work->fp_entry);
6647 spin_unlock(&fp->f_lock);
6648 rsp->hdr.Status =
6649 STATUS_CANCELLED;
6650 kfree(smb_lock);
6651 smb2_send_interim_resp(work,
6652 STATUS_CANCELLED);
6653 work->send_no_response = 1;
6654 goto out;
6655 }
6656 init_smb2_rsp_hdr(work);
6657 smb2_set_err_rsp(work);
6658 rsp->hdr.Status =
6659 STATUS_RANGE_NOT_LOCKED;
6660 kfree(smb_lock);
6661 goto out2;
6662 }
6663
6664 list_del(&smb_lock->llist);
6665 list_del(&smb_lock->glist);
6666 spin_lock(&fp->f_lock);
6667 list_del(&work->fp_entry);
6668 spin_unlock(&fp->f_lock);
6669 goto retry;
6670 } else if (!err) {
6671 list_add_tail(&smb_lock->glist,
6672 &global_lock_list);
6673 list_add(&smb_lock->llist, &rollback_list);
6674 ksmbd_debug(SMB, "successful in taking lock\n");
6675 } else {
6676 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6677 goto out;
6678 }
6679 }
6680 }
6681
6682 if (atomic_read(&fp->f_ci->op_count) > 1)
6683 smb_break_all_oplock(work, fp);
6684
6685 rsp->StructureSize = cpu_to_le16(4);
6686 ksmbd_debug(SMB, "successful in taking lock\n");
6687 rsp->hdr.Status = STATUS_SUCCESS;
6688 rsp->Reserved = 0;
6689 inc_rfc1001_len(rsp, 4);
6690 ksmbd_fd_put(work, fp);
6691 return err;
6692
6693out:
6694 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6695 locks_free_lock(smb_lock->fl);
6696 list_del(&smb_lock->llist);
6697 kfree(smb_lock);
6698 }
6699
6700 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
6701 struct file_lock *rlock = NULL;
6702
6703 rlock = smb_flock_init(filp);
6704 rlock->fl_type = F_UNLCK;
6705 rlock->fl_start = smb_lock->start;
6706 rlock->fl_end = smb_lock->end;
6707
6708 err = ksmbd_vfs_lock(filp, 0, rlock);
6709 if (err)
6710 ksmbd_err("rollback unlock fail : %d\n", err);
6711 list_del(&smb_lock->llist);
6712 list_del(&smb_lock->glist);
6713 locks_free_lock(smb_lock->fl);
6714 locks_free_lock(rlock);
6715 kfree(smb_lock);
6716 }
6717out2:
6718 ksmbd_debug(SMB, "failed in taking lock(flags : %x)\n", flags);
6719 smb2_set_err_rsp(work);
6720 ksmbd_fd_put(work, fp);
6721 return 0;
6722}
6723
64b39f4a
NJ
6724static int fsctl_copychunk(struct ksmbd_work *work, struct smb2_ioctl_req *req,
6725 struct smb2_ioctl_rsp *rsp)
e2f34481
NJ
6726{
6727 struct copychunk_ioctl_req *ci_req;
6728 struct copychunk_ioctl_rsp *ci_rsp;
6729 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
6730 struct srv_copychunk *chunks;
6731 unsigned int i, chunk_count, chunk_count_written = 0;
6732 unsigned int chunk_size_written = 0;
6733 loff_t total_size_written = 0;
6734 int ret, cnt_code;
6735
6736 cnt_code = le32_to_cpu(req->CntCode);
6737 ci_req = (struct copychunk_ioctl_req *)&req->Buffer[0];
6738 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
6739
6740 rsp->VolatileFileId = req->VolatileFileId;
6741 rsp->PersistentFileId = req->PersistentFileId;
64b39f4a
NJ
6742 ci_rsp->ChunksWritten =
6743 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
6744 ci_rsp->ChunkBytesWritten =
6745 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
6746 ci_rsp->TotalBytesWritten =
6747 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
e2f34481
NJ
6748
6749 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
6750 chunk_count = le32_to_cpu(ci_req->ChunkCount);
6751 total_size_written = 0;
6752
6753 /* verify the SRV_COPYCHUNK_COPY packet */
6754 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
64b39f4a
NJ
6755 le32_to_cpu(req->InputCount) <
6756 offsetof(struct copychunk_ioctl_req, Chunks) +
6757 chunk_count * sizeof(struct srv_copychunk)) {
e2f34481
NJ
6758 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6759 return -EINVAL;
6760 }
6761
6762 for (i = 0; i < chunk_count; i++) {
6763 if (le32_to_cpu(chunks[i].Length) == 0 ||
64b39f4a 6764 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
e2f34481
NJ
6765 break;
6766 total_size_written += le32_to_cpu(chunks[i].Length);
6767 }
64b39f4a
NJ
6768
6769 if (i < chunk_count ||
6770 total_size_written > ksmbd_server_side_copy_max_total_size()) {
e2f34481
NJ
6771 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6772 return -EINVAL;
6773 }
6774
6775 src_fp = ksmbd_lookup_foreign_fd(work,
6776 le64_to_cpu(ci_req->ResumeKey[0]));
6777 dst_fp = ksmbd_lookup_fd_slow(work,
6778 le64_to_cpu(req->VolatileFileId),
6779 le64_to_cpu(req->PersistentFileId));
e2f34481 6780 ret = -EINVAL;
64b39f4a
NJ
6781 if (!src_fp ||
6782 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
e2f34481
NJ
6783 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
6784 goto out;
6785 }
64b39f4a 6786
e2f34481
NJ
6787 if (!dst_fp) {
6788 rsp->hdr.Status = STATUS_FILE_CLOSED;
6789 goto out;
6790 }
6791
6792 /*
6793 * FILE_READ_DATA should only be included in
6794 * the FSCTL_COPYCHUNK case
6795 */
6796 if (cnt_code == FSCTL_COPYCHUNK && !(dst_fp->daccess &
6797 (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
6798 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6799 goto out;
6800 }
6801
6802 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
6803 chunks, chunk_count,
6804 &chunk_count_written, &chunk_size_written,
6805 &total_size_written);
6806 if (ret < 0) {
6807 if (ret == -EACCES)
6808 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6809 if (ret == -EAGAIN)
6810 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6811 else if (ret == -EBADF)
6812 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6813 else if (ret == -EFBIG || ret == -ENOSPC)
6814 rsp->hdr.Status = STATUS_DISK_FULL;
6815 else if (ret == -EINVAL)
6816 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6817 else if (ret == -EISDIR)
6818 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
6819 else if (ret == -E2BIG)
6820 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
6821 else
6822 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6823 }
6824
6825 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
6826 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
6827 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
6828out:
6829 ksmbd_fd_put(work, src_fp);
6830 ksmbd_fd_put(work, dst_fp);
6831 return ret;
6832}
6833
6834static __be32 idev_ipv4_address(struct in_device *idev)
6835{
6836 __be32 addr = 0;
6837
6838 struct in_ifaddr *ifa;
6839
6840 rcu_read_lock();
6841 in_dev_for_each_ifa_rcu(ifa, idev) {
6842 if (ifa->ifa_flags & IFA_F_SECONDARY)
6843 continue;
6844
6845 addr = ifa->ifa_address;
6846 break;
6847 }
6848 rcu_read_unlock();
6849 return addr;
6850}
6851
6852static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
64b39f4a 6853 struct smb2_ioctl_req *req, struct smb2_ioctl_rsp *rsp)
e2f34481
NJ
6854{
6855 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
6856 int nbytes = 0;
6857 struct net_device *netdev;
6858 struct sockaddr_storage_rsp *sockaddr_storage;
6859 unsigned int flags;
6860 unsigned long long speed;
6861
6862 rtnl_lock();
6863 for_each_netdev(&init_net, netdev) {
6864 if (unlikely(!netdev)) {
6865 rtnl_unlock();
6866 return -EINVAL;
6867 }
6868
6869 if (netdev->type == ARPHRD_LOOPBACK)
6870 continue;
6871
6872 flags = dev_get_flags(netdev);
6873 if (!(flags & IFF_RUNNING))
6874 continue;
6875
6876 nii_rsp = (struct network_interface_info_ioctl_rsp *)
6877 &rsp->Buffer[nbytes];
6878 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
6879
6880 /* TODO: specify the RDMA capabilities */
6881 if (netdev->num_tx_queues > 1)
6882 nii_rsp->Capability = cpu_to_le32(RSS_CAPABLE);
6883 else
6884 nii_rsp->Capability = 0;
6885
6886 nii_rsp->Next = cpu_to_le32(152);
6887 nii_rsp->Reserved = 0;
6888
6889 if (netdev->ethtool_ops->get_link_ksettings) {
6890 struct ethtool_link_ksettings cmd;
6891
6892 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
6893 speed = cmd.base.speed;
6894 } else {
64b39f4a
NJ
6895 ksmbd_err("%s %s\n", netdev->name,
6896 "speed is unknown, defaulting to 1Gb/sec");
e2f34481
NJ
6897 speed = SPEED_1000;
6898 }
6899
6900 speed *= 1000000;
6901 nii_rsp->LinkSpeed = cpu_to_le64(speed);
6902
6903 sockaddr_storage = (struct sockaddr_storage_rsp *)
6904 nii_rsp->SockAddr_Storage;
6905 memset(sockaddr_storage, 0, 128);
6906
6907 if (conn->peer_addr.ss_family == PF_INET) {
6908 struct in_device *idev;
6909
6910 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
6911 sockaddr_storage->addr4.Port = 0;
6912
6913 idev = __in_dev_get_rtnl(netdev);
6914 if (!idev)
6915 continue;
6916 sockaddr_storage->addr4.IPv4address =
6917 idev_ipv4_address(idev);
6918 } else {
6919 struct inet6_dev *idev6;
6920 struct inet6_ifaddr *ifa;
6921 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
6922
6923 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
6924 sockaddr_storage->addr6.Port = 0;
6925 sockaddr_storage->addr6.FlowInfo = 0;
6926
6927 idev6 = __in6_dev_get(netdev);
6928 if (!idev6)
6929 continue;
6930
6931 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
6932 if (ifa->flags & (IFA_F_TENTATIVE |
6933 IFA_F_DEPRECATED))
6934 continue;
6935 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
6936 break;
6937 }
6938 sockaddr_storage->addr6.ScopeId = 0;
6939 }
6940
6941 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
6942 }
6943 rtnl_unlock();
6944
6945 /* zero if this is last one */
6946 if (nii_rsp)
6947 nii_rsp->Next = 0;
6948
6949 if (!nbytes) {
6950 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
6951 return -EINVAL;
6952 }
6953
6954 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
6955 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
6956 return nbytes;
6957}
6958
e2f34481 6959static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
64b39f4a
NJ
6960 struct validate_negotiate_info_req *neg_req,
6961 struct validate_negotiate_info_rsp *neg_rsp)
e2f34481
NJ
6962{
6963 int ret = 0;
6964 int dialect;
6965
6966 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
6967 neg_req->DialectCount);
6968 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
6969 ret = -EINVAL;
6970 goto err_out;
6971 }
6972
6973 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
6974 ret = -EINVAL;
6975 goto err_out;
6976 }
6977
6978 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
6979 ret = -EINVAL;
6980 goto err_out;
6981 }
6982
6983 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
6984 ret = -EINVAL;
6985 goto err_out;
6986 }
6987
6988 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
6989 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
6990 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
6991 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
6992err_out:
6993 return ret;
6994}
6995
64b39f4a
NJ
6996static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
6997 struct file_allocated_range_buffer *qar_req,
6998 struct file_allocated_range_buffer *qar_rsp,
6999 int in_count, int *out_count)
e2f34481
NJ
7000{
7001 struct ksmbd_file *fp;
7002 loff_t start, length;
7003 int ret = 0;
7004
7005 *out_count = 0;
7006 if (in_count == 0)
7007 return -EINVAL;
7008
7009 fp = ksmbd_lookup_fd_fast(work, id);
7010 if (!fp)
7011 return -ENOENT;
7012
7013 start = le64_to_cpu(qar_req->file_offset);
7014 length = le64_to_cpu(qar_req->length);
7015
7016 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
7017 qar_rsp, in_count, out_count);
7018 if (ret && ret != -E2BIG)
7019 *out_count = 0;
7020
7021 ksmbd_fd_put(work, fp);
7022 return ret;
7023}
7024
64b39f4a
NJ
7025static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
7026 int out_buf_len, struct smb2_ioctl_req *req,
7027 struct smb2_ioctl_rsp *rsp)
e2f34481
NJ
7028{
7029 struct ksmbd_rpc_command *rpc_resp;
7030 char *data_buf = (char *)&req->Buffer[0];
7031 int nbytes = 0;
7032
64b39f4a 7033 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
e2f34481
NJ
7034 le32_to_cpu(req->InputCount));
7035 if (rpc_resp) {
7036 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7037 /*
7038 * set STATUS_SOME_NOT_MAPPED response
7039 * for unknown domain sid.
7040 */
7041 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7042 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7043 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7044 goto out;
7045 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7046 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7047 goto out;
7048 }
7049
7050 nbytes = rpc_resp->payload_sz;
7051 if (rpc_resp->payload_sz > out_buf_len) {
7052 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7053 nbytes = out_buf_len;
7054 }
7055
7056 if (!rpc_resp->payload_sz) {
7057 rsp->hdr.Status =
7058 STATUS_UNEXPECTED_IO_ERROR;
7059 goto out;
7060 }
7061
7062 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7063 }
7064out:
79f6b11a 7065 kvfree(rpc_resp);
e2f34481
NJ
7066 return nbytes;
7067}
7068
64b39f4a
NJ
7069static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
7070 struct file_sparse *sparse)
e2f34481
NJ
7071{
7072 struct ksmbd_file *fp;
7073 int ret = 0;
7074 __le32 old_fattr;
7075
7076 fp = ksmbd_lookup_fd_fast(work, id);
7077 if (!fp)
7078 return -ENOENT;
7079
7080 old_fattr = fp->f_ci->m_fattr;
7081 if (sparse->SetSparse)
7082 fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE;
7083 else
7084 fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE;
7085
7086 if (fp->f_ci->m_fattr != old_fattr &&
64b39f4a
NJ
7087 test_share_config_flag(work->tcon->share_conf,
7088 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
e2f34481
NJ
7089 struct xattr_dos_attrib da;
7090
7091 ret = ksmbd_vfs_get_dos_attrib_xattr(fp->filp->f_path.dentry, &da);
7092 if (ret <= 0)
7093 goto out;
7094
7095 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
7096 ret = ksmbd_vfs_set_dos_attrib_xattr(fp->filp->f_path.dentry, &da);
7097 if (ret)
7098 fp->f_ci->m_fattr = old_fattr;
7099 }
7100
7101out:
7102 ksmbd_fd_put(work, fp);
7103 return ret;
7104}
7105
7106static int fsctl_request_resume_key(struct ksmbd_work *work,
64b39f4a
NJ
7107 struct smb2_ioctl_req *req,
7108 struct resume_key_ioctl_rsp *key_rsp)
e2f34481
NJ
7109{
7110 struct ksmbd_file *fp;
7111
7112 fp = ksmbd_lookup_fd_slow(work,
7113 le64_to_cpu(req->VolatileFileId),
7114 le64_to_cpu(req->PersistentFileId));
7115 if (!fp)
7116 return -ENOENT;
7117
7118 memset(key_rsp, 0, sizeof(*key_rsp));
7119 key_rsp->ResumeKey[0] = req->VolatileFileId;
7120 key_rsp->ResumeKey[1] = req->PersistentFileId;
7121 ksmbd_fd_put(work, fp);
7122
7123 return 0;
7124}
7125
7126/**
7127 * smb2_ioctl() - handler for smb2 ioctl command
7128 * @work: smb work containing ioctl command buffer
7129 *
7130 * Return: 0 on success, otherwise error
7131 */
7132int smb2_ioctl(struct ksmbd_work *work)
7133{
7134 struct smb2_ioctl_req *req;
7135 struct smb2_ioctl_rsp *rsp, *rsp_org;
7136 int cnt_code, nbytes = 0;
7137 int out_buf_len;
64b39f4a 7138 u64 id = KSMBD_NO_FID;
e2f34481
NJ
7139 struct ksmbd_conn *conn = work->conn;
7140 int ret = 0;
7141
e5066499 7142 rsp_org = work->response_buf;
e2f34481
NJ
7143 if (work->next_smb2_rcv_hdr_off) {
7144 req = REQUEST_BUF_NEXT(work);
7145 rsp = RESPONSE_BUF_NEXT(work);
7146 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
7147 ksmbd_debug(SMB, "Compound request set FID = %u\n",
7148 work->compound_fid);
7149 id = work->compound_fid;
7150 }
7151 } else {
e5066499
NJ
7152 req = work->request_buf;
7153 rsp = work->response_buf;
e2f34481
NJ
7154 }
7155
7156 if (!HAS_FILE_ID(id))
7157 id = le64_to_cpu(req->VolatileFileId);
7158
7159 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7160 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7161 goto out;
7162 }
7163
7164 cnt_code = le32_to_cpu(req->CntCode);
7165 out_buf_len = le32_to_cpu(req->MaxOutputResponse);
7166 out_buf_len = min(KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
7167
7168 switch (cnt_code) {
7169 case FSCTL_DFS_GET_REFERRALS:
7170 case FSCTL_DFS_GET_REFERRALS_EX:
7171 /* Not support DFS yet */
7172 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7173 goto out;
7174 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7175 {
7176 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7177
7178 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7179 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7180 &rsp->Buffer[0];
7181
7182 /*
7183 * TODO: This is dummy implementation to pass smbtorture
7184 * Need to check correct response later
7185 */
7186 memset(obj_buf->ObjectId, 0x0, 16);
7187 memset(obj_buf->BirthVolumeId, 0x0, 16);
7188 memset(obj_buf->BirthObjectId, 0x0, 16);
7189 memset(obj_buf->DomainId, 0x0, 16);
7190
7191 break;
7192 }
7193 case FSCTL_PIPE_TRANSCEIVE:
7194 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7195 break;
7196 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7197 if (conn->dialect < SMB30_PROT_ID) {
7198 ret = -EOPNOTSUPP;
7199 goto out;
7200 }
7201
7202 ret = fsctl_validate_negotiate_info(conn,
7203 (struct validate_negotiate_info_req *)&req->Buffer[0],
7204 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0]);
7205 if (ret < 0)
7206 goto out;
7207
7208 nbytes = sizeof(struct validate_negotiate_info_rsp);
7209 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7210 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7211 break;
7212 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
7213 nbytes = fsctl_query_iface_info_ioctl(conn, req, rsp);
7214 if (nbytes < 0)
7215 goto out;
7216 break;
7217 case FSCTL_REQUEST_RESUME_KEY:
7218 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7219 ret = -EINVAL;
7220 goto out;
7221 }
7222
7223 ret = fsctl_request_resume_key(work, req,
7224 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
7225 if (ret < 0)
7226 goto out;
7227 rsp->PersistentFileId = req->PersistentFileId;
7228 rsp->VolatileFileId = req->VolatileFileId;
7229 nbytes = sizeof(struct resume_key_ioctl_rsp);
7230 break;
7231 case FSCTL_COPYCHUNK:
7232 case FSCTL_COPYCHUNK_WRITE:
64b39f4a 7233 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
e2f34481
NJ
7234 ksmbd_debug(SMB,
7235 "User does not have write permission\n");
7236 ret = -EACCES;
7237 goto out;
7238 }
7239
7240 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7241 ret = -EINVAL;
7242 goto out;
7243 }
7244
7245 nbytes = sizeof(struct copychunk_ioctl_rsp);
7246 fsctl_copychunk(work, req, rsp);
7247 break;
7248 case FSCTL_SET_SPARSE:
7249 ret = fsctl_set_sparse(work, id,
7250 (struct file_sparse *)&req->Buffer[0]);
7251 if (ret < 0)
7252 goto out;
7253 break;
7254 case FSCTL_SET_ZERO_DATA:
7255 {
7256 struct file_zero_data_information *zero_data;
7257 struct ksmbd_file *fp;
7258 loff_t off, len;
7259
64b39f4a 7260 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
e2f34481
NJ
7261 ksmbd_debug(SMB,
7262 "User does not have write permission\n");
7263 ret = -EACCES;
7264 goto out;
7265 }
7266
7267 zero_data =
7268 (struct file_zero_data_information *)&req->Buffer[0];
7269
7270 fp = ksmbd_lookup_fd_fast(work, id);
7271 if (!fp) {
7272 ret = -ENOENT;
7273 goto out;
7274 }
7275
7276 off = le64_to_cpu(zero_data->FileOffset);
7277 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7278
7279 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7280 ksmbd_fd_put(work, fp);
7281 if (ret < 0)
7282 goto out;
7283 break;
7284 }
7285 case FSCTL_QUERY_ALLOCATED_RANGES:
7286 ret = fsctl_query_allocated_ranges(work, id,
7287 (struct file_allocated_range_buffer *)&req->Buffer[0],
7288 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7289 out_buf_len /
7290 sizeof(struct file_allocated_range_buffer), &nbytes);
7291 if (ret == -E2BIG) {
7292 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7293 } else if (ret < 0) {
7294 nbytes = 0;
7295 goto out;
7296 }
7297
7298 nbytes *= sizeof(struct file_allocated_range_buffer);
7299 break;
7300 case FSCTL_GET_REPARSE_POINT:
7301 {
7302 struct reparse_data_buffer *reparse_ptr;
7303 struct ksmbd_file *fp;
7304
7305 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7306 fp = ksmbd_lookup_fd_fast(work, id);
7307 if (!fp) {
7308 ksmbd_err("not found fp!!\n");
7309 ret = -ENOENT;
7310 goto out;
7311 }
7312
7313 reparse_ptr->ReparseTag =
7314 smb2_get_reparse_tag_special_file(FP_INODE(fp)->i_mode);
7315 reparse_ptr->ReparseDataLength = 0;
7316 ksmbd_fd_put(work, fp);
7317 nbytes = sizeof(struct reparse_data_buffer);
7318 break;
7319 }
7320 default:
7321 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
7322 cnt_code);
7323 ret = -EOPNOTSUPP;
7324 goto out;
7325 }
7326
7327 rsp->CntCode = cpu_to_le32(cnt_code);
7328 rsp->InputCount = cpu_to_le32(0);
7329 rsp->InputOffset = cpu_to_le32(112);
7330 rsp->OutputOffset = cpu_to_le32(112);
7331 rsp->OutputCount = cpu_to_le32(nbytes);
7332 rsp->StructureSize = cpu_to_le16(49);
7333 rsp->Reserved = cpu_to_le16(0);
7334 rsp->Flags = cpu_to_le32(0);
7335 rsp->Reserved2 = cpu_to_le32(0);
7336 inc_rfc1001_len(rsp_org, 48 + nbytes);
7337
7338 return 0;
7339
7340out:
7341 if (ret == -EACCES)
7342 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7343 else if (ret == -ENOENT)
7344 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7345 else if (ret == -EOPNOTSUPP)
7346 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7347 else if (ret < 0 || rsp->hdr.Status == 0)
7348 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7349 smb2_set_err_rsp(work);
7350 return 0;
7351}
7352
7353/**
7354 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7355 * @work: smb work containing oplock break command buffer
7356 *
7357 * Return: 0
7358 */
7359static void smb20_oplock_break_ack(struct ksmbd_work *work)
7360{
e5066499
NJ
7361 struct smb2_oplock_break *req = work->request_buf;
7362 struct smb2_oplock_break *rsp = work->response_buf;
e2f34481
NJ
7363 struct ksmbd_file *fp;
7364 struct oplock_info *opinfo = NULL;
7365 __le32 err = 0;
7366 int ret = 0;
64b39f4a 7367 u64 volatile_id, persistent_id;
e2f34481
NJ
7368 char req_oplevel = 0, rsp_oplevel = 0;
7369 unsigned int oplock_change_type;
7370
7371 volatile_id = le64_to_cpu(req->VolatileFid);
7372 persistent_id = le64_to_cpu(req->PersistentFid);
7373 req_oplevel = req->OplockLevel;
7374 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7375 volatile_id, persistent_id, req_oplevel);
7376
7377 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7378 if (!fp) {
7379 rsp->hdr.Status = STATUS_FILE_CLOSED;
7380 smb2_set_err_rsp(work);
7381 return;
7382 }
7383
7384 opinfo = opinfo_get(fp);
7385 if (!opinfo) {
7386 ksmbd_err("unexpected null oplock_info\n");
7387 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7388 smb2_set_err_rsp(work);
7389 ksmbd_fd_put(work, fp);
7390 return;
7391 }
7392
7393 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7394 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7395 goto err_out;
7396 }
7397
7398 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7399 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7400 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7401 goto err_out;
7402 }
7403
64b39f4a
NJ
7404 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7405 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7406 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7407 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
e2f34481
NJ
7408 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7409 oplock_change_type = OPLOCK_WRITE_TO_NONE;
64b39f4a
NJ
7410 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7411 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
e2f34481
NJ
7412 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7413 oplock_change_type = OPLOCK_READ_TO_NONE;
64b39f4a
NJ
7414 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7415 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
e2f34481 7416 err = STATUS_INVALID_DEVICE_STATE;
64b39f4a
NJ
7417 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7418 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7419 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
e2f34481 7420 oplock_change_type = OPLOCK_WRITE_TO_READ;
64b39f4a
NJ
7421 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7422 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7423 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
e2f34481 7424 oplock_change_type = OPLOCK_WRITE_TO_NONE;
64b39f4a
NJ
7425 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7426 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
e2f34481 7427 oplock_change_type = OPLOCK_READ_TO_NONE;
64b39f4a 7428 } else {
e2f34481 7429 oplock_change_type = 0;
64b39f4a
NJ
7430 }
7431 } else {
e2f34481 7432 oplock_change_type = 0;
64b39f4a 7433 }
e2f34481
NJ
7434
7435 switch (oplock_change_type) {
7436 case OPLOCK_WRITE_TO_READ:
7437 ret = opinfo_write_to_read(opinfo);
7438 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7439 break;
7440 case OPLOCK_WRITE_TO_NONE:
7441 ret = opinfo_write_to_none(opinfo);
7442 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7443 break;
7444 case OPLOCK_READ_TO_NONE:
7445 ret = opinfo_read_to_none(opinfo);
7446 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7447 break;
7448 default:
7449 ksmbd_err("unknown oplock change 0x%x -> 0x%x\n",
7450 opinfo->level, rsp_oplevel);
7451 }
7452
7453 if (ret < 0) {
7454 rsp->hdr.Status = err;
7455 goto err_out;
7456 }
7457
7458 opinfo_put(opinfo);
7459 ksmbd_fd_put(work, fp);
7460 opinfo->op_state = OPLOCK_STATE_NONE;
7461 wake_up_interruptible_all(&opinfo->oplock_q);
7462
7463 rsp->StructureSize = cpu_to_le16(24);
7464 rsp->OplockLevel = rsp_oplevel;
7465 rsp->Reserved = 0;
7466 rsp->Reserved2 = 0;
7467 rsp->VolatileFid = cpu_to_le64(volatile_id);
7468 rsp->PersistentFid = cpu_to_le64(persistent_id);
7469 inc_rfc1001_len(rsp, 24);
7470 return;
7471
7472err_out:
7473 opinfo->op_state = OPLOCK_STATE_NONE;
7474 wake_up_interruptible_all(&opinfo->oplock_q);
7475
7476 opinfo_put(opinfo);
7477 ksmbd_fd_put(work, fp);
7478 smb2_set_err_rsp(work);
7479}
7480
7481static int check_lease_state(struct lease *lease, __le32 req_state)
7482{
7483 if ((lease->new_state ==
64b39f4a
NJ
7484 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7485 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
e2f34481
NJ
7486 lease->new_state = req_state;
7487 return 0;
7488 }
7489
7490 if (lease->new_state == req_state)
7491 return 0;
7492
7493 return 1;
7494}
7495
7496/**
7497 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7498 * @work: smb work containing lease break command buffer
7499 *
7500 * Return: 0
7501 */
7502static void smb21_lease_break_ack(struct ksmbd_work *work)
7503{
7504 struct ksmbd_conn *conn = work->conn;
e5066499
NJ
7505 struct smb2_lease_ack *req = work->request_buf;
7506 struct smb2_lease_ack *rsp = work->response_buf;
e2f34481
NJ
7507 struct oplock_info *opinfo;
7508 __le32 err = 0;
7509 int ret = 0;
7510 unsigned int lease_change_type;
7511 __le32 lease_state;
7512 struct lease *lease;
7513
7514 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
7515 le32_to_cpu(req->LeaseState));
7516 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7517 if (!opinfo) {
7518 ksmbd_debug(OPLOCK, "file not opened\n");
7519 smb2_set_err_rsp(work);
7520 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7521 return;
7522 }
7523 lease = opinfo->o_lease;
7524
7525 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7526 ksmbd_err("unexpected lease break state 0x%x\n",
7527 opinfo->op_state);
7528 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7529 goto err_out;
7530 }
7531
7532 if (check_lease_state(lease, req->LeaseState)) {
7533 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
7534 ksmbd_debug(OPLOCK,
7535 "req lease state: 0x%x, expected state: 0x%x\n",
7536 req->LeaseState, lease->new_state);
7537 goto err_out;
7538 }
7539
7540 if (!atomic_read(&opinfo->breaking_cnt)) {
7541 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7542 goto err_out;
7543 }
7544
7545 /* check for bad lease state */
7546 if (req->LeaseState & (~(SMB2_LEASE_READ_CACHING_LE |
64b39f4a 7547 SMB2_LEASE_HANDLE_CACHING_LE))) {
e2f34481
NJ
7548 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7549 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7550 lease_change_type = OPLOCK_WRITE_TO_NONE;
7551 else
7552 lease_change_type = OPLOCK_READ_TO_NONE;
7553 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
7554 le32_to_cpu(lease->state),
7555 le32_to_cpu(req->LeaseState));
64b39f4a
NJ
7556 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
7557 req->LeaseState != SMB2_LEASE_NONE_LE) {
e2f34481
NJ
7558 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7559 lease_change_type = OPLOCK_READ_TO_NONE;
7560 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
7561 le32_to_cpu(lease->state),
7562 le32_to_cpu(req->LeaseState));
7563 } else {
7564 /* valid lease state changes */
7565 err = STATUS_INVALID_DEVICE_STATE;
7566 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
7567 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7568 lease_change_type = OPLOCK_WRITE_TO_NONE;
7569 else
7570 lease_change_type = OPLOCK_READ_TO_NONE;
7571 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
7572 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7573 lease_change_type = OPLOCK_WRITE_TO_READ;
7574 else
7575 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
64b39f4a 7576 } else {
e2f34481 7577 lease_change_type = 0;
64b39f4a 7578 }
e2f34481
NJ
7579 }
7580
7581 switch (lease_change_type) {
7582 case OPLOCK_WRITE_TO_READ:
7583 ret = opinfo_write_to_read(opinfo);
7584 break;
7585 case OPLOCK_READ_HANDLE_TO_READ:
7586 ret = opinfo_read_handle_to_read(opinfo);
7587 break;
7588 case OPLOCK_WRITE_TO_NONE:
7589 ret = opinfo_write_to_none(opinfo);
7590 break;
7591 case OPLOCK_READ_TO_NONE:
7592 ret = opinfo_read_to_none(opinfo);
7593 break;
7594 default:
7595 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
7596 le32_to_cpu(lease->state),
7597 le32_to_cpu(req->LeaseState));
7598 }
7599
7600 lease_state = lease->state;
7601 opinfo->op_state = OPLOCK_STATE_NONE;
7602 wake_up_interruptible_all(&opinfo->oplock_q);
7603 atomic_dec(&opinfo->breaking_cnt);
7604 wake_up_interruptible_all(&opinfo->oplock_brk);
7605 opinfo_put(opinfo);
7606
7607 if (ret < 0) {
7608 rsp->hdr.Status = err;
7609 goto err_out;
7610 }
7611
7612 rsp->StructureSize = cpu_to_le16(36);
7613 rsp->Reserved = 0;
7614 rsp->Flags = 0;
7615 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
7616 rsp->LeaseState = lease_state;
7617 rsp->LeaseDuration = 0;
7618 inc_rfc1001_len(rsp, 36);
7619 return;
7620
7621err_out:
7622 opinfo->op_state = OPLOCK_STATE_NONE;
7623 wake_up_interruptible_all(&opinfo->oplock_q);
7624 atomic_dec(&opinfo->breaking_cnt);
7625 wake_up_interruptible_all(&opinfo->oplock_brk);
7626
7627 opinfo_put(opinfo);
7628 smb2_set_err_rsp(work);
7629}
7630
7631/**
7632 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
7633 * @work: smb work containing oplock/lease break command buffer
7634 *
7635 * Return: 0
7636 */
7637int smb2_oplock_break(struct ksmbd_work *work)
7638{
e5066499
NJ
7639 struct smb2_oplock_break *req = work->request_buf;
7640 struct smb2_oplock_break *rsp = work->response_buf;
e2f34481
NJ
7641
7642 switch (le16_to_cpu(req->StructureSize)) {
7643 case OP_BREAK_STRUCT_SIZE_20:
7644 smb20_oplock_break_ack(work);
7645 break;
7646 case OP_BREAK_STRUCT_SIZE_21:
7647 smb21_lease_break_ack(work);
7648 break;
7649 default:
7650 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
7651 le16_to_cpu(req->StructureSize));
7652 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7653 smb2_set_err_rsp(work);
7654 }
7655
7656 return 0;
7657}
7658
7659/**
7660 * smb2_notify() - handler for smb2 notify request
95fa1ce9 7661 * @work: smb work containing notify command buffer
e2f34481
NJ
7662 *
7663 * Return: 0
7664 */
7665int smb2_notify(struct ksmbd_work *work)
7666{
7667 struct smb2_notify_req *req;
7668 struct smb2_notify_rsp *rsp;
7669
7670 WORK_BUFFERS(work, req, rsp);
7671
7672 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
7673 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
7674 smb2_set_err_rsp(work);
7675 return 0;
7676 }
7677
7678 smb2_set_err_rsp(work);
7679 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
7680 return 0;
7681}
7682
7683/**
7684 * smb2_is_sign_req() - handler for checking packet signing status
95fa1ce9
HL
7685 * @work: smb work containing notify command buffer
7686 * @command: SMB2 command id
e2f34481
NJ
7687 *
7688 * Return: true if packed is signed, false otherwise
7689 */
7690bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
7691{
e5066499 7692 struct smb2_hdr *rcv_hdr2 = work->request_buf;
e2f34481
NJ
7693
7694 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
64b39f4a
NJ
7695 command != SMB2_NEGOTIATE_HE &&
7696 command != SMB2_SESSION_SETUP_HE &&
7697 command != SMB2_OPLOCK_BREAK_HE)
e2f34481
NJ
7698 return true;
7699
7700 return 0;
7701}
7702
7703/**
7704 * smb2_check_sign_req() - handler for req packet sign processing
7705 * @work: smb work containing notify command buffer
7706 *
7707 * Return: 1 on success, 0 otherwise
7708 */
7709int smb2_check_sign_req(struct ksmbd_work *work)
7710{
7711 struct smb2_hdr *hdr, *hdr_org;
7712 char signature_req[SMB2_SIGNATURE_SIZE];
7713 char signature[SMB2_HMACSHA256_SIZE];
7714 struct kvec iov[1];
7715 size_t len;
7716
e5066499 7717 hdr_org = hdr = work->request_buf;
e2f34481
NJ
7718 if (work->next_smb2_rcv_hdr_off)
7719 hdr = REQUEST_BUF_NEXT(work);
7720
7721 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
7722 len = be32_to_cpu(hdr_org->smb2_buf_length);
7723 else if (hdr->NextCommand)
7724 len = le32_to_cpu(hdr->NextCommand);
7725 else
7726 len = be32_to_cpu(hdr_org->smb2_buf_length) -
7727 work->next_smb2_rcv_hdr_off;
7728
7729 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
7730 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7731
7732 iov[0].iov_base = (char *)&hdr->ProtocolId;
7733 iov[0].iov_len = len;
7734
7735 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
64b39f4a 7736 signature))
e2f34481
NJ
7737 return 0;
7738
7739 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
7740 ksmbd_err("bad smb2 signature\n");
7741 return 0;
7742 }
7743
7744 return 1;
7745}
7746
7747/**
7748 * smb2_set_sign_rsp() - handler for rsp packet sign processing
7749 * @work: smb work containing notify command buffer
7750 *
7751 */
7752void smb2_set_sign_rsp(struct ksmbd_work *work)
7753{
7754 struct smb2_hdr *hdr, *hdr_org;
7755 struct smb2_hdr *req_hdr;
7756 char signature[SMB2_HMACSHA256_SIZE];
7757 struct kvec iov[2];
7758 size_t len;
7759 int n_vec = 1;
7760
e5066499 7761 hdr_org = hdr = work->response_buf;
e2f34481
NJ
7762 if (work->next_smb2_rsp_hdr_off)
7763 hdr = RESPONSE_BUF_NEXT(work);
7764
7765 req_hdr = REQUEST_BUF_NEXT(work);
7766
7767 if (!work->next_smb2_rsp_hdr_off) {
7768 len = get_rfc1002_len(hdr_org);
7769 if (req_hdr->NextCommand)
7770 len = ALIGN(len, 8);
7771 } else {
7772 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
7773 len = ALIGN(len, 8);
7774 }
7775
7776 if (req_hdr->NextCommand)
7777 hdr->NextCommand = cpu_to_le32(len);
7778
7779 hdr->Flags |= SMB2_FLAGS_SIGNED;
7780 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7781
7782 iov[0].iov_base = (char *)&hdr->ProtocolId;
7783 iov[0].iov_len = len;
7784
e5066499
NJ
7785 if (work->aux_payload_sz) {
7786 iov[0].iov_len -= work->aux_payload_sz;
e2f34481 7787
e5066499
NJ
7788 iov[1].iov_base = work->aux_payload_buf;
7789 iov[1].iov_len = work->aux_payload_sz;
e2f34481
NJ
7790 n_vec++;
7791 }
7792
7793 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
64b39f4a 7794 signature))
e2f34481
NJ
7795 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
7796}
7797
7798/**
7799 * smb3_check_sign_req() - handler for req packet sign processing
7800 * @work: smb work containing notify command buffer
7801 *
7802 * Return: 1 on success, 0 otherwise
7803 */
7804int smb3_check_sign_req(struct ksmbd_work *work)
7805{
7806 struct ksmbd_conn *conn;
7807 char *signing_key;
7808 struct smb2_hdr *hdr, *hdr_org;
7809 struct channel *chann;
7810 char signature_req[SMB2_SIGNATURE_SIZE];
7811 char signature[SMB2_CMACAES_SIZE];
7812 struct kvec iov[1];
7813 size_t len;
7814
e5066499 7815 hdr_org = hdr = work->request_buf;
e2f34481
NJ
7816 if (work->next_smb2_rcv_hdr_off)
7817 hdr = REQUEST_BUF_NEXT(work);
7818
7819 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
7820 len = be32_to_cpu(hdr_org->smb2_buf_length);
7821 else if (hdr->NextCommand)
7822 len = le32_to_cpu(hdr->NextCommand);
7823 else
7824 len = be32_to_cpu(hdr_org->smb2_buf_length) -
7825 work->next_smb2_rcv_hdr_off;
7826
7827 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
7828 signing_key = work->sess->smb3signingkey;
7829 conn = work->sess->conn;
7830 } else {
7831 chann = lookup_chann_list(work->sess);
7832 if (!chann)
7833 return 0;
7834 signing_key = chann->smb3signingkey;
7835 conn = chann->conn;
7836 }
7837
7838 if (!signing_key) {
7839 ksmbd_err("SMB3 signing key is not generated\n");
7840 return 0;
7841 }
7842
7843 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
7844 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7845 iov[0].iov_base = (char *)&hdr->ProtocolId;
7846 iov[0].iov_len = len;
7847
7848 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
7849 return 0;
7850
7851 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
7852 ksmbd_err("bad smb2 signature\n");
7853 return 0;
7854 }
7855
7856 return 1;
7857}
7858
7859/**
7860 * smb3_set_sign_rsp() - handler for rsp packet sign processing
7861 * @work: smb work containing notify command buffer
7862 *
7863 */
7864void smb3_set_sign_rsp(struct ksmbd_work *work)
7865{
7866 struct ksmbd_conn *conn;
7867 struct smb2_hdr *req_hdr;
7868 struct smb2_hdr *hdr, *hdr_org;
7869 struct channel *chann;
7870 char signature[SMB2_CMACAES_SIZE];
7871 struct kvec iov[2];
7872 int n_vec = 1;
7873 size_t len;
7874 char *signing_key;
7875
e5066499 7876 hdr_org = hdr = work->response_buf;
e2f34481
NJ
7877 if (work->next_smb2_rsp_hdr_off)
7878 hdr = RESPONSE_BUF_NEXT(work);
7879
7880 req_hdr = REQUEST_BUF_NEXT(work);
7881
7882 if (!work->next_smb2_rsp_hdr_off) {
7883 len = get_rfc1002_len(hdr_org);
7884 if (req_hdr->NextCommand)
7885 len = ALIGN(len, 8);
7886 } else {
7887 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
7888 len = ALIGN(len, 8);
7889 }
7890
7891 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
7892 signing_key = work->sess->smb3signingkey;
7893 conn = work->sess->conn;
7894 } else {
7895 chann = lookup_chann_list(work->sess);
7896 if (!chann)
7897 return;
7898 signing_key = chann->smb3signingkey;
7899 conn = chann->conn;
7900 }
7901
7902 if (!signing_key)
7903 return;
7904
7905 if (req_hdr->NextCommand)
7906 hdr->NextCommand = cpu_to_le32(len);
7907
7908 hdr->Flags |= SMB2_FLAGS_SIGNED;
7909 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7910 iov[0].iov_base = (char *)&hdr->ProtocolId;
7911 iov[0].iov_len = len;
e5066499
NJ
7912 if (work->aux_payload_sz) {
7913 iov[0].iov_len -= work->aux_payload_sz;
7914 iov[1].iov_base = work->aux_payload_buf;
7915 iov[1].iov_len = work->aux_payload_sz;
e2f34481
NJ
7916 n_vec++;
7917 }
7918
7919 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
7920 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
7921}
7922
7923/**
7924 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
7925 * @work: smb work containing response buffer
7926 *
7927 */
7928void smb3_preauth_hash_rsp(struct ksmbd_work *work)
7929{
7930 struct ksmbd_conn *conn = work->conn;
7931 struct ksmbd_session *sess = work->sess;
7932 struct smb2_hdr *req, *rsp;
7933
7934 if (conn->dialect != SMB311_PROT_ID)
7935 return;
7936
7937 WORK_BUFFERS(work, req, rsp);
7938
7939 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE)
7940 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
7941 conn->preauth_info->Preauth_HashValue);
7942
7943 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
64b39f4a 7944 sess && sess->state == SMB2_SESSION_IN_PROGRESS) {
e2f34481
NJ
7945 __u8 *hash_value;
7946
7947 hash_value = sess->Preauth_HashValue;
7948 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
7949 hash_value);
7950 }
7951}
7952
64b39f4a
NJ
7953static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf,
7954 __le16 cipher_type)
e2f34481
NJ
7955{
7956 struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf;
7957 unsigned int orig_len = get_rfc1002_len(old_buf);
7958
7959 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
7960 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
7961 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
7962 tr_hdr->Flags = cpu_to_le16(0x01);
7963 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
7964 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
7965 else
7966 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
7967 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
7968 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
7969 inc_rfc1001_len(tr_hdr, orig_len);
7970}
7971
7972int smb3_encrypt_resp(struct ksmbd_work *work)
7973{
e5066499 7974 char *buf = work->response_buf;
e2f34481
NJ
7975 struct smb2_transform_hdr *tr_hdr;
7976 struct kvec iov[3];
7977 int rc = -ENOMEM;
e5066499 7978 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
e2f34481
NJ
7979
7980 if (ARRAY_SIZE(iov) < rq_nvec)
7981 return -ENOMEM;
7982
20ea7fd2 7983 tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
e2f34481
NJ
7984 if (!tr_hdr)
7985 return rc;
7986
7987 /* fill transform header */
7988 fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type);
7989
7990 iov[0].iov_base = tr_hdr;
7991 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
7992 buf_size += iov[0].iov_len - 4;
7993
7994 iov[1].iov_base = buf + 4;
7995 iov[1].iov_len = get_rfc1002_len(buf);
e5066499
NJ
7996 if (work->aux_payload_sz) {
7997 iov[1].iov_len = work->resp_hdr_sz - 4;
e2f34481 7998
e5066499
NJ
7999 iov[2].iov_base = work->aux_payload_buf;
8000 iov[2].iov_len = work->aux_payload_sz;
e2f34481
NJ
8001 buf_size += iov[2].iov_len;
8002 }
8003 buf_size += iov[1].iov_len;
8004 work->resp_hdr_sz = iov[1].iov_len;
8005
8006 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8007 if (rc)
8008 return rc;
8009
8010 memmove(buf, iov[1].iov_base, iov[1].iov_len);
8011 tr_hdr->smb2_buf_length = cpu_to_be32(buf_size);
8012 work->tr_buf = tr_hdr;
8013
8014 return rc;
8015}
8016
8017int smb3_is_transform_hdr(void *buf)
8018{
8019 struct smb2_transform_hdr *trhdr = buf;
8020
8021 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8022}
8023
8024int smb3_decrypt_req(struct ksmbd_work *work)
8025{
8026 struct ksmbd_conn *conn = work->conn;
8027 struct ksmbd_session *sess;
e5066499 8028 char *buf = work->request_buf;
e2f34481
NJ
8029 struct smb2_hdr *hdr;
8030 unsigned int pdu_length = get_rfc1002_len(buf);
8031 struct kvec iov[2];
8032 unsigned int buf_data_size = pdu_length + 4 -
8033 sizeof(struct smb2_transform_hdr);
8034 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
8035 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
8036 int rc = 0;
8037
8038 sess = ksmbd_session_lookup(conn, le64_to_cpu(tr_hdr->SessionId));
8039 if (!sess) {
8040 ksmbd_err("invalid session id(%llx) in transform header\n",
64b39f4a 8041 le64_to_cpu(tr_hdr->SessionId));
e2f34481
NJ
8042 return -ECONNABORTED;
8043 }
8044
8045 if (pdu_length + 4 < sizeof(struct smb2_transform_hdr) +
8046 sizeof(struct smb2_hdr)) {
8047 ksmbd_err("Transform message is too small (%u)\n",
8048 pdu_length);
8049 return -ECONNABORTED;
8050 }
8051
8052 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
8053 ksmbd_err("Transform message is broken\n");
8054 return -ECONNABORTED;
8055 }
8056
8057 iov[0].iov_base = buf;
8058 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8059 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
8060 iov[1].iov_len = buf_data_size;
8061 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8062 if (rc)
8063 return rc;
8064
8065 memmove(buf + 4, iov[1].iov_base, buf_data_size);
8066 hdr = (struct smb2_hdr *)buf;
8067 hdr->smb2_buf_length = cpu_to_be32(buf_data_size);
8068
8069 return rc;
8070}
8071
8072bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8073{
8074 struct ksmbd_conn *conn = work->conn;
e5066499 8075 struct smb2_hdr *rsp = work->response_buf;
e2f34481
NJ
8076
8077 if (conn->dialect < SMB30_PROT_ID)
8078 return false;
8079
8080 if (work->next_smb2_rcv_hdr_off)
8081 rsp = RESPONSE_BUF_NEXT(work);
8082
8083 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
64b39f4a 8084 rsp->Status == STATUS_SUCCESS)
e2f34481
NJ
8085 return true;
8086 return false;
8087}