blob: d40288b685089f7055df6cc4d4742f6d588fdf59 [file] [log] [blame]
Sreeram Ramachandrand736d4b2014-03-26 18:33:47 -07001/*
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 Ramachandranf4cfad32014-05-21 08:54:07 -070017#ifndef NETD_SERVER_ROUTE_CONTROLLER_H
18#define NETD_SERVER_ROUTE_CONTROLLER_H
Sreeram Ramachandrand736d4b2014-03-26 18:33:47 -070019
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070020#include "NetdConstants.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070021#include "Permission.h"
22
Luke Huang534980c2018-07-06 17:50:11 +080023#include <android-base/thread_annotations.h>
24
Lorenzo Colitti60367db2017-02-13 16:31:45 +090025#include <linux/netlink.h>
Luke Huangd1ee4622018-06-29 13:49:58 +080026#include <sys/types.h>
27#include <map>
Luke Huang534980c2018-07-06 17:50:11 +080028#include <mutex>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070029
Lorenzo Colitti7035f222017-02-13 18:29:00 +090030namespace android {
31namespace net {
32
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070033class UidRanges;
34
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070035class RouteController {
36public:
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070037 // How the routing table number is determined for route modification requests.
38 enum TableType {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070039 INTERFACE, // Compute the table number based on the interface index.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070040 LOCAL_NETWORK, // A fixed table used for routes to directly-connected clients/peers.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070041 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 Ramachandran38b7af12014-05-22 14:21:49 -070043 };
44
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070045 static const int ROUTE_TABLE_OFFSET_FROM_INDEX = 1000;
46
Lorenzo Colittid78843e2017-03-27 05:52:31 +090047 static const char* const LOCAL_MANGLE_INPUT;
48
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070049 static int Init(unsigned localNetId) WARN_UNUSED_RESULT;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070050
Rubin Xu6c00b612018-04-27 14:27:59 +010051 // 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 Huang534980c2018-07-06 17:50:11 +080058 static uint32_t getIfIndex(const char* interface) EXCLUDES(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +010059
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070060 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 Ramachandran5009d5e2014-07-03 12:20:48 -070064 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 Ramachandran9c0d3132014-04-10 20:35:04 -070068
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070069 static int addInterfaceToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070070 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070071 static int removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070072 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070073
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070074 static int modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
75 Permission oldPermission,
76 Permission newPermission) WARN_UNUSED_RESULT;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070077
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070078 static int addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070079 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070080 static int removeUsersFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070081 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070082
Robin Leeb8087362016-03-30 18:43:08 +010083 static int addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges)
84 WARN_UNUSED_RESULT;
85 static int removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges)
86 WARN_UNUSED_RESULT;
87
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070088 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 Ramachandranb1425cc2014-06-23 18:54:27 -070092
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -070093 // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
Lorenzo Colitti4c95a122014-09-18 16:01:50 +090094 // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090095 static int addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -070096 TableType tableType) WARN_UNUSED_RESULT;
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090097 static int removeRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -070098 TableType tableType) WARN_UNUSED_RESULT;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070099
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 Ramachandran48e19b02014-07-22 22:23:20 -0700104
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 Colittic1306ea2017-03-27 05:52:31 +0900109
110 // For testing.
111 static int (*iptablesRestoreCommandFunction)(IptablesTarget, const std::string&,
112 const std::string&, std::string *);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900113
114private:
115 friend class RouteControllerTest;
Luke Huang534980c2018-07-06 17:50:11 +0800116
117 static std::mutex sInterfaceToTableLock;
118 static std::map<std::string, uint32_t> sInterfaceToTable GUARDED_BY(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900119
120 static int configureDummyNetwork();
Luke Huang534980c2018-07-06 17:50:11 +0800121 static int flushRoutes(const char* interface) EXCLUDES(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900122 static int flushRoutes(uint32_t table);
Luke Huang534980c2018-07-06 17:50:11 +0800123 static uint32_t getRouteTableForInterfaceLocked(const char *interface)
124 REQUIRES(sInterfaceToTableLock);
125 static uint32_t getRouteTableForInterface(const char *interface) EXCLUDES(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900126 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 Huang534980c2018-07-06 17:50:11 +0800138 static void updateTableNamesFile() EXCLUDES(sInterfaceToTableLock);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700139};
140
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900141// 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.
144int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination,
145 const char* nexthop) WARN_UNUSED_RESULT;
146int flushRoutes(uint32_t table) WARN_UNUSED_RESULT;
147uint32_t getRulePriority(const nlmsghdr *nlh);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900148WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
149 Permission permission, bool add);
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900150
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900151} // namespace net
152} // namespace android
153
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700154#endif // NETD_SERVER_ROUTE_CONTROLLER_H