Sreeram Ramachandran | d736d4b | 2014-03-26 18:33:47 -0700 | [diff] [blame] | 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 | |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 17 | #ifndef NETD_SERVER_ROUTE_CONTROLLER_H |
| 18 | #define NETD_SERVER_ROUTE_CONTROLLER_H |
Sreeram Ramachandran | d736d4b | 2014-03-26 18:33:47 -0700 | [diff] [blame] | 19 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 20 | #include "NetdConstants.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 21 | #include "Permission.h" |
| 22 | |
Luke Huang | 534980c | 2018-07-06 17:50:11 +0800 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
| 24 | |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 25 | #include <linux/netlink.h> |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <map> |
Luke Huang | 534980c | 2018-07-06 17:50:11 +0800 | [diff] [blame] | 28 | #include <mutex> |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 29 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace net { |
| 32 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 33 | class UidRanges; |
| 34 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 35 | class RouteController { |
| 36 | public: |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 37 | // How the routing table number is determined for route modification requests. |
| 38 | enum TableType { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 39 | INTERFACE, // Compute the table number based on the interface index. |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 40 | LOCAL_NETWORK, // A fixed table used for routes to directly-connected clients/peers. |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 41 | LEGACY_NETWORK, // Use a fixed table that's used to override the default network. |
| 42 | LEGACY_SYSTEM, // A fixed table, only modifiable by system apps; overrides VPNs too. |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 45 | static const int ROUTE_TABLE_OFFSET_FROM_INDEX = 1000; |
| 46 | |
Lorenzo Colitti | d78843e | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 47 | static const char* const LOCAL_MANGLE_INPUT; |
| 48 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 49 | static int Init(unsigned localNetId) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 50 | |
Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 51 | // Returns an ifindex given the interface name, by looking up in sInterfaceToTable. |
| 52 | // This is currently only used by NetworkController::addInterfaceToNetwork |
| 53 | // and should probabaly be changed to passing the ifindex into RouteController instead. |
| 54 | // We do this instead of calling if_nametoindex because the same interface name can |
| 55 | // correspond to different interface indices over time. This way, even if the interface |
| 56 | // index has changed, we can still free any map entries indexed by the ifindex that was |
| 57 | // used to add them. |
Luke Huang | 534980c | 2018-07-06 17:50:11 +0800 | [diff] [blame] | 58 | static uint32_t getIfIndex(const char* interface) EXCLUDES(sInterfaceToTableLock); |
Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 59 | |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 60 | static int addInterfaceToLocalNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT; |
| 61 | static int removeInterfaceFromLocalNetwork(unsigned netId, |
| 62 | const char* interface) WARN_UNUSED_RESULT; |
| 63 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 64 | static int addInterfaceToPhysicalNetwork(unsigned netId, const char* interface, |
| 65 | Permission permission) WARN_UNUSED_RESULT; |
| 66 | static int removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface, |
| 67 | Permission permission) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 68 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 69 | static int addInterfaceToVirtualNetwork(unsigned netId, const char* interface, bool secure, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 70 | const UidRanges& uidRanges) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 71 | static int removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface, bool secure, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 72 | const UidRanges& uidRanges) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 73 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 74 | static int modifyPhysicalNetworkPermission(unsigned netId, const char* interface, |
| 75 | Permission oldPermission, |
| 76 | Permission newPermission) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 77 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 78 | static int addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 79 | const UidRanges& uidRanges) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 80 | static int removeUsersFromVirtualNetwork(unsigned netId, const char* interface, bool secure, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 81 | const UidRanges& uidRanges) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 82 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 83 | static int addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) |
| 84 | WARN_UNUSED_RESULT; |
| 85 | static int removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) |
| 86 | WARN_UNUSED_RESULT; |
| 87 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 88 | static int addInterfaceToDefaultNetwork(const char* interface, |
| 89 | Permission permission) WARN_UNUSED_RESULT; |
| 90 | static int removeInterfaceFromDefaultNetwork(const char* interface, |
| 91 | Permission permission) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 92 | |
Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 93 | // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a |
Lorenzo Colitti | 4c95a12 | 2014-09-18 16:01:50 +0900 | [diff] [blame] | 94 | // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address. |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 95 | static int addRoute(const char* interface, const char* destination, const char* nexthop, |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 96 | TableType tableType) WARN_UNUSED_RESULT; |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 97 | static int removeRoute(const char* interface, const char* destination, const char* nexthop, |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 98 | TableType tableType) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 99 | |
| 100 | static int enableTethering(const char* inputInterface, |
| 101 | const char* outputInterface) WARN_UNUSED_RESULT; |
| 102 | static int disableTethering(const char* inputInterface, |
| 103 | const char* outputInterface) WARN_UNUSED_RESULT; |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 104 | |
| 105 | static int addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface, |
| 106 | Permission permission) WARN_UNUSED_RESULT; |
| 107 | static int removeVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface, |
| 108 | Permission permission) WARN_UNUSED_RESULT; |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 109 | |
| 110 | // For testing. |
| 111 | static int (*iptablesRestoreCommandFunction)(IptablesTarget, const std::string&, |
| 112 | const std::string&, std::string *); |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | friend class RouteControllerTest; |
Luke Huang | 534980c | 2018-07-06 17:50:11 +0800 | [diff] [blame] | 116 | |
| 117 | static std::mutex sInterfaceToTableLock; |
| 118 | static std::map<std::string, uint32_t> sInterfaceToTable GUARDED_BY(sInterfaceToTableLock); |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 119 | |
| 120 | static int configureDummyNetwork(); |
Luke Huang | 534980c | 2018-07-06 17:50:11 +0800 | [diff] [blame] | 121 | static int flushRoutes(const char* interface) EXCLUDES(sInterfaceToTableLock); |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 122 | static int flushRoutes(uint32_t table); |
Luke Huang | 534980c | 2018-07-06 17:50:11 +0800 | [diff] [blame] | 123 | static uint32_t getRouteTableForInterfaceLocked(const char *interface) |
| 124 | REQUIRES(sInterfaceToTableLock); |
| 125 | static uint32_t getRouteTableForInterface(const char *interface) EXCLUDES(sInterfaceToTableLock); |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 126 | static int modifyDefaultNetwork(uint16_t action, const char* interface, Permission permission); |
| 127 | static int modifyPhysicalNetwork(unsigned netId, const char* interface, Permission permission, |
| 128 | bool add); |
| 129 | static int modifyRoute(uint16_t action, const char* interface, const char* destination, |
| 130 | const char* nexthop, TableType tableType); |
| 131 | static int modifyTetheredNetwork(uint16_t action, const char* inputInterface, |
| 132 | const char* outputInterface); |
| 133 | static int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId, |
| 134 | const char* physicalInterface, Permission permission); |
| 135 | static int modifyVirtualNetwork(unsigned netId, const char* interface, |
| 136 | const UidRanges& uidRanges, bool secure, bool add, |
| 137 | bool modifyNonUidBasedRules); |
Luke Huang | 534980c | 2018-07-06 17:50:11 +0800 | [diff] [blame] | 138 | static void updateTableNamesFile() EXCLUDES(sInterfaceToTableLock); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 141 | // Public because they are called by by RouteControllerTest.cpp. |
| 142 | // TODO: come up with a scheme of unit testing this code that does not rely on making all its |
| 143 | // functions public. |
| 144 | int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination, |
| 145 | const char* nexthop) WARN_UNUSED_RESULT; |
| 146 | int flushRoutes(uint32_t table) WARN_UNUSED_RESULT; |
| 147 | uint32_t getRulePriority(const nlmsghdr *nlh); |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 148 | WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface, |
| 149 | Permission permission, bool add); |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 150 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 151 | } // namespace net |
| 152 | } // namespace android |
| 153 | |
Sreeram Ramachandran | f4cfad3 | 2014-05-21 08:54:07 -0700 | [diff] [blame] | 154 | #endif // NETD_SERVER_ROUTE_CONTROLLER_H |