]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
74b2466e LP |
2 | #pragma once |
3 | ||
74b2466e LP |
4 | #include <netinet/in.h> |
5 | ||
50f1e641 | 6 | #include "bitmap.h" |
71d35b6b | 7 | #include "dns-type.h" |
322345fd | 8 | #include "hashmap.h" |
623a4c97 | 9 | #include "in-addr-util.h" |
2001c805 | 10 | #include "list.h" |
1c02e7ba | 11 | #include "string-util.h" |
74b2466e LP |
12 | |
13 | typedef struct DnsResourceKey DnsResourceKey; | |
14 | typedef struct DnsResourceRecord DnsResourceRecord; | |
2001c805 | 15 | typedef struct DnsTxtItem DnsTxtItem; |
74b2466e | 16 | |
8730bccf | 17 | /* DNSKEY RR flags */ |
8730bccf | 18 | #define DNSKEY_FLAG_SEP (UINT16_C(1) << 0) |
28b8191e LP |
19 | #define DNSKEY_FLAG_REVOKE (UINT16_C(1) << 7) |
20 | #define DNSKEY_FLAG_ZONE_KEY (UINT16_C(1) << 8) | |
8730bccf | 21 | |
23502de3 DM |
22 | /* mDNS RR flags */ |
23 | #define MDNS_RR_CACHE_FLUSH (UINT16_C(1) << 15) | |
24 | ||
8730bccf LP |
25 | /* DNSSEC algorithm identifiers, see |
26 | * http://tools.ietf.org/html/rfc4034#appendix-A.1 and | |
27 | * https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */ | |
28 | enum { | |
29 | DNSSEC_ALGORITHM_RSAMD5 = 1, | |
30 | DNSSEC_ALGORITHM_DH, | |
31 | DNSSEC_ALGORITHM_DSA, | |
32 | DNSSEC_ALGORITHM_ECC, | |
33 | DNSSEC_ALGORITHM_RSASHA1, | |
34 | DNSSEC_ALGORITHM_DSA_NSEC3_SHA1, | |
35 | DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1, | |
6f717d08 LP |
36 | DNSSEC_ALGORITHM_RSASHA256 = 8, /* RFC 5702 */ |
37 | DNSSEC_ALGORITHM_RSASHA512 = 10, /* RFC 5702 */ | |
38 | DNSSEC_ALGORITHM_ECC_GOST = 12, /* RFC 5933 */ | |
e0240c64 LP |
39 | DNSSEC_ALGORITHM_ECDSAP256SHA256 = 13, /* RFC 6605 */ |
40 | DNSSEC_ALGORITHM_ECDSAP384SHA384 = 14, /* RFC 6605 */ | |
cb9eeb06 MCO |
41 | DNSSEC_ALGORITHM_ED25519 = 15, /* RFC 8080 */ |
42 | DNSSEC_ALGORITHM_ED448 = 16, /* RFC 8080 */ | |
8730bccf LP |
43 | DNSSEC_ALGORITHM_INDIRECT = 252, |
44 | DNSSEC_ALGORITHM_PRIVATEDNS, | |
45 | DNSSEC_ALGORITHM_PRIVATEOID, | |
46 | _DNSSEC_ALGORITHM_MAX_DEFINED | |
47 | }; | |
48 | ||
49 | /* DNSSEC digest identifiers, see | |
50 | * https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */ | |
51 | enum { | |
52 | DNSSEC_DIGEST_SHA1 = 1, | |
6f717d08 LP |
53 | DNSSEC_DIGEST_SHA256 = 2, /* RFC 4509 */ |
54 | DNSSEC_DIGEST_GOST_R_34_11_94 = 3, /* RFC 5933 */ | |
55 | DNSSEC_DIGEST_SHA384 = 4, /* RFC 6605 */ | |
8730bccf LP |
56 | _DNSSEC_DIGEST_MAX_DEFINED |
57 | }; | |
58 | ||
d15ad742 LP |
59 | /* DNSSEC NSEC3 hash algorithms, see |
60 | * https://www.iana.org/assignments/dnssec-nsec3-parameters/dnssec-nsec3-parameters.xhtml */ | |
61 | enum { | |
62 | NSEC3_ALGORITHM_SHA1 = 1, | |
63 | _NSEC3_ALGORITHM_MAX_DEFINED | |
64 | }; | |
65 | ||
74b2466e | 66 | struct DnsResourceKey { |
f57e3cd5 | 67 | unsigned n_ref; /* (unsigned -1) for const keys, see below */ |
faa133f3 | 68 | uint16_t class, type; |
96d49011 | 69 | char *_name; /* don't access directly, use dns_resource_key_name()! */ |
74b2466e LP |
70 | }; |
71 | ||
1b4f6e79 LP |
72 | /* Creates a temporary resource key. This is only useful to quickly |
73 | * look up something, without allocating a full DnsResourceKey object | |
74 | * for it. Note that it is not OK to take references to this kind of | |
75 | * resource key object. */ | |
76 | #define DNS_RESOURCE_KEY_CONST(c, t, n) \ | |
77 | ((DnsResourceKey) { \ | |
78 | .n_ref = (unsigned) -1, \ | |
79 | .class = c, \ | |
80 | .type = t, \ | |
81 | ._name = (char*) n, \ | |
82 | }) | |
83 | ||
2001c805 LP |
84 | struct DnsTxtItem { |
85 | size_t length; | |
86 | LIST_FIELDS(DnsTxtItem, items); | |
87 | uint8_t data[]; | |
88 | }; | |
89 | ||
74b2466e LP |
90 | struct DnsResourceRecord { |
91 | unsigned n_ref; | |
faa133f3 | 92 | DnsResourceKey *key; |
97c67192 | 93 | |
7b50eb2e | 94 | char *to_string; |
97c67192 | 95 | |
74b2466e | 96 | uint32_t ttl; |
ee3d6aff | 97 | usec_t expiry; /* RRSIG signature expiry */ |
97c67192 LP |
98 | |
99 | /* How many labels to strip to determine "signer" of the RRSIG (aka, the zone). -1 if not signed. */ | |
100 | unsigned n_skip_labels_signer; | |
101 | /* How many labels to strip to determine "synthesizing source" of this RR, i.e. the wildcard's immediate parent. -1 if not signed. */ | |
102 | unsigned n_skip_labels_source; | |
103 | ||
a8812dd7 | 104 | bool unparseable:1; |
97c67192 | 105 | |
a8812dd7 LP |
106 | bool wire_format_canonical:1; |
107 | void *wire_format; | |
108 | size_t wire_format_size; | |
109 | size_t wire_format_rdata_offset; | |
97c67192 | 110 | |
74b2466e LP |
111 | union { |
112 | struct { | |
113 | void *data; | |
a43a068a | 114 | size_t data_size; |
d75acfb0 | 115 | } generic, opt; |
74b2466e | 116 | |
9c92ce6d LP |
117 | struct { |
118 | uint16_t priority; | |
119 | uint16_t weight; | |
120 | uint16_t port; | |
121 | char *name; | |
122 | } srv; | |
74b2466e LP |
123 | |
124 | struct { | |
125 | char *name; | |
8ac4e9e1 | 126 | } ptr, ns, cname, dname; |
74b2466e LP |
127 | |
128 | struct { | |
129 | char *cpu; | |
130 | char *os; | |
131 | } hinfo; | |
132 | ||
2e276efc | 133 | struct { |
2001c805 | 134 | DnsTxtItem *items; |
c0eb11cf | 135 | } txt, spf; |
74b2466e LP |
136 | |
137 | struct { | |
138 | struct in_addr in_addr; | |
139 | } a; | |
140 | ||
141 | struct { | |
142 | struct in6_addr in6_addr; | |
143 | } aaaa; | |
7e8e0422 LP |
144 | |
145 | struct { | |
146 | char *mname; | |
147 | char *rname; | |
148 | uint32_t serial; | |
149 | uint32_t refresh; | |
150 | uint32_t retry; | |
151 | uint32_t expire; | |
152 | uint32_t minimum; | |
153 | } soa; | |
946c7094 ZJS |
154 | |
155 | struct { | |
156 | uint16_t priority; | |
157 | char *exchange; | |
158 | } mx; | |
0dae31d4 | 159 | |
6af47493 | 160 | /* https://tools.ietf.org/html/rfc1876 */ |
0dae31d4 ZJS |
161 | struct { |
162 | uint8_t version; | |
163 | uint8_t size; | |
164 | uint8_t horiz_pre; | |
165 | uint8_t vert_pre; | |
166 | uint32_t latitude; | |
167 | uint32_t longitude; | |
168 | uint32_t altitude; | |
169 | } loc; | |
42cc2eeb | 170 | |
549c1a25 | 171 | /* https://tools.ietf.org/html/rfc4255#section-3.1 */ |
42cc2eeb LP |
172 | struct { |
173 | uint8_t algorithm; | |
174 | uint8_t fptype; | |
549c1a25 TG |
175 | void *fingerprint; |
176 | size_t fingerprint_size; | |
42cc2eeb | 177 | } sshfp; |
8db0d2f5 ZJS |
178 | |
179 | /* http://tools.ietf.org/html/rfc4034#section-2.1 */ | |
180 | struct { | |
f91dc240 LP |
181 | uint16_t flags; |
182 | uint8_t protocol; | |
8db0d2f5 ZJS |
183 | uint8_t algorithm; |
184 | void* key; | |
185 | size_t key_size; | |
186 | } dnskey; | |
151226ab ZJS |
187 | |
188 | /* http://tools.ietf.org/html/rfc4034#section-3.1 */ | |
189 | struct { | |
190 | uint16_t type_covered; | |
191 | uint8_t algorithm; | |
192 | uint8_t labels; | |
193 | uint32_t original_ttl; | |
194 | uint32_t expiration; | |
195 | uint32_t inception; | |
196 | uint16_t key_tag; | |
197 | char *signer; | |
198 | void *signature; | |
199 | size_t signature_size; | |
200 | } rrsig; | |
50f1e641 | 201 | |
9ead3519 | 202 | /* https://tools.ietf.org/html/rfc4034#section-4.1 */ |
50f1e641 TG |
203 | struct { |
204 | char *next_domain_name; | |
205 | Bitmap *types; | |
206 | } nsec; | |
5d45a880 | 207 | |
6af47493 LP |
208 | /* https://tools.ietf.org/html/rfc4034#section-5.1 */ |
209 | struct { | |
210 | uint16_t key_tag; | |
211 | uint8_t algorithm; | |
212 | uint8_t digest_type; | |
213 | void *digest; | |
214 | size_t digest_size; | |
215 | } ds; | |
216 | ||
5d45a880 TG |
217 | struct { |
218 | uint8_t algorithm; | |
219 | uint8_t flags; | |
220 | uint16_t iterations; | |
221 | void *salt; | |
222 | size_t salt_size; | |
223 | void *next_hashed_name; | |
224 | size_t next_hashed_name_size; | |
225 | Bitmap *types; | |
226 | } nsec3; | |
48d45d2b ZJS |
227 | |
228 | /* https://tools.ietf.org/html/draft-ietf-dane-protocol-23 */ | |
229 | struct { | |
230 | uint8_t cert_usage; | |
231 | uint8_t selector; | |
232 | uint8_t matching_type; | |
233 | void *data; | |
234 | size_t data_size; | |
235 | } tlsa; | |
95052df3 ZJS |
236 | |
237 | /* https://tools.ietf.org/html/rfc6844 */ | |
238 | struct { | |
239 | uint8_t flags; | |
240 | char *tag; | |
241 | void *value; | |
242 | size_t value_size; | |
243 | } caa; | |
74b2466e LP |
244 | }; |
245 | }; | |
246 | ||
93bab288 | 247 | static inline const void* DNS_RESOURCE_RECORD_RDATA(const DnsResourceRecord *rr) { |
85aeaccc LP |
248 | if (!rr) |
249 | return NULL; | |
250 | ||
251 | if (!rr->wire_format) | |
252 | return NULL; | |
253 | ||
254 | assert(rr->wire_format_rdata_offset <= rr->wire_format_size); | |
255 | return (uint8_t*) rr->wire_format + rr->wire_format_rdata_offset; | |
256 | } | |
257 | ||
93bab288 | 258 | static inline size_t DNS_RESOURCE_RECORD_RDATA_SIZE(const DnsResourceRecord *rr) { |
85aeaccc LP |
259 | if (!rr) |
260 | return 0; | |
261 | if (!rr->wire_format) | |
262 | return 0; | |
263 | ||
264 | assert(rr->wire_format_rdata_offset <= rr->wire_format_size); | |
265 | return rr->wire_format_size - rr->wire_format_rdata_offset; | |
266 | } | |
267 | ||
93bab288 | 268 | static inline uint8_t DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(const DnsResourceRecord *rr) { |
b30bf55d LP |
269 | assert(rr); |
270 | assert(rr->key->type == DNS_TYPE_OPT); | |
271 | ||
272 | return ((rr->ttl >> 16) & 0xFF) == 0; | |
273 | } | |
274 | ||
faa133f3 | 275 | DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name); |
36d9205d | 276 | DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname); |
801ad6a6 | 277 | int dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name); |
faa133f3 LP |
278 | DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name); |
279 | DnsResourceKey* dns_resource_key_ref(DnsResourceKey *key); | |
280 | DnsResourceKey* dns_resource_key_unref(DnsResourceKey *key); | |
1c02e7ba | 281 | const char* dns_resource_key_name(const DnsResourceKey *key); |
28b9b764 | 282 | bool dns_resource_key_is_address(const DnsResourceKey *key); |
a2bf8a19 | 283 | bool dns_resource_key_is_dnssd_ptr(const DnsResourceKey *key); |
faa133f3 | 284 | int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b); |
105e1512 | 285 | int dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain); |
5d27351f | 286 | int dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain); |
547973de | 287 | int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa); |
202b76ae ZJS |
288 | |
289 | /* _DNS_{CLASS,TYPE}_STRING_MAX include one byte for NUL, which we use for space instead below. | |
290 | * DNS_HOSTNAME_MAX does not include the NUL byte, so we need to add 1. */ | |
291 | #define DNS_RESOURCE_KEY_STRING_MAX (_DNS_CLASS_STRING_MAX + _DNS_TYPE_STRING_MAX + DNS_HOSTNAME_MAX + 1) | |
292 | ||
293 | char* dns_resource_key_to_string(const DnsResourceKey *key, char *buf, size_t buf_size); | |
2e74028a ZJS |
294 | ssize_t dns_resource_record_payload(DnsResourceRecord *rr, void **out); |
295 | ||
faa133f3 | 296 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref); |
322345fd | 297 | |
7778dfff DM |
298 | static inline bool dns_key_is_shared(const DnsResourceKey *key) { |
299 | return IN_SET(key->type, DNS_TYPE_PTR); | |
300 | } | |
301 | ||
f57e3cd5 LP |
302 | bool dns_resource_key_reduce(DnsResourceKey **a, DnsResourceKey **b); |
303 | ||
faa133f3 | 304 | DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key); |
8bf52d3d | 305 | DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name); |
74b2466e LP |
306 | DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr); |
307 | DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr); | |
623a4c97 | 308 | int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name); |
78c6a153 | 309 | int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name); |
322345fd | 310 | int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b); |
7b50eb2e | 311 | const char* dns_resource_record_to_string(DnsResourceRecord *rr); |
17c8de63 | 312 | DnsResourceRecord *dns_resource_record_copy(DnsResourceRecord *rr); |
faa133f3 | 313 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceRecord*, dns_resource_record_unref); |
322345fd | 314 | |
a8812dd7 LP |
315 | int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical); |
316 | ||
97c67192 LP |
317 | int dns_resource_record_signer(DnsResourceRecord *rr, const char **ret); |
318 | int dns_resource_record_source(DnsResourceRecord *rr, const char **ret); | |
319 | int dns_resource_record_is_signer(DnsResourceRecord *rr, const char *zone); | |
ab481675 | 320 | int dns_resource_record_is_synthetic(DnsResourceRecord *rr); |
97c67192 | 321 | |
17c8de63 LP |
322 | int dns_resource_record_clamp_ttl(DnsResourceRecord **rr, uint32_t max_ttl); |
323 | ||
2001c805 LP |
324 | DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i); |
325 | bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b); | |
17c8de63 | 326 | DnsTxtItem *dns_txt_item_copy(DnsTxtItem *i); |
ebb779dc | 327 | int dns_txt_item_new_empty(DnsTxtItem **ret); |
2001c805 | 328 | |
6d99904f ZJS |
329 | void dns_resource_record_hash_func(const void *i, struct siphash *state); |
330 | ||
d5099efc | 331 | extern const struct hash_ops dns_resource_key_hash_ops; |
c9c72065 | 332 | extern const struct hash_ops dns_resource_record_hash_ops; |
8730bccf | 333 | |
8e54f5d9 | 334 | int dnssec_algorithm_to_string_alloc(int i, char **ret); |
8730bccf LP |
335 | int dnssec_algorithm_from_string(const char *s) _pure_; |
336 | ||
8e54f5d9 | 337 | int dnssec_digest_to_string_alloc(int i, char **ret); |
8730bccf | 338 | int dnssec_digest_from_string(const char *s) _pure_; |