blob: a8b985a8e53ff2cc48c8197fb5edcc67748431bb [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>
Joel Scherpelzde937962017-06-01 13:20:21 +090026#include <android-base/properties.h>
Elliott Hughesbbd56262015-12-04 15:45:10 -080027#include <android-base/stringprintf.h>
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070028#include <cutils/log.h>
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090029#include <logwrap/logwrap.h>
Erik Klinec296f092016-08-02 15:22:53 +090030#include <netutils/ifc.h>
Lorenzo Colitti70afde62013-03-04 17:58:40 +090031
Joel Scherpelzde937962017-06-01 13:20:21 +090032#include <android/net/INetd.h>
33#include <netdutils/Misc.h>
34#include <netdutils/Slice.h>
35#include <netdutils/Syscalls.h>
36
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070037#include "InterfaceController.h"
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070038#include "RouteController.h"
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070039
Erik Klineb218a872016-07-04 09:57:18 +090040using android::base::ReadFileToString;
Joel Scherpelzde937962017-06-01 13:20:21 +090041using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070042using android::base::WriteStringToFile;
Joel Scherpelzde937962017-06-01 13:20:21 +090043using android::net::INetd;
Lorenzo Colitti7035f222017-02-13 18:29:00 +090044using android::net::RouteController;
Joel Scherpelzde937962017-06-01 13:20:21 +090045using android::netdutils::Status;
46using android::netdutils::StatusOr;
47using android::netdutils::makeSlice;
48using android::netdutils::sSyscalls;
49using android::netdutils::status::ok;
50using android::netdutils::statusFromErrno;
51using android::netdutils::toString;
Dan Albert5407e142015-03-16 10:05:59 -070052
Erik Klinee1da4842015-05-12 15:56:06 +090053namespace {
54
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090055const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
56
Erik Kline145fd252015-05-12 15:58:49 +090057const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh";
58
59const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh";
60
Erik Klineb218a872016-07-04 09:57:18 +090061const char proc_net_path[] = "/proc/sys/net";
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -070062const char sys_net_path[] = "/sys/class/net";
63
Erik Klinebdcba112016-05-26 17:00:31 +090064const char wl_util_path[] = "/vendor/xbin/wlutil";
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090065
Joel Scherpelz31e25992017-03-24 12:40:00 +090066constexpr int kRouteInfoMinPrefixLen = 48;
67
68// RFC 7421 prefix length.
69constexpr int kRouteInfoMaxPrefixLen = 64;
70
Joel Scherpelzde937962017-06-01 13:20:21 +090071// Property used to persist RFC 7217 stable secret. Protected by SELinux policy.
72const 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.
78StatusOr<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 Klineb218a872016-07-04 09:57:18 +090086inline bool isNormalPathComponent(const char *component) {
87 return (strcmp(component, ".") != 0) &&
88 (strcmp(component, "..") != 0) &&
89 (strchr(component, '/') == nullptr);
90}
91
92inline bool isAddressFamilyPathComponent(const char *component) {
93 return strcmp(component, "ipv4") == 0 || strcmp(component, "ipv6") == 0;
94}
95
96inline bool isInterfaceName(const char *name) {
97 return isNormalPathComponent(name) &&
98 (strcmp(name, "default") != 0) &&
99 (strcmp(name, "all") != 0);
Erik Klinee1da4842015-05-12 15:56:06 +0900100}
101
102int 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 Kline3f957772015-06-03 17:44:24 +0900106 return WriteStringToFile(value, path) ? 0 : -1;
Erik Klinee1da4842015-05-12 15:56:06 +0900107}
108
Joel Scherpelz31e25992017-03-24 12:40:00 +0900109// Run @fn on each interface as well as 'default' in the path @dirname.
110void 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 Klinee1da4842015-05-12 15:56:06 +0900115 if (!dir) {
Joel Scherpelz31e25992017-03-24 12:40:00 +0900116 ALOGE("Can't list %s: %s", dirname.c_str(), strerror(errno));
Erik Klinee1da4842015-05-12 15:56:06 +0900117 return;
118 }
Joel Scherpelz31e25992017-03-24 12:40:00 +0900119 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 Klinee1da4842015-05-12 15:56:06 +0900125 continue;
126 }
Joel Scherpelz31e25992017-03-24 12:40:00 +0900127 fn(dirname, ent->d_name);
Erik Klinee1da4842015-05-12 15:56:06 +0900128 }
129 closedir(dir);
130}
131
Joel Scherpelz31e25992017-03-24 12:40:00 +0900132void 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 Kline7adf8d72015-07-28 18:51:01 +0900139void setIPv6UseOutgoingInterfaceAddrsOnly(const char *value) {
140 setOnAllInterfaces(ipv6_proc_path, "use_oif_addrs_only", value);
141}
142
Erik Klineb218a872016-07-04 09:57:18 +0900143std::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 Scherpelz31e25992017-03-24 12:40:00 +0900158void 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 Scherpelzde937962017-06-01 13:20:21 +0900173// 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.
177std::string getProperty(const std::string& key, const std::string& dflt) {
178 return android::base::GetProperty(key, dflt);
179};
180
181Status setProperty(const std::string& key, const std::string& val) {
182 // SetProperty tries to encode something useful in errno, however
183 // the value may get clobbered by async_safe_format_log() in
184 // __system_property_set(). Use with care.
185 return android::base::SetProperty(key, val)
186 ? ok
187 : statusFromErrno(errno, "SetProperty failed");
188};
189
190
Erik Klinee1da4842015-05-12 15:56:06 +0900191} // namespace
192
Joel Scherpelzde937962017-06-01 13:20:21 +0900193android::netdutils::Status InterfaceController::enableStablePrivacyAddresses(
194 const std::string& iface, GetPropertyFn getProperty, SetPropertyFn setProperty) {
195 const auto& sys = sSyscalls.get();
196 const std::string procTarget = std::string(ipv6_proc_path) + "/" + iface + "/stable_secret";
197 auto procFd = sys.open(procTarget, O_CLOEXEC | O_WRONLY);
198
199 // Devices with old kernels (typically < 4.4) don't support
200 // RFC 7217 stable privacy addresses.
201 if (equalToErrno(procFd, ENOENT)) {
202 return statusFromErrno(EOPNOTSUPP,
203 "Failed to open stable_secret. Assuming unsupported kernel version");
204 }
205
206 // If stable_secret exists but we can't open it, something strange is going on.
207 RETURN_IF_NOT_OK(procFd);
208
209 const char kUninitialized[] = "uninitialized";
210 const auto oldSecret = getProperty(kStableSecretProperty, kUninitialized);
211 std::string secret = oldSecret;
212
213 // Generate a new secret if no persistent property existed.
214 if (oldSecret == kUninitialized) {
215 ASSIGN_OR_RETURN(secret, randomIPv6Address());
216 }
217
218 // Ask the OS to generate SLAAC addresses on iface using secret.
219 RETURN_IF_NOT_OK(sys.write(procFd.value(), makeSlice(secret)));
220
221 // Don't persist an existing secret.
222 if (oldSecret != kUninitialized) {
223 return ok;
224 }
225
226 return setProperty(kStableSecretProperty, secret);
227}
228
Erik Kline2c5aaa12016-06-08 13:24:45 +0900229void InterfaceController::initializeAll() {
230 // Initial IPv6 settings.
231 // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
232 // This causes RAs to work or not work based on whether forwarding is on, and causes routes
233 // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
234 // by always setting accept_ra to 2.
235 setAcceptRA("2");
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900236
Joel Scherpelz31e25992017-03-24 12:40:00 +0900237 // Accept RIOs with prefix length in the closed interval [48, 64].
238 setAcceptIPv6RIO(kRouteInfoMinPrefixLen, kRouteInfoMaxPrefixLen);
239
Erik Kline2c5aaa12016-06-08 13:24:45 +0900240 setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
Erik Kline59273ed2014-12-08 16:05:28 +0900241
Erik Kline2c5aaa12016-06-08 13:24:45 +0900242 // Enable optimistic DAD for IPv6 addresses on all interfaces.
243 setIPv6OptimisticMode("1");
Erik Kline145fd252015-05-12 15:58:49 +0900244
Erik Kline2c5aaa12016-06-08 13:24:45 +0900245 // Reduce the ARP/ND base reachable time from the default (30sec) to 15sec.
246 setBaseReachableTimeMs(15 * 1000);
Erik Kline7adf8d72015-07-28 18:51:01 +0900247
Erik Kline2c5aaa12016-06-08 13:24:45 +0900248 // When sending traffic via a given interface use only addresses configured
249 // on that interface as possible source addresses.
250 setIPv6UseOutgoingInterfaceAddrsOnly("1");
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -0700251}
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900252
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900253int InterfaceController::setEnableIPv6(const char *interface, const int on) {
Erik Klinee1da4842015-05-12 15:56:06 +0900254 if (!isIfaceName(interface)) {
255 errno = ENOENT;
256 return -1;
257 }
258 // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
259 // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
260 // addresses and routes and disables IPv6 on the interface.
261 const char *disable_ipv6 = on ? "0" : "1";
262 return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6);
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900263}
264
Joel Scherpelzde937962017-06-01 13:20:21 +0900265// Changes to addrGenMode will not fully take effect until the next
266// time disable_ipv6 transitions from 1 to 0.
267Status InterfaceController::setIPv6AddrGenMode(const std::string& interface, int mode) {
268 if (!isIfaceName(interface)) {
269 return statusFromErrno(ENOENT, "invalid iface name: " + interface);
270 }
271
272 switch (mode) {
273 case INetd::IPV6_ADDR_GEN_MODE_EUI64:
274 // Ignore return value. If /proc/.../stable_secret is
275 // missing we're probably in EUI64 mode already.
276 writeValueToPath(ipv6_proc_path, interface.c_str(), "stable_secret", "");
277 break;
278 case INetd::IPV6_ADDR_GEN_MODE_STABLE_PRIVACY: {
279 return enableStablePrivacyAddresses(interface, getProperty, setProperty);
280 }
281 case INetd::IPV6_ADDR_GEN_MODE_NONE:
282 case INetd::IPV6_ADDR_GEN_MODE_RANDOM:
283 default:
284 return statusFromErrno(EOPNOTSUPP, "unsupported addrGenMode");
285 }
286
287 return ok;
288}
289
Erik Kline2c5aaa12016-06-08 13:24:45 +0900290int InterfaceController::setAcceptIPv6Ra(const char *interface, const int on) {
291 if (!isIfaceName(interface)) {
292 errno = ENOENT;
293 return -1;
294 }
295 // Because forwarding can be enabled even when tethering is off, we always
296 // use mode "2" (accept RAs, even if forwarding is enabled).
297 const char *accept_ra = on ? "2" : "0";
298 return writeValueToPath(ipv6_proc_path, interface, "accept_ra", accept_ra);
299}
300
301int InterfaceController::setAcceptIPv6Dad(const char *interface, const int on) {
302 if (!isIfaceName(interface)) {
303 errno = ENOENT;
304 return -1;
305 }
306 const char *accept_dad = on ? "1" : "0";
307 return writeValueToPath(ipv6_proc_path, interface, "accept_dad", accept_dad);
308}
309
Erik Kline59d8c482016-08-09 15:28:42 +0900310int InterfaceController::setIPv6DadTransmits(const char *interface, const char *value) {
311 if (!isIfaceName(interface)) {
312 errno = ENOENT;
313 return -1;
314 }
315 return writeValueToPath(ipv6_proc_path, interface, "dad_transmits", value);
316}
317
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900318int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
Erik Klinee1da4842015-05-12 15:56:06 +0900319 if (!isIfaceName(interface)) {
320 errno = ENOENT;
321 return -1;
322 }
323 // 0: disable IPv6 privacy addresses
Joel Scherpelzde937962017-06-01 13:20:21 +0900324 // 2: enable IPv6 privacy addresses and prefer them over non-privacy ones.
Erik Klinee1da4842015-05-12 15:56:06 +0900325 return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0");
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900326}
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900327
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +0900328// Enables or disables IPv6 ND offload. This is useful for 464xlat on wifi, IPv6 tethering, and
329// generally implementing IPv6 neighbour discovery and duplicate address detection properly.
330// TODO: This should be implemented in wpa_supplicant via driver commands instead.
331int InterfaceController::setIPv6NdOffload(char* interface, const int on) {
332 // Only supported on Broadcom chipsets via wlutil for now.
333 if (access(wl_util_path, X_OK) == 0) {
334 const char *argv[] = {
335 wl_util_path,
336 "-a",
337 interface,
338 "ndoe",
339 on ? "1" : "0"
340 };
341 int ret = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL,
342 false, false);
343 ALOGD("%s ND offload on %s: %d (%s)",
344 (on ? "enabling" : "disabling"), interface, ret, strerror(errno));
345 return ret;
346 } else {
347 return 0;
348 }
349}
350
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700351void InterfaceController::setAcceptRA(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900352 setOnAllInterfaces(ipv6_proc_path, "accept_ra", value);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700353}
354
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700355// |tableOrOffset| is interpreted as:
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700356// If == 0: default. Routes go into RT6_TABLE_MAIN.
357// If > 0: user set. Routes go into the specified table.
358// If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
359// ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
360// table 1005, etc.
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700361void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
Erik Klinee1da4842015-05-12 15:56:06 +0900362 std::string value(StringPrintf("%d", tableOrOffset));
363 setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str());
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900364}
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700365
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700366int InterfaceController::setMtu(const char *interface, const char *mtu)
367{
Erik Klinee1da4842015-05-12 15:56:06 +0900368 if (!isIfaceName(interface)) {
369 errno = ENOENT;
370 return -1;
371 }
372 return writeValueToPath(sys_net_path, interface, "mtu", mtu);
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700373}
Erik Kline59273ed2014-12-08 16:05:28 +0900374
Erik Klinec296f092016-08-02 15:22:53 +0900375int InterfaceController::addAddress(const char *interface,
376 const char *addrString, int prefixLength) {
377 return ifc_add_address(interface, addrString, prefixLength);
378}
379
380int InterfaceController::delAddress(const char *interface,
381 const char *addrString, int prefixLength) {
382 return ifc_del_address(interface, addrString, prefixLength);
383}
384
Erik Klineb218a872016-07-04 09:57:18 +0900385int InterfaceController::getParameter(
386 const char *family, const char *which, const char *interface, const char *parameter,
387 std::string *value) {
388 const std::string path(getParameterPathname(family, which, interface, parameter));
389 if (path.empty()) {
390 return -errno;
391 }
392 return ReadFileToString(path, value) ? 0 : -errno;
393}
394
395int InterfaceController::setParameter(
396 const char *family, const char *which, const char *interface, const char *parameter,
397 const char *value) {
398 const std::string path(getParameterPathname(family, which, interface, parameter));
399 if (path.empty()) {
400 return -errno;
401 }
402 return WriteStringToFile(value, path) ? 0 : -errno;
403}
404
Erik Kline145fd252015-05-12 15:58:49 +0900405void InterfaceController::setBaseReachableTimeMs(unsigned int millis) {
406 std::string value(StringPrintf("%u", millis));
407 setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
408 setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
409}
410
Erik Kline59273ed2014-12-08 16:05:28 +0900411void InterfaceController::setIPv6OptimisticMode(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900412 setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value);
413 setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value);
Erik Kline59273ed2014-12-08 16:05:28 +0900414}