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 | |
| 17 | #include <stdlib.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <string.h> |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 21 | #include <dirent.h> |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 22 | |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 23 | #include <dlfcn.h> |
| 24 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 25 | #include <sys/socket.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #include <netinet/in.h> |
| 31 | #include <arpa/inet.h> |
| 32 | |
| 33 | #define LOG_TAG "InterfaceController" |
| 34 | #include <cutils/log.h> |
| 35 | #include <netutils/ifc.h> |
| 36 | #include <private/android_filesystem_config.h> |
| 37 | |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 38 | #include "NetdConstants.h" |
| 39 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 40 | #include "InterfaceController.h" |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 41 | #include "RouteController.h" |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 42 | |
Sasha Levitskiy | 17609ed | 2014-04-21 11:03:56 -0700 | [diff] [blame] | 43 | char if_cmd_lib_file_name[] = "libnetcmdiface.so"; |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 44 | char set_cmd_func_name[] = "net_iface_send_command"; |
| 45 | char set_cmd_init_func_name[] = "net_iface_send_command_init"; |
| 46 | char set_cmd_fini_func_name[] = "net_iface_send_command_fini"; |
| 47 | |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 48 | const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf"; |
| 49 | |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 50 | const char sys_net_path[] = "/sys/class/net"; |
| 51 | |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 52 | InterfaceController::InterfaceController() |
| 53 | : sendCommand_(NULL) { |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 54 | // Initial IPv6 settings. |
| 55 | // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces. |
| 56 | // This causes RAs to work or not work based on whether forwarding is on, and causes routes |
| 57 | // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable |
| 58 | // by always setting accept_ra to 2. |
| 59 | setAcceptRA("2"); |
| 60 | |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 61 | libh_ = dlopen(if_cmd_lib_file_name, RTLD_NOW | RTLD_LOCAL); |
| 62 | if (libh_ == NULL) { |
| 63 | const char *err_str = dlerror(); |
Sasha Levitskiy | 87482aa | 2012-09-11 11:04:29 -0700 | [diff] [blame] | 64 | ALOGW("Warning (%s) while opening the net interface command library", err_str ? err_str : "unknown"); |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 65 | } else { |
| 66 | sendCommandInit_ = (int (*)(void))dlsym(libh_, set_cmd_init_func_name); |
| 67 | if (sendCommandInit_ == NULL) { |
| 68 | const char *err_str = dlerror(); |
| 69 | ALOGW("Error (%s) while searching for the interface command init function", err_str ? err_str : "unknown"); |
| 70 | } else if (sendCommandInit_()) { |
| 71 | ALOGE("Can't init the interface command API"); |
| 72 | return; |
| 73 | } |
| 74 | sendCommandFini_ = (int (*)(void))dlsym(libh_, set_cmd_fini_func_name); |
| 75 | if (sendCommandFini_ == NULL) { |
| 76 | const char *err_str = dlerror(); |
| 77 | ALOGW("Error (%s) while searching for the interface command fini function", err_str ? err_str : "unknown"); |
| 78 | } |
| 79 | sendCommand_ = (int (*)(int, char **, char **))dlsym(libh_, set_cmd_func_name); |
| 80 | if (sendCommand_ == NULL) { |
| 81 | const char *err_str = dlerror(); |
| 82 | ALOGE("Error (%s) while searching for the interface command function", err_str ? err_str : "unknown"); |
| 83 | return; |
| 84 | } |
| 85 | } |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 86 | |
| 87 | setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX); |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | InterfaceController::~InterfaceController() { |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 91 | if (sendCommandFini_) { |
| 92 | if (sendCommandFini_()) { |
| 93 | ALOGE("Can't shutdown the interface command API"); |
| 94 | } |
| 95 | } |
| 96 | if (libh_) { |
| 97 | int err = dlclose(libh_); |
| 98 | if (err) { |
| 99 | const char *err_str = dlerror(); |
| 100 | ALOGE("Error (%s) while closing the net interface command library", err_str ? err_str : "unknown"); |
| 101 | } |
| 102 | } |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Arguments: |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 107 | * argv[2] - wlan interface |
| 108 | * argv[3] - command |
| 109 | * argv[4] - argument |
| 110 | * rbuf - returned buffer |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 111 | */ |
| 112 | int InterfaceController::interfaceCommand(int argc, char *argv[], char **rbuf) { |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 113 | int ret = -ENOSYS; |
| 114 | if (sendCommand_) |
| 115 | ret = sendCommand_(argc, argv, rbuf); |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 116 | |
Sasha Levitskiy | 329c3b4 | 2012-07-30 16:11:23 -0700 | [diff] [blame] | 117 | return ret; |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 118 | } |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 119 | |
| 120 | int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) { |
| 121 | char *path; |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 122 | asprintf(&path, "%s/%s/%s", ipv6_proc_path, interface, setting); |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 123 | int success = writeFile(path, value, strlen(value)); |
| 124 | free(path); |
| 125 | return success; |
| 126 | } |
| 127 | |
| 128 | int InterfaceController::setEnableIPv6(const char *interface, const int on) { |
| 129 | // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf. |
| 130 | // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf |
| 131 | // addresses and routes and disables IPv6 on the interface. |
| 132 | const char *disable_ipv6 = on ? "0" : "1"; |
| 133 | return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6); |
| 134 | } |
| 135 | |
| 136 | int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) { |
| 137 | // 0: disable IPv6 privacy addresses |
| 138 | // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones. |
| 139 | return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0"); |
| 140 | } |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 141 | |
| 142 | int InterfaceController::isInterfaceName(const char *name) { |
| 143 | return strcmp(name, ".") && |
| 144 | strcmp(name, "..") && |
| 145 | strcmp(name, "default") && |
| 146 | strcmp(name, "all"); |
| 147 | } |
| 148 | |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 149 | void InterfaceController::setOnAllInterfaces(const char* filename, const char* value) { |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 150 | // 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] | 151 | writeIPv6ProcPath("default", filename, value); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 152 | |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 153 | // Set the value on all the interfaces that currently exist. |
| 154 | DIR* dir = opendir(ipv6_proc_path); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 155 | if (!dir) { |
| 156 | ALOGE("Can't list %s: %s", ipv6_proc_path, strerror(errno)); |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 157 | return; |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 158 | } |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 159 | dirent* d; |
| 160 | while ((d = readdir(dir))) { |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 161 | if (d->d_type == DT_DIR && isInterfaceName(d->d_name)) { |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 162 | writeIPv6ProcPath(d->d_name, filename, value); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | closedir(dir); |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void InterfaceController::setAcceptRA(const char *value) { |
| 169 | setOnAllInterfaces("accept_ra", value); |
| 170 | } |
| 171 | |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame^] | 172 | // |tableOrOffset| is interpreted as: |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 173 | // If == 0: default. Routes go into RT6_TABLE_MAIN. |
| 174 | // If > 0: user set. Routes go into the specified table. |
| 175 | // If < 0: automatic. The absolute value is intepreted as an offset and added to the interface |
| 176 | // ID to get the table. If it's set to -1000, routes from interface ID 5 will go into |
| 177 | // table 1005, etc. |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame^] | 178 | void InterfaceController::setAcceptRARouteTable(int tableOrOffset) { |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 179 | char* value; |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame^] | 180 | asprintf(&value, "%d", tableOrOffset); |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 181 | setOnAllInterfaces("accept_ra_rt_table", value); |
| 182 | free(value); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 183 | } |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 184 | |
| 185 | int InterfaceController::getMtu(const char *interface, int *mtu) |
| 186 | { |
| 187 | char buf[16]; |
| 188 | int size = sizeof(buf); |
| 189 | char *path; |
| 190 | asprintf(&path, "%s/%s/mtu", sys_net_path, interface); |
| 191 | int success = readFile(path, buf, &size); |
| 192 | if (!success && mtu) |
| 193 | *mtu = atoi(buf); |
| 194 | free(path); |
| 195 | return success; |
| 196 | |
| 197 | } |
| 198 | |
| 199 | int InterfaceController::setMtu(const char *interface, const char *mtu) |
| 200 | { |
| 201 | char *path; |
| 202 | asprintf(&path, "%s/%s/mtu", sys_net_path, interface); |
| 203 | int success = writeFile(path, mtu, strlen(mtu)); |
| 204 | free(path); |
| 205 | return success; |
| 206 | } |