]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-network.c
Merge pull request #4395 from s-urbaniak/rw-support
[thirdparty/systemd.git] / src / network / networkd-network.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <ctype.h>
21 #include <net/if.h>
22
23 #include "alloc-util.h"
24 #include "conf-files.h"
25 #include "conf-parser.h"
26 #include "dns-domain.h"
27 #include "fd-util.h"
28 #include "hostname-util.h"
29 #include "network-internal.h"
30 #include "networkd-manager.h"
31 #include "networkd-network.h"
32 #include "parse-util.h"
33 #include "set.h"
34 #include "stat-util.h"
35 #include "string-table.h"
36 #include "string-util.h"
37 #include "util.h"
38
39 static int network_load_one(Manager *manager, const char *filename) {
40 _cleanup_network_free_ Network *network = NULL;
41 _cleanup_fclose_ FILE *file = NULL;
42 char *d;
43 const char *dropin_dirname;
44 Route *route;
45 Address *address;
46 int r;
47
48 assert(manager);
49 assert(filename);
50
51 file = fopen(filename, "re");
52 if (!file) {
53 if (errno == ENOENT)
54 return 0;
55
56 return -errno;
57 }
58
59 if (null_or_empty_fd(fileno(file))) {
60 log_debug("Skipping empty file: %s", filename);
61 return 0;
62 }
63
64 network = new0(Network, 1);
65 if (!network)
66 return log_oom();
67
68 network->manager = manager;
69
70 LIST_HEAD_INIT(network->static_addresses);
71 LIST_HEAD_INIT(network->static_routes);
72 LIST_HEAD_INIT(network->static_fdb_entries);
73
74 network->stacked_netdevs = hashmap_new(&string_hash_ops);
75 if (!network->stacked_netdevs)
76 return log_oom();
77
78 network->addresses_by_section = hashmap_new(NULL);
79 if (!network->addresses_by_section)
80 return log_oom();
81
82 network->routes_by_section = hashmap_new(NULL);
83 if (!network->routes_by_section)
84 return log_oom();
85
86 network->fdb_entries_by_section = hashmap_new(NULL);
87 if (!network->fdb_entries_by_section)
88 return log_oom();
89
90 network->filename = strdup(filename);
91 if (!network->filename)
92 return log_oom();
93
94 network->name = strdup(basename(filename));
95 if (!network->name)
96 return log_oom();
97
98 d = strrchr(network->name, '.');
99 if (!d)
100 return -EINVAL;
101
102 assert(streq(d, ".network"));
103
104 *d = '\0';
105
106 network->dhcp = ADDRESS_FAMILY_NO;
107 network->dhcp_use_ntp = true;
108 network->dhcp_use_dns = true;
109 network->dhcp_use_hostname = true;
110 network->dhcp_use_routes = true;
111 network->dhcp_send_hostname = true;
112 network->dhcp_route_metric = DHCP_ROUTE_METRIC;
113 network->dhcp_client_identifier = DHCP_CLIENT_ID_DUID;
114 network->dhcp_route_table = RT_TABLE_MAIN;
115
116 network->dhcp_server_emit_dns = true;
117 network->dhcp_server_emit_ntp = true;
118 network->dhcp_server_emit_router = true;
119 network->dhcp_server_emit_timezone = true;
120
121 network->use_bpdu = true;
122 network->allow_port_to_be_root = true;
123 network->unicast_flood = true;
124
125 network->lldp_mode = LLDP_MODE_ROUTERS_ONLY;
126
127 network->llmnr = RESOLVE_SUPPORT_YES;
128 network->mdns = RESOLVE_SUPPORT_NO;
129 network->dnssec_mode = _DNSSEC_MODE_INVALID;
130
131 network->link_local = ADDRESS_FAMILY_IPV6;
132
133 network->ipv6_privacy_extensions = IPV6_PRIVACY_EXTENSIONS_NO;
134 network->ipv6_accept_ra = -1;
135 network->ipv6_dad_transmits = -1;
136 network->ipv6_hop_limit = -1;
137 network->duid.type = _DUID_TYPE_INVALID;
138 network->proxy_arp = -1;
139 network->arp = -1;
140 network->ipv6_accept_ra_use_dns = true;
141 network->ipv6_accept_ra_route_table = RT_TABLE_MAIN;
142
143 dropin_dirname = strjoina(network->name, ".network.d");
144
145 r = config_parse_many(filename, network_dirs, dropin_dirname,
146 "Match\0"
147 "Link\0"
148 "Network\0"
149 "Address\0"
150 "Route\0"
151 "DHCP\0"
152 "DHCPv4\0" /* compat */
153 "DHCPServer\0"
154 "IPv6AcceptRA\0"
155 "Bridge\0"
156 "BridgeFDB\0"
157 "BridgeVLAN\0",
158 config_item_perf_lookup, network_network_gperf_lookup,
159 false, network);
160 if (r < 0)
161 return r;
162
163 /* IPMasquerade=yes implies IPForward=yes */
164 if (network->ip_masquerade)
165 network->ip_forward |= ADDRESS_FAMILY_IPV4;
166
167 LIST_PREPEND(networks, manager->networks, network);
168
169 r = hashmap_ensure_allocated(&manager->networks_by_name, &string_hash_ops);
170 if (r < 0)
171 return r;
172
173 r = hashmap_put(manager->networks_by_name, network->name, network);
174 if (r < 0)
175 return r;
176
177 LIST_FOREACH(routes, route, network->static_routes) {
178 if (!route->family) {
179 log_warning("Route section without Gateway field configured in %s. "
180 "Ignoring", filename);
181 return 0;
182 }
183 }
184
185 LIST_FOREACH(addresses, address, network->static_addresses) {
186 if (!address->family) {
187 log_warning("Address section without Address field configured in %s. "
188 "Ignoring", filename);
189 return 0;
190 }
191 }
192
193 network = NULL;
194
195 return 0;
196 }
197
198 int network_load(Manager *manager) {
199 Network *network;
200 _cleanup_strv_free_ char **files = NULL;
201 char **f;
202 int r;
203
204 assert(manager);
205
206 while ((network = manager->networks))
207 network_free(network);
208
209 r = conf_files_list_strv(&files, ".network", NULL, network_dirs);
210 if (r < 0)
211 return log_error_errno(r, "Failed to enumerate network files: %m");
212
213 STRV_FOREACH_BACKWARDS(f, files) {
214 r = network_load_one(manager, *f);
215 if (r < 0)
216 return r;
217 }
218
219 return 0;
220 }
221
222 void network_free(Network *network) {
223 NetDev *netdev;
224 Route *route;
225 Address *address;
226 FdbEntry *fdb_entry;
227 Iterator i;
228
229 if (!network)
230 return;
231
232 free(network->filename);
233
234 free(network->match_mac);
235 strv_free(network->match_path);
236 strv_free(network->match_driver);
237 strv_free(network->match_type);
238 strv_free(network->match_name);
239
240 free(network->description);
241 free(network->dhcp_vendor_class_identifier);
242 free(network->dhcp_hostname);
243
244 free(network->mac);
245
246 strv_free(network->ntp);
247 strv_free(network->dns);
248 strv_free(network->search_domains);
249 strv_free(network->route_domains);
250 strv_free(network->bind_carrier);
251
252 netdev_unref(network->bridge);
253 netdev_unref(network->bond);
254 netdev_unref(network->vrf);
255
256 HASHMAP_FOREACH(netdev, network->stacked_netdevs, i) {
257 hashmap_remove(network->stacked_netdevs, netdev->ifname);
258 netdev_unref(netdev);
259 }
260 hashmap_free(network->stacked_netdevs);
261
262 while ((route = network->static_routes))
263 route_free(route);
264
265 while ((address = network->static_addresses))
266 address_free(address);
267
268 while ((fdb_entry = network->static_fdb_entries))
269 fdb_entry_free(fdb_entry);
270
271 hashmap_free(network->addresses_by_section);
272 hashmap_free(network->routes_by_section);
273 hashmap_free(network->fdb_entries_by_section);
274
275 if (network->manager) {
276 if (network->manager->networks)
277 LIST_REMOVE(networks, network->manager->networks, network);
278
279 if (network->manager->networks_by_name)
280 hashmap_remove(network->manager->networks_by_name, network->name);
281 }
282
283 free(network->name);
284
285 condition_free_list(network->match_host);
286 condition_free_list(network->match_virt);
287 condition_free_list(network->match_kernel);
288 condition_free_list(network->match_arch);
289
290 free(network->dhcp_server_timezone);
291 free(network->dhcp_server_dns);
292 free(network->dhcp_server_ntp);
293
294 set_free_free(network->dnssec_negative_trust_anchors);
295
296 free(network);
297 }
298
299 int network_get_by_name(Manager *manager, const char *name, Network **ret) {
300 Network *network;
301
302 assert(manager);
303 assert(name);
304 assert(ret);
305
306 network = hashmap_get(manager->networks_by_name, name);
307 if (!network)
308 return -ENOENT;
309
310 *ret = network;
311
312 return 0;
313 }
314
315 int network_get(Manager *manager, struct udev_device *device,
316 const char *ifname, const struct ether_addr *address,
317 Network **ret) {
318 Network *network;
319 struct udev_device *parent;
320 const char *path = NULL, *parent_driver = NULL, *driver = NULL, *devtype = NULL;
321
322 assert(manager);
323 assert(ret);
324
325 if (device) {
326 path = udev_device_get_property_value(device, "ID_PATH");
327
328 parent = udev_device_get_parent(device);
329 if (parent)
330 parent_driver = udev_device_get_driver(parent);
331
332 driver = udev_device_get_property_value(device, "ID_NET_DRIVER");
333
334 devtype = udev_device_get_devtype(device);
335 }
336
337 LIST_FOREACH(networks, network, manager->networks) {
338 if (net_match_config(network->match_mac, network->match_path,
339 network->match_driver, network->match_type,
340 network->match_name, network->match_host,
341 network->match_virt, network->match_kernel,
342 network->match_arch,
343 address, path, parent_driver, driver,
344 devtype, ifname)) {
345 if (network->match_name && device) {
346 const char *attr;
347 uint8_t name_assign_type = NET_NAME_UNKNOWN;
348
349 attr = udev_device_get_sysattr_value(device, "name_assign_type");
350 if (attr)
351 (void) safe_atou8(attr, &name_assign_type);
352
353 if (name_assign_type == NET_NAME_ENUM)
354 log_warning("%s: found matching network '%s', based on potentially unpredictable ifname",
355 ifname, network->filename);
356 else
357 log_debug("%s: found matching network '%s'", ifname, network->filename);
358 } else
359 log_debug("%s: found matching network '%s'", ifname, network->filename);
360
361 *ret = network;
362 return 0;
363 }
364 }
365
366 *ret = NULL;
367
368 return -ENOENT;
369 }
370
371 int network_apply(Network *network, Link *link) {
372 int r;
373
374 assert(network);
375 assert(link);
376
377 link->network = network;
378
379 if (network->ipv4ll_route) {
380 Route *route;
381
382 r = route_new_static(network, 0, &route);
383 if (r < 0)
384 return r;
385
386 r = inet_pton(AF_INET, "169.254.0.0", &route->dst.in);
387 if (r == 0)
388 return -EINVAL;
389 if (r < 0)
390 return -errno;
391
392 route->family = AF_INET;
393 route->dst_prefixlen = 16;
394 route->scope = RT_SCOPE_LINK;
395 route->priority = IPV4LL_ROUTE_METRIC;
396 route->protocol = RTPROT_STATIC;
397 }
398
399 if (!strv_isempty(network->dns) ||
400 !strv_isempty(network->ntp) ||
401 !strv_isempty(network->search_domains) ||
402 !strv_isempty(network->route_domains))
403 link_dirty(link);
404
405 return 0;
406 }
407
408 bool network_has_static_ipv6_addresses(Network *network) {
409 Address *address;
410
411 assert(network);
412
413 LIST_FOREACH(addresses, address, network->static_addresses) {
414 if (address->family == AF_INET6)
415 return true;
416 }
417
418 return false;
419 }
420
421 int config_parse_netdev(const char *unit,
422 const char *filename,
423 unsigned line,
424 const char *section,
425 unsigned section_line,
426 const char *lvalue,
427 int ltype,
428 const char *rvalue,
429 void *data,
430 void *userdata) {
431 Network *network = userdata;
432 _cleanup_free_ char *kind_string = NULL;
433 char *p;
434 NetDev *netdev;
435 NetDevKind kind;
436 int r;
437
438 assert(filename);
439 assert(lvalue);
440 assert(rvalue);
441 assert(data);
442
443 kind_string = strdup(lvalue);
444 if (!kind_string)
445 return log_oom();
446
447 /* the keys are CamelCase versions of the kind */
448 for (p = kind_string; *p; p++)
449 *p = tolower(*p);
450
451 kind = netdev_kind_from_string(kind_string);
452 if (kind == _NETDEV_KIND_INVALID) {
453 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid NetDev kind: %s", lvalue);
454 return 0;
455 }
456
457 r = netdev_get(network->manager, rvalue, &netdev);
458 if (r < 0) {
459 log_syntax(unit, LOG_ERR, filename, line, r, "%s could not be found, ignoring assignment: %s", lvalue, rvalue);
460 return 0;
461 }
462
463 if (netdev->kind != kind) {
464 log_syntax(unit, LOG_ERR, filename, line, 0, "NetDev is not a %s, ignoring assignment: %s", lvalue, rvalue);
465 return 0;
466 }
467
468 switch (kind) {
469 case NETDEV_KIND_BRIDGE:
470 network->bridge = netdev;
471
472 break;
473 case NETDEV_KIND_BOND:
474 network->bond = netdev;
475
476 break;
477 case NETDEV_KIND_VRF:
478 network->vrf = netdev;
479
480 break;
481 case NETDEV_KIND_VLAN:
482 case NETDEV_KIND_MACVLAN:
483 case NETDEV_KIND_MACVTAP:
484 case NETDEV_KIND_IPVLAN:
485 case NETDEV_KIND_VXLAN:
486 case NETDEV_KIND_VCAN:
487 r = hashmap_put(network->stacked_netdevs, netdev->ifname, netdev);
488 if (r < 0) {
489 log_syntax(unit, LOG_ERR, filename, line, r, "Can not add NetDev '%s' to network: %m", rvalue);
490 return 0;
491 }
492
493 break;
494 default:
495 assert_not_reached("Can not parse NetDev");
496 }
497
498 netdev_ref(netdev);
499
500 return 0;
501 }
502
503 int config_parse_domains(
504 const char *unit,
505 const char *filename,
506 unsigned line,
507 const char *section,
508 unsigned section_line,
509 const char *lvalue,
510 int ltype,
511 const char *rvalue,
512 void *data,
513 void *userdata) {
514
515 const char *p;
516 Network *n = data;
517 int r;
518
519 assert(n);
520 assert(lvalue);
521 assert(rvalue);
522
523 if (isempty(rvalue)) {
524 n->search_domains = strv_free(n->search_domains);
525 n->route_domains = strv_free(n->route_domains);
526 return 0;
527 }
528
529 p = rvalue;
530 for (;;) {
531 _cleanup_free_ char *w = NULL, *normalized = NULL;
532 const char *domain;
533 bool is_route;
534
535 r = extract_first_word(&p, &w, NULL, 0);
536 if (r < 0) {
537 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract search or route domain, ignoring: %s", rvalue);
538 break;
539 }
540 if (r == 0)
541 break;
542
543 is_route = w[0] == '~';
544 domain = is_route ? w + 1 : w;
545
546 if (dns_name_is_root(domain) || streq(domain, "*")) {
547 /* If the root domain appears as is, or the special token "*" is found, we'll consider this as
548 * routing domain, unconditionally. */
549 is_route = true;
550 domain = "."; /* make sure we don't allow empty strings, thus write the root domain as "." */
551
552 } else {
553 r = dns_name_normalize(domain, &normalized);
554 if (r < 0) {
555 log_syntax(unit, LOG_ERR, filename, line, r, "'%s' is not a valid domain name, ignoring.", domain);
556 continue;
557 }
558
559 domain = normalized;
560
561 if (is_localhost(domain)) {
562 log_syntax(unit, LOG_ERR, filename, line, 0, "'localhost' domain names may not be configure as search or route domains, ignoring assignment: %s", domain);
563 continue;
564 }
565 }
566
567 if (is_route) {
568 r = strv_extend(&n->route_domains, domain);
569 if (r < 0)
570 return log_oom();
571
572 } else {
573 r = strv_extend(&n->search_domains, domain);
574 if (r < 0)
575 return log_oom();
576 }
577 }
578
579 strv_uniq(n->route_domains);
580 strv_uniq(n->search_domains);
581
582 return 0;
583 }
584
585 int config_parse_tunnel(const char *unit,
586 const char *filename,
587 unsigned line,
588 const char *section,
589 unsigned section_line,
590 const char *lvalue,
591 int ltype,
592 const char *rvalue,
593 void *data,
594 void *userdata) {
595 Network *network = userdata;
596 NetDev *netdev;
597 int r;
598
599 assert(filename);
600 assert(lvalue);
601 assert(rvalue);
602 assert(data);
603
604 r = netdev_get(network->manager, rvalue, &netdev);
605 if (r < 0) {
606 log_syntax(unit, LOG_ERR, filename, line, r, "Tunnel is invalid, ignoring assignment: %s", rvalue);
607 return 0;
608 }
609
610 if (netdev->kind != NETDEV_KIND_IPIP &&
611 netdev->kind != NETDEV_KIND_SIT &&
612 netdev->kind != NETDEV_KIND_GRE &&
613 netdev->kind != NETDEV_KIND_GRETAP &&
614 netdev->kind != NETDEV_KIND_IP6GRE &&
615 netdev->kind != NETDEV_KIND_IP6GRETAP &&
616 netdev->kind != NETDEV_KIND_VTI &&
617 netdev->kind != NETDEV_KIND_VTI6 &&
618 netdev->kind != NETDEV_KIND_IP6TNL
619 ) {
620 log_syntax(unit, LOG_ERR, filename, line, 0,
621 "NetDev is not a tunnel, ignoring assignment: %s", rvalue);
622 return 0;
623 }
624
625 r = hashmap_put(network->stacked_netdevs, netdev->ifname, netdev);
626 if (r < 0) {
627 log_syntax(unit, LOG_ERR, filename, line, r, "Cannot add VLAN '%s' to network, ignoring: %m", rvalue);
628 return 0;
629 }
630
631 netdev_ref(netdev);
632
633 return 0;
634 }
635
636 int config_parse_ipv4ll(
637 const char* unit,
638 const char *filename,
639 unsigned line,
640 const char *section,
641 unsigned section_line,
642 const char *lvalue,
643 int ltype,
644 const char *rvalue,
645 void *data,
646 void *userdata) {
647
648 AddressFamilyBoolean *link_local = data;
649
650 assert(filename);
651 assert(lvalue);
652 assert(rvalue);
653 assert(data);
654
655 /* Note that this is mostly like
656 * config_parse_address_family_boolean(), except that it
657 * applies only to IPv4 */
658
659 SET_FLAG(*link_local, ADDRESS_FAMILY_IPV4, parse_boolean(rvalue));
660
661 return 0;
662 }
663
664 int config_parse_dhcp(
665 const char* unit,
666 const char *filename,
667 unsigned line,
668 const char *section,
669 unsigned section_line,
670 const char *lvalue,
671 int ltype,
672 const char *rvalue,
673 void *data,
674 void *userdata) {
675
676 AddressFamilyBoolean *dhcp = data, s;
677
678 assert(filename);
679 assert(lvalue);
680 assert(rvalue);
681 assert(data);
682
683 /* Note that this is mostly like
684 * config_parse_address_family_boolean(), except that it
685 * understands some old names for the enum values */
686
687 s = address_family_boolean_from_string(rvalue);
688 if (s < 0) {
689
690 /* Previously, we had a slightly different enum here,
691 * support its values for compatbility. */
692
693 if (streq(rvalue, "none"))
694 s = ADDRESS_FAMILY_NO;
695 else if (streq(rvalue, "v4"))
696 s = ADDRESS_FAMILY_IPV4;
697 else if (streq(rvalue, "v6"))
698 s = ADDRESS_FAMILY_IPV6;
699 else if (streq(rvalue, "both"))
700 s = ADDRESS_FAMILY_YES;
701 else {
702 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse DHCP option, ignoring: %s", rvalue);
703 return 0;
704 }
705 }
706
707 *dhcp = s;
708 return 0;
709 }
710
711 static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = {
712 [DHCP_CLIENT_ID_MAC] = "mac",
713 [DHCP_CLIENT_ID_DUID] = "duid"
714 };
715
716 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DCHPClientIdentifier);
717 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DCHPClientIdentifier, "Failed to parse client identifier type");
718
719 int config_parse_ipv6token(
720 const char* unit,
721 const char *filename,
722 unsigned line,
723 const char *section,
724 unsigned section_line,
725 const char *lvalue,
726 int ltype,
727 const char *rvalue,
728 void *data,
729 void *userdata) {
730
731 union in_addr_union buffer;
732 struct in6_addr *token = data;
733 int r;
734
735 assert(filename);
736 assert(lvalue);
737 assert(rvalue);
738 assert(token);
739
740 r = in_addr_from_string(AF_INET6, rvalue, &buffer);
741 if (r < 0) {
742 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IPv6 token, ignoring: %s", rvalue);
743 return 0;
744 }
745
746 r = in_addr_is_null(AF_INET6, &buffer);
747 if (r != 0) {
748 log_syntax(unit, LOG_ERR, filename, line, r, "IPv6 token can not be the ANY address, ignoring: %s", rvalue);
749 return 0;
750 }
751
752 if ((buffer.in6.s6_addr32[0] | buffer.in6.s6_addr32[1]) != 0) {
753 log_syntax(unit, LOG_ERR, filename, line, 0, "IPv6 token can not be longer than 64 bits, ignoring: %s", rvalue);
754 return 0;
755 }
756
757 *token = buffer.in6;
758
759 return 0;
760 }
761
762 static const char* const ipv6_privacy_extensions_table[_IPV6_PRIVACY_EXTENSIONS_MAX] = {
763 [IPV6_PRIVACY_EXTENSIONS_NO] = "no",
764 [IPV6_PRIVACY_EXTENSIONS_PREFER_PUBLIC] = "prefer-public",
765 [IPV6_PRIVACY_EXTENSIONS_YES] = "yes",
766 };
767
768 DEFINE_STRING_TABLE_LOOKUP(ipv6_privacy_extensions, IPv6PrivacyExtensions);
769
770 int config_parse_ipv6_privacy_extensions(
771 const char* unit,
772 const char *filename,
773 unsigned line,
774 const char *section,
775 unsigned section_line,
776 const char *lvalue,
777 int ltype,
778 const char *rvalue,
779 void *data,
780 void *userdata) {
781
782 IPv6PrivacyExtensions *ipv6_privacy_extensions = data;
783 int k;
784
785 assert(filename);
786 assert(lvalue);
787 assert(rvalue);
788 assert(ipv6_privacy_extensions);
789
790 /* Our enum shall be a superset of booleans, hence first try
791 * to parse as boolean, and then as enum */
792
793 k = parse_boolean(rvalue);
794 if (k > 0)
795 *ipv6_privacy_extensions = IPV6_PRIVACY_EXTENSIONS_YES;
796 else if (k == 0)
797 *ipv6_privacy_extensions = IPV6_PRIVACY_EXTENSIONS_NO;
798 else {
799 IPv6PrivacyExtensions s;
800
801 s = ipv6_privacy_extensions_from_string(rvalue);
802 if (s < 0) {
803
804 if (streq(rvalue, "kernel"))
805 s = _IPV6_PRIVACY_EXTENSIONS_INVALID;
806 else {
807 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPv6 privacy extensions option, ignoring: %s", rvalue);
808 return 0;
809 }
810 }
811
812 *ipv6_privacy_extensions = s;
813 }
814
815 return 0;
816 }
817
818 int config_parse_hostname(
819 const char *unit,
820 const char *filename,
821 unsigned line,
822 const char *section,
823 unsigned section_line,
824 const char *lvalue,
825 int ltype,
826 const char *rvalue,
827 void *data,
828 void *userdata) {
829
830 char **hostname = data, *hn = NULL;
831 int r;
832
833 assert(filename);
834 assert(lvalue);
835 assert(rvalue);
836
837 r = config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &hn, userdata);
838 if (r < 0)
839 return r;
840
841 if (!hostname_is_valid(hn, false)) {
842 log_syntax(unit, LOG_ERR, filename, line, 0, "Hostname is not valid, ignoring assignment: %s", rvalue);
843 free(hn);
844 return 0;
845 }
846
847 free(*hostname);
848 *hostname = hostname_cleanup(hn);
849 return 0;
850 }
851
852 int config_parse_timezone(
853 const char *unit,
854 const char *filename,
855 unsigned line,
856 const char *section,
857 unsigned section_line,
858 const char *lvalue,
859 int ltype,
860 const char *rvalue,
861 void *data,
862 void *userdata) {
863
864 char **datap = data, *tz = NULL;
865 int r;
866
867 assert(filename);
868 assert(lvalue);
869 assert(rvalue);
870
871 r = config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &tz, userdata);
872 if (r < 0)
873 return r;
874
875 if (!timezone_is_valid(tz)) {
876 log_syntax(unit, LOG_ERR, filename, line, 0, "Timezone is not valid, ignoring assignment: %s", rvalue);
877 free(tz);
878 return 0;
879 }
880
881 free(*datap);
882 *datap = tz;
883
884 return 0;
885 }
886
887 int config_parse_dhcp_server_dns(
888 const char *unit,
889 const char *filename,
890 unsigned line,
891 const char *section,
892 unsigned section_line,
893 const char *lvalue,
894 int ltype,
895 const char *rvalue,
896 void *data,
897 void *userdata) {
898
899 Network *n = data;
900 const char *p = rvalue;
901 int r;
902
903 assert(filename);
904 assert(lvalue);
905 assert(rvalue);
906
907 for (;;) {
908 _cleanup_free_ char *w = NULL;
909 struct in_addr a, *m;
910
911 r = extract_first_word(&p, &w, NULL, 0);
912 if (r < 0) {
913 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract word, ignoring: %s", rvalue);
914 return 0;
915 }
916
917 if (r == 0)
918 return 0;
919
920 if (inet_pton(AF_INET, w, &a) <= 0) {
921 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse DNS server address, ignoring: %s", w);
922 continue;
923 }
924
925 m = realloc(n->dhcp_server_dns, (n->n_dhcp_server_dns + 1) * sizeof(struct in_addr));
926 if (!m)
927 return log_oom();
928
929 m[n->n_dhcp_server_dns++] = a;
930 n->dhcp_server_dns = m;
931 }
932 }
933
934 int config_parse_dhcp_server_ntp(
935 const char *unit,
936 const char *filename,
937 unsigned line,
938 const char *section,
939 unsigned section_line,
940 const char *lvalue,
941 int ltype,
942 const char *rvalue,
943 void *data,
944 void *userdata) {
945
946 Network *n = data;
947 const char *p = rvalue;
948 int r;
949
950 assert(filename);
951 assert(lvalue);
952 assert(rvalue);
953
954 for (;;) {
955 _cleanup_free_ char *w = NULL;
956 struct in_addr a, *m;
957
958 r = extract_first_word(&p, &w, NULL, 0);
959 if (r < 0) {
960 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract word, ignoring: %s", rvalue);
961 return 0;
962 }
963
964 if (r == 0)
965 return 0;
966
967 if (inet_pton(AF_INET, w, &a) <= 0) {
968 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse NTP server address, ignoring: %s", w);
969 continue;
970 }
971
972 m = realloc(n->dhcp_server_ntp, (n->n_dhcp_server_ntp + 1) * sizeof(struct in_addr));
973 if (!m)
974 return log_oom();
975
976 m[n->n_dhcp_server_ntp++] = a;
977 n->dhcp_server_ntp = m;
978 }
979 }
980
981 int config_parse_dns(
982 const char *unit,
983 const char *filename,
984 unsigned line,
985 const char *section,
986 unsigned section_line,
987 const char *lvalue,
988 int ltype,
989 const char *rvalue,
990 void *data,
991 void *userdata) {
992
993 Network *n = userdata;
994 int r;
995
996 assert(filename);
997 assert(lvalue);
998 assert(rvalue);
999
1000 for (;;) {
1001 _cleanup_free_ char *w = NULL;
1002 union in_addr_union a;
1003 int family;
1004
1005 r = extract_first_word(&rvalue, &w, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
1006 if (r == 0)
1007 break;
1008 if (r == -ENOMEM)
1009 return log_oom();
1010 if (r < 0) {
1011 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
1012 break;
1013 }
1014
1015 r = in_addr_from_string_auto(w, &family, &a);
1016 if (r < 0) {
1017 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse dns server address, ignoring: %s", w);
1018 continue;
1019 }
1020
1021 r = strv_consume(&n->dns, w);
1022 if (r < 0)
1023 return log_oom();
1024
1025 w = NULL;
1026 }
1027
1028 return 0;
1029 }
1030
1031 int config_parse_dnssec_negative_trust_anchors(
1032 const char *unit,
1033 const char *filename,
1034 unsigned line,
1035 const char *section,
1036 unsigned section_line,
1037 const char *lvalue,
1038 int ltype,
1039 const char *rvalue,
1040 void *data,
1041 void *userdata) {
1042
1043 const char *p = rvalue;
1044 Network *n = data;
1045 int r;
1046
1047 assert(n);
1048 assert(lvalue);
1049 assert(rvalue);
1050
1051 if (isempty(rvalue)) {
1052 n->dnssec_negative_trust_anchors = set_free_free(n->dnssec_negative_trust_anchors);
1053 return 0;
1054 }
1055
1056 for (;;) {
1057 _cleanup_free_ char *w = NULL;
1058
1059 r = extract_first_word(&p, &w, NULL, 0);
1060 if (r < 0) {
1061 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract negative trust anchor domain, ignoring: %s", rvalue);
1062 break;
1063 }
1064 if (r == 0)
1065 break;
1066
1067 r = dns_name_is_valid(w);
1068 if (r <= 0) {
1069 log_syntax(unit, LOG_ERR, filename, line, r, "%s is not a valid domain name, ignoring.", w);
1070 continue;
1071 }
1072
1073 r = set_ensure_allocated(&n->dnssec_negative_trust_anchors, &dns_name_hash_ops);
1074 if (r < 0)
1075 return log_oom();
1076
1077 r = set_put(n->dnssec_negative_trust_anchors, w);
1078 if (r < 0)
1079 return log_oom();
1080 if (r > 0)
1081 w = NULL;
1082 }
1083
1084 return 0;
1085 }
1086
1087 int config_parse_dhcp_route_table(const char *unit,
1088 const char *filename,
1089 unsigned line,
1090 const char *section,
1091 unsigned section_line,
1092 const char *lvalue,
1093 int ltype,
1094 const char *rvalue,
1095 void *data,
1096 void *userdata) {
1097 uint32_t rt;
1098 int r;
1099
1100 assert(filename);
1101 assert(lvalue);
1102 assert(rvalue);
1103 assert(data);
1104
1105 r = safe_atou32(rvalue, &rt);
1106 if (r < 0) {
1107 log_syntax(unit, LOG_ERR, filename, line, r,
1108 "Unable to read RouteTable, ignoring assignment: %s", rvalue);
1109 return 0;
1110 }
1111
1112 *((uint32_t *)data) = rt;
1113
1114 return 0;
1115 }
1116
1117 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_use_domains, dhcp_use_domains, DHCPUseDomains, "Failed to parse DHCP use domains setting");
1118
1119 static const char* const dhcp_use_domains_table[_DHCP_USE_DOMAINS_MAX] = {
1120 [DHCP_USE_DOMAINS_NO] = "no",
1121 [DHCP_USE_DOMAINS_ROUTE] = "route",
1122 [DHCP_USE_DOMAINS_YES] = "yes",
1123 };
1124
1125 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dhcp_use_domains, DHCPUseDomains, DHCP_USE_DOMAINS_YES);
1126
1127 DEFINE_CONFIG_PARSE_ENUM(config_parse_lldp_mode, lldp_mode, LLDPMode, "Failed to parse LLDP= setting.");
1128
1129 static const char* const lldp_mode_table[_LLDP_MODE_MAX] = {
1130 [LLDP_MODE_NO] = "no",
1131 [LLDP_MODE_YES] = "yes",
1132 [LLDP_MODE_ROUTERS_ONLY] = "routers-only",
1133 };
1134
1135 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(lldp_mode, LLDPMode, LLDP_MODE_YES);