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