]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
b30bf55d LP |
2 | |
3 | #include "fd-util.h" | |
ef118d00 | 4 | #include "missing_network.h" |
b30bf55d LP |
5 | #include "resolved-dns-stub.h" |
6 | #include "socket-util.h" | |
7 | ||
8 | /* The MTU of the loopback device is 64K on Linux, advertise that as maximum datagram size, but subtract the Ethernet, | |
9 | * IP and UDP header sizes */ | |
10 | #define ADVERTISE_DATAGRAM_SIZE_MAX (65536U-14U-20U-8U) | |
11 | ||
424e490b ZJS |
12 | static int manager_dns_stub_udp_fd(Manager *m); |
13 | static int manager_dns_stub_tcp_fd(Manager *m); | |
14 | ||
b30bf55d | 15 | static int dns_stub_make_reply_packet( |
e8d23f92 | 16 | DnsPacket **p, |
51027656 | 17 | size_t max_size, |
b30bf55d | 18 | DnsQuestion *q, |
51027656 LP |
19 | DnsAnswer *answer, |
20 | bool *ret_truncated) { | |
b30bf55d | 21 | |
51027656 | 22 | bool truncated = false; |
b30bf55d LP |
23 | DnsResourceRecord *rr; |
24 | unsigned c = 0; | |
25 | int r; | |
26 | ||
e8d23f92 LP |
27 | assert(p); |
28 | ||
b30bf55d LP |
29 | /* Note that we don't bother with any additional RRs, as this is stub is for local lookups only, and hence |
30 | * roundtrips aren't expensive. */ | |
31 | ||
e8d23f92 | 32 | if (!*p) { |
51027656 | 33 | r = dns_packet_new(p, DNS_PROTOCOL_DNS, 0, max_size); |
e8d23f92 LP |
34 | if (r < 0) |
35 | return r; | |
b30bf55d | 36 | |
e8d23f92 LP |
37 | r = dns_packet_append_question(*p, q); |
38 | if (r < 0) | |
39 | return r; | |
b30bf55d | 40 | |
e8d23f92 LP |
41 | DNS_PACKET_HEADER(*p)->qdcount = htobe16(dns_question_size(q)); |
42 | } | |
b30bf55d LP |
43 | |
44 | DNS_ANSWER_FOREACH(rr, answer) { | |
e8d23f92 | 45 | |
b30bf55d LP |
46 | r = dns_question_matches_rr(q, rr, NULL); |
47 | if (r < 0) | |
48 | return r; | |
49 | if (r > 0) | |
50 | goto add; | |
51 | ||
52 | r = dns_question_matches_cname_or_dname(q, rr, NULL); | |
53 | if (r < 0) | |
54 | return r; | |
55 | if (r > 0) | |
56 | goto add; | |
57 | ||
58 | continue; | |
59 | add: | |
01c901e2 | 60 | r = dns_packet_append_rr(*p, rr, 0, NULL, NULL); |
51027656 LP |
61 | if (r == -EMSGSIZE) { |
62 | truncated = true; | |
63 | break; | |
64 | } | |
b30bf55d LP |
65 | if (r < 0) |
66 | return r; | |
67 | ||
68 | c++; | |
69 | } | |
e8d23f92 | 70 | |
51027656 LP |
71 | if (ret_truncated) |
72 | *ret_truncated = truncated; | |
73 | else if (truncated) | |
74 | return -EMSGSIZE; | |
75 | ||
e8d23f92 LP |
76 | DNS_PACKET_HEADER(*p)->ancount = htobe16(be16toh(DNS_PACKET_HEADER(*p)->ancount) + c); |
77 | ||
78 | return 0; | |
79 | } | |
80 | ||
81 | static int dns_stub_finish_reply_packet( | |
82 | DnsPacket *p, | |
83 | uint16_t id, | |
84 | int rcode, | |
51027656 | 85 | bool tc, /* set the Truncated bit? */ |
e8d23f92 LP |
86 | bool add_opt, /* add an OPT RR to this packet? */ |
87 | bool edns0_do, /* set the EDNS0 DNSSEC OK bit? */ | |
88 | bool ad) { /* set the DNSSEC authenticated data bit? */ | |
89 | ||
90 | int r; | |
91 | ||
92 | assert(p); | |
93 | ||
ff4caaae LP |
94 | if (add_opt) { |
95 | r = dns_packet_append_opt(p, ADVERTISE_DATAGRAM_SIZE_MAX, edns0_do, rcode, NULL); | |
96 | if (r == -EMSGSIZE) /* Hit the size limit? then indicate truncation */ | |
97 | tc = true; | |
98 | else if (r < 0) | |
99 | return r; | |
100 | ||
101 | } else { | |
941dd294 LP |
102 | /* If the client can't to EDNS0, don't do DO either */ |
103 | edns0_do = false; | |
104 | ||
105 | /* If the client didn't do EDNS, clamp the rcode to 4 bit */ | |
106 | if (rcode > 0xF) | |
107 | rcode = DNS_RCODE_SERVFAIL; | |
108 | } | |
109 | ||
110 | /* Don't set the AD bit unless DO is on, too */ | |
111 | if (!edns0_do) | |
112 | ad = false; | |
e8d23f92 LP |
113 | |
114 | DNS_PACKET_HEADER(p)->id = id; | |
115 | ||
116 | DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS( | |
51027656 LP |
117 | 1 /* qr */, |
118 | 0 /* opcode */, | |
119 | 0 /* aa */, | |
120 | tc /* tc */, | |
121 | 1 /* rd */, | |
122 | 1 /* ra */, | |
e8d23f92 | 123 | ad /* ad */, |
51027656 | 124 | 0 /* cd */, |
e8d23f92 | 125 | rcode)); |
b30bf55d | 126 | |
b30bf55d LP |
127 | return 0; |
128 | } | |
129 | ||
b30bf55d LP |
130 | static int dns_stub_send(Manager *m, DnsStream *s, DnsPacket *p, DnsPacket *reply) { |
131 | int r; | |
132 | ||
133 | assert(m); | |
134 | assert(p); | |
135 | assert(reply); | |
136 | ||
137 | if (s) | |
138 | r = dns_stream_write_packet(s, reply); | |
139 | else { | |
140 | int fd; | |
141 | ||
b30bf55d LP |
142 | fd = manager_dns_stub_udp_fd(m); |
143 | if (fd < 0) | |
144 | return log_debug_errno(fd, "Failed to get reply socket: %m"); | |
145 | ||
146 | /* Note that it is essential here that we explicitly choose the source IP address for this packet. This | |
147 | * is because otherwise the kernel will choose it automatically based on the routing table and will | |
148 | * thus pick 127.0.0.1 rather than 127.0.0.53. */ | |
149 | ||
150 | r = manager_send(m, fd, LOOPBACK_IFINDEX, p->family, &p->sender, p->sender_port, &p->destination, reply); | |
151 | } | |
152 | if (r < 0) | |
153 | return log_debug_errno(r, "Failed to send reply packet: %m"); | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
2b2d98c1 | 158 | static int dns_stub_send_failure(Manager *m, DnsStream *s, DnsPacket *p, int rcode, bool authenticated) { |
b30bf55d LP |
159 | _cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL; |
160 | int r; | |
161 | ||
162 | assert(m); | |
163 | assert(p); | |
164 | ||
51027656 | 165 | r = dns_stub_make_reply_packet(&reply, DNS_PACKET_PAYLOAD_SIZE_MAX(p), p->question, NULL, NULL); |
e8d23f92 LP |
166 | if (r < 0) |
167 | return log_debug_errno(r, "Failed to make failure packet: %m"); | |
168 | ||
51027656 | 169 | r = dns_stub_finish_reply_packet(reply, DNS_PACKET_ID(p), rcode, false, !!p->opt, DNS_PACKET_DO(p), authenticated); |
b30bf55d LP |
170 | if (r < 0) |
171 | return log_debug_errno(r, "Failed to build failure packet: %m"); | |
172 | ||
173 | return dns_stub_send(m, s, p, reply); | |
174 | } | |
175 | ||
176 | static void dns_stub_query_complete(DnsQuery *q) { | |
177 | int r; | |
178 | ||
179 | assert(q); | |
180 | assert(q->request_dns_packet); | |
181 | ||
182 | switch (q->state) { | |
183 | ||
51027656 LP |
184 | case DNS_TRANSACTION_SUCCESS: { |
185 | bool truncated; | |
e8d23f92 | 186 | |
51027656 | 187 | r = dns_stub_make_reply_packet(&q->reply_dns_packet, DNS_PACKET_PAYLOAD_SIZE_MAX(q->request_dns_packet), q->question_idna, q->answer, &truncated); |
e8d23f92 LP |
188 | if (r < 0) { |
189 | log_debug_errno(r, "Failed to build reply packet: %m"); | |
190 | break; | |
191 | } | |
b30bf55d | 192 | |
aa11cab9 LP |
193 | if (!truncated) { |
194 | r = dns_query_process_cname(q); | |
195 | if (r == -ELOOP) { | |
196 | (void) dns_stub_send_failure(q->manager, q->request_dns_stream, q->request_dns_packet, DNS_RCODE_SERVFAIL, false); | |
197 | break; | |
198 | } | |
199 | if (r < 0) { | |
200 | log_debug_errno(r, "Failed to process CNAME: %m"); | |
201 | break; | |
202 | } | |
203 | if (r == DNS_QUERY_RESTARTED) | |
204 | return; | |
e8d23f92 | 205 | } |
e8d23f92 LP |
206 | |
207 | r = dns_stub_finish_reply_packet( | |
208 | q->reply_dns_packet, | |
b30bf55d LP |
209 | DNS_PACKET_ID(q->request_dns_packet), |
210 | q->answer_rcode, | |
51027656 | 211 | truncated, |
b30bf55d LP |
212 | !!q->request_dns_packet->opt, |
213 | DNS_PACKET_DO(q->request_dns_packet), | |
941dd294 | 214 | dns_query_fully_authenticated(q)); |
b30bf55d | 215 | if (r < 0) { |
e8d23f92 | 216 | log_debug_errno(r, "Failed to finish reply packet: %m"); |
b30bf55d LP |
217 | break; |
218 | } | |
219 | ||
e8d23f92 | 220 | (void) dns_stub_send(q->manager, q->request_dns_stream, q->request_dns_packet, q->reply_dns_packet); |
b30bf55d | 221 | break; |
51027656 | 222 | } |
b30bf55d LP |
223 | |
224 | case DNS_TRANSACTION_RCODE_FAILURE: | |
2b2d98c1 | 225 | (void) dns_stub_send_failure(q->manager, q->request_dns_stream, q->request_dns_packet, q->answer_rcode, dns_query_fully_authenticated(q)); |
b30bf55d LP |
226 | break; |
227 | ||
228 | case DNS_TRANSACTION_NOT_FOUND: | |
2b2d98c1 | 229 | (void) dns_stub_send_failure(q->manager, q->request_dns_stream, q->request_dns_packet, DNS_RCODE_NXDOMAIN, dns_query_fully_authenticated(q)); |
b30bf55d LP |
230 | break; |
231 | ||
232 | case DNS_TRANSACTION_TIMEOUT: | |
233 | case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED: | |
234 | /* Propagate a timeout as a no packet, i.e. that the client also gets a timeout */ | |
235 | break; | |
236 | ||
237 | case DNS_TRANSACTION_NO_SERVERS: | |
238 | case DNS_TRANSACTION_INVALID_REPLY: | |
239 | case DNS_TRANSACTION_ERRNO: | |
240 | case DNS_TRANSACTION_ABORTED: | |
241 | case DNS_TRANSACTION_DNSSEC_FAILED: | |
242 | case DNS_TRANSACTION_NO_TRUST_ANCHOR: | |
243 | case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED: | |
244 | case DNS_TRANSACTION_NETWORK_DOWN: | |
2b2d98c1 | 245 | (void) dns_stub_send_failure(q->manager, q->request_dns_stream, q->request_dns_packet, DNS_RCODE_SERVFAIL, false); |
b30bf55d LP |
246 | break; |
247 | ||
248 | case DNS_TRANSACTION_NULL: | |
249 | case DNS_TRANSACTION_PENDING: | |
250 | case DNS_TRANSACTION_VALIDATING: | |
251 | default: | |
252 | assert_not_reached("Impossible state"); | |
253 | } | |
254 | ||
b30bf55d LP |
255 | dns_query_free(q); |
256 | } | |
257 | ||
258 | static int dns_stub_stream_complete(DnsStream *s, int error) { | |
259 | assert(s); | |
260 | ||
b412af57 LP |
261 | log_debug_errno(error, "DNS TCP connection terminated, destroying queries: %m"); |
262 | ||
263 | for (;;) { | |
264 | DnsQuery *q; | |
265 | ||
266 | q = set_first(s->queries); | |
267 | if (!q) | |
268 | break; | |
b30bf55d | 269 | |
b412af57 LP |
270 | dns_query_free(q); |
271 | } | |
b30bf55d | 272 | |
b412af57 LP |
273 | /* This drops the implicit ref we keep around since it was allocated, as incoming stub connections |
274 | * should be kept as long as the client wants to. */ | |
275 | dns_stream_unref(s); | |
b30bf55d LP |
276 | return 0; |
277 | } | |
278 | ||
279 | static void dns_stub_process_query(Manager *m, DnsStream *s, DnsPacket *p) { | |
280 | DnsQuery *q = NULL; | |
281 | int r; | |
282 | ||
283 | assert(m); | |
284 | assert(p); | |
285 | assert(p->protocol == DNS_PROTOCOL_DNS); | |
286 | ||
b30bf55d LP |
287 | if (in_addr_is_localhost(p->family, &p->sender) <= 0 || |
288 | in_addr_is_localhost(p->family, &p->destination) <= 0) { | |
289 | log_error("Got packet on unexpected IP range, refusing."); | |
2b2d98c1 | 290 | dns_stub_send_failure(m, s, p, DNS_RCODE_SERVFAIL, false); |
b30bf55d LP |
291 | goto fail; |
292 | } | |
293 | ||
294 | r = dns_packet_extract(p); | |
295 | if (r < 0) { | |
296 | log_debug_errno(r, "Failed to extract resources from incoming packet, ignoring packet: %m"); | |
2b2d98c1 | 297 | dns_stub_send_failure(m, s, p, DNS_RCODE_FORMERR, false); |
b30bf55d LP |
298 | goto fail; |
299 | } | |
300 | ||
301 | if (!DNS_PACKET_VERSION_SUPPORTED(p)) { | |
302 | log_debug("Got EDNS OPT field with unsupported version number."); | |
2b2d98c1 | 303 | dns_stub_send_failure(m, s, p, DNS_RCODE_BADVERS, false); |
b30bf55d LP |
304 | goto fail; |
305 | } | |
306 | ||
307 | if (dns_type_is_obsolete(p->question->keys[0]->type)) { | |
308 | log_debug("Got message with obsolete key type, refusing."); | |
2b2d98c1 | 309 | dns_stub_send_failure(m, s, p, DNS_RCODE_NOTIMP, false); |
b30bf55d LP |
310 | goto fail; |
311 | } | |
312 | ||
313 | if (dns_type_is_zone_transer(p->question->keys[0]->type)) { | |
314 | log_debug("Got request for zone transfer, refusing."); | |
2b2d98c1 | 315 | dns_stub_send_failure(m, s, p, DNS_RCODE_NOTIMP, false); |
b30bf55d LP |
316 | goto fail; |
317 | } | |
318 | ||
319 | if (!DNS_PACKET_RD(p)) { | |
320 | /* If the "rd" bit is off (i.e. recursion was not requested), then refuse operation */ | |
321 | log_debug("Got request with recursion disabled, refusing."); | |
2b2d98c1 | 322 | dns_stub_send_failure(m, s, p, DNS_RCODE_REFUSED, false); |
b30bf55d LP |
323 | goto fail; |
324 | } | |
325 | ||
326 | if (DNS_PACKET_DO(p) && DNS_PACKET_CD(p)) { | |
327 | log_debug("Got request with DNSSEC CD bit set, refusing."); | |
2b2d98c1 | 328 | dns_stub_send_failure(m, s, p, DNS_RCODE_NOTIMP, false); |
b30bf55d LP |
329 | goto fail; |
330 | } | |
331 | ||
e8d23f92 | 332 | r = dns_query_new(m, &q, p->question, p->question, 0, SD_RESOLVED_PROTOCOLS_ALL|SD_RESOLVED_NO_SEARCH); |
b30bf55d LP |
333 | if (r < 0) { |
334 | log_error_errno(r, "Failed to generate query object: %m"); | |
2b2d98c1 | 335 | dns_stub_send_failure(m, s, p, DNS_RCODE_SERVFAIL, false); |
b30bf55d LP |
336 | goto fail; |
337 | } | |
338 | ||
339 | /* Request that the TTL is corrected by the cached time for this lookup, so that we return vaguely useful TTLs */ | |
340 | q->clamp_ttl = true; | |
341 | ||
342 | q->request_dns_packet = dns_packet_ref(p); | |
343 | q->request_dns_stream = dns_stream_ref(s); /* make sure the stream stays around until we can send a reply through it */ | |
344 | q->complete = dns_stub_query_complete; | |
345 | ||
346 | if (s) { | |
b412af57 LP |
347 | /* Remember which queries belong to this stream, so that we can cancel them when the stream |
348 | * is disconnected early */ | |
349 | ||
350 | r = set_ensure_allocated(&s->queries, &trivial_hash_ops); | |
351 | if (r < 0) { | |
352 | log_oom(); | |
353 | goto fail; | |
354 | } | |
355 | ||
356 | if (set_put(s->queries, q) < 0) { | |
357 | log_oom(); | |
358 | goto fail; | |
359 | } | |
b30bf55d LP |
360 | } |
361 | ||
362 | r = dns_query_go(q); | |
363 | if (r < 0) { | |
364 | log_error_errno(r, "Failed to start query: %m"); | |
2b2d98c1 | 365 | dns_stub_send_failure(m, s, p, DNS_RCODE_SERVFAIL, false); |
b30bf55d LP |
366 | goto fail; |
367 | } | |
368 | ||
52e63427 | 369 | log_debug("Processing query..."); |
b30bf55d LP |
370 | return; |
371 | ||
372 | fail: | |
b30bf55d LP |
373 | dns_query_free(q); |
374 | } | |
375 | ||
376 | static int on_dns_stub_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
377 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; | |
378 | Manager *m = userdata; | |
379 | int r; | |
380 | ||
381 | r = manager_recv(m, fd, DNS_PROTOCOL_DNS, &p); | |
382 | if (r <= 0) | |
383 | return r; | |
384 | ||
385 | if (dns_packet_validate_query(p) > 0) { | |
386 | log_debug("Got DNS stub UDP query packet for id %u", DNS_PACKET_ID(p)); | |
387 | ||
388 | dns_stub_process_query(m, NULL, p); | |
389 | } else | |
390 | log_debug("Invalid DNS stub UDP packet, ignoring."); | |
391 | ||
392 | return 0; | |
393 | } | |
394 | ||
424e490b | 395 | static int manager_dns_stub_udp_fd(Manager *m) { |
b30bf55d LP |
396 | union sockaddr_union sa = { |
397 | .in.sin_family = AF_INET, | |
398 | .in.sin_port = htobe16(53), | |
399 | .in.sin_addr.s_addr = htobe32(INADDR_DNS_STUB), | |
400 | }; | |
424e490b | 401 | _cleanup_close_ int fd = -1; |
b30bf55d LP |
402 | int r; |
403 | ||
404 | if (m->dns_stub_udp_fd >= 0) | |
405 | return m->dns_stub_udp_fd; | |
406 | ||
424e490b ZJS |
407 | fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
408 | if (fd < 0) | |
b30bf55d LP |
409 | return -errno; |
410 | ||
2ff48e98 LP |
411 | r = setsockopt_int(fd, SOL_SOCKET, SO_REUSEADDR, true); |
412 | if (r < 0) | |
413 | return r; | |
b30bf55d | 414 | |
2ff48e98 LP |
415 | r = setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, true); |
416 | if (r < 0) | |
417 | return r; | |
b30bf55d | 418 | |
2ff48e98 LP |
419 | r = setsockopt_int(fd, IPPROTO_IP, IP_RECVTTL, true); |
420 | if (r < 0) | |
421 | return r; | |
b30bf55d LP |
422 | |
423 | /* Make sure no traffic from outside the local host can leak to onto this socket */ | |
424e490b ZJS |
424 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, "lo", 3) < 0) |
425 | return -errno; | |
b30bf55d | 426 | |
424e490b ZJS |
427 | if (bind(fd, &sa.sa, sizeof(sa.in)) < 0) |
428 | return -errno; | |
b30bf55d | 429 | |
424e490b | 430 | r = sd_event_add_io(m->event, &m->dns_stub_udp_event_source, fd, EPOLLIN, on_dns_stub_packet, m); |
b30bf55d | 431 | if (r < 0) |
424e490b | 432 | return r; |
b30bf55d LP |
433 | |
434 | (void) sd_event_source_set_description(m->dns_stub_udp_event_source, "dns-stub-udp"); | |
435 | ||
c10d6bdb | 436 | return m->dns_stub_udp_fd = TAKE_FD(fd); |
b30bf55d LP |
437 | } |
438 | ||
439 | static int on_dns_stub_stream_packet(DnsStream *s) { | |
aa337a5e LP |
440 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; |
441 | ||
b30bf55d | 442 | assert(s); |
b30bf55d | 443 | |
aa337a5e LP |
444 | p = dns_stream_take_read_packet(s); |
445 | assert(p); | |
446 | ||
447 | if (dns_packet_validate_query(p) > 0) { | |
448 | log_debug("Got DNS stub TCP query packet for id %u", DNS_PACKET_ID(p)); | |
b30bf55d | 449 | |
aa337a5e | 450 | dns_stub_process_query(s->manager, s, p); |
b30bf55d LP |
451 | } else |
452 | log_debug("Invalid DNS stub TCP packet, ignoring."); | |
453 | ||
b30bf55d LP |
454 | return 0; |
455 | } | |
456 | ||
457 | static int on_dns_stub_stream(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
458 | DnsStream *stream; | |
459 | Manager *m = userdata; | |
460 | int cfd, r; | |
461 | ||
462 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); | |
463 | if (cfd < 0) { | |
3742095b | 464 | if (IN_SET(errno, EAGAIN, EINTR)) |
b30bf55d LP |
465 | return 0; |
466 | ||
467 | return -errno; | |
468 | } | |
469 | ||
652ba568 | 470 | r = dns_stream_new(m, &stream, DNS_STREAM_STUB, DNS_PROTOCOL_DNS, cfd, NULL); |
b30bf55d LP |
471 | if (r < 0) { |
472 | safe_close(cfd); | |
473 | return r; | |
474 | } | |
475 | ||
476 | stream->on_packet = on_dns_stub_stream_packet; | |
b412af57 | 477 | stream->complete = dns_stub_stream_complete; |
b30bf55d | 478 | |
b412af57 | 479 | /* We let the reference to the stream dangle here, it will be dropped later by the complete callback. */ |
b30bf55d LP |
480 | |
481 | return 0; | |
482 | } | |
483 | ||
424e490b | 484 | static int manager_dns_stub_tcp_fd(Manager *m) { |
b30bf55d LP |
485 | union sockaddr_union sa = { |
486 | .in.sin_family = AF_INET, | |
487 | .in.sin_addr.s_addr = htobe32(INADDR_DNS_STUB), | |
488 | .in.sin_port = htobe16(53), | |
489 | }; | |
424e490b | 490 | _cleanup_close_ int fd = -1; |
b30bf55d LP |
491 | int r; |
492 | ||
493 | if (m->dns_stub_tcp_fd >= 0) | |
494 | return m->dns_stub_tcp_fd; | |
495 | ||
424e490b ZJS |
496 | fd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
497 | if (fd < 0) | |
b30bf55d LP |
498 | return -errno; |
499 | ||
2ff48e98 LP |
500 | r = setsockopt_int(fd, IPPROTO_IP, IP_TTL, true); |
501 | if (r < 0) | |
502 | return r; | |
b30bf55d | 503 | |
2ff48e98 LP |
504 | r = setsockopt_int(fd, SOL_SOCKET, SO_REUSEADDR, true); |
505 | if (r < 0) | |
506 | return r; | |
b30bf55d | 507 | |
2ff48e98 LP |
508 | r = setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, true); |
509 | if (r < 0) | |
510 | return r; | |
b30bf55d | 511 | |
2ff48e98 LP |
512 | r = setsockopt_int(fd, IPPROTO_IP, IP_RECVTTL, true); |
513 | if (r < 0) | |
514 | return r; | |
b30bf55d LP |
515 | |
516 | /* Make sure no traffic from outside the local host can leak to onto this socket */ | |
424e490b ZJS |
517 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, "lo", 3) < 0) |
518 | return -errno; | |
b30bf55d | 519 | |
424e490b ZJS |
520 | if (bind(fd, &sa.sa, sizeof(sa.in)) < 0) |
521 | return -errno; | |
b30bf55d | 522 | |
424e490b ZJS |
523 | if (listen(fd, SOMAXCONN) < 0) |
524 | return -errno; | |
b30bf55d | 525 | |
424e490b | 526 | r = sd_event_add_io(m->event, &m->dns_stub_tcp_event_source, fd, EPOLLIN, on_dns_stub_stream, m); |
b30bf55d | 527 | if (r < 0) |
424e490b | 528 | return r; |
b30bf55d LP |
529 | |
530 | (void) sd_event_source_set_description(m->dns_stub_tcp_event_source, "dns-stub-tcp"); | |
531 | ||
c10d6bdb | 532 | return m->dns_stub_tcp_fd = TAKE_FD(fd); |
b30bf55d LP |
533 | } |
534 | ||
535 | int manager_dns_stub_start(Manager *m) { | |
424e490b | 536 | const char *t = "UDP"; |
01b0669e | 537 | int r = 0; |
b30bf55d LP |
538 | |
539 | assert(m); | |
540 | ||
d5da7707 ZJS |
541 | if (m->dns_stub_listener_mode == DNS_STUB_LISTENER_NO) |
542 | log_debug("Not creating stub listener."); | |
543 | else | |
544 | log_debug("Creating stub listener using %s.", | |
545 | m->dns_stub_listener_mode == DNS_STUB_LISTENER_UDP ? "UDP" : | |
546 | m->dns_stub_listener_mode == DNS_STUB_LISTENER_TCP ? "TCP" : | |
547 | "UDP/TCP"); | |
548 | ||
424e490b | 549 | if (IN_SET(m->dns_stub_listener_mode, DNS_STUB_LISTENER_YES, DNS_STUB_LISTENER_UDP)) |
1ae43295 | 550 | r = manager_dns_stub_udp_fd(m); |
b30bf55d | 551 | |
424e490b ZJS |
552 | if (r >= 0 && |
553 | IN_SET(m->dns_stub_listener_mode, DNS_STUB_LISTENER_YES, DNS_STUB_LISTENER_TCP)) { | |
554 | t = "TCP"; | |
1ae43295 | 555 | r = manager_dns_stub_tcp_fd(m); |
1ae43295 | 556 | } |
b30bf55d | 557 | |
0f4db364 ZJS |
558 | if (IN_SET(r, -EADDRINUSE, -EPERM)) { |
559 | if (r == -EADDRINUSE) | |
560 | log_warning_errno(r, | |
561 | "Another process is already listening on %s socket 127.0.0.53:53.\n" | |
562 | "Turning off local DNS stub support.", t); | |
563 | else | |
564 | log_warning_errno(r, | |
565 | "Failed to listen on %s socket 127.0.0.53:53: %m.\n" | |
566 | "Turning off local DNS stub support.", t); | |
424e490b ZJS |
567 | manager_dns_stub_stop(m); |
568 | } else if (r < 0) | |
569 | return log_error_errno(r, "Failed to listen on %s socket 127.0.0.53:53: %m", t); | |
b30bf55d LP |
570 | |
571 | return 0; | |
572 | } | |
573 | ||
574 | void manager_dns_stub_stop(Manager *m) { | |
575 | assert(m); | |
576 | ||
577 | m->dns_stub_udp_event_source = sd_event_source_unref(m->dns_stub_udp_event_source); | |
578 | m->dns_stub_tcp_event_source = sd_event_source_unref(m->dns_stub_tcp_event_source); | |
579 | ||
580 | m->dns_stub_udp_fd = safe_close(m->dns_stub_udp_fd); | |
581 | m->dns_stub_tcp_fd = safe_close(m->dns_stub_tcp_fd); | |
582 | } |