Imported Upstream version 2.3.3
[platform/upstream/cryptsetup.git] / lib / utils_dm.h
1 /*
2  * libdevmapper - device-mapper backend for cryptsetup
3  *
4  * Copyright (C) 2004 Jana Saout <jana@saout.de>
5  * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2020 Milan Broz
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #ifndef _UTILS_DM_H
25 #define _UTILS_DM_H
26
27 /* device-mapper library helpers */
28 #include <inttypes.h>
29
30 struct crypt_device;
31 struct volume_key;
32 struct crypt_params_verity;
33 struct device;
34 struct crypt_params_integrity;
35
36 /* Device mapper internal flags */
37 #define DM_RESUME_PRIVATE      (1 << 4) /* CRYPT_ACTIVATE_PRIVATE */
38 #define DM_SUSPEND_SKIP_LOCKFS (1 << 5)
39 #define DM_SUSPEND_WIPE_KEY    (1 << 6)
40 #define DM_SUSPEND_NOFLUSH     (1 << 7)
41
42 static inline uint32_t act2dmflags(uint32_t act_flags)
43 {
44         return (act_flags & DM_RESUME_PRIVATE);
45 }
46
47 /* Device mapper backend - kernel support flags */
48 #define DM_KEY_WIPE_SUPPORTED (1 << 0)  /* key wipe message */
49 #define DM_LMK_SUPPORTED      (1 << 1)  /* lmk mode */
50 #define DM_SECURE_SUPPORTED   (1 << 2)  /* wipe (secure) buffer flag */
51 #define DM_PLAIN64_SUPPORTED  (1 << 3)  /* plain64 IV */
52 #define DM_DISCARDS_SUPPORTED (1 << 4)  /* discards/TRIM option is supported */
53 #define DM_VERITY_SUPPORTED   (1 << 5)  /* dm-verity target supported */
54 #define DM_TCW_SUPPORTED      (1 << 6)  /* tcw (TCRYPT CBC with whitening) */
55 #define DM_SAME_CPU_CRYPT_SUPPORTED (1 << 7) /* same_cpu_crypt */
56 #define DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED (1 << 8) /* submit_from_crypt_cpus */
57 #define DM_VERITY_ON_CORRUPTION_SUPPORTED (1 << 9) /* ignore/restart_on_corruption, ignore_zero_block */
58 #define DM_VERITY_FEC_SUPPORTED (1 << 10) /* Forward Error Correction (FEC) */
59 #define DM_KERNEL_KEYRING_SUPPORTED (1 << 11) /* dm-crypt allows loading kernel keyring keys */
60 #define DM_INTEGRITY_SUPPORTED (1 << 12) /* dm-integrity target supported */
61 #define DM_SECTOR_SIZE_SUPPORTED (1 << 13) /* support for sector size setting in dm-crypt/dm-integrity */
62 #define DM_CAPI_STRING_SUPPORTED (1 << 14) /* support for cryptoapi format cipher definition */
63 #define DM_DEFERRED_SUPPORTED (1 << 15) /* deferred removal of device */
64 #define DM_INTEGRITY_RECALC_SUPPORTED (1 << 16) /* dm-integrity automatic recalculation supported */
65 #define DM_INTEGRITY_BITMAP_SUPPORTED (1 << 17) /* dm-integrity bitmap mode supported */
66 #define DM_GET_TARGET_VERSION_SUPPORTED (1 << 18) /* dm DM_GET_TARGET version ioctl supported */
67 #define DM_INTEGRITY_FIX_PADDING_SUPPORTED (1 << 19) /* supports the parameter fix_padding that fixes a bug that caused excessive padding */
68 #define DM_BITLK_EBOIV_SUPPORTED (1 << 20) /* EBOIV for BITLK supported */
69 #define DM_BITLK_ELEPHANT_SUPPORTED (1 << 21) /* Elephant diffuser for BITLK supported */
70 #define DM_VERITY_SIGNATURE_SUPPORTED (1 << 22) /* Verity option root_hash_sig_key_desc supported */
71 #define DM_INTEGRITY_DISCARDS_SUPPORTED (1 << 23) /* dm-integrity discards/TRIM option is supported */
72
73 typedef enum { DM_CRYPT = 0, DM_VERITY, DM_INTEGRITY, DM_LINEAR, DM_ERROR, DM_ZERO, DM_UNKNOWN } dm_target_type;
74 enum tdirection { TARGET_SET = 1, TARGET_QUERY };
75
76 int dm_flags(struct crypt_device *cd, dm_target_type target, uint32_t *flags);
77
78 #define DM_ACTIVE_DEVICE        (1 << 0)
79 #define DM_ACTIVE_UUID          (1 << 1)
80 #define DM_ACTIVE_HOLDERS       (1 << 2)
81
82 #define DM_ACTIVE_CRYPT_CIPHER  (1 << 3)
83 #define DM_ACTIVE_CRYPT_KEYSIZE (1 << 4)
84 #define DM_ACTIVE_CRYPT_KEY     (1 << 5)
85
86 #define DM_ACTIVE_VERITY_ROOT_HASH      (1 << 6)
87 #define DM_ACTIVE_VERITY_HASH_DEVICE    (1 << 7)
88 #define DM_ACTIVE_VERITY_PARAMS         (1 << 8)
89
90 #define DM_ACTIVE_INTEGRITY_PARAMS      (1 << 9)
91
92 struct dm_target {
93         dm_target_type type;
94         enum tdirection direction;
95         uint64_t offset;
96         uint64_t size;
97         struct device *data_device;
98         union {
99         struct {
100                 const char *cipher;
101                 const char *integrity;
102
103                 /* Active key for device */
104                 struct volume_key *vk;
105
106                 /* struct crypt_active_device */
107                 uint64_t offset;        /* offset in sectors */
108                 uint64_t iv_offset;     /* IV initialisation sector */
109                 uint32_t tag_size;      /* additional on-disk tag size */
110                 uint32_t sector_size;   /* encryption sector size */
111         } crypt;
112         struct {
113                 struct device *hash_device;
114                 struct device *fec_device;
115
116                 const char *root_hash;
117                 uint32_t root_hash_size;
118                 const char *root_hash_sig_key_desc;
119
120                 uint64_t hash_offset;   /* hash offset in blocks (not header) */
121                 uint64_t hash_blocks;   /* size of hash device (in hash blocks) */
122                 uint64_t fec_offset;    /* FEC offset in blocks (not header) */
123                 uint64_t fec_blocks;    /* size of FEC device (in hash blocks) */
124                 struct crypt_params_verity *vp;
125         } verity;
126         struct {
127                 uint64_t journal_size;
128                 uint32_t journal_watermark;
129                 uint32_t journal_commit_time;
130                 uint32_t interleave_sectors;
131                 uint32_t tag_size;
132                 uint64_t offset;        /* offset in sectors */
133                 uint32_t sector_size;   /* integrity sector size */
134                 uint32_t buffer_sectors;
135
136                 const char *integrity;
137                 /* Active key for device */
138                 struct volume_key *vk;
139
140                 const char *journal_integrity;
141                 struct volume_key *journal_integrity_key;
142
143                 const char *journal_crypt;
144                 struct volume_key *journal_crypt_key;
145
146                 struct device *meta_device;
147
148                 bool fix_padding;
149         } integrity;
150         struct {
151                 uint64_t offset;
152         } linear;
153         struct {
154         } zero;
155         } u;
156
157         char *params;
158         struct dm_target *next;
159 };
160
161 struct crypt_dm_active_device {
162         uint64_t size;          /* active device size */
163         uint32_t flags;         /* activation flags */
164         const char *uuid;
165
166         unsigned holders:1;     /* device holders detected (on query only) */
167
168         struct dm_target segment;
169 };
170
171 static inline bool single_segment(const struct crypt_dm_active_device *dmd)
172 {
173         return dmd && !dmd->segment.next;
174 }
175
176 void dm_backend_init(struct crypt_device *cd);
177 void dm_backend_exit(struct crypt_device *cd);
178
179 int dm_targets_allocate(struct dm_target *first, unsigned count);
180 void dm_targets_free(struct crypt_device *cd, struct crypt_dm_active_device *dmd);
181
182 int dm_crypt_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
183         struct device *data_device, struct volume_key *vk, const char *cipher,
184         uint64_t iv_offset, uint64_t data_offset, const char *integrity,
185         uint32_t tag_size, uint32_t sector_size);
186 int dm_verity_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
187         struct device *data_device, struct device *hash_device, struct device *fec_device,
188         const char *root_hash, uint32_t root_hash_size, const char *root_hash_sig_key_desc,
189         uint64_t hash_offset_block, uint64_t hash_blocks, struct crypt_params_verity *vp);
190 int dm_integrity_target_set(struct crypt_device *cd,
191         struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
192         struct device *meta_device,
193         struct device *data_device, uint64_t tag_size, uint64_t offset, uint32_t sector_size,
194         struct volume_key *vk,
195         struct volume_key *journal_crypt_key, struct volume_key *journal_mac_key,
196         const struct crypt_params_integrity *ip);
197 int dm_linear_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size,
198         struct device *data_device, uint64_t data_offset);
199 int dm_zero_target_set(struct dm_target *tgt, uint64_t seg_offset, uint64_t seg_size);
200
201 int dm_remove_device(struct crypt_device *cd, const char *name, uint32_t flags);
202 int dm_status_device(struct crypt_device *cd, const char *name);
203 int dm_status_suspended(struct crypt_device *cd, const char *name);
204 int dm_status_verity_ok(struct crypt_device *cd, const char *name);
205 int dm_status_integrity_failures(struct crypt_device *cd, const char *name, uint64_t *count);
206 int dm_query_device(struct crypt_device *cd, const char *name,
207                     uint32_t get_flags, struct crypt_dm_active_device *dmd);
208 int dm_device_deps(struct crypt_device *cd, const char *name, const char *prefix,
209                    char **names, size_t names_length);
210 int dm_create_device(struct crypt_device *cd, const char *name,
211                      const char *type, struct crypt_dm_active_device *dmd);
212 int dm_reload_device(struct crypt_device *cd, const char *name,
213                      struct crypt_dm_active_device *dmd, uint32_t dmflags, unsigned resume);
214 int dm_suspend_device(struct crypt_device *cd, const char *name, uint32_t dmflags);
215 int dm_resume_device(struct crypt_device *cd, const char *name, uint32_t dmflags);
216 int dm_resume_and_reinstate_key(struct crypt_device *cd, const char *name,
217                                 const struct volume_key *vk);
218 int dm_error_device(struct crypt_device *cd, const char *name);
219 int dm_clear_device(struct crypt_device *cd, const char *name);
220
221 const char *dm_get_dir(void);
222
223 int lookup_dm_dev_by_uuid(struct crypt_device *cd, const char *uuid, const char *type);
224
225 /* These are DM helpers used only by utils_devpath file */
226 int dm_is_dm_device(int major);
227 int dm_is_dm_kernel_name(const char *name);
228 char *dm_device_path(const char *prefix, int major, int minor);
229 char *dm_device_name(const char *path);
230
231 #endif /* _UTILS_DM_H */