InterfaceController.cpp revision be95c1599783ffd4cf0661db263c2e6783e27d6e
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 <utils/file.h>
24#include <utils/stringprintf.h>
25
26#include "InterfaceController.h"
27#include "RouteController.h"
28
29const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
30
31const char sys_net_path[] = "/sys/class/net";
32
33InterfaceController::InterfaceController() {
34	// Initial IPv6 settings.
35	// By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
36	// This causes RAs to work or not work based on whether forwarding is on, and causes routes
37	// learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
38	// by always setting accept_ra to 2.
39	setAcceptRA("2");
40
41	setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
42}
43
44InterfaceController::~InterfaceController() {
45}
46
47int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) {
48	if (!isIfaceName(interface)) {
49		errno = ENOENT;
50		return -1;
51	}
52	std::string path(android::StringPrintf("%s/%s/%s", ipv6_proc_path, interface, setting));
53	return android::WriteStringToFile(value, path);
54}
55
56int InterfaceController::setEnableIPv6(const char *interface, const int on) {
57	// When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
58	// When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
59	// addresses and routes and disables IPv6 on the interface.
60	const char *disable_ipv6 = on ? "0" : "1";
61	return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6);
62}
63
64int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
65	// 0: disable IPv6 privacy addresses
66	// 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
67	return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0");
68}
69
70int InterfaceController::isInterfaceName(const char *name) {
71	return strcmp(name, ".") &&
72		strcmp(name, "..") &&
73		strcmp(name, "default") &&
74		strcmp(name, "all");
75}
76
77void InterfaceController::setOnAllInterfaces(const char* filename, const char* value) {
78	// Set the default value, which is used by any interfaces that are created in the future.
79	writeIPv6ProcPath("default", filename, value);
80
81	// Set the value on all the interfaces that currently exist.
82	DIR* dir = opendir(ipv6_proc_path);
83	if (!dir) {
84		ALOGE("Can't list %s: %s", ipv6_proc_path, strerror(errno));
85		return;
86	}
87	dirent* d;
88	while ((d = readdir(dir))) {
89		if (d->d_type == DT_DIR && isInterfaceName(d->d_name)) {
90			writeIPv6ProcPath(d->d_name, filename, value);
91		}
92	}
93	closedir(dir);
94}
95
96void InterfaceController::setAcceptRA(const char *value) {
97	setOnAllInterfaces("accept_ra", value);
98}
99
100// |tableOrOffset| is interpreted as:
101//     If == 0: default. Routes go into RT6_TABLE_MAIN.
102//     If > 0: user set. Routes go into the specified table.
103//     If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
104//             ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
105//             table 1005, etc.
106void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
107	std::string value(android::StringPrintf("%d", tableOrOffset));
108	setOnAllInterfaces("accept_ra_rt_table", value.c_str());
109}
110
111int InterfaceController::setMtu(const char *interface, const char *mtu)
112{
113	if (!isIfaceName(interface)) {
114		errno = ENOENT;
115		return -1;
116	}
117	std::string path(android::StringPrintf("%s/%s/mtu", sys_net_path, interface));
118	return android::WriteStringToFile(mtu, path);
119}
120