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