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> |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 21 | #include <net/if_arp.h> |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 22 | #include <sys/socket.h> |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 23 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 24 | #include <functional> |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 25 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 26 | #define LOG_TAG "InterfaceController" |
Elliott Hughes | bbd5626 | 2015-12-04 15:45:10 -0800 | [diff] [blame] | 27 | #include <android-base/file.h> |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 28 | #include <android-base/properties.h> |
Elliott Hughes | bbd5626 | 2015-12-04 15:45:10 -0800 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 30 | #include <android-base/strings.h> |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 31 | #include <linux/if_ether.h> |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 32 | #include <log/log.h> |
Erik Kline | c296f09 | 2016-08-02 15:22:53 +0900 | [diff] [blame] | 33 | #include <netutils/ifc.h> |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 34 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 35 | #include <android/net/INetd.h> |
| 36 | #include <netdutils/Misc.h> |
| 37 | #include <netdutils/Slice.h> |
| 38 | #include <netdutils/Syscalls.h> |
| 39 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 40 | #include "InterfaceController.h" |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 41 | #include "RouteController.h" |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 42 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 43 | using android::base::ReadFileToString; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 44 | using android::base::StringPrintf; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 45 | using android::base::Trim; |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 46 | using android::base::WriteStringToFile; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 47 | using android::net::INetd; |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 48 | using android::net::RouteController; |
Nathan Harold | 172f8e4 | 2018-04-19 13:10:19 -0700 | [diff] [blame] | 49 | using android::netdutils::isOk; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 50 | using android::netdutils::makeSlice; |
| 51 | using android::netdutils::sSyscalls; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 52 | using android::netdutils::Status; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 53 | using android::netdutils::statusFromErrno; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 54 | using android::netdutils::StatusOr; |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 55 | using android::netdutils::toString; |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 56 | using android::netdutils::status::ok; |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 57 | |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 58 | namespace { |
| 59 | |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 60 | const char ipv4_proc_path[] = "/proc/sys/net/ipv4/conf"; |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 61 | const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf"; |
| 62 | |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 63 | const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh"; |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 64 | const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh"; |
| 65 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 66 | const char proc_net_path[] = "/proc/sys/net"; |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 67 | const char sys_net_path[] = "/sys/class/net"; |
| 68 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 69 | constexpr int kRouteInfoMinPrefixLen = 48; |
| 70 | |
| 71 | // RFC 7421 prefix length. |
| 72 | constexpr int kRouteInfoMaxPrefixLen = 64; |
| 73 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 74 | // Property used to persist RFC 7217 stable secret. Protected by SELinux policy. |
| 75 | const char kStableSecretProperty[] = "persist.netd.stable_secret"; |
| 76 | |
| 77 | // RFC 7217 stable secret on linux is formatted as an IPv6 address. |
| 78 | // This function uses 128 bits of high quality entropy to generate an |
| 79 | // address for this purpose. This function should be not be called |
| 80 | // frequently. |
| 81 | StatusOr<std::string> randomIPv6Address() { |
| 82 | in6_addr addr = {}; |
| 83 | const auto& sys = sSyscalls.get(); |
| 84 | ASSIGN_OR_RETURN(auto fd, sys.open("/dev/random", O_RDONLY)); |
| 85 | RETURN_IF_NOT_OK(sys.read(fd, makeSlice(addr))); |
| 86 | return toString(addr); |
| 87 | } |
| 88 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 89 | inline bool isNormalPathComponent(const char *component) { |
| 90 | return (strcmp(component, ".") != 0) && |
| 91 | (strcmp(component, "..") != 0) && |
| 92 | (strchr(component, '/') == nullptr); |
| 93 | } |
| 94 | |
| 95 | inline bool isAddressFamilyPathComponent(const char *component) { |
| 96 | return strcmp(component, "ipv4") == 0 || strcmp(component, "ipv6") == 0; |
| 97 | } |
| 98 | |
| 99 | inline bool isInterfaceName(const char *name) { |
| 100 | return isNormalPathComponent(name) && |
| 101 | (strcmp(name, "default") != 0) && |
| 102 | (strcmp(name, "all") != 0); |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int writeValueToPath( |
| 106 | const char* dirname, const char* subdirname, const char* basename, |
| 107 | const char* value) { |
| 108 | std::string path(StringPrintf("%s/%s/%s", dirname, subdirname, basename)); |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 109 | return WriteStringToFile(value, path) ? 0 : -EREMOTEIO; |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 110 | } |
| 111 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 112 | // 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] | 113 | void forEachInterface( |
| 114 | const std::string& dirname, |
| 115 | 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] | 116 | // Run on default, which controls the behavior of any interfaces that are created in the future. |
| 117 | fn(dirname, "default"); |
| 118 | DIR* dir = opendir(dirname.c_str()); |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 119 | if (!dir) { |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 120 | ALOGE("Can't list %s: %s", dirname.c_str(), strerror(errno)); |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 121 | return; |
| 122 | } |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 123 | while (true) { |
| 124 | const dirent *ent = readdir(dir); |
| 125 | if (!ent) { |
| 126 | break; |
| 127 | } |
| 128 | if ((ent->d_type != DT_DIR) || !isInterfaceName(ent->d_name)) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 129 | continue; |
| 130 | } |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 131 | fn(dirname, ent->d_name); |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 132 | } |
| 133 | closedir(dir); |
| 134 | } |
| 135 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 136 | void setOnAllInterfaces(const char* dirname, const char* basename, const char* value) { |
| 137 | auto fn = [basename, value](const std::string& path, const std::string& iface) { |
| 138 | writeValueToPath(path.c_str(), iface.c_str(), basename, value); |
| 139 | }; |
| 140 | forEachInterface(dirname, fn); |
| 141 | } |
| 142 | |
Erik Kline | 7adf8d7 | 2015-07-28 18:51:01 +0900 | [diff] [blame] | 143 | void setIPv6UseOutgoingInterfaceAddrsOnly(const char *value) { |
| 144 | setOnAllInterfaces(ipv6_proc_path, "use_oif_addrs_only", value); |
| 145 | } |
| 146 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 147 | std::string getParameterPathname( |
| 148 | const char *family, const char *which, const char *interface, const char *parameter) { |
| 149 | if (!isAddressFamilyPathComponent(family)) { |
| 150 | errno = EAFNOSUPPORT; |
| 151 | return ""; |
| 152 | } else if (!isNormalPathComponent(which) || |
| 153 | !isInterfaceName(interface) || |
| 154 | !isNormalPathComponent(parameter)) { |
| 155 | errno = EINVAL; |
| 156 | return ""; |
| 157 | } |
| 158 | |
| 159 | return StringPrintf("%s/%s/%s/%s/%s", proc_net_path, family, which, interface, parameter); |
| 160 | } |
| 161 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 162 | void setAcceptIPv6RIO(int min, int max) { |
| 163 | auto fn = [min, max](const std::string& prefix, const std::string& iface) { |
| 164 | int rv = writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_min_plen", |
| 165 | std::to_string(min).c_str()); |
| 166 | if (rv != 0) { |
| 167 | // Only update max_plen if the write to min_plen succeeded. This ordering will prevent |
| 168 | // RIOs from being accepted unless both min and max are written successfully. |
| 169 | return; |
| 170 | } |
| 171 | writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_max_plen", |
| 172 | std::to_string(max).c_str()); |
| 173 | }; |
| 174 | forEachInterface(ipv6_proc_path, fn); |
| 175 | } |
| 176 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 177 | // Ideally this function would return StatusOr<std::string>, however |
| 178 | // there is no safe value for dflt that will always differ from the |
| 179 | // stored property. Bugs code could conceivably end up persisting the |
| 180 | // reserved value resulting in surprising behavior. |
| 181 | std::string getProperty(const std::string& key, const std::string& dflt) { |
| 182 | return android::base::GetProperty(key, dflt); |
| 183 | }; |
| 184 | |
| 185 | Status setProperty(const std::string& key, const std::string& val) { |
Lorenzo Colitti | 516764f | 2017-07-10 19:13:23 +0900 | [diff] [blame] | 186 | // SetProperty does not dependably set errno to a meaningful value. Use our own error code so |
| 187 | // callers don't get confused. |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 188 | return android::base::SetProperty(key, val) |
| 189 | ? ok |
Lorenzo Colitti | 516764f | 2017-07-10 19:13:23 +0900 | [diff] [blame] | 190 | : statusFromErrno(EREMOTEIO, "SetProperty failed, see libc logs"); |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 194 | } // namespace |
| 195 | |
Bernie Innocenti | 4debf3b | 2018-09-12 13:54:50 +0900 | [diff] [blame] | 196 | namespace android { |
| 197 | namespace net { |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 198 | std::mutex InterfaceController::mutex; |
Bernie Innocenti | 4debf3b | 2018-09-12 13:54:50 +0900 | [diff] [blame] | 199 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 200 | android::netdutils::Status InterfaceController::enableStablePrivacyAddresses( |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 201 | const std::string& iface, |
| 202 | const GetPropertyFn& getProperty, |
| 203 | const SetPropertyFn& setProperty) { |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 204 | const auto& sys = sSyscalls.get(); |
| 205 | const std::string procTarget = std::string(ipv6_proc_path) + "/" + iface + "/stable_secret"; |
| 206 | auto procFd = sys.open(procTarget, O_CLOEXEC | O_WRONLY); |
| 207 | |
| 208 | // Devices with old kernels (typically < 4.4) don't support |
| 209 | // RFC 7217 stable privacy addresses. |
| 210 | if (equalToErrno(procFd, ENOENT)) { |
| 211 | return statusFromErrno(EOPNOTSUPP, |
| 212 | "Failed to open stable_secret. Assuming unsupported kernel version"); |
| 213 | } |
| 214 | |
| 215 | // If stable_secret exists but we can't open it, something strange is going on. |
| 216 | RETURN_IF_NOT_OK(procFd); |
| 217 | |
| 218 | const char kUninitialized[] = "uninitialized"; |
| 219 | const auto oldSecret = getProperty(kStableSecretProperty, kUninitialized); |
| 220 | std::string secret = oldSecret; |
| 221 | |
| 222 | // Generate a new secret if no persistent property existed. |
| 223 | if (oldSecret == kUninitialized) { |
| 224 | ASSIGN_OR_RETURN(secret, randomIPv6Address()); |
| 225 | } |
| 226 | |
| 227 | // Ask the OS to generate SLAAC addresses on iface using secret. |
| 228 | RETURN_IF_NOT_OK(sys.write(procFd.value(), makeSlice(secret))); |
| 229 | |
| 230 | // Don't persist an existing secret. |
| 231 | if (oldSecret != kUninitialized) { |
| 232 | return ok; |
| 233 | } |
| 234 | |
| 235 | return setProperty(kStableSecretProperty, secret); |
| 236 | } |
| 237 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 238 | void InterfaceController::initializeAll() { |
| 239 | // Initial IPv6 settings. |
| 240 | // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces. |
| 241 | // This causes RAs to work or not work based on whether forwarding is on, and causes routes |
| 242 | // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable |
| 243 | // by always setting accept_ra to 2. |
| 244 | setAcceptRA("2"); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 245 | |
Joel Scherpelz | 31e2599 | 2017-03-24 12:40:00 +0900 | [diff] [blame] | 246 | // Accept RIOs with prefix length in the closed interval [48, 64]. |
| 247 | setAcceptIPv6RIO(kRouteInfoMinPrefixLen, kRouteInfoMaxPrefixLen); |
| 248 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 249 | setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX); |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 250 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 251 | // Enable optimistic DAD for IPv6 addresses on all interfaces. |
| 252 | setIPv6OptimisticMode("1"); |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 253 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 254 | // Reduce the ARP/ND base reachable time from the default (30sec) to 15sec. |
| 255 | setBaseReachableTimeMs(15 * 1000); |
Erik Kline | 7adf8d7 | 2015-07-28 18:51:01 +0900 | [diff] [blame] | 256 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 257 | // When sending traffic via a given interface use only addresses configured |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 258 | // on that interface as possible source addresses. |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 259 | setIPv6UseOutgoingInterfaceAddrsOnly("1"); |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 260 | |
| 261 | // Ensure that ICMP redirects are rejected globally on all interfaces. |
| 262 | disableIcmpRedirects(); |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 263 | } |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 264 | |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 265 | int InterfaceController::setEnableIPv6(const char *interface, const int on) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 266 | if (!isIfaceName(interface)) { |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 267 | return -ENOENT; |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 268 | } |
| 269 | // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf. |
| 270 | // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf |
| 271 | // addresses and routes and disables IPv6 on the interface. |
| 272 | const char *disable_ipv6 = on ? "0" : "1"; |
| 273 | return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6); |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 274 | } |
| 275 | |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 276 | // Changes to addrGenMode will not fully take effect until the next |
| 277 | // time disable_ipv6 transitions from 1 to 0. |
| 278 | Status InterfaceController::setIPv6AddrGenMode(const std::string& interface, int mode) { |
| 279 | if (!isIfaceName(interface)) { |
| 280 | return statusFromErrno(ENOENT, "invalid iface name: " + interface); |
| 281 | } |
| 282 | |
| 283 | switch (mode) { |
| 284 | case INetd::IPV6_ADDR_GEN_MODE_EUI64: |
Maciej Żenczykowski | 92424d5 | 2019-08-09 14:29:12 -0700 | [diff] [blame] | 285 | // Ignore return value. If /proc/.../addr_gen_mode is |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 286 | // missing we're probably in EUI64 mode already. |
Maciej Żenczykowski | 92424d5 | 2019-08-09 14:29:12 -0700 | [diff] [blame] | 287 | writeValueToPath(ipv6_proc_path, interface.c_str(), "addr_gen_mode", "0"); |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 288 | break; |
| 289 | case INetd::IPV6_ADDR_GEN_MODE_STABLE_PRIVACY: { |
| 290 | return enableStablePrivacyAddresses(interface, getProperty, setProperty); |
| 291 | } |
| 292 | case INetd::IPV6_ADDR_GEN_MODE_NONE: |
| 293 | case INetd::IPV6_ADDR_GEN_MODE_RANDOM: |
| 294 | default: |
| 295 | return statusFromErrno(EOPNOTSUPP, "unsupported addrGenMode"); |
| 296 | } |
| 297 | |
| 298 | return ok; |
| 299 | } |
| 300 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 301 | int InterfaceController::setAcceptIPv6Ra(const char *interface, const int on) { |
| 302 | if (!isIfaceName(interface)) { |
| 303 | errno = ENOENT; |
| 304 | return -1; |
| 305 | } |
| 306 | // Because forwarding can be enabled even when tethering is off, we always |
| 307 | // use mode "2" (accept RAs, even if forwarding is enabled). |
| 308 | const char *accept_ra = on ? "2" : "0"; |
| 309 | return writeValueToPath(ipv6_proc_path, interface, "accept_ra", accept_ra); |
| 310 | } |
| 311 | |
| 312 | int InterfaceController::setAcceptIPv6Dad(const char *interface, const int on) { |
| 313 | if (!isIfaceName(interface)) { |
| 314 | errno = ENOENT; |
| 315 | return -1; |
| 316 | } |
| 317 | const char *accept_dad = on ? "1" : "0"; |
| 318 | return writeValueToPath(ipv6_proc_path, interface, "accept_dad", accept_dad); |
| 319 | } |
| 320 | |
Erik Kline | 59d8c48 | 2016-08-09 15:28:42 +0900 | [diff] [blame] | 321 | int InterfaceController::setIPv6DadTransmits(const char *interface, const char *value) { |
| 322 | if (!isIfaceName(interface)) { |
| 323 | errno = ENOENT; |
| 324 | return -1; |
| 325 | } |
| 326 | return writeValueToPath(ipv6_proc_path, interface, "dad_transmits", value); |
| 327 | } |
| 328 | |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 329 | int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 330 | if (!isIfaceName(interface)) { |
| 331 | errno = ENOENT; |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 332 | return -errno; |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 333 | } |
| 334 | // 0: disable IPv6 privacy addresses |
Joel Scherpelz | de93796 | 2017-06-01 13:20:21 +0900 | [diff] [blame] | 335 | // 2: enable IPv6 privacy addresses and prefer them over non-privacy ones. |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 336 | return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0"); |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 337 | } |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 338 | |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 339 | void InterfaceController::setAcceptRA(const char *value) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 340 | setOnAllInterfaces(ipv6_proc_path, "accept_ra", value); |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 343 | // |tableOrOffset| is interpreted as: |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 344 | // If == 0: default. Routes go into RT6_TABLE_MAIN. |
| 345 | // If > 0: user set. Routes go into the specified table. |
| 346 | // If < 0: automatic. The absolute value is intepreted as an offset and added to the interface |
| 347 | // ID to get the table. If it's set to -1000, routes from interface ID 5 will go into |
| 348 | // table 1005, etc. |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 349 | void InterfaceController::setAcceptRARouteTable(int tableOrOffset) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 350 | std::string value(StringPrintf("%d", tableOrOffset)); |
| 351 | setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str()); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 352 | } |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 353 | |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 354 | int InterfaceController::setMtu(const char *interface, const char *mtu) |
| 355 | { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 356 | if (!isIfaceName(interface)) { |
| 357 | errno = ENOENT; |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 358 | return -errno; |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 359 | } |
| 360 | return writeValueToPath(sys_net_path, interface, "mtu", mtu); |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 361 | } |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 362 | |
Maciej Żenczykowski | fd8e005 | 2019-10-17 17:10:40 -0700 | [diff] [blame] | 363 | // Returns zero on success and negative errno on failure. |
Erik Kline | c296f09 | 2016-08-02 15:22:53 +0900 | [diff] [blame] | 364 | int InterfaceController::addAddress(const char *interface, |
| 365 | const char *addrString, int prefixLength) { |
| 366 | return ifc_add_address(interface, addrString, prefixLength); |
| 367 | } |
| 368 | |
Maciej Żenczykowski | fd8e005 | 2019-10-17 17:10:40 -0700 | [diff] [blame] | 369 | // Returns zero on success and negative errno on failure. |
Erik Kline | c296f09 | 2016-08-02 15:22:53 +0900 | [diff] [blame] | 370 | int InterfaceController::delAddress(const char *interface, |
| 371 | const char *addrString, int prefixLength) { |
| 372 | return ifc_del_address(interface, addrString, prefixLength); |
| 373 | } |
| 374 | |
Hugo Benichi | c4b3a0c | 2018-05-22 08:48:40 +0900 | [diff] [blame] | 375 | int InterfaceController::disableIcmpRedirects() { |
| 376 | int rv = 0; |
| 377 | rv |= writeValueToPath(ipv4_proc_path, "all", "accept_redirects", "0"); |
| 378 | rv |= writeValueToPath(ipv6_proc_path, "all", "accept_redirects", "0"); |
| 379 | setOnAllInterfaces(ipv4_proc_path, "accept_redirects", "0"); |
| 380 | setOnAllInterfaces(ipv6_proc_path, "accept_redirects", "0"); |
| 381 | return rv; |
| 382 | } |
| 383 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 384 | int InterfaceController::getParameter( |
| 385 | const char *family, const char *which, const char *interface, const char *parameter, |
| 386 | std::string *value) { |
| 387 | const std::string path(getParameterPathname(family, which, interface, parameter)); |
| 388 | if (path.empty()) { |
| 389 | return -errno; |
| 390 | } |
Erik Kline | 38e51f1 | 2018-09-06 20:14:44 +0900 | [diff] [blame] | 391 | if (ReadFileToString(path, value)) { |
| 392 | *value = Trim(*value); |
| 393 | return 0; |
| 394 | } |
| 395 | return -errno; |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | int InterfaceController::setParameter( |
| 399 | const char *family, const char *which, const char *interface, const char *parameter, |
| 400 | const char *value) { |
| 401 | const std::string path(getParameterPathname(family, which, interface, parameter)); |
| 402 | if (path.empty()) { |
| 403 | return -errno; |
| 404 | } |
| 405 | return WriteStringToFile(value, path) ? 0 : -errno; |
| 406 | } |
| 407 | |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 408 | void InterfaceController::setBaseReachableTimeMs(unsigned int millis) { |
| 409 | std::string value(StringPrintf("%u", millis)); |
| 410 | setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str()); |
| 411 | setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str()); |
| 412 | } |
| 413 | |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 414 | void InterfaceController::setIPv6OptimisticMode(const char *value) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 415 | setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value); |
| 416 | setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value); |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 417 | } |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 418 | |
Nathan Harold | 172f8e4 | 2018-04-19 13:10:19 -0700 | [diff] [blame] | 419 | StatusOr<std::vector<std::string>> InterfaceController::getIfaceNames() { |
| 420 | std::vector<std::string> ifaceNames; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 421 | DIR* d; |
| 422 | struct dirent* de; |
| 423 | |
| 424 | if (!(d = opendir("/sys/class/net"))) { |
| 425 | return statusFromErrno(errno, "Cannot open iface directory"); |
| 426 | } |
| 427 | while ((de = readdir(d))) { |
Maciej Żenczykowski | ee0f48d | 2019-04-16 13:00:53 -0700 | [diff] [blame] | 428 | if ((de->d_type != DT_DIR) && (de->d_type != DT_LNK)) continue; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 429 | if (de->d_name[0] == '.') continue; |
Nathan Harold | 172f8e4 | 2018-04-19 13:10:19 -0700 | [diff] [blame] | 430 | ifaceNames.push_back(std::string(de->d_name)); |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 431 | } |
| 432 | closedir(d); |
Nathan Harold | 172f8e4 | 2018-04-19 13:10:19 -0700 | [diff] [blame] | 433 | return ifaceNames; |
| 434 | } |
| 435 | |
| 436 | StatusOr<std::map<std::string, uint32_t>> InterfaceController::getIfaceList() { |
| 437 | std::map<std::string, uint32_t> ifacePairs; |
| 438 | |
| 439 | ASSIGN_OR_RETURN(auto ifaceNames, getIfaceNames()); |
| 440 | |
| 441 | for (const auto& name : ifaceNames) { |
| 442 | uint32_t ifaceIndex = if_nametoindex(name.c_str()); |
| 443 | if (ifaceIndex) { |
| 444 | ifacePairs.insert(std::pair<std::string, uint32_t>(name, ifaceIndex)); |
| 445 | } |
| 446 | } |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 447 | return ifacePairs; |
| 448 | } |
Bernie Innocenti | 4debf3b | 2018-09-12 13:54:50 +0900 | [diff] [blame] | 449 | |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 450 | namespace { |
| 451 | |
| 452 | std::string hwAddrToStr(unsigned char* hwaddr) { |
| 453 | return StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], |
| 454 | hwaddr[4], hwaddr[5]); |
| 455 | } |
| 456 | |
| 457 | int ipv4NetmaskToPrefixLength(in_addr_t mask) { |
| 458 | int prefixLength = 0; |
| 459 | uint32_t m = ntohl(mask); |
| 460 | while (m & (1 << 31)) { |
| 461 | prefixLength++; |
| 462 | m = m << 1; |
| 463 | } |
| 464 | return prefixLength; |
| 465 | } |
| 466 | |
| 467 | std::string toStdString(const String16& s) { |
| 468 | return std::string(String8(s.string())); |
| 469 | } |
| 470 | |
| 471 | } // namespace |
| 472 | |
| 473 | Status InterfaceController::setCfg(const InterfaceConfigurationParcel& cfg) { |
| 474 | const auto& sys = sSyscalls.get(); |
| 475 | ASSIGN_OR_RETURN(auto fd, sys.socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 476 | struct ifreq ifr = { |
| 477 | .ifr_addr = {.sa_family = AF_INET}, // Clear the IPv4 address. |
| 478 | }; |
| 479 | strlcpy(ifr.ifr_name, cfg.ifName.c_str(), IFNAMSIZ); |
| 480 | |
| 481 | // Make sure that clear IPv4 address before set flag |
| 482 | // SIOCGIFFLAGS might override ifr and caused clear IPv4 addr ioctl error |
| 483 | RETURN_IF_NOT_OK(sys.ioctl(fd, SIOCSIFADDR, &ifr)); |
| 484 | |
| 485 | if (!cfg.flags.empty()) { |
| 486 | RETURN_IF_NOT_OK(sys.ioctl(fd, SIOCGIFFLAGS, &ifr)); |
| 487 | uint16_t flags = ifr.ifr_flags; |
| 488 | |
| 489 | for (const auto& flag : cfg.flags) { |
| 490 | if (flag == toStdString(INetd::IF_STATE_UP())) { |
| 491 | ifr.ifr_flags = ifr.ifr_flags | IFF_UP; |
| 492 | } else if (flag == toStdString(INetd::IF_STATE_DOWN())) { |
| 493 | ifr.ifr_flags = (ifr.ifr_flags & (~IFF_UP)); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (ifr.ifr_flags != flags) { |
| 498 | RETURN_IF_NOT_OK(sys.ioctl(fd, SIOCSIFFLAGS, &ifr)); |
| 499 | } |
| 500 | } |
| 501 | |
Maciej Żenczykowski | 28275a8 | 2019-10-17 17:18:30 -0700 | [diff] [blame^] | 502 | if (int ret = ifc_add_address(cfg.ifName.c_str(), cfg.ipv4Addr.c_str(), cfg.prefixLength)) { |
| 503 | return statusFromErrno(-ret, "Failed to add addr"); |
| 504 | } |
Luke Huang | f778204 | 2018-08-08 13:13:04 +0800 | [diff] [blame] | 505 | |
| 506 | return ok; |
| 507 | } |
| 508 | |
| 509 | StatusOr<InterfaceConfigurationParcel> InterfaceController::getCfg(const std::string& ifName) { |
| 510 | struct in_addr addr = {}; |
| 511 | int prefixLength = 0; |
| 512 | unsigned char hwaddr[ETH_ALEN] = {}; |
| 513 | unsigned flags = 0; |
| 514 | InterfaceConfigurationParcel cfgResult; |
| 515 | |
| 516 | const auto& sys = sSyscalls.get(); |
| 517 | ASSIGN_OR_RETURN(auto fd, sys.socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 518 | |
| 519 | struct ifreq ifr = {}; |
| 520 | strlcpy(ifr.ifr_name, ifName.c_str(), IFNAMSIZ); |
| 521 | |
| 522 | if (isOk(sys.ioctl(fd, SIOCGIFADDR, &ifr))) { |
| 523 | addr.s_addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; |
| 524 | } |
| 525 | |
| 526 | if (isOk(sys.ioctl(fd, SIOCGIFNETMASK, &ifr))) { |
| 527 | prefixLength = |
| 528 | ipv4NetmaskToPrefixLength(((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr); |
| 529 | } |
| 530 | |
| 531 | if (isOk(sys.ioctl(fd, SIOCGIFFLAGS, &ifr))) { |
| 532 | flags = ifr.ifr_flags; |
| 533 | } |
| 534 | |
| 535 | // ETH_ALEN is for ARPHRD_ETHER, it is better to check the sa_family. |
| 536 | // However, we keep old design for the consistency. |
| 537 | if (isOk(sys.ioctl(fd, SIOCGIFHWADDR, &ifr))) { |
| 538 | memcpy((void*) hwaddr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); |
| 539 | } else { |
| 540 | ALOGW("Failed to retrieve HW addr for %s (%s)", ifName.c_str(), strerror(errno)); |
| 541 | } |
| 542 | |
| 543 | cfgResult.ifName = ifName; |
| 544 | cfgResult.hwAddr = hwAddrToStr(hwaddr); |
| 545 | cfgResult.ipv4Addr = std::string(inet_ntoa(addr)); |
| 546 | cfgResult.prefixLength = prefixLength; |
| 547 | cfgResult.flags.push_back(flags & IFF_UP ? toStdString(INetd::IF_STATE_UP()) |
| 548 | : toStdString(INetd::IF_STATE_DOWN())); |
| 549 | |
| 550 | if (flags & IFF_BROADCAST) cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_BROADCAST())); |
| 551 | if (flags & IFF_LOOPBACK) cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_LOOPBACK())); |
| 552 | if (flags & IFF_POINTOPOINT) |
| 553 | cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_POINTOPOINT())); |
| 554 | if (flags & IFF_RUNNING) cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_RUNNING())); |
| 555 | if (flags & IFF_MULTICAST) cfgResult.flags.push_back(toStdString(INetd::IF_FLAG_MULTICAST())); |
| 556 | |
| 557 | return cfgResult; |
| 558 | } |
| 559 | |
| 560 | int InterfaceController::clearAddrs(const std::string& ifName) { |
| 561 | return ifc_clear_addresses(ifName.c_str()); |
| 562 | } |
| 563 | |
Bernie Innocenti | 4debf3b | 2018-09-12 13:54:50 +0900 | [diff] [blame] | 564 | } // namespace net |
| 565 | } // namespace android |