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