]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp-server.c
b28d13ba65abab8c2684f6529ca885030eea394c
[thirdparty/systemd.git] / src / network / networkd-dhcp-server.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/in.h>
4 #include <linux/if_arp.h>
5 #include <linux/if.h>
6
7 #include "sd-dhcp-server.h"
8
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "networkd-address.h"
12 #include "networkd-dhcp-server-bus.h"
13 #include "networkd-dhcp-server-static-lease.h"
14 #include "networkd-dhcp-server.h"
15 #include "networkd-link.h"
16 #include "networkd-manager.h"
17 #include "networkd-network.h"
18 #include "networkd-queue.h"
19 #include "networkd-route-util.h"
20 #include "parse-util.h"
21 #include "socket-netlink.h"
22 #include "string-table.h"
23 #include "string-util.h"
24 #include "strv.h"
25
26 static bool link_dhcp4_server_enabled(Link *link) {
27 assert(link);
28
29 if (link->flags & IFF_LOOPBACK)
30 return false;
31
32 if (!link->network)
33 return false;
34
35 if (link->iftype == ARPHRD_CAN)
36 return false;
37
38 return link->network->dhcp_server;
39 }
40
41 void network_adjust_dhcp_server(Network *network) {
42 assert(network);
43
44 if (!network->dhcp_server)
45 return;
46
47 if (network->bond) {
48 log_warning("%s: DHCPServer= is enabled for bond slave. Disabling DHCP server.",
49 network->filename);
50 network->dhcp_server = false;
51 return;
52 }
53
54 if (!in4_addr_is_set(&network->dhcp_server_address)) {
55 Address *address;
56 bool have = false;
57
58 ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) {
59 if (section_is_invalid(address->section))
60 continue;
61
62 if (address->family != AF_INET)
63 continue;
64
65 if (in4_addr_is_localhost(&address->in_addr.in))
66 continue;
67
68 if (in4_addr_is_link_local(&address->in_addr.in))
69 continue;
70
71 if (in4_addr_is_set(&address->in_addr_peer.in))
72 continue;
73
74 have = true;
75 break;
76 }
77 if (!have) {
78 log_warning("%s: DHCPServer= is enabled, but no static address configured. "
79 "Disabling DHCP server.",
80 network->filename);
81 network->dhcp_server = false;
82 return;
83 }
84 }
85 }
86
87 int link_request_dhcp_server_address(Link *link) {
88 _cleanup_(address_freep) Address *address = NULL;
89 Address *existing;
90 int r;
91
92 assert(link);
93 assert(link->network);
94
95 if (!link_dhcp4_server_enabled(link))
96 return 0;
97
98 if (!in4_addr_is_set(&link->network->dhcp_server_address))
99 return 0;
100
101 r = address_new(&address);
102 if (r < 0)
103 return r;
104
105 address->source = NETWORK_CONFIG_SOURCE_STATIC;
106 address->family = AF_INET;
107 address->in_addr.in = link->network->dhcp_server_address;
108 address->prefixlen = link->network->dhcp_server_address_prefixlen;
109 address_set_broadcast(address, link);
110
111 if (address_get(link, address, &existing) >= 0 &&
112 address_exists(existing) &&
113 existing->source == NETWORK_CONFIG_SOURCE_STATIC)
114 /* The same address seems explicitly configured in [Address] or [Network] section.
115 * Configure the DHCP server address only when it is not. */
116 return 0;
117
118 return link_request_static_address(link, TAKE_PTR(address), true);
119 }
120
121 static int link_find_dhcp_server_address(Link *link, Address **ret) {
122 Address *address;
123
124 assert(link);
125 assert(link->network);
126
127 /* If ServerAddress= is specified, then use the address. */
128 if (in4_addr_is_set(&link->network->dhcp_server_address))
129 return link_get_ipv4_address(link, &link->network->dhcp_server_address,
130 link->network->dhcp_server_address_prefixlen, ret);
131
132 /* If not, then select one from static addresses. */
133 SET_FOREACH(address, link->addresses) {
134 if (address->source != NETWORK_CONFIG_SOURCE_STATIC)
135 continue;
136 if (!address_exists(address))
137 continue;
138 if (address->family != AF_INET)
139 continue;
140 if (in4_addr_is_localhost(&address->in_addr.in))
141 continue;
142 if (in4_addr_is_link_local(&address->in_addr.in))
143 continue;
144 if (in4_addr_is_set(&address->in_addr_peer.in))
145 continue;
146
147 *ret = address;
148 return 0;
149 }
150
151 return -ENOENT;
152 }
153
154 static int dhcp_server_find_uplink(Link *link, Link **ret) {
155 assert(link);
156
157 if (link->network->dhcp_server_uplink_name)
158 return link_get_by_name(link->manager, link->network->dhcp_server_uplink_name, ret);
159
160 if (link->network->dhcp_server_uplink_index > 0)
161 return link_get_by_index(link->manager, link->network->dhcp_server_uplink_index, ret);
162
163 if (link->network->dhcp_server_uplink_index == UPLINK_INDEX_AUTO) {
164 /* It is not necessary to propagate error in automatic selection. */
165 if (manager_find_uplink(link->manager, AF_INET, link, ret) < 0)
166 *ret = NULL;
167 return 0;
168 }
169
170 *ret = NULL;
171 return 0;
172 }
173
174 static int link_push_uplink_to_dhcp_server(
175 Link *link,
176 sd_dhcp_lease_server_type_t what,
177 sd_dhcp_server *s) {
178
179 _cleanup_free_ struct in_addr *addresses = NULL;
180 bool use_dhcp_lease_data = true;
181 size_t n_addresses = 0;
182
183 assert(link);
184
185 if (!link->network)
186 return 0;
187 assert(link->network);
188
189 log_link_debug(link, "Copying %s from link", dhcp_lease_server_type_to_string(what));
190
191 switch (what) {
192
193 case SD_DHCP_LEASE_DNS:
194 /* For DNS we have a special case. We the data configured explicitly locally along with the
195 * data from the DHCP lease. */
196
197 for (unsigned i = 0; i < link->network->n_dns; i++) {
198 struct in_addr ia;
199
200 /* Only look for IPv4 addresses */
201 if (link->network->dns[i]->family != AF_INET)
202 continue;
203
204 ia = link->network->dns[i]->address.in;
205
206 /* Never propagate obviously borked data */
207 if (in4_addr_is_null(&ia) || in4_addr_is_localhost(&ia))
208 continue;
209
210 if (!GREEDY_REALLOC(addresses, n_addresses + 1))
211 return log_oom();
212
213 addresses[n_addresses++] = ia;
214 }
215
216 use_dhcp_lease_data = link->network->dhcp_use_dns;
217 break;
218
219 case SD_DHCP_LEASE_NTP: {
220 char **i;
221
222 /* For NTP things are similar, but for NTP hostnames can be configured too, which we cannot
223 * propagate via DHCP. Hence let's only propagate those which are IP addresses. */
224
225 STRV_FOREACH(i, link->network->ntp) {
226 union in_addr_union ia;
227
228 if (in_addr_from_string(AF_INET, *i, &ia) < 0)
229 continue;
230
231 /* Never propagate obviously borked data */
232 if (in4_addr_is_null(&ia.in) || in4_addr_is_localhost(&ia.in))
233 continue;
234
235 if (!GREEDY_REALLOC(addresses, n_addresses + 1))
236 return log_oom();
237
238 addresses[n_addresses++] = ia.in;
239 }
240
241 use_dhcp_lease_data = link->network->dhcp_use_ntp;
242 break;
243 }
244
245 case SD_DHCP_LEASE_SIP:
246
247 /* For SIP we don't allow explicit, local configuration, but there's control whether to use the data */
248 use_dhcp_lease_data = link->network->dhcp_use_sip;
249 break;
250
251 case SD_DHCP_LEASE_POP3:
252 case SD_DHCP_LEASE_SMTP:
253 case SD_DHCP_LEASE_LPR:
254 /* For the other server types we currently do not allow local configuration of server data,
255 * since there are typically no local consumers of the data. */
256 break;
257
258 default:
259 assert_not_reached();
260 }
261
262 if (use_dhcp_lease_data && link->dhcp_lease) {
263 const struct in_addr *da;
264
265 int n = sd_dhcp_lease_get_servers(link->dhcp_lease, what, &da);
266 if (n > 0) {
267 if (!GREEDY_REALLOC(addresses, n_addresses + n))
268 return log_oom();
269
270 for (int j = 0; j < n; j++)
271 if (in4_addr_is_non_local(&da[j]))
272 addresses[n_addresses++] = da[j];
273 }
274 }
275
276 if (n_addresses <= 0)
277 return 0;
278
279 return sd_dhcp_server_set_servers(s, what, addresses, n_addresses);
280 }
281
282 static int dhcp4_server_parse_dns_server_string_and_warn(
283 const char *string,
284 struct in_addr **addresses,
285 size_t *n_addresses) {
286
287 for (;;) {
288 _cleanup_free_ char *word = NULL, *server_name = NULL;
289 union in_addr_union address;
290 int family, r, ifindex = 0;
291
292 r = extract_first_word(&string, &word, NULL, 0);
293 if (r < 0)
294 return r;
295 if (r == 0)
296 break;
297
298 r = in_addr_ifindex_name_from_string_auto(word, &family, &address, &ifindex, &server_name);
299 if (r < 0) {
300 log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring: %m", word);
301 continue;
302 }
303
304 /* Only look for IPv4 addresses */
305 if (family != AF_INET)
306 continue;
307
308 /* Never propagate obviously borked data */
309 if (in4_addr_is_null(&address.in) || in4_addr_is_localhost(&address.in))
310 continue;
311
312 if (!GREEDY_REALLOC(*addresses, *n_addresses + 1))
313 return log_oom();
314
315 (*addresses)[(*n_addresses)++] = address.in;
316 }
317
318 return 0;
319 }
320
321 static int dhcp4_server_set_dns_from_resolve_conf(Link *link) {
322 _cleanup_free_ struct in_addr *addresses = NULL;
323 _cleanup_fclose_ FILE *f = NULL;
324 size_t n_addresses = 0;
325 int n = 0, r;
326
327 f = fopen(PRIVATE_UPLINK_RESOLV_CONF, "re");
328 if (!f) {
329 if (errno == ENOENT)
330 return 0;
331
332 return log_warning_errno(errno, "Failed to open " PRIVATE_UPLINK_RESOLV_CONF ": %m");
333 }
334
335 for (;;) {
336 _cleanup_free_ char *line = NULL;
337 const char *a;
338 char *l;
339
340 r = read_line(f, LONG_LINE_MAX, &line);
341 if (r < 0)
342 return log_error_errno(r, "Failed to read " PRIVATE_UPLINK_RESOLV_CONF ": %m");
343 if (r == 0)
344 break;
345
346 n++;
347
348 l = strstrip(line);
349 if (IN_SET(*l, '#', ';', 0))
350 continue;
351
352 a = first_word(l, "nameserver");
353 if (!a)
354 continue;
355
356 r = dhcp4_server_parse_dns_server_string_and_warn(a, &addresses, &n_addresses);
357 if (r < 0)
358 log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
359 }
360
361 if (n_addresses <= 0)
362 return 0;
363
364 return sd_dhcp_server_set_dns(link->dhcp_server, addresses, n_addresses);
365 }
366
367 static int dhcp4_server_configure(Link *link) {
368 bool acquired_uplink = false;
369 sd_dhcp_option *p;
370 DHCPStaticLease *static_lease;
371 Link *uplink = NULL;
372 Address *address;
373 bool bind_to_interface;
374 int r;
375
376 assert(link);
377
378 log_link_debug(link, "Configuring DHCP Server.");
379
380 if (link->dhcp_server)
381 return -EBUSY;
382
383 r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
384 if (r < 0)
385 return r;
386
387 r = sd_dhcp_server_attach_event(link->dhcp_server, link->manager->event, 0);
388 if (r < 0)
389 return r;
390
391 r = sd_dhcp_server_set_callback(link->dhcp_server, dhcp_server_callback, link);
392 if (r < 0)
393 return log_link_warning_errno(link, r, "Failed to set callback for DHCPv4 server instance: %m");
394
395 r = link_find_dhcp_server_address(link, &address);
396 if (r < 0)
397 return log_link_error_errno(link, r, "Failed to find suitable address for DHCPv4 server instance: %m");
398
399 /* use the server address' subnet as the pool */
400 r = sd_dhcp_server_configure_pool(link->dhcp_server, &address->in_addr.in, address->prefixlen,
401 link->network->dhcp_server_pool_offset, link->network->dhcp_server_pool_size);
402 if (r < 0)
403 return log_link_error_errno(link, r, "Failed to configure address pool for DHCPv4 server instance: %m");
404
405 if (link->network->dhcp_server_max_lease_time_usec > 0) {
406 r = sd_dhcp_server_set_max_lease_time(link->dhcp_server,
407 DIV_ROUND_UP(link->network->dhcp_server_max_lease_time_usec, USEC_PER_SEC));
408 if (r < 0)
409 return log_link_error_errno(link, r, "Failed to set maximum lease time for DHCPv4 server instance: %m");
410 }
411
412 if (link->network->dhcp_server_default_lease_time_usec > 0) {
413 r = sd_dhcp_server_set_default_lease_time(link->dhcp_server,
414 DIV_ROUND_UP(link->network->dhcp_server_default_lease_time_usec, USEC_PER_SEC));
415 if (r < 0)
416 return log_link_error_errno(link, r, "Failed to set default lease time for DHCPv4 server instance: %m");
417 }
418
419 for (sd_dhcp_lease_server_type_t type = 0; type < _SD_DHCP_LEASE_SERVER_TYPE_MAX; type ++) {
420
421 if (!link->network->dhcp_server_emit[type].emit)
422 continue;
423
424 if (link->network->dhcp_server_emit[type].n_addresses > 0)
425 /* Explicitly specified servers to emit */
426 r = sd_dhcp_server_set_servers(
427 link->dhcp_server,
428 type,
429 link->network->dhcp_server_emit[type].addresses,
430 link->network->dhcp_server_emit[type].n_addresses);
431 else {
432 /* Emission is requested, but nothing explicitly configured. Let's find a suitable upling */
433 if (!acquired_uplink) {
434 (void) dhcp_server_find_uplink(link, &uplink);
435 acquired_uplink = true;
436 }
437
438 if (uplink && uplink->network)
439 r = link_push_uplink_to_dhcp_server(uplink, type, link->dhcp_server);
440 else if (type == SD_DHCP_LEASE_DNS)
441 r = dhcp4_server_set_dns_from_resolve_conf(link);
442 else {
443 log_link_debug(link,
444 "Not emitting %s on link, couldn't find suitable uplink.",
445 dhcp_lease_server_type_to_string(type));
446 continue;
447 }
448 }
449
450 if (r < 0)
451 log_link_warning_errno(link, r,
452 "Failed to set %s for DHCP server, ignoring: %m",
453 dhcp_lease_server_type_to_string(type));
454 }
455
456 if (link->network->dhcp_server_emit_router) {
457 r = sd_dhcp_server_set_router(link->dhcp_server, &link->network->dhcp_server_router);
458 if (r < 0)
459 return log_link_error_errno(link, r, "Failed to set router address for DHCP server: %m");
460 }
461
462 r = sd_dhcp_server_set_relay_target(link->dhcp_server, &link->network->dhcp_server_relay_target);
463 if (r < 0)
464 return log_link_error_errno(link, r, "Failed to set relay target for DHCP server: %m");
465
466 bind_to_interface = sd_dhcp_server_is_in_relay_mode(link->dhcp_server) ? false : link->network->dhcp_server_bind_to_interface;
467 r = sd_dhcp_server_set_bind_to_interface(link->dhcp_server, bind_to_interface);
468 if (r < 0)
469 return log_link_error_errno(link, r, "Failed to set interface binding for DHCP server: %m");
470
471 r = sd_dhcp_server_set_relay_agent_information(link->dhcp_server, link->network->dhcp_server_relay_agent_circuit_id, link->network->dhcp_server_relay_agent_remote_id);
472 if (r < 0)
473 return log_link_error_errno(link, r, "Failed to set agent circuit/remote id for DHCP server: %m");
474
475 if (link->network->dhcp_server_emit_timezone) {
476 _cleanup_free_ char *buffer = NULL;
477 const char *tz = NULL;
478
479 if (link->network->dhcp_server_timezone)
480 tz = link->network->dhcp_server_timezone;
481 else {
482 r = get_timezone(&buffer);
483 if (r < 0)
484 log_link_warning_errno(link, r, "Failed to determine timezone, not sending timezone: %m");
485 else
486 tz = buffer;
487 }
488
489 if (tz) {
490 r = sd_dhcp_server_set_timezone(link->dhcp_server, tz);
491 if (r < 0)
492 return log_link_error_errno(link, r, "Failed to set timezone for DHCP server: %m");
493 }
494 }
495
496 ORDERED_HASHMAP_FOREACH(p, link->network->dhcp_server_send_options) {
497 r = sd_dhcp_server_add_option(link->dhcp_server, p);
498 if (r == -EEXIST)
499 continue;
500 if (r < 0)
501 return log_link_error_errno(link, r, "Failed to set DHCPv4 option: %m");
502 }
503
504 ORDERED_HASHMAP_FOREACH(p, link->network->dhcp_server_send_vendor_options) {
505 r = sd_dhcp_server_add_vendor_option(link->dhcp_server, p);
506 if (r == -EEXIST)
507 continue;
508 if (r < 0)
509 return log_link_error_errno(link, r, "Failed to set DHCPv4 option: %m");
510 }
511
512 HASHMAP_FOREACH(static_lease, link->network->dhcp_static_leases_by_section) {
513 r = sd_dhcp_server_set_static_lease(link->dhcp_server, &static_lease->address, static_lease->client_id, static_lease->client_id_size);
514 if (r < 0)
515 return log_link_error_errno(link, r, "Failed to set DHCPv4 static lease for DHCP server: %m");
516 }
517
518 r = sd_dhcp_server_start(link->dhcp_server);
519 if (r < 0)
520 return log_link_error_errno(link, r, "Could not start DHCPv4 server instance: %m");
521
522 log_link_debug(link, "Offering DHCPv4 leases");
523
524 return 1;
525 }
526
527 int link_request_dhcp_server(Link *link) {
528 assert(link);
529
530 if (!link_dhcp4_server_enabled(link))
531 return 0;
532
533 if (link->dhcp_server)
534 return 0;
535
536 log_link_debug(link, "Requesting DHCP server.");
537 return link_queue_request(link, REQUEST_TYPE_DHCP_SERVER, NULL, false, NULL, NULL, NULL);
538 }
539
540 static bool dhcp_server_is_ready_to_configure(Link *link) {
541 Link *uplink = NULL;
542 Address *a;
543
544 assert(link);
545
546 if (!link->network)
547 return false;
548
549 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
550 return false;
551
552 if (link->set_flags_messages > 0)
553 return false;
554
555 if (!link_has_carrier(link))
556 return false;
557
558 if (!link->static_addresses_configured)
559 return false;
560
561 if (link_find_dhcp_server_address(link, &a) < 0)
562 return false;
563
564 if (!address_is_ready(a))
565 return false;
566
567 if (dhcp_server_find_uplink(link, &uplink) < 0)
568 return false;
569
570 if (uplink && !uplink->network)
571 return false;
572
573 return true;
574 }
575
576 int request_process_dhcp_server(Request *req) {
577 assert(req);
578 assert(req->link);
579 assert(req->type == REQUEST_TYPE_DHCP_SERVER);
580
581 if (!dhcp_server_is_ready_to_configure(req->link))
582 return 0;
583
584 return dhcp4_server_configure(req->link);
585 }
586
587 int config_parse_dhcp_server_relay_agent_suboption(
588 const char *unit,
589 const char *filename,
590 unsigned line,
591 const char *section,
592 unsigned section_line,
593 const char *lvalue,
594 int ltype,
595 const char *rvalue,
596 void *data,
597 void *userdata) {
598
599 char **suboption_value = data;
600 char* p;
601
602 assert(filename);
603 assert(lvalue);
604 assert(rvalue);
605
606 if (isempty(rvalue)) {
607 *suboption_value = mfree(*suboption_value);
608 return 0;
609 }
610
611 p = startswith(rvalue, "string:");
612 if (!p) {
613 log_syntax(unit, LOG_WARNING, filename, line, 0,
614 "Failed to parse %s=%s'. Invalid format, ignoring.", lvalue, rvalue);
615 return 0;
616 }
617 return free_and_strdup(suboption_value, empty_to_null(p));
618 }
619
620 int config_parse_dhcp_server_emit(
621 const char *unit,
622 const char *filename,
623 unsigned line,
624 const char *section,
625 unsigned section_line,
626 const char *lvalue,
627 int ltype,
628 const char *rvalue,
629 void *data,
630 void *userdata) {
631
632 NetworkDHCPServerEmitAddress *emit = data;
633
634 assert(emit);
635 assert(rvalue);
636
637 if (isempty(rvalue)) {
638 emit->addresses = mfree(emit->addresses);
639 emit->n_addresses = 0;
640 return 0;
641 }
642
643 for (const char *p = rvalue;;) {
644 _cleanup_free_ char *w = NULL;
645 union in_addr_union a;
646 int r;
647
648 r = extract_first_word(&p, &w, NULL, 0);
649 if (r == -ENOMEM)
650 return log_oom();
651 if (r < 0) {
652 log_syntax(unit, LOG_WARNING, filename, line, r,
653 "Failed to extract word, ignoring: %s", rvalue);
654 return 0;
655 }
656 if (r == 0)
657 return 0;
658
659 if (streq(w, "_server_address"))
660 a = IN_ADDR_NULL; /* null address will be converted to the server address. */
661 else {
662 r = in_addr_from_string(AF_INET, w, &a);
663 if (r < 0) {
664 log_syntax(unit, LOG_WARNING, filename, line, r,
665 "Failed to parse %s= address '%s', ignoring: %m", lvalue, w);
666 continue;
667 }
668
669 if (in4_addr_is_null(&a.in)) {
670 log_syntax(unit, LOG_WARNING, filename, line, 0,
671 "Found a null address in %s=, ignoring.", lvalue);
672 continue;
673 }
674 }
675
676 if (!GREEDY_REALLOC(emit->addresses, emit->n_addresses + 1))
677 return log_oom();
678
679 emit->addresses[emit->n_addresses++] = a.in;
680 }
681 }
682
683 int config_parse_dhcp_server_address(
684 const char *unit,
685 const char *filename,
686 unsigned line,
687 const char *section,
688 unsigned section_line,
689 const char *lvalue,
690 int ltype,
691 const char *rvalue,
692 void *data,
693 void *userdata) {
694
695 Network *network = userdata;
696 union in_addr_union a;
697 unsigned char prefixlen;
698 int r;
699
700 assert(filename);
701 assert(lvalue);
702 assert(rvalue);
703
704 if (isempty(rvalue)) {
705 network->dhcp_server_address = (struct in_addr) {};
706 network->dhcp_server_address_prefixlen = 0;
707 return 0;
708 }
709
710 r = in_addr_prefix_from_string(rvalue, AF_INET, &a, &prefixlen);
711 if (r < 0) {
712 log_syntax(unit, LOG_WARNING, filename, line, r,
713 "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
714 return 0;
715 }
716 if (in4_addr_is_null(&a.in) || in4_addr_is_localhost(&a.in)) {
717 log_syntax(unit, LOG_WARNING, filename, line, 0,
718 "DHCP server address cannot be the ANY address or a localhost address, "
719 "ignoring assignment: %s", rvalue);
720 return 0;
721 }
722
723 network->dhcp_server_address = a.in;
724 network->dhcp_server_address_prefixlen = prefixlen;
725 return 0;
726 }