]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-util.c
basic/terminal-util: rename our replacement highlight-yellow and test both the origin...
[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 static const char* const address_family_table[_ADDRESS_FAMILY_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_table[_ADDRESS_FAMILY_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 static const char* const routing_policy_rule_address_family_table[_ADDRESS_FAMILY_MAX] = {
28 [ADDRESS_FAMILY_YES] = "both",
29 [ADDRESS_FAMILY_IPV4] = "ipv4",
30 [ADDRESS_FAMILY_IPV6] = "ipv6",
31 };
32
33 static const char* const duplicate_address_detection_address_family_table[_ADDRESS_FAMILY_MAX] = {
34 [ADDRESS_FAMILY_NO] = "none",
35 [ADDRESS_FAMILY_YES] = "both",
36 [ADDRESS_FAMILY_IPV4] = "ipv4",
37 [ADDRESS_FAMILY_IPV6] = "ipv6",
38 };
39
40 static const char* const dhcp_lease_server_type_table[_SD_DHCP_LEASE_SERVER_TYPE_MAX] = {
41 [SD_DHCP_LEASE_DNS] = "DNS servers",
42 [SD_DHCP_LEASE_NTP] = "NTP servers",
43 [SD_DHCP_LEASE_SIP] = "SIP servers",
44 [SD_DHCP_LEASE_POP3] = "POP3 servers",
45 [SD_DHCP_LEASE_SMTP] = "SMTP servers",
46 [SD_DHCP_LEASE_LPR] = "LPR servers",
47 };
48
49 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(address_family, AddressFamily, ADDRESS_FAMILY_YES);
50 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(link_local_address_family, AddressFamily, ADDRESS_FAMILY_YES);
51 DEFINE_STRING_TABLE_LOOKUP(routing_policy_rule_address_family, AddressFamily);
52 DEFINE_STRING_TABLE_LOOKUP(duplicate_address_detection_address_family, AddressFamily);
53 DEFINE_CONFIG_PARSE_ENUM(config_parse_link_local_address_family, link_local_address_family,
54 AddressFamily, "Failed to parse option");
55 DEFINE_STRING_TABLE_LOOKUP(dhcp_lease_server_type, sd_dhcp_lease_server_type);
56
57 int config_parse_address_family_with_kernel(
58 const char* unit,
59 const char *filename,
60 unsigned line,
61 const char *section,
62 unsigned section_line,
63 const char *lvalue,
64 int ltype,
65 const char *rvalue,
66 void *data,
67 void *userdata) {
68
69 AddressFamily *fwd = data, s;
70
71 assert(filename);
72 assert(lvalue);
73 assert(rvalue);
74 assert(data);
75
76 /* This function is mostly obsolete now. It simply redirects
77 * "kernel" to "no". In older networkd versions we used to
78 * distinguish IPForward=off from IPForward=kernel, where the
79 * former would explicitly turn off forwarding while the
80 * latter would simply not touch the setting. But that logic
81 * is gone, hence silently accept the old setting, but turn it
82 * to "no". */
83
84 s = address_family_from_string(rvalue);
85 if (s < 0) {
86 if (streq(rvalue, "kernel"))
87 s = ADDRESS_FAMILY_NO;
88 else {
89 log_syntax(unit, LOG_WARNING, filename, line, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue);
90 return 0;
91 }
92 }
93
94 *fwd = s;
95
96 return 0;
97 }
98
99 /* Router lifetime can be set with netlink interface since kernel >= 4.5
100 * so for the supported kernel we don't need to expire routes in userspace */
101 int kernel_route_expiration_supported(void) {
102 static int cached = -1;
103 int r;
104
105 if (cached < 0) {
106 Condition c = {
107 .type = CONDITION_KERNEL_VERSION,
108 .parameter = (char *) ">= 4.5"
109 };
110 r = condition_test(&c, NULL);
111 if (r < 0)
112 return r;
113
114 cached = r;
115 }
116 return cached;
117 }
118
119 static void network_config_hash_func(const NetworkConfigSection *c, struct siphash *state) {
120 siphash24_compress(c->filename, strlen(c->filename), state);
121 siphash24_compress(&c->line, sizeof(c->line), state);
122 }
123
124 static int network_config_compare_func(const NetworkConfigSection *x, const NetworkConfigSection *y) {
125 int r;
126
127 r = strcmp(x->filename, y->filename);
128 if (r != 0)
129 return r;
130
131 return CMP(x->line, y->line);
132 }
133
134 DEFINE_HASH_OPS(network_config_hash_ops, NetworkConfigSection, network_config_hash_func, network_config_compare_func);
135
136 int network_config_section_new(const char *filename, unsigned line, NetworkConfigSection **s) {
137 NetworkConfigSection *cs;
138
139 cs = malloc0(offsetof(NetworkConfigSection, filename) + strlen(filename) + 1);
140 if (!cs)
141 return -ENOMEM;
142
143 strcpy(cs->filename, filename);
144 cs->line = line;
145
146 *s = TAKE_PTR(cs);
147
148 return 0;
149 }
150
151 void network_config_section_free(NetworkConfigSection *cs) {
152 free(cs);
153 }