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