Debian Codegraph @

src/network/networkd-dhcp-server.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/if_arp.h>
4 #include <netinet/in.h>
5
6 #include "sd-dhcp-server.h"
7
8 #include "conf-parser.h"
9 #include "dhcp-protocol.h"
10 #include "dhcp-server-internal.h"
11 #include "dhcp-server-lease-internal.h"
12 #include "dns-domain.h"
13 #include "errno-util.h"
14 #include "extract-word.h"
15 #include "fd-util.h"
16 #include "fileio.h"
17 #include "hashmap.h"
18 #include "hostname-setup.h"
19 #include "iovec-util.h"
20 #include "network-common.h"
21 #include "networkd-address.h"
22 #include "networkd-dhcp-server.h"
23 #include "networkd-dhcp-server-bus.h"
24 #include "networkd-dhcp-server-static-lease.h"
25 #include "networkd-link.h"
26 #include "networkd-manager.h"
27 #include "networkd-network.h"
28 #include "networkd-ntp.h"
29 #include "networkd-queue.h"
30 #include "networkd-resolve-hook.h"
31 #include "networkd-route-util.h"
32 #include "path-util.h"
33 #include "set.h"
34 #include "socket-netlink.h"
35 #include "string-table.h"
36 #include "string-util.h"
37 #include "strv.h"
38
39 static bool link_dhcp4_server_enabled(Link *link) {
40 assert(link);
41
42 if (link->flags & IFF_LOOPBACK)
43 return false;
44
45 if (!link->network)
46 return false;
47
48 if (link->iftype == ARPHRD_CAN)
49 return false;
50
51 return link->network->dhcp_server;
52 }
53
54 int network_adjust_dhcp_server(Network *network, Set **addresses) {
55 int r;
56
57 assert(network);
58 assert(addresses);
59
60 if (!network->dhcp_server)
61 return 0;
62
63 if (network->bond) {
64 log_warning("%s: DHCPServer= is enabled for bond slave. Disabling DHCP server.",
65 network->filename);
66 network->dhcp_server = false;
67 return 0;
68 }
69
70 if (in4_addr_is_set(&network->dhcp_relay_target_address)) {
71 if (!in4_addr_is_set(&network->manager->dhcp_relay_server_address)) {
72 /* [DHCPServer] RelayTarget= in .network file is replaced with
73 * [DHCPRelay] ServerAddress= in networkd.conf. */
74 network->manager->dhcp_relay_server_address = network->dhcp_relay_target_address;
75
76 /* [DHCPServer] RelayAgentRemoteId= in .network file is replaced with
77 * [DHCPRelay] RemoteId= in networkd.conf. */
78 if (!iovec_is_set(&network->manager->dhcp_relay_remote_id)) {
79 iovec_done(&network->manager->dhcp_relay_remote_id);
80 network->manager->dhcp_relay_remote_id = TAKE_STRUCT(network->dhcp_relay_remote_id);
81 }
82 }
83
84 /* Copy [DHCPServer] ServerAddress= to [DHCPRelay] AgentAddress= if unspecified. */
85 if (!in4_addr_is_set(&network->dhcp_relay_agent_address_in_addr))
86 network->dhcp_relay_agent_address_in_addr = network->dhcp_server_address_in_addr;
87
88 /* Assume this interface acts as a downstream interface of the DHCP relay agent. Also,
89 * configure a catch-all upstream socket. */
90 network->dhcp_relay_interface_mode = DHCP_RELAY_INTERFACE_COMPAT;
91 network->dhcp_server = false;
92 return 0;
93 }
94
95 assert(network->dhcp_server_address_prefixlen <= 32);
96
97 if (network->dhcp_server_address_prefixlen == 0) {
98 Address *address;
99
100 /* If the server address is not specified, then find suitable static address. */
101
102 ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) {
103 assert(!section_is_invalid(address->section));
104
105 if (address->family != AF_INET)
106 continue;
107
108 if (in4_addr_is_localhost(&address->in_addr.in))
109 continue;
110
111 if (in4_addr_is_link_local(&address->in_addr.in))
112 continue;
113
114 if (in4_addr_is_set(&address->in_addr_peer.in))
115 continue;
116
117 /* TODO: check if the prefix length is small enough for the pool. */
118
119 network->dhcp_server_address = address;
120 address->used_by_dhcp_server = true;
121 break;
122 }
123 if (!network->dhcp_server_address) {
124 log_warning("%s: DHCPServer= is enabled, but no suitable static address configured. "
125 "Disabling DHCP server.",
126 network->filename);
127 network->dhcp_server = false;
128 return 0;
129 }
130
131 } else {
132 _cleanup_(address_unrefp) Address *a = NULL;
133 Address *existing;
134 unsigned line;
135
136 /* TODO: check if the prefix length is small enough for the pool. */
137
138 /* If an address is explicitly specified, then check if the corresponding [Address] section
139 * is configured, and add one if not. */
140
141 existing = set_get(*addresses,
142 &(Address) {
143 .family = AF_INET,
144 .in_addr.in = network->dhcp_server_address_in_addr,
145 .prefixlen = network->dhcp_server_address_prefixlen,
146 });
147 if (existing) {
148 /* Corresponding [Address] section already exists. */
149 network->dhcp_server_address = existing;
150 return 0;
151 }
152
153 r = ordered_hashmap_by_section_find_unused_line(network->addresses_by_section, network->filename, &line);
154 if (r < 0)
155 return log_warning_errno(r, "%s: Failed to find unused line number for DHCP server address: %m",
156 network->filename);
157
158 r = address_new_static(network, network->filename, line, &a);
159 if (r < 0)
160 return log_warning_errno(r, "%s: Failed to add new static address object for DHCP server: %m",
161 network->filename);
162
163 a->family = AF_INET;
164 a->prefixlen = network->dhcp_server_address_prefixlen;
165 a->in_addr.in = network->dhcp_server_address_in_addr;
166 a->requested_as_null = !in4_addr_is_set(&network->dhcp_server_address_in_addr);
167 a->used_by_dhcp_server = true;
168
169 r = address_section_verify(a);
170 if (r < 0)
171 return r;
172
173 r = set_ensure_put(addresses, &address_hash_ops, a);
174 if (r < 0)
175 return log_oom();
176 assert(r > 0);
177
178 network->dhcp_server_address = TAKE_PTR(a);
179 }
180
181 return 0;
182 }
183
184 static DHCPServerPersistLeases link_get_dhcp_server_persist_leases(Link *link) {
185 assert(link);
186 assert(link->manager);
187 assert(link->network);
188
189 if (link->network->dhcp_server_persist_leases >= 0)
190 return link->network->dhcp_server_persist_leases;
191
192 return link->manager->dhcp_server_persist_leases;
193 }
194
195 static int link_get_dhcp_server_lease_file(Link *link, int *ret_dir_fd, char **ret_path) {
196 assert(link);
197 assert(link->ifname);
198 assert(ret_dir_fd);
199 assert(ret_path);
200
201 /* This does not copy fd. Do not close fd stored in ret_dir_fd. */
202
203 switch (link_get_dhcp_server_persist_leases(link)) {
204 case DHCP_SERVER_PERSIST_LEASES_NO:
205 *ret_dir_fd = -EBADF;
206 *ret_path = NULL;
207 return 0;
208
209 case DHCP_SERVER_PERSIST_LEASES_YES: {
210 if (link->manager->persistent_storage_fd < 0)
211 return -EBUSY; /* persistent storage is not ready. */
212
213 char *p = path_join("dhcp-server-lease", link->ifname);
214 if (!p)
215 return -ENOMEM;
216
217 *ret_dir_fd = link->manager->persistent_storage_fd;
218 *ret_path = p;
219 return 1;
220 }
221 case DHCP_SERVER_PERSIST_LEASES_RUNTIME: {
222 char *p = path_join("/run/systemd/netif/dhcp-server-lease", link->ifname);
223 if (!p)
224 return -ENOMEM;
225
226 *ret_dir_fd = AT_FDCWD;
227 *ret_path = p;
228 return 1;
229 }
230 default:
231 assert_not_reached();
232 }
233 }
234
235 int address_acquire_from_dhcp_server_leases_file(Link *link, const Address *address, union in_addr_union *ret) {
236 int r;
237
238 assert(link);
239 assert(link->manager);
240 assert(address);
241 assert(ret);
242
243 /* If the DHCP server address is configured as a null address, reuse the server address of the
244 * previous instance. */
245 if (address->family != AF_INET)
246 return -ENOENT;
247
248 if (!address->used_by_dhcp_server)
249 return -ENOENT;
250
251 if (!link_dhcp4_server_enabled(link))
252 return -ENOENT;
253
254 _cleanup_free_ char *lease_file = NULL;
255 int dir_fd;
256 r = link_get_dhcp_server_lease_file(link, &dir_fd, &lease_file);
257 if (r < 0)
258 return r;
259 if (r == 0) /* persistent leases is disabled */
260 return -ENOENT;
261
262 struct in_addr a;
263 uint8_t prefixlen;
264 r = dhcp_server_leases_file_get_server_address(
265 dir_fd,
266 lease_file,
267 &a,
268 &prefixlen);
269 if (r == -ENOENT)
270 return r;
271 if (r < 0)
272 return log_warning_errno(r, "Failed to load lease file %s: %s",
273 lease_file,
274 r == -ENXIO ? "expected JSON content not found" :
275 r == -EINVAL ? "invalid JSON" :
276 STRERROR(r));
277
278 if (prefixlen != address->prefixlen)
279 return -ENOENT;
280
281 ret->in = a;
282 return 0;
283 }
284
285 int link_start_dhcp4_server(Link *link) {
286 int r;
287
288 assert(link);
289 assert(link->manager);
290
291 if (!link->dhcp_server)
292 return 0; /* Not configured yet. */
293
294 if (!link_has_carrier(link))
295 return 0;
296
297 if (sd_dhcp_server_is_running(link->dhcp_server))
298 return 0; /* already started. */
299
300 /* TODO: Maybe, also check the system time is synced. If the system does not have RTC battery, then
301 * the realtime clock in not usable in the early boot stage, and all saved leases may be wrongly
302 * handled as expired and dropped. */
303 _cleanup_free_ char *lease_file = NULL;
304 int dir_fd;
305 r = link_get_dhcp_server_lease_file(link, &dir_fd, &lease_file);
306 if (r == -EBUSY)
307 return 0; /* persistent storage is not ready. */
308 if (r < 0)
309 return r;
310 if (r > 0) {
311 r = sd_dhcp_server_set_lease_file(link->dhcp_server, dir_fd, lease_file);
312 if (r < 0)
313 return r;
314 }
315
316 r = sd_dhcp_server_start(link->dhcp_server);
317 if (r < 0)
318 return r;
319
320 log_link_debug(link, "Offering DHCPv4 leases");
321 return 0;
322 }
323
324 void manager_toggle_dhcp4_server_state(Manager *manager, bool start) {
325 Link *link;
326 int r;
327
328 assert(manager);
329
330 HASHMAP_FOREACH(link, manager->links_by_index) {
331 if (!link->dhcp_server)
332 continue;
333 if (link_get_dhcp_server_persist_leases(link) != DHCP_SERVER_PERSIST_LEASES_YES)
334 continue;
335
336 /* Even if 'start' is true, first we need to stop the server. Otherwise, we cannot (re)set
337 * the lease file in link_start_dhcp4_server(). */
338 r = sd_dhcp_server_stop(link->dhcp_server);
339 if (r < 0)
340 log_link_debug_errno(link, r, "Failed to stop DHCP server, ignoring: %m");
341
342 if (!start)
343 continue;
344
345 r = link_start_dhcp4_server(link);
346 if (r < 0)
347 log_link_debug_errno(link, r, "Failed to start DHCP server, ignoring: %m");
348 }
349 }
350
351 static int dhcp_server_find_uplink(Link *link, Link **ret) {
352 assert(link);
353 assert(ret);
354
355 if (link->network->dhcp_server_uplink_name)
356 return link_get_by_name(link->manager, link->network->dhcp_server_uplink_name, ret);
357
358 if (link->network->dhcp_server_uplink_index > 0)
359 return link_get_by_index(link->manager, link->network->dhcp_server_uplink_index, ret);
360
361 if (link->network->dhcp_server_uplink_index == UPLINK_INDEX_AUTO) {
362 /* It is not necessary to propagate error in automatic selection. */
363 if (manager_find_uplink(link->manager, AF_INET, link, ret) < 0)
364 *ret = NULL;
365 return 0;
366 }
367
368 *ret = NULL;
369 return 0;
370 }
371
372 static int link_push_uplink_to_dhcp_server(
373 Link *link,
374 sd_dhcp_lease_server_type_t what,
375 sd_dhcp_server *s) {
376
377 _cleanup_free_ struct in_addr *addresses = NULL;
378 bool use_dhcp_lease_data = true;
379 size_t n_addresses = 0;
380
381 assert(link);
382
383 if (!link->network)
384 return 0;
385 assert(link->network);
386
387 log_link_debug(link, "Copying %s from link", dhcp_lease_server_type_to_string(what));
388
389 switch (what) {
390
391 case SD_DHCP_LEASE_DNS:
392 /* For DNS we have a special case. We the data configured explicitly locally along with the
393 * data from the DHCP lease. */
394
395 for (unsigned i = 0; i < link->network->n_dns; i++) {
396 struct in_addr ia;
397
398 /* Only look for IPv4 addresses */
399 if (link->network->dns[i]->family != AF_INET)
400 continue;
401
402 ia = link->network->dns[i]->address.in;
403
404 /* Never propagate obviously borked data */
405 if (in4_addr_is_null(&ia) || in4_addr_is_localhost(&ia))
406 continue;
407
408 if (!GREEDY_REALLOC(addresses, n_addresses + 1))
409 return log_oom();
410
411 addresses[n_addresses++] = ia;
412 }
413
414 use_dhcp_lease_data = link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4);
415 break;
416
417 case SD_DHCP_LEASE_NTP: {
418 /* For NTP things are similar, but for NTP hostnames can be configured too, which we cannot
419 * propagate via DHCP. Hence let's only propagate those which are IP addresses. */
420
421 STRV_FOREACH(i, link->network->ntp) {
422 union in_addr_union ia;
423
424 if (in_addr_from_string(AF_INET, *i, &ia) < 0)
425 continue;
426
427 /* Never propagate obviously borked data */
428 if (in4_addr_is_null(&ia.in) || in4_addr_is_localhost(&ia.in))
429 continue;
430
431 if (!GREEDY_REALLOC(addresses, n_addresses + 1))
432 return log_oom();
433
434 addresses[n_addresses++] = ia.in;
435 }
436
437 use_dhcp_lease_data = link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4);
438 break;
439 }
440
441 case SD_DHCP_LEASE_SIP:
442
443 /* For SIP we don't allow explicit, local configuration, but there's control whether to use the data */
444 use_dhcp_lease_data = link->network->dhcp_use_sip;
445 break;
446
447 case SD_DHCP_LEASE_POP3:
448 case SD_DHCP_LEASE_SMTP:
449 case SD_DHCP_LEASE_LPR:
450 /* For the other server types we currently do not allow local configuration of server data,
451 * since there are typically no local consumers of the data. */
452 break;
453
454 default:
455 assert_not_reached();
456 }
457
458 if (use_dhcp_lease_data && link->dhcp_lease) {
459 const struct in_addr *da;
460
461 int n = sd_dhcp_lease_get_servers(link->dhcp_lease, what, &da);
462 if (n > 0) {
463 if (!GREEDY_REALLOC(addresses, n_addresses + n))
464 return log_oom();
465
466 for (int j = 0; j < n; j++)
467 if (in4_addr_is_non_local(&da[j]))
468 addresses[n_addresses++] = da[j];
469 }
470 }
471
472 if (n_addresses <= 0)
473 return 0;
474
475 return sd_dhcp_server_set_servers(s, what, addresses, n_addresses);
476 }
477
478 static int dhcp4_server_parse_dns_server_string_and_warn(
479 const char *string,
480 struct in_addr **addresses,
481 size_t *n_addresses) {
482
483 assert(addresses);
484 assert(n_addresses);
485
486 for (;;) {
487 _cleanup_free_ char *word = NULL, *server_name = NULL;
488 union in_addr_union address;
489 int family, r, ifindex = 0;
490
491 r = extract_first_word(&string, &word, NULL, 0);
492 if (r < 0)
493 return r;
494 if (r == 0)
495 break;
496
497 r = in_addr_ifindex_name_from_string_auto(word, &family, &address, &ifindex, &server_name);
498 if (r < 0) {
499 log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring: %m", word);
500 continue;
501 }
502
503 /* Only look for IPv4 addresses */
504 if (family != AF_INET)
505 continue;
506
507 /* Never propagate obviously borked data */
508 if (in4_addr_is_null(&address.in) || in4_addr_is_localhost(&address.in))
509 continue;
510
511 if (!GREEDY_REALLOC(*addresses, *n_addresses + 1))
512 return log_oom();
513
514 (*addresses)[(*n_addresses)++] = address.in;
515 }
516
517 return 0;
518 }
519
520 static int dhcp4_server_set_dns_from_resolve_conf(Link *link) {
521 _cleanup_free_ struct in_addr *addresses = NULL;
522 _cleanup_fclose_ FILE *f = NULL;
523 size_t n_addresses = 0;
524 int r;
525
526 f = fopen(PRIVATE_UPLINK_RESOLV_CONF, "re");
527 if (!f) {
528 if (errno == ENOENT)
529 return 0;
530
531 return log_warning_errno(errno, "Failed to open " PRIVATE_UPLINK_RESOLV_CONF ": %m");
532 }
533
534 for (;;) {
535 _cleanup_free_ char *line = NULL;
536 const char *a;
537
538 r = read_stripped_line(f, LONG_LINE_MAX, &line);
539 if (r < 0)
540 return log_error_errno(r, "Failed to read " PRIVATE_UPLINK_RESOLV_CONF ": %m");
541 if (r == 0)
542 break;
543
544 if (IN_SET(*line, '#', ';', 0))
545 continue;
546
547 a = first_word(line, "nameserver");
548 if (!a)
549 continue;
550
551 r = dhcp4_server_parse_dns_server_string_and_warn(a, &addresses, &n_addresses);
552 if (r < 0)
553 log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
554 }
555
556 if (n_addresses <= 0)
557 return 0;
558
559 return sd_dhcp_server_set_dns(link->dhcp_server, addresses, n_addresses);
560 }
561
562 static int dhcp_server_set_domain(Link *link) {
563 int r;
564
565 assert(link);
566 assert(link->network);
567 assert(link->dhcp_server);
568
569 if (!link->network->dhcp_server_emit_domain)
570 return 0;
571
572 if (link->network->dhcp_server_domain)
573 return sd_dhcp_server_set_domain_name(link->dhcp_server, link->network->dhcp_server_domain);
574
575 /* When domain is not specified, use the domain part of the current hostname. */
576 _cleanup_free_ char *hostname = NULL;
577 r = gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT, &hostname);
578 if (r < 0)
579 return r;
580
581 const char *domain = hostname;
582 r = dns_name_parent(&domain);
583 if (r < 0)
584 return r;
585
586 if (isempty(domain))
587 return -ENXIO;
588
589 return sd_dhcp_server_set_domain_name(link->dhcp_server, domain);
590 }
591
592 static int dhcp4_server_configure(Link *link) {
593 bool acquired_uplink = false;
594 DHCPStaticLease *static_lease;
595 Link *uplink = NULL;
596 Address *address;
597 int r;
598
599 assert(link);
600 assert(link->network);
601 assert(link->network->dhcp_server_address);
602
603 log_link_debug(link, "Configuring DHCP Server.");
604
605 if (link->dhcp_server)
606 return -EBUSY;
607
608 r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
609 if (r < 0)
610 return r;
611
612 r = sd_dhcp_server_attach_event(link->dhcp_server, link->manager->event, 0);
613 if (r < 0)
614 return r;
615
616 r = sd_dhcp_server_set_callback(link->dhcp_server, dhcp_server_callback, link);
617 if (r < 0)
618 return log_link_warning_errno(link, r, "Failed to set callback for DHCPv4 server instance: %m");
619
620 r = address_get(link, link->network->dhcp_server_address, &address);
621 if (r < 0)
622 return log_link_error_errno(link, r, "Failed to find suitable address for DHCPv4 server instance: %m");
623
624 /* use the server address' subnet as the pool */
625 r = sd_dhcp_server_configure_pool(link->dhcp_server, &address->in_addr.in, address->prefixlen,
626 link->network->dhcp_server_pool_offset, link->network->dhcp_server_pool_size);
627 if (r < 0)
628 return log_link_error_errno(link, r, "Failed to configure address pool for DHCPv4 server instance: %m");
629
630 if (link->network->dhcp_server_max_lease_time_usec > 0) {
631 r = sd_dhcp_server_set_max_lease_time(link->dhcp_server, link->network->dhcp_server_max_lease_time_usec);
632 if (r < 0)
633 return log_link_error_errno(link, r, "Failed to set maximum lease time for DHCPv4 server instance: %m");
634 }
635
636 if (link->network->dhcp_server_default_lease_time_usec > 0) {
637 r = sd_dhcp_server_set_default_lease_time(link->dhcp_server, link->network->dhcp_server_default_lease_time_usec);
638 if (r < 0)
639 return log_link_error_errno(link, r, "Failed to set default lease time for DHCPv4 server instance: %m");
640 }
641
642 r = sd_dhcp_server_set_ipv6_only_preferred_usec(link->dhcp_server, link->network->dhcp_server_ipv6_only_preferred_usec);
643 if (r < 0)
644 return log_link_error_errno(link, r, "Failed to set IPv6 only preferred time for DHCPv4 server instance: %m");
645
646 r = sd_dhcp_server_set_boot_server_address(link->dhcp_server, &link->network->dhcp_server_boot_server_address);
647 if (r < 0)
648 return log_link_warning_errno(link, r, "Failed to set boot server address for DHCPv4 server instance: %m");
649
650 r = sd_dhcp_server_set_boot_server_name(link->dhcp_server, link->network->dhcp_server_boot_server_name);
651 if (r < 0)
652 return log_link_warning_errno(link, r, "Failed to set boot server name for DHCPv4 server instance: %m");
653
654 r = sd_dhcp_server_set_boot_filename(link->dhcp_server, link->network->dhcp_server_boot_filename);
655 if (r < 0)
656 return log_link_warning_errno(link, r, "Failed to set boot filename for DHCPv4 server instance: %m");
657
658 r = sd_dhcp_server_set_rapid_commit(link->dhcp_server, link->network->dhcp_server_rapid_commit);
659 if (r < 0)
660 return log_link_warning_errno(link, r, "Failed to %s Rapid Commit support for DHCPv4 server instance: %m",
661 enable_disable(link->network->dhcp_server_rapid_commit));
662
663 for (sd_dhcp_lease_server_type_t type = 0; type < _SD_DHCP_LEASE_SERVER_TYPE_MAX; type++) {
664
665 if (!link->network->dhcp_server_emit[type].emit)
666 continue;
667
668 if (link->network->dhcp_server_emit[type].n_addresses > 0)
669 /* Explicitly specified servers to emit */
670 r = sd_dhcp_server_set_servers(
671 link->dhcp_server,
672 type,
673 link->network->dhcp_server_emit[type].addresses,
674 link->network->dhcp_server_emit[type].n_addresses);
675 else {
676 /* Emission is requested, but nothing explicitly configured. Let's find a suitable upling */
677 if (!acquired_uplink) {
678 (void) dhcp_server_find_uplink(link, &uplink);
679 acquired_uplink = true;
680 }
681
682 if (uplink && uplink->network)
683 r = link_push_uplink_to_dhcp_server(uplink, type, link->dhcp_server);
684 else if (type == SD_DHCP_LEASE_DNS)
685 r = dhcp4_server_set_dns_from_resolve_conf(link);
686 else {
687 log_link_debug(link,
688 "Not emitting %s on link, couldn't find suitable uplink.",
689 dhcp_lease_server_type_to_string(type));
690 continue;
691 }
692 }
693
694 if (r < 0)
695 log_link_warning_errno(link, r,
696 "Failed to set %s for DHCP server, ignoring: %m",
697 dhcp_lease_server_type_to_string(type));
698 }
699
700 if (link->network->dhcp_server_emit_router) {
701 r = sd_dhcp_server_set_router(link->dhcp_server, &link->network->dhcp_server_router);
702 if (r < 0)
703 return log_link_error_errno(link, r, "Failed to set router address for DHCP server: %m");
704 }
705
706 if (link->network->dhcp_server_emit_timezone) {
707 _cleanup_free_ char *buffer = NULL;
708 const char *tz = NULL;
709
710 if (link->network->dhcp_server_timezone)
711 tz = link->network->dhcp_server_timezone;
712 else {
713 r = get_timezone(&buffer);
714 if (r < 0)
715 log_link_warning_errno(link, r, "Failed to determine timezone, not sending timezone: %m");
716 else
717 tz = buffer;
718 }
719
720 if (tz) {
721 r = sd_dhcp_server_set_timezone(link->dhcp_server, tz);
722 if (r < 0)
723 return log_link_error_errno(link, r, "Failed to set timezone for DHCP server: %m");
724 }
725 }
726
727 r = dhcp_server_set_domain(link);
728 if (r == -ENXIO)
729 log_link_warning_errno(link, r, "Cannot get domain from the current hostname, DHCP server will not emit domain option.");
730 else if (r < 0)
731 return log_link_error_errno(link, r, "Failed to set domain name for DHCP server: %m");
732
733 r = dhcp_server_set_extra_options(link->dhcp_server, &link->network->dhcp_server_extra_options);
734 if (r < 0)
735 return log_link_error_errno(link, r, "Failed to set DHCPv4 extra options: %m");
736
737 r = dhcp_server_set_vendor_options(link->dhcp_server, &link->network->dhcp_server_vendor_options);
738 if (r < 0)
739 return log_link_error_errno(link, r, "Failed to set DHCPv4 vendor options: %m");
740
741 HASHMAP_FOREACH(static_lease, link->network->dhcp_static_leases_by_section) {
742 r = sd_dhcp_server_set_static_lease(
743 link->dhcp_server,
744 &static_lease->address,
745 static_lease->client_id,
746 static_lease->client_id_size,
747 static_lease->hostname);
748 if (r < 0)
749 return log_link_error_errno(link, r, "Failed to set DHCPv4 static lease for DHCP server: %m");
750 }
751
752 r = link_start_dhcp4_server(link);
753 if (r < 0)
754 return log_link_error_errno(link, r, "Could not start DHCPv4 server instance: %m");
755
756 manager_notify_hook_filters(link->manager);
757
758 return 0;
759 }
760
761 static bool dhcp_server_is_ready_to_configure(Link *link) {
762 Link *uplink = NULL;
763 Address *a;
764
765 assert(link);
766 assert(link->network);
767 assert(link->network->dhcp_server_address);
768
769 if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ false))
770 return false;
771
772 if (!link_has_carrier(link))
773 return false;
774
775 if (!link->static_addresses_configured)
776 return false;
777
778 if (address_get(link, link->network->dhcp_server_address, &a) < 0)
779 return false;
780
781 if (!address_is_ready(a))
782 return false;
783
784 if (dhcp_server_find_uplink(link, &uplink) < 0)
785 return false;
786
787 if (uplink && !uplink->network)
788 return false;
789
790 return true;
791 }
792
793 static int dhcp_server_process_request(Request *req, Link *link, void *userdata) {
794 int r;
795
796 assert(link);
797
798 if (!dhcp_server_is_ready_to_configure(link))
799 return 0;
800
801 r = dhcp4_server_configure(link);
802 if (r < 0)
803 return log_link_warning_errno(link, r, "Failed to configure DHCP server: %m");
804
805 return 1;
806 }
807
808 int link_request_dhcp_server(Link *link) {
809 int r;
810
811 assert(link);
812
813 if (!link_dhcp4_server_enabled(link))
814 return 0;
815
816 if (link->dhcp_server)
817 return 0;
818
819 log_link_debug(link, "Requesting DHCP server.");
820 r = link_queue_request(link, REQUEST_TYPE_DHCP_SERVER, dhcp_server_process_request, NULL);
821 if (r < 0)
822 return log_link_warning_errno(link, r, "Failed to request configuration of DHCP server: %m");
823
824 return 0;
825 }
826
827 int config_parse_dhcp_server_emit(
828 const char *unit,
829 const char *filename,
830 unsigned line,
831 const char *section,
832 unsigned section_line,
833 const char *lvalue,
834 int ltype,
835 const char *rvalue,
836 void *data,
837 void *userdata) {
838
839 NetworkDHCPServerEmitAddress *emit = ASSERT_PTR(data);
840
841 assert(rvalue);
842
843 if (isempty(rvalue)) {
844 emit->addresses = mfree(emit->addresses);
845 emit->n_addresses = 0;
846 return 0;
847 }
848
849 for (const char *p = rvalue;;) {
850 _cleanup_free_ char *w = NULL;
851 union in_addr_union a;
852 int r;
853
854 r = extract_first_word(&p, &w, NULL, 0);
855 if (r == -ENOMEM)
856 return log_oom();
857 if (r < 0) {
858 log_syntax(unit, LOG_WARNING, filename, line, r,
859 "Failed to extract word, ignoring: %s", rvalue);
860 return 0;
861 }
862 if (r == 0)
863 return 0;
864
865 if (streq(w, "_server_address"))
866 a = IN_ADDR_NULL; /* null address will be converted to the server address. */
867 else {
868 r = in_addr_from_string(AF_INET, w, &a);
869 if (r < 0) {
870 log_syntax(unit, LOG_WARNING, filename, line, r,
871 "Failed to parse %s= address '%s', ignoring: %m", lvalue, w);
872 continue;
873 }
874
875 if (in4_addr_is_null(&a.in)) {
876 log_syntax(unit, LOG_WARNING, filename, line, 0,
877 "Found a null address in %s=, ignoring.", lvalue);
878 continue;
879 }
880 }
881
882 if (!GREEDY_REALLOC(emit->addresses, emit->n_addresses + 1))
883 return log_oom();
884
885 emit->addresses[emit->n_addresses++] = a.in;
886 }
887 }
888
889 int config_parse_dhcp_server_address(
890 const char *unit,
891 const char *filename,
892 unsigned line,
893 const char *section,
894 unsigned section_line,
895 const char *lvalue,
896 int ltype,
897 const char *rvalue,
898 void *data,
899 void *userdata) {
900
901 Network *network = ASSERT_PTR(userdata);
902 union in_addr_union a;
903 unsigned char prefixlen;
904 int r;
905
906 assert(filename);
907 assert(lvalue);
908 assert(rvalue);
909
910 if (isempty(rvalue)) {
911 network->dhcp_server_address_in_addr = (struct in_addr) {};
912 network->dhcp_server_address_prefixlen = 0;
913 return 0;
914 }
915
916 r = in_addr_prefix_from_string(rvalue, AF_INET, &a, &prefixlen);
917 if (r < 0) {
918 log_syntax(unit, LOG_WARNING, filename, line, r,
919 "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
920 return 0;
921 }
922 if (in4_addr_is_localhost(&a.in) || in4_addr_is_link_local(&a.in)) {
923 log_syntax(unit, LOG_WARNING, filename, line, 0,
924 "DHCP server address cannot be a localhost or link-local address, "
925 "ignoring assignment: %s", rvalue);
926 return 0;
927 }
928
929 network->dhcp_server_address_in_addr = a.in;
930 network->dhcp_server_address_prefixlen = prefixlen;
931 return 0;
932 }
933
934 int config_parse_dhcp_server_ipv6_only_preferred(
935 const char *unit,
936 const char *filename,
937 unsigned line,
938 const char *section,
939 unsigned section_line,
940 const char *lvalue,
941 int ltype,
942 const char *rvalue,
943 void *data,
944 void *userdata) {
945
946 usec_t t, *usec = ASSERT_PTR(data);
947 int r;
948
949 assert(filename);
950 assert(section);
951 assert(lvalue);
952 assert(rvalue);
953
954 if (isempty(rvalue)) {
955 *usec = 0;
956 return 0;
957 }
958
959 r = parse_sec(rvalue, &t);
960 if (r < 0) {
961 log_syntax(unit, LOG_WARNING, filename, line, r,
962 "Failed to parse [%s] %s=, ignoring assignment: %s", section, lvalue, rvalue);
963 return 0;
964 }
965
966 if (t < MIN_V6ONLY_WAIT_USEC && !network_test_mode_enabled()) {
967 log_syntax(unit, LOG_WARNING, filename, line, 0,
968 "Invalid [%s] %s=, ignoring assignment: %s", section, lvalue, rvalue);
969 return 0;
970 }
971
972 *usec = t;
973 return 0;
974 }
975
976 static const char* const dhcp_server_persist_leases_table[_DHCP_SERVER_PERSIST_LEASES_MAX] = {
977 [DHCP_SERVER_PERSIST_LEASES_NO] = "no",
978 [DHCP_SERVER_PERSIST_LEASES_YES] = "yes",
979 [DHCP_SERVER_PERSIST_LEASES_RUNTIME] = "runtime",
980 };
981
982 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(
983 dhcp_server_persist_leases,
984 DHCPServerPersistLeases,
985 DHCP_SERVER_PERSIST_LEASES_YES);
986
987 DEFINE_CONFIG_PARSE_ENUM(
988 config_parse_dhcp_server_persist_leases,
989 dhcp_server_persist_leases,
990 DHCPServerPersistLeases);