]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-util.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / network / networkd-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
fc2f9534 2/***
96b2fb93 3 Copyright © 2013 Tom Gundersen <teg@jklm.no>
fc2f9534
LP
4***/
5
f02ba163 6#include "condition.h"
fc2f9534 7#include "conf-parser.h"
fc2f9534 8#include "networkd-util.h"
6bedfcbb 9#include "parse-util.h"
8b43440b 10#include "string-table.h"
07630cea
LP
11#include "string-util.h"
12#include "util.h"
fc2f9534
LP
13
14const char *address_family_boolean_to_string(AddressFamilyBoolean b) {
3742095b 15 if (IN_SET(b, ADDRESS_FAMILY_YES, ADDRESS_FAMILY_NO))
fc2f9534
LP
16 return yes_no(b == ADDRESS_FAMILY_YES);
17
18 if (b == ADDRESS_FAMILY_IPV4)
19 return "ipv4";
20 if (b == ADDRESS_FAMILY_IPV6)
21 return "ipv6";
22
23 return NULL;
24}
25
26AddressFamilyBoolean address_family_boolean_from_string(const char *s) {
27 int r;
28
29 /* Make this a true superset of a boolean */
30
31 r = parse_boolean(s);
32 if (r > 0)
33 return ADDRESS_FAMILY_YES;
34 if (r == 0)
35 return ADDRESS_FAMILY_NO;
36
37 if (streq(s, "ipv4"))
38 return ADDRESS_FAMILY_IPV4;
39 if (streq(s, "ipv6"))
40 return ADDRESS_FAMILY_IPV6;
41
42 return _ADDRESS_FAMILY_BOOLEAN_INVALID;
43}
44
45DEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");
46
47int config_parse_address_family_boolean_with_kernel(
48 const char* unit,
49 const char *filename,
50 unsigned line,
51 const char *section,
52 unsigned section_line,
53 const char *lvalue,
54 int ltype,
55 const char *rvalue,
56 void *data,
57 void *userdata) {
58
59 AddressFamilyBoolean *fwd = data, s;
60
61 assert(filename);
62 assert(lvalue);
63 assert(rvalue);
64 assert(data);
65
765afd5c
LP
66 /* This function is mostly obsolete now. It simply redirects
67 * "kernel" to "no". In older networkd versions we used to
68 * distuingish IPForward=off from IPForward=kernel, where the
69 * former would explicitly turn off forwarding while the
70 * latter would simply not touch the setting. But that logic
71 * is gone, hence silently accept the old setting, but turn it
72 * to "no". */
73
fc2f9534
LP
74 s = address_family_boolean_from_string(rvalue);
75 if (s < 0) {
76 if (streq(rvalue, "kernel"))
765afd5c 77 s = ADDRESS_FAMILY_NO;
fc2f9534 78 else {
85e070c2 79 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue);
fc2f9534
LP
80 return 0;
81 }
82 }
83
84 *fwd = s;
85
86 return 0;
87}
f02ba163
DD
88
89/* Router lifetime can be set with netlink interface since kernel >= 4.5
90 * so for the supported kernel we dont need to expire routes in userspace */
91int kernel_route_expiration_supported(void) {
92 static int cached = -1;
93 int r;
94
95 if (cached < 0) {
96 Condition c = {
97 .type = CONDITION_KERNEL_VERSION,
98 .parameter = (char *) ">= 4.5"
99 };
100 r = condition_test(&c);
101 if (r < 0)
102 return r;
103
104 cached = r;
105 }
106 return cached;
107}