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