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>
14 #include <linux/mount.h>
15 #include <linux/filelock.h>
24 #include "connection.h"
25 #include "transport_ipc.h"
26 #include "transport_rdma.h"
28 #include "vfs_cache.h"
32 #include "smb_common.h"
33 #include "smbstatus.h"
34 #include "ksmbd_work.h"
35 #include "mgmt/user_config.h"
36 #include "mgmt/share_config.h"
37 #include "mgmt/tree_connect.h"
38 #include "mgmt/user_session.h"
39 #include "mgmt/ksmbd_ida.h"
42 static void __wbuf(struct ksmbd_work
*work
, void **req
, void **rsp
)
44 if (work
->next_smb2_rcv_hdr_off
) {
45 *req
= ksmbd_req_buf_next(work
);
46 *rsp
= ksmbd_resp_buf_next(work
);
48 *req
= smb2_get_msg(work
->request_buf
);
49 *rsp
= smb2_get_msg(work
->response_buf
);
53 #define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs))
56 * check_session_id() - check for valid session id in smb header
57 * @conn: connection instance
58 * @id: session id from smb header
60 * Return: 1 if valid session id, otherwise 0
62 static inline bool check_session_id(struct ksmbd_conn
*conn
, u64 id
)
64 struct ksmbd_session
*sess
;
66 if (id
== 0 || id
== -1)
69 sess
= ksmbd_session_lookup_all(conn
, id
);
72 pr_err("Invalid user session id: %llu\n", id
);
76 struct channel
*lookup_chann_list(struct ksmbd_session
*sess
, struct ksmbd_conn
*conn
)
78 return xa_load(&sess
->ksmbd_chann_list
, (long)conn
);
82 * smb2_get_ksmbd_tcon() - get tree connection information using a tree id.
85 * Return: 0 if there is a tree connection matched or these are
86 * skipable commands, otherwise error
88 int smb2_get_ksmbd_tcon(struct ksmbd_work
*work
)
90 struct smb2_hdr
*req_hdr
= ksmbd_req_buf_next(work
);
91 unsigned int cmd
= le16_to_cpu(req_hdr
->Command
);
94 if (cmd
== SMB2_TREE_CONNECT_HE
||
95 cmd
== SMB2_CANCEL_HE
||
96 cmd
== SMB2_LOGOFF_HE
) {
97 ksmbd_debug(SMB
, "skip to check tree connect request\n");
101 if (xa_empty(&work
->sess
->tree_conns
)) {
102 ksmbd_debug(SMB
, "NO tree connected\n");
106 tree_id
= le32_to_cpu(req_hdr
->Id
.SyncId
.TreeId
);
109 * If request is not the first in Compound request,
110 * Just validate tree id in header with work->tcon->id.
112 if (work
->next_smb2_rcv_hdr_off
) {
114 pr_err("The first operation in the compound does not have tcon\n");
117 if (tree_id
!= UINT_MAX
&& work
->tcon
->id
!= tree_id
) {
118 pr_err("tree id(%u) is different with id(%u) in first operation\n",
119 tree_id
, work
->tcon
->id
);
125 work
->tcon
= ksmbd_tree_conn_lookup(work
->sess
, tree_id
);
127 pr_err("Invalid tid %d\n", tree_id
);
135 * smb2_set_err_rsp() - set error response code on smb response
136 * @work: smb work containing response buffer
138 void smb2_set_err_rsp(struct ksmbd_work
*work
)
140 struct smb2_err_rsp
*err_rsp
;
142 if (work
->next_smb2_rcv_hdr_off
)
143 err_rsp
= ksmbd_resp_buf_next(work
);
145 err_rsp
= smb2_get_msg(work
->response_buf
);
147 if (err_rsp
->hdr
.Status
!= STATUS_STOPPED_ON_SYMLINK
) {
150 err_rsp
->StructureSize
= SMB2_ERROR_STRUCTURE_SIZE2_LE
;
151 err_rsp
->ErrorContextCount
= 0;
152 err_rsp
->Reserved
= 0;
153 err_rsp
->ByteCount
= 0;
154 err_rsp
->ErrorData
[0] = 0;
155 err
= ksmbd_iov_pin_rsp(work
, (void *)err_rsp
,
156 __SMB2_HEADER_STRUCTURE_SIZE
+
157 SMB2_ERROR_STRUCTURE_SIZE2
);
159 work
->send_no_response
= 1;
164 * is_smb2_neg_cmd() - is it smb2 negotiation command
165 * @work: smb work containing smb header
167 * Return: true if smb2 negotiation command, otherwise false
169 bool is_smb2_neg_cmd(struct ksmbd_work
*work
)
171 struct smb2_hdr
*hdr
= smb2_get_msg(work
->request_buf
);
173 /* is it SMB2 header ? */
174 if (hdr
->ProtocolId
!= SMB2_PROTO_NUMBER
)
177 /* make sure it is request not response message */
178 if (hdr
->Flags
& SMB2_FLAGS_SERVER_TO_REDIR
)
181 if (hdr
->Command
!= SMB2_NEGOTIATE
)
188 * is_smb2_rsp() - is it smb2 response
189 * @work: smb work containing smb response buffer
191 * Return: true if smb2 response, otherwise false
193 bool is_smb2_rsp(struct ksmbd_work
*work
)
195 struct smb2_hdr
*hdr
= smb2_get_msg(work
->response_buf
);
197 /* is it SMB2 header ? */
198 if (hdr
->ProtocolId
!= SMB2_PROTO_NUMBER
)
201 /* make sure it is response not request message */
202 if (!(hdr
->Flags
& SMB2_FLAGS_SERVER_TO_REDIR
))
209 * get_smb2_cmd_val() - get smb command code from smb header
210 * @work: smb work containing smb request buffer
212 * Return: smb2 request command value
214 u16
get_smb2_cmd_val(struct ksmbd_work
*work
)
216 struct smb2_hdr
*rcv_hdr
;
218 if (work
->next_smb2_rcv_hdr_off
)
219 rcv_hdr
= ksmbd_req_buf_next(work
);
221 rcv_hdr
= smb2_get_msg(work
->request_buf
);
222 return le16_to_cpu(rcv_hdr
->Command
);
226 * set_smb2_rsp_status() - set error response code on smb2 header
227 * @work: smb work containing response buffer
228 * @err: error response code
230 void set_smb2_rsp_status(struct ksmbd_work
*work
, __le32 err
)
232 struct smb2_hdr
*rsp_hdr
;
234 rsp_hdr
= smb2_get_msg(work
->response_buf
);
235 rsp_hdr
->Status
= err
;
239 work
->next_smb2_rcv_hdr_off
= 0;
240 smb2_set_err_rsp(work
);
244 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command
245 * @work: smb work containing smb request buffer
247 * smb2 negotiate response is sent in reply of smb1 negotiate command for
248 * dialect auto-negotiation.
250 int init_smb2_neg_rsp(struct ksmbd_work
*work
)
252 struct smb2_hdr
*rsp_hdr
;
253 struct smb2_negotiate_rsp
*rsp
;
254 struct ksmbd_conn
*conn
= work
->conn
;
257 rsp_hdr
= smb2_get_msg(work
->response_buf
);
258 memset(rsp_hdr
, 0, sizeof(struct smb2_hdr
) + 2);
259 rsp_hdr
->ProtocolId
= SMB2_PROTO_NUMBER
;
260 rsp_hdr
->StructureSize
= SMB2_HEADER_STRUCTURE_SIZE
;
261 rsp_hdr
->CreditRequest
= cpu_to_le16(2);
262 rsp_hdr
->Command
= SMB2_NEGOTIATE
;
263 rsp_hdr
->Flags
= (SMB2_FLAGS_SERVER_TO_REDIR
);
264 rsp_hdr
->NextCommand
= 0;
265 rsp_hdr
->MessageId
= 0;
266 rsp_hdr
->Id
.SyncId
.ProcessId
= 0;
267 rsp_hdr
->Id
.SyncId
.TreeId
= 0;
268 rsp_hdr
->SessionId
= 0;
269 memset(rsp_hdr
->Signature
, 0, 16);
271 rsp
= smb2_get_msg(work
->response_buf
);
273 WARN_ON(ksmbd_conn_good(conn
));
275 rsp
->StructureSize
= cpu_to_le16(65);
276 ksmbd_debug(SMB
, "conn->dialect 0x%x\n", conn
->dialect
);
277 rsp
->DialectRevision
= cpu_to_le16(conn
->dialect
);
278 /* Not setting conn guid rsp->ServerGUID, as it
279 * not used by client for identifying connection
281 rsp
->Capabilities
= cpu_to_le32(conn
->vals
->capabilities
);
282 /* Default Max Message Size till SMB2.0, 64K*/
283 rsp
->MaxTransactSize
= cpu_to_le32(conn
->vals
->max_trans_size
);
284 rsp
->MaxReadSize
= cpu_to_le32(conn
->vals
->max_read_size
);
285 rsp
->MaxWriteSize
= cpu_to_le32(conn
->vals
->max_write_size
);
287 rsp
->SystemTime
= cpu_to_le64(ksmbd_systime());
288 rsp
->ServerStartTime
= 0;
290 rsp
->SecurityBufferOffset
= cpu_to_le16(128);
291 rsp
->SecurityBufferLength
= cpu_to_le16(AUTH_GSS_LENGTH
);
292 ksmbd_copy_gss_neg_header((char *)(&rsp
->hdr
) +
293 le16_to_cpu(rsp
->SecurityBufferOffset
));
294 rsp
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_ENABLED_LE
;
295 if (server_conf
.signing
== KSMBD_CONFIG_OPT_MANDATORY
)
296 rsp
->SecurityMode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE
;
297 err
= ksmbd_iov_pin_rsp(work
, rsp
,
298 sizeof(struct smb2_negotiate_rsp
) + AUTH_GSS_LENGTH
);
301 conn
->use_spnego
= true;
303 ksmbd_conn_set_need_negotiate(conn
);
308 * smb2_set_rsp_credits() - set number of credits in response buffer
309 * @work: smb work containing smb response buffer
311 int smb2_set_rsp_credits(struct ksmbd_work
*work
)
313 struct smb2_hdr
*req_hdr
= ksmbd_req_buf_next(work
);
314 struct smb2_hdr
*hdr
= ksmbd_resp_buf_next(work
);
315 struct ksmbd_conn
*conn
= work
->conn
;
316 unsigned short credits_requested
, aux_max
;
317 unsigned short credit_charge
, credits_granted
= 0;
319 if (work
->send_no_response
)
322 hdr
->CreditCharge
= req_hdr
->CreditCharge
;
324 if (conn
->total_credits
> conn
->vals
->max_credits
) {
325 hdr
->CreditRequest
= 0;
326 pr_err("Total credits overflow: %d\n", conn
->total_credits
);
330 credit_charge
= max_t(unsigned short,
331 le16_to_cpu(req_hdr
->CreditCharge
), 1);
332 if (credit_charge
> conn
->total_credits
) {
333 ksmbd_debug(SMB
, "Insufficient credits granted, given: %u, granted: %u\n",
334 credit_charge
, conn
->total_credits
);
338 conn
->total_credits
-= credit_charge
;
339 conn
->outstanding_credits
-= credit_charge
;
340 credits_requested
= max_t(unsigned short,
341 le16_to_cpu(req_hdr
->CreditRequest
), 1);
343 /* according to smb2.credits smbtorture, Windows server
344 * 2016 or later grant up to 8192 credits at once.
346 * TODO: Need to adjuct CreditRequest value according to
349 if (hdr
->Command
== SMB2_NEGOTIATE
)
352 aux_max
= conn
->vals
->max_credits
- conn
->total_credits
;
353 credits_granted
= min_t(unsigned short, credits_requested
, aux_max
);
355 conn
->total_credits
+= credits_granted
;
356 work
->credits_granted
+= credits_granted
;
358 if (!req_hdr
->NextCommand
) {
359 /* Update CreditRequest in last request */
360 hdr
->CreditRequest
= cpu_to_le16(work
->credits_granted
);
363 "credits: requested[%d] granted[%d] total_granted[%d]\n",
364 credits_requested
, credits_granted
,
365 conn
->total_credits
);
370 * init_chained_smb2_rsp() - initialize smb2 chained response
371 * @work: smb work containing smb response buffer
373 static void init_chained_smb2_rsp(struct ksmbd_work
*work
)
375 struct smb2_hdr
*req
= ksmbd_req_buf_next(work
);
376 struct smb2_hdr
*rsp
= ksmbd_resp_buf_next(work
);
377 struct smb2_hdr
*rsp_hdr
;
378 struct smb2_hdr
*rcv_hdr
;
379 int next_hdr_offset
= 0;
382 /* Len of this response = updated RFC len - offset of previous cmd
383 * in the compound rsp
386 /* Storing the current local FID which may be needed by subsequent
387 * command in the compound request
389 if (req
->Command
== SMB2_CREATE
&& rsp
->Status
== STATUS_SUCCESS
) {
390 work
->compound_fid
= ((struct smb2_create_rsp
*)rsp
)->VolatileFileId
;
391 work
->compound_pfid
= ((struct smb2_create_rsp
*)rsp
)->PersistentFileId
;
392 work
->compound_sid
= le64_to_cpu(rsp
->SessionId
);
395 len
= get_rfc1002_len(work
->response_buf
) - work
->next_smb2_rsp_hdr_off
;
396 next_hdr_offset
= le32_to_cpu(req
->NextCommand
);
398 new_len
= ALIGN(len
, 8);
399 work
->iov
[work
->iov_idx
].iov_len
+= (new_len
- len
);
400 inc_rfc1001_len(work
->response_buf
, new_len
- len
);
401 rsp
->NextCommand
= cpu_to_le32(new_len
);
403 work
->next_smb2_rcv_hdr_off
+= next_hdr_offset
;
404 work
->curr_smb2_rsp_hdr_off
= work
->next_smb2_rsp_hdr_off
;
405 work
->next_smb2_rsp_hdr_off
+= new_len
;
407 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
408 new_len
, work
->next_smb2_rcv_hdr_off
,
409 work
->next_smb2_rsp_hdr_off
);
411 rsp_hdr
= ksmbd_resp_buf_next(work
);
412 rcv_hdr
= ksmbd_req_buf_next(work
);
414 if (!(rcv_hdr
->Flags
& SMB2_FLAGS_RELATED_OPERATIONS
)) {
415 ksmbd_debug(SMB
, "related flag should be set\n");
416 work
->compound_fid
= KSMBD_NO_FID
;
417 work
->compound_pfid
= KSMBD_NO_FID
;
419 memset((char *)rsp_hdr
, 0, sizeof(struct smb2_hdr
) + 2);
420 rsp_hdr
->ProtocolId
= SMB2_PROTO_NUMBER
;
421 rsp_hdr
->StructureSize
= SMB2_HEADER_STRUCTURE_SIZE
;
422 rsp_hdr
->Command
= rcv_hdr
->Command
;
425 * Message is response. We don't grant oplock yet.
427 rsp_hdr
->Flags
= (SMB2_FLAGS_SERVER_TO_REDIR
|
428 SMB2_FLAGS_RELATED_OPERATIONS
);
429 rsp_hdr
->NextCommand
= 0;
430 rsp_hdr
->MessageId
= rcv_hdr
->MessageId
;
431 rsp_hdr
->Id
.SyncId
.ProcessId
= rcv_hdr
->Id
.SyncId
.ProcessId
;
432 rsp_hdr
->Id
.SyncId
.TreeId
= rcv_hdr
->Id
.SyncId
.TreeId
;
433 rsp_hdr
->SessionId
= rcv_hdr
->SessionId
;
434 memcpy(rsp_hdr
->Signature
, rcv_hdr
->Signature
, 16);
438 * is_chained_smb2_message() - check for chained command
439 * @work: smb work containing smb request buffer
441 * Return: true if chained request, otherwise false
443 bool is_chained_smb2_message(struct ksmbd_work
*work
)
445 struct smb2_hdr
*hdr
= smb2_get_msg(work
->request_buf
);
446 unsigned int len
, next_cmd
;
448 if (hdr
->ProtocolId
!= SMB2_PROTO_NUMBER
)
451 hdr
= ksmbd_req_buf_next(work
);
452 next_cmd
= le32_to_cpu(hdr
->NextCommand
);
454 if ((u64
)work
->next_smb2_rcv_hdr_off
+ next_cmd
+
455 __SMB2_HEADER_STRUCTURE_SIZE
>
456 get_rfc1002_len(work
->request_buf
)) {
457 pr_err("next command(%u) offset exceeds smb msg size\n",
462 if ((u64
)get_rfc1002_len(work
->response_buf
) + MAX_CIFS_SMALL_BUFFER_SIZE
>
464 pr_err("next response offset exceeds response buffer size\n");
468 ksmbd_debug(SMB
, "got SMB2 chained command\n");
469 init_chained_smb2_rsp(work
);
471 } else if (work
->next_smb2_rcv_hdr_off
) {
473 * This is last request in chained command,
474 * align response to 8 byte
476 len
= ALIGN(get_rfc1002_len(work
->response_buf
), 8);
477 len
= len
- get_rfc1002_len(work
->response_buf
);
479 ksmbd_debug(SMB
, "padding len %u\n", len
);
480 work
->iov
[work
->iov_idx
].iov_len
+= len
;
481 inc_rfc1001_len(work
->response_buf
, len
);
483 work
->curr_smb2_rsp_hdr_off
= work
->next_smb2_rsp_hdr_off
;
489 * init_smb2_rsp_hdr() - initialize smb2 response
490 * @work: smb work containing smb request buffer
494 int init_smb2_rsp_hdr(struct ksmbd_work
*work
)
496 struct smb2_hdr
*rsp_hdr
= smb2_get_msg(work
->response_buf
);
497 struct smb2_hdr
*rcv_hdr
= smb2_get_msg(work
->request_buf
);
499 memset(rsp_hdr
, 0, sizeof(struct smb2_hdr
) + 2);
500 rsp_hdr
->ProtocolId
= rcv_hdr
->ProtocolId
;
501 rsp_hdr
->StructureSize
= SMB2_HEADER_STRUCTURE_SIZE
;
502 rsp_hdr
->Command
= rcv_hdr
->Command
;
505 * Message is response. We don't grant oplock yet.
507 rsp_hdr
->Flags
= (SMB2_FLAGS_SERVER_TO_REDIR
);
508 rsp_hdr
->NextCommand
= 0;
509 rsp_hdr
->MessageId
= rcv_hdr
->MessageId
;
510 rsp_hdr
->Id
.SyncId
.ProcessId
= rcv_hdr
->Id
.SyncId
.ProcessId
;
511 rsp_hdr
->Id
.SyncId
.TreeId
= rcv_hdr
->Id
.SyncId
.TreeId
;
512 rsp_hdr
->SessionId
= rcv_hdr
->SessionId
;
513 memcpy(rsp_hdr
->Signature
, rcv_hdr
->Signature
, 16);
519 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
520 * @work: smb work containing smb request buffer
522 * Return: 0 on success, otherwise -ENOMEM
524 int smb2_allocate_rsp_buf(struct ksmbd_work
*work
)
526 struct smb2_hdr
*hdr
= smb2_get_msg(work
->request_buf
);
527 size_t small_sz
= MAX_CIFS_SMALL_BUFFER_SIZE
;
528 size_t large_sz
= small_sz
+ work
->conn
->vals
->max_trans_size
;
529 size_t sz
= small_sz
;
530 int cmd
= le16_to_cpu(hdr
->Command
);
532 if (cmd
== SMB2_IOCTL_HE
|| cmd
== SMB2_QUERY_DIRECTORY_HE
)
535 if (cmd
== SMB2_QUERY_INFO_HE
) {
536 struct smb2_query_info_req
*req
;
538 req
= smb2_get_msg(work
->request_buf
);
539 if ((req
->InfoType
== SMB2_O_INFO_FILE
&&
540 (req
->FileInfoClass
== FILE_FULL_EA_INFORMATION
||
541 req
->FileInfoClass
== FILE_ALL_INFORMATION
)) ||
542 req
->InfoType
== SMB2_O_INFO_SECURITY
)
546 /* allocate large response buf for chained commands */
547 if (le32_to_cpu(hdr
->NextCommand
) > 0)
550 work
->response_buf
= kvzalloc(sz
, GFP_KERNEL
);
551 if (!work
->response_buf
)
554 work
->response_sz
= sz
;
559 * smb2_check_user_session() - check for valid session for a user
560 * @work: smb work containing smb request buffer
562 * Return: 0 on success, otherwise error
564 int smb2_check_user_session(struct ksmbd_work
*work
)
566 struct smb2_hdr
*req_hdr
= ksmbd_req_buf_next(work
);
567 struct ksmbd_conn
*conn
= work
->conn
;
568 unsigned int cmd
= le16_to_cpu(req_hdr
->Command
);
569 unsigned long long sess_id
;
572 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
573 * require a session id, so no need to validate user session's for
576 if (cmd
== SMB2_ECHO_HE
|| cmd
== SMB2_NEGOTIATE_HE
||
577 cmd
== SMB2_SESSION_SETUP_HE
)
580 if (!ksmbd_conn_good(conn
))
583 sess_id
= le64_to_cpu(req_hdr
->SessionId
);
586 * If request is not the first in Compound request,
587 * Just validate session id in header with work->sess->id.
589 if (work
->next_smb2_rcv_hdr_off
) {
591 pr_err("The first operation in the compound does not have sess\n");
594 if (sess_id
!= ULLONG_MAX
&& work
->sess
->id
!= sess_id
) {
595 pr_err("session id(%llu) is different with the first operation(%lld)\n",
596 sess_id
, work
->sess
->id
);
602 /* Check for validity of user session */
603 work
->sess
= ksmbd_session_lookup_all(conn
, sess_id
);
606 ksmbd_debug(SMB
, "Invalid user session, Uid %llu\n", sess_id
);
610 static void destroy_previous_session(struct ksmbd_conn
*conn
,
611 struct ksmbd_user
*user
, u64 id
)
613 struct ksmbd_session
*prev_sess
= ksmbd_session_lookup_slowpath(id
);
614 struct ksmbd_user
*prev_user
;
615 struct channel
*chann
;
621 prev_user
= prev_sess
->user
;
624 strcmp(user
->name
, prev_user
->name
) ||
625 user
->passkey_sz
!= prev_user
->passkey_sz
||
626 memcmp(user
->passkey
, prev_user
->passkey
, user
->passkey_sz
))
629 prev_sess
->state
= SMB2_SESSION_EXPIRED
;
630 xa_for_each(&prev_sess
->ksmbd_chann_list
, index
, chann
)
631 ksmbd_conn_set_exiting(chann
->conn
);
635 * smb2_get_name() - get filename string from on the wire smb format
636 * @src: source buffer
637 * @maxlen: maxlen of source string
638 * @local_nls: nls_table pointer
640 * Return: matching converted filename on success, otherwise error ptr
643 smb2_get_name(const char *src
, const int maxlen
, struct nls_table
*local_nls
)
647 name
= smb_strndup_from_utf16(src
, maxlen
, 1, local_nls
);
649 pr_err("failed to get name %ld\n", PTR_ERR(name
));
653 ksmbd_conv_path_to_unix(name
);
654 ksmbd_strip_last_slash(name
);
658 int setup_async_work(struct ksmbd_work
*work
, void (*fn
)(void **), void **arg
)
660 struct ksmbd_conn
*conn
= work
->conn
;
663 id
= ksmbd_acquire_async_msg_id(&conn
->async_ida
);
665 pr_err("Failed to alloc async message id\n");
668 work
->asynchronous
= true;
672 "Send interim Response to inform async request id : %d\n",
675 work
->cancel_fn
= fn
;
676 work
->cancel_argv
= arg
;
678 if (list_empty(&work
->async_request_entry
)) {
679 spin_lock(&conn
->request_lock
);
680 list_add_tail(&work
->async_request_entry
, &conn
->async_requests
);
681 spin_unlock(&conn
->request_lock
);
687 void release_async_work(struct ksmbd_work
*work
)
689 struct ksmbd_conn
*conn
= work
->conn
;
691 spin_lock(&conn
->request_lock
);
692 list_del_init(&work
->async_request_entry
);
693 spin_unlock(&conn
->request_lock
);
695 work
->asynchronous
= 0;
696 work
->cancel_fn
= NULL
;
697 kfree(work
->cancel_argv
);
698 work
->cancel_argv
= NULL
;
699 if (work
->async_id
) {
700 ksmbd_release_id(&conn
->async_ida
, work
->async_id
);
705 void smb2_send_interim_resp(struct ksmbd_work
*work
, __le32 status
)
707 struct smb2_hdr
*rsp_hdr
;
708 struct ksmbd_work
*in_work
= ksmbd_alloc_work_struct();
710 if (allocate_interim_rsp_buf(in_work
)) {
711 pr_err("smb_allocate_rsp_buf failed!\n");
712 ksmbd_free_work_struct(in_work
);
716 in_work
->conn
= work
->conn
;
717 memcpy(smb2_get_msg(in_work
->response_buf
), ksmbd_resp_buf_next(work
),
718 __SMB2_HEADER_STRUCTURE_SIZE
);
720 rsp_hdr
= smb2_get_msg(in_work
->response_buf
);
721 rsp_hdr
->Flags
|= SMB2_FLAGS_ASYNC_COMMAND
;
722 rsp_hdr
->Id
.AsyncId
= cpu_to_le64(work
->async_id
);
723 smb2_set_err_rsp(in_work
);
724 rsp_hdr
->Status
= status
;
726 ksmbd_conn_write(in_work
);
727 ksmbd_free_work_struct(in_work
);
730 static __le32
smb2_get_reparse_tag_special_file(umode_t mode
)
732 if (S_ISDIR(mode
) || S_ISREG(mode
))
736 return IO_REPARSE_TAG_LX_SYMLINK_LE
;
737 else if (S_ISFIFO(mode
))
738 return IO_REPARSE_TAG_LX_FIFO_LE
;
739 else if (S_ISSOCK(mode
))
740 return IO_REPARSE_TAG_AF_UNIX_LE
;
741 else if (S_ISCHR(mode
))
742 return IO_REPARSE_TAG_LX_CHR_LE
;
743 else if (S_ISBLK(mode
))
744 return IO_REPARSE_TAG_LX_BLK_LE
;
750 * smb2_get_dos_mode() - get file mode in dos format from unix mode
751 * @stat: kstat containing file mode
752 * @attribute: attribute flags
754 * Return: converted dos mode
756 static int smb2_get_dos_mode(struct kstat
*stat
, int attribute
)
760 if (S_ISDIR(stat
->mode
)) {
761 attr
= FILE_ATTRIBUTE_DIRECTORY
|
762 (attribute
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
));
764 attr
= (attribute
& 0x00005137) | FILE_ATTRIBUTE_ARCHIVE
;
765 attr
&= ~(FILE_ATTRIBUTE_DIRECTORY
);
766 if (S_ISREG(stat
->mode
) && (server_conf
.share_fake_fscaps
&
767 FILE_SUPPORTS_SPARSE_FILES
))
768 attr
|= FILE_ATTRIBUTE_SPARSE_FILE
;
770 if (smb2_get_reparse_tag_special_file(stat
->mode
))
771 attr
|= FILE_ATTRIBUTE_REPARSE_POINT
;
777 static void build_preauth_ctxt(struct smb2_preauth_neg_context
*pneg_ctxt
,
780 pneg_ctxt
->ContextType
= SMB2_PREAUTH_INTEGRITY_CAPABILITIES
;
781 pneg_ctxt
->DataLength
= cpu_to_le16(38);
782 pneg_ctxt
->HashAlgorithmCount
= cpu_to_le16(1);
783 pneg_ctxt
->Reserved
= cpu_to_le32(0);
784 pneg_ctxt
->SaltLength
= cpu_to_le16(SMB311_SALT_SIZE
);
785 get_random_bytes(pneg_ctxt
->Salt
, SMB311_SALT_SIZE
);
786 pneg_ctxt
->HashAlgorithms
= hash_id
;
789 static void build_encrypt_ctxt(struct smb2_encryption_neg_context
*pneg_ctxt
,
792 pneg_ctxt
->ContextType
= SMB2_ENCRYPTION_CAPABILITIES
;
793 pneg_ctxt
->DataLength
= cpu_to_le16(4);
794 pneg_ctxt
->Reserved
= cpu_to_le32(0);
795 pneg_ctxt
->CipherCount
= cpu_to_le16(1);
796 pneg_ctxt
->Ciphers
[0] = cipher_type
;
799 static void build_sign_cap_ctxt(struct smb2_signing_capabilities
*pneg_ctxt
,
802 pneg_ctxt
->ContextType
= SMB2_SIGNING_CAPABILITIES
;
803 pneg_ctxt
->DataLength
=
804 cpu_to_le16((sizeof(struct smb2_signing_capabilities
) + 2)
805 - sizeof(struct smb2_neg_context
));
806 pneg_ctxt
->Reserved
= cpu_to_le32(0);
807 pneg_ctxt
->SigningAlgorithmCount
= cpu_to_le16(1);
808 pneg_ctxt
->SigningAlgorithms
[0] = sign_algo
;
811 static void build_posix_ctxt(struct smb2_posix_neg_context
*pneg_ctxt
)
813 pneg_ctxt
->ContextType
= SMB2_POSIX_EXTENSIONS_AVAILABLE
;
814 pneg_ctxt
->DataLength
= cpu_to_le16(POSIX_CTXT_DATA_LEN
);
815 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
816 pneg_ctxt
->Name
[0] = 0x93;
817 pneg_ctxt
->Name
[1] = 0xAD;
818 pneg_ctxt
->Name
[2] = 0x25;
819 pneg_ctxt
->Name
[3] = 0x50;
820 pneg_ctxt
->Name
[4] = 0x9C;
821 pneg_ctxt
->Name
[5] = 0xB4;
822 pneg_ctxt
->Name
[6] = 0x11;
823 pneg_ctxt
->Name
[7] = 0xE7;
824 pneg_ctxt
->Name
[8] = 0xB4;
825 pneg_ctxt
->Name
[9] = 0x23;
826 pneg_ctxt
->Name
[10] = 0x83;
827 pneg_ctxt
->Name
[11] = 0xDE;
828 pneg_ctxt
->Name
[12] = 0x96;
829 pneg_ctxt
->Name
[13] = 0x8B;
830 pneg_ctxt
->Name
[14] = 0xCD;
831 pneg_ctxt
->Name
[15] = 0x7C;
834 static unsigned int assemble_neg_contexts(struct ksmbd_conn
*conn
,
835 struct smb2_negotiate_rsp
*rsp
)
837 char * const pneg_ctxt
= (char *)rsp
+
838 le32_to_cpu(rsp
->NegotiateContextOffset
);
839 int neg_ctxt_cnt
= 1;
843 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
844 build_preauth_ctxt((struct smb2_preauth_neg_context
*)pneg_ctxt
,
845 conn
->preauth_info
->Preauth_HashId
);
846 ctxt_size
= sizeof(struct smb2_preauth_neg_context
);
848 if (conn
->cipher_type
) {
849 /* Round to 8 byte boundary */
850 ctxt_size
= round_up(ctxt_size
, 8);
852 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
853 build_encrypt_ctxt((struct smb2_encryption_neg_context
*)
854 (pneg_ctxt
+ ctxt_size
),
857 ctxt_size
+= sizeof(struct smb2_encryption_neg_context
) + 2;
860 /* compression context not yet supported */
861 WARN_ON(conn
->compress_algorithm
!= SMB3_COMPRESS_NONE
);
863 if (conn
->posix_ext_supported
) {
864 ctxt_size
= round_up(ctxt_size
, 8);
866 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
867 build_posix_ctxt((struct smb2_posix_neg_context
*)
868 (pneg_ctxt
+ ctxt_size
));
870 ctxt_size
+= sizeof(struct smb2_posix_neg_context
);
873 if (conn
->signing_negotiated
) {
874 ctxt_size
= round_up(ctxt_size
, 8);
876 "assemble SMB2_SIGNING_CAPABILITIES context\n");
877 build_sign_cap_ctxt((struct smb2_signing_capabilities
*)
878 (pneg_ctxt
+ ctxt_size
),
879 conn
->signing_algorithm
);
881 ctxt_size
+= sizeof(struct smb2_signing_capabilities
) + 2;
884 rsp
->NegotiateContextCount
= cpu_to_le16(neg_ctxt_cnt
);
885 return ctxt_size
+ AUTH_GSS_PADDING
;
888 static __le32
decode_preauth_ctxt(struct ksmbd_conn
*conn
,
889 struct smb2_preauth_neg_context
*pneg_ctxt
,
893 * sizeof(smb2_preauth_neg_context) assumes SMB311_SALT_SIZE Salt,
894 * which may not be present. Only check for used HashAlgorithms[1].
897 sizeof(struct smb2_neg_context
) + MIN_PREAUTH_CTXT_DATA_LEN
)
898 return STATUS_INVALID_PARAMETER
;
900 if (pneg_ctxt
->HashAlgorithms
!= SMB2_PREAUTH_INTEGRITY_SHA512
)
901 return STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP
;
903 conn
->preauth_info
->Preauth_HashId
= SMB2_PREAUTH_INTEGRITY_SHA512
;
904 return STATUS_SUCCESS
;
907 static void decode_encrypt_ctxt(struct ksmbd_conn
*conn
,
908 struct smb2_encryption_neg_context
*pneg_ctxt
,
914 if (sizeof(struct smb2_encryption_neg_context
) > ctxt_len
) {
915 pr_err("Invalid SMB2_ENCRYPTION_CAPABILITIES context size\n");
919 conn
->cipher_type
= 0;
921 cph_cnt
= le16_to_cpu(pneg_ctxt
->CipherCount
);
922 cphs_size
= cph_cnt
* sizeof(__le16
);
924 if (sizeof(struct smb2_encryption_neg_context
) + cphs_size
>
926 pr_err("Invalid cipher count(%d)\n", cph_cnt
);
930 if (server_conf
.flags
& KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION_OFF
)
933 for (i
= 0; i
< cph_cnt
; i
++) {
934 if (pneg_ctxt
->Ciphers
[i
] == SMB2_ENCRYPTION_AES128_GCM
||
935 pneg_ctxt
->Ciphers
[i
] == SMB2_ENCRYPTION_AES128_CCM
||
936 pneg_ctxt
->Ciphers
[i
] == SMB2_ENCRYPTION_AES256_CCM
||
937 pneg_ctxt
->Ciphers
[i
] == SMB2_ENCRYPTION_AES256_GCM
) {
938 ksmbd_debug(SMB
, "Cipher ID = 0x%x\n",
939 pneg_ctxt
->Ciphers
[i
]);
940 conn
->cipher_type
= pneg_ctxt
->Ciphers
[i
];
947 * smb3_encryption_negotiated() - checks if server and client agreed on enabling encryption
948 * @conn: smb connection
950 * Return: true if connection should be encrypted, else false
952 bool smb3_encryption_negotiated(struct ksmbd_conn
*conn
)
954 if (!conn
->ops
->generate_encryptionkey
)
958 * SMB 3.0 and 3.0.2 dialects use the SMB2_GLOBAL_CAP_ENCRYPTION flag.
959 * SMB 3.1.1 uses the cipher_type field.
961 return (conn
->vals
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
) ||
965 static void decode_compress_ctxt(struct ksmbd_conn
*conn
,
966 struct smb2_compression_capabilities_context
*pneg_ctxt
)
968 conn
->compress_algorithm
= SMB3_COMPRESS_NONE
;
971 static void decode_sign_cap_ctxt(struct ksmbd_conn
*conn
,
972 struct smb2_signing_capabilities
*pneg_ctxt
,
976 int i
, sign_alos_size
;
978 if (sizeof(struct smb2_signing_capabilities
) > ctxt_len
) {
979 pr_err("Invalid SMB2_SIGNING_CAPABILITIES context length\n");
983 conn
->signing_negotiated
= false;
984 sign_algo_cnt
= le16_to_cpu(pneg_ctxt
->SigningAlgorithmCount
);
985 sign_alos_size
= sign_algo_cnt
* sizeof(__le16
);
987 if (sizeof(struct smb2_signing_capabilities
) + sign_alos_size
>
989 pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt
);
993 for (i
= 0; i
< sign_algo_cnt
; i
++) {
994 if (pneg_ctxt
->SigningAlgorithms
[i
] == SIGNING_ALG_HMAC_SHA256_LE
||
995 pneg_ctxt
->SigningAlgorithms
[i
] == SIGNING_ALG_AES_CMAC_LE
) {
996 ksmbd_debug(SMB
, "Signing Algorithm ID = 0x%x\n",
997 pneg_ctxt
->SigningAlgorithms
[i
]);
998 conn
->signing_negotiated
= true;
999 conn
->signing_algorithm
=
1000 pneg_ctxt
->SigningAlgorithms
[i
];
1006 static __le32
deassemble_neg_contexts(struct ksmbd_conn
*conn
,
1007 struct smb2_negotiate_req
*req
,
1008 unsigned int len_of_smb
)
1010 /* +4 is to account for the RFC1001 len field */
1011 struct smb2_neg_context
*pctx
= (struct smb2_neg_context
*)req
;
1012 int i
= 0, len_of_ctxts
;
1013 unsigned int offset
= le32_to_cpu(req
->NegotiateContextOffset
);
1014 unsigned int neg_ctxt_cnt
= le16_to_cpu(req
->NegotiateContextCount
);
1015 __le32 status
= STATUS_INVALID_PARAMETER
;
1017 ksmbd_debug(SMB
, "decoding %d negotiate contexts\n", neg_ctxt_cnt
);
1018 if (len_of_smb
<= offset
) {
1019 ksmbd_debug(SMB
, "Invalid response: negotiate context offset\n");
1023 len_of_ctxts
= len_of_smb
- offset
;
1025 while (i
++ < neg_ctxt_cnt
) {
1028 if (len_of_ctxts
< (int)sizeof(struct smb2_neg_context
))
1031 pctx
= (struct smb2_neg_context
*)((char *)pctx
+ offset
);
1032 clen
= le16_to_cpu(pctx
->DataLength
);
1033 ctxt_len
= clen
+ sizeof(struct smb2_neg_context
);
1035 if (ctxt_len
> len_of_ctxts
)
1038 if (pctx
->ContextType
== SMB2_PREAUTH_INTEGRITY_CAPABILITIES
) {
1040 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
1041 if (conn
->preauth_info
->Preauth_HashId
)
1044 status
= decode_preauth_ctxt(conn
,
1045 (struct smb2_preauth_neg_context
*)pctx
,
1047 if (status
!= STATUS_SUCCESS
)
1049 } else if (pctx
->ContextType
== SMB2_ENCRYPTION_CAPABILITIES
) {
1051 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
1052 if (conn
->cipher_type
)
1055 decode_encrypt_ctxt(conn
,
1056 (struct smb2_encryption_neg_context
*)pctx
,
1058 } else if (pctx
->ContextType
== SMB2_COMPRESSION_CAPABILITIES
) {
1060 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
1061 if (conn
->compress_algorithm
)
1064 decode_compress_ctxt(conn
,
1065 (struct smb2_compression_capabilities_context
*)pctx
);
1066 } else if (pctx
->ContextType
== SMB2_NETNAME_NEGOTIATE_CONTEXT_ID
) {
1068 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
1069 } else if (pctx
->ContextType
== SMB2_POSIX_EXTENSIONS_AVAILABLE
) {
1071 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
1072 conn
->posix_ext_supported
= true;
1073 } else if (pctx
->ContextType
== SMB2_SIGNING_CAPABILITIES
) {
1075 "deassemble SMB2_SIGNING_CAPABILITIES context\n");
1077 decode_sign_cap_ctxt(conn
,
1078 (struct smb2_signing_capabilities
*)pctx
,
1082 /* offsets must be 8 byte aligned */
1083 offset
= (ctxt_len
+ 7) & ~0x7;
1084 len_of_ctxts
-= offset
;
1090 * smb2_handle_negotiate() - handler for smb2 negotiate command
1091 * @work: smb work containing smb request buffer
1095 int smb2_handle_negotiate(struct ksmbd_work
*work
)
1097 struct ksmbd_conn
*conn
= work
->conn
;
1098 struct smb2_negotiate_req
*req
= smb2_get_msg(work
->request_buf
);
1099 struct smb2_negotiate_rsp
*rsp
= smb2_get_msg(work
->response_buf
);
1101 unsigned int smb2_buf_len
, smb2_neg_size
, neg_ctxt_len
= 0;
1104 ksmbd_debug(SMB
, "Received negotiate request\n");
1105 conn
->need_neg
= false;
1106 if (ksmbd_conn_good(conn
)) {
1107 pr_err("conn->tcp_status is already in CifsGood State\n");
1108 work
->send_no_response
= 1;
1112 smb2_buf_len
= get_rfc1002_len(work
->request_buf
);
1113 smb2_neg_size
= offsetof(struct smb2_negotiate_req
, Dialects
);
1114 if (smb2_neg_size
> smb2_buf_len
) {
1115 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1120 if (req
->DialectCount
== 0) {
1121 pr_err("malformed packet\n");
1122 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1127 if (conn
->dialect
== SMB311_PROT_ID
) {
1128 unsigned int nego_ctxt_off
= le32_to_cpu(req
->NegotiateContextOffset
);
1130 if (smb2_buf_len
< nego_ctxt_off
) {
1131 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1136 if (smb2_neg_size
> nego_ctxt_off
) {
1137 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1142 if (smb2_neg_size
+ le16_to_cpu(req
->DialectCount
) * sizeof(__le16
) >
1144 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1149 if (smb2_neg_size
+ le16_to_cpu(req
->DialectCount
) * sizeof(__le16
) >
1151 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1157 conn
->cli_cap
= le32_to_cpu(req
->Capabilities
);
1158 switch (conn
->dialect
) {
1159 case SMB311_PROT_ID
:
1160 conn
->preauth_info
=
1161 kzalloc(sizeof(struct preauth_integrity_info
),
1163 if (!conn
->preauth_info
) {
1165 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1169 status
= deassemble_neg_contexts(conn
, req
,
1170 get_rfc1002_len(work
->request_buf
));
1171 if (status
!= STATUS_SUCCESS
) {
1172 pr_err("deassemble_neg_contexts error(0x%x)\n",
1174 rsp
->hdr
.Status
= status
;
1176 kfree(conn
->preauth_info
);
1177 conn
->preauth_info
= NULL
;
1181 rc
= init_smb3_11_server(conn
);
1183 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1184 kfree(conn
->preauth_info
);
1185 conn
->preauth_info
= NULL
;
1189 ksmbd_gen_preauth_integrity_hash(conn
,
1191 conn
->preauth_info
->Preauth_HashValue
);
1192 rsp
->NegotiateContextOffset
=
1193 cpu_to_le32(OFFSET_OF_NEG_CONTEXT
);
1194 neg_ctxt_len
= assemble_neg_contexts(conn
, rsp
);
1196 case SMB302_PROT_ID
:
1197 init_smb3_02_server(conn
);
1200 init_smb3_0_server(conn
);
1203 init_smb2_1_server(conn
);
1208 ksmbd_debug(SMB
, "Server dialect :0x%x not supported\n",
1210 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
1214 rsp
->Capabilities
= cpu_to_le32(conn
->vals
->capabilities
);
1217 conn
->connection_type
= conn
->dialect
;
1219 rsp
->MaxTransactSize
= cpu_to_le32(conn
->vals
->max_trans_size
);
1220 rsp
->MaxReadSize
= cpu_to_le32(conn
->vals
->max_read_size
);
1221 rsp
->MaxWriteSize
= cpu_to_le32(conn
->vals
->max_write_size
);
1223 memcpy(conn
->ClientGUID
, req
->ClientGUID
,
1224 SMB2_CLIENT_GUID_SIZE
);
1225 conn
->cli_sec_mode
= le16_to_cpu(req
->SecurityMode
);
1227 rsp
->StructureSize
= cpu_to_le16(65);
1228 rsp
->DialectRevision
= cpu_to_le16(conn
->dialect
);
1229 /* Not setting conn guid rsp->ServerGUID, as it
1230 * not used by client for identifying server
1232 memset(rsp
->ServerGUID
, 0, SMB2_CLIENT_GUID_SIZE
);
1234 rsp
->SystemTime
= cpu_to_le64(ksmbd_systime());
1235 rsp
->ServerStartTime
= 0;
1236 ksmbd_debug(SMB
, "negotiate context offset %d, count %d\n",
1237 le32_to_cpu(rsp
->NegotiateContextOffset
),
1238 le16_to_cpu(rsp
->NegotiateContextCount
));
1240 rsp
->SecurityBufferOffset
= cpu_to_le16(128);
1241 rsp
->SecurityBufferLength
= cpu_to_le16(AUTH_GSS_LENGTH
);
1242 ksmbd_copy_gss_neg_header((char *)(&rsp
->hdr
) +
1243 le16_to_cpu(rsp
->SecurityBufferOffset
));
1245 rsp
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_ENABLED_LE
;
1246 conn
->use_spnego
= true;
1248 if ((server_conf
.signing
== KSMBD_CONFIG_OPT_AUTO
||
1249 server_conf
.signing
== KSMBD_CONFIG_OPT_DISABLED
) &&
1250 req
->SecurityMode
& SMB2_NEGOTIATE_SIGNING_REQUIRED_LE
)
1252 else if (server_conf
.signing
== KSMBD_CONFIG_OPT_MANDATORY
) {
1253 server_conf
.enforced_signing
= true;
1254 rsp
->SecurityMode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE
;
1258 conn
->srv_sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
1259 ksmbd_conn_set_need_negotiate(conn
);
1263 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
1266 rc
= ksmbd_iov_pin_rsp(work
, rsp
,
1267 sizeof(struct smb2_negotiate_rsp
) +
1268 AUTH_GSS_LENGTH
+ neg_ctxt_len
);
1270 smb2_set_err_rsp(work
);
1274 static int alloc_preauth_hash(struct ksmbd_session
*sess
,
1275 struct ksmbd_conn
*conn
)
1277 if (sess
->Preauth_HashValue
)
1280 sess
->Preauth_HashValue
= kmemdup(conn
->preauth_info
->Preauth_HashValue
,
1281 PREAUTH_HASHVALUE_SIZE
, GFP_KERNEL
);
1282 if (!sess
->Preauth_HashValue
)
1288 static int generate_preauth_hash(struct ksmbd_work
*work
)
1290 struct ksmbd_conn
*conn
= work
->conn
;
1291 struct ksmbd_session
*sess
= work
->sess
;
1294 if (conn
->dialect
!= SMB311_PROT_ID
)
1297 if (conn
->binding
) {
1298 struct preauth_session
*preauth_sess
;
1300 preauth_sess
= ksmbd_preauth_session_lookup(conn
, sess
->id
);
1301 if (!preauth_sess
) {
1302 preauth_sess
= ksmbd_preauth_session_alloc(conn
, sess
->id
);
1307 preauth_hash
= preauth_sess
->Preauth_HashValue
;
1309 if (!sess
->Preauth_HashValue
)
1310 if (alloc_preauth_hash(sess
, conn
))
1312 preauth_hash
= sess
->Preauth_HashValue
;
1315 ksmbd_gen_preauth_integrity_hash(conn
, work
->request_buf
, preauth_hash
);
1319 static int decode_negotiation_token(struct ksmbd_conn
*conn
,
1320 struct negotiate_message
*negblob
,
1323 if (!conn
->use_spnego
)
1326 if (ksmbd_decode_negTokenInit((char *)negblob
, sz
, conn
)) {
1327 if (ksmbd_decode_negTokenTarg((char *)negblob
, sz
, conn
)) {
1328 conn
->auth_mechs
|= KSMBD_AUTH_NTLMSSP
;
1329 conn
->preferred_auth_mech
= KSMBD_AUTH_NTLMSSP
;
1330 conn
->use_spnego
= false;
1336 static int ntlm_negotiate(struct ksmbd_work
*work
,
1337 struct negotiate_message
*negblob
,
1338 size_t negblob_len
, struct smb2_sess_setup_rsp
*rsp
)
1340 struct challenge_message
*chgblob
;
1341 unsigned char *spnego_blob
= NULL
;
1342 u16 spnego_blob_len
;
1346 ksmbd_debug(SMB
, "negotiate phase\n");
1347 rc
= ksmbd_decode_ntlmssp_neg_blob(negblob
, negblob_len
, work
->conn
);
1351 sz
= le16_to_cpu(rsp
->SecurityBufferOffset
);
1353 (struct challenge_message
*)((char *)&rsp
->hdr
.ProtocolId
+ sz
);
1354 memset(chgblob
, 0, sizeof(struct challenge_message
));
1356 if (!work
->conn
->use_spnego
) {
1357 sz
= ksmbd_build_ntlmssp_challenge_blob(chgblob
, work
->conn
);
1361 rsp
->SecurityBufferLength
= cpu_to_le16(sz
);
1365 sz
= sizeof(struct challenge_message
);
1366 sz
+= (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1368 neg_blob
= kzalloc(sz
, GFP_KERNEL
);
1372 chgblob
= (struct challenge_message
*)neg_blob
;
1373 sz
= ksmbd_build_ntlmssp_challenge_blob(chgblob
, work
->conn
);
1379 rc
= build_spnego_ntlmssp_neg_blob(&spnego_blob
, &spnego_blob_len
,
1386 sz
= le16_to_cpu(rsp
->SecurityBufferOffset
);
1387 memcpy((char *)&rsp
->hdr
.ProtocolId
+ sz
, spnego_blob
, spnego_blob_len
);
1388 rsp
->SecurityBufferLength
= cpu_to_le16(spnego_blob_len
);
1396 static struct authenticate_message
*user_authblob(struct ksmbd_conn
*conn
,
1397 struct smb2_sess_setup_req
*req
)
1401 if (conn
->use_spnego
&& conn
->mechToken
)
1402 return (struct authenticate_message
*)conn
->mechToken
;
1404 sz
= le16_to_cpu(req
->SecurityBufferOffset
);
1405 return (struct authenticate_message
*)((char *)&req
->hdr
.ProtocolId
1409 static struct ksmbd_user
*session_user(struct ksmbd_conn
*conn
,
1410 struct smb2_sess_setup_req
*req
)
1412 struct authenticate_message
*authblob
;
1413 struct ksmbd_user
*user
;
1415 unsigned int name_off
, name_len
, secbuf_len
;
1417 secbuf_len
= le16_to_cpu(req
->SecurityBufferLength
);
1418 if (secbuf_len
< sizeof(struct authenticate_message
)) {
1419 ksmbd_debug(SMB
, "blob len %d too small\n", secbuf_len
);
1422 authblob
= user_authblob(conn
, req
);
1423 name_off
= le32_to_cpu(authblob
->UserName
.BufferOffset
);
1424 name_len
= le16_to_cpu(authblob
->UserName
.Length
);
1426 if (secbuf_len
< (u64
)name_off
+ name_len
)
1429 name
= smb_strndup_from_utf16((const char *)authblob
+ name_off
,
1434 pr_err("cannot allocate memory\n");
1438 ksmbd_debug(SMB
, "session setup request for user %s\n", name
);
1439 user
= ksmbd_login_user(name
);
1444 static int ntlm_authenticate(struct ksmbd_work
*work
,
1445 struct smb2_sess_setup_req
*req
,
1446 struct smb2_sess_setup_rsp
*rsp
)
1448 struct ksmbd_conn
*conn
= work
->conn
;
1449 struct ksmbd_session
*sess
= work
->sess
;
1450 struct channel
*chann
= NULL
;
1451 struct ksmbd_user
*user
;
1455 ksmbd_debug(SMB
, "authenticate phase\n");
1456 if (conn
->use_spnego
) {
1457 unsigned char *spnego_blob
;
1458 u16 spnego_blob_len
;
1460 rc
= build_spnego_ntlmssp_auth_blob(&spnego_blob
,
1466 sz
= le16_to_cpu(rsp
->SecurityBufferOffset
);
1467 memcpy((char *)&rsp
->hdr
.ProtocolId
+ sz
, spnego_blob
, spnego_blob_len
);
1468 rsp
->SecurityBufferLength
= cpu_to_le16(spnego_blob_len
);
1472 user
= session_user(conn
, req
);
1474 ksmbd_debug(SMB
, "Unknown user name or an error\n");
1478 /* Check for previous session */
1479 prev_id
= le64_to_cpu(req
->PreviousSessionId
);
1480 if (prev_id
&& prev_id
!= sess
->id
)
1481 destroy_previous_session(conn
, user
, prev_id
);
1483 if (sess
->state
== SMB2_SESSION_VALID
) {
1485 * Reuse session if anonymous try to connect
1486 * on reauthetication.
1488 if (conn
->binding
== false && ksmbd_anonymous_user(user
)) {
1489 ksmbd_free_user(user
);
1493 if (!ksmbd_compare_user(sess
->user
, user
)) {
1494 ksmbd_free_user(user
);
1497 ksmbd_free_user(user
);
1502 if (conn
->binding
== false && user_guest(sess
->user
)) {
1503 rsp
->SessionFlags
= SMB2_SESSION_FLAG_IS_GUEST_LE
;
1505 struct authenticate_message
*authblob
;
1507 authblob
= user_authblob(conn
, req
);
1508 sz
= le16_to_cpu(req
->SecurityBufferLength
);
1509 rc
= ksmbd_decode_ntlmssp_auth_blob(authblob
, sz
, conn
, sess
);
1511 set_user_flag(sess
->user
, KSMBD_USER_FLAG_BAD_PASSWORD
);
1512 ksmbd_debug(SMB
, "authentication failed\n");
1518 * If session state is SMB2_SESSION_VALID, We can assume
1519 * that it is reauthentication. And the user/password
1520 * has been verified, so return it here.
1522 if (sess
->state
== SMB2_SESSION_VALID
) {
1524 goto binding_session
;
1528 if ((rsp
->SessionFlags
!= SMB2_SESSION_FLAG_IS_GUEST_LE
&&
1529 (conn
->sign
|| server_conf
.enforced_signing
)) ||
1530 (req
->SecurityMode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
))
1533 if (smb3_encryption_negotiated(conn
) &&
1534 !(req
->Flags
& SMB2_SESSION_REQ_FLAG_BINDING
)) {
1535 rc
= conn
->ops
->generate_encryptionkey(conn
, sess
);
1538 "SMB3 encryption key generation failed\n");
1542 if (server_conf
.flags
& KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION
)
1543 rsp
->SessionFlags
= SMB2_SESSION_FLAG_ENCRYPT_DATA_LE
;
1545 * signing is disable if encryption is enable
1552 if (conn
->dialect
>= SMB30_PROT_ID
) {
1553 chann
= lookup_chann_list(sess
, conn
);
1555 chann
= kmalloc(sizeof(struct channel
), GFP_KERNEL
);
1560 xa_store(&sess
->ksmbd_chann_list
, (long)conn
, chann
, GFP_KERNEL
);
1564 if (conn
->ops
->generate_signingkey
) {
1565 rc
= conn
->ops
->generate_signingkey(sess
, conn
);
1567 ksmbd_debug(SMB
, "SMB3 signing key generation failed\n");
1572 if (!ksmbd_conn_lookup_dialect(conn
)) {
1573 pr_err("fail to verify the dialect\n");
1579 #ifdef CONFIG_SMB_SERVER_KERBEROS5
1580 static int krb5_authenticate(struct ksmbd_work
*work
,
1581 struct smb2_sess_setup_req
*req
,
1582 struct smb2_sess_setup_rsp
*rsp
)
1584 struct ksmbd_conn
*conn
= work
->conn
;
1585 struct ksmbd_session
*sess
= work
->sess
;
1586 char *in_blob
, *out_blob
;
1587 struct channel
*chann
= NULL
;
1589 int in_len
, out_len
;
1592 in_blob
= (char *)&req
->hdr
.ProtocolId
+
1593 le16_to_cpu(req
->SecurityBufferOffset
);
1594 in_len
= le16_to_cpu(req
->SecurityBufferLength
);
1595 out_blob
= (char *)&rsp
->hdr
.ProtocolId
+
1596 le16_to_cpu(rsp
->SecurityBufferOffset
);
1597 out_len
= work
->response_sz
-
1598 (le16_to_cpu(rsp
->SecurityBufferOffset
) + 4);
1600 /* Check previous session */
1601 prev_sess_id
= le64_to_cpu(req
->PreviousSessionId
);
1602 if (prev_sess_id
&& prev_sess_id
!= sess
->id
)
1603 destroy_previous_session(conn
, sess
->user
, prev_sess_id
);
1605 if (sess
->state
== SMB2_SESSION_VALID
)
1606 ksmbd_free_user(sess
->user
);
1608 retval
= ksmbd_krb5_authenticate(sess
, in_blob
, in_len
,
1609 out_blob
, &out_len
);
1611 ksmbd_debug(SMB
, "krb5 authentication failed\n");
1614 rsp
->SecurityBufferLength
= cpu_to_le16(out_len
);
1616 if ((conn
->sign
|| server_conf
.enforced_signing
) ||
1617 (req
->SecurityMode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
))
1620 if (smb3_encryption_negotiated(conn
)) {
1621 retval
= conn
->ops
->generate_encryptionkey(conn
, sess
);
1624 "SMB3 encryption key generation failed\n");
1628 if (server_conf
.flags
& KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION
)
1629 rsp
->SessionFlags
= SMB2_SESSION_FLAG_ENCRYPT_DATA_LE
;
1633 if (conn
->dialect
>= SMB30_PROT_ID
) {
1634 chann
= lookup_chann_list(sess
, conn
);
1636 chann
= kmalloc(sizeof(struct channel
), GFP_KERNEL
);
1641 xa_store(&sess
->ksmbd_chann_list
, (long)conn
, chann
, GFP_KERNEL
);
1645 if (conn
->ops
->generate_signingkey
) {
1646 retval
= conn
->ops
->generate_signingkey(sess
, conn
);
1648 ksmbd_debug(SMB
, "SMB3 signing key generation failed\n");
1653 if (!ksmbd_conn_lookup_dialect(conn
)) {
1654 pr_err("fail to verify the dialect\n");
1660 static int krb5_authenticate(struct ksmbd_work
*work
,
1661 struct smb2_sess_setup_req
*req
,
1662 struct smb2_sess_setup_rsp
*rsp
)
1668 int smb2_sess_setup(struct ksmbd_work
*work
)
1670 struct ksmbd_conn
*conn
= work
->conn
;
1671 struct smb2_sess_setup_req
*req
;
1672 struct smb2_sess_setup_rsp
*rsp
;
1673 struct ksmbd_session
*sess
;
1674 struct negotiate_message
*negblob
;
1675 unsigned int negblob_len
, negblob_off
;
1678 ksmbd_debug(SMB
, "Received request for session setup\n");
1680 WORK_BUFFERS(work
, req
, rsp
);
1682 rsp
->StructureSize
= cpu_to_le16(9);
1683 rsp
->SessionFlags
= 0;
1684 rsp
->SecurityBufferOffset
= cpu_to_le16(72);
1685 rsp
->SecurityBufferLength
= 0;
1687 ksmbd_conn_lock(conn
);
1688 if (!req
->hdr
.SessionId
) {
1689 sess
= ksmbd_smb2_session_create();
1694 rsp
->hdr
.SessionId
= cpu_to_le64(sess
->id
);
1695 rc
= ksmbd_session_register(conn
, sess
);
1698 } else if (conn
->dialect
>= SMB30_PROT_ID
&&
1699 (server_conf
.flags
& KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL
) &&
1700 req
->Flags
& SMB2_SESSION_REQ_FLAG_BINDING
) {
1701 u64 sess_id
= le64_to_cpu(req
->hdr
.SessionId
);
1703 sess
= ksmbd_session_lookup_slowpath(sess_id
);
1709 if (conn
->dialect
!= sess
->dialect
) {
1714 if (!(req
->hdr
.Flags
& SMB2_FLAGS_SIGNED
)) {
1719 if (strncmp(conn
->ClientGUID
, sess
->ClientGUID
,
1720 SMB2_CLIENT_GUID_SIZE
)) {
1725 if (sess
->state
== SMB2_SESSION_IN_PROGRESS
) {
1730 if (sess
->state
== SMB2_SESSION_EXPIRED
) {
1735 if (ksmbd_conn_need_reconnect(conn
)) {
1741 if (ksmbd_session_lookup(conn
, sess_id
)) {
1746 if (user_guest(sess
->user
)) {
1751 conn
->binding
= true;
1752 } else if ((conn
->dialect
< SMB30_PROT_ID
||
1753 server_conf
.flags
& KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL
) &&
1754 (req
->Flags
& SMB2_SESSION_REQ_FLAG_BINDING
)) {
1759 sess
= ksmbd_session_lookup(conn
,
1760 le64_to_cpu(req
->hdr
.SessionId
));
1766 if (sess
->state
== SMB2_SESSION_EXPIRED
) {
1771 if (ksmbd_conn_need_reconnect(conn
)) {
1779 negblob_off
= le16_to_cpu(req
->SecurityBufferOffset
);
1780 negblob_len
= le16_to_cpu(req
->SecurityBufferLength
);
1781 if (negblob_off
< offsetof(struct smb2_sess_setup_req
, Buffer
) ||
1782 negblob_len
< offsetof(struct negotiate_message
, NegotiateFlags
)) {
1787 negblob
= (struct negotiate_message
*)((char *)&req
->hdr
.ProtocolId
+
1790 if (decode_negotiation_token(conn
, negblob
, negblob_len
) == 0) {
1791 if (conn
->mechToken
)
1792 negblob
= (struct negotiate_message
*)conn
->mechToken
;
1795 if (server_conf
.auth_mechs
& conn
->auth_mechs
) {
1796 rc
= generate_preauth_hash(work
);
1800 if (conn
->preferred_auth_mech
&
1801 (KSMBD_AUTH_KRB5
| KSMBD_AUTH_MSKRB5
)) {
1802 rc
= krb5_authenticate(work
, req
, rsp
);
1808 if (!ksmbd_conn_need_reconnect(conn
)) {
1809 ksmbd_conn_set_good(conn
);
1810 sess
->state
= SMB2_SESSION_VALID
;
1812 kfree(sess
->Preauth_HashValue
);
1813 sess
->Preauth_HashValue
= NULL
;
1814 } else if (conn
->preferred_auth_mech
== KSMBD_AUTH_NTLMSSP
) {
1815 if (negblob
->MessageType
== NtLmNegotiate
) {
1816 rc
= ntlm_negotiate(work
, negblob
, negblob_len
, rsp
);
1820 STATUS_MORE_PROCESSING_REQUIRED
;
1821 } else if (negblob
->MessageType
== NtLmAuthenticate
) {
1822 rc
= ntlm_authenticate(work
, req
, rsp
);
1826 if (!ksmbd_conn_need_reconnect(conn
)) {
1827 ksmbd_conn_set_good(conn
);
1828 sess
->state
= SMB2_SESSION_VALID
;
1830 if (conn
->binding
) {
1831 struct preauth_session
*preauth_sess
;
1834 ksmbd_preauth_session_lookup(conn
, sess
->id
);
1836 list_del(&preauth_sess
->preauth_entry
);
1837 kfree(preauth_sess
);
1840 kfree(sess
->Preauth_HashValue
);
1841 sess
->Preauth_HashValue
= NULL
;
1843 pr_info_ratelimited("Unknown NTLMSSP message type : 0x%x\n",
1844 le32_to_cpu(negblob
->MessageType
));
1848 /* TODO: need one more negotiation */
1849 pr_err("Not support the preferred authentication\n");
1853 pr_err("Not support authentication\n");
1859 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1860 else if (rc
== -ENOENT
)
1861 rsp
->hdr
.Status
= STATUS_USER_SESSION_DELETED
;
1862 else if (rc
== -EACCES
)
1863 rsp
->hdr
.Status
= STATUS_REQUEST_NOT_ACCEPTED
;
1864 else if (rc
== -EFAULT
)
1865 rsp
->hdr
.Status
= STATUS_NETWORK_SESSION_EXPIRED
;
1866 else if (rc
== -ENOMEM
)
1867 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
1868 else if (rc
== -EOPNOTSUPP
)
1869 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
1871 rsp
->hdr
.Status
= STATUS_LOGON_FAILURE
;
1873 if (conn
->use_spnego
&& conn
->mechToken
) {
1874 kfree(conn
->mechToken
);
1875 conn
->mechToken
= NULL
;
1880 * SecurityBufferOffset should be set to zero
1881 * in session setup error response.
1883 rsp
->SecurityBufferOffset
= 0;
1886 bool try_delay
= false;
1889 * To avoid dictionary attacks (repeated session setups rapidly sent) to
1890 * connect to server, ksmbd make a delay of a 5 seconds on session setup
1891 * failure to make it harder to send enough random connection requests
1892 * to break into a server.
1894 if (sess
->user
&& sess
->user
->flags
& KSMBD_USER_FLAG_DELAY_SESSION
)
1897 sess
->last_active
= jiffies
;
1898 sess
->state
= SMB2_SESSION_EXPIRED
;
1900 ksmbd_conn_set_need_reconnect(conn
);
1902 ksmbd_conn_set_need_negotiate(conn
);
1905 smb2_set_err_rsp(work
);
1907 unsigned int iov_len
;
1909 if (rsp
->SecurityBufferLength
)
1910 iov_len
= offsetof(struct smb2_sess_setup_rsp
, Buffer
) +
1911 le16_to_cpu(rsp
->SecurityBufferLength
);
1913 iov_len
= sizeof(struct smb2_sess_setup_rsp
);
1914 rc
= ksmbd_iov_pin_rsp(work
, rsp
, iov_len
);
1916 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
1919 ksmbd_conn_unlock(conn
);
1924 * smb2_tree_connect() - handler for smb2 tree connect command
1925 * @work: smb work containing smb request buffer
1927 * Return: 0 on success, otherwise error
1929 int smb2_tree_connect(struct ksmbd_work
*work
)
1931 struct ksmbd_conn
*conn
= work
->conn
;
1932 struct smb2_tree_connect_req
*req
;
1933 struct smb2_tree_connect_rsp
*rsp
;
1934 struct ksmbd_session
*sess
= work
->sess
;
1935 char *treename
= NULL
, *name
= NULL
;
1936 struct ksmbd_tree_conn_status status
;
1937 struct ksmbd_share_config
*share
;
1940 WORK_BUFFERS(work
, req
, rsp
);
1942 treename
= smb_strndup_from_utf16(req
->Buffer
,
1943 le16_to_cpu(req
->PathLength
), true,
1945 if (IS_ERR(treename
)) {
1946 pr_err("treename is NULL\n");
1947 status
.ret
= KSMBD_TREE_CONN_STATUS_ERROR
;
1951 name
= ksmbd_extract_sharename(conn
->um
, treename
);
1953 status
.ret
= KSMBD_TREE_CONN_STATUS_ERROR
;
1957 ksmbd_debug(SMB
, "tree connect request for tree %s treename %s\n",
1960 status
= ksmbd_tree_conn_connect(conn
, sess
, name
);
1961 if (status
.ret
== KSMBD_TREE_CONN_STATUS_OK
)
1962 rsp
->hdr
.Id
.SyncId
.TreeId
= cpu_to_le32(status
.tree_conn
->id
);
1966 share
= status
.tree_conn
->share_conf
;
1967 if (test_share_config_flag(share
, KSMBD_SHARE_FLAG_PIPE
)) {
1968 ksmbd_debug(SMB
, "IPC share path request\n");
1969 rsp
->ShareType
= SMB2_SHARE_TYPE_PIPE
;
1970 rsp
->MaximalAccess
= FILE_READ_DATA_LE
| FILE_READ_EA_LE
|
1971 FILE_EXECUTE_LE
| FILE_READ_ATTRIBUTES_LE
|
1972 FILE_DELETE_LE
| FILE_READ_CONTROL_LE
|
1973 FILE_WRITE_DAC_LE
| FILE_WRITE_OWNER_LE
|
1974 FILE_SYNCHRONIZE_LE
;
1976 rsp
->ShareType
= SMB2_SHARE_TYPE_DISK
;
1977 rsp
->MaximalAccess
= FILE_READ_DATA_LE
| FILE_READ_EA_LE
|
1978 FILE_EXECUTE_LE
| FILE_READ_ATTRIBUTES_LE
;
1979 if (test_tree_conn_flag(status
.tree_conn
,
1980 KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
1981 rsp
->MaximalAccess
|= FILE_WRITE_DATA_LE
|
1982 FILE_APPEND_DATA_LE
| FILE_WRITE_EA_LE
|
1983 FILE_DELETE_LE
| FILE_WRITE_ATTRIBUTES_LE
|
1984 FILE_DELETE_CHILD_LE
| FILE_READ_CONTROL_LE
|
1985 FILE_WRITE_DAC_LE
| FILE_WRITE_OWNER_LE
|
1986 FILE_SYNCHRONIZE_LE
;
1990 status
.tree_conn
->maximal_access
= le32_to_cpu(rsp
->MaximalAccess
);
1991 if (conn
->posix_ext_supported
)
1992 status
.tree_conn
->posix_extensions
= true;
1994 write_lock(&sess
->tree_conns_lock
);
1995 status
.tree_conn
->t_state
= TREE_CONNECTED
;
1996 write_unlock(&sess
->tree_conns_lock
);
1997 rsp
->StructureSize
= cpu_to_le16(16);
1999 rsp
->Capabilities
= 0;
2001 /* default manual caching */
2002 rsp
->ShareFlags
= SMB2_SHAREFLAG_MANUAL_CACHING
;
2004 rc
= ksmbd_iov_pin_rsp(work
, rsp
, sizeof(struct smb2_tree_connect_rsp
));
2006 status
.ret
= KSMBD_TREE_CONN_STATUS_NOMEM
;
2008 if (!IS_ERR(treename
))
2013 switch (status
.ret
) {
2014 case KSMBD_TREE_CONN_STATUS_OK
:
2015 rsp
->hdr
.Status
= STATUS_SUCCESS
;
2020 case KSMBD_TREE_CONN_STATUS_NO_SHARE
:
2021 rsp
->hdr
.Status
= STATUS_BAD_NETWORK_NAME
;
2024 case KSMBD_TREE_CONN_STATUS_NOMEM
:
2025 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
2027 case KSMBD_TREE_CONN_STATUS_ERROR
:
2028 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS
:
2029 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS
:
2030 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
2033 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
2036 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
2039 if (status
.ret
!= KSMBD_TREE_CONN_STATUS_OK
)
2040 smb2_set_err_rsp(work
);
2046 * smb2_create_open_flags() - convert smb open flags to unix open flags
2047 * @file_present: is file already present
2048 * @access: file access flags
2049 * @disposition: file disposition flags
2050 * @may_flags: set with MAY_ flags
2052 * Return: file open flags
2054 static int smb2_create_open_flags(bool file_present
, __le32 access
,
2058 int oflags
= O_NONBLOCK
| O_LARGEFILE
;
2060 if (access
& FILE_READ_DESIRED_ACCESS_LE
&&
2061 access
& FILE_WRITE_DESIRE_ACCESS_LE
) {
2063 *may_flags
= MAY_OPEN
| MAY_READ
| MAY_WRITE
;
2064 } else if (access
& FILE_WRITE_DESIRE_ACCESS_LE
) {
2066 *may_flags
= MAY_OPEN
| MAY_WRITE
;
2069 *may_flags
= MAY_OPEN
| MAY_READ
;
2072 if (access
== FILE_READ_ATTRIBUTES_LE
)
2076 switch (disposition
& FILE_CREATE_MASK_LE
) {
2078 case FILE_CREATE_LE
:
2080 case FILE_SUPERSEDE_LE
:
2081 case FILE_OVERWRITE_LE
:
2082 case FILE_OVERWRITE_IF_LE
:
2089 switch (disposition
& FILE_CREATE_MASK_LE
) {
2090 case FILE_SUPERSEDE_LE
:
2091 case FILE_CREATE_LE
:
2092 case FILE_OPEN_IF_LE
:
2093 case FILE_OVERWRITE_IF_LE
:
2097 case FILE_OVERWRITE_LE
:
2109 * smb2_tree_disconnect() - handler for smb tree connect request
2110 * @work: smb work containing request buffer
2114 int smb2_tree_disconnect(struct ksmbd_work
*work
)
2116 struct smb2_tree_disconnect_rsp
*rsp
;
2117 struct smb2_tree_disconnect_req
*req
;
2118 struct ksmbd_session
*sess
= work
->sess
;
2119 struct ksmbd_tree_connect
*tcon
= work
->tcon
;
2122 WORK_BUFFERS(work
, req
, rsp
);
2124 ksmbd_debug(SMB
, "request\n");
2127 ksmbd_debug(SMB
, "Invalid tid %d\n", req
->hdr
.Id
.SyncId
.TreeId
);
2129 rsp
->hdr
.Status
= STATUS_NETWORK_NAME_DELETED
;
2134 ksmbd_close_tree_conn_fds(work
);
2136 write_lock(&sess
->tree_conns_lock
);
2137 if (tcon
->t_state
== TREE_DISCONNECTED
) {
2138 write_unlock(&sess
->tree_conns_lock
);
2139 rsp
->hdr
.Status
= STATUS_NETWORK_NAME_DELETED
;
2144 WARN_ON_ONCE(atomic_dec_and_test(&tcon
->refcount
));
2145 tcon
->t_state
= TREE_DISCONNECTED
;
2146 write_unlock(&sess
->tree_conns_lock
);
2148 err
= ksmbd_tree_conn_disconnect(sess
, tcon
);
2150 rsp
->hdr
.Status
= STATUS_NETWORK_NAME_DELETED
;
2156 rsp
->StructureSize
= cpu_to_le16(4);
2157 err
= ksmbd_iov_pin_rsp(work
, rsp
,
2158 sizeof(struct smb2_tree_disconnect_rsp
));
2160 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
2167 smb2_set_err_rsp(work
);
2173 * smb2_session_logoff() - handler for session log off request
2174 * @work: smb work containing request buffer
2178 int smb2_session_logoff(struct ksmbd_work
*work
)
2180 struct ksmbd_conn
*conn
= work
->conn
;
2181 struct smb2_logoff_req
*req
;
2182 struct smb2_logoff_rsp
*rsp
;
2183 struct ksmbd_session
*sess
;
2187 WORK_BUFFERS(work
, req
, rsp
);
2189 ksmbd_debug(SMB
, "request\n");
2191 ksmbd_conn_lock(conn
);
2192 if (!ksmbd_conn_good(conn
)) {
2193 ksmbd_conn_unlock(conn
);
2194 rsp
->hdr
.Status
= STATUS_NETWORK_NAME_DELETED
;
2195 smb2_set_err_rsp(work
);
2198 sess_id
= le64_to_cpu(req
->hdr
.SessionId
);
2199 ksmbd_all_conn_set_status(sess_id
, KSMBD_SESS_NEED_RECONNECT
);
2200 ksmbd_conn_unlock(conn
);
2202 ksmbd_close_session_fds(work
);
2203 ksmbd_conn_wait_idle(conn
, sess_id
);
2206 * Re-lookup session to validate if session is deleted
2207 * while waiting request complete
2209 sess
= ksmbd_session_lookup_all(conn
, sess_id
);
2210 if (ksmbd_tree_conn_session_logoff(sess
)) {
2211 ksmbd_debug(SMB
, "Invalid tid %d\n", req
->hdr
.Id
.SyncId
.TreeId
);
2212 rsp
->hdr
.Status
= STATUS_NETWORK_NAME_DELETED
;
2213 smb2_set_err_rsp(work
);
2217 ksmbd_destroy_file_table(&sess
->file_table
);
2218 sess
->state
= SMB2_SESSION_EXPIRED
;
2220 ksmbd_free_user(sess
->user
);
2222 ksmbd_all_conn_set_status(sess_id
, KSMBD_SESS_NEED_NEGOTIATE
);
2224 rsp
->StructureSize
= cpu_to_le16(4);
2225 err
= ksmbd_iov_pin_rsp(work
, rsp
, sizeof(struct smb2_logoff_rsp
));
2227 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
2228 smb2_set_err_rsp(work
);
2235 * create_smb2_pipe() - create IPC pipe
2236 * @work: smb work containing request buffer
2238 * Return: 0 on success, otherwise error
2240 static noinline
int create_smb2_pipe(struct ksmbd_work
*work
)
2242 struct smb2_create_rsp
*rsp
;
2243 struct smb2_create_req
*req
;
2248 WORK_BUFFERS(work
, req
, rsp
);
2250 name
= smb_strndup_from_utf16(req
->Buffer
, le16_to_cpu(req
->NameLength
),
2251 1, work
->conn
->local_nls
);
2253 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
2254 err
= PTR_ERR(name
);
2258 id
= ksmbd_session_rpc_open(work
->sess
, name
);
2260 pr_err("Unable to open RPC pipe: %d\n", id
);
2265 rsp
->hdr
.Status
= STATUS_SUCCESS
;
2266 rsp
->StructureSize
= cpu_to_le16(89);
2267 rsp
->OplockLevel
= SMB2_OPLOCK_LEVEL_NONE
;
2269 rsp
->CreateAction
= cpu_to_le32(FILE_OPENED
);
2271 rsp
->CreationTime
= cpu_to_le64(0);
2272 rsp
->LastAccessTime
= cpu_to_le64(0);
2273 rsp
->ChangeTime
= cpu_to_le64(0);
2274 rsp
->AllocationSize
= cpu_to_le64(0);
2275 rsp
->EndofFile
= cpu_to_le64(0);
2276 rsp
->FileAttributes
= FILE_ATTRIBUTE_NORMAL_LE
;
2278 rsp
->VolatileFileId
= id
;
2279 rsp
->PersistentFileId
= 0;
2280 rsp
->CreateContextsOffset
= 0;
2281 rsp
->CreateContextsLength
= 0;
2283 err
= ksmbd_iov_pin_rsp(work
, rsp
, offsetof(struct smb2_create_rsp
, Buffer
));
2293 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
2297 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
2304 smb2_set_err_rsp(work
);
2309 * smb2_set_ea() - handler for setting extended attributes using set
2311 * @eabuf: set info command buffer
2312 * @buf_len: set info command buffer length
2313 * @path: dentry path for get ea
2315 * Return: 0 on success, otherwise error
2317 static int smb2_set_ea(struct smb2_ea_info
*eabuf
, unsigned int buf_len
,
2318 const struct path
*path
)
2320 struct mnt_idmap
*idmap
= mnt_idmap(path
->mnt
);
2321 char *attr_name
= NULL
, *value
;
2323 unsigned int next
= 0;
2325 if (buf_len
< sizeof(struct smb2_ea_info
) + eabuf
->EaNameLength
+
2326 le16_to_cpu(eabuf
->EaValueLength
))
2329 attr_name
= kmalloc(XATTR_NAME_MAX
+ 1, GFP_KERNEL
);
2334 if (!eabuf
->EaNameLength
)
2338 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2339 eabuf
->name
, eabuf
->EaNameLength
,
2340 le16_to_cpu(eabuf
->EaValueLength
),
2341 le32_to_cpu(eabuf
->NextEntryOffset
));
2343 if (eabuf
->EaNameLength
>
2344 (XATTR_NAME_MAX
- XATTR_USER_PREFIX_LEN
)) {
2349 memcpy(attr_name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
);
2350 memcpy(&attr_name
[XATTR_USER_PREFIX_LEN
], eabuf
->name
,
2351 eabuf
->EaNameLength
);
2352 attr_name
[XATTR_USER_PREFIX_LEN
+ eabuf
->EaNameLength
] = '\0';
2353 value
= (char *)&eabuf
->name
+ eabuf
->EaNameLength
+ 1;
2355 if (!eabuf
->EaValueLength
) {
2356 rc
= ksmbd_vfs_casexattr_len(idmap
,
2359 XATTR_USER_PREFIX_LEN
+
2360 eabuf
->EaNameLength
);
2362 /* delete the EA only when it exits */
2364 rc
= ksmbd_vfs_remove_xattr(idmap
,
2370 "remove xattr failed(%d)\n",
2376 /* if the EA doesn't exist, just do nothing. */
2379 rc
= ksmbd_vfs_setxattr(idmap
, path
, attr_name
, value
,
2380 le16_to_cpu(eabuf
->EaValueLength
),
2384 "ksmbd_vfs_setxattr is failed(%d)\n",
2391 next
= le32_to_cpu(eabuf
->NextEntryOffset
);
2392 if (next
== 0 || buf_len
< next
)
2395 eabuf
= (struct smb2_ea_info
*)((char *)eabuf
+ next
);
2396 if (buf_len
< sizeof(struct smb2_ea_info
)) {
2401 if (buf_len
< sizeof(struct smb2_ea_info
) + eabuf
->EaNameLength
+
2402 le16_to_cpu(eabuf
->EaValueLength
)) {
2406 } while (next
!= 0);
2412 static noinline
int smb2_set_stream_name_xattr(const struct path
*path
,
2413 struct ksmbd_file
*fp
,
2414 char *stream_name
, int s_type
)
2416 struct mnt_idmap
*idmap
= mnt_idmap(path
->mnt
);
2417 size_t xattr_stream_size
;
2418 char *xattr_stream_name
;
2421 rc
= ksmbd_vfs_xattr_stream_name(stream_name
,
2428 fp
->stream
.name
= xattr_stream_name
;
2429 fp
->stream
.size
= xattr_stream_size
;
2431 /* Check if there is stream prefix in xattr space */
2432 rc
= ksmbd_vfs_casexattr_len(idmap
,
2439 if (fp
->cdoption
== FILE_OPEN_LE
) {
2440 ksmbd_debug(SMB
, "XATTR stream name lookup failed: %d\n", rc
);
2444 rc
= ksmbd_vfs_setxattr(idmap
, path
, xattr_stream_name
, NULL
, 0, 0, false);
2446 pr_err("Failed to store XATTR stream name :%d\n", rc
);
2450 static int smb2_remove_smb_xattrs(const struct path
*path
)
2452 struct mnt_idmap
*idmap
= mnt_idmap(path
->mnt
);
2453 char *name
, *xattr_list
= NULL
;
2454 ssize_t xattr_list_len
;
2457 xattr_list_len
= ksmbd_vfs_listxattr(path
->dentry
, &xattr_list
);
2458 if (xattr_list_len
< 0) {
2460 } else if (!xattr_list_len
) {
2461 ksmbd_debug(SMB
, "empty xattr in the file\n");
2465 for (name
= xattr_list
; name
- xattr_list
< xattr_list_len
;
2466 name
+= strlen(name
) + 1) {
2467 ksmbd_debug(SMB
, "%s, len %zd\n", name
, strlen(name
));
2469 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
) &&
2470 !strncmp(&name
[XATTR_USER_PREFIX_LEN
], STREAM_PREFIX
,
2471 STREAM_PREFIX_LEN
)) {
2472 err
= ksmbd_vfs_remove_xattr(idmap
, path
,
2475 ksmbd_debug(SMB
, "remove xattr failed : %s\n",
2484 static int smb2_create_truncate(const struct path
*path
)
2486 int rc
= vfs_truncate(path
, 0);
2489 pr_err("vfs_truncate failed, rc %d\n", rc
);
2493 rc
= smb2_remove_smb_xattrs(path
);
2494 if (rc
== -EOPNOTSUPP
)
2498 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2503 static void smb2_new_xattrs(struct ksmbd_tree_connect
*tcon
, const struct path
*path
,
2504 struct ksmbd_file
*fp
)
2506 struct xattr_dos_attrib da
= {0};
2509 if (!test_share_config_flag(tcon
->share_conf
,
2510 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS
))
2514 da
.attr
= le32_to_cpu(fp
->f_ci
->m_fattr
);
2515 da
.itime
= da
.create_time
= fp
->create_time
;
2516 da
.flags
= XATTR_DOSINFO_ATTRIB
| XATTR_DOSINFO_CREATE_TIME
|
2517 XATTR_DOSINFO_ITIME
;
2519 rc
= ksmbd_vfs_set_dos_attrib_xattr(mnt_idmap(path
->mnt
), path
, &da
, true);
2521 ksmbd_debug(SMB
, "failed to store file attribute into xattr\n");
2524 static void smb2_update_xattrs(struct ksmbd_tree_connect
*tcon
,
2525 const struct path
*path
, struct ksmbd_file
*fp
)
2527 struct xattr_dos_attrib da
;
2530 fp
->f_ci
->m_fattr
&= ~(FILE_ATTRIBUTE_HIDDEN_LE
| FILE_ATTRIBUTE_SYSTEM_LE
);
2532 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2533 if (!test_share_config_flag(tcon
->share_conf
,
2534 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS
))
2537 rc
= ksmbd_vfs_get_dos_attrib_xattr(mnt_idmap(path
->mnt
),
2540 fp
->f_ci
->m_fattr
= cpu_to_le32(da
.attr
);
2541 fp
->create_time
= da
.create_time
;
2542 fp
->itime
= da
.itime
;
2546 static int smb2_creat(struct ksmbd_work
*work
, struct path
*parent_path
,
2547 struct path
*path
, char *name
, int open_flags
,
2548 umode_t posix_mode
, bool is_dir
)
2550 struct ksmbd_tree_connect
*tcon
= work
->tcon
;
2551 struct ksmbd_share_config
*share
= tcon
->share_conf
;
2555 if (!(open_flags
& O_CREAT
))
2558 ksmbd_debug(SMB
, "file does not exist, so creating\n");
2559 if (is_dir
== true) {
2560 ksmbd_debug(SMB
, "creating directory\n");
2562 mode
= share_config_directory_mode(share
, posix_mode
);
2563 rc
= ksmbd_vfs_mkdir(work
, name
, mode
);
2567 ksmbd_debug(SMB
, "creating regular file\n");
2569 mode
= share_config_create_mode(share
, posix_mode
);
2570 rc
= ksmbd_vfs_create(work
, name
, mode
);
2575 rc
= ksmbd_vfs_kern_path_locked(work
, name
, 0, parent_path
, path
, 0);
2577 pr_err("cannot get linux path (%s), err = %d\n",
2584 static int smb2_create_sd_buffer(struct ksmbd_work
*work
,
2585 struct smb2_create_req
*req
,
2586 const struct path
*path
)
2588 struct create_context
*context
;
2589 struct create_sd_buf_req
*sd_buf
;
2591 if (!req
->CreateContextsOffset
)
2594 /* Parse SD BUFFER create contexts */
2595 context
= smb2_find_context_vals(req
, SMB2_CREATE_SD_BUFFER
, 4);
2598 else if (IS_ERR(context
))
2599 return PTR_ERR(context
);
2602 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2603 sd_buf
= (struct create_sd_buf_req
*)context
;
2604 if (le16_to_cpu(context
->DataOffset
) +
2605 le32_to_cpu(context
->DataLength
) <
2606 sizeof(struct create_sd_buf_req
))
2608 return set_info_sec(work
->conn
, work
->tcon
, path
, &sd_buf
->ntsd
,
2609 le32_to_cpu(sd_buf
->ccontext
.DataLength
), true, false);
2612 static void ksmbd_acls_fattr(struct smb_fattr
*fattr
,
2613 struct mnt_idmap
*idmap
,
2614 struct inode
*inode
)
2616 vfsuid_t vfsuid
= i_uid_into_vfsuid(idmap
, inode
);
2617 vfsgid_t vfsgid
= i_gid_into_vfsgid(idmap
, inode
);
2619 fattr
->cf_uid
= vfsuid_into_kuid(vfsuid
);
2620 fattr
->cf_gid
= vfsgid_into_kgid(vfsgid
);
2621 fattr
->cf_mode
= inode
->i_mode
;
2622 fattr
->cf_acls
= NULL
;
2623 fattr
->cf_dacls
= NULL
;
2625 if (IS_ENABLED(CONFIG_FS_POSIX_ACL
)) {
2626 fattr
->cf_acls
= get_inode_acl(inode
, ACL_TYPE_ACCESS
);
2627 if (S_ISDIR(inode
->i_mode
))
2628 fattr
->cf_dacls
= get_inode_acl(inode
, ACL_TYPE_DEFAULT
);
2633 * smb2_open() - handler for smb file open request
2634 * @work: smb work containing request buffer
2636 * Return: 0 on success, otherwise error
2638 int smb2_open(struct ksmbd_work
*work
)
2640 struct ksmbd_conn
*conn
= work
->conn
;
2641 struct ksmbd_session
*sess
= work
->sess
;
2642 struct ksmbd_tree_connect
*tcon
= work
->tcon
;
2643 struct smb2_create_req
*req
;
2644 struct smb2_create_rsp
*rsp
;
2645 struct path path
, parent_path
;
2646 struct ksmbd_share_config
*share
= tcon
->share_conf
;
2647 struct ksmbd_file
*fp
= NULL
;
2648 struct file
*filp
= NULL
;
2649 struct mnt_idmap
*idmap
= NULL
;
2651 struct create_context
*context
;
2652 struct lease_ctx_info
*lc
= NULL
;
2653 struct create_ea_buf_req
*ea_buf
= NULL
;
2654 struct oplock_info
*opinfo
;
2655 __le32
*next_ptr
= NULL
;
2656 int req_op_level
= 0, open_flags
= 0, may_flags
= 0, file_info
= 0;
2658 int contxt_cnt
= 0, query_disk_id
= 0;
2659 int maximal_access_ctxt
= 0, posix_ctxt
= 0;
2663 char *stream_name
= NULL
;
2664 bool file_present
= false, created
= false, already_permitted
= false;
2665 int share_ret
, need_truncate
= 0;
2667 umode_t posix_mode
= 0;
2668 __le32 daccess
, maximal_access
= 0;
2671 WORK_BUFFERS(work
, req
, rsp
);
2673 if (req
->hdr
.NextCommand
&& !work
->next_smb2_rcv_hdr_off
&&
2674 (req
->hdr
.Flags
& SMB2_FLAGS_RELATED_OPERATIONS
)) {
2675 ksmbd_debug(SMB
, "invalid flag in chained command\n");
2676 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
2677 smb2_set_err_rsp(work
);
2681 if (test_share_config_flag(share
, KSMBD_SHARE_FLAG_PIPE
)) {
2682 ksmbd_debug(SMB
, "IPC pipe create request\n");
2683 return create_smb2_pipe(work
);
2686 if (req
->NameLength
) {
2687 if ((req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
) &&
2688 *(char *)req
->Buffer
== '\\') {
2689 pr_err("not allow directory name included leading slash\n");
2694 name
= smb2_get_name(req
->Buffer
,
2695 le16_to_cpu(req
->NameLength
),
2696 work
->conn
->local_nls
);
2705 ksmbd_debug(SMB
, "converted name = %s\n", name
);
2706 if (strchr(name
, ':')) {
2707 if (!test_share_config_flag(work
->tcon
->share_conf
,
2708 KSMBD_SHARE_FLAG_STREAMS
)) {
2712 rc
= parse_stream_name(name
, &stream_name
, &s_type
);
2717 rc
= ksmbd_validate_filename(name
);
2721 if (ksmbd_share_veto_filename(share
, name
)) {
2723 ksmbd_debug(SMB
, "Reject open(), vetoed file: %s\n",
2728 name
= kstrdup("", GFP_KERNEL
);
2735 if (le32_to_cpu(req
->ImpersonationLevel
) > le32_to_cpu(IL_DELEGATE
)) {
2736 pr_err("Invalid impersonationlevel : 0x%x\n",
2737 le32_to_cpu(req
->ImpersonationLevel
));
2739 rsp
->hdr
.Status
= STATUS_BAD_IMPERSONATION_LEVEL
;
2743 if (req
->CreateOptions
&& !(req
->CreateOptions
& CREATE_OPTIONS_MASK_LE
)) {
2744 pr_err("Invalid create options : 0x%x\n",
2745 le32_to_cpu(req
->CreateOptions
));
2749 if (req
->CreateOptions
& FILE_SEQUENTIAL_ONLY_LE
&&
2750 req
->CreateOptions
& FILE_RANDOM_ACCESS_LE
)
2751 req
->CreateOptions
= ~(FILE_SEQUENTIAL_ONLY_LE
);
2753 if (req
->CreateOptions
&
2754 (FILE_OPEN_BY_FILE_ID_LE
| CREATE_TREE_CONNECTION
|
2755 FILE_RESERVE_OPFILTER_LE
)) {
2760 if (req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
) {
2761 if (req
->CreateOptions
& FILE_NON_DIRECTORY_FILE_LE
) {
2764 } else if (req
->CreateOptions
& FILE_NO_COMPRESSION_LE
) {
2765 req
->CreateOptions
= ~(FILE_NO_COMPRESSION_LE
);
2770 if (le32_to_cpu(req
->CreateDisposition
) >
2771 le32_to_cpu(FILE_OVERWRITE_IF_LE
)) {
2772 pr_err("Invalid create disposition : 0x%x\n",
2773 le32_to_cpu(req
->CreateDisposition
));
2778 if (!(req
->DesiredAccess
& DESIRED_ACCESS_MASK
)) {
2779 pr_err("Invalid desired access : 0x%x\n",
2780 le32_to_cpu(req
->DesiredAccess
));
2785 if (req
->FileAttributes
&& !(req
->FileAttributes
& FILE_ATTRIBUTE_MASK_LE
)) {
2786 pr_err("Invalid file attribute : 0x%x\n",
2787 le32_to_cpu(req
->FileAttributes
));
2792 if (req
->CreateContextsOffset
) {
2793 /* Parse non-durable handle create contexts */
2794 context
= smb2_find_context_vals(req
, SMB2_CREATE_EA_BUFFER
, 4);
2795 if (IS_ERR(context
)) {
2796 rc
= PTR_ERR(context
);
2798 } else if (context
) {
2799 ea_buf
= (struct create_ea_buf_req
*)context
;
2800 if (le16_to_cpu(context
->DataOffset
) +
2801 le32_to_cpu(context
->DataLength
) <
2802 sizeof(struct create_ea_buf_req
)) {
2806 if (req
->CreateOptions
& FILE_NO_EA_KNOWLEDGE_LE
) {
2807 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
2813 context
= smb2_find_context_vals(req
,
2814 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST
, 4);
2815 if (IS_ERR(context
)) {
2816 rc
= PTR_ERR(context
);
2818 } else if (context
) {
2820 "get query maximal access context\n");
2821 maximal_access_ctxt
= 1;
2824 context
= smb2_find_context_vals(req
,
2825 SMB2_CREATE_TIMEWARP_REQUEST
, 4);
2826 if (IS_ERR(context
)) {
2827 rc
= PTR_ERR(context
);
2829 } else if (context
) {
2830 ksmbd_debug(SMB
, "get timewarp context\n");
2835 if (tcon
->posix_extensions
) {
2836 context
= smb2_find_context_vals(req
,
2837 SMB2_CREATE_TAG_POSIX
, 16);
2838 if (IS_ERR(context
)) {
2839 rc
= PTR_ERR(context
);
2841 } else if (context
) {
2842 struct create_posix
*posix
=
2843 (struct create_posix
*)context
;
2844 if (le16_to_cpu(context
->DataOffset
) +
2845 le32_to_cpu(context
->DataLength
) <
2846 sizeof(struct create_posix
) - 4) {
2850 ksmbd_debug(SMB
, "get posix context\n");
2852 posix_mode
= le32_to_cpu(posix
->Mode
);
2858 if (ksmbd_override_fsids(work
)) {
2863 rc
= ksmbd_vfs_kern_path_locked(work
, name
, LOOKUP_NO_SYMLINKS
,
2864 &parent_path
, &path
, 1);
2866 file_present
= true;
2868 if (req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
) {
2870 * If file exists with under flags, return access
2873 if (req
->CreateDisposition
== FILE_OVERWRITE_IF_LE
||
2874 req
->CreateDisposition
== FILE_OPEN_IF_LE
) {
2879 if (!test_tree_conn_flag(tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
2881 "User does not have write permission\n");
2885 } else if (d_is_symlink(path
.dentry
)) {
2890 file_present
= true;
2891 idmap
= mnt_idmap(path
.mnt
);
2895 ksmbd_debug(SMB
, "can not get linux path for %s, rc = %d\n",
2901 if (req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
) {
2902 if (s_type
== DATA_STREAM
) {
2904 rsp
->hdr
.Status
= STATUS_NOT_A_DIRECTORY
;
2907 if (file_present
&& S_ISDIR(d_inode(path
.dentry
)->i_mode
) &&
2908 s_type
== DATA_STREAM
) {
2910 rsp
->hdr
.Status
= STATUS_FILE_IS_A_DIRECTORY
;
2914 if (req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
&&
2915 req
->FileAttributes
& FILE_ATTRIBUTE_NORMAL_LE
) {
2916 rsp
->hdr
.Status
= STATUS_NOT_A_DIRECTORY
;
2924 if (file_present
&& req
->CreateOptions
& FILE_NON_DIRECTORY_FILE_LE
&&
2925 S_ISDIR(d_inode(path
.dentry
)->i_mode
) &&
2926 !(req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
)) {
2927 ksmbd_debug(SMB
, "open() argument is a directory: %s, %x\n",
2928 name
, req
->CreateOptions
);
2929 rsp
->hdr
.Status
= STATUS_FILE_IS_A_DIRECTORY
;
2934 if (file_present
&& (req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
) &&
2935 !(req
->CreateDisposition
== FILE_CREATE_LE
) &&
2936 !S_ISDIR(d_inode(path
.dentry
)->i_mode
)) {
2937 rsp
->hdr
.Status
= STATUS_NOT_A_DIRECTORY
;
2942 if (!stream_name
&& file_present
&&
2943 req
->CreateDisposition
== FILE_CREATE_LE
) {
2948 daccess
= smb_map_generic_desired_access(req
->DesiredAccess
);
2950 if (file_present
&& !(req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
)) {
2951 rc
= smb_check_perm_dacl(conn
, &path
, &daccess
,
2957 if (daccess
& FILE_MAXIMAL_ACCESS_LE
) {
2958 if (!file_present
) {
2959 daccess
= cpu_to_le32(GENERIC_ALL_FLAGS
);
2961 ksmbd_vfs_query_maximal_access(idmap
,
2964 already_permitted
= true;
2966 maximal_access
= daccess
;
2969 open_flags
= smb2_create_open_flags(file_present
, daccess
,
2970 req
->CreateDisposition
,
2973 if (!test_tree_conn_flag(tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
2974 if (open_flags
& O_CREAT
) {
2976 "User does not have write permission\n");
2982 /*create file if not present */
2983 if (!file_present
) {
2984 rc
= smb2_creat(work
, &parent_path
, &path
, name
, open_flags
,
2986 req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
);
2988 if (rc
== -ENOENT
) {
2990 rsp
->hdr
.Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
2996 idmap
= mnt_idmap(path
.mnt
);
2998 if (le32_to_cpu(ea_buf
->ccontext
.DataLength
) <
2999 sizeof(struct smb2_ea_info
)) {
3004 rc
= smb2_set_ea(&ea_buf
->ea
,
3005 le32_to_cpu(ea_buf
->ccontext
.DataLength
),
3007 if (rc
== -EOPNOTSUPP
)
3012 } else if (!already_permitted
) {
3013 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
3014 * because execute(search) permission on a parent directory,
3015 * is already granted.
3017 if (daccess
& ~(FILE_READ_ATTRIBUTES_LE
| FILE_READ_CONTROL_LE
)) {
3018 rc
= inode_permission(idmap
,
3019 d_inode(path
.dentry
),
3024 if ((daccess
& FILE_DELETE_LE
) ||
3025 (req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
)) {
3026 rc
= inode_permission(idmap
,
3027 d_inode(path
.dentry
->d_parent
),
3028 MAY_EXEC
| MAY_WRITE
);
3035 rc
= ksmbd_query_inode_status(path
.dentry
->d_parent
);
3036 if (rc
== KSMBD_INODE_STATUS_PENDING_DELETE
) {
3042 filp
= dentry_open(&path
, open_flags
, current_cred());
3045 pr_err("dentry open for dir failed, rc %d\n", rc
);
3050 if (!(open_flags
& O_TRUNC
))
3051 file_info
= FILE_OPENED
;
3053 file_info
= FILE_OVERWRITTEN
;
3055 if ((req
->CreateDisposition
& FILE_CREATE_MASK_LE
) ==
3057 file_info
= FILE_SUPERSEDED
;
3058 } else if (open_flags
& O_CREAT
) {
3059 file_info
= FILE_CREATED
;
3062 ksmbd_vfs_set_fadvise(filp
, req
->CreateOptions
);
3064 /* Obtain Volatile-ID */
3065 fp
= ksmbd_open_fd(work
, filp
);
3073 /* Get Persistent-ID */
3074 ksmbd_open_durable_fd(fp
);
3075 if (!has_file_id(fp
->persistent_id
)) {
3080 fp
->cdoption
= req
->CreateDisposition
;
3081 fp
->daccess
= daccess
;
3082 fp
->saccess
= req
->ShareAccess
;
3083 fp
->coption
= req
->CreateOptions
;
3085 /* Set default windows and posix acls if creating new file */
3088 struct inode
*inode
= d_inode(path
.dentry
);
3090 posix_acl_rc
= ksmbd_vfs_inherit_posix_acl(idmap
,
3092 d_inode(path
.dentry
->d_parent
));
3094 ksmbd_debug(SMB
, "inherit posix acl failed : %d\n", posix_acl_rc
);
3096 if (test_share_config_flag(work
->tcon
->share_conf
,
3097 KSMBD_SHARE_FLAG_ACL_XATTR
)) {
3098 rc
= smb_inherit_dacl(conn
, &path
, sess
->user
->uid
,
3103 rc
= smb2_create_sd_buffer(work
, req
, &path
);
3106 ksmbd_vfs_set_init_posix_acl(idmap
,
3109 if (test_share_config_flag(work
->tcon
->share_conf
,
3110 KSMBD_SHARE_FLAG_ACL_XATTR
)) {
3111 struct smb_fattr fattr
;
3112 struct smb_ntsd
*pntsd
;
3113 int pntsd_size
, ace_num
= 0;
3115 ksmbd_acls_fattr(&fattr
, idmap
, inode
);
3117 ace_num
= fattr
.cf_acls
->a_count
;
3119 ace_num
+= fattr
.cf_dacls
->a_count
;
3121 pntsd
= kmalloc(sizeof(struct smb_ntsd
) +
3122 sizeof(struct smb_sid
) * 3 +
3123 sizeof(struct smb_acl
) +
3124 sizeof(struct smb_ace
) * ace_num
* 2,
3127 posix_acl_release(fattr
.cf_acls
);
3128 posix_acl_release(fattr
.cf_dacls
);
3132 rc
= build_sec_desc(idmap
,
3137 &pntsd_size
, &fattr
);
3138 posix_acl_release(fattr
.cf_acls
);
3139 posix_acl_release(fattr
.cf_dacls
);
3145 rc
= ksmbd_vfs_set_sd_xattr(conn
,
3153 pr_err("failed to store ntacl in xattr : %d\n",
3162 rc
= smb2_set_stream_name_xattr(&path
,
3168 file_info
= FILE_CREATED
;
3171 fp
->attrib_only
= !(req
->DesiredAccess
& ~(FILE_READ_ATTRIBUTES_LE
|
3172 FILE_WRITE_ATTRIBUTES_LE
| FILE_SYNCHRONIZE_LE
));
3174 /* fp should be searchable through ksmbd_inode.m_fp_list
3175 * after daccess, saccess, attrib_only, and stream are
3178 write_lock(&fp
->f_ci
->m_lock
);
3179 list_add(&fp
->node
, &fp
->f_ci
->m_fp_list
);
3180 write_unlock(&fp
->f_ci
->m_lock
);
3182 /* Check delete pending among previous fp before oplock break */
3183 if (ksmbd_inode_pending_delete(fp
)) {
3188 if (file_present
|| created
)
3189 ksmbd_vfs_kern_path_unlock(&parent_path
, &path
);
3191 if (!S_ISDIR(file_inode(filp
)->i_mode
) && open_flags
& O_TRUNC
&&
3192 !fp
->attrib_only
&& !stream_name
) {
3193 smb_break_all_oplock(work
, fp
);
3197 req_op_level
= req
->RequestedOplockLevel
;
3198 if (req_op_level
== SMB2_OPLOCK_LEVEL_LEASE
)
3199 lc
= parse_lease_state(req
, S_ISDIR(file_inode(filp
)->i_mode
));
3201 share_ret
= ksmbd_smb_check_shared_mode(fp
->filp
, fp
);
3202 if (!test_share_config_flag(work
->tcon
->share_conf
, KSMBD_SHARE_FLAG_OPLOCKS
) ||
3203 (req_op_level
== SMB2_OPLOCK_LEVEL_LEASE
&&
3204 !(conn
->vals
->capabilities
& SMB2_GLOBAL_CAP_LEASING
))) {
3205 if (share_ret
< 0 && !S_ISDIR(file_inode(fp
->filp
)->i_mode
)) {
3210 if (req_op_level
== SMB2_OPLOCK_LEVEL_LEASE
) {
3212 * Compare parent lease using parent key. If there is no
3213 * a lease that has same parent key, Send lease break
3216 smb_send_parent_lease_break_noti(fp
, lc
);
3218 req_op_level
= smb2_map_lease_to_oplock(lc
->req_state
);
3220 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
3221 name
, req_op_level
, lc
->req_state
);
3222 rc
= find_same_lease_key(sess
, fp
->f_ci
, lc
);
3225 } else if (open_flags
== O_RDONLY
&&
3226 (req_op_level
== SMB2_OPLOCK_LEVEL_BATCH
||
3227 req_op_level
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
))
3228 req_op_level
= SMB2_OPLOCK_LEVEL_II
;
3230 rc
= smb_grant_oplock(work
, req_op_level
,
3231 fp
->persistent_id
, fp
,
3232 le32_to_cpu(req
->hdr
.Id
.SyncId
.TreeId
),
3238 if (req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
)
3239 ksmbd_fd_set_delete_on_close(fp
, file_info
);
3241 if (need_truncate
) {
3242 rc
= smb2_create_truncate(&fp
->filp
->f_path
);
3247 if (req
->CreateContextsOffset
) {
3248 struct create_alloc_size_req
*az_req
;
3250 az_req
= (struct create_alloc_size_req
*)smb2_find_context_vals(req
,
3251 SMB2_CREATE_ALLOCATION_SIZE
, 4);
3252 if (IS_ERR(az_req
)) {
3253 rc
= PTR_ERR(az_req
);
3255 } else if (az_req
) {
3259 if (le16_to_cpu(az_req
->ccontext
.DataOffset
) +
3260 le32_to_cpu(az_req
->ccontext
.DataLength
) <
3261 sizeof(struct create_alloc_size_req
)) {
3265 alloc_size
= le64_to_cpu(az_req
->AllocationSize
);
3267 "request smb2 create allocate size : %llu\n",
3269 smb_break_all_levII_oplock(work
, fp
, 1);
3270 err
= vfs_fallocate(fp
->filp
, FALLOC_FL_KEEP_SIZE
, 0,
3274 "vfs_fallocate is failed : %d\n",
3278 context
= smb2_find_context_vals(req
, SMB2_CREATE_QUERY_ON_DISK_ID
, 4);
3279 if (IS_ERR(context
)) {
3280 rc
= PTR_ERR(context
);
3282 } else if (context
) {
3283 ksmbd_debug(SMB
, "get query on disk id context\n");
3288 rc
= ksmbd_vfs_getattr(&path
, &stat
);
3292 if (stat
.result_mask
& STATX_BTIME
)
3293 fp
->create_time
= ksmbd_UnixTimeToNT(stat
.btime
);
3295 fp
->create_time
= ksmbd_UnixTimeToNT(stat
.ctime
);
3296 if (req
->FileAttributes
|| fp
->f_ci
->m_fattr
== 0)
3298 cpu_to_le32(smb2_get_dos_mode(&stat
, le32_to_cpu(req
->FileAttributes
)));
3301 smb2_update_xattrs(tcon
, &path
, fp
);
3303 smb2_new_xattrs(tcon
, &path
, fp
);
3305 memcpy(fp
->client_guid
, conn
->ClientGUID
, SMB2_CLIENT_GUID_SIZE
);
3307 rsp
->StructureSize
= cpu_to_le16(89);
3309 opinfo
= rcu_dereference(fp
->f_opinfo
);
3310 rsp
->OplockLevel
= opinfo
!= NULL
? opinfo
->level
: 0;
3313 rsp
->CreateAction
= cpu_to_le32(file_info
);
3314 rsp
->CreationTime
= cpu_to_le64(fp
->create_time
);
3315 time
= ksmbd_UnixTimeToNT(stat
.atime
);
3316 rsp
->LastAccessTime
= cpu_to_le64(time
);
3317 time
= ksmbd_UnixTimeToNT(stat
.mtime
);
3318 rsp
->LastWriteTime
= cpu_to_le64(time
);
3319 time
= ksmbd_UnixTimeToNT(stat
.ctime
);
3320 rsp
->ChangeTime
= cpu_to_le64(time
);
3321 rsp
->AllocationSize
= S_ISDIR(stat
.mode
) ? 0 :
3322 cpu_to_le64(stat
.blocks
<< 9);
3323 rsp
->EndofFile
= S_ISDIR(stat
.mode
) ? 0 : cpu_to_le64(stat
.size
);
3324 rsp
->FileAttributes
= fp
->f_ci
->m_fattr
;
3328 rsp
->PersistentFileId
= fp
->persistent_id
;
3329 rsp
->VolatileFileId
= fp
->volatile_id
;
3331 rsp
->CreateContextsOffset
= 0;
3332 rsp
->CreateContextsLength
= 0;
3333 iov_len
= offsetof(struct smb2_create_rsp
, Buffer
);
3335 /* If lease is request send lease context response */
3336 if (opinfo
&& opinfo
->is_lease
) {
3337 struct create_context
*lease_ccontext
;
3339 ksmbd_debug(SMB
, "lease granted on(%s) lease state 0x%x\n",
3340 name
, opinfo
->o_lease
->state
);
3341 rsp
->OplockLevel
= SMB2_OPLOCK_LEVEL_LEASE
;
3343 lease_ccontext
= (struct create_context
*)rsp
->Buffer
;
3345 create_lease_buf(rsp
->Buffer
, opinfo
->o_lease
);
3346 le32_add_cpu(&rsp
->CreateContextsLength
,
3347 conn
->vals
->create_lease_size
);
3348 iov_len
+= conn
->vals
->create_lease_size
;
3349 next_ptr
= &lease_ccontext
->Next
;
3350 next_off
= conn
->vals
->create_lease_size
;
3353 if (maximal_access_ctxt
) {
3354 struct create_context
*mxac_ccontext
;
3356 if (maximal_access
== 0)
3357 ksmbd_vfs_query_maximal_access(idmap
,
3360 mxac_ccontext
= (struct create_context
*)(rsp
->Buffer
+
3361 le32_to_cpu(rsp
->CreateContextsLength
));
3363 create_mxac_rsp_buf(rsp
->Buffer
+
3364 le32_to_cpu(rsp
->CreateContextsLength
),
3365 le32_to_cpu(maximal_access
));
3366 le32_add_cpu(&rsp
->CreateContextsLength
,
3367 conn
->vals
->create_mxac_size
);
3368 iov_len
+= conn
->vals
->create_mxac_size
;
3370 *next_ptr
= cpu_to_le32(next_off
);
3371 next_ptr
= &mxac_ccontext
->Next
;
3372 next_off
= conn
->vals
->create_mxac_size
;
3375 if (query_disk_id
) {
3376 struct create_context
*disk_id_ccontext
;
3378 disk_id_ccontext
= (struct create_context
*)(rsp
->Buffer
+
3379 le32_to_cpu(rsp
->CreateContextsLength
));
3381 create_disk_id_rsp_buf(rsp
->Buffer
+
3382 le32_to_cpu(rsp
->CreateContextsLength
),
3383 stat
.ino
, tcon
->id
);
3384 le32_add_cpu(&rsp
->CreateContextsLength
,
3385 conn
->vals
->create_disk_id_size
);
3386 iov_len
+= conn
->vals
->create_disk_id_size
;
3388 *next_ptr
= cpu_to_le32(next_off
);
3389 next_ptr
= &disk_id_ccontext
->Next
;
3390 next_off
= conn
->vals
->create_disk_id_size
;
3395 create_posix_rsp_buf(rsp
->Buffer
+
3396 le32_to_cpu(rsp
->CreateContextsLength
),
3398 le32_add_cpu(&rsp
->CreateContextsLength
,
3399 conn
->vals
->create_posix_size
);
3400 iov_len
+= conn
->vals
->create_posix_size
;
3402 *next_ptr
= cpu_to_le32(next_off
);
3405 if (contxt_cnt
> 0) {
3406 rsp
->CreateContextsOffset
=
3407 cpu_to_le32(offsetof(struct smb2_create_rsp
, Buffer
));
3411 if (rc
&& (file_present
|| created
))
3412 ksmbd_vfs_kern_path_unlock(&parent_path
, &path
);
3415 ksmbd_revert_fsids(work
);
3419 ksmbd_update_fstate(&work
->sess
->file_table
, fp
, FP_INITED
);
3420 rc
= ksmbd_iov_pin_rsp(work
, (void *)rsp
, iov_len
);
3424 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
3425 else if (rc
== -EOPNOTSUPP
)
3426 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
3427 else if (rc
== -EACCES
|| rc
== -ESTALE
|| rc
== -EXDEV
)
3428 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
3429 else if (rc
== -ENOENT
)
3430 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_INVALID
;
3431 else if (rc
== -EPERM
)
3432 rsp
->hdr
.Status
= STATUS_SHARING_VIOLATION
;
3433 else if (rc
== -EBUSY
)
3434 rsp
->hdr
.Status
= STATUS_DELETE_PENDING
;
3435 else if (rc
== -EBADF
)
3436 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
3437 else if (rc
== -ENOEXEC
)
3438 rsp
->hdr
.Status
= STATUS_DUPLICATE_OBJECTID
;
3439 else if (rc
== -ENXIO
)
3440 rsp
->hdr
.Status
= STATUS_NO_SUCH_DEVICE
;
3441 else if (rc
== -EEXIST
)
3442 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_COLLISION
;
3443 else if (rc
== -EMFILE
)
3444 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
3445 if (!rsp
->hdr
.Status
)
3446 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
3449 ksmbd_fd_put(work
, fp
);
3450 smb2_set_err_rsp(work
);
3451 ksmbd_debug(SMB
, "Error response: %x\n", rsp
->hdr
.Status
);
3460 static int readdir_info_level_struct_sz(int info_level
)
3462 switch (info_level
) {
3463 case FILE_FULL_DIRECTORY_INFORMATION
:
3464 return sizeof(struct file_full_directory_info
);
3465 case FILE_BOTH_DIRECTORY_INFORMATION
:
3466 return sizeof(struct file_both_directory_info
);
3467 case FILE_DIRECTORY_INFORMATION
:
3468 return sizeof(struct file_directory_info
);
3469 case FILE_NAMES_INFORMATION
:
3470 return sizeof(struct file_names_info
);
3471 case FILEID_FULL_DIRECTORY_INFORMATION
:
3472 return sizeof(struct file_id_full_dir_info
);
3473 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3474 return sizeof(struct file_id_both_directory_info
);
3475 case SMB_FIND_FILE_POSIX_INFO
:
3476 return sizeof(struct smb2_posix_info
);
3482 static int dentry_name(struct ksmbd_dir_info
*d_info
, int info_level
)
3484 switch (info_level
) {
3485 case FILE_FULL_DIRECTORY_INFORMATION
:
3487 struct file_full_directory_info
*ffdinfo
;
3489 ffdinfo
= (struct file_full_directory_info
*)d_info
->rptr
;
3490 d_info
->rptr
+= le32_to_cpu(ffdinfo
->NextEntryOffset
);
3491 d_info
->name
= ffdinfo
->FileName
;
3492 d_info
->name_len
= le32_to_cpu(ffdinfo
->FileNameLength
);
3495 case FILE_BOTH_DIRECTORY_INFORMATION
:
3497 struct file_both_directory_info
*fbdinfo
;
3499 fbdinfo
= (struct file_both_directory_info
*)d_info
->rptr
;
3500 d_info
->rptr
+= le32_to_cpu(fbdinfo
->NextEntryOffset
);
3501 d_info
->name
= fbdinfo
->FileName
;
3502 d_info
->name_len
= le32_to_cpu(fbdinfo
->FileNameLength
);
3505 case FILE_DIRECTORY_INFORMATION
:
3507 struct file_directory_info
*fdinfo
;
3509 fdinfo
= (struct file_directory_info
*)d_info
->rptr
;
3510 d_info
->rptr
+= le32_to_cpu(fdinfo
->NextEntryOffset
);
3511 d_info
->name
= fdinfo
->FileName
;
3512 d_info
->name_len
= le32_to_cpu(fdinfo
->FileNameLength
);
3515 case FILE_NAMES_INFORMATION
:
3517 struct file_names_info
*fninfo
;
3519 fninfo
= (struct file_names_info
*)d_info
->rptr
;
3520 d_info
->rptr
+= le32_to_cpu(fninfo
->NextEntryOffset
);
3521 d_info
->name
= fninfo
->FileName
;
3522 d_info
->name_len
= le32_to_cpu(fninfo
->FileNameLength
);
3525 case FILEID_FULL_DIRECTORY_INFORMATION
:
3527 struct file_id_full_dir_info
*dinfo
;
3529 dinfo
= (struct file_id_full_dir_info
*)d_info
->rptr
;
3530 d_info
->rptr
+= le32_to_cpu(dinfo
->NextEntryOffset
);
3531 d_info
->name
= dinfo
->FileName
;
3532 d_info
->name_len
= le32_to_cpu(dinfo
->FileNameLength
);
3535 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3537 struct file_id_both_directory_info
*fibdinfo
;
3539 fibdinfo
= (struct file_id_both_directory_info
*)d_info
->rptr
;
3540 d_info
->rptr
+= le32_to_cpu(fibdinfo
->NextEntryOffset
);
3541 d_info
->name
= fibdinfo
->FileName
;
3542 d_info
->name_len
= le32_to_cpu(fibdinfo
->FileNameLength
);
3545 case SMB_FIND_FILE_POSIX_INFO
:
3547 struct smb2_posix_info
*posix_info
;
3549 posix_info
= (struct smb2_posix_info
*)d_info
->rptr
;
3550 d_info
->rptr
+= le32_to_cpu(posix_info
->NextEntryOffset
);
3551 d_info
->name
= posix_info
->name
;
3552 d_info
->name_len
= le32_to_cpu(posix_info
->name_len
);
3561 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3563 * @conn: connection instance
3564 * @info_level: smb information level
3565 * @d_info: structure included variables for query dir
3566 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3568 * if directory has many entries, find first can't read it fully.
3569 * find next might be called multiple times to read remaining dir entries
3571 * Return: 0 on success, otherwise error
3573 static int smb2_populate_readdir_entry(struct ksmbd_conn
*conn
, int info_level
,
3574 struct ksmbd_dir_info
*d_info
,
3575 struct ksmbd_kstat
*ksmbd_kstat
)
3577 int next_entry_offset
= 0;
3581 int struct_sz
, rc
= 0;
3583 conv_name
= ksmbd_convert_dir_info_name(d_info
,
3589 /* Somehow the name has only terminating NULL bytes */
3592 goto free_conv_name
;
3595 struct_sz
= readdir_info_level_struct_sz(info_level
) + conv_len
;
3596 next_entry_offset
= ALIGN(struct_sz
, KSMBD_DIR_INFO_ALIGNMENT
);
3597 d_info
->last_entry_off_align
= next_entry_offset
- struct_sz
;
3599 if (next_entry_offset
> d_info
->out_buf_len
) {
3600 d_info
->out_buf_len
= 0;
3602 goto free_conv_name
;
3605 kstat
= d_info
->wptr
;
3606 if (info_level
!= FILE_NAMES_INFORMATION
)
3607 kstat
= ksmbd_vfs_init_kstat(&d_info
->wptr
, ksmbd_kstat
);
3609 switch (info_level
) {
3610 case FILE_FULL_DIRECTORY_INFORMATION
:
3612 struct file_full_directory_info
*ffdinfo
;
3614 ffdinfo
= (struct file_full_directory_info
*)kstat
;
3615 ffdinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3617 smb2_get_reparse_tag_special_file(ksmbd_kstat
->kstat
->mode
);
3618 if (ffdinfo
->EaSize
)
3619 ffdinfo
->ExtFileAttributes
= FILE_ATTRIBUTE_REPARSE_POINT_LE
;
3620 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3621 ffdinfo
->ExtFileAttributes
|= FILE_ATTRIBUTE_HIDDEN_LE
;
3622 memcpy(ffdinfo
->FileName
, conv_name
, conv_len
);
3623 ffdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3626 case FILE_BOTH_DIRECTORY_INFORMATION
:
3628 struct file_both_directory_info
*fbdinfo
;
3630 fbdinfo
= (struct file_both_directory_info
*)kstat
;
3631 fbdinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3633 smb2_get_reparse_tag_special_file(ksmbd_kstat
->kstat
->mode
);
3634 if (fbdinfo
->EaSize
)
3635 fbdinfo
->ExtFileAttributes
= FILE_ATTRIBUTE_REPARSE_POINT_LE
;
3636 fbdinfo
->ShortNameLength
= 0;
3637 fbdinfo
->Reserved
= 0;
3638 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3639 fbdinfo
->ExtFileAttributes
|= FILE_ATTRIBUTE_HIDDEN_LE
;
3640 memcpy(fbdinfo
->FileName
, conv_name
, conv_len
);
3641 fbdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3644 case FILE_DIRECTORY_INFORMATION
:
3646 struct file_directory_info
*fdinfo
;
3648 fdinfo
= (struct file_directory_info
*)kstat
;
3649 fdinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3650 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3651 fdinfo
->ExtFileAttributes
|= FILE_ATTRIBUTE_HIDDEN_LE
;
3652 memcpy(fdinfo
->FileName
, conv_name
, conv_len
);
3653 fdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3656 case FILE_NAMES_INFORMATION
:
3658 struct file_names_info
*fninfo
;
3660 fninfo
= (struct file_names_info
*)kstat
;
3661 fninfo
->FileNameLength
= cpu_to_le32(conv_len
);
3662 memcpy(fninfo
->FileName
, conv_name
, conv_len
);
3663 fninfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3666 case FILEID_FULL_DIRECTORY_INFORMATION
:
3668 struct file_id_full_dir_info
*dinfo
;
3670 dinfo
= (struct file_id_full_dir_info
*)kstat
;
3671 dinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3673 smb2_get_reparse_tag_special_file(ksmbd_kstat
->kstat
->mode
);
3675 dinfo
->ExtFileAttributes
= FILE_ATTRIBUTE_REPARSE_POINT_LE
;
3676 dinfo
->Reserved
= 0;
3677 dinfo
->UniqueId
= cpu_to_le64(ksmbd_kstat
->kstat
->ino
);
3678 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3679 dinfo
->ExtFileAttributes
|= FILE_ATTRIBUTE_HIDDEN_LE
;
3680 memcpy(dinfo
->FileName
, conv_name
, conv_len
);
3681 dinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3684 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3686 struct file_id_both_directory_info
*fibdinfo
;
3688 fibdinfo
= (struct file_id_both_directory_info
*)kstat
;
3689 fibdinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3691 smb2_get_reparse_tag_special_file(ksmbd_kstat
->kstat
->mode
);
3692 if (fibdinfo
->EaSize
)
3693 fibdinfo
->ExtFileAttributes
= FILE_ATTRIBUTE_REPARSE_POINT_LE
;
3694 fibdinfo
->UniqueId
= cpu_to_le64(ksmbd_kstat
->kstat
->ino
);
3695 fibdinfo
->ShortNameLength
= 0;
3696 fibdinfo
->Reserved
= 0;
3697 fibdinfo
->Reserved2
= cpu_to_le16(0);
3698 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3699 fibdinfo
->ExtFileAttributes
|= FILE_ATTRIBUTE_HIDDEN_LE
;
3700 memcpy(fibdinfo
->FileName
, conv_name
, conv_len
);
3701 fibdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3704 case SMB_FIND_FILE_POSIX_INFO
:
3706 struct smb2_posix_info
*posix_info
;
3709 posix_info
= (struct smb2_posix_info
*)kstat
;
3710 posix_info
->Ignored
= 0;
3711 posix_info
->CreationTime
= cpu_to_le64(ksmbd_kstat
->create_time
);
3712 time
= ksmbd_UnixTimeToNT(ksmbd_kstat
->kstat
->ctime
);
3713 posix_info
->ChangeTime
= cpu_to_le64(time
);
3714 time
= ksmbd_UnixTimeToNT(ksmbd_kstat
->kstat
->atime
);
3715 posix_info
->LastAccessTime
= cpu_to_le64(time
);
3716 time
= ksmbd_UnixTimeToNT(ksmbd_kstat
->kstat
->mtime
);
3717 posix_info
->LastWriteTime
= cpu_to_le64(time
);
3718 posix_info
->EndOfFile
= cpu_to_le64(ksmbd_kstat
->kstat
->size
);
3719 posix_info
->AllocationSize
= cpu_to_le64(ksmbd_kstat
->kstat
->blocks
<< 9);
3720 posix_info
->DeviceId
= cpu_to_le32(ksmbd_kstat
->kstat
->rdev
);
3721 posix_info
->HardLinks
= cpu_to_le32(ksmbd_kstat
->kstat
->nlink
);
3722 posix_info
->Mode
= cpu_to_le32(ksmbd_kstat
->kstat
->mode
& 0777);
3723 posix_info
->Inode
= cpu_to_le64(ksmbd_kstat
->kstat
->ino
);
3724 posix_info
->DosAttributes
=
3725 S_ISDIR(ksmbd_kstat
->kstat
->mode
) ?
3726 FILE_ATTRIBUTE_DIRECTORY_LE
: FILE_ATTRIBUTE_ARCHIVE_LE
;
3727 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3728 posix_info
->DosAttributes
|= FILE_ATTRIBUTE_HIDDEN_LE
;
3730 * SidBuffer(32) contain two sids(Domain sid(16), UNIX group sid(16)).
3731 * UNIX sid(16) = revision(1) + num_subauth(1) + authority(6) +
3732 * sub_auth(4 * 1(num_subauth)) + RID(4).
3734 id_to_sid(from_kuid_munged(&init_user_ns
, ksmbd_kstat
->kstat
->uid
),
3735 SIDUNIX_USER
, (struct smb_sid
*)&posix_info
->SidBuffer
[0]);
3736 id_to_sid(from_kgid_munged(&init_user_ns
, ksmbd_kstat
->kstat
->gid
),
3737 SIDUNIX_GROUP
, (struct smb_sid
*)&posix_info
->SidBuffer
[16]);
3738 memcpy(posix_info
->name
, conv_name
, conv_len
);
3739 posix_info
->name_len
= cpu_to_le32(conv_len
);
3740 posix_info
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3744 } /* switch (info_level) */
3746 d_info
->last_entry_offset
= d_info
->data_count
;
3747 d_info
->data_count
+= next_entry_offset
;
3748 d_info
->out_buf_len
-= next_entry_offset
;
3749 d_info
->wptr
+= next_entry_offset
;
3752 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3753 info_level
, d_info
->out_buf_len
,
3754 next_entry_offset
, d_info
->data_count
);
3761 struct smb2_query_dir_private
{
3762 struct ksmbd_work
*work
;
3763 char *search_pattern
;
3764 struct ksmbd_file
*dir_fp
;
3766 struct ksmbd_dir_info
*d_info
;
3770 static void lock_dir(struct ksmbd_file
*dir_fp
)
3772 struct dentry
*dir
= dir_fp
->filp
->f_path
.dentry
;
3774 inode_lock_nested(d_inode(dir
), I_MUTEX_PARENT
);
3777 static void unlock_dir(struct ksmbd_file
*dir_fp
)
3779 struct dentry
*dir
= dir_fp
->filp
->f_path
.dentry
;
3781 inode_unlock(d_inode(dir
));
3784 static int process_query_dir_entries(struct smb2_query_dir_private
*priv
)
3786 struct mnt_idmap
*idmap
= file_mnt_idmap(priv
->dir_fp
->filp
);
3788 struct ksmbd_kstat ksmbd_kstat
;
3792 for (i
= 0; i
< priv
->d_info
->num_entry
; i
++) {
3793 struct dentry
*dent
;
3795 if (dentry_name(priv
->d_info
, priv
->info_level
))
3798 lock_dir(priv
->dir_fp
);
3799 dent
= lookup_one(idmap
, priv
->d_info
->name
,
3800 priv
->dir_fp
->filp
->f_path
.dentry
,
3801 priv
->d_info
->name_len
);
3802 unlock_dir(priv
->dir_fp
);
3805 ksmbd_debug(SMB
, "Cannot lookup `%s' [%ld]\n",
3810 if (unlikely(d_is_negative(dent
))) {
3812 ksmbd_debug(SMB
, "Negative dentry `%s'\n",
3813 priv
->d_info
->name
);
3817 ksmbd_kstat
.kstat
= &kstat
;
3818 if (priv
->info_level
!= FILE_NAMES_INFORMATION
)
3819 ksmbd_vfs_fill_dentry_attrs(priv
->work
,
3824 rc
= smb2_populate_readdir_entry(priv
->work
->conn
,
3835 static int reserve_populate_dentry(struct ksmbd_dir_info
*d_info
,
3840 int next_entry_offset
;
3842 struct_sz
= readdir_info_level_struct_sz(info_level
);
3843 if (struct_sz
== -EOPNOTSUPP
)
3846 conv_len
= (d_info
->name_len
+ 1) * 2;
3847 next_entry_offset
= ALIGN(struct_sz
+ conv_len
,
3848 KSMBD_DIR_INFO_ALIGNMENT
);
3850 if (next_entry_offset
> d_info
->out_buf_len
) {
3851 d_info
->out_buf_len
= 0;
3855 switch (info_level
) {
3856 case FILE_FULL_DIRECTORY_INFORMATION
:
3858 struct file_full_directory_info
*ffdinfo
;
3860 ffdinfo
= (struct file_full_directory_info
*)d_info
->wptr
;
3861 memcpy(ffdinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3862 ffdinfo
->FileName
[d_info
->name_len
] = 0x00;
3863 ffdinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3864 ffdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3867 case FILE_BOTH_DIRECTORY_INFORMATION
:
3869 struct file_both_directory_info
*fbdinfo
;
3871 fbdinfo
= (struct file_both_directory_info
*)d_info
->wptr
;
3872 memcpy(fbdinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3873 fbdinfo
->FileName
[d_info
->name_len
] = 0x00;
3874 fbdinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3875 fbdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3878 case FILE_DIRECTORY_INFORMATION
:
3880 struct file_directory_info
*fdinfo
;
3882 fdinfo
= (struct file_directory_info
*)d_info
->wptr
;
3883 memcpy(fdinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3884 fdinfo
->FileName
[d_info
->name_len
] = 0x00;
3885 fdinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3886 fdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3889 case FILE_NAMES_INFORMATION
:
3891 struct file_names_info
*fninfo
;
3893 fninfo
= (struct file_names_info
*)d_info
->wptr
;
3894 memcpy(fninfo
->FileName
, d_info
->name
, d_info
->name_len
);
3895 fninfo
->FileName
[d_info
->name_len
] = 0x00;
3896 fninfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3897 fninfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3900 case FILEID_FULL_DIRECTORY_INFORMATION
:
3902 struct file_id_full_dir_info
*dinfo
;
3904 dinfo
= (struct file_id_full_dir_info
*)d_info
->wptr
;
3905 memcpy(dinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3906 dinfo
->FileName
[d_info
->name_len
] = 0x00;
3907 dinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3908 dinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3911 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3913 struct file_id_both_directory_info
*fibdinfo
;
3915 fibdinfo
= (struct file_id_both_directory_info
*)d_info
->wptr
;
3916 memcpy(fibdinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3917 fibdinfo
->FileName
[d_info
->name_len
] = 0x00;
3918 fibdinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3919 fibdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3922 case SMB_FIND_FILE_POSIX_INFO
:
3924 struct smb2_posix_info
*posix_info
;
3926 posix_info
= (struct smb2_posix_info
*)d_info
->wptr
;
3927 memcpy(posix_info
->name
, d_info
->name
, d_info
->name_len
);
3928 posix_info
->name
[d_info
->name_len
] = 0x00;
3929 posix_info
->name_len
= cpu_to_le32(d_info
->name_len
);
3930 posix_info
->NextEntryOffset
=
3931 cpu_to_le32(next_entry_offset
);
3934 } /* switch (info_level) */
3936 d_info
->num_entry
++;
3937 d_info
->out_buf_len
-= next_entry_offset
;
3938 d_info
->wptr
+= next_entry_offset
;
3942 static bool __query_dir(struct dir_context
*ctx
, const char *name
, int namlen
,
3943 loff_t offset
, u64 ino
, unsigned int d_type
)
3945 struct ksmbd_readdir_data
*buf
;
3946 struct smb2_query_dir_private
*priv
;
3947 struct ksmbd_dir_info
*d_info
;
3950 buf
= container_of(ctx
, struct ksmbd_readdir_data
, ctx
);
3951 priv
= buf
->private;
3952 d_info
= priv
->d_info
;
3954 /* dot and dotdot entries are already reserved */
3955 if (!strcmp(".", name
) || !strcmp("..", name
))
3957 if (ksmbd_share_veto_filename(priv
->work
->tcon
->share_conf
, name
))
3959 if (!match_pattern(name
, namlen
, priv
->search_pattern
))
3962 d_info
->name
= name
;
3963 d_info
->name_len
= namlen
;
3964 rc
= reserve_populate_dentry(d_info
, priv
->info_level
);
3967 if (d_info
->flags
& SMB2_RETURN_SINGLE_ENTRY
)
3968 d_info
->out_buf_len
= 0;
3972 static int verify_info_level(int info_level
)
3974 switch (info_level
) {
3975 case FILE_FULL_DIRECTORY_INFORMATION
:
3976 case FILE_BOTH_DIRECTORY_INFORMATION
:
3977 case FILE_DIRECTORY_INFORMATION
:
3978 case FILE_NAMES_INFORMATION
:
3979 case FILEID_FULL_DIRECTORY_INFORMATION
:
3980 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3981 case SMB_FIND_FILE_POSIX_INFO
:
3990 static int smb2_resp_buf_len(struct ksmbd_work
*work
, unsigned short hdr2_len
)
3994 free_len
= (int)(work
->response_sz
-
3995 (get_rfc1002_len(work
->response_buf
) + 4)) - hdr2_len
;
3999 static int smb2_calc_max_out_buf_len(struct ksmbd_work
*work
,
4000 unsigned short hdr2_len
,
4001 unsigned int out_buf_len
)
4005 if (out_buf_len
> work
->conn
->vals
->max_trans_size
)
4008 free_len
= smb2_resp_buf_len(work
, hdr2_len
);
4012 return min_t(int, out_buf_len
, free_len
);
4015 int smb2_query_dir(struct ksmbd_work
*work
)
4017 struct ksmbd_conn
*conn
= work
->conn
;
4018 struct smb2_query_directory_req
*req
;
4019 struct smb2_query_directory_rsp
*rsp
;
4020 struct ksmbd_share_config
*share
= work
->tcon
->share_conf
;
4021 struct ksmbd_file
*dir_fp
= NULL
;
4022 struct ksmbd_dir_info d_info
;
4024 char *srch_ptr
= NULL
;
4025 unsigned char srch_flag
;
4027 struct smb2_query_dir_private query_dir_private
= {NULL
, };
4029 WORK_BUFFERS(work
, req
, rsp
);
4031 if (ksmbd_override_fsids(work
)) {
4032 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
4033 smb2_set_err_rsp(work
);
4037 rc
= verify_info_level(req
->FileInformationClass
);
4043 dir_fp
= ksmbd_lookup_fd_slow(work
, req
->VolatileFileId
, req
->PersistentFileId
);
4049 if (!(dir_fp
->daccess
& FILE_LIST_DIRECTORY_LE
) ||
4050 inode_permission(file_mnt_idmap(dir_fp
->filp
),
4051 file_inode(dir_fp
->filp
),
4052 MAY_READ
| MAY_EXEC
)) {
4053 pr_err("no right to enumerate directory (%pD)\n", dir_fp
->filp
);
4058 if (!S_ISDIR(file_inode(dir_fp
->filp
)->i_mode
)) {
4059 pr_err("can't do query dir for a file\n");
4064 srch_flag
= req
->Flags
;
4065 srch_ptr
= smb_strndup_from_utf16(req
->Buffer
,
4066 le16_to_cpu(req
->FileNameLength
), 1,
4068 if (IS_ERR(srch_ptr
)) {
4069 ksmbd_debug(SMB
, "Search Pattern not found\n");
4073 ksmbd_debug(SMB
, "Search pattern is %s\n", srch_ptr
);
4076 if (srch_flag
& SMB2_REOPEN
|| srch_flag
& SMB2_RESTART_SCANS
) {
4077 ksmbd_debug(SMB
, "Restart directory scan\n");
4078 generic_file_llseek(dir_fp
->filp
, 0, SEEK_SET
);
4081 memset(&d_info
, 0, sizeof(struct ksmbd_dir_info
));
4082 d_info
.wptr
= (char *)rsp
->Buffer
;
4083 d_info
.rptr
= (char *)rsp
->Buffer
;
4084 d_info
.out_buf_len
=
4085 smb2_calc_max_out_buf_len(work
, 8,
4086 le32_to_cpu(req
->OutputBufferLength
));
4087 if (d_info
.out_buf_len
< 0) {
4091 d_info
.flags
= srch_flag
;
4094 * reserve dot and dotdot entries in head of buffer
4097 rc
= ksmbd_populate_dot_dotdot_entries(work
, req
->FileInformationClass
,
4098 dir_fp
, &d_info
, srch_ptr
,
4099 smb2_populate_readdir_entry
);
4105 if (test_share_config_flag(share
, KSMBD_SHARE_FLAG_HIDE_DOT_FILES
))
4106 d_info
.hide_dot_file
= true;
4108 buffer_sz
= d_info
.out_buf_len
;
4109 d_info
.rptr
= d_info
.wptr
;
4110 query_dir_private
.work
= work
;
4111 query_dir_private
.search_pattern
= srch_ptr
;
4112 query_dir_private
.dir_fp
= dir_fp
;
4113 query_dir_private
.d_info
= &d_info
;
4114 query_dir_private
.info_level
= req
->FileInformationClass
;
4115 dir_fp
->readdir_data
.private = &query_dir_private
;
4116 set_ctx_actor(&dir_fp
->readdir_data
.ctx
, __query_dir
);
4118 rc
= iterate_dir(dir_fp
->filp
, &dir_fp
->readdir_data
.ctx
);
4120 * req->OutputBufferLength is too small to contain even one entry.
4121 * In this case, it immediately returns OutputBufferLength 0 to client.
4123 if (!d_info
.out_buf_len
&& !d_info
.num_entry
)
4125 if (rc
> 0 || rc
== -ENOSPC
)
4130 d_info
.wptr
= d_info
.rptr
;
4131 d_info
.out_buf_len
= buffer_sz
;
4132 rc
= process_query_dir_entries(&query_dir_private
);
4136 if (!d_info
.data_count
&& d_info
.out_buf_len
>= 0) {
4137 if (srch_flag
& SMB2_RETURN_SINGLE_ENTRY
&& !is_asterisk(srch_ptr
)) {
4138 rsp
->hdr
.Status
= STATUS_NO_SUCH_FILE
;
4140 dir_fp
->dot_dotdot
[0] = dir_fp
->dot_dotdot
[1] = 0;
4141 rsp
->hdr
.Status
= STATUS_NO_MORE_FILES
;
4143 rsp
->StructureSize
= cpu_to_le16(9);
4144 rsp
->OutputBufferOffset
= cpu_to_le16(0);
4145 rsp
->OutputBufferLength
= cpu_to_le32(0);
4147 rc
= ksmbd_iov_pin_rsp(work
, (void *)rsp
,
4148 sizeof(struct smb2_query_directory_rsp
));
4153 ((struct file_directory_info
*)
4154 ((char *)rsp
->Buffer
+ d_info
.last_entry_offset
))
4155 ->NextEntryOffset
= 0;
4156 if (d_info
.data_count
>= d_info
.last_entry_off_align
)
4157 d_info
.data_count
-= d_info
.last_entry_off_align
;
4159 rsp
->StructureSize
= cpu_to_le16(9);
4160 rsp
->OutputBufferOffset
= cpu_to_le16(72);
4161 rsp
->OutputBufferLength
= cpu_to_le32(d_info
.data_count
);
4162 rc
= ksmbd_iov_pin_rsp(work
, (void *)rsp
,
4163 offsetof(struct smb2_query_directory_rsp
, Buffer
) +
4170 ksmbd_fd_put(work
, dir_fp
);
4171 ksmbd_revert_fsids(work
);
4175 pr_err("error while processing smb2 query dir rc = %d\n", rc
);
4180 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
4181 else if (rc
== -EACCES
)
4182 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
4183 else if (rc
== -ENOENT
)
4184 rsp
->hdr
.Status
= STATUS_NO_SUCH_FILE
;
4185 else if (rc
== -EBADF
)
4186 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
4187 else if (rc
== -ENOMEM
)
4188 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
4189 else if (rc
== -EFAULT
)
4190 rsp
->hdr
.Status
= STATUS_INVALID_INFO_CLASS
;
4191 else if (rc
== -EIO
)
4192 rsp
->hdr
.Status
= STATUS_FILE_CORRUPT_ERROR
;
4193 if (!rsp
->hdr
.Status
)
4194 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
4196 smb2_set_err_rsp(work
);
4197 ksmbd_fd_put(work
, dir_fp
);
4198 ksmbd_revert_fsids(work
);
4203 * buffer_check_err() - helper function to check buffer errors
4204 * @reqOutputBufferLength: max buffer length expected in command response
4205 * @rsp: query info response buffer contains output buffer length
4206 * @rsp_org: base response buffer pointer in case of chained response
4208 * Return: 0 on success, otherwise error
4210 static int buffer_check_err(int reqOutputBufferLength
,
4211 struct smb2_query_info_rsp
*rsp
,
4214 if (reqOutputBufferLength
< le32_to_cpu(rsp
->OutputBufferLength
)) {
4215 pr_err("Invalid Buffer Size Requested\n");
4216 rsp
->hdr
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
4217 *(__be32
*)rsp_org
= cpu_to_be32(sizeof(struct smb2_hdr
));
4223 static void get_standard_info_pipe(struct smb2_query_info_rsp
*rsp
,
4226 struct smb2_file_standard_info
*sinfo
;
4228 sinfo
= (struct smb2_file_standard_info
*)rsp
->Buffer
;
4230 sinfo
->AllocationSize
= cpu_to_le64(4096);
4231 sinfo
->EndOfFile
= cpu_to_le64(0);
4232 sinfo
->NumberOfLinks
= cpu_to_le32(1);
4233 sinfo
->DeletePending
= 1;
4234 sinfo
->Directory
= 0;
4235 rsp
->OutputBufferLength
=
4236 cpu_to_le32(sizeof(struct smb2_file_standard_info
));
4239 static void get_internal_info_pipe(struct smb2_query_info_rsp
*rsp
, u64 num
,
4242 struct smb2_file_internal_info
*file_info
;
4244 file_info
= (struct smb2_file_internal_info
*)rsp
->Buffer
;
4246 /* any unique number */
4247 file_info
->IndexNumber
= cpu_to_le64(num
| (1ULL << 63));
4248 rsp
->OutputBufferLength
=
4249 cpu_to_le32(sizeof(struct smb2_file_internal_info
));
4252 static int smb2_get_info_file_pipe(struct ksmbd_session
*sess
,
4253 struct smb2_query_info_req
*req
,
4254 struct smb2_query_info_rsp
*rsp
,
4261 * Windows can sometime send query file info request on
4262 * pipe without opening it, checking error condition here
4264 id
= req
->VolatileFileId
;
4265 if (!ksmbd_session_rpc_method(sess
, id
))
4268 ksmbd_debug(SMB
, "FileInfoClass %u, FileId 0x%llx\n",
4269 req
->FileInfoClass
, req
->VolatileFileId
);
4271 switch (req
->FileInfoClass
) {
4272 case FILE_STANDARD_INFORMATION
:
4273 get_standard_info_pipe(rsp
, rsp_org
);
4274 rc
= buffer_check_err(le32_to_cpu(req
->OutputBufferLength
),
4277 case FILE_INTERNAL_INFORMATION
:
4278 get_internal_info_pipe(rsp
, id
, rsp_org
);
4279 rc
= buffer_check_err(le32_to_cpu(req
->OutputBufferLength
),
4283 ksmbd_debug(SMB
, "smb2_info_file_pipe for %u not supported\n",
4284 req
->FileInfoClass
);
4291 * smb2_get_ea() - handler for smb2 get extended attribute command
4292 * @work: smb work containing query info command buffer
4293 * @fp: ksmbd_file pointer
4294 * @req: get extended attribute request
4295 * @rsp: response buffer pointer
4296 * @rsp_org: base response buffer pointer in case of chained response
4298 * Return: 0 on success, otherwise error
4300 static int smb2_get_ea(struct ksmbd_work
*work
, struct ksmbd_file
*fp
,
4301 struct smb2_query_info_req
*req
,
4302 struct smb2_query_info_rsp
*rsp
, void *rsp_org
)
4304 struct smb2_ea_info
*eainfo
, *prev_eainfo
;
4305 char *name
, *ptr
, *xattr_list
= NULL
, *buf
;
4306 int rc
, name_len
, value_len
, xattr_list_len
, idx
;
4307 ssize_t buf_free_len
, alignment_bytes
, next_offset
, rsp_data_cnt
= 0;
4308 struct smb2_ea_info_req
*ea_req
= NULL
;
4309 const struct path
*path
;
4310 struct mnt_idmap
*idmap
= file_mnt_idmap(fp
->filp
);
4312 if (!(fp
->daccess
& FILE_READ_EA_LE
)) {
4313 pr_err("Not permitted to read ext attr : 0x%x\n",
4318 path
= &fp
->filp
->f_path
;
4319 /* single EA entry is requested with given user.* name */
4320 if (req
->InputBufferLength
) {
4321 if (le32_to_cpu(req
->InputBufferLength
) <
4322 sizeof(struct smb2_ea_info_req
))
4325 ea_req
= (struct smb2_ea_info_req
*)req
->Buffer
;
4327 /* need to send all EAs, if no specific EA is requested*/
4328 if (le32_to_cpu(req
->Flags
) & SL_RETURN_SINGLE_ENTRY
)
4330 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4331 le32_to_cpu(req
->Flags
));
4335 smb2_calc_max_out_buf_len(work
, 8,
4336 le32_to_cpu(req
->OutputBufferLength
));
4337 if (buf_free_len
< 0)
4340 rc
= ksmbd_vfs_listxattr(path
->dentry
, &xattr_list
);
4342 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
4344 } else if (!rc
) { /* there is no EA in the file */
4345 ksmbd_debug(SMB
, "no ea data in the file\n");
4348 xattr_list_len
= rc
;
4350 ptr
= (char *)rsp
->Buffer
;
4351 eainfo
= (struct smb2_ea_info
*)ptr
;
4352 prev_eainfo
= eainfo
;
4355 while (idx
< xattr_list_len
) {
4356 name
= xattr_list
+ idx
;
4357 name_len
= strlen(name
);
4359 ksmbd_debug(SMB
, "%s, len %d\n", name
, name_len
);
4360 idx
+= name_len
+ 1;
4363 * CIFS does not support EA other than user.* namespace,
4364 * still keep the framework generic, to list other attrs
4367 if (strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
4370 if (!strncmp(&name
[XATTR_USER_PREFIX_LEN
], STREAM_PREFIX
,
4374 if (req
->InputBufferLength
&&
4375 strncmp(&name
[XATTR_USER_PREFIX_LEN
], ea_req
->name
,
4376 ea_req
->EaNameLength
))
4379 if (!strncmp(&name
[XATTR_USER_PREFIX_LEN
],
4380 DOS_ATTRIBUTE_PREFIX
, DOS_ATTRIBUTE_PREFIX_LEN
))
4383 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
4384 name_len
-= XATTR_USER_PREFIX_LEN
;
4386 ptr
= eainfo
->name
+ name_len
+ 1;
4387 buf_free_len
-= (offsetof(struct smb2_ea_info
, name
) +
4389 /* bailout if xattr can't fit in buf_free_len */
4390 value_len
= ksmbd_vfs_getxattr(idmap
, path
->dentry
,
4392 if (value_len
<= 0) {
4394 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
4398 buf_free_len
-= value_len
;
4399 if (buf_free_len
< 0) {
4404 memcpy(ptr
, buf
, value_len
);
4409 eainfo
->EaNameLength
= name_len
;
4411 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
4412 memcpy(eainfo
->name
, &name
[XATTR_USER_PREFIX_LEN
],
4415 memcpy(eainfo
->name
, name
, name_len
);
4417 eainfo
->name
[name_len
] = '\0';
4418 eainfo
->EaValueLength
= cpu_to_le16(value_len
);
4419 next_offset
= offsetof(struct smb2_ea_info
, name
) +
4420 name_len
+ 1 + value_len
;
4422 /* align next xattr entry at 4 byte bundary */
4423 alignment_bytes
= ((next_offset
+ 3) & ~3) - next_offset
;
4424 if (alignment_bytes
) {
4425 memset(ptr
, '\0', alignment_bytes
);
4426 ptr
+= alignment_bytes
;
4427 next_offset
+= alignment_bytes
;
4428 buf_free_len
-= alignment_bytes
;
4430 eainfo
->NextEntryOffset
= cpu_to_le32(next_offset
);
4431 prev_eainfo
= eainfo
;
4432 eainfo
= (struct smb2_ea_info
*)ptr
;
4433 rsp_data_cnt
+= next_offset
;
4435 if (req
->InputBufferLength
) {
4436 ksmbd_debug(SMB
, "single entry requested\n");
4441 /* no more ea entries */
4442 prev_eainfo
->NextEntryOffset
= 0;
4445 if (rsp_data_cnt
== 0)
4446 rsp
->hdr
.Status
= STATUS_NO_EAS_ON_FILE
;
4447 rsp
->OutputBufferLength
= cpu_to_le32(rsp_data_cnt
);
4453 static void get_file_access_info(struct smb2_query_info_rsp
*rsp
,
4454 struct ksmbd_file
*fp
, void *rsp_org
)
4456 struct smb2_file_access_info
*file_info
;
4458 file_info
= (struct smb2_file_access_info
*)rsp
->Buffer
;
4459 file_info
->AccessFlags
= fp
->daccess
;
4460 rsp
->OutputBufferLength
=
4461 cpu_to_le32(sizeof(struct smb2_file_access_info
));
4464 static int get_file_basic_info(struct smb2_query_info_rsp
*rsp
,
4465 struct ksmbd_file
*fp
, void *rsp_org
)
4467 struct smb2_file_basic_info
*basic_info
;
4471 if (!(fp
->daccess
& FILE_READ_ATTRIBUTES_LE
)) {
4472 pr_err("no right to read the attributes : 0x%x\n",
4477 basic_info
= (struct smb2_file_basic_info
*)rsp
->Buffer
;
4478 generic_fillattr(file_mnt_idmap(fp
->filp
), STATX_BASIC_STATS
,
4479 file_inode(fp
->filp
), &stat
);
4480 basic_info
->CreationTime
= cpu_to_le64(fp
->create_time
);
4481 time
= ksmbd_UnixTimeToNT(stat
.atime
);
4482 basic_info
->LastAccessTime
= cpu_to_le64(time
);
4483 time
= ksmbd_UnixTimeToNT(stat
.mtime
);
4484 basic_info
->LastWriteTime
= cpu_to_le64(time
);
4485 time
= ksmbd_UnixTimeToNT(stat
.ctime
);
4486 basic_info
->ChangeTime
= cpu_to_le64(time
);
4487 basic_info
->Attributes
= fp
->f_ci
->m_fattr
;
4488 basic_info
->Pad1
= 0;
4489 rsp
->OutputBufferLength
=
4490 cpu_to_le32(sizeof(struct smb2_file_basic_info
));
4494 static void get_file_standard_info(struct smb2_query_info_rsp
*rsp
,
4495 struct ksmbd_file
*fp
, void *rsp_org
)
4497 struct smb2_file_standard_info
*sinfo
;
4498 unsigned int delete_pending
;
4499 struct inode
*inode
;
4502 inode
= file_inode(fp
->filp
);
4503 generic_fillattr(file_mnt_idmap(fp
->filp
), STATX_BASIC_STATS
, inode
, &stat
);
4505 sinfo
= (struct smb2_file_standard_info
*)rsp
->Buffer
;
4506 delete_pending
= ksmbd_inode_pending_delete(fp
);
4508 sinfo
->AllocationSize
= cpu_to_le64(inode
->i_blocks
<< 9);
4509 sinfo
->EndOfFile
= S_ISDIR(stat
.mode
) ? 0 : cpu_to_le64(stat
.size
);
4510 sinfo
->NumberOfLinks
= cpu_to_le32(get_nlink(&stat
) - delete_pending
);
4511 sinfo
->DeletePending
= delete_pending
;
4512 sinfo
->Directory
= S_ISDIR(stat
.mode
) ? 1 : 0;
4513 rsp
->OutputBufferLength
=
4514 cpu_to_le32(sizeof(struct smb2_file_standard_info
));
4517 static void get_file_alignment_info(struct smb2_query_info_rsp
*rsp
,
4520 struct smb2_file_alignment_info
*file_info
;
4522 file_info
= (struct smb2_file_alignment_info
*)rsp
->Buffer
;
4523 file_info
->AlignmentRequirement
= 0;
4524 rsp
->OutputBufferLength
=
4525 cpu_to_le32(sizeof(struct smb2_file_alignment_info
));
4528 static int get_file_all_info(struct ksmbd_work
*work
,
4529 struct smb2_query_info_rsp
*rsp
,
4530 struct ksmbd_file
*fp
,
4533 struct ksmbd_conn
*conn
= work
->conn
;
4534 struct smb2_file_all_info
*file_info
;
4535 unsigned int delete_pending
;
4536 struct inode
*inode
;
4542 if (!(fp
->daccess
& FILE_READ_ATTRIBUTES_LE
)) {
4543 ksmbd_debug(SMB
, "no right to read the attributes : 0x%x\n",
4548 filename
= convert_to_nt_pathname(work
->tcon
->share_conf
, &fp
->filp
->f_path
);
4549 if (IS_ERR(filename
))
4550 return PTR_ERR(filename
);
4552 inode
= file_inode(fp
->filp
);
4553 generic_fillattr(file_mnt_idmap(fp
->filp
), STATX_BASIC_STATS
, inode
, &stat
);
4555 ksmbd_debug(SMB
, "filename = %s\n", filename
);
4556 delete_pending
= ksmbd_inode_pending_delete(fp
);
4557 file_info
= (struct smb2_file_all_info
*)rsp
->Buffer
;
4559 file_info
->CreationTime
= cpu_to_le64(fp
->create_time
);
4560 time
= ksmbd_UnixTimeToNT(stat
.atime
);
4561 file_info
->LastAccessTime
= cpu_to_le64(time
);
4562 time
= ksmbd_UnixTimeToNT(stat
.mtime
);
4563 file_info
->LastWriteTime
= cpu_to_le64(time
);
4564 time
= ksmbd_UnixTimeToNT(stat
.ctime
);
4565 file_info
->ChangeTime
= cpu_to_le64(time
);
4566 file_info
->Attributes
= fp
->f_ci
->m_fattr
;
4567 file_info
->Pad1
= 0;
4568 file_info
->AllocationSize
=
4569 cpu_to_le64(inode
->i_blocks
<< 9);
4570 file_info
->EndOfFile
= S_ISDIR(stat
.mode
) ? 0 : cpu_to_le64(stat
.size
);
4571 file_info
->NumberOfLinks
=
4572 cpu_to_le32(get_nlink(&stat
) - delete_pending
);
4573 file_info
->DeletePending
= delete_pending
;
4574 file_info
->Directory
= S_ISDIR(stat
.mode
) ? 1 : 0;
4575 file_info
->Pad2
= 0;
4576 file_info
->IndexNumber
= cpu_to_le64(stat
.ino
);
4577 file_info
->EASize
= 0;
4578 file_info
->AccessFlags
= fp
->daccess
;
4579 file_info
->CurrentByteOffset
= cpu_to_le64(fp
->filp
->f_pos
);
4580 file_info
->Mode
= fp
->coption
;
4581 file_info
->AlignmentRequirement
= 0;
4582 conv_len
= smbConvertToUTF16((__le16
*)file_info
->FileName
, filename
,
4583 PATH_MAX
, conn
->local_nls
, 0);
4585 file_info
->FileNameLength
= cpu_to_le32(conv_len
);
4586 rsp
->OutputBufferLength
=
4587 cpu_to_le32(sizeof(struct smb2_file_all_info
) + conv_len
- 1);
4592 static void get_file_alternate_info(struct ksmbd_work
*work
,
4593 struct smb2_query_info_rsp
*rsp
,
4594 struct ksmbd_file
*fp
,
4597 struct ksmbd_conn
*conn
= work
->conn
;
4598 struct smb2_file_alt_name_info
*file_info
;
4599 struct dentry
*dentry
= fp
->filp
->f_path
.dentry
;
4602 spin_lock(&dentry
->d_lock
);
4603 file_info
= (struct smb2_file_alt_name_info
*)rsp
->Buffer
;
4604 conv_len
= ksmbd_extract_shortname(conn
,
4605 dentry
->d_name
.name
,
4606 file_info
->FileName
);
4607 spin_unlock(&dentry
->d_lock
);
4608 file_info
->FileNameLength
= cpu_to_le32(conv_len
);
4609 rsp
->OutputBufferLength
=
4610 cpu_to_le32(sizeof(struct smb2_file_alt_name_info
) + conv_len
);
4613 static void get_file_stream_info(struct ksmbd_work
*work
,
4614 struct smb2_query_info_rsp
*rsp
,
4615 struct ksmbd_file
*fp
,
4618 struct ksmbd_conn
*conn
= work
->conn
;
4619 struct smb2_file_stream_info
*file_info
;
4620 char *stream_name
, *xattr_list
= NULL
, *stream_buf
;
4622 const struct path
*path
= &fp
->filp
->f_path
;
4623 ssize_t xattr_list_len
;
4624 int nbytes
= 0, streamlen
, stream_name_len
, next
, idx
= 0;
4626 struct smb2_query_info_req
*req
= ksmbd_req_buf_next(work
);
4628 generic_fillattr(file_mnt_idmap(fp
->filp
), STATX_BASIC_STATS
,
4629 file_inode(fp
->filp
), &stat
);
4630 file_info
= (struct smb2_file_stream_info
*)rsp
->Buffer
;
4633 smb2_calc_max_out_buf_len(work
, 8,
4634 le32_to_cpu(req
->OutputBufferLength
));
4635 if (buf_free_len
< 0)
4638 xattr_list_len
= ksmbd_vfs_listxattr(path
->dentry
, &xattr_list
);
4639 if (xattr_list_len
< 0) {
4641 } else if (!xattr_list_len
) {
4642 ksmbd_debug(SMB
, "empty xattr in the file\n");
4646 while (idx
< xattr_list_len
) {
4647 stream_name
= xattr_list
+ idx
;
4648 streamlen
= strlen(stream_name
);
4649 idx
+= streamlen
+ 1;
4651 ksmbd_debug(SMB
, "%s, len %d\n", stream_name
, streamlen
);
4653 if (strncmp(&stream_name
[XATTR_USER_PREFIX_LEN
],
4654 STREAM_PREFIX
, STREAM_PREFIX_LEN
))
4657 stream_name_len
= streamlen
- (XATTR_USER_PREFIX_LEN
+
4659 streamlen
= stream_name_len
;
4663 stream_buf
= kmalloc(streamlen
+ 1, GFP_KERNEL
);
4667 streamlen
= snprintf(stream_buf
, streamlen
+ 1,
4668 ":%s", &stream_name
[XATTR_NAME_STREAM_LEN
]);
4670 next
= sizeof(struct smb2_file_stream_info
) + streamlen
* 2;
4671 if (next
> buf_free_len
) {
4676 file_info
= (struct smb2_file_stream_info
*)&rsp
->Buffer
[nbytes
];
4677 streamlen
= smbConvertToUTF16((__le16
*)file_info
->StreamName
,
4678 stream_buf
, streamlen
,
4679 conn
->local_nls
, 0);
4682 file_info
->StreamNameLength
= cpu_to_le32(streamlen
);
4683 file_info
->StreamSize
= cpu_to_le64(stream_name_len
);
4684 file_info
->StreamAllocationSize
= cpu_to_le64(stream_name_len
);
4687 buf_free_len
-= next
;
4688 file_info
->NextEntryOffset
= cpu_to_le32(next
);
4692 if (!S_ISDIR(stat
.mode
) &&
4693 buf_free_len
>= sizeof(struct smb2_file_stream_info
) + 7 * 2) {
4694 file_info
= (struct smb2_file_stream_info
*)
4695 &rsp
->Buffer
[nbytes
];
4696 streamlen
= smbConvertToUTF16((__le16
*)file_info
->StreamName
,
4697 "::$DATA", 7, conn
->local_nls
, 0);
4699 file_info
->StreamNameLength
= cpu_to_le32(streamlen
);
4700 file_info
->StreamSize
= cpu_to_le64(stat
.size
);
4701 file_info
->StreamAllocationSize
= cpu_to_le64(stat
.blocks
<< 9);
4702 nbytes
+= sizeof(struct smb2_file_stream_info
) + streamlen
;
4705 /* last entry offset should be 0 */
4706 file_info
->NextEntryOffset
= 0;
4709 rsp
->OutputBufferLength
= cpu_to_le32(nbytes
);
4712 static void get_file_internal_info(struct smb2_query_info_rsp
*rsp
,
4713 struct ksmbd_file
*fp
, void *rsp_org
)
4715 struct smb2_file_internal_info
*file_info
;
4718 generic_fillattr(file_mnt_idmap(fp
->filp
), STATX_BASIC_STATS
,
4719 file_inode(fp
->filp
), &stat
);
4720 file_info
= (struct smb2_file_internal_info
*)rsp
->Buffer
;
4721 file_info
->IndexNumber
= cpu_to_le64(stat
.ino
);
4722 rsp
->OutputBufferLength
=
4723 cpu_to_le32(sizeof(struct smb2_file_internal_info
));
4726 static int get_file_network_open_info(struct smb2_query_info_rsp
*rsp
,
4727 struct ksmbd_file
*fp
, void *rsp_org
)
4729 struct smb2_file_ntwrk_info
*file_info
;
4730 struct inode
*inode
;
4734 if (!(fp
->daccess
& FILE_READ_ATTRIBUTES_LE
)) {
4735 pr_err("no right to read the attributes : 0x%x\n",
4740 file_info
= (struct smb2_file_ntwrk_info
*)rsp
->Buffer
;
4742 inode
= file_inode(fp
->filp
);
4743 generic_fillattr(file_mnt_idmap(fp
->filp
), STATX_BASIC_STATS
, inode
, &stat
);
4745 file_info
->CreationTime
= cpu_to_le64(fp
->create_time
);
4746 time
= ksmbd_UnixTimeToNT(stat
.atime
);
4747 file_info
->LastAccessTime
= cpu_to_le64(time
);
4748 time
= ksmbd_UnixTimeToNT(stat
.mtime
);
4749 file_info
->LastWriteTime
= cpu_to_le64(time
);
4750 time
= ksmbd_UnixTimeToNT(stat
.ctime
);
4751 file_info
->ChangeTime
= cpu_to_le64(time
);
4752 file_info
->Attributes
= fp
->f_ci
->m_fattr
;
4753 file_info
->AllocationSize
=
4754 cpu_to_le64(inode
->i_blocks
<< 9);
4755 file_info
->EndOfFile
= S_ISDIR(stat
.mode
) ? 0 : cpu_to_le64(stat
.size
);
4756 file_info
->Reserved
= cpu_to_le32(0);
4757 rsp
->OutputBufferLength
=
4758 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info
));
4762 static void get_file_ea_info(struct smb2_query_info_rsp
*rsp
, void *rsp_org
)
4764 struct smb2_file_ea_info
*file_info
;
4766 file_info
= (struct smb2_file_ea_info
*)rsp
->Buffer
;
4767 file_info
->EASize
= 0;
4768 rsp
->OutputBufferLength
=
4769 cpu_to_le32(sizeof(struct smb2_file_ea_info
));
4772 static void get_file_position_info(struct smb2_query_info_rsp
*rsp
,
4773 struct ksmbd_file
*fp
, void *rsp_org
)
4775 struct smb2_file_pos_info
*file_info
;
4777 file_info
= (struct smb2_file_pos_info
*)rsp
->Buffer
;
4778 file_info
->CurrentByteOffset
= cpu_to_le64(fp
->filp
->f_pos
);
4779 rsp
->OutputBufferLength
=
4780 cpu_to_le32(sizeof(struct smb2_file_pos_info
));
4783 static void get_file_mode_info(struct smb2_query_info_rsp
*rsp
,
4784 struct ksmbd_file
*fp
, void *rsp_org
)
4786 struct smb2_file_mode_info
*file_info
;
4788 file_info
= (struct smb2_file_mode_info
*)rsp
->Buffer
;
4789 file_info
->Mode
= fp
->coption
& FILE_MODE_INFO_MASK
;
4790 rsp
->OutputBufferLength
=
4791 cpu_to_le32(sizeof(struct smb2_file_mode_info
));
4794 static void get_file_compression_info(struct smb2_query_info_rsp
*rsp
,
4795 struct ksmbd_file
*fp
, void *rsp_org
)
4797 struct smb2_file_comp_info
*file_info
;
4800 generic_fillattr(file_mnt_idmap(fp
->filp
), STATX_BASIC_STATS
,
4801 file_inode(fp
->filp
), &stat
);
4803 file_info
= (struct smb2_file_comp_info
*)rsp
->Buffer
;
4804 file_info
->CompressedFileSize
= cpu_to_le64(stat
.blocks
<< 9);
4805 file_info
->CompressionFormat
= COMPRESSION_FORMAT_NONE
;
4806 file_info
->CompressionUnitShift
= 0;
4807 file_info
->ChunkShift
= 0;
4808 file_info
->ClusterShift
= 0;
4809 memset(&file_info
->Reserved
[0], 0, 3);
4811 rsp
->OutputBufferLength
=
4812 cpu_to_le32(sizeof(struct smb2_file_comp_info
));
4815 static int get_file_attribute_tag_info(struct smb2_query_info_rsp
*rsp
,
4816 struct ksmbd_file
*fp
, void *rsp_org
)
4818 struct smb2_file_attr_tag_info
*file_info
;
4820 if (!(fp
->daccess
& FILE_READ_ATTRIBUTES_LE
)) {
4821 pr_err("no right to read the attributes : 0x%x\n",
4826 file_info
= (struct smb2_file_attr_tag_info
*)rsp
->Buffer
;
4827 file_info
->FileAttributes
= fp
->f_ci
->m_fattr
;
4828 file_info
->ReparseTag
= 0;
4829 rsp
->OutputBufferLength
=
4830 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info
));
4834 static void find_file_posix_info(struct smb2_query_info_rsp
*rsp
,
4835 struct ksmbd_file
*fp
, void *rsp_org
)
4837 struct smb311_posix_qinfo
*file_info
;
4838 struct inode
*inode
= file_inode(fp
->filp
);
4839 struct mnt_idmap
*idmap
= file_mnt_idmap(fp
->filp
);
4840 vfsuid_t vfsuid
= i_uid_into_vfsuid(idmap
, inode
);
4841 vfsgid_t vfsgid
= i_gid_into_vfsgid(idmap
, inode
);
4843 int out_buf_len
= sizeof(struct smb311_posix_qinfo
) + 32;
4845 file_info
= (struct smb311_posix_qinfo
*)rsp
->Buffer
;
4846 file_info
->CreationTime
= cpu_to_le64(fp
->create_time
);
4847 time
= ksmbd_UnixTimeToNT(inode_get_atime(inode
));
4848 file_info
->LastAccessTime
= cpu_to_le64(time
);
4849 time
= ksmbd_UnixTimeToNT(inode_get_mtime(inode
));
4850 file_info
->LastWriteTime
= cpu_to_le64(time
);
4851 time
= ksmbd_UnixTimeToNT(inode_get_ctime(inode
));
4852 file_info
->ChangeTime
= cpu_to_le64(time
);
4853 file_info
->DosAttributes
= fp
->f_ci
->m_fattr
;
4854 file_info
->Inode
= cpu_to_le64(inode
->i_ino
);
4855 file_info
->EndOfFile
= cpu_to_le64(inode
->i_size
);
4856 file_info
->AllocationSize
= cpu_to_le64(inode
->i_blocks
<< 9);
4857 file_info
->HardLinks
= cpu_to_le32(inode
->i_nlink
);
4858 file_info
->Mode
= cpu_to_le32(inode
->i_mode
& 0777);
4859 file_info
->DeviceId
= cpu_to_le32(inode
->i_rdev
);
4862 * Sids(32) contain two sids(Domain sid(16), UNIX group sid(16)).
4863 * UNIX sid(16) = revision(1) + num_subauth(1) + authority(6) +
4864 * sub_auth(4 * 1(num_subauth)) + RID(4).
4866 id_to_sid(from_kuid_munged(&init_user_ns
, vfsuid_into_kuid(vfsuid
)),
4867 SIDUNIX_USER
, (struct smb_sid
*)&file_info
->Sids
[0]);
4868 id_to_sid(from_kgid_munged(&init_user_ns
, vfsgid_into_kgid(vfsgid
)),
4869 SIDUNIX_GROUP
, (struct smb_sid
*)&file_info
->Sids
[16]);
4871 rsp
->OutputBufferLength
= cpu_to_le32(out_buf_len
);
4874 static int smb2_get_info_file(struct ksmbd_work
*work
,
4875 struct smb2_query_info_req
*req
,
4876 struct smb2_query_info_rsp
*rsp
)
4878 struct ksmbd_file
*fp
;
4879 int fileinfoclass
= 0;
4881 unsigned int id
= KSMBD_NO_FID
, pid
= KSMBD_NO_FID
;
4883 if (test_share_config_flag(work
->tcon
->share_conf
,
4884 KSMBD_SHARE_FLAG_PIPE
)) {
4885 /* smb2 info file called for pipe */
4886 return smb2_get_info_file_pipe(work
->sess
, req
, rsp
,
4887 work
->response_buf
);
4890 if (work
->next_smb2_rcv_hdr_off
) {
4891 if (!has_file_id(req
->VolatileFileId
)) {
4892 ksmbd_debug(SMB
, "Compound request set FID = %llu\n",
4893 work
->compound_fid
);
4894 id
= work
->compound_fid
;
4895 pid
= work
->compound_pfid
;
4899 if (!has_file_id(id
)) {
4900 id
= req
->VolatileFileId
;
4901 pid
= req
->PersistentFileId
;
4904 fp
= ksmbd_lookup_fd_slow(work
, id
, pid
);
4908 fileinfoclass
= req
->FileInfoClass
;
4910 switch (fileinfoclass
) {
4911 case FILE_ACCESS_INFORMATION
:
4912 get_file_access_info(rsp
, fp
, work
->response_buf
);
4915 case FILE_BASIC_INFORMATION
:
4916 rc
= get_file_basic_info(rsp
, fp
, work
->response_buf
);
4919 case FILE_STANDARD_INFORMATION
:
4920 get_file_standard_info(rsp
, fp
, work
->response_buf
);
4923 case FILE_ALIGNMENT_INFORMATION
:
4924 get_file_alignment_info(rsp
, work
->response_buf
);
4927 case FILE_ALL_INFORMATION
:
4928 rc
= get_file_all_info(work
, rsp
, fp
, work
->response_buf
);
4931 case FILE_ALTERNATE_NAME_INFORMATION
:
4932 get_file_alternate_info(work
, rsp
, fp
, work
->response_buf
);
4935 case FILE_STREAM_INFORMATION
:
4936 get_file_stream_info(work
, rsp
, fp
, work
->response_buf
);
4939 case FILE_INTERNAL_INFORMATION
:
4940 get_file_internal_info(rsp
, fp
, work
->response_buf
);
4943 case FILE_NETWORK_OPEN_INFORMATION
:
4944 rc
= get_file_network_open_info(rsp
, fp
, work
->response_buf
);
4947 case FILE_EA_INFORMATION
:
4948 get_file_ea_info(rsp
, work
->response_buf
);
4951 case FILE_FULL_EA_INFORMATION
:
4952 rc
= smb2_get_ea(work
, fp
, req
, rsp
, work
->response_buf
);
4955 case FILE_POSITION_INFORMATION
:
4956 get_file_position_info(rsp
, fp
, work
->response_buf
);
4959 case FILE_MODE_INFORMATION
:
4960 get_file_mode_info(rsp
, fp
, work
->response_buf
);
4963 case FILE_COMPRESSION_INFORMATION
:
4964 get_file_compression_info(rsp
, fp
, work
->response_buf
);
4967 case FILE_ATTRIBUTE_TAG_INFORMATION
:
4968 rc
= get_file_attribute_tag_info(rsp
, fp
, work
->response_buf
);
4970 case SMB_FIND_FILE_POSIX_INFO
:
4971 if (!work
->tcon
->posix_extensions
) {
4972 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4975 find_file_posix_info(rsp
, fp
, work
->response_buf
);
4979 ksmbd_debug(SMB
, "fileinfoclass %d not supported yet\n",
4984 rc
= buffer_check_err(le32_to_cpu(req
->OutputBufferLength
),
4985 rsp
, work
->response_buf
);
4986 ksmbd_fd_put(work
, fp
);
4990 static int smb2_get_info_filesystem(struct ksmbd_work
*work
,
4991 struct smb2_query_info_req
*req
,
4992 struct smb2_query_info_rsp
*rsp
)
4994 struct ksmbd_session
*sess
= work
->sess
;
4995 struct ksmbd_conn
*conn
= work
->conn
;
4996 struct ksmbd_share_config
*share
= work
->tcon
->share_conf
;
4997 int fsinfoclass
= 0;
4998 struct kstatfs stfs
;
5005 rc
= kern_path(share
->path
, LOOKUP_NO_SYMLINKS
, &path
);
5007 pr_err("cannot create vfs path\n");
5011 rc
= vfs_statfs(&path
, &stfs
);
5013 pr_err("cannot do stat of path %s\n", share
->path
);
5018 fsinfoclass
= req
->FileInfoClass
;
5020 switch (fsinfoclass
) {
5021 case FS_DEVICE_INFORMATION
:
5023 struct filesystem_device_info
*info
;
5025 info
= (struct filesystem_device_info
*)rsp
->Buffer
;
5027 info
->DeviceType
= cpu_to_le32(stfs
.f_type
);
5028 info
->DeviceCharacteristics
= cpu_to_le32(0x00000020);
5029 rsp
->OutputBufferLength
= cpu_to_le32(8);
5032 case FS_ATTRIBUTE_INFORMATION
:
5034 struct filesystem_attribute_info
*info
;
5037 info
= (struct filesystem_attribute_info
*)rsp
->Buffer
;
5038 info
->Attributes
= cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS
|
5039 FILE_PERSISTENT_ACLS
|
5040 FILE_UNICODE_ON_DISK
|
5041 FILE_CASE_PRESERVED_NAMES
|
5042 FILE_CASE_SENSITIVE_SEARCH
|
5043 FILE_SUPPORTS_BLOCK_REFCOUNTING
);
5045 info
->Attributes
|= cpu_to_le32(server_conf
.share_fake_fscaps
);
5047 if (test_share_config_flag(work
->tcon
->share_conf
,
5048 KSMBD_SHARE_FLAG_STREAMS
))
5049 info
->Attributes
|= cpu_to_le32(FILE_NAMED_STREAMS
);
5051 info
->MaxPathNameComponentLength
= cpu_to_le32(stfs
.f_namelen
);
5052 len
= smbConvertToUTF16((__le16
*)info
->FileSystemName
,
5053 "NTFS", PATH_MAX
, conn
->local_nls
, 0);
5055 info
->FileSystemNameLen
= cpu_to_le32(len
);
5056 sz
= sizeof(struct filesystem_attribute_info
) - 2 + len
;
5057 rsp
->OutputBufferLength
= cpu_to_le32(sz
);
5060 case FS_VOLUME_INFORMATION
:
5062 struct filesystem_vol_info
*info
;
5064 unsigned int serial_crc
= 0;
5066 info
= (struct filesystem_vol_info
*)(rsp
->Buffer
);
5067 info
->VolumeCreationTime
= 0;
5068 serial_crc
= crc32_le(serial_crc
, share
->name
,
5069 strlen(share
->name
));
5070 serial_crc
= crc32_le(serial_crc
, share
->path
,
5071 strlen(share
->path
));
5072 serial_crc
= crc32_le(serial_crc
, ksmbd_netbios_name(),
5073 strlen(ksmbd_netbios_name()));
5074 /* Taking dummy value of serial number*/
5075 info
->SerialNumber
= cpu_to_le32(serial_crc
);
5076 len
= smbConvertToUTF16((__le16
*)info
->VolumeLabel
,
5077 share
->name
, PATH_MAX
,
5078 conn
->local_nls
, 0);
5080 info
->VolumeLabelSize
= cpu_to_le32(len
);
5082 sz
= sizeof(struct filesystem_vol_info
) - 2 + len
;
5083 rsp
->OutputBufferLength
= cpu_to_le32(sz
);
5086 case FS_SIZE_INFORMATION
:
5088 struct filesystem_info
*info
;
5090 info
= (struct filesystem_info
*)(rsp
->Buffer
);
5091 info
->TotalAllocationUnits
= cpu_to_le64(stfs
.f_blocks
);
5092 info
->FreeAllocationUnits
= cpu_to_le64(stfs
.f_bfree
);
5093 info
->SectorsPerAllocationUnit
= cpu_to_le32(1);
5094 info
->BytesPerSector
= cpu_to_le32(stfs
.f_bsize
);
5095 rsp
->OutputBufferLength
= cpu_to_le32(24);
5098 case FS_FULL_SIZE_INFORMATION
:
5100 struct smb2_fs_full_size_info
*info
;
5102 info
= (struct smb2_fs_full_size_info
*)(rsp
->Buffer
);
5103 info
->TotalAllocationUnits
= cpu_to_le64(stfs
.f_blocks
);
5104 info
->CallerAvailableAllocationUnits
=
5105 cpu_to_le64(stfs
.f_bavail
);
5106 info
->ActualAvailableAllocationUnits
=
5107 cpu_to_le64(stfs
.f_bfree
);
5108 info
->SectorsPerAllocationUnit
= cpu_to_le32(1);
5109 info
->BytesPerSector
= cpu_to_le32(stfs
.f_bsize
);
5110 rsp
->OutputBufferLength
= cpu_to_le32(32);
5113 case FS_OBJECT_ID_INFORMATION
:
5115 struct object_id_info
*info
;
5117 info
= (struct object_id_info
*)(rsp
->Buffer
);
5119 if (!user_guest(sess
->user
))
5120 memcpy(info
->objid
, user_passkey(sess
->user
), 16);
5122 memset(info
->objid
, 0, 16);
5124 info
->extended_info
.magic
= cpu_to_le32(EXTENDED_INFO_MAGIC
);
5125 info
->extended_info
.version
= cpu_to_le32(1);
5126 info
->extended_info
.release
= cpu_to_le32(1);
5127 info
->extended_info
.rel_date
= 0;
5128 memcpy(info
->extended_info
.version_string
, "1.1.0", strlen("1.1.0"));
5129 rsp
->OutputBufferLength
= cpu_to_le32(64);
5132 case FS_SECTOR_SIZE_INFORMATION
:
5134 struct smb3_fs_ss_info
*info
;
5135 unsigned int sector_size
=
5136 min_t(unsigned int, path
.mnt
->mnt_sb
->s_blocksize
, 4096);
5138 info
= (struct smb3_fs_ss_info
*)(rsp
->Buffer
);
5140 info
->LogicalBytesPerSector
= cpu_to_le32(sector_size
);
5141 info
->PhysicalBytesPerSectorForAtomicity
=
5142 cpu_to_le32(sector_size
);
5143 info
->PhysicalBytesPerSectorForPerf
= cpu_to_le32(sector_size
);
5144 info
->FSEffPhysicalBytesPerSectorForAtomicity
=
5145 cpu_to_le32(sector_size
);
5146 info
->Flags
= cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE
|
5147 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE
);
5148 info
->ByteOffsetForSectorAlignment
= 0;
5149 info
->ByteOffsetForPartitionAlignment
= 0;
5150 rsp
->OutputBufferLength
= cpu_to_le32(28);
5153 case FS_CONTROL_INFORMATION
:
5156 * TODO : The current implementation is based on
5157 * test result with win7(NTFS) server. It's need to
5158 * modify this to get valid Quota values
5161 struct smb2_fs_control_info
*info
;
5163 info
= (struct smb2_fs_control_info
*)(rsp
->Buffer
);
5164 info
->FreeSpaceStartFiltering
= 0;
5165 info
->FreeSpaceThreshold
= 0;
5166 info
->FreeSpaceStopFiltering
= 0;
5167 info
->DefaultQuotaThreshold
= cpu_to_le64(SMB2_NO_FID
);
5168 info
->DefaultQuotaLimit
= cpu_to_le64(SMB2_NO_FID
);
5170 rsp
->OutputBufferLength
= cpu_to_le32(48);
5173 case FS_POSIX_INFORMATION
:
5175 struct filesystem_posix_info
*info
;
5177 if (!work
->tcon
->posix_extensions
) {
5178 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
5181 info
= (struct filesystem_posix_info
*)(rsp
->Buffer
);
5182 info
->OptimalTransferSize
= cpu_to_le32(stfs
.f_bsize
);
5183 info
->BlockSize
= cpu_to_le32(stfs
.f_bsize
);
5184 info
->TotalBlocks
= cpu_to_le64(stfs
.f_blocks
);
5185 info
->BlocksAvail
= cpu_to_le64(stfs
.f_bfree
);
5186 info
->UserBlocksAvail
= cpu_to_le64(stfs
.f_bavail
);
5187 info
->TotalFileNodes
= cpu_to_le64(stfs
.f_files
);
5188 info
->FreeFileNodes
= cpu_to_le64(stfs
.f_ffree
);
5189 rsp
->OutputBufferLength
= cpu_to_le32(56);
5197 rc
= buffer_check_err(le32_to_cpu(req
->OutputBufferLength
),
5198 rsp
, work
->response_buf
);
5203 static int smb2_get_info_sec(struct ksmbd_work
*work
,
5204 struct smb2_query_info_req
*req
,
5205 struct smb2_query_info_rsp
*rsp
)
5207 struct ksmbd_file
*fp
;
5208 struct mnt_idmap
*idmap
;
5209 struct smb_ntsd
*pntsd
= (struct smb_ntsd
*)rsp
->Buffer
, *ppntsd
= NULL
;
5210 struct smb_fattr fattr
= {{0}};
5211 struct inode
*inode
;
5212 __u32 secdesclen
= 0;
5213 unsigned int id
= KSMBD_NO_FID
, pid
= KSMBD_NO_FID
;
5214 int addition_info
= le32_to_cpu(req
->AdditionalInformation
);
5215 int rc
= 0, ppntsd_size
= 0;
5217 if (addition_info
& ~(OWNER_SECINFO
| GROUP_SECINFO
| DACL_SECINFO
|
5218 PROTECTED_DACL_SECINFO
|
5219 UNPROTECTED_DACL_SECINFO
)) {
5220 ksmbd_debug(SMB
, "Unsupported addition info: 0x%x)\n",
5223 pntsd
->revision
= cpu_to_le16(1);
5224 pntsd
->type
= cpu_to_le16(SELF_RELATIVE
| DACL_PROTECTED
);
5225 pntsd
->osidoffset
= 0;
5226 pntsd
->gsidoffset
= 0;
5227 pntsd
->sacloffset
= 0;
5228 pntsd
->dacloffset
= 0;
5230 secdesclen
= sizeof(struct smb_ntsd
);
5231 rsp
->OutputBufferLength
= cpu_to_le32(secdesclen
);
5236 if (work
->next_smb2_rcv_hdr_off
) {
5237 if (!has_file_id(req
->VolatileFileId
)) {
5238 ksmbd_debug(SMB
, "Compound request set FID = %llu\n",
5239 work
->compound_fid
);
5240 id
= work
->compound_fid
;
5241 pid
= work
->compound_pfid
;
5245 if (!has_file_id(id
)) {
5246 id
= req
->VolatileFileId
;
5247 pid
= req
->PersistentFileId
;
5250 fp
= ksmbd_lookup_fd_slow(work
, id
, pid
);
5254 idmap
= file_mnt_idmap(fp
->filp
);
5255 inode
= file_inode(fp
->filp
);
5256 ksmbd_acls_fattr(&fattr
, idmap
, inode
);
5258 if (test_share_config_flag(work
->tcon
->share_conf
,
5259 KSMBD_SHARE_FLAG_ACL_XATTR
))
5260 ppntsd_size
= ksmbd_vfs_get_sd_xattr(work
->conn
, idmap
,
5261 fp
->filp
->f_path
.dentry
,
5264 /* Check if sd buffer size exceeds response buffer size */
5265 if (smb2_resp_buf_len(work
, 8) > ppntsd_size
)
5266 rc
= build_sec_desc(idmap
, pntsd
, ppntsd
, ppntsd_size
,
5267 addition_info
, &secdesclen
, &fattr
);
5268 posix_acl_release(fattr
.cf_acls
);
5269 posix_acl_release(fattr
.cf_dacls
);
5271 ksmbd_fd_put(work
, fp
);
5275 rsp
->OutputBufferLength
= cpu_to_le32(secdesclen
);
5280 * smb2_query_info() - handler for smb2 query info command
5281 * @work: smb work containing query info request buffer
5283 * Return: 0 on success, otherwise error
5285 int smb2_query_info(struct ksmbd_work
*work
)
5287 struct smb2_query_info_req
*req
;
5288 struct smb2_query_info_rsp
*rsp
;
5291 WORK_BUFFERS(work
, req
, rsp
);
5293 ksmbd_debug(SMB
, "GOT query info request\n");
5295 switch (req
->InfoType
) {
5296 case SMB2_O_INFO_FILE
:
5297 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_FILE\n");
5298 rc
= smb2_get_info_file(work
, req
, rsp
);
5300 case SMB2_O_INFO_FILESYSTEM
:
5301 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_FILESYSTEM\n");
5302 rc
= smb2_get_info_filesystem(work
, req
, rsp
);
5304 case SMB2_O_INFO_SECURITY
:
5305 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_SECURITY\n");
5306 rc
= smb2_get_info_sec(work
, req
, rsp
);
5309 ksmbd_debug(SMB
, "InfoType %d not supported yet\n",
5315 rsp
->StructureSize
= cpu_to_le16(9);
5316 rsp
->OutputBufferOffset
= cpu_to_le16(72);
5317 rc
= ksmbd_iov_pin_rsp(work
, (void *)rsp
,
5318 offsetof(struct smb2_query_info_rsp
, Buffer
) +
5319 le32_to_cpu(rsp
->OutputBufferLength
));
5324 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
5325 else if (rc
== -ENOENT
)
5326 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
5327 else if (rc
== -EIO
)
5328 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
5329 else if (rc
== -ENOMEM
)
5330 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
5331 else if (rc
== -EOPNOTSUPP
|| rsp
->hdr
.Status
== 0)
5332 rsp
->hdr
.Status
= STATUS_INVALID_INFO_CLASS
;
5333 smb2_set_err_rsp(work
);
5335 ksmbd_debug(SMB
, "error while processing smb2 query rc = %d\n",
5343 * smb2_close_pipe() - handler for closing IPC pipe
5344 * @work: smb work containing close request buffer
5348 static noinline
int smb2_close_pipe(struct ksmbd_work
*work
)
5351 struct smb2_close_req
*req
;
5352 struct smb2_close_rsp
*rsp
;
5354 WORK_BUFFERS(work
, req
, rsp
);
5356 id
= req
->VolatileFileId
;
5357 ksmbd_session_rpc_close(work
->sess
, id
);
5359 rsp
->StructureSize
= cpu_to_le16(60);
5362 rsp
->CreationTime
= 0;
5363 rsp
->LastAccessTime
= 0;
5364 rsp
->LastWriteTime
= 0;
5365 rsp
->ChangeTime
= 0;
5366 rsp
->AllocationSize
= 0;
5368 rsp
->Attributes
= 0;
5370 return ksmbd_iov_pin_rsp(work
, (void *)rsp
,
5371 sizeof(struct smb2_close_rsp
));
5375 * smb2_close() - handler for smb2 close file command
5376 * @work: smb work containing close request buffer
5380 int smb2_close(struct ksmbd_work
*work
)
5382 u64 volatile_id
= KSMBD_NO_FID
;
5384 struct smb2_close_req
*req
;
5385 struct smb2_close_rsp
*rsp
;
5386 struct ksmbd_conn
*conn
= work
->conn
;
5387 struct ksmbd_file
*fp
;
5388 struct inode
*inode
;
5392 WORK_BUFFERS(work
, req
, rsp
);
5394 if (test_share_config_flag(work
->tcon
->share_conf
,
5395 KSMBD_SHARE_FLAG_PIPE
)) {
5396 ksmbd_debug(SMB
, "IPC pipe close request\n");
5397 return smb2_close_pipe(work
);
5400 sess_id
= le64_to_cpu(req
->hdr
.SessionId
);
5401 if (req
->hdr
.Flags
& SMB2_FLAGS_RELATED_OPERATIONS
)
5402 sess_id
= work
->compound_sid
;
5404 work
->compound_sid
= 0;
5405 if (check_session_id(conn
, sess_id
)) {
5406 work
->compound_sid
= sess_id
;
5408 rsp
->hdr
.Status
= STATUS_USER_SESSION_DELETED
;
5409 if (req
->hdr
.Flags
& SMB2_FLAGS_RELATED_OPERATIONS
)
5410 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
5415 if (work
->next_smb2_rcv_hdr_off
&&
5416 !has_file_id(req
->VolatileFileId
)) {
5417 if (!has_file_id(work
->compound_fid
)) {
5418 /* file already closed, return FILE_CLOSED */
5419 ksmbd_debug(SMB
, "file already closed\n");
5420 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
5425 "Compound request set FID = %llu:%llu\n",
5427 work
->compound_pfid
);
5428 volatile_id
= work
->compound_fid
;
5430 /* file closed, stored id is not valid anymore */
5431 work
->compound_fid
= KSMBD_NO_FID
;
5432 work
->compound_pfid
= KSMBD_NO_FID
;
5435 volatile_id
= req
->VolatileFileId
;
5437 ksmbd_debug(SMB
, "volatile_id = %llu\n", volatile_id
);
5439 rsp
->StructureSize
= cpu_to_le16(60);
5442 if (req
->Flags
== SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB
) {
5443 fp
= ksmbd_lookup_fd_fast(work
, volatile_id
);
5449 inode
= file_inode(fp
->filp
);
5450 rsp
->Flags
= SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB
;
5451 rsp
->AllocationSize
= S_ISDIR(inode
->i_mode
) ? 0 :
5452 cpu_to_le64(inode
->i_blocks
<< 9);
5453 rsp
->EndOfFile
= cpu_to_le64(inode
->i_size
);
5454 rsp
->Attributes
= fp
->f_ci
->m_fattr
;
5455 rsp
->CreationTime
= cpu_to_le64(fp
->create_time
);
5456 time
= ksmbd_UnixTimeToNT(inode_get_atime(inode
));
5457 rsp
->LastAccessTime
= cpu_to_le64(time
);
5458 time
= ksmbd_UnixTimeToNT(inode_get_mtime(inode
));
5459 rsp
->LastWriteTime
= cpu_to_le64(time
);
5460 time
= ksmbd_UnixTimeToNT(inode_get_ctime(inode
));
5461 rsp
->ChangeTime
= cpu_to_le64(time
);
5462 ksmbd_fd_put(work
, fp
);
5465 rsp
->AllocationSize
= 0;
5467 rsp
->Attributes
= 0;
5468 rsp
->CreationTime
= 0;
5469 rsp
->LastAccessTime
= 0;
5470 rsp
->LastWriteTime
= 0;
5471 rsp
->ChangeTime
= 0;
5474 err
= ksmbd_close_fd(work
, volatile_id
);
5477 err
= ksmbd_iov_pin_rsp(work
, (void *)rsp
,
5478 sizeof(struct smb2_close_rsp
));
5481 if (rsp
->hdr
.Status
== 0)
5482 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
5483 smb2_set_err_rsp(work
);
5490 * smb2_echo() - handler for smb2 echo(ping) command
5491 * @work: smb work containing echo request buffer
5495 int smb2_echo(struct ksmbd_work
*work
)
5497 struct smb2_echo_rsp
*rsp
= smb2_get_msg(work
->response_buf
);
5499 if (work
->next_smb2_rcv_hdr_off
)
5500 rsp
= ksmbd_resp_buf_next(work
);
5502 rsp
->StructureSize
= cpu_to_le16(4);
5504 return ksmbd_iov_pin_rsp(work
, rsp
, sizeof(struct smb2_echo_rsp
));
5507 static int smb2_rename(struct ksmbd_work
*work
,
5508 struct ksmbd_file
*fp
,
5509 struct smb2_file_rename_info
*file_info
,
5510 struct nls_table
*local_nls
)
5512 struct ksmbd_share_config
*share
= fp
->tcon
->share_conf
;
5513 char *new_name
= NULL
;
5516 ksmbd_debug(SMB
, "setting FILE_RENAME_INFO\n");
5517 new_name
= smb2_get_name(file_info
->FileName
,
5518 le32_to_cpu(file_info
->FileNameLength
),
5520 if (IS_ERR(new_name
))
5521 return PTR_ERR(new_name
);
5523 if (strchr(new_name
, ':')) {
5525 char *xattr_stream_name
, *stream_name
= NULL
;
5526 size_t xattr_stream_size
;
5529 rc
= parse_stream_name(new_name
, &stream_name
, &s_type
);
5533 len
= strlen(new_name
);
5534 if (len
> 0 && new_name
[len
- 1] != '/') {
5535 pr_err("not allow base filename in rename\n");
5540 rc
= ksmbd_vfs_xattr_stream_name(stream_name
,
5547 rc
= ksmbd_vfs_setxattr(file_mnt_idmap(fp
->filp
),
5552 pr_err("failed to store stream name in xattr: %d\n",
5561 ksmbd_debug(SMB
, "new name %s\n", new_name
);
5562 if (ksmbd_share_veto_filename(share
, new_name
)) {
5564 ksmbd_debug(SMB
, "Can't rename vetoed file: %s\n", new_name
);
5568 if (!file_info
->ReplaceIfExists
)
5569 flags
= RENAME_NOREPLACE
;
5571 rc
= ksmbd_vfs_rename(work
, &fp
->filp
->f_path
, new_name
, flags
);
5577 static int smb2_create_link(struct ksmbd_work
*work
,
5578 struct ksmbd_share_config
*share
,
5579 struct smb2_file_link_info
*file_info
,
5580 unsigned int buf_len
, struct file
*filp
,
5581 struct nls_table
*local_nls
)
5583 char *link_name
= NULL
, *target_name
= NULL
, *pathname
= NULL
;
5584 struct path path
, parent_path
;
5585 bool file_present
= false;
5588 if (buf_len
< (u64
)sizeof(struct smb2_file_link_info
) +
5589 le32_to_cpu(file_info
->FileNameLength
))
5592 ksmbd_debug(SMB
, "setting FILE_LINK_INFORMATION\n");
5593 pathname
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5597 link_name
= smb2_get_name(file_info
->FileName
,
5598 le32_to_cpu(file_info
->FileNameLength
),
5600 if (IS_ERR(link_name
) || S_ISDIR(file_inode(filp
)->i_mode
)) {
5605 ksmbd_debug(SMB
, "link name is %s\n", link_name
);
5606 target_name
= file_path(filp
, pathname
, PATH_MAX
);
5607 if (IS_ERR(target_name
)) {
5612 ksmbd_debug(SMB
, "target name is %s\n", target_name
);
5613 rc
= ksmbd_vfs_kern_path_locked(work
, link_name
, LOOKUP_NO_SYMLINKS
,
5614 &parent_path
, &path
, 0);
5619 file_present
= true;
5621 if (file_info
->ReplaceIfExists
) {
5623 rc
= ksmbd_vfs_remove_file(work
, &path
);
5626 ksmbd_debug(SMB
, "cannot delete %s\n",
5634 ksmbd_debug(SMB
, "link already exists\n");
5639 rc
= ksmbd_vfs_link(work
, target_name
, link_name
);
5644 ksmbd_vfs_kern_path_unlock(&parent_path
, &path
);
5646 if (!IS_ERR(link_name
))
5652 static int set_file_basic_info(struct ksmbd_file
*fp
,
5653 struct smb2_file_basic_info
*file_info
,
5654 struct ksmbd_share_config
*share
)
5658 struct inode
*inode
;
5659 struct mnt_idmap
*idmap
;
5662 if (!(fp
->daccess
& FILE_WRITE_ATTRIBUTES_LE
))
5667 inode
= file_inode(filp
);
5668 idmap
= file_mnt_idmap(filp
);
5670 if (file_info
->CreationTime
)
5671 fp
->create_time
= le64_to_cpu(file_info
->CreationTime
);
5673 if (file_info
->LastAccessTime
) {
5674 attrs
.ia_atime
= ksmbd_NTtimeToUnix(file_info
->LastAccessTime
);
5675 attrs
.ia_valid
|= (ATTR_ATIME
| ATTR_ATIME_SET
);
5678 attrs
.ia_valid
|= ATTR_CTIME
;
5679 if (file_info
->ChangeTime
)
5680 attrs
.ia_ctime
= ksmbd_NTtimeToUnix(file_info
->ChangeTime
);
5682 attrs
.ia_ctime
= inode_get_ctime(inode
);
5684 if (file_info
->LastWriteTime
) {
5685 attrs
.ia_mtime
= ksmbd_NTtimeToUnix(file_info
->LastWriteTime
);
5686 attrs
.ia_valid
|= (ATTR_MTIME
| ATTR_MTIME_SET
);
5689 if (file_info
->Attributes
) {
5690 if (!S_ISDIR(inode
->i_mode
) &&
5691 file_info
->Attributes
& FILE_ATTRIBUTE_DIRECTORY_LE
) {
5692 pr_err("can't change a file to a directory\n");
5696 if (!(S_ISDIR(inode
->i_mode
) && file_info
->Attributes
== FILE_ATTRIBUTE_NORMAL_LE
))
5697 fp
->f_ci
->m_fattr
= file_info
->Attributes
|
5698 (fp
->f_ci
->m_fattr
& FILE_ATTRIBUTE_DIRECTORY_LE
);
5701 if (test_share_config_flag(share
, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS
) &&
5702 (file_info
->CreationTime
|| file_info
->Attributes
)) {
5703 struct xattr_dos_attrib da
= {0};
5706 da
.itime
= fp
->itime
;
5707 da
.create_time
= fp
->create_time
;
5708 da
.attr
= le32_to_cpu(fp
->f_ci
->m_fattr
);
5709 da
.flags
= XATTR_DOSINFO_ATTRIB
| XATTR_DOSINFO_CREATE_TIME
|
5710 XATTR_DOSINFO_ITIME
;
5712 rc
= ksmbd_vfs_set_dos_attrib_xattr(idmap
, &filp
->f_path
, &da
,
5716 "failed to restore file attribute in EA\n");
5720 if (attrs
.ia_valid
) {
5721 struct dentry
*dentry
= filp
->f_path
.dentry
;
5722 struct inode
*inode
= d_inode(dentry
);
5724 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
5728 inode_set_ctime_to_ts(inode
, attrs
.ia_ctime
);
5729 attrs
.ia_valid
&= ~ATTR_CTIME
;
5730 rc
= notify_change(idmap
, dentry
, &attrs
, NULL
);
5731 inode_unlock(inode
);
5736 static int set_file_allocation_info(struct ksmbd_work
*work
,
5737 struct ksmbd_file
*fp
,
5738 struct smb2_file_alloc_info
*file_alloc_info
)
5741 * TODO : It's working fine only when store dos attributes
5742 * is not yes. need to implement a logic which works
5743 * properly with any smb.conf option
5747 struct inode
*inode
;
5750 if (!(fp
->daccess
& FILE_WRITE_DATA_LE
))
5753 alloc_blks
= (le64_to_cpu(file_alloc_info
->AllocationSize
) + 511) >> 9;
5754 inode
= file_inode(fp
->filp
);
5756 if (alloc_blks
> inode
->i_blocks
) {
5757 smb_break_all_levII_oplock(work
, fp
, 1);
5758 rc
= vfs_fallocate(fp
->filp
, FALLOC_FL_KEEP_SIZE
, 0,
5760 if (rc
&& rc
!= -EOPNOTSUPP
) {
5761 pr_err("vfs_fallocate is failed : %d\n", rc
);
5764 } else if (alloc_blks
< inode
->i_blocks
) {
5768 * Allocation size could be smaller than original one
5769 * which means allocated blocks in file should be
5770 * deallocated. use truncate to cut out it, but inode
5771 * size is also updated with truncate offset.
5772 * inode size is retained by backup inode size.
5774 size
= i_size_read(inode
);
5775 rc
= ksmbd_vfs_truncate(work
, fp
, alloc_blks
* 512);
5777 pr_err("truncate failed!, err %d\n", rc
);
5780 if (size
< alloc_blks
* 512)
5781 i_size_write(inode
, size
);
5786 static int set_end_of_file_info(struct ksmbd_work
*work
, struct ksmbd_file
*fp
,
5787 struct smb2_file_eof_info
*file_eof_info
)
5790 struct inode
*inode
;
5793 if (!(fp
->daccess
& FILE_WRITE_DATA_LE
))
5796 newsize
= le64_to_cpu(file_eof_info
->EndOfFile
);
5797 inode
= file_inode(fp
->filp
);
5800 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5801 * on FAT32 shared device, truncate execution time is too long
5802 * and network error could cause from windows client. because
5803 * truncate of some filesystem like FAT32 fill zero data in
5806 if (inode
->i_sb
->s_magic
!= MSDOS_SUPER_MAGIC
) {
5807 ksmbd_debug(SMB
, "truncated to newsize %lld\n", newsize
);
5808 rc
= ksmbd_vfs_truncate(work
, fp
, newsize
);
5810 ksmbd_debug(SMB
, "truncate failed!, err %d\n", rc
);
5819 static int set_rename_info(struct ksmbd_work
*work
, struct ksmbd_file
*fp
,
5820 struct smb2_file_rename_info
*rename_info
,
5821 unsigned int buf_len
)
5823 if (!(fp
->daccess
& FILE_DELETE_LE
)) {
5824 pr_err("no right to delete : 0x%x\n", fp
->daccess
);
5828 if (buf_len
< (u64
)sizeof(struct smb2_file_rename_info
) +
5829 le32_to_cpu(rename_info
->FileNameLength
))
5832 if (!le32_to_cpu(rename_info
->FileNameLength
))
5835 return smb2_rename(work
, fp
, rename_info
, work
->conn
->local_nls
);
5838 static int set_file_disposition_info(struct ksmbd_file
*fp
,
5839 struct smb2_file_disposition_info
*file_info
)
5841 struct inode
*inode
;
5843 if (!(fp
->daccess
& FILE_DELETE_LE
)) {
5844 pr_err("no right to delete : 0x%x\n", fp
->daccess
);
5848 inode
= file_inode(fp
->filp
);
5849 if (file_info
->DeletePending
) {
5850 if (S_ISDIR(inode
->i_mode
) &&
5851 ksmbd_vfs_empty_dir(fp
) == -ENOTEMPTY
)
5853 ksmbd_set_inode_pending_delete(fp
);
5855 ksmbd_clear_inode_pending_delete(fp
);
5860 static int set_file_position_info(struct ksmbd_file
*fp
,
5861 struct smb2_file_pos_info
*file_info
)
5863 loff_t current_byte_offset
;
5864 unsigned long sector_size
;
5865 struct inode
*inode
;
5867 inode
= file_inode(fp
->filp
);
5868 current_byte_offset
= le64_to_cpu(file_info
->CurrentByteOffset
);
5869 sector_size
= inode
->i_sb
->s_blocksize
;
5871 if (current_byte_offset
< 0 ||
5872 (fp
->coption
== FILE_NO_INTERMEDIATE_BUFFERING_LE
&&
5873 current_byte_offset
& (sector_size
- 1))) {
5874 pr_err("CurrentByteOffset is not valid : %llu\n",
5875 current_byte_offset
);
5879 fp
->filp
->f_pos
= current_byte_offset
;
5883 static int set_file_mode_info(struct ksmbd_file
*fp
,
5884 struct smb2_file_mode_info
*file_info
)
5888 mode
= file_info
->Mode
;
5890 if ((mode
& ~FILE_MODE_INFO_MASK
)) {
5891 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode
));
5896 * TODO : need to implement consideration for
5897 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5899 ksmbd_vfs_set_fadvise(fp
->filp
, mode
);
5905 * smb2_set_info_file() - handler for smb2 set info command
5906 * @work: smb work containing set info command buffer
5907 * @fp: ksmbd_file pointer
5908 * @req: request buffer pointer
5909 * @share: ksmbd_share_config pointer
5911 * Return: 0 on success, otherwise error
5912 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5914 static int smb2_set_info_file(struct ksmbd_work
*work
, struct ksmbd_file
*fp
,
5915 struct smb2_set_info_req
*req
,
5916 struct ksmbd_share_config
*share
)
5918 unsigned int buf_len
= le32_to_cpu(req
->BufferLength
);
5920 switch (req
->FileInfoClass
) {
5921 case FILE_BASIC_INFORMATION
:
5923 if (buf_len
< sizeof(struct smb2_file_basic_info
))
5926 return set_file_basic_info(fp
, (struct smb2_file_basic_info
*)req
->Buffer
, share
);
5928 case FILE_ALLOCATION_INFORMATION
:
5930 if (buf_len
< sizeof(struct smb2_file_alloc_info
))
5933 return set_file_allocation_info(work
, fp
,
5934 (struct smb2_file_alloc_info
*)req
->Buffer
);
5936 case FILE_END_OF_FILE_INFORMATION
:
5938 if (buf_len
< sizeof(struct smb2_file_eof_info
))
5941 return set_end_of_file_info(work
, fp
,
5942 (struct smb2_file_eof_info
*)req
->Buffer
);
5944 case FILE_RENAME_INFORMATION
:
5946 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
5948 "User does not have write permission\n");
5952 if (buf_len
< sizeof(struct smb2_file_rename_info
))
5955 return set_rename_info(work
, fp
,
5956 (struct smb2_file_rename_info
*)req
->Buffer
,
5959 case FILE_LINK_INFORMATION
:
5961 if (buf_len
< sizeof(struct smb2_file_link_info
))
5964 return smb2_create_link(work
, work
->tcon
->share_conf
,
5965 (struct smb2_file_link_info
*)req
->Buffer
,
5967 work
->conn
->local_nls
);
5969 case FILE_DISPOSITION_INFORMATION
:
5971 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
5973 "User does not have write permission\n");
5977 if (buf_len
< sizeof(struct smb2_file_disposition_info
))
5980 return set_file_disposition_info(fp
,
5981 (struct smb2_file_disposition_info
*)req
->Buffer
);
5983 case FILE_FULL_EA_INFORMATION
:
5985 if (!(fp
->daccess
& FILE_WRITE_EA_LE
)) {
5986 pr_err("Not permitted to write ext attr: 0x%x\n",
5991 if (buf_len
< sizeof(struct smb2_ea_info
))
5994 return smb2_set_ea((struct smb2_ea_info
*)req
->Buffer
,
5995 buf_len
, &fp
->filp
->f_path
);
5997 case FILE_POSITION_INFORMATION
:
5999 if (buf_len
< sizeof(struct smb2_file_pos_info
))
6002 return set_file_position_info(fp
, (struct smb2_file_pos_info
*)req
->Buffer
);
6004 case FILE_MODE_INFORMATION
:
6006 if (buf_len
< sizeof(struct smb2_file_mode_info
))
6009 return set_file_mode_info(fp
, (struct smb2_file_mode_info
*)req
->Buffer
);
6013 pr_err("Unimplemented Fileinfoclass :%d\n", req
->FileInfoClass
);
6017 static int smb2_set_info_sec(struct ksmbd_file
*fp
, int addition_info
,
6018 char *buffer
, int buf_len
)
6020 struct smb_ntsd
*pntsd
= (struct smb_ntsd
*)buffer
;
6022 fp
->saccess
|= FILE_SHARE_DELETE_LE
;
6024 return set_info_sec(fp
->conn
, fp
->tcon
, &fp
->filp
->f_path
, pntsd
,
6025 buf_len
, false, true);
6029 * smb2_set_info() - handler for smb2 set info command handler
6030 * @work: smb work containing set info request buffer
6032 * Return: 0 on success, otherwise error
6034 int smb2_set_info(struct ksmbd_work
*work
)
6036 struct smb2_set_info_req
*req
;
6037 struct smb2_set_info_rsp
*rsp
;
6038 struct ksmbd_file
*fp
;
6040 unsigned int id
= KSMBD_NO_FID
, pid
= KSMBD_NO_FID
;
6042 ksmbd_debug(SMB
, "Received set info request\n");
6044 if (work
->next_smb2_rcv_hdr_off
) {
6045 req
= ksmbd_req_buf_next(work
);
6046 rsp
= ksmbd_resp_buf_next(work
);
6047 if (!has_file_id(req
->VolatileFileId
)) {
6048 ksmbd_debug(SMB
, "Compound request set FID = %llu\n",
6049 work
->compound_fid
);
6050 id
= work
->compound_fid
;
6051 pid
= work
->compound_pfid
;
6054 req
= smb2_get_msg(work
->request_buf
);
6055 rsp
= smb2_get_msg(work
->response_buf
);
6058 if (!has_file_id(id
)) {
6059 id
= req
->VolatileFileId
;
6060 pid
= req
->PersistentFileId
;
6063 fp
= ksmbd_lookup_fd_slow(work
, id
, pid
);
6065 ksmbd_debug(SMB
, "Invalid id for close: %u\n", id
);
6070 switch (req
->InfoType
) {
6071 case SMB2_O_INFO_FILE
:
6072 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_FILE\n");
6073 rc
= smb2_set_info_file(work
, fp
, req
, work
->tcon
->share_conf
);
6075 case SMB2_O_INFO_SECURITY
:
6076 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_SECURITY\n");
6077 if (ksmbd_override_fsids(work
)) {
6081 rc
= smb2_set_info_sec(fp
,
6082 le32_to_cpu(req
->AdditionalInformation
),
6084 le32_to_cpu(req
->BufferLength
));
6085 ksmbd_revert_fsids(work
);
6094 rsp
->StructureSize
= cpu_to_le16(2);
6095 rc
= ksmbd_iov_pin_rsp(work
, (void *)rsp
,
6096 sizeof(struct smb2_set_info_rsp
));
6099 ksmbd_fd_put(work
, fp
);
6103 if (rc
== -EACCES
|| rc
== -EPERM
|| rc
== -EXDEV
)
6104 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
6105 else if (rc
== -EINVAL
)
6106 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
6107 else if (rc
== -ESHARE
)
6108 rsp
->hdr
.Status
= STATUS_SHARING_VIOLATION
;
6109 else if (rc
== -ENOENT
)
6110 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_INVALID
;
6111 else if (rc
== -EBUSY
|| rc
== -ENOTEMPTY
)
6112 rsp
->hdr
.Status
= STATUS_DIRECTORY_NOT_EMPTY
;
6113 else if (rc
== -EAGAIN
)
6114 rsp
->hdr
.Status
= STATUS_FILE_LOCK_CONFLICT
;
6115 else if (rc
== -EBADF
|| rc
== -ESTALE
)
6116 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6117 else if (rc
== -EEXIST
)
6118 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_COLLISION
;
6119 else if (rsp
->hdr
.Status
== 0 || rc
== -EOPNOTSUPP
)
6120 rsp
->hdr
.Status
= STATUS_INVALID_INFO_CLASS
;
6121 smb2_set_err_rsp(work
);
6122 ksmbd_fd_put(work
, fp
);
6123 ksmbd_debug(SMB
, "error while processing smb2 query rc = %d\n", rc
);
6128 * smb2_read_pipe() - handler for smb2 read from IPC pipe
6129 * @work: smb work containing read IPC pipe command buffer
6131 * Return: 0 on success, otherwise error
6133 static noinline
int smb2_read_pipe(struct ksmbd_work
*work
)
6135 int nbytes
= 0, err
;
6137 struct ksmbd_rpc_command
*rpc_resp
;
6138 struct smb2_read_req
*req
;
6139 struct smb2_read_rsp
*rsp
;
6141 WORK_BUFFERS(work
, req
, rsp
);
6143 id
= req
->VolatileFileId
;
6145 rpc_resp
= ksmbd_rpc_read(work
->sess
, id
);
6147 void *aux_payload_buf
;
6149 if (rpc_resp
->flags
!= KSMBD_RPC_OK
) {
6155 kvmalloc(rpc_resp
->payload_sz
, GFP_KERNEL
);
6156 if (!aux_payload_buf
) {
6161 memcpy(aux_payload_buf
, rpc_resp
->payload
, rpc_resp
->payload_sz
);
6163 nbytes
= rpc_resp
->payload_sz
;
6164 err
= ksmbd_iov_pin_rsp_read(work
, (void *)rsp
,
6165 offsetof(struct smb2_read_rsp
, Buffer
),
6166 aux_payload_buf
, nbytes
);
6171 err
= ksmbd_iov_pin_rsp(work
, (void *)rsp
,
6172 offsetof(struct smb2_read_rsp
, Buffer
));
6177 rsp
->StructureSize
= cpu_to_le16(17);
6178 rsp
->DataOffset
= 80;
6180 rsp
->DataLength
= cpu_to_le32(nbytes
);
6181 rsp
->DataRemaining
= 0;
6186 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
6187 smb2_set_err_rsp(work
);
6192 static int smb2_set_remote_key_for_rdma(struct ksmbd_work
*work
,
6193 struct smb2_buffer_desc_v1
*desc
,
6195 __le16 ChannelInfoLength
)
6197 unsigned int i
, ch_count
;
6199 if (work
->conn
->dialect
== SMB30_PROT_ID
&&
6200 Channel
!= SMB2_CHANNEL_RDMA_V1
)
6203 ch_count
= le16_to_cpu(ChannelInfoLength
) / sizeof(*desc
);
6204 if (ksmbd_debug_types
& KSMBD_DEBUG_RDMA
) {
6205 for (i
= 0; i
< ch_count
; i
++) {
6206 pr_info("RDMA r/w request %#x: token %#x, length %#x\n",
6208 le32_to_cpu(desc
[i
].token
),
6209 le32_to_cpu(desc
[i
].length
));
6215 work
->need_invalidate_rkey
=
6216 (Channel
== SMB2_CHANNEL_RDMA_V1_INVALIDATE
);
6217 if (Channel
== SMB2_CHANNEL_RDMA_V1_INVALIDATE
)
6218 work
->remote_key
= le32_to_cpu(desc
->token
);
6222 static ssize_t
smb2_read_rdma_channel(struct ksmbd_work
*work
,
6223 struct smb2_read_req
*req
, void *data_buf
,
6228 err
= ksmbd_conn_rdma_write(work
->conn
, data_buf
, length
,
6229 (struct smb2_buffer_desc_v1
*)
6230 ((char *)req
+ le16_to_cpu(req
->ReadChannelInfoOffset
)),
6231 le16_to_cpu(req
->ReadChannelInfoLength
));
6239 * smb2_read() - handler for smb2 read from file
6240 * @work: smb work containing read command buffer
6242 * Return: 0 on success, otherwise error
6244 int smb2_read(struct ksmbd_work
*work
)
6246 struct ksmbd_conn
*conn
= work
->conn
;
6247 struct smb2_read_req
*req
;
6248 struct smb2_read_rsp
*rsp
;
6249 struct ksmbd_file
*fp
= NULL
;
6251 size_t length
, mincount
;
6252 ssize_t nbytes
= 0, remain_bytes
= 0;
6254 bool is_rdma_channel
= false;
6255 unsigned int max_read_size
= conn
->vals
->max_read_size
;
6256 unsigned int id
= KSMBD_NO_FID
, pid
= KSMBD_NO_FID
;
6257 void *aux_payload_buf
;
6259 if (test_share_config_flag(work
->tcon
->share_conf
,
6260 KSMBD_SHARE_FLAG_PIPE
)) {
6261 ksmbd_debug(SMB
, "IPC pipe read request\n");
6262 return smb2_read_pipe(work
);
6265 if (work
->next_smb2_rcv_hdr_off
) {
6266 req
= ksmbd_req_buf_next(work
);
6267 rsp
= ksmbd_resp_buf_next(work
);
6268 if (!has_file_id(req
->VolatileFileId
)) {
6269 ksmbd_debug(SMB
, "Compound request set FID = %llu\n",
6270 work
->compound_fid
);
6271 id
= work
->compound_fid
;
6272 pid
= work
->compound_pfid
;
6275 req
= smb2_get_msg(work
->request_buf
);
6276 rsp
= smb2_get_msg(work
->response_buf
);
6279 if (!has_file_id(id
)) {
6280 id
= req
->VolatileFileId
;
6281 pid
= req
->PersistentFileId
;
6284 if (req
->Channel
== SMB2_CHANNEL_RDMA_V1_INVALIDATE
||
6285 req
->Channel
== SMB2_CHANNEL_RDMA_V1
) {
6286 is_rdma_channel
= true;
6287 max_read_size
= get_smbd_max_read_write_size();
6290 if (is_rdma_channel
== true) {
6291 unsigned int ch_offset
= le16_to_cpu(req
->ReadChannelInfoOffset
);
6293 if (ch_offset
< offsetof(struct smb2_read_req
, Buffer
)) {
6297 err
= smb2_set_remote_key_for_rdma(work
,
6298 (struct smb2_buffer_desc_v1
*)
6299 ((char *)req
+ ch_offset
),
6301 req
->ReadChannelInfoLength
);
6306 fp
= ksmbd_lookup_fd_slow(work
, id
, pid
);
6312 if (!(fp
->daccess
& (FILE_READ_DATA_LE
| FILE_READ_ATTRIBUTES_LE
))) {
6313 pr_err("Not permitted to read : 0x%x\n", fp
->daccess
);
6318 offset
= le64_to_cpu(req
->Offset
);
6319 length
= le32_to_cpu(req
->Length
);
6320 mincount
= le32_to_cpu(req
->MinimumCount
);
6322 if (length
> max_read_size
) {
6323 ksmbd_debug(SMB
, "limiting read size to max size(%u)\n",
6329 ksmbd_debug(SMB
, "filename %pD, offset %lld, len %zu\n",
6330 fp
->filp
, offset
, length
);
6332 aux_payload_buf
= kvzalloc(length
, GFP_KERNEL
);
6333 if (!aux_payload_buf
) {
6338 nbytes
= ksmbd_vfs_read(work
, fp
, length
, &offset
, aux_payload_buf
);
6344 if ((nbytes
== 0 && length
!= 0) || nbytes
< mincount
) {
6345 kvfree(aux_payload_buf
);
6346 rsp
->hdr
.Status
= STATUS_END_OF_FILE
;
6347 smb2_set_err_rsp(work
);
6348 ksmbd_fd_put(work
, fp
);
6352 ksmbd_debug(SMB
, "nbytes %zu, offset %lld mincount %zu\n",
6353 nbytes
, offset
, mincount
);
6355 if (is_rdma_channel
== true) {
6356 /* write data to the client using rdma channel */
6357 remain_bytes
= smb2_read_rdma_channel(work
, req
,
6360 kvfree(aux_payload_buf
);
6361 aux_payload_buf
= NULL
;
6363 if (remain_bytes
< 0) {
6364 err
= (int)remain_bytes
;
6369 rsp
->StructureSize
= cpu_to_le16(17);
6370 rsp
->DataOffset
= 80;
6372 rsp
->DataLength
= cpu_to_le32(nbytes
);
6373 rsp
->DataRemaining
= cpu_to_le32(remain_bytes
);
6375 err
= ksmbd_iov_pin_rsp_read(work
, (void *)rsp
,
6376 offsetof(struct smb2_read_rsp
, Buffer
),
6377 aux_payload_buf
, nbytes
);
6380 ksmbd_fd_put(work
, fp
);
6386 rsp
->hdr
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
6387 else if (err
== -EAGAIN
)
6388 rsp
->hdr
.Status
= STATUS_FILE_LOCK_CONFLICT
;
6389 else if (err
== -ENOENT
)
6390 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
6391 else if (err
== -EACCES
)
6392 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
6393 else if (err
== -ESHARE
)
6394 rsp
->hdr
.Status
= STATUS_SHARING_VIOLATION
;
6395 else if (err
== -EINVAL
)
6396 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
6398 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6400 smb2_set_err_rsp(work
);
6402 ksmbd_fd_put(work
, fp
);
6407 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6408 * @work: smb work containing write IPC pipe command buffer
6410 * Return: 0 on success, otherwise error
6412 static noinline
int smb2_write_pipe(struct ksmbd_work
*work
)
6414 struct smb2_write_req
*req
;
6415 struct smb2_write_rsp
*rsp
;
6416 struct ksmbd_rpc_command
*rpc_resp
;
6418 int err
= 0, ret
= 0;
6422 WORK_BUFFERS(work
, req
, rsp
);
6424 length
= le32_to_cpu(req
->Length
);
6425 id
= req
->VolatileFileId
;
6427 if ((u64
)le16_to_cpu(req
->DataOffset
) + length
>
6428 get_rfc1002_len(work
->request_buf
)) {
6429 pr_err("invalid write data offset %u, smb_len %u\n",
6430 le16_to_cpu(req
->DataOffset
),
6431 get_rfc1002_len(work
->request_buf
));
6436 data_buf
= (char *)(((char *)&req
->hdr
.ProtocolId
) +
6437 le16_to_cpu(req
->DataOffset
));
6439 rpc_resp
= ksmbd_rpc_write(work
->sess
, id
, data_buf
, length
);
6441 if (rpc_resp
->flags
== KSMBD_RPC_ENOTIMPLEMENTED
) {
6442 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
6444 smb2_set_err_rsp(work
);
6447 if (rpc_resp
->flags
!= KSMBD_RPC_OK
) {
6448 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6449 smb2_set_err_rsp(work
);
6456 rsp
->StructureSize
= cpu_to_le16(17);
6457 rsp
->DataOffset
= 0;
6459 rsp
->DataLength
= cpu_to_le32(length
);
6460 rsp
->DataRemaining
= 0;
6462 err
= ksmbd_iov_pin_rsp(work
, (void *)rsp
,
6463 offsetof(struct smb2_write_rsp
, Buffer
));
6466 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6467 smb2_set_err_rsp(work
);
6473 static ssize_t
smb2_write_rdma_channel(struct ksmbd_work
*work
,
6474 struct smb2_write_req
*req
,
6475 struct ksmbd_file
*fp
,
6476 loff_t offset
, size_t length
, bool sync
)
6482 data_buf
= kvzalloc(length
, GFP_KERNEL
);
6486 ret
= ksmbd_conn_rdma_read(work
->conn
, data_buf
, length
,
6487 (struct smb2_buffer_desc_v1
*)
6488 ((char *)req
+ le16_to_cpu(req
->WriteChannelInfoOffset
)),
6489 le16_to_cpu(req
->WriteChannelInfoLength
));
6495 ret
= ksmbd_vfs_write(work
, fp
, data_buf
, length
, &offset
, sync
, &nbytes
);
6504 * smb2_write() - handler for smb2 write from file
6505 * @work: smb work containing write command buffer
6507 * Return: 0 on success, otherwise error
6509 int smb2_write(struct ksmbd_work
*work
)
6511 struct smb2_write_req
*req
;
6512 struct smb2_write_rsp
*rsp
;
6513 struct ksmbd_file
*fp
= NULL
;
6518 bool writethrough
= false, is_rdma_channel
= false;
6520 unsigned int max_write_size
= work
->conn
->vals
->max_write_size
;
6522 WORK_BUFFERS(work
, req
, rsp
);
6524 if (test_share_config_flag(work
->tcon
->share_conf
, KSMBD_SHARE_FLAG_PIPE
)) {
6525 ksmbd_debug(SMB
, "IPC pipe write request\n");
6526 return smb2_write_pipe(work
);
6529 offset
= le64_to_cpu(req
->Offset
);
6530 length
= le32_to_cpu(req
->Length
);
6532 if (req
->Channel
== SMB2_CHANNEL_RDMA_V1
||
6533 req
->Channel
== SMB2_CHANNEL_RDMA_V1_INVALIDATE
) {
6534 is_rdma_channel
= true;
6535 max_write_size
= get_smbd_max_read_write_size();
6536 length
= le32_to_cpu(req
->RemainingBytes
);
6539 if (is_rdma_channel
== true) {
6540 unsigned int ch_offset
= le16_to_cpu(req
->WriteChannelInfoOffset
);
6542 if (req
->Length
!= 0 || req
->DataOffset
!= 0 ||
6543 ch_offset
< offsetof(struct smb2_write_req
, Buffer
)) {
6547 err
= smb2_set_remote_key_for_rdma(work
,
6548 (struct smb2_buffer_desc_v1
*)
6549 ((char *)req
+ ch_offset
),
6551 req
->WriteChannelInfoLength
);
6556 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
6557 ksmbd_debug(SMB
, "User does not have write permission\n");
6562 fp
= ksmbd_lookup_fd_slow(work
, req
->VolatileFileId
, req
->PersistentFileId
);
6568 if (!(fp
->daccess
& (FILE_WRITE_DATA_LE
| FILE_READ_ATTRIBUTES_LE
))) {
6569 pr_err("Not permitted to write : 0x%x\n", fp
->daccess
);
6574 if (length
> max_write_size
) {
6575 ksmbd_debug(SMB
, "limiting write size to max size(%u)\n",
6581 ksmbd_debug(SMB
, "flags %u\n", le32_to_cpu(req
->Flags
));
6582 if (le32_to_cpu(req
->Flags
) & SMB2_WRITEFLAG_WRITE_THROUGH
)
6583 writethrough
= true;
6585 if (is_rdma_channel
== false) {
6586 if (le16_to_cpu(req
->DataOffset
) <
6587 offsetof(struct smb2_write_req
, Buffer
)) {
6592 data_buf
= (char *)(((char *)&req
->hdr
.ProtocolId
) +
6593 le16_to_cpu(req
->DataOffset
));
6595 ksmbd_debug(SMB
, "filename %pD, offset %lld, len %zu\n",
6596 fp
->filp
, offset
, length
);
6597 err
= ksmbd_vfs_write(work
, fp
, data_buf
, length
, &offset
,
6598 writethrough
, &nbytes
);
6602 /* read data from the client using rdma channel, and
6605 nbytes
= smb2_write_rdma_channel(work
, req
, fp
, offset
, length
,
6613 rsp
->StructureSize
= cpu_to_le16(17);
6614 rsp
->DataOffset
= 0;
6616 rsp
->DataLength
= cpu_to_le32(nbytes
);
6617 rsp
->DataRemaining
= 0;
6619 err
= ksmbd_iov_pin_rsp(work
, rsp
, offsetof(struct smb2_write_rsp
, Buffer
));
6622 ksmbd_fd_put(work
, fp
);
6627 rsp
->hdr
.Status
= STATUS_FILE_LOCK_CONFLICT
;
6628 else if (err
== -ENOSPC
|| err
== -EFBIG
)
6629 rsp
->hdr
.Status
= STATUS_DISK_FULL
;
6630 else if (err
== -ENOENT
)
6631 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
6632 else if (err
== -EACCES
)
6633 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
6634 else if (err
== -ESHARE
)
6635 rsp
->hdr
.Status
= STATUS_SHARING_VIOLATION
;
6636 else if (err
== -EINVAL
)
6637 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
6639 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6641 smb2_set_err_rsp(work
);
6642 ksmbd_fd_put(work
, fp
);
6647 * smb2_flush() - handler for smb2 flush file - fsync
6648 * @work: smb work containing flush command buffer
6650 * Return: 0 on success, otherwise error
6652 int smb2_flush(struct ksmbd_work
*work
)
6654 struct smb2_flush_req
*req
;
6655 struct smb2_flush_rsp
*rsp
;
6658 WORK_BUFFERS(work
, req
, rsp
);
6660 ksmbd_debug(SMB
, "SMB2_FLUSH called for fid %llu\n", req
->VolatileFileId
);
6662 err
= ksmbd_vfs_fsync(work
, req
->VolatileFileId
, req
->PersistentFileId
);
6666 rsp
->StructureSize
= cpu_to_le16(4);
6668 return ksmbd_iov_pin_rsp(work
, rsp
, sizeof(struct smb2_flush_rsp
));
6671 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6672 smb2_set_err_rsp(work
);
6677 * smb2_cancel() - handler for smb2 cancel command
6678 * @work: smb work containing cancel command buffer
6680 * Return: 0 on success, otherwise error
6682 int smb2_cancel(struct ksmbd_work
*work
)
6684 struct ksmbd_conn
*conn
= work
->conn
;
6685 struct smb2_hdr
*hdr
= smb2_get_msg(work
->request_buf
);
6686 struct smb2_hdr
*chdr
;
6687 struct ksmbd_work
*iter
;
6688 struct list_head
*command_list
;
6690 if (work
->next_smb2_rcv_hdr_off
)
6691 hdr
= ksmbd_resp_buf_next(work
);
6693 ksmbd_debug(SMB
, "smb2 cancel called on mid %llu, async flags 0x%x\n",
6694 hdr
->MessageId
, hdr
->Flags
);
6696 if (hdr
->Flags
& SMB2_FLAGS_ASYNC_COMMAND
) {
6697 command_list
= &conn
->async_requests
;
6699 spin_lock(&conn
->request_lock
);
6700 list_for_each_entry(iter
, command_list
,
6701 async_request_entry
) {
6702 chdr
= smb2_get_msg(iter
->request_buf
);
6704 if (iter
->async_id
!=
6705 le64_to_cpu(hdr
->Id
.AsyncId
))
6709 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6710 le64_to_cpu(hdr
->Id
.AsyncId
),
6711 le16_to_cpu(chdr
->Command
));
6712 iter
->state
= KSMBD_WORK_CANCELLED
;
6713 if (iter
->cancel_fn
)
6714 iter
->cancel_fn(iter
->cancel_argv
);
6717 spin_unlock(&conn
->request_lock
);
6719 command_list
= &conn
->requests
;
6721 spin_lock(&conn
->request_lock
);
6722 list_for_each_entry(iter
, command_list
, request_entry
) {
6723 chdr
= smb2_get_msg(iter
->request_buf
);
6725 if (chdr
->MessageId
!= hdr
->MessageId
||
6730 "smb2 with mid %llu cancelled command = 0x%x\n",
6731 le64_to_cpu(hdr
->MessageId
),
6732 le16_to_cpu(chdr
->Command
));
6733 iter
->state
= KSMBD_WORK_CANCELLED
;
6736 spin_unlock(&conn
->request_lock
);
6739 /* For SMB2_CANCEL command itself send no response*/
6740 work
->send_no_response
= 1;
6744 struct file_lock
*smb_flock_init(struct file
*f
)
6746 struct file_lock
*fl
;
6748 fl
= locks_alloc_lock();
6752 locks_init_lock(fl
);
6755 fl
->fl_pid
= current
->tgid
;
6757 fl
->fl_flags
= FL_POSIX
;
6759 fl
->fl_lmops
= NULL
;
6765 static int smb2_set_flock_flags(struct file_lock
*flock
, int flags
)
6769 /* Checking for wrong flag combination during lock request*/
6771 case SMB2_LOCKFLAG_SHARED
:
6772 ksmbd_debug(SMB
, "received shared request\n");
6774 flock
->fl_type
= F_RDLCK
;
6775 flock
->fl_flags
|= FL_SLEEP
;
6777 case SMB2_LOCKFLAG_EXCLUSIVE
:
6778 ksmbd_debug(SMB
, "received exclusive request\n");
6780 flock
->fl_type
= F_WRLCK
;
6781 flock
->fl_flags
|= FL_SLEEP
;
6783 case SMB2_LOCKFLAG_SHARED
| SMB2_LOCKFLAG_FAIL_IMMEDIATELY
:
6785 "received shared & fail immediately request\n");
6787 flock
->fl_type
= F_RDLCK
;
6789 case SMB2_LOCKFLAG_EXCLUSIVE
| SMB2_LOCKFLAG_FAIL_IMMEDIATELY
:
6791 "received exclusive & fail immediately request\n");
6793 flock
->fl_type
= F_WRLCK
;
6795 case SMB2_LOCKFLAG_UNLOCK
:
6796 ksmbd_debug(SMB
, "received unlock request\n");
6797 flock
->fl_type
= F_UNLCK
;
6805 static struct ksmbd_lock
*smb2_lock_init(struct file_lock
*flock
,
6806 unsigned int cmd
, int flags
,
6807 struct list_head
*lock_list
)
6809 struct ksmbd_lock
*lock
;
6811 lock
= kzalloc(sizeof(struct ksmbd_lock
), GFP_KERNEL
);
6817 lock
->start
= flock
->fl_start
;
6818 lock
->end
= flock
->fl_end
;
6819 lock
->flags
= flags
;
6820 if (lock
->start
== lock
->end
)
6822 INIT_LIST_HEAD(&lock
->clist
);
6823 INIT_LIST_HEAD(&lock
->flist
);
6824 INIT_LIST_HEAD(&lock
->llist
);
6825 list_add_tail(&lock
->llist
, lock_list
);
6830 static void smb2_remove_blocked_lock(void **argv
)
6832 struct file_lock
*flock
= (struct file_lock
*)argv
[0];
6834 ksmbd_vfs_posix_lock_unblock(flock
);
6835 wake_up(&flock
->fl_wait
);
6838 static inline bool lock_defer_pending(struct file_lock
*fl
)
6840 /* check pending lock waiters */
6841 return waitqueue_active(&fl
->fl_wait
);
6845 * smb2_lock() - handler for smb2 file lock command
6846 * @work: smb work containing lock command buffer
6848 * Return: 0 on success, otherwise error
6850 int smb2_lock(struct ksmbd_work
*work
)
6852 struct smb2_lock_req
*req
;
6853 struct smb2_lock_rsp
*rsp
;
6854 struct smb2_lock_element
*lock_ele
;
6855 struct ksmbd_file
*fp
= NULL
;
6856 struct file_lock
*flock
= NULL
;
6857 struct file
*filp
= NULL
;
6861 int err
= -EIO
, i
, rc
= 0;
6862 u64 lock_start
, lock_length
;
6863 struct ksmbd_lock
*smb_lock
= NULL
, *cmp_lock
, *tmp
, *tmp2
;
6864 struct ksmbd_conn
*conn
;
6866 LIST_HEAD(lock_list
);
6867 LIST_HEAD(rollback_list
);
6870 WORK_BUFFERS(work
, req
, rsp
);
6872 ksmbd_debug(SMB
, "Received lock request\n");
6873 fp
= ksmbd_lookup_fd_slow(work
, req
->VolatileFileId
, req
->PersistentFileId
);
6875 ksmbd_debug(SMB
, "Invalid file id for lock : %llu\n", req
->VolatileFileId
);
6881 lock_count
= le16_to_cpu(req
->LockCount
);
6882 lock_ele
= req
->locks
;
6884 ksmbd_debug(SMB
, "lock count is %d\n", lock_count
);
6890 for (i
= 0; i
< lock_count
; i
++) {
6891 flags
= le32_to_cpu(lock_ele
[i
].Flags
);
6893 flock
= smb_flock_init(filp
);
6897 cmd
= smb2_set_flock_flags(flock
, flags
);
6899 lock_start
= le64_to_cpu(lock_ele
[i
].Offset
);
6900 lock_length
= le64_to_cpu(lock_ele
[i
].Length
);
6901 if (lock_start
> U64_MAX
- lock_length
) {
6902 pr_err("Invalid lock range requested\n");
6903 rsp
->hdr
.Status
= STATUS_INVALID_LOCK_RANGE
;
6904 locks_free_lock(flock
);
6908 if (lock_start
> OFFSET_MAX
)
6909 flock
->fl_start
= OFFSET_MAX
;
6911 flock
->fl_start
= lock_start
;
6913 lock_length
= le64_to_cpu(lock_ele
[i
].Length
);
6914 if (lock_length
> OFFSET_MAX
- flock
->fl_start
)
6915 lock_length
= OFFSET_MAX
- flock
->fl_start
;
6917 flock
->fl_end
= flock
->fl_start
+ lock_length
;
6919 if (flock
->fl_end
< flock
->fl_start
) {
6921 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6922 flock
->fl_end
, flock
->fl_start
);
6923 rsp
->hdr
.Status
= STATUS_INVALID_LOCK_RANGE
;
6924 locks_free_lock(flock
);
6928 /* Check conflict locks in one request */
6929 list_for_each_entry(cmp_lock
, &lock_list
, llist
) {
6930 if (cmp_lock
->fl
->fl_start
<= flock
->fl_start
&&
6931 cmp_lock
->fl
->fl_end
>= flock
->fl_end
) {
6932 if (cmp_lock
->fl
->fl_type
!= F_UNLCK
&&
6933 flock
->fl_type
!= F_UNLCK
) {
6934 pr_err("conflict two locks in one request\n");
6936 locks_free_lock(flock
);
6942 smb_lock
= smb2_lock_init(flock
, cmd
, flags
, &lock_list
);
6945 locks_free_lock(flock
);
6950 list_for_each_entry_safe(smb_lock
, tmp
, &lock_list
, llist
) {
6951 if (smb_lock
->cmd
< 0) {
6956 if (!(smb_lock
->flags
& SMB2_LOCKFLAG_MASK
)) {
6961 if ((prior_lock
& (SMB2_LOCKFLAG_EXCLUSIVE
| SMB2_LOCKFLAG_SHARED
) &&
6962 smb_lock
->flags
& SMB2_LOCKFLAG_UNLOCK
) ||
6963 (prior_lock
== SMB2_LOCKFLAG_UNLOCK
&&
6964 !(smb_lock
->flags
& SMB2_LOCKFLAG_UNLOCK
))) {
6969 prior_lock
= smb_lock
->flags
;
6971 if (!(smb_lock
->flags
& SMB2_LOCKFLAG_UNLOCK
) &&
6972 !(smb_lock
->flags
& SMB2_LOCKFLAG_FAIL_IMMEDIATELY
))
6976 /* check locks in connection list */
6977 down_read(&conn_list_lock
);
6978 list_for_each_entry(conn
, &conn_list
, conns_list
) {
6979 spin_lock(&conn
->llist_lock
);
6980 list_for_each_entry_safe(cmp_lock
, tmp2
, &conn
->lock_list
, clist
) {
6981 if (file_inode(cmp_lock
->fl
->fl_file
) !=
6982 file_inode(smb_lock
->fl
->fl_file
))
6985 if (smb_lock
->fl
->fl_type
== F_UNLCK
) {
6986 if (cmp_lock
->fl
->fl_file
== smb_lock
->fl
->fl_file
&&
6987 cmp_lock
->start
== smb_lock
->start
&&
6988 cmp_lock
->end
== smb_lock
->end
&&
6989 !lock_defer_pending(cmp_lock
->fl
)) {
6991 list_del(&cmp_lock
->flist
);
6992 list_del(&cmp_lock
->clist
);
6993 spin_unlock(&conn
->llist_lock
);
6994 up_read(&conn_list_lock
);
6996 locks_free_lock(cmp_lock
->fl
);
7003 if (cmp_lock
->fl
->fl_file
== smb_lock
->fl
->fl_file
) {
7004 if (smb_lock
->flags
& SMB2_LOCKFLAG_SHARED
)
7007 if (cmp_lock
->flags
& SMB2_LOCKFLAG_SHARED
)
7011 /* check zero byte lock range */
7012 if (cmp_lock
->zero_len
&& !smb_lock
->zero_len
&&
7013 cmp_lock
->start
> smb_lock
->start
&&
7014 cmp_lock
->start
< smb_lock
->end
) {
7015 spin_unlock(&conn
->llist_lock
);
7016 up_read(&conn_list_lock
);
7017 pr_err("previous lock conflict with zero byte lock range\n");
7021 if (smb_lock
->zero_len
&& !cmp_lock
->zero_len
&&
7022 smb_lock
->start
> cmp_lock
->start
&&
7023 smb_lock
->start
< cmp_lock
->end
) {
7024 spin_unlock(&conn
->llist_lock
);
7025 up_read(&conn_list_lock
);
7026 pr_err("current lock conflict with zero byte lock range\n");
7030 if (((cmp_lock
->start
<= smb_lock
->start
&&
7031 cmp_lock
->end
> smb_lock
->start
) ||
7032 (cmp_lock
->start
< smb_lock
->end
&&
7033 cmp_lock
->end
>= smb_lock
->end
)) &&
7034 !cmp_lock
->zero_len
&& !smb_lock
->zero_len
) {
7035 spin_unlock(&conn
->llist_lock
);
7036 up_read(&conn_list_lock
);
7037 pr_err("Not allow lock operation on exclusive lock range\n");
7041 spin_unlock(&conn
->llist_lock
);
7043 up_read(&conn_list_lock
);
7045 if (smb_lock
->fl
->fl_type
== F_UNLCK
&& nolock
) {
7046 pr_err("Try to unlock nolocked range\n");
7047 rsp
->hdr
.Status
= STATUS_RANGE_NOT_LOCKED
;
7052 if (smb_lock
->zero_len
) {
7057 flock
= smb_lock
->fl
;
7058 list_del(&smb_lock
->llist
);
7060 rc
= vfs_lock_file(filp
, smb_lock
->cmd
, flock
, NULL
);
7062 if (flags
& SMB2_LOCKFLAG_UNLOCK
) {
7064 ksmbd_debug(SMB
, "File unlocked\n");
7065 } else if (rc
== -ENOENT
) {
7066 rsp
->hdr
.Status
= STATUS_NOT_LOCKED
;
7069 locks_free_lock(flock
);
7072 if (rc
== FILE_LOCK_DEFERRED
) {
7076 "would have to wait for getting lock\n");
7077 list_add(&smb_lock
->llist
, &rollback_list
);
7079 argv
= kmalloc(sizeof(void *), GFP_KERNEL
);
7086 rc
= setup_async_work(work
,
7087 smb2_remove_blocked_lock
,
7094 spin_lock(&fp
->f_lock
);
7095 list_add(&work
->fp_entry
, &fp
->blocked_works
);
7096 spin_unlock(&fp
->f_lock
);
7098 smb2_send_interim_resp(work
, STATUS_PENDING
);
7100 ksmbd_vfs_posix_lock_wait(flock
);
7102 spin_lock(&fp
->f_lock
);
7103 list_del(&work
->fp_entry
);
7104 spin_unlock(&fp
->f_lock
);
7106 if (work
->state
!= KSMBD_WORK_ACTIVE
) {
7107 list_del(&smb_lock
->llist
);
7108 locks_free_lock(flock
);
7110 if (work
->state
== KSMBD_WORK_CANCELLED
) {
7114 smb2_send_interim_resp(work
,
7116 work
->send_no_response
= 1;
7121 STATUS_RANGE_NOT_LOCKED
;
7126 list_del(&smb_lock
->llist
);
7127 release_async_work(work
);
7130 list_add(&smb_lock
->llist
, &rollback_list
);
7131 spin_lock(&work
->conn
->llist_lock
);
7132 list_add_tail(&smb_lock
->clist
,
7133 &work
->conn
->lock_list
);
7134 list_add_tail(&smb_lock
->flist
,
7136 spin_unlock(&work
->conn
->llist_lock
);
7137 ksmbd_debug(SMB
, "successful in taking lock\n");
7144 if (atomic_read(&fp
->f_ci
->op_count
) > 1)
7145 smb_break_all_oplock(work
, fp
);
7147 rsp
->StructureSize
= cpu_to_le16(4);
7148 ksmbd_debug(SMB
, "successful in taking lock\n");
7149 rsp
->hdr
.Status
= STATUS_SUCCESS
;
7151 err
= ksmbd_iov_pin_rsp(work
, rsp
, sizeof(struct smb2_lock_rsp
));
7155 ksmbd_fd_put(work
, fp
);
7159 list_for_each_entry_safe(smb_lock
, tmp
, &lock_list
, llist
) {
7160 locks_free_lock(smb_lock
->fl
);
7161 list_del(&smb_lock
->llist
);
7165 list_for_each_entry_safe(smb_lock
, tmp
, &rollback_list
, llist
) {
7166 struct file_lock
*rlock
= NULL
;
7168 rlock
= smb_flock_init(filp
);
7169 rlock
->fl_type
= F_UNLCK
;
7170 rlock
->fl_start
= smb_lock
->start
;
7171 rlock
->fl_end
= smb_lock
->end
;
7173 rc
= vfs_lock_file(filp
, F_SETLK
, rlock
, NULL
);
7175 pr_err("rollback unlock fail : %d\n", rc
);
7177 list_del(&smb_lock
->llist
);
7178 spin_lock(&work
->conn
->llist_lock
);
7179 if (!list_empty(&smb_lock
->flist
))
7180 list_del(&smb_lock
->flist
);
7181 list_del(&smb_lock
->clist
);
7182 spin_unlock(&work
->conn
->llist_lock
);
7184 locks_free_lock(smb_lock
->fl
);
7185 locks_free_lock(rlock
);
7189 ksmbd_debug(SMB
, "failed in taking lock(flags : %x), err : %d\n", flags
, err
);
7191 if (!rsp
->hdr
.Status
) {
7193 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7194 else if (err
== -ENOMEM
)
7195 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
7196 else if (err
== -ENOENT
)
7197 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
7199 rsp
->hdr
.Status
= STATUS_LOCK_NOT_GRANTED
;
7202 smb2_set_err_rsp(work
);
7203 ksmbd_fd_put(work
, fp
);
7207 static int fsctl_copychunk(struct ksmbd_work
*work
,
7208 struct copychunk_ioctl_req
*ci_req
,
7209 unsigned int cnt_code
,
7210 unsigned int input_count
,
7211 unsigned long long volatile_id
,
7212 unsigned long long persistent_id
,
7213 struct smb2_ioctl_rsp
*rsp
)
7215 struct copychunk_ioctl_rsp
*ci_rsp
;
7216 struct ksmbd_file
*src_fp
= NULL
, *dst_fp
= NULL
;
7217 struct srv_copychunk
*chunks
;
7218 unsigned int i
, chunk_count
, chunk_count_written
= 0;
7219 unsigned int chunk_size_written
= 0;
7220 loff_t total_size_written
= 0;
7223 ci_rsp
= (struct copychunk_ioctl_rsp
*)&rsp
->Buffer
[0];
7225 rsp
->VolatileFileId
= volatile_id
;
7226 rsp
->PersistentFileId
= persistent_id
;
7227 ci_rsp
->ChunksWritten
=
7228 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
7229 ci_rsp
->ChunkBytesWritten
=
7230 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
7231 ci_rsp
->TotalBytesWritten
=
7232 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
7234 chunks
= (struct srv_copychunk
*)&ci_req
->Chunks
[0];
7235 chunk_count
= le32_to_cpu(ci_req
->ChunkCount
);
7236 if (chunk_count
== 0)
7238 total_size_written
= 0;
7240 /* verify the SRV_COPYCHUNK_COPY packet */
7241 if (chunk_count
> ksmbd_server_side_copy_max_chunk_count() ||
7242 input_count
< offsetof(struct copychunk_ioctl_req
, Chunks
) +
7243 chunk_count
* sizeof(struct srv_copychunk
)) {
7244 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7248 for (i
= 0; i
< chunk_count
; i
++) {
7249 if (le32_to_cpu(chunks
[i
].Length
) == 0 ||
7250 le32_to_cpu(chunks
[i
].Length
) > ksmbd_server_side_copy_max_chunk_size())
7252 total_size_written
+= le32_to_cpu(chunks
[i
].Length
);
7255 if (i
< chunk_count
||
7256 total_size_written
> ksmbd_server_side_copy_max_total_size()) {
7257 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7261 src_fp
= ksmbd_lookup_foreign_fd(work
,
7262 le64_to_cpu(ci_req
->ResumeKey
[0]));
7263 dst_fp
= ksmbd_lookup_fd_slow(work
, volatile_id
, persistent_id
);
7266 src_fp
->persistent_id
!= le64_to_cpu(ci_req
->ResumeKey
[1])) {
7267 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
7272 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
7277 * FILE_READ_DATA should only be included in
7278 * the FSCTL_COPYCHUNK case
7280 if (cnt_code
== FSCTL_COPYCHUNK
&&
7281 !(dst_fp
->daccess
& (FILE_READ_DATA_LE
| FILE_GENERIC_READ_LE
))) {
7282 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
7286 ret
= ksmbd_vfs_copy_file_ranges(work
, src_fp
, dst_fp
,
7287 chunks
, chunk_count
,
7288 &chunk_count_written
,
7289 &chunk_size_written
,
7290 &total_size_written
);
7293 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
7295 rsp
->hdr
.Status
= STATUS_FILE_LOCK_CONFLICT
;
7296 else if (ret
== -EBADF
)
7297 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
7298 else if (ret
== -EFBIG
|| ret
== -ENOSPC
)
7299 rsp
->hdr
.Status
= STATUS_DISK_FULL
;
7300 else if (ret
== -EINVAL
)
7301 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7302 else if (ret
== -EISDIR
)
7303 rsp
->hdr
.Status
= STATUS_FILE_IS_A_DIRECTORY
;
7304 else if (ret
== -E2BIG
)
7305 rsp
->hdr
.Status
= STATUS_INVALID_VIEW_SIZE
;
7307 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
7310 ci_rsp
->ChunksWritten
= cpu_to_le32(chunk_count_written
);
7311 ci_rsp
->ChunkBytesWritten
= cpu_to_le32(chunk_size_written
);
7312 ci_rsp
->TotalBytesWritten
= cpu_to_le32(total_size_written
);
7314 ksmbd_fd_put(work
, src_fp
);
7315 ksmbd_fd_put(work
, dst_fp
);
7319 static __be32
idev_ipv4_address(struct in_device
*idev
)
7323 struct in_ifaddr
*ifa
;
7326 in_dev_for_each_ifa_rcu(ifa
, idev
) {
7327 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
7330 addr
= ifa
->ifa_address
;
7337 static int fsctl_query_iface_info_ioctl(struct ksmbd_conn
*conn
,
7338 struct smb2_ioctl_rsp
*rsp
,
7339 unsigned int out_buf_len
)
7341 struct network_interface_info_ioctl_rsp
*nii_rsp
= NULL
;
7343 struct net_device
*netdev
;
7344 struct sockaddr_storage_rsp
*sockaddr_storage
;
7346 unsigned long long speed
;
7349 for_each_netdev(&init_net
, netdev
) {
7350 bool ipv4_set
= false;
7352 if (netdev
->type
== ARPHRD_LOOPBACK
)
7355 flags
= dev_get_flags(netdev
);
7356 if (!(flags
& IFF_RUNNING
))
7360 nbytes
+ sizeof(struct network_interface_info_ioctl_rsp
)) {
7365 nii_rsp
= (struct network_interface_info_ioctl_rsp
*)
7366 &rsp
->Buffer
[nbytes
];
7367 nii_rsp
->IfIndex
= cpu_to_le32(netdev
->ifindex
);
7369 nii_rsp
->Capability
= 0;
7370 if (netdev
->real_num_tx_queues
> 1)
7371 nii_rsp
->Capability
|= cpu_to_le32(RSS_CAPABLE
);
7372 if (ksmbd_rdma_capable_netdev(netdev
))
7373 nii_rsp
->Capability
|= cpu_to_le32(RDMA_CAPABLE
);
7375 nii_rsp
->Next
= cpu_to_le32(152);
7376 nii_rsp
->Reserved
= 0;
7378 if (netdev
->ethtool_ops
->get_link_ksettings
) {
7379 struct ethtool_link_ksettings cmd
;
7381 netdev
->ethtool_ops
->get_link_ksettings(netdev
, &cmd
);
7382 speed
= cmd
.base
.speed
;
7384 ksmbd_debug(SMB
, "%s %s\n", netdev
->name
,
7385 "speed is unknown, defaulting to 1Gb/sec");
7390 nii_rsp
->LinkSpeed
= cpu_to_le64(speed
);
7392 sockaddr_storage
= (struct sockaddr_storage_rsp
*)
7393 nii_rsp
->SockAddr_Storage
;
7394 memset(sockaddr_storage
, 0, 128);
7397 struct in_device
*idev
;
7399 sockaddr_storage
->Family
= cpu_to_le16(INTERNETWORK
);
7400 sockaddr_storage
->addr4
.Port
= 0;
7402 idev
= __in_dev_get_rtnl(netdev
);
7405 sockaddr_storage
->addr4
.IPv4address
=
7406 idev_ipv4_address(idev
);
7407 nbytes
+= sizeof(struct network_interface_info_ioctl_rsp
);
7411 struct inet6_dev
*idev6
;
7412 struct inet6_ifaddr
*ifa
;
7413 __u8
*ipv6_addr
= sockaddr_storage
->addr6
.IPv6address
;
7415 sockaddr_storage
->Family
= cpu_to_le16(INTERNETWORKV6
);
7416 sockaddr_storage
->addr6
.Port
= 0;
7417 sockaddr_storage
->addr6
.FlowInfo
= 0;
7419 idev6
= __in6_dev_get(netdev
);
7423 list_for_each_entry(ifa
, &idev6
->addr_list
, if_list
) {
7424 if (ifa
->flags
& (IFA_F_TENTATIVE
|
7427 memcpy(ipv6_addr
, ifa
->addr
.s6_addr
, 16);
7430 sockaddr_storage
->addr6
.ScopeId
= 0;
7431 nbytes
+= sizeof(struct network_interface_info_ioctl_rsp
);
7436 /* zero if this is last one */
7440 rsp
->PersistentFileId
= SMB2_NO_FID
;
7441 rsp
->VolatileFileId
= SMB2_NO_FID
;
7445 static int fsctl_validate_negotiate_info(struct ksmbd_conn
*conn
,
7446 struct validate_negotiate_info_req
*neg_req
,
7447 struct validate_negotiate_info_rsp
*neg_rsp
,
7448 unsigned int in_buf_len
)
7453 if (in_buf_len
< offsetof(struct validate_negotiate_info_req
, Dialects
) +
7454 le16_to_cpu(neg_req
->DialectCount
) * sizeof(__le16
))
7457 dialect
= ksmbd_lookup_dialect_by_id(neg_req
->Dialects
,
7458 neg_req
->DialectCount
);
7459 if (dialect
== BAD_PROT_ID
|| dialect
!= conn
->dialect
) {
7464 if (strncmp(neg_req
->Guid
, conn
->ClientGUID
, SMB2_CLIENT_GUID_SIZE
)) {
7469 if (le16_to_cpu(neg_req
->SecurityMode
) != conn
->cli_sec_mode
) {
7474 if (le32_to_cpu(neg_req
->Capabilities
) != conn
->cli_cap
) {
7479 neg_rsp
->Capabilities
= cpu_to_le32(conn
->vals
->capabilities
);
7480 memset(neg_rsp
->Guid
, 0, SMB2_CLIENT_GUID_SIZE
);
7481 neg_rsp
->SecurityMode
= cpu_to_le16(conn
->srv_sec_mode
);
7482 neg_rsp
->Dialect
= cpu_to_le16(conn
->dialect
);
7487 static int fsctl_query_allocated_ranges(struct ksmbd_work
*work
, u64 id
,
7488 struct file_allocated_range_buffer
*qar_req
,
7489 struct file_allocated_range_buffer
*qar_rsp
,
7490 unsigned int in_count
, unsigned int *out_count
)
7492 struct ksmbd_file
*fp
;
7493 loff_t start
, length
;
7500 start
= le64_to_cpu(qar_req
->file_offset
);
7501 length
= le64_to_cpu(qar_req
->length
);
7503 if (start
< 0 || length
< 0)
7506 fp
= ksmbd_lookup_fd_fast(work
, id
);
7510 ret
= ksmbd_vfs_fqar_lseek(fp
, start
, length
,
7511 qar_rsp
, in_count
, out_count
);
7512 if (ret
&& ret
!= -E2BIG
)
7515 ksmbd_fd_put(work
, fp
);
7519 static int fsctl_pipe_transceive(struct ksmbd_work
*work
, u64 id
,
7520 unsigned int out_buf_len
,
7521 struct smb2_ioctl_req
*req
,
7522 struct smb2_ioctl_rsp
*rsp
)
7524 struct ksmbd_rpc_command
*rpc_resp
;
7525 char *data_buf
= (char *)&req
->Buffer
[0];
7528 rpc_resp
= ksmbd_rpc_ioctl(work
->sess
, id
, data_buf
,
7529 le32_to_cpu(req
->InputCount
));
7531 if (rpc_resp
->flags
== KSMBD_RPC_SOME_NOT_MAPPED
) {
7533 * set STATUS_SOME_NOT_MAPPED response
7534 * for unknown domain sid.
7536 rsp
->hdr
.Status
= STATUS_SOME_NOT_MAPPED
;
7537 } else if (rpc_resp
->flags
== KSMBD_RPC_ENOTIMPLEMENTED
) {
7538 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
7540 } else if (rpc_resp
->flags
!= KSMBD_RPC_OK
) {
7541 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7545 nbytes
= rpc_resp
->payload_sz
;
7546 if (rpc_resp
->payload_sz
> out_buf_len
) {
7547 rsp
->hdr
.Status
= STATUS_BUFFER_OVERFLOW
;
7548 nbytes
= out_buf_len
;
7551 if (!rpc_resp
->payload_sz
) {
7553 STATUS_UNEXPECTED_IO_ERROR
;
7557 memcpy((char *)rsp
->Buffer
, rpc_resp
->payload
, nbytes
);
7564 static inline int fsctl_set_sparse(struct ksmbd_work
*work
, u64 id
,
7565 struct file_sparse
*sparse
)
7567 struct ksmbd_file
*fp
;
7568 struct mnt_idmap
*idmap
;
7572 fp
= ksmbd_lookup_fd_fast(work
, id
);
7575 idmap
= file_mnt_idmap(fp
->filp
);
7577 old_fattr
= fp
->f_ci
->m_fattr
;
7578 if (sparse
->SetSparse
)
7579 fp
->f_ci
->m_fattr
|= FILE_ATTRIBUTE_SPARSE_FILE_LE
;
7581 fp
->f_ci
->m_fattr
&= ~FILE_ATTRIBUTE_SPARSE_FILE_LE
;
7583 if (fp
->f_ci
->m_fattr
!= old_fattr
&&
7584 test_share_config_flag(work
->tcon
->share_conf
,
7585 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS
)) {
7586 struct xattr_dos_attrib da
;
7588 ret
= ksmbd_vfs_get_dos_attrib_xattr(idmap
,
7589 fp
->filp
->f_path
.dentry
, &da
);
7593 da
.attr
= le32_to_cpu(fp
->f_ci
->m_fattr
);
7594 ret
= ksmbd_vfs_set_dos_attrib_xattr(idmap
,
7598 fp
->f_ci
->m_fattr
= old_fattr
;
7602 ksmbd_fd_put(work
, fp
);
7606 static int fsctl_request_resume_key(struct ksmbd_work
*work
,
7607 struct smb2_ioctl_req
*req
,
7608 struct resume_key_ioctl_rsp
*key_rsp
)
7610 struct ksmbd_file
*fp
;
7612 fp
= ksmbd_lookup_fd_slow(work
, req
->VolatileFileId
, req
->PersistentFileId
);
7616 memset(key_rsp
, 0, sizeof(*key_rsp
));
7617 key_rsp
->ResumeKey
[0] = req
->VolatileFileId
;
7618 key_rsp
->ResumeKey
[1] = req
->PersistentFileId
;
7619 ksmbd_fd_put(work
, fp
);
7625 * smb2_ioctl() - handler for smb2 ioctl command
7626 * @work: smb work containing ioctl command buffer
7628 * Return: 0 on success, otherwise error
7630 int smb2_ioctl(struct ksmbd_work
*work
)
7632 struct smb2_ioctl_req
*req
;
7633 struct smb2_ioctl_rsp
*rsp
;
7634 unsigned int cnt_code
, nbytes
= 0, out_buf_len
, in_buf_len
;
7635 u64 id
= KSMBD_NO_FID
;
7636 struct ksmbd_conn
*conn
= work
->conn
;
7639 if (work
->next_smb2_rcv_hdr_off
) {
7640 req
= ksmbd_req_buf_next(work
);
7641 rsp
= ksmbd_resp_buf_next(work
);
7642 if (!has_file_id(req
->VolatileFileId
)) {
7643 ksmbd_debug(SMB
, "Compound request set FID = %llu\n",
7644 work
->compound_fid
);
7645 id
= work
->compound_fid
;
7648 req
= smb2_get_msg(work
->request_buf
);
7649 rsp
= smb2_get_msg(work
->response_buf
);
7652 if (!has_file_id(id
))
7653 id
= req
->VolatileFileId
;
7655 if (req
->Flags
!= cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL
)) {
7656 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
7660 cnt_code
= le32_to_cpu(req
->CtlCode
);
7661 ret
= smb2_calc_max_out_buf_len(work
, 48,
7662 le32_to_cpu(req
->MaxOutputResponse
));
7664 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7667 out_buf_len
= (unsigned int)ret
;
7668 in_buf_len
= le32_to_cpu(req
->InputCount
);
7671 case FSCTL_DFS_GET_REFERRALS
:
7672 case FSCTL_DFS_GET_REFERRALS_EX
:
7673 /* Not support DFS yet */
7674 rsp
->hdr
.Status
= STATUS_FS_DRIVER_REQUIRED
;
7676 case FSCTL_CREATE_OR_GET_OBJECT_ID
:
7678 struct file_object_buf_type1_ioctl_rsp
*obj_buf
;
7680 nbytes
= sizeof(struct file_object_buf_type1_ioctl_rsp
);
7681 obj_buf
= (struct file_object_buf_type1_ioctl_rsp
*)
7685 * TODO: This is dummy implementation to pass smbtorture
7686 * Need to check correct response later
7688 memset(obj_buf
->ObjectId
, 0x0, 16);
7689 memset(obj_buf
->BirthVolumeId
, 0x0, 16);
7690 memset(obj_buf
->BirthObjectId
, 0x0, 16);
7691 memset(obj_buf
->DomainId
, 0x0, 16);
7695 case FSCTL_PIPE_TRANSCEIVE
:
7696 out_buf_len
= min_t(u32
, KSMBD_IPC_MAX_PAYLOAD
, out_buf_len
);
7697 nbytes
= fsctl_pipe_transceive(work
, id
, out_buf_len
, req
, rsp
);
7699 case FSCTL_VALIDATE_NEGOTIATE_INFO
:
7700 if (conn
->dialect
< SMB30_PROT_ID
) {
7705 if (in_buf_len
< offsetof(struct validate_negotiate_info_req
,
7711 if (out_buf_len
< sizeof(struct validate_negotiate_info_rsp
)) {
7716 ret
= fsctl_validate_negotiate_info(conn
,
7717 (struct validate_negotiate_info_req
*)&req
->Buffer
[0],
7718 (struct validate_negotiate_info_rsp
*)&rsp
->Buffer
[0],
7723 nbytes
= sizeof(struct validate_negotiate_info_rsp
);
7724 rsp
->PersistentFileId
= SMB2_NO_FID
;
7725 rsp
->VolatileFileId
= SMB2_NO_FID
;
7727 case FSCTL_QUERY_NETWORK_INTERFACE_INFO
:
7728 ret
= fsctl_query_iface_info_ioctl(conn
, rsp
, out_buf_len
);
7733 case FSCTL_REQUEST_RESUME_KEY
:
7734 if (out_buf_len
< sizeof(struct resume_key_ioctl_rsp
)) {
7739 ret
= fsctl_request_resume_key(work
, req
,
7740 (struct resume_key_ioctl_rsp
*)&rsp
->Buffer
[0]);
7743 rsp
->PersistentFileId
= req
->PersistentFileId
;
7744 rsp
->VolatileFileId
= req
->VolatileFileId
;
7745 nbytes
= sizeof(struct resume_key_ioctl_rsp
);
7747 case FSCTL_COPYCHUNK
:
7748 case FSCTL_COPYCHUNK_WRITE
:
7749 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
7751 "User does not have write permission\n");
7756 if (in_buf_len
< sizeof(struct copychunk_ioctl_req
)) {
7761 if (out_buf_len
< sizeof(struct copychunk_ioctl_rsp
)) {
7766 nbytes
= sizeof(struct copychunk_ioctl_rsp
);
7767 rsp
->VolatileFileId
= req
->VolatileFileId
;
7768 rsp
->PersistentFileId
= req
->PersistentFileId
;
7769 fsctl_copychunk(work
,
7770 (struct copychunk_ioctl_req
*)&req
->Buffer
[0],
7771 le32_to_cpu(req
->CtlCode
),
7772 le32_to_cpu(req
->InputCount
),
7773 req
->VolatileFileId
,
7774 req
->PersistentFileId
,
7777 case FSCTL_SET_SPARSE
:
7778 if (in_buf_len
< sizeof(struct file_sparse
)) {
7783 ret
= fsctl_set_sparse(work
, id
,
7784 (struct file_sparse
*)&req
->Buffer
[0]);
7788 case FSCTL_SET_ZERO_DATA
:
7790 struct file_zero_data_information
*zero_data
;
7791 struct ksmbd_file
*fp
;
7792 loff_t off
, len
, bfz
;
7794 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
7796 "User does not have write permission\n");
7801 if (in_buf_len
< sizeof(struct file_zero_data_information
)) {
7807 (struct file_zero_data_information
*)&req
->Buffer
[0];
7809 off
= le64_to_cpu(zero_data
->FileOffset
);
7810 bfz
= le64_to_cpu(zero_data
->BeyondFinalZero
);
7811 if (off
< 0 || bfz
< 0 || off
> bfz
) {
7818 fp
= ksmbd_lookup_fd_fast(work
, id
);
7824 ret
= ksmbd_vfs_zero_data(work
, fp
, off
, len
);
7825 ksmbd_fd_put(work
, fp
);
7831 case FSCTL_QUERY_ALLOCATED_RANGES
:
7832 if (in_buf_len
< sizeof(struct file_allocated_range_buffer
)) {
7837 ret
= fsctl_query_allocated_ranges(work
, id
,
7838 (struct file_allocated_range_buffer
*)&req
->Buffer
[0],
7839 (struct file_allocated_range_buffer
*)&rsp
->Buffer
[0],
7841 sizeof(struct file_allocated_range_buffer
), &nbytes
);
7842 if (ret
== -E2BIG
) {
7843 rsp
->hdr
.Status
= STATUS_BUFFER_OVERFLOW
;
7844 } else if (ret
< 0) {
7849 nbytes
*= sizeof(struct file_allocated_range_buffer
);
7851 case FSCTL_GET_REPARSE_POINT
:
7853 struct reparse_data_buffer
*reparse_ptr
;
7854 struct ksmbd_file
*fp
;
7856 reparse_ptr
= (struct reparse_data_buffer
*)&rsp
->Buffer
[0];
7857 fp
= ksmbd_lookup_fd_fast(work
, id
);
7859 pr_err("not found fp!!\n");
7864 reparse_ptr
->ReparseTag
=
7865 smb2_get_reparse_tag_special_file(file_inode(fp
->filp
)->i_mode
);
7866 reparse_ptr
->ReparseDataLength
= 0;
7867 ksmbd_fd_put(work
, fp
);
7868 nbytes
= sizeof(struct reparse_data_buffer
);
7871 case FSCTL_DUPLICATE_EXTENTS_TO_FILE
:
7873 struct ksmbd_file
*fp_in
, *fp_out
= NULL
;
7874 struct duplicate_extents_to_file
*dup_ext
;
7875 loff_t src_off
, dst_off
, length
, cloned
;
7877 if (in_buf_len
< sizeof(struct duplicate_extents_to_file
)) {
7882 dup_ext
= (struct duplicate_extents_to_file
*)&req
->Buffer
[0];
7884 fp_in
= ksmbd_lookup_fd_slow(work
, dup_ext
->VolatileFileHandle
,
7885 dup_ext
->PersistentFileHandle
);
7887 pr_err("not found file handle in duplicate extent to file\n");
7892 fp_out
= ksmbd_lookup_fd_fast(work
, id
);
7894 pr_err("not found fp\n");
7899 src_off
= le64_to_cpu(dup_ext
->SourceFileOffset
);
7900 dst_off
= le64_to_cpu(dup_ext
->TargetFileOffset
);
7901 length
= le64_to_cpu(dup_ext
->ByteCount
);
7903 * XXX: It is not clear if FSCTL_DUPLICATE_EXTENTS_TO_FILE
7904 * should fall back to vfs_copy_file_range(). This could be
7905 * beneficial when re-exporting nfs/smb mount, but note that
7906 * this can result in partial copy that returns an error status.
7907 * If/when FSCTL_DUPLICATE_EXTENTS_TO_FILE_EX is implemented,
7908 * fall back to vfs_copy_file_range(), should be avoided when
7909 * the flag DUPLICATE_EXTENTS_DATA_EX_SOURCE_ATOMIC is set.
7911 cloned
= vfs_clone_file_range(fp_in
->filp
, src_off
,
7912 fp_out
->filp
, dst_off
, length
, 0);
7913 if (cloned
== -EXDEV
|| cloned
== -EOPNOTSUPP
) {
7916 } else if (cloned
!= length
) {
7917 cloned
= vfs_copy_file_range(fp_in
->filp
, src_off
,
7918 fp_out
->filp
, dst_off
,
7920 if (cloned
!= length
) {
7929 ksmbd_fd_put(work
, fp_in
);
7930 ksmbd_fd_put(work
, fp_out
);
7936 ksmbd_debug(SMB
, "not implemented yet ioctl command 0x%x\n",
7942 rsp
->CtlCode
= cpu_to_le32(cnt_code
);
7943 rsp
->InputCount
= cpu_to_le32(0);
7944 rsp
->InputOffset
= cpu_to_le32(112);
7945 rsp
->OutputOffset
= cpu_to_le32(112);
7946 rsp
->OutputCount
= cpu_to_le32(nbytes
);
7947 rsp
->StructureSize
= cpu_to_le16(49);
7948 rsp
->Reserved
= cpu_to_le16(0);
7949 rsp
->Flags
= cpu_to_le32(0);
7950 rsp
->Reserved2
= cpu_to_le32(0);
7951 ret
= ksmbd_iov_pin_rsp(work
, rsp
, sizeof(struct smb2_ioctl_rsp
) + nbytes
);
7957 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
7958 else if (ret
== -ENOENT
)
7959 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
7960 else if (ret
== -EOPNOTSUPP
)
7961 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
7962 else if (ret
== -ENOSPC
)
7963 rsp
->hdr
.Status
= STATUS_BUFFER_TOO_SMALL
;
7964 else if (ret
< 0 || rsp
->hdr
.Status
== 0)
7965 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7966 smb2_set_err_rsp(work
);
7971 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7972 * @work: smb work containing oplock break command buffer
7976 static void smb20_oplock_break_ack(struct ksmbd_work
*work
)
7978 struct smb2_oplock_break
*req
;
7979 struct smb2_oplock_break
*rsp
;
7980 struct ksmbd_file
*fp
;
7981 struct oplock_info
*opinfo
= NULL
;
7984 u64 volatile_id
, persistent_id
;
7985 char req_oplevel
= 0, rsp_oplevel
= 0;
7986 unsigned int oplock_change_type
;
7988 WORK_BUFFERS(work
, req
, rsp
);
7990 volatile_id
= req
->VolatileFid
;
7991 persistent_id
= req
->PersistentFid
;
7992 req_oplevel
= req
->OplockLevel
;
7993 ksmbd_debug(OPLOCK
, "v_id %llu, p_id %llu request oplock level %d\n",
7994 volatile_id
, persistent_id
, req_oplevel
);
7996 fp
= ksmbd_lookup_fd_slow(work
, volatile_id
, persistent_id
);
7998 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
7999 smb2_set_err_rsp(work
);
8003 opinfo
= opinfo_get(fp
);
8005 pr_err("unexpected null oplock_info\n");
8006 rsp
->hdr
.Status
= STATUS_INVALID_OPLOCK_PROTOCOL
;
8007 smb2_set_err_rsp(work
);
8008 ksmbd_fd_put(work
, fp
);
8012 if (opinfo
->level
== SMB2_OPLOCK_LEVEL_NONE
) {
8013 rsp
->hdr
.Status
= STATUS_INVALID_OPLOCK_PROTOCOL
;
8017 if (opinfo
->op_state
== OPLOCK_STATE_NONE
) {
8018 ksmbd_debug(SMB
, "unexpected oplock state 0x%x\n", opinfo
->op_state
);
8019 rsp
->hdr
.Status
= STATUS_UNSUCCESSFUL
;
8023 if ((opinfo
->level
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
||
8024 opinfo
->level
== SMB2_OPLOCK_LEVEL_BATCH
) &&
8025 (req_oplevel
!= SMB2_OPLOCK_LEVEL_II
&&
8026 req_oplevel
!= SMB2_OPLOCK_LEVEL_NONE
)) {
8027 err
= STATUS_INVALID_OPLOCK_PROTOCOL
;
8028 oplock_change_type
= OPLOCK_WRITE_TO_NONE
;
8029 } else if (opinfo
->level
== SMB2_OPLOCK_LEVEL_II
&&
8030 req_oplevel
!= SMB2_OPLOCK_LEVEL_NONE
) {
8031 err
= STATUS_INVALID_OPLOCK_PROTOCOL
;
8032 oplock_change_type
= OPLOCK_READ_TO_NONE
;
8033 } else if (req_oplevel
== SMB2_OPLOCK_LEVEL_II
||
8034 req_oplevel
== SMB2_OPLOCK_LEVEL_NONE
) {
8035 err
= STATUS_INVALID_DEVICE_STATE
;
8036 if ((opinfo
->level
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
||
8037 opinfo
->level
== SMB2_OPLOCK_LEVEL_BATCH
) &&
8038 req_oplevel
== SMB2_OPLOCK_LEVEL_II
) {
8039 oplock_change_type
= OPLOCK_WRITE_TO_READ
;
8040 } else if ((opinfo
->level
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
||
8041 opinfo
->level
== SMB2_OPLOCK_LEVEL_BATCH
) &&
8042 req_oplevel
== SMB2_OPLOCK_LEVEL_NONE
) {
8043 oplock_change_type
= OPLOCK_WRITE_TO_NONE
;
8044 } else if (opinfo
->level
== SMB2_OPLOCK_LEVEL_II
&&
8045 req_oplevel
== SMB2_OPLOCK_LEVEL_NONE
) {
8046 oplock_change_type
= OPLOCK_READ_TO_NONE
;
8048 oplock_change_type
= 0;
8051 oplock_change_type
= 0;
8054 switch (oplock_change_type
) {
8055 case OPLOCK_WRITE_TO_READ
:
8056 ret
= opinfo_write_to_read(opinfo
);
8057 rsp_oplevel
= SMB2_OPLOCK_LEVEL_II
;
8059 case OPLOCK_WRITE_TO_NONE
:
8060 ret
= opinfo_write_to_none(opinfo
);
8061 rsp_oplevel
= SMB2_OPLOCK_LEVEL_NONE
;
8063 case OPLOCK_READ_TO_NONE
:
8064 ret
= opinfo_read_to_none(opinfo
);
8065 rsp_oplevel
= SMB2_OPLOCK_LEVEL_NONE
;
8068 pr_err("unknown oplock change 0x%x -> 0x%x\n",
8069 opinfo
->level
, rsp_oplevel
);
8073 rsp
->hdr
.Status
= err
;
8077 opinfo
->op_state
= OPLOCK_STATE_NONE
;
8078 wake_up_interruptible_all(&opinfo
->oplock_q
);
8080 ksmbd_fd_put(work
, fp
);
8082 rsp
->StructureSize
= cpu_to_le16(24);
8083 rsp
->OplockLevel
= rsp_oplevel
;
8086 rsp
->VolatileFid
= volatile_id
;
8087 rsp
->PersistentFid
= persistent_id
;
8088 ret
= ksmbd_iov_pin_rsp(work
, rsp
, sizeof(struct smb2_oplock_break
));
8093 opinfo
->op_state
= OPLOCK_STATE_NONE
;
8094 wake_up_interruptible_all(&opinfo
->oplock_q
);
8097 ksmbd_fd_put(work
, fp
);
8098 smb2_set_err_rsp(work
);
8101 static int check_lease_state(struct lease
*lease
, __le32 req_state
)
8103 if ((lease
->new_state
==
8104 (SMB2_LEASE_READ_CACHING_LE
| SMB2_LEASE_HANDLE_CACHING_LE
)) &&
8105 !(req_state
& SMB2_LEASE_WRITE_CACHING_LE
)) {
8106 lease
->new_state
= req_state
;
8110 if (lease
->new_state
== req_state
)
8117 * smb21_lease_break_ack() - handler for smb2.1 lease break command
8118 * @work: smb work containing lease break command buffer
8122 static void smb21_lease_break_ack(struct ksmbd_work
*work
)
8124 struct ksmbd_conn
*conn
= work
->conn
;
8125 struct smb2_lease_ack
*req
;
8126 struct smb2_lease_ack
*rsp
;
8127 struct oplock_info
*opinfo
;
8130 unsigned int lease_change_type
;
8132 struct lease
*lease
;
8134 WORK_BUFFERS(work
, req
, rsp
);
8136 ksmbd_debug(OPLOCK
, "smb21 lease break, lease state(0x%x)\n",
8137 le32_to_cpu(req
->LeaseState
));
8138 opinfo
= lookup_lease_in_table(conn
, req
->LeaseKey
);
8140 ksmbd_debug(OPLOCK
, "file not opened\n");
8141 smb2_set_err_rsp(work
);
8142 rsp
->hdr
.Status
= STATUS_UNSUCCESSFUL
;
8145 lease
= opinfo
->o_lease
;
8147 if (opinfo
->op_state
== OPLOCK_STATE_NONE
) {
8148 pr_err("unexpected lease break state 0x%x\n",
8150 rsp
->hdr
.Status
= STATUS_UNSUCCESSFUL
;
8154 if (check_lease_state(lease
, req
->LeaseState
)) {
8155 rsp
->hdr
.Status
= STATUS_REQUEST_NOT_ACCEPTED
;
8157 "req lease state: 0x%x, expected state: 0x%x\n",
8158 req
->LeaseState
, lease
->new_state
);
8162 if (!atomic_read(&opinfo
->breaking_cnt
)) {
8163 rsp
->hdr
.Status
= STATUS_UNSUCCESSFUL
;
8167 /* check for bad lease state */
8168 if (req
->LeaseState
&
8169 (~(SMB2_LEASE_READ_CACHING_LE
| SMB2_LEASE_HANDLE_CACHING_LE
))) {
8170 err
= STATUS_INVALID_OPLOCK_PROTOCOL
;
8171 if (lease
->state
& SMB2_LEASE_WRITE_CACHING_LE
)
8172 lease_change_type
= OPLOCK_WRITE_TO_NONE
;
8174 lease_change_type
= OPLOCK_READ_TO_NONE
;
8175 ksmbd_debug(OPLOCK
, "handle bad lease state 0x%x -> 0x%x\n",
8176 le32_to_cpu(lease
->state
),
8177 le32_to_cpu(req
->LeaseState
));
8178 } else if (lease
->state
== SMB2_LEASE_READ_CACHING_LE
&&
8179 req
->LeaseState
!= SMB2_LEASE_NONE_LE
) {
8180 err
= STATUS_INVALID_OPLOCK_PROTOCOL
;
8181 lease_change_type
= OPLOCK_READ_TO_NONE
;
8182 ksmbd_debug(OPLOCK
, "handle bad lease state 0x%x -> 0x%x\n",
8183 le32_to_cpu(lease
->state
),
8184 le32_to_cpu(req
->LeaseState
));
8186 /* valid lease state changes */
8187 err
= STATUS_INVALID_DEVICE_STATE
;
8188 if (req
->LeaseState
== SMB2_LEASE_NONE_LE
) {
8189 if (lease
->state
& SMB2_LEASE_WRITE_CACHING_LE
)
8190 lease_change_type
= OPLOCK_WRITE_TO_NONE
;
8192 lease_change_type
= OPLOCK_READ_TO_NONE
;
8193 } else if (req
->LeaseState
& SMB2_LEASE_READ_CACHING_LE
) {
8194 if (lease
->state
& SMB2_LEASE_WRITE_CACHING_LE
)
8195 lease_change_type
= OPLOCK_WRITE_TO_READ
;
8197 lease_change_type
= OPLOCK_READ_HANDLE_TO_READ
;
8199 lease_change_type
= 0;
8203 switch (lease_change_type
) {
8204 case OPLOCK_WRITE_TO_READ
:
8205 ret
= opinfo_write_to_read(opinfo
);
8207 case OPLOCK_READ_HANDLE_TO_READ
:
8208 ret
= opinfo_read_handle_to_read(opinfo
);
8210 case OPLOCK_WRITE_TO_NONE
:
8211 ret
= opinfo_write_to_none(opinfo
);
8213 case OPLOCK_READ_TO_NONE
:
8214 ret
= opinfo_read_to_none(opinfo
);
8217 ksmbd_debug(OPLOCK
, "unknown lease change 0x%x -> 0x%x\n",
8218 le32_to_cpu(lease
->state
),
8219 le32_to_cpu(req
->LeaseState
));
8223 rsp
->hdr
.Status
= err
;
8227 lease_state
= lease
->state
;
8228 opinfo
->op_state
= OPLOCK_STATE_NONE
;
8229 wake_up_interruptible_all(&opinfo
->oplock_q
);
8230 atomic_dec(&opinfo
->breaking_cnt
);
8231 wake_up_interruptible_all(&opinfo
->oplock_brk
);
8234 rsp
->StructureSize
= cpu_to_le16(36);
8237 memcpy(rsp
->LeaseKey
, req
->LeaseKey
, 16);
8238 rsp
->LeaseState
= lease_state
;
8239 rsp
->LeaseDuration
= 0;
8240 ret
= ksmbd_iov_pin_rsp(work
, rsp
, sizeof(struct smb2_lease_ack
));
8245 wake_up_interruptible_all(&opinfo
->oplock_q
);
8246 atomic_dec(&opinfo
->breaking_cnt
);
8247 wake_up_interruptible_all(&opinfo
->oplock_brk
);
8250 smb2_set_err_rsp(work
);
8254 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
8255 * @work: smb work containing oplock/lease break command buffer
8259 int smb2_oplock_break(struct ksmbd_work
*work
)
8261 struct smb2_oplock_break
*req
;
8262 struct smb2_oplock_break
*rsp
;
8264 WORK_BUFFERS(work
, req
, rsp
);
8266 switch (le16_to_cpu(req
->StructureSize
)) {
8267 case OP_BREAK_STRUCT_SIZE_20
:
8268 smb20_oplock_break_ack(work
);
8270 case OP_BREAK_STRUCT_SIZE_21
:
8271 smb21_lease_break_ack(work
);
8274 ksmbd_debug(OPLOCK
, "invalid break cmd %d\n",
8275 le16_to_cpu(req
->StructureSize
));
8276 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
8277 smb2_set_err_rsp(work
);
8284 * smb2_notify() - handler for smb2 notify request
8285 * @work: smb work containing notify command buffer
8289 int smb2_notify(struct ksmbd_work
*work
)
8291 struct smb2_change_notify_req
*req
;
8292 struct smb2_change_notify_rsp
*rsp
;
8294 WORK_BUFFERS(work
, req
, rsp
);
8296 if (work
->next_smb2_rcv_hdr_off
&& req
->hdr
.NextCommand
) {
8297 rsp
->hdr
.Status
= STATUS_INTERNAL_ERROR
;
8298 smb2_set_err_rsp(work
);
8302 smb2_set_err_rsp(work
);
8303 rsp
->hdr
.Status
= STATUS_NOT_IMPLEMENTED
;
8308 * smb2_is_sign_req() - handler for checking packet signing status
8309 * @work: smb work containing notify command buffer
8310 * @command: SMB2 command id
8312 * Return: true if packed is signed, false otherwise
8314 bool smb2_is_sign_req(struct ksmbd_work
*work
, unsigned int command
)
8316 struct smb2_hdr
*rcv_hdr2
= smb2_get_msg(work
->request_buf
);
8318 if ((rcv_hdr2
->Flags
& SMB2_FLAGS_SIGNED
) &&
8319 command
!= SMB2_NEGOTIATE_HE
&&
8320 command
!= SMB2_SESSION_SETUP_HE
&&
8321 command
!= SMB2_OPLOCK_BREAK_HE
)
8328 * smb2_check_sign_req() - handler for req packet sign processing
8329 * @work: smb work containing notify command buffer
8331 * Return: 1 on success, 0 otherwise
8333 int smb2_check_sign_req(struct ksmbd_work
*work
)
8335 struct smb2_hdr
*hdr
;
8336 char signature_req
[SMB2_SIGNATURE_SIZE
];
8337 char signature
[SMB2_HMACSHA256_SIZE
];
8341 hdr
= smb2_get_msg(work
->request_buf
);
8342 if (work
->next_smb2_rcv_hdr_off
)
8343 hdr
= ksmbd_req_buf_next(work
);
8345 if (!hdr
->NextCommand
&& !work
->next_smb2_rcv_hdr_off
)
8346 len
= get_rfc1002_len(work
->request_buf
);
8347 else if (hdr
->NextCommand
)
8348 len
= le32_to_cpu(hdr
->NextCommand
);
8350 len
= get_rfc1002_len(work
->request_buf
) -
8351 work
->next_smb2_rcv_hdr_off
;
8353 memcpy(signature_req
, hdr
->Signature
, SMB2_SIGNATURE_SIZE
);
8354 memset(hdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
8356 iov
[0].iov_base
= (char *)&hdr
->ProtocolId
;
8357 iov
[0].iov_len
= len
;
8359 if (ksmbd_sign_smb2_pdu(work
->conn
, work
->sess
->sess_key
, iov
, 1,
8363 if (memcmp(signature
, signature_req
, SMB2_SIGNATURE_SIZE
)) {
8364 pr_err("bad smb2 signature\n");
8372 * smb2_set_sign_rsp() - handler for rsp packet sign processing
8373 * @work: smb work containing notify command buffer
8376 void smb2_set_sign_rsp(struct ksmbd_work
*work
)
8378 struct smb2_hdr
*hdr
;
8379 char signature
[SMB2_HMACSHA256_SIZE
];
8383 hdr
= ksmbd_resp_buf_curr(work
);
8384 hdr
->Flags
|= SMB2_FLAGS_SIGNED
;
8385 memset(hdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
8387 if (hdr
->Command
== SMB2_READ
) {
8388 iov
= &work
->iov
[work
->iov_idx
- 1];
8391 iov
= &work
->iov
[work
->iov_idx
];
8394 if (!ksmbd_sign_smb2_pdu(work
->conn
, work
->sess
->sess_key
, iov
, n_vec
,
8396 memcpy(hdr
->Signature
, signature
, SMB2_SIGNATURE_SIZE
);
8400 * smb3_check_sign_req() - handler for req packet sign processing
8401 * @work: smb work containing notify command buffer
8403 * Return: 1 on success, 0 otherwise
8405 int smb3_check_sign_req(struct ksmbd_work
*work
)
8407 struct ksmbd_conn
*conn
= work
->conn
;
8409 struct smb2_hdr
*hdr
;
8410 struct channel
*chann
;
8411 char signature_req
[SMB2_SIGNATURE_SIZE
];
8412 char signature
[SMB2_CMACAES_SIZE
];
8416 hdr
= smb2_get_msg(work
->request_buf
);
8417 if (work
->next_smb2_rcv_hdr_off
)
8418 hdr
= ksmbd_req_buf_next(work
);
8420 if (!hdr
->NextCommand
&& !work
->next_smb2_rcv_hdr_off
)
8421 len
= get_rfc1002_len(work
->request_buf
);
8422 else if (hdr
->NextCommand
)
8423 len
= le32_to_cpu(hdr
->NextCommand
);
8425 len
= get_rfc1002_len(work
->request_buf
) -
8426 work
->next_smb2_rcv_hdr_off
;
8428 if (le16_to_cpu(hdr
->Command
) == SMB2_SESSION_SETUP_HE
) {
8429 signing_key
= work
->sess
->smb3signingkey
;
8431 chann
= lookup_chann_list(work
->sess
, conn
);
8435 signing_key
= chann
->smb3signingkey
;
8439 pr_err("SMB3 signing key is not generated\n");
8443 memcpy(signature_req
, hdr
->Signature
, SMB2_SIGNATURE_SIZE
);
8444 memset(hdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
8445 iov
[0].iov_base
= (char *)&hdr
->ProtocolId
;
8446 iov
[0].iov_len
= len
;
8448 if (ksmbd_sign_smb3_pdu(conn
, signing_key
, iov
, 1, signature
))
8451 if (memcmp(signature
, signature_req
, SMB2_SIGNATURE_SIZE
)) {
8452 pr_err("bad smb2 signature\n");
8460 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8461 * @work: smb work containing notify command buffer
8464 void smb3_set_sign_rsp(struct ksmbd_work
*work
)
8466 struct ksmbd_conn
*conn
= work
->conn
;
8467 struct smb2_hdr
*hdr
;
8468 struct channel
*chann
;
8469 char signature
[SMB2_CMACAES_SIZE
];
8474 hdr
= ksmbd_resp_buf_curr(work
);
8476 if (conn
->binding
== false &&
8477 le16_to_cpu(hdr
->Command
) == SMB2_SESSION_SETUP_HE
) {
8478 signing_key
= work
->sess
->smb3signingkey
;
8480 chann
= lookup_chann_list(work
->sess
, work
->conn
);
8484 signing_key
= chann
->smb3signingkey
;
8490 hdr
->Flags
|= SMB2_FLAGS_SIGNED
;
8491 memset(hdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
8493 if (hdr
->Command
== SMB2_READ
) {
8494 iov
= &work
->iov
[work
->iov_idx
- 1];
8497 iov
= &work
->iov
[work
->iov_idx
];
8500 if (!ksmbd_sign_smb3_pdu(conn
, signing_key
, iov
, n_vec
,
8502 memcpy(hdr
->Signature
, signature
, SMB2_SIGNATURE_SIZE
);
8506 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8507 * @work: smb work containing response buffer
8510 void smb3_preauth_hash_rsp(struct ksmbd_work
*work
)
8512 struct ksmbd_conn
*conn
= work
->conn
;
8513 struct ksmbd_session
*sess
= work
->sess
;
8514 struct smb2_hdr
*req
, *rsp
;
8516 if (conn
->dialect
!= SMB311_PROT_ID
)
8519 WORK_BUFFERS(work
, req
, rsp
);
8521 if (le16_to_cpu(req
->Command
) == SMB2_NEGOTIATE_HE
&&
8523 ksmbd_gen_preauth_integrity_hash(conn
, work
->response_buf
,
8524 conn
->preauth_info
->Preauth_HashValue
);
8526 if (le16_to_cpu(rsp
->Command
) == SMB2_SESSION_SETUP_HE
&& sess
) {
8529 if (conn
->binding
) {
8530 struct preauth_session
*preauth_sess
;
8532 preauth_sess
= ksmbd_preauth_session_lookup(conn
, sess
->id
);
8535 hash_value
= preauth_sess
->Preauth_HashValue
;
8537 hash_value
= sess
->Preauth_HashValue
;
8541 ksmbd_gen_preauth_integrity_hash(conn
, work
->response_buf
,
8546 static void fill_transform_hdr(void *tr_buf
, char *old_buf
, __le16 cipher_type
)
8548 struct smb2_transform_hdr
*tr_hdr
= tr_buf
+ 4;
8549 struct smb2_hdr
*hdr
= smb2_get_msg(old_buf
);
8550 unsigned int orig_len
= get_rfc1002_len(old_buf
);
8552 /* tr_buf must be cleared by the caller */
8553 tr_hdr
->ProtocolId
= SMB2_TRANSFORM_PROTO_NUM
;
8554 tr_hdr
->OriginalMessageSize
= cpu_to_le32(orig_len
);
8555 tr_hdr
->Flags
= cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED
);
8556 if (cipher_type
== SMB2_ENCRYPTION_AES128_GCM
||
8557 cipher_type
== SMB2_ENCRYPTION_AES256_GCM
)
8558 get_random_bytes(&tr_hdr
->Nonce
, SMB3_AES_GCM_NONCE
);
8560 get_random_bytes(&tr_hdr
->Nonce
, SMB3_AES_CCM_NONCE
);
8561 memcpy(&tr_hdr
->SessionId
, &hdr
->SessionId
, 8);
8562 inc_rfc1001_len(tr_buf
, sizeof(struct smb2_transform_hdr
));
8563 inc_rfc1001_len(tr_buf
, orig_len
);
8566 int smb3_encrypt_resp(struct ksmbd_work
*work
)
8568 struct kvec
*iov
= work
->iov
;
8572 tr_buf
= kzalloc(sizeof(struct smb2_transform_hdr
) + 4, GFP_KERNEL
);
8576 /* fill transform header */
8577 fill_transform_hdr(tr_buf
, work
->response_buf
, work
->conn
->cipher_type
);
8579 iov
[0].iov_base
= tr_buf
;
8580 iov
[0].iov_len
= sizeof(struct smb2_transform_hdr
) + 4;
8581 work
->tr_buf
= tr_buf
;
8583 return ksmbd_crypt_message(work
, iov
, work
->iov_idx
+ 1, 1);
8586 bool smb3_is_transform_hdr(void *buf
)
8588 struct smb2_transform_hdr
*trhdr
= smb2_get_msg(buf
);
8590 return trhdr
->ProtocolId
== SMB2_TRANSFORM_PROTO_NUM
;
8593 int smb3_decrypt_req(struct ksmbd_work
*work
)
8595 struct ksmbd_session
*sess
;
8596 char *buf
= work
->request_buf
;
8597 unsigned int pdu_length
= get_rfc1002_len(buf
);
8599 int buf_data_size
= pdu_length
- sizeof(struct smb2_transform_hdr
);
8600 struct smb2_transform_hdr
*tr_hdr
= smb2_get_msg(buf
);
8603 if (pdu_length
< sizeof(struct smb2_transform_hdr
) ||
8604 buf_data_size
< sizeof(struct smb2_hdr
)) {
8605 pr_err("Transform message is too small (%u)\n",
8607 return -ECONNABORTED
;
8610 if (buf_data_size
< le32_to_cpu(tr_hdr
->OriginalMessageSize
)) {
8611 pr_err("Transform message is broken\n");
8612 return -ECONNABORTED
;
8615 sess
= ksmbd_session_lookup_all(work
->conn
, le64_to_cpu(tr_hdr
->SessionId
));
8617 pr_err("invalid session id(%llx) in transform header\n",
8618 le64_to_cpu(tr_hdr
->SessionId
));
8619 return -ECONNABORTED
;
8622 iov
[0].iov_base
= buf
;
8623 iov
[0].iov_len
= sizeof(struct smb2_transform_hdr
) + 4;
8624 iov
[1].iov_base
= buf
+ sizeof(struct smb2_transform_hdr
) + 4;
8625 iov
[1].iov_len
= buf_data_size
;
8626 rc
= ksmbd_crypt_message(work
, iov
, 2, 0);
8630 memmove(buf
+ 4, iov
[1].iov_base
, buf_data_size
);
8631 *(__be32
*)buf
= cpu_to_be32(buf_data_size
);
8636 bool smb3_11_final_sess_setup_resp(struct ksmbd_work
*work
)
8638 struct ksmbd_conn
*conn
= work
->conn
;
8639 struct ksmbd_session
*sess
= work
->sess
;
8640 struct smb2_hdr
*rsp
= smb2_get_msg(work
->response_buf
);
8642 if (conn
->dialect
< SMB30_PROT_ID
)
8645 if (work
->next_smb2_rcv_hdr_off
)
8646 rsp
= ksmbd_resp_buf_next(work
);
8648 if (le16_to_cpu(rsp
->Command
) == SMB2_SESSION_SETUP_HE
&&
8649 sess
->user
&& !user_guest(sess
->user
) &&
8650 rsp
->Status
== STATUS_SUCCESS
)