xref: /openbmc/linux/fs/smb/server/smb_common.h (revision 536bb492)
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_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 #define SMB1_CLIENT_GUID_SIZE		(16)
162 
163 #define SMBFLG_RESPONSE 0x80	/* this PDU is a response from server */
164 
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)
169 
170 struct smb_hdr {
171 	__be32 smb_buf_length;
172 	__u8 Protocol[4];
173 	__u8 Command;
174 	union {
175 		struct {
176 			__u8 ErrorClass;
177 			__u8 Reserved;
178 			__le16 Error;
179 		} __packed DosError;
180 		__le32 CifsError;
181 	} __packed Status;
182 	__u8 Flags;
183 	__le16 Flags2;          /* note: le */
184 	__le16 PidHigh;
185 	union {
186 		struct {
187 			__le32 SequenceNumber;  /* le */
188 			__u32 Reserved; /* zero */
189 		} __packed Sequence;
190 		__u8 SecuritySignature[8];      /* le */
191 	} __packed Signature;
192 	__u8 pad[2];
193 	__le16 Tid;
194 	__le16 Pid;
195 	__le16 Uid;
196 	__le16 Mid;
197 	__u8 WordCount;
198 } __packed;
199 
200 struct smb_negotiate_req {
201 	struct smb_hdr hdr;     /* wct = 0 */
202 	__le16 ByteCount;
203 	unsigned char DialectsArray[];
204 } __packed;
205 
206 struct smb_negotiate_rsp {
207 	struct smb_hdr hdr;     /* wct = 17 */
208 	__le16 DialectIndex; /* 0xFFFF = no dialect acceptable */
209 	__le16 ByteCount;
210 } __packed;
211 
212 struct filesystem_attribute_info {
213 	__le32 Attributes;
214 	__le32 MaxPathNameComponentLength;
215 	__le32 FileSystemNameLen;
216 	__le16 FileSystemName[1]; /* do not have to save this - get subset? */
217 } __packed;
218 
219 struct filesystem_device_info {
220 	__le32 DeviceType;
221 	__le32 DeviceCharacteristics;
222 } __packed; /* device info level 0x104 */
223 
224 struct filesystem_vol_info {
225 	__le64 VolumeCreationTime;
226 	__le32 SerialNumber;
227 	__le32 VolumeLabelSize;
228 	__le16 Reserved;
229 	__le16 VolumeLabel[1];
230 } __packed;
231 
232 struct filesystem_info {
233 	__le64 TotalAllocationUnits;
234 	__le64 FreeAllocationUnits;
235 	__le32 SectorsPerAllocationUnit;
236 	__le32 BytesPerSector;
237 } __packed;     /* size info, level 0x103 */
238 
239 #define EXTENDED_INFO_MAGIC 0x43667364	/* Cfsd */
240 #define STRING_LENGTH 28
241 
242 struct fs_extended_info {
243 	__le32 magic;
244 	__le32 version;
245 	__le32 release;
246 	__u64 rel_date;
247 	char    version_string[STRING_LENGTH];
248 } __packed;
249 
250 struct object_id_info {
251 	char objid[16];
252 	struct fs_extended_info extended_info;
253 } __packed;
254 
255 struct file_directory_info {
256 	__le32 NextEntryOffset;
257 	__u32 FileIndex;
258 	__le64 CreationTime;
259 	__le64 LastAccessTime;
260 	__le64 LastWriteTime;
261 	__le64 ChangeTime;
262 	__le64 EndOfFile;
263 	__le64 AllocationSize;
264 	__le32 ExtFileAttributes;
265 	__le32 FileNameLength;
266 	char FileName[];
267 } __packed;   /* level 0x101 FF resp data */
268 
269 struct file_names_info {
270 	__le32 NextEntryOffset;
271 	__u32 FileIndex;
272 	__le32 FileNameLength;
273 	char FileName[];
274 } __packed;   /* level 0xc FF resp data */
275 
276 struct file_full_directory_info {
277 	__le32 NextEntryOffset;
278 	__u32 FileIndex;
279 	__le64 CreationTime;
280 	__le64 LastAccessTime;
281 	__le64 LastWriteTime;
282 	__le64 ChangeTime;
283 	__le64 EndOfFile;
284 	__le64 AllocationSize;
285 	__le32 ExtFileAttributes;
286 	__le32 FileNameLength;
287 	__le32 EaSize;
288 	char FileName[];
289 } __packed; /* level 0x102 FF resp */
290 
291 struct file_both_directory_info {
292 	__le32 NextEntryOffset;
293 	__u32 FileIndex;
294 	__le64 CreationTime;
295 	__le64 LastAccessTime;
296 	__le64 LastWriteTime;
297 	__le64 ChangeTime;
298 	__le64 EndOfFile;
299 	__le64 AllocationSize;
300 	__le32 ExtFileAttributes;
301 	__le32 FileNameLength;
302 	__le32 EaSize; /* length of the xattrs */
303 	__u8   ShortNameLength;
304 	__u8   Reserved;
305 	__u8   ShortName[24];
306 	char FileName[];
307 } __packed; /* level 0x104 FFrsp data */
308 
309 struct file_id_both_directory_info {
310 	__le32 NextEntryOffset;
311 	__u32 FileIndex;
312 	__le64 CreationTime;
313 	__le64 LastAccessTime;
314 	__le64 LastWriteTime;
315 	__le64 ChangeTime;
316 	__le64 EndOfFile;
317 	__le64 AllocationSize;
318 	__le32 ExtFileAttributes;
319 	__le32 FileNameLength;
320 	__le32 EaSize; /* length of the xattrs */
321 	__u8   ShortNameLength;
322 	__u8   Reserved;
323 	__u8   ShortName[24];
324 	__le16 Reserved2;
325 	__le64 UniqueId;
326 	char FileName[];
327 } __packed;
328 
329 struct file_id_full_dir_info {
330 	__le32 NextEntryOffset;
331 	__u32 FileIndex;
332 	__le64 CreationTime;
333 	__le64 LastAccessTime;
334 	__le64 LastWriteTime;
335 	__le64 ChangeTime;
336 	__le64 EndOfFile;
337 	__le64 AllocationSize;
338 	__le32 ExtFileAttributes;
339 	__le32 FileNameLength;
340 	__le32 EaSize; /* EA size */
341 	__le32 Reserved;
342 	__le64 UniqueId; /* inode num - le since Samba puts ino in low 32 bit*/
343 	char FileName[];
344 } __packed; /* level 0x105 FF rsp data */
345 
346 struct smb_version_values {
347 	char		*version_string;
348 	__u16		protocol_id;
349 	__le16		lock_cmd;
350 	__u32		capabilities;
351 	__u32		max_read_size;
352 	__u32		max_write_size;
353 	__u32		max_trans_size;
354 	__u32		max_credits;
355 	__u32		large_lock_type;
356 	__u32		exclusive_lock_type;
357 	__u32		shared_lock_type;
358 	__u32		unlock_lock_type;
359 	size_t		header_size;
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;
373 };
374 
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 */
378 	__le32 BlockSize;
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
386 	 */
387 	__le64 TotalBlocks;
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   */
396 } __packed;
397 
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);
414 };
415 
416 struct smb_version_cmds {
417 	int (*proc)(struct ksmbd_work *swork);
418 };
419 
420 int ksmbd_min_protocol(void);
421 int ksmbd_max_protocol(void);
422 
423 int ksmbd_lookup_protocol_idx(char *str);
424 
425 int ksmbd_verify_smb_message(struct ksmbd_work *work);
426 bool ksmbd_smb_request(struct ksmbd_conn *conn);
427 
428 int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count);
429 
430 int ksmbd_init_smb_server(struct ksmbd_work *work);
431 
432 struct ksmbd_kstat;
433 int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work,
434 				      int info_level,
435 				      struct ksmbd_file *dir,
436 				      struct ksmbd_dir_info *d_info,
437 				      char *search_pattern,
438 				      int (*fn)(struct ksmbd_conn *,
439 						int,
440 						struct ksmbd_dir_info *,
441 						struct ksmbd_kstat *));
442 
443 int ksmbd_extract_shortname(struct ksmbd_conn *conn,
444 			    const char *longname,
445 			    char *shortname);
446 
447 int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command);
448 
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);
452 
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);
458 
get_rfc1002_len(void * buf)459 static inline unsigned int get_rfc1002_len(void *buf)
460 {
461 	return be32_to_cpu(*((__be32 *)buf)) & 0xffffff;
462 }
463 
inc_rfc1001_len(void * buf,int count)464 static inline void inc_rfc1001_len(void *buf, int count)
465 {
466 	be32_add_cpu((__be32 *)buf, count);
467 }
468 #endif /* __SMB_COMMON_H__ */
469