]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-util.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / network / networkd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2013 Tom Gundersen <teg@jklm.no>
4 ***/
5
6 #include "condition.h"
7 #include "conf-parser.h"
8 #include "networkd-util.h"
9 #include "parse-util.h"
10 #include "string-table.h"
11 #include "string-util.h"
12 #include "util.h"
13
14 const char *address_family_boolean_to_string(AddressFamilyBoolean b) {
15 if (IN_SET(b, ADDRESS_FAMILY_YES, ADDRESS_FAMILY_NO))
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
26 AddressFamilyBoolean 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
45 DEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");
46
47 int 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
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
74 s = address_family_boolean_from_string(rvalue);
75 if (s < 0) {
76 if (streq(rvalue, "kernel"))
77 s = ADDRESS_FAMILY_NO;
78 else {
79 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue);
80 return 0;
81 }
82 }
83
84 *fwd = s;
85
86 return 0;
87 }
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 */
91 int 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 }