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