1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/in.h>
4 #include <linux/if_arp.h>
7 #include "sd-dhcp-server.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"
26 static bool link_dhcp4_server_enabled(Link
*link
) {
29 if (link
->flags
& IFF_LOOPBACK
)
35 if (link
->iftype
== ARPHRD_CAN
)
38 return link
->network
->dhcp_server
;
41 void network_adjust_dhcp_server(Network
*network
) {
44 if (!network
->dhcp_server
)
48 log_warning("%s: DHCPServer= is enabled for bond slave. Disabling DHCP server.",
50 network
->dhcp_server
= false;
54 if (!in4_addr_is_set(&network
->dhcp_server_address
)) {
58 ORDERED_HASHMAP_FOREACH(address
, network
->addresses_by_section
) {
59 if (section_is_invalid(address
->section
))
62 if (address
->family
!= AF_INET
)
65 if (in4_addr_is_localhost(&address
->in_addr
.in
))
68 if (in4_addr_is_link_local(&address
->in_addr
.in
))
71 if (in4_addr_is_set(&address
->in_addr_peer
.in
))
78 log_warning("%s: DHCPServer= is enabled, but no static address configured. "
79 "Disabling DHCP server.",
81 network
->dhcp_server
= false;
87 int link_request_dhcp_server_address(Link
*link
) {
88 _cleanup_(address_freep
) Address
*address
= NULL
;
93 assert(link
->network
);
95 if (!link_dhcp4_server_enabled(link
))
98 if (!in4_addr_is_set(&link
->network
->dhcp_server_address
))
101 r
= address_new(&address
);
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
);
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. */
118 return link_request_static_address(link
, TAKE_PTR(address
), true);
121 static int link_find_dhcp_server_address(Link
*link
, Address
**ret
) {
125 assert(link
->network
);
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
);
132 /* If not, then select one from static addresses. */
133 SET_FOREACH(address
, link
->addresses
) {
134 if (address
->source
!= NETWORK_CONFIG_SOURCE_STATIC
)
136 if (!address_exists(address
))
138 if (address
->family
!= AF_INET
)
140 if (in4_addr_is_localhost(&address
->in_addr
.in
))
142 if (in4_addr_is_link_local(&address
->in_addr
.in
))
144 if (in4_addr_is_set(&address
->in_addr_peer
.in
))
154 static int dhcp_server_find_uplink(Link
*link
, Link
**ret
) {
157 if (link
->network
->dhcp_server_uplink_name
)
158 return link_get_by_name(link
->manager
, link
->network
->dhcp_server_uplink_name
, ret
);
160 if (link
->network
->dhcp_server_uplink_index
> 0)
161 return link_get_by_index(link
->manager
, link
->network
->dhcp_server_uplink_index
, ret
);
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)
174 static int link_push_uplink_to_dhcp_server(
176 sd_dhcp_lease_server_type_t what
,
179 _cleanup_free_
struct in_addr
*addresses
= NULL
;
180 bool use_dhcp_lease_data
= true;
181 size_t n_addresses
= 0;
187 assert(link
->network
);
189 log_link_debug(link
, "Copying %s from link", dhcp_lease_server_type_to_string(what
));
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. */
197 for (unsigned i
= 0; i
< link
->network
->n_dns
; i
++) {
200 /* Only look for IPv4 addresses */
201 if (link
->network
->dns
[i
]->family
!= AF_INET
)
204 ia
= link
->network
->dns
[i
]->address
.in
;
206 /* Never propagate obviously borked data */
207 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
210 if (!GREEDY_REALLOC(addresses
, n_addresses
+ 1))
213 addresses
[n_addresses
++] = ia
;
216 use_dhcp_lease_data
= link
->network
->dhcp_use_dns
;
219 case SD_DHCP_LEASE_NTP
: {
220 /* For NTP things are similar, but for NTP hostnames can be configured too, which we cannot
221 * propagate via DHCP. Hence let's only propagate those which are IP addresses. */
223 STRV_FOREACH(i
, link
->network
->ntp
) {
224 union in_addr_union ia
;
226 if (in_addr_from_string(AF_INET
, *i
, &ia
) < 0)
229 /* Never propagate obviously borked data */
230 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
233 if (!GREEDY_REALLOC(addresses
, n_addresses
+ 1))
236 addresses
[n_addresses
++] = ia
.in
;
239 use_dhcp_lease_data
= link
->network
->dhcp_use_ntp
;
243 case SD_DHCP_LEASE_SIP
:
245 /* For SIP we don't allow explicit, local configuration, but there's control whether to use the data */
246 use_dhcp_lease_data
= link
->network
->dhcp_use_sip
;
249 case SD_DHCP_LEASE_POP3
:
250 case SD_DHCP_LEASE_SMTP
:
251 case SD_DHCP_LEASE_LPR
:
252 /* For the other server types we currently do not allow local configuration of server data,
253 * since there are typically no local consumers of the data. */
257 assert_not_reached();
260 if (use_dhcp_lease_data
&& link
->dhcp_lease
) {
261 const struct in_addr
*da
;
263 int n
= sd_dhcp_lease_get_servers(link
->dhcp_lease
, what
, &da
);
265 if (!GREEDY_REALLOC(addresses
, n_addresses
+ n
))
268 for (int j
= 0; j
< n
; j
++)
269 if (in4_addr_is_non_local(&da
[j
]))
270 addresses
[n_addresses
++] = da
[j
];
274 if (n_addresses
<= 0)
277 return sd_dhcp_server_set_servers(s
, what
, addresses
, n_addresses
);
280 static int dhcp4_server_parse_dns_server_string_and_warn(
282 struct in_addr
**addresses
,
283 size_t *n_addresses
) {
286 _cleanup_free_
char *word
= NULL
, *server_name
= NULL
;
287 union in_addr_union address
;
288 int family
, r
, ifindex
= 0;
290 r
= extract_first_word(&string
, &word
, NULL
, 0);
296 r
= in_addr_ifindex_name_from_string_auto(word
, &family
, &address
, &ifindex
, &server_name
);
298 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring: %m", word
);
302 /* Only look for IPv4 addresses */
303 if (family
!= AF_INET
)
306 /* Never propagate obviously borked data */
307 if (in4_addr_is_null(&address
.in
) || in4_addr_is_localhost(&address
.in
))
310 if (!GREEDY_REALLOC(*addresses
, *n_addresses
+ 1))
313 (*addresses
)[(*n_addresses
)++] = address
.in
;
319 static int dhcp4_server_set_dns_from_resolve_conf(Link
*link
) {
320 _cleanup_free_
struct in_addr
*addresses
= NULL
;
321 _cleanup_fclose_
FILE *f
= NULL
;
322 size_t n_addresses
= 0;
325 f
= fopen(PRIVATE_UPLINK_RESOLV_CONF
, "re");
330 return log_warning_errno(errno
, "Failed to open " PRIVATE_UPLINK_RESOLV_CONF
": %m");
334 _cleanup_free_
char *line
= NULL
;
338 r
= read_line(f
, LONG_LINE_MAX
, &line
);
340 return log_error_errno(r
, "Failed to read " PRIVATE_UPLINK_RESOLV_CONF
": %m");
347 if (IN_SET(*l
, '#', ';', 0))
350 a
= first_word(l
, "nameserver");
354 r
= dhcp4_server_parse_dns_server_string_and_warn(a
, &addresses
, &n_addresses
);
356 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
359 if (n_addresses
<= 0)
362 return sd_dhcp_server_set_dns(link
->dhcp_server
, addresses
, n_addresses
);
365 static int dhcp4_server_configure(Link
*link
) {
366 bool acquired_uplink
= false;
368 DHCPStaticLease
*static_lease
;
371 bool bind_to_interface
;
376 log_link_debug(link
, "Configuring DHCP Server.");
378 if (link
->dhcp_server
)
381 r
= sd_dhcp_server_new(&link
->dhcp_server
, link
->ifindex
);
385 r
= sd_dhcp_server_attach_event(link
->dhcp_server
, link
->manager
->event
, 0);
389 r
= sd_dhcp_server_set_callback(link
->dhcp_server
, dhcp_server_callback
, link
);
391 return log_link_warning_errno(link
, r
, "Failed to set callback for DHCPv4 server instance: %m");
393 r
= link_find_dhcp_server_address(link
, &address
);
395 return log_link_error_errno(link
, r
, "Failed to find suitable address for DHCPv4 server instance: %m");
397 /* use the server address' subnet as the pool */
398 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
399 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
401 return log_link_error_errno(link
, r
, "Failed to configure address pool for DHCPv4 server instance: %m");
403 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
404 r
= sd_dhcp_server_set_max_lease_time(link
->dhcp_server
,
405 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
407 return log_link_error_errno(link
, r
, "Failed to set maximum lease time for DHCPv4 server instance: %m");
410 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
411 r
= sd_dhcp_server_set_default_lease_time(link
->dhcp_server
,
412 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
414 return log_link_error_errno(link
, r
, "Failed to set default lease time for DHCPv4 server instance: %m");
417 r
= sd_dhcp_server_set_boot_server_address(link
->dhcp_server
, &link
->network
->dhcp_server_boot_server_address
);
419 return log_link_warning_errno(link
, r
, "Failed to set boot server address for DHCPv4 server instance: %m");
421 r
= sd_dhcp_server_set_boot_server_name(link
->dhcp_server
, link
->network
->dhcp_server_boot_server_name
);
423 return log_link_warning_errno(link
, r
, "Failed to set boot server name for DHCPv4 server instance: %m");
425 r
= sd_dhcp_server_set_boot_filename(link
->dhcp_server
, link
->network
->dhcp_server_boot_filename
);
427 return log_link_warning_errno(link
, r
, "Failed to set boot filename for DHCPv4 server instance: %m");
429 for (sd_dhcp_lease_server_type_t type
= 0; type
< _SD_DHCP_LEASE_SERVER_TYPE_MAX
; type
++) {
431 if (!link
->network
->dhcp_server_emit
[type
].emit
)
434 if (link
->network
->dhcp_server_emit
[type
].n_addresses
> 0)
435 /* Explicitly specified servers to emit */
436 r
= sd_dhcp_server_set_servers(
439 link
->network
->dhcp_server_emit
[type
].addresses
,
440 link
->network
->dhcp_server_emit
[type
].n_addresses
);
442 /* Emission is requested, but nothing explicitly configured. Let's find a suitable upling */
443 if (!acquired_uplink
) {
444 (void) dhcp_server_find_uplink(link
, &uplink
);
445 acquired_uplink
= true;
448 if (uplink
&& uplink
->network
)
449 r
= link_push_uplink_to_dhcp_server(uplink
, type
, link
->dhcp_server
);
450 else if (type
== SD_DHCP_LEASE_DNS
)
451 r
= dhcp4_server_set_dns_from_resolve_conf(link
);
454 "Not emitting %s on link, couldn't find suitable uplink.",
455 dhcp_lease_server_type_to_string(type
));
461 log_link_warning_errno(link
, r
,
462 "Failed to set %s for DHCP server, ignoring: %m",
463 dhcp_lease_server_type_to_string(type
));
466 if (link
->network
->dhcp_server_emit_router
) {
467 r
= sd_dhcp_server_set_router(link
->dhcp_server
, &link
->network
->dhcp_server_router
);
469 return log_link_error_errno(link
, r
, "Failed to set router address for DHCP server: %m");
472 r
= sd_dhcp_server_set_relay_target(link
->dhcp_server
, &link
->network
->dhcp_server_relay_target
);
474 return log_link_error_errno(link
, r
, "Failed to set relay target for DHCP server: %m");
476 bind_to_interface
= sd_dhcp_server_is_in_relay_mode(link
->dhcp_server
) ? false : link
->network
->dhcp_server_bind_to_interface
;
477 r
= sd_dhcp_server_set_bind_to_interface(link
->dhcp_server
, bind_to_interface
);
479 return log_link_error_errno(link
, r
, "Failed to set interface binding for DHCP server: %m");
481 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
);
483 return log_link_error_errno(link
, r
, "Failed to set agent circuit/remote id for DHCP server: %m");
485 if (link
->network
->dhcp_server_emit_timezone
) {
486 _cleanup_free_
char *buffer
= NULL
;
487 const char *tz
= NULL
;
489 if (link
->network
->dhcp_server_timezone
)
490 tz
= link
->network
->dhcp_server_timezone
;
492 r
= get_timezone(&buffer
);
494 log_link_warning_errno(link
, r
, "Failed to determine timezone, not sending timezone: %m");
500 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
502 return log_link_error_errno(link
, r
, "Failed to set timezone for DHCP server: %m");
506 ORDERED_HASHMAP_FOREACH(p
, link
->network
->dhcp_server_send_options
) {
507 r
= sd_dhcp_server_add_option(link
->dhcp_server
, p
);
511 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 option: %m");
514 ORDERED_HASHMAP_FOREACH(p
, link
->network
->dhcp_server_send_vendor_options
) {
515 r
= sd_dhcp_server_add_vendor_option(link
->dhcp_server
, p
);
519 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 option: %m");
522 HASHMAP_FOREACH(static_lease
, link
->network
->dhcp_static_leases_by_section
) {
523 r
= sd_dhcp_server_set_static_lease(link
->dhcp_server
, &static_lease
->address
, static_lease
->client_id
, static_lease
->client_id_size
);
525 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 static lease for DHCP server: %m");
528 r
= sd_dhcp_server_start(link
->dhcp_server
);
530 return log_link_error_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
532 log_link_debug(link
, "Offering DHCPv4 leases");
536 static bool dhcp_server_is_ready_to_configure(Link
*link
) {
545 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
548 if (link
->set_flags_messages
> 0)
551 if (!link_has_carrier(link
))
554 if (!link
->static_addresses_configured
)
557 if (link_find_dhcp_server_address(link
, &a
) < 0)
560 if (!address_is_ready(a
))
563 if (dhcp_server_find_uplink(link
, &uplink
) < 0)
566 if (uplink
&& !uplink
->network
)
572 static int dhcp_server_process_request(Request
*req
, Link
*link
, void *userdata
) {
577 if (!dhcp_server_is_ready_to_configure(link
))
580 r
= dhcp4_server_configure(link
);
582 return log_link_warning_errno(link
, r
, "Failed to configure DHCP server: %m");
587 int link_request_dhcp_server(Link
*link
) {
592 if (!link_dhcp4_server_enabled(link
))
595 if (link
->dhcp_server
)
598 log_link_debug(link
, "Requesting DHCP server.");
599 r
= link_queue_request(link
, REQUEST_TYPE_DHCP_SERVER
, dhcp_server_process_request
, NULL
);
601 return log_link_warning_errno(link
, r
, "Failed to request configuration of DHCP server: %m");
606 int config_parse_dhcp_server_relay_agent_suboption(
608 const char *filename
,
611 unsigned section_line
,
618 char **suboption_value
= data
;
625 if (isempty(rvalue
)) {
626 *suboption_value
= mfree(*suboption_value
);
630 p
= startswith(rvalue
, "string:");
632 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
633 "Failed to parse %s=%s'. Invalid format, ignoring.", lvalue
, rvalue
);
636 return free_and_strdup(suboption_value
, empty_to_null(p
));
639 int config_parse_dhcp_server_emit(
641 const char *filename
,
644 unsigned section_line
,
651 NetworkDHCPServerEmitAddress
*emit
= data
;
656 if (isempty(rvalue
)) {
657 emit
->addresses
= mfree(emit
->addresses
);
658 emit
->n_addresses
= 0;
662 for (const char *p
= rvalue
;;) {
663 _cleanup_free_
char *w
= NULL
;
664 union in_addr_union a
;
667 r
= extract_first_word(&p
, &w
, NULL
, 0);
671 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
672 "Failed to extract word, ignoring: %s", rvalue
);
678 if (streq(w
, "_server_address"))
679 a
= IN_ADDR_NULL
; /* null address will be converted to the server address. */
681 r
= in_addr_from_string(AF_INET
, w
, &a
);
683 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
684 "Failed to parse %s= address '%s', ignoring: %m", lvalue
, w
);
688 if (in4_addr_is_null(&a
.in
)) {
689 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
690 "Found a null address in %s=, ignoring.", lvalue
);
695 if (!GREEDY_REALLOC(emit
->addresses
, emit
->n_addresses
+ 1))
698 emit
->addresses
[emit
->n_addresses
++] = a
.in
;
702 int config_parse_dhcp_server_address(
704 const char *filename
,
707 unsigned section_line
,
714 Network
*network
= ASSERT_PTR(userdata
);
715 union in_addr_union a
;
716 unsigned char prefixlen
;
723 if (isempty(rvalue
)) {
724 network
->dhcp_server_address
= (struct in_addr
) {};
725 network
->dhcp_server_address_prefixlen
= 0;
729 r
= in_addr_prefix_from_string(rvalue
, AF_INET
, &a
, &prefixlen
);
731 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
732 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
735 if (in4_addr_is_null(&a
.in
) || in4_addr_is_localhost(&a
.in
)) {
736 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
737 "DHCP server address cannot be the ANY address or a localhost address, "
738 "ignoring assignment: %s", rvalue
);
742 network
->dhcp_server_address
= a
.in
;
743 network
->dhcp_server_address_prefixlen
= prefixlen
;