xref: /aosp_15_r20/system/netd/server/RouteController.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  */
16*8542734aSAndroid Build Coastguard Worker 
17*8542734aSAndroid Build Coastguard Worker #include "RouteController.h"
18*8542734aSAndroid Build Coastguard Worker 
19*8542734aSAndroid Build Coastguard Worker #include <arpa/inet.h>
20*8542734aSAndroid Build Coastguard Worker #include <errno.h>
21*8542734aSAndroid Build Coastguard Worker #include <fcntl.h>
22*8542734aSAndroid Build Coastguard Worker #include <linux/fib_rules.h>
23*8542734aSAndroid Build Coastguard Worker #include <net/if.h>
24*8542734aSAndroid Build Coastguard Worker #include <netdutils/InternetAddresses.h>
25*8542734aSAndroid Build Coastguard Worker #include <private/android_filesystem_config.h>
26*8542734aSAndroid Build Coastguard Worker #include <sys/stat.h>
27*8542734aSAndroid Build Coastguard Worker 
28*8542734aSAndroid Build Coastguard Worker #include <map>
29*8542734aSAndroid Build Coastguard Worker 
30*8542734aSAndroid Build Coastguard Worker #include "DummyNetwork.h"
31*8542734aSAndroid Build Coastguard Worker #include "Fwmark.h"
32*8542734aSAndroid Build Coastguard Worker #include "NetdConstants.h"
33*8542734aSAndroid Build Coastguard Worker #include "NetlinkCommands.h"
34*8542734aSAndroid Build Coastguard Worker #include "TcUtils.h"
35*8542734aSAndroid Build Coastguard Worker 
36*8542734aSAndroid Build Coastguard Worker #include <android-base/file.h>
37*8542734aSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
38*8542734aSAndroid Build Coastguard Worker #include <android-base/strings.h>
39*8542734aSAndroid Build Coastguard Worker #include "log/log.h"
40*8542734aSAndroid Build Coastguard Worker #include "netid_client.h"
41*8542734aSAndroid Build Coastguard Worker #include "netutils/ifc.h"
42*8542734aSAndroid Build Coastguard Worker 
43*8542734aSAndroid Build Coastguard Worker using android::base::StartsWith;
44*8542734aSAndroid Build Coastguard Worker using android::base::StringPrintf;
45*8542734aSAndroid Build Coastguard Worker using android::base::WriteStringToFile;
46*8542734aSAndroid Build Coastguard Worker using android::netdutils::IPPrefix;
47*8542734aSAndroid Build Coastguard Worker 
48*8542734aSAndroid Build Coastguard Worker namespace android::net {
49*8542734aSAndroid Build Coastguard Worker 
50*8542734aSAndroid Build Coastguard Worker auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
51*8542734aSAndroid Build Coastguard Worker auto RouteController::ifNameToIndexFunction = if_nametoindex;
52*8542734aSAndroid Build Coastguard Worker // BEGIN CONSTANTS --------------------------------------------------------------------------------
53*8542734aSAndroid Build Coastguard Worker 
54*8542734aSAndroid Build Coastguard Worker const uint32_t ROUTE_TABLE_LOCAL_NETWORK  = 97;
55*8542734aSAndroid Build Coastguard Worker const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
56*8542734aSAndroid Build Coastguard Worker const uint32_t ROUTE_TABLE_LEGACY_SYSTEM  = 99;
57*8542734aSAndroid Build Coastguard Worker 
58*8542734aSAndroid Build Coastguard Worker const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK  = "local_network";
59*8542734aSAndroid Build Coastguard Worker const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
60*8542734aSAndroid Build Coastguard Worker const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM  = "legacy_system";
61*8542734aSAndroid Build Coastguard Worker 
62*8542734aSAndroid Build Coastguard Worker const char* const ROUTE_TABLE_NAME_LOCAL = "local";
63*8542734aSAndroid Build Coastguard Worker const char* const ROUTE_TABLE_NAME_MAIN  = "main";
64*8542734aSAndroid Build Coastguard Worker 
65*8542734aSAndroid Build Coastguard Worker const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
66*8542734aSAndroid Build Coastguard Worker 
67*8542734aSAndroid Build Coastguard Worker const IPPrefix V4_LOCAL_PREFIXES[] = {
68*8542734aSAndroid Build Coastguard Worker         IPPrefix::forString("169.254.0.0/16"),  // Link Local
69*8542734aSAndroid Build Coastguard Worker         IPPrefix::forString("100.64.0.0/10"),   // CGNAT
70*8542734aSAndroid Build Coastguard Worker         IPPrefix::forString("10.0.0.0/8"),      // RFC1918
71*8542734aSAndroid Build Coastguard Worker         IPPrefix::forString("172.16.0.0/12"),   // RFC1918
72*8542734aSAndroid Build Coastguard Worker         IPPrefix::forString("192.168.0.0/16")   // RFC1918
73*8542734aSAndroid Build Coastguard Worker };
74*8542734aSAndroid Build Coastguard Worker 
75*8542734aSAndroid Build Coastguard Worker const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
76*8542734aSAndroid Build Coastguard Worker 
77*8542734aSAndroid Build Coastguard Worker const uid_t UID_ROOT = 0;
78*8542734aSAndroid Build Coastguard Worker const uint32_t FWMARK_NONE = 0;
79*8542734aSAndroid Build Coastguard Worker const uint32_t MASK_NONE = 0;
80*8542734aSAndroid Build Coastguard Worker const char* const IIF_LOOPBACK = "lo";
81*8542734aSAndroid Build Coastguard Worker const char* const IIF_NONE = nullptr;
82*8542734aSAndroid Build Coastguard Worker const char* const OIF_NONE = nullptr;
83*8542734aSAndroid Build Coastguard Worker const bool ACTION_ADD = true;
84*8542734aSAndroid Build Coastguard Worker const bool ACTION_DEL = false;
85*8542734aSAndroid Build Coastguard Worker const bool MODIFY_NON_UID_BASED_RULES = true;
86*8542734aSAndroid Build Coastguard Worker 
87*8542734aSAndroid Build Coastguard Worker const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;  // mode 0644, rw-r--r--
88*8542734aSAndroid Build Coastguard Worker 
89*8542734aSAndroid Build Coastguard Worker // Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
90*8542734aSAndroid Build Coastguard Worker // warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
U16_RTA_LENGTH(uint16_t x)91*8542734aSAndroid Build Coastguard Worker static constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
92*8542734aSAndroid Build Coastguard Worker     return RTA_LENGTH(x);
93*8542734aSAndroid Build Coastguard Worker }
94*8542734aSAndroid Build Coastguard Worker 
95*8542734aSAndroid Build Coastguard Worker // These are practically const, but can't be declared so, because they are used to initialize
96*8542734aSAndroid Build Coastguard Worker // non-const pointers ("void* iov_base") in iovec arrays.
97*8542734aSAndroid Build Coastguard Worker rtattr FRATTR_PRIORITY  = { U16_RTA_LENGTH(sizeof(uint32_t)),           FRA_PRIORITY };
98*8542734aSAndroid Build Coastguard Worker rtattr FRATTR_TABLE     = { U16_RTA_LENGTH(sizeof(uint32_t)),           FRA_TABLE };
99*8542734aSAndroid Build Coastguard Worker rtattr FRATTR_FWMARK    = { U16_RTA_LENGTH(sizeof(uint32_t)),           FRA_FWMARK };
100*8542734aSAndroid Build Coastguard Worker rtattr FRATTR_FWMASK    = { U16_RTA_LENGTH(sizeof(uint32_t)),           FRA_FWMASK };
101*8542734aSAndroid Build Coastguard Worker rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
102*8542734aSAndroid Build Coastguard Worker 
103*8542734aSAndroid Build Coastguard Worker rtattr RTATTR_TABLE     = { U16_RTA_LENGTH(sizeof(uint32_t)),           RTA_TABLE };
104*8542734aSAndroid Build Coastguard Worker rtattr RTATTR_OIF       = { U16_RTA_LENGTH(sizeof(uint32_t)),           RTA_OIF };
105*8542734aSAndroid Build Coastguard Worker rtattr RTATTR_PRIO      = { U16_RTA_LENGTH(sizeof(uint32_t)),           RTA_PRIORITY };
106*8542734aSAndroid Build Coastguard Worker 
107*8542734aSAndroid Build Coastguard Worker // One or more nested attributes in the RTA_METRICS attribute.
108*8542734aSAndroid Build Coastguard Worker rtattr RTATTRX_MTU      = { U16_RTA_LENGTH(sizeof(uint32_t)),           RTAX_MTU};
109*8542734aSAndroid Build Coastguard Worker constexpr size_t RTATTRX_MTU_SIZE = RTA_SPACE(sizeof(uint32_t));
110*8542734aSAndroid Build Coastguard Worker 
111*8542734aSAndroid Build Coastguard Worker // The RTA_METRICS attribute itself.
112*8542734aSAndroid Build Coastguard Worker constexpr size_t RTATTR_METRICS_SIZE = RTATTRX_MTU_SIZE;
113*8542734aSAndroid Build Coastguard Worker rtattr RTATTR_METRICS   = { U16_RTA_LENGTH(RTATTR_METRICS_SIZE),         RTA_METRICS };
114*8542734aSAndroid Build Coastguard Worker 
115*8542734aSAndroid Build Coastguard Worker uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
116*8542734aSAndroid Build Coastguard Worker 
117*8542734aSAndroid Build Coastguard Worker constexpr bool EXPLICIT = true;
118*8542734aSAndroid Build Coastguard Worker constexpr bool IMPLICIT = false;
119*8542734aSAndroid Build Coastguard Worker 
120*8542734aSAndroid Build Coastguard Worker // END CONSTANTS ----------------------------------------------------------------------------------
121*8542734aSAndroid Build Coastguard Worker 
actionName(uint16_t action)122*8542734aSAndroid Build Coastguard Worker static const char* actionName(uint16_t action) {
123*8542734aSAndroid Build Coastguard Worker     static const char *ops[4] = {"adding", "deleting", "getting", "???"};
124*8542734aSAndroid Build Coastguard Worker     return ops[action % 4];
125*8542734aSAndroid Build Coastguard Worker }
126*8542734aSAndroid Build Coastguard Worker 
familyName(uint8_t family)127*8542734aSAndroid Build Coastguard Worker static const char* familyName(uint8_t family) {
128*8542734aSAndroid Build Coastguard Worker     switch (family) {
129*8542734aSAndroid Build Coastguard Worker         case AF_INET: return "IPv4";
130*8542734aSAndroid Build Coastguard Worker         case AF_INET6: return "IPv6";
131*8542734aSAndroid Build Coastguard Worker         default: return "???";
132*8542734aSAndroid Build Coastguard Worker     }
133*8542734aSAndroid Build Coastguard Worker }
134*8542734aSAndroid Build Coastguard Worker 
135*8542734aSAndroid Build Coastguard Worker static void maybeModifyQdiscClsact(const char* interface, bool add);
136*8542734aSAndroid Build Coastguard Worker 
getRouteTableIndexFromGlobalRouteTableIndex(uint32_t index,bool local)137*8542734aSAndroid Build Coastguard Worker static uint32_t getRouteTableIndexFromGlobalRouteTableIndex(uint32_t index, bool local) {
138*8542734aSAndroid Build Coastguard Worker     // The local table is
139*8542734aSAndroid Build Coastguard Worker     // "global table - ROUTE_TABLE_OFFSET_FROM_INDEX + ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL"
140*8542734aSAndroid Build Coastguard Worker     const uint32_t localTableOffset = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL -
141*8542734aSAndroid Build Coastguard Worker                                       RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
142*8542734aSAndroid Build Coastguard Worker     return local ? index + localTableOffset : index;
143*8542734aSAndroid Build Coastguard Worker }
144*8542734aSAndroid Build Coastguard Worker 
145*8542734aSAndroid Build Coastguard Worker // Caller must hold sInterfaceToTableLock.
getRouteTableForInterfaceLocked(const char * interface,bool local)146*8542734aSAndroid Build Coastguard Worker uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface, bool local) {
147*8542734aSAndroid Build Coastguard Worker     // If we already know the routing table for this interface name, use it.
148*8542734aSAndroid Build Coastguard Worker     // This ensures we can remove rules and routes for an interface that has been removed,
149*8542734aSAndroid Build Coastguard Worker     // or has been removed and re-added with a different interface index.
150*8542734aSAndroid Build Coastguard Worker     //
151*8542734aSAndroid Build Coastguard Worker     // The caller is responsible for ensuring that an interface is never added to a network
152*8542734aSAndroid Build Coastguard Worker     // until it has been removed from any network it was previously in. This ensures that
153*8542734aSAndroid Build Coastguard Worker     // if the same interface disconnects and then reconnects with a different interface ID
154*8542734aSAndroid Build Coastguard Worker     // when the reconnect happens the interface will not be in the map, and the code will
155*8542734aSAndroid Build Coastguard Worker     // determine the new routing table from the interface ID, below.
156*8542734aSAndroid Build Coastguard Worker     //
157*8542734aSAndroid Build Coastguard Worker     // sInterfaceToTable stores the *global* routing table for the interface, and the local table is
158*8542734aSAndroid Build Coastguard Worker     // "global table - ROUTE_TABLE_OFFSET_FROM_INDEX + ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL"
159*8542734aSAndroid Build Coastguard Worker     auto iter = sInterfaceToTable.find(interface);
160*8542734aSAndroid Build Coastguard Worker     if (iter != sInterfaceToTable.end()) {
161*8542734aSAndroid Build Coastguard Worker         return getRouteTableIndexFromGlobalRouteTableIndex(iter->second, local);
162*8542734aSAndroid Build Coastguard Worker     }
163*8542734aSAndroid Build Coastguard Worker 
164*8542734aSAndroid Build Coastguard Worker     uint32_t index = RouteController::ifNameToIndexFunction(interface);
165*8542734aSAndroid Build Coastguard Worker     if (index == 0) {
166*8542734aSAndroid Build Coastguard Worker         ALOGE("cannot find interface %s: %s", interface, strerror(errno));
167*8542734aSAndroid Build Coastguard Worker         return RT_TABLE_UNSPEC;
168*8542734aSAndroid Build Coastguard Worker     }
169*8542734aSAndroid Build Coastguard Worker     index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
170*8542734aSAndroid Build Coastguard Worker     sInterfaceToTable[interface] = index;
171*8542734aSAndroid Build Coastguard Worker     return getRouteTableIndexFromGlobalRouteTableIndex(index, local);
172*8542734aSAndroid Build Coastguard Worker }
173*8542734aSAndroid Build Coastguard Worker 
getIfIndex(const char * interface)174*8542734aSAndroid Build Coastguard Worker uint32_t RouteController::getIfIndex(const char* interface) {
175*8542734aSAndroid Build Coastguard Worker     std::lock_guard lock(sInterfaceToTableLock);
176*8542734aSAndroid Build Coastguard Worker 
177*8542734aSAndroid Build Coastguard Worker     auto iter = sInterfaceToTable.find(interface);
178*8542734aSAndroid Build Coastguard Worker     if (iter == sInterfaceToTable.end()) {
179*8542734aSAndroid Build Coastguard Worker         ALOGE("getIfIndex: cannot find interface %s", interface);
180*8542734aSAndroid Build Coastguard Worker         return 0;
181*8542734aSAndroid Build Coastguard Worker     }
182*8542734aSAndroid Build Coastguard Worker 
183*8542734aSAndroid Build Coastguard Worker     // For interfaces that are not in the local network, the routing table is always the interface
184*8542734aSAndroid Build Coastguard Worker     // index plus ROUTE_TABLE_OFFSET_FROM_INDEX. But for interfaces in the local network, there's no
185*8542734aSAndroid Build Coastguard Worker     // way to know the interface index from this table. Return 0 here so callers of this method do
186*8542734aSAndroid Build Coastguard Worker     // not get confused.
187*8542734aSAndroid Build Coastguard Worker     // TODO: stop calling this method from any caller that only wants interfaces in client mode.
188*8542734aSAndroid Build Coastguard Worker     int ifindex = iter->second;
189*8542734aSAndroid Build Coastguard Worker     if (ifindex == ROUTE_TABLE_LOCAL_NETWORK) {
190*8542734aSAndroid Build Coastguard Worker         return 0;
191*8542734aSAndroid Build Coastguard Worker     }
192*8542734aSAndroid Build Coastguard Worker 
193*8542734aSAndroid Build Coastguard Worker     return ifindex - ROUTE_TABLE_OFFSET_FROM_INDEX;
194*8542734aSAndroid Build Coastguard Worker }
195*8542734aSAndroid Build Coastguard Worker 
getRouteTableForInterface(const char * interface,bool local)196*8542734aSAndroid Build Coastguard Worker uint32_t RouteController::getRouteTableForInterface(const char* interface, bool local) {
197*8542734aSAndroid Build Coastguard Worker     std::lock_guard lock(sInterfaceToTableLock);
198*8542734aSAndroid Build Coastguard Worker     return getRouteTableForInterfaceLocked(interface, local);
199*8542734aSAndroid Build Coastguard Worker }
200*8542734aSAndroid Build Coastguard Worker 
addTableName(uint32_t table,const std::string & name,std::string * contents)201*8542734aSAndroid Build Coastguard Worker void addTableName(uint32_t table, const std::string& name, std::string* contents) {
202*8542734aSAndroid Build Coastguard Worker     char tableString[UINT32_STRLEN];
203*8542734aSAndroid Build Coastguard Worker     snprintf(tableString, sizeof(tableString), "%u", table);
204*8542734aSAndroid Build Coastguard Worker     *contents += tableString;
205*8542734aSAndroid Build Coastguard Worker     *contents += " ";
206*8542734aSAndroid Build Coastguard Worker     *contents += name;
207*8542734aSAndroid Build Coastguard Worker     *contents += "\n";
208*8542734aSAndroid Build Coastguard Worker }
209*8542734aSAndroid Build Coastguard Worker 
210*8542734aSAndroid Build Coastguard Worker // Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
updateTableNamesFile()211*8542734aSAndroid Build Coastguard Worker void RouteController::updateTableNamesFile() {
212*8542734aSAndroid Build Coastguard Worker     std::string contents;
213*8542734aSAndroid Build Coastguard Worker 
214*8542734aSAndroid Build Coastguard Worker     addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
215*8542734aSAndroid Build Coastguard Worker     addTableName(RT_TABLE_MAIN,  ROUTE_TABLE_NAME_MAIN,  &contents);
216*8542734aSAndroid Build Coastguard Worker 
217*8542734aSAndroid Build Coastguard Worker     addTableName(ROUTE_TABLE_LOCAL_NETWORK,  ROUTE_TABLE_NAME_LOCAL_NETWORK,  &contents);
218*8542734aSAndroid Build Coastguard Worker     addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
219*8542734aSAndroid Build Coastguard Worker     addTableName(ROUTE_TABLE_LEGACY_SYSTEM,  ROUTE_TABLE_NAME_LEGACY_SYSTEM,  &contents);
220*8542734aSAndroid Build Coastguard Worker 
221*8542734aSAndroid Build Coastguard Worker     std::lock_guard lock(sInterfaceToTableLock);
222*8542734aSAndroid Build Coastguard Worker     for (const auto& [ifName, table] : sInterfaceToTable) {
223*8542734aSAndroid Build Coastguard Worker         if (table <= ROUTE_TABLE_OFFSET_FROM_INDEX) {
224*8542734aSAndroid Build Coastguard Worker             continue;
225*8542734aSAndroid Build Coastguard Worker         }
226*8542734aSAndroid Build Coastguard Worker         addTableName(table, ifName, &contents);
227*8542734aSAndroid Build Coastguard Worker         // Add table for the local route of the network. It's expected to be used for excluding the
228*8542734aSAndroid Build Coastguard Worker         // local traffic in the VPN network.
229*8542734aSAndroid Build Coastguard Worker         // Start from ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL plus with the interface table index.
230*8542734aSAndroid Build Coastguard Worker         uint32_t offset = ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL - ROUTE_TABLE_OFFSET_FROM_INDEX;
231*8542734aSAndroid Build Coastguard Worker         addTableName(offset + table, ifName + INTERFACE_LOCAL_SUFFIX, &contents);
232*8542734aSAndroid Build Coastguard Worker     }
233*8542734aSAndroid Build Coastguard Worker 
234*8542734aSAndroid Build Coastguard Worker     if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
235*8542734aSAndroid Build Coastguard Worker         ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
236*8542734aSAndroid Build Coastguard Worker         return;
237*8542734aSAndroid Build Coastguard Worker     }
238*8542734aSAndroid Build Coastguard Worker }
239*8542734aSAndroid Build Coastguard Worker 
240*8542734aSAndroid Build Coastguard Worker // Returns 0 on success or negative errno on failure.
padInterfaceName(const char * input,char * name,size_t * length,uint16_t * padding)241*8542734aSAndroid Build Coastguard Worker int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
242*8542734aSAndroid Build Coastguard Worker     if (!input) {
243*8542734aSAndroid Build Coastguard Worker         *length = 0;
244*8542734aSAndroid Build Coastguard Worker         *padding = 0;
245*8542734aSAndroid Build Coastguard Worker         return 0;
246*8542734aSAndroid Build Coastguard Worker     }
247*8542734aSAndroid Build Coastguard Worker     *length = strlcpy(name, input, IFNAMSIZ) + 1;
248*8542734aSAndroid Build Coastguard Worker     if (*length > IFNAMSIZ) {
249*8542734aSAndroid Build Coastguard Worker         ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
250*8542734aSAndroid Build Coastguard Worker         return -ENAMETOOLONG;
251*8542734aSAndroid Build Coastguard Worker     }
252*8542734aSAndroid Build Coastguard Worker     *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
253*8542734aSAndroid Build Coastguard Worker     return 0;
254*8542734aSAndroid Build Coastguard Worker }
255*8542734aSAndroid Build Coastguard Worker 
256*8542734aSAndroid Build Coastguard Worker // Adds or removes a routing rule for IPv4 and IPv6.
257*8542734aSAndroid Build Coastguard Worker //
258*8542734aSAndroid Build Coastguard Worker // + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
259*8542734aSAndroid Build Coastguard Worker //   unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
260*8542734aSAndroid Build Coastguard Worker // + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
261*8542734aSAndroid Build Coastguard Worker //   ignored.
262*8542734aSAndroid Build Coastguard Worker // + If |iif| is non-NULL, the rule matches the specified incoming interface.
263*8542734aSAndroid Build Coastguard Worker // + If |oif| is non-NULL, the rule matches the specified outgoing interface.
264*8542734aSAndroid Build Coastguard Worker // + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
265*8542734aSAndroid Build Coastguard Worker //   range (inclusive). Otherwise, the rule matches packets from all UIDs.
266*8542734aSAndroid Build Coastguard Worker //
267*8542734aSAndroid Build Coastguard Worker // Returns 0 on success or negative errno on failure.
modifyIpRule(uint16_t action,int32_t priority,uint8_t ruleType,uint32_t table,uint32_t fwmark,uint32_t mask,const char * iif,const char * oif,uid_t uidStart,uid_t uidEnd)268*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint8_t ruleType,
269*8542734aSAndroid Build Coastguard Worker                                       uint32_t table, uint32_t fwmark, uint32_t mask,
270*8542734aSAndroid Build Coastguard Worker                                       const char* iif, const char* oif, uid_t uidStart,
271*8542734aSAndroid Build Coastguard Worker                                       uid_t uidEnd) {
272*8542734aSAndroid Build Coastguard Worker     if (priority < 0) {
273*8542734aSAndroid Build Coastguard Worker         ALOGE("invalid IP-rule priority %d", priority);
274*8542734aSAndroid Build Coastguard Worker         return -ERANGE;
275*8542734aSAndroid Build Coastguard Worker     }
276*8542734aSAndroid Build Coastguard Worker 
277*8542734aSAndroid Build Coastguard Worker     // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
278*8542734aSAndroid Build Coastguard Worker     if (fwmark & ~mask) {
279*8542734aSAndroid Build Coastguard Worker         ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
280*8542734aSAndroid Build Coastguard Worker         return -ERANGE;
281*8542734aSAndroid Build Coastguard Worker     }
282*8542734aSAndroid Build Coastguard Worker 
283*8542734aSAndroid Build Coastguard Worker     // Interface names must include exactly one terminating NULL and be properly padded, or older
284*8542734aSAndroid Build Coastguard Worker     // kernels will refuse to delete rules.
285*8542734aSAndroid Build Coastguard Worker     char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
286*8542734aSAndroid Build Coastguard Worker     size_t iifLength, oifLength;
287*8542734aSAndroid Build Coastguard Worker     uint16_t iifPadding, oifPadding;
288*8542734aSAndroid Build Coastguard Worker     if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
289*8542734aSAndroid Build Coastguard Worker         return ret;
290*8542734aSAndroid Build Coastguard Worker     }
291*8542734aSAndroid Build Coastguard Worker     if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
292*8542734aSAndroid Build Coastguard Worker         return ret;
293*8542734aSAndroid Build Coastguard Worker     }
294*8542734aSAndroid Build Coastguard Worker 
295*8542734aSAndroid Build Coastguard Worker     // Either both start and end UID must be specified, or neither.
296*8542734aSAndroid Build Coastguard Worker     if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
297*8542734aSAndroid Build Coastguard Worker         ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
298*8542734aSAndroid Build Coastguard Worker         return -EUSERS;
299*8542734aSAndroid Build Coastguard Worker     }
300*8542734aSAndroid Build Coastguard Worker 
301*8542734aSAndroid Build Coastguard Worker     bool isUidRule = (uidStart != INVALID_UID);
302*8542734aSAndroid Build Coastguard Worker 
303*8542734aSAndroid Build Coastguard Worker     // Assemble a rule request and put it in an array of iovec structures.
304*8542734aSAndroid Build Coastguard Worker     fib_rule_hdr rule = {
305*8542734aSAndroid Build Coastguard Worker         .action = ruleType,
306*8542734aSAndroid Build Coastguard Worker         // Note that here we're implicitly setting rule.table to 0. When we want to specify a
307*8542734aSAndroid Build Coastguard Worker         // non-zero table, we do this via the FRATTR_TABLE attribute.
308*8542734aSAndroid Build Coastguard Worker     };
309*8542734aSAndroid Build Coastguard Worker 
310*8542734aSAndroid Build Coastguard Worker     // Don't ever create a rule that looks up table 0, because table 0 is the local table.
311*8542734aSAndroid Build Coastguard Worker     // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
312*8542734aSAndroid Build Coastguard Worker     // table 0, it's a wildcard that matches anything.
313*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
314*8542734aSAndroid Build Coastguard Worker         ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
315*8542734aSAndroid Build Coastguard Worker         return -ENOTUNIQ;
316*8542734aSAndroid Build Coastguard Worker     }
317*8542734aSAndroid Build Coastguard Worker 
318*8542734aSAndroid Build Coastguard Worker     rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
319*8542734aSAndroid Build Coastguard Worker     rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
320*8542734aSAndroid Build Coastguard Worker     struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
321*8542734aSAndroid Build Coastguard Worker 
322*8542734aSAndroid Build Coastguard Worker     iovec iov[] = {
323*8542734aSAndroid Build Coastguard Worker         { nullptr,              0 },
324*8542734aSAndroid Build Coastguard Worker         { &rule,             sizeof(rule) },
325*8542734aSAndroid Build Coastguard Worker         { &FRATTR_PRIORITY,  sizeof(FRATTR_PRIORITY) },
326*8542734aSAndroid Build Coastguard Worker         { &priority,         sizeof(priority) },
327*8542734aSAndroid Build Coastguard Worker         { &FRATTR_TABLE,     table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
328*8542734aSAndroid Build Coastguard Worker         { &table,            table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
329*8542734aSAndroid Build Coastguard Worker         { &FRATTR_FWMARK,    mask ? sizeof(FRATTR_FWMARK) : 0 },
330*8542734aSAndroid Build Coastguard Worker         { &fwmark,           mask ? sizeof(fwmark) : 0 },
331*8542734aSAndroid Build Coastguard Worker         { &FRATTR_FWMASK,    mask ? sizeof(FRATTR_FWMASK) : 0 },
332*8542734aSAndroid Build Coastguard Worker         { &mask,             mask ? sizeof(mask) : 0 },
333*8542734aSAndroid Build Coastguard Worker         { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
334*8542734aSAndroid Build Coastguard Worker         { &uidRange,         isUidRule ? sizeof(uidRange) : 0 },
335*8542734aSAndroid Build Coastguard Worker         { &fraIifName,       iif != IIF_NONE ? sizeof(fraIifName) : 0 },
336*8542734aSAndroid Build Coastguard Worker         { iifName,           iifLength },
337*8542734aSAndroid Build Coastguard Worker         { PADDING_BUFFER,    iifPadding },
338*8542734aSAndroid Build Coastguard Worker         { &fraOifName,       oif != OIF_NONE ? sizeof(fraOifName) : 0 },
339*8542734aSAndroid Build Coastguard Worker         { oifName,           oifLength },
340*8542734aSAndroid Build Coastguard Worker         { PADDING_BUFFER,    oifPadding },
341*8542734aSAndroid Build Coastguard Worker     };
342*8542734aSAndroid Build Coastguard Worker 
343*8542734aSAndroid Build Coastguard Worker     uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
344*8542734aSAndroid Build Coastguard Worker     for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
345*8542734aSAndroid Build Coastguard Worker         rule.family = AF_FAMILIES[i];
346*8542734aSAndroid Build Coastguard Worker         if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
347*8542734aSAndroid Build Coastguard Worker             if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
348*8542734aSAndroid Build Coastguard Worker                 // Don't log when deleting a tethering rule that's not there. This matches the
349*8542734aSAndroid Build Coastguard Worker                 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
350*8542734aSAndroid Build Coastguard Worker                 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
351*8542734aSAndroid Build Coastguard Worker                       strerror(-ret));
352*8542734aSAndroid Build Coastguard Worker             }
353*8542734aSAndroid Build Coastguard Worker             return ret;
354*8542734aSAndroid Build Coastguard Worker         }
355*8542734aSAndroid Build Coastguard Worker     }
356*8542734aSAndroid Build Coastguard Worker 
357*8542734aSAndroid Build Coastguard Worker     return 0;
358*8542734aSAndroid Build Coastguard Worker }
359*8542734aSAndroid Build Coastguard Worker 
modifyIpRule(uint16_t action,int32_t priority,uint32_t table,uint32_t fwmark,uint32_t mask,const char * iif,const char * oif,uid_t uidStart,uid_t uidEnd)360*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint32_t table,
361*8542734aSAndroid Build Coastguard Worker                                       uint32_t fwmark, uint32_t mask, const char* iif,
362*8542734aSAndroid Build Coastguard Worker                                       const char* oif, uid_t uidStart, uid_t uidEnd) {
363*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
364*8542734aSAndroid Build Coastguard Worker                         uidEnd);
365*8542734aSAndroid Build Coastguard Worker }
366*8542734aSAndroid Build Coastguard Worker 
modifyIpRule(uint16_t action,int32_t priority,uint32_t table,uint32_t fwmark,uint32_t mask)367*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint32_t table,
368*8542734aSAndroid Build Coastguard Worker                                       uint32_t fwmark, uint32_t mask) {
369*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
370*8542734aSAndroid Build Coastguard Worker                         INVALID_UID);
371*8542734aSAndroid Build Coastguard Worker }
372*8542734aSAndroid Build Coastguard Worker 
373*8542734aSAndroid Build Coastguard Worker // Adds or deletes an IPv4 or IPv6 route.
374*8542734aSAndroid Build Coastguard Worker // Returns 0 on success or negative errno on failure.
modifyIpRoute(uint16_t action,uint16_t flags,uint32_t table,const char * interface,const char * destination,const char * nexthop,uint32_t mtu,uint32_t priority)375*8542734aSAndroid Build Coastguard Worker int modifyIpRoute(uint16_t action, uint16_t flags, uint32_t table, const char* interface,
376*8542734aSAndroid Build Coastguard Worker                   const char* destination, const char* nexthop, uint32_t mtu, uint32_t priority) {
377*8542734aSAndroid Build Coastguard Worker     // At least the destination must be non-null.
378*8542734aSAndroid Build Coastguard Worker     if (!destination) {
379*8542734aSAndroid Build Coastguard Worker         ALOGE("null destination");
380*8542734aSAndroid Build Coastguard Worker         return -EFAULT;
381*8542734aSAndroid Build Coastguard Worker     }
382*8542734aSAndroid Build Coastguard Worker 
383*8542734aSAndroid Build Coastguard Worker     // Parse the prefix.
384*8542734aSAndroid Build Coastguard Worker     uint8_t rawAddress[sizeof(in6_addr)];
385*8542734aSAndroid Build Coastguard Worker     uint8_t family;
386*8542734aSAndroid Build Coastguard Worker     uint8_t prefixLength;
387*8542734aSAndroid Build Coastguard Worker     int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
388*8542734aSAndroid Build Coastguard Worker                                 &prefixLength);
389*8542734aSAndroid Build Coastguard Worker     if (rawLength < 0) {
390*8542734aSAndroid Build Coastguard Worker         ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
391*8542734aSAndroid Build Coastguard Worker         return rawLength;
392*8542734aSAndroid Build Coastguard Worker     }
393*8542734aSAndroid Build Coastguard Worker 
394*8542734aSAndroid Build Coastguard Worker     if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
395*8542734aSAndroid Build Coastguard Worker         ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
396*8542734aSAndroid Build Coastguard Worker         return -ENOBUFS;  // Cannot happen; parsePrefix only supports IPv4 and IPv6.
397*8542734aSAndroid Build Coastguard Worker     }
398*8542734aSAndroid Build Coastguard Worker 
399*8542734aSAndroid Build Coastguard Worker     uint8_t type = RTN_UNICAST;
400*8542734aSAndroid Build Coastguard Worker     uint32_t ifindex;
401*8542734aSAndroid Build Coastguard Worker     uint8_t rawNexthop[sizeof(in6_addr)];
402*8542734aSAndroid Build Coastguard Worker 
403*8542734aSAndroid Build Coastguard Worker     if (nexthop && !strcmp(nexthop, "unreachable")) {
404*8542734aSAndroid Build Coastguard Worker         type = RTN_UNREACHABLE;
405*8542734aSAndroid Build Coastguard Worker         // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
406*8542734aSAndroid Build Coastguard Worker         // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
407*8542734aSAndroid Build Coastguard Worker         // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
408*8542734aSAndroid Build Coastguard Worker         interface = OIF_NONE;
409*8542734aSAndroid Build Coastguard Worker         nexthop = nullptr;
410*8542734aSAndroid Build Coastguard Worker     } else if (nexthop && !strcmp(nexthop, "throw")) {
411*8542734aSAndroid Build Coastguard Worker         type = RTN_THROW;
412*8542734aSAndroid Build Coastguard Worker         interface = OIF_NONE;
413*8542734aSAndroid Build Coastguard Worker         nexthop = nullptr;
414*8542734aSAndroid Build Coastguard Worker     } else {
415*8542734aSAndroid Build Coastguard Worker         // If an interface was specified, find the ifindex.
416*8542734aSAndroid Build Coastguard Worker         if (interface != OIF_NONE) {
417*8542734aSAndroid Build Coastguard Worker             ifindex = RouteController::ifNameToIndexFunction(interface);
418*8542734aSAndroid Build Coastguard Worker 
419*8542734aSAndroid Build Coastguard Worker             if (!ifindex) {
420*8542734aSAndroid Build Coastguard Worker                 ALOGE("cannot find interface %s", interface);
421*8542734aSAndroid Build Coastguard Worker                 return -ENODEV;
422*8542734aSAndroid Build Coastguard Worker             }
423*8542734aSAndroid Build Coastguard Worker         }
424*8542734aSAndroid Build Coastguard Worker 
425*8542734aSAndroid Build Coastguard Worker         // If a nexthop was specified, parse it as the same family as the prefix.
426*8542734aSAndroid Build Coastguard Worker         if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
427*8542734aSAndroid Build Coastguard Worker             ALOGE("inet_pton failed for nexthop %s", nexthop);
428*8542734aSAndroid Build Coastguard Worker             return -EINVAL;
429*8542734aSAndroid Build Coastguard Worker         }
430*8542734aSAndroid Build Coastguard Worker     }
431*8542734aSAndroid Build Coastguard Worker 
432*8542734aSAndroid Build Coastguard Worker     // Assemble a rtmsg and put it in an array of iovec structures.
433*8542734aSAndroid Build Coastguard Worker     rtmsg route = {
434*8542734aSAndroid Build Coastguard Worker             .rtm_family = family,
435*8542734aSAndroid Build Coastguard Worker             .rtm_dst_len = prefixLength,
436*8542734aSAndroid Build Coastguard Worker             .rtm_protocol = RTPROT_STATIC,
437*8542734aSAndroid Build Coastguard Worker             .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
438*8542734aSAndroid Build Coastguard Worker             .rtm_type = type,
439*8542734aSAndroid Build Coastguard Worker     };
440*8542734aSAndroid Build Coastguard Worker 
441*8542734aSAndroid Build Coastguard Worker     rtattr rtaDst     = { U16_RTA_LENGTH(rawLength), RTA_DST };
442*8542734aSAndroid Build Coastguard Worker     rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
443*8542734aSAndroid Build Coastguard Worker 
444*8542734aSAndroid Build Coastguard Worker     iovec iov[] = {
445*8542734aSAndroid Build Coastguard Worker             {nullptr, 0},
446*8542734aSAndroid Build Coastguard Worker             {&route, sizeof(route)},
447*8542734aSAndroid Build Coastguard Worker             {&RTATTR_TABLE, sizeof(RTATTR_TABLE)},
448*8542734aSAndroid Build Coastguard Worker             {&table, sizeof(table)},
449*8542734aSAndroid Build Coastguard Worker             {&rtaDst, sizeof(rtaDst)},
450*8542734aSAndroid Build Coastguard Worker             {rawAddress, static_cast<size_t>(rawLength)},
451*8542734aSAndroid Build Coastguard Worker             {&RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0},
452*8542734aSAndroid Build Coastguard Worker             {&ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0},
453*8542734aSAndroid Build Coastguard Worker             {&rtaGateway, nexthop ? sizeof(rtaGateway) : 0},
454*8542734aSAndroid Build Coastguard Worker             {rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0},
455*8542734aSAndroid Build Coastguard Worker             {&RTATTR_METRICS, mtu != 0 ? sizeof(RTATTR_METRICS) : 0},
456*8542734aSAndroid Build Coastguard Worker             {&RTATTRX_MTU, mtu != 0 ? sizeof(RTATTRX_MTU) : 0},
457*8542734aSAndroid Build Coastguard Worker             {&mtu, mtu != 0 ? sizeof(mtu) : 0},
458*8542734aSAndroid Build Coastguard Worker             {&RTATTR_PRIO, priority != 0 ? sizeof(RTATTR_PRIO) : 0},
459*8542734aSAndroid Build Coastguard Worker             {&priority, priority != 0 ? sizeof(priority) : 0},
460*8542734aSAndroid Build Coastguard Worker     };
461*8542734aSAndroid Build Coastguard Worker 
462*8542734aSAndroid Build Coastguard Worker     // Allow creating multiple link-local routes in the same table, so we can make IPv6
463*8542734aSAndroid Build Coastguard Worker     // work on all interfaces in the local_network table.
464*8542734aSAndroid Build Coastguard Worker     if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
465*8542734aSAndroid Build Coastguard Worker         flags &= ~NLM_F_EXCL;
466*8542734aSAndroid Build Coastguard Worker     }
467*8542734aSAndroid Build Coastguard Worker 
468*8542734aSAndroid Build Coastguard Worker     int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
469*8542734aSAndroid Build Coastguard Worker     if (ret) {
470*8542734aSAndroid Build Coastguard Worker         ALOGE("Error %s route %s -> %s %s to table %u: %s",
471*8542734aSAndroid Build Coastguard Worker               actionName(action), destination, nexthop, interface, table, strerror(-ret));
472*8542734aSAndroid Build Coastguard Worker     }
473*8542734aSAndroid Build Coastguard Worker     return ret;
474*8542734aSAndroid Build Coastguard Worker }
475*8542734aSAndroid Build Coastguard Worker 
476*8542734aSAndroid Build Coastguard Worker // An iptables rule to mark incoming packets on a network with the netId of the network.
477*8542734aSAndroid Build Coastguard Worker //
478*8542734aSAndroid Build Coastguard Worker // This is so that the kernel can:
479*8542734aSAndroid Build Coastguard Worker // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
480*8542734aSAndroid Build Coastguard Worker //   replies, SYN-ACKs, etc).
481*8542734aSAndroid Build Coastguard Worker // + Mark sockets that accept connections from this interface so that the connection stays on the
482*8542734aSAndroid Build Coastguard Worker //   same interface.
modifyIncomingPacketMark(unsigned netId,const char * interface,Permission permission,bool add)483*8542734aSAndroid Build Coastguard Worker int modifyIncomingPacketMark(unsigned netId, const char* interface, Permission permission,
484*8542734aSAndroid Build Coastguard Worker                              bool add) {
485*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
486*8542734aSAndroid Build Coastguard Worker 
487*8542734aSAndroid Build Coastguard Worker     fwmark.netId = netId;
488*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = true;
489*8542734aSAndroid Build Coastguard Worker     fwmark.protectedFromVpn = true;
490*8542734aSAndroid Build Coastguard Worker     fwmark.permission = permission;
491*8542734aSAndroid Build Coastguard Worker 
492*8542734aSAndroid Build Coastguard Worker     const uint32_t mask = Fwmark::getUidBillingMask() | Fwmark::getIngressCpuWakeupMask();
493*8542734aSAndroid Build Coastguard Worker 
494*8542734aSAndroid Build Coastguard Worker     std::string cmd = StringPrintf(
495*8542734aSAndroid Build Coastguard Worker         "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
496*8542734aSAndroid Build Coastguard Worker         RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, ~mask);
497*8542734aSAndroid Build Coastguard Worker     if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
498*8542734aSAndroid Build Coastguard Worker         ALOGE("failed to change iptables rule that sets incoming packet mark");
499*8542734aSAndroid Build Coastguard Worker         return -EREMOTEIO;
500*8542734aSAndroid Build Coastguard Worker     }
501*8542734aSAndroid Build Coastguard Worker 
502*8542734aSAndroid Build Coastguard Worker     return 0;
503*8542734aSAndroid Build Coastguard Worker }
504*8542734aSAndroid Build Coastguard Worker 
505*8542734aSAndroid Build Coastguard Worker // A rule to route responses to the local network forwarded via the VPN.
506*8542734aSAndroid Build Coastguard Worker //
507*8542734aSAndroid Build Coastguard Worker // When a VPN is in effect, packets from the local network to upstream networks are forwarded into
508*8542734aSAndroid Build Coastguard Worker // the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
modifyVpnOutputToLocalRule(const char * vpnInterface,bool add)509*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
510*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
511*8542734aSAndroid Build Coastguard Worker                         ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
512*8542734aSAndroid Build Coastguard Worker                         INVALID_UID, INVALID_UID);
513*8542734aSAndroid Build Coastguard Worker }
514*8542734aSAndroid Build Coastguard Worker 
515*8542734aSAndroid Build Coastguard Worker // A rule to route all traffic from a given set of UIDs to go over the VPN.
516*8542734aSAndroid Build Coastguard Worker //
517*8542734aSAndroid Build Coastguard Worker // Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
518*8542734aSAndroid Build Coastguard Worker // have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
519*8542734aSAndroid Build Coastguard Worker // bypass the VPN if the protectedFromVpn bit is set.
modifyVpnUidRangeRule(uint32_t table,uid_t uidStart,uid_t uidEnd,int32_t subPriority,bool secure,bool add,bool excludeLocalRoutes)520*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
521*8542734aSAndroid Build Coastguard Worker                                                int32_t subPriority, bool secure, bool add,
522*8542734aSAndroid Build Coastguard Worker                                                bool excludeLocalRoutes) {
523*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
524*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
525*8542734aSAndroid Build Coastguard Worker 
526*8542734aSAndroid Build Coastguard Worker     fwmark.protectedFromVpn = false;
527*8542734aSAndroid Build Coastguard Worker     mask.protectedFromVpn = true;
528*8542734aSAndroid Build Coastguard Worker 
529*8542734aSAndroid Build Coastguard Worker     int32_t priority;
530*8542734aSAndroid Build Coastguard Worker 
531*8542734aSAndroid Build Coastguard Worker     if (secure) {
532*8542734aSAndroid Build Coastguard Worker         priority = RULE_PRIORITY_SECURE_VPN;
533*8542734aSAndroid Build Coastguard Worker     } else {
534*8542734aSAndroid Build Coastguard Worker         priority = excludeLocalRoutes ? RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION
535*8542734aSAndroid Build Coastguard Worker                                       : RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION;
536*8542734aSAndroid Build Coastguard Worker 
537*8542734aSAndroid Build Coastguard Worker         fwmark.explicitlySelected = false;
538*8542734aSAndroid Build Coastguard Worker         mask.explicitlySelected = true;
539*8542734aSAndroid Build Coastguard Worker     }
540*8542734aSAndroid Build Coastguard Worker 
541*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority + subPriority, table,
542*8542734aSAndroid Build Coastguard Worker                         fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
543*8542734aSAndroid Build Coastguard Worker }
544*8542734aSAndroid Build Coastguard Worker 
545*8542734aSAndroid Build Coastguard Worker // A rule to allow system apps to send traffic over this VPN even if they are not part of the target
546*8542734aSAndroid Build Coastguard Worker // set of UIDs.
547*8542734aSAndroid Build Coastguard Worker //
548*8542734aSAndroid Build Coastguard Worker // This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
549*8542734aSAndroid Build Coastguard Worker // target set, but where the DnsProxyListener itself is not.
modifyVpnSystemPermissionRule(unsigned netId,uint32_t table,bool secure,bool add,bool excludeLocalRoutes)550*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
551*8542734aSAndroid Build Coastguard Worker                                                        bool add, bool excludeLocalRoutes) {
552*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
553*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
554*8542734aSAndroid Build Coastguard Worker 
555*8542734aSAndroid Build Coastguard Worker     fwmark.netId = netId;
556*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
557*8542734aSAndroid Build Coastguard Worker 
558*8542734aSAndroid Build Coastguard Worker     fwmark.permission = PERMISSION_SYSTEM;
559*8542734aSAndroid Build Coastguard Worker     mask.permission = PERMISSION_SYSTEM;
560*8542734aSAndroid Build Coastguard Worker 
561*8542734aSAndroid Build Coastguard Worker     uint32_t priority;
562*8542734aSAndroid Build Coastguard Worker 
563*8542734aSAndroid Build Coastguard Worker     if (secure) {
564*8542734aSAndroid Build Coastguard Worker         priority = RULE_PRIORITY_SECURE_VPN;
565*8542734aSAndroid Build Coastguard Worker     } else {
566*8542734aSAndroid Build Coastguard Worker         priority = excludeLocalRoutes ? RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION
567*8542734aSAndroid Build Coastguard Worker                                       : RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION;
568*8542734aSAndroid Build Coastguard Worker     }
569*8542734aSAndroid Build Coastguard Worker 
570*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
571*8542734aSAndroid Build Coastguard Worker                         mask.intValue);
572*8542734aSAndroid Build Coastguard Worker }
573*8542734aSAndroid Build Coastguard Worker 
574*8542734aSAndroid Build Coastguard Worker // A rule to route traffic based on an explicitly chosen network.
575*8542734aSAndroid Build Coastguard Worker //
576*8542734aSAndroid Build Coastguard Worker // Supports apps that use the multinetwork APIs to restrict their traffic to a network.
577*8542734aSAndroid Build Coastguard Worker //
578*8542734aSAndroid Build Coastguard Worker // Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
579*8542734aSAndroid Build Coastguard Worker // to check it again in the rules here, because a network's permissions may have been updated via
580*8542734aSAndroid Build Coastguard Worker // modifyNetworkPermission().
modifyExplicitNetworkRule(unsigned netId,uint32_t table,Permission permission,uid_t uidStart,uid_t uidEnd,int32_t subPriority,bool add)581*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
582*8542734aSAndroid Build Coastguard Worker                                                    Permission permission, uid_t uidStart,
583*8542734aSAndroid Build Coastguard Worker                                                    uid_t uidEnd, int32_t subPriority, bool add) {
584*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
585*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
586*8542734aSAndroid Build Coastguard Worker 
587*8542734aSAndroid Build Coastguard Worker     fwmark.netId = netId;
588*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
589*8542734aSAndroid Build Coastguard Worker 
590*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = true;
591*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
592*8542734aSAndroid Build Coastguard Worker 
593*8542734aSAndroid Build Coastguard Worker     fwmark.permission = permission;
594*8542734aSAndroid Build Coastguard Worker     mask.permission = permission;
595*8542734aSAndroid Build Coastguard Worker 
596*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
597*8542734aSAndroid Build Coastguard Worker                         RULE_PRIORITY_EXPLICIT_NETWORK + subPriority, table, fwmark.intValue,
598*8542734aSAndroid Build Coastguard Worker                         mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
599*8542734aSAndroid Build Coastguard Worker }
600*8542734aSAndroid Build Coastguard Worker 
601*8542734aSAndroid Build Coastguard Worker // A rule to route traffic based on an local network.
602*8542734aSAndroid Build Coastguard Worker //
603*8542734aSAndroid Build Coastguard Worker // Supports apps that send traffic to local IPs without binding to a particular network.
604*8542734aSAndroid Build Coastguard Worker //
modifyLocalNetworkRule(uint32_t table,bool add)605*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyLocalNetworkRule(uint32_t table, bool add) {
606*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
607*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
608*8542734aSAndroid Build Coastguard Worker 
609*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = false;
610*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
611*8542734aSAndroid Build Coastguard Worker 
612*8542734aSAndroid Build Coastguard Worker     if (const int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_LOCAL_NETWORK,
613*8542734aSAndroid Build Coastguard Worker                                      table, fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE,
614*8542734aSAndroid Build Coastguard Worker                                      INVALID_UID, INVALID_UID)) {
615*8542734aSAndroid Build Coastguard Worker         return ret;
616*8542734aSAndroid Build Coastguard Worker     }
617*8542734aSAndroid Build Coastguard Worker 
618*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = true;
619*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
620*8542734aSAndroid Build Coastguard Worker 
621*8542734aSAndroid Build Coastguard Worker     fwmark.netId = INetd::LOCAL_NET_ID;
622*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
623*8542734aSAndroid Build Coastguard Worker 
624*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
625*8542734aSAndroid Build Coastguard Worker                         fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
626*8542734aSAndroid Build Coastguard Worker                         INVALID_UID);
627*8542734aSAndroid Build Coastguard Worker }
628*8542734aSAndroid Build Coastguard Worker 
629*8542734aSAndroid Build Coastguard Worker // A rule to route traffic based on a chosen outgoing interface.
630*8542734aSAndroid Build Coastguard Worker //
631*8542734aSAndroid Build Coastguard Worker // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
632*8542734aSAndroid Build Coastguard Worker // the outgoing interface (typically for link-local communications).
modifyOutputInterfaceRules(const char * interface,uint32_t table,Permission permission,uid_t uidStart,uid_t uidEnd,int32_t subPriority,bool add)633*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyOutputInterfaceRules(const char* interface, uint32_t table,
634*8542734aSAndroid Build Coastguard Worker                                                     Permission permission, uid_t uidStart,
635*8542734aSAndroid Build Coastguard Worker                                                     uid_t uidEnd, int32_t subPriority, bool add) {
636*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
637*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
638*8542734aSAndroid Build Coastguard Worker 
639*8542734aSAndroid Build Coastguard Worker     fwmark.permission = permission;
640*8542734aSAndroid Build Coastguard Worker     mask.permission = permission;
641*8542734aSAndroid Build Coastguard Worker 
642*8542734aSAndroid Build Coastguard Worker     // If this rule does not specify a UID range, then also add a corresponding high-priority rule
643*8542734aSAndroid Build Coastguard Worker     // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
644*8542734aSAndroid Build Coastguard Worker     // sockets as root.
645*8542734aSAndroid Build Coastguard Worker     if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
646*8542734aSAndroid Build Coastguard Worker         if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
647*8542734aSAndroid Build Coastguard Worker                                    table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
648*8542734aSAndroid Build Coastguard Worker                                    UID_ROOT, UID_ROOT)) {
649*8542734aSAndroid Build Coastguard Worker             return ret;
650*8542734aSAndroid Build Coastguard Worker         }
651*8542734aSAndroid Build Coastguard Worker     }
652*8542734aSAndroid Build Coastguard Worker 
653*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
654*8542734aSAndroid Build Coastguard Worker                         RULE_PRIORITY_OUTPUT_INTERFACE + subPriority, table, fwmark.intValue,
655*8542734aSAndroid Build Coastguard Worker                         mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
656*8542734aSAndroid Build Coastguard Worker }
657*8542734aSAndroid Build Coastguard Worker 
658*8542734aSAndroid Build Coastguard Worker // A rule to route traffic based on the chosen network.
659*8542734aSAndroid Build Coastguard Worker //
660*8542734aSAndroid Build Coastguard Worker // This is for sockets that have not explicitly requested a particular network, but have been
661*8542734aSAndroid Build Coastguard Worker // bound to one when they called connect(). This ensures that sockets connected on a particular
662*8542734aSAndroid Build Coastguard Worker // network stay on that network even if the default network changes.
modifyImplicitNetworkRule(unsigned netId,uint32_t table,bool add)663*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
664*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
665*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
666*8542734aSAndroid Build Coastguard Worker 
667*8542734aSAndroid Build Coastguard Worker     fwmark.netId = netId;
668*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
669*8542734aSAndroid Build Coastguard Worker 
670*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = false;
671*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
672*8542734aSAndroid Build Coastguard Worker 
673*8542734aSAndroid Build Coastguard Worker     fwmark.permission = PERMISSION_NONE;
674*8542734aSAndroid Build Coastguard Worker     mask.permission = PERMISSION_NONE;
675*8542734aSAndroid Build Coastguard Worker 
676*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
677*8542734aSAndroid Build Coastguard Worker                         fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
678*8542734aSAndroid Build Coastguard Worker                         INVALID_UID);
679*8542734aSAndroid Build Coastguard Worker }
680*8542734aSAndroid Build Coastguard Worker 
modifyVpnLocalExclusionRule(bool add,const char * physicalInterface)681*8542734aSAndroid Build Coastguard Worker int RouteController::modifyVpnLocalExclusionRule(bool add, const char* physicalInterface) {
682*8542734aSAndroid Build Coastguard Worker     uint32_t table = getRouteTableForInterface(physicalInterface, true /* local */);
683*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC) {
684*8542734aSAndroid Build Coastguard Worker         return -ESRCH;
685*8542734aSAndroid Build Coastguard Worker     }
686*8542734aSAndroid Build Coastguard Worker 
687*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
688*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
689*8542734aSAndroid Build Coastguard Worker 
690*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = false;
691*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
692*8542734aSAndroid Build Coastguard Worker 
693*8542734aSAndroid Build Coastguard Worker     fwmark.permission = PERMISSION_NONE;
694*8542734aSAndroid Build Coastguard Worker     mask.permission = PERMISSION_NONE;
695*8542734aSAndroid Build Coastguard Worker 
696*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_LOCAL_ROUTES, table,
697*8542734aSAndroid Build Coastguard Worker                         fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
698*8542734aSAndroid Build Coastguard Worker                         INVALID_UID);
699*8542734aSAndroid Build Coastguard Worker }
700*8542734aSAndroid Build Coastguard Worker 
addFixedLocalRoutes(const char * interface)701*8542734aSAndroid Build Coastguard Worker int RouteController::addFixedLocalRoutes(const char* interface) {
702*8542734aSAndroid Build Coastguard Worker     for (size_t i = 0; i < ARRAY_SIZE(V4_FIXED_LOCAL_PREFIXES); ++i) {
703*8542734aSAndroid Build Coastguard Worker         if (int ret = modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface,
704*8542734aSAndroid Build Coastguard Worker                                   V4_FIXED_LOCAL_PREFIXES[i], nullptr /* nexthop */,
705*8542734aSAndroid Build Coastguard Worker                                   RouteController::INTERFACE, 0 /* mtu */, 0 /* priority */,
706*8542734aSAndroid Build Coastguard Worker                                   true /* isLocal */)) {
707*8542734aSAndroid Build Coastguard Worker             return ret;
708*8542734aSAndroid Build Coastguard Worker         }
709*8542734aSAndroid Build Coastguard Worker     }
710*8542734aSAndroid Build Coastguard Worker 
711*8542734aSAndroid Build Coastguard Worker     return 0;
712*8542734aSAndroid Build Coastguard Worker }
713*8542734aSAndroid Build Coastguard Worker 
714*8542734aSAndroid Build Coastguard Worker // A rule to enable split tunnel VPNs.
715*8542734aSAndroid Build Coastguard Worker //
716*8542734aSAndroid Build Coastguard Worker // If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
717*8542734aSAndroid Build Coastguard Worker // go over the default network, provided it has the permissions required by the default network.
modifyVpnFallthroughRule(uint16_t action,unsigned vpnNetId,const char * physicalInterface,Permission permission)718*8542734aSAndroid Build Coastguard Worker int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
719*8542734aSAndroid Build Coastguard Worker                                               const char* physicalInterface,
720*8542734aSAndroid Build Coastguard Worker                                               Permission permission) {
721*8542734aSAndroid Build Coastguard Worker     uint32_t table = getRouteTableForInterface(physicalInterface, false /* local */);
722*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC) {
723*8542734aSAndroid Build Coastguard Worker         return -ESRCH;
724*8542734aSAndroid Build Coastguard Worker     }
725*8542734aSAndroid Build Coastguard Worker 
726*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
727*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
728*8542734aSAndroid Build Coastguard Worker 
729*8542734aSAndroid Build Coastguard Worker     fwmark.netId = vpnNetId;
730*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
731*8542734aSAndroid Build Coastguard Worker 
732*8542734aSAndroid Build Coastguard Worker     fwmark.permission = permission;
733*8542734aSAndroid Build Coastguard Worker     mask.permission = permission;
734*8542734aSAndroid Build Coastguard Worker 
735*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
736*8542734aSAndroid Build Coastguard Worker                         mask.intValue);
737*8542734aSAndroid Build Coastguard Worker }
738*8542734aSAndroid Build Coastguard Worker 
739*8542734aSAndroid Build Coastguard Worker // Add rules to allow legacy routes added through the requestRouteToHost() API.
addLegacyRouteRules()740*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int addLegacyRouteRules() {
741*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
742*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
743*8542734aSAndroid Build Coastguard Worker 
744*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = false;
745*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
746*8542734aSAndroid Build Coastguard Worker 
747*8542734aSAndroid Build Coastguard Worker     // Rules to allow legacy routes to override the default network.
748*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
749*8542734aSAndroid Build Coastguard Worker                                fwmark.intValue, mask.intValue)) {
750*8542734aSAndroid Build Coastguard Worker         return ret;
751*8542734aSAndroid Build Coastguard Worker     }
752*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
753*8542734aSAndroid Build Coastguard Worker                                ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
754*8542734aSAndroid Build Coastguard Worker         return ret;
755*8542734aSAndroid Build Coastguard Worker     }
756*8542734aSAndroid Build Coastguard Worker 
757*8542734aSAndroid Build Coastguard Worker     fwmark.permission = PERMISSION_SYSTEM;
758*8542734aSAndroid Build Coastguard Worker     mask.permission = PERMISSION_SYSTEM;
759*8542734aSAndroid Build Coastguard Worker 
760*8542734aSAndroid Build Coastguard Worker     // A rule to allow legacy routes from system apps to override VPNs.
761*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
762*8542734aSAndroid Build Coastguard Worker                         fwmark.intValue, mask.intValue);
763*8542734aSAndroid Build Coastguard Worker }
764*8542734aSAndroid Build Coastguard Worker 
765*8542734aSAndroid Build Coastguard Worker // Add rules to lookup the local network when specified explicitly or otherwise.
addLocalNetworkRules(unsigned localNetId)766*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int addLocalNetworkRules(unsigned localNetId) {
767*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
768*8542734aSAndroid Build Coastguard Worker                                             INVALID_UID, INVALID_UID,
769*8542734aSAndroid Build Coastguard Worker                                             UidRanges::SUB_PRIORITY_HIGHEST, ACTION_ADD)) {
770*8542734aSAndroid Build Coastguard Worker         return ret;
771*8542734aSAndroid Build Coastguard Worker     }
772*8542734aSAndroid Build Coastguard Worker 
773*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
774*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
775*8542734aSAndroid Build Coastguard Worker 
776*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = false;
777*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
778*8542734aSAndroid Build Coastguard Worker 
779*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
780*8542734aSAndroid Build Coastguard Worker                         fwmark.intValue, mask.intValue);
781*8542734aSAndroid Build Coastguard Worker }
782*8542734aSAndroid Build Coastguard Worker 
783*8542734aSAndroid Build Coastguard Worker /* static */
configureDummyNetwork()784*8542734aSAndroid Build Coastguard Worker int RouteController::configureDummyNetwork() {
785*8542734aSAndroid Build Coastguard Worker     const char *interface = DummyNetwork::INTERFACE_NAME;
786*8542734aSAndroid Build Coastguard Worker     uint32_t table = getRouteTableForInterface(interface, false /* local */);
787*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC) {
788*8542734aSAndroid Build Coastguard Worker         // getRouteTableForInterface has already logged an error.
789*8542734aSAndroid Build Coastguard Worker         return -ESRCH;
790*8542734aSAndroid Build Coastguard Worker     }
791*8542734aSAndroid Build Coastguard Worker 
792*8542734aSAndroid Build Coastguard Worker     ifc_init();
793*8542734aSAndroid Build Coastguard Worker     int ret = ifc_up(interface);
794*8542734aSAndroid Build Coastguard Worker     ifc_close();
795*8542734aSAndroid Build Coastguard Worker     if (ret) {
796*8542734aSAndroid Build Coastguard Worker         ALOGE("Can't bring up %s: %s", interface, strerror(errno));
797*8542734aSAndroid Build Coastguard Worker         return -errno;
798*8542734aSAndroid Build Coastguard Worker     }
799*8542734aSAndroid Build Coastguard Worker 
800*8542734aSAndroid Build Coastguard Worker     if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, INVALID_UID,
801*8542734aSAndroid Build Coastguard Worker                                           INVALID_UID, UidRanges::SUB_PRIORITY_HIGHEST,
802*8542734aSAndroid Build Coastguard Worker                                           ACTION_ADD))) {
803*8542734aSAndroid Build Coastguard Worker         ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
804*8542734aSAndroid Build Coastguard Worker         return ret;
805*8542734aSAndroid Build Coastguard Worker     }
806*8542734aSAndroid Build Coastguard Worker 
807*8542734aSAndroid Build Coastguard Worker     if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface,
808*8542734aSAndroid Build Coastguard Worker                              "0.0.0.0/0", nullptr, 0 /* mtu */, 0 /* priority */))) {
809*8542734aSAndroid Build Coastguard Worker         return ret;
810*8542734aSAndroid Build Coastguard Worker     }
811*8542734aSAndroid Build Coastguard Worker 
812*8542734aSAndroid Build Coastguard Worker     if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface, "::/0",
813*8542734aSAndroid Build Coastguard Worker                              nullptr, 0 /* mtu */, 0 /* priority */))) {
814*8542734aSAndroid Build Coastguard Worker         return ret;
815*8542734aSAndroid Build Coastguard Worker     }
816*8542734aSAndroid Build Coastguard Worker 
817*8542734aSAndroid Build Coastguard Worker     return 0;
818*8542734aSAndroid Build Coastguard Worker }
819*8542734aSAndroid Build Coastguard Worker 
820*8542734aSAndroid Build Coastguard Worker // Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
821*8542734aSAndroid Build Coastguard Worker // relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
822*8542734aSAndroid Build Coastguard Worker // behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
823*8542734aSAndroid Build Coastguard Worker // rule will hopefully make things even clearer.
addUnreachableRule()824*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int addUnreachableRule() {
825*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
826*8542734aSAndroid Build Coastguard Worker                         MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
827*8542734aSAndroid Build Coastguard Worker }
828*8542734aSAndroid Build Coastguard Worker 
modifyLocalNetwork(unsigned netId,const char * interface,bool add)829*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
830*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
831*8542734aSAndroid Build Coastguard Worker         return ret;
832*8542734aSAndroid Build Coastguard Worker     }
833*8542734aSAndroid Build Coastguard Worker     maybeModifyQdiscClsact(interface, add);
834*8542734aSAndroid Build Coastguard Worker     return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
835*8542734aSAndroid Build Coastguard Worker                                       INVALID_UID, INVALID_UID, UidRanges::SUB_PRIORITY_HIGHEST,
836*8542734aSAndroid Build Coastguard Worker                                       add);
837*8542734aSAndroid Build Coastguard Worker }
838*8542734aSAndroid Build Coastguard Worker 
modifyUidNetworkRule(unsigned netId,uint32_t table,uid_t uidStart,uid_t uidEnd,int32_t subPriority,bool add,bool explicitSelect)839*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyUidNetworkRule(unsigned netId, uint32_t table, uid_t uidStart,
840*8542734aSAndroid Build Coastguard Worker                                               uid_t uidEnd, int32_t subPriority, bool add,
841*8542734aSAndroid Build Coastguard Worker                                               bool explicitSelect) {
842*8542734aSAndroid Build Coastguard Worker     if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
843*8542734aSAndroid Build Coastguard Worker         ALOGE("modifyUidNetworkRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
844*8542734aSAndroid Build Coastguard Worker         return -EUSERS;
845*8542734aSAndroid Build Coastguard Worker     }
846*8542734aSAndroid Build Coastguard Worker 
847*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
848*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
849*8542734aSAndroid Build Coastguard Worker 
850*8542734aSAndroid Build Coastguard Worker     fwmark.netId = netId;
851*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
852*8542734aSAndroid Build Coastguard Worker 
853*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = explicitSelect;
854*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
855*8542734aSAndroid Build Coastguard Worker 
856*8542734aSAndroid Build Coastguard Worker     // Access to this network is controlled by UID rules, not permission bits.
857*8542734aSAndroid Build Coastguard Worker     fwmark.permission = PERMISSION_NONE;
858*8542734aSAndroid Build Coastguard Worker     mask.permission = PERMISSION_NONE;
859*8542734aSAndroid Build Coastguard Worker 
860*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
861*8542734aSAndroid Build Coastguard Worker                         explicitSelect ? (RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority)
862*8542734aSAndroid Build Coastguard Worker                                        : (RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority),
863*8542734aSAndroid Build Coastguard Worker                         table, fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart,
864*8542734aSAndroid Build Coastguard Worker                         uidEnd);
865*8542734aSAndroid Build Coastguard Worker }
866*8542734aSAndroid Build Coastguard Worker 
modifyUidDefaultNetworkRule(uint32_t table,uid_t uidStart,uid_t uidEnd,int32_t subPriority,bool add)867*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyUidDefaultNetworkRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
868*8542734aSAndroid Build Coastguard Worker                                                      int32_t subPriority, bool add) {
869*8542734aSAndroid Build Coastguard Worker     if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
870*8542734aSAndroid Build Coastguard Worker         ALOGE("modifyUidDefaultNetworkRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
871*8542734aSAndroid Build Coastguard Worker         return -EUSERS;
872*8542734aSAndroid Build Coastguard Worker     }
873*8542734aSAndroid Build Coastguard Worker 
874*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
875*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
876*8542734aSAndroid Build Coastguard Worker 
877*8542734aSAndroid Build Coastguard Worker     fwmark.netId = NETID_UNSET;
878*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
879*8542734aSAndroid Build Coastguard Worker 
880*8542734aSAndroid Build Coastguard Worker     // Access to this network is controlled by UID rules, not permission bits.
881*8542734aSAndroid Build Coastguard Worker     fwmark.permission = PERMISSION_NONE;
882*8542734aSAndroid Build Coastguard Worker     mask.permission = PERMISSION_NONE;
883*8542734aSAndroid Build Coastguard Worker 
884*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
885*8542734aSAndroid Build Coastguard Worker                         RULE_PRIORITY_UID_DEFAULT_NETWORK + subPriority, table, fwmark.intValue,
886*8542734aSAndroid Build Coastguard Worker                         mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
887*8542734aSAndroid Build Coastguard Worker }
888*8542734aSAndroid Build Coastguard Worker 
889*8542734aSAndroid Build Coastguard Worker /* static */
modifyPhysicalNetwork(unsigned netId,const char * interface,const UidRangeMap & uidRangeMap,Permission permission,bool add,bool modifyNonUidBasedRules,bool local)890*8542734aSAndroid Build Coastguard Worker int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
891*8542734aSAndroid Build Coastguard Worker                                            const UidRangeMap& uidRangeMap, Permission permission,
892*8542734aSAndroid Build Coastguard Worker                                            bool add, bool modifyNonUidBasedRules, bool local) {
893*8542734aSAndroid Build Coastguard Worker     uint32_t table = getRouteTableForInterface(interface, false /* local */);
894*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC) {
895*8542734aSAndroid Build Coastguard Worker         return -ESRCH;
896*8542734aSAndroid Build Coastguard Worker     }
897*8542734aSAndroid Build Coastguard Worker 
898*8542734aSAndroid Build Coastguard Worker     for (const auto& [subPriority, uidRanges] : uidRangeMap) {
899*8542734aSAndroid Build Coastguard Worker         for (const UidRangeParcel& range : uidRanges.getRanges()) {
900*8542734aSAndroid Build Coastguard Worker             if (int ret = modifyUidNetworkRule(netId, table, range.start, range.stop, subPriority,
901*8542734aSAndroid Build Coastguard Worker                                                add, EXPLICIT)) {
902*8542734aSAndroid Build Coastguard Worker                 return ret;
903*8542734aSAndroid Build Coastguard Worker             }
904*8542734aSAndroid Build Coastguard Worker             if (int ret = modifyUidNetworkRule(netId, table, range.start, range.stop, subPriority,
905*8542734aSAndroid Build Coastguard Worker                                                add, IMPLICIT)) {
906*8542734aSAndroid Build Coastguard Worker                 return ret;
907*8542734aSAndroid Build Coastguard Worker             }
908*8542734aSAndroid Build Coastguard Worker             // SUB_PRIORITY_NO_DEFAULT is "special" and does not require a
909*8542734aSAndroid Build Coastguard Worker             // default network rule, see UidRanges.h.
910*8542734aSAndroid Build Coastguard Worker             if (subPriority != UidRanges::SUB_PRIORITY_NO_DEFAULT) {
911*8542734aSAndroid Build Coastguard Worker                 if (int ret = modifyUidDefaultNetworkRule(table, range.start, range.stop,
912*8542734aSAndroid Build Coastguard Worker                                                           subPriority, add)) {
913*8542734aSAndroid Build Coastguard Worker                     return ret;
914*8542734aSAndroid Build Coastguard Worker                 }
915*8542734aSAndroid Build Coastguard Worker 
916*8542734aSAndroid Build Coastguard Worker                 // Per-UID local network rules must always match per-app default network rules,
917*8542734aSAndroid Build Coastguard Worker                 // because their purpose is to allow the UIDs to use the default network for
918*8542734aSAndroid Build Coastguard Worker                 // local destinations within it.
919*8542734aSAndroid Build Coastguard Worker                 if (int ret = modifyUidLocalNetworkRule(interface, range.start, range.stop, add)) {
920*8542734aSAndroid Build Coastguard Worker                     return ret;
921*8542734aSAndroid Build Coastguard Worker                 }
922*8542734aSAndroid Build Coastguard Worker             }
923*8542734aSAndroid Build Coastguard Worker         }
924*8542734aSAndroid Build Coastguard Worker     }
925*8542734aSAndroid Build Coastguard Worker 
926*8542734aSAndroid Build Coastguard Worker     if (!modifyNonUidBasedRules) {
927*8542734aSAndroid Build Coastguard Worker         // we are done.
928*8542734aSAndroid Build Coastguard Worker         return 0;
929*8542734aSAndroid Build Coastguard Worker     }
930*8542734aSAndroid Build Coastguard Worker 
931*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
932*8542734aSAndroid Build Coastguard Worker         return ret;
933*8542734aSAndroid Build Coastguard Worker     }
934*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
935*8542734aSAndroid Build Coastguard Worker                                             UidRanges::SUB_PRIORITY_HIGHEST, add)) {
936*8542734aSAndroid Build Coastguard Worker         return ret;
937*8542734aSAndroid Build Coastguard Worker     }
938*8542734aSAndroid Build Coastguard Worker     if (local) {
939*8542734aSAndroid Build Coastguard Worker         if (const int ret = modifyLocalNetworkRule(table, add)) {
940*8542734aSAndroid Build Coastguard Worker             return ret;
941*8542734aSAndroid Build Coastguard Worker         }
942*8542734aSAndroid Build Coastguard Worker     }
943*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
944*8542734aSAndroid Build Coastguard Worker                                              UidRanges::SUB_PRIORITY_HIGHEST, add)) {
945*8542734aSAndroid Build Coastguard Worker         return ret;
946*8542734aSAndroid Build Coastguard Worker     }
947*8542734aSAndroid Build Coastguard Worker 
948*8542734aSAndroid Build Coastguard Worker     // Only set implicit rules for networks that don't require permissions.
949*8542734aSAndroid Build Coastguard Worker     //
950*8542734aSAndroid Build Coastguard Worker     // This is so that if the default network ceases to be the default network and then switches
951*8542734aSAndroid Build Coastguard Worker     // from requiring no permissions to requiring permissions, we ensure that apps only use the
952*8542734aSAndroid Build Coastguard Worker     // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
953*8542734aSAndroid Build Coastguard Worker     // - it closes all sockets on the network except sockets that are explicitly selected.
954*8542734aSAndroid Build Coastguard Worker     //
955*8542734aSAndroid Build Coastguard Worker     // The lack of this rule only affects the special case above, because:
956*8542734aSAndroid Build Coastguard Worker     // - The only cases where we implicitly bind a socket to a network are the default network and
957*8542734aSAndroid Build Coastguard Worker     //   the bypassable VPN that applies to the app, if any.
958*8542734aSAndroid Build Coastguard Worker     // - This rule doesn't affect VPNs because they don't support permissions at all.
959*8542734aSAndroid Build Coastguard Worker     // - The default network doesn't require permissions. While we support doing this, the framework
960*8542734aSAndroid Build Coastguard Worker     //   never does it (partly because we'd end up in the situation where we tell apps that there is
961*8542734aSAndroid Build Coastguard Worker     //   a default network, but they can't use it).
962*8542734aSAndroid Build Coastguard Worker     // - If the network is still the default network, the presence or absence of this rule does not
963*8542734aSAndroid Build Coastguard Worker     //   matter.
964*8542734aSAndroid Build Coastguard Worker     //
965*8542734aSAndroid Build Coastguard Worker     // Therefore, for the lack of this rule to affect a socket, the socket has to have been
966*8542734aSAndroid Build Coastguard Worker     // implicitly bound to a network because at the time of connect() it was the default, and that
967*8542734aSAndroid Build Coastguard Worker     // network must no longer be the default, and must now require permissions.
968*8542734aSAndroid Build Coastguard Worker     if (permission == PERMISSION_NONE) {
969*8542734aSAndroid Build Coastguard Worker         return modifyImplicitNetworkRule(netId, table, add);
970*8542734aSAndroid Build Coastguard Worker     }
971*8542734aSAndroid Build Coastguard Worker     return 0;
972*8542734aSAndroid Build Coastguard Worker }
973*8542734aSAndroid Build Coastguard Worker 
modifyUidLocalNetworkRule(const char * interface,uid_t uidStart,uid_t uidEnd,bool add)974*8542734aSAndroid Build Coastguard Worker int RouteController::modifyUidLocalNetworkRule(const char* interface, uid_t uidStart, uid_t uidEnd,
975*8542734aSAndroid Build Coastguard Worker                                                bool add) {
976*8542734aSAndroid Build Coastguard Worker     uint32_t table = getRouteTableForInterface(interface, true /* local */);
977*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC) {
978*8542734aSAndroid Build Coastguard Worker         return -ESRCH;
979*8542734aSAndroid Build Coastguard Worker     }
980*8542734aSAndroid Build Coastguard Worker 
981*8542734aSAndroid Build Coastguard Worker     if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
982*8542734aSAndroid Build Coastguard Worker         ALOGE("modifyUidLocalNetworkRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
983*8542734aSAndroid Build Coastguard Worker         return -EUSERS;
984*8542734aSAndroid Build Coastguard Worker     }
985*8542734aSAndroid Build Coastguard Worker 
986*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
987*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
988*8542734aSAndroid Build Coastguard Worker 
989*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = false;
990*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
991*8542734aSAndroid Build Coastguard Worker 
992*8542734aSAndroid Build Coastguard Worker     // Access to this network is controlled by UID rules, not permission bits.
993*8542734aSAndroid Build Coastguard Worker     fwmark.permission = PERMISSION_NONE;
994*8542734aSAndroid Build Coastguard Worker     mask.permission = PERMISSION_NONE;
995*8542734aSAndroid Build Coastguard Worker 
996*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_UID_LOCAL_ROUTES, table,
997*8542734aSAndroid Build Coastguard Worker                         fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
998*8542734aSAndroid Build Coastguard Worker }
999*8542734aSAndroid Build Coastguard Worker 
modifyUidUnreachableRule(unsigned netId,uid_t uidStart,uid_t uidEnd,int32_t subPriority,bool add,bool explicitSelect)1000*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyUidUnreachableRule(unsigned netId, uid_t uidStart, uid_t uidEnd,
1001*8542734aSAndroid Build Coastguard Worker                                                   int32_t subPriority, bool add,
1002*8542734aSAndroid Build Coastguard Worker                                                   bool explicitSelect) {
1003*8542734aSAndroid Build Coastguard Worker     if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
1004*8542734aSAndroid Build Coastguard Worker         ALOGE("modifyUidUnreachableRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
1005*8542734aSAndroid Build Coastguard Worker         return -EUSERS;
1006*8542734aSAndroid Build Coastguard Worker     }
1007*8542734aSAndroid Build Coastguard Worker 
1008*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
1009*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
1010*8542734aSAndroid Build Coastguard Worker 
1011*8542734aSAndroid Build Coastguard Worker     fwmark.netId = netId;
1012*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
1013*8542734aSAndroid Build Coastguard Worker 
1014*8542734aSAndroid Build Coastguard Worker     fwmark.explicitlySelected = explicitSelect;
1015*8542734aSAndroid Build Coastguard Worker     mask.explicitlySelected = true;
1016*8542734aSAndroid Build Coastguard Worker 
1017*8542734aSAndroid Build Coastguard Worker     // Access to this network is controlled by UID rules, not permission bits.
1018*8542734aSAndroid Build Coastguard Worker     fwmark.permission = PERMISSION_NONE;
1019*8542734aSAndroid Build Coastguard Worker     mask.permission = PERMISSION_NONE;
1020*8542734aSAndroid Build Coastguard Worker 
1021*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
1022*8542734aSAndroid Build Coastguard Worker                         explicitSelect ? (RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority)
1023*8542734aSAndroid Build Coastguard Worker                                        : (RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority),
1024*8542734aSAndroid Build Coastguard Worker                         FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
1025*8542734aSAndroid Build Coastguard Worker                         IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
1026*8542734aSAndroid Build Coastguard Worker }
1027*8542734aSAndroid Build Coastguard Worker 
modifyUidDefaultUnreachableRule(uid_t uidStart,uid_t uidEnd,int32_t subPriority,bool add)1028*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyUidDefaultUnreachableRule(uid_t uidStart, uid_t uidEnd,
1029*8542734aSAndroid Build Coastguard Worker                                                          int32_t subPriority, bool add) {
1030*8542734aSAndroid Build Coastguard Worker     if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
1031*8542734aSAndroid Build Coastguard Worker         ALOGE("modifyUidDefaultUnreachableRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
1032*8542734aSAndroid Build Coastguard Worker         return -EUSERS;
1033*8542734aSAndroid Build Coastguard Worker     }
1034*8542734aSAndroid Build Coastguard Worker 
1035*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
1036*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
1037*8542734aSAndroid Build Coastguard Worker 
1038*8542734aSAndroid Build Coastguard Worker     fwmark.netId = NETID_UNSET;
1039*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
1040*8542734aSAndroid Build Coastguard Worker 
1041*8542734aSAndroid Build Coastguard Worker     // Access to this network is controlled by UID rules, not permission bits.
1042*8542734aSAndroid Build Coastguard Worker     fwmark.permission = PERMISSION_NONE;
1043*8542734aSAndroid Build Coastguard Worker     mask.permission = PERMISSION_NONE;
1044*8542734aSAndroid Build Coastguard Worker 
1045*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
1046*8542734aSAndroid Build Coastguard Worker                         RULE_PRIORITY_UID_DEFAULT_UNREACHABLE + subPriority, FR_ACT_UNREACHABLE,
1047*8542734aSAndroid Build Coastguard Worker                         RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
1048*8542734aSAndroid Build Coastguard Worker                         uidStart, uidEnd);
1049*8542734aSAndroid Build Coastguard Worker }
1050*8542734aSAndroid Build Coastguard Worker 
modifyUnreachableNetwork(unsigned netId,const UidRangeMap & uidRangeMap,bool add)1051*8542734aSAndroid Build Coastguard Worker int RouteController::modifyUnreachableNetwork(unsigned netId, const UidRangeMap& uidRangeMap,
1052*8542734aSAndroid Build Coastguard Worker                                               bool add) {
1053*8542734aSAndroid Build Coastguard Worker     for (const auto& [subPriority, uidRanges] : uidRangeMap) {
1054*8542734aSAndroid Build Coastguard Worker         for (const UidRangeParcel& range : uidRanges.getRanges()) {
1055*8542734aSAndroid Build Coastguard Worker             if (int ret = modifyUidUnreachableRule(netId, range.start, range.stop, subPriority, add,
1056*8542734aSAndroid Build Coastguard Worker                                                    EXPLICIT)) {
1057*8542734aSAndroid Build Coastguard Worker                 return ret;
1058*8542734aSAndroid Build Coastguard Worker             }
1059*8542734aSAndroid Build Coastguard Worker             if (int ret = modifyUidUnreachableRule(netId, range.start, range.stop, subPriority, add,
1060*8542734aSAndroid Build Coastguard Worker                                                    IMPLICIT)) {
1061*8542734aSAndroid Build Coastguard Worker                 return ret;
1062*8542734aSAndroid Build Coastguard Worker             }
1063*8542734aSAndroid Build Coastguard Worker             if (int ret = modifyUidDefaultUnreachableRule(range.start, range.stop, subPriority,
1064*8542734aSAndroid Build Coastguard Worker                                                           add)) {
1065*8542734aSAndroid Build Coastguard Worker                 return ret;
1066*8542734aSAndroid Build Coastguard Worker             }
1067*8542734aSAndroid Build Coastguard Worker         }
1068*8542734aSAndroid Build Coastguard Worker     }
1069*8542734aSAndroid Build Coastguard Worker 
1070*8542734aSAndroid Build Coastguard Worker     return 0;
1071*8542734aSAndroid Build Coastguard Worker }
1072*8542734aSAndroid Build Coastguard Worker 
modifyRejectNonSecureNetworkRule(const UidRanges & uidRanges,bool add)1073*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
1074*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
1075*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
1076*8542734aSAndroid Build Coastguard Worker     fwmark.protectedFromVpn = false;
1077*8542734aSAndroid Build Coastguard Worker     mask.protectedFromVpn = true;
1078*8542734aSAndroid Build Coastguard Worker 
1079*8542734aSAndroid Build Coastguard Worker     for (const UidRangeParcel& range : uidRanges.getRanges()) {
1080*8542734aSAndroid Build Coastguard Worker         if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
1081*8542734aSAndroid Build Coastguard Worker                                    FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
1082*8542734aSAndroid Build Coastguard Worker                                    IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
1083*8542734aSAndroid Build Coastguard Worker             return ret;
1084*8542734aSAndroid Build Coastguard Worker         }
1085*8542734aSAndroid Build Coastguard Worker     }
1086*8542734aSAndroid Build Coastguard Worker 
1087*8542734aSAndroid Build Coastguard Worker     return 0;
1088*8542734aSAndroid Build Coastguard Worker }
1089*8542734aSAndroid Build Coastguard Worker 
modifyVirtualNetwork(unsigned netId,const char * interface,const UidRangeMap & uidRangeMap,bool secure,bool add,bool modifyNonUidBasedRules,bool excludeLocalRoutes)1090*8542734aSAndroid Build Coastguard Worker int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
1091*8542734aSAndroid Build Coastguard Worker                                           const UidRangeMap& uidRangeMap, bool secure, bool add,
1092*8542734aSAndroid Build Coastguard Worker                                           bool modifyNonUidBasedRules, bool excludeLocalRoutes) {
1093*8542734aSAndroid Build Coastguard Worker     uint32_t table = getRouteTableForInterface(interface, false /* false */);
1094*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC) {
1095*8542734aSAndroid Build Coastguard Worker         return -ESRCH;
1096*8542734aSAndroid Build Coastguard Worker     }
1097*8542734aSAndroid Build Coastguard Worker 
1098*8542734aSAndroid Build Coastguard Worker     for (const auto& [subPriority, uidRanges] : uidRangeMap) {
1099*8542734aSAndroid Build Coastguard Worker         for (const UidRangeParcel& range : uidRanges.getRanges()) {
1100*8542734aSAndroid Build Coastguard Worker             if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, subPriority, secure,
1101*8542734aSAndroid Build Coastguard Worker                                                 add, excludeLocalRoutes)) {
1102*8542734aSAndroid Build Coastguard Worker                 return ret;
1103*8542734aSAndroid Build Coastguard Worker             }
1104*8542734aSAndroid Build Coastguard Worker             if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
1105*8542734aSAndroid Build Coastguard Worker                                                     range.stop, subPriority, add)) {
1106*8542734aSAndroid Build Coastguard Worker                 return ret;
1107*8542734aSAndroid Build Coastguard Worker             }
1108*8542734aSAndroid Build Coastguard Worker             if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
1109*8542734aSAndroid Build Coastguard Worker                                                      range.stop, subPriority, add)) {
1110*8542734aSAndroid Build Coastguard Worker                 return ret;
1111*8542734aSAndroid Build Coastguard Worker             }
1112*8542734aSAndroid Build Coastguard Worker         }
1113*8542734aSAndroid Build Coastguard Worker     }
1114*8542734aSAndroid Build Coastguard Worker 
1115*8542734aSAndroid Build Coastguard Worker     if (modifyNonUidBasedRules) {
1116*8542734aSAndroid Build Coastguard Worker         if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
1117*8542734aSAndroid Build Coastguard Worker             return ret;
1118*8542734aSAndroid Build Coastguard Worker         }
1119*8542734aSAndroid Build Coastguard Worker         if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
1120*8542734aSAndroid Build Coastguard Worker             return ret;
1121*8542734aSAndroid Build Coastguard Worker         }
1122*8542734aSAndroid Build Coastguard Worker         if (int ret =
1123*8542734aSAndroid Build Coastguard Worker                     modifyVpnSystemPermissionRule(netId, table, secure, add, excludeLocalRoutes)) {
1124*8542734aSAndroid Build Coastguard Worker             return ret;
1125*8542734aSAndroid Build Coastguard Worker         }
1126*8542734aSAndroid Build Coastguard Worker         return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT,
1127*8542734aSAndroid Build Coastguard Worker                                          UidRanges::SUB_PRIORITY_HIGHEST, add);
1128*8542734aSAndroid Build Coastguard Worker     }
1129*8542734aSAndroid Build Coastguard Worker 
1130*8542734aSAndroid Build Coastguard Worker     return 0;
1131*8542734aSAndroid Build Coastguard Worker }
1132*8542734aSAndroid Build Coastguard Worker 
modifyDefaultNetwork(uint16_t action,const char * interface,Permission permission)1133*8542734aSAndroid Build Coastguard Worker int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
1134*8542734aSAndroid Build Coastguard Worker                                           Permission permission) {
1135*8542734aSAndroid Build Coastguard Worker     uint32_t table = getRouteTableForInterface(interface, false /* local */);
1136*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC) {
1137*8542734aSAndroid Build Coastguard Worker         return -ESRCH;
1138*8542734aSAndroid Build Coastguard Worker     }
1139*8542734aSAndroid Build Coastguard Worker 
1140*8542734aSAndroid Build Coastguard Worker     Fwmark fwmark;
1141*8542734aSAndroid Build Coastguard Worker     Fwmark mask;
1142*8542734aSAndroid Build Coastguard Worker 
1143*8542734aSAndroid Build Coastguard Worker     fwmark.netId = NETID_UNSET;
1144*8542734aSAndroid Build Coastguard Worker     mask.netId = FWMARK_NET_ID_MASK;
1145*8542734aSAndroid Build Coastguard Worker 
1146*8542734aSAndroid Build Coastguard Worker     fwmark.permission = permission;
1147*8542734aSAndroid Build Coastguard Worker     mask.permission = permission;
1148*8542734aSAndroid Build Coastguard Worker 
1149*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
1150*8542734aSAndroid Build Coastguard Worker                         mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
1151*8542734aSAndroid Build Coastguard Worker }
1152*8542734aSAndroid Build Coastguard Worker 
modifyTetheredNetwork(uint16_t action,const char * inputInterface,const char * outputInterface)1153*8542734aSAndroid Build Coastguard Worker int RouteController::modifyTetheredNetwork(uint16_t action, const char* inputInterface,
1154*8542734aSAndroid Build Coastguard Worker                                            const char* outputInterface) {
1155*8542734aSAndroid Build Coastguard Worker     uint32_t table = getRouteTableForInterface(outputInterface, false /* local */);
1156*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC) {
1157*8542734aSAndroid Build Coastguard Worker         return -ESRCH;
1158*8542734aSAndroid Build Coastguard Worker     }
1159*8542734aSAndroid Build Coastguard Worker 
1160*8542734aSAndroid Build Coastguard Worker     return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
1161*8542734aSAndroid Build Coastguard Worker                         inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
1162*8542734aSAndroid Build Coastguard Worker }
1163*8542734aSAndroid Build Coastguard Worker 
1164*8542734aSAndroid Build Coastguard Worker // Adds or removes an IPv4 or IPv6 route to the specified table.
1165*8542734aSAndroid Build Coastguard Worker // Returns 0 on success or negative errno on failure.
modifyRoute(uint16_t action,uint16_t flags,const char * interface,const char * destination,const char * nexthop,TableType tableType,int mtu,int priority,bool isLocal)1166*8542734aSAndroid Build Coastguard Worker int RouteController::modifyRoute(uint16_t action, uint16_t flags, const char* interface,
1167*8542734aSAndroid Build Coastguard Worker                                  const char* destination, const char* nexthop, TableType tableType,
1168*8542734aSAndroid Build Coastguard Worker                                  int mtu, int priority, bool isLocal) {
1169*8542734aSAndroid Build Coastguard Worker     uint32_t table;
1170*8542734aSAndroid Build Coastguard Worker     switch (tableType) {
1171*8542734aSAndroid Build Coastguard Worker         case RouteController::INTERFACE: {
1172*8542734aSAndroid Build Coastguard Worker             table = getRouteTableForInterface(interface, isLocal);
1173*8542734aSAndroid Build Coastguard Worker             if (table == RT_TABLE_UNSPEC) {
1174*8542734aSAndroid Build Coastguard Worker                 return -ESRCH;
1175*8542734aSAndroid Build Coastguard Worker             }
1176*8542734aSAndroid Build Coastguard Worker             break;
1177*8542734aSAndroid Build Coastguard Worker         }
1178*8542734aSAndroid Build Coastguard Worker         case RouteController::LOCAL_NETWORK: {
1179*8542734aSAndroid Build Coastguard Worker             table = ROUTE_TABLE_LOCAL_NETWORK;
1180*8542734aSAndroid Build Coastguard Worker             break;
1181*8542734aSAndroid Build Coastguard Worker         }
1182*8542734aSAndroid Build Coastguard Worker         case RouteController::LEGACY_NETWORK: {
1183*8542734aSAndroid Build Coastguard Worker             table = ROUTE_TABLE_LEGACY_NETWORK;
1184*8542734aSAndroid Build Coastguard Worker             break;
1185*8542734aSAndroid Build Coastguard Worker         }
1186*8542734aSAndroid Build Coastguard Worker         case RouteController::LEGACY_SYSTEM: {
1187*8542734aSAndroid Build Coastguard Worker             table = ROUTE_TABLE_LEGACY_SYSTEM;
1188*8542734aSAndroid Build Coastguard Worker             break;
1189*8542734aSAndroid Build Coastguard Worker         }
1190*8542734aSAndroid Build Coastguard Worker     }
1191*8542734aSAndroid Build Coastguard Worker 
1192*8542734aSAndroid Build Coastguard Worker     int ret = modifyIpRoute(action, flags, table, interface, destination, nexthop, mtu, priority);
1193*8542734aSAndroid Build Coastguard Worker     // Trying to add a route that already exists shouldn't cause an error.
1194*8542734aSAndroid Build Coastguard Worker     if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
1195*8542734aSAndroid Build Coastguard Worker         return ret;
1196*8542734aSAndroid Build Coastguard Worker     }
1197*8542734aSAndroid Build Coastguard Worker 
1198*8542734aSAndroid Build Coastguard Worker     return 0;
1199*8542734aSAndroid Build Coastguard Worker }
1200*8542734aSAndroid Build Coastguard Worker 
maybeModifyQdiscClsact(const char * interface,bool add)1201*8542734aSAndroid Build Coastguard Worker static void maybeModifyQdiscClsact(const char* interface, bool add) {
1202*8542734aSAndroid Build Coastguard Worker     // The clsact attaching of v4- tun interface is triggered by ClatdController::maybeStartBpf
1203*8542734aSAndroid Build Coastguard Worker     // because the clat is started before the v4- interface is added to the network and the
1204*8542734aSAndroid Build Coastguard Worker     // clat startup needs to add {in, e}gress filters.
1205*8542734aSAndroid Build Coastguard Worker     // TODO: remove this workaround once v4- tun interface clsact attaching is moved out from
1206*8542734aSAndroid Build Coastguard Worker     // ClatdController::maybeStartBpf.
1207*8542734aSAndroid Build Coastguard Worker     if (StartsWith(interface, "v4-") && add) return;
1208*8542734aSAndroid Build Coastguard Worker 
1209*8542734aSAndroid Build Coastguard Worker     // The interface may have already gone away in the delete case.
1210*8542734aSAndroid Build Coastguard Worker     uint32_t ifindex = RouteController::ifNameToIndexFunction(interface);
1211*8542734aSAndroid Build Coastguard Worker     if (!ifindex) {
1212*8542734aSAndroid Build Coastguard Worker         ALOGE("cannot find interface %s", interface);
1213*8542734aSAndroid Build Coastguard Worker         return;
1214*8542734aSAndroid Build Coastguard Worker     }
1215*8542734aSAndroid Build Coastguard Worker 
1216*8542734aSAndroid Build Coastguard Worker     if (add) {
1217*8542734aSAndroid Build Coastguard Worker         if (int ret = tcQdiscAddDevClsact(ifindex)) {
1218*8542734aSAndroid Build Coastguard Worker             ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
1219*8542734aSAndroid Build Coastguard Worker             return;
1220*8542734aSAndroid Build Coastguard Worker         }
1221*8542734aSAndroid Build Coastguard Worker     } else {
1222*8542734aSAndroid Build Coastguard Worker         if (int ret = tcQdiscDelDevClsact(ifindex)) {
1223*8542734aSAndroid Build Coastguard Worker             ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
1224*8542734aSAndroid Build Coastguard Worker             return;
1225*8542734aSAndroid Build Coastguard Worker         }
1226*8542734aSAndroid Build Coastguard Worker     }
1227*8542734aSAndroid Build Coastguard Worker 
1228*8542734aSAndroid Build Coastguard Worker     return;
1229*8542734aSAndroid Build Coastguard Worker }
1230*8542734aSAndroid Build Coastguard Worker 
clearTetheringRules(const char * inputInterface)1231*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int clearTetheringRules(const char* inputInterface) {
1232*8542734aSAndroid Build Coastguard Worker     int ret = 0;
1233*8542734aSAndroid Build Coastguard Worker     while (ret == 0) {
1234*8542734aSAndroid Build Coastguard Worker         ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
1235*8542734aSAndroid Build Coastguard Worker                            inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
1236*8542734aSAndroid Build Coastguard Worker     }
1237*8542734aSAndroid Build Coastguard Worker 
1238*8542734aSAndroid Build Coastguard Worker     if (ret == -ENOENT) {
1239*8542734aSAndroid Build Coastguard Worker         return 0;
1240*8542734aSAndroid Build Coastguard Worker     } else {
1241*8542734aSAndroid Build Coastguard Worker         return ret;
1242*8542734aSAndroid Build Coastguard Worker     }
1243*8542734aSAndroid Build Coastguard Worker }
1244*8542734aSAndroid Build Coastguard Worker 
getRulePriority(const nlmsghdr * nlh)1245*8542734aSAndroid Build Coastguard Worker uint32_t getRulePriority(const nlmsghdr *nlh) {
1246*8542734aSAndroid Build Coastguard Worker     return getRtmU32Attribute(nlh, FRA_PRIORITY);
1247*8542734aSAndroid Build Coastguard Worker }
1248*8542734aSAndroid Build Coastguard Worker 
getRouteTable(const nlmsghdr * nlh)1249*8542734aSAndroid Build Coastguard Worker uint32_t getRouteTable(const nlmsghdr *nlh) {
1250*8542734aSAndroid Build Coastguard Worker     return getRtmU32Attribute(nlh, RTA_TABLE);
1251*8542734aSAndroid Build Coastguard Worker }
1252*8542734aSAndroid Build Coastguard Worker 
flushRules()1253*8542734aSAndroid Build Coastguard Worker [[nodiscard]] static int flushRules() {
1254*8542734aSAndroid Build Coastguard Worker     NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
1255*8542734aSAndroid Build Coastguard Worker         // Don't touch rules at priority 0 because by default they are used for local input.
1256*8542734aSAndroid Build Coastguard Worker         return getRulePriority(nlh) != 0;
1257*8542734aSAndroid Build Coastguard Worker     };
1258*8542734aSAndroid Build Coastguard Worker     return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
1259*8542734aSAndroid Build Coastguard Worker }
1260*8542734aSAndroid Build Coastguard Worker 
flushRoutes(uint32_t table)1261*8542734aSAndroid Build Coastguard Worker int RouteController::flushRoutes(uint32_t table) {
1262*8542734aSAndroid Build Coastguard Worker     NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
1263*8542734aSAndroid Build Coastguard Worker         return getRouteTable(nlh) == table;
1264*8542734aSAndroid Build Coastguard Worker     };
1265*8542734aSAndroid Build Coastguard Worker 
1266*8542734aSAndroid Build Coastguard Worker     return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
1267*8542734aSAndroid Build Coastguard Worker }
1268*8542734aSAndroid Build Coastguard Worker 
flushRoutes(const char * interface)1269*8542734aSAndroid Build Coastguard Worker int RouteController::flushRoutes(const char* interface) {
1270*8542734aSAndroid Build Coastguard Worker     // Try to flush both local and global routing tables.
1271*8542734aSAndroid Build Coastguard Worker     //
1272*8542734aSAndroid Build Coastguard Worker     // Flush local first because flush global routing tables may erase the sInterfaceToTable map.
1273*8542734aSAndroid Build Coastguard Worker     // Then the fake <iface>_local interface will be unable to find the index because the local
1274*8542734aSAndroid Build Coastguard Worker     // interface depends physical interface to find the correct index.
1275*8542734aSAndroid Build Coastguard Worker     int ret = flushRoutes(interface, true);
1276*8542734aSAndroid Build Coastguard Worker     ret |= flushRoutes(interface, false);
1277*8542734aSAndroid Build Coastguard Worker     return ret;
1278*8542734aSAndroid Build Coastguard Worker }
1279*8542734aSAndroid Build Coastguard Worker 
1280*8542734aSAndroid Build Coastguard Worker // Returns 0 on success or negative errno on failure.
flushRoutes(const char * interface,bool local)1281*8542734aSAndroid Build Coastguard Worker int RouteController::flushRoutes(const char* interface, bool local) {
1282*8542734aSAndroid Build Coastguard Worker     std::lock_guard lock(sInterfaceToTableLock);
1283*8542734aSAndroid Build Coastguard Worker 
1284*8542734aSAndroid Build Coastguard Worker     uint32_t table = getRouteTableForInterfaceLocked(interface, local);
1285*8542734aSAndroid Build Coastguard Worker     if (table == RT_TABLE_UNSPEC) {
1286*8542734aSAndroid Build Coastguard Worker         return -ESRCH;
1287*8542734aSAndroid Build Coastguard Worker     }
1288*8542734aSAndroid Build Coastguard Worker 
1289*8542734aSAndroid Build Coastguard Worker     int ret = flushRoutes(table);
1290*8542734aSAndroid Build Coastguard Worker 
1291*8542734aSAndroid Build Coastguard Worker     // If we failed to flush routes, the caller may elect to keep this interface around, so keep
1292*8542734aSAndroid Build Coastguard Worker     // track of its name.
1293*8542734aSAndroid Build Coastguard Worker     // Skip erasing local fake interface since it does not exist in sInterfaceToTable.
1294*8542734aSAndroid Build Coastguard Worker     if (ret == 0 && !local) {
1295*8542734aSAndroid Build Coastguard Worker         sInterfaceToTable.erase(interface);
1296*8542734aSAndroid Build Coastguard Worker     }
1297*8542734aSAndroid Build Coastguard Worker 
1298*8542734aSAndroid Build Coastguard Worker     return ret;
1299*8542734aSAndroid Build Coastguard Worker }
1300*8542734aSAndroid Build Coastguard Worker 
Init(unsigned localNetId)1301*8542734aSAndroid Build Coastguard Worker int RouteController::Init(unsigned localNetId) {
1302*8542734aSAndroid Build Coastguard Worker     if (int ret = flushRules()) {
1303*8542734aSAndroid Build Coastguard Worker         return ret;
1304*8542734aSAndroid Build Coastguard Worker     }
1305*8542734aSAndroid Build Coastguard Worker     if (int ret = addLegacyRouteRules()) {
1306*8542734aSAndroid Build Coastguard Worker         return ret;
1307*8542734aSAndroid Build Coastguard Worker     }
1308*8542734aSAndroid Build Coastguard Worker     if (int ret = addLocalNetworkRules(localNetId)) {
1309*8542734aSAndroid Build Coastguard Worker         return ret;
1310*8542734aSAndroid Build Coastguard Worker     }
1311*8542734aSAndroid Build Coastguard Worker     if (int ret = addUnreachableRule()) {
1312*8542734aSAndroid Build Coastguard Worker         return ret;
1313*8542734aSAndroid Build Coastguard Worker     }
1314*8542734aSAndroid Build Coastguard Worker     // Don't complain if we can't add the dummy network, since not all devices support it.
1315*8542734aSAndroid Build Coastguard Worker     configureDummyNetwork();
1316*8542734aSAndroid Build Coastguard Worker 
1317*8542734aSAndroid Build Coastguard Worker     updateTableNamesFile();
1318*8542734aSAndroid Build Coastguard Worker     return 0;
1319*8542734aSAndroid Build Coastguard Worker }
1320*8542734aSAndroid Build Coastguard Worker 
addInterfaceToLocalNetwork(unsigned netId,const char * interface)1321*8542734aSAndroid Build Coastguard Worker int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
1322*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyLocalNetwork(netId, interface, ACTION_ADD)) {
1323*8542734aSAndroid Build Coastguard Worker         return ret;
1324*8542734aSAndroid Build Coastguard Worker     }
1325*8542734aSAndroid Build Coastguard Worker     std::lock_guard lock(sInterfaceToTableLock);
1326*8542734aSAndroid Build Coastguard Worker     sInterfaceToTable[interface] = ROUTE_TABLE_LOCAL_NETWORK;
1327*8542734aSAndroid Build Coastguard Worker     return 0;
1328*8542734aSAndroid Build Coastguard Worker }
1329*8542734aSAndroid Build Coastguard Worker 
removeInterfaceFromLocalNetwork(unsigned netId,const char * interface)1330*8542734aSAndroid Build Coastguard Worker int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
1331*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyLocalNetwork(netId, interface, ACTION_DEL)) {
1332*8542734aSAndroid Build Coastguard Worker         return ret;
1333*8542734aSAndroid Build Coastguard Worker     }
1334*8542734aSAndroid Build Coastguard Worker     std::lock_guard lock(sInterfaceToTableLock);
1335*8542734aSAndroid Build Coastguard Worker     sInterfaceToTable.erase(interface);
1336*8542734aSAndroid Build Coastguard Worker     return 0;
1337*8542734aSAndroid Build Coastguard Worker }
1338*8542734aSAndroid Build Coastguard Worker 
addInterfaceToPhysicalNetwork(unsigned netId,const char * interface,Permission permission,const UidRangeMap & uidRangeMap,bool local)1339*8542734aSAndroid Build Coastguard Worker int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
1340*8542734aSAndroid Build Coastguard Worker                                                    Permission permission,
1341*8542734aSAndroid Build Coastguard Worker                                                    const UidRangeMap& uidRangeMap, bool local) {
1342*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyPhysicalNetwork(netId, interface, uidRangeMap, permission, ACTION_ADD,
1343*8542734aSAndroid Build Coastguard Worker                                         MODIFY_NON_UID_BASED_RULES, local)) {
1344*8542734aSAndroid Build Coastguard Worker         return ret;
1345*8542734aSAndroid Build Coastguard Worker     }
1346*8542734aSAndroid Build Coastguard Worker 
1347*8542734aSAndroid Build Coastguard Worker     maybeModifyQdiscClsact(interface, ACTION_ADD);
1348*8542734aSAndroid Build Coastguard Worker     updateTableNamesFile();
1349*8542734aSAndroid Build Coastguard Worker 
1350*8542734aSAndroid Build Coastguard Worker     if (int ret = addFixedLocalRoutes(interface)) {
1351*8542734aSAndroid Build Coastguard Worker         return ret;
1352*8542734aSAndroid Build Coastguard Worker     }
1353*8542734aSAndroid Build Coastguard Worker 
1354*8542734aSAndroid Build Coastguard Worker     return 0;
1355*8542734aSAndroid Build Coastguard Worker }
1356*8542734aSAndroid Build Coastguard Worker 
removeInterfaceFromPhysicalNetwork(unsigned netId,const char * interface,Permission permission,const UidRangeMap & uidRangeMap,bool local)1357*8542734aSAndroid Build Coastguard Worker int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1358*8542734aSAndroid Build Coastguard Worker                                                         Permission permission,
1359*8542734aSAndroid Build Coastguard Worker                                                         const UidRangeMap& uidRangeMap,
1360*8542734aSAndroid Build Coastguard Worker                                                         bool local) {
1361*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyPhysicalNetwork(netId, interface, uidRangeMap, permission, ACTION_DEL,
1362*8542734aSAndroid Build Coastguard Worker                                         MODIFY_NON_UID_BASED_RULES, local)) {
1363*8542734aSAndroid Build Coastguard Worker         return ret;
1364*8542734aSAndroid Build Coastguard Worker     }
1365*8542734aSAndroid Build Coastguard Worker 
1366*8542734aSAndroid Build Coastguard Worker     if (int ret = flushRoutes(interface)) {
1367*8542734aSAndroid Build Coastguard Worker         return ret;
1368*8542734aSAndroid Build Coastguard Worker     }
1369*8542734aSAndroid Build Coastguard Worker 
1370*8542734aSAndroid Build Coastguard Worker     if (int ret = clearTetheringRules(interface)) {
1371*8542734aSAndroid Build Coastguard Worker         return ret;
1372*8542734aSAndroid Build Coastguard Worker     }
1373*8542734aSAndroid Build Coastguard Worker 
1374*8542734aSAndroid Build Coastguard Worker     maybeModifyQdiscClsact(interface, ACTION_DEL);
1375*8542734aSAndroid Build Coastguard Worker     updateTableNamesFile();
1376*8542734aSAndroid Build Coastguard Worker     return 0;
1377*8542734aSAndroid Build Coastguard Worker }
1378*8542734aSAndroid Build Coastguard Worker 
addInterfaceToVirtualNetwork(unsigned netId,const char * interface,bool secure,const UidRangeMap & uidRangeMap,bool excludeLocalRoutes)1379*8542734aSAndroid Build Coastguard Worker int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
1380*8542734aSAndroid Build Coastguard Worker                                                   bool secure, const UidRangeMap& uidRangeMap,
1381*8542734aSAndroid Build Coastguard Worker                                                   bool excludeLocalRoutes) {
1382*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_ADD,
1383*8542734aSAndroid Build Coastguard Worker                                        MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes)) {
1384*8542734aSAndroid Build Coastguard Worker         return ret;
1385*8542734aSAndroid Build Coastguard Worker     }
1386*8542734aSAndroid Build Coastguard Worker     updateTableNamesFile();
1387*8542734aSAndroid Build Coastguard Worker     return 0;
1388*8542734aSAndroid Build Coastguard Worker }
1389*8542734aSAndroid Build Coastguard Worker 
removeInterfaceFromVirtualNetwork(unsigned netId,const char * interface,bool secure,const UidRangeMap & uidRangeMap,bool excludeLocalRoutes)1390*8542734aSAndroid Build Coastguard Worker int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
1391*8542734aSAndroid Build Coastguard Worker                                                        bool secure, const UidRangeMap& uidRangeMap,
1392*8542734aSAndroid Build Coastguard Worker                                                        bool excludeLocalRoutes) {
1393*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_DEL,
1394*8542734aSAndroid Build Coastguard Worker                                        MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes)) {
1395*8542734aSAndroid Build Coastguard Worker         return ret;
1396*8542734aSAndroid Build Coastguard Worker     }
1397*8542734aSAndroid Build Coastguard Worker     if (int ret = flushRoutes(interface)) {
1398*8542734aSAndroid Build Coastguard Worker         return ret;
1399*8542734aSAndroid Build Coastguard Worker     }
1400*8542734aSAndroid Build Coastguard Worker     updateTableNamesFile();
1401*8542734aSAndroid Build Coastguard Worker     return 0;
1402*8542734aSAndroid Build Coastguard Worker }
1403*8542734aSAndroid Build Coastguard Worker 
modifyPhysicalNetworkPermission(unsigned netId,const char * interface,Permission oldPermission,Permission newPermission,bool local)1404*8542734aSAndroid Build Coastguard Worker int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1405*8542734aSAndroid Build Coastguard Worker                                                      Permission oldPermission,
1406*8542734aSAndroid Build Coastguard Worker                                                      Permission newPermission, bool local) {
1407*8542734aSAndroid Build Coastguard Worker     // Physical network rules either use permission bits or UIDs, but not both.
1408*8542734aSAndroid Build Coastguard Worker     // So permission changes don't affect any UID-based rules.
1409*8542734aSAndroid Build Coastguard Worker     UidRangeMap emptyUidRangeMap;
1410*8542734aSAndroid Build Coastguard Worker     // Add the new rules before deleting the old ones, to avoid race conditions.
1411*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyPhysicalNetwork(netId, interface, emptyUidRangeMap, newPermission,
1412*8542734aSAndroid Build Coastguard Worker                                         ACTION_ADD, MODIFY_NON_UID_BASED_RULES, local)) {
1413*8542734aSAndroid Build Coastguard Worker         return ret;
1414*8542734aSAndroid Build Coastguard Worker     }
1415*8542734aSAndroid Build Coastguard Worker     return modifyPhysicalNetwork(netId, interface, emptyUidRangeMap, oldPermission, ACTION_DEL,
1416*8542734aSAndroid Build Coastguard Worker                                  MODIFY_NON_UID_BASED_RULES, local);
1417*8542734aSAndroid Build Coastguard Worker }
1418*8542734aSAndroid Build Coastguard Worker 
addUsersToRejectNonSecureNetworkRule(const UidRanges & uidRanges)1419*8542734aSAndroid Build Coastguard Worker int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1420*8542734aSAndroid Build Coastguard Worker     return modifyRejectNonSecureNetworkRule(uidRanges, true);
1421*8542734aSAndroid Build Coastguard Worker }
1422*8542734aSAndroid Build Coastguard Worker 
removeUsersFromRejectNonSecureNetworkRule(const UidRanges & uidRanges)1423*8542734aSAndroid Build Coastguard Worker int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1424*8542734aSAndroid Build Coastguard Worker     return modifyRejectNonSecureNetworkRule(uidRanges, false);
1425*8542734aSAndroid Build Coastguard Worker }
1426*8542734aSAndroid Build Coastguard Worker 
addUsersToVirtualNetwork(unsigned netId,const char * interface,bool secure,const UidRangeMap & uidRangeMap,bool excludeLocalRoutes)1427*8542734aSAndroid Build Coastguard Worker int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
1428*8542734aSAndroid Build Coastguard Worker                                               const UidRangeMap& uidRangeMap,
1429*8542734aSAndroid Build Coastguard Worker                                               bool excludeLocalRoutes) {
1430*8542734aSAndroid Build Coastguard Worker     return modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_ADD,
1431*8542734aSAndroid Build Coastguard Worker                                 !MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes);
1432*8542734aSAndroid Build Coastguard Worker }
1433*8542734aSAndroid Build Coastguard Worker 
removeUsersFromVirtualNetwork(unsigned netId,const char * interface,bool secure,const UidRangeMap & uidRangeMap,bool excludeLocalRoutes)1434*8542734aSAndroid Build Coastguard Worker int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
1435*8542734aSAndroid Build Coastguard Worker                                                    bool secure, const UidRangeMap& uidRangeMap,
1436*8542734aSAndroid Build Coastguard Worker                                                    bool excludeLocalRoutes) {
1437*8542734aSAndroid Build Coastguard Worker     return modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_DEL,
1438*8542734aSAndroid Build Coastguard Worker                                 !MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes);
1439*8542734aSAndroid Build Coastguard Worker }
1440*8542734aSAndroid Build Coastguard Worker 
addInterfaceToDefaultNetwork(const char * interface,Permission permission)1441*8542734aSAndroid Build Coastguard Worker int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1442*8542734aSAndroid Build Coastguard Worker     return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
1443*8542734aSAndroid Build Coastguard Worker }
1444*8542734aSAndroid Build Coastguard Worker 
removeInterfaceFromDefaultNetwork(const char * interface,Permission permission)1445*8542734aSAndroid Build Coastguard Worker int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1446*8542734aSAndroid Build Coastguard Worker                                                        Permission permission) {
1447*8542734aSAndroid Build Coastguard Worker     return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
1448*8542734aSAndroid Build Coastguard Worker }
1449*8542734aSAndroid Build Coastguard Worker 
isWithinIpv4LocalPrefix(const char * dst)1450*8542734aSAndroid Build Coastguard Worker bool RouteController::isWithinIpv4LocalPrefix(const char* dst) {
1451*8542734aSAndroid Build Coastguard Worker     for (IPPrefix addr : V4_LOCAL_PREFIXES) {
1452*8542734aSAndroid Build Coastguard Worker         if (addr.contains(IPPrefix::forString(dst))) {
1453*8542734aSAndroid Build Coastguard Worker             return true;
1454*8542734aSAndroid Build Coastguard Worker         }
1455*8542734aSAndroid Build Coastguard Worker     }
1456*8542734aSAndroid Build Coastguard Worker     return false;
1457*8542734aSAndroid Build Coastguard Worker }
1458*8542734aSAndroid Build Coastguard Worker 
isLocalRoute(TableType tableType,const char * destination,const char * nexthop)1459*8542734aSAndroid Build Coastguard Worker bool RouteController::isLocalRoute(TableType tableType, const char* destination,
1460*8542734aSAndroid Build Coastguard Worker                                    const char* nexthop) {
1461*8542734aSAndroid Build Coastguard Worker     IPPrefix prefix = IPPrefix::forString(destination);
1462*8542734aSAndroid Build Coastguard Worker     return nexthop == nullptr && tableType == RouteController::INTERFACE &&
1463*8542734aSAndroid Build Coastguard Worker            // Skip default route to prevent network being modeled as point-to-point interfaces.
1464*8542734aSAndroid Build Coastguard Worker            ((prefix.family() == AF_INET6 && prefix != IPPrefix::forString("::/0")) ||
1465*8542734aSAndroid Build Coastguard Worker             // Skip adding non-target local network range.
1466*8542734aSAndroid Build Coastguard Worker             (prefix.family() == AF_INET && isWithinIpv4LocalPrefix(destination)));
1467*8542734aSAndroid Build Coastguard Worker }
1468*8542734aSAndroid Build Coastguard Worker 
addRoute(const char * interface,const char * destination,const char * nexthop,TableType tableType,int mtu,int priority)1469*8542734aSAndroid Build Coastguard Worker int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
1470*8542734aSAndroid Build Coastguard Worker                               TableType tableType, int mtu, int priority) {
1471*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface, destination,
1472*8542734aSAndroid Build Coastguard Worker                               nexthop, tableType, mtu, priority, false /* isLocal */)) {
1473*8542734aSAndroid Build Coastguard Worker         return ret;
1474*8542734aSAndroid Build Coastguard Worker     }
1475*8542734aSAndroid Build Coastguard Worker 
1476*8542734aSAndroid Build Coastguard Worker     if (isLocalRoute(tableType, destination, nexthop)) {
1477*8542734aSAndroid Build Coastguard Worker         return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface, destination,
1478*8542734aSAndroid Build Coastguard Worker                            nexthop, tableType, mtu, priority, true /* isLocal */);
1479*8542734aSAndroid Build Coastguard Worker     }
1480*8542734aSAndroid Build Coastguard Worker 
1481*8542734aSAndroid Build Coastguard Worker     return 0;
1482*8542734aSAndroid Build Coastguard Worker }
1483*8542734aSAndroid Build Coastguard Worker 
removeRoute(const char * interface,const char * destination,const char * nexthop,TableType tableType,int priority)1484*8542734aSAndroid Build Coastguard Worker int RouteController::removeRoute(const char* interface, const char* destination,
1485*8542734aSAndroid Build Coastguard Worker                                  const char* nexthop, TableType tableType, int priority) {
1486*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, interface, destination, nexthop,
1487*8542734aSAndroid Build Coastguard Worker                               tableType, 0 /* mtu */, priority, false /* isLocal */)) {
1488*8542734aSAndroid Build Coastguard Worker         return ret;
1489*8542734aSAndroid Build Coastguard Worker     }
1490*8542734aSAndroid Build Coastguard Worker 
1491*8542734aSAndroid Build Coastguard Worker     if (isLocalRoute(tableType, destination, nexthop)) {
1492*8542734aSAndroid Build Coastguard Worker         return modifyRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, interface, destination, nexthop,
1493*8542734aSAndroid Build Coastguard Worker                            tableType, 0 /* mtu */, priority, true /* isLocal */);
1494*8542734aSAndroid Build Coastguard Worker     }
1495*8542734aSAndroid Build Coastguard Worker     return 0;
1496*8542734aSAndroid Build Coastguard Worker }
1497*8542734aSAndroid Build Coastguard Worker 
updateRoute(const char * interface,const char * destination,const char * nexthop,TableType tableType,int mtu)1498*8542734aSAndroid Build Coastguard Worker int RouteController::updateRoute(const char* interface, const char* destination,
1499*8542734aSAndroid Build Coastguard Worker                                  const char* nexthop, TableType tableType, int mtu) {
1500*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_REPLACE_FLAGS, interface, destination,
1501*8542734aSAndroid Build Coastguard Worker                               nexthop, tableType, mtu, 0 /* priority */, false /* isLocal */)) {
1502*8542734aSAndroid Build Coastguard Worker         return ret;
1503*8542734aSAndroid Build Coastguard Worker     }
1504*8542734aSAndroid Build Coastguard Worker 
1505*8542734aSAndroid Build Coastguard Worker     if (isLocalRoute(tableType, destination, nexthop)) {
1506*8542734aSAndroid Build Coastguard Worker         return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_REPLACE_FLAGS, interface, destination,
1507*8542734aSAndroid Build Coastguard Worker                            nexthop, tableType, mtu, 0 /* priority */, true /* isLocal */);
1508*8542734aSAndroid Build Coastguard Worker     }
1509*8542734aSAndroid Build Coastguard Worker     return 0;
1510*8542734aSAndroid Build Coastguard Worker }
1511*8542734aSAndroid Build Coastguard Worker 
enableTethering(const char * inputInterface,const char * outputInterface)1512*8542734aSAndroid Build Coastguard Worker int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
1513*8542734aSAndroid Build Coastguard Worker     return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
1514*8542734aSAndroid Build Coastguard Worker }
1515*8542734aSAndroid Build Coastguard Worker 
disableTethering(const char * inputInterface,const char * outputInterface)1516*8542734aSAndroid Build Coastguard Worker int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
1517*8542734aSAndroid Build Coastguard Worker     return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
1518*8542734aSAndroid Build Coastguard Worker }
1519*8542734aSAndroid Build Coastguard Worker 
addVirtualNetworkFallthrough(unsigned vpnNetId,const char * physicalInterface,Permission permission)1520*8542734aSAndroid Build Coastguard Worker int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1521*8542734aSAndroid Build Coastguard Worker                                                   Permission permission) {
1522*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission)) {
1523*8542734aSAndroid Build Coastguard Worker         return ret;
1524*8542734aSAndroid Build Coastguard Worker     }
1525*8542734aSAndroid Build Coastguard Worker 
1526*8542734aSAndroid Build Coastguard Worker     return modifyVpnLocalExclusionRule(true /* add */, physicalInterface);
1527*8542734aSAndroid Build Coastguard Worker }
1528*8542734aSAndroid Build Coastguard Worker 
removeVirtualNetworkFallthrough(unsigned vpnNetId,const char * physicalInterface,Permission permission)1529*8542734aSAndroid Build Coastguard Worker int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1530*8542734aSAndroid Build Coastguard Worker                                                      const char* physicalInterface,
1531*8542734aSAndroid Build Coastguard Worker                                                      Permission permission) {
1532*8542734aSAndroid Build Coastguard Worker     if (int ret = modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission)) {
1533*8542734aSAndroid Build Coastguard Worker         return ret;
1534*8542734aSAndroid Build Coastguard Worker     }
1535*8542734aSAndroid Build Coastguard Worker 
1536*8542734aSAndroid Build Coastguard Worker     return modifyVpnLocalExclusionRule(false /* add */, physicalInterface);
1537*8542734aSAndroid Build Coastguard Worker }
1538*8542734aSAndroid Build Coastguard Worker 
addUsersToPhysicalNetwork(unsigned netId,const char * interface,const UidRangeMap & uidRangeMap,bool local)1539*8542734aSAndroid Build Coastguard Worker int RouteController::addUsersToPhysicalNetwork(unsigned netId, const char* interface,
1540*8542734aSAndroid Build Coastguard Worker                                                const UidRangeMap& uidRangeMap, bool local) {
1541*8542734aSAndroid Build Coastguard Worker     return modifyPhysicalNetwork(netId, interface, uidRangeMap, PERMISSION_NONE, ACTION_ADD,
1542*8542734aSAndroid Build Coastguard Worker                                  !MODIFY_NON_UID_BASED_RULES, local);
1543*8542734aSAndroid Build Coastguard Worker }
1544*8542734aSAndroid Build Coastguard Worker 
removeUsersFromPhysicalNetwork(unsigned netId,const char * interface,const UidRangeMap & uidRangeMap,bool local)1545*8542734aSAndroid Build Coastguard Worker int RouteController::removeUsersFromPhysicalNetwork(unsigned netId, const char* interface,
1546*8542734aSAndroid Build Coastguard Worker                                                     const UidRangeMap& uidRangeMap, bool local) {
1547*8542734aSAndroid Build Coastguard Worker     return modifyPhysicalNetwork(netId, interface, uidRangeMap, PERMISSION_NONE, ACTION_DEL,
1548*8542734aSAndroid Build Coastguard Worker                                  !MODIFY_NON_UID_BASED_RULES, local);
1549*8542734aSAndroid Build Coastguard Worker }
1550*8542734aSAndroid Build Coastguard Worker 
addUsersToUnreachableNetwork(unsigned netId,const UidRangeMap & uidRangeMap)1551*8542734aSAndroid Build Coastguard Worker int RouteController::addUsersToUnreachableNetwork(unsigned netId, const UidRangeMap& uidRangeMap) {
1552*8542734aSAndroid Build Coastguard Worker     return modifyUnreachableNetwork(netId, uidRangeMap, ACTION_ADD);
1553*8542734aSAndroid Build Coastguard Worker }
1554*8542734aSAndroid Build Coastguard Worker 
removeUsersFromUnreachableNetwork(unsigned netId,const UidRangeMap & uidRangeMap)1555*8542734aSAndroid Build Coastguard Worker int RouteController::removeUsersFromUnreachableNetwork(unsigned netId,
1556*8542734aSAndroid Build Coastguard Worker                                                        const UidRangeMap& uidRangeMap) {
1557*8542734aSAndroid Build Coastguard Worker     return modifyUnreachableNetwork(netId, uidRangeMap, ACTION_DEL);
1558*8542734aSAndroid Build Coastguard Worker }
1559*8542734aSAndroid Build Coastguard Worker 
1560*8542734aSAndroid Build Coastguard Worker // Protects sInterfaceToTable.
1561*8542734aSAndroid Build Coastguard Worker std::mutex RouteController::sInterfaceToTableLock;
1562*8542734aSAndroid Build Coastguard Worker std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1563*8542734aSAndroid Build Coastguard Worker 
1564*8542734aSAndroid Build Coastguard Worker }  // namespace android::net
1565