1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "RouteController.h"
18 
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <linux/fib_rules.h>
23 #include <net/if.h>
24 #include <sys/stat.h>
25 
26 #include <private/android_filesystem_config.h>
27 
28 #include <map>
29 
30 #define LOG_TAG "Netd"
31 
32 #include "DummyNetwork.h"
33 #include "Fwmark.h"
34 #include "NetdConstants.h"
35 #include "NetlinkCommands.h"
36 #include "OffloadUtils.h"
37 #include "UidRanges.h"
38 
39 #include <android-base/file.h>
40 #include <android-base/stringprintf.h>
41 #include <android-base/strings.h>
42 #include "log/log.h"
43 #include "netid_client.h"
44 #include "netutils/ifc.h"
45 
46 using android::base::StartsWith;
47 using android::base::StringPrintf;
48 using android::base::WriteStringToFile;
49 using android::net::UidRangeParcel;
50 
51 namespace android::net {
52 
53 auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
54 
55 // BEGIN CONSTANTS --------------------------------------------------------------------------------
56 
57 const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
58 const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF    = 10500;
59 const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
60 const uint32_t RULE_PRIORITY_SECURE_VPN          = 12000;
61 const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN    = 12500;
62 const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK    = 13000;
63 const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE    = 14000;
64 const uint32_t RULE_PRIORITY_LEGACY_SYSTEM       = 15000;
65 const uint32_t RULE_PRIORITY_LEGACY_NETWORK      = 16000;
66 const uint32_t RULE_PRIORITY_LOCAL_NETWORK       = 17000;
67 const uint32_t RULE_PRIORITY_TETHERING           = 18000;
68 const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK    = 19000;
69 const uint32_t RULE_PRIORITY_BYPASSABLE_VPN      = 20000;
70 const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH     = 21000;
71 const uint32_t RULE_PRIORITY_DEFAULT_NETWORK     = 22000;
72 const uint32_t RULE_PRIORITY_UNREACHABLE         = 32000;
73 
74 const uint32_t ROUTE_TABLE_LOCAL_NETWORK  = 97;
75 const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
76 const uint32_t ROUTE_TABLE_LEGACY_SYSTEM  = 99;
77 
78 const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK  = "local_network";
79 const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
80 const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM  = "legacy_system";
81 
82 const char* const ROUTE_TABLE_NAME_LOCAL = "local";
83 const char* const ROUTE_TABLE_NAME_MAIN  = "main";
84 
85 // None of our regular routes specify priority, which causes them to have the default priority.
86 // For default throw routes, we use a fixed priority of 100000.
87 uint32_t PRIO_THROW = 100000;
88 
89 const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
90 
91 const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
92 
93 const uid_t UID_ROOT = 0;
94 const uint32_t FWMARK_NONE = 0;
95 const uint32_t MASK_NONE = 0;
96 const char* const IIF_LOOPBACK = "lo";
97 const char* const IIF_NONE = nullptr;
98 const char* const OIF_NONE = nullptr;
99 const bool ACTION_ADD = true;
100 const bool ACTION_DEL = false;
101 const bool MODIFY_NON_UID_BASED_RULES = true;
102 
103 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
104 const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;  // mode 0644, rw-r--r--
105 
106 // Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
107 // warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
U16_RTA_LENGTH(uint16_t x)108 static constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
109     return RTA_LENGTH(x);
110 }
111 
112 // These are practically const, but can't be declared so, because they are used to initialize
113 // non-const pointers ("void* iov_base") in iovec arrays.
114 rtattr FRATTR_PRIORITY  = { U16_RTA_LENGTH(sizeof(uint32_t)),           FRA_PRIORITY };
115 rtattr FRATTR_TABLE     = { U16_RTA_LENGTH(sizeof(uint32_t)),           FRA_TABLE };
116 rtattr FRATTR_FWMARK    = { U16_RTA_LENGTH(sizeof(uint32_t)),           FRA_FWMARK };
117 rtattr FRATTR_FWMASK    = { U16_RTA_LENGTH(sizeof(uint32_t)),           FRA_FWMASK };
118 rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
119 
120 rtattr RTATTR_TABLE     = { U16_RTA_LENGTH(sizeof(uint32_t)),           RTA_TABLE };
121 rtattr RTATTR_OIF       = { U16_RTA_LENGTH(sizeof(uint32_t)),           RTA_OIF };
122 rtattr RTATTR_PRIO      = { U16_RTA_LENGTH(sizeof(uint32_t)),           RTA_PRIORITY };
123 
124 // One or more nested attributes in the RTA_METRICS attribute.
125 rtattr RTATTRX_MTU      = { U16_RTA_LENGTH(sizeof(uint32_t)),           RTAX_MTU};
126 constexpr size_t RTATTRX_MTU_SIZE = RTA_SPACE(sizeof(uint32_t));
127 
128 // The RTA_METRICS attribute itself.
129 constexpr size_t RTATTR_METRICS_SIZE = RTATTRX_MTU_SIZE;
130 rtattr RTATTR_METRICS   = { U16_RTA_LENGTH(RTATTR_METRICS_SIZE),         RTA_METRICS };
131 
132 uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
133 
134 // END CONSTANTS ----------------------------------------------------------------------------------
135 
actionName(uint16_t action)136 static const char* actionName(uint16_t action) {
137     static const char *ops[4] = {"adding", "deleting", "getting", "???"};
138     return ops[action % 4];
139 }
140 
familyName(uint8_t family)141 static const char* familyName(uint8_t family) {
142     switch (family) {
143         case AF_INET: return "IPv4";
144         case AF_INET6: return "IPv6";
145         default: return "???";
146     }
147 }
148 
149 // Caller must hold sInterfaceToTableLock.
getRouteTableForInterfaceLocked(const char * interface)150 uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
151     // If we already know the routing table for this interface name, use it.
152     // This ensures we can remove rules and routes for an interface that has been removed,
153     // or has been removed and re-added with a different interface index.
154     //
155     // The caller is responsible for ensuring that an interface is never added to a network
156     // until it has been removed from any network it was previously in. This ensures that
157     // if the same interface disconnects and then reconnects with a different interface ID
158     // when the reconnect happens the interface will not be in the map, and the code will
159     // determine the new routing table from the interface ID, below.
160     auto iter = sInterfaceToTable.find(interface);
161     if (iter != sInterfaceToTable.end()) {
162         return iter->second;
163     }
164 
165     uint32_t index = if_nametoindex(interface);
166     if (index == 0) {
167         ALOGE("cannot find interface %s: %s", interface, strerror(errno));
168         return RT_TABLE_UNSPEC;
169     }
170     index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
171     sInterfaceToTable[interface] = index;
172     return index;
173 }
174 
getIfIndex(const char * interface)175 uint32_t RouteController::getIfIndex(const char* interface) {
176     std::lock_guard lock(sInterfaceToTableLock);
177 
178     auto iter = sInterfaceToTable.find(interface);
179     if (iter == sInterfaceToTable.end()) {
180         ALOGE("getIfIndex: cannot find interface %s", interface);
181         return 0;
182     }
183 
184     // For interfaces that are not in the local network, the routing table is always the interface
185     // index plus ROUTE_TABLE_OFFSET_FROM_INDEX. But for interfaces in the local network, there's no
186     // way to know the interface index from this table. Return 0 here so callers of this method do
187     // not get confused.
188     // TODO: stop calling this method from any caller that only wants interfaces in client mode.
189     int ifindex = iter->second;
190     if (ifindex == ROUTE_TABLE_LOCAL_NETWORK) {
191         return 0;
192     }
193 
194     return ifindex - ROUTE_TABLE_OFFSET_FROM_INDEX;
195 }
196 
getRouteTableForInterface(const char * interface)197 uint32_t RouteController::getRouteTableForInterface(const char* interface) {
198     std::lock_guard lock(sInterfaceToTableLock);
199     return getRouteTableForInterfaceLocked(interface);
200 }
201 
addTableName(uint32_t table,const std::string & name,std::string * contents)202 void addTableName(uint32_t table, const std::string& name, std::string* contents) {
203     char tableString[UINT32_STRLEN];
204     snprintf(tableString, sizeof(tableString), "%u", table);
205     *contents += tableString;
206     *contents += " ";
207     *contents += name;
208     *contents += "\n";
209 }
210 
211 // Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
updateTableNamesFile()212 void RouteController::updateTableNamesFile() {
213     std::string contents;
214 
215     addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
216     addTableName(RT_TABLE_MAIN,  ROUTE_TABLE_NAME_MAIN,  &contents);
217 
218     addTableName(ROUTE_TABLE_LOCAL_NETWORK,  ROUTE_TABLE_NAME_LOCAL_NETWORK,  &contents);
219     addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
220     addTableName(ROUTE_TABLE_LEGACY_SYSTEM,  ROUTE_TABLE_NAME_LEGACY_SYSTEM,  &contents);
221 
222     std::lock_guard lock(sInterfaceToTableLock);
223     for (const auto& entry : sInterfaceToTable) {
224         addTableName(entry.second, entry.first, &contents);
225     }
226 
227     if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
228         ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
229         return;
230     }
231 }
232 
233 // Returns 0 on success or negative errno on failure.
padInterfaceName(const char * input,char * name,size_t * length,uint16_t * padding)234 int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
235     if (!input) {
236         *length = 0;
237         *padding = 0;
238         return 0;
239     }
240     *length = strlcpy(name, input, IFNAMSIZ) + 1;
241     if (*length > IFNAMSIZ) {
242         ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
243         return -ENAMETOOLONG;
244     }
245     *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
246     return 0;
247 }
248 
249 // Adds or removes a routing rule for IPv4 and IPv6.
250 //
251 // + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
252 //   unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
253 // + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
254 //   ignored.
255 // + If |iif| is non-NULL, the rule matches the specified incoming interface.
256 // + If |oif| is non-NULL, the rule matches the specified outgoing interface.
257 // + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
258 //   range (inclusive). Otherwise, the rule matches packets from all UIDs.
259 //
260 // Returns 0 on success or negative errno on failure.
modifyIpRule(uint16_t action,uint32_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)261 [[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
262                                       uint32_t table, uint32_t fwmark, uint32_t mask,
263                                       const char* iif, const char* oif, uid_t uidStart,
264                                       uid_t uidEnd) {
265     // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
266     if (fwmark & ~mask) {
267         ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
268         return -ERANGE;
269     }
270 
271     // Interface names must include exactly one terminating NULL and be properly padded, or older
272     // kernels will refuse to delete rules.
273     char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
274     size_t iifLength, oifLength;
275     uint16_t iifPadding, oifPadding;
276     if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
277         return ret;
278     }
279     if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
280         return ret;
281     }
282 
283     // Either both start and end UID must be specified, or neither.
284     if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
285         ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
286         return -EUSERS;
287     }
288 
289     bool isUidRule = (uidStart != INVALID_UID);
290 
291     // Assemble a rule request and put it in an array of iovec structures.
292     fib_rule_hdr rule = {
293         .action = ruleType,
294         // Note that here we're implicitly setting rule.table to 0. When we want to specify a
295         // non-zero table, we do this via the FRATTR_TABLE attribute.
296     };
297 
298     // Don't ever create a rule that looks up table 0, because table 0 is the local table.
299     // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
300     // table 0, it's a wildcard that matches anything.
301     if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
302         ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
303         return -ENOTUNIQ;
304     }
305 
306     rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
307     rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
308     struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
309 
310     iovec iov[] = {
311         { nullptr,              0 },
312         { &rule,             sizeof(rule) },
313         { &FRATTR_PRIORITY,  sizeof(FRATTR_PRIORITY) },
314         { &priority,         sizeof(priority) },
315         { &FRATTR_TABLE,     table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
316         { &table,            table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
317         { &FRATTR_FWMARK,    mask ? sizeof(FRATTR_FWMARK) : 0 },
318         { &fwmark,           mask ? sizeof(fwmark) : 0 },
319         { &FRATTR_FWMASK,    mask ? sizeof(FRATTR_FWMASK) : 0 },
320         { &mask,             mask ? sizeof(mask) : 0 },
321         { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
322         { &uidRange,         isUidRule ? sizeof(uidRange) : 0 },
323         { &fraIifName,       iif != IIF_NONE ? sizeof(fraIifName) : 0 },
324         { iifName,           iifLength },
325         { PADDING_BUFFER,    iifPadding },
326         { &fraOifName,       oif != OIF_NONE ? sizeof(fraOifName) : 0 },
327         { oifName,           oifLength },
328         { PADDING_BUFFER,    oifPadding },
329     };
330 
331     uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
332     for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
333         rule.family = AF_FAMILIES[i];
334         if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
335             if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
336                 // Don't log when deleting a tethering rule that's not there. This matches the
337                 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
338                 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
339                       strerror(-ret));
340             }
341             return ret;
342         }
343     }
344 
345     return 0;
346 }
347 
modifyIpRule(uint16_t action,uint32_t priority,uint32_t table,uint32_t fwmark,uint32_t mask,const char * iif,const char * oif,uid_t uidStart,uid_t uidEnd)348 [[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
349                                       uint32_t fwmark, uint32_t mask, const char* iif,
350                                       const char* oif, uid_t uidStart, uid_t uidEnd) {
351     return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
352                         uidEnd);
353 }
354 
modifyIpRule(uint16_t action,uint32_t priority,uint32_t table,uint32_t fwmark,uint32_t mask)355 [[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
356                                       uint32_t fwmark, uint32_t mask) {
357     return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
358                         INVALID_UID);
359 }
360 
361 // Adds or deletes an IPv4 or IPv6 route.
362 // 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)363 int modifyIpRoute(uint16_t action, uint16_t flags, uint32_t table, const char* interface,
364                   const char* destination, const char* nexthop, uint32_t mtu) {
365     // At least the destination must be non-null.
366     if (!destination) {
367         ALOGE("null destination");
368         return -EFAULT;
369     }
370 
371     // Parse the prefix.
372     uint8_t rawAddress[sizeof(in6_addr)];
373     uint8_t family;
374     uint8_t prefixLength;
375     int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
376                                 &prefixLength);
377     if (rawLength < 0) {
378         ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
379         return rawLength;
380     }
381 
382     if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
383         ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
384         return -ENOBUFS;  // Cannot happen; parsePrefix only supports IPv4 and IPv6.
385     }
386 
387     uint8_t type = RTN_UNICAST;
388     uint32_t ifindex;
389     uint8_t rawNexthop[sizeof(in6_addr)];
390 
391     if (nexthop && !strcmp(nexthop, "unreachable")) {
392         type = RTN_UNREACHABLE;
393         // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
394         // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
395         // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
396         interface = OIF_NONE;
397         nexthop = nullptr;
398     } else if (nexthop && !strcmp(nexthop, "throw")) {
399         type = RTN_THROW;
400         interface = OIF_NONE;
401         nexthop = nullptr;
402     } else {
403         // If an interface was specified, find the ifindex.
404         if (interface != OIF_NONE) {
405             ifindex = if_nametoindex(interface);
406             if (!ifindex) {
407                 ALOGE("cannot find interface %s", interface);
408                 return -ENODEV;
409             }
410         }
411 
412         // If a nexthop was specified, parse it as the same family as the prefix.
413         if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
414             ALOGE("inet_pton failed for nexthop %s", nexthop);
415             return -EINVAL;
416         }
417     }
418 
419     bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
420 
421     // Assemble a rtmsg and put it in an array of iovec structures.
422     rtmsg route = {
423             .rtm_family = family,
424             .rtm_dst_len = prefixLength,
425             .rtm_protocol = RTPROT_STATIC,
426             .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
427             .rtm_type = type,
428     };
429 
430     rtattr rtaDst     = { U16_RTA_LENGTH(rawLength), RTA_DST };
431     rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
432 
433     iovec iov[] = {
434         { nullptr,         0 },
435         { &route,          sizeof(route) },
436         { &RTATTR_TABLE,   sizeof(RTATTR_TABLE) },
437         { &table,          sizeof(table) },
438         { &rtaDst,         sizeof(rtaDst) },
439         { rawAddress,      static_cast<size_t>(rawLength) },
440         { &RTATTR_OIF,     interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
441         { &ifindex,        interface != OIF_NONE ? sizeof(ifindex) : 0 },
442         { &rtaGateway,     nexthop ? sizeof(rtaGateway) : 0 },
443         { rawNexthop,      nexthop ? static_cast<size_t>(rawLength) : 0 },
444         { &RTATTR_METRICS, mtu != 0 ? sizeof(RTATTR_METRICS) : 0 },
445         { &RTATTRX_MTU,    mtu != 0 ? sizeof(RTATTRX_MTU) : 0 },
446         { &mtu,            mtu != 0 ? sizeof(mtu) : 0 },
447         { &RTATTR_PRIO,    isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
448         { &PRIO_THROW,     isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
449     };
450 
451     // Allow creating multiple link-local routes in the same table, so we can make IPv6
452     // work on all interfaces in the local_network table.
453     if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
454         flags &= ~NLM_F_EXCL;
455     }
456 
457     int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
458     if (ret) {
459         ALOGE("Error %s route %s -> %s %s to table %u: %s",
460               actionName(action), destination, nexthop, interface, table, strerror(-ret));
461     }
462     return ret;
463 }
464 
465 // An iptables rule to mark incoming packets on a network with the netId of the network.
466 //
467 // This is so that the kernel can:
468 // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
469 //   replies, SYN-ACKs, etc).
470 // + Mark sockets that accept connections from this interface so that the connection stays on the
471 //   same interface.
modifyIncomingPacketMark(unsigned netId,const char * interface,Permission permission,bool add)472 int modifyIncomingPacketMark(unsigned netId, const char* interface, Permission permission,
473                              bool add) {
474     Fwmark fwmark;
475 
476     fwmark.netId = netId;
477     fwmark.explicitlySelected = true;
478     fwmark.protectedFromVpn = true;
479     fwmark.permission = permission;
480 
481     const uint32_t mask = ~Fwmark::getUidBillingMask();
482 
483     std::string cmd = StringPrintf(
484         "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
485         RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
486     if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
487         ALOGE("failed to change iptables rule that sets incoming packet mark");
488         return -EREMOTEIO;
489     }
490 
491     return 0;
492 }
493 
494 // A rule to route responses to the local network forwarded via the VPN.
495 //
496 // When a VPN is in effect, packets from the local network to upstream networks are forwarded into
497 // the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
modifyVpnOutputToLocalRule(const char * vpnInterface,bool add)498 [[nodiscard]] static int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
499     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
500                         ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
501                         INVALID_UID, INVALID_UID);
502 }
503 
504 // A rule to route all traffic from a given set of UIDs to go over the VPN.
505 //
506 // Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
507 // have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
508 // bypass the VPN if the protectedFromVpn bit is set.
modifyVpnUidRangeRule(uint32_t table,uid_t uidStart,uid_t uidEnd,bool secure,bool add)509 [[nodiscard]] static int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
510                                                bool secure, bool add) {
511     Fwmark fwmark;
512     Fwmark mask;
513 
514     fwmark.protectedFromVpn = false;
515     mask.protectedFromVpn = true;
516 
517     uint32_t priority;
518 
519     if (secure) {
520         priority = RULE_PRIORITY_SECURE_VPN;
521     } else {
522         priority = RULE_PRIORITY_BYPASSABLE_VPN;
523 
524         fwmark.explicitlySelected = false;
525         mask.explicitlySelected = true;
526     }
527 
528     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
529                         mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
530 }
531 
532 // A rule to allow system apps to send traffic over this VPN even if they are not part of the target
533 // set of UIDs.
534 //
535 // This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
536 // target set, but where the DnsProxyListener itself is not.
modifyVpnSystemPermissionRule(unsigned netId,uint32_t table,bool secure,bool add)537 [[nodiscard]] static int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
538                                                        bool add) {
539     Fwmark fwmark;
540     Fwmark mask;
541 
542     fwmark.netId = netId;
543     mask.netId = FWMARK_NET_ID_MASK;
544 
545     fwmark.permission = PERMISSION_SYSTEM;
546     mask.permission = PERMISSION_SYSTEM;
547 
548     uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
549 
550     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
551                         mask.intValue);
552 }
553 
554 // A rule to route traffic based on an explicitly chosen network.
555 //
556 // Supports apps that use the multinetwork APIs to restrict their traffic to a network.
557 //
558 // Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
559 // to check it again in the rules here, because a network's permissions may have been updated via
560 // modifyNetworkPermission().
modifyExplicitNetworkRule(unsigned netId,uint32_t table,Permission permission,uid_t uidStart,uid_t uidEnd,bool add)561 [[nodiscard]] static int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
562                                                    Permission permission, uid_t uidStart,
563                                                    uid_t uidEnd, bool add) {
564     Fwmark fwmark;
565     Fwmark mask;
566 
567     fwmark.netId = netId;
568     mask.netId = FWMARK_NET_ID_MASK;
569 
570     fwmark.explicitlySelected = true;
571     mask.explicitlySelected = true;
572 
573     fwmark.permission = permission;
574     mask.permission = permission;
575 
576     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
577                         fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
578 }
579 
580 // A rule to route traffic based on a chosen outgoing interface.
581 //
582 // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
583 // the outgoing interface (typically for link-local communications).
modifyOutputInterfaceRules(const char * interface,uint32_t table,Permission permission,uid_t uidStart,uid_t uidEnd,bool add)584 [[nodiscard]] static int modifyOutputInterfaceRules(const char* interface, uint32_t table,
585                                                     Permission permission, uid_t uidStart,
586                                                     uid_t uidEnd, bool add) {
587     Fwmark fwmark;
588     Fwmark mask;
589 
590     fwmark.permission = permission;
591     mask.permission = permission;
592 
593     // If this rule does not specify a UID range, then also add a corresponding high-priority rule
594     // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
595     // sockets as root.
596     if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
597         if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
598                                    table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
599                                    UID_ROOT, UID_ROOT)) {
600             return ret;
601         }
602     }
603 
604     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
605                         fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
606 }
607 
608 // A rule to route traffic based on the chosen network.
609 //
610 // This is for sockets that have not explicitly requested a particular network, but have been
611 // bound to one when they called connect(). This ensures that sockets connected on a particular
612 // network stay on that network even if the default network changes.
modifyImplicitNetworkRule(unsigned netId,uint32_t table,bool add)613 [[nodiscard]] static int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
614     Fwmark fwmark;
615     Fwmark mask;
616 
617     fwmark.netId = netId;
618     mask.netId = FWMARK_NET_ID_MASK;
619 
620     fwmark.explicitlySelected = false;
621     mask.explicitlySelected = true;
622 
623     fwmark.permission = PERMISSION_NONE;
624     mask.permission = PERMISSION_NONE;
625 
626     return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
627                         fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
628                         INVALID_UID);
629 }
630 
631 // A rule to enable split tunnel VPNs.
632 //
633 // If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
634 // 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)635 int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
636                                               const char* physicalInterface,
637                                               Permission permission) {
638     uint32_t table = getRouteTableForInterface(physicalInterface);
639     if (table == RT_TABLE_UNSPEC) {
640         return -ESRCH;
641     }
642 
643     Fwmark fwmark;
644     Fwmark mask;
645 
646     fwmark.netId = vpnNetId;
647     mask.netId = FWMARK_NET_ID_MASK;
648 
649     fwmark.permission = permission;
650     mask.permission = permission;
651 
652     return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
653                         mask.intValue);
654 }
655 
656 // Add rules to allow legacy routes added through the requestRouteToHost() API.
addLegacyRouteRules()657 [[nodiscard]] static int addLegacyRouteRules() {
658     Fwmark fwmark;
659     Fwmark mask;
660 
661     fwmark.explicitlySelected = false;
662     mask.explicitlySelected = true;
663 
664     // Rules to allow legacy routes to override the default network.
665     if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
666                                fwmark.intValue, mask.intValue)) {
667         return ret;
668     }
669     if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
670                                ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
671         return ret;
672     }
673 
674     fwmark.permission = PERMISSION_SYSTEM;
675     mask.permission = PERMISSION_SYSTEM;
676 
677     // A rule to allow legacy routes from system apps to override VPNs.
678     return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
679                         fwmark.intValue, mask.intValue);
680 }
681 
682 // Add rules to lookup the local network when specified explicitly or otherwise.
addLocalNetworkRules(unsigned localNetId)683 [[nodiscard]] static int addLocalNetworkRules(unsigned localNetId) {
684     if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
685                                             INVALID_UID, INVALID_UID, ACTION_ADD)) {
686         return ret;
687     }
688 
689     Fwmark fwmark;
690     Fwmark mask;
691 
692     fwmark.explicitlySelected = false;
693     mask.explicitlySelected = true;
694 
695     return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
696                         fwmark.intValue, mask.intValue);
697 }
698 
699 /* static */
configureDummyNetwork()700 int RouteController::configureDummyNetwork() {
701     const char *interface = DummyNetwork::INTERFACE_NAME;
702     uint32_t table = getRouteTableForInterface(interface);
703     if (table == RT_TABLE_UNSPEC) {
704         // getRouteTableForInterface has already logged an error.
705         return -ESRCH;
706     }
707 
708     ifc_init();
709     int ret = ifc_up(interface);
710     ifc_close();
711     if (ret) {
712         ALOGE("Can't bring up %s: %s", interface, strerror(errno));
713         return -errno;
714     }
715 
716     if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
717                                           INVALID_UID, INVALID_UID, ACTION_ADD))) {
718         ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
719         return ret;
720     }
721 
722     if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface,
723                              "0.0.0.0/0", nullptr, 0 /* mtu */))) {
724         return ret;
725     }
726 
727     if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface, "::/0",
728                              nullptr, 0 /* mtu */))) {
729         return ret;
730     }
731 
732     return 0;
733 }
734 
735 // Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
736 // relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
737 // behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
738 // rule will hopefully make things even clearer.
addUnreachableRule()739 [[nodiscard]] static int addUnreachableRule() {
740     return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
741                         MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
742 }
743 
modifyLocalNetwork(unsigned netId,const char * interface,bool add)744 [[nodiscard]] static int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
745     if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
746         return ret;
747     }
748     return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
749                                       INVALID_UID, INVALID_UID, add);
750 }
751 
752 /* static */
modifyPhysicalNetwork(unsigned netId,const char * interface,Permission permission,bool add)753 int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
754                                            Permission permission, bool add) {
755     uint32_t table = getRouteTableForInterface(interface);
756     if (table == RT_TABLE_UNSPEC) {
757         return -ESRCH;
758     }
759 
760     if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
761         return ret;
762     }
763     if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
764                                             add)) {
765         return ret;
766     }
767     if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
768                                             add)) {
769         return ret;
770     }
771 
772     // Only set implicit rules for networks that don't require permissions.
773     //
774     // This is so that if the default network ceases to be the default network and then switches
775     // from requiring no permissions to requiring permissions, we ensure that apps only use the
776     // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
777     // - it closes all sockets on the network except sockets that are explicitly selected.
778     //
779     // The lack of this rule only affects the special case above, because:
780     // - The only cases where we implicitly bind a socket to a network are the default network and
781     //   the bypassable VPN that applies to the app, if any.
782     // - This rule doesn't affect VPNs because they don't support permissions at all.
783     // - The default network doesn't require permissions. While we support doing this, the framework
784     //   never does it (partly because we'd end up in the situation where we tell apps that there is
785     //   a default network, but they can't use it).
786     // - If the network is still the default network, the presence or absence of this rule does not
787     //   matter.
788     //
789     // Therefore, for the lack of this rule to affect a socket, the socket has to have been
790     // implicitly bound to a network because at the time of connect() it was the default, and that
791     // network must no longer be the default, and must now require permissions.
792     if (permission == PERMISSION_NONE) {
793         return modifyImplicitNetworkRule(netId, table, add);
794     }
795     return 0;
796 }
797 
modifyRejectNonSecureNetworkRule(const UidRanges & uidRanges,bool add)798 [[nodiscard]] static int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
799     Fwmark fwmark;
800     Fwmark mask;
801     fwmark.protectedFromVpn = false;
802     mask.protectedFromVpn = true;
803 
804     for (const UidRangeParcel& range : uidRanges.getRanges()) {
805         if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
806                                    FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
807                                    IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
808             return ret;
809         }
810     }
811 
812     return 0;
813 }
814 
modifyVirtualNetwork(unsigned netId,const char * interface,const UidRanges & uidRanges,bool secure,bool add,bool modifyNonUidBasedRules)815 int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
816                                           const UidRanges& uidRanges, bool secure, bool add,
817                                           bool modifyNonUidBasedRules) {
818     uint32_t table = getRouteTableForInterface(interface);
819     if (table == RT_TABLE_UNSPEC) {
820         return -ESRCH;
821     }
822 
823     for (const UidRangeParcel& range : uidRanges.getRanges()) {
824         if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) {
825             return ret;
826         }
827         if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
828                                                 range.stop, add)) {
829             return ret;
830         }
831         if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
832                                                  range.stop, add)) {
833             return ret;
834         }
835     }
836 
837     if (modifyNonUidBasedRules) {
838         if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
839             return ret;
840         }
841         if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
842             return ret;
843         }
844         if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
845             return ret;
846         }
847         return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
848     }
849 
850     return 0;
851 }
852 
modifyDefaultNetwork(uint16_t action,const char * interface,Permission permission)853 int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
854                                           Permission permission) {
855     uint32_t table = getRouteTableForInterface(interface);
856     if (table == RT_TABLE_UNSPEC) {
857         return -ESRCH;
858     }
859 
860     Fwmark fwmark;
861     Fwmark mask;
862 
863     fwmark.netId = NETID_UNSET;
864     mask.netId = FWMARK_NET_ID_MASK;
865 
866     fwmark.permission = permission;
867     mask.permission = permission;
868 
869     return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
870                         mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
871 }
872 
modifyTetheredNetwork(uint16_t action,const char * inputInterface,const char * outputInterface)873 int RouteController::modifyTetheredNetwork(uint16_t action, const char* inputInterface,
874                                            const char* outputInterface) {
875     uint32_t table = getRouteTableForInterface(outputInterface);
876     if (table == RT_TABLE_UNSPEC) {
877         return -ESRCH;
878     }
879 
880     return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
881                         inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
882 }
883 
884 // Adds or removes an IPv4 or IPv6 route to the specified table.
885 // 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)886 int RouteController::modifyRoute(uint16_t action, uint16_t flags, const char* interface,
887                                  const char* destination, const char* nexthop, TableType tableType,
888                                  int mtu) {
889     uint32_t table;
890     switch (tableType) {
891         case RouteController::INTERFACE: {
892             table = getRouteTableForInterface(interface);
893             if (table == RT_TABLE_UNSPEC) {
894                 return -ESRCH;
895             }
896             break;
897         }
898         case RouteController::LOCAL_NETWORK: {
899             table = ROUTE_TABLE_LOCAL_NETWORK;
900             break;
901         }
902         case RouteController::LEGACY_NETWORK: {
903             table = ROUTE_TABLE_LEGACY_NETWORK;
904             break;
905         }
906         case RouteController::LEGACY_SYSTEM: {
907             table = ROUTE_TABLE_LEGACY_SYSTEM;
908             break;
909         }
910     }
911 
912     int ret = modifyIpRoute(action, flags, table, interface, destination, nexthop, mtu);
913     // Trying to add a route that already exists shouldn't cause an error.
914     if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
915         return ret;
916     }
917 
918     return 0;
919 }
920 
maybeModifyQdiscClsact(const char * interface,bool add)921 void maybeModifyQdiscClsact(const char* interface, bool add) {
922     if (!bpf::isBpfSupported()) return;
923 
924     // The clsact attaching of v4- tun interface is triggered by ClatdController::maybeStartBpf
925     // because the clat is started before the v4- interface is added to the network and the
926     // clat startup needs to add {in, e}gress filters.
927     // TODO: remove this workaround once v4- tun interface clsact attaching is moved out from
928     // ClatdController::maybeStartBpf.
929     if (StartsWith(interface, "v4-") && add) return;
930 
931     // The interface may have already gone away in the delete case.
932     uint32_t ifindex = if_nametoindex(interface);
933     if (!ifindex) {
934         ALOGE("cannot find interface %s", interface);
935         return;
936     }
937 
938     if (add) {
939         if (int ret = tcQdiscAddDevClsact(ifindex)) {
940             ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
941             return;
942         }
943     } else {
944         if (int ret = tcQdiscDelDevClsact(ifindex)) {
945             ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
946             return;
947         }
948     }
949 
950     return;
951 }
952 
clearTetheringRules(const char * inputInterface)953 [[nodiscard]] static int clearTetheringRules(const char* inputInterface) {
954     int ret = 0;
955     while (ret == 0) {
956         ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
957                            inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
958     }
959 
960     if (ret == -ENOENT) {
961         return 0;
962     } else {
963         return ret;
964     }
965 }
966 
getRulePriority(const nlmsghdr * nlh)967 uint32_t getRulePriority(const nlmsghdr *nlh) {
968     return getRtmU32Attribute(nlh, FRA_PRIORITY);
969 }
970 
getRouteTable(const nlmsghdr * nlh)971 uint32_t getRouteTable(const nlmsghdr *nlh) {
972     return getRtmU32Attribute(nlh, RTA_TABLE);
973 }
974 
flushRules()975 [[nodiscard]] static int flushRules() {
976     NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
977         // Don't touch rules at priority 0 because by default they are used for local input.
978         return getRulePriority(nlh) != 0;
979     };
980     return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
981 }
982 
flushRoutes(uint32_t table)983 int RouteController::flushRoutes(uint32_t table) {
984     NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
985         return getRouteTable(nlh) == table;
986     };
987 
988     return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
989 }
990 
991 // Returns 0 on success or negative errno on failure.
flushRoutes(const char * interface)992 int RouteController::flushRoutes(const char* interface) {
993     std::lock_guard lock(sInterfaceToTableLock);
994 
995     uint32_t table = getRouteTableForInterfaceLocked(interface);
996     if (table == RT_TABLE_UNSPEC) {
997         return -ESRCH;
998     }
999 
1000     int ret = flushRoutes(table);
1001 
1002     // If we failed to flush routes, the caller may elect to keep this interface around, so keep
1003     // track of its name.
1004     if (ret == 0) {
1005         sInterfaceToTable.erase(interface);
1006     }
1007 
1008     return ret;
1009 }
1010 
Init(unsigned localNetId)1011 int RouteController::Init(unsigned localNetId) {
1012     if (int ret = flushRules()) {
1013         return ret;
1014     }
1015     if (int ret = addLegacyRouteRules()) {
1016         return ret;
1017     }
1018     if (int ret = addLocalNetworkRules(localNetId)) {
1019         return ret;
1020     }
1021     if (int ret = addUnreachableRule()) {
1022         return ret;
1023     }
1024     // Don't complain if we can't add the dummy network, since not all devices support it.
1025     configureDummyNetwork();
1026 
1027     updateTableNamesFile();
1028     return 0;
1029 }
1030 
addInterfaceToLocalNetwork(unsigned netId,const char * interface)1031 int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
1032     if (int ret = modifyLocalNetwork(netId, interface, ACTION_ADD)) {
1033         return ret;
1034     }
1035     std::lock_guard lock(sInterfaceToTableLock);
1036     sInterfaceToTable[interface] = ROUTE_TABLE_LOCAL_NETWORK;
1037     return 0;
1038 }
1039 
removeInterfaceFromLocalNetwork(unsigned netId,const char * interface)1040 int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
1041     if (int ret = modifyLocalNetwork(netId, interface, ACTION_DEL)) {
1042         return ret;
1043     }
1044     std::lock_guard lock(sInterfaceToTableLock);
1045     sInterfaceToTable.erase(interface);
1046     return 0;
1047 }
1048 
addInterfaceToPhysicalNetwork(unsigned netId,const char * interface,Permission permission)1049 int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
1050                                                    Permission permission) {
1051     if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
1052         return ret;
1053     }
1054     maybeModifyQdiscClsact(interface, ACTION_ADD);
1055     updateTableNamesFile();
1056     return 0;
1057 }
1058 
removeInterfaceFromPhysicalNetwork(unsigned netId,const char * interface,Permission permission)1059 int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1060                                                         Permission permission) {
1061     if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
1062         return ret;
1063     }
1064     if (int ret = flushRoutes(interface)) {
1065         return ret;
1066     }
1067     if (int ret = clearTetheringRules(interface)) {
1068         return ret;
1069     }
1070     maybeModifyQdiscClsact(interface, ACTION_DEL);
1071     updateTableNamesFile();
1072     return 0;
1073 }
1074 
addInterfaceToVirtualNetwork(unsigned netId,const char * interface,bool secure,const UidRanges & uidRanges)1075 int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
1076                                                   bool secure, const UidRanges& uidRanges) {
1077     if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
1078                                        MODIFY_NON_UID_BASED_RULES)) {
1079         return ret;
1080     }
1081     updateTableNamesFile();
1082     return 0;
1083 }
1084 
removeInterfaceFromVirtualNetwork(unsigned netId,const char * interface,bool secure,const UidRanges & uidRanges)1085 int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
1086                                                        bool secure, const UidRanges& uidRanges) {
1087     if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
1088                                        MODIFY_NON_UID_BASED_RULES)) {
1089         return ret;
1090     }
1091     if (int ret = flushRoutes(interface)) {
1092         return ret;
1093     }
1094     updateTableNamesFile();
1095     return 0;
1096 }
1097 
modifyPhysicalNetworkPermission(unsigned netId,const char * interface,Permission oldPermission,Permission newPermission)1098 int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1099                                                      Permission oldPermission,
1100                                                      Permission newPermission) {
1101     // Add the new rules before deleting the old ones, to avoid race conditions.
1102     if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
1103         return ret;
1104     }
1105     return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
1106 }
1107 
addUsersToRejectNonSecureNetworkRule(const UidRanges & uidRanges)1108 int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1109     return modifyRejectNonSecureNetworkRule(uidRanges, true);
1110 }
1111 
removeUsersFromRejectNonSecureNetworkRule(const UidRanges & uidRanges)1112 int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1113     return modifyRejectNonSecureNetworkRule(uidRanges, false);
1114 }
1115 
addUsersToVirtualNetwork(unsigned netId,const char * interface,bool secure,const UidRanges & uidRanges)1116 int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
1117                                               const UidRanges& uidRanges) {
1118     return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
1119                                 !MODIFY_NON_UID_BASED_RULES);
1120 }
1121 
removeUsersFromVirtualNetwork(unsigned netId,const char * interface,bool secure,const UidRanges & uidRanges)1122 int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
1123                                                    bool secure, const UidRanges& uidRanges) {
1124     return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
1125                                 !MODIFY_NON_UID_BASED_RULES);
1126 }
1127 
addInterfaceToDefaultNetwork(const char * interface,Permission permission)1128 int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1129     return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
1130 }
1131 
removeInterfaceFromDefaultNetwork(const char * interface,Permission permission)1132 int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1133                                                        Permission permission) {
1134     return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
1135 }
1136 
addRoute(const char * interface,const char * destination,const char * nexthop,TableType tableType,int mtu)1137 int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
1138                               TableType tableType, int mtu) {
1139     return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface, destination, nexthop,
1140                        tableType, mtu);
1141 }
1142 
removeRoute(const char * interface,const char * destination,const char * nexthop,TableType tableType)1143 int RouteController::removeRoute(const char* interface, const char* destination,
1144                                  const char* nexthop, TableType tableType) {
1145     return modifyRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, interface, destination, nexthop,
1146                        tableType, 0);
1147 }
1148 
updateRoute(const char * interface,const char * destination,const char * nexthop,TableType tableType,int mtu)1149 int RouteController::updateRoute(const char* interface, const char* destination,
1150                                  const char* nexthop, TableType tableType, int mtu) {
1151     return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_REPLACE_FLAGS, interface, destination, nexthop,
1152                        tableType, mtu);
1153 }
1154 
enableTethering(const char * inputInterface,const char * outputInterface)1155 int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
1156     return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
1157 }
1158 
disableTethering(const char * inputInterface,const char * outputInterface)1159 int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
1160     return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
1161 }
1162 
addVirtualNetworkFallthrough(unsigned vpnNetId,const char * physicalInterface,Permission permission)1163 int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1164                                                   Permission permission) {
1165     return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1166 }
1167 
removeVirtualNetworkFallthrough(unsigned vpnNetId,const char * physicalInterface,Permission permission)1168 int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1169                                                      const char* physicalInterface,
1170                                                      Permission permission) {
1171     return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1172 }
1173 
1174 // Protects sInterfaceToTable.
1175 std::mutex RouteController::sInterfaceToTableLock;
1176 std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1177 
1178 }  // namespace android::net
1179