1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 #include <linux/inetdevice.h>
8 #include <net/addrconf.h>
9 #include <linux/syscalls.h>
10 #include <linux/namei.h>
11 #include <linux/statfs.h>
12 #include <linux/ethtool.h>
13 #include <linux/falloc.h>
23 #include "connection.h"
24 #include "transport_ipc.h"
25 #include "transport_rdma.h"
27 #include "vfs_cache.h"
31 #include "smb_common.h"
32 #include "smbstatus.h"
33 #include "ksmbd_work.h"
34 #include "mgmt/user_config.h"
35 #include "mgmt/share_config.h"
36 #include "mgmt/tree_connect.h"
37 #include "mgmt/user_session.h"
38 #include "mgmt/ksmbd_ida.h"
41 static void __wbuf(struct ksmbd_work
*work
, void **req
, void **rsp
)
43 if (work
->next_smb2_rcv_hdr_off
) {
44 *req
= ksmbd_req_buf_next(work
);
45 *rsp
= ksmbd_resp_buf_next(work
);
47 *req
= work
->request_buf
;
48 *rsp
= work
->response_buf
;
52 #define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs))
55 * check_session_id() - check for valid session id in smb header
56 * @conn: connection instance
57 * @id: session id from smb header
59 * Return: 1 if valid session id, otherwise 0
61 static inline bool check_session_id(struct ksmbd_conn
*conn
, u64 id
)
63 struct ksmbd_session
*sess
;
65 if (id
== 0 || id
== -1)
68 sess
= ksmbd_session_lookup_all(conn
, id
);
71 pr_err("Invalid user session id: %llu\n", id
);
75 struct channel
*lookup_chann_list(struct ksmbd_session
*sess
, struct ksmbd_conn
*conn
)
77 struct channel
*chann
;
79 list_for_each_entry(chann
, &sess
->ksmbd_chann_list
, chann_list
) {
80 if (chann
->conn
== conn
)
88 * smb2_get_ksmbd_tcon() - get tree connection information using a tree id.
91 * Return: 0 if there is a tree connection matched or these are
92 * skipable commands, otherwise error
94 int smb2_get_ksmbd_tcon(struct ksmbd_work
*work
)
96 struct smb2_hdr
*req_hdr
= work
->request_buf
;
100 if (work
->conn
->ops
->get_cmd_val(work
) == SMB2_TREE_CONNECT_HE
||
101 work
->conn
->ops
->get_cmd_val(work
) == SMB2_CANCEL_HE
||
102 work
->conn
->ops
->get_cmd_val(work
) == SMB2_LOGOFF_HE
) {
103 ksmbd_debug(SMB
, "skip to check tree connect request\n");
107 if (xa_empty(&work
->sess
->tree_conns
)) {
108 ksmbd_debug(SMB
, "NO tree connected\n");
112 tree_id
= le32_to_cpu(req_hdr
->Id
.SyncId
.TreeId
);
113 work
->tcon
= ksmbd_tree_conn_lookup(work
->sess
, tree_id
);
115 pr_err("Invalid tid %d\n", tree_id
);
123 * smb2_set_err_rsp() - set error response code on smb response
124 * @work: smb work containing response buffer
126 void smb2_set_err_rsp(struct ksmbd_work
*work
)
128 struct smb2_err_rsp
*err_rsp
;
130 if (work
->next_smb2_rcv_hdr_off
)
131 err_rsp
= ksmbd_resp_buf_next(work
);
133 err_rsp
= work
->response_buf
;
135 if (err_rsp
->hdr
.Status
!= STATUS_STOPPED_ON_SYMLINK
) {
136 err_rsp
->StructureSize
= SMB2_ERROR_STRUCTURE_SIZE2_LE
;
137 err_rsp
->ErrorContextCount
= 0;
138 err_rsp
->Reserved
= 0;
139 err_rsp
->ByteCount
= 0;
140 err_rsp
->ErrorData
[0] = 0;
141 inc_rfc1001_len(work
->response_buf
, SMB2_ERROR_STRUCTURE_SIZE2
);
146 * is_smb2_neg_cmd() - is it smb2 negotiation command
147 * @work: smb work containing smb header
149 * Return: true if smb2 negotiation command, otherwise false
151 bool is_smb2_neg_cmd(struct ksmbd_work
*work
)
153 struct smb2_hdr
*hdr
= work
->request_buf
;
155 /* is it SMB2 header ? */
156 if (hdr
->ProtocolId
!= SMB2_PROTO_NUMBER
)
159 /* make sure it is request not response message */
160 if (hdr
->Flags
& SMB2_FLAGS_SERVER_TO_REDIR
)
163 if (hdr
->Command
!= SMB2_NEGOTIATE
)
170 * is_smb2_rsp() - is it smb2 response
171 * @work: smb work containing smb response buffer
173 * Return: true if smb2 response, otherwise false
175 bool is_smb2_rsp(struct ksmbd_work
*work
)
177 struct smb2_hdr
*hdr
= work
->response_buf
;
179 /* is it SMB2 header ? */
180 if (hdr
->ProtocolId
!= SMB2_PROTO_NUMBER
)
183 /* make sure it is response not request message */
184 if (!(hdr
->Flags
& SMB2_FLAGS_SERVER_TO_REDIR
))
191 * get_smb2_cmd_val() - get smb command code from smb header
192 * @work: smb work containing smb request buffer
194 * Return: smb2 request command value
196 u16
get_smb2_cmd_val(struct ksmbd_work
*work
)
198 struct smb2_hdr
*rcv_hdr
;
200 if (work
->next_smb2_rcv_hdr_off
)
201 rcv_hdr
= ksmbd_req_buf_next(work
);
203 rcv_hdr
= work
->request_buf
;
204 return le16_to_cpu(rcv_hdr
->Command
);
208 * set_smb2_rsp_status() - set error response code on smb2 header
209 * @work: smb work containing response buffer
210 * @err: error response code
212 void set_smb2_rsp_status(struct ksmbd_work
*work
, __le32 err
)
214 struct smb2_hdr
*rsp_hdr
;
216 if (work
->next_smb2_rcv_hdr_off
)
217 rsp_hdr
= ksmbd_resp_buf_next(work
);
219 rsp_hdr
= work
->response_buf
;
220 rsp_hdr
->Status
= err
;
221 smb2_set_err_rsp(work
);
225 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command
226 * @work: smb work containing smb request buffer
228 * smb2 negotiate response is sent in reply of smb1 negotiate command for
229 * dialect auto-negotiation.
231 int init_smb2_neg_rsp(struct ksmbd_work
*work
)
233 struct smb2_hdr
*rsp_hdr
;
234 struct smb2_negotiate_rsp
*rsp
;
235 struct ksmbd_conn
*conn
= work
->conn
;
237 if (conn
->need_neg
== false)
240 rsp_hdr
= work
->response_buf
;
242 memset(rsp_hdr
, 0, sizeof(struct smb2_hdr
) + 2);
244 rsp_hdr
->smb2_buf_length
=
245 cpu_to_be32(smb2_hdr_size_no_buflen(conn
->vals
));
247 rsp_hdr
->ProtocolId
= SMB2_PROTO_NUMBER
;
248 rsp_hdr
->StructureSize
= SMB2_HEADER_STRUCTURE_SIZE
;
249 rsp_hdr
->CreditRequest
= cpu_to_le16(2);
250 rsp_hdr
->Command
= SMB2_NEGOTIATE
;
251 rsp_hdr
->Flags
= (SMB2_FLAGS_SERVER_TO_REDIR
);
252 rsp_hdr
->NextCommand
= 0;
253 rsp_hdr
->MessageId
= 0;
254 rsp_hdr
->Id
.SyncId
.ProcessId
= 0;
255 rsp_hdr
->Id
.SyncId
.TreeId
= 0;
256 rsp_hdr
->SessionId
= 0;
257 memset(rsp_hdr
->Signature
, 0, 16);
259 rsp
= work
->response_buf
;
261 WARN_ON(ksmbd_conn_good(work
));
263 rsp
->StructureSize
= cpu_to_le16(65);
264 ksmbd_debug(SMB
, "conn->dialect 0x%x\n", conn
->dialect
);
265 rsp
->DialectRevision
= cpu_to_le16(conn
->dialect
);
266 /* Not setting conn guid rsp->ServerGUID, as it
267 * not used by client for identifying connection
269 rsp
->Capabilities
= cpu_to_le32(conn
->vals
->capabilities
);
270 /* Default Max Message Size till SMB2.0, 64K*/
271 rsp
->MaxTransactSize
= cpu_to_le32(conn
->vals
->max_trans_size
);
272 rsp
->MaxReadSize
= cpu_to_le32(conn
->vals
->max_read_size
);
273 rsp
->MaxWriteSize
= cpu_to_le32(conn
->vals
->max_write_size
);
275 rsp
->SystemTime
= cpu_to_le64(ksmbd_systime());
276 rsp
->ServerStartTime
= 0;
278 rsp
->SecurityBufferOffset
= cpu_to_le16(128);
279 rsp
->SecurityBufferLength
= cpu_to_le16(AUTH_GSS_LENGTH
);
280 ksmbd_copy_gss_neg_header(((char *)(&rsp
->hdr
) +
281 sizeof(rsp
->hdr
.smb2_buf_length
)) +
282 le16_to_cpu(rsp
->SecurityBufferOffset
));
283 inc_rfc1001_len(rsp
, sizeof(struct smb2_negotiate_rsp
) -
284 sizeof(struct smb2_hdr
) - sizeof(rsp
->Buffer
) +
286 rsp
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_ENABLED_LE
;
287 if (server_conf
.signing
== KSMBD_CONFIG_OPT_MANDATORY
)
288 rsp
->SecurityMode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE
;
289 conn
->use_spnego
= true;
291 ksmbd_conn_set_need_negotiate(work
);
295 static int smb2_consume_credit_charge(struct ksmbd_work
*work
,
296 unsigned short credit_charge
)
298 struct ksmbd_conn
*conn
= work
->conn
;
299 unsigned int rsp_credits
= 1;
301 if (!conn
->total_credits
)
304 if (credit_charge
> 0)
305 rsp_credits
= credit_charge
;
307 conn
->total_credits
-= rsp_credits
;
312 * smb2_set_rsp_credits() - set number of credits in response buffer
313 * @work: smb work containing smb response buffer
315 int smb2_set_rsp_credits(struct ksmbd_work
*work
)
317 struct smb2_hdr
*req_hdr
= ksmbd_req_buf_next(work
);
318 struct smb2_hdr
*hdr
= ksmbd_resp_buf_next(work
);
319 struct ksmbd_conn
*conn
= work
->conn
;
320 unsigned short credits_requested
= le16_to_cpu(req_hdr
->CreditRequest
);
321 unsigned short credit_charge
= 1, credits_granted
= 0;
322 unsigned short aux_max
, aux_credits
, min_credits
;
323 int rsp_credit_charge
;
325 if (hdr
->Command
== SMB2_CANCEL
)
328 /* get default minimum credits by shifting maximum credits by 4 */
329 min_credits
= conn
->max_credits
>> 4;
331 if (conn
->total_credits
>= conn
->max_credits
) {
332 pr_err("Total credits overflow: %d\n", conn
->total_credits
);
333 conn
->total_credits
= min_credits
;
337 smb2_consume_credit_charge(work
, le16_to_cpu(req_hdr
->CreditCharge
));
338 if (rsp_credit_charge
< 0)
341 hdr
->CreditCharge
= cpu_to_le16(rsp_credit_charge
);
343 if (credits_requested
> 0) {
344 aux_credits
= credits_requested
- 1;
346 if (hdr
->Command
== SMB2_NEGOTIATE
)
348 aux_credits
= (aux_credits
< aux_max
) ? aux_credits
: aux_max
;
349 credits_granted
= aux_credits
+ credit_charge
;
351 /* if credits granted per client is getting bigger than default
352 * minimum credits then we should wrap it up within the limits.
354 if ((conn
->total_credits
+ credits_granted
) > min_credits
)
355 credits_granted
= min_credits
- conn
->total_credits
;
357 * TODO: Need to adjuct CreditRequest value according to
360 } else if (conn
->total_credits
== 0) {
364 conn
->total_credits
+= credits_granted
;
365 work
->credits_granted
+= credits_granted
;
367 if (!req_hdr
->NextCommand
) {
368 /* Update CreditRequest in last request */
369 hdr
->CreditRequest
= cpu_to_le16(work
->credits_granted
);
373 "credits: requested[%d] granted[%d] total_granted[%d]\n",
374 credits_requested
, credits_granted
,
375 conn
->total_credits
);
380 * init_chained_smb2_rsp() - initialize smb2 chained response
381 * @work: smb work containing smb response buffer
383 static void init_chained_smb2_rsp(struct ksmbd_work
*work
)
385 struct smb2_hdr
*req
= ksmbd_req_buf_next(work
);
386 struct smb2_hdr
*rsp
= ksmbd_resp_buf_next(work
);
387 struct smb2_hdr
*rsp_hdr
;
388 struct smb2_hdr
*rcv_hdr
;
389 int next_hdr_offset
= 0;
392 /* Len of this response = updated RFC len - offset of previous cmd
393 * in the compound rsp
396 /* Storing the current local FID which may be needed by subsequent
397 * command in the compound request
399 if (req
->Command
== SMB2_CREATE
&& rsp
->Status
== STATUS_SUCCESS
) {
401 le64_to_cpu(((struct smb2_create_rsp
*)rsp
)->
403 work
->compound_pfid
=
404 le64_to_cpu(((struct smb2_create_rsp
*)rsp
)->
406 work
->compound_sid
= le64_to_cpu(rsp
->SessionId
);
409 len
= get_rfc1002_len(work
->response_buf
) - work
->next_smb2_rsp_hdr_off
;
410 next_hdr_offset
= le32_to_cpu(req
->NextCommand
);
412 new_len
= ALIGN(len
, 8);
413 inc_rfc1001_len(work
->response_buf
, ((sizeof(struct smb2_hdr
) - 4)
415 rsp
->NextCommand
= cpu_to_le32(new_len
);
417 work
->next_smb2_rcv_hdr_off
+= next_hdr_offset
;
418 work
->next_smb2_rsp_hdr_off
+= new_len
;
420 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
421 new_len
, work
->next_smb2_rcv_hdr_off
,
422 work
->next_smb2_rsp_hdr_off
);
424 rsp_hdr
= ksmbd_resp_buf_next(work
);
425 rcv_hdr
= ksmbd_req_buf_next(work
);
427 if (!(rcv_hdr
->Flags
& SMB2_FLAGS_RELATED_OPERATIONS
)) {
428 ksmbd_debug(SMB
, "related flag should be set\n");
429 work
->compound_fid
= KSMBD_NO_FID
;
430 work
->compound_pfid
= KSMBD_NO_FID
;
432 memset((char *)rsp_hdr
+ 4, 0, sizeof(struct smb2_hdr
) + 2);
433 rsp_hdr
->ProtocolId
= SMB2_PROTO_NUMBER
;
434 rsp_hdr
->StructureSize
= SMB2_HEADER_STRUCTURE_SIZE
;
435 rsp_hdr
->Command
= rcv_hdr
->Command
;
438 * Message is response. We don't grant oplock yet.
440 rsp_hdr
->Flags
= (SMB2_FLAGS_SERVER_TO_REDIR
|
441 SMB2_FLAGS_RELATED_OPERATIONS
);
442 rsp_hdr
->NextCommand
= 0;
443 rsp_hdr
->MessageId
= rcv_hdr
->MessageId
;
444 rsp_hdr
->Id
.SyncId
.ProcessId
= rcv_hdr
->Id
.SyncId
.ProcessId
;
445 rsp_hdr
->Id
.SyncId
.TreeId
= rcv_hdr
->Id
.SyncId
.TreeId
;
446 rsp_hdr
->SessionId
= rcv_hdr
->SessionId
;
447 memcpy(rsp_hdr
->Signature
, rcv_hdr
->Signature
, 16);
451 * is_chained_smb2_message() - check for chained command
452 * @work: smb work containing smb request buffer
454 * Return: true if chained request, otherwise false
456 bool is_chained_smb2_message(struct ksmbd_work
*work
)
458 struct smb2_hdr
*hdr
= work
->request_buf
;
459 unsigned int len
, next_cmd
;
461 if (hdr
->ProtocolId
!= SMB2_PROTO_NUMBER
)
464 hdr
= ksmbd_req_buf_next(work
);
465 next_cmd
= le32_to_cpu(hdr
->NextCommand
);
467 if ((u64
)work
->next_smb2_rcv_hdr_off
+ next_cmd
+
468 __SMB2_HEADER_STRUCTURE_SIZE
>
469 get_rfc1002_len(work
->request_buf
)) {
470 pr_err("next command(%u) offset exceeds smb msg size\n",
475 ksmbd_debug(SMB
, "got SMB2 chained command\n");
476 init_chained_smb2_rsp(work
);
478 } else if (work
->next_smb2_rcv_hdr_off
) {
480 * This is last request in chained command,
481 * align response to 8 byte
483 len
= ALIGN(get_rfc1002_len(work
->response_buf
), 8);
484 len
= len
- get_rfc1002_len(work
->response_buf
);
486 ksmbd_debug(SMB
, "padding len %u\n", len
);
487 inc_rfc1001_len(work
->response_buf
, len
);
488 if (work
->aux_payload_sz
)
489 work
->aux_payload_sz
+= len
;
496 * init_smb2_rsp_hdr() - initialize smb2 response
497 * @work: smb work containing smb request buffer
501 int init_smb2_rsp_hdr(struct ksmbd_work
*work
)
503 struct smb2_hdr
*rsp_hdr
= work
->response_buf
;
504 struct smb2_hdr
*rcv_hdr
= work
->request_buf
;
505 struct ksmbd_conn
*conn
= work
->conn
;
507 memset(rsp_hdr
, 0, sizeof(struct smb2_hdr
) + 2);
508 rsp_hdr
->smb2_buf_length
=
509 cpu_to_be32(smb2_hdr_size_no_buflen(conn
->vals
));
510 rsp_hdr
->ProtocolId
= rcv_hdr
->ProtocolId
;
511 rsp_hdr
->StructureSize
= SMB2_HEADER_STRUCTURE_SIZE
;
512 rsp_hdr
->Command
= rcv_hdr
->Command
;
515 * Message is response. We don't grant oplock yet.
517 rsp_hdr
->Flags
= (SMB2_FLAGS_SERVER_TO_REDIR
);
518 rsp_hdr
->NextCommand
= 0;
519 rsp_hdr
->MessageId
= rcv_hdr
->MessageId
;
520 rsp_hdr
->Id
.SyncId
.ProcessId
= rcv_hdr
->Id
.SyncId
.ProcessId
;
521 rsp_hdr
->Id
.SyncId
.TreeId
= rcv_hdr
->Id
.SyncId
.TreeId
;
522 rsp_hdr
->SessionId
= rcv_hdr
->SessionId
;
523 memcpy(rsp_hdr
->Signature
, rcv_hdr
->Signature
, 16);
525 work
->syncronous
= true;
526 if (work
->async_id
) {
527 ksmbd_release_id(&conn
->async_ida
, work
->async_id
);
535 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
536 * @work: smb work containing smb request buffer
538 * Return: 0 on success, otherwise -ENOMEM
540 int smb2_allocate_rsp_buf(struct ksmbd_work
*work
)
542 struct smb2_hdr
*hdr
= work
->request_buf
;
543 size_t small_sz
= MAX_CIFS_SMALL_BUFFER_SIZE
;
544 size_t large_sz
= work
->conn
->vals
->max_trans_size
+ MAX_SMB2_HDR_SIZE
;
545 size_t sz
= small_sz
;
546 int cmd
= le16_to_cpu(hdr
->Command
);
548 if (cmd
== SMB2_IOCTL_HE
|| cmd
== SMB2_QUERY_DIRECTORY_HE
)
551 if (cmd
== SMB2_QUERY_INFO_HE
) {
552 struct smb2_query_info_req
*req
;
554 req
= work
->request_buf
;
555 if (req
->InfoType
== SMB2_O_INFO_FILE
&&
556 (req
->FileInfoClass
== FILE_FULL_EA_INFORMATION
||
557 req
->FileInfoClass
== FILE_ALL_INFORMATION
))
561 /* allocate large response buf for chained commands */
562 if (le32_to_cpu(hdr
->NextCommand
) > 0)
565 work
->response_buf
= kvmalloc(sz
, GFP_KERNEL
| __GFP_ZERO
);
566 if (!work
->response_buf
)
569 work
->response_sz
= sz
;
574 * smb2_check_user_session() - check for valid session for a user
575 * @work: smb work containing smb request buffer
577 * Return: 0 on success, otherwise error
579 int smb2_check_user_session(struct ksmbd_work
*work
)
581 struct smb2_hdr
*req_hdr
= work
->request_buf
;
582 struct ksmbd_conn
*conn
= work
->conn
;
583 unsigned int cmd
= conn
->ops
->get_cmd_val(work
);
584 unsigned long long sess_id
;
588 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
589 * require a session id, so no need to validate user session's for
592 if (cmd
== SMB2_ECHO_HE
|| cmd
== SMB2_NEGOTIATE_HE
||
593 cmd
== SMB2_SESSION_SETUP_HE
)
596 if (!ksmbd_conn_good(work
))
599 sess_id
= le64_to_cpu(req_hdr
->SessionId
);
600 /* Check for validity of user session */
601 work
->sess
= ksmbd_session_lookup_all(conn
, sess_id
);
604 ksmbd_debug(SMB
, "Invalid user session, Uid %llu\n", sess_id
);
608 static void destroy_previous_session(struct ksmbd_user
*user
, u64 id
)
610 struct ksmbd_session
*prev_sess
= ksmbd_session_lookup_slowpath(id
);
611 struct ksmbd_user
*prev_user
;
616 prev_user
= prev_sess
->user
;
619 strcmp(user
->name
, prev_user
->name
) ||
620 user
->passkey_sz
!= prev_user
->passkey_sz
||
621 memcmp(user
->passkey
, prev_user
->passkey
, user
->passkey_sz
)) {
622 put_session(prev_sess
);
626 put_session(prev_sess
);
627 ksmbd_session_destroy(prev_sess
);
631 * smb2_get_name() - get filename string from on the wire smb format
632 * @share: ksmbd_share_config pointer
633 * @src: source buffer
634 * @maxlen: maxlen of source string
635 * @nls_table: nls_table pointer
637 * Return: matching converted filename on success, otherwise error ptr
640 smb2_get_name(struct ksmbd_share_config
*share
, const char *src
,
641 const int maxlen
, struct nls_table
*local_nls
)
645 name
= smb_strndup_from_utf16(src
, maxlen
, 1, local_nls
);
647 pr_err("failed to get name %ld\n", PTR_ERR(name
));
651 ksmbd_conv_path_to_unix(name
);
652 ksmbd_strip_last_slash(name
);
656 int setup_async_work(struct ksmbd_work
*work
, void (*fn
)(void **), void **arg
)
658 struct smb2_hdr
*rsp_hdr
;
659 struct ksmbd_conn
*conn
= work
->conn
;
662 rsp_hdr
= work
->response_buf
;
663 rsp_hdr
->Flags
|= SMB2_FLAGS_ASYNC_COMMAND
;
665 id
= ksmbd_acquire_async_msg_id(&conn
->async_ida
);
667 pr_err("Failed to alloc async message id\n");
670 work
->syncronous
= false;
672 rsp_hdr
->Id
.AsyncId
= cpu_to_le64(id
);
675 "Send interim Response to inform async request id : %d\n",
678 work
->cancel_fn
= fn
;
679 work
->cancel_argv
= arg
;
681 if (list_empty(&work
->async_request_entry
)) {
682 spin_lock(&conn
->request_lock
);
683 list_add_tail(&work
->async_request_entry
, &conn
->async_requests
);
684 spin_unlock(&conn
->request_lock
);
690 void smb2_send_interim_resp(struct ksmbd_work
*work
, __le32 status
)
692 struct smb2_hdr
*rsp_hdr
;
694 rsp_hdr
= work
->response_buf
;
695 smb2_set_err_rsp(work
);
696 rsp_hdr
->Status
= status
;
699 ksmbd_conn_write(work
);
704 static __le32
smb2_get_reparse_tag_special_file(umode_t mode
)
706 if (S_ISDIR(mode
) || S_ISREG(mode
))
710 return IO_REPARSE_TAG_LX_SYMLINK_LE
;
711 else if (S_ISFIFO(mode
))
712 return IO_REPARSE_TAG_LX_FIFO_LE
;
713 else if (S_ISSOCK(mode
))
714 return IO_REPARSE_TAG_AF_UNIX_LE
;
715 else if (S_ISCHR(mode
))
716 return IO_REPARSE_TAG_LX_CHR_LE
;
717 else if (S_ISBLK(mode
))
718 return IO_REPARSE_TAG_LX_BLK_LE
;
724 * smb2_get_dos_mode() - get file mode in dos format from unix mode
725 * @stat: kstat containing file mode
726 * @attribute: attribute flags
728 * Return: converted dos mode
730 static int smb2_get_dos_mode(struct kstat
*stat
, int attribute
)
734 if (S_ISDIR(stat
->mode
)) {
735 attr
= ATTR_DIRECTORY
|
736 (attribute
& (ATTR_HIDDEN
| ATTR_SYSTEM
));
738 attr
= (attribute
& 0x00005137) | ATTR_ARCHIVE
;
739 attr
&= ~(ATTR_DIRECTORY
);
740 if (S_ISREG(stat
->mode
) && (server_conf
.share_fake_fscaps
&
741 FILE_SUPPORTS_SPARSE_FILES
))
744 if (smb2_get_reparse_tag_special_file(stat
->mode
))
745 attr
|= ATTR_REPARSE
;
751 static void build_preauth_ctxt(struct smb2_preauth_neg_context
*pneg_ctxt
,
754 pneg_ctxt
->ContextType
= SMB2_PREAUTH_INTEGRITY_CAPABILITIES
;
755 pneg_ctxt
->DataLength
= cpu_to_le16(38);
756 pneg_ctxt
->HashAlgorithmCount
= cpu_to_le16(1);
757 pneg_ctxt
->Reserved
= cpu_to_le32(0);
758 pneg_ctxt
->SaltLength
= cpu_to_le16(SMB311_SALT_SIZE
);
759 get_random_bytes(pneg_ctxt
->Salt
, SMB311_SALT_SIZE
);
760 pneg_ctxt
->HashAlgorithms
= hash_id
;
763 static void build_encrypt_ctxt(struct smb2_encryption_neg_context
*pneg_ctxt
,
766 pneg_ctxt
->ContextType
= SMB2_ENCRYPTION_CAPABILITIES
;
767 pneg_ctxt
->DataLength
= cpu_to_le16(4);
768 pneg_ctxt
->Reserved
= cpu_to_le32(0);
769 pneg_ctxt
->CipherCount
= cpu_to_le16(1);
770 pneg_ctxt
->Ciphers
[0] = cipher_type
;
773 static void build_compression_ctxt(struct smb2_compression_ctx
*pneg_ctxt
,
776 pneg_ctxt
->ContextType
= SMB2_COMPRESSION_CAPABILITIES
;
777 pneg_ctxt
->DataLength
=
778 cpu_to_le16(sizeof(struct smb2_compression_ctx
)
779 - sizeof(struct smb2_neg_context
));
780 pneg_ctxt
->Reserved
= cpu_to_le32(0);
781 pneg_ctxt
->CompressionAlgorithmCount
= cpu_to_le16(1);
782 pneg_ctxt
->Reserved1
= cpu_to_le32(0);
783 pneg_ctxt
->CompressionAlgorithms
[0] = comp_algo
;
786 static void build_sign_cap_ctxt(struct smb2_signing_capabilities
*pneg_ctxt
,
789 pneg_ctxt
->ContextType
= SMB2_SIGNING_CAPABILITIES
;
790 pneg_ctxt
->DataLength
=
791 cpu_to_le16((sizeof(struct smb2_signing_capabilities
) + 2)
792 - sizeof(struct smb2_neg_context
));
793 pneg_ctxt
->Reserved
= cpu_to_le32(0);
794 pneg_ctxt
->SigningAlgorithmCount
= cpu_to_le16(1);
795 pneg_ctxt
->SigningAlgorithms
[0] = sign_algo
;
798 static void build_posix_ctxt(struct smb2_posix_neg_context
*pneg_ctxt
)
800 pneg_ctxt
->ContextType
= SMB2_POSIX_EXTENSIONS_AVAILABLE
;
801 pneg_ctxt
->DataLength
= cpu_to_le16(POSIX_CTXT_DATA_LEN
);
802 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
803 pneg_ctxt
->Name
[0] = 0x93;
804 pneg_ctxt
->Name
[1] = 0xAD;
805 pneg_ctxt
->Name
[2] = 0x25;
806 pneg_ctxt
->Name
[3] = 0x50;
807 pneg_ctxt
->Name
[4] = 0x9C;
808 pneg_ctxt
->Name
[5] = 0xB4;
809 pneg_ctxt
->Name
[6] = 0x11;
810 pneg_ctxt
->Name
[7] = 0xE7;
811 pneg_ctxt
->Name
[8] = 0xB4;
812 pneg_ctxt
->Name
[9] = 0x23;
813 pneg_ctxt
->Name
[10] = 0x83;
814 pneg_ctxt
->Name
[11] = 0xDE;
815 pneg_ctxt
->Name
[12] = 0x96;
816 pneg_ctxt
->Name
[13] = 0x8B;
817 pneg_ctxt
->Name
[14] = 0xCD;
818 pneg_ctxt
->Name
[15] = 0x7C;
821 static void assemble_neg_contexts(struct ksmbd_conn
*conn
,
822 struct smb2_negotiate_rsp
*rsp
)
824 /* +4 is to account for the RFC1001 len field */
825 char *pneg_ctxt
= (char *)rsp
+
826 le32_to_cpu(rsp
->NegotiateContextOffset
) + 4;
827 int neg_ctxt_cnt
= 1;
831 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
832 build_preauth_ctxt((struct smb2_preauth_neg_context
*)pneg_ctxt
,
833 conn
->preauth_info
->Preauth_HashId
);
834 rsp
->NegotiateContextCount
= cpu_to_le16(neg_ctxt_cnt
);
835 inc_rfc1001_len(rsp
, AUTH_GSS_PADDING
);
836 ctxt_size
= sizeof(struct smb2_preauth_neg_context
);
837 /* Round to 8 byte boundary */
838 pneg_ctxt
+= round_up(sizeof(struct smb2_preauth_neg_context
), 8);
840 if (conn
->cipher_type
) {
841 ctxt_size
= round_up(ctxt_size
, 8);
843 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
844 build_encrypt_ctxt((struct smb2_encryption_neg_context
*)pneg_ctxt
,
846 rsp
->NegotiateContextCount
= cpu_to_le16(++neg_ctxt_cnt
);
847 ctxt_size
+= sizeof(struct smb2_encryption_neg_context
) + 2;
848 /* Round to 8 byte boundary */
850 round_up(sizeof(struct smb2_encryption_neg_context
) + 2,
854 if (conn
->compress_algorithm
) {
855 ctxt_size
= round_up(ctxt_size
, 8);
857 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
858 /* Temporarily set to SMB3_COMPRESS_NONE */
859 build_compression_ctxt((struct smb2_compression_ctx
*)pneg_ctxt
,
860 conn
->compress_algorithm
);
861 rsp
->NegotiateContextCount
= cpu_to_le16(++neg_ctxt_cnt
);
862 ctxt_size
+= sizeof(struct smb2_compression_ctx
) + 2;
863 /* Round to 8 byte boundary */
864 pneg_ctxt
+= round_up(sizeof(struct smb2_compression_ctx
) + 2,
868 if (conn
->posix_ext_supported
) {
869 ctxt_size
= round_up(ctxt_size
, 8);
871 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
872 build_posix_ctxt((struct smb2_posix_neg_context
*)pneg_ctxt
);
873 rsp
->NegotiateContextCount
= cpu_to_le16(++neg_ctxt_cnt
);
874 ctxt_size
+= sizeof(struct smb2_posix_neg_context
);
875 /* Round to 8 byte boundary */
876 pneg_ctxt
+= round_up(sizeof(struct smb2_posix_neg_context
), 8);
879 if (conn
->signing_negotiated
) {
880 ctxt_size
= round_up(ctxt_size
, 8);
882 "assemble SMB2_SIGNING_CAPABILITIES context\n");
883 build_sign_cap_ctxt((struct smb2_signing_capabilities
*)pneg_ctxt
,
884 conn
->signing_algorithm
);
885 rsp
->NegotiateContextCount
= cpu_to_le16(++neg_ctxt_cnt
);
886 ctxt_size
+= sizeof(struct smb2_signing_capabilities
) + 2;
889 inc_rfc1001_len(rsp
, ctxt_size
);
892 static __le32
decode_preauth_ctxt(struct ksmbd_conn
*conn
,
893 struct smb2_preauth_neg_context
*pneg_ctxt
)
895 __le32 err
= STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP
;
897 if (pneg_ctxt
->HashAlgorithms
== SMB2_PREAUTH_INTEGRITY_SHA512
) {
898 conn
->preauth_info
->Preauth_HashId
=
899 SMB2_PREAUTH_INTEGRITY_SHA512
;
900 err
= STATUS_SUCCESS
;
906 static void decode_encrypt_ctxt(struct ksmbd_conn
*conn
,
907 struct smb2_encryption_neg_context
*pneg_ctxt
,
910 int cph_cnt
= le16_to_cpu(pneg_ctxt
->CipherCount
);
911 int i
, cphs_size
= cph_cnt
* sizeof(__le16
);
913 conn
->cipher_type
= 0;
915 if (sizeof(struct smb2_encryption_neg_context
) + cphs_size
>
917 pr_err("Invalid cipher count(%d)\n", cph_cnt
);
921 if (!(server_conf
.flags
& KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION
))
924 for (i
= 0; i
< cph_cnt
; i
++) {
925 if (pneg_ctxt
->Ciphers
[i
] == SMB2_ENCRYPTION_AES128_GCM
||
926 pneg_ctxt
->Ciphers
[i
] == SMB2_ENCRYPTION_AES128_CCM
||
927 pneg_ctxt
->Ciphers
[i
] == SMB2_ENCRYPTION_AES256_CCM
||
928 pneg_ctxt
->Ciphers
[i
] == SMB2_ENCRYPTION_AES256_GCM
) {
929 ksmbd_debug(SMB
, "Cipher ID = 0x%x\n",
930 pneg_ctxt
->Ciphers
[i
]);
931 conn
->cipher_type
= pneg_ctxt
->Ciphers
[i
];
937 static void decode_compress_ctxt(struct ksmbd_conn
*conn
,
938 struct smb2_compression_ctx
*pneg_ctxt
)
940 conn
->compress_algorithm
= SMB3_COMPRESS_NONE
;
943 static void decode_sign_cap_ctxt(struct ksmbd_conn
*conn
,
944 struct smb2_signing_capabilities
*pneg_ctxt
,
947 int sign_algo_cnt
= le16_to_cpu(pneg_ctxt
->SigningAlgorithmCount
);
948 int i
, sign_alos_size
= sign_algo_cnt
* sizeof(__le16
);
950 conn
->signing_negotiated
= false;
952 if (sizeof(struct smb2_signing_capabilities
) + sign_alos_size
>
954 pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt
);
958 for (i
= 0; i
< sign_algo_cnt
; i
++) {
959 if (pneg_ctxt
->SigningAlgorithms
[i
] == SIGNING_ALG_HMAC_SHA256
||
960 pneg_ctxt
->SigningAlgorithms
[i
] == SIGNING_ALG_AES_CMAC
) {
961 ksmbd_debug(SMB
, "Signing Algorithm ID = 0x%x\n",
962 pneg_ctxt
->SigningAlgorithms
[i
]);
963 conn
->signing_negotiated
= true;
964 conn
->signing_algorithm
=
965 pneg_ctxt
->SigningAlgorithms
[i
];
971 static __le32
deassemble_neg_contexts(struct ksmbd_conn
*conn
,
972 struct smb2_negotiate_req
*req
)
974 /* +4 is to account for the RFC1001 len field */
975 struct smb2_neg_context
*pctx
= (struct smb2_neg_context
*)((char *)req
+ 4);
976 int i
= 0, len_of_ctxts
;
977 int offset
= le32_to_cpu(req
->NegotiateContextOffset
);
978 int neg_ctxt_cnt
= le16_to_cpu(req
->NegotiateContextCount
);
979 int len_of_smb
= be32_to_cpu(req
->hdr
.smb2_buf_length
);
980 __le32 status
= STATUS_INVALID_PARAMETER
;
982 ksmbd_debug(SMB
, "decoding %d negotiate contexts\n", neg_ctxt_cnt
);
983 if (len_of_smb
<= offset
) {
984 ksmbd_debug(SMB
, "Invalid response: negotiate context offset\n");
988 len_of_ctxts
= len_of_smb
- offset
;
990 while (i
++ < neg_ctxt_cnt
) {
993 /* check that offset is not beyond end of SMB */
994 if (len_of_ctxts
== 0)
997 if (len_of_ctxts
< sizeof(struct smb2_neg_context
))
1000 pctx
= (struct smb2_neg_context
*)((char *)pctx
+ offset
);
1001 clen
= le16_to_cpu(pctx
->DataLength
);
1002 if (clen
+ sizeof(struct smb2_neg_context
) > len_of_ctxts
)
1005 if (pctx
->ContextType
== SMB2_PREAUTH_INTEGRITY_CAPABILITIES
) {
1007 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
1008 if (conn
->preauth_info
->Preauth_HashId
)
1011 status
= decode_preauth_ctxt(conn
,
1012 (struct smb2_preauth_neg_context
*)pctx
);
1013 if (status
!= STATUS_SUCCESS
)
1015 } else if (pctx
->ContextType
== SMB2_ENCRYPTION_CAPABILITIES
) {
1017 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
1018 if (conn
->cipher_type
)
1021 decode_encrypt_ctxt(conn
,
1022 (struct smb2_encryption_neg_context
*)pctx
,
1024 } else if (pctx
->ContextType
== SMB2_COMPRESSION_CAPABILITIES
) {
1026 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
1027 if (conn
->compress_algorithm
)
1030 decode_compress_ctxt(conn
,
1031 (struct smb2_compression_ctx
*)pctx
);
1032 } else if (pctx
->ContextType
== SMB2_NETNAME_NEGOTIATE_CONTEXT_ID
) {
1034 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
1035 } else if (pctx
->ContextType
== SMB2_POSIX_EXTENSIONS_AVAILABLE
) {
1037 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
1038 conn
->posix_ext_supported
= true;
1039 } else if (pctx
->ContextType
== SMB2_SIGNING_CAPABILITIES
) {
1041 "deassemble SMB2_SIGNING_CAPABILITIES context\n");
1042 decode_sign_cap_ctxt(conn
,
1043 (struct smb2_signing_capabilities
*)pctx
,
1047 /* offsets must be 8 byte aligned */
1048 clen
= (clen
+ 7) & ~0x7;
1049 offset
= clen
+ sizeof(struct smb2_neg_context
);
1050 len_of_ctxts
-= clen
+ sizeof(struct smb2_neg_context
);
1056 * smb2_handle_negotiate() - handler for smb2 negotiate command
1057 * @work: smb work containing smb request buffer
1061 int smb2_handle_negotiate(struct ksmbd_work
*work
)
1063 struct ksmbd_conn
*conn
= work
->conn
;
1064 struct smb2_negotiate_req
*req
= work
->request_buf
;
1065 struct smb2_negotiate_rsp
*rsp
= work
->response_buf
;
1067 unsigned int smb2_buf_len
, smb2_neg_size
;
1070 ksmbd_debug(SMB
, "Received negotiate request\n");
1071 conn
->need_neg
= false;
1072 if (ksmbd_conn_good(work
)) {
1073 pr_err("conn->tcp_status is already in CifsGood State\n");
1074 work
->send_no_response
= 1;
1078 if (req
->DialectCount
== 0) {
1079 pr_err("malformed packet\n");
1080 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1085 smb2_buf_len
= get_rfc1002_len(work
->request_buf
);
1086 smb2_neg_size
= offsetof(struct smb2_negotiate_req
, Dialects
) - 4;
1087 if (smb2_neg_size
> smb2_buf_len
) {
1088 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1093 if (conn
->dialect
== SMB311_PROT_ID
) {
1094 unsigned int nego_ctxt_off
= le32_to_cpu(req
->NegotiateContextOffset
);
1096 if (smb2_buf_len
< nego_ctxt_off
) {
1097 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1102 if (smb2_neg_size
> nego_ctxt_off
) {
1103 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1108 if (smb2_neg_size
+ le16_to_cpu(req
->DialectCount
) * sizeof(__le16
) >
1110 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1115 if (smb2_neg_size
+ le16_to_cpu(req
->DialectCount
) * sizeof(__le16
) >
1117 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1123 conn
->cli_cap
= le32_to_cpu(req
->Capabilities
);
1124 switch (conn
->dialect
) {
1125 case SMB311_PROT_ID
:
1126 conn
->preauth_info
=
1127 kzalloc(sizeof(struct preauth_integrity_info
),
1129 if (!conn
->preauth_info
) {
1131 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1135 status
= deassemble_neg_contexts(conn
, req
);
1136 if (status
!= STATUS_SUCCESS
) {
1137 pr_err("deassemble_neg_contexts error(0x%x)\n",
1139 rsp
->hdr
.Status
= status
;
1144 rc
= init_smb3_11_server(conn
);
1146 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1150 ksmbd_gen_preauth_integrity_hash(conn
,
1152 conn
->preauth_info
->Preauth_HashValue
);
1153 rsp
->NegotiateContextOffset
=
1154 cpu_to_le32(OFFSET_OF_NEG_CONTEXT
);
1155 assemble_neg_contexts(conn
, rsp
);
1157 case SMB302_PROT_ID
:
1158 init_smb3_02_server(conn
);
1161 init_smb3_0_server(conn
);
1164 init_smb2_1_server(conn
);
1169 ksmbd_debug(SMB
, "Server dialect :0x%x not supported\n",
1171 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
1175 rsp
->Capabilities
= cpu_to_le32(conn
->vals
->capabilities
);
1178 conn
->connection_type
= conn
->dialect
;
1180 rsp
->MaxTransactSize
= cpu_to_le32(conn
->vals
->max_trans_size
);
1181 rsp
->MaxReadSize
= cpu_to_le32(conn
->vals
->max_read_size
);
1182 rsp
->MaxWriteSize
= cpu_to_le32(conn
->vals
->max_write_size
);
1184 memcpy(conn
->ClientGUID
, req
->ClientGUID
,
1185 SMB2_CLIENT_GUID_SIZE
);
1186 conn
->cli_sec_mode
= le16_to_cpu(req
->SecurityMode
);
1188 rsp
->StructureSize
= cpu_to_le16(65);
1189 rsp
->DialectRevision
= cpu_to_le16(conn
->dialect
);
1190 /* Not setting conn guid rsp->ServerGUID, as it
1191 * not used by client for identifying server
1193 memset(rsp
->ServerGUID
, 0, SMB2_CLIENT_GUID_SIZE
);
1195 rsp
->SystemTime
= cpu_to_le64(ksmbd_systime());
1196 rsp
->ServerStartTime
= 0;
1197 ksmbd_debug(SMB
, "negotiate context offset %d, count %d\n",
1198 le32_to_cpu(rsp
->NegotiateContextOffset
),
1199 le16_to_cpu(rsp
->NegotiateContextCount
));
1201 rsp
->SecurityBufferOffset
= cpu_to_le16(128);
1202 rsp
->SecurityBufferLength
= cpu_to_le16(AUTH_GSS_LENGTH
);
1203 ksmbd_copy_gss_neg_header(((char *)(&rsp
->hdr
) +
1204 sizeof(rsp
->hdr
.smb2_buf_length
)) +
1205 le16_to_cpu(rsp
->SecurityBufferOffset
));
1206 inc_rfc1001_len(rsp
, sizeof(struct smb2_negotiate_rsp
) -
1207 sizeof(struct smb2_hdr
) - sizeof(rsp
->Buffer
) +
1209 rsp
->SecurityMode
= SMB2_NEGOTIATE_SIGNING_ENABLED_LE
;
1210 conn
->use_spnego
= true;
1212 if ((server_conf
.signing
== KSMBD_CONFIG_OPT_AUTO
||
1213 server_conf
.signing
== KSMBD_CONFIG_OPT_DISABLED
) &&
1214 req
->SecurityMode
& SMB2_NEGOTIATE_SIGNING_REQUIRED_LE
)
1216 else if (server_conf
.signing
== KSMBD_CONFIG_OPT_MANDATORY
) {
1217 server_conf
.enforced_signing
= true;
1218 rsp
->SecurityMode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE
;
1222 conn
->srv_sec_mode
= le16_to_cpu(rsp
->SecurityMode
);
1223 ksmbd_conn_set_need_negotiate(work
);
1227 smb2_set_err_rsp(work
);
1232 static int alloc_preauth_hash(struct ksmbd_session
*sess
,
1233 struct ksmbd_conn
*conn
)
1235 if (sess
->Preauth_HashValue
)
1238 sess
->Preauth_HashValue
= kmemdup(conn
->preauth_info
->Preauth_HashValue
,
1239 PREAUTH_HASHVALUE_SIZE
, GFP_KERNEL
);
1240 if (!sess
->Preauth_HashValue
)
1246 static int generate_preauth_hash(struct ksmbd_work
*work
)
1248 struct ksmbd_conn
*conn
= work
->conn
;
1249 struct ksmbd_session
*sess
= work
->sess
;
1252 if (conn
->dialect
!= SMB311_PROT_ID
)
1255 if (conn
->binding
) {
1256 struct preauth_session
*preauth_sess
;
1258 preauth_sess
= ksmbd_preauth_session_lookup(conn
, sess
->id
);
1259 if (!preauth_sess
) {
1260 preauth_sess
= ksmbd_preauth_session_alloc(conn
, sess
->id
);
1265 preauth_hash
= preauth_sess
->Preauth_HashValue
;
1267 if (!sess
->Preauth_HashValue
)
1268 if (alloc_preauth_hash(sess
, conn
))
1270 preauth_hash
= sess
->Preauth_HashValue
;
1273 ksmbd_gen_preauth_integrity_hash(conn
, work
->request_buf
, preauth_hash
);
1277 static int decode_negotiation_token(struct ksmbd_work
*work
,
1278 struct negotiate_message
*negblob
)
1280 struct ksmbd_conn
*conn
= work
->conn
;
1281 struct smb2_sess_setup_req
*req
;
1284 if (!conn
->use_spnego
)
1287 req
= work
->request_buf
;
1288 sz
= le16_to_cpu(req
->SecurityBufferLength
);
1290 if (ksmbd_decode_negTokenInit((char *)negblob
, sz
, conn
)) {
1291 if (ksmbd_decode_negTokenTarg((char *)negblob
, sz
, conn
)) {
1292 conn
->auth_mechs
|= KSMBD_AUTH_NTLMSSP
;
1293 conn
->preferred_auth_mech
= KSMBD_AUTH_NTLMSSP
;
1294 conn
->use_spnego
= false;
1300 static int ntlm_negotiate(struct ksmbd_work
*work
,
1301 struct negotiate_message
*negblob
)
1303 struct smb2_sess_setup_req
*req
= work
->request_buf
;
1304 struct smb2_sess_setup_rsp
*rsp
= work
->response_buf
;
1305 struct challenge_message
*chgblob
;
1306 unsigned char *spnego_blob
= NULL
;
1307 u16 spnego_blob_len
;
1311 ksmbd_debug(SMB
, "negotiate phase\n");
1312 sz
= le16_to_cpu(req
->SecurityBufferLength
);
1313 rc
= ksmbd_decode_ntlmssp_neg_blob(negblob
, sz
, work
->sess
);
1317 sz
= le16_to_cpu(rsp
->SecurityBufferOffset
);
1319 (struct challenge_message
*)((char *)&rsp
->hdr
.ProtocolId
+ sz
);
1320 memset(chgblob
, 0, sizeof(struct challenge_message
));
1322 if (!work
->conn
->use_spnego
) {
1323 sz
= ksmbd_build_ntlmssp_challenge_blob(chgblob
, work
->sess
);
1327 rsp
->SecurityBufferLength
= cpu_to_le16(sz
);
1331 sz
= sizeof(struct challenge_message
);
1332 sz
+= (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1334 neg_blob
= kzalloc(sz
, GFP_KERNEL
);
1338 chgblob
= (struct challenge_message
*)neg_blob
;
1339 sz
= ksmbd_build_ntlmssp_challenge_blob(chgblob
, work
->sess
);
1345 rc
= build_spnego_ntlmssp_neg_blob(&spnego_blob
, &spnego_blob_len
,
1352 sz
= le16_to_cpu(rsp
->SecurityBufferOffset
);
1353 memcpy((char *)&rsp
->hdr
.ProtocolId
+ sz
, spnego_blob
, spnego_blob_len
);
1354 rsp
->SecurityBufferLength
= cpu_to_le16(spnego_blob_len
);
1362 static struct authenticate_message
*user_authblob(struct ksmbd_conn
*conn
,
1363 struct smb2_sess_setup_req
*req
)
1367 if (conn
->use_spnego
&& conn
->mechToken
)
1368 return (struct authenticate_message
*)conn
->mechToken
;
1370 sz
= le16_to_cpu(req
->SecurityBufferOffset
);
1371 return (struct authenticate_message
*)((char *)&req
->hdr
.ProtocolId
1375 static struct ksmbd_user
*session_user(struct ksmbd_conn
*conn
,
1376 struct smb2_sess_setup_req
*req
)
1378 struct authenticate_message
*authblob
;
1379 struct ksmbd_user
*user
;
1383 authblob
= user_authblob(conn
, req
);
1384 sz
= le32_to_cpu(authblob
->UserName
.BufferOffset
);
1385 name
= smb_strndup_from_utf16((const char *)authblob
+ sz
,
1386 le16_to_cpu(authblob
->UserName
.Length
),
1390 pr_err("cannot allocate memory\n");
1394 ksmbd_debug(SMB
, "session setup request for user %s\n", name
);
1395 user
= ksmbd_login_user(name
);
1400 static int ntlm_authenticate(struct ksmbd_work
*work
)
1402 struct smb2_sess_setup_req
*req
= work
->request_buf
;
1403 struct smb2_sess_setup_rsp
*rsp
= work
->response_buf
;
1404 struct ksmbd_conn
*conn
= work
->conn
;
1405 struct ksmbd_session
*sess
= work
->sess
;
1406 struct channel
*chann
= NULL
;
1407 struct ksmbd_user
*user
;
1411 ksmbd_debug(SMB
, "authenticate phase\n");
1412 if (conn
->use_spnego
) {
1413 unsigned char *spnego_blob
;
1414 u16 spnego_blob_len
;
1416 rc
= build_spnego_ntlmssp_auth_blob(&spnego_blob
,
1422 sz
= le16_to_cpu(rsp
->SecurityBufferOffset
);
1423 memcpy((char *)&rsp
->hdr
.ProtocolId
+ sz
, spnego_blob
, spnego_blob_len
);
1424 rsp
->SecurityBufferLength
= cpu_to_le16(spnego_blob_len
);
1426 inc_rfc1001_len(rsp
, spnego_blob_len
- 1);
1429 user
= session_user(conn
, req
);
1431 ksmbd_debug(SMB
, "Unknown user name or an error\n");
1435 /* Check for previous session */
1436 prev_id
= le64_to_cpu(req
->PreviousSessionId
);
1437 if (prev_id
&& prev_id
!= sess
->id
)
1438 destroy_previous_session(user
, prev_id
);
1440 if (sess
->state
== SMB2_SESSION_VALID
) {
1442 * Reuse session if anonymous try to connect
1443 * on reauthetication.
1445 if (ksmbd_anonymous_user(user
)) {
1446 ksmbd_free_user(user
);
1449 ksmbd_free_user(sess
->user
);
1453 if (user_guest(sess
->user
)) {
1455 ksmbd_debug(SMB
, "Guest login not allowed when signing enabled\n");
1459 rsp
->SessionFlags
= SMB2_SESSION_FLAG_IS_GUEST_LE
;
1461 struct authenticate_message
*authblob
;
1463 authblob
= user_authblob(conn
, req
);
1464 sz
= le16_to_cpu(req
->SecurityBufferLength
);
1465 rc
= ksmbd_decode_ntlmssp_auth_blob(authblob
, sz
, sess
);
1467 set_user_flag(sess
->user
, KSMBD_USER_FLAG_BAD_PASSWORD
);
1468 ksmbd_debug(SMB
, "authentication failed\n");
1473 * If session state is SMB2_SESSION_VALID, We can assume
1474 * that it is reauthentication. And the user/password
1475 * has been verified, so return it here.
1477 if (sess
->state
== SMB2_SESSION_VALID
) {
1479 goto binding_session
;
1483 if ((conn
->sign
|| server_conf
.enforced_signing
) ||
1484 (req
->SecurityMode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
))
1487 if (conn
->vals
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
&&
1488 conn
->ops
->generate_encryptionkey
&&
1489 !(req
->Flags
& SMB2_SESSION_REQ_FLAG_BINDING
)) {
1490 rc
= conn
->ops
->generate_encryptionkey(sess
);
1493 "SMB3 encryption key generation failed\n");
1497 rsp
->SessionFlags
= SMB2_SESSION_FLAG_ENCRYPT_DATA_LE
;
1499 * signing is disable if encryption is enable
1507 if (conn
->dialect
>= SMB30_PROT_ID
) {
1508 chann
= lookup_chann_list(sess
, conn
);
1510 chann
= kmalloc(sizeof(struct channel
), GFP_KERNEL
);
1515 INIT_LIST_HEAD(&chann
->chann_list
);
1516 list_add(&chann
->chann_list
, &sess
->ksmbd_chann_list
);
1520 if (conn
->ops
->generate_signingkey
) {
1521 rc
= conn
->ops
->generate_signingkey(sess
, conn
);
1523 ksmbd_debug(SMB
, "SMB3 signing key generation failed\n");
1528 if (!ksmbd_conn_lookup_dialect(conn
)) {
1529 pr_err("fail to verify the dialect\n");
1535 #ifdef CONFIG_SMB_SERVER_KERBEROS5
1536 static int krb5_authenticate(struct ksmbd_work
*work
)
1538 struct smb2_sess_setup_req
*req
= work
->request_buf
;
1539 struct smb2_sess_setup_rsp
*rsp
= work
->response_buf
;
1540 struct ksmbd_conn
*conn
= work
->conn
;
1541 struct ksmbd_session
*sess
= work
->sess
;
1542 char *in_blob
, *out_blob
;
1543 struct channel
*chann
= NULL
;
1545 int in_len
, out_len
;
1548 in_blob
= (char *)&req
->hdr
.ProtocolId
+
1549 le16_to_cpu(req
->SecurityBufferOffset
);
1550 in_len
= le16_to_cpu(req
->SecurityBufferLength
);
1551 out_blob
= (char *)&rsp
->hdr
.ProtocolId
+
1552 le16_to_cpu(rsp
->SecurityBufferOffset
);
1553 out_len
= work
->response_sz
-
1554 offsetof(struct smb2_hdr
, smb2_buf_length
) -
1555 le16_to_cpu(rsp
->SecurityBufferOffset
);
1557 /* Check previous session */
1558 prev_sess_id
= le64_to_cpu(req
->PreviousSessionId
);
1559 if (prev_sess_id
&& prev_sess_id
!= sess
->id
)
1560 destroy_previous_session(sess
->user
, prev_sess_id
);
1562 if (sess
->state
== SMB2_SESSION_VALID
)
1563 ksmbd_free_user(sess
->user
);
1565 retval
= ksmbd_krb5_authenticate(sess
, in_blob
, in_len
,
1566 out_blob
, &out_len
);
1568 ksmbd_debug(SMB
, "krb5 authentication failed\n");
1571 rsp
->SecurityBufferLength
= cpu_to_le16(out_len
);
1572 inc_rfc1001_len(rsp
, out_len
- 1);
1574 if ((conn
->sign
|| server_conf
.enforced_signing
) ||
1575 (req
->SecurityMode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
))
1578 if ((conn
->vals
->capabilities
& SMB2_GLOBAL_CAP_ENCRYPTION
) &&
1579 conn
->ops
->generate_encryptionkey
) {
1580 retval
= conn
->ops
->generate_encryptionkey(sess
);
1583 "SMB3 encryption key generation failed\n");
1587 rsp
->SessionFlags
= SMB2_SESSION_FLAG_ENCRYPT_DATA_LE
;
1591 if (conn
->dialect
>= SMB30_PROT_ID
) {
1592 chann
= lookup_chann_list(sess
, conn
);
1594 chann
= kmalloc(sizeof(struct channel
), GFP_KERNEL
);
1599 INIT_LIST_HEAD(&chann
->chann_list
);
1600 list_add(&chann
->chann_list
, &sess
->ksmbd_chann_list
);
1604 if (conn
->ops
->generate_signingkey
) {
1605 retval
= conn
->ops
->generate_signingkey(sess
, conn
);
1607 ksmbd_debug(SMB
, "SMB3 signing key generation failed\n");
1612 if (!ksmbd_conn_lookup_dialect(conn
)) {
1613 pr_err("fail to verify the dialect\n");
1619 static int krb5_authenticate(struct ksmbd_work
*work
)
1625 int smb2_sess_setup(struct ksmbd_work
*work
)
1627 struct ksmbd_conn
*conn
= work
->conn
;
1628 struct smb2_sess_setup_req
*req
= work
->request_buf
;
1629 struct smb2_sess_setup_rsp
*rsp
= work
->response_buf
;
1630 struct ksmbd_session
*sess
;
1631 struct negotiate_message
*negblob
;
1634 ksmbd_debug(SMB
, "Received request for session setup\n");
1636 rsp
->StructureSize
= cpu_to_le16(9);
1637 rsp
->SessionFlags
= 0;
1638 rsp
->SecurityBufferOffset
= cpu_to_le16(72);
1639 rsp
->SecurityBufferLength
= 0;
1640 inc_rfc1001_len(rsp
, 9);
1642 if (!req
->hdr
.SessionId
) {
1643 sess
= ksmbd_smb2_session_create();
1648 rsp
->hdr
.SessionId
= cpu_to_le64(sess
->id
);
1649 ksmbd_session_register(conn
, sess
);
1650 } else if (conn
->dialect
>= SMB30_PROT_ID
&&
1651 (server_conf
.flags
& KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL
) &&
1652 req
->Flags
& SMB2_SESSION_REQ_FLAG_BINDING
) {
1653 u64 sess_id
= le64_to_cpu(req
->hdr
.SessionId
);
1655 sess
= ksmbd_session_lookup_slowpath(sess_id
);
1661 if (conn
->dialect
!= sess
->conn
->dialect
) {
1666 if (!(req
->hdr
.Flags
& SMB2_FLAGS_SIGNED
)) {
1671 if (strncmp(conn
->ClientGUID
, sess
->conn
->ClientGUID
,
1672 SMB2_CLIENT_GUID_SIZE
)) {
1677 if (sess
->state
== SMB2_SESSION_IN_PROGRESS
) {
1682 if (sess
->state
== SMB2_SESSION_EXPIRED
) {
1687 if (ksmbd_session_lookup(conn
, sess_id
)) {
1692 conn
->binding
= true;
1693 } else if ((conn
->dialect
< SMB30_PROT_ID
||
1694 server_conf
.flags
& KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL
) &&
1695 (req
->Flags
& SMB2_SESSION_REQ_FLAG_BINDING
)) {
1700 sess
= ksmbd_session_lookup(conn
,
1701 le64_to_cpu(req
->hdr
.SessionId
));
1709 if (sess
->state
== SMB2_SESSION_EXPIRED
)
1710 sess
->state
= SMB2_SESSION_IN_PROGRESS
;
1712 negblob
= (struct negotiate_message
*)((char *)&req
->hdr
.ProtocolId
+
1713 le16_to_cpu(req
->SecurityBufferOffset
));
1715 if (decode_negotiation_token(work
, negblob
) == 0) {
1716 if (conn
->mechToken
)
1717 negblob
= (struct negotiate_message
*)conn
->mechToken
;
1720 if (server_conf
.auth_mechs
& conn
->auth_mechs
) {
1721 rc
= generate_preauth_hash(work
);
1725 if (conn
->preferred_auth_mech
&
1726 (KSMBD_AUTH_KRB5
| KSMBD_AUTH_MSKRB5
)) {
1727 rc
= krb5_authenticate(work
);
1733 ksmbd_conn_set_good(work
);
1734 sess
->state
= SMB2_SESSION_VALID
;
1735 kfree(sess
->Preauth_HashValue
);
1736 sess
->Preauth_HashValue
= NULL
;
1737 } else if (conn
->preferred_auth_mech
== KSMBD_AUTH_NTLMSSP
) {
1738 if (negblob
->MessageType
== NtLmNegotiate
) {
1739 rc
= ntlm_negotiate(work
, negblob
);
1743 STATUS_MORE_PROCESSING_REQUIRED
;
1745 * Note: here total size -1 is done as an
1746 * adjustment for 0 size blob
1748 inc_rfc1001_len(rsp
, le16_to_cpu(rsp
->SecurityBufferLength
) - 1);
1750 } else if (negblob
->MessageType
== NtLmAuthenticate
) {
1751 rc
= ntlm_authenticate(work
);
1755 ksmbd_conn_set_good(work
);
1756 sess
->state
= SMB2_SESSION_VALID
;
1757 if (conn
->binding
) {
1758 struct preauth_session
*preauth_sess
;
1761 ksmbd_preauth_session_lookup(conn
, sess
->id
);
1763 list_del(&preauth_sess
->preauth_entry
);
1764 kfree(preauth_sess
);
1767 kfree(sess
->Preauth_HashValue
);
1768 sess
->Preauth_HashValue
= NULL
;
1771 /* TODO: need one more negotiation */
1772 pr_err("Not support the preferred authentication\n");
1776 pr_err("Not support authentication\n");
1782 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1783 else if (rc
== -ENOENT
)
1784 rsp
->hdr
.Status
= STATUS_USER_SESSION_DELETED
;
1785 else if (rc
== -EACCES
)
1786 rsp
->hdr
.Status
= STATUS_REQUEST_NOT_ACCEPTED
;
1787 else if (rc
== -EFAULT
)
1788 rsp
->hdr
.Status
= STATUS_NETWORK_SESSION_EXPIRED
;
1789 else if (rc
== -ENOMEM
)
1790 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
1792 rsp
->hdr
.Status
= STATUS_LOGON_FAILURE
;
1794 if (conn
->use_spnego
&& conn
->mechToken
) {
1795 kfree(conn
->mechToken
);
1796 conn
->mechToken
= NULL
;
1799 if (rc
< 0 && sess
) {
1800 ksmbd_session_destroy(sess
);
1808 * smb2_tree_connect() - handler for smb2 tree connect command
1809 * @work: smb work containing smb request buffer
1811 * Return: 0 on success, otherwise error
1813 int smb2_tree_connect(struct ksmbd_work
*work
)
1815 struct ksmbd_conn
*conn
= work
->conn
;
1816 struct smb2_tree_connect_req
*req
= work
->request_buf
;
1817 struct smb2_tree_connect_rsp
*rsp
= work
->response_buf
;
1818 struct ksmbd_session
*sess
= work
->sess
;
1819 char *treename
= NULL
, *name
= NULL
;
1820 struct ksmbd_tree_conn_status status
;
1821 struct ksmbd_share_config
*share
;
1824 treename
= smb_strndup_from_utf16(req
->Buffer
,
1825 le16_to_cpu(req
->PathLength
), true,
1827 if (IS_ERR(treename
)) {
1828 pr_err("treename is NULL\n");
1829 status
.ret
= KSMBD_TREE_CONN_STATUS_ERROR
;
1833 name
= ksmbd_extract_sharename(treename
);
1835 status
.ret
= KSMBD_TREE_CONN_STATUS_ERROR
;
1839 ksmbd_debug(SMB
, "tree connect request for tree %s treename %s\n",
1842 status
= ksmbd_tree_conn_connect(sess
, name
);
1843 if (status
.ret
== KSMBD_TREE_CONN_STATUS_OK
)
1844 rsp
->hdr
.Id
.SyncId
.TreeId
= cpu_to_le32(status
.tree_conn
->id
);
1848 share
= status
.tree_conn
->share_conf
;
1849 if (test_share_config_flag(share
, KSMBD_SHARE_FLAG_PIPE
)) {
1850 ksmbd_debug(SMB
, "IPC share path request\n");
1851 rsp
->ShareType
= SMB2_SHARE_TYPE_PIPE
;
1852 rsp
->MaximalAccess
= FILE_READ_DATA_LE
| FILE_READ_EA_LE
|
1853 FILE_EXECUTE_LE
| FILE_READ_ATTRIBUTES_LE
|
1854 FILE_DELETE_LE
| FILE_READ_CONTROL_LE
|
1855 FILE_WRITE_DAC_LE
| FILE_WRITE_OWNER_LE
|
1856 FILE_SYNCHRONIZE_LE
;
1858 rsp
->ShareType
= SMB2_SHARE_TYPE_DISK
;
1859 rsp
->MaximalAccess
= FILE_READ_DATA_LE
| FILE_READ_EA_LE
|
1860 FILE_EXECUTE_LE
| FILE_READ_ATTRIBUTES_LE
;
1861 if (test_tree_conn_flag(status
.tree_conn
,
1862 KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
1863 rsp
->MaximalAccess
|= FILE_WRITE_DATA_LE
|
1864 FILE_APPEND_DATA_LE
| FILE_WRITE_EA_LE
|
1865 FILE_DELETE_LE
| FILE_WRITE_ATTRIBUTES_LE
|
1866 FILE_DELETE_CHILD_LE
| FILE_READ_CONTROL_LE
|
1867 FILE_WRITE_DAC_LE
| FILE_WRITE_OWNER_LE
|
1868 FILE_SYNCHRONIZE_LE
;
1872 status
.tree_conn
->maximal_access
= le32_to_cpu(rsp
->MaximalAccess
);
1873 if (conn
->posix_ext_supported
)
1874 status
.tree_conn
->posix_extensions
= true;
1877 rsp
->StructureSize
= cpu_to_le16(16);
1878 rsp
->Capabilities
= 0;
1880 /* default manual caching */
1881 rsp
->ShareFlags
= SMB2_SHAREFLAG_MANUAL_CACHING
;
1882 inc_rfc1001_len(rsp
, 16);
1884 if (!IS_ERR(treename
))
1889 switch (status
.ret
) {
1890 case KSMBD_TREE_CONN_STATUS_OK
:
1891 rsp
->hdr
.Status
= STATUS_SUCCESS
;
1894 case KSMBD_TREE_CONN_STATUS_NO_SHARE
:
1895 rsp
->hdr
.Status
= STATUS_BAD_NETWORK_PATH
;
1898 case KSMBD_TREE_CONN_STATUS_NOMEM
:
1899 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
1901 case KSMBD_TREE_CONN_STATUS_ERROR
:
1902 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS
:
1903 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS
:
1904 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
1907 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
1910 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
1917 * smb2_create_open_flags() - convert smb open flags to unix open flags
1918 * @file_present: is file already present
1919 * @access: file access flags
1920 * @disposition: file disposition flags
1921 * @may_flags: set with MAY_ flags
1923 * Return: file open flags
1925 static int smb2_create_open_flags(bool file_present
, __le32 access
,
1929 int oflags
= O_NONBLOCK
| O_LARGEFILE
;
1931 if (access
& FILE_READ_DESIRED_ACCESS_LE
&&
1932 access
& FILE_WRITE_DESIRE_ACCESS_LE
) {
1934 *may_flags
= MAY_OPEN
| MAY_READ
| MAY_WRITE
;
1935 } else if (access
& FILE_WRITE_DESIRE_ACCESS_LE
) {
1937 *may_flags
= MAY_OPEN
| MAY_WRITE
;
1940 *may_flags
= MAY_OPEN
| MAY_READ
;
1943 if (access
== FILE_READ_ATTRIBUTES_LE
)
1947 switch (disposition
& FILE_CREATE_MASK_LE
) {
1949 case FILE_CREATE_LE
:
1951 case FILE_SUPERSEDE_LE
:
1952 case FILE_OVERWRITE_LE
:
1953 case FILE_OVERWRITE_IF_LE
:
1960 switch (disposition
& FILE_CREATE_MASK_LE
) {
1961 case FILE_SUPERSEDE_LE
:
1962 case FILE_CREATE_LE
:
1963 case FILE_OPEN_IF_LE
:
1964 case FILE_OVERWRITE_IF_LE
:
1968 case FILE_OVERWRITE_LE
:
1980 * smb2_tree_disconnect() - handler for smb tree connect request
1981 * @work: smb work containing request buffer
1985 int smb2_tree_disconnect(struct ksmbd_work
*work
)
1987 struct smb2_tree_disconnect_rsp
*rsp
= work
->response_buf
;
1988 struct ksmbd_session
*sess
= work
->sess
;
1989 struct ksmbd_tree_connect
*tcon
= work
->tcon
;
1991 rsp
->StructureSize
= cpu_to_le16(4);
1992 inc_rfc1001_len(rsp
, 4);
1994 ksmbd_debug(SMB
, "request\n");
1997 struct smb2_tree_disconnect_req
*req
= work
->request_buf
;
1999 ksmbd_debug(SMB
, "Invalid tid %d\n", req
->hdr
.Id
.SyncId
.TreeId
);
2000 rsp
->hdr
.Status
= STATUS_NETWORK_NAME_DELETED
;
2001 smb2_set_err_rsp(work
);
2005 ksmbd_close_tree_conn_fds(work
);
2006 ksmbd_tree_conn_disconnect(sess
, tcon
);
2011 * smb2_session_logoff() - handler for session log off request
2012 * @work: smb work containing request buffer
2016 int smb2_session_logoff(struct ksmbd_work
*work
)
2018 struct ksmbd_conn
*conn
= work
->conn
;
2019 struct smb2_logoff_rsp
*rsp
= work
->response_buf
;
2020 struct ksmbd_session
*sess
= work
->sess
;
2022 rsp
->StructureSize
= cpu_to_le16(4);
2023 inc_rfc1001_len(rsp
, 4);
2025 ksmbd_debug(SMB
, "request\n");
2027 /* Got a valid session, set connection state */
2028 WARN_ON(sess
->conn
!= conn
);
2030 /* setting CifsExiting here may race with start_tcp_sess */
2031 ksmbd_conn_set_need_reconnect(work
);
2032 ksmbd_close_session_fds(work
);
2033 ksmbd_conn_wait_idle(conn
);
2035 if (ksmbd_tree_conn_session_logoff(sess
)) {
2036 struct smb2_logoff_req
*req
= work
->request_buf
;
2038 ksmbd_debug(SMB
, "Invalid tid %d\n", req
->hdr
.Id
.SyncId
.TreeId
);
2039 rsp
->hdr
.Status
= STATUS_NETWORK_NAME_DELETED
;
2040 smb2_set_err_rsp(work
);
2044 ksmbd_destroy_file_table(&sess
->file_table
);
2045 sess
->state
= SMB2_SESSION_EXPIRED
;
2047 ksmbd_free_user(sess
->user
);
2050 /* let start_tcp_sess free connection info now */
2051 ksmbd_conn_set_need_negotiate(work
);
2056 * create_smb2_pipe() - create IPC pipe
2057 * @work: smb work containing request buffer
2059 * Return: 0 on success, otherwise error
2061 static noinline
int create_smb2_pipe(struct ksmbd_work
*work
)
2063 struct smb2_create_rsp
*rsp
= work
->response_buf
;
2064 struct smb2_create_req
*req
= work
->request_buf
;
2069 name
= smb_strndup_from_utf16(req
->Buffer
, le16_to_cpu(req
->NameLength
),
2070 1, work
->conn
->local_nls
);
2072 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
2073 err
= PTR_ERR(name
);
2077 id
= ksmbd_session_rpc_open(work
->sess
, name
);
2079 pr_err("Unable to open RPC pipe: %d\n", id
);
2084 rsp
->hdr
.Status
= STATUS_SUCCESS
;
2085 rsp
->StructureSize
= cpu_to_le16(89);
2086 rsp
->OplockLevel
= SMB2_OPLOCK_LEVEL_NONE
;
2088 rsp
->CreateAction
= cpu_to_le32(FILE_OPENED
);
2090 rsp
->CreationTime
= cpu_to_le64(0);
2091 rsp
->LastAccessTime
= cpu_to_le64(0);
2092 rsp
->ChangeTime
= cpu_to_le64(0);
2093 rsp
->AllocationSize
= cpu_to_le64(0);
2094 rsp
->EndofFile
= cpu_to_le64(0);
2095 rsp
->FileAttributes
= ATTR_NORMAL_LE
;
2097 rsp
->VolatileFileId
= cpu_to_le64(id
);
2098 rsp
->PersistentFileId
= 0;
2099 rsp
->CreateContextsOffset
= 0;
2100 rsp
->CreateContextsLength
= 0;
2102 inc_rfc1001_len(rsp
, 88); /* StructureSize - 1*/
2109 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
2113 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
2120 smb2_set_err_rsp(work
);
2125 * smb2_set_ea() - handler for setting extended attributes using set
2127 * @eabuf: set info command buffer
2128 * @buf_len: set info command buffer length
2129 * @path: dentry path for get ea
2131 * Return: 0 on success, otherwise error
2133 static int smb2_set_ea(struct smb2_ea_info
*eabuf
, unsigned int buf_len
,
2136 struct user_namespace
*user_ns
= mnt_user_ns(path
->mnt
);
2137 char *attr_name
= NULL
, *value
;
2139 unsigned int next
= 0;
2141 if (buf_len
< sizeof(struct smb2_ea_info
) + eabuf
->EaNameLength
+
2142 le16_to_cpu(eabuf
->EaValueLength
))
2145 attr_name
= kmalloc(XATTR_NAME_MAX
+ 1, GFP_KERNEL
);
2150 if (!eabuf
->EaNameLength
)
2154 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2155 eabuf
->name
, eabuf
->EaNameLength
,
2156 le16_to_cpu(eabuf
->EaValueLength
),
2157 le32_to_cpu(eabuf
->NextEntryOffset
));
2159 if (eabuf
->EaNameLength
>
2160 (XATTR_NAME_MAX
- XATTR_USER_PREFIX_LEN
)) {
2165 memcpy(attr_name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
);
2166 memcpy(&attr_name
[XATTR_USER_PREFIX_LEN
], eabuf
->name
,
2167 eabuf
->EaNameLength
);
2168 attr_name
[XATTR_USER_PREFIX_LEN
+ eabuf
->EaNameLength
] = '\0';
2169 value
= (char *)&eabuf
->name
+ eabuf
->EaNameLength
+ 1;
2171 if (!eabuf
->EaValueLength
) {
2172 rc
= ksmbd_vfs_casexattr_len(user_ns
,
2175 XATTR_USER_PREFIX_LEN
+
2176 eabuf
->EaNameLength
);
2178 /* delete the EA only when it exits */
2180 rc
= ksmbd_vfs_remove_xattr(user_ns
,
2186 "remove xattr failed(%d)\n",
2192 /* if the EA doesn't exist, just do nothing. */
2195 rc
= ksmbd_vfs_setxattr(user_ns
,
2196 path
->dentry
, attr_name
, value
,
2197 le16_to_cpu(eabuf
->EaValueLength
), 0);
2200 "ksmbd_vfs_setxattr is failed(%d)\n",
2207 next
= le32_to_cpu(eabuf
->NextEntryOffset
);
2208 if (next
== 0 || buf_len
< next
)
2211 eabuf
= (struct smb2_ea_info
*)((char *)eabuf
+ next
);
2212 if (next
< (u32
)eabuf
->EaNameLength
+ le16_to_cpu(eabuf
->EaValueLength
))
2215 } while (next
!= 0);
2221 static noinline
int smb2_set_stream_name_xattr(struct path
*path
,
2222 struct ksmbd_file
*fp
,
2223 char *stream_name
, int s_type
)
2225 struct user_namespace
*user_ns
= mnt_user_ns(path
->mnt
);
2226 size_t xattr_stream_size
;
2227 char *xattr_stream_name
;
2230 rc
= ksmbd_vfs_xattr_stream_name(stream_name
,
2237 fp
->stream
.name
= xattr_stream_name
;
2238 fp
->stream
.size
= xattr_stream_size
;
2240 /* Check if there is stream prefix in xattr space */
2241 rc
= ksmbd_vfs_casexattr_len(user_ns
,
2248 if (fp
->cdoption
== FILE_OPEN_LE
) {
2249 ksmbd_debug(SMB
, "XATTR stream name lookup failed: %d\n", rc
);
2253 rc
= ksmbd_vfs_setxattr(user_ns
, path
->dentry
,
2254 xattr_stream_name
, NULL
, 0, 0);
2256 pr_err("Failed to store XATTR stream name :%d\n", rc
);
2260 static int smb2_remove_smb_xattrs(struct path
*path
)
2262 struct user_namespace
*user_ns
= mnt_user_ns(path
->mnt
);
2263 char *name
, *xattr_list
= NULL
;
2264 ssize_t xattr_list_len
;
2267 xattr_list_len
= ksmbd_vfs_listxattr(path
->dentry
, &xattr_list
);
2268 if (xattr_list_len
< 0) {
2270 } else if (!xattr_list_len
) {
2271 ksmbd_debug(SMB
, "empty xattr in the file\n");
2275 for (name
= xattr_list
; name
- xattr_list
< xattr_list_len
;
2276 name
+= strlen(name
) + 1) {
2277 ksmbd_debug(SMB
, "%s, len %zd\n", name
, strlen(name
));
2279 if (strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
) &&
2280 strncmp(&name
[XATTR_USER_PREFIX_LEN
], DOS_ATTRIBUTE_PREFIX
,
2281 DOS_ATTRIBUTE_PREFIX_LEN
) &&
2282 strncmp(&name
[XATTR_USER_PREFIX_LEN
], STREAM_PREFIX
, STREAM_PREFIX_LEN
))
2285 err
= ksmbd_vfs_remove_xattr(user_ns
, path
->dentry
, name
);
2287 ksmbd_debug(SMB
, "remove xattr failed : %s\n", name
);
2294 static int smb2_create_truncate(struct path
*path
)
2296 int rc
= vfs_truncate(path
, 0);
2299 pr_err("vfs_truncate failed, rc %d\n", rc
);
2303 rc
= smb2_remove_smb_xattrs(path
);
2304 if (rc
== -EOPNOTSUPP
)
2308 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2313 static void smb2_new_xattrs(struct ksmbd_tree_connect
*tcon
, struct path
*path
,
2314 struct ksmbd_file
*fp
)
2316 struct xattr_dos_attrib da
= {0};
2319 if (!test_share_config_flag(tcon
->share_conf
,
2320 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS
))
2324 da
.attr
= le32_to_cpu(fp
->f_ci
->m_fattr
);
2325 da
.itime
= da
.create_time
= fp
->create_time
;
2326 da
.flags
= XATTR_DOSINFO_ATTRIB
| XATTR_DOSINFO_CREATE_TIME
|
2327 XATTR_DOSINFO_ITIME
;
2329 rc
= ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path
->mnt
),
2332 ksmbd_debug(SMB
, "failed to store file attribute into xattr\n");
2335 static void smb2_update_xattrs(struct ksmbd_tree_connect
*tcon
,
2336 struct path
*path
, struct ksmbd_file
*fp
)
2338 struct xattr_dos_attrib da
;
2341 fp
->f_ci
->m_fattr
&= ~(ATTR_HIDDEN_LE
| ATTR_SYSTEM_LE
);
2343 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2344 if (!test_share_config_flag(tcon
->share_conf
,
2345 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS
))
2348 rc
= ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path
->mnt
),
2351 fp
->f_ci
->m_fattr
= cpu_to_le32(da
.attr
);
2352 fp
->create_time
= da
.create_time
;
2353 fp
->itime
= da
.itime
;
2357 static int smb2_creat(struct ksmbd_work
*work
, struct path
*path
, char *name
,
2358 int open_flags
, umode_t posix_mode
, bool is_dir
)
2360 struct ksmbd_tree_connect
*tcon
= work
->tcon
;
2361 struct ksmbd_share_config
*share
= tcon
->share_conf
;
2365 if (!(open_flags
& O_CREAT
))
2368 ksmbd_debug(SMB
, "file does not exist, so creating\n");
2369 if (is_dir
== true) {
2370 ksmbd_debug(SMB
, "creating directory\n");
2372 mode
= share_config_directory_mode(share
, posix_mode
);
2373 rc
= ksmbd_vfs_mkdir(work
, name
, mode
);
2377 ksmbd_debug(SMB
, "creating regular file\n");
2379 mode
= share_config_create_mode(share
, posix_mode
);
2380 rc
= ksmbd_vfs_create(work
, name
, mode
);
2385 rc
= ksmbd_vfs_kern_path(work
, name
, 0, path
, 0);
2387 pr_err("cannot get linux path (%s), err = %d\n",
2394 static int smb2_create_sd_buffer(struct ksmbd_work
*work
,
2395 struct smb2_create_req
*req
,
2398 struct create_context
*context
;
2399 struct create_sd_buf_req
*sd_buf
;
2401 if (!req
->CreateContextsOffset
)
2404 /* Parse SD BUFFER create contexts */
2405 context
= smb2_find_context_vals(req
, SMB2_CREATE_SD_BUFFER
);
2408 else if (IS_ERR(context
))
2409 return PTR_ERR(context
);
2412 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2413 sd_buf
= (struct create_sd_buf_req
*)context
;
2414 if (le16_to_cpu(context
->DataOffset
) +
2415 le32_to_cpu(context
->DataLength
) <
2416 sizeof(struct create_sd_buf_req
))
2418 return set_info_sec(work
->conn
, work
->tcon
, path
, &sd_buf
->ntsd
,
2419 le32_to_cpu(sd_buf
->ccontext
.DataLength
), true);
2422 static void ksmbd_acls_fattr(struct smb_fattr
*fattr
,
2423 struct user_namespace
*mnt_userns
,
2424 struct inode
*inode
)
2426 fattr
->cf_uid
= i_uid_into_mnt(mnt_userns
, inode
);
2427 fattr
->cf_gid
= i_gid_into_mnt(mnt_userns
, inode
);
2428 fattr
->cf_mode
= inode
->i_mode
;
2429 fattr
->cf_acls
= NULL
;
2430 fattr
->cf_dacls
= NULL
;
2432 if (IS_ENABLED(CONFIG_FS_POSIX_ACL
)) {
2433 fattr
->cf_acls
= get_acl(inode
, ACL_TYPE_ACCESS
);
2434 if (S_ISDIR(inode
->i_mode
))
2435 fattr
->cf_dacls
= get_acl(inode
, ACL_TYPE_DEFAULT
);
2440 * smb2_open() - handler for smb file open request
2441 * @work: smb work containing request buffer
2443 * Return: 0 on success, otherwise error
2445 int smb2_open(struct ksmbd_work
*work
)
2447 struct ksmbd_conn
*conn
= work
->conn
;
2448 struct ksmbd_session
*sess
= work
->sess
;
2449 struct ksmbd_tree_connect
*tcon
= work
->tcon
;
2450 struct smb2_create_req
*req
;
2451 struct smb2_create_rsp
*rsp
, *rsp_org
;
2453 struct ksmbd_share_config
*share
= tcon
->share_conf
;
2454 struct ksmbd_file
*fp
= NULL
;
2455 struct file
*filp
= NULL
;
2456 struct user_namespace
*user_ns
= NULL
;
2458 struct create_context
*context
;
2459 struct lease_ctx_info
*lc
= NULL
;
2460 struct create_ea_buf_req
*ea_buf
= NULL
;
2461 struct oplock_info
*opinfo
;
2462 __le32
*next_ptr
= NULL
;
2463 int req_op_level
= 0, open_flags
= 0, may_flags
= 0, file_info
= 0;
2465 int contxt_cnt
= 0, query_disk_id
= 0;
2466 int maximal_access_ctxt
= 0, posix_ctxt
= 0;
2470 char *stream_name
= NULL
;
2471 bool file_present
= false, created
= false, already_permitted
= false;
2472 int share_ret
, need_truncate
= 0;
2474 umode_t posix_mode
= 0;
2475 __le32 daccess
, maximal_access
= 0;
2477 rsp_org
= work
->response_buf
;
2478 WORK_BUFFERS(work
, req
, rsp
);
2480 if (req
->hdr
.NextCommand
&& !work
->next_smb2_rcv_hdr_off
&&
2481 (req
->hdr
.Flags
& SMB2_FLAGS_RELATED_OPERATIONS
)) {
2482 ksmbd_debug(SMB
, "invalid flag in chained command\n");
2483 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
2484 smb2_set_err_rsp(work
);
2488 if (test_share_config_flag(share
, KSMBD_SHARE_FLAG_PIPE
)) {
2489 ksmbd_debug(SMB
, "IPC pipe create request\n");
2490 return create_smb2_pipe(work
);
2493 if (req
->NameLength
) {
2494 if ((req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
) &&
2495 *(char *)req
->Buffer
== '\\') {
2496 pr_err("not allow directory name included leading slash\n");
2501 name
= smb2_get_name(share
,
2503 le16_to_cpu(req
->NameLength
),
2504 work
->conn
->local_nls
);
2513 ksmbd_debug(SMB
, "converted name = %s\n", name
);
2514 if (strchr(name
, ':')) {
2515 if (!test_share_config_flag(work
->tcon
->share_conf
,
2516 KSMBD_SHARE_FLAG_STREAMS
)) {
2520 rc
= parse_stream_name(name
, &stream_name
, &s_type
);
2525 rc
= ksmbd_validate_filename(name
);
2529 if (ksmbd_share_veto_filename(share
, name
)) {
2531 ksmbd_debug(SMB
, "Reject open(), vetoed file: %s\n",
2536 name
= kstrdup("", GFP_KERNEL
);
2543 req_op_level
= req
->RequestedOplockLevel
;
2544 if (req_op_level
== SMB2_OPLOCK_LEVEL_LEASE
)
2545 lc
= parse_lease_state(req
);
2547 if (le32_to_cpu(req
->ImpersonationLevel
) > le32_to_cpu(IL_DELEGATE_LE
)) {
2548 pr_err("Invalid impersonationlevel : 0x%x\n",
2549 le32_to_cpu(req
->ImpersonationLevel
));
2551 rsp
->hdr
.Status
= STATUS_BAD_IMPERSONATION_LEVEL
;
2555 if (req
->CreateOptions
&& !(req
->CreateOptions
& CREATE_OPTIONS_MASK
)) {
2556 pr_err("Invalid create options : 0x%x\n",
2557 le32_to_cpu(req
->CreateOptions
));
2561 if (req
->CreateOptions
& FILE_SEQUENTIAL_ONLY_LE
&&
2562 req
->CreateOptions
& FILE_RANDOM_ACCESS_LE
)
2563 req
->CreateOptions
= ~(FILE_SEQUENTIAL_ONLY_LE
);
2565 if (req
->CreateOptions
&
2566 (FILE_OPEN_BY_FILE_ID_LE
| CREATE_TREE_CONNECTION
|
2567 FILE_RESERVE_OPFILTER_LE
)) {
2572 if (req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
) {
2573 if (req
->CreateOptions
& FILE_NON_DIRECTORY_FILE_LE
) {
2576 } else if (req
->CreateOptions
& FILE_NO_COMPRESSION_LE
) {
2577 req
->CreateOptions
= ~(FILE_NO_COMPRESSION_LE
);
2582 if (le32_to_cpu(req
->CreateDisposition
) >
2583 le32_to_cpu(FILE_OVERWRITE_IF_LE
)) {
2584 pr_err("Invalid create disposition : 0x%x\n",
2585 le32_to_cpu(req
->CreateDisposition
));
2590 if (!(req
->DesiredAccess
& DESIRED_ACCESS_MASK
)) {
2591 pr_err("Invalid desired access : 0x%x\n",
2592 le32_to_cpu(req
->DesiredAccess
));
2597 if (req
->FileAttributes
&& !(req
->FileAttributes
& ATTR_MASK_LE
)) {
2598 pr_err("Invalid file attribute : 0x%x\n",
2599 le32_to_cpu(req
->FileAttributes
));
2604 if (req
->CreateContextsOffset
) {
2605 /* Parse non-durable handle create contexts */
2606 context
= smb2_find_context_vals(req
, SMB2_CREATE_EA_BUFFER
);
2607 if (IS_ERR(context
)) {
2608 rc
= PTR_ERR(context
);
2610 } else if (context
) {
2611 ea_buf
= (struct create_ea_buf_req
*)context
;
2612 if (le16_to_cpu(context
->DataOffset
) +
2613 le32_to_cpu(context
->DataLength
) <
2614 sizeof(struct create_ea_buf_req
)) {
2618 if (req
->CreateOptions
& FILE_NO_EA_KNOWLEDGE_LE
) {
2619 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
2625 context
= smb2_find_context_vals(req
,
2626 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST
);
2627 if (IS_ERR(context
)) {
2628 rc
= PTR_ERR(context
);
2630 } else if (context
) {
2632 "get query maximal access context\n");
2633 maximal_access_ctxt
= 1;
2636 context
= smb2_find_context_vals(req
,
2637 SMB2_CREATE_TIMEWARP_REQUEST
);
2638 if (IS_ERR(context
)) {
2639 rc
= PTR_ERR(context
);
2641 } else if (context
) {
2642 ksmbd_debug(SMB
, "get timewarp context\n");
2647 if (tcon
->posix_extensions
) {
2648 context
= smb2_find_context_vals(req
,
2649 SMB2_CREATE_TAG_POSIX
);
2650 if (IS_ERR(context
)) {
2651 rc
= PTR_ERR(context
);
2653 } else if (context
) {
2654 struct create_posix
*posix
=
2655 (struct create_posix
*)context
;
2656 if (le16_to_cpu(context
->DataOffset
) +
2657 le32_to_cpu(context
->DataLength
) <
2658 sizeof(struct create_posix
)) {
2662 ksmbd_debug(SMB
, "get posix context\n");
2664 posix_mode
= le32_to_cpu(posix
->Mode
);
2670 if (ksmbd_override_fsids(work
)) {
2675 rc
= ksmbd_vfs_kern_path(work
, name
, LOOKUP_NO_SYMLINKS
, &path
, 1);
2677 if (req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
) {
2679 * If file exists with under flags, return access
2682 if (req
->CreateDisposition
== FILE_OVERWRITE_IF_LE
||
2683 req
->CreateDisposition
== FILE_OPEN_IF_LE
) {
2689 if (!test_tree_conn_flag(tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
2691 "User does not have write permission\n");
2696 } else if (d_is_symlink(path
.dentry
)) {
2706 ksmbd_debug(SMB
, "can not get linux path for %s, rc = %d\n",
2710 file_present
= true;
2711 user_ns
= mnt_user_ns(path
.mnt
);
2712 generic_fillattr(user_ns
, d_inode(path
.dentry
), &stat
);
2715 if (req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
) {
2716 if (s_type
== DATA_STREAM
) {
2718 rsp
->hdr
.Status
= STATUS_NOT_A_DIRECTORY
;
2721 if (S_ISDIR(stat
.mode
) && s_type
== DATA_STREAM
) {
2723 rsp
->hdr
.Status
= STATUS_FILE_IS_A_DIRECTORY
;
2727 if (req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
&&
2728 req
->FileAttributes
& ATTR_NORMAL_LE
) {
2729 rsp
->hdr
.Status
= STATUS_NOT_A_DIRECTORY
;
2737 if (file_present
&& req
->CreateOptions
& FILE_NON_DIRECTORY_FILE_LE
&&
2738 S_ISDIR(stat
.mode
) && !(req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
)) {
2739 ksmbd_debug(SMB
, "open() argument is a directory: %s, %x\n",
2740 name
, req
->CreateOptions
);
2741 rsp
->hdr
.Status
= STATUS_FILE_IS_A_DIRECTORY
;
2746 if (file_present
&& (req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
) &&
2747 !(req
->CreateDisposition
== FILE_CREATE_LE
) &&
2748 !S_ISDIR(stat
.mode
)) {
2749 rsp
->hdr
.Status
= STATUS_NOT_A_DIRECTORY
;
2754 if (!stream_name
&& file_present
&&
2755 req
->CreateDisposition
== FILE_CREATE_LE
) {
2760 daccess
= smb_map_generic_desired_access(req
->DesiredAccess
);
2762 if (file_present
&& !(req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
)) {
2763 rc
= smb_check_perm_dacl(conn
, &path
, &daccess
,
2769 if (daccess
& FILE_MAXIMAL_ACCESS_LE
) {
2770 if (!file_present
) {
2771 daccess
= cpu_to_le32(GENERIC_ALL_FLAGS
);
2773 rc
= ksmbd_vfs_query_maximal_access(user_ns
,
2778 already_permitted
= true;
2780 maximal_access
= daccess
;
2783 open_flags
= smb2_create_open_flags(file_present
, daccess
,
2784 req
->CreateDisposition
,
2787 if (!test_tree_conn_flag(tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
2788 if (open_flags
& O_CREAT
) {
2790 "User does not have write permission\n");
2796 /*create file if not present */
2797 if (!file_present
) {
2798 rc
= smb2_creat(work
, &path
, name
, open_flags
, posix_mode
,
2799 req
->CreateOptions
& FILE_DIRECTORY_FILE_LE
);
2801 if (rc
== -ENOENT
) {
2803 rsp
->hdr
.Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
2809 user_ns
= mnt_user_ns(path
.mnt
);
2811 if (le32_to_cpu(ea_buf
->ccontext
.DataLength
) <
2812 sizeof(struct smb2_ea_info
)) {
2817 rc
= smb2_set_ea(&ea_buf
->ea
,
2818 le32_to_cpu(ea_buf
->ccontext
.DataLength
),
2820 if (rc
== -EOPNOTSUPP
)
2825 } else if (!already_permitted
) {
2826 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2827 * because execute(search) permission on a parent directory,
2828 * is already granted.
2830 if (daccess
& ~(FILE_READ_ATTRIBUTES_LE
| FILE_READ_CONTROL_LE
)) {
2831 rc
= inode_permission(user_ns
,
2832 d_inode(path
.dentry
),
2837 if ((daccess
& FILE_DELETE_LE
) ||
2838 (req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
)) {
2839 rc
= ksmbd_vfs_may_delete(user_ns
,
2847 rc
= ksmbd_query_inode_status(d_inode(path
.dentry
->d_parent
));
2848 if (rc
== KSMBD_INODE_STATUS_PENDING_DELETE
) {
2854 filp
= dentry_open(&path
, open_flags
, current_cred());
2857 pr_err("dentry open for dir failed, rc %d\n", rc
);
2862 if (!(open_flags
& O_TRUNC
))
2863 file_info
= FILE_OPENED
;
2865 file_info
= FILE_OVERWRITTEN
;
2867 if ((req
->CreateDisposition
& FILE_CREATE_MASK_LE
) ==
2869 file_info
= FILE_SUPERSEDED
;
2870 } else if (open_flags
& O_CREAT
) {
2871 file_info
= FILE_CREATED
;
2874 ksmbd_vfs_set_fadvise(filp
, req
->CreateOptions
);
2876 /* Obtain Volatile-ID */
2877 fp
= ksmbd_open_fd(work
, filp
);
2885 /* Get Persistent-ID */
2886 ksmbd_open_durable_fd(fp
);
2887 if (!has_file_id(fp
->persistent_id
)) {
2892 fp
->filename
= name
;
2893 fp
->cdoption
= req
->CreateDisposition
;
2894 fp
->daccess
= daccess
;
2895 fp
->saccess
= req
->ShareAccess
;
2896 fp
->coption
= req
->CreateOptions
;
2898 /* Set default windows and posix acls if creating new file */
2901 struct inode
*inode
= d_inode(path
.dentry
);
2903 posix_acl_rc
= ksmbd_vfs_inherit_posix_acl(user_ns
,
2905 d_inode(path
.dentry
->d_parent
));
2907 ksmbd_debug(SMB
, "inherit posix acl failed : %d\n", posix_acl_rc
);
2909 if (test_share_config_flag(work
->tcon
->share_conf
,
2910 KSMBD_SHARE_FLAG_ACL_XATTR
)) {
2911 rc
= smb_inherit_dacl(conn
, &path
, sess
->user
->uid
,
2916 rc
= smb2_create_sd_buffer(work
, req
, &path
);
2919 ksmbd_vfs_set_init_posix_acl(user_ns
,
2922 if (test_share_config_flag(work
->tcon
->share_conf
,
2923 KSMBD_SHARE_FLAG_ACL_XATTR
)) {
2924 struct smb_fattr fattr
;
2925 struct smb_ntsd
*pntsd
;
2926 int pntsd_size
, ace_num
= 0;
2928 ksmbd_acls_fattr(&fattr
, user_ns
, inode
);
2930 ace_num
= fattr
.cf_acls
->a_count
;
2932 ace_num
+= fattr
.cf_dacls
->a_count
;
2934 pntsd
= kmalloc(sizeof(struct smb_ntsd
) +
2935 sizeof(struct smb_sid
) * 3 +
2936 sizeof(struct smb_acl
) +
2937 sizeof(struct smb_ace
) * ace_num
* 2,
2942 rc
= build_sec_desc(user_ns
,
2947 &pntsd_size
, &fattr
);
2948 posix_acl_release(fattr
.cf_acls
);
2949 posix_acl_release(fattr
.cf_dacls
);
2951 rc
= ksmbd_vfs_set_sd_xattr(conn
,
2958 pr_err("failed to store ntacl in xattr : %d\n",
2967 rc
= smb2_set_stream_name_xattr(&path
,
2973 file_info
= FILE_CREATED
;
2976 fp
->attrib_only
= !(req
->DesiredAccess
& ~(FILE_READ_ATTRIBUTES_LE
|
2977 FILE_WRITE_ATTRIBUTES_LE
| FILE_SYNCHRONIZE_LE
));
2978 if (!S_ISDIR(file_inode(filp
)->i_mode
) && open_flags
& O_TRUNC
&&
2979 !fp
->attrib_only
&& !stream_name
) {
2980 smb_break_all_oplock(work
, fp
);
2984 /* fp should be searchable through ksmbd_inode.m_fp_list
2985 * after daccess, saccess, attrib_only, and stream are
2988 write_lock(&fp
->f_ci
->m_lock
);
2989 list_add(&fp
->node
, &fp
->f_ci
->m_fp_list
);
2990 write_unlock(&fp
->f_ci
->m_lock
);
2992 rc
= ksmbd_vfs_getattr(&path
, &stat
);
2994 generic_fillattr(user_ns
, d_inode(path
.dentry
), &stat
);
2998 /* Check delete pending among previous fp before oplock break */
2999 if (ksmbd_inode_pending_delete(fp
)) {
3004 share_ret
= ksmbd_smb_check_shared_mode(fp
->filp
, fp
);
3005 if (!test_share_config_flag(work
->tcon
->share_conf
, KSMBD_SHARE_FLAG_OPLOCKS
) ||
3006 (req_op_level
== SMB2_OPLOCK_LEVEL_LEASE
&&
3007 !(conn
->vals
->capabilities
& SMB2_GLOBAL_CAP_LEASING
))) {
3008 if (share_ret
< 0 && !S_ISDIR(file_inode(fp
->filp
)->i_mode
)) {
3013 if (req_op_level
== SMB2_OPLOCK_LEVEL_LEASE
) {
3014 req_op_level
= smb2_map_lease_to_oplock(lc
->req_state
);
3016 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
3017 name
, req_op_level
, lc
->req_state
);
3018 rc
= find_same_lease_key(sess
, fp
->f_ci
, lc
);
3021 } else if (open_flags
== O_RDONLY
&&
3022 (req_op_level
== SMB2_OPLOCK_LEVEL_BATCH
||
3023 req_op_level
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
))
3024 req_op_level
= SMB2_OPLOCK_LEVEL_II
;
3026 rc
= smb_grant_oplock(work
, req_op_level
,
3027 fp
->persistent_id
, fp
,
3028 le32_to_cpu(req
->hdr
.Id
.SyncId
.TreeId
),
3034 if (req
->CreateOptions
& FILE_DELETE_ON_CLOSE_LE
)
3035 ksmbd_fd_set_delete_on_close(fp
, file_info
);
3037 if (need_truncate
) {
3038 rc
= smb2_create_truncate(&path
);
3043 if (req
->CreateContextsOffset
) {
3044 struct create_alloc_size_req
*az_req
;
3046 az_req
= (struct create_alloc_size_req
*)smb2_find_context_vals(req
,
3047 SMB2_CREATE_ALLOCATION_SIZE
);
3048 if (IS_ERR(az_req
)) {
3049 rc
= PTR_ERR(az_req
);
3051 } else if (az_req
) {
3055 if (le16_to_cpu(az_req
->ccontext
.DataOffset
) +
3056 le32_to_cpu(az_req
->ccontext
.DataLength
) <
3057 sizeof(struct create_alloc_size_req
)) {
3061 alloc_size
= le64_to_cpu(az_req
->AllocationSize
);
3063 "request smb2 create allocate size : %llu\n",
3065 smb_break_all_levII_oplock(work
, fp
, 1);
3066 err
= vfs_fallocate(fp
->filp
, FALLOC_FL_KEEP_SIZE
, 0,
3070 "vfs_fallocate is failed : %d\n",
3074 context
= smb2_find_context_vals(req
, SMB2_CREATE_QUERY_ON_DISK_ID
);
3075 if (IS_ERR(context
)) {
3076 rc
= PTR_ERR(context
);
3078 } else if (context
) {
3079 ksmbd_debug(SMB
, "get query on disk id context\n");
3084 if (stat
.result_mask
& STATX_BTIME
)
3085 fp
->create_time
= ksmbd_UnixTimeToNT(stat
.btime
);
3087 fp
->create_time
= ksmbd_UnixTimeToNT(stat
.ctime
);
3088 if (req
->FileAttributes
|| fp
->f_ci
->m_fattr
== 0)
3090 cpu_to_le32(smb2_get_dos_mode(&stat
, le32_to_cpu(req
->FileAttributes
)));
3093 smb2_update_xattrs(tcon
, &path
, fp
);
3095 smb2_new_xattrs(tcon
, &path
, fp
);
3097 memcpy(fp
->client_guid
, conn
->ClientGUID
, SMB2_CLIENT_GUID_SIZE
);
3099 generic_fillattr(user_ns
, file_inode(fp
->filp
),
3102 rsp
->StructureSize
= cpu_to_le16(89);
3104 opinfo
= rcu_dereference(fp
->f_opinfo
);
3105 rsp
->OplockLevel
= opinfo
!= NULL
? opinfo
->level
: 0;
3108 rsp
->CreateAction
= cpu_to_le32(file_info
);
3109 rsp
->CreationTime
= cpu_to_le64(fp
->create_time
);
3110 time
= ksmbd_UnixTimeToNT(stat
.atime
);
3111 rsp
->LastAccessTime
= cpu_to_le64(time
);
3112 time
= ksmbd_UnixTimeToNT(stat
.mtime
);
3113 rsp
->LastWriteTime
= cpu_to_le64(time
);
3114 time
= ksmbd_UnixTimeToNT(stat
.ctime
);
3115 rsp
->ChangeTime
= cpu_to_le64(time
);
3116 rsp
->AllocationSize
= S_ISDIR(stat
.mode
) ? 0 :
3117 cpu_to_le64(stat
.blocks
<< 9);
3118 rsp
->EndofFile
= S_ISDIR(stat
.mode
) ? 0 : cpu_to_le64(stat
.size
);
3119 rsp
->FileAttributes
= fp
->f_ci
->m_fattr
;
3123 rsp
->PersistentFileId
= cpu_to_le64(fp
->persistent_id
);
3124 rsp
->VolatileFileId
= cpu_to_le64(fp
->volatile_id
);
3126 rsp
->CreateContextsOffset
= 0;
3127 rsp
->CreateContextsLength
= 0;
3128 inc_rfc1001_len(rsp_org
, 88); /* StructureSize - 1*/
3130 /* If lease is request send lease context response */
3131 if (opinfo
&& opinfo
->is_lease
) {
3132 struct create_context
*lease_ccontext
;
3134 ksmbd_debug(SMB
, "lease granted on(%s) lease state 0x%x\n",
3135 name
, opinfo
->o_lease
->state
);
3136 rsp
->OplockLevel
= SMB2_OPLOCK_LEVEL_LEASE
;
3138 lease_ccontext
= (struct create_context
*)rsp
->Buffer
;
3140 create_lease_buf(rsp
->Buffer
, opinfo
->o_lease
);
3141 le32_add_cpu(&rsp
->CreateContextsLength
,
3142 conn
->vals
->create_lease_size
);
3143 inc_rfc1001_len(rsp_org
, conn
->vals
->create_lease_size
);
3144 next_ptr
= &lease_ccontext
->Next
;
3145 next_off
= conn
->vals
->create_lease_size
;
3148 if (maximal_access_ctxt
) {
3149 struct create_context
*mxac_ccontext
;
3151 if (maximal_access
== 0)
3152 ksmbd_vfs_query_maximal_access(user_ns
,
3155 mxac_ccontext
= (struct create_context
*)(rsp
->Buffer
+
3156 le32_to_cpu(rsp
->CreateContextsLength
));
3158 create_mxac_rsp_buf(rsp
->Buffer
+
3159 le32_to_cpu(rsp
->CreateContextsLength
),
3160 le32_to_cpu(maximal_access
));
3161 le32_add_cpu(&rsp
->CreateContextsLength
,
3162 conn
->vals
->create_mxac_size
);
3163 inc_rfc1001_len(rsp_org
, conn
->vals
->create_mxac_size
);
3165 *next_ptr
= cpu_to_le32(next_off
);
3166 next_ptr
= &mxac_ccontext
->Next
;
3167 next_off
= conn
->vals
->create_mxac_size
;
3170 if (query_disk_id
) {
3171 struct create_context
*disk_id_ccontext
;
3173 disk_id_ccontext
= (struct create_context
*)(rsp
->Buffer
+
3174 le32_to_cpu(rsp
->CreateContextsLength
));
3176 create_disk_id_rsp_buf(rsp
->Buffer
+
3177 le32_to_cpu(rsp
->CreateContextsLength
),
3178 stat
.ino
, tcon
->id
);
3179 le32_add_cpu(&rsp
->CreateContextsLength
,
3180 conn
->vals
->create_disk_id_size
);
3181 inc_rfc1001_len(rsp_org
, conn
->vals
->create_disk_id_size
);
3183 *next_ptr
= cpu_to_le32(next_off
);
3184 next_ptr
= &disk_id_ccontext
->Next
;
3185 next_off
= conn
->vals
->create_disk_id_size
;
3190 create_posix_rsp_buf(rsp
->Buffer
+
3191 le32_to_cpu(rsp
->CreateContextsLength
),
3193 le32_add_cpu(&rsp
->CreateContextsLength
,
3194 conn
->vals
->create_posix_size
);
3195 inc_rfc1001_len(rsp_org
, conn
->vals
->create_posix_size
);
3197 *next_ptr
= cpu_to_le32(next_off
);
3200 if (contxt_cnt
> 0) {
3201 rsp
->CreateContextsOffset
=
3202 cpu_to_le32(offsetof(struct smb2_create_rsp
, Buffer
)
3207 if (file_present
|| created
)
3209 ksmbd_revert_fsids(work
);
3213 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
3214 else if (rc
== -EOPNOTSUPP
)
3215 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
3216 else if (rc
== -EACCES
|| rc
== -ESTALE
|| rc
== -EXDEV
)
3217 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
3218 else if (rc
== -ENOENT
)
3219 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_INVALID
;
3220 else if (rc
== -EPERM
)
3221 rsp
->hdr
.Status
= STATUS_SHARING_VIOLATION
;
3222 else if (rc
== -EBUSY
)
3223 rsp
->hdr
.Status
= STATUS_DELETE_PENDING
;
3224 else if (rc
== -EBADF
)
3225 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
3226 else if (rc
== -ENOEXEC
)
3227 rsp
->hdr
.Status
= STATUS_DUPLICATE_OBJECTID
;
3228 else if (rc
== -ENXIO
)
3229 rsp
->hdr
.Status
= STATUS_NO_SUCH_DEVICE
;
3230 else if (rc
== -EEXIST
)
3231 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_COLLISION
;
3232 else if (rc
== -EMFILE
)
3233 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
3234 if (!rsp
->hdr
.Status
)
3235 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
3237 if (!fp
|| !fp
->filename
)
3240 ksmbd_fd_put(work
, fp
);
3241 smb2_set_err_rsp(work
);
3242 ksmbd_debug(SMB
, "Error response: %x\n", rsp
->hdr
.Status
);
3250 static int readdir_info_level_struct_sz(int info_level
)
3252 switch (info_level
) {
3253 case FILE_FULL_DIRECTORY_INFORMATION
:
3254 return sizeof(struct file_full_directory_info
);
3255 case FILE_BOTH_DIRECTORY_INFORMATION
:
3256 return sizeof(struct file_both_directory_info
);
3257 case FILE_DIRECTORY_INFORMATION
:
3258 return sizeof(struct file_directory_info
);
3259 case FILE_NAMES_INFORMATION
:
3260 return sizeof(struct file_names_info
);
3261 case FILEID_FULL_DIRECTORY_INFORMATION
:
3262 return sizeof(struct file_id_full_dir_info
);
3263 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3264 return sizeof(struct file_id_both_directory_info
);
3265 case SMB_FIND_FILE_POSIX_INFO
:
3266 return sizeof(struct smb2_posix_info
);
3272 static int dentry_name(struct ksmbd_dir_info
*d_info
, int info_level
)
3274 switch (info_level
) {
3275 case FILE_FULL_DIRECTORY_INFORMATION
:
3277 struct file_full_directory_info
*ffdinfo
;
3279 ffdinfo
= (struct file_full_directory_info
*)d_info
->rptr
;
3280 d_info
->rptr
+= le32_to_cpu(ffdinfo
->NextEntryOffset
);
3281 d_info
->name
= ffdinfo
->FileName
;
3282 d_info
->name_len
= le32_to_cpu(ffdinfo
->FileNameLength
);
3285 case FILE_BOTH_DIRECTORY_INFORMATION
:
3287 struct file_both_directory_info
*fbdinfo
;
3289 fbdinfo
= (struct file_both_directory_info
*)d_info
->rptr
;
3290 d_info
->rptr
+= le32_to_cpu(fbdinfo
->NextEntryOffset
);
3291 d_info
->name
= fbdinfo
->FileName
;
3292 d_info
->name_len
= le32_to_cpu(fbdinfo
->FileNameLength
);
3295 case FILE_DIRECTORY_INFORMATION
:
3297 struct file_directory_info
*fdinfo
;
3299 fdinfo
= (struct file_directory_info
*)d_info
->rptr
;
3300 d_info
->rptr
+= le32_to_cpu(fdinfo
->NextEntryOffset
);
3301 d_info
->name
= fdinfo
->FileName
;
3302 d_info
->name_len
= le32_to_cpu(fdinfo
->FileNameLength
);
3305 case FILE_NAMES_INFORMATION
:
3307 struct file_names_info
*fninfo
;
3309 fninfo
= (struct file_names_info
*)d_info
->rptr
;
3310 d_info
->rptr
+= le32_to_cpu(fninfo
->NextEntryOffset
);
3311 d_info
->name
= fninfo
->FileName
;
3312 d_info
->name_len
= le32_to_cpu(fninfo
->FileNameLength
);
3315 case FILEID_FULL_DIRECTORY_INFORMATION
:
3317 struct file_id_full_dir_info
*dinfo
;
3319 dinfo
= (struct file_id_full_dir_info
*)d_info
->rptr
;
3320 d_info
->rptr
+= le32_to_cpu(dinfo
->NextEntryOffset
);
3321 d_info
->name
= dinfo
->FileName
;
3322 d_info
->name_len
= le32_to_cpu(dinfo
->FileNameLength
);
3325 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3327 struct file_id_both_directory_info
*fibdinfo
;
3329 fibdinfo
= (struct file_id_both_directory_info
*)d_info
->rptr
;
3330 d_info
->rptr
+= le32_to_cpu(fibdinfo
->NextEntryOffset
);
3331 d_info
->name
= fibdinfo
->FileName
;
3332 d_info
->name_len
= le32_to_cpu(fibdinfo
->FileNameLength
);
3335 case SMB_FIND_FILE_POSIX_INFO
:
3337 struct smb2_posix_info
*posix_info
;
3339 posix_info
= (struct smb2_posix_info
*)d_info
->rptr
;
3340 d_info
->rptr
+= le32_to_cpu(posix_info
->NextEntryOffset
);
3341 d_info
->name
= posix_info
->name
;
3342 d_info
->name_len
= le32_to_cpu(posix_info
->name_len
);
3351 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3353 * @conn: connection instance
3354 * @info_level: smb information level
3355 * @d_info: structure included variables for query dir
3356 * @user_ns: user namespace
3357 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3359 * if directory has many entries, find first can't read it fully.
3360 * find next might be called multiple times to read remaining dir entries
3362 * Return: 0 on success, otherwise error
3364 static int smb2_populate_readdir_entry(struct ksmbd_conn
*conn
, int info_level
,
3365 struct ksmbd_dir_info
*d_info
,
3366 struct ksmbd_kstat
*ksmbd_kstat
)
3368 int next_entry_offset
= 0;
3372 int struct_sz
, rc
= 0;
3374 conv_name
= ksmbd_convert_dir_info_name(d_info
,
3380 /* Somehow the name has only terminating NULL bytes */
3383 goto free_conv_name
;
3386 struct_sz
= readdir_info_level_struct_sz(info_level
);
3387 next_entry_offset
= ALIGN(struct_sz
- 1 + conv_len
,
3388 KSMBD_DIR_INFO_ALIGNMENT
);
3390 if (next_entry_offset
> d_info
->out_buf_len
) {
3391 d_info
->out_buf_len
= 0;
3393 goto free_conv_name
;
3396 kstat
= d_info
->wptr
;
3397 if (info_level
!= FILE_NAMES_INFORMATION
)
3398 kstat
= ksmbd_vfs_init_kstat(&d_info
->wptr
, ksmbd_kstat
);
3400 switch (info_level
) {
3401 case FILE_FULL_DIRECTORY_INFORMATION
:
3403 struct file_full_directory_info
*ffdinfo
;
3405 ffdinfo
= (struct file_full_directory_info
*)kstat
;
3406 ffdinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3408 smb2_get_reparse_tag_special_file(ksmbd_kstat
->kstat
->mode
);
3409 if (ffdinfo
->EaSize
)
3410 ffdinfo
->ExtFileAttributes
= ATTR_REPARSE_POINT_LE
;
3411 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3412 ffdinfo
->ExtFileAttributes
|= ATTR_HIDDEN_LE
;
3413 memcpy(ffdinfo
->FileName
, conv_name
, conv_len
);
3414 ffdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3417 case FILE_BOTH_DIRECTORY_INFORMATION
:
3419 struct file_both_directory_info
*fbdinfo
;
3421 fbdinfo
= (struct file_both_directory_info
*)kstat
;
3422 fbdinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3424 smb2_get_reparse_tag_special_file(ksmbd_kstat
->kstat
->mode
);
3425 if (fbdinfo
->EaSize
)
3426 fbdinfo
->ExtFileAttributes
= ATTR_REPARSE_POINT_LE
;
3427 fbdinfo
->ShortNameLength
= 0;
3428 fbdinfo
->Reserved
= 0;
3429 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3430 fbdinfo
->ExtFileAttributes
|= ATTR_HIDDEN_LE
;
3431 memcpy(fbdinfo
->FileName
, conv_name
, conv_len
);
3432 fbdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3435 case FILE_DIRECTORY_INFORMATION
:
3437 struct file_directory_info
*fdinfo
;
3439 fdinfo
= (struct file_directory_info
*)kstat
;
3440 fdinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3441 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3442 fdinfo
->ExtFileAttributes
|= ATTR_HIDDEN_LE
;
3443 memcpy(fdinfo
->FileName
, conv_name
, conv_len
);
3444 fdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3447 case FILE_NAMES_INFORMATION
:
3449 struct file_names_info
*fninfo
;
3451 fninfo
= (struct file_names_info
*)kstat
;
3452 fninfo
->FileNameLength
= cpu_to_le32(conv_len
);
3453 memcpy(fninfo
->FileName
, conv_name
, conv_len
);
3454 fninfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3457 case FILEID_FULL_DIRECTORY_INFORMATION
:
3459 struct file_id_full_dir_info
*dinfo
;
3461 dinfo
= (struct file_id_full_dir_info
*)kstat
;
3462 dinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3464 smb2_get_reparse_tag_special_file(ksmbd_kstat
->kstat
->mode
);
3466 dinfo
->ExtFileAttributes
= ATTR_REPARSE_POINT_LE
;
3467 dinfo
->Reserved
= 0;
3468 dinfo
->UniqueId
= cpu_to_le64(ksmbd_kstat
->kstat
->ino
);
3469 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3470 dinfo
->ExtFileAttributes
|= ATTR_HIDDEN_LE
;
3471 memcpy(dinfo
->FileName
, conv_name
, conv_len
);
3472 dinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3475 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3477 struct file_id_both_directory_info
*fibdinfo
;
3479 fibdinfo
= (struct file_id_both_directory_info
*)kstat
;
3480 fibdinfo
->FileNameLength
= cpu_to_le32(conv_len
);
3482 smb2_get_reparse_tag_special_file(ksmbd_kstat
->kstat
->mode
);
3483 if (fibdinfo
->EaSize
)
3484 fibdinfo
->ExtFileAttributes
= ATTR_REPARSE_POINT_LE
;
3485 fibdinfo
->UniqueId
= cpu_to_le64(ksmbd_kstat
->kstat
->ino
);
3486 fibdinfo
->ShortNameLength
= 0;
3487 fibdinfo
->Reserved
= 0;
3488 fibdinfo
->Reserved2
= cpu_to_le16(0);
3489 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3490 fibdinfo
->ExtFileAttributes
|= ATTR_HIDDEN_LE
;
3491 memcpy(fibdinfo
->FileName
, conv_name
, conv_len
);
3492 fibdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3495 case SMB_FIND_FILE_POSIX_INFO
:
3497 struct smb2_posix_info
*posix_info
;
3500 posix_info
= (struct smb2_posix_info
*)kstat
;
3501 posix_info
->Ignored
= 0;
3502 posix_info
->CreationTime
= cpu_to_le64(ksmbd_kstat
->create_time
);
3503 time
= ksmbd_UnixTimeToNT(ksmbd_kstat
->kstat
->ctime
);
3504 posix_info
->ChangeTime
= cpu_to_le64(time
);
3505 time
= ksmbd_UnixTimeToNT(ksmbd_kstat
->kstat
->atime
);
3506 posix_info
->LastAccessTime
= cpu_to_le64(time
);
3507 time
= ksmbd_UnixTimeToNT(ksmbd_kstat
->kstat
->mtime
);
3508 posix_info
->LastWriteTime
= cpu_to_le64(time
);
3509 posix_info
->EndOfFile
= cpu_to_le64(ksmbd_kstat
->kstat
->size
);
3510 posix_info
->AllocationSize
= cpu_to_le64(ksmbd_kstat
->kstat
->blocks
<< 9);
3511 posix_info
->DeviceId
= cpu_to_le32(ksmbd_kstat
->kstat
->rdev
);
3512 posix_info
->HardLinks
= cpu_to_le32(ksmbd_kstat
->kstat
->nlink
);
3513 posix_info
->Mode
= cpu_to_le32(ksmbd_kstat
->kstat
->mode
);
3514 posix_info
->Inode
= cpu_to_le64(ksmbd_kstat
->kstat
->ino
);
3515 posix_info
->DosAttributes
=
3516 S_ISDIR(ksmbd_kstat
->kstat
->mode
) ? ATTR_DIRECTORY_LE
: ATTR_ARCHIVE_LE
;
3517 if (d_info
->hide_dot_file
&& d_info
->name
[0] == '.')
3518 posix_info
->DosAttributes
|= ATTR_HIDDEN_LE
;
3519 id_to_sid(from_kuid_munged(&init_user_ns
, ksmbd_kstat
->kstat
->uid
),
3520 SIDNFS_USER
, (struct smb_sid
*)&posix_info
->SidBuffer
[0]);
3521 id_to_sid(from_kgid_munged(&init_user_ns
, ksmbd_kstat
->kstat
->gid
),
3522 SIDNFS_GROUP
, (struct smb_sid
*)&posix_info
->SidBuffer
[20]);
3523 memcpy(posix_info
->name
, conv_name
, conv_len
);
3524 posix_info
->name_len
= cpu_to_le32(conv_len
);
3525 posix_info
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3529 } /* switch (info_level) */
3531 d_info
->last_entry_offset
= d_info
->data_count
;
3532 d_info
->data_count
+= next_entry_offset
;
3533 d_info
->out_buf_len
-= next_entry_offset
;
3534 d_info
->wptr
+= next_entry_offset
;
3537 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3538 info_level
, d_info
->out_buf_len
,
3539 next_entry_offset
, d_info
->data_count
);
3546 struct smb2_query_dir_private
{
3547 struct ksmbd_work
*work
;
3548 char *search_pattern
;
3549 struct ksmbd_file
*dir_fp
;
3551 struct ksmbd_dir_info
*d_info
;
3555 static void lock_dir(struct ksmbd_file
*dir_fp
)
3557 struct dentry
*dir
= dir_fp
->filp
->f_path
.dentry
;
3559 inode_lock_nested(d_inode(dir
), I_MUTEX_PARENT
);
3562 static void unlock_dir(struct ksmbd_file
*dir_fp
)
3564 struct dentry
*dir
= dir_fp
->filp
->f_path
.dentry
;
3566 inode_unlock(d_inode(dir
));
3569 static int process_query_dir_entries(struct smb2_query_dir_private
*priv
)
3571 struct user_namespace
*user_ns
= file_mnt_user_ns(priv
->dir_fp
->filp
);
3573 struct ksmbd_kstat ksmbd_kstat
;
3577 for (i
= 0; i
< priv
->d_info
->num_entry
; i
++) {
3578 struct dentry
*dent
;
3580 if (dentry_name(priv
->d_info
, priv
->info_level
))
3583 lock_dir(priv
->dir_fp
);
3584 dent
= lookup_one(user_ns
, priv
->d_info
->name
,
3585 priv
->dir_fp
->filp
->f_path
.dentry
,
3586 priv
->d_info
->name_len
);
3587 unlock_dir(priv
->dir_fp
);
3590 ksmbd_debug(SMB
, "Cannot lookup `%s' [%ld]\n",
3595 if (unlikely(d_is_negative(dent
))) {
3597 ksmbd_debug(SMB
, "Negative dentry `%s'\n",
3598 priv
->d_info
->name
);
3602 ksmbd_kstat
.kstat
= &kstat
;
3603 if (priv
->info_level
!= FILE_NAMES_INFORMATION
)
3604 ksmbd_vfs_fill_dentry_attrs(priv
->work
,
3609 rc
= smb2_populate_readdir_entry(priv
->work
->conn
,
3620 static int reserve_populate_dentry(struct ksmbd_dir_info
*d_info
,
3625 int next_entry_offset
;
3627 struct_sz
= readdir_info_level_struct_sz(info_level
);
3628 if (struct_sz
== -EOPNOTSUPP
)
3631 conv_len
= (d_info
->name_len
+ 1) * 2;
3632 next_entry_offset
= ALIGN(struct_sz
- 1 + conv_len
,
3633 KSMBD_DIR_INFO_ALIGNMENT
);
3635 if (next_entry_offset
> d_info
->out_buf_len
) {
3636 d_info
->out_buf_len
= 0;
3640 switch (info_level
) {
3641 case FILE_FULL_DIRECTORY_INFORMATION
:
3643 struct file_full_directory_info
*ffdinfo
;
3645 ffdinfo
= (struct file_full_directory_info
*)d_info
->wptr
;
3646 memcpy(ffdinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3647 ffdinfo
->FileName
[d_info
->name_len
] = 0x00;
3648 ffdinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3649 ffdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3652 case FILE_BOTH_DIRECTORY_INFORMATION
:
3654 struct file_both_directory_info
*fbdinfo
;
3656 fbdinfo
= (struct file_both_directory_info
*)d_info
->wptr
;
3657 memcpy(fbdinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3658 fbdinfo
->FileName
[d_info
->name_len
] = 0x00;
3659 fbdinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3660 fbdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3663 case FILE_DIRECTORY_INFORMATION
:
3665 struct file_directory_info
*fdinfo
;
3667 fdinfo
= (struct file_directory_info
*)d_info
->wptr
;
3668 memcpy(fdinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3669 fdinfo
->FileName
[d_info
->name_len
] = 0x00;
3670 fdinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3671 fdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3674 case FILE_NAMES_INFORMATION
:
3676 struct file_names_info
*fninfo
;
3678 fninfo
= (struct file_names_info
*)d_info
->wptr
;
3679 memcpy(fninfo
->FileName
, d_info
->name
, d_info
->name_len
);
3680 fninfo
->FileName
[d_info
->name_len
] = 0x00;
3681 fninfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3682 fninfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3685 case FILEID_FULL_DIRECTORY_INFORMATION
:
3687 struct file_id_full_dir_info
*dinfo
;
3689 dinfo
= (struct file_id_full_dir_info
*)d_info
->wptr
;
3690 memcpy(dinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3691 dinfo
->FileName
[d_info
->name_len
] = 0x00;
3692 dinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3693 dinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3696 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3698 struct file_id_both_directory_info
*fibdinfo
;
3700 fibdinfo
= (struct file_id_both_directory_info
*)d_info
->wptr
;
3701 memcpy(fibdinfo
->FileName
, d_info
->name
, d_info
->name_len
);
3702 fibdinfo
->FileName
[d_info
->name_len
] = 0x00;
3703 fibdinfo
->FileNameLength
= cpu_to_le32(d_info
->name_len
);
3704 fibdinfo
->NextEntryOffset
= cpu_to_le32(next_entry_offset
);
3707 case SMB_FIND_FILE_POSIX_INFO
:
3709 struct smb2_posix_info
*posix_info
;
3711 posix_info
= (struct smb2_posix_info
*)d_info
->wptr
;
3712 memcpy(posix_info
->name
, d_info
->name
, d_info
->name_len
);
3713 posix_info
->name
[d_info
->name_len
] = 0x00;
3714 posix_info
->name_len
= cpu_to_le32(d_info
->name_len
);
3715 posix_info
->NextEntryOffset
=
3716 cpu_to_le32(next_entry_offset
);
3719 } /* switch (info_level) */
3721 d_info
->num_entry
++;
3722 d_info
->out_buf_len
-= next_entry_offset
;
3723 d_info
->wptr
+= next_entry_offset
;
3727 static int __query_dir(struct dir_context
*ctx
, const char *name
, int namlen
,
3728 loff_t offset
, u64 ino
, unsigned int d_type
)
3730 struct ksmbd_readdir_data
*buf
;
3731 struct smb2_query_dir_private
*priv
;
3732 struct ksmbd_dir_info
*d_info
;
3735 buf
= container_of(ctx
, struct ksmbd_readdir_data
, ctx
);
3736 priv
= buf
->private;
3737 d_info
= priv
->d_info
;
3739 /* dot and dotdot entries are already reserved */
3740 if (!strcmp(".", name
) || !strcmp("..", name
))
3742 if (ksmbd_share_veto_filename(priv
->work
->tcon
->share_conf
, name
))
3744 if (!match_pattern(name
, namlen
, priv
->search_pattern
))
3747 d_info
->name
= name
;
3748 d_info
->name_len
= namlen
;
3749 rc
= reserve_populate_dentry(d_info
, priv
->info_level
);
3752 if (d_info
->flags
& SMB2_RETURN_SINGLE_ENTRY
) {
3753 d_info
->out_buf_len
= 0;
3759 static void restart_ctx(struct dir_context
*ctx
)
3764 static int verify_info_level(int info_level
)
3766 switch (info_level
) {
3767 case FILE_FULL_DIRECTORY_INFORMATION
:
3768 case FILE_BOTH_DIRECTORY_INFORMATION
:
3769 case FILE_DIRECTORY_INFORMATION
:
3770 case FILE_NAMES_INFORMATION
:
3771 case FILEID_FULL_DIRECTORY_INFORMATION
:
3772 case FILEID_BOTH_DIRECTORY_INFORMATION
:
3773 case SMB_FIND_FILE_POSIX_INFO
:
3782 int smb2_query_dir(struct ksmbd_work
*work
)
3784 struct ksmbd_conn
*conn
= work
->conn
;
3785 struct smb2_query_directory_req
*req
;
3786 struct smb2_query_directory_rsp
*rsp
, *rsp_org
;
3787 struct ksmbd_share_config
*share
= work
->tcon
->share_conf
;
3788 struct ksmbd_file
*dir_fp
= NULL
;
3789 struct ksmbd_dir_info d_info
;
3791 char *srch_ptr
= NULL
;
3792 unsigned char srch_flag
;
3794 struct smb2_query_dir_private query_dir_private
= {NULL
, };
3796 rsp_org
= work
->response_buf
;
3797 WORK_BUFFERS(work
, req
, rsp
);
3799 if (ksmbd_override_fsids(work
)) {
3800 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
3801 smb2_set_err_rsp(work
);
3805 rc
= verify_info_level(req
->FileInformationClass
);
3811 dir_fp
= ksmbd_lookup_fd_slow(work
,
3812 le64_to_cpu(req
->VolatileFileId
),
3813 le64_to_cpu(req
->PersistentFileId
));
3819 if (!(dir_fp
->daccess
& FILE_LIST_DIRECTORY_LE
) ||
3820 inode_permission(file_mnt_user_ns(dir_fp
->filp
),
3821 file_inode(dir_fp
->filp
),
3822 MAY_READ
| MAY_EXEC
)) {
3823 pr_err("no right to enumerate directory (%pd)\n",
3824 dir_fp
->filp
->f_path
.dentry
);
3829 if (!S_ISDIR(file_inode(dir_fp
->filp
)->i_mode
)) {
3830 pr_err("can't do query dir for a file\n");
3835 srch_flag
= req
->Flags
;
3836 srch_ptr
= smb_strndup_from_utf16(req
->Buffer
,
3837 le16_to_cpu(req
->FileNameLength
), 1,
3839 if (IS_ERR(srch_ptr
)) {
3840 ksmbd_debug(SMB
, "Search Pattern not found\n");
3844 ksmbd_debug(SMB
, "Search pattern is %s\n", srch_ptr
);
3847 ksmbd_debug(SMB
, "Directory name is %s\n", dir_fp
->filename
);
3849 if (srch_flag
& SMB2_REOPEN
|| srch_flag
& SMB2_RESTART_SCANS
) {
3850 ksmbd_debug(SMB
, "Restart directory scan\n");
3851 generic_file_llseek(dir_fp
->filp
, 0, SEEK_SET
);
3852 restart_ctx(&dir_fp
->readdir_data
.ctx
);
3855 memset(&d_info
, 0, sizeof(struct ksmbd_dir_info
));
3856 d_info
.wptr
= (char *)rsp
->Buffer
;
3857 d_info
.rptr
= (char *)rsp
->Buffer
;
3858 d_info
.out_buf_len
= (work
->response_sz
- (get_rfc1002_len(rsp_org
) + 4));
3859 d_info
.out_buf_len
= min_t(int, d_info
.out_buf_len
, le32_to_cpu(req
->OutputBufferLength
)) -
3860 sizeof(struct smb2_query_directory_rsp
);
3861 d_info
.flags
= srch_flag
;
3864 * reserve dot and dotdot entries in head of buffer
3867 rc
= ksmbd_populate_dot_dotdot_entries(work
, req
->FileInformationClass
,
3868 dir_fp
, &d_info
, srch_ptr
,
3869 smb2_populate_readdir_entry
);
3875 if (test_share_config_flag(share
, KSMBD_SHARE_FLAG_HIDE_DOT_FILES
))
3876 d_info
.hide_dot_file
= true;
3878 buffer_sz
= d_info
.out_buf_len
;
3879 d_info
.rptr
= d_info
.wptr
;
3880 query_dir_private
.work
= work
;
3881 query_dir_private
.search_pattern
= srch_ptr
;
3882 query_dir_private
.dir_fp
= dir_fp
;
3883 query_dir_private
.d_info
= &d_info
;
3884 query_dir_private
.info_level
= req
->FileInformationClass
;
3885 dir_fp
->readdir_data
.private = &query_dir_private
;
3886 set_ctx_actor(&dir_fp
->readdir_data
.ctx
, __query_dir
);
3888 rc
= iterate_dir(dir_fp
->filp
, &dir_fp
->readdir_data
.ctx
);
3890 restart_ctx(&dir_fp
->readdir_data
.ctx
);
3896 d_info
.wptr
= d_info
.rptr
;
3897 d_info
.out_buf_len
= buffer_sz
;
3898 rc
= process_query_dir_entries(&query_dir_private
);
3902 if (!d_info
.data_count
&& d_info
.out_buf_len
>= 0) {
3903 if (srch_flag
& SMB2_RETURN_SINGLE_ENTRY
&& !is_asterisk(srch_ptr
)) {
3904 rsp
->hdr
.Status
= STATUS_NO_SUCH_FILE
;
3906 dir_fp
->dot_dotdot
[0] = dir_fp
->dot_dotdot
[1] = 0;
3907 rsp
->hdr
.Status
= STATUS_NO_MORE_FILES
;
3909 rsp
->StructureSize
= cpu_to_le16(9);
3910 rsp
->OutputBufferOffset
= cpu_to_le16(0);
3911 rsp
->OutputBufferLength
= cpu_to_le32(0);
3913 inc_rfc1001_len(rsp_org
, 9);
3915 ((struct file_directory_info
*)
3916 ((char *)rsp
->Buffer
+ d_info
.last_entry_offset
))
3917 ->NextEntryOffset
= 0;
3919 rsp
->StructureSize
= cpu_to_le16(9);
3920 rsp
->OutputBufferOffset
= cpu_to_le16(72);
3921 rsp
->OutputBufferLength
= cpu_to_le32(d_info
.data_count
);
3922 inc_rfc1001_len(rsp_org
, 8 + d_info
.data_count
);
3926 ksmbd_fd_put(work
, dir_fp
);
3927 ksmbd_revert_fsids(work
);
3931 pr_err("error while processing smb2 query dir rc = %d\n", rc
);
3936 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
3937 else if (rc
== -EACCES
)
3938 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
3939 else if (rc
== -ENOENT
)
3940 rsp
->hdr
.Status
= STATUS_NO_SUCH_FILE
;
3941 else if (rc
== -EBADF
)
3942 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
3943 else if (rc
== -ENOMEM
)
3944 rsp
->hdr
.Status
= STATUS_NO_MEMORY
;
3945 else if (rc
== -EFAULT
)
3946 rsp
->hdr
.Status
= STATUS_INVALID_INFO_CLASS
;
3947 if (!rsp
->hdr
.Status
)
3948 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
3950 smb2_set_err_rsp(work
);
3951 ksmbd_fd_put(work
, dir_fp
);
3952 ksmbd_revert_fsids(work
);
3957 * buffer_check_err() - helper function to check buffer errors
3958 * @reqOutputBufferLength: max buffer length expected in command response
3959 * @rsp: query info response buffer contains output buffer length
3960 * @infoclass_size: query info class response buffer size
3962 * Return: 0 on success, otherwise error
3964 static int buffer_check_err(int reqOutputBufferLength
,
3965 struct smb2_query_info_rsp
*rsp
, int infoclass_size
)
3967 if (reqOutputBufferLength
< le32_to_cpu(rsp
->OutputBufferLength
)) {
3968 if (reqOutputBufferLength
< infoclass_size
) {
3969 pr_err("Invalid Buffer Size Requested\n");
3970 rsp
->hdr
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
3971 rsp
->hdr
.smb2_buf_length
= cpu_to_be32(sizeof(struct smb2_hdr
) - 4);
3975 ksmbd_debug(SMB
, "Buffer Overflow\n");
3976 rsp
->hdr
.Status
= STATUS_BUFFER_OVERFLOW
;
3977 rsp
->hdr
.smb2_buf_length
= cpu_to_be32(sizeof(struct smb2_hdr
) - 4 +
3978 reqOutputBufferLength
);
3979 rsp
->OutputBufferLength
= cpu_to_le32(reqOutputBufferLength
);
3984 static void get_standard_info_pipe(struct smb2_query_info_rsp
*rsp
)
3986 struct smb2_file_standard_info
*sinfo
;
3988 sinfo
= (struct smb2_file_standard_info
*)rsp
->Buffer
;
3990 sinfo
->AllocationSize
= cpu_to_le64(4096);
3991 sinfo
->EndOfFile
= cpu_to_le64(0);
3992 sinfo
->NumberOfLinks
= cpu_to_le32(1);
3993 sinfo
->DeletePending
= 1;
3994 sinfo
->Directory
= 0;
3995 rsp
->OutputBufferLength
=
3996 cpu_to_le32(sizeof(struct smb2_file_standard_info
));
3997 inc_rfc1001_len(rsp
, sizeof(struct smb2_file_standard_info
));
4000 static void get_internal_info_pipe(struct smb2_query_info_rsp
*rsp
, u64 num
)
4002 struct smb2_file_internal_info
*file_info
;
4004 file_info
= (struct smb2_file_internal_info
*)rsp
->Buffer
;
4006 /* any unique number */
4007 file_info
->IndexNumber
= cpu_to_le64(num
| (1ULL << 63));
4008 rsp
->OutputBufferLength
=
4009 cpu_to_le32(sizeof(struct smb2_file_internal_info
));
4010 inc_rfc1001_len(rsp
, sizeof(struct smb2_file_internal_info
));
4013 static int smb2_get_info_file_pipe(struct ksmbd_session
*sess
,
4014 struct smb2_query_info_req
*req
,
4015 struct smb2_query_info_rsp
*rsp
)
4021 * Windows can sometime send query file info request on
4022 * pipe without opening it, checking error condition here
4024 id
= le64_to_cpu(req
->VolatileFileId
);
4025 if (!ksmbd_session_rpc_method(sess
, id
))
4028 ksmbd_debug(SMB
, "FileInfoClass %u, FileId 0x%llx\n",
4029 req
->FileInfoClass
, le64_to_cpu(req
->VolatileFileId
));
4031 switch (req
->FileInfoClass
) {
4032 case FILE_STANDARD_INFORMATION
:
4033 get_standard_info_pipe(rsp
);
4034 rc
= buffer_check_err(le32_to_cpu(req
->OutputBufferLength
),
4035 rsp
, FILE_STANDARD_INFORMATION_SIZE
);
4037 case FILE_INTERNAL_INFORMATION
:
4038 get_internal_info_pipe(rsp
, id
);
4039 rc
= buffer_check_err(le32_to_cpu(req
->OutputBufferLength
),
4040 rsp
, FILE_INTERNAL_INFORMATION_SIZE
);
4043 ksmbd_debug(SMB
, "smb2_info_file_pipe for %u not supported\n",
4044 req
->FileInfoClass
);
4051 * smb2_get_ea() - handler for smb2 get extended attribute command
4052 * @work: smb work containing query info command buffer
4053 * @fp: ksmbd_file pointer
4054 * @req: get extended attribute request
4055 * @rsp: response buffer pointer
4056 * @rsp_org: base response buffer pointer in case of chained response
4058 * Return: 0 on success, otherwise error
4060 static int smb2_get_ea(struct ksmbd_work
*work
, struct ksmbd_file
*fp
,
4061 struct smb2_query_info_req
*req
,
4062 struct smb2_query_info_rsp
*rsp
, void *rsp_org
)
4064 struct smb2_ea_info
*eainfo
, *prev_eainfo
;
4065 char *name
, *ptr
, *xattr_list
= NULL
, *buf
;
4066 int rc
, name_len
, value_len
, xattr_list_len
, idx
;
4067 ssize_t buf_free_len
, alignment_bytes
, next_offset
, rsp_data_cnt
= 0;
4068 struct smb2_ea_info_req
*ea_req
= NULL
;
4070 struct user_namespace
*user_ns
= file_mnt_user_ns(fp
->filp
);
4072 if (!(fp
->daccess
& FILE_READ_EA_LE
)) {
4073 pr_err("Not permitted to read ext attr : 0x%x\n",
4078 path
= &fp
->filp
->f_path
;
4079 /* single EA entry is requested with given user.* name */
4080 if (req
->InputBufferLength
) {
4081 if (le32_to_cpu(req
->InputBufferLength
) <
4082 sizeof(struct smb2_ea_info_req
))
4085 ea_req
= (struct smb2_ea_info_req
*)req
->Buffer
;
4087 /* need to send all EAs, if no specific EA is requested*/
4088 if (le32_to_cpu(req
->Flags
) & SL_RETURN_SINGLE_ENTRY
)
4090 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4091 le32_to_cpu(req
->Flags
));
4094 buf_free_len
= work
->response_sz
-
4095 (get_rfc1002_len(rsp_org
) + 4) -
4096 sizeof(struct smb2_query_info_rsp
);
4098 if (le32_to_cpu(req
->OutputBufferLength
) < buf_free_len
)
4099 buf_free_len
= le32_to_cpu(req
->OutputBufferLength
);
4101 rc
= ksmbd_vfs_listxattr(path
->dentry
, &xattr_list
);
4103 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
4105 } else if (!rc
) { /* there is no EA in the file */
4106 ksmbd_debug(SMB
, "no ea data in the file\n");
4109 xattr_list_len
= rc
;
4111 ptr
= (char *)rsp
->Buffer
;
4112 eainfo
= (struct smb2_ea_info
*)ptr
;
4113 prev_eainfo
= eainfo
;
4116 while (idx
< xattr_list_len
) {
4117 name
= xattr_list
+ idx
;
4118 name_len
= strlen(name
);
4120 ksmbd_debug(SMB
, "%s, len %d\n", name
, name_len
);
4121 idx
+= name_len
+ 1;
4124 * CIFS does not support EA other than user.* namespace,
4125 * still keep the framework generic, to list other attrs
4128 if (strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
4131 if (!strncmp(&name
[XATTR_USER_PREFIX_LEN
], STREAM_PREFIX
,
4135 if (req
->InputBufferLength
&&
4136 strncmp(&name
[XATTR_USER_PREFIX_LEN
], ea_req
->name
,
4137 ea_req
->EaNameLength
))
4140 if (!strncmp(&name
[XATTR_USER_PREFIX_LEN
],
4141 DOS_ATTRIBUTE_PREFIX
, DOS_ATTRIBUTE_PREFIX_LEN
))
4144 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
4145 name_len
-= XATTR_USER_PREFIX_LEN
;
4147 ptr
= (char *)(&eainfo
->name
+ name_len
+ 1);
4148 buf_free_len
-= (offsetof(struct smb2_ea_info
, name
) +
4150 /* bailout if xattr can't fit in buf_free_len */
4151 value_len
= ksmbd_vfs_getxattr(user_ns
, path
->dentry
,
4153 if (value_len
<= 0) {
4155 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
4159 buf_free_len
-= value_len
;
4160 if (buf_free_len
< 0) {
4165 memcpy(ptr
, buf
, value_len
);
4170 eainfo
->EaNameLength
= name_len
;
4172 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
))
4173 memcpy(eainfo
->name
, &name
[XATTR_USER_PREFIX_LEN
],
4176 memcpy(eainfo
->name
, name
, name_len
);
4178 eainfo
->name
[name_len
] = '\0';
4179 eainfo
->EaValueLength
= cpu_to_le16(value_len
);
4180 next_offset
= offsetof(struct smb2_ea_info
, name
) +
4181 name_len
+ 1 + value_len
;
4183 /* align next xattr entry at 4 byte bundary */
4184 alignment_bytes
= ((next_offset
+ 3) & ~3) - next_offset
;
4185 if (alignment_bytes
) {
4186 memset(ptr
, '\0', alignment_bytes
);
4187 ptr
+= alignment_bytes
;
4188 next_offset
+= alignment_bytes
;
4189 buf_free_len
-= alignment_bytes
;
4191 eainfo
->NextEntryOffset
= cpu_to_le32(next_offset
);
4192 prev_eainfo
= eainfo
;
4193 eainfo
= (struct smb2_ea_info
*)ptr
;
4194 rsp_data_cnt
+= next_offset
;
4196 if (req
->InputBufferLength
) {
4197 ksmbd_debug(SMB
, "single entry requested\n");
4202 /* no more ea entries */
4203 prev_eainfo
->NextEntryOffset
= 0;
4206 if (rsp_data_cnt
== 0)
4207 rsp
->hdr
.Status
= STATUS_NO_EAS_ON_FILE
;
4208 rsp
->OutputBufferLength
= cpu_to_le32(rsp_data_cnt
);
4209 inc_rfc1001_len(rsp_org
, rsp_data_cnt
);
4215 static void get_file_access_info(struct smb2_query_info_rsp
*rsp
,
4216 struct ksmbd_file
*fp
, void *rsp_org
)
4218 struct smb2_file_access_info
*file_info
;
4220 file_info
= (struct smb2_file_access_info
*)rsp
->Buffer
;
4221 file_info
->AccessFlags
= fp
->daccess
;
4222 rsp
->OutputBufferLength
=
4223 cpu_to_le32(sizeof(struct smb2_file_access_info
));
4224 inc_rfc1001_len(rsp_org
, sizeof(struct smb2_file_access_info
));
4227 static int get_file_basic_info(struct smb2_query_info_rsp
*rsp
,
4228 struct ksmbd_file
*fp
, void *rsp_org
)
4230 struct smb2_file_basic_info
*basic_info
;
4234 if (!(fp
->daccess
& FILE_READ_ATTRIBUTES_LE
)) {
4235 pr_err("no right to read the attributes : 0x%x\n",
4240 basic_info
= (struct smb2_file_basic_info
*)rsp
->Buffer
;
4241 generic_fillattr(file_mnt_user_ns(fp
->filp
), file_inode(fp
->filp
),
4243 basic_info
->CreationTime
= cpu_to_le64(fp
->create_time
);
4244 time
= ksmbd_UnixTimeToNT(stat
.atime
);
4245 basic_info
->LastAccessTime
= cpu_to_le64(time
);
4246 time
= ksmbd_UnixTimeToNT(stat
.mtime
);
4247 basic_info
->LastWriteTime
= cpu_to_le64(time
);
4248 time
= ksmbd_UnixTimeToNT(stat
.ctime
);
4249 basic_info
->ChangeTime
= cpu_to_le64(time
);
4250 basic_info
->Attributes
= fp
->f_ci
->m_fattr
;
4251 basic_info
->Pad1
= 0;
4252 rsp
->OutputBufferLength
=
4253 cpu_to_le32(sizeof(struct smb2_file_basic_info
));
4254 inc_rfc1001_len(rsp_org
, sizeof(struct smb2_file_basic_info
));
4258 static unsigned long long get_allocation_size(struct inode
*inode
,
4261 unsigned long long alloc_size
= 0;
4263 if (!S_ISDIR(stat
->mode
)) {
4264 if ((inode
->i_blocks
<< 9) <= stat
->size
)
4265 alloc_size
= stat
->size
;
4267 alloc_size
= inode
->i_blocks
<< 9;
4273 static void get_file_standard_info(struct smb2_query_info_rsp
*rsp
,
4274 struct ksmbd_file
*fp
, void *rsp_org
)
4276 struct smb2_file_standard_info
*sinfo
;
4277 unsigned int delete_pending
;
4278 struct inode
*inode
;
4281 inode
= file_inode(fp
->filp
);
4282 generic_fillattr(file_mnt_user_ns(fp
->filp
), inode
, &stat
);
4284 sinfo
= (struct smb2_file_standard_info
*)rsp
->Buffer
;
4285 delete_pending
= ksmbd_inode_pending_delete(fp
);
4287 sinfo
->AllocationSize
= cpu_to_le64(get_allocation_size(inode
, &stat
));
4288 sinfo
->EndOfFile
= S_ISDIR(stat
.mode
) ? 0 : cpu_to_le64(stat
.size
);
4289 sinfo
->NumberOfLinks
= cpu_to_le32(get_nlink(&stat
) - delete_pending
);
4290 sinfo
->DeletePending
= delete_pending
;
4291 sinfo
->Directory
= S_ISDIR(stat
.mode
) ? 1 : 0;
4292 rsp
->OutputBufferLength
=
4293 cpu_to_le32(sizeof(struct smb2_file_standard_info
));
4294 inc_rfc1001_len(rsp_org
,
4295 sizeof(struct smb2_file_standard_info
));
4298 static void get_file_alignment_info(struct smb2_query_info_rsp
*rsp
,
4301 struct smb2_file_alignment_info
*file_info
;
4303 file_info
= (struct smb2_file_alignment_info
*)rsp
->Buffer
;
4304 file_info
->AlignmentRequirement
= 0;
4305 rsp
->OutputBufferLength
=
4306 cpu_to_le32(sizeof(struct smb2_file_alignment_info
));
4307 inc_rfc1001_len(rsp_org
,
4308 sizeof(struct smb2_file_alignment_info
));
4311 static int get_file_all_info(struct ksmbd_work
*work
,
4312 struct smb2_query_info_rsp
*rsp
,
4313 struct ksmbd_file
*fp
,
4316 struct ksmbd_conn
*conn
= work
->conn
;
4317 struct smb2_file_all_info
*file_info
;
4318 unsigned int delete_pending
;
4319 struct inode
*inode
;
4325 if (!(fp
->daccess
& FILE_READ_ATTRIBUTES_LE
)) {
4326 ksmbd_debug(SMB
, "no right to read the attributes : 0x%x\n",
4331 filename
= convert_to_nt_pathname(fp
->filename
);
4335 inode
= file_inode(fp
->filp
);
4336 generic_fillattr(file_mnt_user_ns(fp
->filp
), inode
, &stat
);
4338 ksmbd_debug(SMB
, "filename = %s\n", filename
);
4339 delete_pending
= ksmbd_inode_pending_delete(fp
);
4340 file_info
= (struct smb2_file_all_info
*)rsp
->Buffer
;
4342 file_info
->CreationTime
= cpu_to_le64(fp
->create_time
);
4343 time
= ksmbd_UnixTimeToNT(stat
.atime
);
4344 file_info
->LastAccessTime
= cpu_to_le64(time
);
4345 time
= ksmbd_UnixTimeToNT(stat
.mtime
);
4346 file_info
->LastWriteTime
= cpu_to_le64(time
);
4347 time
= ksmbd_UnixTimeToNT(stat
.ctime
);
4348 file_info
->ChangeTime
= cpu_to_le64(time
);
4349 file_info
->Attributes
= fp
->f_ci
->m_fattr
;
4350 file_info
->Pad1
= 0;
4351 file_info
->AllocationSize
=
4352 cpu_to_le64(get_allocation_size(inode
, &stat
));
4353 file_info
->EndOfFile
= S_ISDIR(stat
.mode
) ? 0 : cpu_to_le64(stat
.size
);
4354 file_info
->NumberOfLinks
=
4355 cpu_to_le32(get_nlink(&stat
) - delete_pending
);
4356 file_info
->DeletePending
= delete_pending
;
4357 file_info
->Directory
= S_ISDIR(stat
.mode
) ? 1 : 0;
4358 file_info
->Pad2
= 0;
4359 file_info
->IndexNumber
= cpu_to_le64(stat
.ino
);
4360 file_info
->EASize
= 0;
4361 file_info
->AccessFlags
= fp
->daccess
;
4362 file_info
->CurrentByteOffset
= cpu_to_le64(fp
->filp
->f_pos
);
4363 file_info
->Mode
= fp
->coption
;
4364 file_info
->AlignmentRequirement
= 0;
4365 conv_len
= smbConvertToUTF16((__le16
*)file_info
->FileName
, filename
,
4366 PATH_MAX
, conn
->local_nls
, 0);
4368 file_info
->FileNameLength
= cpu_to_le32(conv_len
);
4369 rsp
->OutputBufferLength
=
4370 cpu_to_le32(sizeof(struct smb2_file_all_info
) + conv_len
- 1);
4372 inc_rfc1001_len(rsp_org
, le32_to_cpu(rsp
->OutputBufferLength
));
4376 static void get_file_alternate_info(struct ksmbd_work
*work
,
4377 struct smb2_query_info_rsp
*rsp
,
4378 struct ksmbd_file
*fp
,
4381 struct ksmbd_conn
*conn
= work
->conn
;
4382 struct smb2_file_alt_name_info
*file_info
;
4383 struct dentry
*dentry
= fp
->filp
->f_path
.dentry
;
4386 spin_lock(&dentry
->d_lock
);
4387 file_info
= (struct smb2_file_alt_name_info
*)rsp
->Buffer
;
4388 conv_len
= ksmbd_extract_shortname(conn
,
4389 dentry
->d_name
.name
,
4390 file_info
->FileName
);
4391 spin_unlock(&dentry
->d_lock
);
4392 file_info
->FileNameLength
= cpu_to_le32(conv_len
);
4393 rsp
->OutputBufferLength
=
4394 cpu_to_le32(sizeof(struct smb2_file_alt_name_info
) + conv_len
);
4395 inc_rfc1001_len(rsp_org
, le32_to_cpu(rsp
->OutputBufferLength
));
4398 static void get_file_stream_info(struct ksmbd_work
*work
,
4399 struct smb2_query_info_rsp
*rsp
,
4400 struct ksmbd_file
*fp
,
4403 struct ksmbd_conn
*conn
= work
->conn
;
4404 struct smb2_file_stream_info
*file_info
;
4405 char *stream_name
, *xattr_list
= NULL
, *stream_buf
;
4407 struct path
*path
= &fp
->filp
->f_path
;
4408 ssize_t xattr_list_len
;
4409 int nbytes
= 0, streamlen
, stream_name_len
, next
, idx
= 0;
4411 generic_fillattr(file_mnt_user_ns(fp
->filp
), file_inode(fp
->filp
),
4413 file_info
= (struct smb2_file_stream_info
*)rsp
->Buffer
;
4415 xattr_list_len
= ksmbd_vfs_listxattr(path
->dentry
, &xattr_list
);
4416 if (xattr_list_len
< 0) {
4418 } else if (!xattr_list_len
) {
4419 ksmbd_debug(SMB
, "empty xattr in the file\n");
4423 while (idx
< xattr_list_len
) {
4424 stream_name
= xattr_list
+ idx
;
4425 streamlen
= strlen(stream_name
);
4426 idx
+= streamlen
+ 1;
4428 ksmbd_debug(SMB
, "%s, len %d\n", stream_name
, streamlen
);
4430 if (strncmp(&stream_name
[XATTR_USER_PREFIX_LEN
],
4431 STREAM_PREFIX
, STREAM_PREFIX_LEN
))
4434 stream_name_len
= streamlen
- (XATTR_USER_PREFIX_LEN
+
4436 streamlen
= stream_name_len
;
4440 stream_buf
= kmalloc(streamlen
+ 1, GFP_KERNEL
);
4444 streamlen
= snprintf(stream_buf
, streamlen
+ 1,
4445 ":%s", &stream_name
[XATTR_NAME_STREAM_LEN
]);
4447 file_info
= (struct smb2_file_stream_info
*)&rsp
->Buffer
[nbytes
];
4448 streamlen
= smbConvertToUTF16((__le16
*)file_info
->StreamName
,
4449 stream_buf
, streamlen
,
4450 conn
->local_nls
, 0);
4453 file_info
->StreamNameLength
= cpu_to_le32(streamlen
);
4454 file_info
->StreamSize
= cpu_to_le64(stream_name_len
);
4455 file_info
->StreamAllocationSize
= cpu_to_le64(stream_name_len
);
4457 next
= sizeof(struct smb2_file_stream_info
) + streamlen
;
4459 file_info
->NextEntryOffset
= cpu_to_le32(next
);
4462 if (!S_ISDIR(stat
.mode
)) {
4463 file_info
= (struct smb2_file_stream_info
*)
4464 &rsp
->Buffer
[nbytes
];
4465 streamlen
= smbConvertToUTF16((__le16
*)file_info
->StreamName
,
4466 "::$DATA", 7, conn
->local_nls
, 0);
4468 file_info
->StreamNameLength
= cpu_to_le32(streamlen
);
4469 file_info
->StreamSize
= 0;
4470 file_info
->StreamAllocationSize
= 0;
4471 nbytes
+= sizeof(struct smb2_file_stream_info
) + streamlen
;
4474 /* last entry offset should be 0 */
4475 file_info
->NextEntryOffset
= 0;
4479 rsp
->OutputBufferLength
= cpu_to_le32(nbytes
);
4480 inc_rfc1001_len(rsp_org
, nbytes
);
4483 static void get_file_internal_info(struct smb2_query_info_rsp
*rsp
,
4484 struct ksmbd_file
*fp
, void *rsp_org
)
4486 struct smb2_file_internal_info
*file_info
;
4489 generic_fillattr(file_mnt_user_ns(fp
->filp
), file_inode(fp
->filp
),
4491 file_info
= (struct smb2_file_internal_info
*)rsp
->Buffer
;
4492 file_info
->IndexNumber
= cpu_to_le64(stat
.ino
);
4493 rsp
->OutputBufferLength
=
4494 cpu_to_le32(sizeof(struct smb2_file_internal_info
));
4495 inc_rfc1001_len(rsp_org
, sizeof(struct smb2_file_internal_info
));
4498 static int get_file_network_open_info(struct smb2_query_info_rsp
*rsp
,
4499 struct ksmbd_file
*fp
, void *rsp_org
)
4501 struct smb2_file_ntwrk_info
*file_info
;
4502 struct inode
*inode
;
4506 if (!(fp
->daccess
& FILE_READ_ATTRIBUTES_LE
)) {
4507 pr_err("no right to read the attributes : 0x%x\n",
4512 file_info
= (struct smb2_file_ntwrk_info
*)rsp
->Buffer
;
4514 inode
= file_inode(fp
->filp
);
4515 generic_fillattr(file_mnt_user_ns(fp
->filp
), inode
, &stat
);
4517 file_info
->CreationTime
= cpu_to_le64(fp
->create_time
);
4518 time
= ksmbd_UnixTimeToNT(stat
.atime
);
4519 file_info
->LastAccessTime
= cpu_to_le64(time
);
4520 time
= ksmbd_UnixTimeToNT(stat
.mtime
);
4521 file_info
->LastWriteTime
= cpu_to_le64(time
);
4522 time
= ksmbd_UnixTimeToNT(stat
.ctime
);
4523 file_info
->ChangeTime
= cpu_to_le64(time
);
4524 file_info
->Attributes
= fp
->f_ci
->m_fattr
;
4525 file_info
->AllocationSize
=
4526 cpu_to_le64(get_allocation_size(inode
, &stat
));
4527 file_info
->EndOfFile
= S_ISDIR(stat
.mode
) ? 0 : cpu_to_le64(stat
.size
);
4528 file_info
->Reserved
= cpu_to_le32(0);
4529 rsp
->OutputBufferLength
=
4530 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info
));
4531 inc_rfc1001_len(rsp_org
, sizeof(struct smb2_file_ntwrk_info
));
4535 static void get_file_ea_info(struct smb2_query_info_rsp
*rsp
, void *rsp_org
)
4537 struct smb2_file_ea_info
*file_info
;
4539 file_info
= (struct smb2_file_ea_info
*)rsp
->Buffer
;
4540 file_info
->EASize
= 0;
4541 rsp
->OutputBufferLength
=
4542 cpu_to_le32(sizeof(struct smb2_file_ea_info
));
4543 inc_rfc1001_len(rsp_org
, sizeof(struct smb2_file_ea_info
));
4546 static void get_file_position_info(struct smb2_query_info_rsp
*rsp
,
4547 struct ksmbd_file
*fp
, void *rsp_org
)
4549 struct smb2_file_pos_info
*file_info
;
4551 file_info
= (struct smb2_file_pos_info
*)rsp
->Buffer
;
4552 file_info
->CurrentByteOffset
= cpu_to_le64(fp
->filp
->f_pos
);
4553 rsp
->OutputBufferLength
=
4554 cpu_to_le32(sizeof(struct smb2_file_pos_info
));
4555 inc_rfc1001_len(rsp_org
, sizeof(struct smb2_file_pos_info
));
4558 static void get_file_mode_info(struct smb2_query_info_rsp
*rsp
,
4559 struct ksmbd_file
*fp
, void *rsp_org
)
4561 struct smb2_file_mode_info
*file_info
;
4563 file_info
= (struct smb2_file_mode_info
*)rsp
->Buffer
;
4564 file_info
->Mode
= fp
->coption
& FILE_MODE_INFO_MASK
;
4565 rsp
->OutputBufferLength
=
4566 cpu_to_le32(sizeof(struct smb2_file_mode_info
));
4567 inc_rfc1001_len(rsp_org
, sizeof(struct smb2_file_mode_info
));
4570 static void get_file_compression_info(struct smb2_query_info_rsp
*rsp
,
4571 struct ksmbd_file
*fp
, void *rsp_org
)
4573 struct smb2_file_comp_info
*file_info
;
4576 generic_fillattr(file_mnt_user_ns(fp
->filp
), file_inode(fp
->filp
),
4579 file_info
= (struct smb2_file_comp_info
*)rsp
->Buffer
;
4580 file_info
->CompressedFileSize
= cpu_to_le64(stat
.blocks
<< 9);
4581 file_info
->CompressionFormat
= COMPRESSION_FORMAT_NONE
;
4582 file_info
->CompressionUnitShift
= 0;
4583 file_info
->ChunkShift
= 0;
4584 file_info
->ClusterShift
= 0;
4585 memset(&file_info
->Reserved
[0], 0, 3);
4587 rsp
->OutputBufferLength
=
4588 cpu_to_le32(sizeof(struct smb2_file_comp_info
));
4589 inc_rfc1001_len(rsp_org
, sizeof(struct smb2_file_comp_info
));
4592 static int get_file_attribute_tag_info(struct smb2_query_info_rsp
*rsp
,
4593 struct ksmbd_file
*fp
, void *rsp_org
)
4595 struct smb2_file_attr_tag_info
*file_info
;
4597 if (!(fp
->daccess
& FILE_READ_ATTRIBUTES_LE
)) {
4598 pr_err("no right to read the attributes : 0x%x\n",
4603 file_info
= (struct smb2_file_attr_tag_info
*)rsp
->Buffer
;
4604 file_info
->FileAttributes
= fp
->f_ci
->m_fattr
;
4605 file_info
->ReparseTag
= 0;
4606 rsp
->OutputBufferLength
=
4607 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info
));
4608 inc_rfc1001_len(rsp_org
, sizeof(struct smb2_file_attr_tag_info
));
4612 static int find_file_posix_info(struct smb2_query_info_rsp
*rsp
,
4613 struct ksmbd_file
*fp
, void *rsp_org
)
4615 struct smb311_posix_qinfo
*file_info
;
4616 struct inode
*inode
= file_inode(fp
->filp
);
4619 file_info
= (struct smb311_posix_qinfo
*)rsp
->Buffer
;
4620 file_info
->CreationTime
= cpu_to_le64(fp
->create_time
);
4621 time
= ksmbd_UnixTimeToNT(inode
->i_atime
);
4622 file_info
->LastAccessTime
= cpu_to_le64(time
);
4623 time
= ksmbd_UnixTimeToNT(inode
->i_mtime
);
4624 file_info
->LastWriteTime
= cpu_to_le64(time
);
4625 time
= ksmbd_UnixTimeToNT(inode
->i_ctime
);
4626 file_info
->ChangeTime
= cpu_to_le64(time
);
4627 file_info
->DosAttributes
= fp
->f_ci
->m_fattr
;
4628 file_info
->Inode
= cpu_to_le64(inode
->i_ino
);
4629 file_info
->EndOfFile
= cpu_to_le64(inode
->i_size
);
4630 file_info
->AllocationSize
= cpu_to_le64(inode
->i_blocks
<< 9);
4631 file_info
->HardLinks
= cpu_to_le32(inode
->i_nlink
);
4632 file_info
->Mode
= cpu_to_le32(inode
->i_mode
);
4633 file_info
->DeviceId
= cpu_to_le32(inode
->i_rdev
);
4634 rsp
->OutputBufferLength
=
4635 cpu_to_le32(sizeof(struct smb311_posix_qinfo
));
4636 inc_rfc1001_len(rsp_org
, sizeof(struct smb311_posix_qinfo
));
4640 static int smb2_get_info_file(struct ksmbd_work
*work
,
4641 struct smb2_query_info_req
*req
,
4642 struct smb2_query_info_rsp
*rsp
, void *rsp_org
)
4644 struct ksmbd_file
*fp
;
4645 int fileinfoclass
= 0;
4647 int file_infoclass_size
;
4648 unsigned int id
= KSMBD_NO_FID
, pid
= KSMBD_NO_FID
;
4650 if (test_share_config_flag(work
->tcon
->share_conf
,
4651 KSMBD_SHARE_FLAG_PIPE
)) {
4652 /* smb2 info file called for pipe */
4653 return smb2_get_info_file_pipe(work
->sess
, req
, rsp
);
4656 if (work
->next_smb2_rcv_hdr_off
) {
4657 if (!has_file_id(le64_to_cpu(req
->VolatileFileId
))) {
4658 ksmbd_debug(SMB
, "Compound request set FID = %llu\n",
4659 work
->compound_fid
);
4660 id
= work
->compound_fid
;
4661 pid
= work
->compound_pfid
;
4665 if (!has_file_id(id
)) {
4666 id
= le64_to_cpu(req
->VolatileFileId
);
4667 pid
= le64_to_cpu(req
->PersistentFileId
);
4670 fp
= ksmbd_lookup_fd_slow(work
, id
, pid
);
4674 fileinfoclass
= req
->FileInfoClass
;
4676 switch (fileinfoclass
) {
4677 case FILE_ACCESS_INFORMATION
:
4678 get_file_access_info(rsp
, fp
, rsp_org
);
4679 file_infoclass_size
= FILE_ACCESS_INFORMATION_SIZE
;
4682 case FILE_BASIC_INFORMATION
:
4683 rc
= get_file_basic_info(rsp
, fp
, rsp_org
);
4684 file_infoclass_size
= FILE_BASIC_INFORMATION_SIZE
;
4687 case FILE_STANDARD_INFORMATION
:
4688 get_file_standard_info(rsp
, fp
, rsp_org
);
4689 file_infoclass_size
= FILE_STANDARD_INFORMATION_SIZE
;
4692 case FILE_ALIGNMENT_INFORMATION
:
4693 get_file_alignment_info(rsp
, rsp_org
);
4694 file_infoclass_size
= FILE_ALIGNMENT_INFORMATION_SIZE
;
4697 case FILE_ALL_INFORMATION
:
4698 rc
= get_file_all_info(work
, rsp
, fp
, rsp_org
);
4699 file_infoclass_size
= FILE_ALL_INFORMATION_SIZE
;
4702 case FILE_ALTERNATE_NAME_INFORMATION
:
4703 get_file_alternate_info(work
, rsp
, fp
, rsp_org
);
4704 file_infoclass_size
= FILE_ALTERNATE_NAME_INFORMATION_SIZE
;
4707 case FILE_STREAM_INFORMATION
:
4708 get_file_stream_info(work
, rsp
, fp
, rsp_org
);
4709 file_infoclass_size
= FILE_STREAM_INFORMATION_SIZE
;
4712 case FILE_INTERNAL_INFORMATION
:
4713 get_file_internal_info(rsp
, fp
, rsp_org
);
4714 file_infoclass_size
= FILE_INTERNAL_INFORMATION_SIZE
;
4717 case FILE_NETWORK_OPEN_INFORMATION
:
4718 rc
= get_file_network_open_info(rsp
, fp
, rsp_org
);
4719 file_infoclass_size
= FILE_NETWORK_OPEN_INFORMATION_SIZE
;
4722 case FILE_EA_INFORMATION
:
4723 get_file_ea_info(rsp
, rsp_org
);
4724 file_infoclass_size
= FILE_EA_INFORMATION_SIZE
;
4727 case FILE_FULL_EA_INFORMATION
:
4728 rc
= smb2_get_ea(work
, fp
, req
, rsp
, rsp_org
);
4729 file_infoclass_size
= FILE_FULL_EA_INFORMATION_SIZE
;
4732 case FILE_POSITION_INFORMATION
:
4733 get_file_position_info(rsp
, fp
, rsp_org
);
4734 file_infoclass_size
= FILE_POSITION_INFORMATION_SIZE
;
4737 case FILE_MODE_INFORMATION
:
4738 get_file_mode_info(rsp
, fp
, rsp_org
);
4739 file_infoclass_size
= FILE_MODE_INFORMATION_SIZE
;
4742 case FILE_COMPRESSION_INFORMATION
:
4743 get_file_compression_info(rsp
, fp
, rsp_org
);
4744 file_infoclass_size
= FILE_COMPRESSION_INFORMATION_SIZE
;
4747 case FILE_ATTRIBUTE_TAG_INFORMATION
:
4748 rc
= get_file_attribute_tag_info(rsp
, fp
, rsp_org
);
4749 file_infoclass_size
= FILE_ATTRIBUTE_TAG_INFORMATION_SIZE
;
4751 case SMB_FIND_FILE_POSIX_INFO
:
4752 if (!work
->tcon
->posix_extensions
) {
4753 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4756 rc
= find_file_posix_info(rsp
, fp
, rsp_org
);
4757 file_infoclass_size
= sizeof(struct smb311_posix_qinfo
);
4761 ksmbd_debug(SMB
, "fileinfoclass %d not supported yet\n",
4766 rc
= buffer_check_err(le32_to_cpu(req
->OutputBufferLength
),
4768 file_infoclass_size
);
4769 ksmbd_fd_put(work
, fp
);
4773 static int smb2_get_info_filesystem(struct ksmbd_work
*work
,
4774 struct smb2_query_info_req
*req
,
4775 struct smb2_query_info_rsp
*rsp
, void *rsp_org
)
4777 struct ksmbd_session
*sess
= work
->sess
;
4778 struct ksmbd_conn
*conn
= sess
->conn
;
4779 struct ksmbd_share_config
*share
= work
->tcon
->share_conf
;
4780 int fsinfoclass
= 0;
4781 struct kstatfs stfs
;
4784 int fs_infoclass_size
= 0;
4786 rc
= kern_path(share
->path
, LOOKUP_NO_SYMLINKS
, &path
);
4788 pr_err("cannot create vfs path\n");
4792 rc
= vfs_statfs(&path
, &stfs
);
4794 pr_err("cannot do stat of path %s\n", share
->path
);
4799 fsinfoclass
= req
->FileInfoClass
;
4801 switch (fsinfoclass
) {
4802 case FS_DEVICE_INFORMATION
:
4804 struct filesystem_device_info
*info
;
4806 info
= (struct filesystem_device_info
*)rsp
->Buffer
;
4808 info
->DeviceType
= cpu_to_le32(stfs
.f_type
);
4809 info
->DeviceCharacteristics
= cpu_to_le32(0x00000020);
4810 rsp
->OutputBufferLength
= cpu_to_le32(8);
4811 inc_rfc1001_len(rsp_org
, 8);
4812 fs_infoclass_size
= FS_DEVICE_INFORMATION_SIZE
;
4815 case FS_ATTRIBUTE_INFORMATION
:
4817 struct filesystem_attribute_info
*info
;
4820 info
= (struct filesystem_attribute_info
*)rsp
->Buffer
;
4821 info
->Attributes
= cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS
|
4822 FILE_PERSISTENT_ACLS
|
4823 FILE_UNICODE_ON_DISK
|
4824 FILE_CASE_PRESERVED_NAMES
|
4825 FILE_CASE_SENSITIVE_SEARCH
|
4826 FILE_SUPPORTS_BLOCK_REFCOUNTING
);
4828 info
->Attributes
|= cpu_to_le32(server_conf
.share_fake_fscaps
);
4830 info
->MaxPathNameComponentLength
= cpu_to_le32(stfs
.f_namelen
);
4831 len
= smbConvertToUTF16((__le16
*)info
->FileSystemName
,
4832 "NTFS", PATH_MAX
, conn
->local_nls
, 0);
4834 info
->FileSystemNameLen
= cpu_to_le32(len
);
4835 sz
= sizeof(struct filesystem_attribute_info
) - 2 + len
;
4836 rsp
->OutputBufferLength
= cpu_to_le32(sz
);
4837 inc_rfc1001_len(rsp_org
, sz
);
4838 fs_infoclass_size
= FS_ATTRIBUTE_INFORMATION_SIZE
;
4841 case FS_VOLUME_INFORMATION
:
4843 struct filesystem_vol_info
*info
;
4846 info
= (struct filesystem_vol_info
*)(rsp
->Buffer
);
4847 info
->VolumeCreationTime
= 0;
4848 /* Taking dummy value of serial number*/
4849 info
->SerialNumber
= cpu_to_le32(0xbc3ac512);
4850 len
= smbConvertToUTF16((__le16
*)info
->VolumeLabel
,
4851 share
->name
, PATH_MAX
,
4852 conn
->local_nls
, 0);
4854 info
->VolumeLabelSize
= cpu_to_le32(len
);
4856 sz
= sizeof(struct filesystem_vol_info
) - 2 + len
;
4857 rsp
->OutputBufferLength
= cpu_to_le32(sz
);
4858 inc_rfc1001_len(rsp_org
, sz
);
4859 fs_infoclass_size
= FS_VOLUME_INFORMATION_SIZE
;
4862 case FS_SIZE_INFORMATION
:
4864 struct filesystem_info
*info
;
4866 info
= (struct filesystem_info
*)(rsp
->Buffer
);
4867 info
->TotalAllocationUnits
= cpu_to_le64(stfs
.f_blocks
);
4868 info
->FreeAllocationUnits
= cpu_to_le64(stfs
.f_bfree
);
4869 info
->SectorsPerAllocationUnit
= cpu_to_le32(1);
4870 info
->BytesPerSector
= cpu_to_le32(stfs
.f_bsize
);
4871 rsp
->OutputBufferLength
= cpu_to_le32(24);
4872 inc_rfc1001_len(rsp_org
, 24);
4873 fs_infoclass_size
= FS_SIZE_INFORMATION_SIZE
;
4876 case FS_FULL_SIZE_INFORMATION
:
4878 struct smb2_fs_full_size_info
*info
;
4880 info
= (struct smb2_fs_full_size_info
*)(rsp
->Buffer
);
4881 info
->TotalAllocationUnits
= cpu_to_le64(stfs
.f_blocks
);
4882 info
->CallerAvailableAllocationUnits
=
4883 cpu_to_le64(stfs
.f_bavail
);
4884 info
->ActualAvailableAllocationUnits
=
4885 cpu_to_le64(stfs
.f_bfree
);
4886 info
->SectorsPerAllocationUnit
= cpu_to_le32(1);
4887 info
->BytesPerSector
= cpu_to_le32(stfs
.f_bsize
);
4888 rsp
->OutputBufferLength
= cpu_to_le32(32);
4889 inc_rfc1001_len(rsp_org
, 32);
4890 fs_infoclass_size
= FS_FULL_SIZE_INFORMATION_SIZE
;
4893 case FS_OBJECT_ID_INFORMATION
:
4895 struct object_id_info
*info
;
4897 info
= (struct object_id_info
*)(rsp
->Buffer
);
4899 if (!user_guest(sess
->user
))
4900 memcpy(info
->objid
, user_passkey(sess
->user
), 16);
4902 memset(info
->objid
, 0, 16);
4904 info
->extended_info
.magic
= cpu_to_le32(EXTENDED_INFO_MAGIC
);
4905 info
->extended_info
.version
= cpu_to_le32(1);
4906 info
->extended_info
.release
= cpu_to_le32(1);
4907 info
->extended_info
.rel_date
= 0;
4908 memcpy(info
->extended_info
.version_string
, "1.1.0", strlen("1.1.0"));
4909 rsp
->OutputBufferLength
= cpu_to_le32(64);
4910 inc_rfc1001_len(rsp_org
, 64);
4911 fs_infoclass_size
= FS_OBJECT_ID_INFORMATION_SIZE
;
4914 case FS_SECTOR_SIZE_INFORMATION
:
4916 struct smb3_fs_ss_info
*info
;
4918 info
= (struct smb3_fs_ss_info
*)(rsp
->Buffer
);
4920 info
->LogicalBytesPerSector
= cpu_to_le32(stfs
.f_bsize
);
4921 info
->PhysicalBytesPerSectorForAtomicity
=
4922 cpu_to_le32(stfs
.f_bsize
);
4923 info
->PhysicalBytesPerSectorForPerf
= cpu_to_le32(stfs
.f_bsize
);
4924 info
->FSEffPhysicalBytesPerSectorForAtomicity
=
4925 cpu_to_le32(stfs
.f_bsize
);
4926 info
->Flags
= cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE
|
4927 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE
);
4928 info
->ByteOffsetForSectorAlignment
= 0;
4929 info
->ByteOffsetForPartitionAlignment
= 0;
4930 rsp
->OutputBufferLength
= cpu_to_le32(28);
4931 inc_rfc1001_len(rsp_org
, 28);
4932 fs_infoclass_size
= FS_SECTOR_SIZE_INFORMATION_SIZE
;
4935 case FS_CONTROL_INFORMATION
:
4938 * TODO : The current implementation is based on
4939 * test result with win7(NTFS) server. It's need to
4940 * modify this to get valid Quota values
4943 struct smb2_fs_control_info
*info
;
4945 info
= (struct smb2_fs_control_info
*)(rsp
->Buffer
);
4946 info
->FreeSpaceStartFiltering
= 0;
4947 info
->FreeSpaceThreshold
= 0;
4948 info
->FreeSpaceStopFiltering
= 0;
4949 info
->DefaultQuotaThreshold
= cpu_to_le64(SMB2_NO_FID
);
4950 info
->DefaultQuotaLimit
= cpu_to_le64(SMB2_NO_FID
);
4952 rsp
->OutputBufferLength
= cpu_to_le32(48);
4953 inc_rfc1001_len(rsp_org
, 48);
4954 fs_infoclass_size
= FS_CONTROL_INFORMATION_SIZE
;
4957 case FS_POSIX_INFORMATION
:
4959 struct filesystem_posix_info
*info
;
4961 if (!work
->tcon
->posix_extensions
) {
4962 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4965 info
= (struct filesystem_posix_info
*)(rsp
->Buffer
);
4966 info
->OptimalTransferSize
= cpu_to_le32(stfs
.f_bsize
);
4967 info
->BlockSize
= cpu_to_le32(stfs
.f_bsize
);
4968 info
->TotalBlocks
= cpu_to_le64(stfs
.f_blocks
);
4969 info
->BlocksAvail
= cpu_to_le64(stfs
.f_bfree
);
4970 info
->UserBlocksAvail
= cpu_to_le64(stfs
.f_bavail
);
4971 info
->TotalFileNodes
= cpu_to_le64(stfs
.f_files
);
4972 info
->FreeFileNodes
= cpu_to_le64(stfs
.f_ffree
);
4973 rsp
->OutputBufferLength
= cpu_to_le32(56);
4974 inc_rfc1001_len(rsp_org
, 56);
4975 fs_infoclass_size
= FS_POSIX_INFORMATION_SIZE
;
4983 rc
= buffer_check_err(le32_to_cpu(req
->OutputBufferLength
),
4990 static int smb2_get_info_sec(struct ksmbd_work
*work
,
4991 struct smb2_query_info_req
*req
,
4992 struct smb2_query_info_rsp
*rsp
, void *rsp_org
)
4994 struct ksmbd_file
*fp
;
4995 struct user_namespace
*user_ns
;
4996 struct smb_ntsd
*pntsd
= (struct smb_ntsd
*)rsp
->Buffer
, *ppntsd
= NULL
;
4997 struct smb_fattr fattr
= {{0}};
4998 struct inode
*inode
;
5000 unsigned int id
= KSMBD_NO_FID
, pid
= KSMBD_NO_FID
;
5001 int addition_info
= le32_to_cpu(req
->AdditionalInformation
);
5004 if (addition_info
& ~(OWNER_SECINFO
| GROUP_SECINFO
| DACL_SECINFO
|
5005 PROTECTED_DACL_SECINFO
|
5006 UNPROTECTED_DACL_SECINFO
)) {
5007 pr_err("Unsupported addition info: 0x%x)\n",
5010 pntsd
->revision
= cpu_to_le16(1);
5011 pntsd
->type
= cpu_to_le16(SELF_RELATIVE
| DACL_PROTECTED
);
5012 pntsd
->osidoffset
= 0;
5013 pntsd
->gsidoffset
= 0;
5014 pntsd
->sacloffset
= 0;
5015 pntsd
->dacloffset
= 0;
5017 secdesclen
= sizeof(struct smb_ntsd
);
5018 rsp
->OutputBufferLength
= cpu_to_le32(secdesclen
);
5019 inc_rfc1001_len(rsp_org
, secdesclen
);
5024 if (work
->next_smb2_rcv_hdr_off
) {
5025 if (!has_file_id(le64_to_cpu(req
->VolatileFileId
))) {
5026 ksmbd_debug(SMB
, "Compound request set FID = %llu\n",
5027 work
->compound_fid
);
5028 id
= work
->compound_fid
;
5029 pid
= work
->compound_pfid
;
5033 if (!has_file_id(id
)) {
5034 id
= le64_to_cpu(req
->VolatileFileId
);
5035 pid
= le64_to_cpu(req
->PersistentFileId
);
5038 fp
= ksmbd_lookup_fd_slow(work
, id
, pid
);
5042 user_ns
= file_mnt_user_ns(fp
->filp
);
5043 inode
= file_inode(fp
->filp
);
5044 ksmbd_acls_fattr(&fattr
, user_ns
, inode
);
5046 if (test_share_config_flag(work
->tcon
->share_conf
,
5047 KSMBD_SHARE_FLAG_ACL_XATTR
))
5048 ksmbd_vfs_get_sd_xattr(work
->conn
, user_ns
,
5049 fp
->filp
->f_path
.dentry
, &ppntsd
);
5051 rc
= build_sec_desc(user_ns
, pntsd
, ppntsd
, addition_info
,
5052 &secdesclen
, &fattr
);
5053 posix_acl_release(fattr
.cf_acls
);
5054 posix_acl_release(fattr
.cf_dacls
);
5056 ksmbd_fd_put(work
, fp
);
5060 rsp
->OutputBufferLength
= cpu_to_le32(secdesclen
);
5061 inc_rfc1001_len(rsp_org
, secdesclen
);
5066 * smb2_query_info() - handler for smb2 query info command
5067 * @work: smb work containing query info request buffer
5069 * Return: 0 on success, otherwise error
5071 int smb2_query_info(struct ksmbd_work
*work
)
5073 struct smb2_query_info_req
*req
;
5074 struct smb2_query_info_rsp
*rsp
, *rsp_org
;
5077 rsp_org
= work
->response_buf
;
5078 WORK_BUFFERS(work
, req
, rsp
);
5080 ksmbd_debug(SMB
, "GOT query info request\n");
5082 switch (req
->InfoType
) {
5083 case SMB2_O_INFO_FILE
:
5084 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_FILE\n");
5085 rc
= smb2_get_info_file(work
, req
, rsp
, (void *)rsp_org
);
5087 case SMB2_O_INFO_FILESYSTEM
:
5088 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_FILESYSTEM\n");
5089 rc
= smb2_get_info_filesystem(work
, req
, rsp
, (void *)rsp_org
);
5091 case SMB2_O_INFO_SECURITY
:
5092 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_SECURITY\n");
5093 rc
= smb2_get_info_sec(work
, req
, rsp
, (void *)rsp_org
);
5096 ksmbd_debug(SMB
, "InfoType %d not supported yet\n",
5103 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
5104 else if (rc
== -ENOENT
)
5105 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
5106 else if (rc
== -EIO
)
5107 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
5108 else if (rc
== -EOPNOTSUPP
|| rsp
->hdr
.Status
== 0)
5109 rsp
->hdr
.Status
= STATUS_INVALID_INFO_CLASS
;
5110 smb2_set_err_rsp(work
);
5112 ksmbd_debug(SMB
, "error while processing smb2 query rc = %d\n",
5116 rsp
->StructureSize
= cpu_to_le16(9);
5117 rsp
->OutputBufferOffset
= cpu_to_le16(72);
5118 inc_rfc1001_len(rsp_org
, 8);
5123 * smb2_close_pipe() - handler for closing IPC pipe
5124 * @work: smb work containing close request buffer
5128 static noinline
int smb2_close_pipe(struct ksmbd_work
*work
)
5131 struct smb2_close_req
*req
= work
->request_buf
;
5132 struct smb2_close_rsp
*rsp
= work
->response_buf
;
5134 id
= le64_to_cpu(req
->VolatileFileId
);
5135 ksmbd_session_rpc_close(work
->sess
, id
);
5137 rsp
->StructureSize
= cpu_to_le16(60);
5140 rsp
->CreationTime
= 0;
5141 rsp
->LastAccessTime
= 0;
5142 rsp
->LastWriteTime
= 0;
5143 rsp
->ChangeTime
= 0;
5144 rsp
->AllocationSize
= 0;
5146 rsp
->Attributes
= 0;
5147 inc_rfc1001_len(rsp
, 60);
5152 * smb2_close() - handler for smb2 close file command
5153 * @work: smb work containing close request buffer
5157 int smb2_close(struct ksmbd_work
*work
)
5159 u64 volatile_id
= KSMBD_NO_FID
;
5161 struct smb2_close_req
*req
;
5162 struct smb2_close_rsp
*rsp
;
5163 struct smb2_close_rsp
*rsp_org
;
5164 struct ksmbd_conn
*conn
= work
->conn
;
5165 struct ksmbd_file
*fp
;
5166 struct inode
*inode
;
5170 rsp_org
= work
->response_buf
;
5171 WORK_BUFFERS(work
, req
, rsp
);
5173 if (test_share_config_flag(work
->tcon
->share_conf
,
5174 KSMBD_SHARE_FLAG_PIPE
)) {
5175 ksmbd_debug(SMB
, "IPC pipe close request\n");
5176 return smb2_close_pipe(work
);
5179 sess_id
= le64_to_cpu(req
->hdr
.SessionId
);
5180 if (req
->hdr
.Flags
& SMB2_FLAGS_RELATED_OPERATIONS
)
5181 sess_id
= work
->compound_sid
;
5183 work
->compound_sid
= 0;
5184 if (check_session_id(conn
, sess_id
)) {
5185 work
->compound_sid
= sess_id
;
5187 rsp
->hdr
.Status
= STATUS_USER_SESSION_DELETED
;
5188 if (req
->hdr
.Flags
& SMB2_FLAGS_RELATED_OPERATIONS
)
5189 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
5194 if (work
->next_smb2_rcv_hdr_off
&&
5195 !has_file_id(le64_to_cpu(req
->VolatileFileId
))) {
5196 if (!has_file_id(work
->compound_fid
)) {
5197 /* file already closed, return FILE_CLOSED */
5198 ksmbd_debug(SMB
, "file already closed\n");
5199 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
5204 "Compound request set FID = %llu:%llu\n",
5206 work
->compound_pfid
);
5207 volatile_id
= work
->compound_fid
;
5209 /* file closed, stored id is not valid anymore */
5210 work
->compound_fid
= KSMBD_NO_FID
;
5211 work
->compound_pfid
= KSMBD_NO_FID
;
5214 volatile_id
= le64_to_cpu(req
->VolatileFileId
);
5216 ksmbd_debug(SMB
, "volatile_id = %llu\n", volatile_id
);
5218 rsp
->StructureSize
= cpu_to_le16(60);
5221 if (req
->Flags
== SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB
) {
5222 fp
= ksmbd_lookup_fd_fast(work
, volatile_id
);
5228 inode
= file_inode(fp
->filp
);
5229 rsp
->Flags
= SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB
;
5230 rsp
->AllocationSize
= S_ISDIR(inode
->i_mode
) ? 0 :
5231 cpu_to_le64(inode
->i_blocks
<< 9);
5232 rsp
->EndOfFile
= cpu_to_le64(inode
->i_size
);
5233 rsp
->Attributes
= fp
->f_ci
->m_fattr
;
5234 rsp
->CreationTime
= cpu_to_le64(fp
->create_time
);
5235 time
= ksmbd_UnixTimeToNT(inode
->i_atime
);
5236 rsp
->LastAccessTime
= cpu_to_le64(time
);
5237 time
= ksmbd_UnixTimeToNT(inode
->i_mtime
);
5238 rsp
->LastWriteTime
= cpu_to_le64(time
);
5239 time
= ksmbd_UnixTimeToNT(inode
->i_ctime
);
5240 rsp
->ChangeTime
= cpu_to_le64(time
);
5241 ksmbd_fd_put(work
, fp
);
5244 rsp
->AllocationSize
= 0;
5246 rsp
->Attributes
= 0;
5247 rsp
->CreationTime
= 0;
5248 rsp
->LastAccessTime
= 0;
5249 rsp
->LastWriteTime
= 0;
5250 rsp
->ChangeTime
= 0;
5253 err
= ksmbd_close_fd(work
, volatile_id
);
5256 if (rsp
->hdr
.Status
== 0)
5257 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
5258 smb2_set_err_rsp(work
);
5260 inc_rfc1001_len(rsp_org
, 60);
5267 * smb2_echo() - handler for smb2 echo(ping) command
5268 * @work: smb work containing echo request buffer
5272 int smb2_echo(struct ksmbd_work
*work
)
5274 struct smb2_echo_rsp
*rsp
= work
->response_buf
;
5276 rsp
->StructureSize
= cpu_to_le16(4);
5278 inc_rfc1001_len(rsp
, 4);
5282 static int smb2_rename(struct ksmbd_work
*work
,
5283 struct ksmbd_file
*fp
,
5284 struct user_namespace
*user_ns
,
5285 struct smb2_file_rename_info
*file_info
,
5286 struct nls_table
*local_nls
)
5288 struct ksmbd_share_config
*share
= fp
->tcon
->share_conf
;
5289 char *new_name
= NULL
, *abs_oldname
= NULL
, *old_name
= NULL
;
5290 char *pathname
= NULL
;
5292 bool file_present
= true;
5295 ksmbd_debug(SMB
, "setting FILE_RENAME_INFO\n");
5296 pathname
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5300 abs_oldname
= d_path(&fp
->filp
->f_path
, pathname
, PATH_MAX
);
5301 if (IS_ERR(abs_oldname
)) {
5305 old_name
= strrchr(abs_oldname
, '/');
5306 if (old_name
&& old_name
[1] != '\0') {
5309 ksmbd_debug(SMB
, "can't get last component in path %s\n",
5315 new_name
= smb2_get_name(share
,
5316 file_info
->FileName
,
5317 le32_to_cpu(file_info
->FileNameLength
),
5319 if (IS_ERR(new_name
)) {
5320 rc
= PTR_ERR(new_name
);
5324 if (strchr(new_name
, ':')) {
5326 char *xattr_stream_name
, *stream_name
= NULL
;
5327 size_t xattr_stream_size
;
5330 rc
= parse_stream_name(new_name
, &stream_name
, &s_type
);
5334 len
= strlen(new_name
);
5335 if (len
> 0 && new_name
[len
- 1] != '/') {
5336 pr_err("not allow base filename in rename\n");
5341 rc
= ksmbd_vfs_xattr_stream_name(stream_name
,
5348 rc
= ksmbd_vfs_setxattr(user_ns
,
5349 fp
->filp
->f_path
.dentry
,
5353 pr_err("failed to store stream name in xattr: %d\n",
5362 ksmbd_debug(SMB
, "new name %s\n", new_name
);
5363 rc
= ksmbd_vfs_kern_path(work
, new_name
, LOOKUP_NO_SYMLINKS
, &path
, 1);
5367 file_present
= false;
5372 if (ksmbd_share_veto_filename(share
, new_name
)) {
5374 ksmbd_debug(SMB
, "Can't rename vetoed file: %s\n", new_name
);
5378 if (file_info
->ReplaceIfExists
) {
5380 rc
= ksmbd_vfs_remove_file(work
, new_name
);
5382 if (rc
!= -ENOTEMPTY
)
5384 ksmbd_debug(SMB
, "cannot delete %s, rc %d\n",
5391 strncmp(old_name
, path
.dentry
->d_name
.name
, strlen(old_name
))) {
5394 "cannot rename already existing file\n");
5399 rc
= ksmbd_vfs_fp_rename(work
, fp
, new_name
);
5402 if (!IS_ERR(new_name
))
5407 static int smb2_create_link(struct ksmbd_work
*work
,
5408 struct ksmbd_share_config
*share
,
5409 struct smb2_file_link_info
*file_info
,
5410 unsigned int buf_len
, struct file
*filp
,
5411 struct nls_table
*local_nls
)
5413 char *link_name
= NULL
, *target_name
= NULL
, *pathname
= NULL
;
5415 bool file_present
= true;
5418 if (buf_len
< (u64
)sizeof(struct smb2_file_link_info
) +
5419 le32_to_cpu(file_info
->FileNameLength
))
5422 ksmbd_debug(SMB
, "setting FILE_LINK_INFORMATION\n");
5423 pathname
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5427 link_name
= smb2_get_name(share
,
5428 file_info
->FileName
,
5429 le32_to_cpu(file_info
->FileNameLength
),
5431 if (IS_ERR(link_name
) || S_ISDIR(file_inode(filp
)->i_mode
)) {
5436 ksmbd_debug(SMB
, "link name is %s\n", link_name
);
5437 target_name
= d_path(&filp
->f_path
, pathname
, PATH_MAX
);
5438 if (IS_ERR(target_name
)) {
5443 ksmbd_debug(SMB
, "target name is %s\n", target_name
);
5444 rc
= ksmbd_vfs_kern_path(work
, link_name
, LOOKUP_NO_SYMLINKS
, &path
, 0);
5448 file_present
= false;
5453 if (file_info
->ReplaceIfExists
) {
5455 rc
= ksmbd_vfs_remove_file(work
, link_name
);
5458 ksmbd_debug(SMB
, "cannot delete %s\n",
5466 ksmbd_debug(SMB
, "link already exists\n");
5471 rc
= ksmbd_vfs_link(work
, target_name
, link_name
);
5475 if (!IS_ERR(link_name
))
5481 static int set_file_basic_info(struct ksmbd_file
*fp
,
5482 struct smb2_file_basic_info
*file_info
,
5483 struct ksmbd_share_config
*share
)
5487 struct inode
*inode
;
5488 struct user_namespace
*user_ns
;
5491 if (!(fp
->daccess
& FILE_WRITE_ATTRIBUTES_LE
))
5496 inode
= file_inode(filp
);
5497 user_ns
= file_mnt_user_ns(filp
);
5499 if (file_info
->CreationTime
)
5500 fp
->create_time
= le64_to_cpu(file_info
->CreationTime
);
5502 if (file_info
->LastAccessTime
) {
5503 attrs
.ia_atime
= ksmbd_NTtimeToUnix(file_info
->LastAccessTime
);
5504 attrs
.ia_valid
|= (ATTR_ATIME
| ATTR_ATIME_SET
);
5507 attrs
.ia_valid
|= ATTR_CTIME
;
5508 if (file_info
->ChangeTime
)
5509 attrs
.ia_ctime
= ksmbd_NTtimeToUnix(file_info
->ChangeTime
);
5511 attrs
.ia_ctime
= inode
->i_ctime
;
5513 if (file_info
->LastWriteTime
) {
5514 attrs
.ia_mtime
= ksmbd_NTtimeToUnix(file_info
->LastWriteTime
);
5515 attrs
.ia_valid
|= (ATTR_MTIME
| ATTR_MTIME_SET
);
5518 if (file_info
->Attributes
) {
5519 if (!S_ISDIR(inode
->i_mode
) &&
5520 file_info
->Attributes
& ATTR_DIRECTORY_LE
) {
5521 pr_err("can't change a file to a directory\n");
5525 if (!(S_ISDIR(inode
->i_mode
) && file_info
->Attributes
== ATTR_NORMAL_LE
))
5526 fp
->f_ci
->m_fattr
= file_info
->Attributes
|
5527 (fp
->f_ci
->m_fattr
& ATTR_DIRECTORY_LE
);
5530 if (test_share_config_flag(share
, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS
) &&
5531 (file_info
->CreationTime
|| file_info
->Attributes
)) {
5532 struct xattr_dos_attrib da
= {0};
5535 da
.itime
= fp
->itime
;
5536 da
.create_time
= fp
->create_time
;
5537 da
.attr
= le32_to_cpu(fp
->f_ci
->m_fattr
);
5538 da
.flags
= XATTR_DOSINFO_ATTRIB
| XATTR_DOSINFO_CREATE_TIME
|
5539 XATTR_DOSINFO_ITIME
;
5541 rc
= ksmbd_vfs_set_dos_attrib_xattr(user_ns
,
5542 filp
->f_path
.dentry
, &da
);
5545 "failed to restore file attribute in EA\n");
5549 if (attrs
.ia_valid
) {
5550 struct dentry
*dentry
= filp
->f_path
.dentry
;
5551 struct inode
*inode
= d_inode(dentry
);
5553 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
5557 inode
->i_ctime
= attrs
.ia_ctime
;
5558 attrs
.ia_valid
&= ~ATTR_CTIME
;
5559 rc
= notify_change(user_ns
, dentry
, &attrs
, NULL
);
5560 inode_unlock(inode
);
5565 static int set_file_allocation_info(struct ksmbd_work
*work
,
5566 struct ksmbd_file
*fp
,
5567 struct smb2_file_alloc_info
*file_alloc_info
)
5570 * TODO : It's working fine only when store dos attributes
5571 * is not yes. need to implement a logic which works
5572 * properly with any smb.conf option
5576 struct inode
*inode
;
5579 if (!(fp
->daccess
& FILE_WRITE_DATA_LE
))
5582 alloc_blks
= (le64_to_cpu(file_alloc_info
->AllocationSize
) + 511) >> 9;
5583 inode
= file_inode(fp
->filp
);
5585 if (alloc_blks
> inode
->i_blocks
) {
5586 smb_break_all_levII_oplock(work
, fp
, 1);
5587 rc
= vfs_fallocate(fp
->filp
, FALLOC_FL_KEEP_SIZE
, 0,
5589 if (rc
&& rc
!= -EOPNOTSUPP
) {
5590 pr_err("vfs_fallocate is failed : %d\n", rc
);
5593 } else if (alloc_blks
< inode
->i_blocks
) {
5597 * Allocation size could be smaller than original one
5598 * which means allocated blocks in file should be
5599 * deallocated. use truncate to cut out it, but inode
5600 * size is also updated with truncate offset.
5601 * inode size is retained by backup inode size.
5603 size
= i_size_read(inode
);
5604 rc
= ksmbd_vfs_truncate(work
, fp
, alloc_blks
* 512);
5606 pr_err("truncate failed! filename : %s, err %d\n",
5610 if (size
< alloc_blks
* 512)
5611 i_size_write(inode
, size
);
5616 static int set_end_of_file_info(struct ksmbd_work
*work
, struct ksmbd_file
*fp
,
5617 struct smb2_file_eof_info
*file_eof_info
)
5620 struct inode
*inode
;
5623 if (!(fp
->daccess
& FILE_WRITE_DATA_LE
))
5626 newsize
= le64_to_cpu(file_eof_info
->EndOfFile
);
5627 inode
= file_inode(fp
->filp
);
5630 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5631 * on FAT32 shared device, truncate execution time is too long
5632 * and network error could cause from windows client. because
5633 * truncate of some filesystem like FAT32 fill zero data in
5636 if (inode
->i_sb
->s_magic
!= MSDOS_SUPER_MAGIC
) {
5637 ksmbd_debug(SMB
, "filename : %s truncated to newsize %lld\n",
5638 fp
->filename
, newsize
);
5639 rc
= ksmbd_vfs_truncate(work
, fp
, newsize
);
5641 ksmbd_debug(SMB
, "truncate failed! filename : %s err %d\n",
5651 static int set_rename_info(struct ksmbd_work
*work
, struct ksmbd_file
*fp
,
5652 struct smb2_file_rename_info
*rename_info
,
5653 unsigned int buf_len
)
5655 struct user_namespace
*user_ns
;
5656 struct ksmbd_file
*parent_fp
;
5657 struct dentry
*parent
;
5658 struct dentry
*dentry
= fp
->filp
->f_path
.dentry
;
5661 if (!(fp
->daccess
& FILE_DELETE_LE
)) {
5662 pr_err("no right to delete : 0x%x\n", fp
->daccess
);
5666 if (buf_len
< (u64
)sizeof(struct smb2_file_rename_info
) +
5667 le32_to_cpu(rename_info
->FileNameLength
))
5670 user_ns
= file_mnt_user_ns(fp
->filp
);
5671 if (ksmbd_stream_fd(fp
))
5674 parent
= dget_parent(dentry
);
5675 ret
= ksmbd_vfs_lock_parent(user_ns
, parent
, dentry
);
5681 parent_fp
= ksmbd_lookup_fd_inode(d_inode(parent
));
5682 inode_unlock(d_inode(parent
));
5686 if (parent_fp
->daccess
& FILE_DELETE_LE
) {
5687 pr_err("parent dir is opened with delete access\n");
5692 return smb2_rename(work
, fp
, user_ns
, rename_info
,
5693 work
->sess
->conn
->local_nls
);
5696 static int set_file_disposition_info(struct ksmbd_file
*fp
,
5697 struct smb2_file_disposition_info
*file_info
)
5699 struct inode
*inode
;
5701 if (!(fp
->daccess
& FILE_DELETE_LE
)) {
5702 pr_err("no right to delete : 0x%x\n", fp
->daccess
);
5706 inode
= file_inode(fp
->filp
);
5707 if (file_info
->DeletePending
) {
5708 if (S_ISDIR(inode
->i_mode
) &&
5709 ksmbd_vfs_empty_dir(fp
) == -ENOTEMPTY
)
5711 ksmbd_set_inode_pending_delete(fp
);
5713 ksmbd_clear_inode_pending_delete(fp
);
5718 static int set_file_position_info(struct ksmbd_file
*fp
,
5719 struct smb2_file_pos_info
*file_info
)
5721 loff_t current_byte_offset
;
5722 unsigned long sector_size
;
5723 struct inode
*inode
;
5725 inode
= file_inode(fp
->filp
);
5726 current_byte_offset
= le64_to_cpu(file_info
->CurrentByteOffset
);
5727 sector_size
= inode
->i_sb
->s_blocksize
;
5729 if (current_byte_offset
< 0 ||
5730 (fp
->coption
== FILE_NO_INTERMEDIATE_BUFFERING_LE
&&
5731 current_byte_offset
& (sector_size
- 1))) {
5732 pr_err("CurrentByteOffset is not valid : %llu\n",
5733 current_byte_offset
);
5737 fp
->filp
->f_pos
= current_byte_offset
;
5741 static int set_file_mode_info(struct ksmbd_file
*fp
,
5742 struct smb2_file_mode_info
*file_info
)
5746 mode
= file_info
->Mode
;
5748 if ((mode
& ~FILE_MODE_INFO_MASK
) ||
5749 (mode
& FILE_SYNCHRONOUS_IO_ALERT_LE
&&
5750 mode
& FILE_SYNCHRONOUS_IO_NONALERT_LE
)) {
5751 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode
));
5756 * TODO : need to implement consideration for
5757 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5759 ksmbd_vfs_set_fadvise(fp
->filp
, mode
);
5765 * smb2_set_info_file() - handler for smb2 set info command
5766 * @work: smb work containing set info command buffer
5767 * @fp: ksmbd_file pointer
5768 * @info_class: smb2 set info class
5769 * @share: ksmbd_share_config pointer
5771 * Return: 0 on success, otherwise error
5772 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5774 static int smb2_set_info_file(struct ksmbd_work
*work
, struct ksmbd_file
*fp
,
5775 struct smb2_set_info_req
*req
,
5776 struct ksmbd_share_config
*share
)
5778 unsigned int buf_len
= le32_to_cpu(req
->BufferLength
);
5780 switch (req
->FileInfoClass
) {
5781 case FILE_BASIC_INFORMATION
:
5783 if (buf_len
< sizeof(struct smb2_file_basic_info
))
5786 return set_file_basic_info(fp
, (struct smb2_file_basic_info
*)req
->Buffer
, share
);
5788 case FILE_ALLOCATION_INFORMATION
:
5790 if (buf_len
< sizeof(struct smb2_file_alloc_info
))
5793 return set_file_allocation_info(work
, fp
,
5794 (struct smb2_file_alloc_info
*)req
->Buffer
);
5796 case FILE_END_OF_FILE_INFORMATION
:
5798 if (buf_len
< sizeof(struct smb2_file_eof_info
))
5801 return set_end_of_file_info(work
, fp
,
5802 (struct smb2_file_eof_info
*)req
->Buffer
);
5804 case FILE_RENAME_INFORMATION
:
5806 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
5808 "User does not have write permission\n");
5812 if (buf_len
< sizeof(struct smb2_file_rename_info
))
5815 return set_rename_info(work
, fp
,
5816 (struct smb2_file_rename_info
*)req
->Buffer
,
5819 case FILE_LINK_INFORMATION
:
5821 if (buf_len
< sizeof(struct smb2_file_link_info
))
5824 return smb2_create_link(work
, work
->tcon
->share_conf
,
5825 (struct smb2_file_link_info
*)req
->Buffer
,
5827 work
->sess
->conn
->local_nls
);
5829 case FILE_DISPOSITION_INFORMATION
:
5831 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
5833 "User does not have write permission\n");
5837 if (buf_len
< sizeof(struct smb2_file_disposition_info
))
5840 return set_file_disposition_info(fp
,
5841 (struct smb2_file_disposition_info
*)req
->Buffer
);
5843 case FILE_FULL_EA_INFORMATION
:
5845 if (!(fp
->daccess
& FILE_WRITE_EA_LE
)) {
5846 pr_err("Not permitted to write ext attr: 0x%x\n",
5851 if (buf_len
< sizeof(struct smb2_ea_info
))
5854 return smb2_set_ea((struct smb2_ea_info
*)req
->Buffer
,
5855 buf_len
, &fp
->filp
->f_path
);
5857 case FILE_POSITION_INFORMATION
:
5859 if (buf_len
< sizeof(struct smb2_file_pos_info
))
5862 return set_file_position_info(fp
, (struct smb2_file_pos_info
*)req
->Buffer
);
5864 case FILE_MODE_INFORMATION
:
5866 if (buf_len
< sizeof(struct smb2_file_mode_info
))
5869 return set_file_mode_info(fp
, (struct smb2_file_mode_info
*)req
->Buffer
);
5873 pr_err("Unimplemented Fileinfoclass :%d\n", req
->FileInfoClass
);
5877 static int smb2_set_info_sec(struct ksmbd_file
*fp
, int addition_info
,
5878 char *buffer
, int buf_len
)
5880 struct smb_ntsd
*pntsd
= (struct smb_ntsd
*)buffer
;
5882 fp
->saccess
|= FILE_SHARE_DELETE_LE
;
5884 return set_info_sec(fp
->conn
, fp
->tcon
, &fp
->filp
->f_path
, pntsd
,
5889 * smb2_set_info() - handler for smb2 set info command handler
5890 * @work: smb work containing set info request buffer
5892 * Return: 0 on success, otherwise error
5894 int smb2_set_info(struct ksmbd_work
*work
)
5896 struct smb2_set_info_req
*req
;
5897 struct smb2_set_info_rsp
*rsp
, *rsp_org
;
5898 struct ksmbd_file
*fp
;
5900 unsigned int id
= KSMBD_NO_FID
, pid
= KSMBD_NO_FID
;
5902 ksmbd_debug(SMB
, "Received set info request\n");
5904 rsp_org
= work
->response_buf
;
5905 if (work
->next_smb2_rcv_hdr_off
) {
5906 req
= ksmbd_req_buf_next(work
);
5907 rsp
= ksmbd_resp_buf_next(work
);
5908 if (!has_file_id(le64_to_cpu(req
->VolatileFileId
))) {
5909 ksmbd_debug(SMB
, "Compound request set FID = %llu\n",
5910 work
->compound_fid
);
5911 id
= work
->compound_fid
;
5912 pid
= work
->compound_pfid
;
5915 req
= work
->request_buf
;
5916 rsp
= work
->response_buf
;
5919 if (!has_file_id(id
)) {
5920 id
= le64_to_cpu(req
->VolatileFileId
);
5921 pid
= le64_to_cpu(req
->PersistentFileId
);
5924 fp
= ksmbd_lookup_fd_slow(work
, id
, pid
);
5926 ksmbd_debug(SMB
, "Invalid id for close: %u\n", id
);
5931 switch (req
->InfoType
) {
5932 case SMB2_O_INFO_FILE
:
5933 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_FILE\n");
5934 rc
= smb2_set_info_file(work
, fp
, req
, work
->tcon
->share_conf
);
5936 case SMB2_O_INFO_SECURITY
:
5937 ksmbd_debug(SMB
, "GOT SMB2_O_INFO_SECURITY\n");
5938 if (ksmbd_override_fsids(work
)) {
5942 rc
= smb2_set_info_sec(fp
,
5943 le32_to_cpu(req
->AdditionalInformation
),
5945 le32_to_cpu(req
->BufferLength
));
5946 ksmbd_revert_fsids(work
);
5955 rsp
->StructureSize
= cpu_to_le16(2);
5956 inc_rfc1001_len(rsp_org
, 2);
5957 ksmbd_fd_put(work
, fp
);
5961 if (rc
== -EACCES
|| rc
== -EPERM
|| rc
== -EXDEV
)
5962 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
5963 else if (rc
== -EINVAL
)
5964 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
5965 else if (rc
== -ESHARE
)
5966 rsp
->hdr
.Status
= STATUS_SHARING_VIOLATION
;
5967 else if (rc
== -ENOENT
)
5968 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_INVALID
;
5969 else if (rc
== -EBUSY
|| rc
== -ENOTEMPTY
)
5970 rsp
->hdr
.Status
= STATUS_DIRECTORY_NOT_EMPTY
;
5971 else if (rc
== -EAGAIN
)
5972 rsp
->hdr
.Status
= STATUS_FILE_LOCK_CONFLICT
;
5973 else if (rc
== -EBADF
|| rc
== -ESTALE
)
5974 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
5975 else if (rc
== -EEXIST
)
5976 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_COLLISION
;
5977 else if (rsp
->hdr
.Status
== 0 || rc
== -EOPNOTSUPP
)
5978 rsp
->hdr
.Status
= STATUS_INVALID_INFO_CLASS
;
5979 smb2_set_err_rsp(work
);
5980 ksmbd_fd_put(work
, fp
);
5981 ksmbd_debug(SMB
, "error while processing smb2 query rc = %d\n", rc
);
5986 * smb2_read_pipe() - handler for smb2 read from IPC pipe
5987 * @work: smb work containing read IPC pipe command buffer
5989 * Return: 0 on success, otherwise error
5991 static noinline
int smb2_read_pipe(struct ksmbd_work
*work
)
5993 int nbytes
= 0, err
;
5995 struct ksmbd_rpc_command
*rpc_resp
;
5996 struct smb2_read_req
*req
= work
->request_buf
;
5997 struct smb2_read_rsp
*rsp
= work
->response_buf
;
5999 id
= le64_to_cpu(req
->VolatileFileId
);
6001 inc_rfc1001_len(rsp
, 16);
6002 rpc_resp
= ksmbd_rpc_read(work
->sess
, id
);
6004 if (rpc_resp
->flags
!= KSMBD_RPC_OK
) {
6009 work
->aux_payload_buf
=
6010 kvmalloc(rpc_resp
->payload_sz
, GFP_KERNEL
| __GFP_ZERO
);
6011 if (!work
->aux_payload_buf
) {
6016 memcpy(work
->aux_payload_buf
, rpc_resp
->payload
,
6017 rpc_resp
->payload_sz
);
6019 nbytes
= rpc_resp
->payload_sz
;
6020 work
->resp_hdr_sz
= get_rfc1002_len(rsp
) + 4;
6021 work
->aux_payload_sz
= nbytes
;
6025 rsp
->StructureSize
= cpu_to_le16(17);
6026 rsp
->DataOffset
= 80;
6028 rsp
->DataLength
= cpu_to_le32(nbytes
);
6029 rsp
->DataRemaining
= 0;
6031 inc_rfc1001_len(rsp
, nbytes
);
6035 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
6036 smb2_set_err_rsp(work
);
6041 static ssize_t
smb2_read_rdma_channel(struct ksmbd_work
*work
,
6042 struct smb2_read_req
*req
, void *data_buf
,
6045 struct smb2_buffer_desc_v1
*desc
=
6046 (struct smb2_buffer_desc_v1
*)&req
->Buffer
[0];
6049 if (work
->conn
->dialect
== SMB30_PROT_ID
&&
6050 req
->Channel
!= SMB2_CHANNEL_RDMA_V1
)
6053 if (req
->ReadChannelInfoOffset
== 0 ||
6054 le16_to_cpu(req
->ReadChannelInfoLength
) < sizeof(*desc
))
6057 work
->need_invalidate_rkey
=
6058 (req
->Channel
== SMB2_CHANNEL_RDMA_V1_INVALIDATE
);
6059 work
->remote_key
= le32_to_cpu(desc
->token
);
6061 err
= ksmbd_conn_rdma_write(work
->conn
, data_buf
, length
,
6062 le32_to_cpu(desc
->token
),
6063 le64_to_cpu(desc
->offset
),
6064 le32_to_cpu(desc
->length
));
6072 * smb2_read() - handler for smb2 read from file
6073 * @work: smb work containing read command buffer
6075 * Return: 0 on success, otherwise error
6077 int smb2_read(struct ksmbd_work
*work
)
6079 struct ksmbd_conn
*conn
= work
->conn
;
6080 struct smb2_read_req
*req
;
6081 struct smb2_read_rsp
*rsp
, *rsp_org
;
6082 struct ksmbd_file
*fp
;
6084 size_t length
, mincount
;
6085 ssize_t nbytes
= 0, remain_bytes
= 0;
6088 rsp_org
= work
->response_buf
;
6089 WORK_BUFFERS(work
, req
, rsp
);
6091 if (test_share_config_flag(work
->tcon
->share_conf
,
6092 KSMBD_SHARE_FLAG_PIPE
)) {
6093 ksmbd_debug(SMB
, "IPC pipe read request\n");
6094 return smb2_read_pipe(work
);
6097 fp
= ksmbd_lookup_fd_slow(work
, le64_to_cpu(req
->VolatileFileId
),
6098 le64_to_cpu(req
->PersistentFileId
));
6104 if (!(fp
->daccess
& (FILE_READ_DATA_LE
| FILE_READ_ATTRIBUTES_LE
))) {
6105 pr_err("Not permitted to read : 0x%x\n", fp
->daccess
);
6110 offset
= le64_to_cpu(req
->Offset
);
6111 length
= le32_to_cpu(req
->Length
);
6112 mincount
= le32_to_cpu(req
->MinimumCount
);
6114 if (length
> conn
->vals
->max_read_size
) {
6115 ksmbd_debug(SMB
, "limiting read size to max size(%u)\n",
6116 conn
->vals
->max_read_size
);
6121 ksmbd_debug(SMB
, "filename %pd, offset %lld, len %zu\n",
6122 fp
->filp
->f_path
.dentry
, offset
, length
);
6124 work
->aux_payload_buf
= kvmalloc(length
, GFP_KERNEL
| __GFP_ZERO
);
6125 if (!work
->aux_payload_buf
) {
6130 nbytes
= ksmbd_vfs_read(work
, fp
, length
, &offset
);
6136 if ((nbytes
== 0 && length
!= 0) || nbytes
< mincount
) {
6137 kvfree(work
->aux_payload_buf
);
6138 work
->aux_payload_buf
= NULL
;
6139 rsp
->hdr
.Status
= STATUS_END_OF_FILE
;
6140 smb2_set_err_rsp(work
);
6141 ksmbd_fd_put(work
, fp
);
6145 ksmbd_debug(SMB
, "nbytes %zu, offset %lld mincount %zu\n",
6146 nbytes
, offset
, mincount
);
6148 if (req
->Channel
== SMB2_CHANNEL_RDMA_V1_INVALIDATE
||
6149 req
->Channel
== SMB2_CHANNEL_RDMA_V1
) {
6150 /* write data to the client using rdma channel */
6151 remain_bytes
= smb2_read_rdma_channel(work
, req
,
6152 work
->aux_payload_buf
,
6154 kvfree(work
->aux_payload_buf
);
6155 work
->aux_payload_buf
= NULL
;
6158 if (remain_bytes
< 0) {
6159 err
= (int)remain_bytes
;
6164 rsp
->StructureSize
= cpu_to_le16(17);
6165 rsp
->DataOffset
= 80;
6167 rsp
->DataLength
= cpu_to_le32(nbytes
);
6168 rsp
->DataRemaining
= cpu_to_le32(remain_bytes
);
6170 inc_rfc1001_len(rsp_org
, 16);
6171 work
->resp_hdr_sz
= get_rfc1002_len(rsp_org
) + 4;
6172 work
->aux_payload_sz
= nbytes
;
6173 inc_rfc1001_len(rsp_org
, nbytes
);
6174 ksmbd_fd_put(work
, fp
);
6180 rsp
->hdr
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
6181 else if (err
== -EAGAIN
)
6182 rsp
->hdr
.Status
= STATUS_FILE_LOCK_CONFLICT
;
6183 else if (err
== -ENOENT
)
6184 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
6185 else if (err
== -EACCES
)
6186 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
6187 else if (err
== -ESHARE
)
6188 rsp
->hdr
.Status
= STATUS_SHARING_VIOLATION
;
6189 else if (err
== -EINVAL
)
6190 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
6192 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6194 smb2_set_err_rsp(work
);
6196 ksmbd_fd_put(work
, fp
);
6201 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6202 * @work: smb work containing write IPC pipe command buffer
6204 * Return: 0 on success, otherwise error
6206 static noinline
int smb2_write_pipe(struct ksmbd_work
*work
)
6208 struct smb2_write_req
*req
= work
->request_buf
;
6209 struct smb2_write_rsp
*rsp
= work
->response_buf
;
6210 struct ksmbd_rpc_command
*rpc_resp
;
6212 int err
= 0, ret
= 0;
6216 length
= le32_to_cpu(req
->Length
);
6217 id
= le64_to_cpu(req
->VolatileFileId
);
6219 if (le16_to_cpu(req
->DataOffset
) ==
6220 (offsetof(struct smb2_write_req
, Buffer
) - 4)) {
6221 data_buf
= (char *)&req
->Buffer
[0];
6223 if ((le16_to_cpu(req
->DataOffset
) > get_rfc1002_len(req
)) ||
6224 (le16_to_cpu(req
->DataOffset
) + length
> get_rfc1002_len(req
))) {
6225 pr_err("invalid write data offset %u, smb_len %u\n",
6226 le16_to_cpu(req
->DataOffset
),
6227 get_rfc1002_len(req
));
6232 data_buf
= (char *)(((char *)&req
->hdr
.ProtocolId
) +
6233 le16_to_cpu(req
->DataOffset
));
6236 rpc_resp
= ksmbd_rpc_write(work
->sess
, id
, data_buf
, length
);
6238 if (rpc_resp
->flags
== KSMBD_RPC_ENOTIMPLEMENTED
) {
6239 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
6241 smb2_set_err_rsp(work
);
6244 if (rpc_resp
->flags
!= KSMBD_RPC_OK
) {
6245 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6246 smb2_set_err_rsp(work
);
6253 rsp
->StructureSize
= cpu_to_le16(17);
6254 rsp
->DataOffset
= 0;
6256 rsp
->DataLength
= cpu_to_le32(length
);
6257 rsp
->DataRemaining
= 0;
6259 inc_rfc1001_len(rsp
, 16);
6263 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6264 smb2_set_err_rsp(work
);
6270 static ssize_t
smb2_write_rdma_channel(struct ksmbd_work
*work
,
6271 struct smb2_write_req
*req
,
6272 struct ksmbd_file
*fp
,
6273 loff_t offset
, size_t length
, bool sync
)
6275 struct smb2_buffer_desc_v1
*desc
;
6280 desc
= (struct smb2_buffer_desc_v1
*)&req
->Buffer
[0];
6282 if (work
->conn
->dialect
== SMB30_PROT_ID
&&
6283 req
->Channel
!= SMB2_CHANNEL_RDMA_V1
)
6286 if (req
->Length
!= 0 || req
->DataOffset
!= 0)
6289 if (req
->WriteChannelInfoOffset
== 0 ||
6290 le16_to_cpu(req
->WriteChannelInfoLength
) < sizeof(*desc
))
6293 work
->need_invalidate_rkey
=
6294 (req
->Channel
== SMB2_CHANNEL_RDMA_V1_INVALIDATE
);
6295 work
->remote_key
= le32_to_cpu(desc
->token
);
6297 data_buf
= kvmalloc(length
, GFP_KERNEL
| __GFP_ZERO
);
6301 ret
= ksmbd_conn_rdma_read(work
->conn
, data_buf
, length
,
6302 le32_to_cpu(desc
->token
),
6303 le64_to_cpu(desc
->offset
),
6304 le32_to_cpu(desc
->length
));
6310 ret
= ksmbd_vfs_write(work
, fp
, data_buf
, length
, &offset
, sync
, &nbytes
);
6319 * smb2_write() - handler for smb2 write from file
6320 * @work: smb work containing write command buffer
6322 * Return: 0 on success, otherwise error
6324 int smb2_write(struct ksmbd_work
*work
)
6326 struct smb2_write_req
*req
;
6327 struct smb2_write_rsp
*rsp
, *rsp_org
;
6328 struct ksmbd_file
*fp
= NULL
;
6333 bool writethrough
= false;
6336 rsp_org
= work
->response_buf
;
6337 WORK_BUFFERS(work
, req
, rsp
);
6339 if (test_share_config_flag(work
->tcon
->share_conf
, KSMBD_SHARE_FLAG_PIPE
)) {
6340 ksmbd_debug(SMB
, "IPC pipe write request\n");
6341 return smb2_write_pipe(work
);
6344 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
6345 ksmbd_debug(SMB
, "User does not have write permission\n");
6350 fp
= ksmbd_lookup_fd_slow(work
, le64_to_cpu(req
->VolatileFileId
),
6351 le64_to_cpu(req
->PersistentFileId
));
6357 if (!(fp
->daccess
& (FILE_WRITE_DATA_LE
| FILE_READ_ATTRIBUTES_LE
))) {
6358 pr_err("Not permitted to write : 0x%x\n", fp
->daccess
);
6363 offset
= le64_to_cpu(req
->Offset
);
6364 length
= le32_to_cpu(req
->Length
);
6366 if (length
> work
->conn
->vals
->max_write_size
) {
6367 ksmbd_debug(SMB
, "limiting write size to max size(%u)\n",
6368 work
->conn
->vals
->max_write_size
);
6373 if (le32_to_cpu(req
->Flags
) & SMB2_WRITEFLAG_WRITE_THROUGH
)
6374 writethrough
= true;
6376 if (req
->Channel
!= SMB2_CHANNEL_RDMA_V1
&&
6377 req
->Channel
!= SMB2_CHANNEL_RDMA_V1_INVALIDATE
) {
6378 if (le16_to_cpu(req
->DataOffset
) ==
6379 (offsetof(struct smb2_write_req
, Buffer
) - 4)) {
6380 data_buf
= (char *)&req
->Buffer
[0];
6382 if ((le16_to_cpu(req
->DataOffset
) > get_rfc1002_len(req
)) ||
6383 (le16_to_cpu(req
->DataOffset
) + length
> get_rfc1002_len(req
))) {
6384 pr_err("invalid write data offset %u, smb_len %u\n",
6385 le16_to_cpu(req
->DataOffset
),
6386 get_rfc1002_len(req
));
6391 data_buf
= (char *)(((char *)&req
->hdr
.ProtocolId
) +
6392 le16_to_cpu(req
->DataOffset
));
6395 ksmbd_debug(SMB
, "flags %u\n", le32_to_cpu(req
->Flags
));
6396 if (le32_to_cpu(req
->Flags
) & SMB2_WRITEFLAG_WRITE_THROUGH
)
6397 writethrough
= true;
6399 ksmbd_debug(SMB
, "filename %pd, offset %lld, len %zu\n",
6400 fp
->filp
->f_path
.dentry
, offset
, length
);
6401 err
= ksmbd_vfs_write(work
, fp
, data_buf
, length
, &offset
,
6402 writethrough
, &nbytes
);
6406 /* read data from the client using rdma channel, and
6409 nbytes
= smb2_write_rdma_channel(work
, req
, fp
, offset
,
6410 le32_to_cpu(req
->RemainingBytes
),
6418 rsp
->StructureSize
= cpu_to_le16(17);
6419 rsp
->DataOffset
= 0;
6421 rsp
->DataLength
= cpu_to_le32(nbytes
);
6422 rsp
->DataRemaining
= 0;
6424 inc_rfc1001_len(rsp_org
, 16);
6425 ksmbd_fd_put(work
, fp
);
6430 rsp
->hdr
.Status
= STATUS_FILE_LOCK_CONFLICT
;
6431 else if (err
== -ENOSPC
|| err
== -EFBIG
)
6432 rsp
->hdr
.Status
= STATUS_DISK_FULL
;
6433 else if (err
== -ENOENT
)
6434 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
6435 else if (err
== -EACCES
)
6436 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
6437 else if (err
== -ESHARE
)
6438 rsp
->hdr
.Status
= STATUS_SHARING_VIOLATION
;
6439 else if (err
== -EINVAL
)
6440 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
6442 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6444 smb2_set_err_rsp(work
);
6445 ksmbd_fd_put(work
, fp
);
6450 * smb2_flush() - handler for smb2 flush file - fsync
6451 * @work: smb work containing flush command buffer
6453 * Return: 0 on success, otherwise error
6455 int smb2_flush(struct ksmbd_work
*work
)
6457 struct smb2_flush_req
*req
;
6458 struct smb2_flush_rsp
*rsp
, *rsp_org
;
6461 rsp_org
= work
->response_buf
;
6462 WORK_BUFFERS(work
, req
, rsp
);
6464 ksmbd_debug(SMB
, "SMB2_FLUSH called for fid %llu\n",
6465 le64_to_cpu(req
->VolatileFileId
));
6467 err
= ksmbd_vfs_fsync(work
,
6468 le64_to_cpu(req
->VolatileFileId
),
6469 le64_to_cpu(req
->PersistentFileId
));
6473 rsp
->StructureSize
= cpu_to_le16(4);
6475 inc_rfc1001_len(rsp_org
, 4);
6480 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
6481 smb2_set_err_rsp(work
);
6488 * smb2_cancel() - handler for smb2 cancel command
6489 * @work: smb work containing cancel command buffer
6491 * Return: 0 on success, otherwise error
6493 int smb2_cancel(struct ksmbd_work
*work
)
6495 struct ksmbd_conn
*conn
= work
->conn
;
6496 struct smb2_hdr
*hdr
= work
->request_buf
;
6497 struct smb2_hdr
*chdr
;
6498 struct ksmbd_work
*cancel_work
= NULL
;
6500 struct list_head
*command_list
;
6502 ksmbd_debug(SMB
, "smb2 cancel called on mid %llu, async flags 0x%x\n",
6503 hdr
->MessageId
, hdr
->Flags
);
6505 if (hdr
->Flags
& SMB2_FLAGS_ASYNC_COMMAND
) {
6506 command_list
= &conn
->async_requests
;
6508 spin_lock(&conn
->request_lock
);
6509 list_for_each_entry(cancel_work
, command_list
,
6510 async_request_entry
) {
6511 chdr
= cancel_work
->request_buf
;
6513 if (cancel_work
->async_id
!=
6514 le64_to_cpu(hdr
->Id
.AsyncId
))
6518 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6519 le64_to_cpu(hdr
->Id
.AsyncId
),
6520 le16_to_cpu(chdr
->Command
));
6524 spin_unlock(&conn
->request_lock
);
6526 command_list
= &conn
->requests
;
6528 spin_lock(&conn
->request_lock
);
6529 list_for_each_entry(cancel_work
, command_list
, request_entry
) {
6530 chdr
= cancel_work
->request_buf
;
6532 if (chdr
->MessageId
!= hdr
->MessageId
||
6533 cancel_work
== work
)
6537 "smb2 with mid %llu cancelled command = 0x%x\n",
6538 le64_to_cpu(hdr
->MessageId
),
6539 le16_to_cpu(chdr
->Command
));
6543 spin_unlock(&conn
->request_lock
);
6547 cancel_work
->state
= KSMBD_WORK_CANCELLED
;
6548 if (cancel_work
->cancel_fn
)
6549 cancel_work
->cancel_fn(cancel_work
->cancel_argv
);
6552 /* For SMB2_CANCEL command itself send no response*/
6553 work
->send_no_response
= 1;
6557 struct file_lock
*smb_flock_init(struct file
*f
)
6559 struct file_lock
*fl
;
6561 fl
= locks_alloc_lock();
6565 locks_init_lock(fl
);
6568 fl
->fl_pid
= current
->tgid
;
6570 fl
->fl_flags
= FL_POSIX
;
6572 fl
->fl_lmops
= NULL
;
6578 static int smb2_set_flock_flags(struct file_lock
*flock
, int flags
)
6582 /* Checking for wrong flag combination during lock request*/
6584 case SMB2_LOCKFLAG_SHARED
:
6585 ksmbd_debug(SMB
, "received shared request\n");
6587 flock
->fl_type
= F_RDLCK
;
6588 flock
->fl_flags
|= FL_SLEEP
;
6590 case SMB2_LOCKFLAG_EXCLUSIVE
:
6591 ksmbd_debug(SMB
, "received exclusive request\n");
6593 flock
->fl_type
= F_WRLCK
;
6594 flock
->fl_flags
|= FL_SLEEP
;
6596 case SMB2_LOCKFLAG_SHARED
| SMB2_LOCKFLAG_FAIL_IMMEDIATELY
:
6598 "received shared & fail immediately request\n");
6600 flock
->fl_type
= F_RDLCK
;
6602 case SMB2_LOCKFLAG_EXCLUSIVE
| SMB2_LOCKFLAG_FAIL_IMMEDIATELY
:
6604 "received exclusive & fail immediately request\n");
6606 flock
->fl_type
= F_WRLCK
;
6608 case SMB2_LOCKFLAG_UNLOCK
:
6609 ksmbd_debug(SMB
, "received unlock request\n");
6610 flock
->fl_type
= F_UNLCK
;
6618 static struct ksmbd_lock
*smb2_lock_init(struct file_lock
*flock
,
6619 unsigned int cmd
, int flags
,
6620 struct list_head
*lock_list
)
6622 struct ksmbd_lock
*lock
;
6624 lock
= kzalloc(sizeof(struct ksmbd_lock
), GFP_KERNEL
);
6630 lock
->start
= flock
->fl_start
;
6631 lock
->end
= flock
->fl_end
;
6632 lock
->flags
= flags
;
6633 if (lock
->start
== lock
->end
)
6635 INIT_LIST_HEAD(&lock
->clist
);
6636 INIT_LIST_HEAD(&lock
->flist
);
6637 INIT_LIST_HEAD(&lock
->llist
);
6638 list_add_tail(&lock
->llist
, lock_list
);
6643 static void smb2_remove_blocked_lock(void **argv
)
6645 struct file_lock
*flock
= (struct file_lock
*)argv
[0];
6647 ksmbd_vfs_posix_lock_unblock(flock
);
6648 wake_up(&flock
->fl_wait
);
6651 static inline bool lock_defer_pending(struct file_lock
*fl
)
6653 /* check pending lock waiters */
6654 return waitqueue_active(&fl
->fl_wait
);
6658 * smb2_lock() - handler for smb2 file lock command
6659 * @work: smb work containing lock command buffer
6661 * Return: 0 on success, otherwise error
6663 int smb2_lock(struct ksmbd_work
*work
)
6665 struct smb2_lock_req
*req
= work
->request_buf
;
6666 struct smb2_lock_rsp
*rsp
= work
->response_buf
;
6667 struct smb2_lock_element
*lock_ele
;
6668 struct ksmbd_file
*fp
= NULL
;
6669 struct file_lock
*flock
= NULL
;
6670 struct file
*filp
= NULL
;
6674 int err
= -EIO
, i
, rc
= 0;
6675 u64 lock_start
, lock_length
;
6676 struct ksmbd_lock
*smb_lock
= NULL
, *cmp_lock
, *tmp
, *tmp2
;
6677 struct ksmbd_conn
*conn
;
6679 LIST_HEAD(lock_list
);
6680 LIST_HEAD(rollback_list
);
6683 ksmbd_debug(SMB
, "Received lock request\n");
6684 fp
= ksmbd_lookup_fd_slow(work
,
6685 le64_to_cpu(req
->VolatileFileId
),
6686 le64_to_cpu(req
->PersistentFileId
));
6688 ksmbd_debug(SMB
, "Invalid file id for lock : %llu\n",
6689 le64_to_cpu(req
->VolatileFileId
));
6695 lock_count
= le16_to_cpu(req
->LockCount
);
6696 lock_ele
= req
->locks
;
6698 ksmbd_debug(SMB
, "lock count is %d\n", lock_count
);
6704 for (i
= 0; i
< lock_count
; i
++) {
6705 flags
= le32_to_cpu(lock_ele
[i
].Flags
);
6707 flock
= smb_flock_init(filp
);
6711 cmd
= smb2_set_flock_flags(flock
, flags
);
6713 lock_start
= le64_to_cpu(lock_ele
[i
].Offset
);
6714 lock_length
= le64_to_cpu(lock_ele
[i
].Length
);
6715 if (lock_start
> U64_MAX
- lock_length
) {
6716 pr_err("Invalid lock range requested\n");
6717 rsp
->hdr
.Status
= STATUS_INVALID_LOCK_RANGE
;
6721 if (lock_start
> OFFSET_MAX
)
6722 flock
->fl_start
= OFFSET_MAX
;
6724 flock
->fl_start
= lock_start
;
6726 lock_length
= le64_to_cpu(lock_ele
[i
].Length
);
6727 if (lock_length
> OFFSET_MAX
- flock
->fl_start
)
6728 lock_length
= OFFSET_MAX
- flock
->fl_start
;
6730 flock
->fl_end
= flock
->fl_start
+ lock_length
;
6732 if (flock
->fl_end
< flock
->fl_start
) {
6734 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6735 flock
->fl_end
, flock
->fl_start
);
6736 rsp
->hdr
.Status
= STATUS_INVALID_LOCK_RANGE
;
6740 /* Check conflict locks in one request */
6741 list_for_each_entry(cmp_lock
, &lock_list
, llist
) {
6742 if (cmp_lock
->fl
->fl_start
<= flock
->fl_start
&&
6743 cmp_lock
->fl
->fl_end
>= flock
->fl_end
) {
6744 if (cmp_lock
->fl
->fl_type
!= F_UNLCK
&&
6745 flock
->fl_type
!= F_UNLCK
) {
6746 pr_err("conflict two locks in one request\n");
6753 smb_lock
= smb2_lock_init(flock
, cmd
, flags
, &lock_list
);
6760 list_for_each_entry_safe(smb_lock
, tmp
, &lock_list
, llist
) {
6761 if (smb_lock
->cmd
< 0) {
6766 if (!(smb_lock
->flags
& SMB2_LOCKFLAG_MASK
)) {
6771 if ((prior_lock
& (SMB2_LOCKFLAG_EXCLUSIVE
| SMB2_LOCKFLAG_SHARED
) &&
6772 smb_lock
->flags
& SMB2_LOCKFLAG_UNLOCK
) ||
6773 (prior_lock
== SMB2_LOCKFLAG_UNLOCK
&&
6774 !(smb_lock
->flags
& SMB2_LOCKFLAG_UNLOCK
))) {
6779 prior_lock
= smb_lock
->flags
;
6781 if (!(smb_lock
->flags
& SMB2_LOCKFLAG_UNLOCK
) &&
6782 !(smb_lock
->flags
& SMB2_LOCKFLAG_FAIL_IMMEDIATELY
))
6786 /* check locks in connection list */
6787 read_lock(&conn_list_lock
);
6788 list_for_each_entry(conn
, &conn_list
, conns_list
) {
6789 spin_lock(&conn
->llist_lock
);
6790 list_for_each_entry_safe(cmp_lock
, tmp2
, &conn
->lock_list
, clist
) {
6791 if (file_inode(cmp_lock
->fl
->fl_file
) !=
6792 file_inode(smb_lock
->fl
->fl_file
))
6795 if (smb_lock
->fl
->fl_type
== F_UNLCK
) {
6796 if (cmp_lock
->fl
->fl_file
== smb_lock
->fl
->fl_file
&&
6797 cmp_lock
->start
== smb_lock
->start
&&
6798 cmp_lock
->end
== smb_lock
->end
&&
6799 !lock_defer_pending(cmp_lock
->fl
)) {
6801 list_del(&cmp_lock
->flist
);
6802 list_del(&cmp_lock
->clist
);
6803 spin_unlock(&conn
->llist_lock
);
6804 read_unlock(&conn_list_lock
);
6806 locks_free_lock(cmp_lock
->fl
);
6813 if (cmp_lock
->fl
->fl_file
== smb_lock
->fl
->fl_file
) {
6814 if (smb_lock
->flags
& SMB2_LOCKFLAG_SHARED
)
6817 if (cmp_lock
->flags
& SMB2_LOCKFLAG_SHARED
)
6821 /* check zero byte lock range */
6822 if (cmp_lock
->zero_len
&& !smb_lock
->zero_len
&&
6823 cmp_lock
->start
> smb_lock
->start
&&
6824 cmp_lock
->start
< smb_lock
->end
) {
6825 spin_unlock(&conn
->llist_lock
);
6826 read_unlock(&conn_list_lock
);
6827 pr_err("previous lock conflict with zero byte lock range\n");
6831 if (smb_lock
->zero_len
&& !cmp_lock
->zero_len
&&
6832 smb_lock
->start
> cmp_lock
->start
&&
6833 smb_lock
->start
< cmp_lock
->end
) {
6834 spin_unlock(&conn
->llist_lock
);
6835 read_unlock(&conn_list_lock
);
6836 pr_err("current lock conflict with zero byte lock range\n");
6840 if (((cmp_lock
->start
<= smb_lock
->start
&&
6841 cmp_lock
->end
> smb_lock
->start
) ||
6842 (cmp_lock
->start
< smb_lock
->end
&&
6843 cmp_lock
->end
>= smb_lock
->end
)) &&
6844 !cmp_lock
->zero_len
&& !smb_lock
->zero_len
) {
6845 spin_unlock(&conn
->llist_lock
);
6846 read_unlock(&conn_list_lock
);
6847 pr_err("Not allow lock operation on exclusive lock range\n");
6851 spin_unlock(&conn
->llist_lock
);
6853 read_unlock(&conn_list_lock
);
6855 if (smb_lock
->fl
->fl_type
== F_UNLCK
&& nolock
) {
6856 pr_err("Try to unlock nolocked range\n");
6857 rsp
->hdr
.Status
= STATUS_RANGE_NOT_LOCKED
;
6862 if (smb_lock
->zero_len
) {
6867 flock
= smb_lock
->fl
;
6868 list_del(&smb_lock
->llist
);
6870 rc
= vfs_lock_file(filp
, smb_lock
->cmd
, flock
, NULL
);
6872 if (flags
& SMB2_LOCKFLAG_UNLOCK
) {
6874 ksmbd_debug(SMB
, "File unlocked\n");
6875 } else if (rc
== -ENOENT
) {
6876 rsp
->hdr
.Status
= STATUS_NOT_LOCKED
;
6879 locks_free_lock(flock
);
6882 if (rc
== FILE_LOCK_DEFERRED
) {
6886 "would have to wait for getting lock\n");
6887 spin_lock(&work
->conn
->llist_lock
);
6888 list_add_tail(&smb_lock
->clist
,
6889 &work
->conn
->lock_list
);
6890 spin_unlock(&work
->conn
->llist_lock
);
6891 list_add(&smb_lock
->llist
, &rollback_list
);
6893 argv
= kmalloc(sizeof(void *), GFP_KERNEL
);
6900 rc
= setup_async_work(work
,
6901 smb2_remove_blocked_lock
,
6907 spin_lock(&fp
->f_lock
);
6908 list_add(&work
->fp_entry
, &fp
->blocked_works
);
6909 spin_unlock(&fp
->f_lock
);
6911 smb2_send_interim_resp(work
, STATUS_PENDING
);
6913 ksmbd_vfs_posix_lock_wait(flock
);
6915 if (work
->state
!= KSMBD_WORK_ACTIVE
) {
6916 list_del(&smb_lock
->llist
);
6917 spin_lock(&work
->conn
->llist_lock
);
6918 list_del(&smb_lock
->clist
);
6919 spin_unlock(&work
->conn
->llist_lock
);
6920 locks_free_lock(flock
);
6922 if (work
->state
== KSMBD_WORK_CANCELLED
) {
6923 spin_lock(&fp
->f_lock
);
6924 list_del(&work
->fp_entry
);
6925 spin_unlock(&fp
->f_lock
);
6929 smb2_send_interim_resp(work
,
6931 work
->send_no_response
= 1;
6934 init_smb2_rsp_hdr(work
);
6935 smb2_set_err_rsp(work
);
6937 STATUS_RANGE_NOT_LOCKED
;
6942 list_del(&smb_lock
->llist
);
6943 spin_lock(&work
->conn
->llist_lock
);
6944 list_del(&smb_lock
->clist
);
6945 spin_unlock(&work
->conn
->llist_lock
);
6947 spin_lock(&fp
->f_lock
);
6948 list_del(&work
->fp_entry
);
6949 spin_unlock(&fp
->f_lock
);
6952 spin_lock(&work
->conn
->llist_lock
);
6953 list_add_tail(&smb_lock
->clist
,
6954 &work
->conn
->lock_list
);
6955 list_add_tail(&smb_lock
->flist
,
6957 spin_unlock(&work
->conn
->llist_lock
);
6958 list_add(&smb_lock
->llist
, &rollback_list
);
6959 ksmbd_debug(SMB
, "successful in taking lock\n");
6966 if (atomic_read(&fp
->f_ci
->op_count
) > 1)
6967 smb_break_all_oplock(work
, fp
);
6969 rsp
->StructureSize
= cpu_to_le16(4);
6970 ksmbd_debug(SMB
, "successful in taking lock\n");
6971 rsp
->hdr
.Status
= STATUS_SUCCESS
;
6973 inc_rfc1001_len(rsp
, 4);
6974 ksmbd_fd_put(work
, fp
);
6978 list_for_each_entry_safe(smb_lock
, tmp
, &lock_list
, llist
) {
6979 locks_free_lock(smb_lock
->fl
);
6980 list_del(&smb_lock
->llist
);
6984 list_for_each_entry_safe(smb_lock
, tmp
, &rollback_list
, llist
) {
6985 struct file_lock
*rlock
= NULL
;
6987 rlock
= smb_flock_init(filp
);
6988 rlock
->fl_type
= F_UNLCK
;
6989 rlock
->fl_start
= smb_lock
->start
;
6990 rlock
->fl_end
= smb_lock
->end
;
6992 rc
= vfs_lock_file(filp
, 0, rlock
, NULL
);
6994 pr_err("rollback unlock fail : %d\n", rc
);
6996 list_del(&smb_lock
->llist
);
6997 spin_lock(&work
->conn
->llist_lock
);
6998 if (!list_empty(&smb_lock
->flist
))
6999 list_del(&smb_lock
->flist
);
7000 list_del(&smb_lock
->clist
);
7001 spin_unlock(&work
->conn
->llist_lock
);
7003 locks_free_lock(smb_lock
->fl
);
7004 locks_free_lock(rlock
);
7008 ksmbd_debug(SMB
, "failed in taking lock(flags : %x), err : %d\n", flags
, err
);
7010 if (!rsp
->hdr
.Status
) {
7012 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7013 else if (err
== -ENOMEM
)
7014 rsp
->hdr
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
7015 else if (err
== -ENOENT
)
7016 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
7018 rsp
->hdr
.Status
= STATUS_LOCK_NOT_GRANTED
;
7021 smb2_set_err_rsp(work
);
7022 ksmbd_fd_put(work
, fp
);
7026 static int fsctl_copychunk(struct ksmbd_work
*work
, struct smb2_ioctl_req
*req
,
7027 struct smb2_ioctl_rsp
*rsp
)
7029 struct copychunk_ioctl_req
*ci_req
;
7030 struct copychunk_ioctl_rsp
*ci_rsp
;
7031 struct ksmbd_file
*src_fp
= NULL
, *dst_fp
= NULL
;
7032 struct srv_copychunk
*chunks
;
7033 unsigned int i
, chunk_count
, chunk_count_written
= 0;
7034 unsigned int chunk_size_written
= 0;
7035 loff_t total_size_written
= 0;
7038 cnt_code
= le32_to_cpu(req
->CntCode
);
7039 ci_req
= (struct copychunk_ioctl_req
*)&req
->Buffer
[0];
7040 ci_rsp
= (struct copychunk_ioctl_rsp
*)&rsp
->Buffer
[0];
7042 rsp
->VolatileFileId
= req
->VolatileFileId
;
7043 rsp
->PersistentFileId
= req
->PersistentFileId
;
7044 ci_rsp
->ChunksWritten
=
7045 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
7046 ci_rsp
->ChunkBytesWritten
=
7047 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
7048 ci_rsp
->TotalBytesWritten
=
7049 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
7051 chunks
= (struct srv_copychunk
*)&ci_req
->Chunks
[0];
7052 chunk_count
= le32_to_cpu(ci_req
->ChunkCount
);
7053 total_size_written
= 0;
7055 /* verify the SRV_COPYCHUNK_COPY packet */
7056 if (chunk_count
> ksmbd_server_side_copy_max_chunk_count() ||
7057 le32_to_cpu(req
->InputCount
) <
7058 offsetof(struct copychunk_ioctl_req
, Chunks
) +
7059 chunk_count
* sizeof(struct srv_copychunk
)) {
7060 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7064 for (i
= 0; i
< chunk_count
; i
++) {
7065 if (le32_to_cpu(chunks
[i
].Length
) == 0 ||
7066 le32_to_cpu(chunks
[i
].Length
) > ksmbd_server_side_copy_max_chunk_size())
7068 total_size_written
+= le32_to_cpu(chunks
[i
].Length
);
7071 if (i
< chunk_count
||
7072 total_size_written
> ksmbd_server_side_copy_max_total_size()) {
7073 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7077 src_fp
= ksmbd_lookup_foreign_fd(work
,
7078 le64_to_cpu(ci_req
->ResumeKey
[0]));
7079 dst_fp
= ksmbd_lookup_fd_slow(work
,
7080 le64_to_cpu(req
->VolatileFileId
),
7081 le64_to_cpu(req
->PersistentFileId
));
7084 src_fp
->persistent_id
!= le64_to_cpu(ci_req
->ResumeKey
[1])) {
7085 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
7090 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
7095 * FILE_READ_DATA should only be included in
7096 * the FSCTL_COPYCHUNK case
7098 if (cnt_code
== FSCTL_COPYCHUNK
&&
7099 !(dst_fp
->daccess
& (FILE_READ_DATA_LE
| FILE_GENERIC_READ_LE
))) {
7100 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
7104 ret
= ksmbd_vfs_copy_file_ranges(work
, src_fp
, dst_fp
,
7105 chunks
, chunk_count
,
7106 &chunk_count_written
,
7107 &chunk_size_written
,
7108 &total_size_written
);
7111 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
7113 rsp
->hdr
.Status
= STATUS_FILE_LOCK_CONFLICT
;
7114 else if (ret
== -EBADF
)
7115 rsp
->hdr
.Status
= STATUS_INVALID_HANDLE
;
7116 else if (ret
== -EFBIG
|| ret
== -ENOSPC
)
7117 rsp
->hdr
.Status
= STATUS_DISK_FULL
;
7118 else if (ret
== -EINVAL
)
7119 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7120 else if (ret
== -EISDIR
)
7121 rsp
->hdr
.Status
= STATUS_FILE_IS_A_DIRECTORY
;
7122 else if (ret
== -E2BIG
)
7123 rsp
->hdr
.Status
= STATUS_INVALID_VIEW_SIZE
;
7125 rsp
->hdr
.Status
= STATUS_UNEXPECTED_IO_ERROR
;
7128 ci_rsp
->ChunksWritten
= cpu_to_le32(chunk_count_written
);
7129 ci_rsp
->ChunkBytesWritten
= cpu_to_le32(chunk_size_written
);
7130 ci_rsp
->TotalBytesWritten
= cpu_to_le32(total_size_written
);
7132 ksmbd_fd_put(work
, src_fp
);
7133 ksmbd_fd_put(work
, dst_fp
);
7137 static __be32
idev_ipv4_address(struct in_device
*idev
)
7141 struct in_ifaddr
*ifa
;
7144 in_dev_for_each_ifa_rcu(ifa
, idev
) {
7145 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
7148 addr
= ifa
->ifa_address
;
7155 static int fsctl_query_iface_info_ioctl(struct ksmbd_conn
*conn
,
7156 struct smb2_ioctl_req
*req
,
7157 struct smb2_ioctl_rsp
*rsp
)
7159 struct network_interface_info_ioctl_rsp
*nii_rsp
= NULL
;
7161 struct net_device
*netdev
;
7162 struct sockaddr_storage_rsp
*sockaddr_storage
;
7164 unsigned long long speed
;
7165 struct sockaddr_in6
*csin6
= (struct sockaddr_in6
*)&conn
->peer_addr
;
7168 for_each_netdev(&init_net
, netdev
) {
7169 if (netdev
->type
== ARPHRD_LOOPBACK
)
7172 flags
= dev_get_flags(netdev
);
7173 if (!(flags
& IFF_RUNNING
))
7176 nii_rsp
= (struct network_interface_info_ioctl_rsp
*)
7177 &rsp
->Buffer
[nbytes
];
7178 nii_rsp
->IfIndex
= cpu_to_le32(netdev
->ifindex
);
7180 nii_rsp
->Capability
= 0;
7181 if (ksmbd_rdma_capable_netdev(netdev
))
7182 nii_rsp
->Capability
|= cpu_to_le32(RDMA_CAPABLE
);
7184 nii_rsp
->Next
= cpu_to_le32(152);
7185 nii_rsp
->Reserved
= 0;
7187 if (netdev
->ethtool_ops
->get_link_ksettings
) {
7188 struct ethtool_link_ksettings cmd
;
7190 netdev
->ethtool_ops
->get_link_ksettings(netdev
, &cmd
);
7191 speed
= cmd
.base
.speed
;
7193 ksmbd_debug(SMB
, "%s %s\n", netdev
->name
,
7194 "speed is unknown, defaulting to 1Gb/sec");
7199 nii_rsp
->LinkSpeed
= cpu_to_le64(speed
);
7201 sockaddr_storage
= (struct sockaddr_storage_rsp
*)
7202 nii_rsp
->SockAddr_Storage
;
7203 memset(sockaddr_storage
, 0, 128);
7205 if (conn
->peer_addr
.ss_family
== PF_INET
||
7206 ipv6_addr_v4mapped(&csin6
->sin6_addr
)) {
7207 struct in_device
*idev
;
7209 sockaddr_storage
->Family
= cpu_to_le16(INTERNETWORK
);
7210 sockaddr_storage
->addr4
.Port
= 0;
7212 idev
= __in_dev_get_rtnl(netdev
);
7215 sockaddr_storage
->addr4
.IPv4address
=
7216 idev_ipv4_address(idev
);
7218 struct inet6_dev
*idev6
;
7219 struct inet6_ifaddr
*ifa
;
7220 __u8
*ipv6_addr
= sockaddr_storage
->addr6
.IPv6address
;
7222 sockaddr_storage
->Family
= cpu_to_le16(INTERNETWORKV6
);
7223 sockaddr_storage
->addr6
.Port
= 0;
7224 sockaddr_storage
->addr6
.FlowInfo
= 0;
7226 idev6
= __in6_dev_get(netdev
);
7230 list_for_each_entry(ifa
, &idev6
->addr_list
, if_list
) {
7231 if (ifa
->flags
& (IFA_F_TENTATIVE
|
7234 memcpy(ipv6_addr
, ifa
->addr
.s6_addr
, 16);
7237 sockaddr_storage
->addr6
.ScopeId
= 0;
7240 nbytes
+= sizeof(struct network_interface_info_ioctl_rsp
);
7244 /* zero if this is last one */
7249 rsp
->hdr
.Status
= STATUS_BUFFER_TOO_SMALL
;
7253 rsp
->PersistentFileId
= cpu_to_le64(SMB2_NO_FID
);
7254 rsp
->VolatileFileId
= cpu_to_le64(SMB2_NO_FID
);
7258 static int fsctl_validate_negotiate_info(struct ksmbd_conn
*conn
,
7259 struct validate_negotiate_info_req
*neg_req
,
7260 struct validate_negotiate_info_rsp
*neg_rsp
)
7265 dialect
= ksmbd_lookup_dialect_by_id(neg_req
->Dialects
,
7266 neg_req
->DialectCount
);
7267 if (dialect
== BAD_PROT_ID
|| dialect
!= conn
->dialect
) {
7272 if (strncmp(neg_req
->Guid
, conn
->ClientGUID
, SMB2_CLIENT_GUID_SIZE
)) {
7277 if (le16_to_cpu(neg_req
->SecurityMode
) != conn
->cli_sec_mode
) {
7282 if (le32_to_cpu(neg_req
->Capabilities
) != conn
->cli_cap
) {
7287 neg_rsp
->Capabilities
= cpu_to_le32(conn
->vals
->capabilities
);
7288 memset(neg_rsp
->Guid
, 0, SMB2_CLIENT_GUID_SIZE
);
7289 neg_rsp
->SecurityMode
= cpu_to_le16(conn
->srv_sec_mode
);
7290 neg_rsp
->Dialect
= cpu_to_le16(conn
->dialect
);
7295 static int fsctl_query_allocated_ranges(struct ksmbd_work
*work
, u64 id
,
7296 struct file_allocated_range_buffer
*qar_req
,
7297 struct file_allocated_range_buffer
*qar_rsp
,
7298 int in_count
, int *out_count
)
7300 struct ksmbd_file
*fp
;
7301 loff_t start
, length
;
7308 fp
= ksmbd_lookup_fd_fast(work
, id
);
7312 start
= le64_to_cpu(qar_req
->file_offset
);
7313 length
= le64_to_cpu(qar_req
->length
);
7315 ret
= ksmbd_vfs_fqar_lseek(fp
, start
, length
,
7316 qar_rsp
, in_count
, out_count
);
7317 if (ret
&& ret
!= -E2BIG
)
7320 ksmbd_fd_put(work
, fp
);
7324 static int fsctl_pipe_transceive(struct ksmbd_work
*work
, u64 id
,
7325 int out_buf_len
, struct smb2_ioctl_req
*req
,
7326 struct smb2_ioctl_rsp
*rsp
)
7328 struct ksmbd_rpc_command
*rpc_resp
;
7329 char *data_buf
= (char *)&req
->Buffer
[0];
7332 rpc_resp
= ksmbd_rpc_ioctl(work
->sess
, id
, data_buf
,
7333 le32_to_cpu(req
->InputCount
));
7335 if (rpc_resp
->flags
== KSMBD_RPC_SOME_NOT_MAPPED
) {
7337 * set STATUS_SOME_NOT_MAPPED response
7338 * for unknown domain sid.
7340 rsp
->hdr
.Status
= STATUS_SOME_NOT_MAPPED
;
7341 } else if (rpc_resp
->flags
== KSMBD_RPC_ENOTIMPLEMENTED
) {
7342 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
7344 } else if (rpc_resp
->flags
!= KSMBD_RPC_OK
) {
7345 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7349 nbytes
= rpc_resp
->payload_sz
;
7350 if (rpc_resp
->payload_sz
> out_buf_len
) {
7351 rsp
->hdr
.Status
= STATUS_BUFFER_OVERFLOW
;
7352 nbytes
= out_buf_len
;
7355 if (!rpc_resp
->payload_sz
) {
7357 STATUS_UNEXPECTED_IO_ERROR
;
7361 memcpy((char *)rsp
->Buffer
, rpc_resp
->payload
, nbytes
);
7368 static inline int fsctl_set_sparse(struct ksmbd_work
*work
, u64 id
,
7369 struct file_sparse
*sparse
)
7371 struct ksmbd_file
*fp
;
7372 struct user_namespace
*user_ns
;
7376 fp
= ksmbd_lookup_fd_fast(work
, id
);
7379 user_ns
= file_mnt_user_ns(fp
->filp
);
7381 old_fattr
= fp
->f_ci
->m_fattr
;
7382 if (sparse
->SetSparse
)
7383 fp
->f_ci
->m_fattr
|= ATTR_SPARSE_FILE_LE
;
7385 fp
->f_ci
->m_fattr
&= ~ATTR_SPARSE_FILE_LE
;
7387 if (fp
->f_ci
->m_fattr
!= old_fattr
&&
7388 test_share_config_flag(work
->tcon
->share_conf
,
7389 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS
)) {
7390 struct xattr_dos_attrib da
;
7392 ret
= ksmbd_vfs_get_dos_attrib_xattr(user_ns
,
7393 fp
->filp
->f_path
.dentry
, &da
);
7397 da
.attr
= le32_to_cpu(fp
->f_ci
->m_fattr
);
7398 ret
= ksmbd_vfs_set_dos_attrib_xattr(user_ns
,
7399 fp
->filp
->f_path
.dentry
, &da
);
7401 fp
->f_ci
->m_fattr
= old_fattr
;
7405 ksmbd_fd_put(work
, fp
);
7409 static int fsctl_request_resume_key(struct ksmbd_work
*work
,
7410 struct smb2_ioctl_req
*req
,
7411 struct resume_key_ioctl_rsp
*key_rsp
)
7413 struct ksmbd_file
*fp
;
7415 fp
= ksmbd_lookup_fd_slow(work
,
7416 le64_to_cpu(req
->VolatileFileId
),
7417 le64_to_cpu(req
->PersistentFileId
));
7421 memset(key_rsp
, 0, sizeof(*key_rsp
));
7422 key_rsp
->ResumeKey
[0] = req
->VolatileFileId
;
7423 key_rsp
->ResumeKey
[1] = req
->PersistentFileId
;
7424 ksmbd_fd_put(work
, fp
);
7430 * smb2_ioctl() - handler for smb2 ioctl command
7431 * @work: smb work containing ioctl command buffer
7433 * Return: 0 on success, otherwise error
7435 int smb2_ioctl(struct ksmbd_work
*work
)
7437 struct smb2_ioctl_req
*req
;
7438 struct smb2_ioctl_rsp
*rsp
, *rsp_org
;
7439 int cnt_code
, nbytes
= 0;
7441 u64 id
= KSMBD_NO_FID
;
7442 struct ksmbd_conn
*conn
= work
->conn
;
7445 rsp_org
= work
->response_buf
;
7446 if (work
->next_smb2_rcv_hdr_off
) {
7447 req
= ksmbd_req_buf_next(work
);
7448 rsp
= ksmbd_resp_buf_next(work
);
7449 if (!has_file_id(le64_to_cpu(req
->VolatileFileId
))) {
7450 ksmbd_debug(SMB
, "Compound request set FID = %llu\n",
7451 work
->compound_fid
);
7452 id
= work
->compound_fid
;
7455 req
= work
->request_buf
;
7456 rsp
= work
->response_buf
;
7459 if (!has_file_id(id
))
7460 id
= le64_to_cpu(req
->VolatileFileId
);
7462 if (req
->Flags
!= cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL
)) {
7463 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
7467 cnt_code
= le32_to_cpu(req
->CntCode
);
7468 out_buf_len
= le32_to_cpu(req
->MaxOutputResponse
);
7469 out_buf_len
= min(KSMBD_IPC_MAX_PAYLOAD
, out_buf_len
);
7472 case FSCTL_DFS_GET_REFERRALS
:
7473 case FSCTL_DFS_GET_REFERRALS_EX
:
7474 /* Not support DFS yet */
7475 rsp
->hdr
.Status
= STATUS_FS_DRIVER_REQUIRED
;
7477 case FSCTL_CREATE_OR_GET_OBJECT_ID
:
7479 struct file_object_buf_type1_ioctl_rsp
*obj_buf
;
7481 nbytes
= sizeof(struct file_object_buf_type1_ioctl_rsp
);
7482 obj_buf
= (struct file_object_buf_type1_ioctl_rsp
*)
7486 * TODO: This is dummy implementation to pass smbtorture
7487 * Need to check correct response later
7489 memset(obj_buf
->ObjectId
, 0x0, 16);
7490 memset(obj_buf
->BirthVolumeId
, 0x0, 16);
7491 memset(obj_buf
->BirthObjectId
, 0x0, 16);
7492 memset(obj_buf
->DomainId
, 0x0, 16);
7496 case FSCTL_PIPE_TRANSCEIVE
:
7497 nbytes
= fsctl_pipe_transceive(work
, id
, out_buf_len
, req
, rsp
);
7499 case FSCTL_VALIDATE_NEGOTIATE_INFO
:
7500 if (conn
->dialect
< SMB30_PROT_ID
) {
7505 ret
= fsctl_validate_negotiate_info(conn
,
7506 (struct validate_negotiate_info_req
*)&req
->Buffer
[0],
7507 (struct validate_negotiate_info_rsp
*)&rsp
->Buffer
[0]);
7511 nbytes
= sizeof(struct validate_negotiate_info_rsp
);
7512 rsp
->PersistentFileId
= cpu_to_le64(SMB2_NO_FID
);
7513 rsp
->VolatileFileId
= cpu_to_le64(SMB2_NO_FID
);
7515 case FSCTL_QUERY_NETWORK_INTERFACE_INFO
:
7516 nbytes
= fsctl_query_iface_info_ioctl(conn
, req
, rsp
);
7520 case FSCTL_REQUEST_RESUME_KEY
:
7521 if (out_buf_len
< sizeof(struct resume_key_ioctl_rsp
)) {
7526 ret
= fsctl_request_resume_key(work
, req
,
7527 (struct resume_key_ioctl_rsp
*)&rsp
->Buffer
[0]);
7530 rsp
->PersistentFileId
= req
->PersistentFileId
;
7531 rsp
->VolatileFileId
= req
->VolatileFileId
;
7532 nbytes
= sizeof(struct resume_key_ioctl_rsp
);
7534 case FSCTL_COPYCHUNK
:
7535 case FSCTL_COPYCHUNK_WRITE
:
7536 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
7538 "User does not have write permission\n");
7543 if (out_buf_len
< sizeof(struct copychunk_ioctl_rsp
)) {
7548 nbytes
= sizeof(struct copychunk_ioctl_rsp
);
7549 fsctl_copychunk(work
, req
, rsp
);
7551 case FSCTL_SET_SPARSE
:
7552 ret
= fsctl_set_sparse(work
, id
,
7553 (struct file_sparse
*)&req
->Buffer
[0]);
7557 case FSCTL_SET_ZERO_DATA
:
7559 struct file_zero_data_information
*zero_data
;
7560 struct ksmbd_file
*fp
;
7563 if (!test_tree_conn_flag(work
->tcon
, KSMBD_TREE_CONN_FLAG_WRITABLE
)) {
7565 "User does not have write permission\n");
7571 (struct file_zero_data_information
*)&req
->Buffer
[0];
7573 fp
= ksmbd_lookup_fd_fast(work
, id
);
7579 off
= le64_to_cpu(zero_data
->FileOffset
);
7580 len
= le64_to_cpu(zero_data
->BeyondFinalZero
) - off
;
7582 ret
= ksmbd_vfs_zero_data(work
, fp
, off
, len
);
7583 ksmbd_fd_put(work
, fp
);
7588 case FSCTL_QUERY_ALLOCATED_RANGES
:
7589 ret
= fsctl_query_allocated_ranges(work
, id
,
7590 (struct file_allocated_range_buffer
*)&req
->Buffer
[0],
7591 (struct file_allocated_range_buffer
*)&rsp
->Buffer
[0],
7593 sizeof(struct file_allocated_range_buffer
), &nbytes
);
7594 if (ret
== -E2BIG
) {
7595 rsp
->hdr
.Status
= STATUS_BUFFER_OVERFLOW
;
7596 } else if (ret
< 0) {
7601 nbytes
*= sizeof(struct file_allocated_range_buffer
);
7603 case FSCTL_GET_REPARSE_POINT
:
7605 struct reparse_data_buffer
*reparse_ptr
;
7606 struct ksmbd_file
*fp
;
7608 reparse_ptr
= (struct reparse_data_buffer
*)&rsp
->Buffer
[0];
7609 fp
= ksmbd_lookup_fd_fast(work
, id
);
7611 pr_err("not found fp!!\n");
7616 reparse_ptr
->ReparseTag
=
7617 smb2_get_reparse_tag_special_file(file_inode(fp
->filp
)->i_mode
);
7618 reparse_ptr
->ReparseDataLength
= 0;
7619 ksmbd_fd_put(work
, fp
);
7620 nbytes
= sizeof(struct reparse_data_buffer
);
7623 case FSCTL_DUPLICATE_EXTENTS_TO_FILE
:
7625 struct ksmbd_file
*fp_in
, *fp_out
= NULL
;
7626 struct duplicate_extents_to_file
*dup_ext
;
7627 loff_t src_off
, dst_off
, length
, cloned
;
7629 dup_ext
= (struct duplicate_extents_to_file
*)&req
->Buffer
[0];
7631 fp_in
= ksmbd_lookup_fd_slow(work
, dup_ext
->VolatileFileHandle
,
7632 dup_ext
->PersistentFileHandle
);
7634 pr_err("not found file handle in duplicate extent to file\n");
7639 fp_out
= ksmbd_lookup_fd_fast(work
, id
);
7641 pr_err("not found fp\n");
7646 src_off
= le64_to_cpu(dup_ext
->SourceFileOffset
);
7647 dst_off
= le64_to_cpu(dup_ext
->TargetFileOffset
);
7648 length
= le64_to_cpu(dup_ext
->ByteCount
);
7649 cloned
= vfs_clone_file_range(fp_in
->filp
, src_off
, fp_out
->filp
,
7650 dst_off
, length
, 0);
7651 if (cloned
== -EXDEV
|| cloned
== -EOPNOTSUPP
) {
7654 } else if (cloned
!= length
) {
7655 cloned
= vfs_copy_file_range(fp_in
->filp
, src_off
,
7656 fp_out
->filp
, dst_off
, length
, 0);
7657 if (cloned
!= length
) {
7666 ksmbd_fd_put(work
, fp_in
);
7667 ksmbd_fd_put(work
, fp_out
);
7673 ksmbd_debug(SMB
, "not implemented yet ioctl command 0x%x\n",
7679 rsp
->CntCode
= cpu_to_le32(cnt_code
);
7680 rsp
->InputCount
= cpu_to_le32(0);
7681 rsp
->InputOffset
= cpu_to_le32(112);
7682 rsp
->OutputOffset
= cpu_to_le32(112);
7683 rsp
->OutputCount
= cpu_to_le32(nbytes
);
7684 rsp
->StructureSize
= cpu_to_le16(49);
7685 rsp
->Reserved
= cpu_to_le16(0);
7686 rsp
->Flags
= cpu_to_le32(0);
7687 rsp
->Reserved2
= cpu_to_le32(0);
7688 inc_rfc1001_len(rsp_org
, 48 + nbytes
);
7694 rsp
->hdr
.Status
= STATUS_ACCESS_DENIED
;
7695 else if (ret
== -ENOENT
)
7696 rsp
->hdr
.Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
7697 else if (ret
== -EOPNOTSUPP
)
7698 rsp
->hdr
.Status
= STATUS_NOT_SUPPORTED
;
7699 else if (ret
< 0 || rsp
->hdr
.Status
== 0)
7700 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
7701 smb2_set_err_rsp(work
);
7706 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7707 * @work: smb work containing oplock break command buffer
7711 static void smb20_oplock_break_ack(struct ksmbd_work
*work
)
7713 struct smb2_oplock_break
*req
= work
->request_buf
;
7714 struct smb2_oplock_break
*rsp
= work
->response_buf
;
7715 struct ksmbd_file
*fp
;
7716 struct oplock_info
*opinfo
= NULL
;
7719 u64 volatile_id
, persistent_id
;
7720 char req_oplevel
= 0, rsp_oplevel
= 0;
7721 unsigned int oplock_change_type
;
7723 volatile_id
= le64_to_cpu(req
->VolatileFid
);
7724 persistent_id
= le64_to_cpu(req
->PersistentFid
);
7725 req_oplevel
= req
->OplockLevel
;
7726 ksmbd_debug(OPLOCK
, "v_id %llu, p_id %llu request oplock level %d\n",
7727 volatile_id
, persistent_id
, req_oplevel
);
7729 fp
= ksmbd_lookup_fd_slow(work
, volatile_id
, persistent_id
);
7731 rsp
->hdr
.Status
= STATUS_FILE_CLOSED
;
7732 smb2_set_err_rsp(work
);
7736 opinfo
= opinfo_get(fp
);
7738 pr_err("unexpected null oplock_info\n");
7739 rsp
->hdr
.Status
= STATUS_INVALID_OPLOCK_PROTOCOL
;
7740 smb2_set_err_rsp(work
);
7741 ksmbd_fd_put(work
, fp
);
7745 if (opinfo
->level
== SMB2_OPLOCK_LEVEL_NONE
) {
7746 rsp
->hdr
.Status
= STATUS_INVALID_OPLOCK_PROTOCOL
;
7750 if (opinfo
->op_state
== OPLOCK_STATE_NONE
) {
7751 ksmbd_debug(SMB
, "unexpected oplock state 0x%x\n", opinfo
->op_state
);
7752 rsp
->hdr
.Status
= STATUS_UNSUCCESSFUL
;
7756 if ((opinfo
->level
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
||
7757 opinfo
->level
== SMB2_OPLOCK_LEVEL_BATCH
) &&
7758 (req_oplevel
!= SMB2_OPLOCK_LEVEL_II
&&
7759 req_oplevel
!= SMB2_OPLOCK_LEVEL_NONE
)) {
7760 err
= STATUS_INVALID_OPLOCK_PROTOCOL
;
7761 oplock_change_type
= OPLOCK_WRITE_TO_NONE
;
7762 } else if (opinfo
->level
== SMB2_OPLOCK_LEVEL_II
&&
7763 req_oplevel
!= SMB2_OPLOCK_LEVEL_NONE
) {
7764 err
= STATUS_INVALID_OPLOCK_PROTOCOL
;
7765 oplock_change_type
= OPLOCK_READ_TO_NONE
;
7766 } else if (req_oplevel
== SMB2_OPLOCK_LEVEL_II
||
7767 req_oplevel
== SMB2_OPLOCK_LEVEL_NONE
) {
7768 err
= STATUS_INVALID_DEVICE_STATE
;
7769 if ((opinfo
->level
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
||
7770 opinfo
->level
== SMB2_OPLOCK_LEVEL_BATCH
) &&
7771 req_oplevel
== SMB2_OPLOCK_LEVEL_II
) {
7772 oplock_change_type
= OPLOCK_WRITE_TO_READ
;
7773 } else if ((opinfo
->level
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
||
7774 opinfo
->level
== SMB2_OPLOCK_LEVEL_BATCH
) &&
7775 req_oplevel
== SMB2_OPLOCK_LEVEL_NONE
) {
7776 oplock_change_type
= OPLOCK_WRITE_TO_NONE
;
7777 } else if (opinfo
->level
== SMB2_OPLOCK_LEVEL_II
&&
7778 req_oplevel
== SMB2_OPLOCK_LEVEL_NONE
) {
7779 oplock_change_type
= OPLOCK_READ_TO_NONE
;
7781 oplock_change_type
= 0;
7784 oplock_change_type
= 0;
7787 switch (oplock_change_type
) {
7788 case OPLOCK_WRITE_TO_READ
:
7789 ret
= opinfo_write_to_read(opinfo
);
7790 rsp_oplevel
= SMB2_OPLOCK_LEVEL_II
;
7792 case OPLOCK_WRITE_TO_NONE
:
7793 ret
= opinfo_write_to_none(opinfo
);
7794 rsp_oplevel
= SMB2_OPLOCK_LEVEL_NONE
;
7796 case OPLOCK_READ_TO_NONE
:
7797 ret
= opinfo_read_to_none(opinfo
);
7798 rsp_oplevel
= SMB2_OPLOCK_LEVEL_NONE
;
7801 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7802 opinfo
->level
, rsp_oplevel
);
7806 rsp
->hdr
.Status
= err
;
7811 ksmbd_fd_put(work
, fp
);
7812 opinfo
->op_state
= OPLOCK_STATE_NONE
;
7813 wake_up_interruptible_all(&opinfo
->oplock_q
);
7815 rsp
->StructureSize
= cpu_to_le16(24);
7816 rsp
->OplockLevel
= rsp_oplevel
;
7819 rsp
->VolatileFid
= cpu_to_le64(volatile_id
);
7820 rsp
->PersistentFid
= cpu_to_le64(persistent_id
);
7821 inc_rfc1001_len(rsp
, 24);
7825 opinfo
->op_state
= OPLOCK_STATE_NONE
;
7826 wake_up_interruptible_all(&opinfo
->oplock_q
);
7829 ksmbd_fd_put(work
, fp
);
7830 smb2_set_err_rsp(work
);
7833 static int check_lease_state(struct lease
*lease
, __le32 req_state
)
7835 if ((lease
->new_state
==
7836 (SMB2_LEASE_READ_CACHING_LE
| SMB2_LEASE_HANDLE_CACHING_LE
)) &&
7837 !(req_state
& SMB2_LEASE_WRITE_CACHING_LE
)) {
7838 lease
->new_state
= req_state
;
7842 if (lease
->new_state
== req_state
)
7849 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7850 * @work: smb work containing lease break command buffer
7854 static void smb21_lease_break_ack(struct ksmbd_work
*work
)
7856 struct ksmbd_conn
*conn
= work
->conn
;
7857 struct smb2_lease_ack
*req
= work
->request_buf
;
7858 struct smb2_lease_ack
*rsp
= work
->response_buf
;
7859 struct oplock_info
*opinfo
;
7862 unsigned int lease_change_type
;
7864 struct lease
*lease
;
7866 ksmbd_debug(OPLOCK
, "smb21 lease break, lease state(0x%x)\n",
7867 le32_to_cpu(req
->LeaseState
));
7868 opinfo
= lookup_lease_in_table(conn
, req
->LeaseKey
);
7870 ksmbd_debug(OPLOCK
, "file not opened\n");
7871 smb2_set_err_rsp(work
);
7872 rsp
->hdr
.Status
= STATUS_UNSUCCESSFUL
;
7875 lease
= opinfo
->o_lease
;
7877 if (opinfo
->op_state
== OPLOCK_STATE_NONE
) {
7878 pr_err("unexpected lease break state 0x%x\n",
7880 rsp
->hdr
.Status
= STATUS_UNSUCCESSFUL
;
7884 if (check_lease_state(lease
, req
->LeaseState
)) {
7885 rsp
->hdr
.Status
= STATUS_REQUEST_NOT_ACCEPTED
;
7887 "req lease state: 0x%x, expected state: 0x%x\n",
7888 req
->LeaseState
, lease
->new_state
);
7892 if (!atomic_read(&opinfo
->breaking_cnt
)) {
7893 rsp
->hdr
.Status
= STATUS_UNSUCCESSFUL
;
7897 /* check for bad lease state */
7898 if (req
->LeaseState
&
7899 (~(SMB2_LEASE_READ_CACHING_LE
| SMB2_LEASE_HANDLE_CACHING_LE
))) {
7900 err
= STATUS_INVALID_OPLOCK_PROTOCOL
;
7901 if (lease
->state
& SMB2_LEASE_WRITE_CACHING_LE
)
7902 lease_change_type
= OPLOCK_WRITE_TO_NONE
;
7904 lease_change_type
= OPLOCK_READ_TO_NONE
;
7905 ksmbd_debug(OPLOCK
, "handle bad lease state 0x%x -> 0x%x\n",
7906 le32_to_cpu(lease
->state
),
7907 le32_to_cpu(req
->LeaseState
));
7908 } else if (lease
->state
== SMB2_LEASE_READ_CACHING_LE
&&
7909 req
->LeaseState
!= SMB2_LEASE_NONE_LE
) {
7910 err
= STATUS_INVALID_OPLOCK_PROTOCOL
;
7911 lease_change_type
= OPLOCK_READ_TO_NONE
;
7912 ksmbd_debug(OPLOCK
, "handle bad lease state 0x%x -> 0x%x\n",
7913 le32_to_cpu(lease
->state
),
7914 le32_to_cpu(req
->LeaseState
));
7916 /* valid lease state changes */
7917 err
= STATUS_INVALID_DEVICE_STATE
;
7918 if (req
->LeaseState
== SMB2_LEASE_NONE_LE
) {
7919 if (lease
->state
& SMB2_LEASE_WRITE_CACHING_LE
)
7920 lease_change_type
= OPLOCK_WRITE_TO_NONE
;
7922 lease_change_type
= OPLOCK_READ_TO_NONE
;
7923 } else if (req
->LeaseState
& SMB2_LEASE_READ_CACHING_LE
) {
7924 if (lease
->state
& SMB2_LEASE_WRITE_CACHING_LE
)
7925 lease_change_type
= OPLOCK_WRITE_TO_READ
;
7927 lease_change_type
= OPLOCK_READ_HANDLE_TO_READ
;
7929 lease_change_type
= 0;
7933 switch (lease_change_type
) {
7934 case OPLOCK_WRITE_TO_READ
:
7935 ret
= opinfo_write_to_read(opinfo
);
7937 case OPLOCK_READ_HANDLE_TO_READ
:
7938 ret
= opinfo_read_handle_to_read(opinfo
);
7940 case OPLOCK_WRITE_TO_NONE
:
7941 ret
= opinfo_write_to_none(opinfo
);
7943 case OPLOCK_READ_TO_NONE
:
7944 ret
= opinfo_read_to_none(opinfo
);
7947 ksmbd_debug(OPLOCK
, "unknown lease change 0x%x -> 0x%x\n",
7948 le32_to_cpu(lease
->state
),
7949 le32_to_cpu(req
->LeaseState
));
7952 lease_state
= lease
->state
;
7953 opinfo
->op_state
= OPLOCK_STATE_NONE
;
7954 wake_up_interruptible_all(&opinfo
->oplock_q
);
7955 atomic_dec(&opinfo
->breaking_cnt
);
7956 wake_up_interruptible_all(&opinfo
->oplock_brk
);
7960 rsp
->hdr
.Status
= err
;
7964 rsp
->StructureSize
= cpu_to_le16(36);
7967 memcpy(rsp
->LeaseKey
, req
->LeaseKey
, 16);
7968 rsp
->LeaseState
= lease_state
;
7969 rsp
->LeaseDuration
= 0;
7970 inc_rfc1001_len(rsp
, 36);
7974 opinfo
->op_state
= OPLOCK_STATE_NONE
;
7975 wake_up_interruptible_all(&opinfo
->oplock_q
);
7976 atomic_dec(&opinfo
->breaking_cnt
);
7977 wake_up_interruptible_all(&opinfo
->oplock_brk
);
7980 smb2_set_err_rsp(work
);
7984 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
7985 * @work: smb work containing oplock/lease break command buffer
7989 int smb2_oplock_break(struct ksmbd_work
*work
)
7991 struct smb2_oplock_break
*req
= work
->request_buf
;
7992 struct smb2_oplock_break
*rsp
= work
->response_buf
;
7994 switch (le16_to_cpu(req
->StructureSize
)) {
7995 case OP_BREAK_STRUCT_SIZE_20
:
7996 smb20_oplock_break_ack(work
);
7998 case OP_BREAK_STRUCT_SIZE_21
:
7999 smb21_lease_break_ack(work
);
8002 ksmbd_debug(OPLOCK
, "invalid break cmd %d\n",
8003 le16_to_cpu(req
->StructureSize
));
8004 rsp
->hdr
.Status
= STATUS_INVALID_PARAMETER
;
8005 smb2_set_err_rsp(work
);
8012 * smb2_notify() - handler for smb2 notify request
8013 * @work: smb work containing notify command buffer
8017 int smb2_notify(struct ksmbd_work
*work
)
8019 struct smb2_notify_req
*req
;
8020 struct smb2_notify_rsp
*rsp
;
8022 WORK_BUFFERS(work
, req
, rsp
);
8024 if (work
->next_smb2_rcv_hdr_off
&& req
->hdr
.NextCommand
) {
8025 rsp
->hdr
.Status
= STATUS_INTERNAL_ERROR
;
8026 smb2_set_err_rsp(work
);
8030 smb2_set_err_rsp(work
);
8031 rsp
->hdr
.Status
= STATUS_NOT_IMPLEMENTED
;
8036 * smb2_is_sign_req() - handler for checking packet signing status
8037 * @work: smb work containing notify command buffer
8038 * @command: SMB2 command id
8040 * Return: true if packed is signed, false otherwise
8042 bool smb2_is_sign_req(struct ksmbd_work
*work
, unsigned int command
)
8044 struct smb2_hdr
*rcv_hdr2
= work
->request_buf
;
8046 if ((rcv_hdr2
->Flags
& SMB2_FLAGS_SIGNED
) &&
8047 command
!= SMB2_NEGOTIATE_HE
&&
8048 command
!= SMB2_SESSION_SETUP_HE
&&
8049 command
!= SMB2_OPLOCK_BREAK_HE
)
8056 * smb2_check_sign_req() - handler for req packet sign processing
8057 * @work: smb work containing notify command buffer
8059 * Return: 1 on success, 0 otherwise
8061 int smb2_check_sign_req(struct ksmbd_work
*work
)
8063 struct smb2_hdr
*hdr
, *hdr_org
;
8064 char signature_req
[SMB2_SIGNATURE_SIZE
];
8065 char signature
[SMB2_HMACSHA256_SIZE
];
8069 hdr_org
= hdr
= work
->request_buf
;
8070 if (work
->next_smb2_rcv_hdr_off
)
8071 hdr
= ksmbd_req_buf_next(work
);
8073 if (!hdr
->NextCommand
&& !work
->next_smb2_rcv_hdr_off
)
8074 len
= be32_to_cpu(hdr_org
->smb2_buf_length
);
8075 else if (hdr
->NextCommand
)
8076 len
= le32_to_cpu(hdr
->NextCommand
);
8078 len
= be32_to_cpu(hdr_org
->smb2_buf_length
) -
8079 work
->next_smb2_rcv_hdr_off
;
8081 memcpy(signature_req
, hdr
->Signature
, SMB2_SIGNATURE_SIZE
);
8082 memset(hdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
8084 iov
[0].iov_base
= (char *)&hdr
->ProtocolId
;
8085 iov
[0].iov_len
= len
;
8087 if (ksmbd_sign_smb2_pdu(work
->conn
, work
->sess
->sess_key
, iov
, 1,
8091 if (memcmp(signature
, signature_req
, SMB2_SIGNATURE_SIZE
)) {
8092 pr_err("bad smb2 signature\n");
8100 * smb2_set_sign_rsp() - handler for rsp packet sign processing
8101 * @work: smb work containing notify command buffer
8104 void smb2_set_sign_rsp(struct ksmbd_work
*work
)
8106 struct smb2_hdr
*hdr
, *hdr_org
;
8107 struct smb2_hdr
*req_hdr
;
8108 char signature
[SMB2_HMACSHA256_SIZE
];
8113 hdr_org
= hdr
= work
->response_buf
;
8114 if (work
->next_smb2_rsp_hdr_off
)
8115 hdr
= ksmbd_resp_buf_next(work
);
8117 req_hdr
= ksmbd_req_buf_next(work
);
8119 if (!work
->next_smb2_rsp_hdr_off
) {
8120 len
= get_rfc1002_len(hdr_org
);
8121 if (req_hdr
->NextCommand
)
8122 len
= ALIGN(len
, 8);
8124 len
= get_rfc1002_len(hdr_org
) - work
->next_smb2_rsp_hdr_off
;
8125 len
= ALIGN(len
, 8);
8128 if (req_hdr
->NextCommand
)
8129 hdr
->NextCommand
= cpu_to_le32(len
);
8131 hdr
->Flags
|= SMB2_FLAGS_SIGNED
;
8132 memset(hdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
8134 iov
[0].iov_base
= (char *)&hdr
->ProtocolId
;
8135 iov
[0].iov_len
= len
;
8137 if (work
->aux_payload_sz
) {
8138 iov
[0].iov_len
-= work
->aux_payload_sz
;
8140 iov
[1].iov_base
= work
->aux_payload_buf
;
8141 iov
[1].iov_len
= work
->aux_payload_sz
;
8145 if (!ksmbd_sign_smb2_pdu(work
->conn
, work
->sess
->sess_key
, iov
, n_vec
,
8147 memcpy(hdr
->Signature
, signature
, SMB2_SIGNATURE_SIZE
);
8151 * smb3_check_sign_req() - handler for req packet sign processing
8152 * @work: smb work containing notify command buffer
8154 * Return: 1 on success, 0 otherwise
8156 int smb3_check_sign_req(struct ksmbd_work
*work
)
8158 struct ksmbd_conn
*conn
= work
->conn
;
8160 struct smb2_hdr
*hdr
, *hdr_org
;
8161 struct channel
*chann
;
8162 char signature_req
[SMB2_SIGNATURE_SIZE
];
8163 char signature
[SMB2_CMACAES_SIZE
];
8167 hdr_org
= hdr
= work
->request_buf
;
8168 if (work
->next_smb2_rcv_hdr_off
)
8169 hdr
= ksmbd_req_buf_next(work
);
8171 if (!hdr
->NextCommand
&& !work
->next_smb2_rcv_hdr_off
)
8172 len
= be32_to_cpu(hdr_org
->smb2_buf_length
);
8173 else if (hdr
->NextCommand
)
8174 len
= le32_to_cpu(hdr
->NextCommand
);
8176 len
= be32_to_cpu(hdr_org
->smb2_buf_length
) -
8177 work
->next_smb2_rcv_hdr_off
;
8179 if (le16_to_cpu(hdr
->Command
) == SMB2_SESSION_SETUP_HE
) {
8180 signing_key
= work
->sess
->smb3signingkey
;
8182 chann
= lookup_chann_list(work
->sess
, conn
);
8185 signing_key
= chann
->smb3signingkey
;
8189 pr_err("SMB3 signing key is not generated\n");
8193 memcpy(signature_req
, hdr
->Signature
, SMB2_SIGNATURE_SIZE
);
8194 memset(hdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
8195 iov
[0].iov_base
= (char *)&hdr
->ProtocolId
;
8196 iov
[0].iov_len
= len
;
8198 if (ksmbd_sign_smb3_pdu(conn
, signing_key
, iov
, 1, signature
))
8201 if (memcmp(signature
, signature_req
, SMB2_SIGNATURE_SIZE
)) {
8202 pr_err("bad smb2 signature\n");
8210 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8211 * @work: smb work containing notify command buffer
8214 void smb3_set_sign_rsp(struct ksmbd_work
*work
)
8216 struct ksmbd_conn
*conn
= work
->conn
;
8217 struct smb2_hdr
*req_hdr
;
8218 struct smb2_hdr
*hdr
, *hdr_org
;
8219 struct channel
*chann
;
8220 char signature
[SMB2_CMACAES_SIZE
];
8226 hdr_org
= hdr
= work
->response_buf
;
8227 if (work
->next_smb2_rsp_hdr_off
)
8228 hdr
= ksmbd_resp_buf_next(work
);
8230 req_hdr
= ksmbd_req_buf_next(work
);
8232 if (!work
->next_smb2_rsp_hdr_off
) {
8233 len
= get_rfc1002_len(hdr_org
);
8234 if (req_hdr
->NextCommand
)
8235 len
= ALIGN(len
, 8);
8237 len
= get_rfc1002_len(hdr_org
) - work
->next_smb2_rsp_hdr_off
;
8238 len
= ALIGN(len
, 8);
8241 if (conn
->binding
== false &&
8242 le16_to_cpu(hdr
->Command
) == SMB2_SESSION_SETUP_HE
) {
8243 signing_key
= work
->sess
->smb3signingkey
;
8245 chann
= lookup_chann_list(work
->sess
, work
->conn
);
8248 signing_key
= chann
->smb3signingkey
;
8254 if (req_hdr
->NextCommand
)
8255 hdr
->NextCommand
= cpu_to_le32(len
);
8257 hdr
->Flags
|= SMB2_FLAGS_SIGNED
;
8258 memset(hdr
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
8259 iov
[0].iov_base
= (char *)&hdr
->ProtocolId
;
8260 iov
[0].iov_len
= len
;
8261 if (work
->aux_payload_sz
) {
8262 iov
[0].iov_len
-= work
->aux_payload_sz
;
8263 iov
[1].iov_base
= work
->aux_payload_buf
;
8264 iov
[1].iov_len
= work
->aux_payload_sz
;
8268 if (!ksmbd_sign_smb3_pdu(conn
, signing_key
, iov
, n_vec
, signature
))
8269 memcpy(hdr
->Signature
, signature
, SMB2_SIGNATURE_SIZE
);
8273 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8274 * @work: smb work containing response buffer
8277 void smb3_preauth_hash_rsp(struct ksmbd_work
*work
)
8279 struct ksmbd_conn
*conn
= work
->conn
;
8280 struct ksmbd_session
*sess
= work
->sess
;
8281 struct smb2_hdr
*req
, *rsp
;
8283 if (conn
->dialect
!= SMB311_PROT_ID
)
8286 WORK_BUFFERS(work
, req
, rsp
);
8288 if (le16_to_cpu(req
->Command
) == SMB2_NEGOTIATE_HE
&&
8290 ksmbd_gen_preauth_integrity_hash(conn
, (char *)rsp
,
8291 conn
->preauth_info
->Preauth_HashValue
);
8293 if (le16_to_cpu(rsp
->Command
) == SMB2_SESSION_SETUP_HE
&& sess
) {
8296 if (conn
->binding
) {
8297 struct preauth_session
*preauth_sess
;
8299 preauth_sess
= ksmbd_preauth_session_lookup(conn
, sess
->id
);
8302 hash_value
= preauth_sess
->Preauth_HashValue
;
8304 hash_value
= sess
->Preauth_HashValue
;
8308 ksmbd_gen_preauth_integrity_hash(conn
, (char *)rsp
,
8313 static void fill_transform_hdr(struct smb2_transform_hdr
*tr_hdr
, char *old_buf
,
8316 struct smb2_hdr
*hdr
= (struct smb2_hdr
*)old_buf
;
8317 unsigned int orig_len
= get_rfc1002_len(old_buf
);
8319 memset(tr_hdr
, 0, sizeof(struct smb2_transform_hdr
));
8320 tr_hdr
->ProtocolId
= SMB2_TRANSFORM_PROTO_NUM
;
8321 tr_hdr
->OriginalMessageSize
= cpu_to_le32(orig_len
);
8322 tr_hdr
->Flags
= cpu_to_le16(0x01);
8323 if (cipher_type
== SMB2_ENCRYPTION_AES128_GCM
||
8324 cipher_type
== SMB2_ENCRYPTION_AES256_GCM
)
8325 get_random_bytes(&tr_hdr
->Nonce
, SMB3_AES_GCM_NONCE
);
8327 get_random_bytes(&tr_hdr
->Nonce
, SMB3_AES_CCM_NONCE
);
8328 memcpy(&tr_hdr
->SessionId
, &hdr
->SessionId
, 8);
8329 inc_rfc1001_len(tr_hdr
, sizeof(struct smb2_transform_hdr
) - 4);
8330 inc_rfc1001_len(tr_hdr
, orig_len
);
8333 int smb3_encrypt_resp(struct ksmbd_work
*work
)
8335 char *buf
= work
->response_buf
;
8336 struct smb2_transform_hdr
*tr_hdr
;
8339 int buf_size
= 0, rq_nvec
= 2 + (work
->aux_payload_sz
? 1 : 0);
8341 if (ARRAY_SIZE(iov
) < rq_nvec
)
8344 tr_hdr
= kzalloc(sizeof(struct smb2_transform_hdr
), GFP_KERNEL
);
8348 /* fill transform header */
8349 fill_transform_hdr(tr_hdr
, buf
, work
->conn
->cipher_type
);
8351 iov
[0].iov_base
= tr_hdr
;
8352 iov
[0].iov_len
= sizeof(struct smb2_transform_hdr
);
8353 buf_size
+= iov
[0].iov_len
- 4;
8355 iov
[1].iov_base
= buf
+ 4;
8356 iov
[1].iov_len
= get_rfc1002_len(buf
);
8357 if (work
->aux_payload_sz
) {
8358 iov
[1].iov_len
= work
->resp_hdr_sz
- 4;
8360 iov
[2].iov_base
= work
->aux_payload_buf
;
8361 iov
[2].iov_len
= work
->aux_payload_sz
;
8362 buf_size
+= iov
[2].iov_len
;
8364 buf_size
+= iov
[1].iov_len
;
8365 work
->resp_hdr_sz
= iov
[1].iov_len
;
8367 rc
= ksmbd_crypt_message(work
->conn
, iov
, rq_nvec
, 1);
8371 memmove(buf
, iov
[1].iov_base
, iov
[1].iov_len
);
8372 tr_hdr
->smb2_buf_length
= cpu_to_be32(buf_size
);
8373 work
->tr_buf
= tr_hdr
;
8378 bool smb3_is_transform_hdr(void *buf
)
8380 struct smb2_transform_hdr
*trhdr
= buf
;
8382 return trhdr
->ProtocolId
== SMB2_TRANSFORM_PROTO_NUM
;
8385 int smb3_decrypt_req(struct ksmbd_work
*work
)
8387 struct ksmbd_conn
*conn
= work
->conn
;
8388 struct ksmbd_session
*sess
;
8389 char *buf
= work
->request_buf
;
8390 struct smb2_hdr
*hdr
;
8391 unsigned int pdu_length
= get_rfc1002_len(buf
);
8393 int buf_data_size
= pdu_length
+ 4 -
8394 sizeof(struct smb2_transform_hdr
);
8395 struct smb2_transform_hdr
*tr_hdr
= (struct smb2_transform_hdr
*)buf
;
8398 if (buf_data_size
< sizeof(struct smb2_hdr
)) {
8399 pr_err("Transform message is too small (%u)\n",
8401 return -ECONNABORTED
;
8404 if (buf_data_size
< le32_to_cpu(tr_hdr
->OriginalMessageSize
)) {
8405 pr_err("Transform message is broken\n");
8406 return -ECONNABORTED
;
8409 sess
= ksmbd_session_lookup_all(conn
, le64_to_cpu(tr_hdr
->SessionId
));
8411 pr_err("invalid session id(%llx) in transform header\n",
8412 le64_to_cpu(tr_hdr
->SessionId
));
8413 return -ECONNABORTED
;
8416 iov
[0].iov_base
= buf
;
8417 iov
[0].iov_len
= sizeof(struct smb2_transform_hdr
);
8418 iov
[1].iov_base
= buf
+ sizeof(struct smb2_transform_hdr
);
8419 iov
[1].iov_len
= buf_data_size
;
8420 rc
= ksmbd_crypt_message(conn
, iov
, 2, 0);
8424 memmove(buf
+ 4, iov
[1].iov_base
, buf_data_size
);
8425 hdr
= (struct smb2_hdr
*)buf
;
8426 hdr
->smb2_buf_length
= cpu_to_be32(buf_data_size
);
8431 bool smb3_11_final_sess_setup_resp(struct ksmbd_work
*work
)
8433 struct ksmbd_conn
*conn
= work
->conn
;
8434 struct smb2_hdr
*rsp
= work
->response_buf
;
8436 if (conn
->dialect
< SMB30_PROT_ID
)
8439 if (work
->next_smb2_rcv_hdr_off
)
8440 rsp
= ksmbd_resp_buf_next(work
);
8442 if (le16_to_cpu(rsp
->Command
) == SMB2_SESSION_SETUP_HE
&&
8443 rsp
->Status
== STATUS_SUCCESS
)