blob: b7a4d0b40a39a0802fba60a6b6356e5c4df40af9 [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>
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070018
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070019#define LOG_TAG "InterfaceController"
20#include <cutils/log.h>
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090021#include <logwrap/logwrap.h>
Lorenzo Colitti70afde62013-03-04 17:58:40 +090022
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070023#include "InterfaceController.h"
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070024#include "RouteController.h"
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070025
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090026const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
27
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -070028const char sys_net_path[] = "/sys/class/net";
29
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090030const char wl_util_path[] = "/system/xbin/wlutil";
31
Sreeram Ramachandran1604e182014-07-19 23:22:33 -070032InterfaceController::InterfaceController() {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090033 // Initial IPv6 settings.
34 // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
35 // This causes RAs to work or not work based on whether forwarding is on, and causes routes
36 // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
37 // by always setting accept_ra to 2.
38 setAcceptRA("2");
39
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070040 setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
Erik Kline59273ed2014-12-08 16:05:28 +090041
42 // Enable optimistic DAD for IPv6 addresses on all interfaces.
43 setIPv6OptimisticMode("1");
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070044}
45
46InterfaceController::~InterfaceController() {
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070047}
Lorenzo Colitti70afde62013-03-04 17:58:40 +090048
49int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) {
50 char *path;
JP Abgrall69261cb2014-06-19 18:35:24 -070051 if (!isIfaceName(interface)) {
52 errno = ENOENT;
53 return -1;
54 }
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090055 asprintf(&path, "%s/%s/%s", ipv6_proc_path, interface, setting);
Lorenzo Colitti70afde62013-03-04 17:58:40 +090056 int success = writeFile(path, value, strlen(value));
57 free(path);
58 return success;
59}
60
61int InterfaceController::setEnableIPv6(const char *interface, const int on) {
62 // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
63 // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
64 // addresses and routes and disables IPv6 on the interface.
65 const char *disable_ipv6 = on ? "0" : "1";
66 return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6);
67}
68
69int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
70 // 0: disable IPv6 privacy addresses
71 // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
72 return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0");
73}
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090074
Lorenzo Colitti0ea8ff82014-10-28 00:15:07 +090075// Enables or disables IPv6 ND offload. This is useful for 464xlat on wifi, IPv6 tethering, and
76// generally implementing IPv6 neighbour discovery and duplicate address detection properly.
77// TODO: This should be implemented in wpa_supplicant via driver commands instead.
78int InterfaceController::setIPv6NdOffload(char* interface, const int on) {
79 // Only supported on Broadcom chipsets via wlutil for now.
80 if (access(wl_util_path, X_OK) == 0) {
81 const char *argv[] = {
82 wl_util_path,
83 "-a",
84 interface,
85 "ndoe",
86 on ? "1" : "0"
87 };
88 int ret = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL,
89 false, false);
90 ALOGD("%s ND offload on %s: %d (%s)",
91 (on ? "enabling" : "disabling"), interface, ret, strerror(errno));
92 return ret;
93 } else {
94 return 0;
95 }
96}
97
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090098int InterfaceController::isInterfaceName(const char *name) {
99 return strcmp(name, ".") &&
100 strcmp(name, "..") &&
101 strcmp(name, "default") &&
102 strcmp(name, "all");
103}
104
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700105void InterfaceController::setOnAllInterfaces(const char* filename, const char* value) {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900106 // Set the default value, which is used by any interfaces that are created in the future.
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700107 writeIPv6ProcPath("default", filename, value);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900108
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700109 // Set the value on all the interfaces that currently exist.
110 DIR* dir = opendir(ipv6_proc_path);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900111 if (!dir) {
112 ALOGE("Can't list %s: %s", ipv6_proc_path, strerror(errno));
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700113 return;
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900114 }
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700115 dirent* d;
116 while ((d = readdir(dir))) {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900117 if (d->d_type == DT_DIR && isInterfaceName(d->d_name)) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700118 writeIPv6ProcPath(d->d_name, filename, value);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900119 }
120 }
121 closedir(dir);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700122}
123
124void InterfaceController::setAcceptRA(const char *value) {
125 setOnAllInterfaces("accept_ra", value);
126}
127
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700128// |tableOrOffset| is interpreted as:
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700129// If == 0: default. Routes go into RT6_TABLE_MAIN.
130// If > 0: user set. Routes go into the specified table.
131// If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
132// ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
133// table 1005, etc.
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700134void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700135 char* value;
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700136 asprintf(&value, "%d", tableOrOffset);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700137 setOnAllInterfaces("accept_ra_rt_table", value);
138 free(value);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900139}
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700140
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700141int InterfaceController::setMtu(const char *interface, const char *mtu)
142{
143 char *path;
JP Abgrall69261cb2014-06-19 18:35:24 -0700144 if (!isIfaceName(interface)) {
145 errno = ENOENT;
146 return -1;
147 }
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700148 asprintf(&path, "%s/%s/mtu", sys_net_path, interface);
149 int success = writeFile(path, mtu, strlen(mtu));
150 free(path);
151 return success;
152}
Erik Kline59273ed2014-12-08 16:05:28 +0900153
154void InterfaceController::setIPv6OptimisticMode(const char *value) {
155 setOnAllInterfaces("optimistic_dad", value);
156 setOnAllInterfaces("use_optimistic", value);
157}