InterfaceController.cpp revision 3f95777d2aafa6c0ac4671d55557cad0d04a223f
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 *
8 *	  http://www.apache.org/licenses/LICENSE-2.0
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
17#include <dirent.h>
18#include <errno.h>
19#include <malloc.h>
20
21#define LOG_TAG "InterfaceController"
22#include <base/file.h>
23#include <base/stringprintf.h>
24#include <cutils/log.h>
25#include <logwrap/logwrap.h>
26
27#include "InterfaceController.h"
28#include "RouteController.h"
29
30using android::base::StringPrintf;
31using android::base::WriteStringToFile;
32
33namespace {
34
35const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
36
37const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh";
38
39const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh";
40
41const char sys_net_path[] = "/sys/class/net";
42
43const char wl_util_path[] = "/system/xbin/wlutil";
44
45bool 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));
56    return WriteStringToFile(value, path) ? 0 : -1;
57}
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
79}  // namespace
80
81InterfaceController::InterfaceController() {
82	// Initial IPv6 settings.
83	// By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
84	// This causes RAs to work or not work based on whether forwarding is on, and causes routes
85	// learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
86	// by always setting accept_ra to 2.
87	setAcceptRA("2");
88
89	setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
90
91	// Enable optimistic DAD for IPv6 addresses on all interfaces.
92	setIPv6OptimisticMode("1");
93
94	// Reduce the ARP/ND base reachable time from the default (30sec) to 15sec.
95	setBaseReachableTimeMs(15 * 1000);
96}
97
98InterfaceController::~InterfaceController() {
99}
100
101int InterfaceController::setEnableIPv6(const char *interface, const int on) {
102    if (!isIfaceName(interface)) {
103        errno = ENOENT;
104        return -1;
105    }
106    // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
107    // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
108    // addresses and routes and disables IPv6 on the interface.
109    const char *disable_ipv6 = on ? "0" : "1";
110    return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6);
111}
112
113int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
114    if (!isIfaceName(interface)) {
115        errno = ENOENT;
116        return -1;
117    }
118    // 0: disable IPv6 privacy addresses
119    // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
120    return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0");
121}
122
123// Enables or disables IPv6 ND offload. This is useful for 464xlat on wifi, IPv6 tethering, and
124// generally implementing IPv6 neighbour discovery and duplicate address detection properly.
125// TODO: This should be implemented in wpa_supplicant via driver commands instead.
126int InterfaceController::setIPv6NdOffload(char* interface, const int on) {
127    // Only supported on Broadcom chipsets via wlutil for now.
128    if (access(wl_util_path, X_OK) == 0) {
129        const char *argv[] = {
130            wl_util_path,
131            "-a",
132            interface,
133            "ndoe",
134            on ? "1" : "0"
135        };
136        int ret = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL,
137                                      false, false);
138        ALOGD("%s ND offload on %s: %d (%s)",
139              (on ? "enabling" : "disabling"), interface, ret, strerror(errno));
140        return ret;
141    } else {
142        return 0;
143    }
144}
145
146void InterfaceController::setAcceptRA(const char *value) {
147    setOnAllInterfaces(ipv6_proc_path, "accept_ra", value);
148}
149
150// |tableOrOffset| is interpreted as:
151//     If == 0: default. Routes go into RT6_TABLE_MAIN.
152//     If > 0: user set. Routes go into the specified table.
153//     If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
154//             ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
155//             table 1005, etc.
156void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
157    std::string value(StringPrintf("%d", tableOrOffset));
158    setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str());
159}
160
161int InterfaceController::setMtu(const char *interface, const char *mtu)
162{
163    if (!isIfaceName(interface)) {
164        errno = ENOENT;
165        return -1;
166    }
167    return writeValueToPath(sys_net_path, interface, "mtu", mtu);
168}
169
170void InterfaceController::setBaseReachableTimeMs(unsigned int millis) {
171    std::string value(StringPrintf("%u", millis));
172    setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
173    setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
174}
175
176void InterfaceController::setIPv6OptimisticMode(const char *value) {
177    setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value);
178    setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value);
179}
180