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 
link_dhcp4_server_enabled(Link * link)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 
network_adjust_dhcp_server(Network * network)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 
link_request_dhcp_server_address(Link * link)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 
link_find_dhcp_server_address(Link * link,Address ** ret)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 
dhcp_server_find_uplink(Link * link,Link ** ret)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 
link_push_uplink_to_dhcp_server(Link * link,sd_dhcp_lease_server_type_t what,sd_dhcp_server * s)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                 /* 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. */
222 
223                 STRV_FOREACH(i, link->network->ntp) {
224                         union in_addr_union ia;
225 
226                         if (in_addr_from_string(AF_INET, *i, &ia) < 0)
227                                 continue;
228 
229                         /* Never propagate obviously borked data */
230                         if (in4_addr_is_null(&ia.in) || in4_addr_is_localhost(&ia.in))
231                                 continue;
232 
233                         if (!GREEDY_REALLOC(addresses, n_addresses + 1))
234                                 return log_oom();
235 
236                         addresses[n_addresses++] = ia.in;
237                 }
238 
239                 use_dhcp_lease_data = link->network->dhcp_use_ntp;
240                 break;
241         }
242 
243         case SD_DHCP_LEASE_SIP:
244 
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;
247                 break;
248 
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. */
254                 break;
255 
256         default:
257                 assert_not_reached();
258         }
259 
260         if (use_dhcp_lease_data && link->dhcp_lease) {
261                 const struct in_addr *da;
262 
263                 int n = sd_dhcp_lease_get_servers(link->dhcp_lease, what, &da);
264                 if (n > 0) {
265                         if (!GREEDY_REALLOC(addresses, n_addresses + n))
266                                 return log_oom();
267 
268                         for (int j = 0; j < n; j++)
269                                 if (in4_addr_is_non_local(&da[j]))
270                                         addresses[n_addresses++] = da[j];
271                 }
272         }
273 
274         if (n_addresses <= 0)
275                 return 0;
276 
277         return sd_dhcp_server_set_servers(s, what, addresses, n_addresses);
278 }
279 
dhcp4_server_parse_dns_server_string_and_warn(const char * string,struct in_addr ** addresses,size_t * n_addresses)280 static int dhcp4_server_parse_dns_server_string_and_warn(
281                 const char *string,
282                 struct in_addr **addresses,
283                 size_t *n_addresses) {
284 
285         for (;;) {
286                 _cleanup_free_ char *word = NULL, *server_name = NULL;
287                 union in_addr_union address;
288                 int family, r, ifindex = 0;
289 
290                 r = extract_first_word(&string, &word, NULL, 0);
291                 if (r < 0)
292                         return r;
293                 if (r == 0)
294                         break;
295 
296                 r = in_addr_ifindex_name_from_string_auto(word, &family, &address, &ifindex, &server_name);
297                 if (r < 0) {
298                         log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring: %m", word);
299                         continue;
300                 }
301 
302                 /* Only look for IPv4 addresses */
303                 if (family != AF_INET)
304                         continue;
305 
306                 /* Never propagate obviously borked data */
307                 if (in4_addr_is_null(&address.in) || in4_addr_is_localhost(&address.in))
308                         continue;
309 
310                 if (!GREEDY_REALLOC(*addresses, *n_addresses + 1))
311                         return log_oom();
312 
313                 (*addresses)[(*n_addresses)++] = address.in;
314         }
315 
316         return 0;
317 }
318 
dhcp4_server_set_dns_from_resolve_conf(Link * link)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;
323         int n = 0, r;
324 
325         f = fopen(PRIVATE_UPLINK_RESOLV_CONF, "re");
326         if (!f) {
327                 if (errno == ENOENT)
328                         return 0;
329 
330                 return log_warning_errno(errno, "Failed to open " PRIVATE_UPLINK_RESOLV_CONF ": %m");
331         }
332 
333         for (;;) {
334                 _cleanup_free_ char *line = NULL;
335                 const char *a;
336                 char *l;
337 
338                 r = read_line(f, LONG_LINE_MAX, &line);
339                 if (r < 0)
340                         return log_error_errno(r, "Failed to read " PRIVATE_UPLINK_RESOLV_CONF ": %m");
341                 if (r == 0)
342                         break;
343 
344                 n++;
345 
346                 l = strstrip(line);
347                 if (IN_SET(*l, '#', ';', 0))
348                         continue;
349 
350                 a = first_word(l, "nameserver");
351                 if (!a)
352                         continue;
353 
354                 r = dhcp4_server_parse_dns_server_string_and_warn(a, &addresses, &n_addresses);
355                 if (r < 0)
356                         log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
357         }
358 
359         if (n_addresses <= 0)
360                 return 0;
361 
362         return sd_dhcp_server_set_dns(link->dhcp_server, addresses, n_addresses);
363 }
364 
dhcp4_server_configure(Link * link)365 static int dhcp4_server_configure(Link *link) {
366         bool acquired_uplink = false;
367         sd_dhcp_option *p;
368         DHCPStaticLease *static_lease;
369         Link *uplink = NULL;
370         Address *address;
371         bool bind_to_interface;
372         int r;
373 
374         assert(link);
375 
376         log_link_debug(link, "Configuring DHCP Server.");
377 
378         if (link->dhcp_server)
379                 return -EBUSY;
380 
381         r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
382         if (r < 0)
383                 return r;
384 
385         r = sd_dhcp_server_attach_event(link->dhcp_server, link->manager->event, 0);
386         if (r < 0)
387                 return r;
388 
389         r = sd_dhcp_server_set_callback(link->dhcp_server, dhcp_server_callback, link);
390         if (r < 0)
391                 return log_link_warning_errno(link, r, "Failed to set callback for DHCPv4 server instance: %m");
392 
393         r = link_find_dhcp_server_address(link, &address);
394         if (r < 0)
395                 return log_link_error_errno(link, r, "Failed to find suitable address for DHCPv4 server instance: %m");
396 
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);
400         if (r < 0)
401                 return log_link_error_errno(link, r, "Failed to configure address pool for DHCPv4 server instance: %m");
402 
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));
406                 if (r < 0)
407                         return log_link_error_errno(link, r, "Failed to set maximum lease time for DHCPv4 server instance: %m");
408         }
409 
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));
413                 if (r < 0)
414                         return log_link_error_errno(link, r, "Failed to set default lease time for DHCPv4 server instance: %m");
415         }
416 
417         r = sd_dhcp_server_set_boot_server_address(link->dhcp_server, &link->network->dhcp_server_boot_server_address);
418         if (r < 0)
419                 return log_link_warning_errno(link, r, "Failed to set boot server address for DHCPv4 server instance: %m");
420 
421         r = sd_dhcp_server_set_boot_server_name(link->dhcp_server, link->network->dhcp_server_boot_server_name);
422         if (r < 0)
423                 return log_link_warning_errno(link, r, "Failed to set boot server name for DHCPv4 server instance: %m");
424 
425         r = sd_dhcp_server_set_boot_filename(link->dhcp_server, link->network->dhcp_server_boot_filename);
426         if (r < 0)
427                 return log_link_warning_errno(link, r, "Failed to set boot filename for DHCPv4 server instance: %m");
428 
429         for (sd_dhcp_lease_server_type_t type = 0; type < _SD_DHCP_LEASE_SERVER_TYPE_MAX; type ++) {
430 
431                 if (!link->network->dhcp_server_emit[type].emit)
432                         continue;
433 
434                 if (link->network->dhcp_server_emit[type].n_addresses > 0)
435                         /* Explicitly specified servers to emit */
436                         r = sd_dhcp_server_set_servers(
437                                         link->dhcp_server,
438                                         type,
439                                         link->network->dhcp_server_emit[type].addresses,
440                                         link->network->dhcp_server_emit[type].n_addresses);
441                 else {
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;
446                         }
447 
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);
452                         else {
453                                 log_link_debug(link,
454                                                "Not emitting %s on link, couldn't find suitable uplink.",
455                                                dhcp_lease_server_type_to_string(type));
456                                 continue;
457                         }
458                 }
459 
460                 if (r < 0)
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));
464         }
465 
466         if (link->network->dhcp_server_emit_router) {
467                 r = sd_dhcp_server_set_router(link->dhcp_server, &link->network->dhcp_server_router);
468                 if (r < 0)
469                         return log_link_error_errno(link, r, "Failed to set router address for DHCP server: %m");
470         }
471 
472         r = sd_dhcp_server_set_relay_target(link->dhcp_server, &link->network->dhcp_server_relay_target);
473         if (r < 0)
474                 return log_link_error_errno(link, r, "Failed to set relay target for DHCP server: %m");
475 
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);
478         if (r < 0)
479                 return log_link_error_errno(link, r, "Failed to set interface binding for DHCP server: %m");
480 
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);
482         if (r < 0)
483                 return log_link_error_errno(link, r, "Failed to set agent circuit/remote id for DHCP server: %m");
484 
485         if (link->network->dhcp_server_emit_timezone) {
486                 _cleanup_free_ char *buffer = NULL;
487                 const char *tz = NULL;
488 
489                 if (link->network->dhcp_server_timezone)
490                         tz = link->network->dhcp_server_timezone;
491                 else {
492                         r = get_timezone(&buffer);
493                         if (r < 0)
494                                 log_link_warning_errno(link, r, "Failed to determine timezone, not sending timezone: %m");
495                         else
496                                 tz = buffer;
497                 }
498 
499                 if (tz) {
500                         r = sd_dhcp_server_set_timezone(link->dhcp_server, tz);
501                         if (r < 0)
502                                 return log_link_error_errno(link, r, "Failed to set timezone for DHCP server: %m");
503                 }
504         }
505 
506         ORDERED_HASHMAP_FOREACH(p, link->network->dhcp_server_send_options) {
507                 r = sd_dhcp_server_add_option(link->dhcp_server, p);
508                 if (r == -EEXIST)
509                         continue;
510                 if (r < 0)
511                         return log_link_error_errno(link, r, "Failed to set DHCPv4 option: %m");
512         }
513 
514         ORDERED_HASHMAP_FOREACH(p, link->network->dhcp_server_send_vendor_options) {
515                 r = sd_dhcp_server_add_vendor_option(link->dhcp_server, p);
516                 if (r == -EEXIST)
517                         continue;
518                 if (r < 0)
519                         return log_link_error_errno(link, r, "Failed to set DHCPv4 option: %m");
520         }
521 
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);
524                 if (r < 0)
525                         return log_link_error_errno(link, r, "Failed to set DHCPv4 static lease for DHCP server: %m");
526         }
527 
528         r = sd_dhcp_server_start(link->dhcp_server);
529         if (r < 0)
530                 return log_link_error_errno(link, r, "Could not start DHCPv4 server instance: %m");
531 
532         log_link_debug(link, "Offering DHCPv4 leases");
533         return 0;
534 }
535 
dhcp_server_is_ready_to_configure(Link * link)536 static bool dhcp_server_is_ready_to_configure(Link *link) {
537         Link *uplink = NULL;
538         Address *a;
539 
540         assert(link);
541 
542         if (!link->network)
543                 return false;
544 
545         if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
546                 return false;
547 
548         if (link->set_flags_messages > 0)
549                 return false;
550 
551         if (!link_has_carrier(link))
552                 return false;
553 
554         if (!link->static_addresses_configured)
555                 return false;
556 
557         if (link_find_dhcp_server_address(link, &a) < 0)
558                 return false;
559 
560         if (!address_is_ready(a))
561                 return false;
562 
563         if (dhcp_server_find_uplink(link, &uplink) < 0)
564                 return false;
565 
566         if (uplink && !uplink->network)
567                 return false;
568 
569         return true;
570 }
571 
dhcp_server_process_request(Request * req,Link * link,void * userdata)572 static int dhcp_server_process_request(Request *req, Link *link, void *userdata) {
573         int r;
574 
575         assert(link);
576 
577         if (!dhcp_server_is_ready_to_configure(link))
578                 return 0;
579 
580         r = dhcp4_server_configure(link);
581         if (r < 0)
582                 return log_link_warning_errno(link, r, "Failed to configure DHCP server: %m");
583 
584         return 1;
585 }
586 
link_request_dhcp_server(Link * link)587 int link_request_dhcp_server(Link *link) {
588         int r;
589 
590         assert(link);
591 
592         if (!link_dhcp4_server_enabled(link))
593                 return 0;
594 
595         if (link->dhcp_server)
596                 return 0;
597 
598         log_link_debug(link, "Requesting DHCP server.");
599         r = link_queue_request(link, REQUEST_TYPE_DHCP_SERVER, dhcp_server_process_request, NULL);
600         if (r < 0)
601                 return log_link_warning_errno(link, r, "Failed to request configuration of DHCP server: %m");
602 
603         return 0;
604 }
605 
config_parse_dhcp_server_relay_agent_suboption(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)606 int config_parse_dhcp_server_relay_agent_suboption(
607                 const char *unit,
608                 const char *filename,
609                 unsigned line,
610                 const char *section,
611                 unsigned section_line,
612                 const char *lvalue,
613                 int ltype,
614                 const char *rvalue,
615                 void *data,
616                 void *userdata) {
617 
618         char **suboption_value = data;
619         char* p;
620 
621         assert(filename);
622         assert(lvalue);
623         assert(rvalue);
624 
625         if (isempty(rvalue)) {
626                 *suboption_value = mfree(*suboption_value);
627                 return 0;
628         }
629 
630         p = startswith(rvalue, "string:");
631         if (!p) {
632                 log_syntax(unit, LOG_WARNING, filename, line, 0,
633                            "Failed to parse %s=%s'. Invalid format, ignoring.", lvalue, rvalue);
634                 return 0;
635         }
636         return free_and_strdup(suboption_value, empty_to_null(p));
637 }
638 
config_parse_dhcp_server_emit(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)639 int config_parse_dhcp_server_emit(
640                 const char *unit,
641                 const char *filename,
642                 unsigned line,
643                 const char *section,
644                 unsigned section_line,
645                 const char *lvalue,
646                 int ltype,
647                 const char *rvalue,
648                 void *data,
649                 void *userdata) {
650 
651         NetworkDHCPServerEmitAddress *emit = data;
652 
653         assert(emit);
654         assert(rvalue);
655 
656         if (isempty(rvalue)) {
657                 emit->addresses = mfree(emit->addresses);
658                 emit->n_addresses = 0;
659                 return 0;
660         }
661 
662         for (const char *p = rvalue;;) {
663                 _cleanup_free_ char *w = NULL;
664                 union in_addr_union a;
665                 int r;
666 
667                 r = extract_first_word(&p, &w, NULL, 0);
668                 if (r == -ENOMEM)
669                         return log_oom();
670                 if (r < 0) {
671                         log_syntax(unit, LOG_WARNING, filename, line, r,
672                                    "Failed to extract word, ignoring: %s", rvalue);
673                         return 0;
674                 }
675                 if (r == 0)
676                         return 0;
677 
678                 if (streq(w, "_server_address"))
679                         a = IN_ADDR_NULL; /* null address will be converted to the server address. */
680                 else {
681                         r = in_addr_from_string(AF_INET, w, &a);
682                         if (r < 0) {
683                                 log_syntax(unit, LOG_WARNING, filename, line, r,
684                                            "Failed to parse %s= address '%s', ignoring: %m", lvalue, w);
685                                 continue;
686                         }
687 
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);
691                                 continue;
692                         }
693                 }
694 
695                 if (!GREEDY_REALLOC(emit->addresses, emit->n_addresses + 1))
696                         return log_oom();
697 
698                 emit->addresses[emit->n_addresses++] = a.in;
699         }
700 }
701 
config_parse_dhcp_server_address(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)702 int config_parse_dhcp_server_address(
703                 const char *unit,
704                 const char *filename,
705                 unsigned line,
706                 const char *section,
707                 unsigned section_line,
708                 const char *lvalue,
709                 int ltype,
710                 const char *rvalue,
711                 void *data,
712                 void *userdata) {
713 
714         Network *network = ASSERT_PTR(userdata);
715         union in_addr_union a;
716         unsigned char prefixlen;
717         int r;
718 
719         assert(filename);
720         assert(lvalue);
721         assert(rvalue);
722 
723         if (isempty(rvalue)) {
724                 network->dhcp_server_address = (struct in_addr) {};
725                 network->dhcp_server_address_prefixlen = 0;
726                 return 0;
727         }
728 
729         r = in_addr_prefix_from_string(rvalue, AF_INET, &a, &prefixlen);
730         if (r < 0) {
731                 log_syntax(unit, LOG_WARNING, filename, line, r,
732                            "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
733                 return 0;
734         }
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);
739                 return 0;
740         }
741 
742         network->dhcp_server_address = a.in;
743         network->dhcp_server_address_prefixlen = prefixlen;
744         return 0;
745 }
746