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