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