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> |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 20 | #include <sys/socket.h> |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 21 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 22 | #define LOG_TAG "InterfaceController" |
Elliott Hughes | bbd5626 | 2015-12-04 15:45:10 -0800 | [diff] [blame] | 23 | #include <android-base/file.h> |
| 24 | #include <android-base/stringprintf.h> |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 25 | #include <cutils/log.h> |
Lorenzo Colitti | 0ea8ff8 | 2014-10-28 00:15:07 +0900 | [diff] [blame] | 26 | #include <logwrap/logwrap.h> |
Erik Kline | c296f09 | 2016-08-02 15:22:53 +0900 | [diff] [blame] | 27 | #include <netutils/ifc.h> |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 28 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 29 | #include "InterfaceController.h" |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 30 | #include "RouteController.h" |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 31 | |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 32 | using android::base::StringPrintf; |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 33 | using android::base::ReadFileToString; |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 34 | using android::base::WriteStringToFile; |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame^] | 35 | using android::net::RouteController; |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 36 | |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 37 | namespace { |
| 38 | |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 39 | const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf"; |
| 40 | |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 41 | const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh"; |
| 42 | |
| 43 | const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh"; |
| 44 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 45 | const char proc_net_path[] = "/proc/sys/net"; |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 46 | const char sys_net_path[] = "/sys/class/net"; |
| 47 | |
Erik Kline | bdcba11 | 2016-05-26 17:00:31 +0900 | [diff] [blame] | 48 | const char wl_util_path[] = "/vendor/xbin/wlutil"; |
Lorenzo Colitti | 0ea8ff8 | 2014-10-28 00:15:07 +0900 | [diff] [blame] | 49 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 50 | inline bool isNormalPathComponent(const char *component) { |
| 51 | return (strcmp(component, ".") != 0) && |
| 52 | (strcmp(component, "..") != 0) && |
| 53 | (strchr(component, '/') == nullptr); |
| 54 | } |
| 55 | |
| 56 | inline bool isAddressFamilyPathComponent(const char *component) { |
| 57 | return strcmp(component, "ipv4") == 0 || strcmp(component, "ipv6") == 0; |
| 58 | } |
| 59 | |
| 60 | inline bool isInterfaceName(const char *name) { |
| 61 | return isNormalPathComponent(name) && |
| 62 | (strcmp(name, "default") != 0) && |
| 63 | (strcmp(name, "all") != 0); |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int writeValueToPath( |
| 67 | const char* dirname, const char* subdirname, const char* basename, |
| 68 | const char* value) { |
| 69 | std::string path(StringPrintf("%s/%s/%s", dirname, subdirname, basename)); |
Erik Kline | 3f95777 | 2015-06-03 17:44:24 +0900 | [diff] [blame] | 70 | return WriteStringToFile(value, path) ? 0 : -1; |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void setOnAllInterfaces(const char* dirname, const char* basename, const char* value) { |
| 74 | // Set the default value, which is used by any interfaces that are created in the future. |
| 75 | writeValueToPath(dirname, "default", basename, value); |
| 76 | |
| 77 | // Set the value on all the interfaces that currently exist. |
| 78 | DIR* dir = opendir(dirname); |
| 79 | if (!dir) { |
| 80 | ALOGE("Can't list %s: %s", dirname, strerror(errno)); |
| 81 | return; |
| 82 | } |
| 83 | dirent* d; |
| 84 | while ((d = readdir(dir))) { |
| 85 | if ((d->d_type != DT_DIR) || !isInterfaceName(d->d_name)) { |
| 86 | continue; |
| 87 | } |
| 88 | writeValueToPath(dirname, d->d_name, basename, value); |
| 89 | } |
| 90 | closedir(dir); |
| 91 | } |
| 92 | |
Erik Kline | 7adf8d7 | 2015-07-28 18:51:01 +0900 | [diff] [blame] | 93 | void setIPv6UseOutgoingInterfaceAddrsOnly(const char *value) { |
| 94 | setOnAllInterfaces(ipv6_proc_path, "use_oif_addrs_only", value); |
| 95 | } |
| 96 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 97 | std::string getParameterPathname( |
| 98 | const char *family, const char *which, const char *interface, const char *parameter) { |
| 99 | if (!isAddressFamilyPathComponent(family)) { |
| 100 | errno = EAFNOSUPPORT; |
| 101 | return ""; |
| 102 | } else if (!isNormalPathComponent(which) || |
| 103 | !isInterfaceName(interface) || |
| 104 | !isNormalPathComponent(parameter)) { |
| 105 | errno = EINVAL; |
| 106 | return ""; |
| 107 | } |
| 108 | |
| 109 | return StringPrintf("%s/%s/%s/%s/%s", proc_net_path, family, which, interface, parameter); |
| 110 | } |
| 111 | |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 112 | } // namespace |
| 113 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 114 | void InterfaceController::initializeAll() { |
| 115 | // Initial IPv6 settings. |
| 116 | // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces. |
| 117 | // This causes RAs to work or not work based on whether forwarding is on, and causes routes |
| 118 | // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable |
| 119 | // by always setting accept_ra to 2. |
| 120 | setAcceptRA("2"); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 121 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 122 | setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX); |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 123 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 124 | // Enable optimistic DAD for IPv6 addresses on all interfaces. |
| 125 | setIPv6OptimisticMode("1"); |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 126 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 127 | // Reduce the ARP/ND base reachable time from the default (30sec) to 15sec. |
| 128 | setBaseReachableTimeMs(15 * 1000); |
Erik Kline | 7adf8d7 | 2015-07-28 18:51:01 +0900 | [diff] [blame] | 129 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 130 | // When sending traffic via a given interface use only addresses configured |
| 131 | // on that interface as possible source addresses. |
| 132 | setIPv6UseOutgoingInterfaceAddrsOnly("1"); |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 133 | } |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 134 | |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 135 | int InterfaceController::setEnableIPv6(const char *interface, const int on) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 136 | if (!isIfaceName(interface)) { |
| 137 | errno = ENOENT; |
| 138 | return -1; |
| 139 | } |
| 140 | // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf. |
| 141 | // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf |
| 142 | // addresses and routes and disables IPv6 on the interface. |
| 143 | const char *disable_ipv6 = on ? "0" : "1"; |
| 144 | return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6); |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Erik Kline | 2c5aaa1 | 2016-06-08 13:24:45 +0900 | [diff] [blame] | 147 | int InterfaceController::setAcceptIPv6Ra(const char *interface, const int on) { |
| 148 | if (!isIfaceName(interface)) { |
| 149 | errno = ENOENT; |
| 150 | return -1; |
| 151 | } |
| 152 | // Because forwarding can be enabled even when tethering is off, we always |
| 153 | // use mode "2" (accept RAs, even if forwarding is enabled). |
| 154 | const char *accept_ra = on ? "2" : "0"; |
| 155 | return writeValueToPath(ipv6_proc_path, interface, "accept_ra", accept_ra); |
| 156 | } |
| 157 | |
| 158 | int InterfaceController::setAcceptIPv6Dad(const char *interface, const int on) { |
| 159 | if (!isIfaceName(interface)) { |
| 160 | errno = ENOENT; |
| 161 | return -1; |
| 162 | } |
| 163 | const char *accept_dad = on ? "1" : "0"; |
| 164 | return writeValueToPath(ipv6_proc_path, interface, "accept_dad", accept_dad); |
| 165 | } |
| 166 | |
Erik Kline | 59d8c48 | 2016-08-09 15:28:42 +0900 | [diff] [blame] | 167 | int InterfaceController::setIPv6DadTransmits(const char *interface, const char *value) { |
| 168 | if (!isIfaceName(interface)) { |
| 169 | errno = ENOENT; |
| 170 | return -1; |
| 171 | } |
| 172 | return writeValueToPath(ipv6_proc_path, interface, "dad_transmits", value); |
| 173 | } |
| 174 | |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 175 | int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 176 | if (!isIfaceName(interface)) { |
| 177 | errno = ENOENT; |
| 178 | return -1; |
| 179 | } |
| 180 | // 0: disable IPv6 privacy addresses |
| 181 | // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones. |
| 182 | return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0"); |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 183 | } |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 184 | |
Lorenzo Colitti | 0ea8ff8 | 2014-10-28 00:15:07 +0900 | [diff] [blame] | 185 | // Enables or disables IPv6 ND offload. This is useful for 464xlat on wifi, IPv6 tethering, and |
| 186 | // generally implementing IPv6 neighbour discovery and duplicate address detection properly. |
| 187 | // TODO: This should be implemented in wpa_supplicant via driver commands instead. |
| 188 | int InterfaceController::setIPv6NdOffload(char* interface, const int on) { |
| 189 | // Only supported on Broadcom chipsets via wlutil for now. |
| 190 | if (access(wl_util_path, X_OK) == 0) { |
| 191 | const char *argv[] = { |
| 192 | wl_util_path, |
| 193 | "-a", |
| 194 | interface, |
| 195 | "ndoe", |
| 196 | on ? "1" : "0" |
| 197 | }; |
| 198 | int ret = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, |
| 199 | false, false); |
| 200 | ALOGD("%s ND offload on %s: %d (%s)", |
| 201 | (on ? "enabling" : "disabling"), interface, ret, strerror(errno)); |
| 202 | return ret; |
| 203 | } else { |
| 204 | return 0; |
| 205 | } |
| 206 | } |
| 207 | |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 208 | void InterfaceController::setAcceptRA(const char *value) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 209 | setOnAllInterfaces(ipv6_proc_path, "accept_ra", value); |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 212 | // |tableOrOffset| is interpreted as: |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 213 | // If == 0: default. Routes go into RT6_TABLE_MAIN. |
| 214 | // If > 0: user set. Routes go into the specified table. |
| 215 | // If < 0: automatic. The absolute value is intepreted as an offset and added to the interface |
| 216 | // ID to get the table. If it's set to -1000, routes from interface ID 5 will go into |
| 217 | // table 1005, etc. |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 218 | void InterfaceController::setAcceptRARouteTable(int tableOrOffset) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 219 | std::string value(StringPrintf("%d", tableOrOffset)); |
| 220 | setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str()); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 221 | } |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 222 | |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 223 | int InterfaceController::setMtu(const char *interface, const char *mtu) |
| 224 | { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 225 | if (!isIfaceName(interface)) { |
| 226 | errno = ENOENT; |
| 227 | return -1; |
| 228 | } |
| 229 | return writeValueToPath(sys_net_path, interface, "mtu", mtu); |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 230 | } |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 231 | |
Erik Kline | c296f09 | 2016-08-02 15:22:53 +0900 | [diff] [blame] | 232 | int InterfaceController::addAddress(const char *interface, |
| 233 | const char *addrString, int prefixLength) { |
| 234 | return ifc_add_address(interface, addrString, prefixLength); |
| 235 | } |
| 236 | |
| 237 | int InterfaceController::delAddress(const char *interface, |
| 238 | const char *addrString, int prefixLength) { |
| 239 | return ifc_del_address(interface, addrString, prefixLength); |
| 240 | } |
| 241 | |
Erik Kline | b218a87 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 242 | int InterfaceController::getParameter( |
| 243 | const char *family, const char *which, const char *interface, const char *parameter, |
| 244 | std::string *value) { |
| 245 | const std::string path(getParameterPathname(family, which, interface, parameter)); |
| 246 | if (path.empty()) { |
| 247 | return -errno; |
| 248 | } |
| 249 | return ReadFileToString(path, value) ? 0 : -errno; |
| 250 | } |
| 251 | |
| 252 | int InterfaceController::setParameter( |
| 253 | const char *family, const char *which, const char *interface, const char *parameter, |
| 254 | const char *value) { |
| 255 | const std::string path(getParameterPathname(family, which, interface, parameter)); |
| 256 | if (path.empty()) { |
| 257 | return -errno; |
| 258 | } |
| 259 | return WriteStringToFile(value, path) ? 0 : -errno; |
| 260 | } |
| 261 | |
Erik Kline | 145fd25 | 2015-05-12 15:58:49 +0900 | [diff] [blame] | 262 | void InterfaceController::setBaseReachableTimeMs(unsigned int millis) { |
| 263 | std::string value(StringPrintf("%u", millis)); |
| 264 | setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str()); |
| 265 | setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str()); |
| 266 | } |
| 267 | |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 268 | void InterfaceController::setIPv6OptimisticMode(const char *value) { |
Erik Kline | e1da484 | 2015-05-12 15:56:06 +0900 | [diff] [blame] | 269 | setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value); |
| 270 | setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value); |
Erik Kline | 59273ed | 2014-12-08 16:05:28 +0900 | [diff] [blame] | 271 | } |