blob: cbc36111cd89244f2484886e092e80daab2b12ea [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"
Elliott Hughesbbd56262015-12-04 15:45:10 -080022#include <android-base/file.h>
23#include <android-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
Erik Kline145fd252015-05-12 15:58:49 +090037const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh";
38
39const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh";
40
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -070041const char sys_net_path[] = "/sys/class/net";
42
Erik Klinebdcba112016-05-26 17:00:31 +090043const char wl_util_path[] = "/vendor/xbin/wlutil";
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090044
Erik Klinee1da4842015-05-12 15:56:06 +090045bool isInterfaceName(const char *name) {
46 return strcmp(name, ".") &&
47 strcmp(name, "..") &&
48 strcmp(name, "default") &&
49 strcmp(name, "all");
50}
51
52int writeValueToPath(
53 const char* dirname, const char* subdirname, const char* basename,
54 const char* value) {
55 std::string path(StringPrintf("%s/%s/%s", dirname, subdirname, basename));
Erik Kline3f957772015-06-03 17:44:24 +090056 return WriteStringToFile(value, path) ? 0 : -1;
Erik Klinee1da4842015-05-12 15:56:06 +090057}
58
59void setOnAllInterfaces(const char* dirname, const char* basename, const char* value) {
60 // Set the default value, which is used by any interfaces that are created in the future.
61 writeValueToPath(dirname, "default", basename, value);
62
63 // Set the value on all the interfaces that currently exist.
64 DIR* dir = opendir(dirname);
65 if (!dir) {
66 ALOGE("Can't list %s: %s", dirname, strerror(errno));
67 return;
68 }
69 dirent* d;
70 while ((d = readdir(dir))) {
71 if ((d->d_type != DT_DIR) || !isInterfaceName(d->d_name)) {
72 continue;
73 }
74 writeValueToPath(dirname, d->d_name, basename, value);
75 }
76 closedir(dir);
77}
78
Erik Kline7adf8d72015-07-28 18:51:01 +090079void setIPv6UseOutgoingInterfaceAddrsOnly(const char *value) {
80 setOnAllInterfaces(ipv6_proc_path, "use_oif_addrs_only", value);
81}
82
Erik Klinee1da4842015-05-12 15:56:06 +090083} // namespace
84
Erik Kline2c5aaa12016-06-08 13:24:45 +090085void InterfaceController::initializeAll() {
86 // Initial IPv6 settings.
87 // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
88 // This causes RAs to work or not work based on whether forwarding is on, and causes routes
89 // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
90 // by always setting accept_ra to 2.
91 setAcceptRA("2");
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090092
Erik Kline2c5aaa12016-06-08 13:24:45 +090093 setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
Erik Kline59273ed2014-12-08 16:05:28 +090094
Erik Kline2c5aaa12016-06-08 13:24:45 +090095 // Enable optimistic DAD for IPv6 addresses on all interfaces.
96 setIPv6OptimisticMode("1");
Erik Kline145fd252015-05-12 15:58:49 +090097
Erik Kline2c5aaa12016-06-08 13:24:45 +090098 // Reduce the ARP/ND base reachable time from the default (30sec) to 15sec.
99 setBaseReachableTimeMs(15 * 1000);
Erik Kline7adf8d72015-07-28 18:51:01 +0900100
Erik Kline2c5aaa12016-06-08 13:24:45 +0900101 // When sending traffic via a given interface use only addresses configured
102 // on that interface as possible source addresses.
103 setIPv6UseOutgoingInterfaceAddrsOnly("1");
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -0700104}
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900105
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900106int InterfaceController::setEnableIPv6(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 // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
112 // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
113 // addresses and routes and disables IPv6 on the interface.
114 const char *disable_ipv6 = on ? "0" : "1";
115 return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6);
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900116}
117
Erik Kline2c5aaa12016-06-08 13:24:45 +0900118int InterfaceController::setAcceptIPv6Ra(const char *interface, const int on) {
119 if (!isIfaceName(interface)) {
120 errno = ENOENT;
121 return -1;
122 }
123 // Because forwarding can be enabled even when tethering is off, we always
124 // use mode "2" (accept RAs, even if forwarding is enabled).
125 const char *accept_ra = on ? "2" : "0";
126 return writeValueToPath(ipv6_proc_path, interface, "accept_ra", accept_ra);
127}
128
129int InterfaceController::setAcceptIPv6Dad(const char *interface, const int on) {
130 if (!isIfaceName(interface)) {
131 errno = ENOENT;
132 return -1;
133 }
134 const char *accept_dad = on ? "1" : "0";
135 return writeValueToPath(ipv6_proc_path, interface, "accept_dad", accept_dad);
136}
137
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900138int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
Erik Klinee1da4842015-05-12 15:56:06 +0900139 if (!isIfaceName(interface)) {
140 errno = ENOENT;
141 return -1;
142 }
143 // 0: disable IPv6 privacy addresses
144 // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
145 return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0");
Lorenzo Colitti70afde62013-03-04 17:58:40 +0900146}
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900147
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +0900148// Enables or disables IPv6 ND offload. This is useful for 464xlat on wifi, IPv6 tethering, and
149// generally implementing IPv6 neighbour discovery and duplicate address detection properly.
150// TODO: This should be implemented in wpa_supplicant via driver commands instead.
151int InterfaceController::setIPv6NdOffload(char* interface, const int on) {
152 // Only supported on Broadcom chipsets via wlutil for now.
153 if (access(wl_util_path, X_OK) == 0) {
154 const char *argv[] = {
155 wl_util_path,
156 "-a",
157 interface,
158 "ndoe",
159 on ? "1" : "0"
160 };
161 int ret = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL,
162 false, false);
163 ALOGD("%s ND offload on %s: %d (%s)",
164 (on ? "enabling" : "disabling"), interface, ret, strerror(errno));
165 return ret;
166 } else {
167 return 0;
168 }
169}
170
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700171void InterfaceController::setAcceptRA(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900172 setOnAllInterfaces(ipv6_proc_path, "accept_ra", value);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700173}
174
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700175// |tableOrOffset| is interpreted as:
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700176// If == 0: default. Routes go into RT6_TABLE_MAIN.
177// If > 0: user set. Routes go into the specified table.
178// If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
179// ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
180// table 1005, etc.
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700181void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
Erik Klinee1da4842015-05-12 15:56:06 +0900182 std::string value(StringPrintf("%d", tableOrOffset));
183 setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str());
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900184}
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700185
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700186int InterfaceController::setMtu(const char *interface, const char *mtu)
187{
Erik Klinee1da4842015-05-12 15:56:06 +0900188 if (!isIfaceName(interface)) {
189 errno = ENOENT;
190 return -1;
191 }
192 return writeValueToPath(sys_net_path, interface, "mtu", mtu);
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700193}
Erik Kline59273ed2014-12-08 16:05:28 +0900194
Erik Kline145fd252015-05-12 15:58:49 +0900195void InterfaceController::setBaseReachableTimeMs(unsigned int millis) {
196 std::string value(StringPrintf("%u", millis));
197 setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
198 setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
199}
200
Erik Kline59273ed2014-12-08 16:05:28 +0900201void InterfaceController::setIPv6OptimisticMode(const char *value) {
Erik Klinee1da4842015-05-12 15:56:06 +0900202 setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value);
203 setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value);
Erik Kline59273ed2014-12-08 16:05:28 +0900204}