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