diff --git a/Makefile.am b/Makefile.am index a6dafe63..275dd1b9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -246,6 +246,7 @@ src_iwd_SOURCES = src/main.c linux/nl80211.h src/iwd.h src/missing.h \ src/diagnostic.h src/diagnostic.c \ src/ip-pool.h src/ip-pool.c \ src/band.h src/band.c \ + src/sysfs.h src/sysfs.c \ $(eap_sources) \ $(builtin_sources) diff --git a/src/sysfs.c b/src/sysfs.c new file mode 100644 index 00000000..d7972fc4 --- /dev/null +++ b/src/sysfs.c @@ -0,0 +1,109 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2021 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include + +#include "src/sysfs.h" + +static int write_string(const char *file, const char *value) +{ + size_t l = strlen(value); + int fd; + int r; + + fd = L_TFR(open(file, O_WRONLY)); + if (fd < 0) + return -errno; + + r = L_TFR(write(fd, value, l)); + L_TFR(close(fd)); + + return r; +} + +static bool sysfs_supports_ip_setting(const char *ipv, const char *ifname, + const char *setting) +{ + struct stat st; + int err; + L_AUTO_FREE_VAR(char *, file) = + l_strdup_printf("/proc/sys/net/%s/conf/%s/%s", ipv, + ifname, setting); + + err = stat(file, &st); + + if (!err && S_ISREG(st.st_mode) != 0) + return true; + + return false; +} + +bool sysfs_supports_ipv6_setting(const char *ifname, const char *setting) +{ + return sysfs_supports_ip_setting("ipv6", ifname, setting); +} + +int sysfs_write_ipv6_setting(const char *ifname, const char *setting, + const char *value) +{ + int r; + + L_AUTO_FREE_VAR(char *, file) = + l_strdup_printf("/proc/sys/net/ipv6/conf/%s/%s", + ifname, setting); + + r = write_string(file, value); + if (r < 0) + l_error("Unable to write %s to %s", setting, file); + + return r; +} + +bool sysfs_supports_ipv4_setting(const char *ifname, const char *setting) +{ + return sysfs_supports_ip_setting("ipv4", ifname, setting); +} + +int sysfs_write_ipv4_setting(const char *ifname, const char *setting, + const char *value) +{ + int r; + + L_AUTO_FREE_VAR(char *, file) = + l_strdup_printf("/proc/sys/net/ipv4/conf/%s/%s", + ifname, setting); + + r = write_string(file, value); + if (r < 0) + l_error("Unable to write %s to %s", setting, file); + + return r; +} diff --git a/src/sysfs.h b/src/sysfs.h new file mode 100644 index 00000000..60207c13 --- /dev/null +++ b/src/sysfs.h @@ -0,0 +1,28 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2021 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +bool sysfs_supports_ipv6_setting(const char *ifname, const char *setting); +int sysfs_write_ipv6_setting(const char *ifname, const char *setting, + const char *value); +bool sysfs_supports_ipv4_setting(const char *ifname, const char *setting); +int sysfs_write_ipv4_setting(const char *ifname, const char *setting, + const char *value);