]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address-label.c
tree-wide: make sure net/if.h is included before any linux/ header
[thirdparty/systemd.git] / src / network / networkd-address-label.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
95b74ef6 2
edda10f2 3/* Make sure the net/if.h header is included before any linux/ one */
95b74ef6
SS
4#include <net/if.h>
5#include <linux/if_addrlabel.h>
6
7#include "alloc-util.h"
95b74ef6 8#include "netlink-util.h"
fb486c90
YW
9#include "networkd-address-label.h"
10#include "networkd-link.h"
95b74ef6 11#include "networkd-manager.h"
fb486c90 12#include "networkd-network.h"
354bc760 13#include "networkd-queue.h"
95b74ef6 14#include "parse-util.h"
95b74ef6 15
cae418a3 16AddressLabel *address_label_free(AddressLabel *label) {
95b74ef6 17 if (!label)
cae418a3 18 return NULL;
95b74ef6
SS
19
20 if (label->network) {
d6a2a0f9
YW
21 assert(label->section);
22 hashmap_remove(label->network->address_labels_by_section, label->section);
95b74ef6
SS
23 }
24
307fe3cd 25 config_section_free(label->section);
cae418a3 26 return mfree(label);
95b74ef6
SS
27}
28
307fe3cd 29DEFINE_SECTION_CLEANUP_FUNCTIONS(AddressLabel, address_label_free);
fb486c90 30
95b74ef6 31static int address_label_new_static(Network *network, const char *filename, unsigned section_line, AddressLabel **ret) {
307fe3cd 32 _cleanup_(config_section_freep) ConfigSection *n = NULL;
8e766630 33 _cleanup_(address_label_freep) AddressLabel *label = NULL;
95b74ef6
SS
34 int r;
35
36 assert(network);
37 assert(ret);
d6a2a0f9
YW
38 assert(filename);
39 assert(section_line > 0);
95b74ef6 40
307fe3cd 41 r = config_section_new(filename, section_line, &n);
d6a2a0f9
YW
42 if (r < 0)
43 return r;
95b74ef6 44
d6a2a0f9
YW
45 label = hashmap_get(network->address_labels_by_section, n);
46 if (label) {
47 *ret = TAKE_PTR(label);
48 return 0;
95b74ef6
SS
49 }
50
0f7f2769
YW
51 label = new(AddressLabel, 1);
52 if (!label)
53 return -ENOMEM;
95b74ef6 54
0f7f2769
YW
55 *label = (AddressLabel) {
56 .network = network,
d6a2a0f9 57 .section = TAKE_PTR(n),
834f4294 58 .label = UINT32_MAX,
0f7f2769 59 };
95b74ef6 60
307fe3cd 61 r = hashmap_ensure_put(&network->address_labels_by_section, &config_section_hash_ops, label->section, label);
d6a2a0f9
YW
62 if (r < 0)
63 return r;
0f7f2769 64
1cc6c93a 65 *ret = TAKE_PTR(label);
95b74ef6
SS
66 return 0;
67}
68
80d62d4f
YW
69static int address_label_configure_handler(
70 sd_netlink *rtnl,
71 sd_netlink_message *m,
72 Request *req,
73 Link *link,
74 void *userdata) {
75
cccf9517
YW
76 int r;
77
cccf9517
YW
78 assert(m);
79 assert(link);
cccf9517
YW
80
81 r = sd_netlink_message_get_errno(m);
4ff296b0 82 if (r < 0 && r != -EEXIST) {
5ecb131d 83 log_link_message_warning_errno(link, m, r, "Could not set address label");
4ff296b0
YW
84 link_enter_failed(link);
85 return 1;
3a1dfdb4 86 }
cccf9517 87
354bc760 88 if (link->static_address_label_messages == 0) {
cccf9517 89 log_link_debug(link, "Addresses label set");
354bc760
YW
90 link->static_address_labels_configured = true;
91 link_check_ready(link);
92 }
cccf9517
YW
93
94 return 1;
95}
96
54ff39f7 97static int address_label_configure(AddressLabel *label, Link *link, Request *req) {
a79a8d16 98 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
95b74ef6
SS
99 int r;
100
101 assert(label);
102 assert(link);
103 assert(link->ifindex > 0);
104 assert(link->manager);
105 assert(link->manager->rtnl);
54ff39f7 106 assert(req);
95b74ef6 107
a79a8d16 108 r = sd_rtnl_message_new_addrlabel(link->manager->rtnl, &m, RTM_NEWADDRLABEL,
f7bf1abe 109 link->ifindex, AF_INET6);
95b74ef6 110 if (r < 0)
a79a8d16 111 return r;
95b74ef6 112
a79a8d16 113 r = sd_rtnl_message_addrlabel_set_prefixlen(m, label->prefixlen);
95b74ef6 114 if (r < 0)
a79a8d16 115 return r;
95b74ef6 116
a79a8d16 117 r = sd_netlink_message_append_u32(m, IFAL_LABEL, label->label);
95b74ef6 118 if (r < 0)
a79a8d16 119 return r;
95b74ef6 120
a79a8d16 121 r = sd_netlink_message_append_in6_addr(m, IFA_ADDRESS, &label->prefix);
95b74ef6 122 if (r < 0)
a79a8d16 123 return r;
95b74ef6 124
80d62d4f 125 return request_call_netlink_async(link->manager->rtnl, m, req);
95b74ef6
SS
126}
127
09d09207 128static int address_label_process_request(Request *req, Link *link, void *userdata) {
ff51134c 129 AddressLabel *label = ASSERT_PTR(userdata);
8bed7c55
YW
130 int r;
131
132 assert(req);
ff51134c 133 assert(link);
8bed7c55
YW
134
135 if (!link_is_ready_to_configure(link, false))
136 return 0;
137
ff51134c 138 r = address_label_configure(label, link, req);
8bed7c55
YW
139 if (r < 0)
140 return log_link_warning_errno(link, r, "Failed to configure address label: %m");
141
142 return 1;
143}
144
354bc760 145int link_request_static_address_labels(Link *link) {
fe2bc17c
YW
146 AddressLabel *label;
147 int r;
148
149 assert(link);
150 assert(link->network);
151
354bc760
YW
152 link->static_address_labels_configured = false;
153
fe2bc17c 154 HASHMAP_FOREACH(label, link->network->address_labels_by_section) {
09d09207
YW
155 r = link_queue_request_full(link, REQUEST_TYPE_ADDRESS_LABEL,
156 label, NULL, trivial_hash_func, trivial_compare_func,
157 address_label_process_request,
158 &link->static_address_label_messages,
159 address_label_configure_handler, NULL);
fe2bc17c 160 if (r < 0)
354bc760
YW
161 return log_link_warning_errno(link, r, "Failed to request address label: %m");
162 }
fe2bc17c 163
354bc760
YW
164 if (link->static_address_label_messages == 0) {
165 link->static_address_labels_configured = true;
166 link_check_ready(link);
167 } else {
168 log_link_debug(link, "Setting address labels.");
169 link_set_state(link, LINK_STATE_CONFIGURING);
fe2bc17c
YW
170 }
171
172 return 0;
173}
174
834f4294
YW
175static int address_label_section_verify(AddressLabel *label) {
176 assert(label);
177 assert(label->section);
178
179 if (section_is_invalid(label->section))
180 return -EINVAL;
181
182 if (!label->prefix_set)
183 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
184 "%s: [IPv6AddressLabel] section without Prefix= setting specified. "
185 "Ignoring [IPv6AddressLabel] section from line %u.",
186 label->section->filename, label->section->line);
187
188 if (label->label == UINT32_MAX)
189 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
190 "%s: [IPv6AddressLabel] section without Label= setting specified. "
191 "Ignoring [IPv6AddressLabel] section from line %u.",
192 label->section->filename, label->section->line);
193
194 return 0;
195}
196
13ffa39f 197void network_drop_invalid_address_labels(Network *network) {
ab316813
YW
198 AddressLabel *label;
199
200 assert(network);
201
202 HASHMAP_FOREACH(label, network->address_labels_by_section)
834f4294 203 if (address_label_section_verify(label) < 0)
ab316813
YW
204 address_label_free(label);
205}
206
354bc760
YW
207int config_parse_address_label_prefix(
208 const char *unit,
209 const char *filename,
210 unsigned line,
211 const char *section,
212 unsigned section_line,
213 const char *lvalue,
214 int ltype,
215 const char *rvalue,
216 void *data,
217 void *userdata) {
95b74ef6 218
fcbf4cb7 219 _cleanup_(address_label_free_or_set_invalidp) AddressLabel *n = NULL;
95b74ef6 220 Network *network = userdata;
2551b422
YW
221 unsigned char prefixlen;
222 union in_addr_union a;
f7bf1abe 223 int r;
95b74ef6
SS
224
225 assert(filename);
226 assert(section);
227 assert(lvalue);
228 assert(rvalue);
229 assert(data);
230
231 r = address_label_new_static(network, filename, section_line, &n);
232 if (r < 0)
d96edb2c 233 return log_oom();
95b74ef6 234
2551b422 235 r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &prefixlen);
95b74ef6 236 if (r < 0) {
2551b422
YW
237 log_syntax(unit, LOG_WARNING, filename, line, r,
238 "Invalid prefix for address label, ignoring assignment: %s", rvalue);
95b74ef6
SS
239 return 0;
240 }
2551b422
YW
241 if (in6_addr_is_ipv4_mapped_address(&a.in6) && prefixlen > 96) {
242 /* See ip6addrlbl_alloc() in net/ipv6/addrlabel.c of kernel. */
243 log_syntax(unit, LOG_WARNING, filename, line, 0,
244 "The prefix length of IPv4 mapped address for address label must be equal to or smaller than 96, "
245 "ignoring assignment: %s", rvalue);
246 return 0;
247 }
248
834f4294 249 n->prefix = a.in6;
2551b422 250 n->prefixlen = prefixlen;
834f4294 251 n->prefix_set = true;
95b74ef6 252
dea161d9 253 TAKE_PTR(n);
95b74ef6
SS
254 return 0;
255}
256
257int config_parse_address_label(
258 const char *unit,
259 const char *filename,
260 unsigned line,
261 const char *section,
262 unsigned section_line,
263 const char *lvalue,
264 int ltype,
265 const char *rvalue,
266 void *data,
267 void *userdata) {
268
fcbf4cb7 269 _cleanup_(address_label_free_or_set_invalidp) AddressLabel *n = NULL;
95b74ef6
SS
270 Network *network = userdata;
271 uint32_t k;
272 int r;
273
274 assert(filename);
275 assert(section);
276 assert(lvalue);
277 assert(rvalue);
278 assert(data);
279
280 r = address_label_new_static(network, filename, section_line, &n);
281 if (r < 0)
d96edb2c 282 return log_oom();
95b74ef6
SS
283
284 r = safe_atou32(rvalue, &k);
285 if (r < 0) {
d96edb2c 286 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse address label, ignoring: %s", rvalue);
95b74ef6
SS
287 return 0;
288 }
289
834f4294 290 if (k == UINT_MAX) {
d96edb2c 291 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address label is invalid, ignoring: %s", rvalue);
95b74ef6
SS
292 return 0;
293 }
294
295 n->label = k;
dea161d9 296 TAKE_PTR(n);
95b74ef6
SS
297
298 return 0;
299}