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