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