xref: /linux/fs/smb/server/smb2pdu.h (revision 318cd7461a7facb9cd5f7a5d1580fd874440e035)
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.1.3.2.1
160  */
161 struct create_posix_rsp {
162 	struct create_context_hdr ccontext;
163 	__u8    Name[16];
164 	__le32 nlink;
165 	__le32 reparse_tag;
166 	__le32 mode;
167 	/* SidBuffer contain two sids(Domain sid(28), UNIX group sid(16)) */
168 	u8 SidBuffer[44];
169 } __packed;
170 
171 __le32 smb2_get_reparse_tag_special_file(umode_t mode);
172 
173 #define SMB2_0_IOCTL_IS_FSCTL 0x00000001
174 
175 struct sockaddr_storage_rsp {
176 	__le16 Family;
177 	union {
178 		struct smb_sockaddr_in addr4;
179 		struct smb_sockaddr_in6 addr6;
180 	};
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 file_sparse {
191 	__u8	SetSparse;
192 } __packed;
193 
194 /* FILE Info response size */
195 #define FILE_DIRECTORY_INFORMATION_SIZE       1
196 #define FILE_FULL_DIRECTORY_INFORMATION_SIZE  2
197 #define FILE_BOTH_DIRECTORY_INFORMATION_SIZE  3
198 #define FILE_BASIC_INFORMATION_SIZE           40
199 #define FILE_STANDARD_INFORMATION_SIZE        24
200 #define FILE_INTERNAL_INFORMATION_SIZE        8
201 #define FILE_EA_INFORMATION_SIZE              4
202 #define FILE_ACCESS_INFORMATION_SIZE          4
203 #define FILE_NAME_INFORMATION_SIZE            8
204 #define FILE_RENAME_INFORMATION_SIZE          10
205 #define FILE_LINK_INFORMATION_SIZE            11
206 #define FILE_NAMES_INFORMATION_SIZE           12
207 #define FILE_DISPOSITION_INFORMATION_SIZE     13
208 #define FILE_POSITION_INFORMATION_SIZE        14
209 #define FILE_FULL_EA_INFORMATION_SIZE         15
210 #define FILE_MODE_INFORMATION_SIZE            4
211 #define FILE_ALIGNMENT_INFORMATION_SIZE       4
212 #define FILE_ALL_INFORMATION_SIZE             104
213 #define FILE_ALLOCATION_INFORMATION_SIZE      19
214 #define FILE_END_OF_FILE_INFORMATION_SIZE     20
215 #define FILE_STREAM_INFORMATION_SIZE          32
216 #define FILE_PIPE_INFORMATION_SIZE            23
217 #define FILE_PIPE_LOCAL_INFORMATION_SIZE      24
218 #define FILE_PIPE_REMOTE_INFORMATION_SIZE     25
219 #define FILE_MAILSLOT_QUERY_INFORMATION_SIZE  26
220 #define FILE_MAILSLOT_SET_INFORMATION_SIZE    27
221 #define FILE_COMPRESSION_INFORMATION_SIZE     16
222 #define FILE_OBJECT_ID_INFORMATION_SIZE       29
223 /* Number 30 not defined in documents */
224 #define FILE_MOVE_CLUSTER_INFORMATION_SIZE    31
225 #define FILE_QUOTA_INFORMATION_SIZE           32
226 #define FILE_REPARSE_POINT_INFORMATION_SIZE   33
227 #define FILE_NETWORK_OPEN_INFORMATION_SIZE    56
228 #define FILE_ATTRIBUTE_TAG_INFORMATION_SIZE   8
229 
230 /* FS Info response  size */
231 #define FS_DEVICE_INFORMATION_SIZE     8
232 #define FS_ATTRIBUTE_INFORMATION_SIZE  16
233 #define FS_VOLUME_INFORMATION_SIZE     24
234 #define FS_SIZE_INFORMATION_SIZE       24
235 #define FS_FULL_SIZE_INFORMATION_SIZE  32
236 #define FS_SECTOR_SIZE_INFORMATION_SIZE 28
237 #define FS_OBJECT_ID_INFORMATION_SIZE 64
238 #define FS_CONTROL_INFORMATION_SIZE 48
239 #define FS_POSIX_INFORMATION_SIZE 56
240 
241 /* FS_ATTRIBUTE_File_System_Name */
242 #define FS_TYPE_SUPPORT_SIZE   44
243 struct fs_type_info {
244 	char		*fs_name;
245 	long		magic_number;
246 } __packed;
247 
248 /*
249  *	PDU query infolevel structure definitions
250  *	BB consider moving to a different header
251  */
252 
253 struct smb2_file_access_info {
254 	__le32 AccessFlags;
255 } __packed;
256 
257 struct smb2_file_alignment_info {
258 	__le32 AlignmentRequirement;
259 } __packed;
260 
261 struct smb2_file_name_info {
262 	__le32 FileNameLength;
263 	char FileName[];
264 } __packed;
265 
266 struct smb2_file_stream_info {
267 	__le32  NextEntryOffset;
268 	__le32  StreamNameLength;
269 	__le64 StreamSize;
270 	__le64 StreamAllocationSize;
271 	char   StreamName[];
272 } __packed;
273 
274 struct srv_snapshot_array {
275 	__le32 NumberOfSnapShots;
276 	__le32 NumberOfSnapShotsReturned;
277 	__le32 SnapShotArraySize;
278 	__le32 Reserved;
279 } __packed;
280 
281 struct smb2_file_standard_info {
282 	__le64 AllocationSize;
283 	__le64 EndOfFile;
284 	__le32 NumberOfLinks;	/* hard links */
285 	__u8   DeletePending;
286 	__u8   Directory;
287 	__le16 Reserved;
288 } __packed; /* level 18 Query */
289 
290 struct smb2_file_ea_info {
291 	__le32 EASize;
292 } __packed;
293 
294 struct smb2_file_disposition_info {
295 	__u8 DeletePending;
296 } __packed;
297 
298 struct smb2_file_pos_info {
299 	__le64 CurrentByteOffset;
300 } __packed;
301 
302 #define FILE_MODE_INFO_MASK cpu_to_le32(0x0000100e)
303 
304 struct smb2_file_mode_info {
305 	__le32 Mode;
306 } __packed;
307 
308 struct smb2_file_comp_info {
309 	__le64 CompressedFileSize;
310 	__le16 CompressionFormat;
311 	__u8 CompressionUnitShift;
312 	__u8 ChunkShift;
313 	__u8 ClusterShift;
314 	__u8 Reserved[3];
315 } __packed;
316 
317 struct smb2_file_attr_tag_info {
318 	__le32 FileAttributes;
319 	__le32 ReparseTag;
320 } __packed;
321 
322 #define SL_RESTART_SCAN	0x00000001
323 #define SL_RETURN_SINGLE_ENTRY	0x00000002
324 #define SL_INDEX_SPECIFIED	0x00000004
325 
326 struct smb2_ea_info_req {
327 	__le32 NextEntryOffset;
328 	__u8   EaNameLength;
329 	char name[];
330 } __packed; /* level 15 Query */
331 
332 struct smb2_ea_info {
333 	__le32 NextEntryOffset;
334 	__u8   Flags;
335 	__u8   EaNameLength;
336 	__le16 EaValueLength;
337 	char name[];
338 	/* optionally followed by value */
339 } __packed; /* level 15 Query */
340 
341 struct create_ea_buf_req {
342 	struct create_context_hdr ccontext;
343 	__u8   Name[8];
344 	struct smb2_ea_info ea;
345 } __packed;
346 
347 struct create_sd_buf_req {
348 	struct create_context_hdr ccontext;
349 	__u8   Name[8];
350 	struct smb_ntsd ntsd;
351 } __packed;
352 
353 /* See POSIX-FSCC 2.2.1 */
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 	/*
368 	 * Beginning of POSIX Create Context Response
369 	 * See POSIX-SMB2 2.1.3.2.1
370 	 */
371 	__le32 HardLinks;
372 	__le32 ReparseTag;
373 	__le32 Mode;
374 	/* SidBuffer contain two sids (UNIX user sid(16), UNIX group sid(16)) */
375 	u8 SidBuffer[32];
376 	/* End of POSIX Create Context Response */
377 	__le32 name_len;
378 	u8 name[];
379 } __packed;
380 
381 /* functions */
382 void init_smb2_1_server(struct ksmbd_conn *conn);
383 void init_smb3_0_server(struct ksmbd_conn *conn);
384 void init_smb3_02_server(struct ksmbd_conn *conn);
385 int init_smb3_11_server(struct ksmbd_conn *conn);
386 
387 void init_smb2_max_read_size(unsigned int sz);
388 void init_smb2_max_write_size(unsigned int sz);
389 void init_smb2_max_trans_size(unsigned int sz);
390 void init_smb2_max_credits(unsigned int sz);
391 
392 bool is_smb2_neg_cmd(struct ksmbd_work *work);
393 bool is_smb2_rsp(struct ksmbd_work *work);
394 
395 u16 get_smb2_cmd_val(struct ksmbd_work *work);
396 void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err);
397 int init_smb2_rsp_hdr(struct ksmbd_work *work);
398 bool smb2_check_compound_fid(struct ksmbd_work *work);
399 int smb2_allocate_rsp_buf(struct ksmbd_work *work);
400 bool is_chained_smb2_message(struct ksmbd_work *work);
401 int init_smb2_neg_rsp(struct ksmbd_work *work);
402 void smb2_set_err_rsp(struct ksmbd_work *work);
403 int smb2_check_user_session(struct ksmbd_work *work);
404 int smb2_get_ksmbd_tcon(struct ksmbd_work *work);
405 bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command);
406 int smb2_check_sign_req(struct ksmbd_work *work);
407 void smb2_set_sign_rsp(struct ksmbd_work *work);
408 int smb3_check_sign_req(struct ksmbd_work *work);
409 void smb3_set_sign_rsp(struct ksmbd_work *work);
410 int find_matching_smb2_dialect(int start_index, __le16 *cli_dialects,
411 			       __le16 dialects_count);
412 struct file_lock *smb_flock_init(struct file *f);
413 int setup_async_work(struct ksmbd_work *work, void (*fn)(void **),
414 		     void **arg);
415 void release_async_work(struct ksmbd_work *work);
416 void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status);
417 struct channel *lookup_chann_list(struct ksmbd_session *sess,
418 				  struct ksmbd_conn *conn);
419 void smb3_preauth_hash_rsp(struct ksmbd_work *work);
420 bool smb3_is_transform_hdr(void *buf);
421 int smb3_decrypt_req(struct ksmbd_work *work);
422 int ksmbd_check_transform_session(struct ksmbd_work *work, u64 tr_sess_id);
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