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