1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
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 #include <linux/falloc.h>
23 #include "connection.h"
24 #include "transport_ipc.h"
25 #include "transport_rdma.h"
27 #include "vfs_cache.h"
31 #include "smb_common.h"
32 #include "smbstatus.h"
33 #include "ksmbd_work.h"
34 #include "mgmt/user_config.h"
35 #include "mgmt/share_config.h"
36 #include "mgmt/tree_connect.h"
37 #include "mgmt/user_session.h"
38 #include "mgmt/ksmbd_ida.h"
41 static void __wbuf(struct ksmbd_work *work, void **req, void **rsp)
43 if (work->next_smb2_rcv_hdr_off) {
44 *req = ksmbd_req_buf_next(work);
45 *rsp = ksmbd_resp_buf_next(work);
47 *req = work->request_buf;
48 *rsp = work->response_buf;
52 #define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs))
55 * check_session_id() - check for valid session id in smb header
56 * @conn: connection instance
57 * @id: session id from smb header
59 * Return: 1 if valid session id, otherwise 0
61 static inline bool check_session_id(struct ksmbd_conn *conn, u64 id)
63 struct ksmbd_session *sess;
65 if (id == 0 || id == -1)
68 sess = ksmbd_session_lookup_all(conn, id);
71 pr_err("Invalid user session id: %llu\n", id);
75 struct channel *lookup_chann_list(struct ksmbd_session *sess, struct ksmbd_conn *conn)
77 struct channel *chann;
79 list_for_each_entry(chann, &sess->ksmbd_chann_list, chann_list) {
80 if (chann->conn == conn)
88 * smb2_get_ksmbd_tcon() - get tree connection information using a tree id.
91 * Return: 0 if there is a tree connection matched or these are
92 * skipable commands, otherwise error
94 int smb2_get_ksmbd_tcon(struct ksmbd_work *work)
96 struct smb2_hdr *req_hdr = work->request_buf;
100 if (work->conn->ops->get_cmd_val(work) == SMB2_TREE_CONNECT_HE ||
101 work->conn->ops->get_cmd_val(work) == SMB2_CANCEL_HE ||
102 work->conn->ops->get_cmd_val(work) == SMB2_LOGOFF_HE) {
103 ksmbd_debug(SMB, "skip to check tree connect request\n");
107 if (xa_empty(&work->sess->tree_conns)) {
108 ksmbd_debug(SMB, "NO tree connected\n");
112 tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId);
113 work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id);
115 pr_err("Invalid tid %d\n", tree_id);
123 * smb2_set_err_rsp() - set error response code on smb response
124 * @work: smb work containing response buffer
126 void smb2_set_err_rsp(struct ksmbd_work *work)
128 struct smb2_err_rsp *err_rsp;
130 if (work->next_smb2_rcv_hdr_off)
131 err_rsp = ksmbd_resp_buf_next(work);
133 err_rsp = work->response_buf;
135 if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) {
136 err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE;
137 err_rsp->ErrorContextCount = 0;
138 err_rsp->Reserved = 0;
139 err_rsp->ByteCount = 0;
140 err_rsp->ErrorData[0] = 0;
141 inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2);
146 * is_smb2_neg_cmd() - is it smb2 negotiation command
147 * @work: smb work containing smb header
149 * Return: true if smb2 negotiation command, otherwise false
151 bool is_smb2_neg_cmd(struct ksmbd_work *work)
153 struct smb2_hdr *hdr = work->request_buf;
155 /* is it SMB2 header ? */
156 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
159 /* make sure it is request not response message */
160 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
163 if (hdr->Command != SMB2_NEGOTIATE)
170 * is_smb2_rsp() - is it smb2 response
171 * @work: smb work containing smb response buffer
173 * Return: true if smb2 response, otherwise false
175 bool is_smb2_rsp(struct ksmbd_work *work)
177 struct smb2_hdr *hdr = work->response_buf;
179 /* is it SMB2 header ? */
180 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
183 /* make sure it is response not request message */
184 if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR))
191 * get_smb2_cmd_val() - get smb command code from smb header
192 * @work: smb work containing smb request buffer
194 * Return: smb2 request command value
196 u16 get_smb2_cmd_val(struct ksmbd_work *work)
198 struct smb2_hdr *rcv_hdr;
200 if (work->next_smb2_rcv_hdr_off)
201 rcv_hdr = ksmbd_req_buf_next(work);
203 rcv_hdr = work->request_buf;
204 return le16_to_cpu(rcv_hdr->Command);
208 * set_smb2_rsp_status() - set error response code on smb2 header
209 * @work: smb work containing response buffer
210 * @err: error response code
212 void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err)
214 struct smb2_hdr *rsp_hdr;
216 if (work->next_smb2_rcv_hdr_off)
217 rsp_hdr = ksmbd_resp_buf_next(work);
219 rsp_hdr = work->response_buf;
220 rsp_hdr->Status = err;
221 smb2_set_err_rsp(work);
225 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command
226 * @work: smb work containing smb request buffer
228 * smb2 negotiate response is sent in reply of smb1 negotiate command for
229 * dialect auto-negotiation.
231 int init_smb2_neg_rsp(struct ksmbd_work *work)
233 struct smb2_hdr *rsp_hdr;
234 struct smb2_negotiate_rsp *rsp;
235 struct ksmbd_conn *conn = work->conn;
237 if (conn->need_neg == false)
239 if (!(conn->dialect >= SMB20_PROT_ID &&
240 conn->dialect <= SMB311_PROT_ID))
243 rsp_hdr = work->response_buf;
245 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
247 rsp_hdr->smb2_buf_length =
248 cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals));
250 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
251 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
252 rsp_hdr->CreditRequest = cpu_to_le16(2);
253 rsp_hdr->Command = SMB2_NEGOTIATE;
254 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
255 rsp_hdr->NextCommand = 0;
256 rsp_hdr->MessageId = 0;
257 rsp_hdr->Id.SyncId.ProcessId = 0;
258 rsp_hdr->Id.SyncId.TreeId = 0;
259 rsp_hdr->SessionId = 0;
260 memset(rsp_hdr->Signature, 0, 16);
262 rsp = work->response_buf;
264 WARN_ON(ksmbd_conn_good(work));
266 rsp->StructureSize = cpu_to_le16(65);
267 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
268 rsp->DialectRevision = cpu_to_le16(conn->dialect);
269 /* Not setting conn guid rsp->ServerGUID, as it
270 * not used by client for identifying connection
272 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
273 /* Default Max Message Size till SMB2.0, 64K*/
274 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
275 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
276 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
278 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
279 rsp->ServerStartTime = 0;
281 rsp->SecurityBufferOffset = cpu_to_le16(128);
282 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
283 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
284 sizeof(rsp->hdr.smb2_buf_length)) +
285 le16_to_cpu(rsp->SecurityBufferOffset));
286 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
287 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
289 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
290 if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY)
291 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
292 conn->use_spnego = true;
294 ksmbd_conn_set_need_negotiate(work);
298 static int smb2_consume_credit_charge(struct ksmbd_work *work,
299 unsigned short credit_charge)
301 struct ksmbd_conn *conn = work->conn;
302 unsigned int rsp_credits = 1;
304 if (!conn->total_credits)
307 if (credit_charge > 0)
308 rsp_credits = credit_charge;
310 conn->total_credits -= rsp_credits;
315 * smb2_set_rsp_credits() - set number of credits in response buffer
316 * @work: smb work containing smb response buffer
318 int smb2_set_rsp_credits(struct ksmbd_work *work)
320 struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work);
321 struct smb2_hdr *hdr = ksmbd_resp_buf_next(work);
322 struct ksmbd_conn *conn = work->conn;
323 unsigned short credits_requested = le16_to_cpu(req_hdr->CreditRequest);
324 unsigned short credit_charge = 1, credits_granted = 0;
325 unsigned short aux_max, aux_credits, min_credits;
326 int rsp_credit_charge;
328 if (hdr->Command == SMB2_CANCEL)
331 /* get default minimum credits by shifting maximum credits by 4 */
332 min_credits = conn->max_credits >> 4;
334 if (conn->total_credits >= conn->max_credits) {
335 pr_err("Total credits overflow: %d\n", conn->total_credits);
336 conn->total_credits = min_credits;
340 smb2_consume_credit_charge(work, le16_to_cpu(req_hdr->CreditCharge));
341 if (rsp_credit_charge < 0)
344 hdr->CreditCharge = cpu_to_le16(rsp_credit_charge);
346 if (credits_requested > 0) {
347 aux_credits = credits_requested - 1;
349 if (hdr->Command == SMB2_NEGOTIATE)
351 aux_credits = (aux_credits < aux_max) ? aux_credits : aux_max;
352 credits_granted = aux_credits + credit_charge;
354 /* if credits granted per client is getting bigger than default
355 * minimum credits then we should wrap it up within the limits.
357 if ((conn->total_credits + credits_granted) > min_credits)
358 credits_granted = min_credits - conn->total_credits;
360 * TODO: Need to adjuct CreditRequest value according to
363 } else if (conn->total_credits == 0) {
367 conn->total_credits += credits_granted;
368 work->credits_granted += credits_granted;
370 if (!req_hdr->NextCommand) {
371 /* Update CreditRequest in last request */
372 hdr->CreditRequest = cpu_to_le16(work->credits_granted);
376 "credits: requested[%d] granted[%d] total_granted[%d]\n",
377 credits_requested, credits_granted,
378 conn->total_credits);
383 * init_chained_smb2_rsp() - initialize smb2 chained response
384 * @work: smb work containing smb response buffer
386 static void init_chained_smb2_rsp(struct ksmbd_work *work)
388 struct smb2_hdr *req = ksmbd_req_buf_next(work);
389 struct smb2_hdr *rsp = ksmbd_resp_buf_next(work);
390 struct smb2_hdr *rsp_hdr;
391 struct smb2_hdr *rcv_hdr;
392 int next_hdr_offset = 0;
395 /* Len of this response = updated RFC len - offset of previous cmd
396 * in the compound rsp
399 /* Storing the current local FID which may be needed by subsequent
400 * command in the compound request
402 if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) {
404 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
406 work->compound_pfid =
407 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
409 work->compound_sid = le64_to_cpu(rsp->SessionId);
412 len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off;
413 next_hdr_offset = le32_to_cpu(req->NextCommand);
415 new_len = ALIGN(len, 8);
416 inc_rfc1001_len(work->response_buf, ((sizeof(struct smb2_hdr) - 4)
418 rsp->NextCommand = cpu_to_le32(new_len);
420 work->next_smb2_rcv_hdr_off += next_hdr_offset;
421 work->next_smb2_rsp_hdr_off += new_len;
423 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
424 new_len, work->next_smb2_rcv_hdr_off,
425 work->next_smb2_rsp_hdr_off);
427 rsp_hdr = ksmbd_resp_buf_next(work);
428 rcv_hdr = ksmbd_req_buf_next(work);
430 if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
431 ksmbd_debug(SMB, "related flag should be set\n");
432 work->compound_fid = KSMBD_NO_FID;
433 work->compound_pfid = KSMBD_NO_FID;
435 memset((char *)rsp_hdr + 4, 0, sizeof(struct smb2_hdr) + 2);
436 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
437 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
438 rsp_hdr->Command = rcv_hdr->Command;
441 * Message is response. We don't grant oplock yet.
443 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR |
444 SMB2_FLAGS_RELATED_OPERATIONS);
445 rsp_hdr->NextCommand = 0;
446 rsp_hdr->MessageId = rcv_hdr->MessageId;
447 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
448 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
449 rsp_hdr->SessionId = rcv_hdr->SessionId;
450 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
454 * is_chained_smb2_message() - check for chained command
455 * @work: smb work containing smb request buffer
457 * Return: true if chained request, otherwise false
459 bool is_chained_smb2_message(struct ksmbd_work *work)
461 struct smb2_hdr *hdr = work->request_buf;
462 unsigned int len, next_cmd;
464 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
467 hdr = ksmbd_req_buf_next(work);
468 next_cmd = le32_to_cpu(hdr->NextCommand);
470 if ((u64)work->next_smb2_rcv_hdr_off + next_cmd +
471 __SMB2_HEADER_STRUCTURE_SIZE >
472 get_rfc1002_len(work->request_buf)) {
473 pr_err("next command(%u) offset exceeds smb msg size\n",
478 ksmbd_debug(SMB, "got SMB2 chained command\n");
479 init_chained_smb2_rsp(work);
481 } else if (work->next_smb2_rcv_hdr_off) {
483 * This is last request in chained command,
484 * align response to 8 byte
486 len = ALIGN(get_rfc1002_len(work->response_buf), 8);
487 len = len - get_rfc1002_len(work->response_buf);
489 ksmbd_debug(SMB, "padding len %u\n", len);
490 inc_rfc1001_len(work->response_buf, len);
491 if (work->aux_payload_sz)
492 work->aux_payload_sz += len;
499 * init_smb2_rsp_hdr() - initialize smb2 response
500 * @work: smb work containing smb request buffer
504 int init_smb2_rsp_hdr(struct ksmbd_work *work)
506 struct smb2_hdr *rsp_hdr = work->response_buf;
507 struct smb2_hdr *rcv_hdr = work->request_buf;
508 struct ksmbd_conn *conn = work->conn;
510 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
511 rsp_hdr->smb2_buf_length =
512 cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals));
513 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
514 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
515 rsp_hdr->Command = rcv_hdr->Command;
518 * Message is response. We don't grant oplock yet.
520 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
521 rsp_hdr->NextCommand = 0;
522 rsp_hdr->MessageId = rcv_hdr->MessageId;
523 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
524 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
525 rsp_hdr->SessionId = rcv_hdr->SessionId;
526 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
528 work->syncronous = true;
529 if (work->async_id) {
530 ksmbd_release_id(&conn->async_ida, work->async_id);
538 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
539 * @work: smb work containing smb request buffer
541 * Return: 0 on success, otherwise -ENOMEM
543 int smb2_allocate_rsp_buf(struct ksmbd_work *work)
545 struct smb2_hdr *hdr = work->request_buf;
546 size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
547 size_t large_sz = work->conn->vals->max_trans_size + MAX_SMB2_HDR_SIZE;
548 size_t sz = small_sz;
549 int cmd = le16_to_cpu(hdr->Command);
551 if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE)
554 if (cmd == SMB2_QUERY_INFO_HE) {
555 struct smb2_query_info_req *req;
557 req = work->request_buf;
558 if (req->InfoType == SMB2_O_INFO_FILE &&
559 (req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
560 req->FileInfoClass == FILE_ALL_INFORMATION))
564 /* allocate large response buf for chained commands */
565 if (le32_to_cpu(hdr->NextCommand) > 0)
568 work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
569 if (!work->response_buf)
572 work->response_sz = sz;
577 * smb2_check_user_session() - check for valid session for a user
578 * @work: smb work containing smb request buffer
580 * Return: 0 on success, otherwise error
582 int smb2_check_user_session(struct ksmbd_work *work)
584 struct smb2_hdr *req_hdr = work->request_buf;
585 struct ksmbd_conn *conn = work->conn;
586 unsigned int cmd = conn->ops->get_cmd_val(work);
587 unsigned long long sess_id;
591 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
592 * require a session id, so no need to validate user session's for
595 if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE ||
596 cmd == SMB2_SESSION_SETUP_HE)
599 if (!ksmbd_conn_good(work))
602 sess_id = le64_to_cpu(req_hdr->SessionId);
603 /* Check for validity of user session */
604 work->sess = ksmbd_session_lookup_all(conn, sess_id);
607 ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id);
611 static void destroy_previous_session(struct ksmbd_user *user, u64 id)
613 struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id);
614 struct ksmbd_user *prev_user;
619 prev_user = prev_sess->user;
622 strcmp(user->name, prev_user->name) ||
623 user->passkey_sz != prev_user->passkey_sz ||
624 memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) {
625 put_session(prev_sess);
629 put_session(prev_sess);
630 ksmbd_session_destroy(prev_sess);
634 * smb2_get_name() - get filename string from on the wire smb format
635 * @share: ksmbd_share_config pointer
636 * @src: source buffer
637 * @maxlen: maxlen of source string
638 * @nls_table: nls_table pointer
640 * Return: matching converted filename on success, otherwise error ptr
643 smb2_get_name(struct ksmbd_share_config *share, const char *src,
644 const int maxlen, struct nls_table *local_nls)
648 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls);
650 pr_err("failed to get name %ld\n", PTR_ERR(name));
654 ksmbd_conv_path_to_unix(name);
655 ksmbd_strip_last_slash(name);
659 int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
661 struct smb2_hdr *rsp_hdr;
662 struct ksmbd_conn *conn = work->conn;
665 rsp_hdr = work->response_buf;
666 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
668 id = ksmbd_acquire_async_msg_id(&conn->async_ida);
670 pr_err("Failed to alloc async message id\n");
673 work->syncronous = false;
675 rsp_hdr->Id.AsyncId = cpu_to_le64(id);
678 "Send interim Response to inform async request id : %d\n",
681 work->cancel_fn = fn;
682 work->cancel_argv = arg;
684 if (list_empty(&work->async_request_entry)) {
685 spin_lock(&conn->request_lock);
686 list_add_tail(&work->async_request_entry, &conn->async_requests);
687 spin_unlock(&conn->request_lock);
693 void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
695 struct smb2_hdr *rsp_hdr;
697 rsp_hdr = work->response_buf;
698 smb2_set_err_rsp(work);
699 rsp_hdr->Status = status;
702 ksmbd_conn_write(work);
707 static __le32 smb2_get_reparse_tag_special_file(umode_t mode)
709 if (S_ISDIR(mode) || S_ISREG(mode))
713 return IO_REPARSE_TAG_LX_SYMLINK_LE;
714 else if (S_ISFIFO(mode))
715 return IO_REPARSE_TAG_LX_FIFO_LE;
716 else if (S_ISSOCK(mode))
717 return IO_REPARSE_TAG_AF_UNIX_LE;
718 else if (S_ISCHR(mode))
719 return IO_REPARSE_TAG_LX_CHR_LE;
720 else if (S_ISBLK(mode))
721 return IO_REPARSE_TAG_LX_BLK_LE;
727 * smb2_get_dos_mode() - get file mode in dos format from unix mode
728 * @stat: kstat containing file mode
729 * @attribute: attribute flags
731 * Return: converted dos mode
733 static int smb2_get_dos_mode(struct kstat *stat, int attribute)
737 if (S_ISDIR(stat->mode)) {
738 attr = ATTR_DIRECTORY |
739 (attribute & (ATTR_HIDDEN | ATTR_SYSTEM));
741 attr = (attribute & 0x00005137) | ATTR_ARCHIVE;
742 attr &= ~(ATTR_DIRECTORY);
743 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
744 FILE_SUPPORTS_SPARSE_FILES))
747 if (smb2_get_reparse_tag_special_file(stat->mode))
748 attr |= ATTR_REPARSE;
754 static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
757 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
758 pneg_ctxt->DataLength = cpu_to_le16(38);
759 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
760 pneg_ctxt->Reserved = cpu_to_le32(0);
761 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
762 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
763 pneg_ctxt->HashAlgorithms = hash_id;
766 static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
769 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
770 pneg_ctxt->DataLength = cpu_to_le16(4);
771 pneg_ctxt->Reserved = cpu_to_le32(0);
772 pneg_ctxt->CipherCount = cpu_to_le16(1);
773 pneg_ctxt->Ciphers[0] = cipher_type;
776 static void build_compression_ctxt(struct smb2_compression_ctx *pneg_ctxt,
779 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
780 pneg_ctxt->DataLength =
781 cpu_to_le16(sizeof(struct smb2_compression_ctx)
782 - sizeof(struct smb2_neg_context));
783 pneg_ctxt->Reserved = cpu_to_le32(0);
784 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1);
785 pneg_ctxt->Reserved1 = cpu_to_le32(0);
786 pneg_ctxt->CompressionAlgorithms[0] = comp_algo;
789 static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt,
792 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
793 pneg_ctxt->DataLength =
794 cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2)
795 - sizeof(struct smb2_neg_context));
796 pneg_ctxt->Reserved = cpu_to_le32(0);
797 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1);
798 pneg_ctxt->SigningAlgorithms[0] = sign_algo;
801 static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
803 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
804 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
805 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
806 pneg_ctxt->Name[0] = 0x93;
807 pneg_ctxt->Name[1] = 0xAD;
808 pneg_ctxt->Name[2] = 0x25;
809 pneg_ctxt->Name[3] = 0x50;
810 pneg_ctxt->Name[4] = 0x9C;
811 pneg_ctxt->Name[5] = 0xB4;
812 pneg_ctxt->Name[6] = 0x11;
813 pneg_ctxt->Name[7] = 0xE7;
814 pneg_ctxt->Name[8] = 0xB4;
815 pneg_ctxt->Name[9] = 0x23;
816 pneg_ctxt->Name[10] = 0x83;
817 pneg_ctxt->Name[11] = 0xDE;
818 pneg_ctxt->Name[12] = 0x96;
819 pneg_ctxt->Name[13] = 0x8B;
820 pneg_ctxt->Name[14] = 0xCD;
821 pneg_ctxt->Name[15] = 0x7C;
824 static void assemble_neg_contexts(struct ksmbd_conn *conn,
825 struct smb2_negotiate_rsp *rsp)
827 /* +4 is to account for the RFC1001 len field */
828 char *pneg_ctxt = (char *)rsp +
829 le32_to_cpu(rsp->NegotiateContextOffset) + 4;
830 int neg_ctxt_cnt = 1;
834 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
835 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
836 conn->preauth_info->Preauth_HashId);
837 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt);
838 inc_rfc1001_len(rsp, AUTH_GSS_PADDING);
839 ctxt_size = sizeof(struct smb2_preauth_neg_context);
840 /* Round to 8 byte boundary */
841 pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8);
843 if (conn->cipher_type) {
844 ctxt_size = round_up(ctxt_size, 8);
846 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
847 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt,
849 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
850 ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2;
851 /* Round to 8 byte boundary */
853 round_up(sizeof(struct smb2_encryption_neg_context) + 2,
857 if (conn->compress_algorithm) {
858 ctxt_size = round_up(ctxt_size, 8);
860 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
861 /* Temporarily set to SMB3_COMPRESS_NONE */
862 build_compression_ctxt((struct smb2_compression_ctx *)pneg_ctxt,
863 conn->compress_algorithm);
864 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
865 ctxt_size += sizeof(struct smb2_compression_ctx) + 2;
866 /* Round to 8 byte boundary */
867 pneg_ctxt += round_up(sizeof(struct smb2_compression_ctx) + 2,
871 if (conn->posix_ext_supported) {
872 ctxt_size = round_up(ctxt_size, 8);
874 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
875 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
876 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
877 ctxt_size += sizeof(struct smb2_posix_neg_context);
878 /* Round to 8 byte boundary */
879 pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8);
882 if (conn->signing_negotiated) {
883 ctxt_size = round_up(ctxt_size, 8);
885 "assemble SMB2_SIGNING_CAPABILITIES context\n");
886 build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt,
887 conn->signing_algorithm);
888 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
889 ctxt_size += sizeof(struct smb2_signing_capabilities) + 2;
892 inc_rfc1001_len(rsp, ctxt_size);
895 static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
896 struct smb2_preauth_neg_context *pneg_ctxt)
898 __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
900 if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) {
901 conn->preauth_info->Preauth_HashId =
902 SMB2_PREAUTH_INTEGRITY_SHA512;
903 err = STATUS_SUCCESS;
909 static void decode_encrypt_ctxt(struct ksmbd_conn *conn,
910 struct smb2_encryption_neg_context *pneg_ctxt,
913 int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount);
914 int i, cphs_size = cph_cnt * sizeof(__le16);
916 conn->cipher_type = 0;
918 if (sizeof(struct smb2_encryption_neg_context) + cphs_size >
920 pr_err("Invalid cipher count(%d)\n", cph_cnt);
924 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION))
927 for (i = 0; i < cph_cnt; i++) {
928 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM ||
929 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM ||
930 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM ||
931 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) {
932 ksmbd_debug(SMB, "Cipher ID = 0x%x\n",
933 pneg_ctxt->Ciphers[i]);
934 conn->cipher_type = pneg_ctxt->Ciphers[i];
940 static void decode_compress_ctxt(struct ksmbd_conn *conn,
941 struct smb2_compression_ctx *pneg_ctxt)
943 conn->compress_algorithm = SMB3_COMPRESS_NONE;
946 static void decode_sign_cap_ctxt(struct ksmbd_conn *conn,
947 struct smb2_signing_capabilities *pneg_ctxt,
950 int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount);
951 int i, sign_alos_size = sign_algo_cnt * sizeof(__le16);
953 conn->signing_negotiated = false;
955 if (sizeof(struct smb2_signing_capabilities) + sign_alos_size >
957 pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt);
961 for (i = 0; i < sign_algo_cnt; i++) {
962 if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256 ||
963 pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC) {
964 ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n",
965 pneg_ctxt->SigningAlgorithms[i]);
966 conn->signing_negotiated = true;
967 conn->signing_algorithm =
968 pneg_ctxt->SigningAlgorithms[i];
974 static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
975 struct smb2_negotiate_req *req)
977 /* +4 is to account for the RFC1001 len field */
978 struct smb2_neg_context *pctx = (struct smb2_neg_context *)((char *)req + 4);
979 int i = 0, len_of_ctxts;
980 int offset = le32_to_cpu(req->NegotiateContextOffset);
981 int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
982 int len_of_smb = be32_to_cpu(req->hdr.smb2_buf_length);
983 __le32 status = STATUS_INVALID_PARAMETER;
985 ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt);
986 if (len_of_smb <= offset) {
987 ksmbd_debug(SMB, "Invalid response: negotiate context offset\n");
991 len_of_ctxts = len_of_smb - offset;
993 while (i++ < neg_ctxt_cnt) {
996 /* check that offset is not beyond end of SMB */
997 if (len_of_ctxts == 0)
1000 if (len_of_ctxts < sizeof(struct smb2_neg_context))
1003 pctx = (struct smb2_neg_context *)((char *)pctx + offset);
1004 clen = le16_to_cpu(pctx->DataLength);
1005 if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts)
1008 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
1010 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
1011 if (conn->preauth_info->Preauth_HashId)
1014 status = decode_preauth_ctxt(conn,
1015 (struct smb2_preauth_neg_context *)pctx);
1016 if (status != STATUS_SUCCESS)
1018 } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
1020 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
1021 if (conn->cipher_type)
1024 decode_encrypt_ctxt(conn,
1025 (struct smb2_encryption_neg_context *)pctx,
1027 } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) {
1029 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
1030 if (conn->compress_algorithm)
1033 decode_compress_ctxt(conn,
1034 (struct smb2_compression_ctx *)pctx);
1035 } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) {
1037 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
1038 } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) {
1040 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
1041 conn->posix_ext_supported = true;
1042 } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) {
1044 "deassemble SMB2_SIGNING_CAPABILITIES context\n");
1045 decode_sign_cap_ctxt(conn,
1046 (struct smb2_signing_capabilities *)pctx,
1050 /* offsets must be 8 byte aligned */
1051 clen = (clen + 7) & ~0x7;
1052 offset = clen + sizeof(struct smb2_neg_context);
1053 len_of_ctxts -= clen + sizeof(struct smb2_neg_context);
1059 * smb2_handle_negotiate() - handler for smb2 negotiate command
1060 * @work: smb work containing smb request buffer
1064 int smb2_handle_negotiate(struct ksmbd_work *work)
1066 struct ksmbd_conn *conn = work->conn;
1067 struct smb2_negotiate_req *req = work->request_buf;
1068 struct smb2_negotiate_rsp *rsp = work->response_buf;
1070 unsigned int smb2_buf_len, smb2_neg_size;
1073 ksmbd_debug(SMB, "Received negotiate request\n");
1074 conn->need_neg = false;
1075 if (ksmbd_conn_good(work)) {
1076 pr_err("conn->tcp_status is already in CifsGood State\n");
1077 work->send_no_response = 1;
1081 if (req->DialectCount == 0) {
1082 pr_err("malformed packet\n");
1083 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1088 smb2_buf_len = get_rfc1002_len(work->request_buf);
1089 smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects) - 4;
1090 if (smb2_neg_size > smb2_buf_len) {
1091 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1096 if (conn->dialect == SMB311_PROT_ID) {
1097 unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset);
1099 if (smb2_buf_len < nego_ctxt_off) {
1100 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1105 if (smb2_neg_size > nego_ctxt_off) {
1106 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1111 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1113 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1118 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1120 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1126 conn->cli_cap = le32_to_cpu(req->Capabilities);
1127 switch (conn->dialect) {
1128 case SMB311_PROT_ID:
1129 conn->preauth_info =
1130 kzalloc(sizeof(struct preauth_integrity_info),
1132 if (!conn->preauth_info) {
1134 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1138 status = deassemble_neg_contexts(conn, req);
1139 if (status != STATUS_SUCCESS) {
1140 pr_err("deassemble_neg_contexts error(0x%x)\n",
1142 rsp->hdr.Status = status;
1147 rc = init_smb3_11_server(conn);
1149 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1153 ksmbd_gen_preauth_integrity_hash(conn,
1155 conn->preauth_info->Preauth_HashValue);
1156 rsp->NegotiateContextOffset =
1157 cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
1158 assemble_neg_contexts(conn, rsp);
1160 case SMB302_PROT_ID:
1161 init_smb3_02_server(conn);
1164 init_smb3_0_server(conn);
1167 init_smb2_1_server(conn);
1170 rc = init_smb2_0_server(conn);
1172 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1179 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n",
1181 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1185 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
1188 conn->connection_type = conn->dialect;
1190 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
1191 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
1192 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
1194 if (conn->dialect > SMB20_PROT_ID) {
1195 memcpy(conn->ClientGUID, req->ClientGUID,
1196 SMB2_CLIENT_GUID_SIZE);
1197 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
1200 rsp->StructureSize = cpu_to_le16(65);
1201 rsp->DialectRevision = cpu_to_le16(conn->dialect);
1202 /* Not setting conn guid rsp->ServerGUID, as it
1203 * not used by client for identifying server
1205 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
1207 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
1208 rsp->ServerStartTime = 0;
1209 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n",
1210 le32_to_cpu(rsp->NegotiateContextOffset),
1211 le16_to_cpu(rsp->NegotiateContextCount));
1213 rsp->SecurityBufferOffset = cpu_to_le16(128);
1214 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
1215 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
1216 sizeof(rsp->hdr.smb2_buf_length)) +
1217 le16_to_cpu(rsp->SecurityBufferOffset));
1218 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
1219 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
1221 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
1222 conn->use_spnego = true;
1224 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
1225 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) &&
1226 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE)
1228 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
1229 server_conf.enforced_signing = true;
1230 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
1234 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode);
1235 ksmbd_conn_set_need_negotiate(work);
1239 smb2_set_err_rsp(work);
1244 static int alloc_preauth_hash(struct ksmbd_session *sess,
1245 struct ksmbd_conn *conn)
1247 if (sess->Preauth_HashValue)
1250 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue,
1251 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL);
1252 if (!sess->Preauth_HashValue)
1258 static int generate_preauth_hash(struct ksmbd_work *work)
1260 struct ksmbd_conn *conn = work->conn;
1261 struct ksmbd_session *sess = work->sess;
1264 if (conn->dialect != SMB311_PROT_ID)
1267 if (conn->binding) {
1268 struct preauth_session *preauth_sess;
1270 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
1271 if (!preauth_sess) {
1272 preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id);
1277 preauth_hash = preauth_sess->Preauth_HashValue;
1279 if (!sess->Preauth_HashValue)
1280 if (alloc_preauth_hash(sess, conn))
1282 preauth_hash = sess->Preauth_HashValue;
1285 ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash);
1289 static int decode_negotiation_token(struct ksmbd_work *work,
1290 struct negotiate_message *negblob)
1292 struct ksmbd_conn *conn = work->conn;
1293 struct smb2_sess_setup_req *req;
1296 if (!conn->use_spnego)
1299 req = work->request_buf;
1300 sz = le16_to_cpu(req->SecurityBufferLength);
1302 if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) {
1303 if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) {
1304 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP;
1305 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP;
1306 conn->use_spnego = false;
1312 static int ntlm_negotiate(struct ksmbd_work *work,
1313 struct negotiate_message *negblob)
1315 struct smb2_sess_setup_req *req = work->request_buf;
1316 struct smb2_sess_setup_rsp *rsp = work->response_buf;
1317 struct challenge_message *chgblob;
1318 unsigned char *spnego_blob = NULL;
1319 u16 spnego_blob_len;
1323 ksmbd_debug(SMB, "negotiate phase\n");
1324 sz = le16_to_cpu(req->SecurityBufferLength);
1325 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, sz, work->sess);
1329 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1331 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);
1332 memset(chgblob, 0, sizeof(struct challenge_message));
1334 if (!work->conn->use_spnego) {
1335 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1339 rsp->SecurityBufferLength = cpu_to_le16(sz);
1343 sz = sizeof(struct challenge_message);
1344 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1346 neg_blob = kzalloc(sz, GFP_KERNEL);
1350 chgblob = (struct challenge_message *)neg_blob;
1351 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1357 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len,
1364 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1365 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1366 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1374 static struct authenticate_message *user_authblob(struct ksmbd_conn *conn,
1375 struct smb2_sess_setup_req *req)
1379 if (conn->use_spnego && conn->mechToken)
1380 return (struct authenticate_message *)conn->mechToken;
1382 sz = le16_to_cpu(req->SecurityBufferOffset);
1383 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId
1387 static struct ksmbd_user *session_user(struct ksmbd_conn *conn,
1388 struct smb2_sess_setup_req *req)
1390 struct authenticate_message *authblob;
1391 struct ksmbd_user *user;
1395 authblob = user_authblob(conn, req);
1396 sz = le32_to_cpu(authblob->UserName.BufferOffset);
1397 name = smb_strndup_from_utf16((const char *)authblob + sz,
1398 le16_to_cpu(authblob->UserName.Length),
1402 pr_err("cannot allocate memory\n");
1406 ksmbd_debug(SMB, "session setup request for user %s\n", name);
1407 user = ksmbd_login_user(name);
1412 static int ntlm_authenticate(struct ksmbd_work *work)
1414 struct smb2_sess_setup_req *req = work->request_buf;
1415 struct smb2_sess_setup_rsp *rsp = work->response_buf;
1416 struct ksmbd_conn *conn = work->conn;
1417 struct ksmbd_session *sess = work->sess;
1418 struct channel *chann = NULL;
1419 struct ksmbd_user *user;
1423 ksmbd_debug(SMB, "authenticate phase\n");
1424 if (conn->use_spnego) {
1425 unsigned char *spnego_blob;
1426 u16 spnego_blob_len;
1428 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob,
1434 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1435 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1436 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1438 inc_rfc1001_len(rsp, spnego_blob_len - 1);
1441 user = session_user(conn, req);
1443 ksmbd_debug(SMB, "Unknown user name or an error\n");
1447 /* Check for previous session */
1448 prev_id = le64_to_cpu(req->PreviousSessionId);
1449 if (prev_id && prev_id != sess->id)
1450 destroy_previous_session(user, prev_id);
1452 if (sess->state == SMB2_SESSION_VALID) {
1454 * Reuse session if anonymous try to connect
1455 * on reauthetication.
1457 if (ksmbd_anonymous_user(user)) {
1458 ksmbd_free_user(user);
1461 ksmbd_free_user(sess->user);
1465 if (user_guest(sess->user)) {
1467 ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n");
1471 rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE;
1473 struct authenticate_message *authblob;
1475 authblob = user_authblob(conn, req);
1476 sz = le16_to_cpu(req->SecurityBufferLength);
1477 rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess);
1479 set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD);
1480 ksmbd_debug(SMB, "authentication failed\n");
1485 * If session state is SMB2_SESSION_VALID, We can assume
1486 * that it is reauthentication. And the user/password
1487 * has been verified, so return it here.
1489 if (sess->state == SMB2_SESSION_VALID) {
1491 goto binding_session;
1495 if ((conn->sign || server_conf.enforced_signing) ||
1496 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
1499 if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION &&
1500 conn->ops->generate_encryptionkey &&
1501 !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
1502 rc = conn->ops->generate_encryptionkey(sess);
1505 "SMB3 encryption key generation failed\n");
1509 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1511 * signing is disable if encryption is enable
1519 if (conn->dialect >= SMB30_PROT_ID) {
1520 chann = lookup_chann_list(sess, conn);
1522 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1527 INIT_LIST_HEAD(&chann->chann_list);
1528 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1532 if (conn->ops->generate_signingkey) {
1533 rc = conn->ops->generate_signingkey(sess, conn);
1535 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
1540 if (conn->dialect > SMB20_PROT_ID) {
1541 if (!ksmbd_conn_lookup_dialect(conn)) {
1542 pr_err("fail to verify the dialect\n");
1549 #ifdef CONFIG_SMB_SERVER_KERBEROS5
1550 static int krb5_authenticate(struct ksmbd_work *work)
1552 struct smb2_sess_setup_req *req = work->request_buf;
1553 struct smb2_sess_setup_rsp *rsp = work->response_buf;
1554 struct ksmbd_conn *conn = work->conn;
1555 struct ksmbd_session *sess = work->sess;
1556 char *in_blob, *out_blob;
1557 struct channel *chann = NULL;
1559 int in_len, out_len;
1562 in_blob = (char *)&req->hdr.ProtocolId +
1563 le16_to_cpu(req->SecurityBufferOffset);
1564 in_len = le16_to_cpu(req->SecurityBufferLength);
1565 out_blob = (char *)&rsp->hdr.ProtocolId +
1566 le16_to_cpu(rsp->SecurityBufferOffset);
1567 out_len = work->response_sz -
1568 offsetof(struct smb2_hdr, smb2_buf_length) -
1569 le16_to_cpu(rsp->SecurityBufferOffset);
1571 /* Check previous session */
1572 prev_sess_id = le64_to_cpu(req->PreviousSessionId);
1573 if (prev_sess_id && prev_sess_id != sess->id)
1574 destroy_previous_session(sess->user, prev_sess_id);
1576 if (sess->state == SMB2_SESSION_VALID)
1577 ksmbd_free_user(sess->user);
1579 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len,
1580 out_blob, &out_len);
1582 ksmbd_debug(SMB, "krb5 authentication failed\n");
1585 rsp->SecurityBufferLength = cpu_to_le16(out_len);
1586 inc_rfc1001_len(rsp, out_len - 1);
1588 if ((conn->sign || server_conf.enforced_signing) ||
1589 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
1592 if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) &&
1593 conn->ops->generate_encryptionkey) {
1594 retval = conn->ops->generate_encryptionkey(sess);
1597 "SMB3 encryption key generation failed\n");
1601 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1605 if (conn->dialect >= SMB30_PROT_ID) {
1606 chann = lookup_chann_list(sess, conn);
1608 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1613 INIT_LIST_HEAD(&chann->chann_list);
1614 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1618 if (conn->ops->generate_signingkey) {
1619 retval = conn->ops->generate_signingkey(sess, conn);
1621 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
1626 if (conn->dialect > SMB20_PROT_ID) {
1627 if (!ksmbd_conn_lookup_dialect(conn)) {
1628 pr_err("fail to verify the dialect\n");
1635 static int krb5_authenticate(struct ksmbd_work *work)
1641 int smb2_sess_setup(struct ksmbd_work *work)
1643 struct ksmbd_conn *conn = work->conn;
1644 struct smb2_sess_setup_req *req = work->request_buf;
1645 struct smb2_sess_setup_rsp *rsp = work->response_buf;
1646 struct ksmbd_session *sess;
1647 struct negotiate_message *negblob;
1650 ksmbd_debug(SMB, "Received request for session setup\n");
1652 rsp->StructureSize = cpu_to_le16(9);
1653 rsp->SessionFlags = 0;
1654 rsp->SecurityBufferOffset = cpu_to_le16(72);
1655 rsp->SecurityBufferLength = 0;
1656 inc_rfc1001_len(rsp, 9);
1658 if (!req->hdr.SessionId) {
1659 sess = ksmbd_smb2_session_create();
1664 rsp->hdr.SessionId = cpu_to_le64(sess->id);
1665 ksmbd_session_register(conn, sess);
1666 } else if (conn->dialect >= SMB30_PROT_ID &&
1667 (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1668 req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) {
1669 u64 sess_id = le64_to_cpu(req->hdr.SessionId);
1671 sess = ksmbd_session_lookup_slowpath(sess_id);
1677 if (conn->dialect != sess->conn->dialect) {
1682 if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) {
1687 if (strncmp(conn->ClientGUID, sess->conn->ClientGUID,
1688 SMB2_CLIENT_GUID_SIZE)) {
1693 if (sess->state == SMB2_SESSION_IN_PROGRESS) {
1698 if (sess->state == SMB2_SESSION_EXPIRED) {
1703 if (ksmbd_session_lookup(conn, sess_id)) {
1708 conn->binding = true;
1709 } else if ((conn->dialect < SMB30_PROT_ID ||
1710 server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1711 (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
1716 sess = ksmbd_session_lookup(conn,
1717 le64_to_cpu(req->hdr.SessionId));
1725 if (sess->state == SMB2_SESSION_EXPIRED)
1726 sess->state = SMB2_SESSION_IN_PROGRESS;
1728 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1729 le16_to_cpu(req->SecurityBufferOffset));
1731 if (decode_negotiation_token(work, negblob) == 0) {
1732 if (conn->mechToken)
1733 negblob = (struct negotiate_message *)conn->mechToken;
1736 if (server_conf.auth_mechs & conn->auth_mechs) {
1737 rc = generate_preauth_hash(work);
1741 if (conn->preferred_auth_mech &
1742 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
1743 rc = krb5_authenticate(work);
1749 ksmbd_conn_set_good(work);
1750 sess->state = SMB2_SESSION_VALID;
1751 kfree(sess->Preauth_HashValue);
1752 sess->Preauth_HashValue = NULL;
1753 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
1754 if (negblob->MessageType == NtLmNegotiate) {
1755 rc = ntlm_negotiate(work, negblob);
1759 STATUS_MORE_PROCESSING_REQUIRED;
1761 * Note: here total size -1 is done as an
1762 * adjustment for 0 size blob
1764 inc_rfc1001_len(rsp, le16_to_cpu(rsp->SecurityBufferLength) - 1);
1766 } else if (negblob->MessageType == NtLmAuthenticate) {
1767 rc = ntlm_authenticate(work);
1771 ksmbd_conn_set_good(work);
1772 sess->state = SMB2_SESSION_VALID;
1773 if (conn->binding) {
1774 struct preauth_session *preauth_sess;
1777 ksmbd_preauth_session_lookup(conn, sess->id);
1779 list_del(&preauth_sess->preauth_entry);
1780 kfree(preauth_sess);
1783 kfree(sess->Preauth_HashValue);
1784 sess->Preauth_HashValue = NULL;
1787 /* TODO: need one more negotiation */
1788 pr_err("Not support the preferred authentication\n");
1792 pr_err("Not support authentication\n");
1798 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1799 else if (rc == -ENOENT)
1800 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1801 else if (rc == -EACCES)
1802 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
1803 else if (rc == -EFAULT)
1804 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED;
1805 else if (rc == -ENOMEM)
1806 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
1808 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1810 if (conn->use_spnego && conn->mechToken) {
1811 kfree(conn->mechToken);
1812 conn->mechToken = NULL;
1815 if (rc < 0 && sess) {
1816 ksmbd_session_destroy(sess);
1824 * smb2_tree_connect() - handler for smb2 tree connect command
1825 * @work: smb work containing smb request buffer
1827 * Return: 0 on success, otherwise error
1829 int smb2_tree_connect(struct ksmbd_work *work)
1831 struct ksmbd_conn *conn = work->conn;
1832 struct smb2_tree_connect_req *req = work->request_buf;
1833 struct smb2_tree_connect_rsp *rsp = work->response_buf;
1834 struct ksmbd_session *sess = work->sess;
1835 char *treename = NULL, *name = NULL;
1836 struct ksmbd_tree_conn_status status;
1837 struct ksmbd_share_config *share;
1840 treename = smb_strndup_from_utf16(req->Buffer,
1841 le16_to_cpu(req->PathLength), true,
1843 if (IS_ERR(treename)) {
1844 pr_err("treename is NULL\n");
1845 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1849 name = ksmbd_extract_sharename(treename);
1851 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1855 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
1858 status = ksmbd_tree_conn_connect(sess, name);
1859 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1860 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1864 share = status.tree_conn->share_conf;
1865 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1866 ksmbd_debug(SMB, "IPC share path request\n");
1867 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1868 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1869 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1870 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1871 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1872 FILE_SYNCHRONIZE_LE;
1874 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1875 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1876 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1877 if (test_tree_conn_flag(status.tree_conn,
1878 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1879 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1880 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
1881 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE |
1882 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE |
1883 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1884 FILE_SYNCHRONIZE_LE;
1888 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1889 if (conn->posix_ext_supported)
1890 status.tree_conn->posix_extensions = true;
1893 rsp->StructureSize = cpu_to_le16(16);
1894 rsp->Capabilities = 0;
1896 /* default manual caching */
1897 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
1898 inc_rfc1001_len(rsp, 16);
1900 if (!IS_ERR(treename))
1905 switch (status.ret) {
1906 case KSMBD_TREE_CONN_STATUS_OK:
1907 rsp->hdr.Status = STATUS_SUCCESS;
1910 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1911 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1914 case KSMBD_TREE_CONN_STATUS_NOMEM:
1915 rsp->hdr.Status = STATUS_NO_MEMORY;
1917 case KSMBD_TREE_CONN_STATUS_ERROR:
1918 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1919 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1920 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1923 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1926 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1933 * smb2_create_open_flags() - convert smb open flags to unix open flags
1934 * @file_present: is file already present
1935 * @access: file access flags
1936 * @disposition: file disposition flags
1937 * @may_flags: set with MAY_ flags
1939 * Return: file open flags
1941 static int smb2_create_open_flags(bool file_present, __le32 access,
1945 int oflags = O_NONBLOCK | O_LARGEFILE;
1947 if (access & FILE_READ_DESIRED_ACCESS_LE &&
1948 access & FILE_WRITE_DESIRE_ACCESS_LE) {
1950 *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE;
1951 } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) {
1953 *may_flags = MAY_OPEN | MAY_WRITE;
1956 *may_flags = MAY_OPEN | MAY_READ;
1959 if (access == FILE_READ_ATTRIBUTES_LE)
1963 switch (disposition & FILE_CREATE_MASK_LE) {
1965 case FILE_CREATE_LE:
1967 case FILE_SUPERSEDE_LE:
1968 case FILE_OVERWRITE_LE:
1969 case FILE_OVERWRITE_IF_LE:
1976 switch (disposition & FILE_CREATE_MASK_LE) {
1977 case FILE_SUPERSEDE_LE:
1978 case FILE_CREATE_LE:
1979 case FILE_OPEN_IF_LE:
1980 case FILE_OVERWRITE_IF_LE:
1984 case FILE_OVERWRITE_LE:
1996 * smb2_tree_disconnect() - handler for smb tree connect request
1997 * @work: smb work containing request buffer
2001 int smb2_tree_disconnect(struct ksmbd_work *work)
2003 struct smb2_tree_disconnect_rsp *rsp = work->response_buf;
2004 struct ksmbd_session *sess = work->sess;
2005 struct ksmbd_tree_connect *tcon = work->tcon;
2007 rsp->StructureSize = cpu_to_le16(4);
2008 inc_rfc1001_len(rsp, 4);
2010 ksmbd_debug(SMB, "request\n");
2013 struct smb2_tree_disconnect_req *req = work->request_buf;
2015 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2016 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2017 smb2_set_err_rsp(work);
2021 ksmbd_close_tree_conn_fds(work);
2022 ksmbd_tree_conn_disconnect(sess, tcon);
2027 * smb2_session_logoff() - handler for session log off request
2028 * @work: smb work containing request buffer
2032 int smb2_session_logoff(struct ksmbd_work *work)
2034 struct ksmbd_conn *conn = work->conn;
2035 struct smb2_logoff_rsp *rsp = work->response_buf;
2036 struct ksmbd_session *sess = work->sess;
2038 rsp->StructureSize = cpu_to_le16(4);
2039 inc_rfc1001_len(rsp, 4);
2041 ksmbd_debug(SMB, "request\n");
2043 /* Got a valid session, set connection state */
2044 WARN_ON(sess->conn != conn);
2046 /* setting CifsExiting here may race with start_tcp_sess */
2047 ksmbd_conn_set_need_reconnect(work);
2048 ksmbd_close_session_fds(work);
2049 ksmbd_conn_wait_idle(conn);
2051 if (ksmbd_tree_conn_session_logoff(sess)) {
2052 struct smb2_logoff_req *req = work->request_buf;
2054 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2055 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2056 smb2_set_err_rsp(work);
2060 ksmbd_destroy_file_table(&sess->file_table);
2061 sess->state = SMB2_SESSION_EXPIRED;
2063 ksmbd_free_user(sess->user);
2066 /* let start_tcp_sess free connection info now */
2067 ksmbd_conn_set_need_negotiate(work);
2072 * create_smb2_pipe() - create IPC pipe
2073 * @work: smb work containing request buffer
2075 * Return: 0 on success, otherwise error
2077 static noinline int create_smb2_pipe(struct ksmbd_work *work)
2079 struct smb2_create_rsp *rsp = work->response_buf;
2080 struct smb2_create_req *req = work->request_buf;
2085 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
2086 1, work->conn->local_nls);
2088 rsp->hdr.Status = STATUS_NO_MEMORY;
2089 err = PTR_ERR(name);
2093 id = ksmbd_session_rpc_open(work->sess, name);
2095 pr_err("Unable to open RPC pipe: %d\n", id);
2100 rsp->hdr.Status = STATUS_SUCCESS;
2101 rsp->StructureSize = cpu_to_le16(89);
2102 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2104 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
2106 rsp->CreationTime = cpu_to_le64(0);
2107 rsp->LastAccessTime = cpu_to_le64(0);
2108 rsp->ChangeTime = cpu_to_le64(0);
2109 rsp->AllocationSize = cpu_to_le64(0);
2110 rsp->EndofFile = cpu_to_le64(0);
2111 rsp->FileAttributes = ATTR_NORMAL_LE;
2113 rsp->VolatileFileId = cpu_to_le64(id);
2114 rsp->PersistentFileId = 0;
2115 rsp->CreateContextsOffset = 0;
2116 rsp->CreateContextsLength = 0;
2118 inc_rfc1001_len(rsp, 88); /* StructureSize - 1*/
2125 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2129 rsp->hdr.Status = STATUS_NO_MEMORY;
2136 smb2_set_err_rsp(work);
2141 * smb2_set_ea() - handler for setting extended attributes using set
2143 * @eabuf: set info command buffer
2144 * @buf_len: set info command buffer length
2145 * @path: dentry path for get ea
2147 * Return: 0 on success, otherwise error
2149 static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
2152 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
2153 char *attr_name = NULL, *value;
2155 unsigned int next = 0;
2157 if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength +
2158 le16_to_cpu(eabuf->EaValueLength))
2161 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
2166 if (!eabuf->EaNameLength)
2170 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2171 eabuf->name, eabuf->EaNameLength,
2172 le16_to_cpu(eabuf->EaValueLength),
2173 le32_to_cpu(eabuf->NextEntryOffset));
2175 if (eabuf->EaNameLength >
2176 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
2181 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2182 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
2183 eabuf->EaNameLength);
2184 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2185 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2187 if (!eabuf->EaValueLength) {
2188 rc = ksmbd_vfs_casexattr_len(user_ns,
2191 XATTR_USER_PREFIX_LEN +
2192 eabuf->EaNameLength);
2194 /* delete the EA only when it exits */
2196 rc = ksmbd_vfs_remove_xattr(user_ns,
2202 "remove xattr failed(%d)\n",
2208 /* if the EA doesn't exist, just do nothing. */
2211 rc = ksmbd_vfs_setxattr(user_ns,
2212 path->dentry, attr_name, value,
2213 le16_to_cpu(eabuf->EaValueLength), 0);
2216 "ksmbd_vfs_setxattr is failed(%d)\n",
2223 next = le32_to_cpu(eabuf->NextEntryOffset);
2224 if (next == 0 || buf_len < next)
2227 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
2228 if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength))
2231 } while (next != 0);
2237 static noinline int smb2_set_stream_name_xattr(struct path *path,
2238 struct ksmbd_file *fp,
2239 char *stream_name, int s_type)
2241 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
2242 size_t xattr_stream_size;
2243 char *xattr_stream_name;
2246 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2253 fp->stream.name = xattr_stream_name;
2254 fp->stream.size = xattr_stream_size;
2256 /* Check if there is stream prefix in xattr space */
2257 rc = ksmbd_vfs_casexattr_len(user_ns,
2264 if (fp->cdoption == FILE_OPEN_LE) {
2265 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2269 rc = ksmbd_vfs_setxattr(user_ns, path->dentry,
2270 xattr_stream_name, NULL, 0, 0);
2272 pr_err("Failed to store XATTR stream name :%d\n", rc);
2276 static int smb2_remove_smb_xattrs(struct path *path)
2278 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
2279 char *name, *xattr_list = NULL;
2280 ssize_t xattr_list_len;
2283 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
2284 if (xattr_list_len < 0) {
2286 } else if (!xattr_list_len) {
2287 ksmbd_debug(SMB, "empty xattr in the file\n");
2291 for (name = xattr_list; name - xattr_list < xattr_list_len;
2292 name += strlen(name) + 1) {
2293 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2295 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
2296 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2297 DOS_ATTRIBUTE_PREFIX_LEN) &&
2298 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
2301 err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name);
2303 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2310 static int smb2_create_truncate(struct path *path)
2312 int rc = vfs_truncate(path, 0);
2315 pr_err("vfs_truncate failed, rc %d\n", rc);
2319 rc = smb2_remove_smb_xattrs(path);
2320 if (rc == -EOPNOTSUPP)
2324 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2329 static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
2330 struct ksmbd_file *fp)
2332 struct xattr_dos_attrib da = {0};
2335 if (!test_share_config_flag(tcon->share_conf,
2336 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2340 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2341 da.itime = da.create_time = fp->create_time;
2342 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2343 XATTR_DOSINFO_ITIME;
2345 rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt),
2348 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2351 static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
2352 struct path *path, struct ksmbd_file *fp)
2354 struct xattr_dos_attrib da;
2357 fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE);
2359 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2360 if (!test_share_config_flag(tcon->share_conf,
2361 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2364 rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt),
2367 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2368 fp->create_time = da.create_time;
2369 fp->itime = da.itime;
2373 static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
2374 int open_flags, umode_t posix_mode, bool is_dir)
2376 struct ksmbd_tree_connect *tcon = work->tcon;
2377 struct ksmbd_share_config *share = tcon->share_conf;
2381 if (!(open_flags & O_CREAT))
2384 ksmbd_debug(SMB, "file does not exist, so creating\n");
2385 if (is_dir == true) {
2386 ksmbd_debug(SMB, "creating directory\n");
2388 mode = share_config_directory_mode(share, posix_mode);
2389 rc = ksmbd_vfs_mkdir(work, name, mode);
2393 ksmbd_debug(SMB, "creating regular file\n");
2395 mode = share_config_create_mode(share, posix_mode);
2396 rc = ksmbd_vfs_create(work, name, mode);
2401 rc = ksmbd_vfs_kern_path(work, name, 0, path, 0);
2403 pr_err("cannot get linux path (%s), err = %d\n",
2410 static int smb2_create_sd_buffer(struct ksmbd_work *work,
2411 struct smb2_create_req *req,
2414 struct create_context *context;
2415 struct create_sd_buf_req *sd_buf;
2417 if (!req->CreateContextsOffset)
2420 /* Parse SD BUFFER create contexts */
2421 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
2424 else if (IS_ERR(context))
2425 return PTR_ERR(context);
2428 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2429 sd_buf = (struct create_sd_buf_req *)context;
2430 if (le16_to_cpu(context->DataOffset) +
2431 le32_to_cpu(context->DataLength) <
2432 sizeof(struct create_sd_buf_req))
2434 return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
2435 le32_to_cpu(sd_buf->ccontext.DataLength), true);
2438 static void ksmbd_acls_fattr(struct smb_fattr *fattr,
2439 struct user_namespace *mnt_userns,
2440 struct inode *inode)
2442 fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode);
2443 fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode);
2444 fattr->cf_mode = inode->i_mode;
2445 fattr->cf_acls = NULL;
2446 fattr->cf_dacls = NULL;
2448 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
2449 fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS);
2450 if (S_ISDIR(inode->i_mode))
2451 fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT);
2456 * smb2_open() - handler for smb file open request
2457 * @work: smb work containing request buffer
2459 * Return: 0 on success, otherwise error
2461 int smb2_open(struct ksmbd_work *work)
2463 struct ksmbd_conn *conn = work->conn;
2464 struct ksmbd_session *sess = work->sess;
2465 struct ksmbd_tree_connect *tcon = work->tcon;
2466 struct smb2_create_req *req;
2467 struct smb2_create_rsp *rsp, *rsp_org;
2469 struct ksmbd_share_config *share = tcon->share_conf;
2470 struct ksmbd_file *fp = NULL;
2471 struct file *filp = NULL;
2472 struct user_namespace *user_ns = NULL;
2474 struct create_context *context;
2475 struct lease_ctx_info *lc = NULL;
2476 struct create_ea_buf_req *ea_buf = NULL;
2477 struct oplock_info *opinfo;
2478 __le32 *next_ptr = NULL;
2479 int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0;
2481 int contxt_cnt = 0, query_disk_id = 0;
2482 int maximal_access_ctxt = 0, posix_ctxt = 0;
2486 char *stream_name = NULL;
2487 bool file_present = false, created = false, already_permitted = false;
2488 int share_ret, need_truncate = 0;
2490 umode_t posix_mode = 0;
2491 __le32 daccess, maximal_access = 0;
2493 rsp_org = work->response_buf;
2494 WORK_BUFFERS(work, req, rsp);
2496 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
2497 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
2498 ksmbd_debug(SMB, "invalid flag in chained command\n");
2499 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2500 smb2_set_err_rsp(work);
2504 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2505 ksmbd_debug(SMB, "IPC pipe create request\n");
2506 return create_smb2_pipe(work);
2509 if (req->NameLength) {
2510 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
2511 *(char *)req->Buffer == '\\') {
2512 pr_err("not allow directory name included leading slash\n");
2517 name = smb2_get_name(share,
2519 le16_to_cpu(req->NameLength),
2520 work->conn->local_nls);
2529 ksmbd_debug(SMB, "converted name = %s\n", name);
2530 if (strchr(name, ':')) {
2531 if (!test_share_config_flag(work->tcon->share_conf,
2532 KSMBD_SHARE_FLAG_STREAMS)) {
2536 rc = parse_stream_name(name, &stream_name, &s_type);
2541 rc = ksmbd_validate_filename(name);
2545 if (ksmbd_share_veto_filename(share, name)) {
2547 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
2552 name = kstrdup("", GFP_KERNEL);
2559 req_op_level = req->RequestedOplockLevel;
2560 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
2561 lc = parse_lease_state(req);
2563 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) {
2564 pr_err("Invalid impersonationlevel : 0x%x\n",
2565 le32_to_cpu(req->ImpersonationLevel));
2567 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2571 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) {
2572 pr_err("Invalid create options : 0x%x\n",
2573 le32_to_cpu(req->CreateOptions));
2577 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
2578 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
2579 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2581 if (req->CreateOptions &
2582 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2583 FILE_RESERVE_OPFILTER_LE)) {
2588 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2589 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2592 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
2593 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
2598 if (le32_to_cpu(req->CreateDisposition) >
2599 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
2600 pr_err("Invalid create disposition : 0x%x\n",
2601 le32_to_cpu(req->CreateDisposition));
2606 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
2607 pr_err("Invalid desired access : 0x%x\n",
2608 le32_to_cpu(req->DesiredAccess));
2613 if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) {
2614 pr_err("Invalid file attribute : 0x%x\n",
2615 le32_to_cpu(req->FileAttributes));
2620 if (req->CreateContextsOffset) {
2621 /* Parse non-durable handle create contexts */
2622 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
2623 if (IS_ERR(context)) {
2624 rc = PTR_ERR(context);
2626 } else if (context) {
2627 ea_buf = (struct create_ea_buf_req *)context;
2628 if (le16_to_cpu(context->DataOffset) +
2629 le32_to_cpu(context->DataLength) <
2630 sizeof(struct create_ea_buf_req)) {
2634 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2635 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2641 context = smb2_find_context_vals(req,
2642 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
2643 if (IS_ERR(context)) {
2644 rc = PTR_ERR(context);
2646 } else if (context) {
2648 "get query maximal access context\n");
2649 maximal_access_ctxt = 1;
2652 context = smb2_find_context_vals(req,
2653 SMB2_CREATE_TIMEWARP_REQUEST);
2654 if (IS_ERR(context)) {
2655 rc = PTR_ERR(context);
2657 } else if (context) {
2658 ksmbd_debug(SMB, "get timewarp context\n");
2663 if (tcon->posix_extensions) {
2664 context = smb2_find_context_vals(req,
2665 SMB2_CREATE_TAG_POSIX);
2666 if (IS_ERR(context)) {
2667 rc = PTR_ERR(context);
2669 } else if (context) {
2670 struct create_posix *posix =
2671 (struct create_posix *)context;
2672 if (le16_to_cpu(context->DataOffset) +
2673 le32_to_cpu(context->DataLength) <
2674 sizeof(struct create_posix)) {
2678 ksmbd_debug(SMB, "get posix context\n");
2680 posix_mode = le32_to_cpu(posix->Mode);
2686 if (ksmbd_override_fsids(work)) {
2691 rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1);
2693 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
2695 * If file exists with under flags, return access
2698 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
2699 req->CreateDisposition == FILE_OPEN_IF_LE) {
2705 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2707 "User does not have write permission\n");
2712 } else if (d_is_symlink(path.dentry)) {
2722 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
2726 file_present = true;
2727 user_ns = mnt_user_ns(path.mnt);
2728 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
2731 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2732 if (s_type == DATA_STREAM) {
2734 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2737 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2739 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2743 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
2744 req->FileAttributes & ATTR_NORMAL_LE) {
2745 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2753 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2754 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
2755 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
2756 name, req->CreateOptions);
2757 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2762 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
2763 !(req->CreateDisposition == FILE_CREATE_LE) &&
2764 !S_ISDIR(stat.mode)) {
2765 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2770 if (!stream_name && file_present &&
2771 req->CreateDisposition == FILE_CREATE_LE) {
2776 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2778 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
2779 rc = smb_check_perm_dacl(conn, &path, &daccess,
2785 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2786 if (!file_present) {
2787 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2789 rc = ksmbd_vfs_query_maximal_access(user_ns,
2794 already_permitted = true;
2796 maximal_access = daccess;
2799 open_flags = smb2_create_open_flags(file_present, daccess,
2800 req->CreateDisposition,
2803 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2804 if (open_flags & O_CREAT) {
2806 "User does not have write permission\n");
2812 /*create file if not present */
2813 if (!file_present) {
2814 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
2815 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
2817 if (rc == -ENOENT) {
2819 rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND;
2825 user_ns = mnt_user_ns(path.mnt);
2827 if (le32_to_cpu(ea_buf->ccontext.DataLength) <
2828 sizeof(struct smb2_ea_info)) {
2833 rc = smb2_set_ea(&ea_buf->ea,
2834 le32_to_cpu(ea_buf->ccontext.DataLength),
2836 if (rc == -EOPNOTSUPP)
2841 } else if (!already_permitted) {
2842 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2843 * because execute(search) permission on a parent directory,
2844 * is already granted.
2846 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
2847 rc = inode_permission(user_ns,
2848 d_inode(path.dentry),
2853 if ((daccess & FILE_DELETE_LE) ||
2854 (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
2855 rc = ksmbd_vfs_may_delete(user_ns,
2863 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2864 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2870 filp = dentry_open(&path, open_flags, current_cred());
2873 pr_err("dentry open for dir failed, rc %d\n", rc);
2878 if (!(open_flags & O_TRUNC))
2879 file_info = FILE_OPENED;
2881 file_info = FILE_OVERWRITTEN;
2883 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2885 file_info = FILE_SUPERSEDED;
2886 } else if (open_flags & O_CREAT) {
2887 file_info = FILE_CREATED;
2890 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2892 /* Obtain Volatile-ID */
2893 fp = ksmbd_open_fd(work, filp);
2901 /* Get Persistent-ID */
2902 ksmbd_open_durable_fd(fp);
2903 if (!has_file_id(fp->persistent_id)) {
2908 fp->filename = name;
2909 fp->cdoption = req->CreateDisposition;
2910 fp->daccess = daccess;
2911 fp->saccess = req->ShareAccess;
2912 fp->coption = req->CreateOptions;
2914 /* Set default windows and posix acls if creating new file */
2917 struct inode *inode = d_inode(path.dentry);
2919 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns,
2921 d_inode(path.dentry->d_parent));
2923 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2925 if (test_share_config_flag(work->tcon->share_conf,
2926 KSMBD_SHARE_FLAG_ACL_XATTR)) {
2927 rc = smb_inherit_dacl(conn, &path, sess->user->uid,
2932 rc = smb2_create_sd_buffer(work, req, &path);
2935 ksmbd_vfs_set_init_posix_acl(user_ns,
2938 if (test_share_config_flag(work->tcon->share_conf,
2939 KSMBD_SHARE_FLAG_ACL_XATTR)) {
2940 struct smb_fattr fattr;
2941 struct smb_ntsd *pntsd;
2942 int pntsd_size, ace_num = 0;
2944 ksmbd_acls_fattr(&fattr, user_ns, inode);
2946 ace_num = fattr.cf_acls->a_count;
2948 ace_num += fattr.cf_dacls->a_count;
2950 pntsd = kmalloc(sizeof(struct smb_ntsd) +
2951 sizeof(struct smb_sid) * 3 +
2952 sizeof(struct smb_acl) +
2953 sizeof(struct smb_ace) * ace_num * 2,
2958 rc = build_sec_desc(user_ns,
2963 &pntsd_size, &fattr);
2964 posix_acl_release(fattr.cf_acls);
2965 posix_acl_release(fattr.cf_dacls);
2967 rc = ksmbd_vfs_set_sd_xattr(conn,
2974 pr_err("failed to store ntacl in xattr : %d\n",
2983 rc = smb2_set_stream_name_xattr(&path,
2989 file_info = FILE_CREATED;
2992 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
2993 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
2994 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
2995 !fp->attrib_only && !stream_name) {
2996 smb_break_all_oplock(work, fp);
3000 /* fp should be searchable through ksmbd_inode.m_fp_list
3001 * after daccess, saccess, attrib_only, and stream are
3004 write_lock(&fp->f_ci->m_lock);
3005 list_add(&fp->node, &fp->f_ci->m_fp_list);
3006 write_unlock(&fp->f_ci->m_lock);
3008 rc = ksmbd_vfs_getattr(&path, &stat);
3010 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
3014 /* Check delete pending among previous fp before oplock break */
3015 if (ksmbd_inode_pending_delete(fp)) {
3020 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
3021 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
3022 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
3023 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
3024 if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) {
3029 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
3030 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
3032 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
3033 name, req_op_level, lc->req_state);
3034 rc = find_same_lease_key(sess, fp->f_ci, lc);
3037 } else if (open_flags == O_RDONLY &&
3038 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
3039 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
3040 req_op_level = SMB2_OPLOCK_LEVEL_II;
3042 rc = smb_grant_oplock(work, req_op_level,
3043 fp->persistent_id, fp,
3044 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
3050 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
3051 ksmbd_fd_set_delete_on_close(fp, file_info);
3053 if (need_truncate) {
3054 rc = smb2_create_truncate(&path);
3059 if (req->CreateContextsOffset) {
3060 struct create_alloc_size_req *az_req;
3062 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
3063 SMB2_CREATE_ALLOCATION_SIZE);
3064 if (IS_ERR(az_req)) {
3065 rc = PTR_ERR(az_req);
3067 } else if (az_req) {
3071 if (le16_to_cpu(az_req->ccontext.DataOffset) +
3072 le32_to_cpu(az_req->ccontext.DataLength) <
3073 sizeof(struct create_alloc_size_req)) {
3077 alloc_size = le64_to_cpu(az_req->AllocationSize);
3079 "request smb2 create allocate size : %llu\n",
3081 smb_break_all_levII_oplock(work, fp, 1);
3082 err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
3086 "vfs_fallocate is failed : %d\n",
3090 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
3091 if (IS_ERR(context)) {
3092 rc = PTR_ERR(context);
3094 } else if (context) {
3095 ksmbd_debug(SMB, "get query on disk id context\n");
3100 if (stat.result_mask & STATX_BTIME)
3101 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
3103 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
3104 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
3106 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
3109 smb2_update_xattrs(tcon, &path, fp);
3111 smb2_new_xattrs(tcon, &path, fp);
3113 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
3115 generic_fillattr(user_ns, file_inode(fp->filp),
3118 rsp->StructureSize = cpu_to_le16(89);
3120 opinfo = rcu_dereference(fp->f_opinfo);
3121 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
3124 rsp->CreateAction = cpu_to_le32(file_info);
3125 rsp->CreationTime = cpu_to_le64(fp->create_time);
3126 time = ksmbd_UnixTimeToNT(stat.atime);
3127 rsp->LastAccessTime = cpu_to_le64(time);
3128 time = ksmbd_UnixTimeToNT(stat.mtime);
3129 rsp->LastWriteTime = cpu_to_le64(time);
3130 time = ksmbd_UnixTimeToNT(stat.ctime);
3131 rsp->ChangeTime = cpu_to_le64(time);
3132 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
3133 cpu_to_le64(stat.blocks << 9);
3134 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
3135 rsp->FileAttributes = fp->f_ci->m_fattr;
3139 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
3140 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
3142 rsp->CreateContextsOffset = 0;
3143 rsp->CreateContextsLength = 0;
3144 inc_rfc1001_len(rsp_org, 88); /* StructureSize - 1*/
3146 /* If lease is request send lease context response */
3147 if (opinfo && opinfo->is_lease) {
3148 struct create_context *lease_ccontext;
3150 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
3151 name, opinfo->o_lease->state);
3152 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
3154 lease_ccontext = (struct create_context *)rsp->Buffer;
3156 create_lease_buf(rsp->Buffer, opinfo->o_lease);
3157 le32_add_cpu(&rsp->CreateContextsLength,
3158 conn->vals->create_lease_size);
3159 inc_rfc1001_len(rsp_org, conn->vals->create_lease_size);
3160 next_ptr = &lease_ccontext->Next;
3161 next_off = conn->vals->create_lease_size;
3164 if (maximal_access_ctxt) {
3165 struct create_context *mxac_ccontext;
3167 if (maximal_access == 0)
3168 ksmbd_vfs_query_maximal_access(user_ns,
3171 mxac_ccontext = (struct create_context *)(rsp->Buffer +
3172 le32_to_cpu(rsp->CreateContextsLength));
3174 create_mxac_rsp_buf(rsp->Buffer +
3175 le32_to_cpu(rsp->CreateContextsLength),
3176 le32_to_cpu(maximal_access));
3177 le32_add_cpu(&rsp->CreateContextsLength,
3178 conn->vals->create_mxac_size);
3179 inc_rfc1001_len(rsp_org, conn->vals->create_mxac_size);
3181 *next_ptr = cpu_to_le32(next_off);
3182 next_ptr = &mxac_ccontext->Next;
3183 next_off = conn->vals->create_mxac_size;
3186 if (query_disk_id) {
3187 struct create_context *disk_id_ccontext;
3189 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
3190 le32_to_cpu(rsp->CreateContextsLength));
3192 create_disk_id_rsp_buf(rsp->Buffer +
3193 le32_to_cpu(rsp->CreateContextsLength),
3194 stat.ino, tcon->id);
3195 le32_add_cpu(&rsp->CreateContextsLength,
3196 conn->vals->create_disk_id_size);
3197 inc_rfc1001_len(rsp_org, conn->vals->create_disk_id_size);
3199 *next_ptr = cpu_to_le32(next_off);
3200 next_ptr = &disk_id_ccontext->Next;
3201 next_off = conn->vals->create_disk_id_size;
3206 create_posix_rsp_buf(rsp->Buffer +
3207 le32_to_cpu(rsp->CreateContextsLength),
3209 le32_add_cpu(&rsp->CreateContextsLength,
3210 conn->vals->create_posix_size);
3211 inc_rfc1001_len(rsp_org, conn->vals->create_posix_size);
3213 *next_ptr = cpu_to_le32(next_off);
3216 if (contxt_cnt > 0) {
3217 rsp->CreateContextsOffset =
3218 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)
3223 if (file_present || created)
3225 ksmbd_revert_fsids(work);
3229 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3230 else if (rc == -EOPNOTSUPP)
3231 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
3232 else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV)
3233 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3234 else if (rc == -ENOENT)
3235 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3236 else if (rc == -EPERM)
3237 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3238 else if (rc == -EBUSY)
3239 rsp->hdr.Status = STATUS_DELETE_PENDING;
3240 else if (rc == -EBADF)
3241 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3242 else if (rc == -ENOEXEC)
3243 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3244 else if (rc == -ENXIO)
3245 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3246 else if (rc == -EEXIST)
3247 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3248 else if (rc == -EMFILE)
3249 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3250 if (!rsp->hdr.Status)
3251 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3253 if (!fp || !fp->filename)
3256 ksmbd_fd_put(work, fp);
3257 smb2_set_err_rsp(work);
3258 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3266 static int readdir_info_level_struct_sz(int info_level)
3268 switch (info_level) {
3269 case FILE_FULL_DIRECTORY_INFORMATION:
3270 return sizeof(struct file_full_directory_info);
3271 case FILE_BOTH_DIRECTORY_INFORMATION:
3272 return sizeof(struct file_both_directory_info);
3273 case FILE_DIRECTORY_INFORMATION:
3274 return sizeof(struct file_directory_info);
3275 case FILE_NAMES_INFORMATION:
3276 return sizeof(struct file_names_info);
3277 case FILEID_FULL_DIRECTORY_INFORMATION:
3278 return sizeof(struct file_id_full_dir_info);
3279 case FILEID_BOTH_DIRECTORY_INFORMATION:
3280 return sizeof(struct file_id_both_directory_info);
3281 case SMB_FIND_FILE_POSIX_INFO:
3282 return sizeof(struct smb2_posix_info);
3288 static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3290 switch (info_level) {
3291 case FILE_FULL_DIRECTORY_INFORMATION:
3293 struct file_full_directory_info *ffdinfo;
3295 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3296 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3297 d_info->name = ffdinfo->FileName;
3298 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3301 case FILE_BOTH_DIRECTORY_INFORMATION:
3303 struct file_both_directory_info *fbdinfo;
3305 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3306 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3307 d_info->name = fbdinfo->FileName;
3308 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3311 case FILE_DIRECTORY_INFORMATION:
3313 struct file_directory_info *fdinfo;
3315 fdinfo = (struct file_directory_info *)d_info->rptr;
3316 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3317 d_info->name = fdinfo->FileName;
3318 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3321 case FILE_NAMES_INFORMATION:
3323 struct file_names_info *fninfo;
3325 fninfo = (struct file_names_info *)d_info->rptr;
3326 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3327 d_info->name = fninfo->FileName;
3328 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3331 case FILEID_FULL_DIRECTORY_INFORMATION:
3333 struct file_id_full_dir_info *dinfo;
3335 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3336 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3337 d_info->name = dinfo->FileName;
3338 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3341 case FILEID_BOTH_DIRECTORY_INFORMATION:
3343 struct file_id_both_directory_info *fibdinfo;
3345 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3346 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3347 d_info->name = fibdinfo->FileName;
3348 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3351 case SMB_FIND_FILE_POSIX_INFO:
3353 struct smb2_posix_info *posix_info;
3355 posix_info = (struct smb2_posix_info *)d_info->rptr;
3356 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3357 d_info->name = posix_info->name;
3358 d_info->name_len = le32_to_cpu(posix_info->name_len);
3367 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3369 * @conn: connection instance
3370 * @info_level: smb information level
3371 * @d_info: structure included variables for query dir
3372 * @user_ns: user namespace
3373 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3375 * if directory has many entries, find first can't read it fully.
3376 * find next might be called multiple times to read remaining dir entries
3378 * Return: 0 on success, otherwise error
3380 static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
3381 struct ksmbd_dir_info *d_info,
3382 struct ksmbd_kstat *ksmbd_kstat)
3384 int next_entry_offset = 0;
3388 int struct_sz, rc = 0;
3390 conv_name = ksmbd_convert_dir_info_name(d_info,
3396 /* Somehow the name has only terminating NULL bytes */
3399 goto free_conv_name;
3402 struct_sz = readdir_info_level_struct_sz(info_level);
3403 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3404 KSMBD_DIR_INFO_ALIGNMENT);
3406 if (next_entry_offset > d_info->out_buf_len) {
3407 d_info->out_buf_len = 0;
3409 goto free_conv_name;
3412 kstat = d_info->wptr;
3413 if (info_level != FILE_NAMES_INFORMATION)
3414 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3416 switch (info_level) {
3417 case FILE_FULL_DIRECTORY_INFORMATION:
3419 struct file_full_directory_info *ffdinfo;
3421 ffdinfo = (struct file_full_directory_info *)kstat;
3422 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3424 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3425 if (ffdinfo->EaSize)
3426 ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3427 if (d_info->hide_dot_file && d_info->name[0] == '.')
3428 ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3429 memcpy(ffdinfo->FileName, conv_name, conv_len);
3430 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3433 case FILE_BOTH_DIRECTORY_INFORMATION:
3435 struct file_both_directory_info *fbdinfo;
3437 fbdinfo = (struct file_both_directory_info *)kstat;
3438 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3440 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3441 if (fbdinfo->EaSize)
3442 fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3443 fbdinfo->ShortNameLength = 0;
3444 fbdinfo->Reserved = 0;
3445 if (d_info->hide_dot_file && d_info->name[0] == '.')
3446 fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3447 memcpy(fbdinfo->FileName, conv_name, conv_len);
3448 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3451 case FILE_DIRECTORY_INFORMATION:
3453 struct file_directory_info *fdinfo;
3455 fdinfo = (struct file_directory_info *)kstat;
3456 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3457 if (d_info->hide_dot_file && d_info->name[0] == '.')
3458 fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3459 memcpy(fdinfo->FileName, conv_name, conv_len);
3460 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3463 case FILE_NAMES_INFORMATION:
3465 struct file_names_info *fninfo;
3467 fninfo = (struct file_names_info *)kstat;
3468 fninfo->FileNameLength = cpu_to_le32(conv_len);
3469 memcpy(fninfo->FileName, conv_name, conv_len);
3470 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3473 case FILEID_FULL_DIRECTORY_INFORMATION:
3475 struct file_id_full_dir_info *dinfo;
3477 dinfo = (struct file_id_full_dir_info *)kstat;
3478 dinfo->FileNameLength = cpu_to_le32(conv_len);
3480 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3482 dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3483 dinfo->Reserved = 0;
3484 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3485 if (d_info->hide_dot_file && d_info->name[0] == '.')
3486 dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3487 memcpy(dinfo->FileName, conv_name, conv_len);
3488 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3491 case FILEID_BOTH_DIRECTORY_INFORMATION:
3493 struct file_id_both_directory_info *fibdinfo;
3495 fibdinfo = (struct file_id_both_directory_info *)kstat;
3496 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3498 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3499 if (fibdinfo->EaSize)
3500 fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3501 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3502 fibdinfo->ShortNameLength = 0;
3503 fibdinfo->Reserved = 0;
3504 fibdinfo->Reserved2 = cpu_to_le16(0);
3505 if (d_info->hide_dot_file && d_info->name[0] == '.')
3506 fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3507 memcpy(fibdinfo->FileName, conv_name, conv_len);
3508 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3511 case SMB_FIND_FILE_POSIX_INFO:
3513 struct smb2_posix_info *posix_info;
3516 posix_info = (struct smb2_posix_info *)kstat;
3517 posix_info->Ignored = 0;
3518 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3519 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3520 posix_info->ChangeTime = cpu_to_le64(time);
3521 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3522 posix_info->LastAccessTime = cpu_to_le64(time);
3523 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3524 posix_info->LastWriteTime = cpu_to_le64(time);
3525 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3526 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3527 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3528 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3529 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3530 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3531 posix_info->DosAttributes =
3532 S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE;
3533 if (d_info->hide_dot_file && d_info->name[0] == '.')
3534 posix_info->DosAttributes |= ATTR_HIDDEN_LE;
3535 id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid),
3536 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
3537 id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid),
3538 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
3539 memcpy(posix_info->name, conv_name, conv_len);
3540 posix_info->name_len = cpu_to_le32(conv_len);
3541 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3545 } /* switch (info_level) */
3547 d_info->last_entry_offset = d_info->data_count;
3548 d_info->data_count += next_entry_offset;
3549 d_info->out_buf_len -= next_entry_offset;
3550 d_info->wptr += next_entry_offset;
3553 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3554 info_level, d_info->out_buf_len,
3555 next_entry_offset, d_info->data_count);
3562 struct smb2_query_dir_private {
3563 struct ksmbd_work *work;
3564 char *search_pattern;
3565 struct ksmbd_file *dir_fp;
3567 struct ksmbd_dir_info *d_info;
3571 static void lock_dir(struct ksmbd_file *dir_fp)
3573 struct dentry *dir = dir_fp->filp->f_path.dentry;
3575 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3578 static void unlock_dir(struct ksmbd_file *dir_fp)
3580 struct dentry *dir = dir_fp->filp->f_path.dentry;
3582 inode_unlock(d_inode(dir));
3585 static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3587 struct user_namespace *user_ns = file_mnt_user_ns(priv->dir_fp->filp);
3589 struct ksmbd_kstat ksmbd_kstat;
3593 for (i = 0; i < priv->d_info->num_entry; i++) {
3594 struct dentry *dent;
3596 if (dentry_name(priv->d_info, priv->info_level))
3599 lock_dir(priv->dir_fp);
3600 dent = lookup_one(user_ns, priv->d_info->name,
3601 priv->dir_fp->filp->f_path.dentry,
3602 priv->d_info->name_len);
3603 unlock_dir(priv->dir_fp);
3606 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
3611 if (unlikely(d_is_negative(dent))) {
3613 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3614 priv->d_info->name);
3618 ksmbd_kstat.kstat = &kstat;
3619 if (priv->info_level != FILE_NAMES_INFORMATION)
3620 ksmbd_vfs_fill_dentry_attrs(priv->work,
3625 rc = smb2_populate_readdir_entry(priv->work->conn,
3636 static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
3641 int next_entry_offset;
3643 struct_sz = readdir_info_level_struct_sz(info_level);
3644 if (struct_sz == -EOPNOTSUPP)
3647 conv_len = (d_info->name_len + 1) * 2;
3648 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3649 KSMBD_DIR_INFO_ALIGNMENT);
3651 if (next_entry_offset > d_info->out_buf_len) {
3652 d_info->out_buf_len = 0;
3656 switch (info_level) {
3657 case FILE_FULL_DIRECTORY_INFORMATION:
3659 struct file_full_directory_info *ffdinfo;
3661 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3662 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3663 ffdinfo->FileName[d_info->name_len] = 0x00;
3664 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3665 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3668 case FILE_BOTH_DIRECTORY_INFORMATION:
3670 struct file_both_directory_info *fbdinfo;
3672 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3673 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3674 fbdinfo->FileName[d_info->name_len] = 0x00;
3675 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3676 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3679 case FILE_DIRECTORY_INFORMATION:
3681 struct file_directory_info *fdinfo;
3683 fdinfo = (struct file_directory_info *)d_info->wptr;
3684 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3685 fdinfo->FileName[d_info->name_len] = 0x00;
3686 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3687 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3690 case FILE_NAMES_INFORMATION:
3692 struct file_names_info *fninfo;
3694 fninfo = (struct file_names_info *)d_info->wptr;
3695 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3696 fninfo->FileName[d_info->name_len] = 0x00;
3697 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3698 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3701 case FILEID_FULL_DIRECTORY_INFORMATION:
3703 struct file_id_full_dir_info *dinfo;
3705 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3706 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3707 dinfo->FileName[d_info->name_len] = 0x00;
3708 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3709 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3712 case FILEID_BOTH_DIRECTORY_INFORMATION:
3714 struct file_id_both_directory_info *fibdinfo;
3716 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3717 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3718 fibdinfo->FileName[d_info->name_len] = 0x00;
3719 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3720 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3723 case SMB_FIND_FILE_POSIX_INFO:
3725 struct smb2_posix_info *posix_info;
3727 posix_info = (struct smb2_posix_info *)d_info->wptr;
3728 memcpy(posix_info->name, d_info->name, d_info->name_len);
3729 posix_info->name[d_info->name_len] = 0x00;
3730 posix_info->name_len = cpu_to_le32(d_info->name_len);
3731 posix_info->NextEntryOffset =
3732 cpu_to_le32(next_entry_offset);
3735 } /* switch (info_level) */
3737 d_info->num_entry++;
3738 d_info->out_buf_len -= next_entry_offset;
3739 d_info->wptr += next_entry_offset;
3743 static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
3744 loff_t offset, u64 ino, unsigned int d_type)
3746 struct ksmbd_readdir_data *buf;
3747 struct smb2_query_dir_private *priv;
3748 struct ksmbd_dir_info *d_info;
3751 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3752 priv = buf->private;
3753 d_info = priv->d_info;
3755 /* dot and dotdot entries are already reserved */
3756 if (!strcmp(".", name) || !strcmp("..", name))
3758 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3760 if (!match_pattern(name, namlen, priv->search_pattern))
3763 d_info->name = name;
3764 d_info->name_len = namlen;
3765 rc = reserve_populate_dentry(d_info, priv->info_level);
3768 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3769 d_info->out_buf_len = 0;
3775 static void restart_ctx(struct dir_context *ctx)
3780 static int verify_info_level(int info_level)
3782 switch (info_level) {
3783 case FILE_FULL_DIRECTORY_INFORMATION:
3784 case FILE_BOTH_DIRECTORY_INFORMATION:
3785 case FILE_DIRECTORY_INFORMATION:
3786 case FILE_NAMES_INFORMATION:
3787 case FILEID_FULL_DIRECTORY_INFORMATION:
3788 case FILEID_BOTH_DIRECTORY_INFORMATION:
3789 case SMB_FIND_FILE_POSIX_INFO:
3798 int smb2_query_dir(struct ksmbd_work *work)
3800 struct ksmbd_conn *conn = work->conn;
3801 struct smb2_query_directory_req *req;
3802 struct smb2_query_directory_rsp *rsp, *rsp_org;
3803 struct ksmbd_share_config *share = work->tcon->share_conf;
3804 struct ksmbd_file *dir_fp = NULL;
3805 struct ksmbd_dir_info d_info;
3807 char *srch_ptr = NULL;
3808 unsigned char srch_flag;
3810 struct smb2_query_dir_private query_dir_private = {NULL, };
3812 rsp_org = work->response_buf;
3813 WORK_BUFFERS(work, req, rsp);
3815 if (ksmbd_override_fsids(work)) {
3816 rsp->hdr.Status = STATUS_NO_MEMORY;
3817 smb2_set_err_rsp(work);
3821 rc = verify_info_level(req->FileInformationClass);
3827 dir_fp = ksmbd_lookup_fd_slow(work,
3828 le64_to_cpu(req->VolatileFileId),
3829 le64_to_cpu(req->PersistentFileId));
3835 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
3836 inode_permission(file_mnt_user_ns(dir_fp->filp),
3837 file_inode(dir_fp->filp),
3838 MAY_READ | MAY_EXEC)) {
3839 pr_err("no right to enumerate directory (%pd)\n",
3840 dir_fp->filp->f_path.dentry);
3845 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
3846 pr_err("can't do query dir for a file\n");
3851 srch_flag = req->Flags;
3852 srch_ptr = smb_strndup_from_utf16(req->Buffer,
3853 le16_to_cpu(req->FileNameLength), 1,
3855 if (IS_ERR(srch_ptr)) {
3856 ksmbd_debug(SMB, "Search Pattern not found\n");
3860 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
3863 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3865 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3866 ksmbd_debug(SMB, "Restart directory scan\n");
3867 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3868 restart_ctx(&dir_fp->readdir_data.ctx);
3871 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3872 d_info.wptr = (char *)rsp->Buffer;
3873 d_info.rptr = (char *)rsp->Buffer;
3874 d_info.out_buf_len = (work->response_sz - (get_rfc1002_len(rsp_org) + 4));
3875 d_info.out_buf_len = min_t(int, d_info.out_buf_len, le32_to_cpu(req->OutputBufferLength)) -
3876 sizeof(struct smb2_query_directory_rsp);
3877 d_info.flags = srch_flag;
3880 * reserve dot and dotdot entries in head of buffer
3883 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
3884 dir_fp, &d_info, srch_ptr,
3885 smb2_populate_readdir_entry);
3891 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3892 d_info.hide_dot_file = true;
3894 buffer_sz = d_info.out_buf_len;
3895 d_info.rptr = d_info.wptr;
3896 query_dir_private.work = work;
3897 query_dir_private.search_pattern = srch_ptr;
3898 query_dir_private.dir_fp = dir_fp;
3899 query_dir_private.d_info = &d_info;
3900 query_dir_private.info_level = req->FileInformationClass;
3901 dir_fp->readdir_data.private = &query_dir_private;
3902 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3904 rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx);
3906 restart_ctx(&dir_fp->readdir_data.ctx);
3912 d_info.wptr = d_info.rptr;
3913 d_info.out_buf_len = buffer_sz;
3914 rc = process_query_dir_entries(&query_dir_private);
3918 if (!d_info.data_count && d_info.out_buf_len >= 0) {
3919 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
3920 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3922 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3923 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3925 rsp->StructureSize = cpu_to_le16(9);
3926 rsp->OutputBufferOffset = cpu_to_le16(0);
3927 rsp->OutputBufferLength = cpu_to_le32(0);
3929 inc_rfc1001_len(rsp_org, 9);
3931 ((struct file_directory_info *)
3932 ((char *)rsp->Buffer + d_info.last_entry_offset))
3933 ->NextEntryOffset = 0;
3935 rsp->StructureSize = cpu_to_le16(9);
3936 rsp->OutputBufferOffset = cpu_to_le16(72);
3937 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
3938 inc_rfc1001_len(rsp_org, 8 + d_info.data_count);
3942 ksmbd_fd_put(work, dir_fp);
3943 ksmbd_revert_fsids(work);
3947 pr_err("error while processing smb2 query dir rc = %d\n", rc);
3952 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3953 else if (rc == -EACCES)
3954 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3955 else if (rc == -ENOENT)
3956 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3957 else if (rc == -EBADF)
3958 rsp->hdr.Status = STATUS_FILE_CLOSED;
3959 else if (rc == -ENOMEM)
3960 rsp->hdr.Status = STATUS_NO_MEMORY;
3961 else if (rc == -EFAULT)
3962 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3963 if (!rsp->hdr.Status)
3964 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3966 smb2_set_err_rsp(work);
3967 ksmbd_fd_put(work, dir_fp);
3968 ksmbd_revert_fsids(work);
3973 * buffer_check_err() - helper function to check buffer errors
3974 * @reqOutputBufferLength: max buffer length expected in command response
3975 * @rsp: query info response buffer contains output buffer length
3976 * @infoclass_size: query info class response buffer size
3978 * Return: 0 on success, otherwise error
3980 static int buffer_check_err(int reqOutputBufferLength,
3981 struct smb2_query_info_rsp *rsp, int infoclass_size)
3983 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
3984 if (reqOutputBufferLength < infoclass_size) {
3985 pr_err("Invalid Buffer Size Requested\n");
3986 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
3987 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4);
3991 ksmbd_debug(SMB, "Buffer Overflow\n");
3992 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
3993 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4 +
3994 reqOutputBufferLength);
3995 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
4000 static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp)
4002 struct smb2_file_standard_info *sinfo;
4004 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4006 sinfo->AllocationSize = cpu_to_le64(4096);
4007 sinfo->EndOfFile = cpu_to_le64(0);
4008 sinfo->NumberOfLinks = cpu_to_le32(1);
4009 sinfo->DeletePending = 1;
4010 sinfo->Directory = 0;
4011 rsp->OutputBufferLength =
4012 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4013 inc_rfc1001_len(rsp, sizeof(struct smb2_file_standard_info));
4016 static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num)
4018 struct smb2_file_internal_info *file_info;
4020 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4022 /* any unique number */
4023 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
4024 rsp->OutputBufferLength =
4025 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4026 inc_rfc1001_len(rsp, sizeof(struct smb2_file_internal_info));
4029 static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
4030 struct smb2_query_info_req *req,
4031 struct smb2_query_info_rsp *rsp)
4037 * Windows can sometime send query file info request on
4038 * pipe without opening it, checking error condition here
4040 id = le64_to_cpu(req->VolatileFileId);
4041 if (!ksmbd_session_rpc_method(sess, id))
4044 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
4045 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
4047 switch (req->FileInfoClass) {
4048 case FILE_STANDARD_INFORMATION:
4049 get_standard_info_pipe(rsp);
4050 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4051 rsp, FILE_STANDARD_INFORMATION_SIZE);
4053 case FILE_INTERNAL_INFORMATION:
4054 get_internal_info_pipe(rsp, id);
4055 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4056 rsp, FILE_INTERNAL_INFORMATION_SIZE);
4059 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
4060 req->FileInfoClass);
4067 * smb2_get_ea() - handler for smb2 get extended attribute command
4068 * @work: smb work containing query info command buffer
4069 * @fp: ksmbd_file pointer
4070 * @req: get extended attribute request
4071 * @rsp: response buffer pointer
4072 * @rsp_org: base response buffer pointer in case of chained response
4074 * Return: 0 on success, otherwise error
4076 static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
4077 struct smb2_query_info_req *req,
4078 struct smb2_query_info_rsp *rsp, void *rsp_org)
4080 struct smb2_ea_info *eainfo, *prev_eainfo;
4081 char *name, *ptr, *xattr_list = NULL, *buf;
4082 int rc, name_len, value_len, xattr_list_len, idx;
4083 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
4084 struct smb2_ea_info_req *ea_req = NULL;
4086 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
4088 if (!(fp->daccess & FILE_READ_EA_LE)) {
4089 pr_err("Not permitted to read ext attr : 0x%x\n",
4094 path = &fp->filp->f_path;
4095 /* single EA entry is requested with given user.* name */
4096 if (req->InputBufferLength) {
4097 if (le32_to_cpu(req->InputBufferLength) <
4098 sizeof(struct smb2_ea_info_req))
4101 ea_req = (struct smb2_ea_info_req *)req->Buffer;
4103 /* need to send all EAs, if no specific EA is requested*/
4104 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
4106 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4107 le32_to_cpu(req->Flags));
4110 buf_free_len = work->response_sz -
4111 (get_rfc1002_len(rsp_org) + 4) -
4112 sizeof(struct smb2_query_info_rsp);
4114 if (le32_to_cpu(req->OutputBufferLength) < buf_free_len)
4115 buf_free_len = le32_to_cpu(req->OutputBufferLength);
4117 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4119 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4121 } else if (!rc) { /* there is no EA in the file */
4122 ksmbd_debug(SMB, "no ea data in the file\n");
4125 xattr_list_len = rc;
4127 ptr = (char *)rsp->Buffer;
4128 eainfo = (struct smb2_ea_info *)ptr;
4129 prev_eainfo = eainfo;
4132 while (idx < xattr_list_len) {
4133 name = xattr_list + idx;
4134 name_len = strlen(name);
4136 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
4137 idx += name_len + 1;
4140 * CIFS does not support EA other than user.* namespace,
4141 * still keep the framework generic, to list other attrs
4144 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4147 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
4151 if (req->InputBufferLength &&
4152 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4153 ea_req->EaNameLength))
4156 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
4157 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
4160 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4161 name_len -= XATTR_USER_PREFIX_LEN;
4163 ptr = (char *)(&eainfo->name + name_len + 1);
4164 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4166 /* bailout if xattr can't fit in buf_free_len */
4167 value_len = ksmbd_vfs_getxattr(user_ns, path->dentry,
4169 if (value_len <= 0) {
4171 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4175 buf_free_len -= value_len;
4176 if (buf_free_len < 0) {
4181 memcpy(ptr, buf, value_len);
4186 eainfo->EaNameLength = name_len;
4188 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4189 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
4192 memcpy(eainfo->name, name, name_len);
4194 eainfo->name[name_len] = '\0';
4195 eainfo->EaValueLength = cpu_to_le16(value_len);
4196 next_offset = offsetof(struct smb2_ea_info, name) +
4197 name_len + 1 + value_len;
4199 /* align next xattr entry at 4 byte bundary */
4200 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4201 if (alignment_bytes) {
4202 memset(ptr, '\0', alignment_bytes);
4203 ptr += alignment_bytes;
4204 next_offset += alignment_bytes;
4205 buf_free_len -= alignment_bytes;
4207 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4208 prev_eainfo = eainfo;
4209 eainfo = (struct smb2_ea_info *)ptr;
4210 rsp_data_cnt += next_offset;
4212 if (req->InputBufferLength) {
4213 ksmbd_debug(SMB, "single entry requested\n");
4218 /* no more ea entries */
4219 prev_eainfo->NextEntryOffset = 0;
4222 if (rsp_data_cnt == 0)
4223 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4224 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4225 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4231 static void get_file_access_info(struct smb2_query_info_rsp *rsp,
4232 struct ksmbd_file *fp, void *rsp_org)
4234 struct smb2_file_access_info *file_info;
4236 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4237 file_info->AccessFlags = fp->daccess;
4238 rsp->OutputBufferLength =
4239 cpu_to_le32(sizeof(struct smb2_file_access_info));
4240 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4243 static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
4244 struct ksmbd_file *fp, void *rsp_org)
4246 struct smb2_file_basic_info *basic_info;
4250 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4251 pr_err("no right to read the attributes : 0x%x\n",
4256 basic_info = (struct smb2_file_basic_info *)rsp->Buffer;
4257 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4259 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4260 time = ksmbd_UnixTimeToNT(stat.atime);
4261 basic_info->LastAccessTime = cpu_to_le64(time);
4262 time = ksmbd_UnixTimeToNT(stat.mtime);
4263 basic_info->LastWriteTime = cpu_to_le64(time);
4264 time = ksmbd_UnixTimeToNT(stat.ctime);
4265 basic_info->ChangeTime = cpu_to_le64(time);
4266 basic_info->Attributes = fp->f_ci->m_fattr;
4267 basic_info->Pad1 = 0;
4268 rsp->OutputBufferLength =
4269 cpu_to_le32(sizeof(struct smb2_file_basic_info));
4270 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info));
4274 static unsigned long long get_allocation_size(struct inode *inode,
4277 unsigned long long alloc_size = 0;
4279 if (!S_ISDIR(stat->mode)) {
4280 if ((inode->i_blocks << 9) <= stat->size)
4281 alloc_size = stat->size;
4283 alloc_size = inode->i_blocks << 9;
4289 static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
4290 struct ksmbd_file *fp, void *rsp_org)
4292 struct smb2_file_standard_info *sinfo;
4293 unsigned int delete_pending;
4294 struct inode *inode;
4297 inode = file_inode(fp->filp);
4298 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
4300 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4301 delete_pending = ksmbd_inode_pending_delete(fp);
4303 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4304 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4305 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4306 sinfo->DeletePending = delete_pending;
4307 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4308 rsp->OutputBufferLength =
4309 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4310 inc_rfc1001_len(rsp_org,
4311 sizeof(struct smb2_file_standard_info));
4314 static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
4317 struct smb2_file_alignment_info *file_info;
4319 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4320 file_info->AlignmentRequirement = 0;
4321 rsp->OutputBufferLength =
4322 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4323 inc_rfc1001_len(rsp_org,
4324 sizeof(struct smb2_file_alignment_info));
4327 static int get_file_all_info(struct ksmbd_work *work,
4328 struct smb2_query_info_rsp *rsp,
4329 struct ksmbd_file *fp,
4332 struct ksmbd_conn *conn = work->conn;
4333 struct smb2_file_all_info *file_info;
4334 unsigned int delete_pending;
4335 struct inode *inode;
4341 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4342 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
4347 filename = convert_to_nt_pathname(fp->filename);
4351 inode = file_inode(fp->filp);
4352 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
4354 ksmbd_debug(SMB, "filename = %s\n", filename);
4355 delete_pending = ksmbd_inode_pending_delete(fp);
4356 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4358 file_info->CreationTime = cpu_to_le64(fp->create_time);
4359 time = ksmbd_UnixTimeToNT(stat.atime);
4360 file_info->LastAccessTime = cpu_to_le64(time);
4361 time = ksmbd_UnixTimeToNT(stat.mtime);
4362 file_info->LastWriteTime = cpu_to_le64(time);
4363 time = ksmbd_UnixTimeToNT(stat.ctime);
4364 file_info->ChangeTime = cpu_to_le64(time);
4365 file_info->Attributes = fp->f_ci->m_fattr;
4366 file_info->Pad1 = 0;
4367 file_info->AllocationSize =
4368 cpu_to_le64(get_allocation_size(inode, &stat));
4369 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4370 file_info->NumberOfLinks =
4371 cpu_to_le32(get_nlink(&stat) - delete_pending);
4372 file_info->DeletePending = delete_pending;
4373 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4374 file_info->Pad2 = 0;
4375 file_info->IndexNumber = cpu_to_le64(stat.ino);
4376 file_info->EASize = 0;
4377 file_info->AccessFlags = fp->daccess;
4378 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4379 file_info->Mode = fp->coption;
4380 file_info->AlignmentRequirement = 0;
4381 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4382 PATH_MAX, conn->local_nls, 0);
4384 file_info->FileNameLength = cpu_to_le32(conv_len);
4385 rsp->OutputBufferLength =
4386 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4388 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4392 static void get_file_alternate_info(struct ksmbd_work *work,
4393 struct smb2_query_info_rsp *rsp,
4394 struct ksmbd_file *fp,
4397 struct ksmbd_conn *conn = work->conn;
4398 struct smb2_file_alt_name_info *file_info;
4399 struct dentry *dentry = fp->filp->f_path.dentry;
4402 spin_lock(&dentry->d_lock);
4403 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4404 conv_len = ksmbd_extract_shortname(conn,
4405 dentry->d_name.name,
4406 file_info->FileName);
4407 spin_unlock(&dentry->d_lock);
4408 file_info->FileNameLength = cpu_to_le32(conv_len);
4409 rsp->OutputBufferLength =
4410 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4411 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4414 static void get_file_stream_info(struct ksmbd_work *work,
4415 struct smb2_query_info_rsp *rsp,
4416 struct ksmbd_file *fp,
4419 struct ksmbd_conn *conn = work->conn;
4420 struct smb2_file_stream_info *file_info;
4421 char *stream_name, *xattr_list = NULL, *stream_buf;
4423 struct path *path = &fp->filp->f_path;
4424 ssize_t xattr_list_len;
4425 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
4427 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4429 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4431 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4432 if (xattr_list_len < 0) {
4434 } else if (!xattr_list_len) {
4435 ksmbd_debug(SMB, "empty xattr in the file\n");
4439 while (idx < xattr_list_len) {
4440 stream_name = xattr_list + idx;
4441 streamlen = strlen(stream_name);
4442 idx += streamlen + 1;
4444 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4446 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
4447 STREAM_PREFIX, STREAM_PREFIX_LEN))
4450 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4452 streamlen = stream_name_len;
4456 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4460 streamlen = snprintf(stream_buf, streamlen + 1,
4461 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
4463 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
4464 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
4465 stream_buf, streamlen,
4466 conn->local_nls, 0);
4469 file_info->StreamNameLength = cpu_to_le32(streamlen);
4470 file_info->StreamSize = cpu_to_le64(stream_name_len);
4471 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4473 next = sizeof(struct smb2_file_stream_info) + streamlen;
4475 file_info->NextEntryOffset = cpu_to_le32(next);
4478 if (!S_ISDIR(stat.mode)) {
4479 file_info = (struct smb2_file_stream_info *)
4480 &rsp->Buffer[nbytes];
4481 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
4482 "::$DATA", 7, conn->local_nls, 0);
4484 file_info->StreamNameLength = cpu_to_le32(streamlen);
4485 file_info->StreamSize = 0;
4486 file_info->StreamAllocationSize = 0;
4487 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4490 /* last entry offset should be 0 */
4491 file_info->NextEntryOffset = 0;
4495 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4496 inc_rfc1001_len(rsp_org, nbytes);
4499 static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
4500 struct ksmbd_file *fp, void *rsp_org)
4502 struct smb2_file_internal_info *file_info;
4505 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4507 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4508 file_info->IndexNumber = cpu_to_le64(stat.ino);
4509 rsp->OutputBufferLength =
4510 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4511 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4514 static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
4515 struct ksmbd_file *fp, void *rsp_org)
4517 struct smb2_file_ntwrk_info *file_info;
4518 struct inode *inode;
4522 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4523 pr_err("no right to read the attributes : 0x%x\n",
4528 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4530 inode = file_inode(fp->filp);
4531 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
4533 file_info->CreationTime = cpu_to_le64(fp->create_time);
4534 time = ksmbd_UnixTimeToNT(stat.atime);
4535 file_info->LastAccessTime = cpu_to_le64(time);
4536 time = ksmbd_UnixTimeToNT(stat.mtime);
4537 file_info->LastWriteTime = cpu_to_le64(time);
4538 time = ksmbd_UnixTimeToNT(stat.ctime);
4539 file_info->ChangeTime = cpu_to_le64(time);
4540 file_info->Attributes = fp->f_ci->m_fattr;
4541 file_info->AllocationSize =
4542 cpu_to_le64(get_allocation_size(inode, &stat));
4543 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4544 file_info->Reserved = cpu_to_le32(0);
4545 rsp->OutputBufferLength =
4546 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4547 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4551 static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
4553 struct smb2_file_ea_info *file_info;
4555 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4556 file_info->EASize = 0;
4557 rsp->OutputBufferLength =
4558 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4559 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4562 static void get_file_position_info(struct smb2_query_info_rsp *rsp,
4563 struct ksmbd_file *fp, void *rsp_org)
4565 struct smb2_file_pos_info *file_info;
4567 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4568 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4569 rsp->OutputBufferLength =
4570 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4571 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4574 static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
4575 struct ksmbd_file *fp, void *rsp_org)
4577 struct smb2_file_mode_info *file_info;
4579 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4580 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4581 rsp->OutputBufferLength =
4582 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4583 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4586 static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
4587 struct ksmbd_file *fp, void *rsp_org)
4589 struct smb2_file_comp_info *file_info;
4592 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4595 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4596 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4597 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4598 file_info->CompressionUnitShift = 0;
4599 file_info->ChunkShift = 0;
4600 file_info->ClusterShift = 0;
4601 memset(&file_info->Reserved[0], 0, 3);
4603 rsp->OutputBufferLength =
4604 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4605 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4608 static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
4609 struct ksmbd_file *fp, void *rsp_org)
4611 struct smb2_file_attr_tag_info *file_info;
4613 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4614 pr_err("no right to read the attributes : 0x%x\n",
4619 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4620 file_info->FileAttributes = fp->f_ci->m_fattr;
4621 file_info->ReparseTag = 0;
4622 rsp->OutputBufferLength =
4623 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
4624 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
4628 static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
4629 struct ksmbd_file *fp, void *rsp_org)
4631 struct smb311_posix_qinfo *file_info;
4632 struct inode *inode = file_inode(fp->filp);
4635 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4636 file_info->CreationTime = cpu_to_le64(fp->create_time);
4637 time = ksmbd_UnixTimeToNT(inode->i_atime);
4638 file_info->LastAccessTime = cpu_to_le64(time);
4639 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4640 file_info->LastWriteTime = cpu_to_le64(time);
4641 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4642 file_info->ChangeTime = cpu_to_le64(time);
4643 file_info->DosAttributes = fp->f_ci->m_fattr;
4644 file_info->Inode = cpu_to_le64(inode->i_ino);
4645 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4646 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4647 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4648 file_info->Mode = cpu_to_le32(inode->i_mode);
4649 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4650 rsp->OutputBufferLength =
4651 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
4652 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
4656 static int smb2_get_info_file(struct ksmbd_work *work,
4657 struct smb2_query_info_req *req,
4658 struct smb2_query_info_rsp *rsp, void *rsp_org)
4660 struct ksmbd_file *fp;
4661 int fileinfoclass = 0;
4663 int file_infoclass_size;
4664 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4666 if (test_share_config_flag(work->tcon->share_conf,
4667 KSMBD_SHARE_FLAG_PIPE)) {
4668 /* smb2 info file called for pipe */
4669 return smb2_get_info_file_pipe(work->sess, req, rsp);
4672 if (work->next_smb2_rcv_hdr_off) {
4673 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4674 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
4675 work->compound_fid);
4676 id = work->compound_fid;
4677 pid = work->compound_pfid;
4681 if (!has_file_id(id)) {
4682 id = le64_to_cpu(req->VolatileFileId);
4683 pid = le64_to_cpu(req->PersistentFileId);
4686 fp = ksmbd_lookup_fd_slow(work, id, pid);
4690 fileinfoclass = req->FileInfoClass;
4692 switch (fileinfoclass) {
4693 case FILE_ACCESS_INFORMATION:
4694 get_file_access_info(rsp, fp, rsp_org);
4695 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4698 case FILE_BASIC_INFORMATION:
4699 rc = get_file_basic_info(rsp, fp, rsp_org);
4700 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4703 case FILE_STANDARD_INFORMATION:
4704 get_file_standard_info(rsp, fp, rsp_org);
4705 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4708 case FILE_ALIGNMENT_INFORMATION:
4709 get_file_alignment_info(rsp, rsp_org);
4710 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4713 case FILE_ALL_INFORMATION:
4714 rc = get_file_all_info(work, rsp, fp, rsp_org);
4715 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4718 case FILE_ALTERNATE_NAME_INFORMATION:
4719 get_file_alternate_info(work, rsp, fp, rsp_org);
4720 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4723 case FILE_STREAM_INFORMATION:
4724 get_file_stream_info(work, rsp, fp, rsp_org);
4725 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4728 case FILE_INTERNAL_INFORMATION:
4729 get_file_internal_info(rsp, fp, rsp_org);
4730 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4733 case FILE_NETWORK_OPEN_INFORMATION:
4734 rc = get_file_network_open_info(rsp, fp, rsp_org);
4735 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4738 case FILE_EA_INFORMATION:
4739 get_file_ea_info(rsp, rsp_org);
4740 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4743 case FILE_FULL_EA_INFORMATION:
4744 rc = smb2_get_ea(work, fp, req, rsp, rsp_org);
4745 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4748 case FILE_POSITION_INFORMATION:
4749 get_file_position_info(rsp, fp, rsp_org);
4750 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4753 case FILE_MODE_INFORMATION:
4754 get_file_mode_info(rsp, fp, rsp_org);
4755 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4758 case FILE_COMPRESSION_INFORMATION:
4759 get_file_compression_info(rsp, fp, rsp_org);
4760 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4763 case FILE_ATTRIBUTE_TAG_INFORMATION:
4764 rc = get_file_attribute_tag_info(rsp, fp, rsp_org);
4765 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4767 case SMB_FIND_FILE_POSIX_INFO:
4768 if (!work->tcon->posix_extensions) {
4769 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4772 rc = find_file_posix_info(rsp, fp, rsp_org);
4773 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4777 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4782 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4784 file_infoclass_size);
4785 ksmbd_fd_put(work, fp);
4789 static int smb2_get_info_filesystem(struct ksmbd_work *work,
4790 struct smb2_query_info_req *req,
4791 struct smb2_query_info_rsp *rsp, void *rsp_org)
4793 struct ksmbd_session *sess = work->sess;
4794 struct ksmbd_conn *conn = sess->conn;
4795 struct ksmbd_share_config *share = work->tcon->share_conf;
4796 int fsinfoclass = 0;
4797 struct kstatfs stfs;
4800 int fs_infoclass_size = 0;
4802 rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path);
4804 pr_err("cannot create vfs path\n");
4808 rc = vfs_statfs(&path, &stfs);
4810 pr_err("cannot do stat of path %s\n", share->path);
4815 fsinfoclass = req->FileInfoClass;
4817 switch (fsinfoclass) {
4818 case FS_DEVICE_INFORMATION:
4820 struct filesystem_device_info *info;
4822 info = (struct filesystem_device_info *)rsp->Buffer;
4824 info->DeviceType = cpu_to_le32(stfs.f_type);
4825 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4826 rsp->OutputBufferLength = cpu_to_le32(8);
4827 inc_rfc1001_len(rsp_org, 8);
4828 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4831 case FS_ATTRIBUTE_INFORMATION:
4833 struct filesystem_attribute_info *info;
4836 info = (struct filesystem_attribute_info *)rsp->Buffer;
4837 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4838 FILE_PERSISTENT_ACLS |
4839 FILE_UNICODE_ON_DISK |
4840 FILE_CASE_PRESERVED_NAMES |
4841 FILE_CASE_SENSITIVE_SEARCH |
4842 FILE_SUPPORTS_BLOCK_REFCOUNTING);
4844 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4846 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4847 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4848 "NTFS", PATH_MAX, conn->local_nls, 0);
4850 info->FileSystemNameLen = cpu_to_le32(len);
4851 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4852 rsp->OutputBufferLength = cpu_to_le32(sz);
4853 inc_rfc1001_len(rsp_org, sz);
4854 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4857 case FS_VOLUME_INFORMATION:
4859 struct filesystem_vol_info *info;
4862 info = (struct filesystem_vol_info *)(rsp->Buffer);
4863 info->VolumeCreationTime = 0;
4864 /* Taking dummy value of serial number*/
4865 info->SerialNumber = cpu_to_le32(0xbc3ac512);
4866 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4867 share->name, PATH_MAX,
4868 conn->local_nls, 0);
4870 info->VolumeLabelSize = cpu_to_le32(len);
4872 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4873 rsp->OutputBufferLength = cpu_to_le32(sz);
4874 inc_rfc1001_len(rsp_org, sz);
4875 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4878 case FS_SIZE_INFORMATION:
4880 struct filesystem_info *info;
4882 info = (struct filesystem_info *)(rsp->Buffer);
4883 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4884 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
4885 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4886 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
4887 rsp->OutputBufferLength = cpu_to_le32(24);
4888 inc_rfc1001_len(rsp_org, 24);
4889 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4892 case FS_FULL_SIZE_INFORMATION:
4894 struct smb2_fs_full_size_info *info;
4896 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
4897 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4898 info->CallerAvailableAllocationUnits =
4899 cpu_to_le64(stfs.f_bavail);
4900 info->ActualAvailableAllocationUnits =
4901 cpu_to_le64(stfs.f_bfree);
4902 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4903 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
4904 rsp->OutputBufferLength = cpu_to_le32(32);
4905 inc_rfc1001_len(rsp_org, 32);
4906 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4909 case FS_OBJECT_ID_INFORMATION:
4911 struct object_id_info *info;
4913 info = (struct object_id_info *)(rsp->Buffer);
4915 if (!user_guest(sess->user))
4916 memcpy(info->objid, user_passkey(sess->user), 16);
4918 memset(info->objid, 0, 16);
4920 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4921 info->extended_info.version = cpu_to_le32(1);
4922 info->extended_info.release = cpu_to_le32(1);
4923 info->extended_info.rel_date = 0;
4924 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
4925 rsp->OutputBufferLength = cpu_to_le32(64);
4926 inc_rfc1001_len(rsp_org, 64);
4927 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4930 case FS_SECTOR_SIZE_INFORMATION:
4932 struct smb3_fs_ss_info *info;
4934 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
4936 info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize);
4937 info->PhysicalBytesPerSectorForAtomicity =
4938 cpu_to_le32(stfs.f_bsize);
4939 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize);
4940 info->FSEffPhysicalBytesPerSectorForAtomicity =
4941 cpu_to_le32(stfs.f_bsize);
4942 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4943 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
4944 info->ByteOffsetForSectorAlignment = 0;
4945 info->ByteOffsetForPartitionAlignment = 0;
4946 rsp->OutputBufferLength = cpu_to_le32(28);
4947 inc_rfc1001_len(rsp_org, 28);
4948 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
4951 case FS_CONTROL_INFORMATION:
4954 * TODO : The current implementation is based on
4955 * test result with win7(NTFS) server. It's need to
4956 * modify this to get valid Quota values
4959 struct smb2_fs_control_info *info;
4961 info = (struct smb2_fs_control_info *)(rsp->Buffer);
4962 info->FreeSpaceStartFiltering = 0;
4963 info->FreeSpaceThreshold = 0;
4964 info->FreeSpaceStopFiltering = 0;
4965 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
4966 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
4968 rsp->OutputBufferLength = cpu_to_le32(48);
4969 inc_rfc1001_len(rsp_org, 48);
4970 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
4973 case FS_POSIX_INFORMATION:
4975 struct filesystem_posix_info *info;
4977 if (!work->tcon->posix_extensions) {
4978 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4981 info = (struct filesystem_posix_info *)(rsp->Buffer);
4982 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize);
4983 info->BlockSize = cpu_to_le32(stfs.f_bsize);
4984 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
4985 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
4986 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
4987 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
4988 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
4989 rsp->OutputBufferLength = cpu_to_le32(56);
4990 inc_rfc1001_len(rsp_org, 56);
4991 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
4999 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
5006 static int smb2_get_info_sec(struct ksmbd_work *work,
5007 struct smb2_query_info_req *req,
5008 struct smb2_query_info_rsp *rsp, void *rsp_org)
5010 struct ksmbd_file *fp;
5011 struct user_namespace *user_ns;
5012 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
5013 struct smb_fattr fattr = {{0}};
5014 struct inode *inode;
5016 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5017 int addition_info = le32_to_cpu(req->AdditionalInformation);
5020 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
5021 PROTECTED_DACL_SECINFO |
5022 UNPROTECTED_DACL_SECINFO)) {
5023 pr_err("Unsupported addition info: 0x%x)\n",
5026 pntsd->revision = cpu_to_le16(1);
5027 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
5028 pntsd->osidoffset = 0;
5029 pntsd->gsidoffset = 0;
5030 pntsd->sacloffset = 0;
5031 pntsd->dacloffset = 0;
5033 secdesclen = sizeof(struct smb_ntsd);
5034 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
5035 inc_rfc1001_len(rsp_org, secdesclen);
5040 if (work->next_smb2_rcv_hdr_off) {
5041 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5042 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
5043 work->compound_fid);
5044 id = work->compound_fid;
5045 pid = work->compound_pfid;
5049 if (!has_file_id(id)) {
5050 id = le64_to_cpu(req->VolatileFileId);
5051 pid = le64_to_cpu(req->PersistentFileId);
5054 fp = ksmbd_lookup_fd_slow(work, id, pid);
5058 user_ns = file_mnt_user_ns(fp->filp);
5059 inode = file_inode(fp->filp);
5060 ksmbd_acls_fattr(&fattr, user_ns, inode);
5062 if (test_share_config_flag(work->tcon->share_conf,
5063 KSMBD_SHARE_FLAG_ACL_XATTR))
5064 ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
5065 fp->filp->f_path.dentry, &ppntsd);
5067 rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
5068 &secdesclen, &fattr);
5069 posix_acl_release(fattr.cf_acls);
5070 posix_acl_release(fattr.cf_dacls);
5072 ksmbd_fd_put(work, fp);
5076 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
5077 inc_rfc1001_len(rsp_org, secdesclen);
5082 * smb2_query_info() - handler for smb2 query info command
5083 * @work: smb work containing query info request buffer
5085 * Return: 0 on success, otherwise error
5087 int smb2_query_info(struct ksmbd_work *work)
5089 struct smb2_query_info_req *req;
5090 struct smb2_query_info_rsp *rsp, *rsp_org;
5093 rsp_org = work->response_buf;
5094 WORK_BUFFERS(work, req, rsp);
5096 ksmbd_debug(SMB, "GOT query info request\n");
5098 switch (req->InfoType) {
5099 case SMB2_O_INFO_FILE:
5100 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5101 rc = smb2_get_info_file(work, req, rsp, (void *)rsp_org);
5103 case SMB2_O_INFO_FILESYSTEM:
5104 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
5105 rc = smb2_get_info_filesystem(work, req, rsp, (void *)rsp_org);
5107 case SMB2_O_INFO_SECURITY:
5108 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5109 rc = smb2_get_info_sec(work, req, rsp, (void *)rsp_org);
5112 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
5119 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5120 else if (rc == -ENOENT)
5121 rsp->hdr.Status = STATUS_FILE_CLOSED;
5122 else if (rc == -EIO)
5123 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5124 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
5125 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5126 smb2_set_err_rsp(work);
5128 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
5132 rsp->StructureSize = cpu_to_le16(9);
5133 rsp->OutputBufferOffset = cpu_to_le16(72);
5134 inc_rfc1001_len(rsp_org, 8);
5139 * smb2_close_pipe() - handler for closing IPC pipe
5140 * @work: smb work containing close request buffer
5144 static noinline int smb2_close_pipe(struct ksmbd_work *work)
5147 struct smb2_close_req *req = work->request_buf;
5148 struct smb2_close_rsp *rsp = work->response_buf;
5150 id = le64_to_cpu(req->VolatileFileId);
5151 ksmbd_session_rpc_close(work->sess, id);
5153 rsp->StructureSize = cpu_to_le16(60);
5156 rsp->CreationTime = 0;
5157 rsp->LastAccessTime = 0;
5158 rsp->LastWriteTime = 0;
5159 rsp->ChangeTime = 0;
5160 rsp->AllocationSize = 0;
5162 rsp->Attributes = 0;
5163 inc_rfc1001_len(rsp, 60);
5168 * smb2_close() - handler for smb2 close file command
5169 * @work: smb work containing close request buffer
5173 int smb2_close(struct ksmbd_work *work)
5175 u64 volatile_id = KSMBD_NO_FID;
5177 struct smb2_close_req *req;
5178 struct smb2_close_rsp *rsp;
5179 struct smb2_close_rsp *rsp_org;
5180 struct ksmbd_conn *conn = work->conn;
5181 struct ksmbd_file *fp;
5182 struct inode *inode;
5186 rsp_org = work->response_buf;
5187 WORK_BUFFERS(work, req, rsp);
5189 if (test_share_config_flag(work->tcon->share_conf,
5190 KSMBD_SHARE_FLAG_PIPE)) {
5191 ksmbd_debug(SMB, "IPC pipe close request\n");
5192 return smb2_close_pipe(work);
5195 sess_id = le64_to_cpu(req->hdr.SessionId);
5196 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5197 sess_id = work->compound_sid;
5199 work->compound_sid = 0;
5200 if (check_session_id(conn, sess_id)) {
5201 work->compound_sid = sess_id;
5203 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5204 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5205 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5210 if (work->next_smb2_rcv_hdr_off &&
5211 !has_file_id(le64_to_cpu(req->VolatileFileId))) {
5212 if (!has_file_id(work->compound_fid)) {
5213 /* file already closed, return FILE_CLOSED */
5214 ksmbd_debug(SMB, "file already closed\n");
5215 rsp->hdr.Status = STATUS_FILE_CLOSED;
5220 "Compound request set FID = %llu:%llu\n",
5222 work->compound_pfid);
5223 volatile_id = work->compound_fid;
5225 /* file closed, stored id is not valid anymore */
5226 work->compound_fid = KSMBD_NO_FID;
5227 work->compound_pfid = KSMBD_NO_FID;
5230 volatile_id = le64_to_cpu(req->VolatileFileId);
5232 ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id);
5234 rsp->StructureSize = cpu_to_le16(60);
5237 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5238 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5244 inode = file_inode(fp->filp);
5245 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5246 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5247 cpu_to_le64(inode->i_blocks << 9);
5248 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5249 rsp->Attributes = fp->f_ci->m_fattr;
5250 rsp->CreationTime = cpu_to_le64(fp->create_time);
5251 time = ksmbd_UnixTimeToNT(inode->i_atime);
5252 rsp->LastAccessTime = cpu_to_le64(time);
5253 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5254 rsp->LastWriteTime = cpu_to_le64(time);
5255 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5256 rsp->ChangeTime = cpu_to_le64(time);
5257 ksmbd_fd_put(work, fp);
5260 rsp->AllocationSize = 0;
5262 rsp->Attributes = 0;
5263 rsp->CreationTime = 0;
5264 rsp->LastAccessTime = 0;
5265 rsp->LastWriteTime = 0;
5266 rsp->ChangeTime = 0;
5269 err = ksmbd_close_fd(work, volatile_id);
5272 if (rsp->hdr.Status == 0)
5273 rsp->hdr.Status = STATUS_FILE_CLOSED;
5274 smb2_set_err_rsp(work);
5276 inc_rfc1001_len(rsp_org, 60);
5283 * smb2_echo() - handler for smb2 echo(ping) command
5284 * @work: smb work containing echo request buffer
5288 int smb2_echo(struct ksmbd_work *work)
5290 struct smb2_echo_rsp *rsp = work->response_buf;
5292 rsp->StructureSize = cpu_to_le16(4);
5294 inc_rfc1001_len(rsp, 4);
5298 static int smb2_rename(struct ksmbd_work *work,
5299 struct ksmbd_file *fp,
5300 struct user_namespace *user_ns,
5301 struct smb2_file_rename_info *file_info,
5302 struct nls_table *local_nls)
5304 struct ksmbd_share_config *share = fp->tcon->share_conf;
5305 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5306 char *pathname = NULL;
5308 bool file_present = true;
5311 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5312 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5316 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5317 if (IS_ERR(abs_oldname)) {
5321 old_name = strrchr(abs_oldname, '/');
5322 if (old_name && old_name[1] != '\0') {
5325 ksmbd_debug(SMB, "can't get last component in path %s\n",
5331 new_name = smb2_get_name(share,
5332 file_info->FileName,
5333 le32_to_cpu(file_info->FileNameLength),
5335 if (IS_ERR(new_name)) {
5336 rc = PTR_ERR(new_name);
5340 if (strchr(new_name, ':')) {
5342 char *xattr_stream_name, *stream_name = NULL;
5343 size_t xattr_stream_size;
5346 rc = parse_stream_name(new_name, &stream_name, &s_type);
5350 len = strlen(new_name);
5351 if (len > 0 && new_name[len - 1] != '/') {
5352 pr_err("not allow base filename in rename\n");
5357 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5364 rc = ksmbd_vfs_setxattr(user_ns,
5365 fp->filp->f_path.dentry,
5369 pr_err("failed to store stream name in xattr: %d\n",
5378 ksmbd_debug(SMB, "new name %s\n", new_name);
5379 rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1);
5383 file_present = false;
5388 if (ksmbd_share_veto_filename(share, new_name)) {
5390 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5394 if (file_info->ReplaceIfExists) {
5396 rc = ksmbd_vfs_remove_file(work, new_name);
5398 if (rc != -ENOTEMPTY)
5400 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
5407 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
5410 "cannot rename already existing file\n");
5415 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5418 if (!IS_ERR(new_name))
5423 static int smb2_create_link(struct ksmbd_work *work,
5424 struct ksmbd_share_config *share,
5425 struct smb2_file_link_info *file_info,
5426 unsigned int buf_len, struct file *filp,
5427 struct nls_table *local_nls)
5429 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5431 bool file_present = true;
5434 if (buf_len < (u64)sizeof(struct smb2_file_link_info) +
5435 le32_to_cpu(file_info->FileNameLength))
5438 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5439 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5443 link_name = smb2_get_name(share,
5444 file_info->FileName,
5445 le32_to_cpu(file_info->FileNameLength),
5447 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5452 ksmbd_debug(SMB, "link name is %s\n", link_name);
5453 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5454 if (IS_ERR(target_name)) {
5459 ksmbd_debug(SMB, "target name is %s\n", target_name);
5460 rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0);
5464 file_present = false;
5469 if (file_info->ReplaceIfExists) {
5471 rc = ksmbd_vfs_remove_file(work, link_name);
5474 ksmbd_debug(SMB, "cannot delete %s\n",
5482 ksmbd_debug(SMB, "link already exists\n");
5487 rc = ksmbd_vfs_link(work, target_name, link_name);
5491 if (!IS_ERR(link_name))
5497 static int set_file_basic_info(struct ksmbd_file *fp,
5498 struct smb2_file_basic_info *file_info,
5499 struct ksmbd_share_config *share)
5502 struct timespec64 ctime;
5504 struct inode *inode;
5505 struct user_namespace *user_ns;
5508 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
5513 inode = file_inode(filp);
5514 user_ns = file_mnt_user_ns(filp);
5516 if (file_info->CreationTime)
5517 fp->create_time = le64_to_cpu(file_info->CreationTime);
5519 if (file_info->LastAccessTime) {
5520 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5521 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5524 if (file_info->ChangeTime) {
5525 attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
5526 ctime = attrs.ia_ctime;
5527 attrs.ia_valid |= ATTR_CTIME;
5529 ctime = inode->i_ctime;
5532 if (file_info->LastWriteTime) {
5533 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5534 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5537 if (file_info->Attributes) {
5538 if (!S_ISDIR(inode->i_mode) &&
5539 file_info->Attributes & ATTR_DIRECTORY_LE) {
5540 pr_err("can't change a file to a directory\n");
5544 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE))
5545 fp->f_ci->m_fattr = file_info->Attributes |
5546 (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE);
5549 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5550 (file_info->CreationTime || file_info->Attributes)) {
5551 struct xattr_dos_attrib da = {0};
5554 da.itime = fp->itime;
5555 da.create_time = fp->create_time;
5556 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5557 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5558 XATTR_DOSINFO_ITIME;
5560 rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
5561 filp->f_path.dentry, &da);
5564 "failed to restore file attribute in EA\n");
5568 if (attrs.ia_valid) {
5569 struct dentry *dentry = filp->f_path.dentry;
5570 struct inode *inode = d_inode(dentry);
5572 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5576 rc = notify_change(user_ns, dentry, &attrs, NULL);
5578 inode->i_ctime = ctime;
5579 mark_inode_dirty(inode);
5581 inode_unlock(inode);
5586 static int set_file_allocation_info(struct ksmbd_work *work,
5587 struct ksmbd_file *fp,
5588 struct smb2_file_alloc_info *file_alloc_info)
5591 * TODO : It's working fine only when store dos attributes
5592 * is not yes. need to implement a logic which works
5593 * properly with any smb.conf option
5597 struct inode *inode;
5600 if (!(fp->daccess & FILE_WRITE_DATA_LE))
5603 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5604 inode = file_inode(fp->filp);
5606 if (alloc_blks > inode->i_blocks) {
5607 smb_break_all_levII_oplock(work, fp, 1);
5608 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
5610 if (rc && rc != -EOPNOTSUPP) {
5611 pr_err("vfs_fallocate is failed : %d\n", rc);
5614 } else if (alloc_blks < inode->i_blocks) {
5618 * Allocation size could be smaller than original one
5619 * which means allocated blocks in file should be
5620 * deallocated. use truncate to cut out it, but inode
5621 * size is also updated with truncate offset.
5622 * inode size is retained by backup inode size.
5624 size = i_size_read(inode);
5625 rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512);
5627 pr_err("truncate failed! filename : %s, err %d\n",
5631 if (size < alloc_blks * 512)
5632 i_size_write(inode, size);
5637 static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
5638 struct smb2_file_eof_info *file_eof_info)
5641 struct inode *inode;
5644 if (!(fp->daccess & FILE_WRITE_DATA_LE))
5647 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5648 inode = file_inode(fp->filp);
5651 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5652 * on FAT32 shared device, truncate execution time is too long
5653 * and network error could cause from windows client. because
5654 * truncate of some filesystem like FAT32 fill zero data in
5657 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5658 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
5659 fp->filename, newsize);
5660 rc = ksmbd_vfs_truncate(work, fp, newsize);
5662 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
5672 static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
5673 struct smb2_file_rename_info *rename_info,
5674 unsigned int buf_len)
5676 struct user_namespace *user_ns;
5677 struct ksmbd_file *parent_fp;
5678 struct dentry *parent;
5679 struct dentry *dentry = fp->filp->f_path.dentry;
5682 if (!(fp->daccess & FILE_DELETE_LE)) {
5683 pr_err("no right to delete : 0x%x\n", fp->daccess);
5687 if (buf_len < (u64)sizeof(struct smb2_file_rename_info) +
5688 le32_to_cpu(rename_info->FileNameLength))
5691 user_ns = file_mnt_user_ns(fp->filp);
5692 if (ksmbd_stream_fd(fp))
5695 parent = dget_parent(dentry);
5696 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
5702 parent_fp = ksmbd_lookup_fd_inode(d_inode(parent));
5703 inode_unlock(d_inode(parent));
5707 if (parent_fp->daccess & FILE_DELETE_LE) {
5708 pr_err("parent dir is opened with delete access\n");
5713 return smb2_rename(work, fp, user_ns, rename_info,
5714 work->sess->conn->local_nls);
5717 static int set_file_disposition_info(struct ksmbd_file *fp,
5718 struct smb2_file_disposition_info *file_info)
5720 struct inode *inode;
5722 if (!(fp->daccess & FILE_DELETE_LE)) {
5723 pr_err("no right to delete : 0x%x\n", fp->daccess);
5727 inode = file_inode(fp->filp);
5728 if (file_info->DeletePending) {
5729 if (S_ISDIR(inode->i_mode) &&
5730 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
5732 ksmbd_set_inode_pending_delete(fp);
5734 ksmbd_clear_inode_pending_delete(fp);
5739 static int set_file_position_info(struct ksmbd_file *fp,
5740 struct smb2_file_pos_info *file_info)
5742 loff_t current_byte_offset;
5743 unsigned long sector_size;
5744 struct inode *inode;
5746 inode = file_inode(fp->filp);
5747 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
5748 sector_size = inode->i_sb->s_blocksize;
5750 if (current_byte_offset < 0 ||
5751 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5752 current_byte_offset & (sector_size - 1))) {
5753 pr_err("CurrentByteOffset is not valid : %llu\n",
5754 current_byte_offset);
5758 fp->filp->f_pos = current_byte_offset;
5762 static int set_file_mode_info(struct ksmbd_file *fp,
5763 struct smb2_file_mode_info *file_info)
5767 mode = file_info->Mode;
5769 if ((mode & ~FILE_MODE_INFO_MASK) ||
5770 (mode & FILE_SYNCHRONOUS_IO_ALERT_LE &&
5771 mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) {
5772 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
5777 * TODO : need to implement consideration for
5778 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5780 ksmbd_vfs_set_fadvise(fp->filp, mode);
5786 * smb2_set_info_file() - handler for smb2 set info command
5787 * @work: smb work containing set info command buffer
5788 * @fp: ksmbd_file pointer
5789 * @info_class: smb2 set info class
5790 * @share: ksmbd_share_config pointer
5792 * Return: 0 on success, otherwise error
5793 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5795 static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
5796 struct smb2_set_info_req *req,
5797 struct ksmbd_share_config *share)
5799 unsigned int buf_len = le32_to_cpu(req->BufferLength);
5801 switch (req->FileInfoClass) {
5802 case FILE_BASIC_INFORMATION:
5804 if (buf_len < sizeof(struct smb2_file_basic_info))
5807 return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share);
5809 case FILE_ALLOCATION_INFORMATION:
5811 if (buf_len < sizeof(struct smb2_file_alloc_info))
5814 return set_file_allocation_info(work, fp,
5815 (struct smb2_file_alloc_info *)req->Buffer);
5817 case FILE_END_OF_FILE_INFORMATION:
5819 if (buf_len < sizeof(struct smb2_file_eof_info))
5822 return set_end_of_file_info(work, fp,
5823 (struct smb2_file_eof_info *)req->Buffer);
5825 case FILE_RENAME_INFORMATION:
5827 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
5829 "User does not have write permission\n");
5833 if (buf_len < sizeof(struct smb2_file_rename_info))
5836 return set_rename_info(work, fp,
5837 (struct smb2_file_rename_info *)req->Buffer,
5840 case FILE_LINK_INFORMATION:
5842 if (buf_len < sizeof(struct smb2_file_link_info))
5845 return smb2_create_link(work, work->tcon->share_conf,
5846 (struct smb2_file_link_info *)req->Buffer,
5848 work->sess->conn->local_nls);
5850 case FILE_DISPOSITION_INFORMATION:
5852 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
5854 "User does not have write permission\n");
5858 if (buf_len < sizeof(struct smb2_file_disposition_info))
5861 return set_file_disposition_info(fp,
5862 (struct smb2_file_disposition_info *)req->Buffer);
5864 case FILE_FULL_EA_INFORMATION:
5866 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
5867 pr_err("Not permitted to write ext attr: 0x%x\n",
5872 if (buf_len < sizeof(struct smb2_ea_info))
5875 return smb2_set_ea((struct smb2_ea_info *)req->Buffer,
5876 buf_len, &fp->filp->f_path);
5878 case FILE_POSITION_INFORMATION:
5880 if (buf_len < sizeof(struct smb2_file_pos_info))
5883 return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer);
5885 case FILE_MODE_INFORMATION:
5887 if (buf_len < sizeof(struct smb2_file_mode_info))
5890 return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer);
5894 pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass);
5898 static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
5899 char *buffer, int buf_len)
5901 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5903 fp->saccess |= FILE_SHARE_DELETE_LE;
5905 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
5910 * smb2_set_info() - handler for smb2 set info command handler
5911 * @work: smb work containing set info request buffer
5913 * Return: 0 on success, otherwise error
5915 int smb2_set_info(struct ksmbd_work *work)
5917 struct smb2_set_info_req *req;
5918 struct smb2_set_info_rsp *rsp, *rsp_org;
5919 struct ksmbd_file *fp;
5921 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5923 ksmbd_debug(SMB, "Received set info request\n");
5925 rsp_org = work->response_buf;
5926 if (work->next_smb2_rcv_hdr_off) {
5927 req = ksmbd_req_buf_next(work);
5928 rsp = ksmbd_resp_buf_next(work);
5929 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5930 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
5931 work->compound_fid);
5932 id = work->compound_fid;
5933 pid = work->compound_pfid;
5936 req = work->request_buf;
5937 rsp = work->response_buf;
5940 if (!has_file_id(id)) {
5941 id = le64_to_cpu(req->VolatileFileId);
5942 pid = le64_to_cpu(req->PersistentFileId);
5945 fp = ksmbd_lookup_fd_slow(work, id, pid);
5947 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5952 switch (req->InfoType) {
5953 case SMB2_O_INFO_FILE:
5954 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5955 rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf);
5957 case SMB2_O_INFO_SECURITY:
5958 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5959 if (ksmbd_override_fsids(work)) {
5963 rc = smb2_set_info_sec(fp,
5964 le32_to_cpu(req->AdditionalInformation),
5966 le32_to_cpu(req->BufferLength));
5967 ksmbd_revert_fsids(work);
5976 rsp->StructureSize = cpu_to_le16(2);
5977 inc_rfc1001_len(rsp_org, 2);
5978 ksmbd_fd_put(work, fp);
5982 if (rc == -EACCES || rc == -EPERM || rc == -EXDEV)
5983 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5984 else if (rc == -EINVAL)
5985 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5986 else if (rc == -ESHARE)
5987 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
5988 else if (rc == -ENOENT)
5989 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
5990 else if (rc == -EBUSY || rc == -ENOTEMPTY)
5991 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
5992 else if (rc == -EAGAIN)
5993 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
5994 else if (rc == -EBADF || rc == -ESTALE)
5995 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5996 else if (rc == -EEXIST)
5997 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
5998 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
5999 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
6000 smb2_set_err_rsp(work);
6001 ksmbd_fd_put(work, fp);
6002 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
6007 * smb2_read_pipe() - handler for smb2 read from IPC pipe
6008 * @work: smb work containing read IPC pipe command buffer
6010 * Return: 0 on success, otherwise error
6012 static noinline int smb2_read_pipe(struct ksmbd_work *work)
6014 int nbytes = 0, err;
6016 struct ksmbd_rpc_command *rpc_resp;
6017 struct smb2_read_req *req = work->request_buf;
6018 struct smb2_read_rsp *rsp = work->response_buf;
6020 id = le64_to_cpu(req->VolatileFileId);
6022 inc_rfc1001_len(rsp, 16);
6023 rpc_resp = ksmbd_rpc_read(work->sess, id);
6025 if (rpc_resp->flags != KSMBD_RPC_OK) {
6030 work->aux_payload_buf =
6031 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
6032 if (!work->aux_payload_buf) {
6037 memcpy(work->aux_payload_buf, rpc_resp->payload,
6038 rpc_resp->payload_sz);
6040 nbytes = rpc_resp->payload_sz;
6041 work->resp_hdr_sz = get_rfc1002_len(rsp) + 4;
6042 work->aux_payload_sz = nbytes;
6046 rsp->StructureSize = cpu_to_le16(17);
6047 rsp->DataOffset = 80;
6049 rsp->DataLength = cpu_to_le32(nbytes);
6050 rsp->DataRemaining = 0;
6052 inc_rfc1001_len(rsp, nbytes);
6056 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6057 smb2_set_err_rsp(work);
6062 static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
6063 struct smb2_read_req *req, void *data_buf,
6066 struct smb2_buffer_desc_v1 *desc =
6067 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6070 if (work->conn->dialect == SMB30_PROT_ID &&
6071 req->Channel != SMB2_CHANNEL_RDMA_V1)
6074 if (req->ReadChannelInfoOffset == 0 ||
6075 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
6078 work->need_invalidate_rkey =
6079 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6080 work->remote_key = le32_to_cpu(desc->token);
6082 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
6083 le32_to_cpu(desc->token),
6084 le64_to_cpu(desc->offset),
6085 le32_to_cpu(desc->length));
6093 * smb2_read() - handler for smb2 read from file
6094 * @work: smb work containing read command buffer
6096 * Return: 0 on success, otherwise error
6098 int smb2_read(struct ksmbd_work *work)
6100 struct ksmbd_conn *conn = work->conn;
6101 struct smb2_read_req *req;
6102 struct smb2_read_rsp *rsp, *rsp_org;
6103 struct ksmbd_file *fp;
6105 size_t length, mincount;
6106 ssize_t nbytes = 0, remain_bytes = 0;
6109 rsp_org = work->response_buf;
6110 WORK_BUFFERS(work, req, rsp);
6112 if (test_share_config_flag(work->tcon->share_conf,
6113 KSMBD_SHARE_FLAG_PIPE)) {
6114 ksmbd_debug(SMB, "IPC pipe read request\n");
6115 return smb2_read_pipe(work);
6118 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
6119 le64_to_cpu(req->PersistentFileId));
6125 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
6126 pr_err("Not permitted to read : 0x%x\n", fp->daccess);
6131 offset = le64_to_cpu(req->Offset);
6132 length = le32_to_cpu(req->Length);
6133 mincount = le32_to_cpu(req->MinimumCount);
6135 if (length > conn->vals->max_read_size) {
6136 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
6137 conn->vals->max_read_size);
6142 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6143 fp->filp->f_path.dentry, offset, length);
6145 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
6146 if (!work->aux_payload_buf) {
6151 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
6157 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
6158 kvfree(work->aux_payload_buf);
6159 work->aux_payload_buf = NULL;
6160 rsp->hdr.Status = STATUS_END_OF_FILE;
6161 smb2_set_err_rsp(work);
6162 ksmbd_fd_put(work, fp);
6166 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
6167 nbytes, offset, mincount);
6169 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
6170 req->Channel == SMB2_CHANNEL_RDMA_V1) {
6171 /* write data to the client using rdma channel */
6172 remain_bytes = smb2_read_rdma_channel(work, req,
6173 work->aux_payload_buf,
6175 kvfree(work->aux_payload_buf);
6176 work->aux_payload_buf = NULL;
6179 if (remain_bytes < 0) {
6180 err = (int)remain_bytes;
6185 rsp->StructureSize = cpu_to_le16(17);
6186 rsp->DataOffset = 80;
6188 rsp->DataLength = cpu_to_le32(nbytes);
6189 rsp->DataRemaining = cpu_to_le32(remain_bytes);
6191 inc_rfc1001_len(rsp_org, 16);
6192 work->resp_hdr_sz = get_rfc1002_len(rsp_org) + 4;
6193 work->aux_payload_sz = nbytes;
6194 inc_rfc1001_len(rsp_org, nbytes);
6195 ksmbd_fd_put(work, fp);
6201 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6202 else if (err == -EAGAIN)
6203 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6204 else if (err == -ENOENT)
6205 rsp->hdr.Status = STATUS_FILE_CLOSED;
6206 else if (err == -EACCES)
6207 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6208 else if (err == -ESHARE)
6209 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6210 else if (err == -EINVAL)
6211 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6213 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6215 smb2_set_err_rsp(work);
6217 ksmbd_fd_put(work, fp);
6222 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6223 * @work: smb work containing write IPC pipe command buffer
6225 * Return: 0 on success, otherwise error
6227 static noinline int smb2_write_pipe(struct ksmbd_work *work)
6229 struct smb2_write_req *req = work->request_buf;
6230 struct smb2_write_rsp *rsp = work->response_buf;
6231 struct ksmbd_rpc_command *rpc_resp;
6233 int err = 0, ret = 0;
6237 length = le32_to_cpu(req->Length);
6238 id = le64_to_cpu(req->VolatileFileId);
6240 if (le16_to_cpu(req->DataOffset) ==
6241 (offsetof(struct smb2_write_req, Buffer) - 4)) {
6242 data_buf = (char *)&req->Buffer[0];
6244 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
6245 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
6246 pr_err("invalid write data offset %u, smb_len %u\n",
6247 le16_to_cpu(req->DataOffset),
6248 get_rfc1002_len(req));
6253 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6254 le16_to_cpu(req->DataOffset));
6257 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6259 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6260 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
6262 smb2_set_err_rsp(work);
6265 if (rpc_resp->flags != KSMBD_RPC_OK) {
6266 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6267 smb2_set_err_rsp(work);
6274 rsp->StructureSize = cpu_to_le16(17);
6275 rsp->DataOffset = 0;
6277 rsp->DataLength = cpu_to_le32(length);
6278 rsp->DataRemaining = 0;
6280 inc_rfc1001_len(rsp, 16);
6284 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6285 smb2_set_err_rsp(work);
6291 static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
6292 struct smb2_write_req *req,
6293 struct ksmbd_file *fp,
6294 loff_t offset, size_t length, bool sync)
6296 struct smb2_buffer_desc_v1 *desc;
6301 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6303 if (work->conn->dialect == SMB30_PROT_ID &&
6304 req->Channel != SMB2_CHANNEL_RDMA_V1)
6307 if (req->Length != 0 || req->DataOffset != 0)
6310 if (req->WriteChannelInfoOffset == 0 ||
6311 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
6314 work->need_invalidate_rkey =
6315 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6316 work->remote_key = le32_to_cpu(desc->token);
6318 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
6322 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
6323 le32_to_cpu(desc->token),
6324 le64_to_cpu(desc->offset),
6325 le32_to_cpu(desc->length));
6331 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
6340 * smb2_write() - handler for smb2 write from file
6341 * @work: smb work containing write command buffer
6343 * Return: 0 on success, otherwise error
6345 int smb2_write(struct ksmbd_work *work)
6347 struct smb2_write_req *req;
6348 struct smb2_write_rsp *rsp, *rsp_org;
6349 struct ksmbd_file *fp = NULL;
6354 bool writethrough = false;
6357 rsp_org = work->response_buf;
6358 WORK_BUFFERS(work, req, rsp);
6360 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
6361 ksmbd_debug(SMB, "IPC pipe write request\n");
6362 return smb2_write_pipe(work);
6365 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6366 ksmbd_debug(SMB, "User does not have write permission\n");
6371 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
6372 le64_to_cpu(req->PersistentFileId));
6378 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
6379 pr_err("Not permitted to write : 0x%x\n", fp->daccess);
6384 offset = le64_to_cpu(req->Offset);
6385 length = le32_to_cpu(req->Length);
6387 if (length > work->conn->vals->max_write_size) {
6388 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6389 work->conn->vals->max_write_size);
6394 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6395 writethrough = true;
6397 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
6398 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
6399 if (le16_to_cpu(req->DataOffset) ==
6400 (offsetof(struct smb2_write_req, Buffer) - 4)) {
6401 data_buf = (char *)&req->Buffer[0];
6403 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
6404 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
6405 pr_err("invalid write data offset %u, smb_len %u\n",
6406 le16_to_cpu(req->DataOffset),
6407 get_rfc1002_len(req));
6412 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6413 le16_to_cpu(req->DataOffset));
6416 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6417 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6418 writethrough = true;
6420 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6421 fp->filp->f_path.dentry, offset, length);
6422 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6423 writethrough, &nbytes);
6427 /* read data from the client using rdma channel, and
6430 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
6431 le32_to_cpu(req->RemainingBytes),
6439 rsp->StructureSize = cpu_to_le16(17);
6440 rsp->DataOffset = 0;
6442 rsp->DataLength = cpu_to_le32(nbytes);
6443 rsp->DataRemaining = 0;
6445 inc_rfc1001_len(rsp_org, 16);
6446 ksmbd_fd_put(work, fp);
6451 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6452 else if (err == -ENOSPC || err == -EFBIG)
6453 rsp->hdr.Status = STATUS_DISK_FULL;
6454 else if (err == -ENOENT)
6455 rsp->hdr.Status = STATUS_FILE_CLOSED;
6456 else if (err == -EACCES)
6457 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6458 else if (err == -ESHARE)
6459 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6460 else if (err == -EINVAL)
6461 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6463 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6465 smb2_set_err_rsp(work);
6466 ksmbd_fd_put(work, fp);
6471 * smb2_flush() - handler for smb2 flush file - fsync
6472 * @work: smb work containing flush command buffer
6474 * Return: 0 on success, otherwise error
6476 int smb2_flush(struct ksmbd_work *work)
6478 struct smb2_flush_req *req;
6479 struct smb2_flush_rsp *rsp, *rsp_org;
6482 rsp_org = work->response_buf;
6483 WORK_BUFFERS(work, req, rsp);
6485 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
6486 le64_to_cpu(req->VolatileFileId));
6488 err = ksmbd_vfs_fsync(work,
6489 le64_to_cpu(req->VolatileFileId),
6490 le64_to_cpu(req->PersistentFileId));
6494 rsp->StructureSize = cpu_to_le16(4);
6496 inc_rfc1001_len(rsp_org, 4);
6501 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6502 smb2_set_err_rsp(work);
6509 * smb2_cancel() - handler for smb2 cancel command
6510 * @work: smb work containing cancel command buffer
6512 * Return: 0 on success, otherwise error
6514 int smb2_cancel(struct ksmbd_work *work)
6516 struct ksmbd_conn *conn = work->conn;
6517 struct smb2_hdr *hdr = work->request_buf;
6518 struct smb2_hdr *chdr;
6519 struct ksmbd_work *cancel_work = NULL;
6521 struct list_head *command_list;
6523 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
6524 hdr->MessageId, hdr->Flags);
6526 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6527 command_list = &conn->async_requests;
6529 spin_lock(&conn->request_lock);
6530 list_for_each_entry(cancel_work, command_list,
6531 async_request_entry) {
6532 chdr = cancel_work->request_buf;
6534 if (cancel_work->async_id !=
6535 le64_to_cpu(hdr->Id.AsyncId))
6539 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6540 le64_to_cpu(hdr->Id.AsyncId),
6541 le16_to_cpu(chdr->Command));
6545 spin_unlock(&conn->request_lock);
6547 command_list = &conn->requests;
6549 spin_lock(&conn->request_lock);
6550 list_for_each_entry(cancel_work, command_list, request_entry) {
6551 chdr = cancel_work->request_buf;
6553 if (chdr->MessageId != hdr->MessageId ||
6554 cancel_work == work)
6558 "smb2 with mid %llu cancelled command = 0x%x\n",
6559 le64_to_cpu(hdr->MessageId),
6560 le16_to_cpu(chdr->Command));
6564 spin_unlock(&conn->request_lock);
6568 cancel_work->state = KSMBD_WORK_CANCELLED;
6569 if (cancel_work->cancel_fn)
6570 cancel_work->cancel_fn(cancel_work->cancel_argv);
6573 /* For SMB2_CANCEL command itself send no response*/
6574 work->send_no_response = 1;
6578 struct file_lock *smb_flock_init(struct file *f)
6580 struct file_lock *fl;
6582 fl = locks_alloc_lock();
6586 locks_init_lock(fl);
6589 fl->fl_pid = current->tgid;
6591 fl->fl_flags = FL_POSIX;
6593 fl->fl_lmops = NULL;
6599 static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6603 /* Checking for wrong flag combination during lock request*/
6605 case SMB2_LOCKFLAG_SHARED:
6606 ksmbd_debug(SMB, "received shared request\n");
6608 flock->fl_type = F_RDLCK;
6609 flock->fl_flags |= FL_SLEEP;
6611 case SMB2_LOCKFLAG_EXCLUSIVE:
6612 ksmbd_debug(SMB, "received exclusive request\n");
6614 flock->fl_type = F_WRLCK;
6615 flock->fl_flags |= FL_SLEEP;
6617 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
6619 "received shared & fail immediately request\n");
6621 flock->fl_type = F_RDLCK;
6623 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
6625 "received exclusive & fail immediately request\n");
6627 flock->fl_type = F_WRLCK;
6629 case SMB2_LOCKFLAG_UNLOCK:
6630 ksmbd_debug(SMB, "received unlock request\n");
6631 flock->fl_type = F_UNLCK;
6639 static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
6640 unsigned int cmd, int flags,
6641 struct list_head *lock_list)
6643 struct ksmbd_lock *lock;
6645 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6651 lock->start = flock->fl_start;
6652 lock->end = flock->fl_end;
6653 lock->flags = flags;
6654 if (lock->start == lock->end)
6656 INIT_LIST_HEAD(&lock->clist);
6657 INIT_LIST_HEAD(&lock->flist);
6658 INIT_LIST_HEAD(&lock->llist);
6659 list_add_tail(&lock->llist, lock_list);
6664 static void smb2_remove_blocked_lock(void **argv)
6666 struct file_lock *flock = (struct file_lock *)argv[0];
6668 ksmbd_vfs_posix_lock_unblock(flock);
6669 wake_up(&flock->fl_wait);
6672 static inline bool lock_defer_pending(struct file_lock *fl)
6674 /* check pending lock waiters */
6675 return waitqueue_active(&fl->fl_wait);
6679 * smb2_lock() - handler for smb2 file lock command
6680 * @work: smb work containing lock command buffer
6682 * Return: 0 on success, otherwise error
6684 int smb2_lock(struct ksmbd_work *work)
6686 struct smb2_lock_req *req = work->request_buf;
6687 struct smb2_lock_rsp *rsp = work->response_buf;
6688 struct smb2_lock_element *lock_ele;
6689 struct ksmbd_file *fp = NULL;
6690 struct file_lock *flock = NULL;
6691 struct file *filp = NULL;
6695 int err = -EIO, i, rc = 0;
6696 u64 lock_start, lock_length;
6697 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2;
6698 struct ksmbd_conn *conn;
6700 LIST_HEAD(lock_list);
6701 LIST_HEAD(rollback_list);
6704 ksmbd_debug(SMB, "Received lock request\n");
6705 fp = ksmbd_lookup_fd_slow(work,
6706 le64_to_cpu(req->VolatileFileId),
6707 le64_to_cpu(req->PersistentFileId));
6709 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
6710 le64_to_cpu(req->VolatileFileId));
6716 lock_count = le16_to_cpu(req->LockCount);
6717 lock_ele = req->locks;
6719 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
6725 for (i = 0; i < lock_count; i++) {
6726 flags = le32_to_cpu(lock_ele[i].Flags);
6728 flock = smb_flock_init(filp);
6732 cmd = smb2_set_flock_flags(flock, flags);
6734 lock_start = le64_to_cpu(lock_ele[i].Offset);
6735 lock_length = le64_to_cpu(lock_ele[i].Length);
6736 if (lock_start > U64_MAX - lock_length) {
6737 pr_err("Invalid lock range requested\n");
6738 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6742 if (lock_start > OFFSET_MAX)
6743 flock->fl_start = OFFSET_MAX;
6745 flock->fl_start = lock_start;
6747 lock_length = le64_to_cpu(lock_ele[i].Length);
6748 if (lock_length > OFFSET_MAX - flock->fl_start)
6749 lock_length = OFFSET_MAX - flock->fl_start;
6751 flock->fl_end = flock->fl_start + lock_length;
6753 if (flock->fl_end < flock->fl_start) {
6755 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6756 flock->fl_end, flock->fl_start);
6757 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6761 /* Check conflict locks in one request */
6762 list_for_each_entry(cmp_lock, &lock_list, llist) {
6763 if (cmp_lock->fl->fl_start <= flock->fl_start &&
6764 cmp_lock->fl->fl_end >= flock->fl_end) {
6765 if (cmp_lock->fl->fl_type != F_UNLCK &&
6766 flock->fl_type != F_UNLCK) {
6767 pr_err("conflict two locks in one request\n");
6774 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6781 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6782 if (smb_lock->cmd < 0) {
6787 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
6792 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6793 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6794 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6795 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
6800 prior_lock = smb_lock->flags;
6802 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
6803 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
6807 /* check locks in connection list */
6808 read_lock(&conn_list_lock);
6809 list_for_each_entry(conn, &conn_list, conns_list) {
6810 spin_lock(&conn->llist_lock);
6811 list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) {
6812 if (file_inode(cmp_lock->fl->fl_file) !=
6813 file_inode(smb_lock->fl->fl_file))
6816 if (smb_lock->fl->fl_type == F_UNLCK) {
6817 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6818 cmp_lock->start == smb_lock->start &&
6819 cmp_lock->end == smb_lock->end &&
6820 !lock_defer_pending(cmp_lock->fl)) {
6822 list_del(&cmp_lock->flist);
6823 list_del(&cmp_lock->clist);
6824 spin_unlock(&conn->llist_lock);
6825 read_unlock(&conn_list_lock);
6827 locks_free_lock(cmp_lock->fl);
6834 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6835 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6838 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6842 /* check zero byte lock range */
6843 if (cmp_lock->zero_len && !smb_lock->zero_len &&
6844 cmp_lock->start > smb_lock->start &&
6845 cmp_lock->start < smb_lock->end) {
6846 spin_unlock(&conn->llist_lock);
6847 read_unlock(&conn_list_lock);
6848 pr_err("previous lock conflict with zero byte lock range\n");
6852 if (smb_lock->zero_len && !cmp_lock->zero_len &&
6853 smb_lock->start > cmp_lock->start &&
6854 smb_lock->start < cmp_lock->end) {
6855 spin_unlock(&conn->llist_lock);
6856 read_unlock(&conn_list_lock);
6857 pr_err("current lock conflict with zero byte lock range\n");
6861 if (((cmp_lock->start <= smb_lock->start &&
6862 cmp_lock->end > smb_lock->start) ||
6863 (cmp_lock->start < smb_lock->end &&
6864 cmp_lock->end >= smb_lock->end)) &&
6865 !cmp_lock->zero_len && !smb_lock->zero_len) {
6866 spin_unlock(&conn->llist_lock);
6867 read_unlock(&conn_list_lock);
6868 pr_err("Not allow lock operation on exclusive lock range\n");
6872 spin_unlock(&conn->llist_lock);
6874 read_unlock(&conn_list_lock);
6876 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
6877 pr_err("Try to unlock nolocked range\n");
6878 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6883 if (smb_lock->zero_len) {
6888 flock = smb_lock->fl;
6889 list_del(&smb_lock->llist);
6891 rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
6893 if (flags & SMB2_LOCKFLAG_UNLOCK) {
6895 ksmbd_debug(SMB, "File unlocked\n");
6896 } else if (rc == -ENOENT) {
6897 rsp->hdr.Status = STATUS_NOT_LOCKED;
6900 locks_free_lock(flock);
6903 if (rc == FILE_LOCK_DEFERRED) {
6907 "would have to wait for getting lock\n");
6908 spin_lock(&work->conn->llist_lock);
6909 list_add_tail(&smb_lock->clist,
6910 &work->conn->lock_list);
6911 spin_unlock(&work->conn->llist_lock);
6912 list_add(&smb_lock->llist, &rollback_list);
6914 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6921 rc = setup_async_work(work,
6922 smb2_remove_blocked_lock,
6928 spin_lock(&fp->f_lock);
6929 list_add(&work->fp_entry, &fp->blocked_works);
6930 spin_unlock(&fp->f_lock);
6932 smb2_send_interim_resp(work, STATUS_PENDING);
6934 ksmbd_vfs_posix_lock_wait(flock);
6936 if (work->state != KSMBD_WORK_ACTIVE) {
6937 list_del(&smb_lock->llist);
6938 spin_lock(&work->conn->llist_lock);
6939 list_del(&smb_lock->clist);
6940 spin_unlock(&work->conn->llist_lock);
6941 locks_free_lock(flock);
6943 if (work->state == KSMBD_WORK_CANCELLED) {
6944 spin_lock(&fp->f_lock);
6945 list_del(&work->fp_entry);
6946 spin_unlock(&fp->f_lock);
6950 smb2_send_interim_resp(work,
6952 work->send_no_response = 1;
6955 init_smb2_rsp_hdr(work);
6956 smb2_set_err_rsp(work);
6958 STATUS_RANGE_NOT_LOCKED;
6963 list_del(&smb_lock->llist);
6964 spin_lock(&work->conn->llist_lock);
6965 list_del(&smb_lock->clist);
6966 spin_unlock(&work->conn->llist_lock);
6968 spin_lock(&fp->f_lock);
6969 list_del(&work->fp_entry);
6970 spin_unlock(&fp->f_lock);
6973 spin_lock(&work->conn->llist_lock);
6974 list_add_tail(&smb_lock->clist,
6975 &work->conn->lock_list);
6976 list_add_tail(&smb_lock->flist,
6978 spin_unlock(&work->conn->llist_lock);
6979 list_add(&smb_lock->llist, &rollback_list);
6980 ksmbd_debug(SMB, "successful in taking lock\n");
6987 if (atomic_read(&fp->f_ci->op_count) > 1)
6988 smb_break_all_oplock(work, fp);
6990 rsp->StructureSize = cpu_to_le16(4);
6991 ksmbd_debug(SMB, "successful in taking lock\n");
6992 rsp->hdr.Status = STATUS_SUCCESS;
6994 inc_rfc1001_len(rsp, 4);
6995 ksmbd_fd_put(work, fp);
6999 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
7000 locks_free_lock(smb_lock->fl);
7001 list_del(&smb_lock->llist);
7005 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
7006 struct file_lock *rlock = NULL;
7008 rlock = smb_flock_init(filp);
7009 rlock->fl_type = F_UNLCK;
7010 rlock->fl_start = smb_lock->start;
7011 rlock->fl_end = smb_lock->end;
7013 rc = vfs_lock_file(filp, 0, rlock, NULL);
7015 pr_err("rollback unlock fail : %d\n", rc);
7017 list_del(&smb_lock->llist);
7018 spin_lock(&work->conn->llist_lock);
7019 if (!list_empty(&smb_lock->flist))
7020 list_del(&smb_lock->flist);
7021 list_del(&smb_lock->clist);
7022 spin_unlock(&work->conn->llist_lock);
7024 locks_free_lock(smb_lock->fl);
7025 locks_free_lock(rlock);
7029 ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err);
7031 if (!rsp->hdr.Status) {
7033 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7034 else if (err == -ENOMEM)
7035 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
7036 else if (err == -ENOENT)
7037 rsp->hdr.Status = STATUS_FILE_CLOSED;
7039 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
7042 smb2_set_err_rsp(work);
7043 ksmbd_fd_put(work, fp);
7047 static int fsctl_copychunk(struct ksmbd_work *work, struct smb2_ioctl_req *req,
7048 struct smb2_ioctl_rsp *rsp)
7050 struct copychunk_ioctl_req *ci_req;
7051 struct copychunk_ioctl_rsp *ci_rsp;
7052 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
7053 struct srv_copychunk *chunks;
7054 unsigned int i, chunk_count, chunk_count_written = 0;
7055 unsigned int chunk_size_written = 0;
7056 loff_t total_size_written = 0;
7059 cnt_code = le32_to_cpu(req->CntCode);
7060 ci_req = (struct copychunk_ioctl_req *)&req->Buffer[0];
7061 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
7063 rsp->VolatileFileId = req->VolatileFileId;
7064 rsp->PersistentFileId = req->PersistentFileId;
7065 ci_rsp->ChunksWritten =
7066 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
7067 ci_rsp->ChunkBytesWritten =
7068 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
7069 ci_rsp->TotalBytesWritten =
7070 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
7072 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
7073 chunk_count = le32_to_cpu(ci_req->ChunkCount);
7074 total_size_written = 0;
7076 /* verify the SRV_COPYCHUNK_COPY packet */
7077 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
7078 le32_to_cpu(req->InputCount) <
7079 offsetof(struct copychunk_ioctl_req, Chunks) +
7080 chunk_count * sizeof(struct srv_copychunk)) {
7081 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7085 for (i = 0; i < chunk_count; i++) {
7086 if (le32_to_cpu(chunks[i].Length) == 0 ||
7087 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
7089 total_size_written += le32_to_cpu(chunks[i].Length);
7092 if (i < chunk_count ||
7093 total_size_written > ksmbd_server_side_copy_max_total_size()) {
7094 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7098 src_fp = ksmbd_lookup_foreign_fd(work,
7099 le64_to_cpu(ci_req->ResumeKey[0]));
7100 dst_fp = ksmbd_lookup_fd_slow(work,
7101 le64_to_cpu(req->VolatileFileId),
7102 le64_to_cpu(req->PersistentFileId));
7105 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
7106 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7111 rsp->hdr.Status = STATUS_FILE_CLOSED;
7116 * FILE_READ_DATA should only be included in
7117 * the FSCTL_COPYCHUNK case
7119 if (cnt_code == FSCTL_COPYCHUNK &&
7120 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
7121 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7125 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
7126 chunks, chunk_count,
7127 &chunk_count_written,
7128 &chunk_size_written,
7129 &total_size_written);
7132 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7134 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
7135 else if (ret == -EBADF)
7136 rsp->hdr.Status = STATUS_INVALID_HANDLE;
7137 else if (ret == -EFBIG || ret == -ENOSPC)
7138 rsp->hdr.Status = STATUS_DISK_FULL;
7139 else if (ret == -EINVAL)
7140 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7141 else if (ret == -EISDIR)
7142 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
7143 else if (ret == -E2BIG)
7144 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
7146 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
7149 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
7150 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
7151 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
7153 ksmbd_fd_put(work, src_fp);
7154 ksmbd_fd_put(work, dst_fp);
7158 static __be32 idev_ipv4_address(struct in_device *idev)
7162 struct in_ifaddr *ifa;
7165 in_dev_for_each_ifa_rcu(ifa, idev) {
7166 if (ifa->ifa_flags & IFA_F_SECONDARY)
7169 addr = ifa->ifa_address;
7176 static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
7177 struct smb2_ioctl_req *req,
7178 struct smb2_ioctl_rsp *rsp)
7180 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
7182 struct net_device *netdev;
7183 struct sockaddr_storage_rsp *sockaddr_storage;
7185 unsigned long long speed;
7186 struct sockaddr_in6 *csin6 = (struct sockaddr_in6 *)&conn->peer_addr;
7189 for_each_netdev(&init_net, netdev) {
7190 if (netdev->type == ARPHRD_LOOPBACK)
7193 flags = dev_get_flags(netdev);
7194 if (!(flags & IFF_RUNNING))
7197 nii_rsp = (struct network_interface_info_ioctl_rsp *)
7198 &rsp->Buffer[nbytes];
7199 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
7201 nii_rsp->Capability = 0;
7202 if (ksmbd_rdma_capable_netdev(netdev))
7203 nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE);
7205 nii_rsp->Next = cpu_to_le32(152);
7206 nii_rsp->Reserved = 0;
7208 if (netdev->ethtool_ops->get_link_ksettings) {
7209 struct ethtool_link_ksettings cmd;
7211 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
7212 speed = cmd.base.speed;
7214 ksmbd_debug(SMB, "%s %s\n", netdev->name,
7215 "speed is unknown, defaulting to 1Gb/sec");
7220 nii_rsp->LinkSpeed = cpu_to_le64(speed);
7222 sockaddr_storage = (struct sockaddr_storage_rsp *)
7223 nii_rsp->SockAddr_Storage;
7224 memset(sockaddr_storage, 0, 128);
7226 if (conn->peer_addr.ss_family == PF_INET ||
7227 ipv6_addr_v4mapped(&csin6->sin6_addr)) {
7228 struct in_device *idev;
7230 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
7231 sockaddr_storage->addr4.Port = 0;
7233 idev = __in_dev_get_rtnl(netdev);
7236 sockaddr_storage->addr4.IPv4address =
7237 idev_ipv4_address(idev);
7239 struct inet6_dev *idev6;
7240 struct inet6_ifaddr *ifa;
7241 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7243 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7244 sockaddr_storage->addr6.Port = 0;
7245 sockaddr_storage->addr6.FlowInfo = 0;
7247 idev6 = __in6_dev_get(netdev);
7251 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7252 if (ifa->flags & (IFA_F_TENTATIVE |
7255 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7258 sockaddr_storage->addr6.ScopeId = 0;
7261 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7265 /* zero if this is last one */
7270 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
7274 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7275 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7279 static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
7280 struct validate_negotiate_info_req *neg_req,
7281 struct validate_negotiate_info_rsp *neg_rsp)
7286 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
7287 neg_req->DialectCount);
7288 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7293 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7298 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7303 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7308 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7309 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7310 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7311 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7316 static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
7317 struct file_allocated_range_buffer *qar_req,
7318 struct file_allocated_range_buffer *qar_rsp,
7319 int in_count, int *out_count)
7321 struct ksmbd_file *fp;
7322 loff_t start, length;
7329 fp = ksmbd_lookup_fd_fast(work, id);
7333 start = le64_to_cpu(qar_req->file_offset);
7334 length = le64_to_cpu(qar_req->length);
7336 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
7337 qar_rsp, in_count, out_count);
7338 if (ret && ret != -E2BIG)
7341 ksmbd_fd_put(work, fp);
7345 static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
7346 int out_buf_len, struct smb2_ioctl_req *req,
7347 struct smb2_ioctl_rsp *rsp)
7349 struct ksmbd_rpc_command *rpc_resp;
7350 char *data_buf = (char *)&req->Buffer[0];
7353 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
7354 le32_to_cpu(req->InputCount));
7356 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7358 * set STATUS_SOME_NOT_MAPPED response
7359 * for unknown domain sid.
7361 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7362 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7363 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7365 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7366 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7370 nbytes = rpc_resp->payload_sz;
7371 if (rpc_resp->payload_sz > out_buf_len) {
7372 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7373 nbytes = out_buf_len;
7376 if (!rpc_resp->payload_sz) {
7378 STATUS_UNEXPECTED_IO_ERROR;
7382 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7389 static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
7390 struct file_sparse *sparse)
7392 struct ksmbd_file *fp;
7393 struct user_namespace *user_ns;
7397 fp = ksmbd_lookup_fd_fast(work, id);
7400 user_ns = file_mnt_user_ns(fp->filp);
7402 old_fattr = fp->f_ci->m_fattr;
7403 if (sparse->SetSparse)
7404 fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE;
7406 fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE;
7408 if (fp->f_ci->m_fattr != old_fattr &&
7409 test_share_config_flag(work->tcon->share_conf,
7410 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
7411 struct xattr_dos_attrib da;
7413 ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns,
7414 fp->filp->f_path.dentry, &da);
7418 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
7419 ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
7420 fp->filp->f_path.dentry, &da);
7422 fp->f_ci->m_fattr = old_fattr;
7426 ksmbd_fd_put(work, fp);
7430 static int fsctl_request_resume_key(struct ksmbd_work *work,
7431 struct smb2_ioctl_req *req,
7432 struct resume_key_ioctl_rsp *key_rsp)
7434 struct ksmbd_file *fp;
7436 fp = ksmbd_lookup_fd_slow(work,
7437 le64_to_cpu(req->VolatileFileId),
7438 le64_to_cpu(req->PersistentFileId));
7442 memset(key_rsp, 0, sizeof(*key_rsp));
7443 key_rsp->ResumeKey[0] = req->VolatileFileId;
7444 key_rsp->ResumeKey[1] = req->PersistentFileId;
7445 ksmbd_fd_put(work, fp);
7451 * smb2_ioctl() - handler for smb2 ioctl command
7452 * @work: smb work containing ioctl command buffer
7454 * Return: 0 on success, otherwise error
7456 int smb2_ioctl(struct ksmbd_work *work)
7458 struct smb2_ioctl_req *req;
7459 struct smb2_ioctl_rsp *rsp, *rsp_org;
7460 int cnt_code, nbytes = 0;
7462 u64 id = KSMBD_NO_FID;
7463 struct ksmbd_conn *conn = work->conn;
7466 rsp_org = work->response_buf;
7467 if (work->next_smb2_rcv_hdr_off) {
7468 req = ksmbd_req_buf_next(work);
7469 rsp = ksmbd_resp_buf_next(work);
7470 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
7471 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
7472 work->compound_fid);
7473 id = work->compound_fid;
7476 req = work->request_buf;
7477 rsp = work->response_buf;
7480 if (!has_file_id(id))
7481 id = le64_to_cpu(req->VolatileFileId);
7483 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7484 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7488 cnt_code = le32_to_cpu(req->CntCode);
7489 out_buf_len = le32_to_cpu(req->MaxOutputResponse);
7490 out_buf_len = min(KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
7493 case FSCTL_DFS_GET_REFERRALS:
7494 case FSCTL_DFS_GET_REFERRALS_EX:
7495 /* Not support DFS yet */
7496 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7498 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7500 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7502 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7503 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7507 * TODO: This is dummy implementation to pass smbtorture
7508 * Need to check correct response later
7510 memset(obj_buf->ObjectId, 0x0, 16);
7511 memset(obj_buf->BirthVolumeId, 0x0, 16);
7512 memset(obj_buf->BirthObjectId, 0x0, 16);
7513 memset(obj_buf->DomainId, 0x0, 16);
7517 case FSCTL_PIPE_TRANSCEIVE:
7518 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7520 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7521 if (conn->dialect < SMB30_PROT_ID) {
7526 ret = fsctl_validate_negotiate_info(conn,
7527 (struct validate_negotiate_info_req *)&req->Buffer[0],
7528 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0]);
7532 nbytes = sizeof(struct validate_negotiate_info_rsp);
7533 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7534 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7536 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
7537 nbytes = fsctl_query_iface_info_ioctl(conn, req, rsp);
7541 case FSCTL_REQUEST_RESUME_KEY:
7542 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7547 ret = fsctl_request_resume_key(work, req,
7548 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
7551 rsp->PersistentFileId = req->PersistentFileId;
7552 rsp->VolatileFileId = req->VolatileFileId;
7553 nbytes = sizeof(struct resume_key_ioctl_rsp);
7555 case FSCTL_COPYCHUNK:
7556 case FSCTL_COPYCHUNK_WRITE:
7557 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
7559 "User does not have write permission\n");
7564 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7569 nbytes = sizeof(struct copychunk_ioctl_rsp);
7570 fsctl_copychunk(work, req, rsp);
7572 case FSCTL_SET_SPARSE:
7573 ret = fsctl_set_sparse(work, id,
7574 (struct file_sparse *)&req->Buffer[0]);
7578 case FSCTL_SET_ZERO_DATA:
7580 struct file_zero_data_information *zero_data;
7581 struct ksmbd_file *fp;
7584 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
7586 "User does not have write permission\n");
7592 (struct file_zero_data_information *)&req->Buffer[0];
7594 fp = ksmbd_lookup_fd_fast(work, id);
7600 off = le64_to_cpu(zero_data->FileOffset);
7601 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7603 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7604 ksmbd_fd_put(work, fp);
7609 case FSCTL_QUERY_ALLOCATED_RANGES:
7610 ret = fsctl_query_allocated_ranges(work, id,
7611 (struct file_allocated_range_buffer *)&req->Buffer[0],
7612 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7614 sizeof(struct file_allocated_range_buffer), &nbytes);
7615 if (ret == -E2BIG) {
7616 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7617 } else if (ret < 0) {
7622 nbytes *= sizeof(struct file_allocated_range_buffer);
7624 case FSCTL_GET_REPARSE_POINT:
7626 struct reparse_data_buffer *reparse_ptr;
7627 struct ksmbd_file *fp;
7629 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7630 fp = ksmbd_lookup_fd_fast(work, id);
7632 pr_err("not found fp!!\n");
7637 reparse_ptr->ReparseTag =
7638 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode);
7639 reparse_ptr->ReparseDataLength = 0;
7640 ksmbd_fd_put(work, fp);
7641 nbytes = sizeof(struct reparse_data_buffer);
7644 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7646 struct ksmbd_file *fp_in, *fp_out = NULL;
7647 struct duplicate_extents_to_file *dup_ext;
7648 loff_t src_off, dst_off, length, cloned;
7650 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7652 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
7653 dup_ext->PersistentFileHandle);
7655 pr_err("not found file handle in duplicate extent to file\n");
7660 fp_out = ksmbd_lookup_fd_fast(work, id);
7662 pr_err("not found fp\n");
7667 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7668 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7669 length = le64_to_cpu(dup_ext->ByteCount);
7670 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
7671 dst_off, length, 0);
7672 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7675 } else if (cloned != length) {
7676 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7677 fp_out->filp, dst_off, length, 0);
7678 if (cloned != length) {
7687 ksmbd_fd_put(work, fp_in);
7688 ksmbd_fd_put(work, fp_out);
7694 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
7700 rsp->CntCode = cpu_to_le32(cnt_code);
7701 rsp->InputCount = cpu_to_le32(0);
7702 rsp->InputOffset = cpu_to_le32(112);
7703 rsp->OutputOffset = cpu_to_le32(112);
7704 rsp->OutputCount = cpu_to_le32(nbytes);
7705 rsp->StructureSize = cpu_to_le16(49);
7706 rsp->Reserved = cpu_to_le16(0);
7707 rsp->Flags = cpu_to_le32(0);
7708 rsp->Reserved2 = cpu_to_le32(0);
7709 inc_rfc1001_len(rsp_org, 48 + nbytes);
7715 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7716 else if (ret == -ENOENT)
7717 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7718 else if (ret == -EOPNOTSUPP)
7719 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7720 else if (ret < 0 || rsp->hdr.Status == 0)
7721 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7722 smb2_set_err_rsp(work);
7727 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7728 * @work: smb work containing oplock break command buffer
7732 static void smb20_oplock_break_ack(struct ksmbd_work *work)
7734 struct smb2_oplock_break *req = work->request_buf;
7735 struct smb2_oplock_break *rsp = work->response_buf;
7736 struct ksmbd_file *fp;
7737 struct oplock_info *opinfo = NULL;
7740 u64 volatile_id, persistent_id;
7741 char req_oplevel = 0, rsp_oplevel = 0;
7742 unsigned int oplock_change_type;
7744 volatile_id = le64_to_cpu(req->VolatileFid);
7745 persistent_id = le64_to_cpu(req->PersistentFid);
7746 req_oplevel = req->OplockLevel;
7747 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7748 volatile_id, persistent_id, req_oplevel);
7750 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7752 rsp->hdr.Status = STATUS_FILE_CLOSED;
7753 smb2_set_err_rsp(work);
7757 opinfo = opinfo_get(fp);
7759 pr_err("unexpected null oplock_info\n");
7760 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7761 smb2_set_err_rsp(work);
7762 ksmbd_fd_put(work, fp);
7766 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7767 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7771 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7772 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7773 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7777 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7778 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7779 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7780 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
7781 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7782 oplock_change_type = OPLOCK_WRITE_TO_NONE;
7783 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7784 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
7785 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7786 oplock_change_type = OPLOCK_READ_TO_NONE;
7787 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7788 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
7789 err = STATUS_INVALID_DEVICE_STATE;
7790 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7791 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7792 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
7793 oplock_change_type = OPLOCK_WRITE_TO_READ;
7794 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7795 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7796 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
7797 oplock_change_type = OPLOCK_WRITE_TO_NONE;
7798 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7799 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
7800 oplock_change_type = OPLOCK_READ_TO_NONE;
7802 oplock_change_type = 0;
7805 oplock_change_type = 0;
7808 switch (oplock_change_type) {
7809 case OPLOCK_WRITE_TO_READ:
7810 ret = opinfo_write_to_read(opinfo);
7811 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7813 case OPLOCK_WRITE_TO_NONE:
7814 ret = opinfo_write_to_none(opinfo);
7815 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7817 case OPLOCK_READ_TO_NONE:
7818 ret = opinfo_read_to_none(opinfo);
7819 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7822 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7823 opinfo->level, rsp_oplevel);
7827 rsp->hdr.Status = err;
7832 ksmbd_fd_put(work, fp);
7833 opinfo->op_state = OPLOCK_STATE_NONE;
7834 wake_up_interruptible_all(&opinfo->oplock_q);
7836 rsp->StructureSize = cpu_to_le16(24);
7837 rsp->OplockLevel = rsp_oplevel;
7840 rsp->VolatileFid = cpu_to_le64(volatile_id);
7841 rsp->PersistentFid = cpu_to_le64(persistent_id);
7842 inc_rfc1001_len(rsp, 24);
7846 opinfo->op_state = OPLOCK_STATE_NONE;
7847 wake_up_interruptible_all(&opinfo->oplock_q);
7850 ksmbd_fd_put(work, fp);
7851 smb2_set_err_rsp(work);
7854 static int check_lease_state(struct lease *lease, __le32 req_state)
7856 if ((lease->new_state ==
7857 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7858 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
7859 lease->new_state = req_state;
7863 if (lease->new_state == req_state)
7870 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7871 * @work: smb work containing lease break command buffer
7875 static void smb21_lease_break_ack(struct ksmbd_work *work)
7877 struct ksmbd_conn *conn = work->conn;
7878 struct smb2_lease_ack *req = work->request_buf;
7879 struct smb2_lease_ack *rsp = work->response_buf;
7880 struct oplock_info *opinfo;
7883 unsigned int lease_change_type;
7885 struct lease *lease;
7887 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
7888 le32_to_cpu(req->LeaseState));
7889 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7891 ksmbd_debug(OPLOCK, "file not opened\n");
7892 smb2_set_err_rsp(work);
7893 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7896 lease = opinfo->o_lease;
7898 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7899 pr_err("unexpected lease break state 0x%x\n",
7901 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7905 if (check_lease_state(lease, req->LeaseState)) {
7906 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
7908 "req lease state: 0x%x, expected state: 0x%x\n",
7909 req->LeaseState, lease->new_state);
7913 if (!atomic_read(&opinfo->breaking_cnt)) {
7914 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7918 /* check for bad lease state */
7919 if (req->LeaseState &
7920 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
7921 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7922 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7923 lease_change_type = OPLOCK_WRITE_TO_NONE;
7925 lease_change_type = OPLOCK_READ_TO_NONE;
7926 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
7927 le32_to_cpu(lease->state),
7928 le32_to_cpu(req->LeaseState));
7929 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
7930 req->LeaseState != SMB2_LEASE_NONE_LE) {
7931 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7932 lease_change_type = OPLOCK_READ_TO_NONE;
7933 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
7934 le32_to_cpu(lease->state),
7935 le32_to_cpu(req->LeaseState));
7937 /* valid lease state changes */
7938 err = STATUS_INVALID_DEVICE_STATE;
7939 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
7940 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7941 lease_change_type = OPLOCK_WRITE_TO_NONE;
7943 lease_change_type = OPLOCK_READ_TO_NONE;
7944 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
7945 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7946 lease_change_type = OPLOCK_WRITE_TO_READ;
7948 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
7950 lease_change_type = 0;
7954 switch (lease_change_type) {
7955 case OPLOCK_WRITE_TO_READ:
7956 ret = opinfo_write_to_read(opinfo);
7958 case OPLOCK_READ_HANDLE_TO_READ:
7959 ret = opinfo_read_handle_to_read(opinfo);
7961 case OPLOCK_WRITE_TO_NONE:
7962 ret = opinfo_write_to_none(opinfo);
7964 case OPLOCK_READ_TO_NONE:
7965 ret = opinfo_read_to_none(opinfo);
7968 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
7969 le32_to_cpu(lease->state),
7970 le32_to_cpu(req->LeaseState));
7973 lease_state = lease->state;
7974 opinfo->op_state = OPLOCK_STATE_NONE;
7975 wake_up_interruptible_all(&opinfo->oplock_q);
7976 atomic_dec(&opinfo->breaking_cnt);
7977 wake_up_interruptible_all(&opinfo->oplock_brk);
7981 rsp->hdr.Status = err;
7985 rsp->StructureSize = cpu_to_le16(36);
7988 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
7989 rsp->LeaseState = lease_state;
7990 rsp->LeaseDuration = 0;
7991 inc_rfc1001_len(rsp, 36);
7995 opinfo->op_state = OPLOCK_STATE_NONE;
7996 wake_up_interruptible_all(&opinfo->oplock_q);
7997 atomic_dec(&opinfo->breaking_cnt);
7998 wake_up_interruptible_all(&opinfo->oplock_brk);
8001 smb2_set_err_rsp(work);
8005 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
8006 * @work: smb work containing oplock/lease break command buffer
8010 int smb2_oplock_break(struct ksmbd_work *work)
8012 struct smb2_oplock_break *req = work->request_buf;
8013 struct smb2_oplock_break *rsp = work->response_buf;
8015 switch (le16_to_cpu(req->StructureSize)) {
8016 case OP_BREAK_STRUCT_SIZE_20:
8017 smb20_oplock_break_ack(work);
8019 case OP_BREAK_STRUCT_SIZE_21:
8020 smb21_lease_break_ack(work);
8023 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
8024 le16_to_cpu(req->StructureSize));
8025 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
8026 smb2_set_err_rsp(work);
8033 * smb2_notify() - handler for smb2 notify request
8034 * @work: smb work containing notify command buffer
8038 int smb2_notify(struct ksmbd_work *work)
8040 struct smb2_notify_req *req;
8041 struct smb2_notify_rsp *rsp;
8043 WORK_BUFFERS(work, req, rsp);
8045 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
8046 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
8047 smb2_set_err_rsp(work);
8051 smb2_set_err_rsp(work);
8052 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
8057 * smb2_is_sign_req() - handler for checking packet signing status
8058 * @work: smb work containing notify command buffer
8059 * @command: SMB2 command id
8061 * Return: true if packed is signed, false otherwise
8063 bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
8065 struct smb2_hdr *rcv_hdr2 = work->request_buf;
8067 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
8068 command != SMB2_NEGOTIATE_HE &&
8069 command != SMB2_SESSION_SETUP_HE &&
8070 command != SMB2_OPLOCK_BREAK_HE)
8077 * smb2_check_sign_req() - handler for req packet sign processing
8078 * @work: smb work containing notify command buffer
8080 * Return: 1 on success, 0 otherwise
8082 int smb2_check_sign_req(struct ksmbd_work *work)
8084 struct smb2_hdr *hdr, *hdr_org;
8085 char signature_req[SMB2_SIGNATURE_SIZE];
8086 char signature[SMB2_HMACSHA256_SIZE];
8090 hdr_org = hdr = work->request_buf;
8091 if (work->next_smb2_rcv_hdr_off)
8092 hdr = ksmbd_req_buf_next(work);
8094 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
8095 len = be32_to_cpu(hdr_org->smb2_buf_length);
8096 else if (hdr->NextCommand)
8097 len = le32_to_cpu(hdr->NextCommand);
8099 len = be32_to_cpu(hdr_org->smb2_buf_length) -
8100 work->next_smb2_rcv_hdr_off;
8102 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8103 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8105 iov[0].iov_base = (char *)&hdr->ProtocolId;
8106 iov[0].iov_len = len;
8108 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
8112 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
8113 pr_err("bad smb2 signature\n");
8121 * smb2_set_sign_rsp() - handler for rsp packet sign processing
8122 * @work: smb work containing notify command buffer
8125 void smb2_set_sign_rsp(struct ksmbd_work *work)
8127 struct smb2_hdr *hdr, *hdr_org;
8128 struct smb2_hdr *req_hdr;
8129 char signature[SMB2_HMACSHA256_SIZE];
8134 hdr_org = hdr = work->response_buf;
8135 if (work->next_smb2_rsp_hdr_off)
8136 hdr = ksmbd_resp_buf_next(work);
8138 req_hdr = ksmbd_req_buf_next(work);
8140 if (!work->next_smb2_rsp_hdr_off) {
8141 len = get_rfc1002_len(hdr_org);
8142 if (req_hdr->NextCommand)
8143 len = ALIGN(len, 8);
8145 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
8146 len = ALIGN(len, 8);
8149 if (req_hdr->NextCommand)
8150 hdr->NextCommand = cpu_to_le32(len);
8152 hdr->Flags |= SMB2_FLAGS_SIGNED;
8153 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8155 iov[0].iov_base = (char *)&hdr->ProtocolId;
8156 iov[0].iov_len = len;
8158 if (work->aux_payload_sz) {
8159 iov[0].iov_len -= work->aux_payload_sz;
8161 iov[1].iov_base = work->aux_payload_buf;
8162 iov[1].iov_len = work->aux_payload_sz;
8166 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
8168 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8172 * smb3_check_sign_req() - handler for req packet sign processing
8173 * @work: smb work containing notify command buffer
8175 * Return: 1 on success, 0 otherwise
8177 int smb3_check_sign_req(struct ksmbd_work *work)
8179 struct ksmbd_conn *conn = work->conn;
8181 struct smb2_hdr *hdr, *hdr_org;
8182 struct channel *chann;
8183 char signature_req[SMB2_SIGNATURE_SIZE];
8184 char signature[SMB2_CMACAES_SIZE];
8188 hdr_org = hdr = work->request_buf;
8189 if (work->next_smb2_rcv_hdr_off)
8190 hdr = ksmbd_req_buf_next(work);
8192 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
8193 len = be32_to_cpu(hdr_org->smb2_buf_length);
8194 else if (hdr->NextCommand)
8195 len = le32_to_cpu(hdr->NextCommand);
8197 len = be32_to_cpu(hdr_org->smb2_buf_length) -
8198 work->next_smb2_rcv_hdr_off;
8200 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8201 signing_key = work->sess->smb3signingkey;
8203 chann = lookup_chann_list(work->sess, conn);
8206 signing_key = chann->smb3signingkey;
8210 pr_err("SMB3 signing key is not generated\n");
8214 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8215 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8216 iov[0].iov_base = (char *)&hdr->ProtocolId;
8217 iov[0].iov_len = len;
8219 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
8222 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
8223 pr_err("bad smb2 signature\n");
8231 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8232 * @work: smb work containing notify command buffer
8235 void smb3_set_sign_rsp(struct ksmbd_work *work)
8237 struct ksmbd_conn *conn = work->conn;
8238 struct smb2_hdr *req_hdr;
8239 struct smb2_hdr *hdr, *hdr_org;
8240 struct channel *chann;
8241 char signature[SMB2_CMACAES_SIZE];
8247 hdr_org = hdr = work->response_buf;
8248 if (work->next_smb2_rsp_hdr_off)
8249 hdr = ksmbd_resp_buf_next(work);
8251 req_hdr = ksmbd_req_buf_next(work);
8253 if (!work->next_smb2_rsp_hdr_off) {
8254 len = get_rfc1002_len(hdr_org);
8255 if (req_hdr->NextCommand)
8256 len = ALIGN(len, 8);
8258 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
8259 len = ALIGN(len, 8);
8262 if (conn->binding == false &&
8263 le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8264 signing_key = work->sess->smb3signingkey;
8266 chann = lookup_chann_list(work->sess, work->conn);
8269 signing_key = chann->smb3signingkey;
8275 if (req_hdr->NextCommand)
8276 hdr->NextCommand = cpu_to_le32(len);
8278 hdr->Flags |= SMB2_FLAGS_SIGNED;
8279 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8280 iov[0].iov_base = (char *)&hdr->ProtocolId;
8281 iov[0].iov_len = len;
8282 if (work->aux_payload_sz) {
8283 iov[0].iov_len -= work->aux_payload_sz;
8284 iov[1].iov_base = work->aux_payload_buf;
8285 iov[1].iov_len = work->aux_payload_sz;
8289 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8290 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8294 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8295 * @work: smb work containing response buffer
8298 void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8300 struct ksmbd_conn *conn = work->conn;
8301 struct ksmbd_session *sess = work->sess;
8302 struct smb2_hdr *req, *rsp;
8304 if (conn->dialect != SMB311_PROT_ID)
8307 WORK_BUFFERS(work, req, rsp);
8309 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE &&
8311 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
8312 conn->preauth_info->Preauth_HashValue);
8314 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
8317 if (conn->binding) {
8318 struct preauth_session *preauth_sess;
8320 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8323 hash_value = preauth_sess->Preauth_HashValue;
8325 hash_value = sess->Preauth_HashValue;
8329 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
8334 static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf,
8337 struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf;
8338 unsigned int orig_len = get_rfc1002_len(old_buf);
8340 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
8341 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8342 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
8343 tr_hdr->Flags = cpu_to_le16(0x01);
8344 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8345 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8346 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
8348 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
8349 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
8350 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
8351 inc_rfc1001_len(tr_hdr, orig_len);
8354 int smb3_encrypt_resp(struct ksmbd_work *work)
8356 char *buf = work->response_buf;
8357 struct smb2_transform_hdr *tr_hdr;
8360 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
8362 if (ARRAY_SIZE(iov) < rq_nvec)
8365 tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
8369 /* fill transform header */
8370 fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type);
8372 iov[0].iov_base = tr_hdr;
8373 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8374 buf_size += iov[0].iov_len - 4;
8376 iov[1].iov_base = buf + 4;
8377 iov[1].iov_len = get_rfc1002_len(buf);
8378 if (work->aux_payload_sz) {
8379 iov[1].iov_len = work->resp_hdr_sz - 4;
8381 iov[2].iov_base = work->aux_payload_buf;
8382 iov[2].iov_len = work->aux_payload_sz;
8383 buf_size += iov[2].iov_len;
8385 buf_size += iov[1].iov_len;
8386 work->resp_hdr_sz = iov[1].iov_len;
8388 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8392 memmove(buf, iov[1].iov_base, iov[1].iov_len);
8393 tr_hdr->smb2_buf_length = cpu_to_be32(buf_size);
8394 work->tr_buf = tr_hdr;
8399 bool smb3_is_transform_hdr(void *buf)
8401 struct smb2_transform_hdr *trhdr = buf;
8403 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8406 int smb3_decrypt_req(struct ksmbd_work *work)
8408 struct ksmbd_conn *conn = work->conn;
8409 struct ksmbd_session *sess;
8410 char *buf = work->request_buf;
8411 struct smb2_hdr *hdr;
8412 unsigned int pdu_length = get_rfc1002_len(buf);
8414 unsigned int buf_data_size = pdu_length + 4 -
8415 sizeof(struct smb2_transform_hdr);
8416 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
8419 if (pdu_length + 4 <
8420 sizeof(struct smb2_transform_hdr) + sizeof(struct smb2_hdr)) {
8421 pr_err("Transform message is too small (%u)\n",
8423 return -ECONNABORTED;
8426 if (pdu_length + 4 <
8427 le32_to_cpu(tr_hdr->OriginalMessageSize) + sizeof(struct smb2_transform_hdr)) {
8428 pr_err("Transform message is broken\n");
8429 return -ECONNABORTED;
8432 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
8434 pr_err("invalid session id(%llx) in transform header\n",
8435 le64_to_cpu(tr_hdr->SessionId));
8436 return -ECONNABORTED;
8439 iov[0].iov_base = buf;
8440 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8441 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
8442 iov[1].iov_len = buf_data_size;
8443 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8447 memmove(buf + 4, iov[1].iov_base, buf_data_size);
8448 hdr = (struct smb2_hdr *)buf;
8449 hdr->smb2_buf_length = cpu_to_be32(buf_data_size);
8454 bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8456 struct ksmbd_conn *conn = work->conn;
8457 struct smb2_hdr *rsp = work->response_buf;
8459 if (conn->dialect < SMB30_PROT_ID)
8462 if (work->next_smb2_rcv_hdr_off)
8463 rsp = ksmbd_resp_buf_next(work);
8465 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
8466 rsp->Status == STATUS_SUCCESS)