Debian Codegraph @

src/resolve/resolved-dns-transaction.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "list.h"
5 #include "resolved-def.h"
6 #include "resolved-dns-dnssec.h"
7 #include "resolved-dns-server.h"
8 #include "resolved-forward.h"
9
10 typedef enum DnsTransactionState {
11 DNS_TRANSACTION_NULL,
12 DNS_TRANSACTION_PENDING,
13 DNS_TRANSACTION_VALIDATING,
14 DNS_TRANSACTION_RCODE_FAILURE,
15 DNS_TRANSACTION_SUCCESS,
16 DNS_TRANSACTION_NO_SERVERS,
17 DNS_TRANSACTION_TIMEOUT,
18 DNS_TRANSACTION_ATTEMPTS_MAX_REACHED,
19 DNS_TRANSACTION_INVALID_REPLY,
20 DNS_TRANSACTION_ERRNO,
21 DNS_TRANSACTION_ABORTED,
22 DNS_TRANSACTION_DNSSEC_FAILED,
23 DNS_TRANSACTION_NO_TRUST_ANCHOR,
24 DNS_TRANSACTION_RR_TYPE_UNSUPPORTED,
25 DNS_TRANSACTION_NETWORK_DOWN,
26 DNS_TRANSACTION_NOT_FOUND, /* like NXDOMAIN, but when LLMNR/TCP connections fail */
27 DNS_TRANSACTION_NO_SOURCE, /* All suitable DnsTransactionSource turned off */
28 DNS_TRANSACTION_STUB_LOOP,
29 _DNS_TRANSACTION_STATE_MAX,
30 _DNS_TRANSACTION_STATE_INVALID = -EINVAL,
31 } DnsTransactionState;
32
33 #define DNS_TRANSACTION_IS_LIVE(state) IN_SET((state), DNS_TRANSACTION_NULL, DNS_TRANSACTION_PENDING, DNS_TRANSACTION_VALIDATING)
34
35 typedef enum DnsTransactionSource {
36 DNS_TRANSACTION_NETWORK,
37 DNS_TRANSACTION_CACHE,
38 DNS_TRANSACTION_ZONE,
39 DNS_TRANSACTION_TRUST_ANCHOR,
40 _DNS_TRANSACTION_SOURCE_MAX,
41 _DNS_TRANSACTION_SOURCE_INVALID = -EINVAL,
42 } DnsTransactionSource;
43
44 typedef struct DnsTransaction {
45 DnsScope *scope;
46
47 DnsResourceKey *key; /* For regular lookups the RR key to look for */
48 DnsPacket *bypass; /* For bypass lookups the full original request packet */
49
50 uint64_t query_flags;
51
52 DnsPacket *sent, *received;
53
54 DnsAnswer *answer;
55 int answer_rcode;
56 int answer_ede_rcode;
57 char *answer_ede_msg;
58 DnssecResult answer_dnssec_result;
59 DnsTransactionSource answer_source;
60 uint32_t answer_nsec_ttl;
61 int answer_errno; /* if state is DNS_TRANSACTION_ERRNO */
62
63 DnsTransactionState state;
64
65 /* SD_RESOLVED_AUTHENTICATED here indicates whether the primary answer is authenticated, i.e. whether
66 * the RRs from answer which directly match the question are authenticated, or, if there are none,
67 * whether the NODATA or NXDOMAIN case is. It says nothing about additional RRs listed in the answer,
68 * however they have their own DNS_ANSWER_AUTHORIZED FLAGS. Note that this bit is defined different
69 * than the AD bit in DNS packets, as that covers more than just the actual primary answer. */
70 uint64_t answer_query_flags;
71
72 /* Contains DNSKEY, DS, SOA RRs we already verified and need
73 * to authenticate this reply */
74 DnsAnswer *validated_keys;
75
76 usec_t start_usec;
77 usec_t next_attempt_after;
78 sd_event_source *timeout_event_source;
79 unsigned n_attempts;
80
81 /* UDP connection logic, if we need it */
82 int dns_udp_fd;
83 sd_event_source *dns_udp_event_source;
84
85 /* TCP connection logic, if we need it */
86 DnsStream *stream;
87
88 /* The active server */
89 DnsServer *server;
90
91 /* The features of the DNS server at time of transaction start */
92 DnsServerFeatureLevel current_feature_level;
93
94 /* If we got SERVFAIL back, we retry the lookup, using a lower feature level than we used before. */
95 DnsServerFeatureLevel clamp_feature_level_servfail;
96
97 uint16_t id;
98
99 bool tried_stream:1;
100
101 bool initial_jitter_scheduled:1;
102 bool initial_jitter_elapsed:1;
103
104 bool probing:1;
105
106 bool seen_timeout:1;
107
108 /* Query candidates this transaction is referenced by and that
109 * shall be notified about this specific transaction
110 * completing. */
111 Set *notify_query_candidates, *notify_query_candidates_done;
112
113 /* Zone items this transaction is referenced by and that shall
114 * be notified about completion. */
115 Set *notify_zone_items, *notify_zone_items_done;
116
117 /* Other transactions that this transactions is referenced by
118 * and that shall be notified about completion. This is used
119 * when transactions want to validate their RRsets, but need
120 * another DNSKEY or DS RR to do so. */
121 Set *notify_transactions, *notify_transactions_done;
122
123 /* The opposite direction: the transactions this transaction
124 * created in order to request DNSKEY or DS RRs. */
125 Set *dnssec_transactions;
126
127 unsigned n_picked_servers;
128
129 unsigned block_gc;
130
131 /* Set when we're willing to let this transaction live beyond it's usefulness for the original query,
132 * for caching purposes. This blocks gc while there is still a chance we might still receive an
133 * answer. */
134 bool wait_for_answer;
135
136 LIST_FIELDS(DnsTransaction, transactions_by_scope);
137 LIST_FIELDS(DnsTransaction, transactions_by_stream);
138 LIST_FIELDS(DnsTransaction, transactions_by_key);
139
140 /* Note: fields should be ordered to minimize alignment gaps. Use pahole! */
141 } DnsTransaction;
142
143 int dns_transaction_new(DnsTransaction **ret, DnsScope *s, DnsResourceKey *key, DnsPacket *bypass, uint64_t flags);
144 DnsTransaction* dns_transaction_free(DnsTransaction *t);
145
146 DnsTransaction* dns_transaction_gc(DnsTransaction *t);
147 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsTransaction*, dns_transaction_gc);
148
149 int dns_transaction_go(DnsTransaction *t);
150
151 void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypted);
152 void dns_transaction_complete(DnsTransaction *t, DnsTransactionState state);
153
154 void dns_transaction_notify(DnsTransaction *t, DnsTransaction *source);
155 int dns_transaction_validate_dnssec(DnsTransaction *t);
156 int dns_transaction_request_dnssec_keys(DnsTransaction *t);
157
158 DnsResourceKey* dns_transaction_key(DnsTransaction *t);
159
160 static inline uint64_t dns_transaction_source_to_query_flags(DnsTransactionSource s) {
161
162 switch (s) {
163
164 case DNS_TRANSACTION_NETWORK:
165 return SD_RESOLVED_FROM_NETWORK;
166
167 case DNS_TRANSACTION_CACHE:
168 return SD_RESOLVED_FROM_CACHE;
169
170 case DNS_TRANSACTION_ZONE:
171 return SD_RESOLVED_FROM_ZONE;
172
173 case DNS_TRANSACTION_TRUST_ANCHOR:
174 return SD_RESOLVED_FROM_TRUST_ANCHOR;
175
176 default:
177 return 0;
178 }
179 }
180
181 DECLARE_STRING_TABLE_LOOKUP(dns_transaction_state, DnsTransactionState);
182
183 DECLARE_STRING_TABLE_LOOKUP(dns_transaction_source, DnsTransactionSource);