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