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