blob: 061060f8188aa6326eedac8b11f25ca7edf90571 [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 Colitti70afde62013-03-04 17:58:40 +090021
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070022#include "InterfaceController.h"
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070023#include "RouteController.h"
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070024
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090025const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
26
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -070027const char sys_net_path[] = "/sys/class/net";
28
Sreeram Ramachandran1604e182014-07-19 23:22:33 -070029InterfaceController::InterfaceController() {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090030 // Initial IPv6 settings.
31 // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
32 // This causes RAs to work or not work based on whether forwarding is on, and causes routes
33 // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
34 // by always setting accept_ra to 2.
35 setAcceptRA("2");
36
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070037 setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070038}
39
40InterfaceController::~InterfaceController() {
Dmitry Shmidt2eab1f72012-07-26 16:08:02 -070041}
Lorenzo Colitti70afde62013-03-04 17:58:40 +090042
43int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) {
44 char *path;
JP Abgrall69261cb2014-06-19 18:35:24 -070045 if (!isIfaceName(interface)) {
46 errno = ENOENT;
47 return -1;
48 }
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090049 asprintf(&path, "%s/%s/%s", ipv6_proc_path, interface, setting);
Lorenzo Colitti70afde62013-03-04 17:58:40 +090050 int success = writeFile(path, value, strlen(value));
51 free(path);
52 return success;
53}
54
55int InterfaceController::setEnableIPv6(const char *interface, const int on) {
56 // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
57 // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
58 // addresses and routes and disables IPv6 on the interface.
59 const char *disable_ipv6 = on ? "0" : "1";
60 return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6);
61}
62
63int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
64 // 0: disable IPv6 privacy addresses
65 // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
66 return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0");
67}
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090068
69int InterfaceController::isInterfaceName(const char *name) {
70 return strcmp(name, ".") &&
71 strcmp(name, "..") &&
72 strcmp(name, "default") &&
73 strcmp(name, "all");
74}
75
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070076void InterfaceController::setOnAllInterfaces(const char* filename, const char* value) {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090077 // Set the default value, which is used by any interfaces that are created in the future.
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070078 writeIPv6ProcPath("default", filename, value);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090079
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070080 // Set the value on all the interfaces that currently exist.
81 DIR* dir = opendir(ipv6_proc_path);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090082 if (!dir) {
83 ALOGE("Can't list %s: %s", ipv6_proc_path, strerror(errno));
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070084 return;
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090085 }
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070086 dirent* d;
87 while ((d = readdir(dir))) {
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090088 if (d->d_type == DT_DIR && isInterfaceName(d->d_name)) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070089 writeIPv6ProcPath(d->d_name, filename, value);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +090090 }
91 }
92 closedir(dir);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070093}
94
95void InterfaceController::setAcceptRA(const char *value) {
96 setOnAllInterfaces("accept_ra", value);
97}
98
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -070099// |tableOrOffset| is interpreted as:
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700100// If == 0: default. Routes go into RT6_TABLE_MAIN.
101// If > 0: user set. Routes go into the specified table.
102// If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
103// ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
104// table 1005, etc.
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700105void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700106 char* value;
Sreeram Ramachandrana01d6ef2014-04-10 19:37:59 -0700107 asprintf(&value, "%d", tableOrOffset);
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700108 setOnAllInterfaces("accept_ra_rt_table", value);
109 free(value);
Lorenzo Colitti37f2e372013-04-12 00:44:06 +0900110}
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700111
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700112int InterfaceController::setMtu(const char *interface, const char *mtu)
113{
114 char *path;
JP Abgrall69261cb2014-06-19 18:35:24 -0700115 if (!isIfaceName(interface)) {
116 errno = ENOENT;
117 return -1;
118 }
Dmitry Shmidt6d6c0e62013-06-11 16:18:06 -0700119 asprintf(&path, "%s/%s/mtu", sys_net_path, interface);
120 int success = writeFile(path, mtu, strlen(mtu));
121 free(path);
122 return success;
123}