]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-util.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / network / networkd-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
fc2f9534
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
fc2f9534 21#include "conf-parser.h"
fc2f9534 22#include "networkd-util.h"
6bedfcbb 23#include "parse-util.h"
8b43440b 24#include "string-table.h"
07630cea
LP
25#include "string-util.h"
26#include "util.h"
fc2f9534
LP
27
28const char *address_family_boolean_to_string(AddressFamilyBoolean b) {
3742095b 29 if (IN_SET(b, ADDRESS_FAMILY_YES, ADDRESS_FAMILY_NO))
fc2f9534
LP
30 return yes_no(b == ADDRESS_FAMILY_YES);
31
32 if (b == ADDRESS_FAMILY_IPV4)
33 return "ipv4";
34 if (b == ADDRESS_FAMILY_IPV6)
35 return "ipv6";
36
37 return NULL;
38}
39
40AddressFamilyBoolean address_family_boolean_from_string(const char *s) {
41 int r;
42
43 /* Make this a true superset of a boolean */
44
45 r = parse_boolean(s);
46 if (r > 0)
47 return ADDRESS_FAMILY_YES;
48 if (r == 0)
49 return ADDRESS_FAMILY_NO;
50
51 if (streq(s, "ipv4"))
52 return ADDRESS_FAMILY_IPV4;
53 if (streq(s, "ipv6"))
54 return ADDRESS_FAMILY_IPV6;
55
56 return _ADDRESS_FAMILY_BOOLEAN_INVALID;
57}
58
59DEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");
60
61int config_parse_address_family_boolean_with_kernel(
62 const char* unit,
63 const char *filename,
64 unsigned line,
65 const char *section,
66 unsigned section_line,
67 const char *lvalue,
68 int ltype,
69 const char *rvalue,
70 void *data,
71 void *userdata) {
72
73 AddressFamilyBoolean *fwd = data, s;
74
75 assert(filename);
76 assert(lvalue);
77 assert(rvalue);
78 assert(data);
79
765afd5c
LP
80 /* This function is mostly obsolete now. It simply redirects
81 * "kernel" to "no". In older networkd versions we used to
82 * distuingish IPForward=off from IPForward=kernel, where the
83 * former would explicitly turn off forwarding while the
84 * latter would simply not touch the setting. But that logic
85 * is gone, hence silently accept the old setting, but turn it
86 * to "no". */
87
fc2f9534
LP
88 s = address_family_boolean_from_string(rvalue);
89 if (s < 0) {
90 if (streq(rvalue, "kernel"))
765afd5c 91 s = ADDRESS_FAMILY_NO;
fc2f9534 92 else {
85e070c2 93 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue);
fc2f9534
LP
94 return 0;
95 }
96 }
97
98 *fwd = s;
99
100 return 0;
101}