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" |
| 22 | #include <cutils/log.h> |
Elliott Hughes | d161406 | 2015-02-02 18:08:59 -0800 | [diff] [blame] | 23 | #include <utils/file.h> |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 24 | |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 25 | #include "InterfaceController.h" |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 26 | #include "RouteController.h" |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 27 | |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 28 | const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf"; |
| 29 | |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 30 | const char sys_net_path[] = "/sys/class/net"; |
| 31 | |
Sreeram Ramachandran | 1604e18 | 2014-07-19 23:22:33 -0700 | [diff] [blame] | 32 | InterfaceController::InterfaceController() { |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 33 | // Initial IPv6 settings. |
| 34 | // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces. |
| 35 | // This causes RAs to work or not work based on whether forwarding is on, and causes routes |
| 36 | // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable |
| 37 | // by always setting accept_ra to 2. |
| 38 | setAcceptRA("2"); |
| 39 | |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 40 | setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX); |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | InterfaceController::~InterfaceController() { |
Dmitry Shmidt | 2eab1f7 | 2012-07-26 16:08:02 -0700 | [diff] [blame] | 44 | } |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 45 | |
| 46 | int InterfaceController::writeIPv6ProcPath(const char *interface, const char *setting, const char *value) { |
| 47 | char *path; |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 48 | if (!isIfaceName(interface)) { |
| 49 | errno = ENOENT; |
| 50 | return -1; |
| 51 | } |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 52 | asprintf(&path, "%s/%s/%s", ipv6_proc_path, interface, setting); |
Elliott Hughes | d161406 | 2015-02-02 18:08:59 -0800 | [diff] [blame] | 53 | bool success = android::WriteStringToFile(value, path); |
Lorenzo Colitti | 70afde6 | 2013-03-04 17:58:40 +0900 | [diff] [blame] | 54 | free(path); |
| 55 | return success; |
| 56 | } |
| 57 | |
| 58 | int InterfaceController::setEnableIPv6(const char *interface, const int on) { |
| 59 | // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf. |
| 60 | // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf |
| 61 | // addresses and routes and disables IPv6 on the interface. |
| 62 | const char *disable_ipv6 = on ? "0" : "1"; |
| 63 | return writeIPv6ProcPath(interface, "disable_ipv6", disable_ipv6); |
| 64 | } |
| 65 | |
| 66 | int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) { |
| 67 | // 0: disable IPv6 privacy addresses |
| 68 | // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones. |
| 69 | return writeIPv6ProcPath(interface, "use_tempaddr", on ? "2" : "0"); |
| 70 | } |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 71 | |
| 72 | int InterfaceController::isInterfaceName(const char *name) { |
| 73 | return strcmp(name, ".") && |
| 74 | strcmp(name, "..") && |
| 75 | strcmp(name, "default") && |
| 76 | strcmp(name, "all"); |
| 77 | } |
| 78 | |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 79 | void InterfaceController::setOnAllInterfaces(const char* filename, const char* value) { |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 80 | // 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] | 81 | writeIPv6ProcPath("default", filename, value); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 82 | |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 83 | // Set the value on all the interfaces that currently exist. |
| 84 | DIR* dir = opendir(ipv6_proc_path); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 85 | if (!dir) { |
| 86 | ALOGE("Can't list %s: %s", ipv6_proc_path, strerror(errno)); |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 87 | return; |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 88 | } |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 89 | dirent* d; |
| 90 | while ((d = readdir(dir))) { |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 91 | if (d->d_type == DT_DIR && isInterfaceName(d->d_name)) { |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 92 | writeIPv6ProcPath(d->d_name, filename, value); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | closedir(dir); |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void InterfaceController::setAcceptRA(const char *value) { |
| 99 | setOnAllInterfaces("accept_ra", value); |
| 100 | } |
| 101 | |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 102 | // |tableOrOffset| is interpreted as: |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 103 | // If == 0: default. Routes go into RT6_TABLE_MAIN. |
| 104 | // If > 0: user set. Routes go into the specified table. |
| 105 | // If < 0: automatic. The absolute value is intepreted as an offset and added to the interface |
| 106 | // ID to get the table. If it's set to -1000, routes from interface ID 5 will go into |
| 107 | // table 1005, etc. |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 108 | void InterfaceController::setAcceptRARouteTable(int tableOrOffset) { |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 109 | char* value; |
Sreeram Ramachandran | a01d6ef | 2014-04-10 19:37:59 -0700 | [diff] [blame] | 110 | asprintf(&value, "%d", tableOrOffset); |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 111 | setOnAllInterfaces("accept_ra_rt_table", value); |
| 112 | free(value); |
Lorenzo Colitti | 37f2e37 | 2013-04-12 00:44:06 +0900 | [diff] [blame] | 113 | } |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 114 | |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 115 | int InterfaceController::setMtu(const char *interface, const char *mtu) |
| 116 | { |
| 117 | char *path; |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 118 | if (!isIfaceName(interface)) { |
| 119 | errno = ENOENT; |
| 120 | return -1; |
| 121 | } |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 122 | asprintf(&path, "%s/%s/mtu", sys_net_path, interface); |
Elliott Hughes | d161406 | 2015-02-02 18:08:59 -0800 | [diff] [blame] | 123 | bool success = android::WriteStringToFile(mtu, path); |
Dmitry Shmidt | 6d6c0e6 | 2013-06-11 16:18:06 -0700 | [diff] [blame] | 124 | free(path); |
| 125 | return success; |
| 126 | } |