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