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 "../smbfs_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_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
84
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. */
120
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 \
126 | FILE_WRITE_EA \
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)
136
137 #define SET_MINIMUM_RIGHTS (FILE_READ_EA | FILE_READ_ATTRIBUTES \
138 | READ_CONTROL | SYNCHRONIZE)
139
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)
144
145 #define GENERIC_WRITE_FLAGS (READ_CONTROL | FILE_WRITE_DATA | \
146 FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | \
147 FILE_APPEND_DATA | SYNCHRONIZE)
148
149 #define GENERIC_EXECUTE_FLAGS (READ_CONTROL | FILE_EXECUTE | \
150 FILE_READ_ATTRIBUTES | SYNCHRONIZE)
151
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)
158
159 #define SMB1_PROTO_NUMBER cpu_to_le32(0x424d53ff)
160 #define SMB_COM_NEGOTIATE 0x72
161
162 #define SMB1_CLIENT_GUID_SIZE (16)
163 struct smb_hdr {
164 __be32 smb_buf_length;
165 __u8 Protocol[4];
166 __u8 Command;
167 union {
168 struct {
169 __u8 ErrorClass;
170 __u8 Reserved;
171 __le16 Error;
172 } __packed DosError;
173 __le32 CifsError;
174 } __packed Status;
175 __u8 Flags;
176 __le16 Flags2; /* note: le */
177 __le16 PidHigh;
178 union {
179 struct {
180 __le32 SequenceNumber; /* le */
181 __u32 Reserved; /* zero */
182 } __packed Sequence;
183 __u8 SecuritySignature[8]; /* le */
184 } __packed Signature;
185 __u8 pad[2];
186 __le16 Tid;
187 __le16 Pid;
188 __le16 Uid;
189 __le16 Mid;
190 __u8 WordCount;
191 } __packed;
192
193 struct smb_negotiate_req {
194 struct smb_hdr hdr; /* wct = 0 */
195 __le16 ByteCount;
196 unsigned char DialectsArray[1];
197 } __packed;
198
199 struct smb_negotiate_rsp {
200 struct smb_hdr hdr; /* wct = 17 */
201 __le16 DialectIndex; /* 0xFFFF = no dialect acceptable */
202 __u8 SecurityMode;
203 __le16 MaxMpxCount;
204 __le16 MaxNumberVcs;
205 __le32 MaxBufferSize;
206 __le32 MaxRawSize;
207 __le32 SessionKey;
208 __le32 Capabilities; /* see below */
209 __le32 SystemTimeLow;
210 __le32 SystemTimeHigh;
211 __le16 ServerTimeZone;
212 __u8 EncryptionKeyLength;
213 __le16 ByteCount;
214 union {
215 unsigned char EncryptionKey[8]; /* cap extended security off */
216 /* followed by Domain name - if extended security is off */
217 /* followed by 16 bytes of server GUID */
218 /* then security blob if cap_extended_security negotiated */
219 struct {
220 unsigned char GUID[SMB1_CLIENT_GUID_SIZE];
221 unsigned char SecurityBlob[1];
222 } __packed extended_response;
223 } __packed u;
224 } __packed;
225
226 struct filesystem_attribute_info {
227 __le32 Attributes;
228 __le32 MaxPathNameComponentLength;
229 __le32 FileSystemNameLen;
230 __le16 FileSystemName[1]; /* do not have to save this - get subset? */
231 } __packed;
232
233 struct filesystem_device_info {
234 __le32 DeviceType;
235 __le32 DeviceCharacteristics;
236 } __packed; /* device info level 0x104 */
237
238 struct filesystem_vol_info {
239 __le64 VolumeCreationTime;
240 __le32 SerialNumber;
241 __le32 VolumeLabelSize;
242 __le16 Reserved;
243 __le16 VolumeLabel[1];
244 } __packed;
245
246 struct filesystem_info {
247 __le64 TotalAllocationUnits;
248 __le64 FreeAllocationUnits;
249 __le32 SectorsPerAllocationUnit;
250 __le32 BytesPerSector;
251 } __packed; /* size info, level 0x103 */
252
253 #define EXTENDED_INFO_MAGIC 0x43667364 /* Cfsd */
254 #define STRING_LENGTH 28
255
256 struct fs_extended_info {
257 __le32 magic;
258 __le32 version;
259 __le32 release;
260 __u64 rel_date;
261 char version_string[STRING_LENGTH];
262 } __packed;
263
264 struct object_id_info {
265 char objid[16];
266 struct fs_extended_info extended_info;
267 } __packed;
268
269 struct file_directory_info {
270 __le32 NextEntryOffset;
271 __u32 FileIndex;
272 __le64 CreationTime;
273 __le64 LastAccessTime;
274 __le64 LastWriteTime;
275 __le64 ChangeTime;
276 __le64 EndOfFile;
277 __le64 AllocationSize;
278 __le32 ExtFileAttributes;
279 __le32 FileNameLength;
280 char FileName[1];
281 } __packed; /* level 0x101 FF resp data */
282
283 struct file_names_info {
284 __le32 NextEntryOffset;
285 __u32 FileIndex;
286 __le32 FileNameLength;
287 char FileName[1];
288 } __packed; /* level 0xc FF resp data */
289
290 struct file_full_directory_info {
291 __le32 NextEntryOffset;
292 __u32 FileIndex;
293 __le64 CreationTime;
294 __le64 LastAccessTime;
295 __le64 LastWriteTime;
296 __le64 ChangeTime;
297 __le64 EndOfFile;
298 __le64 AllocationSize;
299 __le32 ExtFileAttributes;
300 __le32 FileNameLength;
301 __le32 EaSize;
302 char FileName[1];
303 } __packed; /* level 0x102 FF resp */
304
305 struct file_both_directory_info {
306 __le32 NextEntryOffset;
307 __u32 FileIndex;
308 __le64 CreationTime;
309 __le64 LastAccessTime;
310 __le64 LastWriteTime;
311 __le64 ChangeTime;
312 __le64 EndOfFile;
313 __le64 AllocationSize;
314 __le32 ExtFileAttributes;
315 __le32 FileNameLength;
316 __le32 EaSize; /* length of the xattrs */
317 __u8 ShortNameLength;
318 __u8 Reserved;
319 __u8 ShortName[24];
320 char FileName[1];
321 } __packed; /* level 0x104 FFrsp data */
322
323 struct file_id_both_directory_info {
324 __le32 NextEntryOffset;
325 __u32 FileIndex;
326 __le64 CreationTime;
327 __le64 LastAccessTime;
328 __le64 LastWriteTime;
329 __le64 ChangeTime;
330 __le64 EndOfFile;
331 __le64 AllocationSize;
332 __le32 ExtFileAttributes;
333 __le32 FileNameLength;
334 __le32 EaSize; /* length of the xattrs */
335 __u8 ShortNameLength;
336 __u8 Reserved;
337 __u8 ShortName[24];
338 __le16 Reserved2;
339 __le64 UniqueId;
340 char FileName[1];
341 } __packed;
342
343 struct file_id_full_dir_info {
344 __le32 NextEntryOffset;
345 __u32 FileIndex;
346 __le64 CreationTime;
347 __le64 LastAccessTime;
348 __le64 LastWriteTime;
349 __le64 ChangeTime;
350 __le64 EndOfFile;
351 __le64 AllocationSize;
352 __le32 ExtFileAttributes;
353 __le32 FileNameLength;
354 __le32 EaSize; /* EA size */
355 __le32 Reserved;
356 __le64 UniqueId; /* inode num - le since Samba puts ino in low 32 bit*/
357 char FileName[1];
358 } __packed; /* level 0x105 FF rsp data */
359
360 struct smb_version_values {
361 char *version_string;
362 __u16 protocol_id;
363 __le16 lock_cmd;
364 __u32 capabilities;
365 __u32 max_read_size;
366 __u32 max_write_size;
367 __u32 max_trans_size;
368 __u32 max_credits;
369 __u32 large_lock_type;
370 __u32 exclusive_lock_type;
371 __u32 shared_lock_type;
372 __u32 unlock_lock_type;
373 size_t header_size;
374 size_t max_header_size;
375 size_t read_rsp_size;
376 unsigned int cap_unix;
377 unsigned int cap_nt_find;
378 unsigned int cap_large_files;
379 __u16 signing_enabled;
380 __u16 signing_required;
381 size_t create_lease_size;
382 size_t create_durable_size;
383 size_t create_durable_v2_size;
384 size_t create_mxac_size;
385 size_t create_disk_id_size;
386 size_t create_posix_size;
387 };
388
389 struct filesystem_posix_info {
390 /* For undefined recommended transfer size return -1 in that field */
391 __le32 OptimalTransferSize; /* bsize on some os, iosize on other os */
392 __le32 BlockSize;
393 /* The next three fields are in terms of the block size.
394 * (above). If block size is unknown, 4096 would be a
395 * reasonable block size for a server to report.
396 * Note that returning the blocks/blocksavail removes need
397 * to make a second call (to QFSInfo level 0x103 to get this info.
398 * UserBlockAvail is typically less than or equal to BlocksAvail,
399 * if no distinction is made return the same value in each
400 */
401 __le64 TotalBlocks;
402 __le64 BlocksAvail; /* bfree */
403 __le64 UserBlocksAvail; /* bavail */
404 /* For undefined Node fields or FSID return -1 */
405 __le64 TotalFileNodes;
406 __le64 FreeFileNodes;
407 __le64 FileSysIdentifier; /* fsid */
408 /* NB Namelen comes from FILE_SYSTEM_ATTRIBUTE_INFO call */
409 /* NB flags can come from FILE_SYSTEM_DEVICE_INFO call */
410 } __packed;
411
412 struct smb_version_ops {
413 u16 (*get_cmd_val)(struct ksmbd_work *swork);
414 int (*init_rsp_hdr)(struct ksmbd_work *swork);
415 void (*set_rsp_status)(struct ksmbd_work *swork, __le32 err);
416 int (*allocate_rsp_buf)(struct ksmbd_work *work);
417 int (*set_rsp_credits)(struct ksmbd_work *work);
418 int (*check_user_session)(struct ksmbd_work *work);
419 int (*get_ksmbd_tcon)(struct ksmbd_work *work);
420 bool (*is_sign_req)(struct ksmbd_work *work, unsigned int command);
421 int (*check_sign_req)(struct ksmbd_work *work);
422 void (*set_sign_rsp)(struct ksmbd_work *work);
423 int (*generate_signingkey)(struct ksmbd_session *sess, struct ksmbd_conn *conn);
424 int (*generate_encryptionkey)(struct ksmbd_conn *conn, struct ksmbd_session *sess);
425 bool (*is_transform_hdr)(void *buf);
426 int (*decrypt_req)(struct ksmbd_work *work);
427 int (*encrypt_resp)(struct ksmbd_work *work);
428 };
429
430 struct smb_version_cmds {
431 int (*proc)(struct ksmbd_work *swork);
432 };
433
434 int ksmbd_min_protocol(void);
435 int ksmbd_max_protocol(void);
436
437 int ksmbd_lookup_protocol_idx(char *str);
438
439 int ksmbd_verify_smb_message(struct ksmbd_work *work);
440 bool ksmbd_smb_request(struct ksmbd_conn *conn);
441
442 int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count);
443
444 int ksmbd_init_smb_server(struct ksmbd_work *work);
445
446 struct ksmbd_kstat;
447 int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work,
448 int info_level,
449 struct ksmbd_file *dir,
450 struct ksmbd_dir_info *d_info,
451 char *search_pattern,
452 int (*fn)(struct ksmbd_conn *,
453 int,
454 struct ksmbd_dir_info *,
455 struct ksmbd_kstat *));
456
457 int ksmbd_extract_shortname(struct ksmbd_conn *conn,
458 const char *longname,
459 char *shortname);
460
461 int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command);
462
463 int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp);
464 int ksmbd_override_fsids(struct ksmbd_work *work);
465 void ksmbd_revert_fsids(struct ksmbd_work *work);
466
467 unsigned int ksmbd_server_side_copy_max_chunk_count(void);
468 unsigned int ksmbd_server_side_copy_max_chunk_size(void);
469 unsigned int ksmbd_server_side_copy_max_total_size(void);
470 bool is_asterisk(char *p);
471 __le32 smb_map_generic_desired_access(__le32 daccess);
472
get_rfc1002_len(void * buf)473 static inline unsigned int get_rfc1002_len(void *buf)
474 {
475 return be32_to_cpu(*((__be32 *)buf)) & 0xffffff;
476 }
477
inc_rfc1001_len(void * buf,int count)478 static inline void inc_rfc1001_len(void *buf, int count)
479 {
480 be32_add_cpu((__be32 *)buf, count);
481 }
482 #endif /* __SMB_COMMON_H__ */
483