Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 17 | #include <dirent.h> |
Dan Albert | aa1be2b | 2015-01-06 09:36:17 -0800 | [diff] [blame] | 18 | #include <errno.h> |
Elliott Hughes | 5f4938f | 2015-01-28 11:22:38 -0800 | [diff] [blame] | 19 | #include <malloc.h> |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 20 | #include <net/if.h> |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 21 | #include <sys/socket.h> |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 22 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 23 | #include <functional> |
| 24 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 25 | #define LOG_TAG "InterfaceController" |
Elliott Hughes | bbd5626 | 2015-12-04 15:45:10 -0800 | [diff] [blame] | 26 | #include <android-base/file.h> |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 27 | #include <android-base/properties.h> |
Elliott Hughes | bbd5626 | 2015-12-04 15:45:10 -0800 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 30 | #include <log/log.h> |
Lorenzo Colitti | 0ea8ff8 | 2014-10-28 00:15:07 +0900 | [diff] [blame] | 31 | #include <logwrap/logwrap.h> |
Erik Kline | c296f09 | 2016-08-02 15:22:53 +0900 | [diff] [blame] | 32 | #include <netutils/ifc.h> |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 33 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 34 | #include <android/net/INetd.h> |
| 35 | #include <netdutils/Misc.h> |
| 36 | #include <netdutils/Slice.h> |
| 37 | #include <netdutils/Syscalls.h> |
| 38 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 39 | #include "InterfaceController.h" |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 40 | #include "RouteController.h" |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 41 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 42 | using android::base::ReadFileToString; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 43 | using android::base::StringPrintf; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 44 | using android::base::Trim; |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 45 | using android::base::WriteStringToFile; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 46 | using android::net::INetd; |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 47 | using android::net::RouteController; |
Nathan Harold | 172f8e4 | 2018-04-19 13:10:19 -0700 | [diff] [blame] | 48 | using android::netdutils::isOk; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 49 | using android::netdutils::makeSlice; |
| 50 | using android::netdutils::sSyscalls; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 51 | using android::netdutils::Status; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 52 | using android::netdutils::statusFromErrno; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 53 | using android::netdutils::StatusOr; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 54 | using android::netdutils::toString; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 55 | using android::netdutils::status::ok; |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 56 | |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 57 | namespace { |
| 58 | |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 59 | const char ipv4_proc_path[] = "/proc/sys/net/ipv4/conf"; |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 60 | const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf"; |
| 61 | |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 62 | const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh"; |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 63 | const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh"; |
| 64 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 65 | const char proc_net_path[] = "/proc/sys/net"; |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 66 | const char sys_net_path[] = "/sys/class/net"; |
| 67 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 68 | constexpr int kRouteInfoMinPrefixLen = 48; |
| 69 | |
| 70 | // RFC 7421 prefix length. |
| 71 | constexpr int kRouteInfoMaxPrefixLen = 64; |
| 72 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 73 | // Property used to persist RFC 7217 stable secret. Protected by SELinux policy. |
| 74 | const char kStableSecretProperty[] = "persist.netd.stable_secret"; |
| 75 | |
| 76 | // RFC 7217 stable secret on linux is formatted as an IPv6 address. |
| 77 | // This function uses 128 bits of high quality entropy to generate an |
| 78 | // address for this purpose. This function should be not be called |
| 79 | // frequently. |
| 80 | StatusOr<std::string> randomIPv6Address() { |
| 81 | in6_addr addr = {}; |
| 82 | const auto& sys = sSyscalls.get(); |
| 83 | ASSIGN_OR_RETURN(auto fd, sys.open("/dev/random", O_RDONLY)); |
| 84 | RETURN_IF_NOT_OK(sys.read(fd, makeSlice(addr))); |
| 85 | return toString(addr); |
| 86 | } |
| 87 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 88 | inline bool isNormalPathComponent(const char *component) { |
| 89 | return (strcmp(component, ".") != 0) && |
| 90 | (strcmp(component, "..") != 0) && |
| 91 | (strchr(component, '/') == nullptr); |
| 92 | } |
| 93 | |
| 94 | inline bool isAddressFamilyPathComponent(const char *component) { |
| 95 | return strcmp(component, "ipv4") == 0 || strcmp(component, "ipv6") == 0; |
| 96 | } |
| 97 | |
| 98 | inline bool isInterfaceName(const char *name) { |
| 99 | return isNormalPathComponent(name) && |
| 100 | (strcmp(name, "default") != 0) && |
| 101 | (strcmp(name, "all") != 0); |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int writeValueToPath( |
| 105 | const char* dirname, const char* subdirname, const char* basename, |
| 106 | const char* value) { |
| 107 | std::string path(StringPrintf("%s/%s/%s", dirname, subdirname, basename)); |
Erik Kline | 3f95777 | 2015-06-03 17:44:24 +0900 | [diff] [blame] | 108 | return WriteStringToFile(value, path) ? 0 : -1; |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 109 | } |
| 110 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 111 | // Run @fn on each interface as well as 'default' in the path @dirname. |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 112 | void forEachInterface( |
| 113 | const std::string& dirname, |
| 114 | const std::function<void(const std::string& path, const std::string& iface)>& fn) { |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 115 | // Run on default, which controls the behavior of any interfaces that are created in the future. |
| 116 | fn(dirname, "default"); |
| 117 | DIR* dir = opendir(dirname.c_str()); |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 118 | if (!dir) { |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 119 | ALOGE("Can't list %s: %s", dirname.c_str(), strerror(errno)); |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 120 | return; |
| 121 | } |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 122 | while (true) { |
| 123 | const dirent *ent = readdir(dir); |
| 124 | if (!ent) { |
| 125 | break; |
| 126 | } |
| 127 | if ((ent->d_type != DT_DIR) || !isInterfaceName(ent->d_name)) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 128 | continue; |
| 129 | } |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 130 | fn(dirname, ent->d_name); |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 131 | } |
| 132 | closedir(dir); |
| 133 | } |
| 134 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 135 | void setOnAllInterfaces(const char* dirname, const char* basename, const char* value) { |
| 136 | auto fn = [basename, value](const std::string& path, const std::string& iface) { |
| 137 | writeValueToPath(path.c_str(), iface.c_str(), basename, value); |
| 138 | }; |
| 139 | forEachInterface(dirname, fn); |
| 140 | } |
| 141 | |
Erik Kline | 7adf8d7 | 2015-07-28 18:51:01 +0900 | [diff] [blame] | 142 | void setIPv6UseOutgoingInterfaceAddrsOnly(const char *value) { |
| 143 | setOnAllInterfaces(ipv6_proc_path, "use_oif_addrs_only", value); |
| 144 | } |
| 145 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 146 | std::string getParameterPathname( |
| 147 | const char *family, const char *which, const char *interface, const char *parameter) { |
| 148 | if (!isAddressFamilyPathComponent(family)) { |
| 149 | errno = EAFNOSUPPORT; |
| 150 | return ""; |
| 151 | } else if (!isNormalPathComponent(which) || |
| 152 | !isInterfaceName(interface) || |
| 153 | !isNormalPathComponent(parameter)) { |
| 154 | errno = EINVAL; |
| 155 | return ""; |
| 156 | } |
| 157 | |
| 158 | return StringPrintf("%s/%s/%s/%s/%s", proc_net_path, family, which, interface, parameter); |
| 159 | } |
| 160 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 161 | void setAcceptIPv6RIO(int min, int max) { |
| 162 | auto fn = [min, max](const std::string& prefix, const std::string& iface) { |
| 163 | int rv = writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_min_plen", |
| 164 | std::to_string(min).c_str()); |
| 165 | if (rv != 0) { |
| 166 | // Only update max_plen if the write to min_plen succeeded. This ordering will prevent |
| 167 | // RIOs from being accepted unless both min and max are written successfully. |
| 168 | return; |
| 169 | } |
| 170 | writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_max_plen", |
| 171 | std::to_string(max).c_str()); |
| 172 | }; |
| 173 | forEachInterface(ipv6_proc_path, fn); |
| 174 | } |
| 175 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 176 | // Ideally this function would return StatusOr<std::string>, however |
| 177 | // there is no safe value for dflt that will always differ from the |
| 178 | // stored property. Bugs code could conceivably end up persisting the |
| 179 | // reserved value resulting in surprising behavior. |
| 180 | std::string getProperty(const std::string& key, const std::string& dflt) { |
| 181 | return android::base::GetProperty(key, dflt); |
| 182 | }; |
| 183 | |
| 184 | Status setProperty(const std::string& key, const std::string& val) { |
Lorenzo Colitti | 516764f | 2017-07-10 19:13:23 +0900 | [diff] [blame] | 185 | // SetProperty does not dependably set errno to a meaningful value. Use our own error code so |
| 186 | // callers don't get confused. |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 187 | return android::base::SetProperty(key, val) |
| 188 | ? ok |
Lorenzo Colitti | 516764f | 2017-07-10 19:13:23 +0900 | [diff] [blame] | 189 | : statusFromErrno(EREMOTEIO, "SetProperty failed, see libc logs"); |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 193 | } // namespace |
| 194 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 195 | android::netdutils::Status InterfaceController::enableStablePrivacyAddresses( |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 196 | const std::string& iface, |
| 197 | const GetPropertyFn& getProperty, |
| 198 | const SetPropertyFn& setProperty) { |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 199 | const auto& sys = sSyscalls.get(); |
| 200 | const std::string procTarget = std::string(ipv6_proc_path) + "/" + iface + "/stable_secret"; |
| 201 | auto procFd = sys.open(procTarget, O_CLOEXEC | O_WRONLY); |
| 202 | |
| 203 | // Devices with old kernels (typically < 4.4) don't support |
| 204 | // RFC 7217 stable privacy addresses. |
| 205 | if (equalToErrno(procFd, ENOENT)) { |
| 206 | return statusFromErrno(EOPNOTSUPP, |
| 207 | "Failed to open stable_secret. Assuming unsupported kernel version"); |
| 208 | } |
| 209 | |
| 210 | // If stable_secret exists but we can't open it, something strange is going on. |
| 211 | RETURN_IF_NOT_OK(procFd); |
| 212 | |
| 213 | const char kUninitialized[] = "uninitialized"; |
| 214 | const auto oldSecret = getProperty(kStableSecretProperty, kUninitialized); |
| 215 | std::string secret = oldSecret; |
| 216 | |
| 217 | // Generate a new secret if no persistent property existed. |
| 218 | if (oldSecret == kUninitialized) { |
| 219 | ASSIGN_OR_RETURN(secret, randomIPv6Address()); |
| 220 | } |
| 221 | |
| 222 | // Ask the OS to generate SLAAC addresses on iface using secret. |
| 223 | RETURN_IF_NOT_OK(sys.write(procFd.value(), makeSlice(secret))); |
| 224 | |
| 225 | // Don't persist an existing secret. |
| 226 | if (oldSecret != kUninitialized) { |
| 227 | return ok; |
| 228 | } |
| 229 | |
| 230 | return setProperty(kStableSecretProperty, secret); |
| 231 | } |
| 232 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 233 | void InterfaceController::initializeAll() { |
| 234 | // Initial IPv6 settings. |
| 235 | // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces. |
| 236 | // This causes RAs to work or not work based on whether forwarding is on, and causes routes |
| 237 | // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable |
| 238 | // by always setting accept_ra to 2. |
| 239 | setAcceptRA("2"); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 240 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 241 | // Accept RIOs with prefix length in the closed interval [48, 64]. |
| 242 | setAcceptIPv6RIO(kRouteInfoMinPrefixLen, kRouteInfoMaxPrefixLen); |
| 243 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 244 | setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX); |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 245 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 246 | // Enable optimistic DAD for IPv6 addresses on all interfaces. |
| 247 | setIPv6OptimisticMode("1"); |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 248 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 249 | // Reduce the ARP/ND base reachable time from the default (30sec) to 15sec. |
| 250 | setBaseReachableTimeMs(15 * 1000); |
Erik Kline | 7adf8d7 | 2015-07-28 18:51:01 +0900 | [diff] [blame] | 251 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 252 | // When sending traffic via a given interface use only addresses configured |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 253 | // on that interface as possible source addresses. |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 254 | setIPv6UseOutgoingInterfaceAddrsOnly("1"); |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 255 | |
| 256 | // Ensure that ICMP redirects are rejected globally on all interfaces. |
| 257 | disableIcmpRedirects(); |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 258 | } |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 259 | |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 260 | int InterfaceController::setEnableIPv6(const char *interface, const int on) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 261 | if (!isIfaceName(interface)) { |
| 262 | errno = ENOENT; |
| 263 | return -1; |
| 264 | } |
| 265 | // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf. |
| 266 | // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf |
| 267 | // addresses and routes and disables IPv6 on the interface. |
| 268 | const char *disable_ipv6 = on ? "0" : "1"; |
| 269 | return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6); |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 270 | } |
| 271 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 272 | // Changes to addrGenMode will not fully take effect until the next |
| 273 | // time disable_ipv6 transitions from 1 to 0. |
| 274 | Status InterfaceController::setIPv6AddrGenMode(const std::string& interface, int mode) { |
| 275 | if (!isIfaceName(interface)) { |
| 276 | return statusFromErrno(ENOENT, "invalid iface name: " + interface); |
| 277 | } |
| 278 | |
| 279 | switch (mode) { |
| 280 | case INetd::IPV6_ADDR_GEN_MODE_EUI64: |
| 281 | // Ignore return value. If /proc/.../stable_secret is |
| 282 | // missing we're probably in EUI64 mode already. |
| 283 | writeValueToPath(ipv6_proc_path, interface.c_str(), "stable_secret", ""); |
| 284 | break; |
| 285 | case INetd::IPV6_ADDR_GEN_MODE_STABLE_PRIVACY: { |
| 286 | return enableStablePrivacyAddresses(interface, getProperty, setProperty); |
| 287 | } |
| 288 | case INetd::IPV6_ADDR_GEN_MODE_NONE: |
| 289 | case INetd::IPV6_ADDR_GEN_MODE_RANDOM: |
| 290 | default: |
| 291 | return statusFromErrno(EOPNOTSUPP, "unsupported addrGenMode"); |
| 292 | } |
| 293 | |
| 294 | return ok; |
| 295 | } |
| 296 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 297 | int InterfaceController::setAcceptIPv6Ra(const char *interface, const int on) { |
| 298 | if (!isIfaceName(interface)) { |
| 299 | errno = ENOENT; |
| 300 | return -1; |
| 301 | } |
| 302 | // Because forwarding can be enabled even when tethering is off, we always |
| 303 | // use mode "2" (accept RAs, even if forwarding is enabled). |
| 304 | const char *accept_ra = on ? "2" : "0"; |
| 305 | return writeValueToPath(ipv6_proc_path, interface, "accept_ra", accept_ra); |
| 306 | } |
| 307 | |
| 308 | int InterfaceController::setAcceptIPv6Dad(const char *interface, const int on) { |
| 309 | if (!isIfaceName(interface)) { |
| 310 | errno = ENOENT; |
| 311 | return -1; |
| 312 | } |
| 313 | const char *accept_dad = on ? "1" : "0"; |
| 314 | return writeValueToPath(ipv6_proc_path, interface, "accept_dad", accept_dad); |
| 315 | } |
| 316 | |
Erik Kline | 59d8c48 | 2016-08-09 15:28:42 +0900 | [diff] [blame] | 317 | int InterfaceController::setIPv6DadTransmits(const char *interface, const char *value) { |
| 318 | if (!isIfaceName(interface)) { |
| 319 | errno = ENOENT; |
| 320 | return -1; |
| 321 | } |
| 322 | return writeValueToPath(ipv6_proc_path, interface, "dad_transmits", value); |
| 323 | } |
| 324 | |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 325 | int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 326 | if (!isIfaceName(interface)) { |
| 327 | errno = ENOENT; |
| 328 | return -1; |
| 329 | } |
| 330 | // 0: disable IPv6 privacy addresses |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 331 | // 2: enable IPv6 privacy addresses and prefer them over non-privacy ones. |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 332 | return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0"); |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 333 | } |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 334 | |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 335 | void InterfaceController::setAcceptRA(const char *value) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 336 | setOnAllInterfaces(ipv6_proc_path, "accept_ra", value); |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 339 | // |tableOrOffset| is interpreted as: |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 340 | // If == 0: default. Routes go into RT6_TABLE_MAIN. |
| 341 | // If > 0: user set. Routes go into the specified table. |
| 342 | // If < 0: automatic. The absolute value is intepreted as an offset and added to the interface |
| 343 | // ID to get the table. If it's set to -1000, routes from interface ID 5 will go into |
| 344 | // table 1005, etc. |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 345 | void InterfaceController::setAcceptRARouteTable(int tableOrOffset) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 346 | std::string value(StringPrintf("%d", tableOrOffset)); |
| 347 | setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str()); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 348 | } |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 349 | |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 350 | int InterfaceController::setMtu(const char *interface, const char *mtu) |
| 351 | { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 352 | if (!isIfaceName(interface)) { |
| 353 | errno = ENOENT; |
| 354 | return -1; |
| 355 | } |
| 356 | return writeValueToPath(sys_net_path, interface, "mtu", mtu); |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 357 | } |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 358 | |
Erik Kline | c296f09 | 2016-08-02 15:22:53 +0900 | [diff] [blame] | 359 | int InterfaceController::addAddress(const char *interface, |
| 360 | const char *addrString, int prefixLength) { |
| 361 | return ifc_add_address(interface, addrString, prefixLength); |
| 362 | } |
| 363 | |
| 364 | int InterfaceController::delAddress(const char *interface, |
| 365 | const char *addrString, int prefixLength) { |
| 366 | return ifc_del_address(interface, addrString, prefixLength); |
| 367 | } |
| 368 | |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 369 | int InterfaceController::disableIcmpRedirects() { |
| 370 | int rv = 0; |
| 371 | rv |= writeValueToPath(ipv4_proc_path, "all", "accept_redirects", "0"); |
| 372 | rv |= writeValueToPath(ipv6_proc_path, "all", "accept_redirects", "0"); |
| 373 | setOnAllInterfaces(ipv4_proc_path, "accept_redirects", "0"); |
| 374 | setOnAllInterfaces(ipv6_proc_path, "accept_redirects", "0"); |
| 375 | return rv; |
| 376 | } |
| 377 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 378 | int InterfaceController::getParameter( |
| 379 | const char *family, const char *which, const char *interface, const char *parameter, |
| 380 | std::string *value) { |
| 381 | const std::string path(getParameterPathname(family, which, interface, parameter)); |
| 382 | if (path.empty()) { |
| 383 | return -errno; |
| 384 | } |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 385 | if (ReadFileToString(path, value)) { |
| 386 | *value = Trim(*value); |
| 387 | return 0; |
| 388 | } |
| 389 | return -errno; |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | int InterfaceController::setParameter( |
| 393 | const char *family, const char *which, const char *interface, const char *parameter, |
| 394 | const char *value) { |
| 395 | const std::string path(getParameterPathname(family, which, interface, parameter)); |
| 396 | if (path.empty()) { |
| 397 | return -errno; |
| 398 | } |
| 399 | return WriteStringToFile(value, path) ? 0 : -errno; |
| 400 | } |
| 401 | |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 402 | void InterfaceController::setBaseReachableTimeMs(unsigned int millis) { |
| 403 | std::string value(StringPrintf("%u", millis)); |
| 404 | setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str()); |
| 405 | setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str()); |
| 406 | } |
| 407 | |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 408 | void InterfaceController::setIPv6OptimisticMode(const char *value) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 409 | setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value); |
| 410 | setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value); |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 411 | } |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 412 | |
Nathan Harold | 172f8e4 | 2018-04-19 13:10:19 -0700 | [diff] [blame] | 413 | StatusOr<std::vector<std::string>> InterfaceController::getIfaceNames() { |
| 414 | std::vector<std::string> ifaceNames; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 415 | DIR* d; |
| 416 | struct dirent* de; |
| 417 | |
| 418 | if (!(d = opendir("/sys/class/net"))) { |
| 419 | return statusFromErrno(errno, "Cannot open iface directory"); |
| 420 | } |
| 421 | while ((de = readdir(d))) { |
| 422 | if (de->d_name[0] == '.') continue; |
Nathan Harold | 172f8e4 | 2018-04-19 13:10:19 -0700 | [diff] [blame] | 423 | ifaceNames.push_back(std::string(de->d_name)); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 424 | } |
| 425 | closedir(d); |
Nathan Harold | 172f8e4 | 2018-04-19 13:10:19 -0700 | [diff] [blame] | 426 | return ifaceNames; |
| 427 | } |
| 428 | |
| 429 | StatusOr<std::map<std::string, uint32_t>> InterfaceController::getIfaceList() { |
| 430 | std::map<std::string, uint32_t> ifacePairs; |
| 431 | |
| 432 | ASSIGN_OR_RETURN(auto ifaceNames, getIfaceNames()); |
| 433 | |
| 434 | for (const auto& name : ifaceNames) { |
| 435 | uint32_t ifaceIndex = if_nametoindex(name.c_str()); |
| 436 | if (ifaceIndex) { |
| 437 | ifacePairs.insert(std::pair<std::string, uint32_t>(name, ifaceIndex)); |
| 438 | } |
| 439 | } |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 440 | return ifacePairs; |
| 441 | } |