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