1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
4 */
5
6 #ifndef __SMB_COMMON_H__
7 #define __SMB_COMMON_H__
8
9 #include <linux/kernel.h>
10
11 #include "glob.h"
12 #include "nterr.h"
13 #include "../common/cifsglob.h"
14 #include "../common/smb2pdu.h"
15 #include "smb2pdu.h"
16
17 /* ksmbd's Specific ERRNO */
18 #define ESHARE 50000
19
20 #define SMB1_PROT 0
21 #define SMB2_PROT 1
22 #define SMB21_PROT 2
23 /* multi-protocol negotiate request */
24 #define SMB2X_PROT 3
25 #define SMB30_PROT 4
26 #define SMB302_PROT 5
27 #define SMB311_PROT 6
28 #define BAD_PROT 0xFFFF
29
30 #define SMB_ECHO_INTERVAL (60 * HZ)
31
32 #define MAX_CIFS_SMALL_BUFFER_SIZE 448 /* big enough for most */
33
34 #define MAX_STREAM_PROT_LEN 0x00FFFFFF
35
36 /* Responses when opening a file. */
37 #define F_SUPERSEDED 0
38 #define F_OPENED 1
39 #define F_CREATED 2
40 #define F_OVERWRITTEN 3
41
42 /*
43 * File Attribute flags
44 */
45 #define ATTR_POSIX_SEMANTICS 0x01000000
46 #define ATTR_BACKUP_SEMANTICS 0x02000000
47 #define ATTR_DELETE_ON_CLOSE 0x04000000
48 #define ATTR_SEQUENTIAL_SCAN 0x08000000
49 #define ATTR_RANDOM_ACCESS 0x10000000
50 #define ATTR_NO_BUFFERING 0x20000000
51 #define ATTR_WRITE_THROUGH 0x80000000
52
53 /* List of FileSystemAttributes - see 2.5.1 of MS-FSCC */
54 #define FILE_SUPPORTS_SPARSE_VDL 0x10000000 /* faster nonsparse extend */
55 #define FILE_SUPPORTS_BLOCK_REFCOUNTING 0x08000000 /* allow ioctl dup extents */
56 #define FILE_SUPPORT_INTEGRITY_STREAMS 0x04000000
57 #define FILE_SUPPORTS_USN_JOURNAL 0x02000000
58 #define FILE_SUPPORTS_OPEN_BY_FILE_ID 0x01000000
59 #define FILE_SUPPORTS_EXTENDED_ATTRIBUTES 0x00800000
60 #define FILE_SUPPORTS_HARD_LINKS 0x00400000
61 #define FILE_SUPPORTS_TRANSACTIONS 0x00200000
62 #define FILE_SEQUENTIAL_WRITE_ONCE 0x00100000
63 #define FILE_READ_ONLY_VOLUME 0x00080000
64 #define FILE_NAMED_STREAMS 0x00040000
65 #define FILE_SUPPORTS_ENCRYPTION 0x00020000
66 #define FILE_SUPPORTS_OBJECT_IDS 0x00010000
67 #define FILE_VOLUME_IS_COMPRESSED 0x00008000
68 #define FILE_SUPPORTS_POSIX_UNLINK_RENAME 0x00000400
69 #define FILE_RETURNS_CLEANUP_RESULT_INFO 0x00000200
70 #define FILE_SUPPORTS_REMOTE_STORAGE 0x00000100
71 #define FILE_SUPPORTS_REPARSE_POINTS 0x00000080
72 #define FILE_SUPPORTS_SPARSE_FILES 0x00000040
73 #define FILE_VOLUME_QUOTAS 0x00000020
74 #define FILE_FILE_COMPRESSION 0x00000010
75 #define FILE_PERSISTENT_ACLS 0x00000008
76 #define FILE_UNICODE_ON_DISK 0x00000004
77 #define FILE_CASE_PRESERVED_NAMES 0x00000002
78 #define FILE_CASE_SENSITIVE_SEARCH 0x00000001
79
80 #define FILE_READ_DATA 0x00000001 /* Data can be read from the file */
81 #define FILE_WRITE_DATA 0x00000002 /* Data can be written to the file */
82 #define FILE_APPEND_DATA 0x00000004 /* Data can be appended to the file */
83 #define FILE_READ_EA 0x00000008 /* Extended attributes associated */
84 /* with the file can be read */
85 #define FILE_WRITE_EA 0x00000010 /* Extended attributes associated */
86 /* with the file can be written */
87 #define FILE_EXECUTE 0x00000020 /*Data can be read into memory from */
88 /* the file using system paging I/O */
89 #define FILE_DELETE_CHILD 0x00000040
90 #define FILE_READ_ATTRIBUTES 0x00000080 /* Attributes associated with the */
91 /* file can be read */
92 #define FILE_WRITE_ATTRIBUTES 0x00000100 /* Attributes associated with the */
93 /* file can be written */
94 #define DELETE 0x00010000 /* The file can be deleted */
95 #define READ_CONTROL 0x00020000 /* The access control list and */
96 /* ownership associated with the */
97 /* file can be read */
98 #define WRITE_DAC 0x00040000 /* The access control list and */
99 /* ownership associated with the */
100 /* file can be written. */
101 #define WRITE_OWNER 0x00080000 /* Ownership information associated */
102 /* with the file can be written */
103 #define SYNCHRONIZE 0x00100000 /* The file handle can waited on to */
104 /* synchronize with the completion */
105 /* of an input/output request */
106 #define GENERIC_ALL 0x10000000
107 #define GENERIC_EXECUTE 0x20000000
108 #define GENERIC_WRITE 0x40000000
109 #define GENERIC_READ 0x80000000
110 /* In summary - Relevant file */
111 /* access flags from CIFS are */
112 /* file_read_data, file_write_data */
113 /* file_execute, file_read_attributes*/
114 /* write_dac, and delete. */
115
116 #define SET_FILE_READ_RIGHTS (FILE_READ_DATA | FILE_READ_EA \
117 | FILE_READ_ATTRIBUTES \
118 | DELETE | READ_CONTROL | WRITE_DAC \
119 | WRITE_OWNER | SYNCHRONIZE)
120 #define SET_FILE_WRITE_RIGHTS (FILE_WRITE_DATA | FILE_APPEND_DATA \
121 | FILE_WRITE_EA \
122 | FILE_DELETE_CHILD \
123 | FILE_WRITE_ATTRIBUTES \
124 | DELETE | READ_CONTROL | WRITE_DAC \
125 | WRITE_OWNER | SYNCHRONIZE)
126 #define SET_FILE_EXEC_RIGHTS (FILE_READ_EA | FILE_WRITE_EA | FILE_EXECUTE \
127 | FILE_READ_ATTRIBUTES \
128 | FILE_WRITE_ATTRIBUTES \
129 | DELETE | READ_CONTROL | WRITE_DAC \
130 | WRITE_OWNER | SYNCHRONIZE)
131
132 #define SET_MINIMUM_RIGHTS (FILE_READ_EA | FILE_READ_ATTRIBUTES \
133 | READ_CONTROL | SYNCHRONIZE)
134
135 /* generic flags for file open */
136 #define GENERIC_READ_FLAGS (READ_CONTROL | FILE_READ_DATA | \
137 FILE_READ_ATTRIBUTES | \
138 FILE_READ_EA | SYNCHRONIZE)
139
140 #define GENERIC_WRITE_FLAGS (READ_CONTROL | FILE_WRITE_DATA | \
141 FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | \
142 FILE_APPEND_DATA | SYNCHRONIZE)
143
144 #define GENERIC_EXECUTE_FLAGS (READ_CONTROL | FILE_EXECUTE | \
145 FILE_READ_ATTRIBUTES | SYNCHRONIZE)
146
147 #define GENERIC_ALL_FLAGS (DELETE | READ_CONTROL | WRITE_DAC | \
148 WRITE_OWNER | SYNCHRONIZE | FILE_READ_DATA | \
149 FILE_WRITE_DATA | FILE_APPEND_DATA | \
150 FILE_READ_EA | FILE_WRITE_EA | \
151 FILE_EXECUTE | FILE_DELETE_CHILD | \
152 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES)
153
154 #define SMB1_PROTO_NUMBER cpu_to_le32(0x424d53ff)
155 #define SMB_COM_NEGOTIATE 0x72
156 #define SMB1_CLIENT_GUID_SIZE (16)
157
158 #define SMBFLG_RESPONSE 0x80 /* this PDU is a response from server */
159
160 #define SMBFLG2_IS_LONG_NAME cpu_to_le16(0x40)
161 #define SMBFLG2_EXT_SEC cpu_to_le16(0x800)
162 #define SMBFLG2_ERR_STATUS cpu_to_le16(0x4000)
163 #define SMBFLG2_UNICODE cpu_to_le16(0x8000)
164
165 struct smb_hdr {
166 __be32 smb_buf_length;
167 __u8 Protocol[4];
168 __u8 Command;
169 union {
170 struct {
171 __u8 ErrorClass;
172 __u8 Reserved;
173 __le16 Error;
174 } __packed DosError;
175 __le32 CifsError;
176 } __packed Status;
177 __u8 Flags;
178 __le16 Flags2; /* note: le */
179 __le16 PidHigh;
180 union {
181 struct {
182 __le32 SequenceNumber; /* le */
183 __u32 Reserved; /* zero */
184 } __packed Sequence;
185 __u8 SecuritySignature[8]; /* le */
186 } __packed Signature;
187 __u8 pad[2];
188 __le16 Tid;
189 __le16 Pid;
190 __le16 Uid;
191 __le16 Mid;
192 __u8 WordCount;
193 } __packed;
194
195 struct smb_negotiate_req {
196 struct smb_hdr hdr; /* wct = 0 */
197 __le16 ByteCount;
198 unsigned char DialectsArray[];
199 } __packed;
200
201 struct smb_negotiate_rsp {
202 struct smb_hdr hdr; /* wct = 17 */
203 __le16 DialectIndex; /* 0xFFFF = no dialect acceptable */
204 __le16 ByteCount;
205 } __packed;
206
207 struct filesystem_attribute_info {
208 __le32 Attributes;
209 __le32 MaxPathNameComponentLength;
210 __le32 FileSystemNameLen;
211 __le16 FileSystemName[]; /* do not have to save this - get subset? */
212 } __packed;
213
214 struct filesystem_device_info {
215 __le32 DeviceType;
216 __le32 DeviceCharacteristics;
217 } __packed; /* device info level 0x104 */
218
219 struct filesystem_vol_info {
220 __le64 VolumeCreationTime;
221 __le32 SerialNumber;
222 __le32 VolumeLabelSize;
223 __le16 Reserved;
224 __le16 VolumeLabel[];
225 } __packed;
226
227 struct filesystem_info {
228 __le64 TotalAllocationUnits;
229 __le64 FreeAllocationUnits;
230 __le32 SectorsPerAllocationUnit;
231 __le32 BytesPerSector;
232 } __packed; /* size info, level 0x103 */
233
234 #define EXTENDED_INFO_MAGIC 0x43667364 /* Cfsd */
235 #define STRING_LENGTH 28
236
237 struct fs_extended_info {
238 __le32 magic;
239 __le32 version;
240 __le32 release;
241 __u64 rel_date;
242 char version_string[STRING_LENGTH];
243 } __packed;
244
245 struct object_id_info {
246 char objid[16];
247 struct fs_extended_info extended_info;
248 } __packed;
249
250 struct file_directory_info {
251 __le32 NextEntryOffset;
252 __u32 FileIndex;
253 __le64 CreationTime;
254 __le64 LastAccessTime;
255 __le64 LastWriteTime;
256 __le64 ChangeTime;
257 __le64 EndOfFile;
258 __le64 AllocationSize;
259 __le32 ExtFileAttributes;
260 __le32 FileNameLength;
261 char FileName[];
262 } __packed; /* level 0x101 FF resp data */
263
264 struct file_names_info {
265 __le32 NextEntryOffset;
266 __u32 FileIndex;
267 __le32 FileNameLength;
268 char FileName[];
269 } __packed; /* level 0xc FF resp data */
270
271 struct file_full_directory_info {
272 __le32 NextEntryOffset;
273 __u32 FileIndex;
274 __le64 CreationTime;
275 __le64 LastAccessTime;
276 __le64 LastWriteTime;
277 __le64 ChangeTime;
278 __le64 EndOfFile;
279 __le64 AllocationSize;
280 __le32 ExtFileAttributes;
281 __le32 FileNameLength;
282 __le32 EaSize;
283 char FileName[];
284 } __packed; /* level 0x102 FF resp */
285
286 struct file_both_directory_info {
287 __le32 NextEntryOffset;
288 __u32 FileIndex;
289 __le64 CreationTime;
290 __le64 LastAccessTime;
291 __le64 LastWriteTime;
292 __le64 ChangeTime;
293 __le64 EndOfFile;
294 __le64 AllocationSize;
295 __le32 ExtFileAttributes;
296 __le32 FileNameLength;
297 __le32 EaSize; /* length of the xattrs */
298 __u8 ShortNameLength;
299 __u8 Reserved;
300 __u8 ShortName[24];
301 char FileName[];
302 } __packed; /* level 0x104 FFrsp data */
303
304 struct file_id_both_directory_info {
305 __le32 NextEntryOffset;
306 __u32 FileIndex;
307 __le64 CreationTime;
308 __le64 LastAccessTime;
309 __le64 LastWriteTime;
310 __le64 ChangeTime;
311 __le64 EndOfFile;
312 __le64 AllocationSize;
313 __le32 ExtFileAttributes;
314 __le32 FileNameLength;
315 __le32 EaSize; /* length of the xattrs */
316 __u8 ShortNameLength;
317 __u8 Reserved;
318 __u8 ShortName[24];
319 __le16 Reserved2;
320 __le64 UniqueId;
321 char FileName[];
322 } __packed;
323
324 struct file_id_full_dir_info {
325 __le32 NextEntryOffset;
326 __u32 FileIndex;
327 __le64 CreationTime;
328 __le64 LastAccessTime;
329 __le64 LastWriteTime;
330 __le64 ChangeTime;
331 __le64 EndOfFile;
332 __le64 AllocationSize;
333 __le32 ExtFileAttributes;
334 __le32 FileNameLength;
335 __le32 EaSize; /* EA size */
336 __le32 Reserved;
337 __le64 UniqueId; /* inode num - le since Samba puts ino in low 32 bit*/
338 char FileName[];
339 } __packed; /* level 0x105 FF rsp data */
340
341 struct smb_version_values {
342 char *version_string;
343 __u16 protocol_id;
344 __le16 lock_cmd;
345 __u32 capabilities;
346 __u32 max_read_size;
347 __u32 max_write_size;
348 __u32 max_trans_size;
349 __u32 max_credits;
350 __u32 large_lock_type;
351 __u32 exclusive_lock_type;
352 __u32 shared_lock_type;
353 __u32 unlock_lock_type;
354 size_t header_size;
355 size_t max_header_size;
356 size_t read_rsp_size;
357 unsigned int cap_unix;
358 unsigned int cap_nt_find;
359 unsigned int cap_large_files;
360 __u16 signing_enabled;
361 __u16 signing_required;
362 size_t create_lease_size;
363 size_t create_durable_size;
364 size_t create_durable_v2_size;
365 size_t create_mxac_size;
366 size_t create_disk_id_size;
367 size_t create_posix_size;
368 };
369
370 struct filesystem_posix_info {
371 /* For undefined recommended transfer size return -1 in that field */
372 __le32 OptimalTransferSize; /* bsize on some os, iosize on other os */
373 __le32 BlockSize;
374 /* The next three fields are in terms of the block size.
375 * (above). If block size is unknown, 4096 would be a
376 * reasonable block size for a server to report.
377 * Note that returning the blocks/blocksavail removes need
378 * to make a second call (to QFSInfo level 0x103 to get this info.
379 * UserBlockAvail is typically less than or equal to BlocksAvail,
380 * if no distinction is made return the same value in each
381 */
382 __le64 TotalBlocks;
383 __le64 BlocksAvail; /* bfree */
384 __le64 UserBlocksAvail; /* bavail */
385 /* For undefined Node fields or FSID return -1 */
386 __le64 TotalFileNodes;
387 __le64 FreeFileNodes;
388 __le64 FileSysIdentifier; /* fsid */
389 /* NB Namelen comes from FILE_SYSTEM_ATTRIBUTE_INFO call */
390 /* NB flags can come from FILE_SYSTEM_DEVICE_INFO call */
391 } __packed;
392
393 struct smb_version_ops {
394 u16 (*get_cmd_val)(struct ksmbd_work *swork);
395 int (*init_rsp_hdr)(struct ksmbd_work *swork);
396 void (*set_rsp_status)(struct ksmbd_work *swork, __le32 err);
397 int (*allocate_rsp_buf)(struct ksmbd_work *work);
398 int (*set_rsp_credits)(struct ksmbd_work *work);
399 int (*check_user_session)(struct ksmbd_work *work);
400 int (*get_ksmbd_tcon)(struct ksmbd_work *work);
401 bool (*is_sign_req)(struct ksmbd_work *work, unsigned int command);
402 int (*check_sign_req)(struct ksmbd_work *work);
403 void (*set_sign_rsp)(struct ksmbd_work *work);
404 int (*generate_signingkey)(struct ksmbd_session *sess, struct ksmbd_conn *conn);
405 int (*generate_encryptionkey)(struct ksmbd_conn *conn, struct ksmbd_session *sess);
406 bool (*is_transform_hdr)(void *buf);
407 int (*decrypt_req)(struct ksmbd_work *work);
408 int (*encrypt_resp)(struct ksmbd_work *work);
409 };
410
411 struct smb_version_cmds {
412 int (*proc)(struct ksmbd_work *swork);
413 };
414
415 int ksmbd_min_protocol(void);
416 int ksmbd_max_protocol(void);
417
418 int ksmbd_lookup_protocol_idx(char *str);
419
420 int ksmbd_verify_smb_message(struct ksmbd_work *work);
421 bool ksmbd_smb_request(struct ksmbd_conn *conn);
422
423 int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count);
424
425 int ksmbd_init_smb_server(struct ksmbd_conn *conn);
426
427 struct ksmbd_kstat;
428 int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work,
429 int info_level,
430 struct ksmbd_file *dir,
431 struct ksmbd_dir_info *d_info,
432 char *search_pattern,
433 int (*fn)(struct ksmbd_conn *,
434 int,
435 struct ksmbd_dir_info *,
436 struct ksmbd_kstat *));
437
438 int ksmbd_extract_shortname(struct ksmbd_conn *conn,
439 const char *longname,
440 char *shortname);
441
442 int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command);
443
444 int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp);
445 int __ksmbd_override_fsids(struct ksmbd_work *work,
446 struct ksmbd_share_config *share);
447 int ksmbd_override_fsids(struct ksmbd_work *work);
448 void ksmbd_revert_fsids(struct ksmbd_work *work);
449
450 unsigned int ksmbd_server_side_copy_max_chunk_count(void);
451 unsigned int ksmbd_server_side_copy_max_chunk_size(void);
452 unsigned int ksmbd_server_side_copy_max_total_size(void);
453 bool is_asterisk(char *p);
454 __le32 smb_map_generic_desired_access(__le32 daccess);
455
get_rfc1002_len(void * buf)456 static inline unsigned int get_rfc1002_len(void *buf)
457 {
458 return be32_to_cpu(*((__be32 *)buf)) & 0xffffff;
459 }
460 #endif /* __SMB_COMMON_H__ */
461