InterfaceController.cpp revision 4a0ab5ff4a87cfc4a987da99546b01e44875a2e5
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 <cutils/log.h>
23#include <logwrap/logwrap.h>
24#include <utils/file.h>
25#include <utils/stringprintf.h>
26
27#include "InterfaceController.h"
28#include "RouteController.h"
29
30const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
31
32const char sys_net_path[] = "/sys/class/net";
33
34const char wl_util_path[] = "/system/xbin/wlutil";
35
36InterfaceController::InterfaceController() {
37	// Initial IPv6 settings.
38	// By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
39	// This causes RAs to work or not work based on whether forwarding is on, and causes routes
40	// learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
41	// by always setting accept_ra to 2.
42	setAcceptRA("2");
43
44	setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
45
46	// Enable optimistic DAD for IPv6 addresses on all interfaces.
47	setIPv6OptimisticMode("1");
48}
49
50InterfaceController::~InterfaceController() {
51}
52
53int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) {
54	if (!isIfaceName(interface)) {
55		errno = ENOENT;
56		return -1;
57	}
58	std::string path(android::StringPrintf("%s/%s/%s", ipv6_proc_path, interface, setting));
59	return android::WriteStringToFile(value, path);
60}
61
62int InterfaceController::setEnableIPv6(const char *interface, const int on) {
63	// When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
64	// When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
65	// addresses and routes and disables IPv6 on the interface.
66	const char *disable_ipv6 = on ? "0" : "1";
67	return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6);
68}
69
70int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
71	// 0: disable IPv6 privacy addresses
72	// 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
73	return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0");
74}
75
76// Enables or disables IPv6 ND offload. This is useful for 464xlat on wifi, IPv6 tethering, and
77// generally implementing IPv6 neighbour discovery and duplicate address detection properly.
78// TODO: This should be implemented in wpa_supplicant via driver commands instead.
79int InterfaceController::setIPv6NdOffload(char* interface, const int on) {
80    // Only supported on Broadcom chipsets via wlutil for now.
81    if (access(wl_util_path, X_OK) == 0) {
82        const char *argv[] = {
83            wl_util_path,
84            "-a",
85            interface,
86            "ndoe",
87            on ? "1" : "0"
88        };
89        int ret = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL,
90                                      false, false);
91        ALOGD("%s ND offload on %s: %d (%s)",
92              (on ? "enabling" : "disabling"), interface, ret, strerror(errno));
93        return ret;
94    } else {
95        return 0;
96    }
97}
98
99int InterfaceController::isInterfaceName(const char *name) {
100	return strcmp(name, ".") &&
101		strcmp(name, "..") &&
102		strcmp(name, "default") &&
103		strcmp(name, "all");
104}
105
106void InterfaceController::setOnAllInterfaces(const char* filename, const char* value) {
107	// Set the default value, which is used by any interfaces that are created in the future.
108	writeIPv6ProcPath("default", filename, value);
109
110	// Set the value on all the interfaces that currently exist.
111	DIR* dir = opendir(ipv6_proc_path);
112	if (!dir) {
113		ALOGE("Can't list %s: %s", ipv6_proc_path, strerror(errno));
114		return;
115	}
116	dirent* d;
117	while ((d = readdir(dir))) {
118		if (d->d_type == DT_DIR && isInterfaceName(d->d_name)) {
119			writeIPv6ProcPath(d->d_name, filename, value);
120		}
121	}
122	closedir(dir);
123}
124
125void InterfaceController::setAcceptRA(const char *value) {
126	setOnAllInterfaces("accept_ra", value);
127}
128
129// |tableOrOffset| is interpreted as:
130//     If == 0: default. Routes go into RT6_TABLE_MAIN.
131//     If > 0: user set. Routes go into the specified table.
132//     If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
133//             ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
134//             table 1005, etc.
135void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
136	std::string value(android::StringPrintf("%d", tableOrOffset));
137	setOnAllInterfaces("accept_ra_rt_table", value.c_str());
138}
139
140int InterfaceController::setMtu(const char *interface, const char *mtu)
141{
142	if (!isIfaceName(interface)) {
143		errno = ENOENT;
144		return -1;
145	}
146	std::string path(android::StringPrintf("%s/%s/mtu", sys_net_path, interface));
147	return android::WriteStringToFile(mtu, path);
148}
149
150void InterfaceController::setIPv6OptimisticMode(const char *value) {
151	setOnAllInterfaces("optimistic_dad", value);
152	setOnAllInterfaces("use_optimistic", value);
153}
154