blob: cb1f0c2cf56a2fabeeaa22ebae7bbd7a36affd44 [file] [log] [blame]
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -07001/*
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 Levitskiy329c3b42012-07-30 16:11:23 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -07009 *
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 Colitti37f2e372013-04-12 00:44:06 +090017#include <dirent.h>
Dan Albertaa1be2b2015-01-06 09:36:17 -080018#include <errno.h>
Elliott Hughes5f4938f2015-01-28 11:22:38 -080019#include <malloc.h>
Erik Klineb218a872016-07-04 09:57:18 +090020#include <sys/socket.h>
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070021
Joel Scherpelz31e25992017-03-24 12:40:00 +090022#include <functional>
23
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070024#define LOG_TAG "InterfaceController"
Elliott Hughesbbd56262015-12-04 15:45:10 -080025#include <android-base/file.h>
26#include <android-base/stringprintf.h>
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070027#include <cutils/log.h>
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090028#include <logwrap/logwrap.h>
Erik Klinec296f092016-08-02 15:22:53 +090029#include <netutils/ifc.h>
Lorenzo Colitti70afde62013-03-04 17:58:40 +090030
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070031#include "InterfaceController.h"
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070032#include "RouteController.h"
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070033
Dan Albert5407e142015-03-16 10:05:59 -070034using android::base::StringPrintf;
Erik Klineb218a872016-07-04 09:57:18 +090035using android::base::ReadFileToString;
Dan Albert5407e142015-03-16 10:05:59 -070036using android::base::WriteStringToFile;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090037using android::net::RouteController;
Dan Albert5407e142015-03-16 10:05:59 -070038
Erik Klinee1da4842015-05-12 15:56:06 +090039namespace {
40
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090041const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
42
Erik Kline145fd252015-05-12 15:58:49 +090043const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh";
44
45const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh";
46
Erik Klineb218a872016-07-04 09:57:18 +090047const char proc_net_path[] = "/proc/sys/net";
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -070048const char sys_net_path[] = "/sys/class/net";
49
Erik Klinebdcba112016-05-26 17:00:31 +090050const char wl_util_path[] = "/vendor/xbin/wlutil";
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090051
Joel Scherpelz31e25992017-03-24 12:40:00 +090052constexpr int kRouteInfoMinPrefixLen = 48;
53
54// RFC 7421 prefix length.
55constexpr int kRouteInfoMaxPrefixLen = 64;
56
Erik Klineb218a872016-07-04 09:57:18 +090057inline bool isNormalPathComponent(const char *component) {
58 return (strcmp(component, ".") != 0) &&
59 (strcmp(component, "..") != 0) &&
60 (strchr(component, '/') == nullptr);
61}
62
63inline bool isAddressFamilyPathComponent(const char *component) {
64 return strcmp(component, "ipv4") == 0 || strcmp(component, "ipv6") == 0;
65}
66
67inline bool isInterfaceName(const char *name) {
68 return isNormalPathComponent(name) &&
69 (strcmp(name, "default") != 0) &&
70 (strcmp(name, "all") != 0);
Erik Klinee1da4842015-05-12 15:56:06 +090071}
72
73int writeValueToPath(
74 const char* dirname, const char* subdirname, const char* basename,
75 const char* value) {
76 std::string path(StringPrintf("%s/%s/%s", dirname, subdirname, basename));
Erik Kline3f957772015-06-03 17:44:24 +090077 return WriteStringToFile(value, path) ? 0 : -1;
Erik Klinee1da4842015-05-12 15:56:06 +090078}
79
Joel Scherpelz31e25992017-03-24 12:40:00 +090080// Run @fn on each interface as well as 'default' in the path @dirname.
81void forEachInterface(const std::string& dirname,
82 std::function<void(const std::string& path, const std::string& iface)> fn) {
83 // Run on default, which controls the behavior of any interfaces that are created in the future.
84 fn(dirname, "default");
85 DIR* dir = opendir(dirname.c_str());
Erik Klinee1da4842015-05-12 15:56:06 +090086 if (!dir) {
Joel Scherpelz31e25992017-03-24 12:40:00 +090087 ALOGE("Can't list %s: %s", dirname.c_str(), strerror(errno));
Erik Klinee1da4842015-05-12 15:56:06 +090088 return;
89 }
Joel Scherpelz31e25992017-03-24 12:40:00 +090090 while (true) {
91 const dirent *ent = readdir(dir);
92 if (!ent) {
93 break;
94 }
95 if ((ent->d_type != DT_DIR) || !isInterfaceName(ent->d_name)) {
Erik Klinee1da4842015-05-12 15:56:06 +090096 continue;
97 }
Joel Scherpelz31e25992017-03-24 12:40:00 +090098 fn(dirname, ent->d_name);
Erik Klinee1da4842015-05-12 15:56:06 +090099 }
100 closedir(dir);
101}
102
Joel Scherpelz31e25992017-03-24 12:40:00 +0900103void setOnAllInterfaces(const char* dirname, const char* basename, const char* value) {
104 auto fn = [basename, value](const std::string& path, const std::string& iface) {
105 writeValueToPath(path.c_str(), iface.c_str(), basename, value);
106 };
107 forEachInterface(dirname, fn);
108}
109
Erik Kline7adf8d72015-07-28 18:51:01 +0900110void setIPv6UseOutgoingInterfaceAddrsOnly(const char *value) {
111 setOnAllInterfaces(ipv6_proc_path, "use_oif_addrs_only", value);
112}
113
Erik Klineb218a872016-07-04 09:57:18 +0900114std::string getParameterPathname(
115 const char *family, const char *which, const char *interface, const char *parameter) {
116 if (!isAddressFamilyPathComponent(family)) {
117 errno = EAFNOSUPPORT;
118 return "";
119 } else if (!isNormalPathComponent(which) ||
120 !isInterfaceName(interface) ||
121 !isNormalPathComponent(parameter)) {
122 errno = EINVAL;
123 return "";
124 }
125
126 return StringPrintf("%s/%s/%s/%s/%s", proc_net_path, family, which, interface, parameter);
127}
128
Joel Scherpelz31e25992017-03-24 12:40:00 +0900129void setAcceptIPv6RIO(int min, int max) {
130 auto fn = [min, max](const std::string& prefix, const std::string& iface) {
131 int rv = writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_min_plen",
132 std::to_string(min).c_str());
133 if (rv != 0) {
134 // Only update max_plen if the write to min_plen succeeded. This ordering will prevent
135 // RIOs from being accepted unless both min and max are written successfully.
136 return;
137 }
138 writeValueToPath(prefix.c_str(), iface.c_str(), "accept_ra_rt_info_max_plen",
139 std::to_string(max).c_str());
140 };
141 forEachInterface(ipv6_proc_path, fn);
142}
143
Erik Klinee1da4842015-05-12 15:56:06 +0900144} // namespace
145
Erik Kline2c5aaa12016-06-08 13:24:45 +0900146void InterfaceController::initializeAll() {
147 // Initial IPv6 settings.
148 // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
149 // This causes RAs to work or not work based on whether forwarding is on, and causes routes
150 // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
151 // by always setting accept_ra to 2.
152 setAcceptRA("2");
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900153
Joel Scherpelz31e25992017-03-24 12:40:00 +0900154 // Accept RIOs with prefix length in the closed interval [48, 64].
155 setAcceptIPv6RIO(kRouteInfoMinPrefixLen, kRouteInfoMaxPrefixLen);
156
Erik Kline2c5aaa12016-06-08 13:24:45 +0900157 setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
Erik Kline59273ed2014-12-08 16:05:28 +0900158
Erik Kline2c5aaa12016-06-08 13:24:45 +0900159 // Enable optimistic DAD for IPv6 addresses on all interfaces.
160 setIPv6OptimisticMode("1");
Erik Kline145fd252015-05-12 15:58:49 +0900161
Erik Kline2c5aaa12016-06-08 13:24:45 +0900162 // Reduce the ARP/ND base reachable time from the default (30sec) to 15sec.
163 setBaseReachableTimeMs(15 * 1000);
Erik Kline7adf8d72015-07-28 18:51:01 +0900164
Erik Kline2c5aaa12016-06-08 13:24:45 +0900165 // When sending traffic via a given interface use only addresses configured
166 // on that interface as possible source addresses.
167 setIPv6UseOutgoingInterfaceAddrsOnly("1");
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -0700168}
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900169
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900170int InterfaceController::setEnableIPv6(const char *interface, const int on) {
Erik Klinee1da4842015-05-12 15:56:06 +0900171 if (!isIfaceName(interface)) {
172 errno = ENOENT;
173 return -1;
174 }
175 // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
176 // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
177 // addresses and routes and disables IPv6 on the interface.
178 const char *disable_ipv6 = on ? "0" : "1";
179 return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6);
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900180}
181
Erik Kline2c5aaa12016-06-08 13:24:45 +0900182int InterfaceController::setAcceptIPv6Ra(const char *interface, const int on) {
183 if (!isIfaceName(interface)) {
184 errno = ENOENT;
185 return -1;
186 }
187 // Because forwarding can be enabled even when tethering is off, we always
188 // use mode "2" (accept RAs, even if forwarding is enabled).
189 const char *accept_ra = on ? "2" : "0";
190 return writeValueToPath(ipv6_proc_path, interface, "accept_ra", accept_ra);
191}
192
193int InterfaceController::setAcceptIPv6Dad(const char *interface, const int on) {
194 if (!isIfaceName(interface)) {
195 errno = ENOENT;
196 return -1;
197 }
198 const char *accept_dad = on ? "1" : "0";
199 return writeValueToPath(ipv6_proc_path, interface, "accept_dad", accept_dad);
200}
201
Erik Kline59d8c482016-08-09 15:28:42 +0900202int InterfaceController::setIPv6DadTransmits(const char *interface, const char *value) {
203 if (!isIfaceName(interface)) {
204 errno = ENOENT;
205 return -1;
206 }
207 return writeValueToPath(ipv6_proc_path, interface, "dad_transmits", value);
208}
209
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900210int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
Erik Klinee1da4842015-05-12 15:56:06 +0900211 if (!isIfaceName(interface)) {
212 errno = ENOENT;
213 return -1;
214 }
215 // 0: disable IPv6 privacy addresses
216 // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
217 return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0");
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900218}
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900219
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +0900220// Enables or disables IPv6 ND offload. This is useful for 464xlat on wifi, IPv6 tethering, and
221// generally implementing IPv6 neighbour discovery and duplicate address detection properly.
222// TODO: This should be implemented in wpa_supplicant via driver commands instead.
223int InterfaceController::setIPv6NdOffload(char* interface, const int on) {
224 // Only supported on Broadcom chipsets via wlutil for now.
225 if (access(wl_util_path, X_OK) == 0) {
226 const char *argv[] = {
227 wl_util_path,
228 "-a",
229 interface,
230 "ndoe",
231 on ? "1" : "0"
232 };
233 int ret = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL,
234 false, false);
235 ALOGD("%s ND offload on %s: %d (%s)",
236 (on ? "enabling" : "disabling"), interface, ret, strerror(errno));
237 return ret;
238 } else {
239 return 0;
240 }
241}
242
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700243void InterfaceController::setAcceptRA(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900244 setOnAllInterfaces(ipv6_proc_path, "accept_ra", value);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700245}
246
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700247// |tableOrOffset| is interpreted as:
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700248// If == 0: default. Routes go into RT6_TABLE_MAIN.
249// If > 0: user set. Routes go into the specified table.
250// If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
251// ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
252// table 1005, etc.
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700253void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
Erik Klinee1da4842015-05-12 15:56:06 +0900254 std::string value(StringPrintf("%d", tableOrOffset));
255 setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str());
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900256}
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700257
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700258int InterfaceController::setMtu(const char *interface, const char *mtu)
259{
Erik Klinee1da4842015-05-12 15:56:06 +0900260 if (!isIfaceName(interface)) {
261 errno = ENOENT;
262 return -1;
263 }
264 return writeValueToPath(sys_net_path, interface, "mtu", mtu);
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700265}
Erik Kline59273ed2014-12-08 16:05:28 +0900266
Erik Klinec296f092016-08-02 15:22:53 +0900267int InterfaceController::addAddress(const char *interface,
268 const char *addrString, int prefixLength) {
269 return ifc_add_address(interface, addrString, prefixLength);
270}
271
272int InterfaceController::delAddress(const char *interface,
273 const char *addrString, int prefixLength) {
274 return ifc_del_address(interface, addrString, prefixLength);
275}
276
Erik Klineb218a872016-07-04 09:57:18 +0900277int InterfaceController::getParameter(
278 const char *family, const char *which, const char *interface, const char *parameter,
279 std::string *value) {
280 const std::string path(getParameterPathname(family, which, interface, parameter));
281 if (path.empty()) {
282 return -errno;
283 }
284 return ReadFileToString(path, value) ? 0 : -errno;
285}
286
287int InterfaceController::setParameter(
288 const char *family, const char *which, const char *interface, const char *parameter,
289 const char *value) {
290 const std::string path(getParameterPathname(family, which, interface, parameter));
291 if (path.empty()) {
292 return -errno;
293 }
294 return WriteStringToFile(value, path) ? 0 : -errno;
295}
296
Erik Kline145fd252015-05-12 15:58:49 +0900297void InterfaceController::setBaseReachableTimeMs(unsigned int millis) {
298 std::string value(StringPrintf("%u", millis));
299 setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
300 setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
301}
302
Erik Kline59273ed2014-12-08 16:05:28 +0900303void InterfaceController::setIPv6OptimisticMode(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900304 setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value);
305 setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value);
Erik Kline59273ed2014-12-08 16:05:28 +0900306}