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