]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
fc2f9534 | 2 | |
f02ba163 | 3 | #include "condition.h" |
fc2f9534 | 4 | #include "conf-parser.h" |
fc2f9534 | 5 | #include "networkd-util.h" |
6bedfcbb | 6 | #include "parse-util.h" |
8b43440b | 7 | #include "string-table.h" |
07630cea LP |
8 | #include "string-util.h" |
9 | #include "util.h" | |
fc2f9534 | 10 | |
e800fd24 YW |
11 | static const char * const address_family_boolean_table[_ADDRESS_FAMILY_BOOLEAN_MAX] = { |
12 | [ADDRESS_FAMILY_NO] = "no", | |
13 | [ADDRESS_FAMILY_YES] = "yes", | |
14 | [ADDRESS_FAMILY_IPV4] = "ipv4", | |
15 | [ADDRESS_FAMILY_IPV6] = "ipv6", | |
16 | }; | |
17 | ||
18 | static const char * const link_local_address_family_boolean_table[_ADDRESS_FAMILY_BOOLEAN_MAX] = { | |
19 | [ADDRESS_FAMILY_NO] = "no", | |
20 | [ADDRESS_FAMILY_YES] = "yes", | |
21 | [ADDRESS_FAMILY_IPV4] = "ipv4", | |
22 | [ADDRESS_FAMILY_IPV6] = "ipv6", | |
23 | [ADDRESS_FAMILY_FALLBACK] = "fallback", | |
24 | [ADDRESS_FAMILY_FALLBACK_IPV4] = "ipv4-fallback", | |
25 | }; | |
26 | ||
27 | DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(address_family_boolean, AddressFamilyBoolean, ADDRESS_FAMILY_YES); | |
28 | DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(link_local_address_family_boolean, AddressFamilyBoolean, ADDRESS_FAMILY_YES); | |
29 | DEFINE_CONFIG_PARSE_ENUM(config_parse_link_local_address_family_boolean, link_local_address_family_boolean, | |
30 | AddressFamilyBoolean, "Failed to parse option"); | |
fc2f9534 LP |
31 | |
32 | int config_parse_address_family_boolean_with_kernel( | |
33 | const char* unit, | |
34 | const char *filename, | |
35 | unsigned line, | |
36 | const char *section, | |
37 | unsigned section_line, | |
38 | const char *lvalue, | |
39 | int ltype, | |
40 | const char *rvalue, | |
41 | void *data, | |
42 | void *userdata) { | |
43 | ||
44 | AddressFamilyBoolean *fwd = data, s; | |
45 | ||
46 | assert(filename); | |
47 | assert(lvalue); | |
48 | assert(rvalue); | |
49 | assert(data); | |
50 | ||
765afd5c LP |
51 | /* This function is mostly obsolete now. It simply redirects |
52 | * "kernel" to "no". In older networkd versions we used to | |
5238e957 | 53 | * distinguish IPForward=off from IPForward=kernel, where the |
765afd5c LP |
54 | * former would explicitly turn off forwarding while the |
55 | * latter would simply not touch the setting. But that logic | |
56 | * is gone, hence silently accept the old setting, but turn it | |
57 | * to "no". */ | |
58 | ||
fc2f9534 LP |
59 | s = address_family_boolean_from_string(rvalue); |
60 | if (s < 0) { | |
61 | if (streq(rvalue, "kernel")) | |
765afd5c | 62 | s = ADDRESS_FAMILY_NO; |
fc2f9534 | 63 | else { |
85e070c2 | 64 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue); |
fc2f9534 LP |
65 | return 0; |
66 | } | |
67 | } | |
68 | ||
69 | *fwd = s; | |
70 | ||
71 | return 0; | |
72 | } | |
f02ba163 DD |
73 | |
74 | /* Router lifetime can be set with netlink interface since kernel >= 4.5 | |
5238e957 | 75 | * so for the supported kernel we don't need to expire routes in userspace */ |
f02ba163 DD |
76 | int kernel_route_expiration_supported(void) { |
77 | static int cached = -1; | |
78 | int r; | |
79 | ||
80 | if (cached < 0) { | |
81 | Condition c = { | |
82 | .type = CONDITION_KERNEL_VERSION, | |
83 | .parameter = (char *) ">= 4.5" | |
84 | }; | |
85 | r = condition_test(&c); | |
86 | if (r < 0) | |
87 | return r; | |
88 | ||
89 | cached = r; | |
90 | } | |
91 | return cached; | |
92 | } | |
48315d3d YW |
93 | |
94 | static void network_config_hash_func(const NetworkConfigSection *c, struct siphash *state) { | |
95 | siphash24_compress(c->filename, strlen(c->filename), state); | |
96 | siphash24_compress(&c->line, sizeof(c->line), state); | |
97 | } | |
98 | ||
99 | static int network_config_compare_func(const NetworkConfigSection *x, const NetworkConfigSection *y) { | |
100 | int r; | |
101 | ||
102 | r = strcmp(x->filename, y->filename); | |
103 | if (r != 0) | |
104 | return r; | |
105 | ||
106 | return CMP(x->line, y->line); | |
107 | } | |
108 | ||
109 | DEFINE_HASH_OPS(network_config_hash_ops, NetworkConfigSection, network_config_hash_func, network_config_compare_func); | |
110 | ||
111 | int network_config_section_new(const char *filename, unsigned line, NetworkConfigSection **s) { | |
112 | NetworkConfigSection *cs; | |
113 | ||
114 | cs = malloc0(offsetof(NetworkConfigSection, filename) + strlen(filename) + 1); | |
115 | if (!cs) | |
116 | return -ENOMEM; | |
117 | ||
118 | strcpy(cs->filename, filename); | |
119 | cs->line = line; | |
120 | ||
121 | *s = TAKE_PTR(cs); | |
122 | ||
123 | return 0; | |
124 | } | |
125 | ||
126 | void network_config_section_free(NetworkConfigSection *cs) { | |
127 | free(cs); | |
128 | } |