1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
6 #ifndef __SMB_COMMON_H__
7 #define __SMB_COMMON_H__
9 #include <linux/kernel.h>
13 #include "../common/smb2pdu.h"
16 /* ksmbd's Specific ERRNO */
22 /* multi-protocol negotiate request */
27 #define BAD_PROT 0xFFFF
29 #define SMB1_VERSION_STRING "1.0"
30 #define SMB20_VERSION_STRING "2.0"
31 #define SMB21_VERSION_STRING "2.1"
32 #define SMB30_VERSION_STRING "3.0"
33 #define SMB302_VERSION_STRING "3.02"
34 #define SMB311_VERSION_STRING "3.1.1"
36 #define SMB_ECHO_INTERVAL (60 * HZ)
38 #define CIFS_DEFAULT_IOSIZE (64 * 1024)
39 #define MAX_CIFS_SMALL_BUFFER_SIZE 448 /* big enough for most */
41 #define MAX_STREAM_PROT_LEN 0x00FFFFFF
43 /* Responses when opening a file. */
44 #define F_SUPERSEDED 0
47 #define F_OVERWRITTEN 3
50 * File Attribute flags
52 #define ATTR_POSIX_SEMANTICS 0x01000000
53 #define ATTR_BACKUP_SEMANTICS 0x02000000
54 #define ATTR_DELETE_ON_CLOSE 0x04000000
55 #define ATTR_SEQUENTIAL_SCAN 0x08000000
56 #define ATTR_RANDOM_ACCESS 0x10000000
57 #define ATTR_NO_BUFFERING 0x20000000
58 #define ATTR_WRITE_THROUGH 0x80000000
60 /* List of FileSystemAttributes - see 2.5.1 of MS-FSCC */
61 #define FILE_SUPPORTS_SPARSE_VDL 0x10000000 /* faster nonsparse extend */
62 #define FILE_SUPPORTS_BLOCK_REFCOUNTING 0x08000000 /* allow ioctl dup extents */
63 #define FILE_SUPPORT_INTEGRITY_STREAMS 0x04000000
64 #define FILE_SUPPORTS_USN_JOURNAL 0x02000000
65 #define FILE_SUPPORTS_OPEN_BY_FILE_ID 0x01000000
66 #define FILE_SUPPORTS_EXTENDED_ATTRIBUTES 0x00800000
67 #define FILE_SUPPORTS_HARD_LINKS 0x00400000
68 #define FILE_SUPPORTS_TRANSACTIONS 0x00200000
69 #define FILE_SEQUENTIAL_WRITE_ONCE 0x00100000
70 #define FILE_READ_ONLY_VOLUME 0x00080000
71 #define FILE_NAMED_STREAMS 0x00040000
72 #define FILE_SUPPORTS_ENCRYPTION 0x00020000
73 #define FILE_SUPPORTS_OBJECT_IDS 0x00010000
74 #define FILE_VOLUME_IS_COMPRESSED 0x00008000
75 #define FILE_SUPPORTS_REMOTE_STORAGE 0x00000100
76 #define FILE_SUPPORTS_REPARSE_POINTS 0x00000080
77 #define FILE_SUPPORTS_SPARSE_FILES 0x00000040
78 #define FILE_VOLUME_QUOTAS 0x00000020
79 #define FILE_FILE_COMPRESSION 0x00000010
80 #define FILE_PERSISTENT_ACLS 0x00000008
81 #define FILE_UNICODE_ON_DISK 0x00000004
82 #define FILE_CASE_PRESERVED_NAMES 0x00000002
83 #define FILE_CASE_SENSITIVE_SEARCH 0x00000001
85 #define FILE_READ_DATA 0x00000001 /* Data can be read from the file */
86 #define FILE_WRITE_DATA 0x00000002 /* Data can be written to the file */
87 #define FILE_APPEND_DATA 0x00000004 /* Data can be appended to the file */
88 #define FILE_READ_EA 0x00000008 /* Extended attributes associated */
89 /* with the file can be read */
90 #define FILE_WRITE_EA 0x00000010 /* Extended attributes associated */
91 /* with the file can be written */
92 #define FILE_EXECUTE 0x00000020 /*Data can be read into memory from */
93 /* the file using system paging I/O */
94 #define FILE_DELETE_CHILD 0x00000040
95 #define FILE_READ_ATTRIBUTES 0x00000080 /* Attributes associated with the */
96 /* file can be read */
97 #define FILE_WRITE_ATTRIBUTES 0x00000100 /* Attributes associated with the */
98 /* file can be written */
99 #define DELETE 0x00010000 /* The file can be deleted */
100 #define READ_CONTROL 0x00020000 /* The access control list and */
101 /* ownership associated with the */
102 /* file can be read */
103 #define WRITE_DAC 0x00040000 /* The access control list and */
104 /* ownership associated with the */
105 /* file can be written. */
106 #define WRITE_OWNER 0x00080000 /* Ownership information associated */
107 /* with the file can be written */
108 #define SYNCHRONIZE 0x00100000 /* The file handle can waited on to */
109 /* synchronize with the completion */
110 /* of an input/output request */
111 #define GENERIC_ALL 0x10000000
112 #define GENERIC_EXECUTE 0x20000000
113 #define GENERIC_WRITE 0x40000000
114 #define GENERIC_READ 0x80000000
115 /* In summary - Relevant file */
116 /* access flags from CIFS are */
117 /* file_read_data, file_write_data */
118 /* file_execute, file_read_attributes*/
119 /* write_dac, and delete. */
121 #define SET_FILE_READ_RIGHTS (FILE_READ_DATA | FILE_READ_EA \
122 | FILE_READ_ATTRIBUTES \
123 | DELETE | READ_CONTROL | WRITE_DAC \
124 | WRITE_OWNER | SYNCHRONIZE)
125 #define SET_FILE_WRITE_RIGHTS (FILE_WRITE_DATA | FILE_APPEND_DATA \
127 | FILE_DELETE_CHILD \
128 | FILE_WRITE_ATTRIBUTES \
129 | DELETE | READ_CONTROL | WRITE_DAC \
130 | WRITE_OWNER | SYNCHRONIZE)
131 #define SET_FILE_EXEC_RIGHTS (FILE_READ_EA | FILE_WRITE_EA | FILE_EXECUTE \
132 | FILE_READ_ATTRIBUTES \
133 | FILE_WRITE_ATTRIBUTES \
134 | DELETE | READ_CONTROL | WRITE_DAC \
135 | WRITE_OWNER | SYNCHRONIZE)
137 #define SET_MINIMUM_RIGHTS (FILE_READ_EA | FILE_READ_ATTRIBUTES \
138 | READ_CONTROL | SYNCHRONIZE)
140 /* generic flags for file open */
141 #define GENERIC_READ_FLAGS (READ_CONTROL | FILE_READ_DATA | \
142 FILE_READ_ATTRIBUTES | \
143 FILE_READ_EA | SYNCHRONIZE)
145 #define GENERIC_WRITE_FLAGS (READ_CONTROL | FILE_WRITE_DATA | \
146 FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | \
147 FILE_APPEND_DATA | SYNCHRONIZE)
149 #define GENERIC_EXECUTE_FLAGS (READ_CONTROL | FILE_EXECUTE | \
150 FILE_READ_ATTRIBUTES | SYNCHRONIZE)
152 #define GENERIC_ALL_FLAGS (DELETE | READ_CONTROL | WRITE_DAC | \
153 WRITE_OWNER | SYNCHRONIZE | FILE_READ_DATA | \
154 FILE_WRITE_DATA | FILE_APPEND_DATA | \
155 FILE_READ_EA | FILE_WRITE_EA | \
156 FILE_EXECUTE | FILE_DELETE_CHILD | \
157 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES)
159 #define SMB1_PROTO_NUMBER cpu_to_le32(0x424d53ff)
160 #define SMB_COM_NEGOTIATE 0x72
161 #define SMB1_CLIENT_GUID_SIZE (16)
163 #define SMBFLG_RESPONSE 0x80 /* this PDU is a response from server */
165 #define SMBFLG2_IS_LONG_NAME cpu_to_le16(0x40)
166 #define SMBFLG2_EXT_SEC cpu_to_le16(0x800)
167 #define SMBFLG2_ERR_STATUS cpu_to_le16(0x4000)
168 #define SMBFLG2_UNICODE cpu_to_le16(0x8000)
171 __be32 smb_buf_length;
183 __le16 Flags2; /* note: le */
187 __le32 SequenceNumber; /* le */
188 __u32 Reserved; /* zero */
190 __u8 SecuritySignature[8]; /* le */
191 } __packed Signature;
200 struct smb_negotiate_req {
201 struct smb_hdr hdr; /* wct = 0 */
203 unsigned char DialectsArray[];
206 struct smb_negotiate_rsp {
207 struct smb_hdr hdr; /* wct = 17 */
208 __le16 DialectIndex; /* 0xFFFF = no dialect acceptable */
212 struct filesystem_attribute_info {
214 __le32 MaxPathNameComponentLength;
215 __le32 FileSystemNameLen;
216 __le16 FileSystemName[1]; /* do not have to save this - get subset? */
219 struct filesystem_device_info {
221 __le32 DeviceCharacteristics;
222 } __packed; /* device info level 0x104 */
224 struct filesystem_vol_info {
225 __le64 VolumeCreationTime;
227 __le32 VolumeLabelSize;
229 __le16 VolumeLabel[1];
232 struct filesystem_info {
233 __le64 TotalAllocationUnits;
234 __le64 FreeAllocationUnits;
235 __le32 SectorsPerAllocationUnit;
236 __le32 BytesPerSector;
237 } __packed; /* size info, level 0x103 */
239 #define EXTENDED_INFO_MAGIC 0x43667364 /* Cfsd */
240 #define STRING_LENGTH 28
242 struct fs_extended_info {
247 char version_string[STRING_LENGTH];
250 struct object_id_info {
252 struct fs_extended_info extended_info;
255 struct file_directory_info {
256 __le32 NextEntryOffset;
259 __le64 LastAccessTime;
260 __le64 LastWriteTime;
263 __le64 AllocationSize;
264 __le32 ExtFileAttributes;
265 __le32 FileNameLength;
267 } __packed; /* level 0x101 FF resp data */
269 struct file_names_info {
270 __le32 NextEntryOffset;
272 __le32 FileNameLength;
274 } __packed; /* level 0xc FF resp data */
276 struct file_full_directory_info {
277 __le32 NextEntryOffset;
280 __le64 LastAccessTime;
281 __le64 LastWriteTime;
284 __le64 AllocationSize;
285 __le32 ExtFileAttributes;
286 __le32 FileNameLength;
289 } __packed; /* level 0x102 FF resp */
291 struct file_both_directory_info {
292 __le32 NextEntryOffset;
295 __le64 LastAccessTime;
296 __le64 LastWriteTime;
299 __le64 AllocationSize;
300 __le32 ExtFileAttributes;
301 __le32 FileNameLength;
302 __le32 EaSize; /* length of the xattrs */
303 __u8 ShortNameLength;
307 } __packed; /* level 0x104 FFrsp data */
309 struct file_id_both_directory_info {
310 __le32 NextEntryOffset;
313 __le64 LastAccessTime;
314 __le64 LastWriteTime;
317 __le64 AllocationSize;
318 __le32 ExtFileAttributes;
319 __le32 FileNameLength;
320 __le32 EaSize; /* length of the xattrs */
321 __u8 ShortNameLength;
329 struct file_id_full_dir_info {
330 __le32 NextEntryOffset;
333 __le64 LastAccessTime;
334 __le64 LastWriteTime;
337 __le64 AllocationSize;
338 __le32 ExtFileAttributes;
339 __le32 FileNameLength;
340 __le32 EaSize; /* EA size */
342 __le64 UniqueId; /* inode num - le since Samba puts ino in low 32 bit*/
344 } __packed; /* level 0x105 FF rsp data */
346 struct smb_version_values {
347 char *version_string;
352 __u32 max_write_size;
353 __u32 max_trans_size;
355 __u32 large_lock_type;
356 __u32 exclusive_lock_type;
357 __u32 shared_lock_type;
358 __u32 unlock_lock_type;
360 size_t max_header_size;
361 size_t read_rsp_size;
362 unsigned int cap_unix;
363 unsigned int cap_nt_find;
364 unsigned int cap_large_files;
365 __u16 signing_enabled;
366 __u16 signing_required;
367 size_t create_lease_size;
368 size_t create_durable_size;
369 size_t create_durable_v2_size;
370 size_t create_mxac_size;
371 size_t create_disk_id_size;
372 size_t create_posix_size;
375 struct filesystem_posix_info {
376 /* For undefined recommended transfer size return -1 in that field */
377 __le32 OptimalTransferSize; /* bsize on some os, iosize on other os */
379 /* The next three fields are in terms of the block size.
380 * (above). If block size is unknown, 4096 would be a
381 * reasonable block size for a server to report.
382 * Note that returning the blocks/blocksavail removes need
383 * to make a second call (to QFSInfo level 0x103 to get this info.
384 * UserBlockAvail is typically less than or equal to BlocksAvail,
385 * if no distinction is made return the same value in each
388 __le64 BlocksAvail; /* bfree */
389 __le64 UserBlocksAvail; /* bavail */
390 /* For undefined Node fields or FSID return -1 */
391 __le64 TotalFileNodes;
392 __le64 FreeFileNodes;
393 __le64 FileSysIdentifier; /* fsid */
394 /* NB Namelen comes from FILE_SYSTEM_ATTRIBUTE_INFO call */
395 /* NB flags can come from FILE_SYSTEM_DEVICE_INFO call */
398 struct smb_version_ops {
399 u16 (*get_cmd_val)(struct ksmbd_work *swork);
400 int (*init_rsp_hdr)(struct ksmbd_work *swork);
401 void (*set_rsp_status)(struct ksmbd_work *swork, __le32 err);
402 int (*allocate_rsp_buf)(struct ksmbd_work *work);
403 int (*set_rsp_credits)(struct ksmbd_work *work);
404 int (*check_user_session)(struct ksmbd_work *work);
405 int (*get_ksmbd_tcon)(struct ksmbd_work *work);
406 bool (*is_sign_req)(struct ksmbd_work *work, unsigned int command);
407 int (*check_sign_req)(struct ksmbd_work *work);
408 void (*set_sign_rsp)(struct ksmbd_work *work);
409 int (*generate_signingkey)(struct ksmbd_session *sess, struct ksmbd_conn *conn);
410 int (*generate_encryptionkey)(struct ksmbd_conn *conn, struct ksmbd_session *sess);
411 bool (*is_transform_hdr)(void *buf);
412 int (*decrypt_req)(struct ksmbd_work *work);
413 int (*encrypt_resp)(struct ksmbd_work *work);
416 struct smb_version_cmds {
417 int (*proc)(struct ksmbd_work *swork);
420 int ksmbd_min_protocol(void);
421 int ksmbd_max_protocol(void);
423 int ksmbd_lookup_protocol_idx(char *str);
425 int ksmbd_verify_smb_message(struct ksmbd_work *work);
426 bool ksmbd_smb_request(struct ksmbd_conn *conn);
428 int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count);
430 int ksmbd_init_smb_server(struct ksmbd_work *work);
433 int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work,
435 struct ksmbd_file *dir,
436 struct ksmbd_dir_info *d_info,
437 char *search_pattern,
438 int (*fn)(struct ksmbd_conn *,
440 struct ksmbd_dir_info *,
441 struct ksmbd_kstat *));
443 int ksmbd_extract_shortname(struct ksmbd_conn *conn,
444 const char *longname,
447 int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command);
449 int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp);
450 int ksmbd_override_fsids(struct ksmbd_work *work);
451 void ksmbd_revert_fsids(struct ksmbd_work *work);
453 unsigned int ksmbd_server_side_copy_max_chunk_count(void);
454 unsigned int ksmbd_server_side_copy_max_chunk_size(void);
455 unsigned int ksmbd_server_side_copy_max_total_size(void);
456 bool is_asterisk(char *p);
457 __le32 smb_map_generic_desired_access(__le32 daccess);
459 static inline unsigned int get_rfc1002_len(void *buf)
461 return be32_to_cpu(*((__be32 *)buf)) & 0xffffff;
464 static inline void inc_rfc1001_len(void *buf, int count)
466 be32_add_cpu((__be32 *)buf, count);
468 #endif /* __SMB_COMMON_H__ */