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_basic_info { /* data block encoding of response to level 18 */ 190 __le64 CreationTime; /* Beginning of FILE_BASIC_INFO equivalent */ 191 __le64 LastAccessTime; 192 __le64 LastWriteTime; 193 __le64 ChangeTime; 194 __le32 Attributes; 195 __u32 Pad1; /* End of FILE_BASIC_INFO_INFO equivalent */ 196 } __packed; 197 198 struct smb2_file_alt_name_info { 199 __le32 FileNameLength; 200 char FileName[]; 201 } __packed; 202 203 struct smb2_file_stream_info { 204 __le32 NextEntryOffset; 205 __le32 StreamNameLength; 206 __le64 StreamSize; 207 __le64 StreamAllocationSize; 208 char StreamName[]; 209 } __packed; 210 211 struct smb2_file_standard_info { 212 __le64 AllocationSize; 213 __le64 EndOfFile; 214 __le32 NumberOfLinks; /* hard links */ 215 __u8 DeletePending; 216 __u8 Directory; 217 __le16 Reserved; 218 } __packed; /* level 18 Query */ 219 220 struct smb2_file_ea_info { 221 __le32 EASize; 222 } __packed; 223 224 struct smb2_file_alloc_info { 225 __le64 AllocationSize; 226 } __packed; 227 228 struct smb2_file_disposition_info { 229 __u8 DeletePending; 230 } __packed; 231 232 struct smb2_file_pos_info { 233 __le64 CurrentByteOffset; 234 } __packed; 235 236 #define FILE_MODE_INFO_MASK cpu_to_le32(0x0000100e) 237 238 struct smb2_file_mode_info { 239 __le32 Mode; 240 } __packed; 241 242 #define COMPRESSION_FORMAT_NONE 0x0000 243 #define COMPRESSION_FORMAT_LZNT1 0x0002 244 245 struct smb2_file_comp_info { 246 __le64 CompressedFileSize; 247 __le16 CompressionFormat; 248 __u8 CompressionUnitShift; 249 __u8 ChunkShift; 250 __u8 ClusterShift; 251 __u8 Reserved[3]; 252 } __packed; 253 254 struct smb2_file_attr_tag_info { 255 __le32 FileAttributes; 256 __le32 ReparseTag; 257 } __packed; 258 259 #define SL_RESTART_SCAN 0x00000001 260 #define SL_RETURN_SINGLE_ENTRY 0x00000002 261 #define SL_INDEX_SPECIFIED 0x00000004 262 263 struct smb2_ea_info_req { 264 __le32 NextEntryOffset; 265 __u8 EaNameLength; 266 char name[]; 267 } __packed; /* level 15 Query */ 268 269 struct smb2_ea_info { 270 __le32 NextEntryOffset; 271 __u8 Flags; 272 __u8 EaNameLength; 273 __le16 EaValueLength; 274 char name[]; 275 /* optionally followed by value */ 276 } __packed; /* level 15 Query */ 277 278 struct create_ea_buf_req { 279 struct create_context_hdr ccontext; 280 __u8 Name[8]; 281 struct smb2_ea_info ea; 282 } __packed; 283 284 struct create_sd_buf_req { 285 struct create_context_hdr ccontext; 286 __u8 Name[8]; 287 struct smb_ntsd ntsd; 288 } __packed; 289 290 struct smb2_posix_info { 291 __le32 NextEntryOffset; 292 __u32 Ignored; 293 __le64 CreationTime; 294 __le64 LastAccessTime; 295 __le64 LastWriteTime; 296 __le64 ChangeTime; 297 __le64 EndOfFile; 298 __le64 AllocationSize; 299 __le32 DosAttributes; 300 __le64 Inode; 301 __le32 DeviceId; 302 __le32 Zero; 303 /* beginning of POSIX Create Context Response */ 304 __le32 HardLinks; 305 __le32 ReparseTag; 306 __le32 Mode; 307 /* SidBuffer contain two sids (UNIX user sid(16), UNIX group sid(16)) */ 308 u8 SidBuffer[32]; 309 __le32 name_len; 310 u8 name[]; 311 /* 312 * var sized owner SID 313 * var sized group SID 314 * le32 filenamelength 315 * u8 filename[] 316 */ 317 } __packed; 318 319 /* functions */ 320 void init_smb2_1_server(struct ksmbd_conn *conn); 321 void init_smb3_0_server(struct ksmbd_conn *conn); 322 void init_smb3_02_server(struct ksmbd_conn *conn); 323 int init_smb3_11_server(struct ksmbd_conn *conn); 324 325 void init_smb2_max_read_size(unsigned int sz); 326 void init_smb2_max_write_size(unsigned int sz); 327 void init_smb2_max_trans_size(unsigned int sz); 328 void init_smb2_max_credits(unsigned int sz); 329 330 bool is_smb2_neg_cmd(struct ksmbd_work *work); 331 bool is_smb2_rsp(struct ksmbd_work *work); 332 333 u16 get_smb2_cmd_val(struct ksmbd_work *work); 334 void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err); 335 int init_smb2_rsp_hdr(struct ksmbd_work *work); 336 int smb2_allocate_rsp_buf(struct ksmbd_work *work); 337 bool is_chained_smb2_message(struct ksmbd_work *work); 338 int init_smb2_neg_rsp(struct ksmbd_work *work); 339 void smb2_set_err_rsp(struct ksmbd_work *work); 340 int smb2_check_user_session(struct ksmbd_work *work); 341 int smb2_get_ksmbd_tcon(struct ksmbd_work *work); 342 bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command); 343 int smb2_check_sign_req(struct ksmbd_work *work); 344 void smb2_set_sign_rsp(struct ksmbd_work *work); 345 int smb3_check_sign_req(struct ksmbd_work *work); 346 void smb3_set_sign_rsp(struct ksmbd_work *work); 347 int find_matching_smb2_dialect(int start_index, __le16 *cli_dialects, 348 __le16 dialects_count); 349 struct file_lock *smb_flock_init(struct file *f); 350 int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), 351 void **arg); 352 void release_async_work(struct ksmbd_work *work); 353 void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status); 354 struct channel *lookup_chann_list(struct ksmbd_session *sess, 355 struct ksmbd_conn *conn); 356 void smb3_preauth_hash_rsp(struct ksmbd_work *work); 357 bool smb3_is_transform_hdr(void *buf); 358 int smb3_decrypt_req(struct ksmbd_work *work); 359 int smb3_encrypt_resp(struct ksmbd_work *work); 360 bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work); 361 int smb2_set_rsp_credits(struct ksmbd_work *work); 362 bool smb3_encryption_negotiated(struct ksmbd_conn *conn); 363 364 /* smb2 misc functions */ 365 int ksmbd_smb2_check_message(struct ksmbd_work *work); 366 367 /* smb2 command handlers */ 368 int smb2_handle_negotiate(struct ksmbd_work *work); 369 int smb2_negotiate_request(struct ksmbd_work *work); 370 int smb2_sess_setup(struct ksmbd_work *work); 371 int smb2_tree_connect(struct ksmbd_work *work); 372 int smb2_tree_disconnect(struct ksmbd_work *work); 373 int smb2_session_logoff(struct ksmbd_work *work); 374 int smb2_open(struct ksmbd_work *work); 375 int smb2_query_info(struct ksmbd_work *work); 376 int smb2_query_dir(struct ksmbd_work *work); 377 int smb2_close(struct ksmbd_work *work); 378 int smb2_echo(struct ksmbd_work *work); 379 int smb2_set_info(struct ksmbd_work *work); 380 int smb2_read(struct ksmbd_work *work); 381 int smb2_write(struct ksmbd_work *work); 382 int smb2_flush(struct ksmbd_work *work); 383 int smb2_cancel(struct ksmbd_work *work); 384 int smb2_lock(struct ksmbd_work *work); 385 int smb2_ioctl(struct ksmbd_work *work); 386 int smb2_oplock_break(struct ksmbd_work *work); 387 int smb2_notify(struct ksmbd_work *ksmbd_work); 388 389 #define POSIX_TYPE_FILE 0 390 #define POSIX_TYPE_DIR 1 391 #define POSIX_TYPE_SYMLINK 2 392 #define POSIX_TYPE_CHARDEV 3 393 #define POSIX_TYPE_BLKDEV 4 394 #define POSIX_TYPE_FIFO 5 395 #define POSIX_TYPE_SOCKET 6 396 397 #define POSIX_FILETYPE_SHIFT 12 398 399 #endif /* _SMB2PDU_H */ 400