1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "sd-dhcp-server.h"
7 #include "networkd-dhcp-server.h"
8 #include "networkd-link.h"
9 #include "networkd-manager.h"
10 #include "networkd-network.h"
11 #include "parse-util.h"
12 #include "socket-netlink.h"
13 #include "string-table.h"
14 #include "string-util.h"
17 static Address
* link_find_dhcp_server_address(Link
*link
) {
21 assert(link
->network
);
23 /* The first statically configured address if there is any */
24 LIST_FOREACH(addresses
, address
, link
->network
->static_addresses
)
25 if (address
->family
== AF_INET
&&
26 !in_addr_is_null(address
->family
, &address
->in_addr
))
29 /* If that didn't work, find a suitable address we got from the pool */
30 LIST_FOREACH(addresses
, address
, link
->pool_addresses
)
31 if (address
->family
== AF_INET
)
37 static int link_push_uplink_to_dhcp_server(
39 sd_dhcp_lease_server_type what
,
42 _cleanup_free_
struct in_addr
*addresses
= NULL
;
43 size_t n_addresses
= 0, n_allocated
= 0;
44 bool use_dhcp_lease_data
= true;
50 assert(link
->network
);
52 log_link_debug(link
, "Copying %s from link", dhcp_lease_server_type_to_string(what
));
56 case SD_DHCP_LEASE_DNS
:
57 /* For DNS we have a special case. We the data configured explicitly locally along with the
58 * data from the DHCP lease. */
60 for (unsigned i
= 0; i
< link
->network
->n_dns
; i
++) {
63 /* Only look for IPv4 addresses */
64 if (link
->network
->dns
[i
]->family
!= AF_INET
)
67 ia
= link
->network
->dns
[i
]->address
.in
;
69 /* Never propagate obviously borked data */
70 if (in4_addr_is_null(&ia
) || in4_addr_is_localhost(&ia
))
73 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
76 addresses
[n_addresses
++] = ia
;
79 use_dhcp_lease_data
= link
->network
->dhcp_use_dns
;
82 case SD_DHCP_LEASE_NTP
: {
85 /* For NTP things are similar, but for NTP hostnames can be configured too, which we cannot
86 * propagate via DHCP. Hence let's only propagate those which are IP addresses. */
88 STRV_FOREACH(i
, link
->network
->ntp
) {
89 union in_addr_union ia
;
91 if (in_addr_from_string(AF_INET
, *i
, &ia
) < 0)
94 /* Never propagate obviously borked data */
95 if (in4_addr_is_null(&ia
.in
) || in4_addr_is_localhost(&ia
.in
))
98 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
101 addresses
[n_addresses
++] = ia
.in
;
104 use_dhcp_lease_data
= link
->network
->dhcp_use_ntp
;
108 case SD_DHCP_LEASE_SIP
:
110 /* For SIP we don't allow explicit, local configuration, but there's control whether to use the data */
111 use_dhcp_lease_data
= link
->network
->dhcp_use_sip
;
114 case SD_DHCP_LEASE_POP3
:
115 case SD_DHCP_LEASE_SMTP
:
116 case SD_DHCP_LEASE_LPR
:
117 /* For the other server types we currently do not allow local configuration of server data,
118 * since there are typically no local consumers of the data. */
122 assert_not_reached("Unexpected server type");
125 if (use_dhcp_lease_data
&& link
->dhcp_lease
) {
126 const struct in_addr
*da
;
128 int n
= sd_dhcp_lease_get_servers(link
->dhcp_lease
, what
, &da
);
130 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ n
))
133 for (int j
= 0; j
< n
; j
++)
134 if (in4_addr_is_non_local(&da
[j
]))
135 addresses
[n_addresses
++] = da
[j
];
139 if (n_addresses
<= 0)
142 return sd_dhcp_server_set_servers(s
, what
, addresses
, n_addresses
);
145 static int dhcp4_server_parse_dns_server_string_and_warn(Link
*l
, const char *string
, struct in_addr
**addresses
, size_t *n_allocated
, size_t *n_addresses
) {
147 _cleanup_free_
char *word
= NULL
, *server_name
= NULL
;
148 union in_addr_union address
;
149 int family
, r
, ifindex
= 0;
151 r
= extract_first_word(&string
, &word
, NULL
, 0);
157 r
= in_addr_ifindex_name_from_string_auto(word
, &family
, &address
, &ifindex
, &server_name
);
159 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring: %m", word
);
163 /* Only look for IPv4 addresses */
164 if (family
!= AF_INET
)
167 /* Never propagate obviously borked data */
168 if (in4_addr_is_null(&address
.in
) || in4_addr_is_localhost(&address
.in
))
171 if (!GREEDY_REALLOC(*addresses
, *n_allocated
, *n_addresses
+ 1))
174 (*addresses
)[(*n_addresses
)++] = address
.in
;
180 static int dhcp4_server_set_dns_from_resolve_conf(Link
*link
) {
181 _cleanup_free_
struct in_addr
*addresses
= NULL
;
182 size_t n_addresses
= 0, n_allocated
= 0;
183 _cleanup_fclose_
FILE *f
= NULL
;
186 f
= fopen(PRIVATE_UPLINK_RESOLV_CONF
, "re");
191 return log_warning_errno(errno
, "Failed to open " PRIVATE_UPLINK_RESOLV_CONF
": %m");
195 _cleanup_free_
char *line
= NULL
;
199 r
= read_line(f
, LONG_LINE_MAX
, &line
);
201 return log_error_errno(r
, "Failed to read " PRIVATE_UPLINK_RESOLV_CONF
": %m");
208 if (IN_SET(*l
, '#', ';', 0))
211 a
= first_word(l
, "nameserver");
215 r
= dhcp4_server_parse_dns_server_string_and_warn(link
, a
, &addresses
, &n_allocated
, &n_addresses
);
217 log_warning_errno(r
, "Failed to parse DNS server address '%s', ignoring.", a
);
220 if (n_addresses
<= 0)
223 return sd_dhcp_server_set_dns(link
->dhcp_server
, addresses
, n_addresses
);
226 int dhcp4_server_configure(Link
*link
) {
227 bool acquired_uplink
= false;
233 address
= link_find_dhcp_server_address(link
);
235 return log_link_error_errno(link
, SYNTHETIC_ERRNO(EBUSY
),
236 "Failed to find suitable address for DHCPv4 server instance.");
238 /* use the server address' subnet as the pool */
239 r
= sd_dhcp_server_configure_pool(link
->dhcp_server
, &address
->in_addr
.in
, address
->prefixlen
,
240 link
->network
->dhcp_server_pool_offset
, link
->network
->dhcp_server_pool_size
);
242 return log_link_error_errno(link
, r
, "Failed to configure address pool for DHCPv4 server instance: %m");
245 r = sd_dhcp_server_set_router(link->dhcp_server, &main_address->in_addr.in);
250 if (link
->network
->dhcp_server_max_lease_time_usec
> 0) {
251 r
= sd_dhcp_server_set_max_lease_time(link
->dhcp_server
,
252 DIV_ROUND_UP(link
->network
->dhcp_server_max_lease_time_usec
, USEC_PER_SEC
));
254 return log_link_error_errno(link
, r
, "Failed to set maximum lease time for DHCPv4 server instance: %m");
257 if (link
->network
->dhcp_server_default_lease_time_usec
> 0) {
258 r
= sd_dhcp_server_set_default_lease_time(link
->dhcp_server
,
259 DIV_ROUND_UP(link
->network
->dhcp_server_default_lease_time_usec
, USEC_PER_SEC
));
261 return log_link_error_errno(link
, r
, "Failed to set default lease time for DHCPv4 server instance: %m");
264 for (sd_dhcp_lease_server_type type
= 0; type
< _SD_DHCP_LEASE_SERVER_TYPE_MAX
; type
++) {
266 if (!link
->network
->dhcp_server_emit
[type
].emit
)
269 if (link
->network
->dhcp_server_emit
[type
].n_addresses
> 0)
270 /* Explicitly specified servers to emit */
271 r
= sd_dhcp_server_set_servers(
274 link
->network
->dhcp_server_emit
[type
].addresses
,
275 link
->network
->dhcp_server_emit
[type
].n_addresses
);
277 /* Emission is requested, but nothing explicitly configured. Let's find a suitable upling */
278 if (!acquired_uplink
) {
279 uplink
= manager_find_uplink(link
->manager
, link
);
280 acquired_uplink
= true;
283 if (uplink
&& uplink
->network
)
284 r
= link_push_uplink_to_dhcp_server(uplink
, type
, link
->dhcp_server
);
285 else if (type
== SD_DHCP_LEASE_DNS
)
286 r
= dhcp4_server_set_dns_from_resolve_conf(link
);
289 "Not emitting %s on link, couldn't find suitable uplink.",
290 dhcp_lease_server_type_to_string(type
));
296 log_link_warning_errno(link
, r
,
297 "Failed to set %s for DHCP server, ignoring: %m",
298 dhcp_lease_server_type_to_string(type
));
301 r
= sd_dhcp_server_set_emit_router(link
->dhcp_server
, link
->network
->dhcp_server_emit_router
);
303 return log_link_error_errno(link
, r
, "Failed to set router emission for DHCP server: %m");
305 if (link
->network
->dhcp_server_emit_timezone
) {
306 _cleanup_free_
char *buffer
= NULL
;
309 if (link
->network
->dhcp_server_timezone
)
310 tz
= link
->network
->dhcp_server_timezone
;
312 r
= get_timezone(&buffer
);
314 return log_link_error_errno(link
, r
, "Failed to determine timezone: %m");
319 r
= sd_dhcp_server_set_timezone(link
->dhcp_server
, tz
);
321 return log_link_error_errno(link
, r
, "Failed to set timezone for DHCP server: %m");
324 ORDERED_HASHMAP_FOREACH(p
, link
->network
->dhcp_server_send_options
) {
325 r
= sd_dhcp_server_add_option(link
->dhcp_server
, p
);
329 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 option: %m");
332 ORDERED_HASHMAP_FOREACH(p
, link
->network
->dhcp_server_send_vendor_options
) {
333 r
= sd_dhcp_server_add_vendor_option(link
->dhcp_server
, p
);
337 return log_link_error_errno(link
, r
, "Failed to set DHCPv4 option: %m");
340 if (!sd_dhcp_server_is_running(link
->dhcp_server
)) {
341 r
= sd_dhcp_server_start(link
->dhcp_server
);
343 return log_link_error_errno(link
, r
, "Could not start DHCPv4 server instance: %m");
349 int config_parse_dhcp_server_emit(
351 const char *filename
,
354 unsigned section_line
,
361 NetworkDHCPServerEmitAddress
*emit
= data
;
366 for (const char *p
= rvalue
;;) {
367 _cleanup_free_
char *w
= NULL
;
368 union in_addr_union a
;
371 r
= extract_first_word(&p
, &w
, NULL
, 0);
375 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
376 "Failed to extract word, ignoring: %s", rvalue
);
382 r
= in_addr_from_string(AF_INET
, w
, &a
);
384 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
385 "Failed to parse %s= address '%s', ignoring: %m", lvalue
, w
);
389 struct in_addr
*m
= reallocarray(emit
->addresses
, emit
->n_addresses
+ 1, sizeof(struct in_addr
));
394 emit
->addresses
[emit
->n_addresses
++] = a
.in
;