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