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