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_H 8 #define _SMB2PDU_H 9 10 #include "ntlmssp.h" 11 #include "smbacl.h" 12 13 /*Create Action Flags*/ 14 #define FILE_SUPERSEDED 0x00000000 15 #define FILE_OPENED 0x00000001 16 #define FILE_CREATED 0x00000002 17 #define FILE_OVERWRITTEN 0x00000003 18 19 /* SMB2 Max Credits */ 20 #define SMB2_MAX_CREDITS 8192 21 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 SMB2 36 * command code name for the struct. Note that structures must be packed. 37 * 38 */ 39 40 struct 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_KERBEROS5 49 /* sizeof(struct smb2_negotiate_rsp) = 50 * header(64) + response(64) + GSS_LENGTH(96) + GSS_PADDING(0) 51 */ 52 #define OFFSET_OF_NEG_CONTEXT 0xe0 53 #else 54 /* sizeof(struct smb2_negotiate_rsp) = 55 * header(64) + response(64) + GSS_LENGTH(74) + GSS_PADDING(6) 56 */ 57 #define OFFSET_OF_NEG_CONTEXT 0xd0 58 #endif 59 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 66 /* Apple Defined Contexts */ 67 #define SMB2_CREATE_AAPL "AAPL" 68 69 #define DURABLE_HANDLE_MAX_TIMEOUT 300000 70 71 struct create_alloc_size_req { 72 struct create_context_hdr ccontext; 73 __u8 Name[8]; 74 __le64 AllocationSize; 75 } __packed; 76 77 struct create_durable_rsp { 78 struct create_context_hdr ccontext; 79 __u8 Name[8]; 80 union { 81 __u8 Reserved[8]; 82 __u64 data; 83 } Data; 84 } __packed; 85 86 /* 87 * See POSIX-SMB2 2.2.14.2.16 88 * Link: https://gitlab.com/samba-team/smb3-posix-spec/-/blob/master/smb3_posix_extensions.md 89 */ 90 struct create_posix_rsp { 91 struct create_context_hdr ccontext; 92 __u8 Name[16]; 93 __le32 nlink; 94 __le32 reparse_tag; 95 __le32 mode; 96 /* SidBuffer contain two sids(Domain sid(28), UNIX group sid(16)) */ 97 u8 SidBuffer[44]; 98 } __packed; 99 100 #define SMB2_0_IOCTL_IS_FSCTL 0x00000001 101 102 struct sockaddr_storage_rsp { 103 __le16 Family; 104 union { 105 struct smb_sockaddr_in addr4; 106 struct smb_sockaddr_in6 addr6; 107 }; 108 } __packed; 109 110 struct file_object_buf_type1_ioctl_rsp { 111 __u8 ObjectId[16]; 112 __u8 BirthVolumeId[16]; 113 __u8 BirthObjectId[16]; 114 __u8 DomainId[16]; 115 } __packed; 116 117 struct file_sparse { 118 __u8 SetSparse; 119 } __packed; 120 121 /* FILE Info response size */ 122 #define FILE_DIRECTORY_INFORMATION_SIZE 1 123 #define FILE_FULL_DIRECTORY_INFORMATION_SIZE 2 124 #define FILE_BOTH_DIRECTORY_INFORMATION_SIZE 3 125 #define FILE_BASIC_INFORMATION_SIZE 40 126 #define FILE_STANDARD_INFORMATION_SIZE 24 127 #define FILE_INTERNAL_INFORMATION_SIZE 8 128 #define FILE_EA_INFORMATION_SIZE 4 129 #define FILE_ACCESS_INFORMATION_SIZE 4 130 #define FILE_NAME_INFORMATION_SIZE 9 131 #define FILE_RENAME_INFORMATION_SIZE 10 132 #define FILE_LINK_INFORMATION_SIZE 11 133 #define FILE_NAMES_INFORMATION_SIZE 12 134 #define FILE_DISPOSITION_INFORMATION_SIZE 13 135 #define FILE_POSITION_INFORMATION_SIZE 14 136 #define FILE_FULL_EA_INFORMATION_SIZE 15 137 #define FILE_MODE_INFORMATION_SIZE 4 138 #define FILE_ALIGNMENT_INFORMATION_SIZE 4 139 #define FILE_ALL_INFORMATION_SIZE 104 140 #define FILE_ALLOCATION_INFORMATION_SIZE 19 141 #define FILE_END_OF_FILE_INFORMATION_SIZE 20 142 #define FILE_ALTERNATE_NAME_INFORMATION_SIZE 8 143 #define FILE_STREAM_INFORMATION_SIZE 32 144 #define FILE_PIPE_INFORMATION_SIZE 23 145 #define FILE_PIPE_LOCAL_INFORMATION_SIZE 24 146 #define FILE_PIPE_REMOTE_INFORMATION_SIZE 25 147 #define FILE_MAILSLOT_QUERY_INFORMATION_SIZE 26 148 #define FILE_MAILSLOT_SET_INFORMATION_SIZE 27 149 #define FILE_COMPRESSION_INFORMATION_SIZE 16 150 #define FILE_OBJECT_ID_INFORMATION_SIZE 29 151 /* Number 30 not defined in documents */ 152 #define FILE_MOVE_CLUSTER_INFORMATION_SIZE 31 153 #define FILE_QUOTA_INFORMATION_SIZE 32 154 #define FILE_REPARSE_POINT_INFORMATION_SIZE 33 155 #define FILE_NETWORK_OPEN_INFORMATION_SIZE 56 156 #define FILE_ATTRIBUTE_TAG_INFORMATION_SIZE 8 157 158 /* FS Info response size */ 159 #define FS_DEVICE_INFORMATION_SIZE 8 160 #define FS_ATTRIBUTE_INFORMATION_SIZE 16 161 #define FS_VOLUME_INFORMATION_SIZE 24 162 #define FS_SIZE_INFORMATION_SIZE 24 163 #define FS_FULL_SIZE_INFORMATION_SIZE 32 164 #define FS_SECTOR_SIZE_INFORMATION_SIZE 28 165 #define FS_OBJECT_ID_INFORMATION_SIZE 64 166 #define FS_CONTROL_INFORMATION_SIZE 48 167 #define FS_POSIX_INFORMATION_SIZE 56 168 169 /* FS_ATTRIBUTE_File_System_Name */ 170 #define FS_TYPE_SUPPORT_SIZE 44 171 struct fs_type_info { 172 char *fs_name; 173 long magic_number; 174 } __packed; 175 176 /* 177 * PDU query infolevel structure definitions 178 * BB consider moving to a different header 179 */ 180 181 struct smb2_file_access_info { 182 __le32 AccessFlags; 183 } __packed; 184 185 struct smb2_file_alignment_info { 186 __le32 AlignmentRequirement; 187 } __packed; 188 189 struct smb2_file_alt_name_info { 190 __le32 FileNameLength; 191 char FileName[]; 192 } __packed; 193 194 struct smb2_file_stream_info { 195 __le32 NextEntryOffset; 196 __le32 StreamNameLength; 197 __le64 StreamSize; 198 __le64 StreamAllocationSize; 199 char StreamName[]; 200 } __packed; 201 202 struct smb2_file_standard_info { 203 __le64 AllocationSize; 204 __le64 EndOfFile; 205 __le32 NumberOfLinks; /* hard links */ 206 __u8 DeletePending; 207 __u8 Directory; 208 __le16 Reserved; 209 } __packed; /* level 18 Query */ 210 211 struct smb2_file_ea_info { 212 __le32 EASize; 213 } __packed; 214 215 struct smb2_file_alloc_info { 216 __le64 AllocationSize; 217 } __packed; 218 219 struct smb2_file_disposition_info { 220 __u8 DeletePending; 221 } __packed; 222 223 struct smb2_file_pos_info { 224 __le64 CurrentByteOffset; 225 } __packed; 226 227 #define FILE_MODE_INFO_MASK cpu_to_le32(0x0000100e) 228 229 struct smb2_file_mode_info { 230 __le32 Mode; 231 } __packed; 232 233 #define COMPRESSION_FORMAT_NONE 0x0000 234 #define COMPRESSION_FORMAT_LZNT1 0x0002 235 236 struct smb2_file_comp_info { 237 __le64 CompressedFileSize; 238 __le16 CompressionFormat; 239 __u8 CompressionUnitShift; 240 __u8 ChunkShift; 241 __u8 ClusterShift; 242 __u8 Reserved[3]; 243 } __packed; 244 245 struct smb2_file_attr_tag_info { 246 __le32 FileAttributes; 247 __le32 ReparseTag; 248 } __packed; 249 250 #define SL_RESTART_SCAN 0x00000001 251 #define SL_RETURN_SINGLE_ENTRY 0x00000002 252 #define SL_INDEX_SPECIFIED 0x00000004 253 254 struct smb2_ea_info_req { 255 __le32 NextEntryOffset; 256 __u8 EaNameLength; 257 char name[]; 258 } __packed; /* level 15 Query */ 259 260 struct smb2_ea_info { 261 __le32 NextEntryOffset; 262 __u8 Flags; 263 __u8 EaNameLength; 264 __le16 EaValueLength; 265 char name[]; 266 /* optionally followed by value */ 267 } __packed; /* level 15 Query */ 268 269 struct create_ea_buf_req { 270 struct create_context_hdr ccontext; 271 __u8 Name[8]; 272 struct smb2_ea_info ea; 273 } __packed; 274 275 struct create_sd_buf_req { 276 struct create_context_hdr ccontext; 277 __u8 Name[8]; 278 struct smb_ntsd ntsd; 279 } __packed; 280 281 struct smb2_posix_info { 282 __le32 NextEntryOffset; 283 __u32 Ignored; 284 __le64 CreationTime; 285 __le64 LastAccessTime; 286 __le64 LastWriteTime; 287 __le64 ChangeTime; 288 __le64 EndOfFile; 289 __le64 AllocationSize; 290 __le32 DosAttributes; 291 __le64 Inode; 292 __le32 DeviceId; 293 __le32 Zero; 294 /* beginning of POSIX Create Context Response */ 295 __le32 HardLinks; 296 __le32 ReparseTag; 297 __le32 Mode; 298 /* SidBuffer contain two sids (UNIX user sid(16), UNIX group sid(16)) */ 299 u8 SidBuffer[32]; 300 __le32 name_len; 301 u8 name[]; 302 /* 303 * var sized owner SID 304 * var sized group SID 305 * le32 filenamelength 306 * u8 filename[] 307 */ 308 } __packed; 309 310 /* functions */ 311 void init_smb2_1_server(struct ksmbd_conn *conn); 312 void init_smb3_0_server(struct ksmbd_conn *conn); 313 void init_smb3_02_server(struct ksmbd_conn *conn); 314 int init_smb3_11_server(struct ksmbd_conn *conn); 315 316 void init_smb2_max_read_size(unsigned int sz); 317 void init_smb2_max_write_size(unsigned int sz); 318 void init_smb2_max_trans_size(unsigned int sz); 319 void init_smb2_max_credits(unsigned int sz); 320 321 bool is_smb2_neg_cmd(struct ksmbd_work *work); 322 bool is_smb2_rsp(struct ksmbd_work *work); 323 324 u16 get_smb2_cmd_val(struct ksmbd_work *work); 325 void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err); 326 int init_smb2_rsp_hdr(struct ksmbd_work *work); 327 int smb2_allocate_rsp_buf(struct ksmbd_work *work); 328 bool is_chained_smb2_message(struct ksmbd_work *work); 329 int init_smb2_neg_rsp(struct ksmbd_work *work); 330 void smb2_set_err_rsp(struct ksmbd_work *work); 331 int smb2_check_user_session(struct ksmbd_work *work); 332 int smb2_get_ksmbd_tcon(struct ksmbd_work *work); 333 bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command); 334 int smb2_check_sign_req(struct ksmbd_work *work); 335 void smb2_set_sign_rsp(struct ksmbd_work *work); 336 int smb3_check_sign_req(struct ksmbd_work *work); 337 void smb3_set_sign_rsp(struct ksmbd_work *work); 338 int find_matching_smb2_dialect(int start_index, __le16 *cli_dialects, 339 __le16 dialects_count); 340 struct file_lock *smb_flock_init(struct file *f); 341 int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), 342 void **arg); 343 void release_async_work(struct ksmbd_work *work); 344 void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status); 345 struct channel *lookup_chann_list(struct ksmbd_session *sess, 346 struct ksmbd_conn *conn); 347 void smb3_preauth_hash_rsp(struct ksmbd_work *work); 348 bool smb3_is_transform_hdr(void *buf); 349 int smb3_decrypt_req(struct ksmbd_work *work); 350 int smb3_encrypt_resp(struct ksmbd_work *work); 351 bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work); 352 int smb2_set_rsp_credits(struct ksmbd_work *work); 353 bool smb3_encryption_negotiated(struct ksmbd_conn *conn); 354 355 /* smb2 misc functions */ 356 int ksmbd_smb2_check_message(struct ksmbd_work *work); 357 358 /* smb2 command handlers */ 359 int smb2_handle_negotiate(struct ksmbd_work *work); 360 int smb2_negotiate_request(struct ksmbd_work *work); 361 int smb2_sess_setup(struct ksmbd_work *work); 362 int smb2_tree_connect(struct ksmbd_work *work); 363 int smb2_tree_disconnect(struct ksmbd_work *work); 364 int smb2_session_logoff(struct ksmbd_work *work); 365 int smb2_open(struct ksmbd_work *work); 366 int smb2_query_info(struct ksmbd_work *work); 367 int smb2_query_dir(struct ksmbd_work *work); 368 int smb2_close(struct ksmbd_work *work); 369 int smb2_echo(struct ksmbd_work *work); 370 int smb2_set_info(struct ksmbd_work *work); 371 int smb2_read(struct ksmbd_work *work); 372 int smb2_write(struct ksmbd_work *work); 373 int smb2_flush(struct ksmbd_work *work); 374 int smb2_cancel(struct ksmbd_work *work); 375 int smb2_lock(struct ksmbd_work *work); 376 int smb2_ioctl(struct ksmbd_work *work); 377 int smb2_oplock_break(struct ksmbd_work *work); 378 int smb2_notify(struct ksmbd_work *ksmbd_work); 379 380 #define POSIX_TYPE_FILE 0 381 #define POSIX_TYPE_DIR 1 382 #define POSIX_TYPE_SYMLINK 2 383 #define POSIX_TYPE_CHARDEV 3 384 #define POSIX_TYPE_BLKDEV 4 385 #define POSIX_TYPE_FIFO 5 386 #define POSIX_TYPE_SOCKET 6 387 388 #define POSIX_FILETYPE_SHIFT 12 389 390 #endif /* _SMB2PDU_H */ 391