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