1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include "conf-parser.h"
5 #include "networkd-util.h"
6 #include "parse-util.h"
7 #include "string-table.h"
8 #include "string-util.h"
11 const char *address_family_boolean_to_string(AddressFamilyBoolean b
) {
12 if (IN_SET(b
, ADDRESS_FAMILY_YES
, ADDRESS_FAMILY_NO
))
13 return yes_no(b
== ADDRESS_FAMILY_YES
);
15 if (b
== ADDRESS_FAMILY_IPV4
)
17 if (b
== ADDRESS_FAMILY_IPV6
)
23 AddressFamilyBoolean
address_family_boolean_from_string(const char *s
) {
26 /* Make this a true superset of a boolean */
30 return ADDRESS_FAMILY_YES
;
32 return ADDRESS_FAMILY_NO
;
35 return ADDRESS_FAMILY_IPV4
;
37 return ADDRESS_FAMILY_IPV6
;
39 return _ADDRESS_FAMILY_BOOLEAN_INVALID
;
42 DEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean
, address_family_boolean
, AddressFamilyBoolean
, "Failed to parse option");
44 int config_parse_address_family_boolean_with_kernel(
49 unsigned section_line
,
56 AddressFamilyBoolean
*fwd
= data
, s
;
63 /* This function is mostly obsolete now. It simply redirects
64 * "kernel" to "no". In older networkd versions we used to
65 * distuingish IPForward=off from IPForward=kernel, where the
66 * former would explicitly turn off forwarding while the
67 * latter would simply not touch the setting. But that logic
68 * is gone, hence silently accept the old setting, but turn it
71 s
= address_family_boolean_from_string(rvalue
);
73 if (streq(rvalue
, "kernel"))
74 s
= ADDRESS_FAMILY_NO
;
76 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue
);
86 /* Router lifetime can be set with netlink interface since kernel >= 4.5
87 * so for the supported kernel we dont need to expire routes in userspace */
88 int kernel_route_expiration_supported(void) {
89 static int cached
= -1;
94 .type
= CONDITION_KERNEL_VERSION
,
95 .parameter
= (char *) ">= 4.5"
97 r
= condition_test(&c
);
106 static void network_config_hash_func(const NetworkConfigSection
*c
, struct siphash
*state
) {
107 siphash24_compress(c
->filename
, strlen(c
->filename
), state
);
108 siphash24_compress(&c
->line
, sizeof(c
->line
), state
);
111 static int network_config_compare_func(const NetworkConfigSection
*x
, const NetworkConfigSection
*y
) {
114 r
= strcmp(x
->filename
, y
->filename
);
118 return CMP(x
->line
, y
->line
);
121 DEFINE_HASH_OPS(network_config_hash_ops
, NetworkConfigSection
, network_config_hash_func
, network_config_compare_func
);
123 int network_config_section_new(const char *filename
, unsigned line
, NetworkConfigSection
**s
) {
124 NetworkConfigSection
*cs
;
126 cs
= malloc0(offsetof(NetworkConfigSection
, filename
) + strlen(filename
) + 1);
130 strcpy(cs
->filename
, filename
);
138 void network_config_section_free(NetworkConfigSection
*cs
) {