xref: /linux/fs/smb/server/smb2pdu.h (revision f4fafaf02174c32bce2f9bb4196fadf13f1fd96e)
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	(4 * 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 /* Apple Defined Contexts */
65 #define SMB2_CREATE_AAPL		"AAPL"
66 
67 /*
68  * AAPL SMB2 extension -- kAAPL_SERVER_QUERY create context.
69  *
70  * Command code and bitmap values are the existing
71  * SMB2_CRTCTX_AAPL_* constants in fs/smb/common/smb2pdu.h.
72  *
73  * Omitting the model string when reply_bitmap includes
74  * SMB2_CRTCTX_AAPL_MODEL_INFO causes smbfs.kext to enter a broken
75  * disconnect path requiring a reboot.
76  *
77  * Layout: ccontext(16) + Name[4] + Pad[4] + cmd(4) + reserved(4) +
78  *         reply_bitmap(8) + server_caps(8) + vol_caps(8)
79  * When MODEL_INFO requested, appended: pad2(4) + model_bytes(4) + UTF-16LE
80  */
81 #define SMB2_CREATE_AAPL_LEN	4
82 
83 /*
84  * Server capability flags (server_caps field) -- SMB2_CRTCTX_AAPL_UNIX_BASED:
85  * prevents macOS Windows-compat mode (question-mark icons).
86  * SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE: enables server-side file copy via
87  * FSCTL_SRV_COPYCHUNK. SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR: inline
88  * FinderInfo per FIND entry, set when client also advertises the bit;
89  * format: EaSize=max_access, ShortName[0..7]=rfork_size,
90  * ShortName[8..23]=FinderInfo(16B), Reserved2=unix_mode.
91  */
92 #define AAPL_SERVER_CAPS_KSMBD	(SMB2_CRTCTX_AAPL_UNIX_BASED | \
93 				 SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE | \
94 				 SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR)
95 
96 /*
97  * READDIR_ATTR_V2 (SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR_V2, see
98  * fs/smb/common/smb2pdu.h) extends the same inline-FinderInfo mechanism
99  * above with a flags field, confirmed byte-identical to V1 otherwise
100  * against AAPL's actual public client behavior. When a client's own
101  * client_caps requests V2, the server advertises V2 instead of V1 in
102  * its own server_caps reply; V1 and V2 are mutually exclusive on the
103  * wire, not both set together. The wire format's ShortNameLength+Reserved
104  * (ignored in V1) become a single flags field in V2 --
105  * AAPL_READDIR_ATTR_V2_NO_XATTR is the only flag bit currently defined,
106  * signaling the item has no xattrs/streams so the client can skip a
107  * separate query.
108  */
109 #define AAPL_READDIR_ATTR_V2_NO_XATTR	0x01
110 
111 /* Model string: up to 31 ASCII chars */
112 #define AAPL_MODEL_MAX_CHARS	31
113 #define AAPL_MODEL_UTF16_BYTES	(AAPL_MODEL_MAX_CHARS * 2)
114 
115 /*
116  * Max AAPL response: header(24) + base data(32) + pad2(4) + model_bytes(4)
117  * + model(62), 8-byte aligned: ALIGN(126, 8) = 128 bytes.
118  */
119 #define AAPL_RSP_MAX_SIZE	128
120 
121 /* AAPL server query request (client->server) */
122 struct aapl_server_query_req {
123 	__le32 cmd;
124 	__le32 reserved;
125 	__le64 req_bitmap;
126 	__le64 client_caps;
127 } __packed;
128 
129 struct create_aapl_rsp {
130 	struct create_context_hdr ccontext;
131 	__u8   Name[4];
132 	__u8   Pad[4];
133 	__le32 cmd;
134 	__le32 reserved;
135 	__le64 reply_bitmap;
136 	__le64 server_caps;
137 	__le64 vol_caps;
138 	/* when MODEL_INFO requested: __le32 pad2; __le32 model_bytes; __le16 model[] */
139 } __packed;
140 
141 #define DURABLE_HANDLE_MAX_TIMEOUT	300000
142 
143 struct create_alloc_size_req {
144 	struct create_context_hdr ccontext;
145 	__u8   Name[8];
146 	__le64 AllocationSize;
147 } __packed;
148 
149 struct create_durable_rsp {
150 	struct create_context_hdr ccontext;
151 	__u8   Name[8];
152 	union {
153 		__u8  Reserved[8];
154 		__u64 data;
155 	} Data;
156 } __packed;
157 
158 /*
159  * See POSIX-SMB2 2.2.14.2.16
160  * Link: https://gitlab.com/samba-team/smb3-posix-spec/-/blob/master/smb3_posix_extensions.md
161  */
162 struct create_posix_rsp {
163 	struct create_context_hdr ccontext;
164 	__u8    Name[16];
165 	__le32 nlink;
166 	__le32 reparse_tag;
167 	__le32 mode;
168 	/* SidBuffer contain two sids(Domain sid(28), UNIX group sid(16)) */
169 	u8 SidBuffer[44];
170 } __packed;
171 
172 #define SMB2_0_IOCTL_IS_FSCTL 0x00000001
173 
174 struct sockaddr_storage_rsp {
175 	__le16 Family;
176 	union {
177 		struct smb_sockaddr_in addr4;
178 		struct smb_sockaddr_in6 addr6;
179 	};
180 } __packed;
181 
182 struct file_object_buf_type1_ioctl_rsp {
183 	__u8 ObjectId[16];
184 	__u8 BirthVolumeId[16];
185 	__u8 BirthObjectId[16];
186 	__u8 DomainId[16];
187 } __packed;
188 
189 struct file_sparse {
190 	__u8	SetSparse;
191 } __packed;
192 
193 /* FILE Info response size */
194 #define FILE_DIRECTORY_INFORMATION_SIZE       1
195 #define FILE_FULL_DIRECTORY_INFORMATION_SIZE  2
196 #define FILE_BOTH_DIRECTORY_INFORMATION_SIZE  3
197 #define FILE_BASIC_INFORMATION_SIZE           40
198 #define FILE_STANDARD_INFORMATION_SIZE        24
199 #define FILE_INTERNAL_INFORMATION_SIZE        8
200 #define FILE_EA_INFORMATION_SIZE              4
201 #define FILE_ACCESS_INFORMATION_SIZE          4
202 #define FILE_NAME_INFORMATION_SIZE            9
203 #define FILE_RENAME_INFORMATION_SIZE          10
204 #define FILE_LINK_INFORMATION_SIZE            11
205 #define FILE_NAMES_INFORMATION_SIZE           12
206 #define FILE_DISPOSITION_INFORMATION_SIZE     13
207 #define FILE_POSITION_INFORMATION_SIZE        14
208 #define FILE_FULL_EA_INFORMATION_SIZE         15
209 #define FILE_MODE_INFORMATION_SIZE            4
210 #define FILE_ALIGNMENT_INFORMATION_SIZE       4
211 #define FILE_ALL_INFORMATION_SIZE             104
212 #define FILE_ALLOCATION_INFORMATION_SIZE      19
213 #define FILE_END_OF_FILE_INFORMATION_SIZE     20
214 #define FILE_ALTERNATE_NAME_INFORMATION_SIZE  8
215 #define FILE_NORMALIZED_NAME_INFORMATION_SIZE 8
216 #define FILE_STREAM_INFORMATION_SIZE          32
217 #define FILE_PIPE_INFORMATION_SIZE            23
218 #define FILE_PIPE_LOCAL_INFORMATION_SIZE      24
219 #define FILE_PIPE_REMOTE_INFORMATION_SIZE     25
220 #define FILE_MAILSLOT_QUERY_INFORMATION_SIZE  26
221 #define FILE_MAILSLOT_SET_INFORMATION_SIZE    27
222 #define FILE_COMPRESSION_INFORMATION_SIZE     16
223 #define FILE_OBJECT_ID_INFORMATION_SIZE       29
224 /* Number 30 not defined in documents */
225 #define FILE_MOVE_CLUSTER_INFORMATION_SIZE    31
226 #define FILE_QUOTA_INFORMATION_SIZE           32
227 #define FILE_REPARSE_POINT_INFORMATION_SIZE   33
228 #define FILE_NETWORK_OPEN_INFORMATION_SIZE    56
229 #define FILE_ATTRIBUTE_TAG_INFORMATION_SIZE   8
230 
231 /* FS Info response  size */
232 #define FS_DEVICE_INFORMATION_SIZE     8
233 #define FS_ATTRIBUTE_INFORMATION_SIZE  16
234 #define FS_VOLUME_INFORMATION_SIZE     24
235 #define FS_SIZE_INFORMATION_SIZE       24
236 #define FS_FULL_SIZE_INFORMATION_SIZE  32
237 #define FS_SECTOR_SIZE_INFORMATION_SIZE 28
238 #define FS_OBJECT_ID_INFORMATION_SIZE 64
239 #define FS_CONTROL_INFORMATION_SIZE 48
240 #define FS_POSIX_INFORMATION_SIZE 56
241 
242 /* FS_ATTRIBUTE_File_System_Name */
243 #define FS_TYPE_SUPPORT_SIZE   44
244 struct fs_type_info {
245 	char		*fs_name;
246 	long		magic_number;
247 } __packed;
248 
249 /*
250  *	PDU query infolevel structure definitions
251  *	BB consider moving to a different header
252  */
253 
254 struct smb2_file_access_info {
255 	__le32 AccessFlags;
256 } __packed;
257 
258 struct smb2_file_alignment_info {
259 	__le32 AlignmentRequirement;
260 } __packed;
261 
262 struct smb2_file_alt_name_info {
263 	__le32 FileNameLength;
264 	char FileName[];
265 } __packed;
266 
267 struct smb2_file_stream_info {
268 	__le32  NextEntryOffset;
269 	__le32  StreamNameLength;
270 	__le64 StreamSize;
271 	__le64 StreamAllocationSize;
272 	char   StreamName[];
273 } __packed;
274 
275 struct srv_snapshot_array {
276 	__le32 NumberOfSnapShots;
277 	__le32 NumberOfSnapShotsReturned;
278 	__le32 SnapShotArraySize;
279 	__le32 Reserved;
280 } __packed;
281 
282 struct smb2_file_standard_info {
283 	__le64 AllocationSize;
284 	__le64 EndOfFile;
285 	__le32 NumberOfLinks;	/* hard links */
286 	__u8   DeletePending;
287 	__u8   Directory;
288 	__le16 Reserved;
289 } __packed; /* level 18 Query */
290 
291 struct smb2_file_ea_info {
292 	__le32 EASize;
293 } __packed;
294 
295 struct smb2_file_disposition_info {
296 	__u8 DeletePending;
297 } __packed;
298 
299 struct smb2_file_pos_info {
300 	__le64 CurrentByteOffset;
301 } __packed;
302 
303 #define FILE_MODE_INFO_MASK cpu_to_le32(0x0000100e)
304 
305 struct smb2_file_mode_info {
306 	__le32 Mode;
307 } __packed;
308 
309 struct smb2_file_comp_info {
310 	__le64 CompressedFileSize;
311 	__le16 CompressionFormat;
312 	__u8 CompressionUnitShift;
313 	__u8 ChunkShift;
314 	__u8 ClusterShift;
315 	__u8 Reserved[3];
316 } __packed;
317 
318 struct smb2_file_attr_tag_info {
319 	__le32 FileAttributes;
320 	__le32 ReparseTag;
321 } __packed;
322 
323 #define SL_RESTART_SCAN	0x00000001
324 #define SL_RETURN_SINGLE_ENTRY	0x00000002
325 #define SL_INDEX_SPECIFIED	0x00000004
326 
327 struct smb2_ea_info_req {
328 	__le32 NextEntryOffset;
329 	__u8   EaNameLength;
330 	char name[];
331 } __packed; /* level 15 Query */
332 
333 struct smb2_ea_info {
334 	__le32 NextEntryOffset;
335 	__u8   Flags;
336 	__u8   EaNameLength;
337 	__le16 EaValueLength;
338 	char name[];
339 	/* optionally followed by value */
340 } __packed; /* level 15 Query */
341 
342 struct create_ea_buf_req {
343 	struct create_context_hdr ccontext;
344 	__u8   Name[8];
345 	struct smb2_ea_info ea;
346 } __packed;
347 
348 struct create_sd_buf_req {
349 	struct create_context_hdr ccontext;
350 	__u8   Name[8];
351 	struct smb_ntsd ntsd;
352 } __packed;
353 
354 struct smb2_posix_info {
355 	__le32 NextEntryOffset;
356 	__u32 Ignored;
357 	__le64 CreationTime;
358 	__le64 LastAccessTime;
359 	__le64 LastWriteTime;
360 	__le64 ChangeTime;
361 	__le64 EndOfFile;
362 	__le64 AllocationSize;
363 	__le32 DosAttributes;
364 	__le64 Inode;
365 	__le32 DeviceId;
366 	__le32 Zero;
367 	/* beginning of POSIX Create Context Response */
368 	__le32 HardLinks;
369 	__le32 ReparseTag;
370 	__le32 Mode;
371 	/* SidBuffer contain two sids (UNIX user sid(16), UNIX group sid(16)) */
372 	u8 SidBuffer[32];
373 	__le32 name_len;
374 	u8 name[];
375 	/*
376 	 * var sized owner SID
377 	 * var sized group SID
378 	 * le32 filenamelength
379 	 * u8  filename[]
380 	 */
381 } __packed;
382 
383 /* functions */
384 void init_smb2_1_server(struct ksmbd_conn *conn);
385 void init_smb3_0_server(struct ksmbd_conn *conn);
386 void init_smb3_02_server(struct ksmbd_conn *conn);
387 int init_smb3_11_server(struct ksmbd_conn *conn);
388 
389 void init_smb2_max_read_size(unsigned int sz);
390 void init_smb2_max_write_size(unsigned int sz);
391 void init_smb2_max_trans_size(unsigned int sz);
392 void init_smb2_max_credits(unsigned int sz);
393 
394 bool is_smb2_neg_cmd(struct ksmbd_work *work);
395 bool is_smb2_rsp(struct ksmbd_work *work);
396 
397 u16 get_smb2_cmd_val(struct ksmbd_work *work);
398 void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err);
399 int init_smb2_rsp_hdr(struct ksmbd_work *work);
400 int smb2_allocate_rsp_buf(struct ksmbd_work *work);
401 bool is_chained_smb2_message(struct ksmbd_work *work);
402 int init_smb2_neg_rsp(struct ksmbd_work *work);
403 void smb2_set_err_rsp(struct ksmbd_work *work);
404 int smb2_check_user_session(struct ksmbd_work *work);
405 int smb2_get_ksmbd_tcon(struct ksmbd_work *work);
406 bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command);
407 int smb2_check_sign_req(struct ksmbd_work *work);
408 void smb2_set_sign_rsp(struct ksmbd_work *work);
409 int smb3_check_sign_req(struct ksmbd_work *work);
410 void smb3_set_sign_rsp(struct ksmbd_work *work);
411 int find_matching_smb2_dialect(int start_index, __le16 *cli_dialects,
412 			       __le16 dialects_count);
413 struct file_lock *smb_flock_init(struct file *f);
414 int setup_async_work(struct ksmbd_work *work, void (*fn)(void **),
415 		     void **arg);
416 void release_async_work(struct ksmbd_work *work);
417 void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status);
418 struct channel *lookup_chann_list(struct ksmbd_session *sess,
419 				  struct ksmbd_conn *conn);
420 void smb3_preauth_hash_rsp(struct ksmbd_work *work);
421 bool smb3_is_transform_hdr(void *buf);
422 int smb3_decrypt_req(struct ksmbd_work *work);
423 int smb3_encrypt_resp(struct ksmbd_work *work);
424 bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work);
425 int smb2_set_rsp_credits(struct ksmbd_work *work);
426 bool smb3_encryption_negotiated(struct ksmbd_conn *conn);
427 
428 /* smb2 misc functions */
429 int ksmbd_smb2_check_message(struct ksmbd_work *work);
430 void smb2_complete_request_open(struct ksmbd_work *work);
431 
432 /* smb2 command handlers */
433 int smb2_handle_negotiate(struct ksmbd_work *work);
434 int smb2_negotiate_request(struct ksmbd_work *work);
435 int smb2_sess_setup(struct ksmbd_work *work);
436 int smb2_tree_connect(struct ksmbd_work *work);
437 int smb2_tree_disconnect(struct ksmbd_work *work);
438 int smb2_session_logoff(struct ksmbd_work *work);
439 int smb2_open(struct ksmbd_work *work);
440 int smb2_query_info(struct ksmbd_work *work);
441 int smb2_query_dir(struct ksmbd_work *work);
442 int smb2_close(struct ksmbd_work *work);
443 int smb2_echo(struct ksmbd_work *work);
444 int smb2_set_info(struct ksmbd_work *work);
445 int smb2_read(struct ksmbd_work *work);
446 int smb2_write(struct ksmbd_work *work);
447 int smb2_flush(struct ksmbd_work *work);
448 int smb2_cancel(struct ksmbd_work *work);
449 int smb2_lock(struct ksmbd_work *work);
450 int smb2_ioctl(struct ksmbd_work *work);
451 int smb2_oplock_break(struct ksmbd_work *work);
452 int smb2_notify(struct ksmbd_work *ksmbd_work);
453 
454 #define POSIX_TYPE_FILE		0
455 #define POSIX_TYPE_DIR		1
456 #define POSIX_TYPE_SYMLINK	2
457 #define POSIX_TYPE_CHARDEV	3
458 #define POSIX_TYPE_BLKDEV	4
459 #define POSIX_TYPE_FIFO		5
460 #define POSIX_TYPE_SOCKET	6
461 
462 #define POSIX_FILETYPE_SHIFT	12
463 
464 #endif	/* _SMB2PDU_H */
465