]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-util.c
codespell: fix spelling errors
[thirdparty/systemd.git] / src / network / networkd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "condition.h"
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"
9 #include "util.h"
10
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);
14
15 if (b == ADDRESS_FAMILY_IPV4)
16 return "ipv4";
17 if (b == ADDRESS_FAMILY_IPV6)
18 return "ipv6";
19
20 return NULL;
21 }
22
23 AddressFamilyBoolean address_family_boolean_from_string(const char *s) {
24 int r;
25
26 /* Make this a true superset of a boolean */
27
28 r = parse_boolean(s);
29 if (r > 0)
30 return ADDRESS_FAMILY_YES;
31 if (r == 0)
32 return ADDRESS_FAMILY_NO;
33
34 if (streq(s, "ipv4"))
35 return ADDRESS_FAMILY_IPV4;
36 if (streq(s, "ipv6"))
37 return ADDRESS_FAMILY_IPV6;
38
39 return _ADDRESS_FAMILY_BOOLEAN_INVALID;
40 }
41
42 DEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");
43
44 int config_parse_address_family_boolean_with_kernel(
45 const char* unit,
46 const char *filename,
47 unsigned line,
48 const char *section,
49 unsigned section_line,
50 const char *lvalue,
51 int ltype,
52 const char *rvalue,
53 void *data,
54 void *userdata) {
55
56 AddressFamilyBoolean *fwd = data, s;
57
58 assert(filename);
59 assert(lvalue);
60 assert(rvalue);
61 assert(data);
62
63 /* This function is mostly obsolete now. It simply redirects
64 * "kernel" to "no". In older networkd versions we used to
65 * distinguish 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
69 * to "no". */
70
71 s = address_family_boolean_from_string(rvalue);
72 if (s < 0) {
73 if (streq(rvalue, "kernel"))
74 s = ADDRESS_FAMILY_NO;
75 else {
76 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue);
77 return 0;
78 }
79 }
80
81 *fwd = s;
82
83 return 0;
84 }
85
86 /* Router lifetime can be set with netlink interface since kernel >= 4.5
87 * so for the supported kernel we don't need to expire routes in userspace */
88 int kernel_route_expiration_supported(void) {
89 static int cached = -1;
90 int r;
91
92 if (cached < 0) {
93 Condition c = {
94 .type = CONDITION_KERNEL_VERSION,
95 .parameter = (char *) ">= 4.5"
96 };
97 r = condition_test(&c);
98 if (r < 0)
99 return r;
100
101 cached = r;
102 }
103 return cached;
104 }
105
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);
109 }
110
111 static int network_config_compare_func(const NetworkConfigSection *x, const NetworkConfigSection *y) {
112 int r;
113
114 r = strcmp(x->filename, y->filename);
115 if (r != 0)
116 return r;
117
118 return CMP(x->line, y->line);
119 }
120
121 DEFINE_HASH_OPS(network_config_hash_ops, NetworkConfigSection, network_config_hash_func, network_config_compare_func);
122
123 int network_config_section_new(const char *filename, unsigned line, NetworkConfigSection **s) {
124 NetworkConfigSection *cs;
125
126 cs = malloc0(offsetof(NetworkConfigSection, filename) + strlen(filename) + 1);
127 if (!cs)
128 return -ENOMEM;
129
130 strcpy(cs->filename, filename);
131 cs->line = line;
132
133 *s = TAKE_PTR(cs);
134
135 return 0;
136 }
137
138 void network_config_section_free(NetworkConfigSection *cs) {
139 free(cs);
140 }