blob: 373bfd0cbbd822d36266bfacdd0d4f2729f28b10 [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>
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070020
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070021#define LOG_TAG "InterfaceController"
Dan Albert5407e142015-03-16 10:05:59 -070022#include <base/file.h>
23#include <base/stringprintf.h>
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070024#include <cutils/log.h>
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090025#include <logwrap/logwrap.h>
Lorenzo Colitti70afde62013-03-04 17:58:40 +090026
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070027#include "InterfaceController.h"
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070028#include "RouteController.h"
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070029
Dan Albert5407e142015-03-16 10:05:59 -070030using android::base::StringPrintf;
31using android::base::WriteStringToFile;
32
Erik Klinee1da4842015-05-12 15:56:06 +090033namespace {
34
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090035const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
36
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -070037const char sys_net_path[] = "/sys/class/net";
38
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090039const char wl_util_path[] = "/system/xbin/wlutil";
40
Erik Klinee1da4842015-05-12 15:56:06 +090041bool isInterfaceName(const char *name) {
42 return strcmp(name, ".") &&
43 strcmp(name, "..") &&
44 strcmp(name, "default") &&
45 strcmp(name, "all");
46}
47
48int writeValueToPath(
49 const char* dirname, const char* subdirname, const char* basename,
50 const char* value) {
51 std::string path(StringPrintf("%s/%s/%s", dirname, subdirname, basename));
52 return WriteStringToFile(value, path);
53}
54
55void setOnAllInterfaces(const char* dirname, const char* basename, const char* value) {
56 // Set the default value, which is used by any interfaces that are created in the future.
57 writeValueToPath(dirname, "default", basename, value);
58
59 // Set the value on all the interfaces that currently exist.
60 DIR* dir = opendir(dirname);
61 if (!dir) {
62 ALOGE("Can't list %s: %s", dirname, strerror(errno));
63 return;
64 }
65 dirent* d;
66 while ((d = readdir(dir))) {
67 if ((d->d_type != DT_DIR) || !isInterfaceName(d->d_name)) {
68 continue;
69 }
70 writeValueToPath(dirname, d->d_name, basename, value);
71 }
72 closedir(dir);
73}
74
75} // namespace
76
Sreeram Ramachandran1604e182014-07-19 23:22:33 -070077InterfaceController::InterfaceController() {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090078 // Initial IPv6 settings.
79 // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
80 // This causes RAs to work or not work based on whether forwarding is on, and causes routes
81 // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
82 // by always setting accept_ra to 2.
83 setAcceptRA("2");
84
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070085 setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
Erik Kline59273ed2014-12-08 16:05:28 +090086
87 // Enable optimistic DAD for IPv6 addresses on all interfaces.
88 setIPv6OptimisticMode("1");
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070089}
90
91InterfaceController::~InterfaceController() {
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070092}
Lorenzo Colitti70afde62013-03-04 17:58:40 +090093
Lorenzo Colitti70afde62013-03-04 17:58:40 +090094int InterfaceController::setEnableIPv6(const char *interface, const int on) {
Erik Klinee1da4842015-05-12 15:56:06 +090095 if (!isIfaceName(interface)) {
96 errno = ENOENT;
97 return -1;
98 }
99 // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
100 // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
101 // addresses and routes and disables IPv6 on the interface.
102 const char *disable_ipv6 = on ? "0" : "1";
103 return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6);
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900104}
105
106int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
Erik Klinee1da4842015-05-12 15:56:06 +0900107 if (!isIfaceName(interface)) {
108 errno = ENOENT;
109 return -1;
110 }
111 // 0: disable IPv6 privacy addresses
112 // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
113 return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0");
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900114}
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900115
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +0900116// Enables or disables IPv6 ND offload. This is useful for 464xlat on wifi, IPv6 tethering, and
117// generally implementing IPv6 neighbour discovery and duplicate address detection properly.
118// TODO: This should be implemented in wpa_supplicant via driver commands instead.
119int InterfaceController::setIPv6NdOffload(char* interface, const int on) {
120 // Only supported on Broadcom chipsets via wlutil for now.
121 if (access(wl_util_path, X_OK) == 0) {
122 const char *argv[] = {
123 wl_util_path,
124 "-a",
125 interface,
126 "ndoe",
127 on ? "1" : "0"
128 };
129 int ret = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL,
130 false, false);
131 ALOGD("%s ND offload on %s: %d (%s)",
132 (on ? "enabling" : "disabling"), interface, ret, strerror(errno));
133 return ret;
134 } else {
135 return 0;
136 }
137}
138
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700139void InterfaceController::setAcceptRA(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900140 setOnAllInterfaces(ipv6_proc_path, "accept_ra", value);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700141}
142
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700143// |tableOrOffset| is interpreted as:
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700144// If == 0: default. Routes go into RT6_TABLE_MAIN.
145// If > 0: user set. Routes go into the specified table.
146// If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
147// ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
148// table 1005, etc.
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700149void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
Erik Klinee1da4842015-05-12 15:56:06 +0900150 std::string value(StringPrintf("%d", tableOrOffset));
151 setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str());
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900152}
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700153
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700154int InterfaceController::setMtu(const char *interface, const char *mtu)
155{
Erik Klinee1da4842015-05-12 15:56:06 +0900156 if (!isIfaceName(interface)) {
157 errno = ENOENT;
158 return -1;
159 }
160 return writeValueToPath(sys_net_path, interface, "mtu", mtu);
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700161}
Erik Kline59273ed2014-12-08 16:05:28 +0900162
163void InterfaceController::setIPv6OptimisticMode(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900164 setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value);
165 setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value);
Erik Kline59273ed2014-12-08 16:05:28 +0900166}