1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/in.h>
4 #include <netinet/tcp.h>
5 #include <resolv.h>
6
7 #include "errno-util.h"
8 #include "fd-util.h"
9 #include "resolved-llmnr.h"
10 #include "resolved-manager.h"
11
manager_llmnr_stop(Manager * m)12 void manager_llmnr_stop(Manager *m) {
13 assert(m);
14
15 m->llmnr_ipv4_udp_event_source = sd_event_source_disable_unref(m->llmnr_ipv4_udp_event_source);
16 m->llmnr_ipv4_udp_fd = safe_close(m->llmnr_ipv4_udp_fd);
17
18 m->llmnr_ipv6_udp_event_source = sd_event_source_disable_unref(m->llmnr_ipv6_udp_event_source);
19 m->llmnr_ipv6_udp_fd = safe_close(m->llmnr_ipv6_udp_fd);
20
21 m->llmnr_ipv4_tcp_event_source = sd_event_source_disable_unref(m->llmnr_ipv4_tcp_event_source);
22 m->llmnr_ipv4_tcp_fd = safe_close(m->llmnr_ipv4_tcp_fd);
23
24 m->llmnr_ipv6_tcp_event_source = sd_event_source_disable_unref(m->llmnr_ipv6_tcp_event_source);
25 m->llmnr_ipv6_tcp_fd = safe_close(m->llmnr_ipv6_tcp_fd);
26 }
27
manager_llmnr_start(Manager * m)28 int manager_llmnr_start(Manager *m) {
29 int r;
30
31 assert(m);
32
33 if (m->llmnr_support == RESOLVE_SUPPORT_NO)
34 return 0;
35
36 r = manager_llmnr_ipv4_udp_fd(m);
37 if (r == -EADDRINUSE)
38 goto eaddrinuse;
39 if (r < 0)
40 return r;
41
42 r = manager_llmnr_ipv4_tcp_fd(m);
43 if (r == -EADDRINUSE)
44 goto eaddrinuse;
45 if (r < 0)
46 return r;
47
48 if (socket_ipv6_is_supported()) {
49 r = manager_llmnr_ipv6_udp_fd(m);
50 if (r == -EADDRINUSE)
51 goto eaddrinuse;
52 if (r < 0)
53 return r;
54
55 r = manager_llmnr_ipv6_tcp_fd(m);
56 if (r == -EADDRINUSE)
57 goto eaddrinuse;
58 if (r < 0)
59 return r;
60 }
61
62 return 0;
63
64 eaddrinuse:
65 log_warning("Another LLMNR responder prohibits binding the socket to the same port. Turning off LLMNR support.");
66 m->llmnr_support = RESOLVE_SUPPORT_NO;
67 manager_llmnr_stop(m);
68
69 return 0;
70 }
71
on_llmnr_packet(sd_event_source * s,int fd,uint32_t revents,void * userdata)72 static int on_llmnr_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
73 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
74 DnsTransaction *t = NULL;
75 Manager *m = userdata;
76 DnsScope *scope;
77 int r;
78
79 assert(s);
80 assert(fd >= 0);
81 assert(m);
82
83 r = manager_recv(m, fd, DNS_PROTOCOL_LLMNR, &p);
84 if (r <= 0)
85 return r;
86
87 if (manager_packet_from_local_address(m, p))
88 return 0;
89
90 scope = manager_find_scope(m, p);
91 if (!scope) {
92 log_debug("Got LLMNR UDP packet on unknown scope. Ignoring.");
93 return 0;
94 }
95
96 if (dns_packet_validate_reply(p) > 0) {
97 log_debug("Got LLMNR UDP reply packet for id %u", DNS_PACKET_ID(p));
98
99 dns_scope_check_conflicts(scope, p);
100
101 t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p)));
102 if (t)
103 dns_transaction_process_reply(t, p, false);
104
105 } else if (dns_packet_validate_query(p) > 0) {
106 log_debug("Got LLMNR UDP query packet for id %u", DNS_PACKET_ID(p));
107
108 dns_scope_process_query(scope, NULL, p);
109 } else
110 log_debug("Invalid LLMNR UDP packet, ignoring.");
111
112 return 0;
113 }
114
set_llmnr_common_socket_options(int fd,int family)115 static int set_llmnr_common_socket_options(int fd, int family) {
116 int r;
117
118 r = socket_set_recvpktinfo(fd, family, true);
119 if (r < 0)
120 return r;
121
122 r = socket_set_recvttl(fd, family, true);
123 if (r < 0)
124 return r;
125
126 return 0;
127 }
128
set_llmnr_common_udp_socket_options(int fd,int family)129 static int set_llmnr_common_udp_socket_options(int fd, int family) {
130 int r;
131
132 /* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */
133 r = socket_set_ttl(fd, family, 255);
134 if (r < 0)
135 return r;
136
137 return 0;
138 }
139
manager_llmnr_ipv4_udp_fd(Manager * m)140 int manager_llmnr_ipv4_udp_fd(Manager *m) {
141 union sockaddr_union sa = {
142 .in.sin_family = AF_INET,
143 .in.sin_port = htobe16(LLMNR_PORT),
144 };
145 _cleanup_close_ int s = -1;
146 int r;
147
148 assert(m);
149
150 if (m->llmnr_ipv4_udp_fd >= 0)
151 return m->llmnr_ipv4_udp_fd;
152
153 s = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
154 if (s < 0)
155 return log_error_errno(errno, "LLMNR-IPv4(UDP): Failed to create socket: %m");
156
157 r = set_llmnr_common_socket_options(s, AF_INET);
158 if (r < 0)
159 return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to set common socket options: %m");
160
161 r = set_llmnr_common_udp_socket_options(s, AF_INET);
162 if (r < 0)
163 return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to set common UDP socket options: %m");
164
165 r = setsockopt_int(s, IPPROTO_IP, IP_MULTICAST_TTL, 255);
166 if (r < 0)
167 return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to set IP_MULTICAST_TTL: %m");
168
169 r = setsockopt_int(s, IPPROTO_IP, IP_MULTICAST_LOOP, true);
170 if (r < 0)
171 return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to set IP_MULTICAST_LOOP: %m");
172
173 /* Disable Don't-Fragment bit in the IP header */
174 r = setsockopt_int(s, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DONT);
175 if (r < 0)
176 return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to set IP_MTU_DISCOVER: %m");
177
178 /* first try to bind without SO_REUSEADDR to detect another LLMNR responder */
179 r = bind(s, &sa.sa, sizeof(sa.in));
180 if (r < 0) {
181 if (errno != EADDRINUSE)
182 return log_error_errno(errno, "LLMNR-IPv4(UDP): Failed to bind socket: %m");
183
184 log_warning("LLMNR-IPv4(UDP): There appears to be another LLMNR responder running, or previously systemd-resolved crashed with some outstanding transfers.");
185
186 /* try again with SO_REUSEADDR */
187 r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
188 if (r < 0)
189 return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to set SO_REUSEADDR: %m");
190
191 r = bind(s, &sa.sa, sizeof(sa.in));
192 if (r < 0)
193 return log_error_errno(errno, "LLMNR-IPv4(UDP): Failed to bind socket: %m");
194 } else {
195 /* enable SO_REUSEADDR for the case that the user really wants multiple LLMNR responders */
196 r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
197 if (r < 0)
198 return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to set SO_REUSEADDR: %m");
199 }
200
201 r = sd_event_add_io(m->event, &m->llmnr_ipv4_udp_event_source, s, EPOLLIN, on_llmnr_packet, m);
202 if (r < 0)
203 return log_error_errno(r, "LLMNR-IPv4(UDP): Failed to create event source: %m");
204
205 (void) sd_event_source_set_description(m->llmnr_ipv4_udp_event_source, "llmnr-ipv4-udp");
206
207 return m->llmnr_ipv4_udp_fd = TAKE_FD(s);
208 }
209
manager_llmnr_ipv6_udp_fd(Manager * m)210 int manager_llmnr_ipv6_udp_fd(Manager *m) {
211 union sockaddr_union sa = {
212 .in6.sin6_family = AF_INET6,
213 .in6.sin6_port = htobe16(LLMNR_PORT),
214 };
215 _cleanup_close_ int s = -1;
216 int r;
217
218 assert(m);
219
220 if (m->llmnr_ipv6_udp_fd >= 0)
221 return m->llmnr_ipv6_udp_fd;
222
223 s = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
224 if (s < 0)
225 return log_error_errno(errno, "LLMNR-IPv6(UDP): Failed to create socket: %m");
226
227 r = set_llmnr_common_socket_options(s, AF_INET6);
228 if (r < 0)
229 return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to set common socket options: %m");
230
231 r = set_llmnr_common_udp_socket_options(s, AF_INET6);
232 if (r < 0)
233 return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to set common UDP socket options: %m");
234
235 /* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */
236 r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 255);
237 if (r < 0)
238 return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to set IPV6_MULTICAST_HOPS: %m");
239
240 r = setsockopt_int(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, true);
241 if (r < 0)
242 return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to set IPV6_MULTICAST_LOOP: %m");
243
244 r = setsockopt_int(s, IPPROTO_IPV6, IPV6_V6ONLY, true);
245 if (r < 0)
246 return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to set IPV6_V6ONLY: %m");
247
248 /* first try to bind without SO_REUSEADDR to detect another LLMNR responder */
249 r = bind(s, &sa.sa, sizeof(sa.in6));
250 if (r < 0) {
251 if (errno != EADDRINUSE)
252 return log_error_errno(errno, "LLMNR-IPv6(UDP): Failed to bind socket: %m");
253
254 log_warning("LLMNR-IPv6(UDP): There appears to be another LLMNR responder running, or previously systemd-resolved crashed with some outstanding transfers.");
255
256 /* try again with SO_REUSEADDR */
257 r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
258 if (r < 0)
259 return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to set SO_REUSEADDR: %m");
260
261 r = bind(s, &sa.sa, sizeof(sa.in6));
262 if (r < 0)
263 return log_error_errno(errno, "LLMNR-IPv6(UDP): Failed to bind socket: %m");
264 } else {
265 /* enable SO_REUSEADDR for the case that the user really wants multiple LLMNR responders */
266 r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
267 if (r < 0)
268 return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to set SO_REUSEADDR: %m");
269 }
270
271 r = sd_event_add_io(m->event, &m->llmnr_ipv6_udp_event_source, s, EPOLLIN, on_llmnr_packet, m);
272 if (r < 0)
273 return log_error_errno(r, "LLMNR-IPv6(UDP): Failed to create event source: %m");
274
275 (void) sd_event_source_set_description(m->llmnr_ipv6_udp_event_source, "llmnr-ipv6-udp");
276
277 return m->llmnr_ipv6_udp_fd = TAKE_FD(s);
278 }
279
on_llmnr_stream_packet(DnsStream * s,DnsPacket * p)280 static int on_llmnr_stream_packet(DnsStream *s, DnsPacket *p) {
281 DnsScope *scope;
282
283 assert(s);
284 assert(s->manager);
285 assert(p);
286
287 scope = manager_find_scope(s->manager, p);
288 if (!scope)
289 log_debug("Got LLMNR TCP packet on unknown scope. Ignoring.");
290 else if (dns_packet_validate_query(p) > 0) {
291 log_debug("Got LLMNR TCP query packet for id %u", DNS_PACKET_ID(p));
292
293 dns_scope_process_query(scope, s, p);
294 } else
295 log_debug("Invalid LLMNR TCP packet, ignoring.");
296
297 return 0;
298 }
299
on_llmnr_stream(sd_event_source * s,int fd,uint32_t revents,void * userdata)300 static int on_llmnr_stream(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
301 DnsStream *stream;
302 Manager *m = userdata;
303 int cfd, r;
304
305 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
306 if (cfd < 0) {
307 if (ERRNO_IS_ACCEPT_AGAIN(errno))
308 return 0;
309
310 return -errno;
311 }
312
313 /* We don't configure a "complete" handler here, we rely on the default handler, thus freeing it */
314 r = dns_stream_new(m, &stream, DNS_STREAM_LLMNR_RECV, DNS_PROTOCOL_LLMNR, cfd, NULL,
315 on_llmnr_stream_packet, NULL, DNS_STREAM_DEFAULT_TIMEOUT_USEC);
316 if (r < 0) {
317 safe_close(cfd);
318 return r;
319 }
320
321 return 0;
322 }
323
set_llmnr_common_tcp_socket_options(int fd,int family)324 static int set_llmnr_common_tcp_socket_options(int fd, int family) {
325 int r;
326
327 /* RFC 4795, section 2.5. requires setting the TTL of TCP streams to 1 */
328 r = socket_set_ttl(fd, family, 1);
329 if (r < 0)
330 return r;
331
332 r = setsockopt_int(fd, IPPROTO_TCP, TCP_FASTOPEN, 5); /* Everybody appears to pick qlen=5, let's do the same here. */
333 if (r < 0)
334 log_debug_errno(r, "Failed to enable TCP_FASTOPEN on TCP listening socket, ignoring: %m");
335
336 r = setsockopt_int(fd, IPPROTO_TCP, TCP_NODELAY, true);
337 if (r < 0)
338 log_debug_errno(r, "Failed to enable TCP_NODELAY mode, ignoring: %m");
339
340 return 0;
341 }
342
manager_llmnr_ipv4_tcp_fd(Manager * m)343 int manager_llmnr_ipv4_tcp_fd(Manager *m) {
344 union sockaddr_union sa = {
345 .in.sin_family = AF_INET,
346 .in.sin_port = htobe16(LLMNR_PORT),
347 };
348 _cleanup_close_ int s = -1;
349 int r;
350
351 assert(m);
352
353 if (m->llmnr_ipv4_tcp_fd >= 0)
354 return m->llmnr_ipv4_tcp_fd;
355
356 s = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
357 if (s < 0)
358 return log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to create socket: %m");
359
360 r = set_llmnr_common_socket_options(s, AF_INET);
361 if (r < 0)
362 return log_error_errno(r, "LLMNR-IPv4(TCP): Failed to set common socket options: %m");
363
364 r = set_llmnr_common_tcp_socket_options(s, AF_INET);
365 if (r < 0)
366 return log_error_errno(r, "LLMNR-IPv4(TCP): Failed to set common TCP socket options: %m");
367
368 /* Disable Don't-Fragment bit in the IP header */
369 r = setsockopt_int(s, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DONT);
370 if (r < 0)
371 return log_error_errno(r, "LLMNR-IPv4(TCP): Failed to set IP_MTU_DISCOVER: %m");
372
373 /* first try to bind without SO_REUSEADDR to detect another LLMNR responder */
374 r = bind(s, &sa.sa, sizeof(sa.in));
375 if (r < 0) {
376 if (errno != EADDRINUSE)
377 return log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to bind socket: %m");
378
379 log_warning("LLMNR-IPv4(TCP): There appears to be another LLMNR responder running, or previously systemd-resolved crashed with some outstanding transfers.");
380
381 /* try again with SO_REUSEADDR */
382 r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
383 if (r < 0)
384 return log_error_errno(r, "LLMNR-IPv4(TCP): Failed to set SO_REUSEADDR: %m");
385
386 r = bind(s, &sa.sa, sizeof(sa.in));
387 if (r < 0)
388 return log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to bind socket: %m");
389 } else {
390 /* enable SO_REUSEADDR for the case that the user really wants multiple LLMNR responders */
391 r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
392 if (r < 0)
393 return log_error_errno(r, "LLMNR-IPv4(TCP): Failed to set SO_REUSEADDR: %m");
394 }
395
396 r = listen(s, SOMAXCONN);
397 if (r < 0)
398 return log_error_errno(errno, "LLMNR-IPv4(TCP): Failed to listen the stream: %m");
399
400 r = sd_event_add_io(m->event, &m->llmnr_ipv4_tcp_event_source, s, EPOLLIN, on_llmnr_stream, m);
401 if (r < 0)
402 return log_error_errno(r, "LLMNR-IPv4(TCP): Failed to create event source: %m");
403
404 (void) sd_event_source_set_description(m->llmnr_ipv4_tcp_event_source, "llmnr-ipv4-tcp");
405
406 return m->llmnr_ipv4_tcp_fd = TAKE_FD(s);
407 }
408
manager_llmnr_ipv6_tcp_fd(Manager * m)409 int manager_llmnr_ipv6_tcp_fd(Manager *m) {
410 union sockaddr_union sa = {
411 .in6.sin6_family = AF_INET6,
412 .in6.sin6_port = htobe16(LLMNR_PORT),
413 };
414 _cleanup_close_ int s = -1;
415 int r;
416
417 assert(m);
418
419 if (m->llmnr_ipv6_tcp_fd >= 0)
420 return m->llmnr_ipv6_tcp_fd;
421
422 s = socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
423 if (s < 0)
424 return log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to create socket: %m");
425
426 r = setsockopt_int(s, IPPROTO_IPV6, IPV6_V6ONLY, true);
427 if (r < 0)
428 return log_error_errno(r, "LLMNR-IPv6(TCP): Failed to set IPV6_V6ONLY: %m");
429
430 r = set_llmnr_common_socket_options(s, AF_INET6);
431 if (r < 0)
432 return log_error_errno(r, "LLMNR-IPv6(TCP): Failed to set common socket options: %m");
433
434 r = set_llmnr_common_tcp_socket_options(s, AF_INET6);
435 if (r < 0)
436 return log_error_errno(r, "LLMNR-IPv6(TCP): Failed to set common TCP socket options: %m");
437
438 /* first try to bind without SO_REUSEADDR to detect another LLMNR responder */
439 r = bind(s, &sa.sa, sizeof(sa.in6));
440 if (r < 0) {
441 if (errno != EADDRINUSE)
442 return log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to bind socket: %m");
443
444 log_warning("LLMNR-IPv6(TCP): There appears to be another LLMNR responder running, or previously systemd-resolved crashed with some outstanding transfers.");
445
446 /* try again with SO_REUSEADDR */
447 r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
448 if (r < 0)
449 return log_error_errno(r, "LLMNR-IPv6(TCP): Failed to set SO_REUSEADDR: %m");
450
451 r = bind(s, &sa.sa, sizeof(sa.in6));
452 if (r < 0)
453 return log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to bind socket: %m");
454 } else {
455 /* enable SO_REUSEADDR for the case that the user really wants multiple LLMNR responders */
456 r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
457 if (r < 0)
458 return log_error_errno(r, "LLMNR-IPv6(TCP): Failed to set SO_REUSEADDR: %m");
459 }
460
461 r = listen(s, SOMAXCONN);
462 if (r < 0)
463 return log_error_errno(errno, "LLMNR-IPv6(TCP): Failed to listen the stream: %m");
464
465 r = sd_event_add_io(m->event, &m->llmnr_ipv6_tcp_event_source, s, EPOLLIN, on_llmnr_stream, m);
466 if (r < 0)
467 return log_error_errno(r, "LLMNR-IPv6(TCP): Failed to create event source: %m");
468
469 (void) sd_event_source_set_description(m->llmnr_ipv6_tcp_event_source, "llmnr-ipv6-tcp");
470
471 return m->llmnr_ipv6_tcp_fd = TAKE_FD(s);
472 }
473