/src/pdns/pdns/dnsdistdist/dnsdist-self-answers.cc
Line | Count | Source |
1 | | /* |
2 | | * This file is part of PowerDNS or dnsdist. |
3 | | * Copyright -- PowerDNS.COM B.V. and its contributors |
4 | | * |
5 | | * This program is free software; you can redistribute it and/or modify |
6 | | * it under the terms of version 2 of the GNU General Public License as |
7 | | * published by the Free Software Foundation. |
8 | | * |
9 | | * In addition, for the avoidance of any doubt, permission is granted to |
10 | | * link this program with OpenSSL and to (re)distribute the binaries |
11 | | * produced as the result of such linking. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
21 | | */ |
22 | | #include <random> |
23 | | |
24 | | #include "dnsdist-self-answers.hh" |
25 | | |
26 | | #include "dnsdist-configuration.hh" |
27 | | #include "dnsdist-ecs.hh" |
28 | | |
29 | | namespace dnsdist::self_answers |
30 | | { |
31 | | static thread_local std::default_random_engine t_randomEngine; |
32 | | |
33 | | static void addRecordHeader(PacketBuffer& packet, size_t& position, uint16_t qclass, uint32_t ttl, QType qtype, uint16_t rdataLen) |
34 | 0 | { |
35 | 0 | std::array<unsigned char, 12> recordstart{ |
36 | 0 | 0xc0, 0x0c, // compressed name |
37 | 0 | 0, 0, // QTYPE |
38 | 0 | 0, 0, // QCLASS |
39 | 0 | 0, 0, 0, 0, // TTL |
40 | 0 | 0, 0 // rdata length |
41 | 0 | }; |
42 | 0 | ttl = htonl(ttl); |
43 | 0 | qclass = htons(qclass); |
44 | 0 | qtype = htons(qtype); |
45 | 0 | rdataLen = htons(rdataLen); |
46 | 0 | static_assert(recordstart.size() == 12, "sizeof(recordstart) must be equal to 12, otherwise the above check is invalid"); |
47 | 0 | memcpy(&recordstart.at(2), &qtype, sizeof(qtype)); |
48 | 0 | memcpy(&recordstart.at(4), &qclass, sizeof(qclass)); |
49 | 0 | memcpy(&recordstart.at(6), &ttl, sizeof(ttl)); |
50 | 0 | memcpy(&recordstart.at(10), &rdataLen, sizeof(rdataLen)); |
51 | 0 | memcpy(&packet.at(position), recordstart.data(), recordstart.size()); |
52 | 0 | position += recordstart.size(); |
53 | 0 | } |
54 | | |
55 | | static std::pair<bool, bool> getEDNSStatusInQuery(DNSQuestion& dnsQuestion) |
56 | 0 | { |
57 | 0 | if (!dnsdist::configuration::getCurrentRuntimeConfiguration().d_addEDNSToSelfGeneratedResponses) { |
58 | 0 | return {false, false}; |
59 | 0 | } |
60 | | |
61 | 0 | if (dnsQuestion.ids.dnssecOK) { |
62 | 0 | if (*dnsQuestion.ids.dnssecOK) { |
63 | | /* DNSSECOK was set, which means the query had EDNS */ |
64 | 0 | return {true, true}; |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | 0 | if (queryHasEDNS(dnsQuestion)) { |
69 | 0 | bool dnssecOK = ((dnsdist::getEDNSZ(dnsQuestion) & EDNS_HEADER_FLAG_DO) != 0); |
70 | 0 | dnsQuestion.ids.dnssecOK = dnssecOK; |
71 | 0 | return {true, dnssecOK}; |
72 | 0 | } |
73 | | |
74 | 0 | dnsQuestion.ids.dnssecOK = false; |
75 | 0 | return {false, false}; |
76 | 0 | } |
77 | | |
78 | | bool generateAnswerFromCNAME(DNSQuestion& dnsQuestion, const DNSName& cname, const dnsdist::ResponseConfig& responseConfig) |
79 | 0 | { |
80 | 0 | QType qtype = QType::CNAME; |
81 | 0 | unsigned int totrdatalen = cname.getStorage().size(); |
82 | 0 | size_t numberOfRecords = 1U; |
83 | 0 | auto qnameWireLength = dnsQuestion.ids.qname.wirelength(); |
84 | 0 | if (dnsQuestion.getMaximumSize() < (sizeof(dnsheader) + qnameWireLength + 4 + numberOfRecords * 12 /* recordstart */ + totrdatalen)) { |
85 | 0 | return false; |
86 | 0 | } |
87 | | |
88 | 0 | auto [hadEDNS, dnssecOK] = getEDNSStatusInQuery(dnsQuestion); |
89 | 0 | auto& data = dnsQuestion.getMutableData(); |
90 | 0 | data.resize(sizeof(dnsheader) + qnameWireLength + 4 + numberOfRecords * 12 /* recordstart */ + totrdatalen); // there goes your EDNS |
91 | 0 | size_t position = sizeof(dnsheader) + qnameWireLength + 4; |
92 | |
|
93 | 0 | dnsdist::PacketMangling::editDNSHeaderFromPacket(dnsQuestion.getMutableData(), [responseConfig](dnsheader& header) { |
94 | 0 | header.qr = true; // for good measure |
95 | 0 | setResponseHeadersFromConfig(header, responseConfig); |
96 | 0 | header.ancount = 0; |
97 | 0 | header.arcount = 0; // for now, forget about your EDNS, we're marching over it |
98 | 0 | return true; |
99 | 0 | }); |
100 | |
|
101 | 0 | const auto& wireData = cname.getStorage(); // Note! This doesn't do compression! |
102 | 0 | addRecordHeader(data, position, dnsQuestion.ids.qclass, responseConfig.ttl, qtype, wireData.length()); |
103 | 0 | memcpy(&data.at(position), wireData.c_str(), wireData.length()); |
104 | |
|
105 | 0 | dnsdist::PacketMangling::editDNSHeaderFromPacket(dnsQuestion.getMutableData(), [numberOfRecords](dnsheader& header) { |
106 | 0 | header.ancount = htons(numberOfRecords); |
107 | 0 | return true; |
108 | 0 | }); |
109 | |
|
110 | 0 | if (hadEDNS) { |
111 | 0 | addEDNS(dnsQuestion.getMutableData(), dnsQuestion.getMaximumSize(), dnssecOK, dnsdist::configuration::getCurrentRuntimeConfiguration().d_payloadSizeSelfGenAnswers, 0); |
112 | 0 | } |
113 | |
|
114 | 0 | dnsQuestion.d_selfGeneratedHandledEDNS = true; |
115 | 0 | return true; |
116 | 0 | } |
117 | | |
118 | | bool generateAnswerFromIPAddresses(DNSQuestion& dnsQuestion, const std::vector<ComboAddress>& addresses, const dnsdist::ResponseConfig& responseConfig) |
119 | 0 | { |
120 | 0 | uint16_t qtype = dnsQuestion.ids.qtype; |
121 | 0 | std::vector<ComboAddress> addrs = {}; |
122 | 0 | unsigned int totrdatalen = 0; |
123 | 0 | size_t numberOfRecords = 0; |
124 | 0 | for (const auto& addr : addresses) { |
125 | 0 | if (qtype != QType::ANY && ((addr.sin4.sin_family == AF_INET && qtype != QType::A) || (addr.sin4.sin_family == AF_INET6 && qtype != QType::AAAA))) { |
126 | 0 | continue; |
127 | 0 | } |
128 | 0 | totrdatalen += addr.sin4.sin_family == AF_INET ? sizeof(addr.sin4.sin_addr.s_addr) : sizeof(addr.sin6.sin6_addr.s6_addr); |
129 | 0 | addrs.push_back(addr); |
130 | 0 | ++numberOfRecords; |
131 | 0 | } |
132 | |
|
133 | 0 | if (addrs.size() > 1) { |
134 | 0 | shuffle(addrs.begin(), addrs.end(), t_randomEngine); |
135 | 0 | } |
136 | |
|
137 | 0 | unsigned int qnameWireLength = dnsQuestion.ids.qname.wirelength(); |
138 | 0 | if (dnsQuestion.getMaximumSize() < (sizeof(dnsheader) + qnameWireLength + 4 + numberOfRecords * 12 /* recordstart */ + totrdatalen)) { |
139 | 0 | return false; |
140 | 0 | } |
141 | | |
142 | 0 | auto [hadEDNS, dnssecOK] = getEDNSStatusInQuery(dnsQuestion); |
143 | 0 | auto& data = dnsQuestion.getMutableData(); |
144 | 0 | data.resize(sizeof(dnsheader) + qnameWireLength + 4 + numberOfRecords * 12 /* recordstart */ + totrdatalen); // there goes your EDNS |
145 | 0 | size_t position = sizeof(dnsheader) + qnameWireLength + 4; |
146 | |
|
147 | 0 | dnsdist::PacketMangling::editDNSHeaderFromPacket(dnsQuestion.getMutableData(), [responseConfig](dnsheader& header) { |
148 | 0 | header.qr = true; // for good measure |
149 | 0 | setResponseHeadersFromConfig(header, responseConfig); |
150 | 0 | header.ancount = 0; |
151 | 0 | header.arcount = 0; // for now, forget about your EDNS, we're marching over it |
152 | 0 | return true; |
153 | 0 | }); |
154 | |
|
155 | 0 | for (const auto& addr : addrs) { |
156 | 0 | uint16_t rdataLen = addr.sin4.sin_family == AF_INET ? sizeof(addr.sin4.sin_addr.s_addr) : sizeof(addr.sin6.sin6_addr.s6_addr); |
157 | 0 | qtype = addr.sin4.sin_family == AF_INET ? QType::A : QType::AAAA; |
158 | |
|
159 | 0 | addRecordHeader(data, position, dnsQuestion.ids.qclass, responseConfig.ttl, qtype, rdataLen); |
160 | |
|
161 | 0 | memcpy(&data.at(position), |
162 | | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
163 | 0 | addr.sin4.sin_family == AF_INET ? reinterpret_cast<const void*>(&addr.sin4.sin_addr.s_addr) : reinterpret_cast<const void*>(&addr.sin6.sin6_addr.s6_addr), |
164 | 0 | addr.sin4.sin_family == AF_INET ? sizeof(addr.sin4.sin_addr.s_addr) : sizeof(addr.sin6.sin6_addr.s6_addr)); |
165 | |
|
166 | 0 | position += (addr.sin4.sin_family == AF_INET ? sizeof(addr.sin4.sin_addr.s_addr) : sizeof(addr.sin6.sin6_addr.s6_addr)); |
167 | 0 | } |
168 | |
|
169 | 0 | auto finalANCount = addrs.size(); |
170 | 0 | dnsdist::PacketMangling::editDNSHeaderFromPacket(dnsQuestion.getMutableData(), [finalANCount](dnsheader& header) { |
171 | 0 | header.ancount = htons(finalANCount); |
172 | 0 | return true; |
173 | 0 | }); |
174 | |
|
175 | 0 | if (hadEDNS) { |
176 | 0 | addEDNS(dnsQuestion.getMutableData(), dnsQuestion.getMaximumSize(), dnssecOK, dnsdist::configuration::getCurrentRuntimeConfiguration().d_payloadSizeSelfGenAnswers, 0); |
177 | 0 | } |
178 | |
|
179 | 0 | dnsQuestion.d_selfGeneratedHandledEDNS = true; |
180 | 0 | return true; |
181 | 0 | } |
182 | | |
183 | | bool generateAnswerFromRDataEntries(DNSQuestion& dnsQuestion, const std::vector<std::string>& entries, std::optional<uint16_t> typeForAny, const dnsdist::ResponseConfig& responseConfig) |
184 | 0 | { |
185 | 0 | unsigned int totrdatalen = 0; |
186 | 0 | size_t numberOfRecords = 0; |
187 | 0 | auto shuffledEntries = entries; |
188 | 0 | for (const auto& entry : shuffledEntries) { |
189 | 0 | totrdatalen += entry.size(); |
190 | 0 | ++numberOfRecords; |
191 | 0 | } |
192 | 0 | if (shuffledEntries.size() > 1) { |
193 | 0 | shuffle(shuffledEntries.begin(), shuffledEntries.end(), t_randomEngine); |
194 | 0 | } |
195 | |
|
196 | 0 | auto qnameWireLength = dnsQuestion.ids.qname.wirelength(); |
197 | 0 | if (dnsQuestion.getMaximumSize() < (sizeof(dnsheader) + qnameWireLength + 4 + numberOfRecords * 12 /* recordstart */ + totrdatalen)) { |
198 | 0 | return false; |
199 | 0 | } |
200 | | |
201 | 0 | auto [hadEDNS, dnssecOK] = getEDNSStatusInQuery(dnsQuestion); |
202 | 0 | auto& data = dnsQuestion.getMutableData(); |
203 | 0 | data.resize(sizeof(dnsheader) + qnameWireLength + 4 + numberOfRecords * 12 /* recordstart */ + totrdatalen); // there goes your EDNS |
204 | 0 | size_t position = sizeof(dnsheader) + qnameWireLength + 4; |
205 | |
|
206 | 0 | dnsdist::PacketMangling::editDNSHeaderFromPacket(dnsQuestion.getMutableData(), [&responseConfig](dnsheader& header) { |
207 | 0 | header.qr = true; // for good measure |
208 | 0 | setResponseHeadersFromConfig(header, responseConfig); |
209 | 0 | header.ancount = 0; |
210 | 0 | header.arcount = 0; // for now, forget about your EDNS, we're marching over it |
211 | 0 | return true; |
212 | 0 | }); |
213 | |
|
214 | 0 | QType qtype = dnsQuestion.ids.qtype; |
215 | 0 | if (qtype == QType::ANY && typeForAny) { |
216 | 0 | qtype = *typeForAny; |
217 | 0 | } |
218 | |
|
219 | 0 | for (const auto& entry : shuffledEntries) { |
220 | 0 | uint16_t rdataLen = entry.size(); |
221 | 0 | addRecordHeader(data, position, dnsQuestion.ids.qclass, responseConfig.ttl, qtype, rdataLen); |
222 | 0 | memcpy(&data.at(position), entry.c_str(), entry.size()); |
223 | 0 | position += entry.size(); |
224 | 0 | } |
225 | |
|
226 | 0 | auto finalANCount = shuffledEntries.size(); |
227 | 0 | dnsdist::PacketMangling::editDNSHeaderFromPacket(dnsQuestion.getMutableData(), [finalANCount](dnsheader& header) { |
228 | 0 | header.ancount = htons(finalANCount); |
229 | 0 | return true; |
230 | 0 | }); |
231 | |
|
232 | 0 | if (hadEDNS) { |
233 | 0 | addEDNS(dnsQuestion.getMutableData(), dnsQuestion.getMaximumSize(), dnssecOK, dnsdist::configuration::getCurrentRuntimeConfiguration().d_payloadSizeSelfGenAnswers, 0); |
234 | 0 | } |
235 | |
|
236 | 0 | dnsQuestion.d_selfGeneratedHandledEDNS = true; |
237 | 0 | return true; |
238 | 0 | } |
239 | | |
240 | | bool generateAnswerFromRawPacket(DNSQuestion& dnsQuestion, const PacketBuffer& packet) |
241 | 0 | { |
242 | 0 | auto questionId = dnsQuestion.getHeader()->id; |
243 | 0 | dnsQuestion.getMutableData() = packet; |
244 | 0 | dnsQuestion.d_selfGeneratedHandledEDNS = true; |
245 | 0 | dnsdist::PacketMangling::editDNSHeaderFromPacket(dnsQuestion.getMutableData(), [questionId](dnsheader& header) { |
246 | 0 | header.id = questionId; |
247 | 0 | return true; |
248 | 0 | }); |
249 | 0 | return true; |
250 | 0 | } |
251 | | |
252 | | bool removeRecordsAndSetRCode(DNSQuestion& dnsQuestion, uint8_t rcode) |
253 | 0 | { |
254 | 0 | auto [hadEDNS, dnssecOK] = getEDNSStatusInQuery(dnsQuestion); |
255 | 0 | dnsdist::PacketMangling::editDNSHeaderFromPacket(dnsQuestion.getMutableData(), [rcode](dnsheader& header) { |
256 | 0 | header.rcode = rcode; |
257 | 0 | header.qr = true; |
258 | 0 | header.qdcount = htons(1); |
259 | 0 | header.ancount = 0; |
260 | 0 | header.nscount = 0; |
261 | 0 | header.arcount = 0; |
262 | 0 | return true; |
263 | 0 | }); |
264 | 0 | auto qnameWireLength = dnsQuestion.ids.qname.wirelength(); |
265 | 0 | dnsQuestion.getMutableData().resize(sizeof(dnsheader) + qnameWireLength + 4); |
266 | |
|
267 | 0 | if (hadEDNS) { |
268 | 0 | addEDNS(dnsQuestion.getMutableData(), dnsQuestion.getMaximumSize(), dnssecOK, dnsdist::configuration::getCurrentRuntimeConfiguration().d_payloadSizeSelfGenAnswers, 0); |
269 | 0 | } |
270 | |
|
271 | 0 | return true; |
272 | 0 | } |
273 | | |
274 | | } |