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