Home | History | Annotate | Download | only in server
      1 /*
      2  * Copyright (C) 2014 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 #ifndef NETD_SERVER_ROUTE_CONTROLLER_H
     18 #define NETD_SERVER_ROUTE_CONTROLLER_H
     19 
     20 #include "NetdConstants.h"
     21 #include "Permission.h"
     22 
     23 #include <sys/types.h>
     24 #include <linux/netlink.h>
     25 
     26 namespace android {
     27 namespace net {
     28 
     29 class UidRanges;
     30 
     31 class RouteController {
     32 public:
     33     // How the routing table number is determined for route modification requests.
     34     enum TableType {
     35         INTERFACE,       // Compute the table number based on the interface index.
     36         LOCAL_NETWORK,   // A fixed table used for routes to directly-connected clients/peers.
     37         LEGACY_NETWORK,  // Use a fixed table that's used to override the default network.
     38         LEGACY_SYSTEM,   // A fixed table, only modifiable by system apps; overrides VPNs too.
     39     };
     40 
     41     static const int ROUTE_TABLE_OFFSET_FROM_INDEX = 1000;
     42 
     43     static int Init(unsigned localNetId) WARN_UNUSED_RESULT;
     44 
     45     static int addInterfaceToLocalNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
     46     static int removeInterfaceFromLocalNetwork(unsigned netId,
     47                                                const char* interface) WARN_UNUSED_RESULT;
     48 
     49     static int addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
     50                                              Permission permission) WARN_UNUSED_RESULT;
     51     static int removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
     52                                                   Permission permission) WARN_UNUSED_RESULT;
     53 
     54     static int addInterfaceToVirtualNetwork(unsigned netId, const char* interface, bool secure,
     55                                             const UidRanges& uidRanges) WARN_UNUSED_RESULT;
     56     static int removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
     57                                                  const UidRanges& uidRanges) WARN_UNUSED_RESULT;
     58 
     59     static int modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
     60                                                Permission oldPermission,
     61                                                Permission newPermission) WARN_UNUSED_RESULT;
     62 
     63     static int addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
     64                                         const UidRanges& uidRanges) WARN_UNUSED_RESULT;
     65     static int removeUsersFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
     66                                              const UidRanges& uidRanges) WARN_UNUSED_RESULT;
     67 
     68     static int addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges)
     69                                                     WARN_UNUSED_RESULT;
     70     static int removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges)
     71                                                          WARN_UNUSED_RESULT;
     72 
     73     static int addInterfaceToDefaultNetwork(const char* interface,
     74                                             Permission permission) WARN_UNUSED_RESULT;
     75     static int removeInterfaceFromDefaultNetwork(const char* interface,
     76                                                  Permission permission) WARN_UNUSED_RESULT;
     77 
     78     // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
     79     // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
     80     static int addRoute(const char* interface, const char* destination, const char* nexthop,
     81                         TableType tableType) WARN_UNUSED_RESULT;
     82     static int removeRoute(const char* interface, const char* destination, const char* nexthop,
     83                            TableType tableType) WARN_UNUSED_RESULT;
     84 
     85     static int enableTethering(const char* inputInterface,
     86                                const char* outputInterface) WARN_UNUSED_RESULT;
     87     static int disableTethering(const char* inputInterface,
     88                                 const char* outputInterface) WARN_UNUSED_RESULT;
     89 
     90     static int addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
     91                                             Permission permission) WARN_UNUSED_RESULT;
     92     static int removeVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
     93                                                Permission permission) WARN_UNUSED_RESULT;
     94 
     95     // For testing.
     96     static int (*iptablesRestoreCommandFunction)(IptablesTarget, const std::string&,
     97                                                  const std::string&, std::string *);
     98 };
     99 
    100 // Public because they are called by by RouteControllerTest.cpp.
    101 // TODO: come up with a scheme of unit testing this code that does not rely on making all its
    102 // functions public.
    103 int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination,
    104                   const char* nexthop) WARN_UNUSED_RESULT;
    105 int flushRoutes(uint32_t table) WARN_UNUSED_RESULT;
    106 uint32_t getRulePriority(const nlmsghdr *nlh);
    107 WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
    108                                                 Permission permission, bool add);
    109 
    110 }  // namespace net
    111 }  // namespace android
    112 
    113 #endif  // NETD_SERVER_ROUTE_CONTROLLER_H
    114