Home | History | Annotate | Download | only in server
      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 #include <sys/socket.h>
     21 
     22 #define LOG_TAG "InterfaceController"
     23 #include <android-base/file.h>
     24 #include <android-base/stringprintf.h>
     25 #include <cutils/log.h>
     26 #include <logwrap/logwrap.h>
     27 #include <netutils/ifc.h>
     28 
     29 #include "InterfaceController.h"
     30 #include "RouteController.h"
     31 
     32 using android::base::StringPrintf;
     33 using android::base::ReadFileToString;
     34 using android::base::WriteStringToFile;
     35 
     36 namespace {
     37 
     38 const char ipv6_proc_path[] = "/proc/sys/net/ipv6/conf";
     39 
     40 const char ipv4_neigh_conf_dir[] = "/proc/sys/net/ipv4/neigh";
     41 
     42 const char ipv6_neigh_conf_dir[] = "/proc/sys/net/ipv6/neigh";
     43 
     44 const char proc_net_path[] = "/proc/sys/net";
     45 const char sys_net_path[] = "/sys/class/net";
     46 
     47 const char wl_util_path[] = "/vendor/xbin/wlutil";
     48 
     49 inline bool isNormalPathComponent(const char *component) {
     50     return (strcmp(component, ".") != 0) &&
     51            (strcmp(component, "..") != 0) &&
     52            (strchr(component, '/') == nullptr);
     53 }
     54 
     55 inline bool isAddressFamilyPathComponent(const char *component) {
     56     return strcmp(component, "ipv4") == 0 || strcmp(component, "ipv6") == 0;
     57 }
     58 
     59 inline bool isInterfaceName(const char *name) {
     60     return isNormalPathComponent(name) &&
     61            (strcmp(name, "default") != 0) &&
     62            (strcmp(name, "all") != 0);
     63 }
     64 
     65 int writeValueToPath(
     66         const char* dirname, const char* subdirname, const char* basename,
     67         const char* value) {
     68     std::string path(StringPrintf("%s/%s/%s", dirname, subdirname, basename));
     69     return WriteStringToFile(value, path) ? 0 : -1;
     70 }
     71 
     72 void setOnAllInterfaces(const char* dirname, const char* basename, const char* value) {
     73     // Set the default value, which is used by any interfaces that are created in the future.
     74     writeValueToPath(dirname, "default", basename, value);
     75 
     76     // Set the value on all the interfaces that currently exist.
     77     DIR* dir = opendir(dirname);
     78     if (!dir) {
     79         ALOGE("Can't list %s: %s", dirname, strerror(errno));
     80         return;
     81     }
     82     dirent* d;
     83     while ((d = readdir(dir))) {
     84         if ((d->d_type != DT_DIR) || !isInterfaceName(d->d_name)) {
     85             continue;
     86         }
     87         writeValueToPath(dirname, d->d_name, basename, value);
     88     }
     89     closedir(dir);
     90 }
     91 
     92 void setIPv6UseOutgoingInterfaceAddrsOnly(const char *value) {
     93     setOnAllInterfaces(ipv6_proc_path, "use_oif_addrs_only", value);
     94 }
     95 
     96 std::string getParameterPathname(
     97         const char *family, const char *which, const char *interface, const char *parameter) {
     98     if (!isAddressFamilyPathComponent(family)) {
     99         errno = EAFNOSUPPORT;
    100         return "";
    101     } else if (!isNormalPathComponent(which) ||
    102                !isInterfaceName(interface) ||
    103                !isNormalPathComponent(parameter)) {
    104         errno = EINVAL;
    105         return "";
    106     }
    107 
    108     return StringPrintf("%s/%s/%s/%s/%s", proc_net_path, family, which, interface, parameter);
    109 }
    110 
    111 }  // namespace
    112 
    113 void InterfaceController::initializeAll() {
    114     // Initial IPv6 settings.
    115     // By default, accept_ra is set to 1 (accept RAs unless forwarding is on) on all interfaces.
    116     // This causes RAs to work or not work based on whether forwarding is on, and causes routes
    117     // learned from RAs to go away when forwarding is turned on. Make this behaviour predictable
    118     // by always setting accept_ra to 2.
    119     setAcceptRA("2");
    120 
    121     setAcceptRARouteTable(-RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX);
    122 
    123     // Enable optimistic DAD for IPv6 addresses on all interfaces.
    124     setIPv6OptimisticMode("1");
    125 
    126     // Reduce the ARP/ND base reachable time from the default (30sec) to 15sec.
    127     setBaseReachableTimeMs(15 * 1000);
    128 
    129     // When sending traffic via a given interface use only addresses configured
    130        // on that interface as possible source addresses.
    131     setIPv6UseOutgoingInterfaceAddrsOnly("1");
    132 }
    133 
    134 int InterfaceController::setEnableIPv6(const char *interface, const int on) {
    135     if (!isIfaceName(interface)) {
    136         errno = ENOENT;
    137         return -1;
    138     }
    139     // When disable_ipv6 changes from 1 to 0, the kernel starts autoconf.
    140     // When disable_ipv6 changes from 0 to 1, the kernel clears all autoconf
    141     // addresses and routes and disables IPv6 on the interface.
    142     const char *disable_ipv6 = on ? "0" : "1";
    143     return writeValueToPath(ipv6_proc_path, interface, "disable_ipv6", disable_ipv6);
    144 }
    145 
    146 int InterfaceController::setAcceptIPv6Ra(const char *interface, const int on) {
    147     if (!isIfaceName(interface)) {
    148         errno = ENOENT;
    149         return -1;
    150     }
    151     // Because forwarding can be enabled even when tethering is off, we always
    152     // use mode "2" (accept RAs, even if forwarding is enabled).
    153     const char *accept_ra = on ? "2" : "0";
    154     return writeValueToPath(ipv6_proc_path, interface, "accept_ra", accept_ra);
    155 }
    156 
    157 int InterfaceController::setAcceptIPv6Dad(const char *interface, const int on) {
    158     if (!isIfaceName(interface)) {
    159         errno = ENOENT;
    160         return -1;
    161     }
    162     const char *accept_dad = on ? "1" : "0";
    163     return writeValueToPath(ipv6_proc_path, interface, "accept_dad", accept_dad);
    164 }
    165 
    166 int InterfaceController::setIPv6DadTransmits(const char *interface, const char *value) {
    167     if (!isIfaceName(interface)) {
    168         errno = ENOENT;
    169         return -1;
    170     }
    171     return writeValueToPath(ipv6_proc_path, interface, "dad_transmits", value);
    172 }
    173 
    174 int InterfaceController::setIPv6PrivacyExtensions(const char *interface, const int on) {
    175     if (!isIfaceName(interface)) {
    176         errno = ENOENT;
    177         return -1;
    178     }
    179     // 0: disable IPv6 privacy addresses
    180     // 0: enable IPv6 privacy addresses and prefer them over non-privacy ones.
    181     return writeValueToPath(ipv6_proc_path, interface, "use_tempaddr", on ? "2" : "0");
    182 }
    183 
    184 // Enables or disables IPv6 ND offload. This is useful for 464xlat on wifi, IPv6 tethering, and
    185 // generally implementing IPv6 neighbour discovery and duplicate address detection properly.
    186 // TODO: This should be implemented in wpa_supplicant via driver commands instead.
    187 int InterfaceController::setIPv6NdOffload(char* interface, const int on) {
    188     // Only supported on Broadcom chipsets via wlutil for now.
    189     if (access(wl_util_path, X_OK) == 0) {
    190         const char *argv[] = {
    191             wl_util_path,
    192             "-a",
    193             interface,
    194             "ndoe",
    195             on ? "1" : "0"
    196         };
    197         int ret = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL,
    198                                       false, false);
    199         ALOGD("%s ND offload on %s: %d (%s)",
    200               (on ? "enabling" : "disabling"), interface, ret, strerror(errno));
    201         return ret;
    202     } else {
    203         return 0;
    204     }
    205 }
    206 
    207 void InterfaceController::setAcceptRA(const char *value) {
    208     setOnAllInterfaces(ipv6_proc_path, "accept_ra", value);
    209 }
    210 
    211 // |tableOrOffset| is interpreted as:
    212 //     If == 0: default. Routes go into RT6_TABLE_MAIN.
    213 //     If > 0: user set. Routes go into the specified table.
    214 //     If < 0: automatic. The absolute value is intepreted as an offset and added to the interface
    215 //             ID to get the table. If it's set to -1000, routes from interface ID 5 will go into
    216 //             table 1005, etc.
    217 void InterfaceController::setAcceptRARouteTable(int tableOrOffset) {
    218     std::string value(StringPrintf("%d", tableOrOffset));
    219     setOnAllInterfaces(ipv6_proc_path, "accept_ra_rt_table", value.c_str());
    220 }
    221 
    222 int InterfaceController::setMtu(const char *interface, const char *mtu)
    223 {
    224     if (!isIfaceName(interface)) {
    225         errno = ENOENT;
    226         return -1;
    227     }
    228     return writeValueToPath(sys_net_path, interface, "mtu", mtu);
    229 }
    230 
    231 int InterfaceController::addAddress(const char *interface,
    232         const char *addrString, int prefixLength) {
    233     return ifc_add_address(interface, addrString, prefixLength);
    234 }
    235 
    236 int InterfaceController::delAddress(const char *interface,
    237         const char *addrString, int prefixLength) {
    238     return ifc_del_address(interface, addrString, prefixLength);
    239 }
    240 
    241 int InterfaceController::getParameter(
    242         const char *family, const char *which, const char *interface, const char *parameter,
    243         std::string *value) {
    244     const std::string path(getParameterPathname(family, which, interface, parameter));
    245     if (path.empty()) {
    246         return -errno;
    247     }
    248     return ReadFileToString(path, value) ? 0 : -errno;
    249 }
    250 
    251 int InterfaceController::setParameter(
    252         const char *family, const char *which, const char *interface, const char *parameter,
    253         const char *value) {
    254     const std::string path(getParameterPathname(family, which, interface, parameter));
    255     if (path.empty()) {
    256         return -errno;
    257     }
    258     return WriteStringToFile(value, path) ? 0 : -errno;
    259 }
    260 
    261 void InterfaceController::setBaseReachableTimeMs(unsigned int millis) {
    262     std::string value(StringPrintf("%u", millis));
    263     setOnAllInterfaces(ipv4_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
    264     setOnAllInterfaces(ipv6_neigh_conf_dir, "base_reachable_time_ms", value.c_str());
    265 }
    266 
    267 void InterfaceController::setIPv6OptimisticMode(const char *value) {
    268     setOnAllInterfaces(ipv6_proc_path, "optimistic_dad", value);
    269     setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value);
    270 }
    271