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