Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
0004  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
0005  */
0006 
0007 #ifndef _SMB2PDU_H
0008 #define _SMB2PDU_H
0009 
0010 #include "ntlmssp.h"
0011 #include "smbacl.h"
0012 
0013 /*Create Action Flags*/
0014 #define FILE_SUPERSEDED                0x00000000
0015 #define FILE_OPENED            0x00000001
0016 #define FILE_CREATED           0x00000002
0017 #define FILE_OVERWRITTEN       0x00000003
0018 
0019 /* SMB2 Max Credits */
0020 #define SMB2_MAX_CREDITS        8192
0021 
0022 /* BB FIXME - analyze following length BB */
0023 #define MAX_SMB2_HDR_SIZE 0x78 /* 4 len + 64 hdr + (2*24 wct) + 2 bct + 2 pad */
0024 
0025 #define SMB21_DEFAULT_IOSIZE    (1024 * 1024)
0026 #define SMB3_DEFAULT_TRANS_SIZE (1024 * 1024)
0027 #define SMB3_MIN_IOSIZE (64 * 1024)
0028 #define SMB3_MAX_IOSIZE (8 * 1024 * 1024)
0029 
0030 /*
0031  *  Definitions for SMB2 Protocol Data Units (network frames)
0032  *
0033  *  See MS-SMB2.PDF specification for protocol details.
0034  *  The Naming convention is the lower case version of the SMB2
0035  *  command code name for the struct. Note that structures must be packed.
0036  *
0037  */
0038 
0039 struct preauth_integrity_info {
0040     /* PreAuth integrity Hash ID */
0041     __le16          Preauth_HashId;
0042     /* PreAuth integrity Hash Value */
0043     __u8            Preauth_HashValue[SMB2_PREAUTH_HASH_SIZE];
0044 };
0045 
0046 /* offset is sizeof smb2_negotiate_rsp but rounded up to 8 bytes. */
0047 #ifdef CONFIG_SMB_SERVER_KERBEROS5
0048 /* sizeof(struct smb2_negotiate_rsp) =
0049  * header(64) + response(64) + GSS_LENGTH(96) + GSS_PADDING(0)
0050  */
0051 #define OFFSET_OF_NEG_CONTEXT   0xe0
0052 #else
0053 /* sizeof(struct smb2_negotiate_rsp) =
0054  * header(64) + response(64) + GSS_LENGTH(74) + GSS_PADDING(6)
0055  */
0056 #define OFFSET_OF_NEG_CONTEXT   0xd0
0057 #endif
0058 
0059 #define SMB2_SESSION_EXPIRED        (0)
0060 #define SMB2_SESSION_IN_PROGRESS    BIT(0)
0061 #define SMB2_SESSION_VALID      BIT(1)
0062 
0063 struct create_durable_req_v2 {
0064     struct create_context ccontext;
0065     __u8   Name[8];
0066     __le32 Timeout;
0067     __le32 Flags;
0068     __u8 Reserved[8];
0069     __u8 CreateGuid[16];
0070 } __packed;
0071 
0072 struct create_durable_reconn_req {
0073     struct create_context ccontext;
0074     __u8   Name[8];
0075     union {
0076         __u8  Reserved[16];
0077         struct {
0078             __u64 PersistentFileId;
0079             __u64 VolatileFileId;
0080         } Fid;
0081     } Data;
0082 } __packed;
0083 
0084 struct create_durable_reconn_v2_req {
0085     struct create_context ccontext;
0086     __u8   Name[8];
0087     struct {
0088         __u64 PersistentFileId;
0089         __u64 VolatileFileId;
0090     } Fid;
0091     __u8 CreateGuid[16];
0092     __le32 Flags;
0093 } __packed;
0094 
0095 struct create_app_inst_id {
0096     struct create_context ccontext;
0097     __u8 Name[8];
0098     __u8 Reserved[8];
0099     __u8 AppInstanceId[16];
0100 } __packed;
0101 
0102 struct create_app_inst_id_vers {
0103     struct create_context ccontext;
0104     __u8 Name[8];
0105     __u8 Reserved[2];
0106     __u8 Padding[4];
0107     __le64 AppInstanceVersionHigh;
0108     __le64 AppInstanceVersionLow;
0109 } __packed;
0110 
0111 struct create_mxac_req {
0112     struct create_context ccontext;
0113     __u8   Name[8];
0114     __le64 Timestamp;
0115 } __packed;
0116 
0117 struct create_alloc_size_req {
0118     struct create_context ccontext;
0119     __u8   Name[8];
0120     __le64 AllocationSize;
0121 } __packed;
0122 
0123 struct create_durable_rsp {
0124     struct create_context ccontext;
0125     __u8   Name[8];
0126     union {
0127         __u8  Reserved[8];
0128         __u64 data;
0129     } Data;
0130 } __packed;
0131 
0132 struct create_durable_v2_rsp {
0133     struct create_context ccontext;
0134     __u8   Name[8];
0135     __le32 Timeout;
0136     __le32 Flags;
0137 } __packed;
0138 
0139 struct create_mxac_rsp {
0140     struct create_context ccontext;
0141     __u8   Name[8];
0142     __le32 QueryStatus;
0143     __le32 MaximalAccess;
0144 } __packed;
0145 
0146 struct create_disk_id_rsp {
0147     struct create_context ccontext;
0148     __u8   Name[8];
0149     __le64 DiskFileId;
0150     __le64 VolumeId;
0151     __u8  Reserved[16];
0152 } __packed;
0153 
0154 /* equivalent of the contents of SMB3.1.1 POSIX open context response */
0155 struct create_posix_rsp {
0156     struct create_context ccontext;
0157     __u8    Name[16];
0158     __le32 nlink;
0159     __le32 reparse_tag;
0160     __le32 mode;
0161     u8 SidBuffer[40];
0162 } __packed;
0163 
0164 struct smb2_buffer_desc_v1 {
0165     __le64 offset;
0166     __le32 token;
0167     __le32 length;
0168 } __packed;
0169 
0170 #define SMB2_0_IOCTL_IS_FSCTL 0x00000001
0171 
0172 struct smb_sockaddr_in {
0173     __be16 Port;
0174     __be32 IPv4address;
0175     __u8 Reserved[8];
0176 } __packed;
0177 
0178 struct smb_sockaddr_in6 {
0179     __be16 Port;
0180     __be32 FlowInfo;
0181     __u8 IPv6address[16];
0182     __be32 ScopeId;
0183 } __packed;
0184 
0185 #define INTERNETWORK    0x0002
0186 #define INTERNETWORKV6  0x0017
0187 
0188 struct sockaddr_storage_rsp {
0189     __le16 Family;
0190     union {
0191         struct smb_sockaddr_in addr4;
0192         struct smb_sockaddr_in6 addr6;
0193     };
0194 } __packed;
0195 
0196 #define RSS_CAPABLE 0x00000001
0197 #define RDMA_CAPABLE    0x00000002
0198 
0199 struct network_interface_info_ioctl_rsp {
0200     __le32 Next; /* next interface. zero if this is last one */
0201     __le32 IfIndex;
0202     __le32 Capability; /* RSS or RDMA Capable */
0203     __le32 Reserved;
0204     __le64 LinkSpeed;
0205     char    SockAddr_Storage[128];
0206 } __packed;
0207 
0208 struct file_object_buf_type1_ioctl_rsp {
0209     __u8 ObjectId[16];
0210     __u8 BirthVolumeId[16];
0211     __u8 BirthObjectId[16];
0212     __u8 DomainId[16];
0213 } __packed;
0214 
0215 struct resume_key_ioctl_rsp {
0216     __u64 ResumeKey[3];
0217     __le32 ContextLength;
0218     __u8 Context[4]; /* ignored, Windows sets to 4 bytes of zero */
0219 } __packed;
0220 
0221 struct copychunk_ioctl_req {
0222     __le64 ResumeKey[3];
0223     __le32 ChunkCount;
0224     __le32 Reserved;
0225     __u8 Chunks[1]; /* array of srv_copychunk */
0226 } __packed;
0227 
0228 struct srv_copychunk {
0229     __le64 SourceOffset;
0230     __le64 TargetOffset;
0231     __le32 Length;
0232     __le32 Reserved;
0233 } __packed;
0234 
0235 struct copychunk_ioctl_rsp {
0236     __le32 ChunksWritten;
0237     __le32 ChunkBytesWritten;
0238     __le32 TotalBytesWritten;
0239 } __packed;
0240 
0241 struct file_sparse {
0242     __u8    SetSparse;
0243 } __packed;
0244 
0245 /* FILE Info response size */
0246 #define FILE_DIRECTORY_INFORMATION_SIZE       1
0247 #define FILE_FULL_DIRECTORY_INFORMATION_SIZE  2
0248 #define FILE_BOTH_DIRECTORY_INFORMATION_SIZE  3
0249 #define FILE_BASIC_INFORMATION_SIZE           40
0250 #define FILE_STANDARD_INFORMATION_SIZE        24
0251 #define FILE_INTERNAL_INFORMATION_SIZE        8
0252 #define FILE_EA_INFORMATION_SIZE              4
0253 #define FILE_ACCESS_INFORMATION_SIZE          4
0254 #define FILE_NAME_INFORMATION_SIZE            9
0255 #define FILE_RENAME_INFORMATION_SIZE          10
0256 #define FILE_LINK_INFORMATION_SIZE            11
0257 #define FILE_NAMES_INFORMATION_SIZE           12
0258 #define FILE_DISPOSITION_INFORMATION_SIZE     13
0259 #define FILE_POSITION_INFORMATION_SIZE        14
0260 #define FILE_FULL_EA_INFORMATION_SIZE         15
0261 #define FILE_MODE_INFORMATION_SIZE            4
0262 #define FILE_ALIGNMENT_INFORMATION_SIZE       4
0263 #define FILE_ALL_INFORMATION_SIZE             104
0264 #define FILE_ALLOCATION_INFORMATION_SIZE      19
0265 #define FILE_END_OF_FILE_INFORMATION_SIZE     20
0266 #define FILE_ALTERNATE_NAME_INFORMATION_SIZE  8
0267 #define FILE_STREAM_INFORMATION_SIZE          32
0268 #define FILE_PIPE_INFORMATION_SIZE            23
0269 #define FILE_PIPE_LOCAL_INFORMATION_SIZE      24
0270 #define FILE_PIPE_REMOTE_INFORMATION_SIZE     25
0271 #define FILE_MAILSLOT_QUERY_INFORMATION_SIZE  26
0272 #define FILE_MAILSLOT_SET_INFORMATION_SIZE    27
0273 #define FILE_COMPRESSION_INFORMATION_SIZE     16
0274 #define FILE_OBJECT_ID_INFORMATION_SIZE       29
0275 /* Number 30 not defined in documents */
0276 #define FILE_MOVE_CLUSTER_INFORMATION_SIZE    31
0277 #define FILE_QUOTA_INFORMATION_SIZE           32
0278 #define FILE_REPARSE_POINT_INFORMATION_SIZE   33
0279 #define FILE_NETWORK_OPEN_INFORMATION_SIZE    56
0280 #define FILE_ATTRIBUTE_TAG_INFORMATION_SIZE   8
0281 
0282 /* FS Info response  size */
0283 #define FS_DEVICE_INFORMATION_SIZE     8
0284 #define FS_ATTRIBUTE_INFORMATION_SIZE  16
0285 #define FS_VOLUME_INFORMATION_SIZE     24
0286 #define FS_SIZE_INFORMATION_SIZE       24
0287 #define FS_FULL_SIZE_INFORMATION_SIZE  32
0288 #define FS_SECTOR_SIZE_INFORMATION_SIZE 28
0289 #define FS_OBJECT_ID_INFORMATION_SIZE 64
0290 #define FS_CONTROL_INFORMATION_SIZE 48
0291 #define FS_POSIX_INFORMATION_SIZE 56
0292 
0293 /* FS_ATTRIBUTE_File_System_Name */
0294 #define FS_TYPE_SUPPORT_SIZE   44
0295 struct fs_type_info {
0296     char        *fs_name;
0297     long        magic_number;
0298 } __packed;
0299 
0300 /*
0301  *  PDU query infolevel structure definitions
0302  *  BB consider moving to a different header
0303  */
0304 
0305 struct smb2_file_access_info {
0306     __le32 AccessFlags;
0307 } __packed;
0308 
0309 struct smb2_file_alignment_info {
0310     __le32 AlignmentRequirement;
0311 } __packed;
0312 
0313 struct smb2_file_basic_info { /* data block encoding of response to level 18 */
0314     __le64 CreationTime;    /* Beginning of FILE_BASIC_INFO equivalent */
0315     __le64 LastAccessTime;
0316     __le64 LastWriteTime;
0317     __le64 ChangeTime;
0318     __le32 Attributes;
0319     __u32  Pad1;        /* End of FILE_BASIC_INFO_INFO equivalent */
0320 } __packed;
0321 
0322 struct smb2_file_alt_name_info {
0323     __le32 FileNameLength;
0324     char FileName[];
0325 } __packed;
0326 
0327 struct smb2_file_stream_info {
0328     __le32  NextEntryOffset;
0329     __le32  StreamNameLength;
0330     __le64 StreamSize;
0331     __le64 StreamAllocationSize;
0332     char   StreamName[];
0333 } __packed;
0334 
0335 struct smb2_file_ntwrk_info {
0336     __le64 CreationTime;
0337     __le64 LastAccessTime;
0338     __le64 LastWriteTime;
0339     __le64 ChangeTime;
0340     __le64 AllocationSize;
0341     __le64 EndOfFile;
0342     __le32 Attributes;
0343     __le32 Reserved;
0344 } __packed;
0345 
0346 struct smb2_file_standard_info {
0347     __le64 AllocationSize;
0348     __le64 EndOfFile;
0349     __le32 NumberOfLinks;   /* hard links */
0350     __u8   DeletePending;
0351     __u8   Directory;
0352     __le16 Reserved;
0353 } __packed; /* level 18 Query */
0354 
0355 struct smb2_file_ea_info {
0356     __le32 EASize;
0357 } __packed;
0358 
0359 struct smb2_file_alloc_info {
0360     __le64 AllocationSize;
0361 } __packed;
0362 
0363 struct smb2_file_disposition_info {
0364     __u8 DeletePending;
0365 } __packed;
0366 
0367 struct smb2_file_pos_info {
0368     __le64 CurrentByteOffset;
0369 } __packed;
0370 
0371 #define FILE_MODE_INFO_MASK cpu_to_le32(0x0000100e)
0372 
0373 struct smb2_file_mode_info {
0374     __le32 Mode;
0375 } __packed;
0376 
0377 #define COMPRESSION_FORMAT_NONE 0x0000
0378 #define COMPRESSION_FORMAT_LZNT1 0x0002
0379 
0380 struct smb2_file_comp_info {
0381     __le64 CompressedFileSize;
0382     __le16 CompressionFormat;
0383     __u8 CompressionUnitShift;
0384     __u8 ChunkShift;
0385     __u8 ClusterShift;
0386     __u8 Reserved[3];
0387 } __packed;
0388 
0389 struct smb2_file_attr_tag_info {
0390     __le32 FileAttributes;
0391     __le32 ReparseTag;
0392 } __packed;
0393 
0394 #define SL_RESTART_SCAN 0x00000001
0395 #define SL_RETURN_SINGLE_ENTRY  0x00000002
0396 #define SL_INDEX_SPECIFIED  0x00000004
0397 
0398 struct smb2_ea_info_req {
0399     __le32 NextEntryOffset;
0400     __u8   EaNameLength;
0401     char name[1];
0402 } __packed; /* level 15 Query */
0403 
0404 struct smb2_ea_info {
0405     __le32 NextEntryOffset;
0406     __u8   Flags;
0407     __u8   EaNameLength;
0408     __le16 EaValueLength;
0409     char name[1];
0410     /* optionally followed by value */
0411 } __packed; /* level 15 Query */
0412 
0413 struct create_ea_buf_req {
0414     struct create_context ccontext;
0415     __u8   Name[8];
0416     struct smb2_ea_info ea;
0417 } __packed;
0418 
0419 struct create_sd_buf_req {
0420     struct create_context ccontext;
0421     __u8   Name[8];
0422     struct smb_ntsd ntsd;
0423 } __packed;
0424 
0425 struct smb2_posix_info {
0426     __le32 NextEntryOffset;
0427     __u32 Ignored;
0428     __le64 CreationTime;
0429     __le64 LastAccessTime;
0430     __le64 LastWriteTime;
0431     __le64 ChangeTime;
0432     __le64 EndOfFile;
0433     __le64 AllocationSize;
0434     __le32 DosAttributes;
0435     __le64 Inode;
0436     __le32 DeviceId;
0437     __le32 Zero;
0438     /* beginning of POSIX Create Context Response */
0439     __le32 HardLinks;
0440     __le32 ReparseTag;
0441     __le32 Mode;
0442     u8 SidBuffer[40];
0443     __le32 name_len;
0444     u8 name[1];
0445     /*
0446      * var sized owner SID
0447      * var sized group SID
0448      * le32 filenamelength
0449      * u8  filename[]
0450      */
0451 } __packed;
0452 
0453 /* functions */
0454 void init_smb2_1_server(struct ksmbd_conn *conn);
0455 void init_smb3_0_server(struct ksmbd_conn *conn);
0456 void init_smb3_02_server(struct ksmbd_conn *conn);
0457 int init_smb3_11_server(struct ksmbd_conn *conn);
0458 
0459 void init_smb2_max_read_size(unsigned int sz);
0460 void init_smb2_max_write_size(unsigned int sz);
0461 void init_smb2_max_trans_size(unsigned int sz);
0462 void init_smb2_max_credits(unsigned int sz);
0463 
0464 bool is_smb2_neg_cmd(struct ksmbd_work *work);
0465 bool is_smb2_rsp(struct ksmbd_work *work);
0466 
0467 u16 get_smb2_cmd_val(struct ksmbd_work *work);
0468 void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err);
0469 int init_smb2_rsp_hdr(struct ksmbd_work *work);
0470 int smb2_allocate_rsp_buf(struct ksmbd_work *work);
0471 bool is_chained_smb2_message(struct ksmbd_work *work);
0472 int init_smb2_neg_rsp(struct ksmbd_work *work);
0473 void smb2_set_err_rsp(struct ksmbd_work *work);
0474 int smb2_check_user_session(struct ksmbd_work *work);
0475 int smb2_get_ksmbd_tcon(struct ksmbd_work *work);
0476 bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command);
0477 int smb2_check_sign_req(struct ksmbd_work *work);
0478 void smb2_set_sign_rsp(struct ksmbd_work *work);
0479 int smb3_check_sign_req(struct ksmbd_work *work);
0480 void smb3_set_sign_rsp(struct ksmbd_work *work);
0481 int find_matching_smb2_dialect(int start_index, __le16 *cli_dialects,
0482                    __le16 dialects_count);
0483 struct file_lock *smb_flock_init(struct file *f);
0484 int setup_async_work(struct ksmbd_work *work, void (*fn)(void **),
0485              void **arg);
0486 void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status);
0487 struct channel *lookup_chann_list(struct ksmbd_session *sess,
0488                   struct ksmbd_conn *conn);
0489 void smb3_preauth_hash_rsp(struct ksmbd_work *work);
0490 bool smb3_is_transform_hdr(void *buf);
0491 int smb3_decrypt_req(struct ksmbd_work *work);
0492 int smb3_encrypt_resp(struct ksmbd_work *work);
0493 bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work);
0494 int smb2_set_rsp_credits(struct ksmbd_work *work);
0495 
0496 /* smb2 misc functions */
0497 int ksmbd_smb2_check_message(struct ksmbd_work *work);
0498 
0499 /* smb2 command handlers */
0500 int smb2_handle_negotiate(struct ksmbd_work *work);
0501 int smb2_negotiate_request(struct ksmbd_work *work);
0502 int smb2_sess_setup(struct ksmbd_work *work);
0503 int smb2_tree_connect(struct ksmbd_work *work);
0504 int smb2_tree_disconnect(struct ksmbd_work *work);
0505 int smb2_session_logoff(struct ksmbd_work *work);
0506 int smb2_open(struct ksmbd_work *work);
0507 int smb2_query_info(struct ksmbd_work *work);
0508 int smb2_query_dir(struct ksmbd_work *work);
0509 int smb2_close(struct ksmbd_work *work);
0510 int smb2_echo(struct ksmbd_work *work);
0511 int smb2_set_info(struct ksmbd_work *work);
0512 int smb2_read(struct ksmbd_work *work);
0513 int smb2_write(struct ksmbd_work *work);
0514 int smb2_flush(struct ksmbd_work *work);
0515 int smb2_cancel(struct ksmbd_work *work);
0516 int smb2_lock(struct ksmbd_work *work);
0517 int smb2_ioctl(struct ksmbd_work *work);
0518 int smb2_oplock_break(struct ksmbd_work *work);
0519 int smb2_notify(struct ksmbd_work *ksmbd_work);
0520 
0521 /*
0522  * Get the body of the smb2 message excluding the 4 byte rfc1002 headers
0523  * from request/response buffer.
0524  */
0525 static inline void *smb2_get_msg(void *buf)
0526 {
0527     return buf + 4;
0528 }
0529 
0530 #endif  /* _SMB2PDU_H */