smb_common.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2018 Samsung Electronics Co., Ltd.
  4. */
  5. #ifndef __SMB_COMMON_H__
  6. #define __SMB_COMMON_H__
  7. #include <linux/kernel.h>
  8. #include "glob.h"
  9. #include "nterr.h"
  10. #include "../common/smb2pdu.h"
  11. #include "smb2pdu.h"
  12. /* ksmbd's Specific ERRNO */
  13. #define ESHARE 50000
  14. #define SMB1_PROT 0
  15. #define SMB2_PROT 1
  16. #define SMB21_PROT 2
  17. /* multi-protocol negotiate request */
  18. #define SMB2X_PROT 3
  19. #define SMB30_PROT 4
  20. #define SMB302_PROT 5
  21. #define SMB311_PROT 6
  22. #define BAD_PROT 0xFFFF
  23. #define SMB1_VERSION_STRING "1.0"
  24. #define SMB20_VERSION_STRING "2.0"
  25. #define SMB21_VERSION_STRING "2.1"
  26. #define SMB30_VERSION_STRING "3.0"
  27. #define SMB302_VERSION_STRING "3.02"
  28. #define SMB311_VERSION_STRING "3.1.1"
  29. #define SMB_ECHO_INTERVAL (60 * HZ)
  30. #define CIFS_DEFAULT_IOSIZE (64 * 1024)
  31. #define MAX_CIFS_SMALL_BUFFER_SIZE 448 /* big enough for most */
  32. #define MAX_STREAM_PROT_LEN 0x00FFFFFF
  33. /* Responses when opening a file. */
  34. #define F_SUPERSEDED 0
  35. #define F_OPENED 1
  36. #define F_CREATED 2
  37. #define F_OVERWRITTEN 3
  38. /*
  39. * File Attribute flags
  40. */
  41. #define ATTR_POSIX_SEMANTICS 0x01000000
  42. #define ATTR_BACKUP_SEMANTICS 0x02000000
  43. #define ATTR_DELETE_ON_CLOSE 0x04000000
  44. #define ATTR_SEQUENTIAL_SCAN 0x08000000
  45. #define ATTR_RANDOM_ACCESS 0x10000000
  46. #define ATTR_NO_BUFFERING 0x20000000
  47. #define ATTR_WRITE_THROUGH 0x80000000
  48. /* List of FileSystemAttributes - see 2.5.1 of MS-FSCC */
  49. #define FILE_SUPPORTS_SPARSE_VDL 0x10000000 /* faster nonsparse extend */
  50. #define FILE_SUPPORTS_BLOCK_REFCOUNTING 0x08000000 /* allow ioctl dup extents */
  51. #define FILE_SUPPORT_INTEGRITY_STREAMS 0x04000000
  52. #define FILE_SUPPORTS_USN_JOURNAL 0x02000000
  53. #define FILE_SUPPORTS_OPEN_BY_FILE_ID 0x01000000
  54. #define FILE_SUPPORTS_EXTENDED_ATTRIBUTES 0x00800000
  55. #define FILE_SUPPORTS_HARD_LINKS 0x00400000
  56. #define FILE_SUPPORTS_TRANSACTIONS 0x00200000
  57. #define FILE_SEQUENTIAL_WRITE_ONCE 0x00100000
  58. #define FILE_READ_ONLY_VOLUME 0x00080000
  59. #define FILE_NAMED_STREAMS 0x00040000
  60. #define FILE_SUPPORTS_ENCRYPTION 0x00020000
  61. #define FILE_SUPPORTS_OBJECT_IDS 0x00010000
  62. #define FILE_VOLUME_IS_COMPRESSED 0x00008000
  63. #define FILE_SUPPORTS_REMOTE_STORAGE 0x00000100
  64. #define FILE_SUPPORTS_REPARSE_POINTS 0x00000080
  65. #define FILE_SUPPORTS_SPARSE_FILES 0x00000040
  66. #define FILE_VOLUME_QUOTAS 0x00000020
  67. #define FILE_FILE_COMPRESSION 0x00000010
  68. #define FILE_PERSISTENT_ACLS 0x00000008
  69. #define FILE_UNICODE_ON_DISK 0x00000004
  70. #define FILE_CASE_PRESERVED_NAMES 0x00000002
  71. #define FILE_CASE_SENSITIVE_SEARCH 0x00000001
  72. #define FILE_READ_DATA 0x00000001 /* Data can be read from the file */
  73. #define FILE_WRITE_DATA 0x00000002 /* Data can be written to the file */
  74. #define FILE_APPEND_DATA 0x00000004 /* Data can be appended to the file */
  75. #define FILE_READ_EA 0x00000008 /* Extended attributes associated */
  76. /* with the file can be read */
  77. #define FILE_WRITE_EA 0x00000010 /* Extended attributes associated */
  78. /* with the file can be written */
  79. #define FILE_EXECUTE 0x00000020 /*Data can be read into memory from */
  80. /* the file using system paging I/O */
  81. #define FILE_DELETE_CHILD 0x00000040
  82. #define FILE_READ_ATTRIBUTES 0x00000080 /* Attributes associated with the */
  83. /* file can be read */
  84. #define FILE_WRITE_ATTRIBUTES 0x00000100 /* Attributes associated with the */
  85. /* file can be written */
  86. #define DELETE 0x00010000 /* The file can be deleted */
  87. #define READ_CONTROL 0x00020000 /* The access control list and */
  88. /* ownership associated with the */
  89. /* file can be read */
  90. #define WRITE_DAC 0x00040000 /* The access control list and */
  91. /* ownership associated with the */
  92. /* file can be written. */
  93. #define WRITE_OWNER 0x00080000 /* Ownership information associated */
  94. /* with the file can be written */
  95. #define SYNCHRONIZE 0x00100000 /* The file handle can waited on to */
  96. /* synchronize with the completion */
  97. /* of an input/output request */
  98. #define GENERIC_ALL 0x10000000
  99. #define GENERIC_EXECUTE 0x20000000
  100. #define GENERIC_WRITE 0x40000000
  101. #define GENERIC_READ 0x80000000
  102. /* In summary - Relevant file */
  103. /* access flags from CIFS are */
  104. /* file_read_data, file_write_data */
  105. /* file_execute, file_read_attributes*/
  106. /* write_dac, and delete. */
  107. #define SET_FILE_READ_RIGHTS (FILE_READ_DATA | FILE_READ_EA \
  108. | FILE_READ_ATTRIBUTES \
  109. | DELETE | READ_CONTROL | WRITE_DAC \
  110. | WRITE_OWNER | SYNCHRONIZE)
  111. #define SET_FILE_WRITE_RIGHTS (FILE_WRITE_DATA | FILE_APPEND_DATA \
  112. | FILE_WRITE_EA \
  113. | FILE_DELETE_CHILD \
  114. | FILE_WRITE_ATTRIBUTES \
  115. | DELETE | READ_CONTROL | WRITE_DAC \
  116. | WRITE_OWNER | SYNCHRONIZE)
  117. #define SET_FILE_EXEC_RIGHTS (FILE_READ_EA | FILE_WRITE_EA | FILE_EXECUTE \
  118. | FILE_READ_ATTRIBUTES \
  119. | FILE_WRITE_ATTRIBUTES \
  120. | DELETE | READ_CONTROL | WRITE_DAC \
  121. | WRITE_OWNER | SYNCHRONIZE)
  122. #define SET_MINIMUM_RIGHTS (FILE_READ_EA | FILE_READ_ATTRIBUTES \
  123. | READ_CONTROL | SYNCHRONIZE)
  124. /* generic flags for file open */
  125. #define GENERIC_READ_FLAGS (READ_CONTROL | FILE_READ_DATA | \
  126. FILE_READ_ATTRIBUTES | \
  127. FILE_READ_EA | SYNCHRONIZE)
  128. #define GENERIC_WRITE_FLAGS (READ_CONTROL | FILE_WRITE_DATA | \
  129. FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | \
  130. FILE_APPEND_DATA | SYNCHRONIZE)
  131. #define GENERIC_EXECUTE_FLAGS (READ_CONTROL | FILE_EXECUTE | \
  132. FILE_READ_ATTRIBUTES | SYNCHRONIZE)
  133. #define GENERIC_ALL_FLAGS (DELETE | READ_CONTROL | WRITE_DAC | \
  134. WRITE_OWNER | SYNCHRONIZE | FILE_READ_DATA | \
  135. FILE_WRITE_DATA | FILE_APPEND_DATA | \
  136. FILE_READ_EA | FILE_WRITE_EA | \
  137. FILE_EXECUTE | FILE_DELETE_CHILD | \
  138. FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES)
  139. #define SMB1_PROTO_NUMBER cpu_to_le32(0x424d53ff)
  140. #define SMB_COM_NEGOTIATE 0x72
  141. #define SMB1_CLIENT_GUID_SIZE (16)
  142. #define SMBFLG_RESPONSE 0x80 /* this PDU is a response from server */
  143. #define SMBFLG2_IS_LONG_NAME cpu_to_le16(0x40)
  144. #define SMBFLG2_EXT_SEC cpu_to_le16(0x800)
  145. #define SMBFLG2_ERR_STATUS cpu_to_le16(0x4000)
  146. #define SMBFLG2_UNICODE cpu_to_le16(0x8000)
  147. struct smb_hdr {
  148. __be32 smb_buf_length;
  149. __u8 Protocol[4];
  150. __u8 Command;
  151. union {
  152. struct {
  153. __u8 ErrorClass;
  154. __u8 Reserved;
  155. __le16 Error;
  156. } __packed DosError;
  157. __le32 CifsError;
  158. } __packed Status;
  159. __u8 Flags;
  160. __le16 Flags2; /* note: le */
  161. __le16 PidHigh;
  162. union {
  163. struct {
  164. __le32 SequenceNumber; /* le */
  165. __u32 Reserved; /* zero */
  166. } __packed Sequence;
  167. __u8 SecuritySignature[8]; /* le */
  168. } __packed Signature;
  169. __u8 pad[2];
  170. __le16 Tid;
  171. __le16 Pid;
  172. __le16 Uid;
  173. __le16 Mid;
  174. __u8 WordCount;
  175. } __packed;
  176. struct smb_negotiate_req {
  177. struct smb_hdr hdr; /* wct = 0 */
  178. __le16 ByteCount;
  179. unsigned char DialectsArray[];
  180. } __packed;
  181. struct smb_negotiate_rsp {
  182. struct smb_hdr hdr; /* wct = 17 */
  183. __le16 DialectIndex; /* 0xFFFF = no dialect acceptable */
  184. __le16 ByteCount;
  185. } __packed;
  186. struct filesystem_attribute_info {
  187. __le32 Attributes;
  188. __le32 MaxPathNameComponentLength;
  189. __le32 FileSystemNameLen;
  190. __le16 FileSystemName[]; /* do not have to save this - get subset? */
  191. } __packed;
  192. struct filesystem_device_info {
  193. __le32 DeviceType;
  194. __le32 DeviceCharacteristics;
  195. } __packed; /* device info level 0x104 */
  196. struct filesystem_vol_info {
  197. __le64 VolumeCreationTime;
  198. __le32 SerialNumber;
  199. __le32 VolumeLabelSize;
  200. __le16 Reserved;
  201. __le16 VolumeLabel[];
  202. } __packed;
  203. struct filesystem_info {
  204. __le64 TotalAllocationUnits;
  205. __le64 FreeAllocationUnits;
  206. __le32 SectorsPerAllocationUnit;
  207. __le32 BytesPerSector;
  208. } __packed; /* size info, level 0x103 */
  209. #define EXTENDED_INFO_MAGIC 0x43667364 /* Cfsd */
  210. #define STRING_LENGTH 28
  211. struct fs_extended_info {
  212. __le32 magic;
  213. __le32 version;
  214. __le32 release;
  215. __u64 rel_date;
  216. char version_string[STRING_LENGTH];
  217. } __packed;
  218. struct object_id_info {
  219. char objid[16];
  220. struct fs_extended_info extended_info;
  221. } __packed;
  222. struct file_directory_info {
  223. __le32 NextEntryOffset;
  224. __u32 FileIndex;
  225. __le64 CreationTime;
  226. __le64 LastAccessTime;
  227. __le64 LastWriteTime;
  228. __le64 ChangeTime;
  229. __le64 EndOfFile;
  230. __le64 AllocationSize;
  231. __le32 ExtFileAttributes;
  232. __le32 FileNameLength;
  233. char FileName[];
  234. } __packed; /* level 0x101 FF resp data */
  235. struct file_names_info {
  236. __le32 NextEntryOffset;
  237. __u32 FileIndex;
  238. __le32 FileNameLength;
  239. char FileName[];
  240. } __packed; /* level 0xc FF resp data */
  241. struct file_full_directory_info {
  242. __le32 NextEntryOffset;
  243. __u32 FileIndex;
  244. __le64 CreationTime;
  245. __le64 LastAccessTime;
  246. __le64 LastWriteTime;
  247. __le64 ChangeTime;
  248. __le64 EndOfFile;
  249. __le64 AllocationSize;
  250. __le32 ExtFileAttributes;
  251. __le32 FileNameLength;
  252. __le32 EaSize;
  253. char FileName[];
  254. } __packed; /* level 0x102 FF resp */
  255. struct file_both_directory_info {
  256. __le32 NextEntryOffset;
  257. __u32 FileIndex;
  258. __le64 CreationTime;
  259. __le64 LastAccessTime;
  260. __le64 LastWriteTime;
  261. __le64 ChangeTime;
  262. __le64 EndOfFile;
  263. __le64 AllocationSize;
  264. __le32 ExtFileAttributes;
  265. __le32 FileNameLength;
  266. __le32 EaSize; /* length of the xattrs */
  267. __u8 ShortNameLength;
  268. __u8 Reserved;
  269. __u8 ShortName[24];
  270. char FileName[];
  271. } __packed; /* level 0x104 FFrsp data */
  272. struct file_id_both_directory_info {
  273. __le32 NextEntryOffset;
  274. __u32 FileIndex;
  275. __le64 CreationTime;
  276. __le64 LastAccessTime;
  277. __le64 LastWriteTime;
  278. __le64 ChangeTime;
  279. __le64 EndOfFile;
  280. __le64 AllocationSize;
  281. __le32 ExtFileAttributes;
  282. __le32 FileNameLength;
  283. __le32 EaSize; /* length of the xattrs */
  284. __u8 ShortNameLength;
  285. __u8 Reserved;
  286. __u8 ShortName[24];
  287. __le16 Reserved2;
  288. __le64 UniqueId;
  289. char FileName[];
  290. } __packed;
  291. struct file_id_full_dir_info {
  292. __le32 NextEntryOffset;
  293. __u32 FileIndex;
  294. __le64 CreationTime;
  295. __le64 LastAccessTime;
  296. __le64 LastWriteTime;
  297. __le64 ChangeTime;
  298. __le64 EndOfFile;
  299. __le64 AllocationSize;
  300. __le32 ExtFileAttributes;
  301. __le32 FileNameLength;
  302. __le32 EaSize; /* EA size */
  303. __le32 Reserved;
  304. __le64 UniqueId; /* inode num - le since Samba puts ino in low 32 bit*/
  305. char FileName[];
  306. } __packed; /* level 0x105 FF rsp data */
  307. struct smb_version_values {
  308. char *version_string;
  309. __u16 protocol_id;
  310. __le16 lock_cmd;
  311. __u32 capabilities;
  312. __u32 max_read_size;
  313. __u32 max_write_size;
  314. __u32 max_trans_size;
  315. __u32 max_credits;
  316. __u32 large_lock_type;
  317. __u32 exclusive_lock_type;
  318. __u32 shared_lock_type;
  319. __u32 unlock_lock_type;
  320. size_t header_size;
  321. size_t max_header_size;
  322. size_t read_rsp_size;
  323. unsigned int cap_unix;
  324. unsigned int cap_nt_find;
  325. unsigned int cap_large_files;
  326. __u16 signing_enabled;
  327. __u16 signing_required;
  328. size_t create_lease_size;
  329. size_t create_durable_size;
  330. size_t create_durable_v2_size;
  331. size_t create_mxac_size;
  332. size_t create_disk_id_size;
  333. size_t create_posix_size;
  334. };
  335. struct filesystem_posix_info {
  336. /* For undefined recommended transfer size return -1 in that field */
  337. __le32 OptimalTransferSize; /* bsize on some os, iosize on other os */
  338. __le32 BlockSize;
  339. /* The next three fields are in terms of the block size.
  340. * (above). If block size is unknown, 4096 would be a
  341. * reasonable block size for a server to report.
  342. * Note that returning the blocks/blocksavail removes need
  343. * to make a second call (to QFSInfo level 0x103 to get this info.
  344. * UserBlockAvail is typically less than or equal to BlocksAvail,
  345. * if no distinction is made return the same value in each
  346. */
  347. __le64 TotalBlocks;
  348. __le64 BlocksAvail; /* bfree */
  349. __le64 UserBlocksAvail; /* bavail */
  350. /* For undefined Node fields or FSID return -1 */
  351. __le64 TotalFileNodes;
  352. __le64 FreeFileNodes;
  353. __le64 FileSysIdentifier; /* fsid */
  354. /* NB Namelen comes from FILE_SYSTEM_ATTRIBUTE_INFO call */
  355. /* NB flags can come from FILE_SYSTEM_DEVICE_INFO call */
  356. } __packed;
  357. struct smb_version_ops {
  358. u16 (*get_cmd_val)(struct ksmbd_work *swork);
  359. int (*init_rsp_hdr)(struct ksmbd_work *swork);
  360. void (*set_rsp_status)(struct ksmbd_work *swork, __le32 err);
  361. int (*allocate_rsp_buf)(struct ksmbd_work *work);
  362. int (*set_rsp_credits)(struct ksmbd_work *work);
  363. int (*check_user_session)(struct ksmbd_work *work);
  364. int (*get_ksmbd_tcon)(struct ksmbd_work *work);
  365. bool (*is_sign_req)(struct ksmbd_work *work, unsigned int command);
  366. int (*check_sign_req)(struct ksmbd_work *work);
  367. void (*set_sign_rsp)(struct ksmbd_work *work);
  368. int (*generate_signingkey)(struct ksmbd_session *sess, struct ksmbd_conn *conn);
  369. int (*generate_encryptionkey)(struct ksmbd_conn *conn, struct ksmbd_session *sess);
  370. bool (*is_transform_hdr)(void *buf);
  371. int (*decrypt_req)(struct ksmbd_work *work);
  372. int (*encrypt_resp)(struct ksmbd_work *work);
  373. };
  374. struct smb_version_cmds {
  375. int (*proc)(struct ksmbd_work *swork);
  376. };
  377. int ksmbd_min_protocol(void);
  378. int ksmbd_max_protocol(void);
  379. int ksmbd_lookup_protocol_idx(char *str);
  380. int ksmbd_verify_smb_message(struct ksmbd_work *work);
  381. bool ksmbd_smb_request(struct ksmbd_conn *conn);
  382. int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count);
  383. int ksmbd_init_smb_server(struct ksmbd_conn *conn);
  384. struct ksmbd_kstat;
  385. int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work,
  386. int info_level,
  387. struct ksmbd_file *dir,
  388. struct ksmbd_dir_info *d_info,
  389. char *search_pattern,
  390. int (*fn)(struct ksmbd_conn *,
  391. int,
  392. struct ksmbd_dir_info *,
  393. struct ksmbd_kstat *));
  394. int ksmbd_extract_shortname(struct ksmbd_conn *conn,
  395. const char *longname,
  396. char *shortname);
  397. int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command);
  398. int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp);
  399. int __ksmbd_override_fsids(struct ksmbd_work *work,
  400. struct ksmbd_share_config *share);
  401. int ksmbd_override_fsids(struct ksmbd_work *work);
  402. void ksmbd_revert_fsids(struct ksmbd_work *work);
  403. unsigned int ksmbd_server_side_copy_max_chunk_count(void);
  404. unsigned int ksmbd_server_side_copy_max_chunk_size(void);
  405. unsigned int ksmbd_server_side_copy_max_total_size(void);
  406. bool is_asterisk(char *p);
  407. __le32 smb_map_generic_desired_access(__le32 daccess);
  408. static inline unsigned int get_rfc1002_len(void *buf)
  409. {
  410. return be32_to_cpu(*((__be32 *)buf)) & 0xffffff;
  411. }
  412. static inline void inc_rfc1001_len(void *buf, int count)
  413. {
  414. be32_add_cpu((__be32 *)buf, count);
  415. }
  416. #endif /* __SMB_COMMON_H__ */