ksmbd: fix encryption failure issue for session logoff response
[linux-block.git] / fs / ksmbd / smb2pdu.h
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#ifndef _SMB2PDU_H
8#define _SMB2PDU_H
9
10#include "ntlmssp.h"
11#include "smbacl.h"
12
e2f34481
NJ
13/*Create Action Flags*/
14#define FILE_SUPERSEDED 0x00000000
15#define FILE_OPENED 0x00000001
16#define FILE_CREATED 0x00000002
17#define FILE_OVERWRITTEN 0x00000003
18
e2f34481
NJ
19/* SMB2 Max Credits */
20#define SMB2_MAX_CREDITS 8192
21
e2f34481
NJ
22/* BB FIXME - analyze following length BB */
23#define MAX_SMB2_HDR_SIZE 0x78 /* 4 len + 64 hdr + (2*24 wct) + 2 bct + 2 pad */
24
e2f34481 25#define SMB21_DEFAULT_IOSIZE (1024 * 1024)
e2f34481 26#define SMB3_DEFAULT_TRANS_SIZE (1024 * 1024)
4bc59477
NJ
27#define SMB3_MIN_IOSIZE (64 * 1024)
28#define SMB3_MAX_IOSIZE (8 * 1024 * 1024)
e2f34481 29
e2f34481
NJ
30/*
31 * Definitions for SMB2 Protocol Data Units (network frames)
32 *
33 * See MS-SMB2.PDF specification for protocol details.
34 * The Naming convention is the lower case version of the SMB2
35 * command code name for the struct. Note that structures must be packed.
36 *
37 */
38
e2f34481
NJ
39struct preauth_integrity_info {
40 /* PreAuth integrity Hash ID */
41 __le16 Preauth_HashId;
42 /* PreAuth integrity Hash Value */
d6c9ad23 43 __u8 Preauth_HashValue[SMB2_PREAUTH_HASH_SIZE];
e2f34481
NJ
44};
45
cb451720 46/* offset is sizeof smb2_negotiate_rsp but rounded up to 8 bytes. */
e2f34481 47#ifdef CONFIG_SMB_SERVER_KERBEROS5
cb451720 48/* sizeof(struct smb2_negotiate_rsp) =
e2f34481
NJ
49 * header(64) + response(64) + GSS_LENGTH(96) + GSS_PADDING(0)
50 */
51#define OFFSET_OF_NEG_CONTEXT 0xe0
52#else
cb451720 53/* sizeof(struct smb2_negotiate_rsp) =
e2f34481
NJ
54 * header(64) + response(64) + GSS_LENGTH(74) + GSS_PADDING(6)
55 */
56#define OFFSET_OF_NEG_CONTEXT 0xd0
57#endif
58
e2f34481 59#define SMB2_SESSION_EXPIRED (0)
64b39f4a
NJ
60#define SMB2_SESSION_IN_PROGRESS BIT(0)
61#define SMB2_SESSION_VALID BIT(1)
e2f34481 62
e2f34481
NJ
63struct create_durable_req_v2 {
64 struct create_context ccontext;
65 __u8 Name[8];
66 __le32 Timeout;
67 __le32 Flags;
68 __u8 Reserved[8];
69 __u8 CreateGuid[16];
70} __packed;
71
72struct create_durable_reconn_req {
73 struct create_context ccontext;
74 __u8 Name[8];
75 union {
76 __u8 Reserved[16];
77 struct {
2d004c6c
PA
78 __u64 PersistentFileId;
79 __u64 VolatileFileId;
e2f34481
NJ
80 } Fid;
81 } Data;
82} __packed;
83
84struct create_durable_reconn_v2_req {
85 struct create_context ccontext;
86 __u8 Name[8];
87 struct {
2d004c6c
PA
88 __u64 PersistentFileId;
89 __u64 VolatileFileId;
e2f34481
NJ
90 } Fid;
91 __u8 CreateGuid[16];
92 __le32 Flags;
93} __packed;
94
95struct create_app_inst_id {
96 struct create_context ccontext;
97 __u8 Name[8];
98 __u8 Reserved[8];
99 __u8 AppInstanceId[16];
100} __packed;
101
102struct create_app_inst_id_vers {
103 struct create_context ccontext;
104 __u8 Name[8];
105 __u8 Reserved[2];
106 __u8 Padding[4];
107 __le64 AppInstanceVersionHigh;
108 __le64 AppInstanceVersionLow;
109} __packed;
110
111struct create_mxac_req {
112 struct create_context ccontext;
113 __u8 Name[8];
114 __le64 Timestamp;
115} __packed;
116
117struct create_alloc_size_req {
118 struct create_context ccontext;
119 __u8 Name[8];
120 __le64 AllocationSize;
121} __packed;
122
e2f34481
NJ
123struct create_durable_rsp {
124 struct create_context ccontext;
125 __u8 Name[8];
126 union {
127 __u8 Reserved[8];
128 __u64 data;
129 } Data;
130} __packed;
131
132struct create_durable_v2_rsp {
133 struct create_context ccontext;
134 __u8 Name[8];
135 __le32 Timeout;
136 __le32 Flags;
137} __packed;
138
139struct create_mxac_rsp {
140 struct create_context ccontext;
141 __u8 Name[8];
142 __le32 QueryStatus;
143 __le32 MaximalAccess;
144} __packed;
145
146struct create_disk_id_rsp {
147 struct create_context ccontext;
148 __u8 Name[8];
149 __le64 DiskFileId;
150 __le64 VolumeId;
151 __u8 Reserved[16];
152} __packed;
153
154/* equivalent of the contents of SMB3.1.1 POSIX open context response */
155struct create_posix_rsp {
156 struct create_context ccontext;
157 __u8 Name[16];
158 __le32 nlink;
159 __le32 reparse_tag;
160 __le32 mode;
5609bdd9
NJ
161 /* SidBuffer contain two sids(Domain sid(28), UNIX group sid(16)) */
162 u8 SidBuffer[44];
e2f34481
NJ
163} __packed;
164
e2f34481
NJ
165struct smb2_buffer_desc_v1 {
166 __le64 offset;
167 __le32 token;
168 __le32 length;
169} __packed;
170
e2f34481
NJ
171#define SMB2_0_IOCTL_IS_FSCTL 0x00000001
172
e2f34481
NJ
173struct smb_sockaddr_in {
174 __be16 Port;
175 __be32 IPv4address;
176 __u8 Reserved[8];
177} __packed;
178
179struct smb_sockaddr_in6 {
180 __be16 Port;
181 __be32 FlowInfo;
182 __u8 IPv6address[16];
183 __be32 ScopeId;
184} __packed;
185
186#define INTERNETWORK 0x0002
187#define INTERNETWORKV6 0x0017
188
189struct sockaddr_storage_rsp {
190 __le16 Family;
191 union {
192 struct smb_sockaddr_in addr4;
193 struct smb_sockaddr_in6 addr6;
194 };
195} __packed;
196
197#define RSS_CAPABLE 0x00000001
198#define RDMA_CAPABLE 0x00000002
199
200struct network_interface_info_ioctl_rsp {
201 __le32 Next; /* next interface. zero if this is last one */
202 __le32 IfIndex;
203 __le32 Capability; /* RSS or RDMA Capable */
204 __le32 Reserved;
205 __le64 LinkSpeed;
206 char SockAddr_Storage[128];
207} __packed;
208
209struct file_object_buf_type1_ioctl_rsp {
210 __u8 ObjectId[16];
211 __u8 BirthVolumeId[16];
212 __u8 BirthObjectId[16];
213 __u8 DomainId[16];
214} __packed;
215
216struct resume_key_ioctl_rsp {
2d004c6c 217 __u64 ResumeKey[3];
e2f34481
NJ
218 __le32 ContextLength;
219 __u8 Context[4]; /* ignored, Windows sets to 4 bytes of zero */
220} __packed;
221
222struct copychunk_ioctl_req {
223 __le64 ResumeKey[3];
224 __le32 ChunkCount;
225 __le32 Reserved;
226 __u8 Chunks[1]; /* array of srv_copychunk */
227} __packed;
228
229struct srv_copychunk {
230 __le64 SourceOffset;
231 __le64 TargetOffset;
232 __le32 Length;
233 __le32 Reserved;
234} __packed;
235
236struct copychunk_ioctl_rsp {
237 __le32 ChunksWritten;
238 __le32 ChunkBytesWritten;
239 __le32 TotalBytesWritten;
240} __packed;
241
242struct file_sparse {
243 __u8 SetSparse;
244} __packed;
245
e2f34481
NJ
246/* FILE Info response size */
247#define FILE_DIRECTORY_INFORMATION_SIZE 1
248#define FILE_FULL_DIRECTORY_INFORMATION_SIZE 2
249#define FILE_BOTH_DIRECTORY_INFORMATION_SIZE 3
250#define FILE_BASIC_INFORMATION_SIZE 40
251#define FILE_STANDARD_INFORMATION_SIZE 24
252#define FILE_INTERNAL_INFORMATION_SIZE 8
253#define FILE_EA_INFORMATION_SIZE 4
254#define FILE_ACCESS_INFORMATION_SIZE 4
255#define FILE_NAME_INFORMATION_SIZE 9
256#define FILE_RENAME_INFORMATION_SIZE 10
257#define FILE_LINK_INFORMATION_SIZE 11
258#define FILE_NAMES_INFORMATION_SIZE 12
259#define FILE_DISPOSITION_INFORMATION_SIZE 13
260#define FILE_POSITION_INFORMATION_SIZE 14
261#define FILE_FULL_EA_INFORMATION_SIZE 15
262#define FILE_MODE_INFORMATION_SIZE 4
263#define FILE_ALIGNMENT_INFORMATION_SIZE 4
264#define FILE_ALL_INFORMATION_SIZE 104
265#define FILE_ALLOCATION_INFORMATION_SIZE 19
266#define FILE_END_OF_FILE_INFORMATION_SIZE 20
267#define FILE_ALTERNATE_NAME_INFORMATION_SIZE 8
268#define FILE_STREAM_INFORMATION_SIZE 32
269#define FILE_PIPE_INFORMATION_SIZE 23
270#define FILE_PIPE_LOCAL_INFORMATION_SIZE 24
271#define FILE_PIPE_REMOTE_INFORMATION_SIZE 25
272#define FILE_MAILSLOT_QUERY_INFORMATION_SIZE 26
273#define FILE_MAILSLOT_SET_INFORMATION_SIZE 27
274#define FILE_COMPRESSION_INFORMATION_SIZE 16
275#define FILE_OBJECT_ID_INFORMATION_SIZE 29
276/* Number 30 not defined in documents */
277#define FILE_MOVE_CLUSTER_INFORMATION_SIZE 31
278#define FILE_QUOTA_INFORMATION_SIZE 32
279#define FILE_REPARSE_POINT_INFORMATION_SIZE 33
280#define FILE_NETWORK_OPEN_INFORMATION_SIZE 56
281#define FILE_ATTRIBUTE_TAG_INFORMATION_SIZE 8
282
e2f34481
NJ
283/* FS Info response size */
284#define FS_DEVICE_INFORMATION_SIZE 8
285#define FS_ATTRIBUTE_INFORMATION_SIZE 16
286#define FS_VOLUME_INFORMATION_SIZE 24
287#define FS_SIZE_INFORMATION_SIZE 24
288#define FS_FULL_SIZE_INFORMATION_SIZE 32
289#define FS_SECTOR_SIZE_INFORMATION_SIZE 28
290#define FS_OBJECT_ID_INFORMATION_SIZE 64
291#define FS_CONTROL_INFORMATION_SIZE 48
292#define FS_POSIX_INFORMATION_SIZE 56
293
294/* FS_ATTRIBUTE_File_System_Name */
295#define FS_TYPE_SUPPORT_SIZE 44
296struct fs_type_info {
297 char *fs_name;
298 long magic_number;
299} __packed;
300
e2f34481 301/*
be135000 302 * PDU query infolevel structure definitions
e2f34481
NJ
303 * BB consider moving to a different header
304 */
305
e2f34481
NJ
306struct smb2_file_access_info {
307 __le32 AccessFlags;
308} __packed;
309
310struct smb2_file_alignment_info {
311 __le32 AlignmentRequirement;
312} __packed;
313
88d30052
NJ
314struct smb2_file_basic_info { /* data block encoding of response to level 18 */
315 __le64 CreationTime; /* Beginning of FILE_BASIC_INFO equivalent */
316 __le64 LastAccessTime;
317 __le64 LastWriteTime;
318 __le64 ChangeTime;
319 __le32 Attributes;
320 __u32 Pad1; /* End of FILE_BASIC_INFO_INFO equivalent */
321} __packed;
322
e2f34481
NJ
323struct smb2_file_alt_name_info {
324 __le32 FileNameLength;
5224f790 325 char FileName[];
e2f34481
NJ
326} __packed;
327
328struct smb2_file_stream_info {
329 __le32 NextEntryOffset;
330 __le32 StreamNameLength;
331 __le64 StreamSize;
332 __le64 StreamAllocationSize;
5224f790 333 char StreamName[];
e2f34481
NJ
334} __packed;
335
e2f34481
NJ
336struct smb2_file_ntwrk_info {
337 __le64 CreationTime;
338 __le64 LastAccessTime;
339 __le64 LastWriteTime;
340 __le64 ChangeTime;
341 __le64 AllocationSize;
342 __le64 EndOfFile;
343 __le32 Attributes;
344 __le32 Reserved;
345} __packed;
346
347struct smb2_file_standard_info {
348 __le64 AllocationSize;
349 __le64 EndOfFile;
350 __le32 NumberOfLinks; /* hard links */
351 __u8 DeletePending;
352 __u8 Directory;
353 __le16 Reserved;
354} __packed; /* level 18 Query */
355
356struct smb2_file_ea_info {
357 __le32 EASize;
358} __packed;
359
360struct smb2_file_alloc_info {
361 __le64 AllocationSize;
362} __packed;
363
364struct smb2_file_disposition_info {
365 __u8 DeletePending;
366} __packed;
367
368struct smb2_file_pos_info {
369 __le64 CurrentByteOffset;
370} __packed;
371
26a2787d 372#define FILE_MODE_INFO_MASK cpu_to_le32(0x0000100e)
e2f34481
NJ
373
374struct smb2_file_mode_info {
375 __le32 Mode;
376} __packed;
377
378#define COMPRESSION_FORMAT_NONE 0x0000
379#define COMPRESSION_FORMAT_LZNT1 0x0002
380
381struct smb2_file_comp_info {
382 __le64 CompressedFileSize;
383 __le16 CompressionFormat;
384 __u8 CompressionUnitShift;
385 __u8 ChunkShift;
386 __u8 ClusterShift;
387 __u8 Reserved[3];
388} __packed;
389
390struct smb2_file_attr_tag_info {
391 __le32 FileAttributes;
392 __le32 ReparseTag;
393} __packed;
394
395#define SL_RESTART_SCAN 0x00000001
396#define SL_RETURN_SINGLE_ENTRY 0x00000002
397#define SL_INDEX_SPECIFIED 0x00000004
398
399struct smb2_ea_info_req {
400 __le32 NextEntryOffset;
401 __u8 EaNameLength;
402 char name[1];
403} __packed; /* level 15 Query */
404
405struct smb2_ea_info {
406 __le32 NextEntryOffset;
407 __u8 Flags;
408 __u8 EaNameLength;
409 __le16 EaValueLength;
410 char name[1];
411 /* optionally followed by value */
412} __packed; /* level 15 Query */
413
414struct create_ea_buf_req {
415 struct create_context ccontext;
416 __u8 Name[8];
417 struct smb2_ea_info ea;
418} __packed;
419
420struct create_sd_buf_req {
421 struct create_context ccontext;
422 __u8 Name[8];
423 struct smb_ntsd ntsd;
424} __packed;
425
e2f34481
NJ
426struct smb2_posix_info {
427 __le32 NextEntryOffset;
428 __u32 Ignored;
429 __le64 CreationTime;
430 __le64 LastAccessTime;
431 __le64 LastWriteTime;
432 __le64 ChangeTime;
433 __le64 EndOfFile;
434 __le64 AllocationSize;
435 __le32 DosAttributes;
436 __le64 Inode;
437 __le32 DeviceId;
438 __le32 Zero;
439 /* beginning of POSIX Create Context Response */
440 __le32 HardLinks;
441 __le32 ReparseTag;
442 __le32 Mode;
5609bdd9
NJ
443 /* SidBuffer contain two sids (UNIX user sid(16), UNIX group sid(16)) */
444 u8 SidBuffer[32];
e2f34481
NJ
445 __le32 name_len;
446 u8 name[1];
447 /*
448 * var sized owner SID
449 * var sized group SID
450 * le32 filenamelength
451 * u8 filename[]
452 */
453} __packed;
454
455/* functions */
64b39f4a
NJ
456void init_smb2_1_server(struct ksmbd_conn *conn);
457void init_smb3_0_server(struct ksmbd_conn *conn);
458void init_smb3_02_server(struct ksmbd_conn *conn);
459int init_smb3_11_server(struct ksmbd_conn *conn);
460
461void init_smb2_max_read_size(unsigned int sz);
462void init_smb2_max_write_size(unsigned int sz);
463void init_smb2_max_trans_size(unsigned int sz);
004443b3 464void init_smb2_max_credits(unsigned int sz);
64b39f4a 465
f4228b67
NJ
466bool is_smb2_neg_cmd(struct ksmbd_work *work);
467bool is_smb2_rsp(struct ksmbd_work *work);
64b39f4a
NJ
468
469u16 get_smb2_cmd_val(struct ksmbd_work *work);
470void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err);
471int init_smb2_rsp_hdr(struct ksmbd_work *work);
472int smb2_allocate_rsp_buf(struct ksmbd_work *work);
473bool is_chained_smb2_message(struct ksmbd_work *work);
474int init_smb2_neg_rsp(struct ksmbd_work *work);
475void smb2_set_err_rsp(struct ksmbd_work *work);
476int smb2_check_user_session(struct ksmbd_work *work);
477int smb2_get_ksmbd_tcon(struct ksmbd_work *work);
478bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command);
479int smb2_check_sign_req(struct ksmbd_work *work);
480void smb2_set_sign_rsp(struct ksmbd_work *work);
481int smb3_check_sign_req(struct ksmbd_work *work);
482void smb3_set_sign_rsp(struct ksmbd_work *work);
483int find_matching_smb2_dialect(int start_index, __le16 *cli_dialects,
d7e5852b 484 __le16 dialects_count);
64b39f4a
NJ
485struct file_lock *smb_flock_init(struct file *f);
486int setup_async_work(struct ksmbd_work *work, void (*fn)(void **),
d7e5852b 487 void **arg);
64b39f4a 488void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status);
f5a544e3
NJ
489struct channel *lookup_chann_list(struct ksmbd_session *sess,
490 struct ksmbd_conn *conn);
64b39f4a 491void smb3_preauth_hash_rsp(struct ksmbd_work *work);
f4228b67 492bool smb3_is_transform_hdr(void *buf);
64b39f4a
NJ
493int smb3_decrypt_req(struct ksmbd_work *work);
494int smb3_encrypt_resp(struct ksmbd_work *work);
495bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work);
496int smb2_set_rsp_credits(struct ksmbd_work *work);
e2f34481
NJ
497
498/* smb2 misc functions */
64b39f4a 499int ksmbd_smb2_check_message(struct ksmbd_work *work);
e2f34481
NJ
500
501/* smb2 command handlers */
64b39f4a
NJ
502int smb2_handle_negotiate(struct ksmbd_work *work);
503int smb2_negotiate_request(struct ksmbd_work *work);
504int smb2_sess_setup(struct ksmbd_work *work);
505int smb2_tree_connect(struct ksmbd_work *work);
506int smb2_tree_disconnect(struct ksmbd_work *work);
507int smb2_session_logoff(struct ksmbd_work *work);
508int smb2_open(struct ksmbd_work *work);
509int smb2_query_info(struct ksmbd_work *work);
510int smb2_query_dir(struct ksmbd_work *work);
511int smb2_close(struct ksmbd_work *work);
512int smb2_echo(struct ksmbd_work *work);
513int smb2_set_info(struct ksmbd_work *work);
514int smb2_read(struct ksmbd_work *work);
515int smb2_write(struct ksmbd_work *work);
516int smb2_flush(struct ksmbd_work *work);
517int smb2_cancel(struct ksmbd_work *work);
518int smb2_lock(struct ksmbd_work *work);
519int smb2_ioctl(struct ksmbd_work *work);
520int smb2_oplock_break(struct ksmbd_work *work);
521int smb2_notify(struct ksmbd_work *ksmbd_work);
e2f34481 522
cb451720
NJ
523/*
524 * Get the body of the smb2 message excluding the 4 byte rfc1002 headers
525 * from request/response buffer.
526 */
527static inline void *smb2_get_msg(void *buf)
528{
529 return buf + 4;
530}
531
e2f34481 532#endif /* _SMB2PDU_H */