blob: f0b313f0c13e80a1b4c515367a1bae3fc21b3e13 [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"
22#include <cutils/log.h>
Lorenzo Colitti70afde62013-03-04 17:58:40 +090023
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070024#include "InterfaceController.h"
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070025#include "RouteController.h"
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070026
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090027const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
28
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -070029const char sys_net_path[] = "/sys/class/net";
30
Sreeram Ramachandran1604e182014-07-19 23:22:33 -070031InterfaceController::InterfaceController() {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090032 // Initial IPv6 settings.
33 // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
34 // This causes RAs to work or not work based on whether forwarding is on, and causes routes
35 // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
36 // by always setting accept_ra to 2.
37 setAcceptRA("2");
38
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070039 setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070040}
41
42InterfaceController::~InterfaceController() {
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070043}
Lorenzo Colitti70afde62013-03-04 17:58:40 +090044
45int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) {
46 char *path;
JP Abgrall69261cb2014-06-19 18:35:24 -070047 if (!isIfaceName(interface)) {
48 errno = ENOENT;
49 return -1;
50 }
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090051 asprintf(&path, "%s/%s/%s", ipv6_proc_path, interface, setting);
Lorenzo Colitti70afde62013-03-04 17:58:40 +090052 int success = writeFile(path, value, strlen(value));
53 free(path);
54 return success;
55}
56
57int InterfaceController::setEnableIPv6(const char *interface, const int on) {
58 // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
59 // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
60 // addresses and routes and disables IPv6 on the interface.
61 const char *disable_ipv6 = on ? "0" : "1";
62 return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6);
63}
64
65int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
66 // 0: disable IPv6 privacy addresses
67 // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
68 return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0");
69}
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090070
71int InterfaceController::isInterfaceName(const char *name) {
72 return strcmp(name, ".") &&
73 strcmp(name, "..") &&
74 strcmp(name, "default") &&
75 strcmp(name, "all");
76}
77
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070078void InterfaceController::setOnAllInterfaces(const char* filename, const char* value) {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090079 // Set the default value, which is used by any interfaces that are created in the future.
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070080 writeIPv6ProcPath("default", filename, value);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090081
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070082 // Set the value on all the interfaces that currently exist.
83 DIR* dir = opendir(ipv6_proc_path);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090084 if (!dir) {
85 ALOGE("Can't list %s: %s", ipv6_proc_path, strerror(errno));
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070086 return;
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090087 }
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070088 dirent* d;
89 while ((d = readdir(dir))) {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090090 if (d->d_type == DT_DIR && isInterfaceName(d->d_name)) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070091 writeIPv6ProcPath(d->d_name, filename, value);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090092 }
93 }
94 closedir(dir);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070095}
96
97void InterfaceController::setAcceptRA(const char *value) {
98 setOnAllInterfaces("accept_ra", value);
99}
100
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700101// |tableOrOffset| is interpreted as:
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700102// If == 0: default. Routes go into RT6_TABLE_MAIN.
103// If > 0: user set. Routes go into the specified table.
104// If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
105// ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
106// table 1005, etc.
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700107void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700108 char* value;
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700109 asprintf(&value, "%d", tableOrOffset);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700110 setOnAllInterfaces("accept_ra_rt_table", value);
111 free(value);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900112}
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700113
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700114int InterfaceController::setMtu(const char *interface, const char *mtu)
115{
116 char *path;
JP Abgrall69261cb2014-06-19 18:35:24 -0700117 if (!isIfaceName(interface)) {
118 errno = ENOENT;
119 return -1;
120 }
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700121 asprintf(&path, "%s/%s/mtu", sys_net_path, interface);
122 int success = writeFile(path, mtu, strlen(mtu));
123 free(path);
124 return success;
125}