blob: de79b61795927bb8cbbb3089c6c2d79ba86d02b3 [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
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +090023#include <map>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070024#include <sys/types.h>
Lorenzo Colitti60367db2017-02-13 16:31:45 +090025#include <linux/netlink.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070026
Lorenzo Colitti7035f222017-02-13 18:29:00 +090027namespace android {
28namespace net {
29
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070030class UidRanges;
31
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070032class RouteController {
33public:
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070034 // How the routing table number is determined for route modification requests.
35 enum TableType {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070036 INTERFACE, // Compute the table number based on the interface index.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070037 LOCAL_NETWORK, // A fixed table used for routes to directly-connected clients/peers.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070038 LEGACY_NETWORK, // Use a fixed table that's used to override the default network.
39 LEGACY_SYSTEM, // A fixed table, only modifiable by system apps; overrides VPNs too.
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070040 };
41
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070042 static const int ROUTE_TABLE_OFFSET_FROM_INDEX = 1000;
43
Lorenzo Colittid78843e2017-03-27 05:52:31 +090044 static const char* const LOCAL_MANGLE_INPUT;
45
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070046 static int Init(unsigned localNetId) WARN_UNUSED_RESULT;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070047
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070048 static int addInterfaceToLocalNetwork(unsigned netId, const char* interface) WARN_UNUSED_RESULT;
49 static int removeInterfaceFromLocalNetwork(unsigned netId,
50 const char* interface) WARN_UNUSED_RESULT;
51
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070052 static int addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
53 Permission permission) WARN_UNUSED_RESULT;
54 static int removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
55 Permission permission) WARN_UNUSED_RESULT;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070056
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070057 static int addInterfaceToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070058 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070059 static int removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070060 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070061
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070062 static int modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
63 Permission oldPermission,
64 Permission newPermission) WARN_UNUSED_RESULT;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070065
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070066 static int addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070067 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070068 static int removeUsersFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070069 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070070
Robin Leeb8087362016-03-30 18:43:08 +010071 static int addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges)
72 WARN_UNUSED_RESULT;
73 static int removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges)
74 WARN_UNUSED_RESULT;
75
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070076 static int addInterfaceToDefaultNetwork(const char* interface,
77 Permission permission) WARN_UNUSED_RESULT;
78 static int removeInterfaceFromDefaultNetwork(const char* interface,
79 Permission permission) WARN_UNUSED_RESULT;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070080
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -070081 // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
Lorenzo Colitti4c95a122014-09-18 16:01:50 +090082 // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090083 static int addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -070084 TableType tableType) WARN_UNUSED_RESULT;
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090085 static int removeRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -070086 TableType tableType) WARN_UNUSED_RESULT;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070087
88 static int enableTethering(const char* inputInterface,
89 const char* outputInterface) WARN_UNUSED_RESULT;
90 static int disableTethering(const char* inputInterface,
91 const char* outputInterface) WARN_UNUSED_RESULT;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070092
93 static int addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
94 Permission permission) WARN_UNUSED_RESULT;
95 static int removeVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
96 Permission permission) WARN_UNUSED_RESULT;
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090097
98 // For testing.
99 static int (*iptablesRestoreCommandFunction)(IptablesTarget, const std::string&,
100 const std::string&, std::string *);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900101
102private:
103 friend class RouteControllerTest;
104
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900105 // Protects access to interfaceToTable.
106 static android::RWLock sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900107 static std::map<std::string, uint32_t> sInterfaceToTable;
108
109 static int configureDummyNetwork();
110 static int flushRoutes(const char* interface);
111 static int flushRoutes(uint32_t table);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900112 static uint32_t getRouteTableForInterfaceLocked(const char *interface);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900113 static uint32_t getRouteTableForInterface(const char *interface);
114 static int modifyDefaultNetwork(uint16_t action, const char* interface, Permission permission);
115 static int modifyPhysicalNetwork(unsigned netId, const char* interface, Permission permission,
116 bool add);
117 static int modifyRoute(uint16_t action, const char* interface, const char* destination,
118 const char* nexthop, TableType tableType);
119 static int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
120 const char* outputInterface);
121 static int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
122 const char* physicalInterface, Permission permission);
123 static int modifyVirtualNetwork(unsigned netId, const char* interface,
124 const UidRanges& uidRanges, bool secure, bool add,
125 bool modifyNonUidBasedRules);
126 static void updateTableNamesFile();
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700127};
128
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900129// Public because they are called by by RouteControllerTest.cpp.
130// TODO: come up with a scheme of unit testing this code that does not rely on making all its
131// functions public.
132int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination,
133 const char* nexthop) WARN_UNUSED_RESULT;
134int flushRoutes(uint32_t table) WARN_UNUSED_RESULT;
135uint32_t getRulePriority(const nlmsghdr *nlh);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900136WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
137 Permission permission, bool add);
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900138
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900139} // namespace net
140} // namespace android
141
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700142#endif // NETD_SERVER_ROUTE_CONTROLLER_H