506 lines · c
1/* SPDX-License-Identifier: GPL-2.0-or-later */2/*3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.5 */6 7#ifndef _SMB2PDU_H8#define _SMB2PDU_H9 10#include "ntlmssp.h"11#include "smbacl.h"12 13/*Create Action Flags*/14#define FILE_SUPERSEDED 0x0000000015#define FILE_OPENED 0x0000000116#define FILE_CREATED 0x0000000217#define FILE_OVERWRITTEN 0x0000000318 19/* SMB2 Max Credits */20#define SMB2_MAX_CREDITS 819221 22/* BB FIXME - analyze following length BB */23#define MAX_SMB2_HDR_SIZE 0x78 /* 4 len + 64 hdr + (2*24 wct) + 2 bct + 2 pad */24 25#define SMB21_DEFAULT_IOSIZE (1024 * 1024)26#define SMB3_DEFAULT_TRANS_SIZE (1024 * 1024)27#define SMB3_MIN_IOSIZE (64 * 1024)28#define SMB3_MAX_IOSIZE (8 * 1024 * 1024)29#define SMB3_MAX_MSGSIZE (4 * 4096)30 31/*32 * Definitions for SMB2 Protocol Data Units (network frames)33 *34 * See MS-SMB2.PDF specification for protocol details.35 * The Naming convention is the lower case version of the SMB236 * command code name for the struct. Note that structures must be packed.37 *38 */39 40struct preauth_integrity_info {41 /* PreAuth integrity Hash ID */42 __le16 Preauth_HashId;43 /* PreAuth integrity Hash Value */44 __u8 Preauth_HashValue[SMB2_PREAUTH_HASH_SIZE];45};46 47/* offset is sizeof smb2_negotiate_rsp but rounded up to 8 bytes. */48#ifdef CONFIG_SMB_SERVER_KERBEROS549/* sizeof(struct smb2_negotiate_rsp) =50 * header(64) + response(64) + GSS_LENGTH(96) + GSS_PADDING(0)51 */52#define OFFSET_OF_NEG_CONTEXT 0xe053#else54/* sizeof(struct smb2_negotiate_rsp) =55 * header(64) + response(64) + GSS_LENGTH(74) + GSS_PADDING(6)56 */57#define OFFSET_OF_NEG_CONTEXT 0xd058#endif59 60#define SMB2_SESSION_EXPIRED (0)61#define SMB2_SESSION_IN_PROGRESS BIT(0)62#define SMB2_SESSION_VALID BIT(1)63 64#define SMB2_SESSION_TIMEOUT (10 * HZ)65 66struct create_durable_req_v2 {67 struct create_context_hdr ccontext;68 __u8 Name[8];69 __le32 Timeout;70 __le32 Flags;71 __u8 Reserved[8];72 __u8 CreateGuid[16];73} __packed;74 75#define DURABLE_HANDLE_MAX_TIMEOUT 30000076 77struct create_durable_reconn_req {78 struct create_context_hdr ccontext;79 __u8 Name[8];80 union {81 __u8 Reserved[16];82 struct {83 __u64 PersistentFileId;84 __u64 VolatileFileId;85 } Fid;86 } Data;87} __packed;88 89struct create_durable_reconn_v2_req {90 struct create_context_hdr ccontext;91 __u8 Name[8];92 struct {93 __u64 PersistentFileId;94 __u64 VolatileFileId;95 } Fid;96 __u8 CreateGuid[16];97 __le32 Flags;98} __packed;99 100struct create_alloc_size_req {101 struct create_context_hdr ccontext;102 __u8 Name[8];103 __le64 AllocationSize;104} __packed;105 106struct create_durable_rsp {107 struct create_context_hdr ccontext;108 __u8 Name[8];109 union {110 __u8 Reserved[8];111 __u64 data;112 } Data;113} __packed;114 115/* See MS-SMB2 2.2.13.2.11 */116/* Flags */117#define SMB2_DHANDLE_FLAG_PERSISTENT 0x00000002118struct create_durable_v2_rsp {119 struct create_context_hdr ccontext;120 __u8 Name[8];121 __le32 Timeout;122 __le32 Flags;123} __packed;124 125/* equivalent of the contents of SMB3.1.1 POSIX open context response */126struct create_posix_rsp {127 struct create_context_hdr ccontext;128 __u8 Name[16];129 __le32 nlink;130 __le32 reparse_tag;131 __le32 mode;132 /* SidBuffer contain two sids(Domain sid(28), UNIX group sid(16)) */133 u8 SidBuffer[44];134} __packed;135 136struct smb2_buffer_desc_v1 {137 __le64 offset;138 __le32 token;139 __le32 length;140} __packed;141 142#define SMB2_0_IOCTL_IS_FSCTL 0x00000001143 144struct smb_sockaddr_in {145 __be16 Port;146 __be32 IPv4address;147 __u8 Reserved[8];148} __packed;149 150struct smb_sockaddr_in6 {151 __be16 Port;152 __be32 FlowInfo;153 __u8 IPv6address[16];154 __be32 ScopeId;155} __packed;156 157#define INTERNETWORK 0x0002158#define INTERNETWORKV6 0x0017159 160struct sockaddr_storage_rsp {161 __le16 Family;162 union {163 struct smb_sockaddr_in addr4;164 struct smb_sockaddr_in6 addr6;165 };166} __packed;167 168#define RSS_CAPABLE 0x00000001169#define RDMA_CAPABLE 0x00000002170 171struct network_interface_info_ioctl_rsp {172 __le32 Next; /* next interface. zero if this is last one */173 __le32 IfIndex;174 __le32 Capability; /* RSS or RDMA Capable */175 __le32 Reserved;176 __le64 LinkSpeed;177 char SockAddr_Storage[128];178} __packed;179 180struct file_object_buf_type1_ioctl_rsp {181 __u8 ObjectId[16];182 __u8 BirthVolumeId[16];183 __u8 BirthObjectId[16];184 __u8 DomainId[16];185} __packed;186 187struct resume_key_ioctl_rsp {188 __u64 ResumeKey[3];189 __le32 ContextLength;190 __u8 Context[4]; /* ignored, Windows sets to 4 bytes of zero */191} __packed;192 193struct srv_copychunk {194 __le64 SourceOffset;195 __le64 TargetOffset;196 __le32 Length;197 __le32 Reserved;198} __packed;199 200struct copychunk_ioctl_req {201 __le64 ResumeKey[3];202 __le32 ChunkCount;203 __le32 Reserved;204 struct srv_copychunk Chunks[] __counted_by_le(ChunkCount);205} __packed;206 207struct copychunk_ioctl_rsp {208 __le32 ChunksWritten;209 __le32 ChunkBytesWritten;210 __le32 TotalBytesWritten;211} __packed;212 213struct file_sparse {214 __u8 SetSparse;215} __packed;216 217/* FILE Info response size */218#define FILE_DIRECTORY_INFORMATION_SIZE 1219#define FILE_FULL_DIRECTORY_INFORMATION_SIZE 2220#define FILE_BOTH_DIRECTORY_INFORMATION_SIZE 3221#define FILE_BASIC_INFORMATION_SIZE 40222#define FILE_STANDARD_INFORMATION_SIZE 24223#define FILE_INTERNAL_INFORMATION_SIZE 8224#define FILE_EA_INFORMATION_SIZE 4225#define FILE_ACCESS_INFORMATION_SIZE 4226#define FILE_NAME_INFORMATION_SIZE 9227#define FILE_RENAME_INFORMATION_SIZE 10228#define FILE_LINK_INFORMATION_SIZE 11229#define FILE_NAMES_INFORMATION_SIZE 12230#define FILE_DISPOSITION_INFORMATION_SIZE 13231#define FILE_POSITION_INFORMATION_SIZE 14232#define FILE_FULL_EA_INFORMATION_SIZE 15233#define FILE_MODE_INFORMATION_SIZE 4234#define FILE_ALIGNMENT_INFORMATION_SIZE 4235#define FILE_ALL_INFORMATION_SIZE 104236#define FILE_ALLOCATION_INFORMATION_SIZE 19237#define FILE_END_OF_FILE_INFORMATION_SIZE 20238#define FILE_ALTERNATE_NAME_INFORMATION_SIZE 8239#define FILE_STREAM_INFORMATION_SIZE 32240#define FILE_PIPE_INFORMATION_SIZE 23241#define FILE_PIPE_LOCAL_INFORMATION_SIZE 24242#define FILE_PIPE_REMOTE_INFORMATION_SIZE 25243#define FILE_MAILSLOT_QUERY_INFORMATION_SIZE 26244#define FILE_MAILSLOT_SET_INFORMATION_SIZE 27245#define FILE_COMPRESSION_INFORMATION_SIZE 16246#define FILE_OBJECT_ID_INFORMATION_SIZE 29247/* Number 30 not defined in documents */248#define FILE_MOVE_CLUSTER_INFORMATION_SIZE 31249#define FILE_QUOTA_INFORMATION_SIZE 32250#define FILE_REPARSE_POINT_INFORMATION_SIZE 33251#define FILE_NETWORK_OPEN_INFORMATION_SIZE 56252#define FILE_ATTRIBUTE_TAG_INFORMATION_SIZE 8253 254/* FS Info response size */255#define FS_DEVICE_INFORMATION_SIZE 8256#define FS_ATTRIBUTE_INFORMATION_SIZE 16257#define FS_VOLUME_INFORMATION_SIZE 24258#define FS_SIZE_INFORMATION_SIZE 24259#define FS_FULL_SIZE_INFORMATION_SIZE 32260#define FS_SECTOR_SIZE_INFORMATION_SIZE 28261#define FS_OBJECT_ID_INFORMATION_SIZE 64262#define FS_CONTROL_INFORMATION_SIZE 48263#define FS_POSIX_INFORMATION_SIZE 56264 265/* FS_ATTRIBUTE_File_System_Name */266#define FS_TYPE_SUPPORT_SIZE 44267struct fs_type_info {268 char *fs_name;269 long magic_number;270} __packed;271 272/*273 * PDU query infolevel structure definitions274 * BB consider moving to a different header275 */276 277struct smb2_file_access_info {278 __le32 AccessFlags;279} __packed;280 281struct smb2_file_alignment_info {282 __le32 AlignmentRequirement;283} __packed;284 285struct smb2_file_basic_info { /* data block encoding of response to level 18 */286 __le64 CreationTime; /* Beginning of FILE_BASIC_INFO equivalent */287 __le64 LastAccessTime;288 __le64 LastWriteTime;289 __le64 ChangeTime;290 __le32 Attributes;291 __u32 Pad1; /* End of FILE_BASIC_INFO_INFO equivalent */292} __packed;293 294struct smb2_file_alt_name_info {295 __le32 FileNameLength;296 char FileName[];297} __packed;298 299struct smb2_file_stream_info {300 __le32 NextEntryOffset;301 __le32 StreamNameLength;302 __le64 StreamSize;303 __le64 StreamAllocationSize;304 char StreamName[];305} __packed;306 307struct smb2_file_ntwrk_info {308 __le64 CreationTime;309 __le64 LastAccessTime;310 __le64 LastWriteTime;311 __le64 ChangeTime;312 __le64 AllocationSize;313 __le64 EndOfFile;314 __le32 Attributes;315 __le32 Reserved;316} __packed;317 318struct smb2_file_standard_info {319 __le64 AllocationSize;320 __le64 EndOfFile;321 __le32 NumberOfLinks; /* hard links */322 __u8 DeletePending;323 __u8 Directory;324 __le16 Reserved;325} __packed; /* level 18 Query */326 327struct smb2_file_ea_info {328 __le32 EASize;329} __packed;330 331struct smb2_file_alloc_info {332 __le64 AllocationSize;333} __packed;334 335struct smb2_file_disposition_info {336 __u8 DeletePending;337} __packed;338 339struct smb2_file_pos_info {340 __le64 CurrentByteOffset;341} __packed;342 343#define FILE_MODE_INFO_MASK cpu_to_le32(0x0000100e)344 345struct smb2_file_mode_info {346 __le32 Mode;347} __packed;348 349#define COMPRESSION_FORMAT_NONE 0x0000350#define COMPRESSION_FORMAT_LZNT1 0x0002351 352struct smb2_file_comp_info {353 __le64 CompressedFileSize;354 __le16 CompressionFormat;355 __u8 CompressionUnitShift;356 __u8 ChunkShift;357 __u8 ClusterShift;358 __u8 Reserved[3];359} __packed;360 361struct smb2_file_attr_tag_info {362 __le32 FileAttributes;363 __le32 ReparseTag;364} __packed;365 366#define SL_RESTART_SCAN 0x00000001367#define SL_RETURN_SINGLE_ENTRY 0x00000002368#define SL_INDEX_SPECIFIED 0x00000004369 370struct smb2_ea_info_req {371 __le32 NextEntryOffset;372 __u8 EaNameLength;373 char name[];374} __packed; /* level 15 Query */375 376struct smb2_ea_info {377 __le32 NextEntryOffset;378 __u8 Flags;379 __u8 EaNameLength;380 __le16 EaValueLength;381 char name[];382 /* optionally followed by value */383} __packed; /* level 15 Query */384 385struct create_ea_buf_req {386 struct create_context_hdr ccontext;387 __u8 Name[8];388 struct smb2_ea_info ea;389} __packed;390 391struct create_sd_buf_req {392 struct create_context_hdr ccontext;393 __u8 Name[8];394 struct smb_ntsd ntsd;395} __packed;396 397struct smb2_posix_info {398 __le32 NextEntryOffset;399 __u32 Ignored;400 __le64 CreationTime;401 __le64 LastAccessTime;402 __le64 LastWriteTime;403 __le64 ChangeTime;404 __le64 EndOfFile;405 __le64 AllocationSize;406 __le32 DosAttributes;407 __le64 Inode;408 __le32 DeviceId;409 __le32 Zero;410 /* beginning of POSIX Create Context Response */411 __le32 HardLinks;412 __le32 ReparseTag;413 __le32 Mode;414 /* SidBuffer contain two sids (UNIX user sid(16), UNIX group sid(16)) */415 u8 SidBuffer[32];416 __le32 name_len;417 u8 name[];418 /*419 * var sized owner SID420 * var sized group SID421 * le32 filenamelength422 * u8 filename[]423 */424} __packed;425 426/* functions */427void init_smb2_1_server(struct ksmbd_conn *conn);428void init_smb3_0_server(struct ksmbd_conn *conn);429void init_smb3_02_server(struct ksmbd_conn *conn);430int init_smb3_11_server(struct ksmbd_conn *conn);431 432void init_smb2_max_read_size(unsigned int sz);433void init_smb2_max_write_size(unsigned int sz);434void init_smb2_max_trans_size(unsigned int sz);435void init_smb2_max_credits(unsigned int sz);436 437bool is_smb2_neg_cmd(struct ksmbd_work *work);438bool is_smb2_rsp(struct ksmbd_work *work);439 440u16 get_smb2_cmd_val(struct ksmbd_work *work);441void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err);442int init_smb2_rsp_hdr(struct ksmbd_work *work);443int smb2_allocate_rsp_buf(struct ksmbd_work *work);444bool is_chained_smb2_message(struct ksmbd_work *work);445int init_smb2_neg_rsp(struct ksmbd_work *work);446void smb2_set_err_rsp(struct ksmbd_work *work);447int smb2_check_user_session(struct ksmbd_work *work);448int smb2_get_ksmbd_tcon(struct ksmbd_work *work);449bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command);450int smb2_check_sign_req(struct ksmbd_work *work);451void smb2_set_sign_rsp(struct ksmbd_work *work);452int smb3_check_sign_req(struct ksmbd_work *work);453void smb3_set_sign_rsp(struct ksmbd_work *work);454int find_matching_smb2_dialect(int start_index, __le16 *cli_dialects,455 __le16 dialects_count);456struct file_lock *smb_flock_init(struct file *f);457int setup_async_work(struct ksmbd_work *work, void (*fn)(void **),458 void **arg);459void release_async_work(struct ksmbd_work *work);460void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status);461struct channel *lookup_chann_list(struct ksmbd_session *sess,462 struct ksmbd_conn *conn);463void smb3_preauth_hash_rsp(struct ksmbd_work *work);464bool smb3_is_transform_hdr(void *buf);465int smb3_decrypt_req(struct ksmbd_work *work);466int smb3_encrypt_resp(struct ksmbd_work *work);467bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work);468int smb2_set_rsp_credits(struct ksmbd_work *work);469bool smb3_encryption_negotiated(struct ksmbd_conn *conn);470 471/* smb2 misc functions */472int ksmbd_smb2_check_message(struct ksmbd_work *work);473 474/* smb2 command handlers */475int smb2_handle_negotiate(struct ksmbd_work *work);476int smb2_negotiate_request(struct ksmbd_work *work);477int smb2_sess_setup(struct ksmbd_work *work);478int smb2_tree_connect(struct ksmbd_work *work);479int smb2_tree_disconnect(struct ksmbd_work *work);480int smb2_session_logoff(struct ksmbd_work *work);481int smb2_open(struct ksmbd_work *work);482int smb2_query_info(struct ksmbd_work *work);483int smb2_query_dir(struct ksmbd_work *work);484int smb2_close(struct ksmbd_work *work);485int smb2_echo(struct ksmbd_work *work);486int smb2_set_info(struct ksmbd_work *work);487int smb2_read(struct ksmbd_work *work);488int smb2_write(struct ksmbd_work *work);489int smb2_flush(struct ksmbd_work *work);490int smb2_cancel(struct ksmbd_work *work);491int smb2_lock(struct ksmbd_work *work);492int smb2_ioctl(struct ksmbd_work *work);493int smb2_oplock_break(struct ksmbd_work *work);494int smb2_notify(struct ksmbd_work *ksmbd_work);495 496/*497 * Get the body of the smb2 message excluding the 4 byte rfc1002 headers498 * from request/response buffer.499 */500static inline void *smb2_get_msg(void *buf)501{502 return buf + 4;503}504 505#endif /* _SMB2PDU_H */506