networkd-util.c revision 85e070c2b3f66d838c11f8f008106b9e42c9afaf
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/***
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering This file is part of systemd.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Copyright 2013 Tom Gundersen <teg@jklm.no>
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is free software; you can redistribute it and/or modify it
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering under the terms of the GNU Lesser General Public License as published by
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (at your option) any later version.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is distributed in the hope that it will be useful, but
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Lesser General Public License for more details.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering You should have received a copy of the GNU Lesser General Public License
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering***/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "conf-parser.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "networkd-util.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "parse-util.h"
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner#include "string-table.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "string-util.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "util.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringconst char *address_family_boolean_to_string(AddressFamilyBoolean b) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (b == ADDRESS_FAMILY_YES ||
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering b == ADDRESS_FAMILY_NO)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers return yes_no(b == ADDRESS_FAMILY_YES);
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
0732ef7acf37473847992888bcb6446726d9d877Zbigniew Jędrzejewski-Szmek if (b == ADDRESS_FAMILY_IPV4)
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar return "ipv4";
7568345034f2890af745747783c5abfbf6eccf0fLennart Poettering if (b == ADDRESS_FAMILY_IPV6)
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering return "ipv6";
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering return NULL;
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering}
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart PoetteringAddressFamilyBoolean address_family_boolean_from_string(const char *s) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int r;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Make this a true superset of a boolean */
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace r = parse_boolean(s);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (r > 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return ADDRESS_FAMILY_YES;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (r == 0)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return ADDRESS_FAMILY_NO;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering if (streq(s, "ipv4"))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return ADDRESS_FAMILY_IPV4;
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyer if (streq(s, "ipv6"))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return ADDRESS_FAMILY_IPV6;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return _ADDRESS_FAMILY_BOOLEAN_INVALID;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringDEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint config_parse_address_family_boolean_with_kernel(
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering const char* unit,
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering const char *filename,
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering unsigned line,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *section,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering unsigned section_line,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *lvalue,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering int ltype,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering const char *rvalue,
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann void *data,
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann void *userdata) {
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann AddressFamilyBoolean *fwd = data, s;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann assert(filename);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(lvalue);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(rvalue);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann assert(data);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann s = address_family_boolean_from_string(rvalue);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann if (s < 0) {
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann if (streq(rvalue, "kernel"))
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann s = _ADDRESS_FAMILY_BOOLEAN_INVALID;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann else {
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IPForward= option, ignoring: %s", rvalue);
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann return 0;
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann }
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann }
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann
e7e55dbdc38f929805ab2407fbd50886043a9e7cDavid Herrmann *fwd = s;
ff9b60f38bf68eba4a47cabff14547d92e083214Torstein Husebø
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar return 0;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar}
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletarstatic const char* const resolve_support_table[_RESOLVE_SUPPORT_MAX] = {
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar [RESOLVE_SUPPORT_NO] = "no",
75f86906c52735c98dc0aa7e24b773edb42ee814Lennart Poettering [RESOLVE_SUPPORT_YES] = "yes",
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar [RESOLVE_SUPPORT_RESOLVE] = "resolve",
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar};
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal SekletarDEFINE_STRING_TABLE_LOOKUP(resolve_support, ResolveSupport);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletarint config_parse_resolve(
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar const char* unit,
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar const char *filename,
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar unsigned line,
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar const char *section,
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar unsigned section_line,
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar const char *lvalue,
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar int ltype,
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar const char *rvalue,
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar void *data,
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar void *userdata) {
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar ResolveSupport *resolve = data;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar int k;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt assert(filename);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar assert(lvalue);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar assert(rvalue);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar assert(resolve);
63229aa1abdb98aa69fda9819ed2f40c8082762bLennart Poettering
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar /* Our enum shall be a superset of booleans, hence first try
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar * to parse as boolean, and then as enum */
b344bcbbfda8fbe14dadc5aa4b5dfb3ced6d76e2Lennart Poettering
ff49bc3212cb07d850dcfd59940539773a0be26fMichal Schmidt k = parse_boolean(rvalue);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar if (k > 0)
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar *resolve = RESOLVE_SUPPORT_YES;
b344bcbbfda8fbe14dadc5aa4b5dfb3ced6d76e2Lennart Poettering else if (k == 0)
ff49bc3212cb07d850dcfd59940539773a0be26fMichal Schmidt *resolve = RESOLVE_SUPPORT_NO;
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar else {
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar ResolveSupport s;
63229aa1abdb98aa69fda9819ed2f40c8082762bLennart Poettering
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar s = resolve_support_from_string(rvalue);
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar if (s < 0){
a34286684ebb78dd3db0d7f34feb2c121c9d00ccMichal Sekletar log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse %s= option, ignoring: %s", lvalue, rvalue);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering *resolve = s;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering