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