Debian Codegraph @

src/network/networkd-route-metric.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-netlink.h"
4
5 #include "alloc-util.h"
6 #include "conf-parser.h"
7 #include "log.h"
8 #include "netlink-util.h"
9 #include "networkd-route.h"
10 #include "networkd-route-metric.h"
11 #include "parse-util.h"
12 #include "siphash24.h"
13 #include "string-util.h"
14
15 void route_metric_done(RouteMetric *metric) {
16 assert(metric);
17
18 free(metric->metrics);
19 free(metric->metrics_set);
20 free(metric->tcp_congestion_control_algo);
21 }
22
23 int route_metric_copy(const RouteMetric *src, RouteMetric *dest) {
24 assert(src);
25 assert(dest);
26
27 dest->n_metrics = src->n_metrics;
28 if (src->n_metrics > 0) {
29 assert(src->n_metrics != 1);
30
31 dest->metrics = newdup(uint32_t, src->metrics, src->n_metrics);
32 if (!dest->metrics)
33 return -ENOMEM;
34 } else
35 dest->metrics = NULL;
36
37 dest->n_metrics_set = src->n_metrics_set;
38 if (src->n_metrics_set > 0) {
39 assert(src->n_metrics_set != 1);
40
41 dest->metrics_set = newdup(bool, src->metrics_set, src->n_metrics_set);
42 if (!dest->metrics_set)
43 return -ENOMEM;
44 } else
45 dest->metrics_set = NULL;
46
47 return strdup_to(&dest->tcp_congestion_control_algo, src->tcp_congestion_control_algo);
48 }
49
50 void route_metric_hash_func(const RouteMetric *metric, struct siphash *state) {
51 assert(metric);
52
53 siphash24_compress_typesafe(metric->n_metrics, state);
54 siphash24_compress_safe(metric->metrics, sizeof(uint32_t) * metric->n_metrics, state);
55 siphash24_compress_string(metric->tcp_congestion_control_algo, state);
56 }
57
58 int route_metric_compare_func(const RouteMetric *a, const RouteMetric *b) {
59 int r;
60
61 assert(a);
62 assert(b);
63
64 r = memcmp_nn(a->metrics, a->n_metrics * sizeof(uint32_t), b->metrics, b->n_metrics * sizeof(uint32_t));
65 if (r != 0)
66 return r;
67
68 return strcmp_ptr(a->tcp_congestion_control_algo, b->tcp_congestion_control_algo);
69 }
70
71 bool route_metric_can_update(const RouteMetric *a, const RouteMetric *b, bool expiration_by_kernel) {
72 assert(a);
73 assert(b);
74
75 /* If the kernel has expiration timer for the route, then only MTU can be updated. */
76
77 if (!expiration_by_kernel)
78 return route_metric_compare_func(a, b) == 0;
79
80 if (a->n_metrics != b->n_metrics)
81 return false;
82
83 if (a->n_metrics > RTAX_MTU && a->metrics[RTAX_MTU] != b->metrics[RTAX_MTU])
84 return false;
85
86 return streq_ptr(a->tcp_congestion_control_algo, b->tcp_congestion_control_algo);
87 }
88
89 int route_metric_set_full(RouteMetric *metric, uint16_t attr, uint32_t value, bool force) {
90 assert(metric);
91
92 if (force) {
93 if (!GREEDY_REALLOC0(metric->metrics_set, attr + 1))
94 return -ENOMEM;
95
96 metric->metrics_set[attr] = true;
97 metric->n_metrics_set = MAX(metric->n_metrics_set, (size_t) (attr + 1));
98 } else {
99 /* Do not override the values specified in conf parsers. */
100 if (metric->n_metrics_set > attr && metric->metrics_set[attr])
101 return 0;
102 }
103
104 if (value != 0) {
105 if (!GREEDY_REALLOC0(metric->metrics, attr + 1))
106 return -ENOMEM;
107
108 metric->metrics[attr] = value;
109 metric->n_metrics = MAX(metric->n_metrics, (size_t) (attr + 1));
110 return 0;
111 }
112
113 if (metric->n_metrics <= attr)
114 return 0;
115
116 metric->metrics[attr] = 0;
117
118 for (size_t i = metric->n_metrics; i > 0; i--)
119 if (metric->metrics[i-1] != 0) {
120 metric->n_metrics = i;
121 return 0;
122 }
123
124 metric->n_metrics = 0;
125 return 0;
126 }
127
128 static void route_metric_unset(RouteMetric *metric, uint16_t attr) {
129 assert(metric);
130
131 if (metric->n_metrics_set > attr)
132 metric->metrics_set[attr] = false;
133
134 assert_se(route_metric_set_full(metric, attr, 0, /* force= */ false) >= 0);
135 }
136
137 uint32_t route_metric_get(const RouteMetric *metric, uint16_t attr) {
138 assert(metric);
139
140 if (metric->n_metrics <= attr)
141 return 0;
142
143 return metric->metrics[attr];
144 }
145
146 int route_metric_set_netlink_message(const RouteMetric *metric, sd_netlink_message *m) {
147 int r;
148
149 assert(metric);
150 assert(m);
151
152 if (metric->n_metrics <= 0 && isempty(metric->tcp_congestion_control_algo))
153 return 0;
154
155 r = sd_netlink_message_open_container(m, RTA_METRICS);
156 if (r < 0)
157 return r;
158
159 for (size_t i = 0; i < metric->n_metrics; i++) {
160 if (i == RTAX_CC_ALGO)
161 continue;
162
163 if (metric->metrics[i] == 0)
164 continue;
165
166 r = sd_netlink_message_append_u32(m, i, metric->metrics[i]);
167 if (r < 0)
168 return r;
169 }
170
171 if (!isempty(metric->tcp_congestion_control_algo)) {
172 r = sd_netlink_message_append_string(m, RTAX_CC_ALGO, metric->tcp_congestion_control_algo);
173 if (r < 0)
174 return r;
175 }
176
177 r = sd_netlink_message_close_container(m);
178 if (r < 0)
179 return r;
180
181 return 0;
182 }
183
184 int route_metric_read_netlink_message(RouteMetric *metric, sd_netlink_message *m) {
185 _cleanup_free_ void *data = NULL;
186 size_t len;
187 int r;
188
189 assert(metric);
190 assert(m);
191
192 r = sd_netlink_message_read_data(m, RTA_METRICS, &len, &data);
193 if (r == -ENODATA)
194 return 0;
195 if (r < 0)
196 return log_warning_errno(r, "rtnl: Could not read RTA_METRICS attribute, ignoring: %m");
197
198 for (struct rtattr *rta = data; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
199 size_t rta_type = RTA_TYPE(rta);
200
201 if (rta_type == RTAX_CC_ALGO) {
202 char *p = memdup_suffix0(RTA_DATA(rta), RTA_PAYLOAD(rta));
203 if (!p)
204 return log_oom();
205
206 free_and_replace(metric->tcp_congestion_control_algo, p);
207
208 } else {
209 if (RTA_PAYLOAD(rta) != sizeof(uint32_t))
210 continue;
211
212 r = route_metric_set(metric, rta_type, *(uint32_t*) RTA_DATA(rta));
213 if (r < 0)
214 return log_oom();
215 }
216 }
217
218 return 0;
219 }
220
221 static int config_parse_route_metric_advmss(
222 const char *unit,
223 const char *filename,
224 unsigned line,
225 const char *section,
226 unsigned section_line,
227 const char *lvalue,
228 int ltype,
229 const char *rvalue,
230 void *data,
231 void *userdata) {
232
233 uint32_t *val = ASSERT_PTR(data);
234 uint64_t u;
235 int r;
236
237 assert(rvalue);
238
239 r = parse_size(rvalue, 1024, &u);
240 if (r < 0)
241 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
242
243 if (u == 0 || u > UINT32_MAX) {
244 log_syntax(unit, LOG_WARNING, filename, line, 0,
245 "Invalid %s=, ignoring assignment: %s", lvalue, rvalue);
246 return 0;
247 }
248
249 *val = (uint32_t) u;
250 return 1;
251 }
252
253 static int config_parse_route_metric_hop_limit(
254 const char *unit,
255 const char *filename,
256 unsigned line,
257 const char *section,
258 unsigned section_line,
259 const char *lvalue,
260 int ltype,
261 const char *rvalue,
262 void *data,
263 void *userdata) {
264
265 uint32_t k, *val = ASSERT_PTR(data);
266 int r;
267
268 assert(rvalue);
269
270 r = safe_atou32(rvalue, &k);
271 if (r < 0)
272 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
273
274 if (k == 0 || k > 255) {
275 log_syntax(unit, LOG_WARNING, filename, line, 0,
276 "Invalid %s=, ignoring assignment: %s", lvalue, rvalue);
277 return 0;
278 }
279
280 *val = k;
281 return 1;
282 }
283
284 int config_parse_tcp_window(
285 const char *unit,
286 const char *filename,
287 unsigned line,
288 const char *section,
289 unsigned section_line,
290 const char *lvalue,
291 int ltype,
292 const char *rvalue,
293 void *data,
294 void *userdata) {
295
296 uint32_t k, *val = ASSERT_PTR(data);
297 int r;
298
299 assert(rvalue);
300
301 r = safe_atou32(rvalue, &k);
302 if (r < 0)
303 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
304
305 if (k == 0 || k >= 1024) {
306 log_syntax(unit, LOG_WARNING, filename, line, 0,
307 "Invalid %s=, ignoring assignment: %s", lvalue, rvalue);
308 return 0;
309 }
310
311 *val = k;
312 return 1;
313 }
314
315 static int config_parse_route_metric_tcp_rto(
316 const char *unit,
317 const char *filename,
318 unsigned line,
319 const char *section,
320 unsigned section_line,
321 const char *lvalue,
322 int ltype,
323 const char *rvalue,
324 void *data,
325 void *userdata) {
326
327 uint32_t *val = ASSERT_PTR(data);
328 usec_t usec;
329 int r;
330
331 assert(rvalue);
332
333 r = parse_sec(rvalue, &usec);
334 if (r < 0)
335 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
336
337 if (!timestamp_is_set(usec) ||
338 DIV_ROUND_UP(usec, USEC_PER_MSEC) > UINT32_MAX) {
339 log_syntax(unit, LOG_WARNING, filename, line, 0,
340 "Invalid %s=, ignoring assignment: %s", lvalue, rvalue);
341 return 0;
342 }
343
344 *val = (uint32_t) DIV_ROUND_UP(usec, USEC_PER_MSEC);
345 return 1;
346 }
347
348 static int config_parse_route_metric_boolean(
349 const char *unit,
350 const char *filename,
351 unsigned line,
352 const char *section,
353 unsigned section_line,
354 const char *lvalue,
355 int ltype,
356 const char *rvalue,
357 void *data,
358 void *userdata) {
359
360 uint32_t *val = ASSERT_PTR(data);
361 int r;
362
363 assert(rvalue);
364
365 r = parse_boolean(rvalue);
366 if (r < 0)
367 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
368
369 *val = r;
370 return 1;
371 }
372
373 int config_parse_route_metric(
374 const char *unit,
375 const char *filename,
376 unsigned line,
377 const char *section,
378 unsigned section_line,
379 const char *lvalue,
380 int ltype,
381 const char *rvalue,
382 void *data,
383 void *userdata) {
384
385 static const ConfigSectionParser table[__RTAX_MAX] = {
386 [RTAX_MTU] = { .parser = config_parse_mtu, .ltype = 0, .offset = 0, },
387 [RTAX_ADVMSS] = { .parser = config_parse_route_metric_advmss, .ltype = 0, .offset = 0, },
388 [RTAX_HOPLIMIT] = { .parser = config_parse_route_metric_hop_limit, .ltype = 0, .offset = 0, },
389 [RTAX_INITCWND] = { .parser = config_parse_tcp_window, .ltype = 0, .offset = 0, },
390 [RTAX_RTO_MIN] = { .parser = config_parse_route_metric_tcp_rto, .ltype = 0, .offset = 0, },
391 [RTAX_INITRWND] = { .parser = config_parse_tcp_window, .ltype = 0, .offset = 0, },
392 [RTAX_QUICKACK] = { .parser = config_parse_route_metric_boolean, .ltype = 0, .offset = 0, },
393 [RTAX_FASTOPEN_NO_COOKIE] = { .parser = config_parse_route_metric_boolean, .ltype = 0, .offset = 0, },
394 };
395
396 Route *route = ASSERT_PTR(userdata);
397 int r;
398
399 if (isempty(rvalue)) {
400 route_metric_unset(&route->metric, ltype);
401 TAKE_PTR(route);
402 return 1;
403 }
404
405 uint32_t k;
406 r = config_section_parse(table, ELEMENTSOF(table),
407 unit, filename, line, section, section_line, lvalue, ltype, rvalue, &k);
408 if (r <= 0)
409 return r;
410
411 if (route_metric_set_full(
412 &route->metric,
413 ltype,
414 k,
415 /* force= */ true) < 0)
416 return log_oom();
417
418 return 1;
419 }