Debian Codegraph @

src/network/networkd-address-label.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/if_addr.h>
4 #include <linux/if_addrlabel.h>
5
6 #include "sd-netlink.h"
7
8 #include "alloc-util.h"
9 #include "conf-parser.h"
10 #include "hashmap.h"
11 #include "networkd-address-label.h"
12 #include "networkd-link.h"
13 #include "networkd-manager.h"
14 #include "networkd-network.h"
15 #include "networkd-queue.h"
16 #include "parse-util.h"
17 #include "string-util.h"
18
19 AddressLabel *address_label_free(AddressLabel *label) {
20 if (!label)
21 return NULL;
22
23 if (label->network) {
24 assert(label->section);
25 hashmap_remove(label->network->address_labels_by_section, label->section);
26 }
27
28 if (label->manager) {
29 assert(label->section);
30 hashmap_remove(label->manager->address_labels_by_section, label->section);
31 }
32
33 config_section_free(label->section);
34 return mfree(label);
35 }
36
37 DEFINE_SECTION_CLEANUP_FUNCTIONS(AddressLabel, address_label_free);
38
39 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
40 address_label_section_hash_ops,
41 ConfigSection,
42 config_section_hash_func,
43 config_section_compare_func,
44 AddressLabel,
45 address_label_free);
46
47 static int address_label_new_static(
48 Manager *manager,
49 Network *network,
50 const char *filename,
51 unsigned section_line,
52 AddressLabel **ret) {
53
54 _cleanup_(config_section_freep) ConfigSection *n = NULL;
55 _cleanup_(address_label_freep) AddressLabel *label = NULL;
56 Hashmap **address_labels_by_section;
57 int r;
58
59 assert(!manager != !network);
60 assert(ret);
61 assert(filename);
62 assert(section_line > 0);
63
64 address_labels_by_section = manager ? &manager->address_labels_by_section : &network->address_labels_by_section;
65
66 r = config_section_new(filename, section_line, &n);
67 if (r < 0)
68 return r;
69
70 label = hashmap_get(*address_labels_by_section, n);
71 if (label) {
72 *ret = TAKE_PTR(label);
73 return 0;
74 }
75
76 label = new(AddressLabel, 1);
77 if (!label)
78 return -ENOMEM;
79
80 *label = (AddressLabel) {
81 .manager = manager,
82 .network = network,
83 .section = TAKE_PTR(n),
84 .label = UINT32_MAX,
85 };
86
87 r = hashmap_ensure_put(address_labels_by_section, &address_label_section_hash_ops, label->section, label);
88 if (r < 0)
89 return r;
90
91 *ret = TAKE_PTR(label);
92 return 0;
93 }
94
95 static int link_address_label_configure_handler(
96 sd_netlink *rtnl,
97 sd_netlink_message *m,
98 Request *req,
99 Link *link,
100 void *userdata) {
101
102 int r;
103
104 assert(m);
105 assert(link);
106
107 r = sd_netlink_message_get_errno(m);
108 if (r < 0 && r != -EEXIST) {
109 log_link_message_warning_errno(link, m, r, "Could not set address label");
110 link_enter_failed(link);
111 return 1;
112 }
113
114 if (link->static_address_label_messages == 0) {
115 log_link_debug(link, "Addresses label set.");
116 link->static_address_labels_configured = true;
117 link_check_ready(link);
118 }
119
120 return 1;
121 }
122
123 static int manager_address_label_configure_handler(
124 sd_netlink *rtnl,
125 sd_netlink_message *m,
126 Request *req,
127 Link *link,
128 void *userdata) {
129
130 Manager *manager = ASSERT_PTR(ASSERT_PTR(req)->manager);
131 int r;
132
133 assert(m);
134 assert(!link);
135
136 r = sd_netlink_message_get_errno(m);
137 if (r < 0 && r != -EEXIST) {
138 log_message_warning_errno(m, r, "Could not set address label");
139 return 1;
140 }
141
142 if (manager->static_address_label_messages == 0) {
143 log_debug("Addresses label set.");
144 manager->static_address_labels_configured = true;
145 }
146
147 return 1;
148 }
149
150 static int address_label_fill_message(AddressLabel *label, sd_netlink_message *m) {
151 int r;
152
153 assert(label);
154 assert(m);
155
156 r = sd_rtnl_message_addrlabel_set_prefixlen(m, label->prefixlen);
157 if (r < 0)
158 return r;
159
160 r = sd_netlink_message_append_u32(m, IFAL_LABEL, label->label);
161 if (r < 0)
162 return r;
163
164 return sd_netlink_message_append_in6_addr(m, IFA_ADDRESS, &label->prefix);
165 }
166
167 static int link_address_label_configure(AddressLabel *label, Link *link, Request *req) {
168 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
169 int r;
170
171 assert(label);
172 assert(link);
173 assert(link->ifindex > 0);
174 assert(link->manager);
175 assert(link->manager->rtnl);
176 assert(req);
177
178 r = sd_rtnl_message_new_addrlabel(link->manager->rtnl, &m, RTM_NEWADDRLABEL,
179 link->ifindex, AF_INET6);
180 if (r < 0)
181 return r;
182
183 r = address_label_fill_message(label, m);
184 if (r < 0)
185 return r;
186
187 return request_call_netlink_async(link->manager->rtnl, m, req);
188 }
189
190 static int manager_address_label_configure(AddressLabel *label, Manager *manager, Request *req) {
191 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
192 int r;
193
194 assert(label);
195 assert(manager);
196 assert(manager->rtnl);
197 assert(req);
198
199 r = sd_rtnl_message_new_addrlabel(manager->rtnl, &m, RTM_NEWADDRLABEL, 0, AF_INET6);
200 if (r < 0)
201 return r;
202
203 r = address_label_fill_message(label, m);
204 if (r < 0)
205 return r;
206
207 return request_call_netlink_async(manager->rtnl, m, req);
208 }
209
210 static int link_address_label_process_request(Request *req, Link *link, void *userdata) {
211 AddressLabel *label = ASSERT_PTR(userdata);
212 int r;
213
214 assert(req);
215 assert(link);
216 assert(link->manager);
217
218 if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ false))
219 return 0;
220
221 if (!link->manager->static_address_labels_configured)
222 return 0;
223
224 r = link_address_label_configure(label, link, req);
225 if (r < 0)
226 return log_link_warning_errno(link, r, "Failed to configure address label: %m");
227
228 return 1;
229 }
230
231 static int manager_address_label_process_request(Request *req, Link *link, void *userdata) {
232 AddressLabel *label = ASSERT_PTR(userdata);
233 int r;
234
235 assert(req);
236 assert(req->manager);
237 assert(!link);
238
239 r = manager_address_label_configure(label, req->manager, req);
240 if (r < 0)
241 return log_warning_errno(r, "Failed to configure address label: %m");
242
243 return 1;
244 }
245
246 int link_request_static_address_labels(Link *link) {
247 AddressLabel *label;
248 int r;
249
250 assert(link);
251 assert(link->network);
252
253 link->static_address_labels_configured = false;
254
255 HASHMAP_FOREACH(label, link->network->address_labels_by_section) {
256 r = link_queue_request_full(link, REQUEST_TYPE_ADDRESS_LABEL,
257 label, NULL, trivial_hash_func, trivial_compare_func,
258 link_address_label_process_request,
259 &link->static_address_label_messages,
260 link_address_label_configure_handler, NULL);
261 if (r < 0)
262 return log_link_warning_errno(link, r, "Failed to request address label: %m");
263 }
264
265 if (link->static_address_label_messages == 0) {
266 link->static_address_labels_configured = true;
267 link_check_ready(link);
268 } else {
269 log_link_debug(link, "Setting address labels.");
270 link_set_state(link, LINK_STATE_CONFIGURING);
271 }
272
273 return 0;
274 }
275
276 int manager_request_static_address_labels(Manager *manager) {
277 AddressLabel *label;
278 int r;
279
280 assert(manager);
281
282 manager->static_address_labels_configured = false;
283
284 HASHMAP_FOREACH(label, manager->address_labels_by_section) {
285 r = manager_queue_request_full(manager, REQUEST_TYPE_ADDRESS_LABEL,
286 label, NULL, trivial_hash_func, trivial_compare_func,
287 manager_address_label_process_request,
288 &manager->static_address_label_messages,
289 manager_address_label_configure_handler, NULL);
290 if (r < 0)
291 return log_warning_errno(r, "Failed to request address label: %m");
292 }
293
294 if (manager->static_address_label_messages == 0)
295 manager->static_address_labels_configured = true;
296 else
297 log_debug("Setting address labels.");
298
299 return 0;
300 }
301
302 #define log_label_section(label, fmt, ...) \
303 ({ \
304 const AddressLabel *_label = (label); \
305 log_section_warning_errno( \
306 _label ? _label->section : NULL, \
307 SYNTHETIC_ERRNO(EINVAL), \
308 fmt " Ignoring [IPv6AddressLabel] section.", \
309 ##__VA_ARGS__); \
310 })
311
312 static int address_label_section_verify(AddressLabel *label) {
313 assert(label);
314 assert(label->section);
315
316 if (section_is_invalid(label->section))
317 return -EINVAL;
318
319 if (!label->prefix_set)
320 return log_label_section(label, "[IPv6AddressLabel] section without Prefix= setting specified.");
321
322 if (label->label == UINT32_MAX)
323 return log_label_section(label, "[IPv6AddressLabel] section without Label= setting specified.");
324
325 return 0;
326 }
327
328 static void drop_invalid_address_labels(Hashmap *address_labels_by_section) {
329 AddressLabel *label;
330
331 HASHMAP_FOREACH(label, address_labels_by_section)
332 if (address_label_section_verify(label) < 0)
333 address_label_free(label);
334 }
335
336 void network_drop_invalid_address_labels(Network *network) {
337 assert(network);
338 drop_invalid_address_labels(network->address_labels_by_section);
339 }
340
341 void manager_drop_invalid_address_labels(Manager *manager) {
342 assert(manager);
343 drop_invalid_address_labels(manager->address_labels_by_section);
344 }
345
346 static int config_parse_ipv6_address_label_prefix(
347 const char *unit,
348 const char *filename,
349 unsigned line,
350 const char *section,
351 unsigned section_line,
352 const char *lvalue,
353 int ltype,
354 const char *rvalue,
355 void *data,
356 void *userdata) {
357
358 AddressLabel *label = ASSERT_PTR(userdata);
359 unsigned char prefixlen;
360 union in_addr_union a;
361 int r;
362
363 if (isempty(rvalue)) {
364 label->prefix_set = false;
365 return 1;
366 }
367
368 r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &prefixlen);
369 if (r < 0)
370 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
371
372 if (in6_addr_is_ipv4_mapped_address(&a.in6) && prefixlen > 96) {
373 /* See ip6addrlbl_alloc() in net/ipv6/addrlabel.c of kernel. */
374 log_syntax(unit, LOG_WARNING, filename, line, 0,
375 "The prefix length of IPv4 mapped address for address label must be equal to or smaller than 96, "
376 "ignoring assignment: %s", rvalue);
377 return 0;
378 }
379
380 label->prefix = a.in6;
381 label->prefixlen = prefixlen;
382 label->prefix_set = true;
383 return 1;
384 }
385
386 static int config_parse_ipv6_address_label(
387 const char *unit,
388 const char *filename,
389 unsigned line,
390 const char *section,
391 unsigned section_line,
392 const char *lvalue,
393 int ltype,
394 const char *rvalue,
395 void *data,
396 void *userdata) {
397
398 uint32_t k, *label = ASSERT_PTR(data);
399 int r;
400
401 if (isempty(rvalue)) {
402 *label = UINT32_MAX;
403 return 1;
404 }
405
406 r = safe_atou32(rvalue, &k);
407 if (r < 0)
408 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
409
410 if (k == UINT32_MAX) {
411 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address label is invalid, ignoring: %s", rvalue);
412 return 0;
413 }
414
415 *label = k;
416 return 1;
417 }
418
419 int config_parse_ipv6_address_label_section(
420 const char *unit,
421 const char *filename,
422 unsigned line,
423 const char *section,
424 unsigned section_line,
425 const char *lvalue,
426 int ltype,
427 const char *rvalue,
428 void *data,
429 void *userdata) {
430
431 static const ConfigSectionParser table[_IPV6_ADDRESS_LABEL_CONF_PARSER_MAX] = {
432 [IPV6_ADDRESS_LABEL] = { .parser = config_parse_ipv6_address_label, .ltype = 0, .offset = offsetof(AddressLabel, label), },
433 [IPV6_ADDRESS_LABEL_PREFIX] = { .parser = config_parse_ipv6_address_label_prefix, .ltype = 0, .offset = 0, },
434 };
435
436 _cleanup_(address_label_free_or_set_invalidp) AddressLabel *label = NULL;
437 Manager *manager = NULL;
438 Network *network = NULL;
439 int r;
440
441 assert(filename);
442
443 if (FLAGS_SET(ltype, IPV6_ADDRESS_LABEL_BY_MANAGER))
444 manager = ASSERT_PTR(userdata);
445 else
446 network = ASSERT_PTR(userdata);
447
448 ltype &= IPV6_ADDRESS_LABEL_SECTION_MASK;
449
450 r = address_label_new_static(manager, network, filename, section_line, &label);
451 if (r < 0)
452 return log_oom();
453
454 r = config_section_parse(table, ELEMENTSOF(table),
455 unit, filename, line, section, section_line, lvalue, ltype, rvalue, label);
456 if (r <= 0)
457 return r;
458
459 TAKE_PTR(label);
460 return 0;
461 }