Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
0004  */
0005 
0006 #ifndef __SMB_COMMON_H__
0007 #define __SMB_COMMON_H__
0008 
0009 #include <linux/kernel.h>
0010 
0011 #include "glob.h"
0012 #include "nterr.h"
0013 #include "../smbfs_common/smb2pdu.h"
0014 #include "smb2pdu.h"
0015 
0016 /* ksmbd's Specific ERRNO */
0017 #define ESHARE          50000
0018 
0019 #define SMB1_PROT       0
0020 #define SMB2_PROT       1
0021 #define SMB21_PROT      2
0022 /* multi-protocol negotiate request */
0023 #define SMB2X_PROT      3
0024 #define SMB30_PROT      4
0025 #define SMB302_PROT     5
0026 #define SMB311_PROT     6
0027 #define BAD_PROT        0xFFFF
0028 
0029 #define SMB1_VERSION_STRING "1.0"
0030 #define SMB20_VERSION_STRING    "2.0"
0031 #define SMB21_VERSION_STRING    "2.1"
0032 #define SMB30_VERSION_STRING    "3.0"
0033 #define SMB302_VERSION_STRING   "3.02"
0034 #define SMB311_VERSION_STRING   "3.1.1"
0035 
0036 #define SMB_ECHO_INTERVAL   (60 * HZ)
0037 
0038 #define CIFS_DEFAULT_IOSIZE (64 * 1024)
0039 #define MAX_CIFS_SMALL_BUFFER_SIZE 448 /* big enough for most */
0040 
0041 #define MAX_STREAM_PROT_LEN 0x00FFFFFF
0042 
0043 /* Responses when opening a file. */
0044 #define F_SUPERSEDED    0
0045 #define F_OPENED    1
0046 #define F_CREATED   2
0047 #define F_OVERWRITTEN   3
0048 
0049 /*
0050  * File Attribute flags
0051  */
0052 #define ATTR_POSIX_SEMANTICS        0x01000000
0053 #define ATTR_BACKUP_SEMANTICS       0x02000000
0054 #define ATTR_DELETE_ON_CLOSE        0x04000000
0055 #define ATTR_SEQUENTIAL_SCAN        0x08000000
0056 #define ATTR_RANDOM_ACCESS      0x10000000
0057 #define ATTR_NO_BUFFERING       0x20000000
0058 #define ATTR_WRITE_THROUGH      0x80000000
0059 
0060 /* List of FileSystemAttributes - see 2.5.1 of MS-FSCC */
0061 #define FILE_SUPPORTS_SPARSE_VDL    0x10000000 /* faster nonsparse extend */
0062 #define FILE_SUPPORTS_BLOCK_REFCOUNTING 0x08000000 /* allow ioctl dup extents */
0063 #define FILE_SUPPORT_INTEGRITY_STREAMS  0x04000000
0064 #define FILE_SUPPORTS_USN_JOURNAL   0x02000000
0065 #define FILE_SUPPORTS_OPEN_BY_FILE_ID   0x01000000
0066 #define FILE_SUPPORTS_EXTENDED_ATTRIBUTES 0x00800000
0067 #define FILE_SUPPORTS_HARD_LINKS    0x00400000
0068 #define FILE_SUPPORTS_TRANSACTIONS  0x00200000
0069 #define FILE_SEQUENTIAL_WRITE_ONCE  0x00100000
0070 #define FILE_READ_ONLY_VOLUME       0x00080000
0071 #define FILE_NAMED_STREAMS      0x00040000
0072 #define FILE_SUPPORTS_ENCRYPTION    0x00020000
0073 #define FILE_SUPPORTS_OBJECT_IDS    0x00010000
0074 #define FILE_VOLUME_IS_COMPRESSED   0x00008000
0075 #define FILE_SUPPORTS_REMOTE_STORAGE    0x00000100
0076 #define FILE_SUPPORTS_REPARSE_POINTS    0x00000080
0077 #define FILE_SUPPORTS_SPARSE_FILES  0x00000040
0078 #define FILE_VOLUME_QUOTAS      0x00000020
0079 #define FILE_FILE_COMPRESSION       0x00000010
0080 #define FILE_PERSISTENT_ACLS        0x00000008
0081 #define FILE_UNICODE_ON_DISK        0x00000004
0082 #define FILE_CASE_PRESERVED_NAMES   0x00000002
0083 #define FILE_CASE_SENSITIVE_SEARCH  0x00000001
0084 
0085 #define FILE_READ_DATA        0x00000001  /* Data can be read from the file   */
0086 #define FILE_WRITE_DATA       0x00000002  /* Data can be written to the file  */
0087 #define FILE_APPEND_DATA      0x00000004  /* Data can be appended to the file */
0088 #define FILE_READ_EA          0x00000008  /* Extended attributes associated   */
0089 /* with the file can be read        */
0090 #define FILE_WRITE_EA         0x00000010  /* Extended attributes associated   */
0091 /* with the file can be written     */
0092 #define FILE_EXECUTE          0x00000020  /*Data can be read into memory from */
0093 /* the file using system paging I/O */
0094 #define FILE_DELETE_CHILD     0x00000040
0095 #define FILE_READ_ATTRIBUTES  0x00000080  /* Attributes associated with the   */
0096 /* file can be read                 */
0097 #define FILE_WRITE_ATTRIBUTES 0x00000100  /* Attributes associated with the   */
0098 /* file can be written              */
0099 #define DELETE                0x00010000  /* The file can be deleted          */
0100 #define READ_CONTROL          0x00020000  /* The access control list and      */
0101 /* ownership associated with the    */
0102 /* file can be read                 */
0103 #define WRITE_DAC             0x00040000  /* The access control list and      */
0104 /* ownership associated with the    */
0105 /* file can be written.             */
0106 #define WRITE_OWNER           0x00080000  /* Ownership information associated */
0107 /* with the file can be written     */
0108 #define SYNCHRONIZE           0x00100000  /* The file handle can waited on to */
0109 /* synchronize with the completion  */
0110 /* of an input/output request       */
0111 #define GENERIC_ALL           0x10000000
0112 #define GENERIC_EXECUTE       0x20000000
0113 #define GENERIC_WRITE         0x40000000
0114 #define GENERIC_READ          0x80000000
0115 /* In summary - Relevant file       */
0116 /* access flags from CIFS are       */
0117 /* file_read_data, file_write_data  */
0118 /* file_execute, file_read_attributes*/
0119 /* write_dac, and delete.           */
0120 
0121 #define SET_FILE_READ_RIGHTS (FILE_READ_DATA | FILE_READ_EA \
0122         | FILE_READ_ATTRIBUTES \
0123         | DELETE | READ_CONTROL | WRITE_DAC \
0124         | WRITE_OWNER | SYNCHRONIZE)
0125 #define SET_FILE_WRITE_RIGHTS (FILE_WRITE_DATA | FILE_APPEND_DATA \
0126         | FILE_WRITE_EA \
0127         | FILE_DELETE_CHILD \
0128         | FILE_WRITE_ATTRIBUTES \
0129         | DELETE | READ_CONTROL | WRITE_DAC \
0130         | WRITE_OWNER | SYNCHRONIZE)
0131 #define SET_FILE_EXEC_RIGHTS (FILE_READ_EA | FILE_WRITE_EA | FILE_EXECUTE \
0132         | FILE_READ_ATTRIBUTES \
0133         | FILE_WRITE_ATTRIBUTES \
0134         | DELETE | READ_CONTROL | WRITE_DAC \
0135         | WRITE_OWNER | SYNCHRONIZE)
0136 
0137 #define SET_MINIMUM_RIGHTS (FILE_READ_EA | FILE_READ_ATTRIBUTES \
0138         | READ_CONTROL | SYNCHRONIZE)
0139 
0140 /* generic flags for file open */
0141 #define GENERIC_READ_FLAGS  (READ_CONTROL | FILE_READ_DATA | \
0142         FILE_READ_ATTRIBUTES | \
0143         FILE_READ_EA | SYNCHRONIZE)
0144 
0145 #define GENERIC_WRITE_FLAGS (READ_CONTROL | FILE_WRITE_DATA | \
0146         FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | \
0147         FILE_APPEND_DATA | SYNCHRONIZE)
0148 
0149 #define GENERIC_EXECUTE_FLAGS   (READ_CONTROL | FILE_EXECUTE | \
0150         FILE_READ_ATTRIBUTES | SYNCHRONIZE)
0151 
0152 #define GENERIC_ALL_FLAGS   (DELETE | READ_CONTROL | WRITE_DAC | \
0153         WRITE_OWNER | SYNCHRONIZE | FILE_READ_DATA | \
0154         FILE_WRITE_DATA | FILE_APPEND_DATA | \
0155         FILE_READ_EA | FILE_WRITE_EA | \
0156         FILE_EXECUTE | FILE_DELETE_CHILD | \
0157         FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES)
0158 
0159 #define SMB1_PROTO_NUMBER       cpu_to_le32(0x424d53ff)
0160 #define SMB_COM_NEGOTIATE       0x72
0161 
0162 #define SMB1_CLIENT_GUID_SIZE       (16)
0163 struct smb_hdr {
0164     __be32 smb_buf_length;
0165     __u8 Protocol[4];
0166     __u8 Command;
0167     union {
0168         struct {
0169             __u8 ErrorClass;
0170             __u8 Reserved;
0171             __le16 Error;
0172         } __packed DosError;
0173         __le32 CifsError;
0174     } __packed Status;
0175     __u8 Flags;
0176     __le16 Flags2;          /* note: le */
0177     __le16 PidHigh;
0178     union {
0179         struct {
0180             __le32 SequenceNumber;  /* le */
0181             __u32 Reserved; /* zero */
0182         } __packed Sequence;
0183         __u8 SecuritySignature[8];      /* le */
0184     } __packed Signature;
0185     __u8 pad[2];
0186     __le16 Tid;
0187     __le16 Pid;
0188     __le16 Uid;
0189     __le16 Mid;
0190     __u8 WordCount;
0191 } __packed;
0192 
0193 struct smb_negotiate_req {
0194     struct smb_hdr hdr;     /* wct = 0 */
0195     __le16 ByteCount;
0196     unsigned char DialectsArray[1];
0197 } __packed;
0198 
0199 struct smb_negotiate_rsp {
0200     struct smb_hdr hdr;     /* wct = 17 */
0201     __le16 DialectIndex; /* 0xFFFF = no dialect acceptable */
0202     __u8 SecurityMode;
0203     __le16 MaxMpxCount;
0204     __le16 MaxNumberVcs;
0205     __le32 MaxBufferSize;
0206     __le32 MaxRawSize;
0207     __le32 SessionKey;
0208     __le32 Capabilities;    /* see below */
0209     __le32 SystemTimeLow;
0210     __le32 SystemTimeHigh;
0211     __le16 ServerTimeZone;
0212     __u8 EncryptionKeyLength;
0213     __le16 ByteCount;
0214     union {
0215         unsigned char EncryptionKey[8]; /* cap extended security off */
0216         /* followed by Domain name - if extended security is off */
0217         /* followed by 16 bytes of server GUID */
0218         /* then security blob if cap_extended_security negotiated */
0219         struct {
0220             unsigned char GUID[SMB1_CLIENT_GUID_SIZE];
0221             unsigned char SecurityBlob[1];
0222         } __packed extended_response;
0223     } __packed u;
0224 } __packed;
0225 
0226 struct filesystem_attribute_info {
0227     __le32 Attributes;
0228     __le32 MaxPathNameComponentLength;
0229     __le32 FileSystemNameLen;
0230     __le16 FileSystemName[1]; /* do not have to save this - get subset? */
0231 } __packed;
0232 
0233 struct filesystem_device_info {
0234     __le32 DeviceType;
0235     __le32 DeviceCharacteristics;
0236 } __packed; /* device info level 0x104 */
0237 
0238 struct filesystem_vol_info {
0239     __le64 VolumeCreationTime;
0240     __le32 SerialNumber;
0241     __le32 VolumeLabelSize;
0242     __le16 Reserved;
0243     __le16 VolumeLabel[1];
0244 } __packed;
0245 
0246 struct filesystem_info {
0247     __le64 TotalAllocationUnits;
0248     __le64 FreeAllocationUnits;
0249     __le32 SectorsPerAllocationUnit;
0250     __le32 BytesPerSector;
0251 } __packed;     /* size info, level 0x103 */
0252 
0253 #define EXTENDED_INFO_MAGIC 0x43667364  /* Cfsd */
0254 #define STRING_LENGTH 28
0255 
0256 struct fs_extended_info {
0257     __le32 magic;
0258     __le32 version;
0259     __le32 release;
0260     __u64 rel_date;
0261     char    version_string[STRING_LENGTH];
0262 } __packed;
0263 
0264 struct object_id_info {
0265     char objid[16];
0266     struct fs_extended_info extended_info;
0267 } __packed;
0268 
0269 struct file_directory_info {
0270     __le32 NextEntryOffset;
0271     __u32 FileIndex;
0272     __le64 CreationTime;
0273     __le64 LastAccessTime;
0274     __le64 LastWriteTime;
0275     __le64 ChangeTime;
0276     __le64 EndOfFile;
0277     __le64 AllocationSize;
0278     __le32 ExtFileAttributes;
0279     __le32 FileNameLength;
0280     char FileName[1];
0281 } __packed;   /* level 0x101 FF resp data */
0282 
0283 struct file_names_info {
0284     __le32 NextEntryOffset;
0285     __u32 FileIndex;
0286     __le32 FileNameLength;
0287     char FileName[1];
0288 } __packed;   /* level 0xc FF resp data */
0289 
0290 struct file_full_directory_info {
0291     __le32 NextEntryOffset;
0292     __u32 FileIndex;
0293     __le64 CreationTime;
0294     __le64 LastAccessTime;
0295     __le64 LastWriteTime;
0296     __le64 ChangeTime;
0297     __le64 EndOfFile;
0298     __le64 AllocationSize;
0299     __le32 ExtFileAttributes;
0300     __le32 FileNameLength;
0301     __le32 EaSize;
0302     char FileName[1];
0303 } __packed; /* level 0x102 FF resp */
0304 
0305 struct file_both_directory_info {
0306     __le32 NextEntryOffset;
0307     __u32 FileIndex;
0308     __le64 CreationTime;
0309     __le64 LastAccessTime;
0310     __le64 LastWriteTime;
0311     __le64 ChangeTime;
0312     __le64 EndOfFile;
0313     __le64 AllocationSize;
0314     __le32 ExtFileAttributes;
0315     __le32 FileNameLength;
0316     __le32 EaSize; /* length of the xattrs */
0317     __u8   ShortNameLength;
0318     __u8   Reserved;
0319     __u8   ShortName[24];
0320     char FileName[1];
0321 } __packed; /* level 0x104 FFrsp data */
0322 
0323 struct file_id_both_directory_info {
0324     __le32 NextEntryOffset;
0325     __u32 FileIndex;
0326     __le64 CreationTime;
0327     __le64 LastAccessTime;
0328     __le64 LastWriteTime;
0329     __le64 ChangeTime;
0330     __le64 EndOfFile;
0331     __le64 AllocationSize;
0332     __le32 ExtFileAttributes;
0333     __le32 FileNameLength;
0334     __le32 EaSize; /* length of the xattrs */
0335     __u8   ShortNameLength;
0336     __u8   Reserved;
0337     __u8   ShortName[24];
0338     __le16 Reserved2;
0339     __le64 UniqueId;
0340     char FileName[1];
0341 } __packed;
0342 
0343 struct file_id_full_dir_info {
0344     __le32 NextEntryOffset;
0345     __u32 FileIndex;
0346     __le64 CreationTime;
0347     __le64 LastAccessTime;
0348     __le64 LastWriteTime;
0349     __le64 ChangeTime;
0350     __le64 EndOfFile;
0351     __le64 AllocationSize;
0352     __le32 ExtFileAttributes;
0353     __le32 FileNameLength;
0354     __le32 EaSize; /* EA size */
0355     __le32 Reserved;
0356     __le64 UniqueId; /* inode num - le since Samba puts ino in low 32 bit*/
0357     char FileName[1];
0358 } __packed; /* level 0x105 FF rsp data */
0359 
0360 struct smb_version_values {
0361     char        *version_string;
0362     __u16       protocol_id;
0363     __le16      lock_cmd;
0364     __u32       capabilities;
0365     __u32       max_read_size;
0366     __u32       max_write_size;
0367     __u32       max_trans_size;
0368     __u32       max_credits;
0369     __u32       large_lock_type;
0370     __u32       exclusive_lock_type;
0371     __u32       shared_lock_type;
0372     __u32       unlock_lock_type;
0373     size_t      header_size;
0374     size_t      max_header_size;
0375     size_t      read_rsp_size;
0376     unsigned int    cap_unix;
0377     unsigned int    cap_nt_find;
0378     unsigned int    cap_large_files;
0379     __u16       signing_enabled;
0380     __u16       signing_required;
0381     size_t      create_lease_size;
0382     size_t      create_durable_size;
0383     size_t      create_durable_v2_size;
0384     size_t      create_mxac_size;
0385     size_t      create_disk_id_size;
0386     size_t      create_posix_size;
0387 };
0388 
0389 struct filesystem_posix_info {
0390     /* For undefined recommended transfer size return -1 in that field */
0391     __le32 OptimalTransferSize;  /* bsize on some os, iosize on other os */
0392     __le32 BlockSize;
0393     /* The next three fields are in terms of the block size.
0394      * (above). If block size is unknown, 4096 would be a
0395      * reasonable block size for a server to report.
0396      * Note that returning the blocks/blocksavail removes need
0397      * to make a second call (to QFSInfo level 0x103 to get this info.
0398      * UserBlockAvail is typically less than or equal to BlocksAvail,
0399      * if no distinction is made return the same value in each
0400      */
0401     __le64 TotalBlocks;
0402     __le64 BlocksAvail;       /* bfree */
0403     __le64 UserBlocksAvail;   /* bavail */
0404     /* For undefined Node fields or FSID return -1 */
0405     __le64 TotalFileNodes;
0406     __le64 FreeFileNodes;
0407     __le64 FileSysIdentifier;   /* fsid */
0408     /* NB Namelen comes from FILE_SYSTEM_ATTRIBUTE_INFO call */
0409     /* NB flags can come from FILE_SYSTEM_DEVICE_INFO call   */
0410 } __packed;
0411 
0412 struct smb_version_ops {
0413     u16 (*get_cmd_val)(struct ksmbd_work *swork);
0414     int (*init_rsp_hdr)(struct ksmbd_work *swork);
0415     void (*set_rsp_status)(struct ksmbd_work *swork, __le32 err);
0416     int (*allocate_rsp_buf)(struct ksmbd_work *work);
0417     int (*set_rsp_credits)(struct ksmbd_work *work);
0418     int (*check_user_session)(struct ksmbd_work *work);
0419     int (*get_ksmbd_tcon)(struct ksmbd_work *work);
0420     bool (*is_sign_req)(struct ksmbd_work *work, unsigned int command);
0421     int (*check_sign_req)(struct ksmbd_work *work);
0422     void (*set_sign_rsp)(struct ksmbd_work *work);
0423     int (*generate_signingkey)(struct ksmbd_session *sess, struct ksmbd_conn *conn);
0424     int (*generate_encryptionkey)(struct ksmbd_conn *conn, struct ksmbd_session *sess);
0425     bool (*is_transform_hdr)(void *buf);
0426     int (*decrypt_req)(struct ksmbd_work *work);
0427     int (*encrypt_resp)(struct ksmbd_work *work);
0428 };
0429 
0430 struct smb_version_cmds {
0431     int (*proc)(struct ksmbd_work *swork);
0432 };
0433 
0434 int ksmbd_min_protocol(void);
0435 int ksmbd_max_protocol(void);
0436 
0437 int ksmbd_lookup_protocol_idx(char *str);
0438 
0439 int ksmbd_verify_smb_message(struct ksmbd_work *work);
0440 bool ksmbd_smb_request(struct ksmbd_conn *conn);
0441 
0442 int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count);
0443 
0444 int ksmbd_init_smb_server(struct ksmbd_work *work);
0445 
0446 struct ksmbd_kstat;
0447 int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work,
0448                       int info_level,
0449                       struct ksmbd_file *dir,
0450                       struct ksmbd_dir_info *d_info,
0451                       char *search_pattern,
0452                       int (*fn)(struct ksmbd_conn *,
0453                         int,
0454                         struct ksmbd_dir_info *,
0455                         struct ksmbd_kstat *));
0456 
0457 int ksmbd_extract_shortname(struct ksmbd_conn *conn,
0458                 const char *longname,
0459                 char *shortname);
0460 
0461 int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command);
0462 
0463 int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp);
0464 int ksmbd_override_fsids(struct ksmbd_work *work);
0465 void ksmbd_revert_fsids(struct ksmbd_work *work);
0466 
0467 unsigned int ksmbd_server_side_copy_max_chunk_count(void);
0468 unsigned int ksmbd_server_side_copy_max_chunk_size(void);
0469 unsigned int ksmbd_server_side_copy_max_total_size(void);
0470 bool is_asterisk(char *p);
0471 __le32 smb_map_generic_desired_access(__le32 daccess);
0472 
0473 static inline unsigned int get_rfc1002_len(void *buf)
0474 {
0475     return be32_to_cpu(*((__be32 *)buf)) & 0xffffff;
0476 }
0477 
0478 static inline void inc_rfc1001_len(void *buf, int count)
0479 {
0480     be32_add_cpu((__be32 *)buf, count);
0481 }
0482 #endif /* __SMB_COMMON_H__ */