blob: 579cfe225f68c6f5329a8d58b07eba39460a4ef0 [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
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070023#include <sys/types.h>
Lorenzo Colitti60367db2017-02-13 16:31:45 +090024#include <linux/netlink.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070025
Lorenzo Colitti7035f222017-02-13 18:29:00 +090026namespace android {
27namespace net {
28
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070029class UidRanges;
30
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031class RouteController {
32public:
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070033 // How the routing table number is determined for route modification requests.
34 enum TableType {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070035 INTERFACE, // Compute the table number based on the interface index.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070036 LOCAL_NETWORK, // A fixed table used for routes to directly-connected clients/peers.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070037 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.
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070039 };
40
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070041 static const int ROUTE_TABLE_OFFSET_FROM_INDEX = 1000;
42
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070043 static int Init(unsigned localNetId) WARN_UNUSED_RESULT;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070044
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070045 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
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070049 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;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -070053
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070054 static int addInterfaceToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070055 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070056 static int removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070057 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -070058
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070059 static int modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
60 Permission oldPermission,
61 Permission newPermission) WARN_UNUSED_RESULT;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070062
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070063 static int addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070064 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070065 static int removeUsersFromVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070066 const UidRanges& uidRanges) WARN_UNUSED_RESULT;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -070067
Robin Leeb8087362016-03-30 18:43:08 +010068 static int addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges)
69 WARN_UNUSED_RESULT;
70 static int removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges)
71 WARN_UNUSED_RESULT;
72
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070073 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;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070077
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -070078 // |nexthop| can be NULL (to indicate a directly-connected route), "unreachable" (to indicate a
Lorenzo Colitti4c95a122014-09-18 16:01:50 +090079 // route that's blocked), "throw" (to indicate the lack of a match), or a regular IP address.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090080 static int addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -070081 TableType tableType) WARN_UNUSED_RESULT;
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +090082 static int removeRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -070083 TableType tableType) WARN_UNUSED_RESULT;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070084
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;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070089
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;
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090094
95 // For testing.
96 static int (*iptablesRestoreCommandFunction)(IptablesTarget, const std::string&,
97 const std::string&, std::string *);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070098};
99
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900100// 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.
103int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination,
104 const char* nexthop) WARN_UNUSED_RESULT;
105int flushRoutes(uint32_t table) WARN_UNUSED_RESULT;
106uint32_t getRulePriority(const nlmsghdr *nlh);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900107WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
108 Permission permission, bool add);
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900109
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900110} // namespace net
111} // namespace android
112
Sreeram Ramachandranf4cfad32014-05-21 08:54:07 -0700113#endif // NETD_SERVER_ROUTE_CONTROLLER_H