2 This file is part of systemd.
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
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.
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.
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/>.
23 #include "alloc-util.h"
24 #include "conf-files.h"
25 #include "conf-parser.h"
26 #include "dns-domain.h"
28 #include "hostname-util.h"
29 #include "network-internal.h"
30 #include "networkd-network.h"
32 #include "parse-util.h"
34 #include "stat-util.h"
35 #include "string-table.h"
36 #include "string-util.h"
39 static int network_load_one(Manager
*manager
, const char *filename
) {
40 _cleanup_network_free_ Network
*network
= NULL
;
41 _cleanup_fclose_
FILE *file
= NULL
;
50 file
= fopen(filename
, "re");
58 if (null_or_empty_fd(fileno(file
))) {
59 log_debug("Skipping empty file: %s", filename
);
63 network
= new0(Network
, 1);
67 network
->manager
= manager
;
69 LIST_HEAD_INIT(network
->static_addresses
);
70 LIST_HEAD_INIT(network
->static_routes
);
71 LIST_HEAD_INIT(network
->static_fdb_entries
);
73 network
->stacked_netdevs
= hashmap_new(&string_hash_ops
);
74 if (!network
->stacked_netdevs
)
77 network
->addresses_by_section
= hashmap_new(NULL
);
78 if (!network
->addresses_by_section
)
81 network
->routes_by_section
= hashmap_new(NULL
);
82 if (!network
->routes_by_section
)
85 network
->fdb_entries_by_section
= hashmap_new(NULL
);
86 if (!network
->fdb_entries_by_section
)
89 network
->filename
= strdup(filename
);
90 if (!network
->filename
)
93 network
->name
= strdup(basename(filename
));
97 d
= strrchr(network
->name
, '.');
101 assert(streq(d
, ".network"));
105 network
->dhcp
= ADDRESS_FAMILY_NO
;
106 network
->dhcp_use_ntp
= true;
107 network
->dhcp_use_dns
= true;
108 network
->dhcp_use_hostname
= true;
109 network
->dhcp_use_routes
= true;
110 network
->dhcp_send_hostname
= true;
111 network
->dhcp_route_metric
= DHCP_ROUTE_METRIC
;
112 network
->dhcp_client_identifier
= DHCP_CLIENT_ID_DUID
;
114 network
->dhcp_server_emit_dns
= true;
115 network
->dhcp_server_emit_ntp
= true;
116 network
->dhcp_server_emit_timezone
= true;
118 network
->use_bpdu
= true;
119 network
->allow_port_to_be_root
= true;
120 network
->unicast_flood
= true;
122 network
->lldp_mode
= LLDP_MODE_ROUTERS_ONLY
;
124 network
->llmnr
= RESOLVE_SUPPORT_YES
;
125 network
->mdns
= RESOLVE_SUPPORT_NO
;
126 network
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
128 network
->link_local
= ADDRESS_FAMILY_IPV6
;
130 network
->ipv6_privacy_extensions
= IPV6_PRIVACY_EXTENSIONS_NO
;
131 network
->ipv6_accept_ra
= -1;
132 network
->ipv6_dad_transmits
= -1;
133 network
->ipv6_hop_limit
= -1;
134 network
->duid
.type
= _DUID_TYPE_INVALID
;
135 network
->proxy_arp
= -1;
137 r
= config_parse(NULL
, filename
, file
,
144 "DHCPv4\0" /* compat */
148 config_item_perf_lookup
, network_network_gperf_lookup
,
149 false, false, true, network
);
153 /* IPMasquerade=yes implies IPForward=yes */
154 if (network
->ip_masquerade
)
155 network
->ip_forward
|= ADDRESS_FAMILY_IPV4
;
157 LIST_PREPEND(networks
, manager
->networks
, network
);
159 r
= hashmap_ensure_allocated(&manager
->networks_by_name
, &string_hash_ops
);
163 r
= hashmap_put(manager
->networks_by_name
, network
->name
, network
);
167 LIST_FOREACH(routes
, route
, network
->static_routes
) {
168 if (!route
->family
) {
169 log_warning("Route section without Gateway field configured in %s. "
170 "Ignoring", filename
);
175 LIST_FOREACH(addresses
, address
, network
->static_addresses
) {
176 if (!address
->family
) {
177 log_warning("Address section without Address field configured in %s. "
178 "Ignoring", filename
);
188 int network_load(Manager
*manager
) {
190 _cleanup_strv_free_
char **files
= NULL
;
196 while ((network
= manager
->networks
))
197 network_free(network
);
199 r
= conf_files_list_strv(&files
, ".network", NULL
, network_dirs
);
201 return log_error_errno(r
, "Failed to enumerate network files: %m");
203 STRV_FOREACH_BACKWARDS(f
, files
) {
204 r
= network_load_one(manager
, *f
);
212 void network_free(Network
*network
) {
222 free(network
->filename
);
224 free(network
->match_mac
);
225 strv_free(network
->match_path
);
226 strv_free(network
->match_driver
);
227 strv_free(network
->match_type
);
228 strv_free(network
->match_name
);
230 free(network
->description
);
231 free(network
->dhcp_vendor_class_identifier
);
232 free(network
->dhcp_hostname
);
236 strv_free(network
->ntp
);
237 strv_free(network
->dns
);
238 strv_free(network
->search_domains
);
239 strv_free(network
->route_domains
);
240 strv_free(network
->bind_carrier
);
242 netdev_unref(network
->bridge
);
244 netdev_unref(network
->bond
);
246 HASHMAP_FOREACH(netdev
, network
->stacked_netdevs
, i
) {
247 hashmap_remove(network
->stacked_netdevs
, netdev
->ifname
);
248 netdev_unref(netdev
);
250 hashmap_free(network
->stacked_netdevs
);
252 while ((route
= network
->static_routes
))
255 while ((address
= network
->static_addresses
))
256 address_free(address
);
258 while ((fdb_entry
= network
->static_fdb_entries
))
259 fdb_entry_free(fdb_entry
);
261 hashmap_free(network
->addresses_by_section
);
262 hashmap_free(network
->routes_by_section
);
263 hashmap_free(network
->fdb_entries_by_section
);
265 if (network
->manager
) {
266 if (network
->manager
->networks
)
267 LIST_REMOVE(networks
, network
->manager
->networks
, network
);
269 if (network
->manager
->networks_by_name
)
270 hashmap_remove(network
->manager
->networks_by_name
, network
->name
);
275 condition_free_list(network
->match_host
);
276 condition_free_list(network
->match_virt
);
277 condition_free_list(network
->match_kernel
);
278 condition_free_list(network
->match_arch
);
280 free(network
->dhcp_server_timezone
);
281 free(network
->dhcp_server_dns
);
282 free(network
->dhcp_server_ntp
);
284 set_free_free(network
->dnssec_negative_trust_anchors
);
289 int network_get_by_name(Manager
*manager
, const char *name
, Network
**ret
) {
296 network
= hashmap_get(manager
->networks_by_name
, name
);
305 int network_get(Manager
*manager
, struct udev_device
*device
,
306 const char *ifname
, const struct ether_addr
*address
,
309 struct udev_device
*parent
;
310 const char *path
= NULL
, *parent_driver
= NULL
, *driver
= NULL
, *devtype
= NULL
;
316 path
= udev_device_get_property_value(device
, "ID_PATH");
318 parent
= udev_device_get_parent(device
);
320 parent_driver
= udev_device_get_driver(parent
);
322 driver
= udev_device_get_property_value(device
, "ID_NET_DRIVER");
324 devtype
= udev_device_get_devtype(device
);
327 LIST_FOREACH(networks
, network
, manager
->networks
) {
328 if (net_match_config(network
->match_mac
, network
->match_path
,
329 network
->match_driver
, network
->match_type
,
330 network
->match_name
, network
->match_host
,
331 network
->match_virt
, network
->match_kernel
,
333 address
, path
, parent_driver
, driver
,
335 if (network
->match_name
&& device
) {
337 uint8_t name_assign_type
= NET_NAME_UNKNOWN
;
339 attr
= udev_device_get_sysattr_value(device
, "name_assign_type");
341 (void) safe_atou8(attr
, &name_assign_type
);
343 if (name_assign_type
== NET_NAME_ENUM
)
344 log_warning("%s: found matching network '%s', based on potentially unpredictable ifname",
345 ifname
, network
->filename
);
347 log_debug("%s: found matching network '%s'", ifname
, network
->filename
);
349 log_debug("%s: found matching network '%s'", ifname
, network
->filename
);
361 int network_apply(Manager
*manager
, Network
*network
, Link
*link
) {
368 link
->network
= network
;
370 if (network
->ipv4ll_route
) {
373 r
= route_new_static(network
, 0, &route
);
377 r
= inet_pton(AF_INET
, "169.254.0.0", &route
->dst
.in
);
383 route
->family
= AF_INET
;
384 route
->dst_prefixlen
= 16;
385 route
->scope
= RT_SCOPE_LINK
;
386 route
->priority
= IPV4LL_ROUTE_METRIC
;
387 route
->protocol
= RTPROT_STATIC
;
390 if (!strv_isempty(network
->dns
) ||
391 !strv_isempty(network
->ntp
) ||
392 !strv_isempty(network
->search_domains
) ||
393 !strv_isempty(network
->route_domains
)) {
394 manager_dirty(manager
);
401 bool network_has_static_ipv6_addresses(Network
*network
) {
406 LIST_FOREACH(addresses
, address
, network
->static_addresses
) {
407 if (address
->family
== AF_INET6
)
414 int config_parse_netdev(const char *unit
,
415 const char *filename
,
418 unsigned section_line
,
424 Network
*network
= userdata
;
425 _cleanup_free_
char *kind_string
= NULL
;
436 kind_string
= strdup(lvalue
);
440 /* the keys are CamelCase versions of the kind */
441 for (p
= kind_string
; *p
; p
++)
444 kind
= netdev_kind_from_string(kind_string
);
445 if (kind
== _NETDEV_KIND_INVALID
) {
446 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid NetDev kind: %s", lvalue
);
450 r
= netdev_get(network
->manager
, rvalue
, &netdev
);
452 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "%s could not be found, ignoring assignment: %s", lvalue
, rvalue
);
456 if (netdev
->kind
!= kind
) {
457 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "NetDev is not a %s, ignoring assignment: %s", lvalue
, rvalue
);
462 case NETDEV_KIND_BRIDGE
:
463 network
->bridge
= netdev
;
466 case NETDEV_KIND_BOND
:
467 network
->bond
= netdev
;
470 case NETDEV_KIND_VLAN
:
471 case NETDEV_KIND_MACVLAN
:
472 case NETDEV_KIND_MACVTAP
:
473 case NETDEV_KIND_IPVLAN
:
474 case NETDEV_KIND_VXLAN
:
475 r
= hashmap_put(network
->stacked_netdevs
, netdev
->ifname
, netdev
);
477 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Can not add VLAN '%s' to network: %m", rvalue
);
483 assert_not_reached("Can not parse NetDev");
491 int config_parse_domains(
493 const char *filename
,
496 unsigned section_line
,
511 if (isempty(rvalue
)) {
512 n
->search_domains
= strv_free(n
->search_domains
);
513 n
->route_domains
= strv_free(n
->route_domains
);
519 _cleanup_free_
char *w
= NULL
, *normalized
= NULL
;
523 r
= extract_first_word(&p
, &w
, NULL
, 0);
525 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract search or route domain, ignoring: %s", rvalue
);
531 is_route
= w
[0] == '~';
532 domain
= is_route
? w
+ 1 : w
;
534 if (dns_name_is_root(domain
) || streq(domain
, "*")) {
535 /* If the root domain appears as is, or the special token "*" is found, we'll consider this as
536 * routing domain, unconditionally. */
538 domain
= "."; /* make sure we don't allow empty strings, thus write the root domain as "." */
541 r
= dns_name_normalize(domain
, &normalized
);
543 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "'%s' is not a valid domain name, ignoring.", domain
);
549 if (is_localhost(domain
)) {
550 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "'localhost' domain names may not be configure as search or route domains, ignoring assignment: %s", domain
);
556 r
= strv_extend(&n
->route_domains
, domain
);
561 r
= strv_extend(&n
->search_domains
, domain
);
567 strv_uniq(n
->route_domains
);
568 strv_uniq(n
->search_domains
);
573 int config_parse_tunnel(const char *unit
,
574 const char *filename
,
577 unsigned section_line
,
583 Network
*network
= userdata
;
592 r
= netdev_get(network
->manager
, rvalue
, &netdev
);
594 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Tunnel is invalid, ignoring assignment: %s", rvalue
);
598 if (netdev
->kind
!= NETDEV_KIND_IPIP
&&
599 netdev
->kind
!= NETDEV_KIND_SIT
&&
600 netdev
->kind
!= NETDEV_KIND_GRE
&&
601 netdev
->kind
!= NETDEV_KIND_GRETAP
&&
602 netdev
->kind
!= NETDEV_KIND_IP6GRE
&&
603 netdev
->kind
!= NETDEV_KIND_IP6GRETAP
&&
604 netdev
->kind
!= NETDEV_KIND_VTI
&&
605 netdev
->kind
!= NETDEV_KIND_VTI6
&&
606 netdev
->kind
!= NETDEV_KIND_IP6TNL
608 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
609 "NetDev is not a tunnel, ignoring assignment: %s", rvalue
);
613 r
= hashmap_put(network
->stacked_netdevs
, netdev
->ifname
, netdev
);
615 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Cannot add VLAN '%s' to network, ignoring: %m", rvalue
);
624 int config_parse_ipv4ll(
626 const char *filename
,
629 unsigned section_line
,
636 AddressFamilyBoolean
*link_local
= data
;
643 /* Note that this is mostly like
644 * config_parse_address_family_boolean(), except that it
645 * applies only to IPv4 */
647 SET_FLAG(*link_local
, ADDRESS_FAMILY_IPV4
, parse_boolean(rvalue
));
652 int config_parse_dhcp(
654 const char *filename
,
657 unsigned section_line
,
664 AddressFamilyBoolean
*dhcp
= data
, s
;
671 /* Note that this is mostly like
672 * config_parse_address_family_boolean(), except that it
673 * understands some old names for the enum values */
675 s
= address_family_boolean_from_string(rvalue
);
678 /* Previously, we had a slightly different enum here,
679 * support its values for compatbility. */
681 if (streq(rvalue
, "none"))
682 s
= ADDRESS_FAMILY_NO
;
683 else if (streq(rvalue
, "v4"))
684 s
= ADDRESS_FAMILY_IPV4
;
685 else if (streq(rvalue
, "v6"))
686 s
= ADDRESS_FAMILY_IPV6
;
687 else if (streq(rvalue
, "both"))
688 s
= ADDRESS_FAMILY_YES
;
690 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse DHCP option, ignoring: %s", rvalue
);
699 static const char* const dhcp_client_identifier_table
[_DHCP_CLIENT_ID_MAX
] = {
700 [DHCP_CLIENT_ID_MAC
] = "mac",
701 [DHCP_CLIENT_ID_DUID
] = "duid"
704 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier
, DCHPClientIdentifier
);
705 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier
, dhcp_client_identifier
, DCHPClientIdentifier
, "Failed to parse client identifier type");
707 int config_parse_ipv6token(
709 const char *filename
,
712 unsigned section_line
,
719 union in_addr_union buffer
;
720 struct in6_addr
*token
= data
;
728 r
= in_addr_from_string(AF_INET6
, rvalue
, &buffer
);
730 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse IPv6 token, ignoring: %s", rvalue
);
734 r
= in_addr_is_null(AF_INET6
, &buffer
);
736 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "IPv6 token can not be the ANY address, ignoring: %s", rvalue
);
740 if ((buffer
.in6
.s6_addr32
[0] | buffer
.in6
.s6_addr32
[1]) != 0) {
741 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "IPv6 token can not be longer than 64 bits, ignoring: %s", rvalue
);
750 static const char* const ipv6_privacy_extensions_table
[_IPV6_PRIVACY_EXTENSIONS_MAX
] = {
751 [IPV6_PRIVACY_EXTENSIONS_NO
] = "no",
752 [IPV6_PRIVACY_EXTENSIONS_PREFER_PUBLIC
] = "prefer-public",
753 [IPV6_PRIVACY_EXTENSIONS_YES
] = "yes",
756 DEFINE_STRING_TABLE_LOOKUP(ipv6_privacy_extensions
, IPv6PrivacyExtensions
);
758 int config_parse_ipv6_privacy_extensions(
760 const char *filename
,
763 unsigned section_line
,
770 IPv6PrivacyExtensions
*ipv6_privacy_extensions
= data
;
776 assert(ipv6_privacy_extensions
);
778 /* Our enum shall be a superset of booleans, hence first try
779 * to parse as boolean, and then as enum */
781 k
= parse_boolean(rvalue
);
783 *ipv6_privacy_extensions
= IPV6_PRIVACY_EXTENSIONS_YES
;
785 *ipv6_privacy_extensions
= IPV6_PRIVACY_EXTENSIONS_NO
;
787 IPv6PrivacyExtensions s
;
789 s
= ipv6_privacy_extensions_from_string(rvalue
);
792 if (streq(rvalue
, "kernel"))
793 s
= _IPV6_PRIVACY_EXTENSIONS_INVALID
;
795 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse IPv6 privacy extensions option, ignoring: %s", rvalue
);
800 *ipv6_privacy_extensions
= s
;
806 int config_parse_hostname(
808 const char *filename
,
811 unsigned section_line
,
818 char **hostname
= data
, *hn
= NULL
;
825 r
= config_parse_string(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &hn
, userdata
);
829 if (!hostname_is_valid(hn
, false)) {
830 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Hostname is not valid, ignoring assignment: %s", rvalue
);
836 *hostname
= hostname_cleanup(hn
);
840 int config_parse_timezone(
842 const char *filename
,
845 unsigned section_line
,
852 char **datap
= data
, *tz
= NULL
;
859 r
= config_parse_string(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &tz
, userdata
);
863 if (!timezone_is_valid(tz
)) {
864 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Timezone is not valid, ignoring assignment: %s", rvalue
);
875 int config_parse_dhcp_server_dns(
877 const char *filename
,
880 unsigned section_line
,
888 const char *p
= rvalue
;
896 _cleanup_free_
char *w
= NULL
;
897 struct in_addr a
, *m
;
899 r
= extract_first_word(&p
, &w
, NULL
, 0);
901 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract word, ignoring: %s", rvalue
);
908 if (inet_pton(AF_INET
, w
, &a
) <= 0) {
909 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse DNS server address, ignoring: %s", w
);
913 m
= realloc(n
->dhcp_server_dns
, (n
->n_dhcp_server_dns
+ 1) * sizeof(struct in_addr
));
917 m
[n
->n_dhcp_server_dns
++] = a
;
918 n
->dhcp_server_dns
= m
;
922 int config_parse_dhcp_server_ntp(
924 const char *filename
,
927 unsigned section_line
,
935 const char *p
= rvalue
;
943 _cleanup_free_
char *w
= NULL
;
944 struct in_addr a
, *m
;
946 r
= extract_first_word(&p
, &w
, NULL
, 0);
948 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract word, ignoring: %s", rvalue
);
955 if (inet_pton(AF_INET
, w
, &a
) <= 0) {
956 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse NTP server address, ignoring: %s", w
);
960 m
= realloc(n
->dhcp_server_ntp
, (n
->n_dhcp_server_ntp
+ 1) * sizeof(struct in_addr
));
964 m
[n
->n_dhcp_server_ntp
++] = a
;
965 n
->dhcp_server_ntp
= m
;
969 int config_parse_dnssec_negative_trust_anchors(
971 const char *filename
,
974 unsigned section_line
,
981 const char *p
= rvalue
;
989 if (isempty(rvalue
)) {
990 n
->dnssec_negative_trust_anchors
= set_free_free(n
->dnssec_negative_trust_anchors
);
995 _cleanup_free_
char *w
= NULL
;
997 r
= extract_first_word(&p
, &w
, NULL
, 0);
999 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract negative trust anchor domain, ignoring: %s", rvalue
);
1005 r
= dns_name_is_valid(w
);
1007 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "%s is not a valid domain name, ignoring.", w
);
1011 r
= set_ensure_allocated(&n
->dnssec_negative_trust_anchors
, &dns_name_hash_ops
);
1015 r
= set_put(n
->dnssec_negative_trust_anchors
, w
);
1025 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_use_domains
, dhcp_use_domains
, DHCPUseDomains
, "Failed to parse DHCP use domains setting");
1027 static const char* const dhcp_use_domains_table
[_DHCP_USE_DOMAINS_MAX
] = {
1028 [DHCP_USE_DOMAINS_NO
] = "no",
1029 [DHCP_USE_DOMAINS_ROUTE
] = "route",
1030 [DHCP_USE_DOMAINS_YES
] = "yes",
1033 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dhcp_use_domains
, DHCPUseDomains
, DHCP_USE_DOMAINS_YES
);
1035 DEFINE_CONFIG_PARSE_ENUM(config_parse_lldp_mode
, lldp_mode
, LLDPMode
, "Failed to parse LLDP= setting.");
1037 static const char* const lldp_mode_table
[_LLDP_MODE_MAX
] = {
1038 [LLDP_MODE_NO
] = "no",
1039 [LLDP_MODE_YES
] = "yes",
1040 [LLDP_MODE_ROUTERS_ONLY
] = "routers-only",
1043 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(lldp_mode
, LLDPMode
, LLDP_MODE_YES
);